docs: convert NULL, TRUE, and FALSE to %NULL, %TRUE, and %FALSE
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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:
110  * @setter: a #GstTocSetter.
111  *
112  * Reset the internal TOC. Elements should call this from within the
113  * state-change handler.
114  */
115 void
116 gst_toc_setter_reset (GstTocSetter * setter)
117 {
118   g_return_if_fail (GST_IS_TOC_SETTER (setter));
119
120   gst_toc_setter_set_toc (setter, NULL);
121 }
122
123 /**
124  * gst_toc_setter_get_toc:
125  * @setter: a #GstTocSetter.
126  *
127  * Return current TOC the setter uses. The TOC should not be
128  * modified without making it writable first.
129  *
130  *
131  * Returns: (transfer full): TOC set, or %NULL. Unref with gst_toc_unref()
132  *     when no longer needed
133  */
134 GstToc *
135 gst_toc_setter_get_toc (GstTocSetter * setter)
136 {
137   GstTocData *data;
138   GstToc *ret = NULL;
139
140   g_return_val_if_fail (GST_IS_TOC_SETTER (setter), NULL);
141
142   data = gst_toc_setter_get_data (setter);
143   g_mutex_lock (&data->lock);
144
145   if (data->toc != NULL)
146     ret = gst_toc_ref (data->toc);
147
148   g_mutex_unlock (&data->lock);
149
150   return ret;
151 }
152
153 /**
154  * gst_toc_setter_set_toc:
155  * @setter: a #GstTocSetter.
156  * @toc: (allow-none): a #GstToc to set.
157  *
158  * Set the given TOC on the setter. Previously set TOC will be
159  * unreffed before setting a new one.
160  */
161 void
162 gst_toc_setter_set_toc (GstTocSetter * setter, GstToc * toc)
163 {
164   GstTocData *data;
165
166   g_return_if_fail (GST_IS_TOC_SETTER (setter));
167
168   data = gst_toc_setter_get_data (setter);
169
170   g_mutex_lock (&data->lock);
171
172   if (data->toc != toc) {
173     if (data->toc)
174       gst_toc_unref (data->toc);
175
176     data->toc = (toc) ? gst_toc_ref (toc) : NULL;
177   }
178
179   g_mutex_unlock (&data->lock);
180 }