2 * Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
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.
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.
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.
26 #include <gst/base/gsttypefindhelper.h>
27 #include <gst/base/gstadapter.h>
28 #include "gstfragment.h"
29 #include "gsturidownloader_debug.h"
31 #define GST_CAT_DEFAULT uridownloader_debug
45 struct _GstFragmentPrivate
52 G_DEFINE_TYPE_WITH_PRIVATE (GstFragment, gst_fragment, G_TYPE_OBJECT);
54 static void gst_fragment_dispose (GObject * object);
55 static void gst_fragment_finalize (GObject * object);
58 gst_fragment_set_property (GObject * object,
59 guint property_id, const GValue * value, GParamSpec * pspec)
61 GstFragment *fragment = GST_FRAGMENT (object);
63 switch (property_id) {
65 gst_fragment_set_caps (fragment, g_value_get_boxed (value));
69 /* We don't have any other property... */
70 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76 gst_fragment_get_property (GObject * object,
77 guint property_id, GValue * value, GParamSpec * pspec)
79 GstFragment *fragment = GST_FRAGMENT (object);
81 switch (property_id) {
83 g_value_set_uint (value, fragment->index);
87 g_value_set_string (value, fragment->name);
91 g_value_set_uint64 (value, fragment->stop_time - fragment->start_time);
94 case PROP_DISCONTINOUS:
95 g_value_set_boolean (value, fragment->discontinuous);
99 g_value_take_boxed (value, gst_fragment_get_buffer (fragment));
103 g_value_take_boxed (value, gst_fragment_get_caps (fragment));
107 /* We don't have any other property... */
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
116 gst_fragment_class_init (GstFragmentClass * klass)
118 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
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;
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));
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));
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));
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));
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));
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));
154 gst_fragment_init (GstFragment * fragment)
156 GstFragmentPrivate *priv;
158 fragment->priv = priv = gst_fragment_get_instance_private (fragment);
160 g_mutex_init (&fragment->priv->lock);
162 fragment->download_start_time = gst_util_get_timestamp ();
163 fragment->start_time = 0;
164 fragment->stop_time = 0;
166 fragment->name = g_strdup ("");
167 fragment->completed = FALSE;
168 fragment->discontinuous = FALSE;
169 fragment->headers = NULL;
173 gst_fragment_new (void)
175 return GST_FRAGMENT (g_object_new (GST_TYPE_FRAGMENT, NULL));
179 gst_fragment_finalize (GObject * gobject)
181 GstFragment *fragment = GST_FRAGMENT (gobject);
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);
190 G_OBJECT_CLASS (gst_fragment_parent_class)->finalize (gobject);
194 gst_fragment_dispose (GObject * object)
196 GstFragmentPrivate *priv = GST_FRAGMENT (object)->priv;
198 if (priv->buffer != NULL) {
199 gst_buffer_unref (priv->buffer);
203 if (priv->caps != NULL) {
204 gst_caps_unref (priv->caps);
208 G_OBJECT_CLASS (gst_fragment_parent_class)->dispose (object);
212 gst_fragment_get_buffer (GstFragment * fragment)
214 g_return_val_if_fail (fragment != NULL, NULL);
216 if (!fragment->completed)
218 if (!fragment->priv->buffer)
221 gst_buffer_ref (fragment->priv->buffer);
222 return fragment->priv->buffer;
226 gst_fragment_set_caps (GstFragment * fragment, GstCaps * caps)
228 g_return_if_fail (fragment != NULL);
230 g_mutex_lock (&fragment->priv->lock);
231 gst_caps_replace (&fragment->priv->caps, caps);
232 g_mutex_unlock (&fragment->priv->lock);
236 gst_fragment_get_caps (GstFragment * fragment)
238 g_return_val_if_fail (fragment != NULL, NULL);
240 if (!fragment->completed)
243 g_mutex_lock (&fragment->priv->lock);
244 if (fragment->priv->caps == NULL) {
245 guint64 offset, offset_end;
247 /* FIXME: This is currently necessary as typefinding only
248 * works with 0 offsets... need to find a better way to
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;
259 gst_caps_ref (fragment->priv->caps);
260 g_mutex_unlock (&fragment->priv->lock);
262 return fragment->priv->caps;
266 gst_fragment_add_buffer (GstFragment * fragment, GstBuffer * buffer)
268 g_return_val_if_fail (fragment != NULL, FALSE);
269 g_return_val_if_fail (buffer != NULL, FALSE);
271 if (fragment->completed) {
272 GST_WARNING ("Fragment is completed, could not add more buffers");
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;
281 fragment->priv->buffer = gst_buffer_append (fragment->priv->buffer, buffer);