2 * Copyright (C) <2005> Edgard Lima <edgard.lima@indt.org.br>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more
19 #include "sdlaudiosink.h"
21 #include <SDL_byteorder.h>
26 GST_DEBUG_CATEGORY_EXTERN (sdl_debug);
27 #define GST_CAT_DEFAULT sdl_debug
29 static void gst_sdlaudio_sink_dispose (GObject * object);
31 static GstCaps *gst_sdlaudio_sink_getcaps (GstBaseSink * bsink);
33 static gboolean gst_sdlaudio_sink_open (GstAudioSink * asink);
34 static gboolean gst_sdlaudio_sink_close (GstAudioSink * asink);
35 static gboolean gst_sdlaudio_sink_prepare (GstAudioSink * asink,
36 GstRingBufferSpec * spec);
37 static gboolean gst_sdlaudio_sink_unprepare (GstAudioSink * asink);
38 static guint gst_sdlaudio_sink_write (GstAudioSink * asink, gpointer data,
42 static guint gst_sdlaudio_sink_delay (GstAudioSink * asink);
43 static void gst_sdlaudio_sink_reset (GstAudioSink * asink);
47 /* SdlaudioSink signals and args */
53 #define SEMAPHORE_INIT(s,f) \
55 s.cond = g_cond_new(); \
56 s.mutex = g_mutex_new(); \
60 #define SEMAPHORE_CLOSE(s) \
63 g_cond_free(s.cond); \
67 g_mutex_free(s.mutex); \
72 #define SEMAPHORE_UP(s) \
75 g_mutex_lock(s.mutex); \
77 g_mutex_unlock(s.mutex); \
78 g_cond_signal(s.cond); \
81 #define SEMAPHORE_DOWN(s, e) \
85 g_mutex_lock(s.mutex); \
88 g_mutex_unlock(s.mutex); \
91 g_cond_wait(s.cond,s.mutex); \
94 s.mutexflag = FALSE; \
95 g_mutex_unlock(s.mutex); \
98 g_mutex_unlock(s.mutex); \
103 static GstStaticPadTemplate sdlaudiosink_sink_factory =
104 GST_STATIC_PAD_TEMPLATE ("sink",
107 GST_STATIC_CAPS ("audio/x-raw-int, "
108 "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
109 "signed = (boolean) { TRUE, FALSE }, "
112 "rate = (int) [ 1, MAX ], "
113 "channels = (int) [ 1, 2 ]; "
115 "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
116 "signed = (boolean) { TRUE, FALSE }, "
119 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
122 GST_BOILERPLATE (GstSDLAudioSink, gst_sdlaudio_sink, GstAudioSink,
123 GST_TYPE_AUDIO_SINK);
126 gst_sdlaudio_sink_dispose (GObject * object)
128 GstSDLAudioSink *sdlaudiosink = GST_SDLAUDIOSINK (object);
130 SEMAPHORE_CLOSE (sdlaudiosink->semB);
132 SEMAPHORE_CLOSE (sdlaudiosink->semA);
134 if (sdlaudiosink->buffer) {
135 g_free (sdlaudiosink->buffer);
138 G_OBJECT_CLASS (parent_class)->dispose (object);
143 gst_sdlaudio_sink_base_init (gpointer g_class)
145 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
147 gst_element_class_set_details_simple (element_class, "SDL audio sink",
149 "Output to a sound card via SDLAUDIO",
150 "Edgard Lima <edgard.lima@indt.org.br>");
152 gst_element_class_add_static_pad_template (element_class,
153 &sdlaudiosink_sink_factory);
157 gst_sdlaudio_sink_class_init (GstSDLAudioSinkClass * klass)
159 GObjectClass *gobject_class;
160 GstBaseSinkClass *gstbasesink_class;
161 GstAudioSinkClass *gstaudiosink_class;
163 gobject_class = (GObjectClass *) klass;
164 gstbasesink_class = (GstBaseSinkClass *) klass;
165 gstaudiosink_class = (GstAudioSinkClass *) klass;
167 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_sdlaudio_sink_dispose);
169 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_sdlaudio_sink_getcaps);
171 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_sdlaudio_sink_open);
172 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_sdlaudio_sink_close);
173 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_sdlaudio_sink_prepare);
174 gstaudiosink_class->unprepare =
175 GST_DEBUG_FUNCPTR (gst_sdlaudio_sink_unprepare);
176 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_sdlaudio_sink_write);
179 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_sdlaudio_sink_delay);
180 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_sdlaudio_sink_reset);
186 gst_sdlaudio_sink_init (GstSDLAudioSink * sdlaudiosink,
187 GstSDLAudioSinkClass * g_class)
189 GST_DEBUG ("initializing sdlaudiosink");
191 memset (&sdlaudiosink->fmt, 0, sizeof (SDL_AudioSpec));
193 sdlaudiosink->buffer = NULL;
194 sdlaudiosink->eos = FALSE;
196 SEMAPHORE_INIT (sdlaudiosink->semA, TRUE);
198 SEMAPHORE_INIT (sdlaudiosink->semB, FALSE);
203 gst_sdlaudio_sink_getcaps (GstBaseSink * bsink)
205 return gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
210 gst_sdlaudio_sink_get_format (GstBufferFormat fmt)
212 gint result = GST_UNKNOWN;
222 result = AUDIO_S16LSB;
225 result = AUDIO_S16MSB;
228 result = AUDIO_U16LSB;
231 result = AUDIO_U16MSB;
240 gst_sdlaudio_sink_open (GstAudioSink * asink)
242 GstSDLAudioSink *sdlaudio;
244 sdlaudio = GST_SDLAUDIOSINK (asink);
246 if (SDL_Init (SDL_INIT_AUDIO) < 0) {
254 GST_ELEMENT_ERROR (sdlaudio, LIBRARY, INIT,
255 ("Unable to init SDL: %s\n", SDL_GetError ()), (NULL));
261 gst_sdlaudio_sink_close (GstAudioSink * asink)
263 GstSDLAudioSink *sdlaudio = GST_SDLAUDIOSINK (asink);
265 sdlaudio->eos = TRUE;
266 SEMAPHORE_UP (sdlaudio->semA);
267 SEMAPHORE_UP (sdlaudio->semB);
268 SDL_QuitSubSystem (SDL_INIT_AUDIO);
273 gst_sdlaudio_sink_write (GstAudioSink * asink, gpointer data, guint length)
275 GstSDLAudioSink *sdlaudio = GST_SDLAUDIOSINK (asink);
277 if (sdlaudio->fmt.size != length) {
278 GST_ERROR ("ring buffer segment length (%u) != sdl buffer len (%u)", length,
282 SEMAPHORE_DOWN (sdlaudio->semA, sdlaudio->eos);
285 memcpy (sdlaudio->buffer, data, length);
287 SEMAPHORE_UP (sdlaudio->semB);
289 return sdlaudio->fmt.size;
294 mixaudio (void *unused, Uint8 * stream, int len)
296 GstSDLAudioSink *sdlaudio;
298 sdlaudio = GST_SDLAUDIOSINK (unused);
300 if (sdlaudio->fmt.size != len) {
301 GST_ERROR ("fmt buffer len (%u) != sdl callback len (%d)",
302 sdlaudio->fmt.size, len);
305 SEMAPHORE_DOWN (sdlaudio->semB, sdlaudio->eos);
308 SDL_MixAudio (stream, sdlaudio->buffer, sdlaudio->fmt.size,
311 SEMAPHORE_UP (sdlaudio->semA);
316 gst_sdlaudio_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
318 GstSDLAudioSink *sdlaudio;
321 sdlaudio = GST_SDLAUDIOSINK (asink);
323 sdlaudio->fmt.format = gst_sdlaudio_sink_get_format (spec->format);
324 if (sdlaudio->fmt.format == 0)
327 if (spec->width != 16 && spec->width != 8)
330 sdlaudio->fmt.freq = spec->rate;
331 sdlaudio->fmt.channels = spec->channels;
332 sdlaudio->fmt.samples =
333 spec->segsize / (spec->channels * ((sdlaudio->fmt.format & 0xFF) >> 3));
334 sdlaudio->fmt.callback = mixaudio;
335 sdlaudio->fmt.userdata = sdlaudio;
337 GST_DEBUG ("set segsize: %d, segtotal: %d, samples: %d", spec->segsize,
338 spec->segtotal, sdlaudio->fmt.samples);
340 while (sdlaudio->fmt.samples) {
341 sdlaudio->fmt.samples >>= 1;
345 sdlaudio->fmt.samples = 1;
346 sdlaudio->fmt.samples <<= power2;
348 GST_DEBUG ("set segsize: %d, segtotal: %d, samples: %d", spec->segsize,
349 spec->segtotal, sdlaudio->fmt.samples);
351 if (SDL_OpenAudio (&sdlaudio->fmt, NULL) < 0) {
355 spec->segsize = sdlaudio->fmt.size;
357 sdlaudio->buffer = g_malloc (sdlaudio->fmt.size);
358 memset (sdlaudio->buffer, sdlaudio->fmt.silence, sdlaudio->fmt.size);
360 GST_DEBUG ("set segsize: %d, segtotal: %d, samples: %d", spec->segsize,
361 spec->segtotal, sdlaudio->fmt.samples);
363 spec->bytes_per_sample =
364 spec->channels * ((sdlaudio->fmt.format & 0xFF) >> 3);
365 memset (spec->silence_sample, sdlaudio->fmt.silence, spec->bytes_per_sample);
373 GST_ELEMENT_ERROR (sdlaudio, RESOURCE, OPEN_READ,
374 ("Unable to open audio: %s", SDL_GetError ()), (NULL));
379 GST_ELEMENT_ERROR (sdlaudio, RESOURCE, OPEN_READ,
380 ("Unable to get format %d", spec->format), (NULL));
385 GST_ELEMENT_ERROR (sdlaudio, RESOURCE, OPEN_READ,
386 ("unexpected width %d", spec->width), (NULL));
392 gst_sdlaudio_sink_unprepare (GstAudioSink * asink)
400 if (!gst_sdlaudio_sink_close (asink))
403 if (!gst_sdlaudio_sink_open (asink))
410 GST_DEBUG ("Could not close the audio device");
415 GST_DEBUG ("Could not reopen the audio device");
424 gst_sdlaudio_sink_delay (GstAudioSink * asink)
426 GstSDLAudioSink *sdlaudio;
428 sdlaudio = GST_SDLAUDIOSINK (asink);
434 gst_sdlaudio_sink_reset (GstAudioSink * asink)