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.
24 * @short_description: A media sample
25 * @see_also: #GstBuffer, #GstCaps, #GstSegment
27 * A #GstSample is a small object containing data, a type, timing and
28 * extra arbitrary information.
30 #include "gst_private.h"
32 #include "gstsample.h"
34 GST_DEBUG_CATEGORY_STATIC (gst_sample_debug);
35 #define GST_CAT_DEFAULT gst_sample_debug
39 GstMiniObject mini_object;
45 GstBufferList *buffer_list;
48 GType _gst_sample_type = 0;
50 GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);
53 _priv_gst_sample_initialize (void)
55 _gst_sample_type = gst_sample_get_type ();
57 GST_DEBUG_CATEGORY_INIT (gst_sample_debug, "sample", 0, "GstSample debug");
61 _gst_sample_copy (GstSample * sample)
65 copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
66 (sample->info) ? gst_structure_copy (sample->info) : NULL);
68 if (sample->buffer_list)
69 copy->buffer_list = (GstBufferList *)
70 gst_mini_object_ref (GST_MINI_OBJECT_CAST (sample->buffer_list));
76 _gst_sample_free (GstSample * sample)
78 GST_LOG ("free %p", sample);
81 gst_buffer_unref (sample->buffer);
83 gst_caps_unref (sample->caps);
85 gst_structure_set_parent_refcount (sample->info, NULL);
86 gst_structure_free (sample->info);
88 if (sample->buffer_list)
89 gst_mini_object_unref (GST_MINI_OBJECT_CAST (sample->buffer_list));
91 g_slice_free1 (sizeof (GstSample), sample);
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
101 * Create a new #GstSample with the provided details.
103 * Free-function: gst_sample_unref
105 * Returns: (transfer full): the new #GstSample. gst_sample_unref()
109 gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
114 sample = g_slice_new0 (GstSample);
116 GST_LOG ("new %p", sample);
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);
122 sample->buffer = buffer ? gst_buffer_ref (buffer) : NULL;
123 sample->caps = caps ? gst_caps_ref (caps) : NULL;
126 gst_segment_copy_into (segment, &sample->segment);
128 gst_segment_init (&sample->segment, GST_FORMAT_TIME);
131 if (!gst_structure_set_parent_refcount (info,
132 &sample->mini_object.refcount))
142 gst_sample_unref (sample);
143 g_warning ("structure is already owned by another object");
149 * gst_sample_get_buffer:
150 * @sample: a #GstSample
152 * Get the buffer associated with @sample
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().
160 gst_sample_get_buffer (GstSample * sample)
162 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
164 return sample->buffer;
168 * gst_sample_get_caps:
169 * @sample: a #GstSample
171 * Get the caps associated with @sample
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().
179 gst_sample_get_caps (GstSample * sample)
181 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
187 * gst_sample_get_segment:
188 * @sample: a #GstSample
190 * Get the segment associated with @sample
192 * Returns: (transfer none): the segment of @sample.
193 * The segment remains valid as long as @sample is valid.
196 gst_sample_get_segment (GstSample * sample)
198 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
200 return &sample->segment;
204 * gst_sample_get_info:
205 * @sample: a #GstSample
207 * Get extra information associated with @sample.
209 * Returns: (transfer none): the extra info of @sample.
210 * The info remains valid as long as @sample is valid.
213 gst_sample_get_info (GstSample * sample)
215 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
221 * gst_sample_get_buffer_list:
222 * @sample: a #GstSample
224 * Get the buffer list associated with @sample
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 ().
234 gst_sample_get_buffer_list (GstSample * sample)
236 g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
238 return sample->buffer_list;
242 * gst_sample_set_buffer_list:
243 * @sample: a #GstSample
244 * @buffer_list: a #GstBufferList
246 * Set the buffer list associated with @sample
251 gst_sample_set_buffer_list (GstSample * sample, GstBufferList * buffer_list)
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));
260 gst_mini_object_unref (GST_MINI_OBJECT_CAST (old));