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 #define GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS
32 #include "gst_private.h"
34 #include "gstsample.h"
36 GST_DEBUG_CATEGORY_STATIC (gst_sample_debug);
37 #define GST_CAT_DEFAULT gst_sample_debug
41 GstMiniObject mini_object;
47 GstBufferList *buffer_list;
50 GType _gst_sample_type = 0;
52 GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);
55 _priv_gst_sample_initialize (void)
57 _gst_sample_type = gst_sample_get_type ();
59 GST_DEBUG_CATEGORY_INIT (gst_sample_debug, "sample", 0, "GstSample debug");
63 _gst_sample_copy (GstSample * sample)
67 copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
68 (sample->info) ? gst_structure_copy (sample->info) : NULL);
70 if (sample->buffer_list) {
71 copy->buffer_list = gst_buffer_list_ref (sample->buffer_list);
72 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (copy->buffer_list),
73 GST_MINI_OBJECT_CAST (copy));
80 _gst_sample_free (GstSample * sample)
82 GST_LOG ("free %p", sample);
85 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer),
86 GST_MINI_OBJECT_CAST (sample));
87 gst_buffer_unref (sample->buffer);
91 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->caps),
92 GST_MINI_OBJECT_CAST (sample));
93 gst_caps_unref (sample->caps);
97 gst_structure_set_parent_refcount (sample->info, NULL);
98 gst_structure_free (sample->info);
100 if (sample->buffer_list) {
101 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
102 GST_MINI_OBJECT_CAST (sample));
103 gst_buffer_list_unref (sample->buffer_list);
106 memset (sample, 0xff, sizeof (GstSample));
109 g_slice_free1 (sizeof (GstSample), sample);
114 * @buffer: (transfer none) (allow-none): a #GstBuffer, or %NULL
115 * @caps: (transfer none) (allow-none): a #GstCaps, or %NULL
116 * @segment: (transfer none) (allow-none): a #GstSegment, or %NULL
117 * @info: (transfer full) (allow-none): a #GstStructure, or %NULL
119 * Create a new #GstSample with the provided details.
121 * Free-function: gst_sample_unref
123 * Returns: (transfer full): the new #GstSample. gst_sample_unref()
127 gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
132 sample = g_slice_new0 (GstSample);
134 GST_LOG ("new %p", sample);
136 gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), 0, _gst_sample_type,
137 (GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
138 (GstMiniObjectFreeFunction) _gst_sample_free);
141 sample->buffer = gst_buffer_ref (buffer);
142 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
143 GST_MINI_OBJECT_CAST (sample));
147 sample->caps = gst_caps_ref (caps);
148 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
149 GST_MINI_OBJECT_CAST (sample));
152 /* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
154 gst_segment_copy_into (segment, &sample->segment);
156 gst_segment_init (&sample->segment, GST_FORMAT_TIME);
159 if (!gst_structure_set_parent_refcount (info,
160 &sample->mini_object.refcount))
170 gst_sample_unref (sample);
171 g_warning ("structure is already owned by another object");
177 * gst_sample_get_buffer:
178 * @sample: a #GstSample
180 * Get the buffer associated with @sample
182 * Returns: (transfer none) (nullable): the buffer of @sample or %NULL
183 * when there is no buffer. The buffer remains valid as long as
184 * @sample is valid. If you need to hold on to it for longer than
185 * that, take a ref to the buffer with gst_buffer_ref().
188 gst_sample_get_buffer (GstSample * sample)
190 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
192 return sample->buffer;
196 * gst_sample_get_caps:
197 * @sample: a #GstSample
199 * Get the caps associated with @sample
201 * Returns: (transfer none) (nullable): the caps of @sample or %NULL
202 * when there is no caps. The caps remain valid as long as @sample is
203 * valid. If you need to hold on to the caps for longer than that,
204 * take a ref to the caps with gst_caps_ref().
207 gst_sample_get_caps (GstSample * sample)
209 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
215 * gst_sample_get_segment:
216 * @sample: a #GstSample
218 * Get the segment associated with @sample
220 * Returns: (transfer none): the segment of @sample.
221 * The segment remains valid as long as @sample is valid.
224 gst_sample_get_segment (GstSample * sample)
226 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
228 return &sample->segment;
232 * gst_sample_get_info:
233 * @sample: a #GstSample
235 * Get extra information associated with @sample.
237 * Returns: (transfer none) (nullable): the extra info of @sample.
238 * The info remains valid as long as @sample is valid.
241 gst_sample_get_info (GstSample * sample)
243 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
249 * gst_sample_get_buffer_list:
250 * @sample: a #GstSample
252 * Get the buffer list associated with @sample
254 * Returns: (transfer none) (nullable): the buffer list of @sample or %NULL
255 * when there is no buffer list. The buffer list remains valid as long as
256 * @sample is valid. If you need to hold on to it for longer than
257 * that, take a ref to the buffer list with gst_mini_object_ref ().
262 gst_sample_get_buffer_list (GstSample * sample)
264 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
266 return sample->buffer_list;
270 * gst_sample_set_buffer_list:
271 * @sample: a #GstSample
272 * @buffer_list: a #GstBufferList
274 * Set the buffer list associated with @sample. @sample must be writable.
279 gst_sample_set_buffer_list (GstSample * sample, GstBufferList * buffer_list)
281 GstBufferList *old = NULL;
282 g_return_if_fail (GST_IS_SAMPLE (sample));
283 g_return_if_fail (gst_sample_is_writable (sample));
285 old = sample->buffer_list;
287 if (old == buffer_list)
291 sample->buffer_list = gst_buffer_list_ref (buffer_list);
292 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
293 GST_MINI_OBJECT_CAST (sample));
295 sample->buffer_list = NULL;
299 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
300 GST_MINI_OBJECT_CAST (sample));
301 gst_buffer_list_unref (old);
306 * gst_sample_set_buffer:
307 * @sample: A #GstSample
308 * @buffer: (transfer none): A #GstBuffer
310 * Set the buffer associated with @sample. @sample must be writable.
315 gst_sample_set_buffer (GstSample * sample, GstBuffer * buffer)
317 GstBuffer *old = NULL;
319 g_return_if_fail (GST_IS_SAMPLE (sample));
320 g_return_if_fail (gst_sample_is_writable (sample));
322 old = sample->buffer;
328 sample->buffer = gst_buffer_ref (buffer);
329 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
330 GST_MINI_OBJECT_CAST (sample));
332 sample->buffer = NULL;
336 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
337 GST_MINI_OBJECT_CAST (sample));
338 gst_buffer_unref (old);
343 * gst_sample_set_caps:
344 * @sample: A #GstSample
345 * @caps: (transfer none): A #GstCaps
347 * Set the caps associated with @sample. @sample must be writable.
352 gst_sample_set_caps (GstSample * sample, GstCaps * caps)
356 g_return_if_fail (GST_IS_SAMPLE (sample));
357 g_return_if_fail (gst_sample_is_writable (sample));
365 sample->caps = gst_caps_ref (caps);
366 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
367 GST_MINI_OBJECT_CAST (sample));
373 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
374 GST_MINI_OBJECT_CAST (sample));
375 gst_caps_unref (old);
380 * gst_sample_set_segment:
381 * @sample: A #GstSample
382 * @segment: (transfer none): A #GstSegment
384 * Set the segment associated with @sample. @sample must be writable.
389 gst_sample_set_segment (GstSample * sample, const GstSegment * segment)
391 g_return_if_fail (GST_IS_SAMPLE (sample));
392 g_return_if_fail (gst_sample_is_writable (sample));
394 /* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
396 gst_segment_copy_into (segment, &sample->segment);
398 gst_segment_init (&sample->segment, GST_FORMAT_TIME);
402 * gst_sample_set_info:
403 * @sample: A #GstSample
404 * @info: (transfer full): A #GstStructure
406 * Set the info structure associated with @sample. @sample must be writable,
407 * and @info must not have a parent set already.
412 gst_sample_set_info (GstSample * sample, GstStructure * info)
414 g_return_val_if_fail (GST_IS_SAMPLE (sample), FALSE);
415 g_return_val_if_fail (gst_sample_is_writable (sample), FALSE);
418 if (!gst_structure_set_parent_refcount (info,
419 &sample->mini_object.refcount))
424 gst_structure_set_parent_refcount (sample->info, NULL);
425 gst_structure_free (sample->info);
433 g_warning ("structure is already owned by another object");
438 * gst_sample_ref: (skip)
439 * @sample: a #GstSample
441 * Increases the refcount of the given sample by one.
443 * Returns: (transfer full): @sample
446 gst_sample_ref (GstSample * sample)
448 return GST_SAMPLE_CAST (gst_mini_object_ref (GST_MINI_OBJECT_CAST (sample)));
452 * gst_sample_unref: (skip)
453 * @sample: (transfer full): a #GstSample
455 * Decreases the refcount of the sample. If the refcount reaches 0, the
456 * sample will be freed.
459 gst_sample_unref (GstSample * sample)
461 gst_mini_object_unref (GST_MINI_OBJECT_CAST (sample));
465 * gst_sample_copy: (skip)
466 * @buf: a #GstSample.
468 * Create a copy of the given sample. This will also make a newly allocated
469 * copy of the data the source sample contains.
471 * Returns: (transfer full): a new copy of @buf.
476 gst_sample_copy (const GstSample * buf)
479 GST_SAMPLE_CAST (gst_mini_object_copy (GST_MINI_OBJECT_CONST_CAST (buf)));