/src/gstreamer/subprojects/gst-plugins-base/gst-libs/gst/audio/gstaudiosink.c
Line | Count | Source |
1 | | /* GStreamer |
2 | | * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu> |
3 | | * 2005 Wim Taymans <wim@fluendo.com> |
4 | | * |
5 | | * gstaudiosink.c: simple audio sink base class |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Library General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Library General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Library General Public |
18 | | * License along with this library; if not, write to the |
19 | | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
20 | | * Boston, MA 02110-1301, USA. |
21 | | */ |
22 | | |
23 | | /** |
24 | | * SECTION:gstaudiosink |
25 | | * @title: GstAudioSink |
26 | | * @short_description: Simple base class for audio sinks |
27 | | * @see_also: #GstAudioBaseSink, #GstAudioRingBuffer, #GstAudioSink. |
28 | | * |
29 | | * This is the most simple base class for audio sinks that only requires |
30 | | * subclasses to implement a set of simple functions: |
31 | | * |
32 | | * * `open()` :Open the device. |
33 | | * |
34 | | * * `prepare()` :Configure the device with the specified format. |
35 | | * |
36 | | * * `write()` :Write samples to the device. |
37 | | * |
38 | | * * `reset()` :Unblock writes and flush the device. |
39 | | * |
40 | | * * `delay()` :Get the number of samples written but not yet played |
41 | | * by the device. |
42 | | * |
43 | | * * `unprepare()` :Undo operations done by prepare. |
44 | | * |
45 | | * * `close()` :Close the device. |
46 | | * |
47 | | * All scheduling of samples and timestamps is done in this base class |
48 | | * together with #GstAudioBaseSink using a default implementation of a |
49 | | * #GstAudioRingBuffer that uses threads. |
50 | | */ |
51 | | #ifdef HAVE_CONFIG_H |
52 | | #include "config.h" |
53 | | #endif |
54 | | |
55 | | #include <string.h> |
56 | | |
57 | | #include <gst/audio/audio.h> |
58 | | #include <gst/audio/gstdsd.h> |
59 | | #include "gstaudiosink.h" |
60 | | #include "gstaudioutilsprivate.h" |
61 | | |
62 | | GST_DEBUG_CATEGORY_STATIC (gst_audio_sink_debug); |
63 | | #define GST_CAT_DEFAULT gst_audio_sink_debug |
64 | | |
65 | | #define GST_TYPE_AUDIO_SINK_RING_BUFFER \ |
66 | 0 | (gst_audio_sink_ring_buffer_get_type()) |
67 | | #define GST_AUDIO_SINK_RING_BUFFER(obj) \ |
68 | | (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_SINK_RING_BUFFER,GstAudioSinkRingBuffer)) |
69 | | #define GST_AUDIO_SINK_RING_BUFFER_CLASS(klass) \ |
70 | | (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_SINK_RING_BUFFER,GstAudioSinkRingBufferClass)) |
71 | | #define GST_AUDIO_SINK_RING_BUFFER_GET_CLASS(obj) \ |
72 | | (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_AUDIO_SINK_RING_BUFFER, GstAudioSinkRingBufferClass)) |
73 | | #define GST_AUDIO_SINK_RING_BUFFER_CAST(obj) \ |
74 | 0 | ((GstAudioSinkRingBuffer *)obj) |
75 | | #define GST_IS_AUDIO_SINK_RING_BUFFER(obj) \ |
76 | | (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_SINK_RING_BUFFER)) |
77 | | #define GST_IS_AUDIO_SINK_RING_BUFFER_CLASS(klass)\ |
78 | | (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_SINK_RING_BUFFER)) |
79 | | |
80 | | typedef struct _GstAudioSinkRingBuffer GstAudioSinkRingBuffer; |
81 | | typedef struct _GstAudioSinkRingBufferClass GstAudioSinkRingBufferClass; |
82 | | |
83 | 0 | #define GST_AUDIO_SINK_RING_BUFFER_GET_COND(buf) (&(((GstAudioSinkRingBuffer *)buf)->cond)) |
84 | 0 | #define GST_AUDIO_SINK_RING_BUFFER_WAIT(buf) (g_cond_wait (GST_AUDIO_SINK_RING_BUFFER_GET_COND (buf), GST_OBJECT_GET_LOCK (buf))) |
85 | 0 | #define GST_AUDIO_SINK_RING_BUFFER_SIGNAL(buf) (g_cond_signal (GST_AUDIO_SINK_RING_BUFFER_GET_COND (buf))) |
86 | | #define GST_AUDIO_SINK_RING_BUFFER_BROADCAST(buf)(g_cond_broadcast (GST_AUDIO_SINK_RING_BUFFER_GET_COND (buf))) |
87 | | |
88 | | struct _GstAudioSinkRingBuffer |
89 | | { |
90 | | GstAudioRingBuffer object; |
91 | | |
92 | | gboolean running; |
93 | | gint queuedseg; |
94 | | |
95 | | GCond cond; |
96 | | }; |
97 | | |
98 | | struct _GstAudioSinkRingBufferClass |
99 | | { |
100 | | GstAudioRingBufferClass parent_class; |
101 | | }; |
102 | | |
103 | | static void gst_audio_sink_ring_buffer_class_init (GstAudioSinkRingBufferClass * |
104 | | klass); |
105 | | static void gst_audio_sink_ring_buffer_init (GstAudioSinkRingBuffer * |
106 | | ringbuffer, GstAudioSinkRingBufferClass * klass); |
107 | | static void gst_audio_sink_ring_buffer_dispose (GObject * object); |
108 | | static void gst_audio_sink_ring_buffer_finalize (GObject * object); |
109 | | |
110 | | static GstAudioRingBufferClass *ring_parent_class = NULL; |
111 | | |
112 | | static gboolean gst_audio_sink_ring_buffer_open_device (GstAudioRingBuffer * |
113 | | buf); |
114 | | static gboolean gst_audio_sink_ring_buffer_close_device (GstAudioRingBuffer * |
115 | | buf); |
116 | | static gboolean gst_audio_sink_ring_buffer_acquire (GstAudioRingBuffer * buf, |
117 | | GstAudioRingBufferSpec * spec); |
118 | | static gboolean gst_audio_sink_ring_buffer_release (GstAudioRingBuffer * buf); |
119 | | static gboolean gst_audio_sink_ring_buffer_start (GstAudioRingBuffer * buf); |
120 | | static gboolean gst_audio_sink_ring_buffer_pause (GstAudioRingBuffer * buf); |
121 | | static gboolean gst_audio_sink_ring_buffer_resume (GstAudioRingBuffer * buf); |
122 | | static gboolean gst_audio_sink_ring_buffer_stop (GstAudioRingBuffer * buf); |
123 | | static guint gst_audio_sink_ring_buffer_delay (GstAudioRingBuffer * buf); |
124 | | static gboolean gst_audio_sink_ring_buffer_activate (GstAudioRingBuffer * buf, |
125 | | gboolean active); |
126 | | static void gst_audio_sink_ring_buffer_clear_all (GstAudioRingBuffer * buf); |
127 | | |
128 | | /* ringbuffer abstract base class */ |
129 | | static GType |
130 | | gst_audio_sink_ring_buffer_get_type (void) |
131 | 0 | { |
132 | 0 | static GType ringbuffer_type = 0; |
133 | |
|
134 | 0 | if (!ringbuffer_type) { |
135 | 0 | static const GTypeInfo ringbuffer_info = { |
136 | 0 | sizeof (GstAudioSinkRingBufferClass), |
137 | 0 | NULL, |
138 | 0 | NULL, |
139 | 0 | (GClassInitFunc) gst_audio_sink_ring_buffer_class_init, |
140 | 0 | NULL, |
141 | 0 | NULL, |
142 | 0 | sizeof (GstAudioSinkRingBuffer), |
143 | 0 | 0, |
144 | 0 | (GInstanceInitFunc) gst_audio_sink_ring_buffer_init, |
145 | 0 | NULL |
146 | 0 | }; |
147 | |
|
148 | 0 | ringbuffer_type = |
149 | 0 | g_type_register_static (GST_TYPE_AUDIO_RING_BUFFER, |
150 | 0 | "GstAudioSinkRingBuffer", &ringbuffer_info, 0); |
151 | 0 | } |
152 | 0 | return ringbuffer_type; |
153 | 0 | } |
154 | | |
155 | | static void |
156 | | gst_audio_sink_ring_buffer_class_init (GstAudioSinkRingBufferClass * klass) |
157 | 0 | { |
158 | 0 | GObjectClass *gobject_class; |
159 | 0 | GstAudioRingBufferClass *gstringbuffer_class; |
160 | |
|
161 | 0 | gobject_class = (GObjectClass *) klass; |
162 | 0 | gstringbuffer_class = (GstAudioRingBufferClass *) klass; |
163 | |
|
164 | 0 | ring_parent_class = g_type_class_peek_parent (klass); |
165 | |
|
166 | 0 | gobject_class->dispose = gst_audio_sink_ring_buffer_dispose; |
167 | 0 | gobject_class->finalize = gst_audio_sink_ring_buffer_finalize; |
168 | |
|
169 | 0 | gstringbuffer_class->open_device = |
170 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_open_device); |
171 | 0 | gstringbuffer_class->close_device = |
172 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_close_device); |
173 | 0 | gstringbuffer_class->acquire = |
174 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_acquire); |
175 | 0 | gstringbuffer_class->release = |
176 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_release); |
177 | 0 | gstringbuffer_class->start = |
178 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_start); |
179 | 0 | gstringbuffer_class->pause = |
180 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_pause); |
181 | 0 | gstringbuffer_class->resume = |
182 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_resume); |
183 | 0 | gstringbuffer_class->stop = |
184 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_stop); |
185 | 0 | gstringbuffer_class->delay = |
186 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_delay); |
187 | 0 | gstringbuffer_class->activate = |
188 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_activate); |
189 | 0 | gstringbuffer_class->clear_all = |
190 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_ring_buffer_clear_all); |
191 | 0 | } |
192 | | |
193 | | typedef gint (*WriteFunc) (GstAudioSink * sink, gpointer data, guint length); |
194 | | |
195 | | /* this internal thread does nothing else but write samples to the audio device. |
196 | | * It will write each segment in the ringbuffer and will update the play |
197 | | * pointer. |
198 | | * The start/stop methods control the thread. |
199 | | */ |
200 | | static gpointer |
201 | | audioringbuffer_thread_func (GstAudioRingBuffer * buf) |
202 | 0 | { |
203 | 0 | GstAudioSink *sink; |
204 | 0 | GstAudioSinkClass *csink; |
205 | 0 | GstAudioSinkRingBuffer *abuf = GST_AUDIO_SINK_RING_BUFFER_CAST (buf); |
206 | 0 | WriteFunc writefunc; |
207 | 0 | GstMessage *message; |
208 | 0 | GValue val = { 0 }; |
209 | 0 | gpointer handle; |
210 | |
|
211 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
212 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
213 | |
|
214 | 0 | GST_DEBUG_OBJECT (sink, "enter thread"); |
215 | |
|
216 | 0 | GST_OBJECT_LOCK (abuf); |
217 | 0 | GST_DEBUG_OBJECT (sink, "signal wait"); |
218 | 0 | GST_AUDIO_SINK_RING_BUFFER_SIGNAL (buf); |
219 | 0 | GST_OBJECT_UNLOCK (abuf); |
220 | |
|
221 | 0 | writefunc = csink->write; |
222 | 0 | if (writefunc == NULL) |
223 | 0 | goto no_function; |
224 | | |
225 | 0 | if (G_UNLIKELY (!__gst_audio_set_thread_priority (&handle))) |
226 | 0 | GST_WARNING_OBJECT (sink, "failed to set thread priority"); |
227 | |
|
228 | 0 | message = gst_message_new_stream_status (GST_OBJECT_CAST (buf), |
229 | 0 | GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (sink)); |
230 | 0 | g_value_init (&val, GST_TYPE_G_THREAD); |
231 | 0 | g_value_set_boxed (&val, g_thread_self ()); |
232 | 0 | gst_message_set_stream_status_object (message, &val); |
233 | 0 | g_value_unset (&val); |
234 | 0 | GST_DEBUG_OBJECT (sink, "posting ENTER stream status"); |
235 | 0 | gst_element_post_message (GST_ELEMENT_CAST (sink), message); |
236 | |
|
237 | 0 | while (TRUE) { |
238 | 0 | gint left, len; |
239 | 0 | guint8 *readptr; |
240 | 0 | gint readseg; |
241 | | |
242 | | /* buffer must be started */ |
243 | 0 | if (gst_audio_ring_buffer_prepare_read (buf, &readseg, &readptr, &len)) { |
244 | 0 | gint written; |
245 | |
|
246 | 0 | left = len; |
247 | 0 | do { |
248 | 0 | written = writefunc (sink, readptr, left); |
249 | 0 | GST_LOG_OBJECT (sink, "transferred %d bytes of %d from segment %d", |
250 | 0 | written, left, readseg); |
251 | 0 | if (written < 0 || written > left) { |
252 | | /* might not be critical, it e.g. happens when aborting playback */ |
253 | 0 | GST_WARNING_OBJECT (sink, |
254 | 0 | "error writing data in %s (reason: %s), skipping segment (left: %d, written: %d)", |
255 | 0 | GST_DEBUG_FUNCPTR_NAME (writefunc), |
256 | 0 | (errno > 1 ? g_strerror (errno) : "unknown"), left, written); |
257 | 0 | break; |
258 | 0 | } else if (written == 0 && G_UNLIKELY (g_atomic_int_get (&buf->state) != |
259 | 0 | GST_AUDIO_RING_BUFFER_STATE_STARTED)) { |
260 | 0 | break; |
261 | 0 | } |
262 | 0 | left -= written; |
263 | 0 | readptr += written; |
264 | 0 | } while (left > 0); |
265 | | |
266 | | /* clear written samples */ |
267 | 0 | gst_audio_ring_buffer_clear (buf, readseg); |
268 | | |
269 | | /* we wrote one segment */ |
270 | 0 | gst_audio_ring_buffer_advance (buf, 1); |
271 | 0 | } else { |
272 | 0 | GST_OBJECT_LOCK (abuf); |
273 | 0 | if (!abuf->running) |
274 | 0 | goto stop_running; |
275 | 0 | if (G_UNLIKELY (g_atomic_int_get (&buf->state) == |
276 | 0 | GST_AUDIO_RING_BUFFER_STATE_STARTED)) { |
277 | 0 | GST_OBJECT_UNLOCK (abuf); |
278 | 0 | continue; |
279 | 0 | } |
280 | 0 | GST_DEBUG_OBJECT (sink, "signal wait"); |
281 | 0 | GST_AUDIO_SINK_RING_BUFFER_SIGNAL (buf); |
282 | 0 | GST_DEBUG_OBJECT (sink, "wait for action"); |
283 | 0 | GST_AUDIO_SINK_RING_BUFFER_WAIT (buf); |
284 | 0 | GST_DEBUG_OBJECT (sink, "got signal"); |
285 | 0 | if (!abuf->running) |
286 | 0 | goto stop_running; |
287 | 0 | GST_DEBUG_OBJECT (sink, "continue running"); |
288 | 0 | GST_OBJECT_UNLOCK (abuf); |
289 | 0 | } |
290 | 0 | } |
291 | | |
292 | | /* Will never be reached */ |
293 | 0 | g_assert_not_reached (); |
294 | 0 | return NULL; |
295 | | |
296 | | /* ERROR */ |
297 | 0 | no_function: |
298 | 0 | { |
299 | 0 | GST_DEBUG_OBJECT (sink, "no write function, exit thread"); |
300 | 0 | return NULL; |
301 | 0 | } |
302 | 0 | stop_running: |
303 | 0 | { |
304 | 0 | GST_OBJECT_UNLOCK (abuf); |
305 | 0 | GST_DEBUG_OBJECT (sink, "stop running, exit thread"); |
306 | 0 | message = gst_message_new_stream_status (GST_OBJECT_CAST (buf), |
307 | 0 | GST_STREAM_STATUS_TYPE_LEAVE, GST_ELEMENT_CAST (sink)); |
308 | 0 | g_value_init (&val, GST_TYPE_G_THREAD); |
309 | 0 | g_value_set_boxed (&val, g_thread_self ()); |
310 | 0 | gst_message_set_stream_status_object (message, &val); |
311 | 0 | g_value_unset (&val); |
312 | 0 | GST_DEBUG_OBJECT (sink, "posting LEAVE stream status"); |
313 | 0 | gst_element_post_message (GST_ELEMENT_CAST (sink), message); |
314 | |
|
315 | 0 | if (G_UNLIKELY (!__gst_audio_restore_thread_priority (handle))) |
316 | 0 | GST_WARNING_OBJECT (sink, "failed to restore thread priority"); |
317 | 0 | return NULL; |
318 | 0 | } |
319 | 0 | } |
320 | | |
321 | | static void |
322 | | gst_audio_sink_ring_buffer_init (GstAudioSinkRingBuffer * ringbuffer, |
323 | | GstAudioSinkRingBufferClass * g_class) |
324 | 0 | { |
325 | 0 | ringbuffer->running = FALSE; |
326 | 0 | ringbuffer->queuedseg = 0; |
327 | |
|
328 | 0 | g_cond_init (&ringbuffer->cond); |
329 | 0 | } |
330 | | |
331 | | static void |
332 | | gst_audio_sink_ring_buffer_dispose (GObject * object) |
333 | 0 | { |
334 | 0 | G_OBJECT_CLASS (ring_parent_class)->dispose (object); |
335 | 0 | } |
336 | | |
337 | | static void |
338 | | gst_audio_sink_ring_buffer_finalize (GObject * object) |
339 | 0 | { |
340 | 0 | GstAudioSinkRingBuffer *ringbuffer = GST_AUDIO_SINK_RING_BUFFER_CAST (object); |
341 | |
|
342 | 0 | g_cond_clear (&ringbuffer->cond); |
343 | |
|
344 | 0 | G_OBJECT_CLASS (ring_parent_class)->finalize (object); |
345 | 0 | } |
346 | | |
347 | | static gboolean |
348 | | gst_audio_sink_ring_buffer_open_device (GstAudioRingBuffer * buf) |
349 | 0 | { |
350 | 0 | GstAudioSink *sink; |
351 | 0 | GstAudioSinkClass *csink; |
352 | 0 | gboolean result = TRUE; |
353 | |
|
354 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
355 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
356 | |
|
357 | 0 | if (csink->open) |
358 | 0 | result = csink->open (sink); |
359 | |
|
360 | 0 | if (!result) |
361 | 0 | goto could_not_open; |
362 | | |
363 | 0 | return result; |
364 | | |
365 | 0 | could_not_open: |
366 | 0 | { |
367 | 0 | GST_DEBUG_OBJECT (sink, "could not open device"); |
368 | 0 | return FALSE; |
369 | 0 | } |
370 | 0 | } |
371 | | |
372 | | static gboolean |
373 | | gst_audio_sink_ring_buffer_close_device (GstAudioRingBuffer * buf) |
374 | 0 | { |
375 | 0 | GstAudioSink *sink; |
376 | 0 | GstAudioSinkClass *csink; |
377 | 0 | gboolean result = TRUE; |
378 | |
|
379 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
380 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
381 | |
|
382 | 0 | if (csink->close) |
383 | 0 | result = csink->close (sink); |
384 | |
|
385 | 0 | if (!result) |
386 | 0 | goto could_not_close; |
387 | | |
388 | 0 | return result; |
389 | | |
390 | 0 | could_not_close: |
391 | 0 | { |
392 | 0 | GST_DEBUG_OBJECT (sink, "could not close device"); |
393 | 0 | return FALSE; |
394 | 0 | } |
395 | 0 | } |
396 | | |
397 | | static gboolean |
398 | | gst_audio_sink_ring_buffer_acquire (GstAudioRingBuffer * buf, |
399 | | GstAudioRingBufferSpec * spec) |
400 | 0 | { |
401 | 0 | GstAudioSink *sink; |
402 | 0 | GstAudioSinkClass *csink; |
403 | 0 | gboolean result = FALSE; |
404 | |
|
405 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
406 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
407 | |
|
408 | 0 | if (csink->prepare) |
409 | 0 | result = csink->prepare (sink, spec); |
410 | 0 | if (!result) |
411 | 0 | goto could_not_prepare; |
412 | | |
413 | | /* set latency to one more segment as we need some headroom */ |
414 | 0 | spec->seglatency = spec->segtotal + 1; |
415 | |
|
416 | 0 | buf->size = spec->segtotal * spec->segsize; |
417 | |
|
418 | 0 | buf->memory = g_malloc (buf->size); |
419 | |
|
420 | 0 | switch (buf->spec.type) { |
421 | 0 | case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW: |
422 | 0 | gst_audio_format_info_fill_silence (buf->spec.info.finfo, buf->memory, |
423 | 0 | buf->size); |
424 | 0 | break; |
425 | 0 | case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DSD: |
426 | 0 | memset (buf->memory, GST_DSD_SILENCE_PATTERN_BYTE, buf->size); |
427 | 0 | break; |
428 | 0 | default: |
429 | | /* FIXME, non-raw formats get 0 as the empty sample */ |
430 | 0 | memset (buf->memory, 0, buf->size); |
431 | 0 | break; |
432 | 0 | } |
433 | | |
434 | | |
435 | 0 | return TRUE; |
436 | | |
437 | | /* ERRORS */ |
438 | 0 | could_not_prepare: |
439 | 0 | { |
440 | 0 | GST_DEBUG_OBJECT (sink, "could not prepare device"); |
441 | 0 | return FALSE; |
442 | 0 | } |
443 | 0 | } |
444 | | |
445 | | static gboolean |
446 | | gst_audio_sink_ring_buffer_activate (GstAudioRingBuffer * buf, gboolean active) |
447 | 0 | { |
448 | 0 | GstAudioSink *sink; |
449 | 0 | GstAudioSinkRingBuffer *abuf; |
450 | 0 | GError *error = NULL; |
451 | |
|
452 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
453 | 0 | abuf = GST_AUDIO_SINK_RING_BUFFER_CAST (buf); |
454 | |
|
455 | 0 | if (active) { |
456 | 0 | abuf->running = TRUE; |
457 | |
|
458 | 0 | GST_DEBUG_OBJECT (sink, "starting thread"); |
459 | |
|
460 | 0 | sink->thread = g_thread_try_new ("audiosink-ringbuffer", |
461 | 0 | (GThreadFunc) audioringbuffer_thread_func, buf, &error); |
462 | |
|
463 | 0 | if (!sink->thread || error != NULL) |
464 | 0 | goto thread_failed; |
465 | | |
466 | 0 | GST_DEBUG_OBJECT (sink, "waiting for thread"); |
467 | | /* the object lock is taken */ |
468 | 0 | GST_AUDIO_SINK_RING_BUFFER_WAIT (buf); |
469 | 0 | GST_DEBUG_OBJECT (sink, "thread is started"); |
470 | 0 | } else { |
471 | 0 | abuf->running = FALSE; |
472 | 0 | GST_DEBUG_OBJECT (sink, "signal wait"); |
473 | 0 | GST_AUDIO_SINK_RING_BUFFER_SIGNAL (buf); |
474 | |
|
475 | 0 | GST_OBJECT_UNLOCK (buf); |
476 | | |
477 | | /* join the thread */ |
478 | 0 | g_thread_join (sink->thread); |
479 | |
|
480 | 0 | GST_OBJECT_LOCK (buf); |
481 | 0 | } |
482 | 0 | return TRUE; |
483 | | |
484 | | /* ERRORS */ |
485 | 0 | thread_failed: |
486 | 0 | { |
487 | 0 | if (error) |
488 | 0 | GST_ERROR_OBJECT (sink, "could not create thread %s", error->message); |
489 | 0 | else |
490 | 0 | GST_ERROR_OBJECT (sink, "could not create thread for unknown reason"); |
491 | 0 | g_clear_error (&error); |
492 | 0 | return FALSE; |
493 | 0 | } |
494 | 0 | } |
495 | | |
496 | | /* function is called with LOCK */ |
497 | | static gboolean |
498 | | gst_audio_sink_ring_buffer_release (GstAudioRingBuffer * buf) |
499 | 0 | { |
500 | 0 | GstAudioSink *sink; |
501 | 0 | GstAudioSinkClass *csink; |
502 | 0 | gboolean result = FALSE; |
503 | |
|
504 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
505 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
506 | | |
507 | | /* free the buffer */ |
508 | 0 | g_free (buf->memory); |
509 | 0 | buf->memory = NULL; |
510 | |
|
511 | 0 | if (csink->unprepare) |
512 | 0 | result = csink->unprepare (sink); |
513 | |
|
514 | 0 | if (!result) |
515 | 0 | goto could_not_unprepare; |
516 | | |
517 | 0 | GST_DEBUG_OBJECT (sink, "unprepared"); |
518 | |
|
519 | 0 | return result; |
520 | | |
521 | 0 | could_not_unprepare: |
522 | 0 | { |
523 | 0 | GST_DEBUG_OBJECT (sink, "could not unprepare device"); |
524 | 0 | return FALSE; |
525 | 0 | } |
526 | 0 | } |
527 | | |
528 | | static gboolean |
529 | | gst_audio_sink_ring_buffer_start (GstAudioRingBuffer * buf) |
530 | 0 | { |
531 | 0 | GstAudioSink *sink; |
532 | |
|
533 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
534 | |
|
535 | 0 | GST_DEBUG_OBJECT (sink, "start, sending signal"); |
536 | 0 | GST_AUDIO_SINK_RING_BUFFER_SIGNAL (buf); |
537 | |
|
538 | 0 | return TRUE; |
539 | 0 | } |
540 | | |
541 | | static gboolean |
542 | | gst_audio_sink_ring_buffer_pause (GstAudioRingBuffer * buf) |
543 | 0 | { |
544 | 0 | GstAudioSink *sink; |
545 | 0 | GstAudioSinkClass *csink; |
546 | |
|
547 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
548 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
549 | | |
550 | | /* unblock any pending writes to the audio device */ |
551 | 0 | if (csink->pause) { |
552 | 0 | GST_DEBUG_OBJECT (sink, "pause..."); |
553 | 0 | csink->pause (sink); |
554 | 0 | GST_DEBUG_OBJECT (sink, "pause done"); |
555 | 0 | } else if (csink->reset) { |
556 | | /* fallback to reset for audio sinks that don't provide pause */ |
557 | 0 | GST_DEBUG_OBJECT (sink, "reset..."); |
558 | 0 | csink->reset (sink); |
559 | 0 | GST_DEBUG_OBJECT (sink, "reset done"); |
560 | 0 | } |
561 | 0 | return TRUE; |
562 | 0 | } |
563 | | |
564 | | static gboolean |
565 | | gst_audio_sink_ring_buffer_resume (GstAudioRingBuffer * buf) |
566 | 0 | { |
567 | 0 | GstAudioSink *sink; |
568 | 0 | GstAudioSinkClass *csink; |
569 | |
|
570 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
571 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
572 | |
|
573 | 0 | if (csink->resume) { |
574 | 0 | GST_DEBUG_OBJECT (sink, "resume..."); |
575 | 0 | csink->resume (sink); |
576 | 0 | GST_DEBUG_OBJECT (sink, "resume done"); |
577 | 0 | } |
578 | |
|
579 | 0 | gst_audio_sink_ring_buffer_start (buf); |
580 | |
|
581 | 0 | return TRUE; |
582 | 0 | } |
583 | | |
584 | | static gboolean |
585 | | gst_audio_sink_ring_buffer_stop (GstAudioRingBuffer * buf) |
586 | 0 | { |
587 | 0 | GstAudioSink *sink; |
588 | 0 | GstAudioSinkClass *csink; |
589 | |
|
590 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
591 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
592 | | |
593 | | /* unblock any pending writes to the audio device */ |
594 | 0 | if (csink->stop) { |
595 | 0 | GST_DEBUG_OBJECT (sink, "stop..."); |
596 | 0 | csink->stop (sink); |
597 | 0 | GST_DEBUG_OBJECT (sink, "stop done"); |
598 | 0 | } else if (csink->reset) { |
599 | | /* fallback to reset for audio sinks that don't provide stop */ |
600 | 0 | GST_DEBUG_OBJECT (sink, "reset..."); |
601 | 0 | csink->reset (sink); |
602 | 0 | GST_DEBUG_OBJECT (sink, "reset done"); |
603 | 0 | } |
604 | | #if 0 |
605 | | if (abuf->running) { |
606 | | GST_DEBUG_OBJECT (sink, "stop, waiting..."); |
607 | | GST_AUDIO_SINK_RING_BUFFER_WAIT (buf); |
608 | | GST_DEBUG_OBJECT (sink, "stopped"); |
609 | | } |
610 | | #endif |
611 | |
|
612 | 0 | return TRUE; |
613 | 0 | } |
614 | | |
615 | | static guint |
616 | | gst_audio_sink_ring_buffer_delay (GstAudioRingBuffer * buf) |
617 | 0 | { |
618 | 0 | GstAudioSink *sink; |
619 | 0 | GstAudioSinkClass *csink; |
620 | 0 | guint res = 0; |
621 | |
|
622 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
623 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
624 | |
|
625 | 0 | if (csink->delay) |
626 | 0 | res = csink->delay (sink); |
627 | |
|
628 | 0 | return res; |
629 | 0 | } |
630 | | |
631 | | static void |
632 | | gst_audio_sink_ring_buffer_clear_all (GstAudioRingBuffer * buf) |
633 | 0 | { |
634 | 0 | GstAudioSink *sink; |
635 | 0 | GstAudioSinkClass *csink; |
636 | |
|
637 | 0 | sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf)); |
638 | 0 | csink = GST_AUDIO_SINK_GET_CLASS (sink); |
639 | |
|
640 | 0 | if (csink->extension->clear_all) { |
641 | 0 | GST_DEBUG_OBJECT (sink, "clear all"); |
642 | 0 | csink->extension->clear_all (sink); |
643 | 0 | } |
644 | | |
645 | | /* chain up to the parent implementation */ |
646 | 0 | ring_parent_class->clear_all (buf); |
647 | 0 | } |
648 | | |
649 | | /* AudioSink signals and args */ |
650 | | enum |
651 | | { |
652 | | /* FILL ME */ |
653 | | LAST_SIGNAL |
654 | | }; |
655 | | |
656 | | enum |
657 | | { |
658 | | ARG_0, |
659 | | }; |
660 | | |
661 | | #define _do_init \ |
662 | | GST_DEBUG_CATEGORY_INIT (gst_audio_sink_debug, "audiosink", 0, "audiosink element"); \ |
663 | | g_type_add_class_private (g_define_type_id, \ |
664 | | sizeof (GstAudioSinkClassExtension)); |
665 | 0 | #define gst_audio_sink_parent_class parent_class |
666 | 0 | G_DEFINE_TYPE_WITH_CODE (GstAudioSink, gst_audio_sink, |
667 | 0 | GST_TYPE_AUDIO_BASE_SINK, _do_init); |
668 | 0 |
|
669 | 0 | static GstAudioRingBuffer *gst_audio_sink_create_ringbuffer (GstAudioBaseSink * |
670 | 0 | sink); |
671 | 0 |
|
672 | 0 | static void |
673 | 0 | gst_audio_sink_class_init (GstAudioSinkClass * klass) |
674 | 0 | { |
675 | 0 | GstAudioBaseSinkClass *gstaudiobasesink_class; |
676 | |
|
677 | 0 | gstaudiobasesink_class = (GstAudioBaseSinkClass *) klass; |
678 | |
|
679 | 0 | gstaudiobasesink_class->create_ringbuffer = |
680 | 0 | GST_DEBUG_FUNCPTR (gst_audio_sink_create_ringbuffer); |
681 | |
|
682 | 0 | g_type_class_ref (GST_TYPE_AUDIO_SINK_RING_BUFFER); |
683 | |
|
684 | 0 | klass->extension = G_TYPE_CLASS_GET_PRIVATE (klass, |
685 | 0 | GST_TYPE_AUDIO_SINK, GstAudioSinkClassExtension); |
686 | 0 | } |
687 | | |
688 | | static void |
689 | | gst_audio_sink_init (GstAudioSink * audiosink) |
690 | 0 | { |
691 | 0 | } |
692 | | |
693 | | static GstAudioRingBuffer * |
694 | | gst_audio_sink_create_ringbuffer (GstAudioBaseSink * sink) |
695 | 0 | { |
696 | 0 | GstAudioRingBuffer *buffer; |
697 | |
|
698 | 0 | GST_DEBUG_OBJECT (sink, "creating ringbuffer"); |
699 | 0 | buffer = g_object_new (GST_TYPE_AUDIO_SINK_RING_BUFFER, NULL); |
700 | 0 | GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer); |
701 | |
|
702 | 0 | return buffer; |
703 | 0 | } |