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