e61ff6784cd2f2ae97591153ad83322dafdfad2d
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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 #include "gst_private.h"
31
32 #include "gstsample.h"
33
34 GST_DEBUG_CATEGORY_STATIC (gst_sample_debug);
35 #define GST_CAT_DEFAULT gst_sample_debug
36
37 struct _GstSample
38 {
39   GstMiniObject mini_object;
40
41   GstBuffer *buffer;
42   GstCaps *caps;
43   GstSegment segment;
44   GstStructure *info;
45   GstBufferList *buffer_list;
46 };
47
48 GType _gst_sample_type = 0;
49
50 GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);
51
52 void
53 _priv_gst_sample_initialize (void)
54 {
55   _gst_sample_type = gst_sample_get_type ();
56
57   GST_DEBUG_CATEGORY_INIT (gst_sample_debug, "sample", 0, "GstSample debug");
58 }
59
60 static GstSample *
61 _gst_sample_copy (GstSample * sample)
62 {
63   GstSample *copy;
64
65   copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
66       (sample->info) ? gst_structure_copy (sample->info) : NULL);
67
68   if (sample->buffer_list)
69     copy->buffer_list = (GstBufferList *)
70         gst_mini_object_ref (GST_MINI_OBJECT_CAST (sample->buffer_list));
71
72   return copy;
73 }
74
75 static void
76 _gst_sample_free (GstSample * sample)
77 {
78   GST_LOG ("free %p", sample);
79
80   if (sample->buffer)
81     gst_buffer_unref (sample->buffer);
82   if (sample->caps)
83     gst_caps_unref (sample->caps);
84   if (sample->info) {
85     gst_structure_set_parent_refcount (sample->info, NULL);
86     gst_structure_free (sample->info);
87   }
88   if (sample->buffer_list)
89     gst_mini_object_unref (GST_MINI_OBJECT_CAST (sample->buffer_list));
90
91   g_slice_free1 (sizeof (GstSample), sample);
92 }
93
94 /**
95  * gst_sample_new:
96  * @buffer: (transfer none) (allow-none): a #GstBuffer, or %NULL
97  * @caps: (transfer none) (allow-none): a #GstCaps, or %NULL
98  * @segment: (transfer none) (allow-none): a #GstSegment, or %NULL
99  * @info: (transfer full) (allow-none): a #GstStructure, or %NULL
100  *
101  * Create a new #GstSample with the provided details.
102  *
103  * Free-function: gst_sample_unref
104  *
105  * Returns: (transfer full): the new #GstSample. gst_sample_unref()
106  *     after usage.
107  */
108 GstSample *
109 gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
110     GstStructure * info)
111 {
112   GstSample *sample;
113
114   sample = g_slice_new0 (GstSample);
115
116   GST_LOG ("new %p", sample);
117
118   gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), 0, _gst_sample_type,
119       (GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
120       (GstMiniObjectFreeFunction) _gst_sample_free);
121
122   sample->buffer = buffer ? gst_buffer_ref (buffer) : NULL;
123   sample->caps = caps ? gst_caps_ref (caps) : NULL;
124
125   if (segment)
126     gst_segment_copy_into (segment, &sample->segment);
127   else
128     gst_segment_init (&sample->segment, GST_FORMAT_TIME);
129
130   if (info) {
131     if (!gst_structure_set_parent_refcount (info,
132             &sample->mini_object.refcount))
133       goto had_parent;
134
135     sample->info = info;
136   }
137   return sample;
138
139   /* ERRORS */
140 had_parent:
141   {
142     gst_sample_unref (sample);
143     g_warning ("structure is already owned by another object");
144     return NULL;
145   }
146 }
147
148 /**
149  * gst_sample_get_buffer:
150  * @sample: a #GstSample
151  *
152  * Get the buffer associated with @sample
153  *
154  * Returns: (transfer none) (nullable): the buffer of @sample or %NULL
155  *  when there is no buffer. The buffer remains valid as long as
156  *  @sample is valid.  If you need to hold on to it for longer than
157  *  that, take a ref to the buffer with gst_buffer_ref().
158  */
159 GstBuffer *
160 gst_sample_get_buffer (GstSample * sample)
161 {
162   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
163
164   return sample->buffer;
165 }
166
167 /**
168  * gst_sample_get_caps:
169  * @sample: a #GstSample
170  *
171  * Get the caps associated with @sample
172  *
173  * Returns: (transfer none) (nullable): the caps of @sample or %NULL
174  *  when there is no caps. The caps remain valid as long as @sample is
175  *  valid.  If you need to hold on to the caps for longer than that,
176  *  take a ref to the caps with gst_caps_ref().
177  */
178 GstCaps *
179 gst_sample_get_caps (GstSample * sample)
180 {
181   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
182
183   return sample->caps;
184 }
185
186 /**
187  * gst_sample_get_segment:
188  * @sample: a #GstSample
189  *
190  * Get the segment associated with @sample
191  *
192  * Returns: (transfer none): the segment of @sample.
193  *  The segment remains valid as long as @sample is valid.
194  */
195 GstSegment *
196 gst_sample_get_segment (GstSample * sample)
197 {
198   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
199
200   return &sample->segment;
201 }
202
203 /**
204  * gst_sample_get_info:
205  * @sample: a #GstSample
206  *
207  * Get extra information associated with @sample.
208  *
209  * Returns: (transfer none): the extra info of @sample.
210  *  The info remains valid as long as @sample is valid.
211  */
212 const GstStructure *
213 gst_sample_get_info (GstSample * sample)
214 {
215   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
216
217   return sample->info;
218 }
219
220 /**
221  * gst_sample_get_buffer_list:
222  * @sample: a #GstSample
223  *
224  * Get the buffer list associated with @sample
225  *
226  * Returns: (transfer none) (nullable): the buffer list of @sample or %NULL
227  *  when there is no buffer list. The buffer list remains valid as long as
228  *  @sample is valid.  If you need to hold on to it for longer than
229  *  that, take a ref to the buffer list with gst_mini_object_ref ().
230  *
231  * Since: 1.6
232  */
233 GstBufferList *
234 gst_sample_get_buffer_list (GstSample * sample)
235 {
236   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
237
238   return sample->buffer_list;
239 }
240
241 /**
242  * gst_sample_set_buffer_list:
243  * @sample: a #GstSample
244  * @buffer_list: a #GstBufferList
245  *
246  * Set the buffer list associated with @sample
247  *
248  * Since: 1.6
249  */
250 void
251 gst_sample_set_buffer_list (GstSample * sample, GstBufferList * buffer_list)
252 {
253   GstBufferList *old = NULL;
254   g_return_if_fail (GST_IS_SAMPLE (sample));
255   old = sample->buffer_list;
256   sample->buffer_list = (GstBufferList *)
257       gst_mini_object_ref (GST_MINI_OBJECT_CAST (buffer_list));
258
259   if (old)
260     gst_mini_object_unref (GST_MINI_OBJECT_CAST (old));
261 }