1 /* GStreamer
2 * Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
3 *
4 * gstfragment.c:
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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <glib.h>
26 #include <gst/base/gsttypefindhelper.h>
27 #include <gst/base/gstadapter.h>
28 #include "gstfragment.h"
29 #include "gsturidownloader_debug.h"
30
31 #define GST_CAT_DEFAULT uridownloader_debug
32
33 enum
34 {
35 PROP_0,
36 PROP_INDEX,
37 PROP_NAME,
38 PROP_DURATION,
39 PROP_DISCONTINOUS,
40 PROP_BUFFER,
41 PROP_CAPS,
42 PROP_LAST
43 };
44
45 struct _GstFragmentPrivate
46 {
47 GstBuffer *buffer;
48 GstCaps *caps;
49 GMutex lock;
50 };
51
52 G_DEFINE_TYPE_WITH_PRIVATE (GstFragment, gst_fragment, G_TYPE_OBJECT);
53
54 static void gst_fragment_dispose (GObject * object);
55 static void gst_fragment_finalize (GObject * object);
56
57 static void
gst_fragment_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)58 gst_fragment_set_property (GObject * object,
59 guint property_id, const GValue * value, GParamSpec * pspec)
60 {
61 GstFragment *fragment = GST_FRAGMENT (object);
62
63 switch (property_id) {
64 case PROP_CAPS:
65 gst_fragment_set_caps (fragment, g_value_get_boxed (value));
66 break;
67
68 default:
69 /* We don't have any other property... */
70 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
71 break;
72 }
73 }
74
75 static void
gst_fragment_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)76 gst_fragment_get_property (GObject * object,
77 guint property_id, GValue * value, GParamSpec * pspec)
78 {
79 GstFragment *fragment = GST_FRAGMENT (object);
80
81 switch (property_id) {
82 case PROP_INDEX:
83 g_value_set_uint (value, fragment->index);
84 break;
85
86 case PROP_NAME:
87 g_value_set_string (value, fragment->name);
88 break;
89
90 case PROP_DURATION:
91 g_value_set_uint64 (value, fragment->stop_time - fragment->start_time);
92 break;
93
94 case PROP_DISCONTINOUS:
95 g_value_set_boolean (value, fragment->discontinuous);
96 break;
97
98 case PROP_BUFFER:
99 g_value_take_boxed (value, gst_fragment_get_buffer (fragment));
100 break;
101
102 case PROP_CAPS:
103 g_value_take_boxed (value, gst_fragment_get_caps (fragment));
104 break;
105
106 default:
107 /* We don't have any other property... */
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
109 break;
110 }
111 }
112
113
114
115 static void
gst_fragment_class_init(GstFragmentClass * klass)116 gst_fragment_class_init (GstFragmentClass * klass)
117 {
118 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119
120 gobject_class->set_property = gst_fragment_set_property;
121 gobject_class->get_property = gst_fragment_get_property;
122 gobject_class->dispose = gst_fragment_dispose;
123 gobject_class->finalize = gst_fragment_finalize;
124
125 g_object_class_install_property (gobject_class, PROP_INDEX,
126 g_param_spec_uint ("index", "Index", "Index of the fragment", 0,
127 G_MAXUINT, 0, G_PARAM_READABLE));
128
129 g_object_class_install_property (gobject_class, PROP_NAME,
130 g_param_spec_string ("name", "Name",
131 "Name of the fragment (eg:fragment-12.ts)", NULL, G_PARAM_READABLE));
132
133 g_object_class_install_property (gobject_class, PROP_DISCONTINOUS,
134 g_param_spec_boolean ("discontinuous", "Discontinuous",
135 "Whether this fragment has a discontinuity or not",
136 FALSE, G_PARAM_READABLE));
137
138 g_object_class_install_property (gobject_class, PROP_DURATION,
139 g_param_spec_uint64 ("duration", "Fragment duration",
140 "Duration of the fragment", 0, G_MAXUINT64, 0, G_PARAM_READABLE));
141
142 g_object_class_install_property (gobject_class, PROP_BUFFER,
143 g_param_spec_boxed ("buffer", "Buffer",
144 "The fragment's buffer", GST_TYPE_BUFFER,
145 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
146
147 g_object_class_install_property (gobject_class, PROP_CAPS,
148 g_param_spec_boxed ("caps", "Fragment caps",
149 "The caps of the fragment's buffer. (NULL = detect)", GST_TYPE_CAPS,
150 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 }
152
153 static void
gst_fragment_init(GstFragment * fragment)154 gst_fragment_init (GstFragment * fragment)
155 {
156 GstFragmentPrivate *priv;
157
158 fragment->priv = priv = gst_fragment_get_instance_private (fragment);
159
160 g_mutex_init (&fragment->priv->lock);
161 priv->buffer = NULL;
162 fragment->download_start_time = gst_util_get_timestamp ();
163 fragment->start_time = 0;
164 fragment->stop_time = 0;
165 fragment->index = 0;
166 fragment->name = g_strdup ("");
167 fragment->completed = FALSE;
168 fragment->discontinuous = FALSE;
169 fragment->headers = NULL;
170 }
171
172 GstFragment *
gst_fragment_new(void)173 gst_fragment_new (void)
174 {
175 return GST_FRAGMENT (g_object_new (GST_TYPE_FRAGMENT, NULL));
176 }
177
178 static void
gst_fragment_finalize(GObject * gobject)179 gst_fragment_finalize (GObject * gobject)
180 {
181 GstFragment *fragment = GST_FRAGMENT (gobject);
182
183 g_free (fragment->uri);
184 g_free (fragment->redirect_uri);
185 g_free (fragment->name);
186 if (fragment->headers)
187 gst_structure_free (fragment->headers);
188 g_mutex_clear (&fragment->priv->lock);
189
190 G_OBJECT_CLASS (gst_fragment_parent_class)->finalize (gobject);
191 }
192
193 void
gst_fragment_dispose(GObject * object)194 gst_fragment_dispose (GObject * object)
195 {
196 GstFragmentPrivate *priv = GST_FRAGMENT (object)->priv;
197
198 if (priv->buffer != NULL) {
199 gst_buffer_unref (priv->buffer);
200 priv->buffer = NULL;
201 }
202
203 if (priv->caps != NULL) {
204 gst_caps_unref (priv->caps);
205 priv->caps = NULL;
206 }
207
208 G_OBJECT_CLASS (gst_fragment_parent_class)->dispose (object);
209 }
210
211 GstBuffer *
gst_fragment_get_buffer(GstFragment * fragment)212 gst_fragment_get_buffer (GstFragment * fragment)
213 {
214 g_return_val_if_fail (fragment != NULL, NULL);
215
216 if (!fragment->completed)
217 return NULL;
218 if (!fragment->priv->buffer)
219 return NULL;
220
221 gst_buffer_ref (fragment->priv->buffer);
222 return fragment->priv->buffer;
223 }
224
225 void
gst_fragment_set_caps(GstFragment * fragment,GstCaps * caps)226 gst_fragment_set_caps (GstFragment * fragment, GstCaps * caps)
227 {
228 g_return_if_fail (fragment != NULL);
229
230 g_mutex_lock (&fragment->priv->lock);
231 gst_caps_replace (&fragment->priv->caps, caps);
232 g_mutex_unlock (&fragment->priv->lock);
233 }
234
235 GstCaps *
gst_fragment_get_caps(GstFragment * fragment)236 gst_fragment_get_caps (GstFragment * fragment)
237 {
238 g_return_val_if_fail (fragment != NULL, NULL);
239
240 if (!fragment->completed)
241 return NULL;
242
243 g_mutex_lock (&fragment->priv->lock);
244 if (fragment->priv->caps == NULL) {
245 guint64 offset, offset_end;
246
247 /* FIXME: This is currently necessary as typefinding only
248 * works with 0 offsets... need to find a better way to
249 * do that */
250 offset = GST_BUFFER_OFFSET (fragment->priv->buffer);
251 offset_end = GST_BUFFER_OFFSET_END (fragment->priv->buffer);
252 GST_BUFFER_OFFSET (fragment->priv->buffer) = GST_BUFFER_OFFSET_NONE;
253 GST_BUFFER_OFFSET_END (fragment->priv->buffer) = GST_BUFFER_OFFSET_NONE;
254 fragment->priv->caps =
255 gst_type_find_helper_for_buffer (NULL, fragment->priv->buffer, NULL);
256 GST_BUFFER_OFFSET (fragment->priv->buffer) = offset;
257 GST_BUFFER_OFFSET_END (fragment->priv->buffer) = offset_end;
258 }
259 gst_caps_ref (fragment->priv->caps);
260 g_mutex_unlock (&fragment->priv->lock);
261
262 return fragment->priv->caps;
263 }
264
265 gboolean
gst_fragment_add_buffer(GstFragment * fragment,GstBuffer * buffer)266 gst_fragment_add_buffer (GstFragment * fragment, GstBuffer * buffer)
267 {
268 g_return_val_if_fail (fragment != NULL, FALSE);
269 g_return_val_if_fail (buffer != NULL, FALSE);
270
271 if (fragment->completed) {
272 GST_WARNING ("Fragment is completed, could not add more buffers");
273 return FALSE;
274 }
275
276 GST_DEBUG ("Adding new buffer to the fragment");
277 /* We steal the buffers you pass in */
278 if (fragment->priv->buffer == NULL)
279 fragment->priv->buffer = buffer;
280 else
281 fragment->priv->buffer = gst_buffer_append (fragment->priv->buffer, buffer);
282 return TRUE;
283 }
284