upload tizen1.0 source
[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_pad_template (element_class,
146       gst_static_pad_template_get (&sink_factory));
147
148   gst_element_class_set_details_simple (element_class, "Audio Sink (OSX)",
149       "Sink/Audio",
150       "Output to a sound card in OS X",
151       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
152 }
153
154 static void
155 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
156 {
157   GObjectClass *gobject_class;
158   GstElementClass *gstelement_class;
159   GstBaseSinkClass *gstbasesink_class;
160   GstBaseAudioSinkClass *gstbaseaudiosink_class;
161
162   gobject_class = (GObjectClass *) klass;
163   gstelement_class = (GstElementClass *) klass;
164   gstbasesink_class = (GstBaseSinkClass *) klass;
165   gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
166
167   parent_class = g_type_class_peek_parent (klass);
168
169   gobject_class->set_property = gst_osx_audio_sink_set_property;
170   gobject_class->get_property = gst_osx_audio_sink_get_property;
171
172   g_object_class_install_property (gobject_class, ARG_DEVICE,
173       g_param_spec_int ("device", "Device ID", "Device ID of output device",
174           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175
176   g_object_class_install_property (gobject_class, ARG_VOLUME,
177       g_param_spec_double ("volume", "Volume", "Volume of this stream",
178           0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179
180   gstbaseaudiosink_class->create_ringbuffer =
181       GST_DEBUG_FUNCPTR (gst_osx_audio_sink_create_ringbuffer);
182 }
183
184 static void
185 gst_osx_audio_sink_init (GstOsxAudioSink * sink, GstOsxAudioSinkClass * gclass)
186 {
187   GST_DEBUG ("Initialising object");
188
189   sink->device_id = kAudioDeviceUnknown;
190   sink->volume = DEFAULT_VOLUME;
191 }
192
193 static void
194 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
195     const GValue * value, GParamSpec * pspec)
196 {
197   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
198
199   switch (prop_id) {
200     case ARG_DEVICE:
201       sink->device_id = g_value_get_int (value);
202       break;
203     case ARG_VOLUME:
204       sink->volume = g_value_get_double (value);
205       gst_osx_audio_sink_set_volume (sink);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210   }
211 }
212
213 static void
214 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
215     GValue * value, GParamSpec * pspec)
216 {
217   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
218   switch (prop_id) {
219     case ARG_DEVICE:
220       g_value_set_int (value, sink->device_id);
221       break;
222     case ARG_VOLUME:
223       g_value_set_double (value, sink->volume);
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227       break;
228   }
229 }
230
231 static GstRingBuffer *
232 gst_osx_audio_sink_create_ringbuffer (GstBaseAudioSink * sink)
233 {
234   GstOsxAudioSink *osxsink;
235   GstOsxRingBuffer *ringbuffer;
236
237   osxsink = GST_OSX_AUDIO_SINK (sink);
238
239   gst_osx_audio_sink_select_device (osxsink);
240
241   GST_DEBUG ("Creating ringbuffer");
242   ringbuffer = g_object_new (GST_TYPE_OSX_RING_BUFFER, NULL);
243   GST_DEBUG ("osx sink 0x%p element 0x%p  ioproc 0x%p", osxsink,
244       GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink),
245       (void *) gst_osx_audio_sink_io_proc);
246
247   gst_osx_audio_sink_set_volume (osxsink);
248
249   ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink);
250   ringbuffer->device_id = osxsink->device_id;
251
252   return GST_RING_BUFFER (ringbuffer);
253 }
254
255 /* HALOutput AudioUnit will request fairly arbitrarily-sized chunks of data,
256  * not of a fixed size. So, we keep track of where in the current ringbuffer
257  * segment we are, and only advance the segment once we've read the whole
258  * thing */
259 static OSStatus
260 gst_osx_audio_sink_io_proc (GstOsxRingBuffer * buf,
261     AudioUnitRenderActionFlags * ioActionFlags,
262     const AudioTimeStamp * inTimeStamp,
263     UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
264 {
265   guint8 *readptr;
266   gint readseg;
267   gint len;
268   gint remaining = bufferList->mBuffers[0].mDataByteSize;
269   gint offset = 0;
270
271   while (remaining) {
272     if (!gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf),
273             &readseg, &readptr, &len))
274       return 0;
275
276     len -= buf->segoffset;
277
278     if (len > remaining)
279       len = remaining;
280
281     memcpy ((char *) bufferList->mBuffers[0].mData + offset,
282         readptr + buf->segoffset, len);
283
284     buf->segoffset += len;
285     offset += len;
286     remaining -= len;
287
288     if ((gint) buf->segoffset == GST_RING_BUFFER (buf)->spec.segsize) {
289       /* clear written samples */
290       gst_ring_buffer_clear (GST_RING_BUFFER (buf), readseg);
291
292       /* we wrote one segment */
293       gst_ring_buffer_advance (GST_RING_BUFFER (buf), 1);
294
295       buf->segoffset = 0;
296     }
297   }
298   return 0;
299 }
300
301 static void
302 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
303 {
304   GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
305
306   iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
307 }
308
309 static void
310 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
311 {
312   if (!sink->audiounit)
313     return;
314
315   AudioUnitSetParameter (sink->audiounit, kHALOutputParam_Volume,
316       kAudioUnitScope_Global, 0, (float) sink->volume, 0);
317 }
318
319 static void
320 gst_osx_audio_sink_select_device (GstOsxAudioSink * osxsink)
321 {
322   OSStatus status;
323   UInt32 propertySize;
324
325   if (osxsink->device_id == kAudioDeviceUnknown) {
326     /* If no specific device has been selected by the user, then pick the
327      * default device */
328     GST_DEBUG_OBJECT (osxsink, "Selecting device for OSXAudioSink");
329     propertySize = sizeof (osxsink->device_id);
330     status =
331         AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice,
332         &propertySize, &osxsink->device_id);
333
334     if (status) {
335       GST_WARNING_OBJECT (osxsink,
336           "AudioHardwareGetProperty returned %d", (int) status);
337     } else {
338       GST_DEBUG_OBJECT (osxsink, "AudioHardwareGetProperty returned 0");
339     }
340
341     if (osxsink->device_id == kAudioDeviceUnknown) {
342       GST_WARNING_OBJECT (osxsink,
343           "AudioHardwareGetProperty: device_id is kAudioDeviceUnknown");
344     }
345
346     GST_DEBUG_OBJECT (osxsink, "AudioHardwareGetProperty: device_id is %lu",
347         (long) osxsink->device_id);
348   }
349 }