gstfunnel: avoid access of freed pad
[platform/upstream/gstreamer.git] / gst / gstsample.c
1 /* GStreamer
2  * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * gstsample.c: media sample
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gstsample
24  * @short_description: A media sample
25  * @see_also: #GstBuffer, #GstCaps, #GstSegment
26  *
27  * A #GstSample is a small object containing data, a type, timing and
28  * extra arbitrary information.
29  *
30  * Last reviewed on 2012-03-29 (0.11.3)
31  */
32 #include "gst_private.h"
33
34 #include "gstsample.h"
35
36 struct _GstSample
37 {
38   GstMiniObject mini_object;
39
40   GstBuffer *buffer;
41   GstCaps *caps;
42   GstSegment segment;
43   GstStructure *info;
44 };
45
46 GType _gst_sample_type = 0;
47
48 GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);
49
50 void
51 _priv_gst_sample_initialize (void)
52 {
53   _gst_sample_type = gst_sample_get_type ();
54 }
55
56 static GstSample *
57 _gst_sample_copy (GstSample * sample)
58 {
59   GstSample *copy;
60
61   copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
62       gst_structure_copy (sample->info));
63
64   return copy;
65 }
66
67 static void
68 _gst_sample_free (GstSample * sample)
69 {
70   GST_LOG ("free %p", sample);
71
72   if (sample->buffer)
73     gst_buffer_unref (sample->buffer);
74   if (sample->caps)
75     gst_caps_unref (sample->caps);
76
77   g_slice_free1 (GST_MINI_OBJECT_SIZE (sample), sample);
78 }
79
80 /**
81  * gst_sample_new:
82  * @buffer: a #GstBuffer
83  * @caps: a #GstCaps
84  * @segment: a #GstSegment
85  * @info: a #GstStructure
86  *
87  * Create a new #GstSample with the provided details.
88  *
89  * Free-function: gst_sample_unref
90  *
91  * Returns: (transfer full): the new #GstSample. gst_sample_unref()
92  *     after usage.
93  *
94  * Since: 0.10.24
95  */
96 GstSample *
97 gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
98     GstStructure * info)
99 {
100   GstSample *sample;
101
102   sample = g_slice_new0 (GstSample);
103
104   GST_LOG ("new %p", sample);
105
106   gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), _gst_sample_type,
107       sizeof (GstSample));
108
109   sample->mini_object.copy = (GstMiniObjectCopyFunction) _gst_sample_copy;
110   sample->mini_object.free = (GstMiniObjectFreeFunction) _gst_sample_free;
111
112   sample->buffer = buffer ? gst_buffer_ref (buffer) : NULL;
113   sample->caps = caps ? gst_caps_ref (caps) : NULL;
114
115   if (segment)
116     gst_segment_copy_into (segment, &sample->segment);
117   else
118     gst_segment_init (&sample->segment, GST_FORMAT_TIME);
119
120   if (info) {
121     if (!gst_structure_set_parent_refcount (info,
122             &sample->mini_object.refcount))
123       goto had_parent;
124
125     sample->info = info;
126   }
127   return sample;
128
129   /* ERRORS */
130 had_parent:
131   {
132     gst_sample_unref (sample);
133     g_warning ("structure is already owned by another object");
134     return NULL;
135   }
136 }
137
138 /**
139  * gst_sample_get_buffer:
140  * @sample: a #GstSample
141  *
142  * Get the buffer associated with @sample
143  *
144  * Returns: (transfer none): the buffer of @sample or NULL when there
145  *  is no buffer. The buffer remains valid as long as @sample is valid.
146  */
147 GstBuffer *
148 gst_sample_get_buffer (GstSample * sample)
149 {
150   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
151
152   return sample->buffer;
153 }
154
155 /**
156  * gst_sample_get_caps:
157  * @sample: a #GstSample
158  *
159  * Get the caps associated with @sample
160  *
161  * Returns: (transfer none): the caps of @sample or NULL when there
162  *  is no caps. The caps remain valid as long as @sample is valid.
163  */
164 GstCaps *
165 gst_sample_get_caps (GstSample * sample)
166 {
167   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
168
169   return sample->caps;
170 }
171
172 /**
173  * gst_sample_get_segment:
174  * @sample: a #GstSample
175  *
176  * Get the segment associated with @sample
177  *
178  * Returns: (transfer none): the segment of @sample.
179  *  The segment remains valid as long as @sample is valid.
180  */
181 GstSegment *
182 gst_sample_get_segment (GstSample * sample)
183 {
184   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
185
186   return &sample->segment;
187 }
188
189 /**
190  * gst_sample_get_info:
191  * @sample: a #GstSample
192  *
193  * Get extra information associated with @sample.
194  *
195  * Returns: (transfer none): the extra info of @sample.
196  *  The info remains valid as long as @sample is valid.
197  */
198 const GstStructure *
199 gst_sample_get_info (GstSample * sample)
200 {
201   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
202
203   return sample->info;
204 }