miniobjects: pass copy, dispose and free function to gst_mini_object_init()
[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       gst_structure_copy (sample->info));
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
77   g_slice_free1 (sizeof (GstSample), sample);
78 }
79
80 /**
81  * gst_sample_new:
82  * @buffer: (transfer none) (allow-none): a #GstBuffer, or NULL
83  * @caps: (transfer none) (allow-none): a #GstCaps, or NULL
84  * @segment: transfer none) (allow-none): a #GstSegment, or NULL
85  * @info: (transfer full) (allow-none): a #GstStructure, or NULL
86  *
87  * Create a new #GstSample with the provided details.
88  *
89  * Free-function: gst_sample_unref
90  *
91  * Returns: (transfer full): the new #GstSample. gst_sample_unref()
92  *     after usage.
93  *
94  * Since: 0.10.24
95  */
96 GstSample *
97 gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
98     GstStructure * info)
99 {
100   GstSample *sample;
101
102   sample = g_slice_new0 (GstSample);
103
104   GST_LOG ("new %p", sample);
105
106   gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), _gst_sample_type,
107       (GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
108       (GstMiniObjectFreeFunction) _gst_sample_free);
109
110   sample->buffer = buffer ? gst_buffer_ref (buffer) : NULL;
111   sample->caps = caps ? gst_caps_ref (caps) : NULL;
112
113   if (segment)
114     gst_segment_copy_into (segment, &sample->segment);
115   else
116     gst_segment_init (&sample->segment, GST_FORMAT_TIME);
117
118   if (info) {
119     if (!gst_structure_set_parent_refcount (info,
120             &sample->mini_object.refcount))
121       goto had_parent;
122
123     sample->info = info;
124   }
125   return sample;
126
127   /* ERRORS */
128 had_parent:
129   {
130     gst_sample_unref (sample);
131     g_warning ("structure is already owned by another object");
132     return NULL;
133   }
134 }
135
136 /**
137  * gst_sample_get_buffer:
138  * @sample: a #GstSample
139  *
140  * Get the buffer associated with @sample
141  *
142  * Returns: (transfer none): the buffer of @sample or NULL when there
143  *  is no buffer. The buffer remains valid as long as @sample is valid.
144  */
145 GstBuffer *
146 gst_sample_get_buffer (GstSample * sample)
147 {
148   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
149
150   return sample->buffer;
151 }
152
153 /**
154  * gst_sample_get_caps:
155  * @sample: a #GstSample
156  *
157  * Get the caps associated with @sample
158  *
159  * Returns: (transfer none): the caps of @sample or NULL when there
160  *  is no caps. The caps remain valid as long as @sample is valid.
161  */
162 GstCaps *
163 gst_sample_get_caps (GstSample * sample)
164 {
165   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
166
167   return sample->caps;
168 }
169
170 /**
171  * gst_sample_get_segment:
172  * @sample: a #GstSample
173  *
174  * Get the segment associated with @sample
175  *
176  * Returns: (transfer none): the segment of @sample.
177  *  The segment remains valid as long as @sample is valid.
178  */
179 GstSegment *
180 gst_sample_get_segment (GstSample * sample)
181 {
182   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
183
184   return &sample->segment;
185 }
186
187 /**
188  * gst_sample_get_info:
189  * @sample: a #GstSample
190  *
191  * Get extra information associated with @sample.
192  *
193  * Returns: (transfer none): the extra info of @sample.
194  *  The info remains valid as long as @sample is valid.
195  */
196 const GstStructure *
197 gst_sample_get_info (GstSample * sample)
198 {
199   g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);
200
201   return sample->info;
202 }