1 /*
2  * GStreamer
3  * Copyright (C) 2005,2006 Zaheer Abbas Merali <zaheerabbas at merali dot org>
4  * Copyright (C) 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., 51 Franklin St, Fifth Floor,
42  * Boston, MA 02110-1301, USA.
43  */
44 
45 /**
46  * SECTION:element-osxaudiosrc
47  *
48  * This element captures raw audio samples using the CoreAudio api.
49  *
50  * <refsect2>
51  * <title>Example launch line</title>
52  * |[
53  * gst-launch-1.0 osxaudiosrc ! wavenc ! filesink location=audio.wav
54  * ]|
55  * </refsect2>
56  */
57 
58 #ifdef HAVE_CONFIG_H
59 #  include <config.h>
60 #endif
61 
62 #include <gst/gst.h>
63 #include "gstosxaudiosrc.h"
64 #include "gstosxaudioelement.h"
65 
66 GST_DEBUG_CATEGORY_STATIC (osx_audiosrc_debug);
67 #define GST_CAT_DEFAULT osx_audiosrc_debug
68 
69 /* Filter signals and args */
70 enum
71 {
72   /* FILL ME */
73   LAST_SIGNAL
74 };
75 
76 enum
77 {
78   ARG_0,
79   ARG_DEVICE
80 };
81 
82 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
83     GST_PAD_SRC,
84     GST_PAD_ALWAYS,
85     GST_STATIC_CAPS (GST_OSX_AUDIO_SRC_CAPS)
86     );
87 
88 static void gst_osx_audio_src_set_property (GObject * object, guint prop_id,
89     const GValue * value, GParamSpec * pspec);
90 static void gst_osx_audio_src_get_property (GObject * object, guint prop_id,
91     GValue * value, GParamSpec * pspec);
92 
93 static GstStateChangeReturn
94 gst_osx_audio_src_change_state (GstElement * element,
95     GstStateChange transition);
96 
97 static GstCaps *gst_osx_audio_src_get_caps (GstBaseSrc * src, GstCaps * filter);
98 
99 static GstAudioRingBuffer *gst_osx_audio_src_create_ringbuffer (GstAudioBaseSrc
100     * src);
101 static void gst_osx_audio_src_osxelement_init (gpointer g_iface,
102     gpointer iface_data);
103 static OSStatus gst_osx_audio_src_io_proc (GstOsxAudioRingBuffer * buf,
104     AudioUnitRenderActionFlags * ioActionFlags,
105     const AudioTimeStamp * inTimeStamp, UInt32 inBusNumber,
106     UInt32 inNumberFrames, AudioBufferList * bufferList);
107 
108 static void
gst_osx_audio_src_do_init(GType type)109 gst_osx_audio_src_do_init (GType type)
110 {
111   static const GInterfaceInfo osxelement_info = {
112     gst_osx_audio_src_osxelement_init,
113     NULL,
114     NULL
115   };
116 
117   GST_DEBUG_CATEGORY_INIT (osx_audiosrc_debug, "osxaudiosrc", 0,
118       "OSX Audio Src");
119   g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
120       &osxelement_info);
121 }
122 
123 #define gst_osx_audio_src_parent_class parent_class
124 G_DEFINE_TYPE_WITH_CODE (GstOsxAudioSrc, gst_osx_audio_src,
125     GST_TYPE_AUDIO_BASE_SRC, gst_osx_audio_src_do_init (g_define_type_id));
126 
127 static void
gst_osx_audio_src_class_init(GstOsxAudioSrcClass * klass)128 gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass)
129 {
130   GObjectClass *gobject_class;
131   GstElementClass *gstelement_class;
132   GstBaseSrcClass *gstbasesrc_class;
133   GstAudioBaseSrcClass *gstaudiobasesrc_class;
134 
135   gobject_class = (GObjectClass *) klass;
136   gstelement_class = (GstElementClass *) klass;
137   gstbasesrc_class = (GstBaseSrcClass *) klass;
138   gstaudiobasesrc_class = (GstAudioBaseSrcClass *) klass;
139 
140   gobject_class->set_property = gst_osx_audio_src_set_property;
141   gobject_class->get_property = gst_osx_audio_src_get_property;
142 
143   gstelement_class->change_state =
144       GST_DEBUG_FUNCPTR (gst_osx_audio_src_change_state);
145 
146   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_caps);
147 
148   g_object_class_install_property (gobject_class, ARG_DEVICE,
149       g_param_spec_int ("device", "Device ID", "Device ID of input device",
150           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 
152   gstaudiobasesrc_class->create_ringbuffer =
153       GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer);
154 
155   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
156 
157   gst_element_class_set_static_metadata (gstelement_class, "Audio Source (OSX)",
158       "Source/Audio",
159       "Input from a sound card in OS X",
160       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
161 }
162 
163 static void
gst_osx_audio_src_init(GstOsxAudioSrc * src)164 gst_osx_audio_src_init (GstOsxAudioSrc * src)
165 {
166   gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
167 
168   src->device_id = kAudioDeviceUnknown;
169 }
170 
171 static void
gst_osx_audio_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)172 gst_osx_audio_src_set_property (GObject * object, guint prop_id,
173     const GValue * value, GParamSpec * pspec)
174 {
175   GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
176 
177   switch (prop_id) {
178     case ARG_DEVICE:
179       src->device_id = g_value_get_int (value);
180       break;
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183       break;
184   }
185 }
186 
187 static void
gst_osx_audio_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)188 gst_osx_audio_src_get_property (GObject * object, guint prop_id,
189     GValue * value, GParamSpec * pspec)
190 {
191   GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
192 
193   switch (prop_id) {
194     case ARG_DEVICE:
195       g_value_set_int (value, src->device_id);
196       break;
197     default:
198       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199       break;
200   }
201 }
202 
203 static GstStateChangeReturn
gst_osx_audio_src_change_state(GstElement * element,GstStateChange transition)204 gst_osx_audio_src_change_state (GstElement * element, GstStateChange transition)
205 {
206   GstOsxAudioSrc *osxsrc = GST_OSX_AUDIO_SRC (element);
207   GstOsxAudioRingBuffer *ringbuffer;
208   GstStateChangeReturn ret;
209 
210   switch (transition) {
211     default:
212       break;
213   }
214 
215   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
216   if (ret == GST_STATE_CHANGE_FAILURE)
217     goto out;
218 
219   switch (transition) {
220     case GST_STATE_CHANGE_NULL_TO_READY:
221       /* The device is open now, so fix our device_id if it changed */
222       ringbuffer =
223           GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SRC (osxsrc)->ringbuffer);
224       if (ringbuffer->core_audio->device_id != osxsrc->device_id) {
225         osxsrc->device_id = ringbuffer->core_audio->device_id;
226         g_object_notify (G_OBJECT (osxsrc), "device");
227       }
228       break;
229 
230     default:
231       break;
232   }
233 
234 out:
235   return ret;
236 }
237 
238 static GstCaps *
gst_osx_audio_src_get_caps(GstBaseSrc * src,GstCaps * filter)239 gst_osx_audio_src_get_caps (GstBaseSrc * src, GstCaps * filter)
240 {
241   GstOsxAudioSrc *osxsrc;
242   GstAudioRingBuffer *buf;
243   GstOsxAudioRingBuffer *osxbuf;
244   GstCaps *caps, *filtered_caps;
245 
246   osxsrc = GST_OSX_AUDIO_SRC (src);
247 
248   GST_OBJECT_LOCK (osxsrc);
249   buf = GST_AUDIO_BASE_SRC (src)->ringbuffer;
250   if (buf)
251     gst_object_ref (buf);
252   GST_OBJECT_UNLOCK (osxsrc);
253 
254   if (!buf) {
255     GST_DEBUG_OBJECT (src, "no ring buffer, using template caps");
256     return GST_BASE_SRC_CLASS (parent_class)->get_caps (src, filter);
257   }
258 
259   osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf);
260 
261   /* protect against cached_caps going away */
262   GST_OBJECT_LOCK (buf);
263 
264   if (osxbuf->core_audio->cached_caps_valid) {
265     GST_LOG_OBJECT (src, "Returning cached caps");
266     caps = gst_caps_ref (osxbuf->core_audio->cached_caps);
267   } else if (buf->open) {
268     GstCaps *template_caps;
269 
270     /* Get template caps */
271     template_caps =
272         gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SRC_PAD (osxsrc));
273 
274     /* Device is open, let's probe its caps */
275     caps = gst_core_audio_probe_caps (osxbuf->core_audio, template_caps);
276     gst_caps_replace (&osxbuf->core_audio->cached_caps, caps);
277 
278     gst_caps_unref (template_caps);
279   } else {
280     GST_DEBUG_OBJECT (src, "ring buffer not open, using template caps");
281     caps = GST_BASE_SRC_CLASS (parent_class)->get_caps (src, NULL);
282   }
283 
284   GST_OBJECT_UNLOCK (buf);
285 
286   gst_object_unref (buf);
287 
288   if (!caps)
289     return NULL;
290 
291   if (!filter)
292     return caps;
293 
294   /* Take care of filtered caps */
295   filtered_caps =
296       gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
297   gst_caps_unref (caps);
298   return filtered_caps;
299 }
300 
301 static GstAudioRingBuffer *
gst_osx_audio_src_create_ringbuffer(GstAudioBaseSrc * src)302 gst_osx_audio_src_create_ringbuffer (GstAudioBaseSrc * src)
303 {
304   GstOsxAudioSrc *osxsrc;
305   GstOsxAudioRingBuffer *ringbuffer;
306 
307   osxsrc = GST_OSX_AUDIO_SRC (src);
308 
309   GST_DEBUG_OBJECT (osxsrc, "Creating ringbuffer");
310   ringbuffer = g_object_new (GST_TYPE_OSX_AUDIO_RING_BUFFER, NULL);
311   GST_DEBUG_OBJECT (osxsrc, "osx src 0x%p element 0x%p  ioproc 0x%p", osxsrc,
312       GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc),
313       (void *) gst_osx_audio_src_io_proc);
314 
315   ringbuffer->core_audio->element =
316       GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc);
317   ringbuffer->core_audio->is_src = TRUE;
318 
319   /* By default the coreaudio instance created by the ringbuffer
320    * has device_id==kAudioDeviceUnknown. The user might have
321    * selected a different one here
322    */
323   if (ringbuffer->core_audio->device_id != osxsrc->device_id)
324     ringbuffer->core_audio->device_id = osxsrc->device_id;
325 
326   return GST_AUDIO_RING_BUFFER (ringbuffer);
327 }
328 
329 static OSStatus
gst_osx_audio_src_io_proc(GstOsxAudioRingBuffer * buf,AudioUnitRenderActionFlags * ioActionFlags,const AudioTimeStamp * inTimeStamp,UInt32 inBusNumber,UInt32 inNumberFrames,AudioBufferList * bufferList)330 gst_osx_audio_src_io_proc (GstOsxAudioRingBuffer * buf,
331     AudioUnitRenderActionFlags * ioActionFlags,
332     const AudioTimeStamp * inTimeStamp,
333     UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
334 {
335   OSStatus status;
336   guint8 *writeptr;
337   gint writeseg;
338   gint len;
339   gint remaining;
340   UInt32 n;
341   gint offset = 0;
342 
343   /* Previous invoke of AudioUnitRender changed mDataByteSize into
344    * number of bytes actually read. Reset the members. */
345   for (n = 0; n < buf->core_audio->recBufferList->mNumberBuffers; ++n) {
346     buf->core_audio->recBufferList->mBuffers[n].mDataByteSize =
347         buf->core_audio->recBufferSize;
348   }
349 
350   status = AudioUnitRender (buf->core_audio->audiounit, ioActionFlags,
351       inTimeStamp, inBusNumber, inNumberFrames, buf->core_audio->recBufferList);
352 
353   if (status) {
354     GST_WARNING_OBJECT (buf, "AudioUnitRender returned %d", (int) status);
355     return status;
356   }
357 
358   /* TODO: To support non-interleaved audio, go over all mBuffers,
359    *       not just the first one. */
360 
361   remaining = buf->core_audio->recBufferList->mBuffers[0].mDataByteSize;
362 
363   while (remaining) {
364     if (!gst_audio_ring_buffer_prepare_read (GST_AUDIO_RING_BUFFER (buf),
365             &writeseg, &writeptr, &len))
366       return 0;
367 
368     len -= buf->segoffset;
369 
370     if (len > remaining)
371       len = remaining;
372 
373     memcpy (writeptr + buf->segoffset,
374         (char *) buf->core_audio->recBufferList->mBuffers[0].mData + offset,
375         len);
376 
377     buf->segoffset += len;
378     offset += len;
379     remaining -= len;
380 
381     if ((gint) buf->segoffset == GST_AUDIO_RING_BUFFER (buf)->spec.segsize) {
382       /* we wrote one segment */
383       gst_audio_ring_buffer_advance (GST_AUDIO_RING_BUFFER (buf), 1);
384 
385       buf->segoffset = 0;
386     }
387   }
388   return 0;
389 }
390 
391 static void
gst_osx_audio_src_osxelement_init(gpointer g_iface,gpointer iface_data)392 gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data)
393 {
394   GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
395 
396   iface->io_proc = (AURenderCallback) gst_osx_audio_src_io_proc;
397 }
398