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 = (GstBufferList *)
71 gst_mini_object_ref (GST_MINI_OBJECT_CAST (sample->buffer_list));
77 _gst_sample_free (GstSample * sample)
79 GST_LOG ("free %p", sample);
82 gst_buffer_unref (sample->buffer);
84 gst_caps_unref (sample->caps);
86 gst_structure_set_parent_refcount (sample->info, NULL);
87 gst_structure_free (sample->info);
89 if (sample->buffer_list)
90 gst_mini_object_unref (GST_MINI_OBJECT_CAST (sample->buffer_list));
92 g_slice_free1 (sizeof (GstSample), sample);
97 * @buffer: (transfer none) (allow-none): a #GstBuffer, or %NULL
98 * @caps: (transfer none) (allow-none): a #GstCaps, or %NULL
99 * @segment: (transfer none) (allow-none): a #GstSegment, or %NULL
100 * @info: (transfer full) (allow-none): a #GstStructure, or %NULL
102 * Create a new #GstSample with the provided details.
104 * Free-function: gst_sample_unref
106 * Returns: (transfer full): the new #GstSample. gst_sample_unref()
110 gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
115 sample = g_slice_new0 (GstSample);
117 GST_LOG ("new %p", sample);
119 gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), 0, _gst_sample_type,
120 (GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
121 (GstMiniObjectFreeFunction) _gst_sample_free);
123 sample->buffer = buffer ? gst_buffer_ref (buffer) : NULL;
124 sample->caps = caps ? gst_caps_ref (caps) : NULL;
127 gst_segment_copy_into (segment, &sample->segment);
129 gst_segment_init (&sample->segment, GST_FORMAT_TIME);
132 if (!gst_structure_set_parent_refcount (info,
133 &sample->mini_object.refcount))
143 gst_sample_unref (sample);
144 g_warning ("structure is already owned by another object");
150 * gst_sample_get_buffer:
151 * @sample: a #GstSample
153 * Get the buffer associated with @sample
155 * Returns: (transfer none) (nullable): the buffer of @sample or %NULL
156 * when there is no buffer. The buffer remains valid as long as
157 * @sample is valid. If you need to hold on to it for longer than
158 * that, take a ref to the buffer with gst_buffer_ref().
161 gst_sample_get_buffer (GstSample * sample)
163 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
165 return sample->buffer;
169 * gst_sample_get_caps:
170 * @sample: a #GstSample
172 * Get the caps associated with @sample
174 * Returns: (transfer none) (nullable): the caps of @sample or %NULL
175 * when there is no caps. The caps remain valid as long as @sample is
176 * valid. If you need to hold on to the caps for longer than that,
177 * take a ref to the caps with gst_caps_ref().
180 gst_sample_get_caps (GstSample * sample)
182 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
188 * gst_sample_get_segment:
189 * @sample: a #GstSample
191 * Get the segment associated with @sample
193 * Returns: (transfer none): the segment of @sample.
194 * The segment remains valid as long as @sample is valid.
197 gst_sample_get_segment (GstSample * sample)
199 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
201 return &sample->segment;
205 * gst_sample_get_info:
206 * @sample: a #GstSample
208 * Get extra information associated with @sample.
210 * Returns: (transfer none): the extra info of @sample.
211 * The info remains valid as long as @sample is valid.
214 gst_sample_get_info (GstSample * sample)
216 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
222 * gst_sample_get_buffer_list:
223 * @sample: a #GstSample
225 * Get the buffer list associated with @sample
227 * Returns: (transfer none) (nullable): the buffer list of @sample or %NULL
228 * when there is no buffer list. The buffer list remains valid as long as
229 * @sample is valid. If you need to hold on to it for longer than
230 * that, take a ref to the buffer list with gst_mini_object_ref ().
235 gst_sample_get_buffer_list (GstSample * sample)
237 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
239 return sample->buffer_list;
243 * gst_sample_set_buffer_list:
244 * @sample: a #GstSample
245 * @buffer_list: a #GstBufferList
247 * Set the buffer list associated with @sample
252 gst_sample_set_buffer_list (GstSample * sample, GstBufferList * buffer_list)
254 GstBufferList *old = NULL;
255 g_return_if_fail (GST_IS_SAMPLE (sample));
256 old = sample->buffer_list;
257 sample->buffer_list = (GstBufferList *)
258 gst_mini_object_ref (GST_MINI_OBJECT_CAST (buffer_list));
261 gst_mini_object_unref (GST_MINI_OBJECT_CAST (old));