2 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
4 * gstsample.c: media sample
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.
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.
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.
25 * @short_description: A media sample
26 * @see_also: #GstBuffer, #GstCaps, #GstSegment
28 * A #GstSample is a small object containing data, a type, timing and
29 * extra arbitrary information.
31 #include "gst_private.h"
33 #include "gstsample.h"
35 GST_DEBUG_CATEGORY_STATIC (gst_sample_debug);
36 #define GST_CAT_DEFAULT gst_sample_debug
40 GstMiniObject mini_object;
46 GstBufferList *buffer_list;
49 GType _gst_sample_type = 0;
51 GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);
54 _priv_gst_sample_initialize (void)
56 _gst_sample_type = gst_sample_get_type ();
58 GST_DEBUG_CATEGORY_INIT (gst_sample_debug, "sample", 0, "GstSample debug");
62 _gst_sample_copy (GstSample * sample)
66 copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
67 (sample->info) ? gst_structure_copy (sample->info) : NULL);
69 if (sample->buffer_list) {
70 copy->buffer_list = gst_buffer_list_ref (sample->buffer_list);
71 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (copy->buffer_list),
72 GST_MINI_OBJECT_CAST (copy));
79 _gst_sample_free (GstSample * sample)
81 GST_LOG ("free %p", sample);
84 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer),
85 GST_MINI_OBJECT_CAST (sample));
86 gst_buffer_unref (sample->buffer);
90 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->caps),
91 GST_MINI_OBJECT_CAST (sample));
92 gst_caps_unref (sample->caps);
96 gst_structure_set_parent_refcount (sample->info, NULL);
97 gst_structure_free (sample->info);
99 if (sample->buffer_list) {
100 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
101 GST_MINI_OBJECT_CAST (sample));
102 gst_buffer_list_unref (sample->buffer_list);
105 memset (sample, 0xff, sizeof (GstSample));
108 g_slice_free1 (sizeof (GstSample), sample);
113 * @buffer: (transfer none) (allow-none): a #GstBuffer, or %NULL
114 * @caps: (transfer none) (allow-none): a #GstCaps, or %NULL
115 * @segment: (transfer none) (allow-none): a #GstSegment, or %NULL
116 * @info: (transfer full) (allow-none): a #GstStructure, or %NULL
118 * Create a new #GstSample with the provided details.
120 * Free-function: gst_sample_unref
122 * Returns: (transfer full): the new #GstSample. gst_sample_unref()
126 gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
131 sample = g_slice_new0 (GstSample);
133 GST_LOG ("new %p", sample);
135 gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), 0, _gst_sample_type,
136 (GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
137 (GstMiniObjectFreeFunction) _gst_sample_free);
140 sample->buffer = gst_buffer_ref (buffer);
141 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
142 GST_MINI_OBJECT_CAST (sample));
146 sample->caps = gst_caps_ref (caps);
147 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
148 GST_MINI_OBJECT_CAST (sample));
151 /* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
153 gst_segment_copy_into (segment, &sample->segment);
155 gst_segment_init (&sample->segment, GST_FORMAT_TIME);
158 if (!gst_structure_set_parent_refcount (info,
159 &sample->mini_object.refcount))
169 gst_sample_unref (sample);
170 g_warning ("structure is already owned by another object");
176 * gst_sample_get_buffer:
177 * @sample: a #GstSample
179 * Get the buffer associated with @sample
181 * Returns: (transfer none) (nullable): the buffer of @sample or %NULL
182 * when there is no buffer. The buffer remains valid as long as
183 * @sample is valid. If you need to hold on to it for longer than
184 * that, take a ref to the buffer with gst_buffer_ref().
187 gst_sample_get_buffer (GstSample * sample)
189 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
191 return sample->buffer;
195 * gst_sample_get_caps:
196 * @sample: a #GstSample
198 * Get the caps associated with @sample
200 * Returns: (transfer none) (nullable): the caps of @sample or %NULL
201 * when there is no caps. The caps remain valid as long as @sample is
202 * valid. If you need to hold on to the caps for longer than that,
203 * take a ref to the caps with gst_caps_ref().
206 gst_sample_get_caps (GstSample * sample)
208 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
214 * gst_sample_get_segment:
215 * @sample: a #GstSample
217 * Get the segment associated with @sample
219 * Returns: (transfer none): the segment of @sample.
220 * The segment remains valid as long as @sample is valid.
223 gst_sample_get_segment (GstSample * sample)
225 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
227 return &sample->segment;
231 * gst_sample_get_info:
232 * @sample: a #GstSample
234 * Get extra information associated with @sample.
236 * Returns: (transfer none) (nullable): the extra info of @sample.
237 * The info remains valid as long as @sample is valid.
240 gst_sample_get_info (GstSample * sample)
242 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
248 * gst_sample_get_buffer_list:
249 * @sample: a #GstSample
251 * Get the buffer list associated with @sample
253 * Returns: (transfer none) (nullable): the buffer list of @sample or %NULL
254 * when there is no buffer list. The buffer list remains valid as long as
255 * @sample is valid. If you need to hold on to it for longer than
256 * that, take a ref to the buffer list with gst_mini_object_ref ().
261 gst_sample_get_buffer_list (GstSample * sample)
263 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
265 return sample->buffer_list;
269 * gst_sample_set_buffer_list:
270 * @sample: a #GstSample
271 * @buffer_list: a #GstBufferList
273 * Set the buffer list associated with @sample. @sample must be writable.
278 gst_sample_set_buffer_list (GstSample * sample, GstBufferList * buffer_list)
280 GstBufferList *old = NULL;
281 g_return_if_fail (GST_IS_SAMPLE (sample));
282 g_return_if_fail (gst_sample_is_writable (sample));
284 old = sample->buffer_list;
286 if (old == buffer_list)
290 sample->buffer_list = gst_buffer_list_ref (buffer_list);
291 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
292 GST_MINI_OBJECT_CAST (sample));
294 sample->buffer_list = NULL;
298 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
299 GST_MINI_OBJECT_CAST (sample));
300 gst_buffer_list_unref (old);
305 * gst_sample_set_buffer:
306 * @sample: A #GstSample
307 * @buffer: (transfer none): A #GstBuffer
309 * Set the buffer associated with @sample. @sample must be writable.
314 gst_sample_set_buffer (GstSample * sample, GstBuffer * buffer)
316 GstBuffer *old = NULL;
318 g_return_if_fail (GST_IS_SAMPLE (sample));
319 g_return_if_fail (gst_sample_is_writable (sample));
321 old = sample->buffer;
327 sample->buffer = gst_buffer_ref (buffer);
328 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
329 GST_MINI_OBJECT_CAST (sample));
331 sample->buffer = NULL;
335 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
336 GST_MINI_OBJECT_CAST (sample));
337 gst_buffer_unref (old);
342 * gst_sample_set_caps:
343 * @sample: A #GstSample
344 * @caps: (transfer none): A #GstCaps
346 * Set the caps associated with @sample. @sample must be writable.
351 gst_sample_set_caps (GstSample * sample, GstCaps * caps)
355 g_return_if_fail (GST_IS_SAMPLE (sample));
356 g_return_if_fail (gst_sample_is_writable (sample));
364 sample->caps = gst_caps_ref (caps);
365 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
366 GST_MINI_OBJECT_CAST (sample));
372 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
373 GST_MINI_OBJECT_CAST (sample));
374 gst_caps_unref (old);
379 * gst_sample_set_segment:
380 * @sample: A #GstSample
381 * @segment: (transfer none): A #GstSegment
383 * Set the segment associated with @sample. @sample must be writable.
388 gst_sample_set_segment (GstSample * sample, const GstSegment * segment)
390 g_return_if_fail (GST_IS_SAMPLE (sample));
391 g_return_if_fail (gst_sample_is_writable (sample));
393 /* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
395 gst_segment_copy_into (segment, &sample->segment);
397 gst_segment_init (&sample->segment, GST_FORMAT_TIME);
401 * gst_sample_set_info:
402 * @sample: A #GstSample
403 * @info: (transfer full): A #GstStructure
405 * Set the info structure associated with @sample. @sample must be writable,
406 * and @info must not have a parent set already.
411 gst_sample_set_info (GstSample * sample, GstStructure * info)
413 g_return_val_if_fail (GST_IS_SAMPLE (sample), FALSE);
414 g_return_val_if_fail (gst_sample_is_writable (sample), FALSE);
417 if (!gst_structure_set_parent_refcount (info,
418 &sample->mini_object.refcount))
423 gst_structure_set_parent_refcount (sample->info, NULL);
424 gst_structure_free (sample->info);
432 g_warning ("structure is already owned by another object");