tocsetter: use G_DEFINE_INTERFACE_* macro
[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 static GQuark gst_toc_key;
54
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"););
57
58 static void
59 gst_toc_setter_default_init (GstTocSetterInterface * klass)
60 {
61   /* nothing to do here, it's a dummy interface */
62 }
63
64 typedef struct
65 {
66   GstToc *toc;
67   GMutex lock;
68 } GstTocData;
69
70 static void
71 gst_toc_data_free (gpointer p)
72 {
73   GstTocData *data = (GstTocData *) p;
74
75   if (data->toc)
76     gst_toc_unref (data->toc);
77
78   g_mutex_clear (&data->lock);
79
80   g_slice_free (GstTocData, data);
81 }
82
83 static GstTocData *
84 gst_toc_setter_get_data (GstTocSetter * setter)
85 {
86   GstTocData *data;
87
88   data = g_object_get_qdata (G_OBJECT (setter), gst_toc_key);
89   if (!data) {
90     static GMutex create_mutex;
91
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);
95     if (!data) {
96       data = g_slice_new (GstTocData);
97       g_mutex_init (&data->lock);
98       data->toc = NULL;
99       g_object_set_qdata_full (G_OBJECT (setter), gst_toc_key, data,
100           gst_toc_data_free);
101     }
102     g_mutex_unlock (&create_mutex);
103   }
104
105   return data;
106 }
107
108 /**
109  * gst_toc_setter_reset_toc:
110  * @setter: a #GstTocSetter.
111  *
112  * Reset the internal TOC. Elements should call this from within the
113  * state-change handler.
114  *
115  * Since: 0.10.37
116  */
117 void
118 gst_toc_setter_reset_toc (GstTocSetter * setter)
119 {
120   GstTocData *data;
121
122   g_return_if_fail (GST_IS_TOC_SETTER (setter));
123
124   data = gst_toc_setter_get_data (setter);
125
126   g_mutex_lock (&data->lock);
127   if (data->toc) {
128     gst_toc_unref (data->toc);
129     data->toc = NULL;
130   }
131   g_mutex_unlock (&data->lock);
132 }
133
134 /**
135  * gst_toc_setter_get_toc:
136  * @setter: a #GstTocSetter.
137  *
138  * Return current TOC the setter uses. The TOC should not be
139  * modified or freed.
140  *
141  * This function is not thread-safe. Use gst_toc_setter_get_toc_copy() instead.
142  *
143  * Returns: a current snapshot of the TOC used in the setter
144  *          or NULL if none is used.
145  *
146  * Since: 0.10.37
147  */
148 const GstToc *
149 gst_toc_setter_get_toc (GstTocSetter * setter)
150 {
151   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
152
153   return gst_toc_setter_get_data (setter)->toc;
154 }
155
156 /**
157  * gst_toc_setter_get_toc_copy:
158  * @setter: a #GstTocSetter.
159  *
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().
164  *
165  * Returns: a copy of the current snapshot of the TOC used in the setter
166  *          or NULL if none is used.
167  *
168  * Since: 0.10.37
169  */
170 GstToc *
171 gst_toc_setter_get_toc_copy (GstTocSetter * setter)
172 {
173   GstTocData *data;
174   GstToc *ret = NULL;
175
176   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
177
178   data = gst_toc_setter_get_data (setter);
179   g_mutex_lock (&data->lock);
180
181   if (data->toc != NULL)
182     ret = gst_toc_copy (data->toc);
183
184   g_mutex_unlock (&data->lock);
185
186   return ret;
187 }
188
189 /**
190  * gst_toc_setter_set_toc:
191  * @setter: a #GstTocSetter.
192  * @toc: a #GstToc to set.
193  *
194  * Set the given TOC on the setter. Previously setted TOC will be
195  * freed before setting a new one.
196  *
197  * Since: 0.10.37
198  */
199 void
200 gst_toc_setter_set_toc (GstTocSetter * setter, const GstToc * toc)
201 {
202   GstTocData *data;
203
204   g_return_if_fail (GST_IS_TOC_SETTER (setter));
205
206   data = gst_toc_setter_get_data (setter);
207
208   g_mutex_lock (&data->lock);
209   if (data->toc)
210     gst_toc_unref (data->toc);
211
212   data->toc = gst_toc_copy (toc);
213
214   g_mutex_unlock (&data->lock);
215 }
216
217 /**
218  * gst_toc_setter_get_toc_entry:
219  * @setter: a #GstTocSetter.
220  * @uid: UID to find entry with.
221  *
222  * Return #GstTocEntry (if any) with given @uid. Returned entry should
223  * not be modified or freed.
224  *
225  * This function is not thread-safe. Use gst_toc_setter_get_toc_entry_copy() instead.
226  *
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.
229  *
230  * Since: 0.10.37
231  */
232 const GstTocEntry *
233 gst_toc_setter_get_toc_entry (GstTocSetter * setter, const gchar * uid)
234 {
235   GstTocData *data;
236   const GstTocEntry *ret;
237
238   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
239   g_return_val_if_fail (uid != NULL, NULL);
240
241   data = gst_toc_setter_get_data (setter);
242
243   g_mutex_lock (&data->lock);
244
245   ret = gst_toc_find_entry (data->toc, uid);
246
247   g_mutex_unlock (&data->lock);
248
249   return ret;
250 }
251
252 /**
253  * gst_toc_setter_get_toc_entry_copy:
254  * @setter: a #GstTocSetter.
255  * @uid: UID to find entry with.
256  *
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.
260  *
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.
263  *
264  * Since: 0.10.37
265  */
266 GstTocEntry *
267 gst_toc_setter_get_toc_entry_copy (GstTocSetter * setter, const gchar * uid)
268 {
269   GstTocData *data;
270   GstTocEntry *ret = NULL;
271   const GstTocEntry *search;
272
273   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
274   g_return_val_if_fail (uid != NULL, NULL);
275
276   data = gst_toc_setter_get_data (setter);
277
278   g_mutex_lock (&data->lock);
279
280   search = gst_toc_find_entry (data->toc, uid);
281   if (search != NULL)
282     ret = gst_toc_entry_copy (search);
283
284   g_mutex_unlock (&data->lock);
285
286   return ret;
287 }
288
289 /**
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.
294  *
295  * Try to find entry with given @parent_uid and append an @entry to that #GstTocEntry.
296  *
297  * Returns: TRUE if entry with @parent_uid was found, FALSE otherwise.
298  *
299  * Since: 0.10.37
300  */
301 gboolean
302 gst_toc_setter_add_toc_entry (GstTocSetter * setter, const gchar * parent_uid,
303     const GstTocEntry * entry)
304 {
305   GstTocData *data;
306   GstTocEntry *parent;
307   GstTocEntry *copy_entry;
308   gboolean ret = FALSE;
309
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);
313
314   data = gst_toc_setter_get_data (setter);
315
316   g_mutex_lock (&data->lock);
317
318   copy_entry = gst_toc_entry_copy (entry);
319
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);
324   } else {
325     parent = gst_toc_find_entry (data->toc, parent_uid);
326
327     if (parent != NULL) {
328       parent->subentries = g_list_append (parent->subentries, copy_entry);
329       ret = TRUE;
330     }
331   }
332
333   g_mutex_unlock (&data->lock);
334
335   return ret;
336 }