Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / sys / osxaudio / gstosxaudiosink.c
1 /*
2  * GStreamer
3  * Copyright (C) 2005,2006 Zaheer Abbas Merali <zaheerabbas at merali dot org>
4  * Copyright (C) 2007,2008 Pioneers of the Inevitable <songbird@songbirdnest.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Alternatively, the contents of this file may be used under the
25  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
26  * which case the following provisions apply instead of the ones
27  * mentioned above:
28  *
29  * This library is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU Library General Public
31  * License as published by the Free Software Foundation; either
32  * version 2 of the License, or (at your option) any later version.
33  *
34  * This library is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37  * Library General Public License for more details.
38  *
39  * You should have received a copy of the GNU Library General Public
40  * License along with this library; if not, write to the
41  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
42  * Boston, MA 02111-1307, USA.
43  *
44  * The development of this code was made possible due to the involvement of
45  * Pioneers of the Inevitable, the creators of the Songbird Music player
46  *
47  */
48
49 /**
50  * SECTION:element-osxaudiosink
51  *
52  * This element renders raw audio samples using the CoreAudio api.
53  *
54  * <refsect2>
55  * <title>Example pipelines</title>
56  * |[
57  * gst-launch filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! osxaudiosink
58  * ]| Play an Ogg/Vorbis file.
59  * </refsect2>
60  *
61  * Last reviewed on 2006-03-01 (0.10.4)
62  */
63
64 #ifdef HAVE_CONFIG_H
65 #  include <config.h>
66 #endif
67
68 #include <gst/gst.h>
69 #include <CoreAudio/CoreAudio.h>
70 #include <CoreAudio/AudioHardware.h>
71 #include "gstosxaudiosink.h"
72 #include "gstosxaudioelement.h"
73
74 GST_DEBUG_CATEGORY_STATIC (osx_audiosink_debug);
75 #define GST_CAT_DEFAULT osx_audiosink_debug
76
77 /* Filter signals and args */
78 enum
79 {
80   /* FILL ME */
81   LAST_SIGNAL
82 };
83
84 enum
85 {
86   ARG_0,
87   ARG_DEVICE,
88   ARG_VOLUME
89 };
90
91 #define DEFAULT_VOLUME 1.0
92
93 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
94     GST_PAD_SINK,
95     GST_PAD_ALWAYS,
96     GST_STATIC_CAPS ("audio/x-raw-float, "
97         "endianness = (int) {" G_STRINGIFY (G_BYTE_ORDER) " }, "
98         "signed = (boolean) { TRUE }, "
99         "width = (int) 32, "
100         "depth = (int) 32, "
101         "rate = (int) [1, MAX], " "channels = (int) [1, MAX]")
102     );
103
104 static void gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
105     const GValue * value, GParamSpec * pspec);
106 static void gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
107     GValue * value, GParamSpec * pspec);
108
109 static GstRingBuffer *gst_osx_audio_sink_create_ringbuffer (GstBaseAudioSink *
110     sink);
111 static void gst_osx_audio_sink_osxelement_init (gpointer g_iface,
112     gpointer iface_data);
113 static void gst_osx_audio_sink_select_device (GstOsxAudioSink * osxsink);
114 static void gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink);
115
116 static OSStatus gst_osx_audio_sink_io_proc (GstOsxRingBuffer * buf,
117     AudioUnitRenderActionFlags * ioActionFlags,
118     const AudioTimeStamp * inTimeStamp,
119     UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList);
120
121 static void
122 gst_osx_audio_sink_do_init (GType type)
123 {
124   static const GInterfaceInfo osxelement_info = {
125     gst_osx_audio_sink_osxelement_init,
126     NULL,
127     NULL
128   };
129
130   GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0,
131       "OSX Audio Sink");
132   GST_DEBUG ("Adding static interface");
133   g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
134       &osxelement_info);
135 }
136
137 GST_BOILERPLATE_FULL (GstOsxAudioSink, gst_osx_audio_sink, GstBaseAudioSink,
138     GST_TYPE_BASE_AUDIO_SINK, gst_osx_audio_sink_do_init);
139
140 static void
141 gst_osx_audio_sink_base_init (gpointer g_class)
142 {
143   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
144
145   gst_element_class_add_static_pad_template (element_class, &sink_factory);
146
147   gst_element_class_set_details_simple (element_class, "Audio Sink (OSX)",
148       "Sink/Audio",
149       "Output to a sound card in OS X",
150       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
151 }
152
153 static void
154 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
155 {
156   GObjectClass *gobject_class;
157   GstElementClass *gstelement_class;
158   GstBaseSinkClass *gstbasesink_class;
159   GstBaseAudioSinkClass *gstbaseaudiosink_class;
160
161   gobject_class = (GObjectClass *) klass;
162   gstelement_class = (GstElementClass *) klass;
163   gstbasesink_class = (GstBaseSinkClass *) klass;
164   gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
165
166   parent_class = g_type_class_peek_parent (klass);
167
168   gobject_class->set_property = gst_osx_audio_sink_set_property;
169   gobject_class->get_property = gst_osx_audio_sink_get_property;
170
171   g_object_class_install_property (gobject_class, ARG_DEVICE,
172       g_param_spec_int ("device", "Device ID", "Device ID of output device",
173           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174
175   g_object_class_install_property (gobject_class, ARG_VOLUME,
176       g_param_spec_double ("volume", "Volume", "Volume of this stream",
177           0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178
179   gstbaseaudiosink_class->create_ringbuffer =
180       GST_DEBUG_FUNCPTR (gst_osx_audio_sink_create_ringbuffer);
181 }
182
183 static void
184 gst_osx_audio_sink_init (GstOsxAudioSink * sink, GstOsxAudioSinkClass * gclass)
185 {
186   GST_DEBUG ("Initialising object");
187
188   sink->device_id = kAudioDeviceUnknown;
189   sink->volume = DEFAULT_VOLUME;
190 }
191
192 static void
193 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
194     const GValue * value, GParamSpec * pspec)
195 {
196   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
197
198   switch (prop_id) {
199     case ARG_DEVICE:
200       sink->device_id = g_value_get_int (value);
201       break;
202     case ARG_VOLUME:
203       sink->volume = g_value_get_double (value);
204       gst_osx_audio_sink_set_volume (sink);
205       break;
206     default:
207       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208       break;
209   }
210 }
211
212 static void
213 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
214     GValue * value, GParamSpec * pspec)
215 {
216   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
217   switch (prop_id) {
218     case ARG_DEVICE:
219       g_value_set_int (value, sink->device_id);
220       break;
221     case ARG_VOLUME:
222       g_value_set_double (value, sink->volume);
223       break;
224     default:
225       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226       break;
227   }
228 }
229
230 static GstRingBuffer *
231 gst_osx_audio_sink_create_ringbuffer (GstBaseAudioSink * sink)
232 {
233   GstOsxAudioSink *osxsink;
234   GstOsxRingBuffer *ringbuffer;
235
236   osxsink = GST_OSX_AUDIO_SINK (sink);
237
238   gst_osx_audio_sink_select_device (osxsink);
239
240   GST_DEBUG ("Creating ringbuffer");
241   ringbuffer = g_object_new (GST_TYPE_OSX_RING_BUFFER, NULL);
242   GST_DEBUG ("osx sink 0x%p element 0x%p  ioproc 0x%p", osxsink,
243       GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink),
244       (void *) gst_osx_audio_sink_io_proc);
245
246   gst_osx_audio_sink_set_volume (osxsink);
247
248   ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink);
249   ringbuffer->device_id = osxsink->device_id;
250
251   return GST_RING_BUFFER (ringbuffer);
252 }
253
254 /* HALOutput AudioUnit will request fairly arbitrarily-sized chunks of data,
255  * not of a fixed size. So, we keep track of where in the current ringbuffer
256  * segment we are, and only advance the segment once we've read the whole
257  * thing */
258 static OSStatus
259 gst_osx_audio_sink_io_proc (GstOsxRingBuffer * buf,
260     AudioUnitRenderActionFlags * ioActionFlags,
261     const AudioTimeStamp * inTimeStamp,
262     UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
263 {
264   guint8 *readptr;
265   gint readseg;
266   gint len;
267   gint remaining = bufferList->mBuffers[0].mDataByteSize;
268   gint offset = 0;
269
270   while (remaining) {
271     if (!gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf),
272             &readseg, &readptr, &len))
273       return 0;
274
275     len -= buf->segoffset;
276
277     if (len > remaining)
278       len = remaining;
279
280     memcpy ((char *) bufferList->mBuffers[0].mData + offset,
281         readptr + buf->segoffset, len);
282
283     buf->segoffset += len;
284     offset += len;
285     remaining -= len;
286
287     if ((gint) buf->segoffset == GST_RING_BUFFER (buf)->spec.segsize) {
288       /* clear written samples */
289       gst_ring_buffer_clear (GST_RING_BUFFER (buf), readseg);
290
291       /* we wrote one segment */
292       gst_ring_buffer_advance (GST_RING_BUFFER (buf), 1);
293
294       buf->segoffset = 0;
295     }
296   }
297   return 0;
298 }
299
300 static void
301 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
302 {
303   GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
304
305   iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
306 }
307
308 static void
309 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
310 {
311   if (!sink->audiounit)
312     return;
313
314   AudioUnitSetParameter (sink->audiounit, kHALOutputParam_Volume,
315       kAudioUnitScope_Global, 0, (float) sink->volume, 0);
316 }
317
318 static void
319 gst_osx_audio_sink_select_device (GstOsxAudioSink * osxsink)
320 {
321   OSStatus status;
322   UInt32 propertySize;
323
324   if (osxsink->device_id == kAudioDeviceUnknown) {
325     /* If no specific device has been selected by the user, then pick the
326      * default device */
327     GST_DEBUG_OBJECT (osxsink, "Selecting device for OSXAudioSink");
328     propertySize = sizeof (osxsink->device_id);
329     status =
330         AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice,
331         &propertySize, &osxsink->device_id);
332
333     if (status) {
334       GST_WARNING_OBJECT (osxsink,
335           "AudioHardwareGetProperty returned %d", (int) status);
336     } else {
337       GST_DEBUG_OBJECT (osxsink, "AudioHardwareGetProperty returned 0");
338     }
339
340     if (osxsink->device_id == kAudioDeviceUnknown) {
341       GST_WARNING_OBJECT (osxsink,
342           "AudioHardwareGetProperty: device_id is kAudioDeviceUnknown");
343     }
344
345     GST_DEBUG_OBJECT (osxsink, "AudioHardwareGetProperty: device_id is %lu",
346         (long) osxsink->device_id);
347   }
348 }