GstSample: fix typo in G-I annotations, allows creating GstSamples from bindings.
[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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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  * Last reviewed on 2012-03-29 (0.11.3)
31  */
32 #include "gst_private.h"
33
34 #include "gstsample.h"
35
36 struct _GstSample
37 {
38   GstMiniObject mini_object;
39
40   GstBuffer *buffer;
41   GstCaps *caps;
42   GstSegment segment;
43   GstStructure *info;
44 };
45
46 GType _gst_sample_type = 0;
47
48 GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);
49
50 void
51 _priv_gst_sample_initialize (void)
52 {
53   _gst_sample_type = gst_sample_get_type ();
54 }
55
56 static GstSample *
57 _gst_sample_copy (GstSample * sample)
58 {
59   GstSample *copy;
60
61   copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
62       (sample->info) ? gst_structure_copy (sample->info) : NULL);
63
64   return copy;
65 }
66
67 static void
68 _gst_sample_free (GstSample * sample)
69 {
70   GST_LOG ("free %p", sample);
71
72   if (sample->buffer)
73     gst_buffer_unref (sample->buffer);
74   if (sample->caps)
75     gst_caps_unref (sample->caps);
76   if (sample->info) {
77     gst_structure_set_parent_refcount (sample->info, NULL);
78     gst_structure_free (sample->info);
79   }
80   g_slice_free1 (sizeof (GstSample), sample);
81 }
82
83 /**
84  * gst_sample_new:
85  * @buffer: (transfer none) (allow-none): a #GstBuffer, or NULL
86  * @caps: (transfer none) (allow-none): a #GstCaps, or NULL
87  * @segment: (transfer none) (allow-none): a #GstSegment, or NULL
88  * @info: (transfer full) (allow-none): a #GstStructure, or NULL
89  *
90  * Create a new #GstSample with the provided details.
91  *
92  * Free-function: gst_sample_unref
93  *
94  * Returns: (transfer full): the new #GstSample. gst_sample_unref()
95  *     after usage.
96  */
97 GstSample *
98 gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
99     GstStructure * info)
100 {
101   GstSample *sample;
102
103   sample = g_slice_new0 (GstSample);
104
105   GST_LOG ("new %p", sample);
106
107   gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), 0, _gst_sample_type,
108       (GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
109       (GstMiniObjectFreeFunction) _gst_sample_free);
110
111   sample->buffer = buffer ? gst_buffer_ref (buffer) : NULL;
112   sample->caps = caps ? gst_caps_ref (caps) : NULL;
113
114   if (segment)
115     gst_segment_copy_into (segment, &sample->segment);
116   else
117     gst_segment_init (&sample->segment, GST_FORMAT_TIME);
118
119   if (info) {
120     if (!gst_structure_set_parent_refcount (info,
121             &sample->mini_object.refcount))
122       goto had_parent;
123
124     sample->info = info;
125   }
126   return sample;
127
128   /* ERRORS */
129 had_parent:
130   {
131     gst_sample_unref (sample);
132     g_warning ("structure is already owned by another object");
133     return NULL;
134   }
135 }
136
137 /**
138  * gst_sample_get_buffer:
139  * @sample: a #GstSample
140  *
141  * Get the buffer associated with @sample
142  *
143  * Returns: (transfer none): the buffer of @sample or NULL when there
144  *  is no buffer. The buffer remains valid as long as @sample is valid.
145  */
146 GstBuffer *
147 gst_sample_get_buffer (GstSample * sample)
148 {
149   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
150
151   return sample->buffer;
152 }
153
154 /**
155  * gst_sample_get_caps:
156  * @sample: a #GstSample
157  *
158  * Get the caps associated with @sample
159  *
160  * Returns: (transfer none): the caps of @sample or NULL when there
161  *  is no caps. The caps remain valid as long as @sample is valid.
162  */
163 GstCaps *
164 gst_sample_get_caps (GstSample * sample)
165 {
166   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
167
168   return sample->caps;
169 }
170
171 /**
172  * gst_sample_get_segment:
173  * @sample: a #GstSample
174  *
175  * Get the segment associated with @sample
176  *
177  * Returns: (transfer none): the segment of @sample.
178  *  The segment remains valid as long as @sample is valid.
179  */
180 GstSegment *
181 gst_sample_get_segment (GstSample * sample)
182 {
183   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
184
185   return &sample->segment;
186 }
187
188 /**
189  * gst_sample_get_info:
190  * @sample: a #GstSample
191  *
192  * Get extra information associated with @sample.
193  *
194  * Returns: (transfer none): the extra info of @sample.
195  *  The info remains valid as long as @sample is valid.
196  */
197 const GstStructure *
198 gst_sample_get_info (GstSample * sample)
199 {
200   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
201
202   return sample->info;
203 }