2 * Copyright (C) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
4 * gsttocsetter.c: interface for TOC setting on elements
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 * SECTION:gsttocsetter
24 * @short_description: Element interface that allows setting and retrieval
27 * Element interface that allows setting of the TOC.
29 * Elements that support some kind of chapters or editions (or tracks like in
30 * the FLAC cue sheet) will implement this interface.
32 * If you just want to retrieve the TOC in your application then all you
33 * need to do is watch for TOC messages on your pipeline's bus (or you can
34 * perform TOC query). This interface is only for setting TOC data, not for
35 * extracting it. To set TOC from the application, find proper tocsetter element
36 * and set TOC using gst_toc_setter_set_toc().
38 * Elements implementing the #GstTocSetter interface can extend existing TOC
39 * by getting extend UID for that (you can use gst_toc_find_entry() to retrieve it)
40 * with any TOC entries received from downstream.
48 #include "gst_private.h"
49 #include "gsttocsetter.h"
50 #include <gobject/gvaluecollector.h>
53 static GQuark gst_toc_key;
55 G_DEFINE_INTERFACE_WITH_CODE (GstTocSetter, gst_toc_setter, GST_TYPE_ELEMENT,
56 gst_toc_key = g_quark_from_static_string ("gst-toc-setter-data"););
59 gst_toc_setter_default_init (GstTocSetterInterface * klass)
61 /* nothing to do here, it's a dummy interface */
71 gst_toc_data_free (gpointer p)
73 GstTocData *data = (GstTocData *) p;
76 gst_toc_unref (data->toc);
78 g_mutex_clear (&data->lock);
80 g_slice_free (GstTocData, data);
84 gst_toc_setter_get_data (GstTocSetter * setter)
88 data = g_object_get_qdata (G_OBJECT (setter), gst_toc_key);
90 static GMutex create_mutex;
92 /* make sure no other thread is creating a GstTocData at the same time */
93 g_mutex_lock (&create_mutex);
94 data = g_object_get_qdata (G_OBJECT (setter), gst_toc_key);
96 data = g_slice_new (GstTocData);
97 g_mutex_init (&data->lock);
99 g_object_set_qdata_full (G_OBJECT (setter), gst_toc_key, data,
102 g_mutex_unlock (&create_mutex);
109 * gst_toc_setter_reset_toc:
110 * @setter: a #GstTocSetter.
112 * Reset the internal TOC. Elements should call this from within the
113 * state-change handler.
118 gst_toc_setter_reset_toc (GstTocSetter * setter)
122 g_return_if_fail (GST_IS_TOC_SETTER (setter));
124 data = gst_toc_setter_get_data (setter);
126 g_mutex_lock (&data->lock);
128 gst_toc_unref (data->toc);
131 g_mutex_unlock (&data->lock);
135 * gst_toc_setter_get_toc:
136 * @setter: a #GstTocSetter.
138 * Return current TOC the setter uses. The TOC should not be
141 * This function is not thread-safe. Use gst_toc_setter_get_toc_copy() instead.
143 * Returns: a current snapshot of the TOC used in the setter
144 * or NULL if none is used.
149 gst_toc_setter_get_toc (GstTocSetter * setter)
151 g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
153 return gst_toc_setter_get_data (setter)->toc;
157 * gst_toc_setter_get_toc_copy:
158 * @setter: a #GstTocSetter.
160 * Return current TOC the setter uses. The difference between this
161 * function and gst_toc_setter_get_toc() is that this function returns deep
162 * copy of the TOC, so you can modify it in any way. This function is thread-safe.
163 * Free it when done with gst_toc_unref().
165 * Returns: a copy of the current snapshot of the TOC used in the setter
166 * or NULL if none is used.
171 gst_toc_setter_get_toc_copy (GstTocSetter * setter)
176 g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
178 data = gst_toc_setter_get_data (setter);
179 g_mutex_lock (&data->lock);
181 if (data->toc != NULL)
182 ret = gst_toc_copy (data->toc);
184 g_mutex_unlock (&data->lock);
190 * gst_toc_setter_set_toc:
191 * @setter: a #GstTocSetter.
192 * @toc: a #GstToc to set.
194 * Set the given TOC on the setter. Previously setted TOC will be
195 * freed before setting a new one.
200 gst_toc_setter_set_toc (GstTocSetter * setter, const GstToc * toc)
204 g_return_if_fail (GST_IS_TOC_SETTER (setter));
206 data = gst_toc_setter_get_data (setter);
208 g_mutex_lock (&data->lock);
210 gst_toc_unref (data->toc);
212 data->toc = gst_toc_copy (toc);
214 g_mutex_unlock (&data->lock);
218 * gst_toc_setter_get_toc_entry:
219 * @setter: a #GstTocSetter.
220 * @uid: UID to find entry with.
222 * Return #GstTocEntry (if any) with given @uid. Returned entry should
223 * not be modified or freed.
225 * This function is not thread-safe. Use gst_toc_setter_get_toc_entry_copy() instead.
227 * Returns: a TOC entry with given @uid from the TOC in the setter
228 * or NULL if none entry with such @uid was found.
233 gst_toc_setter_get_toc_entry (GstTocSetter * setter, const gchar * uid)
236 const GstTocEntry *ret;
238 g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
239 g_return_val_if_fail (uid != NULL, NULL);
241 data = gst_toc_setter_get_data (setter);
243 g_mutex_lock (&data->lock);
245 ret = gst_toc_find_entry (data->toc, uid);
247 g_mutex_unlock (&data->lock);
253 * gst_toc_setter_get_toc_entry_copy:
254 * @setter: a #GstTocSetter.
255 * @uid: UID to find entry with.
257 * Return #GstTocEntry (if any) with given @uid. It perform a deep copying,
258 * so you can modify returned value. Free it when done with gst_toc_entry_free().
259 * This function is thread-safe.
261 * Returns: a TOC entry with given @uid from the TOC in the setter
262 * or NULL if none entry with such @uid was found.
267 gst_toc_setter_get_toc_entry_copy (GstTocSetter * setter, const gchar * uid)
270 GstTocEntry *ret = NULL;
271 const GstTocEntry *search;
273 g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
274 g_return_val_if_fail (uid != NULL, NULL);
276 data = gst_toc_setter_get_data (setter);
278 g_mutex_lock (&data->lock);
280 search = gst_toc_find_entry (data->toc, uid);
282 ret = gst_toc_entry_copy (search);
284 g_mutex_unlock (&data->lock);
290 * gst_toc_setter_add_toc_entry:
291 * @setter: a #GstTocSetter.
292 * @parent_uid: UID of the parent entry to append given @entry. Use 0 for the TOC root level.
293 * @entry: #GstTocEntry to append.
295 * Try to find entry with given @parent_uid and append an @entry to that #GstTocEntry.
297 * Returns: TRUE if entry with @parent_uid was found, FALSE otherwise.
302 gst_toc_setter_add_toc_entry (GstTocSetter * setter, const gchar * parent_uid,
303 const GstTocEntry * entry)
307 GstTocEntry *copy_entry;
308 gboolean ret = FALSE;
310 g_return_val_if_fail (GST_IS_TOC_SETTER (setter), FALSE);
311 g_return_val_if_fail (parent_uid != NULL, FALSE);
312 g_return_val_if_fail (entry != NULL, FALSE);
314 data = gst_toc_setter_get_data (setter);
316 g_mutex_lock (&data->lock);
318 copy_entry = gst_toc_entry_copy (entry);
320 if (g_strcmp0 (parent_uid, "0") == 0) {
321 if (data->toc == NULL)
322 data->toc = gst_toc_new ();
323 data->toc->entries = g_list_append (data->toc->entries, copy_entry);
325 parent = gst_toc_find_entry (data->toc, parent_uid);
327 if (parent != NULL) {
328 parent->subentries = g_list_append (parent->subentries, copy_entry);
333 g_mutex_unlock (&data->lock);