docs: convert NULL, TRUE, and FALSE to %NULL, %TRUE, and %FALSE
[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 };
46
47 GType _gst_sample_type = 0;
48
49 GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);
50
51 void
52 _priv_gst_sample_initialize (void)
53 {
54   _gst_sample_type = gst_sample_get_type ();
55
56   GST_DEBUG_CATEGORY_INIT (gst_sample_debug, "sample", 0, "GstSample debug");
57 }
58
59 static GstSample *
60 _gst_sample_copy (GstSample * sample)
61 {
62   GstSample *copy;
63
64   copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
65       (sample->info) ? gst_structure_copy (sample->info) : NULL);
66
67   return copy;
68 }
69
70 static void
71 _gst_sample_free (GstSample * sample)
72 {
73   GST_LOG ("free %p", sample);
74
75   if (sample->buffer)
76     gst_buffer_unref (sample->buffer);
77   if (sample->caps)
78     gst_caps_unref (sample->caps);
79   if (sample->info) {
80     gst_structure_set_parent_refcount (sample->info, NULL);
81     gst_structure_free (sample->info);
82   }
83   g_slice_free1 (sizeof (GstSample), sample);
84 }
85
86 /**
87  * gst_sample_new:
88  * @buffer: (transfer none) (allow-none): a #GstBuffer, or %NULL
89  * @caps: (transfer none) (allow-none): a #GstCaps, or %NULL
90  * @segment: (transfer none) (allow-none): a #GstSegment, or %NULL
91  * @info: (transfer full) (allow-none): a #GstStructure, or %NULL
92  *
93  * Create a new #GstSample with the provided details.
94  *
95  * Free-function: gst_sample_unref
96  *
97  * Returns: (transfer full): the new #GstSample. gst_sample_unref()
98  *     after usage.
99  */
100 GstSample *
101 gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
102     GstStructure * info)
103 {
104   GstSample *sample;
105
106   sample = g_slice_new0 (GstSample);
107
108   GST_LOG ("new %p", sample);
109
110   gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), 0, _gst_sample_type,
111       (GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
112       (GstMiniObjectFreeFunction) _gst_sample_free);
113
114   sample->buffer = buffer ? gst_buffer_ref (buffer) : NULL;
115   sample->caps = caps ? gst_caps_ref (caps) : NULL;
116
117   if (segment)
118     gst_segment_copy_into (segment, &sample->segment);
119   else
120     gst_segment_init (&sample->segment, GST_FORMAT_TIME);
121
122   if (info) {
123     if (!gst_structure_set_parent_refcount (info,
124             &sample->mini_object.refcount))
125       goto had_parent;
126
127     sample->info = info;
128   }
129   return sample;
130
131   /* ERRORS */
132 had_parent:
133   {
134     gst_sample_unref (sample);
135     g_warning ("structure is already owned by another object");
136     return NULL;
137   }
138 }
139
140 /**
141  * gst_sample_get_buffer:
142  * @sample: a #GstSample
143  *
144  * Get the buffer associated with @sample
145  *
146  * Returns: (transfer none): the buffer of @sample or %NULL when there
147  *  is no buffer. The buffer remains valid as long as @sample is valid.
148  *  If you need to hold on to it for longer than that, take a ref to
149  *  the buffer with gst_buffer_ref().
150  */
151 GstBuffer *
152 gst_sample_get_buffer (GstSample * sample)
153 {
154   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
155
156   return sample->buffer;
157 }
158
159 /**
160  * gst_sample_get_caps:
161  * @sample: a #GstSample
162  *
163  * Get the caps associated with @sample
164  *
165  * Returns: (transfer none): the caps of @sample or %NULL when there
166  *  is no caps. The caps remain valid as long as @sample is valid.
167  *  If you need to hold on to the caps for longer than that, take a ref to
168  *  the caps with gst_caps_ref().
169  */
170 GstCaps *
171 gst_sample_get_caps (GstSample * sample)
172 {
173   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
174
175   return sample->caps;
176 }
177
178 /**
179  * gst_sample_get_segment:
180  * @sample: a #GstSample
181  *
182  * Get the segment associated with @sample
183  *
184  * Returns: (transfer none): the segment of @sample.
185  *  The segment remains valid as long as @sample is valid.
186  */
187 GstSegment *
188 gst_sample_get_segment (GstSample * sample)
189 {
190   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
191
192   return &sample->segment;
193 }
194
195 /**
196  * gst_sample_get_info:
197  * @sample: a #GstSample
198  *
199  * Get extra information associated with @sample.
200  *
201  * Returns: (transfer none): the extra info of @sample.
202  *  The info remains valid as long as @sample is valid.
203  */
204 const GstStructure *
205 gst_sample_get_info (GstSample * sample)
206 {
207   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
208
209   return sample->info;
210 }