Merge branch '0.10'
[platform/upstream/gstreamer.git] / gst / gsttocsetter.c
1 /* GStreamer
2  * Copyright (C) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
3  *
4  * gsttocsetter.c: interface for TOC setting on elements
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:gsttocsetter
24  * @short_description: Element interface that allows setting and retrieval
25  *                     of the TOC
26  *
27  * Element interface that allows setting of the TOC.
28  *
29  * Elements that support some kind of chapters or editions (or tracks like in
30  * the FLAC cue sheet) will implement this interface.
31  * 
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().
37  * 
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.
41  */
42
43
44 #ifdef HAVE_CONFIG_H
45 #  include "config.h"
46 #endif
47
48 #include "gst_private.h"
49 #include "gsttocsetter.h"
50 #include <gobject/gvaluecollector.h>
51 #include <string.h>
52
53 GST_DEBUG_CATEGORY_STATIC (gst_toc_interface_debug);
54 #define GST_CAT_DEFAULT tag_toc_interface_debug
55
56 static GQuark gst_toc_key;
57
58 typedef struct
59 {
60   GstToc *toc;
61   GMutex lock;
62 } GstTocData;
63
64 GType
65 gst_toc_setter_get_type (void)
66 {
67   static volatile gsize toc_setter_type = 0;
68
69   if (g_once_init_enter (&toc_setter_type)) {
70     GType _type;
71     static const GTypeInfo toc_setter_info = {
72       sizeof (GstTocSetterIFace),       /* class_size */
73       NULL,                     /* base_init */
74       NULL,                     /* base_finalize */
75       NULL,
76       NULL,                     /* class_finalize */
77       NULL,                     /* class_data */
78       0,
79       0,
80       NULL
81     };
82
83     GST_DEBUG_CATEGORY_INIT (gst_toc_interface_debug, "GstTocInterface", 0,
84         "interfaces for the TOC");
85
86     _type = g_type_register_static (G_TYPE_INTERFACE, "GstTocSetter",
87         &toc_setter_info, 0);
88
89     g_type_interface_add_prerequisite (_type, GST_TYPE_ELEMENT);
90
91     gst_toc_key = g_quark_from_static_string ("GST_TOC_SETTER");
92     g_once_init_leave (&toc_setter_type, _type);
93   }
94
95   return toc_setter_type;
96 }
97
98 static void
99 gst_toc_data_free (gpointer p)
100 {
101   GstTocData *data = (GstTocData *) p;
102
103   if (data->toc)
104     gst_toc_free (data->toc);
105
106   g_slice_free (GstTocData, data);
107 }
108
109 static GstTocData *
110 gst_toc_setter_get_data (GstTocSetter * setter)
111 {
112   GstTocData *data;
113
114   data = g_object_get_qdata (G_OBJECT (setter), gst_toc_key);
115   if (!data) {
116     static GMutex create_mutex;
117
118     /* make sure no other thread is creating a GstTocData at the same time */
119     g_mutex_lock (&create_mutex);
120     data = g_object_get_qdata (G_OBJECT (setter), gst_toc_key);
121     if (!data) {
122       data = g_slice_new (GstTocData);
123       g_mutex_init (&data->lock);
124       data->toc = NULL;
125       g_object_set_qdata_full (G_OBJECT (setter), gst_toc_key, data,
126           gst_toc_data_free);
127     }
128     g_mutex_unlock (&create_mutex);
129   }
130
131   return data;
132 }
133
134 /**
135  * gst_toc_setter_reset_toc:
136  * @setter: a #GstTocSetter.
137  *
138  * Reset the internal TOC. Elements should call this from within the
139  * state-change handler.
140  *
141  * Since: 0.10.37
142  */
143 void
144 gst_toc_setter_reset_toc (GstTocSetter * setter)
145 {
146   GstTocData *data;
147
148   g_return_if_fail (GST_IS_TOC_SETTER (setter));
149
150   data = gst_toc_setter_get_data (setter);
151
152   g_mutex_lock (&data->lock);
153   if (data->toc) {
154     gst_toc_free (data->toc);
155     data->toc = NULL;
156   }
157   g_mutex_unlock (&data->lock);
158 }
159
160 /**
161  * gst_toc_setter_get_toc:
162  * @setter: a #GstTocSetter.
163  *
164  * Return current TOC the setter uses. The TOC should not be
165  * modified or freed.
166  *
167  * This function is not thread-safe. Use gst_toc_setter_get_toc_copy() instead.
168  *
169  * Returns: a current snapshot of the TOC used in the setter
170  *          or NULL if none is used.
171  *
172  * Since: 0.10.37
173  */
174 const GstToc *
175 gst_toc_setter_get_toc (GstTocSetter * setter)
176 {
177   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
178
179   return gst_toc_setter_get_data (setter)->toc;
180 }
181
182 /**
183  * gst_toc_setter_get_toc_copy:
184  * @setter: a #GstTocSetter.
185  *
186  * Return current TOC the setter uses. The difference between this
187  * function and gst_toc_setter_get_toc() is that this function returns deep
188  * copy of the TOC, so you can modify it in any way. This function is thread-safe.
189  * Free it when done with gst_toc_free().
190  *
191  * Returns: a copy of the current snapshot of the TOC used in the setter
192  *          or NULL if none is used.
193  *
194  * Since: 0.10.37
195  */
196 GstToc *
197 gst_toc_setter_get_toc_copy (GstTocSetter * setter)
198 {
199   GstTocData *data;
200   GstToc *ret = NULL;
201
202   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
203
204   data = gst_toc_setter_get_data (setter);
205   g_mutex_lock (&data->lock);
206
207   if (data->toc != NULL)
208     ret = gst_toc_copy (data->toc);
209
210   g_mutex_unlock (&data->lock);
211
212   return ret;
213 }
214
215 /**
216  * gst_toc_setter_set_toc:
217  * @setter: a #GstTocSetter.
218  * @toc: a #GstToc to set.
219  *
220  * Set the given TOC on the setter. Previously setted TOC will be
221  * freed before setting a new one.
222  *
223  * Since: 0.10.37
224  */
225 void
226 gst_toc_setter_set_toc (GstTocSetter * setter, const GstToc * toc)
227 {
228   GstTocData *data;
229
230   g_return_if_fail (GST_IS_TOC_SETTER (setter));
231
232   data = gst_toc_setter_get_data (setter);
233
234   g_mutex_lock (&data->lock);
235   if (data->toc)
236     gst_toc_free (data->toc);
237
238   data->toc = gst_toc_copy (toc);
239
240   g_mutex_unlock (&data->lock);
241 }
242
243 /**
244  * gst_toc_setter_get_toc_entry:
245  * @setter: a #GstTocSetter.
246  * @uid: UID to find entry with.
247  *
248  * Return #GstTocEntry (if any) with given @uid. Returned entry should
249  * not be modified or freed.
250  *
251  * This function is not thread-safe. Use gst_toc_setter_get_toc_entry_copy() instead.
252  *
253  * Returns: a TOC entry with given @uid from the TOC in the setter
254  *          or NULL if none entry with such @uid was found.
255  *
256  * Since: 0.10.37
257  */
258 const GstTocEntry *
259 gst_toc_setter_get_toc_entry (GstTocSetter * setter, const gchar * uid)
260 {
261   GstTocData *data;
262   const GstTocEntry *ret;
263
264   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
265   g_return_val_if_fail (uid != NULL, NULL);
266
267   data = gst_toc_setter_get_data (setter);
268
269   g_mutex_lock (&data->lock);
270
271   ret = gst_toc_find_entry (data->toc, uid);
272
273   g_mutex_unlock (&data->lock);
274
275   return ret;
276 }
277
278 /**
279  * gst_toc_setter_get_toc_entry_copy:
280  * @setter: a #GstTocSetter.
281  * @uid: UID to find entry with.
282  *
283  * Return #GstTocEntry (if any) with given @uid. It perform a deep copying,
284  * so you can modify returned value. Free it when done with gst_toc_entry_free().
285  * This function is thread-safe.
286  *
287  * Returns: a TOC entry with given @uid from the TOC in the setter
288  *          or NULL if none entry with such @uid was found.
289  *
290  * Since: 0.10.37
291  */
292 GstTocEntry *
293 gst_toc_setter_get_toc_entry_copy (GstTocSetter * setter, const gchar * uid)
294 {
295   GstTocData *data;
296   GstTocEntry *ret = NULL;
297   const GstTocEntry *search;
298
299   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
300   g_return_val_if_fail (uid != NULL, NULL);
301
302   data = gst_toc_setter_get_data (setter);
303
304   g_mutex_lock (&data->lock);
305
306   search = gst_toc_find_entry (data->toc, uid);
307   if (search != NULL)
308     ret = gst_toc_entry_copy (search);
309
310   g_mutex_unlock (&data->lock);
311
312   return ret;
313 }
314
315 /**
316  * gst_toc_setter_add_toc_entry:
317  * @setter: a #GstTocSetter.
318  * @parent_uid: UID of the parent entry to append given @entry. Use 0 for the TOC root level.
319  * @entry: #GstTocEntry to append.
320  *
321  * Try to find entry with given @parent_uid and append an @entry to that #GstTocEntry.
322  *
323  * Returns: TRUE if entry with @parent_uid was found, FALSE otherwise.
324  *
325  * Since: 0.10.37
326  */
327 gboolean
328 gst_toc_setter_add_toc_entry (GstTocSetter * setter, const gchar * parent_uid,
329     const GstTocEntry * entry)
330 {
331   GstTocData *data;
332   GstTocEntry *parent;
333   GstTocEntry *copy_entry;
334   gboolean ret = FALSE;
335
336   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), FALSE);
337   g_return_val_if_fail (parent_uid != NULL, FALSE);
338   g_return_val_if_fail (entry != NULL, FALSE);
339
340   data = gst_toc_setter_get_data (setter);
341
342   g_mutex_lock (&data->lock);
343
344   copy_entry = gst_toc_entry_copy (entry);
345
346   if (g_strcmp0 (parent_uid, "0") == 0)
347     data->toc->entries = g_list_append (data->toc->entries, copy_entry);
348   else {
349     parent = gst_toc_find_entry (data->toc, parent_uid);
350
351     if (parent != NULL) {
352       parent->subentries = g_list_append (parent->subentries, copy_entry);
353       ret = TRUE;
354     }
355   }
356
357   g_mutex_unlock (&data->lock);
358
359   return ret;
360 }