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 * @title: GstTocSetter
25 * @short_description: Element interface that allows setting and retrieval
26 * of the TOC
27 *
28 * Element interface that allows setting of the TOC.
29 *
30 * Elements that support some kind of chapters or editions (or tracks like in
31 * the FLAC cue sheet) will implement this interface.
32 *
33 * If you just want to retrieve the TOC in your application then all you
34 * need to do is watch for TOC messages on your pipeline's bus (or you can
35 * perform TOC query). This interface is only for setting TOC data, not for
36 * extracting it. To set TOC from the application, find proper tocsetter element
37 * and set TOC using gst_toc_setter_set_toc().
38 *
39 * Elements implementing the #GstTocSetter interface can extend existing TOC
40 * by getting extend UID for that (you can use gst_toc_find_entry() to retrieve it)
41 * with any TOC entries received from downstream.
42 */
43
44
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
48
49 #include "gst_private.h"
50 #include "gsttocsetter.h"
51 #include <gobject/gvaluecollector.h>
52 #include <string.h>
53
54 static GQuark gst_toc_key;
55
56 G_DEFINE_INTERFACE_WITH_CODE (GstTocSetter, gst_toc_setter, GST_TYPE_ELEMENT,
57 gst_toc_key = g_quark_from_static_string ("gst-toc-setter-data"););
58
59 static void
gst_toc_setter_default_init(GstTocSetterInterface * klass)60 gst_toc_setter_default_init (GstTocSetterInterface * klass)
61 {
62 /* nothing to do here, it's a dummy interface */
63 }
64
65 typedef struct
66 {
67 GstToc *toc;
68 GMutex lock;
69 } GstTocData;
70
71 static void
gst_toc_data_free(gpointer p)72 gst_toc_data_free (gpointer p)
73 {
74 GstTocData *data = (GstTocData *) p;
75
76 if (data->toc)
77 gst_toc_unref (data->toc);
78
79 g_mutex_clear (&data->lock);
80
81 g_slice_free (GstTocData, data);
82 }
83
84 static GstTocData *
gst_toc_setter_get_data(GstTocSetter * setter)85 gst_toc_setter_get_data (GstTocSetter * setter)
86 {
87 GstTocData *data;
88
89 data = g_object_get_qdata (G_OBJECT (setter), gst_toc_key);
90 if (!data) {
91 static GMutex create_mutex;
92
93 /* make sure no other thread is creating a GstTocData at the same time */
94 g_mutex_lock (&create_mutex);
95 data = g_object_get_qdata (G_OBJECT (setter), gst_toc_key);
96 if (!data) {
97 data = g_slice_new (GstTocData);
98 g_mutex_init (&data->lock);
99 data->toc = NULL;
100 g_object_set_qdata_full (G_OBJECT (setter), gst_toc_key, data,
101 gst_toc_data_free);
102 }
103 g_mutex_unlock (&create_mutex);
104 }
105
106 return data;
107 }
108
109 /**
110 * gst_toc_setter_reset:
111 * @setter: a #GstTocSetter.
112 *
113 * Reset the internal TOC. Elements should call this from within the
114 * state-change handler.
115 */
116 void
gst_toc_setter_reset(GstTocSetter * setter)117 gst_toc_setter_reset (GstTocSetter * setter)
118 {
119 g_return_if_fail (GST_IS_TOC_SETTER (setter));
120
121 gst_toc_setter_set_toc (setter, NULL);
122 }
123
124 /**
125 * gst_toc_setter_get_toc:
126 * @setter: a #GstTocSetter.
127 *
128 * Return current TOC the setter uses. The TOC should not be
129 * modified without making it writable first.
130 *
131 * Returns: (transfer full) (nullable): TOC set, or %NULL. Unref with
132 * gst_toc_unref() when no longer needed
133 */
134 GstToc *
gst_toc_setter_get_toc(GstTocSetter * setter)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
gst_toc_setter_set_toc(GstTocSetter * setter,GstToc * toc)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 }
181