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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include <gst/base/gsttypefindhelper.h>
24 #include "glibcompat.h"
25 #include "gstfragmented.h"
26 #include "gstfragment.h"
28 #define GST_CAT_DEFAULT fragmented_debug
30 #define GST_FRAGMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_FRAGMENT, GstFragmentPrivate))
44 struct _GstFragmentPrivate
46 GstBufferList *buffer_list;
48 GstBufferListIterator *buffer_iterator;
53 G_DEFINE_TYPE (GstFragment, gst_fragment, G_TYPE_OBJECT);
55 static void gst_fragment_dispose (GObject * object);
56 static void gst_fragment_finalize (GObject * object);
59 gst_fragment_set_property (GObject * object,
60 guint property_id, const GValue * value, GParamSpec * pspec)
62 GstFragment *fragment = GST_FRAGMENT (object);
64 switch (property_id) {
66 gst_fragment_set_caps (fragment, g_value_get_boxed (value));
70 /* We don't have any other property... */
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
77 gst_fragment_get_property (GObject * object,
78 guint property_id, GValue * value, GParamSpec * pspec)
80 GstFragment *fragment = GST_FRAGMENT (object);
82 switch (property_id) {
84 g_value_set_uint (value, fragment->index);
88 g_value_set_string (value, fragment->name);
92 g_value_set_uint64 (value, fragment->stop_time - fragment->start_time);
95 case PROP_DISCONTINOUS:
96 g_value_set_boolean (value, fragment->discontinuous);
99 case PROP_BUFFER_LIST:
100 g_value_set_object (value, gst_fragment_get_buffer_list (fragment));
104 g_value_set_boxed (value, gst_fragment_get_caps (fragment));
108 /* We don't have any other property... */
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
117 gst_fragment_class_init (GstFragmentClass * klass)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
121 g_type_class_add_private (klass, sizeof (GstFragmentPrivate));
123 gobject_class->set_property = gst_fragment_set_property;
124 gobject_class->get_property = gst_fragment_get_property;
125 gobject_class->dispose = gst_fragment_dispose;
126 gobject_class->finalize = gst_fragment_finalize;
128 g_object_class_install_property (gobject_class, PROP_INDEX,
129 g_param_spec_uint ("index", "Index", "Index of the fragment", 0,
130 G_MAXUINT, 0, G_PARAM_READABLE));
132 g_object_class_install_property (gobject_class, PROP_NAME,
133 g_param_spec_string ("name", "Name",
134 "Name of the fragment (eg:fragment-12.ts)", NULL, G_PARAM_READABLE));
136 g_object_class_install_property (gobject_class, PROP_DISCONTINOUS,
137 g_param_spec_boolean ("discontinuous", "Discontinous",
138 "Whether this fragment has a discontinuity or not",
139 FALSE, G_PARAM_READABLE));
141 g_object_class_install_property (gobject_class, PROP_DURATION,
142 g_param_spec_uint64 ("duration", "Fragment duration",
143 "Duration of the fragment", 0, G_MAXUINT64, 0, G_PARAM_READABLE));
145 g_object_class_install_property (gobject_class, PROP_BUFFER_LIST,
146 g_param_spec_object ("buffer-list", "Buffer List",
147 "A list with the fragment's buffers", GST_TYPE_FRAGMENT,
150 g_object_class_install_property (gobject_class, PROP_CAPS,
151 g_param_spec_boxed ("caps", "Fragment caps",
152 "The caps of the fragment's buffer. (NULL = detect)", GST_TYPE_CAPS,
153 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 gst_fragment_init (GstFragment * fragment)
159 GstFragmentPrivate *priv;
161 fragment->priv = priv = GST_FRAGMENT_GET_PRIVATE (fragment);
163 G_MUTEX_INIT (fragment->priv->lock);
164 priv->buffer_list = gst_buffer_list_new ();
166 priv->buffer_iterator = gst_buffer_list_iterate (priv->buffer_list);
167 gst_buffer_list_iterator_add_group (priv->buffer_iterator);
168 fragment->download_start_time = g_get_real_time ();
169 fragment->start_time = 0;
170 fragment->stop_time = 0;
172 fragment->name = g_strdup ("");
173 fragment->completed = FALSE;
174 fragment->discontinuous = FALSE;
178 gst_fragment_new (void)
180 return GST_FRAGMENT (g_object_new (GST_TYPE_FRAGMENT, NULL));
184 gst_fragment_finalize (GObject * gobject)
186 GstFragment *fragment = GST_FRAGMENT (gobject);
188 g_free (fragment->name);
189 G_MUTEX_CLEAR (fragment->priv->lock);
191 G_OBJECT_CLASS (gst_fragment_parent_class)->finalize (gobject);
195 gst_fragment_dispose (GObject * object)
197 GstFragmentPrivate *priv = GST_FRAGMENT (object)->priv;
199 if (priv->buffer_list != NULL) {
200 gst_buffer_list_iterator_free (priv->buffer_iterator);
201 gst_buffer_list_unref (priv->buffer_list);
202 priv->buffer_list = NULL;
206 if (priv->caps != NULL) {
207 gst_caps_unref (priv->caps);
211 G_OBJECT_CLASS (gst_fragment_parent_class)->dispose (object);
215 gst_fragment_get_buffer_list (GstFragment * fragment)
217 g_return_val_if_fail (fragment != NULL, NULL);
219 if (!fragment->completed)
222 gst_buffer_list_ref (fragment->priv->buffer_list);
223 return fragment->priv->buffer_list;
227 gst_fragment_set_caps (GstFragment * fragment, GstCaps * caps)
229 g_return_if_fail (fragment != NULL);
231 G_MUTEX_LOCK (fragment->priv->lock);
232 gst_caps_replace (&fragment->priv->caps, caps);
233 G_MUTEX_UNLOCK (fragment->priv->lock);
237 gst_fragment_get_caps (GstFragment * fragment)
239 g_return_val_if_fail (fragment != NULL, NULL);
241 if (!fragment->completed)
244 G_MUTEX_LOCK (fragment->priv->lock);
245 if (fragment->priv->caps == NULL) {
246 GstBuffer *buf = gst_buffer_list_get (fragment->priv->buffer_list, 0, 0);
247 fragment->priv->caps = gst_type_find_helper_for_buffer (NULL, buf, NULL);
249 gst_caps_ref (fragment->priv->caps);
250 G_MUTEX_UNLOCK (fragment->priv->lock);
252 return fragment->priv->caps;
256 gst_fragment_get_buffer_size (GstFragment * fragment)
258 g_return_val_if_fail (fragment != NULL, 0);
260 if (!fragment->completed)
262 return fragment->priv->size;
268 gst_fragment_add_buffer (GstFragment * fragment, GstBuffer * buffer)
270 g_return_val_if_fail (fragment != NULL, FALSE);
271 g_return_val_if_fail (buffer != NULL, FALSE);
273 if (fragment->completed) {
274 GST_WARNING ("Fragment is completed, could not add more buffers");
278 gst_buffer_list_iterator_add (fragment->priv->buffer_iterator, buffer);
279 fragment->priv->size = fragment->priv->size + GST_BUFFER_SIZE (buffer);