Merge remote-tracking branch 'origin/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_mutex_clear (&data->lock);
107
108   g_slice_free (GstTocData, data);
109 }
110
111 static GstTocData *
112 gst_toc_setter_get_data (GstTocSetter * setter)
113 {
114   GstTocData *data;
115
116   data = g_object_get_qdata (G_OBJECT (setter), gst_toc_key);
117   if (!data) {
118     static GMutex create_mutex;
119
120     /* make sure no other thread is creating a GstTocData at the same time */
121     g_mutex_lock (&create_mutex);
122     data = g_object_get_qdata (G_OBJECT (setter), gst_toc_key);
123     if (!data) {
124       data = g_slice_new (GstTocData);
125       g_mutex_init (&data->lock);
126       data->toc = NULL;
127       g_object_set_qdata_full (G_OBJECT (setter), gst_toc_key, data,
128           gst_toc_data_free);
129     }
130     g_mutex_unlock (&create_mutex);
131   }
132
133   return data;
134 }
135
136 /**
137  * gst_toc_setter_reset_toc:
138  * @setter: a #GstTocSetter.
139  *
140  * Reset the internal TOC. Elements should call this from within the
141  * state-change handler.
142  *
143  * Since: 0.10.37
144  */
145 void
146 gst_toc_setter_reset_toc (GstTocSetter * setter)
147 {
148   GstTocData *data;
149
150   g_return_if_fail (GST_IS_TOC_SETTER (setter));
151
152   data = gst_toc_setter_get_data (setter);
153
154   g_mutex_lock (&data->lock);
155   if (data->toc) {
156     gst_toc_free (data->toc);
157     data->toc = NULL;
158   }
159   g_mutex_unlock (&data->lock);
160 }
161
162 /**
163  * gst_toc_setter_get_toc:
164  * @setter: a #GstTocSetter.
165  *
166  * Return current TOC the setter uses. The TOC should not be
167  * modified or freed.
168  *
169  * This function is not thread-safe. Use gst_toc_setter_get_toc_copy() instead.
170  *
171  * Returns: a current snapshot of the TOC used in the setter
172  *          or NULL if none is used.
173  *
174  * Since: 0.10.37
175  */
176 const GstToc *
177 gst_toc_setter_get_toc (GstTocSetter * setter)
178 {
179   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
180
181   return gst_toc_setter_get_data (setter)->toc;
182 }
183
184 /**
185  * gst_toc_setter_get_toc_copy:
186  * @setter: a #GstTocSetter.
187  *
188  * Return current TOC the setter uses. The difference between this
189  * function and gst_toc_setter_get_toc() is that this function returns deep
190  * copy of the TOC, so you can modify it in any way. This function is thread-safe.
191  * Free it when done with gst_toc_free().
192  *
193  * Returns: a copy of the current snapshot of the TOC used in the setter
194  *          or NULL if none is used.
195  *
196  * Since: 0.10.37
197  */
198 GstToc *
199 gst_toc_setter_get_toc_copy (GstTocSetter * setter)
200 {
201   GstTocData *data;
202   GstToc *ret = NULL;
203
204   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
205
206   data = gst_toc_setter_get_data (setter);
207   g_mutex_lock (&data->lock);
208
209   if (data->toc != NULL)
210     ret = gst_toc_copy (data->toc);
211
212   g_mutex_unlock (&data->lock);
213
214   return ret;
215 }
216
217 /**
218  * gst_toc_setter_set_toc:
219  * @setter: a #GstTocSetter.
220  * @toc: a #GstToc to set.
221  *
222  * Set the given TOC on the setter. Previously setted TOC will be
223  * freed before setting a new one.
224  *
225  * Since: 0.10.37
226  */
227 void
228 gst_toc_setter_set_toc (GstTocSetter * setter, const GstToc * toc)
229 {
230   GstTocData *data;
231
232   g_return_if_fail (GST_IS_TOC_SETTER (setter));
233
234   data = gst_toc_setter_get_data (setter);
235
236   g_mutex_lock (&data->lock);
237   if (data->toc)
238     gst_toc_free (data->toc);
239
240   data->toc = gst_toc_copy (toc);
241
242   g_mutex_unlock (&data->lock);
243 }
244
245 /**
246  * gst_toc_setter_get_toc_entry:
247  * @setter: a #GstTocSetter.
248  * @uid: UID to find entry with.
249  *
250  * Return #GstTocEntry (if any) with given @uid. Returned entry should
251  * not be modified or freed.
252  *
253  * This function is not thread-safe. Use gst_toc_setter_get_toc_entry_copy() instead.
254  *
255  * Returns: a TOC entry with given @uid from the TOC in the setter
256  *          or NULL if none entry with such @uid was found.
257  *
258  * Since: 0.10.37
259  */
260 const GstTocEntry *
261 gst_toc_setter_get_toc_entry (GstTocSetter * setter, const gchar * uid)
262 {
263   GstTocData *data;
264   const GstTocEntry *ret;
265
266   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
267   g_return_val_if_fail (uid != NULL, NULL);
268
269   data = gst_toc_setter_get_data (setter);
270
271   g_mutex_lock (&data->lock);
272
273   ret = gst_toc_find_entry (data->toc, uid);
274
275   g_mutex_unlock (&data->lock);
276
277   return ret;
278 }
279
280 /**
281  * gst_toc_setter_get_toc_entry_copy:
282  * @setter: a #GstTocSetter.
283  * @uid: UID to find entry with.
284  *
285  * Return #GstTocEntry (if any) with given @uid. It perform a deep copying,
286  * so you can modify returned value. Free it when done with gst_toc_entry_free().
287  * This function is thread-safe.
288  *
289  * Returns: a TOC entry with given @uid from the TOC in the setter
290  *          or NULL if none entry with such @uid was found.
291  *
292  * Since: 0.10.37
293  */
294 GstTocEntry *
295 gst_toc_setter_get_toc_entry_copy (GstTocSetter * setter, const gchar * uid)
296 {
297   GstTocData *data;
298   GstTocEntry *ret = NULL;
299   const GstTocEntry *search;
300
301   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
302   g_return_val_if_fail (uid != NULL, NULL);
303
304   data = gst_toc_setter_get_data (setter);
305
306   g_mutex_lock (&data->lock);
307
308   search = gst_toc_find_entry (data->toc, uid);
309   if (search != NULL)
310     ret = gst_toc_entry_copy (search);
311
312   g_mutex_unlock (&data->lock);
313
314   return ret;
315 }
316
317 /**
318  * gst_toc_setter_add_toc_entry:
319  * @setter: a #GstTocSetter.
320  * @parent_uid: UID of the parent entry to append given @entry. Use 0 for the TOC root level.
321  * @entry: #GstTocEntry to append.
322  *
323  * Try to find entry with given @parent_uid and append an @entry to that #GstTocEntry.
324  *
325  * Returns: TRUE if entry with @parent_uid was found, FALSE otherwise.
326  *
327  * Since: 0.10.37
328  */
329 gboolean
330 gst_toc_setter_add_toc_entry (GstTocSetter * setter, const gchar * parent_uid,
331     const GstTocEntry * entry)
332 {
333   GstTocData *data;
334   GstTocEntry *parent;
335   GstTocEntry *copy_entry;
336   gboolean ret = FALSE;
337
338   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), FALSE);
339   g_return_val_if_fail (parent_uid != NULL, FALSE);
340   g_return_val_if_fail (entry != NULL, FALSE);
341
342   data = gst_toc_setter_get_data (setter);
343
344   g_mutex_lock (&data->lock);
345
346   copy_entry = gst_toc_entry_copy (entry);
347
348   if (g_strcmp0 (parent_uid, "0") == 0)
349     data->toc->entries = g_list_append (data->toc->entries, copy_entry);
350   else {
351     parent = gst_toc_find_entry (data->toc, parent_uid);
352
353     if (parent != NULL) {
354       parent->subentries = g_list_append (parent->subentries, copy_entry);
355       ret = TRUE;
356     }
357   }
358
359   g_mutex_unlock (&data->lock);
360
361   return ret;
362 }