Debian Codegraph @

sys/osxaudio/gstosxaudiosrc.c

raw
1 /*
2 * GStreamer
3 * Copyright (C) 2005,2006 Zaheer Abbas Merali <zaheerabbas at merali dot org>
4 * Copyright (C) 2008 Pioneers of the Inevitable <[email protected]>
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 * @title: osxaudiosrc
48 *
49 * This element captures raw audio samples using the CoreAudio api.
50 *
51 * ## Example launch line
52 * |[
53 * gst-launch-1.0 osxaudiosrc ! wavenc ! filesink location=audio.wav
54 * ]|
55 *
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 ARG_UNIQUE_ID,
81 ARG_CONFIGURE_SESSION,
82 };
83
84 #define DEFAULT_CONFIGURE_SESSION TRUE
85
86 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
87 GST_PAD_SRC,
88 GST_PAD_ALWAYS,
89 GST_STATIC_CAPS (GST_OSX_AUDIO_SRC_CAPS)
90 );
91
92 static void gst_osx_audio_src_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec);
94 static void gst_osx_audio_src_get_property (GObject * object, guint prop_id,
95 GValue * value, GParamSpec * pspec);
96 static void gst_osx_audio_src_finalize (GObject * object);
97
98 static GstStateChangeReturn
99 gst_osx_audio_src_change_state (GstElement * element,
100 GstStateChange transition);
101
102 static GstCaps *gst_osx_audio_src_get_caps (GstBaseSrc * src, GstCaps * filter);
103
104 static GstAudioRingBuffer *gst_osx_audio_src_create_ringbuffer (GstAudioBaseSrc
105 * src);
106 static void gst_osx_audio_src_osxelement_init (gpointer g_iface,
107 gpointer iface_data);
108 static OSStatus gst_osx_audio_src_io_proc (GstOsxAudioRingBuffer * buf,
109 AudioUnitRenderActionFlags * ioActionFlags,
110 const AudioTimeStamp * inTimeStamp, UInt32 inBusNumber,
111 UInt32 inNumberFrames, AudioBufferList * bufferList);
112
113 static void
114 gst_osx_audio_src_do_init (GType type)
115 {
116 static const GInterfaceInfo osxelement_info = {
117 gst_osx_audio_src_osxelement_init,
118 NULL,
119 NULL
120 };
121
122 GST_DEBUG_CATEGORY_INIT (osx_audiosrc_debug, "osxaudiosrc", 0,
123 "OSX Audio Src");
124 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
125 &osxelement_info);
126 }
127
128 #define gst_osx_audio_src_parent_class parent_class
129 G_DEFINE_TYPE_WITH_CODE (GstOsxAudioSrc, gst_osx_audio_src,
130 GST_TYPE_AUDIO_BASE_SRC, gst_osx_audio_src_do_init (g_define_type_id));
131 GST_ELEMENT_REGISTER_DEFINE (osxaudiosrc, "osxaudiosrc", GST_RANK_PRIMARY,
132 GST_TYPE_OSX_AUDIO_SRC);
133
134 static void
135 gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass)
136 {
137 GObjectClass *gobject_class;
138 GstElementClass *gstelement_class;
139 GstBaseSrcClass *gstbasesrc_class;
140 GstAudioBaseSrcClass *gstaudiobasesrc_class;
141
142 gobject_class = (GObjectClass *) klass;
143 gstelement_class = (GstElementClass *) klass;
144 gstbasesrc_class = (GstBaseSrcClass *) klass;
145 gstaudiobasesrc_class = (GstAudioBaseSrcClass *) klass;
146
147 gobject_class->set_property = gst_osx_audio_src_set_property;
148 gobject_class->get_property = gst_osx_audio_src_get_property;
149 gobject_class->finalize = gst_osx_audio_src_finalize;
150
151 gstelement_class->change_state =
152 GST_DEBUG_FUNCPTR (gst_osx_audio_src_change_state);
153
154 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_caps);
155
156 g_object_class_install_property (gobject_class, ARG_DEVICE,
157 g_param_spec_int ("device", "Device ID", "Device ID of input device",
158 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159
160 /**
161 * osxaudiosrc:unique-id
162 *
163 * Unique persistent ID for the input device
164 *
165 * Since: 1.26
166 */
167 g_object_class_install_property (gobject_class, ARG_UNIQUE_ID,
168 g_param_spec_string ("unique-id", "Unique ID",
169 "Unique persistent ID for the input device",
170 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171
172 #if !TARGET_OS_OSX
173 /**
174 * GstOsxAudioSrc:configure-session:
175 *
176 * Whether the app-wide AVAudioSession should be automatically set up for audio capture.
177 * This will set the category to AVAudioSessionCategoryPlayAndRecord and activate
178 * the session when the element goes to READY. No other settings will be changed.
179 *
180 * If your application needs to configure anything more than the category, set this to FALSE
181 * for all osxaudiosink/src instances and handle the AVAudioSession setup yourself.
182 *
183 * Since: 1.26
184 */
185 g_object_class_install_property (gobject_class, ARG_CONFIGURE_SESSION,
186 g_param_spec_boolean ("configure-session",
187 "Enable automatic AVAudioSession setup",
188 "Whether the app-wide AVAudioSession should be automatically configured for audio capture",
189 DEFAULT_CONFIGURE_SESSION,
190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 #endif
192
193 gstaudiobasesrc_class->create_ringbuffer =
194 GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer);
195
196 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
197
198 gst_element_class_set_static_metadata (gstelement_class,
199 "Audio Source (macOS)", "Source/Audio",
200 "Input from a sound card on macOS",
201 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
202 }
203
204 static void
205 gst_osx_audio_src_init (GstOsxAudioSrc * src)
206 {
207 gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
208
209 src->device_id = kAudioDeviceUnknown;
210 src->unique_id = NULL;
211
212 #if !TARGET_OS_OSX
213 src->configure_session = DEFAULT_CONFIGURE_SESSION;
214 #endif
215 }
216
217 static void
218 gst_osx_audio_src_finalize (GObject * object)
219 {
220 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
221 g_clear_pointer (&src->unique_id, g_free);
222 G_OBJECT_CLASS (parent_class)->finalize (object);
223 }
224
225 static void
226 gst_osx_audio_src_set_property (GObject * object, guint prop_id,
227 const GValue * value, GParamSpec * pspec)
228 {
229 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
230
231 switch (prop_id) {
232 case ARG_DEVICE:
233 src->device_id = g_value_get_int (value);
234 break;
235 case ARG_UNIQUE_ID:
236 src->unique_id = g_value_dup_string (value);
237 break;
238 #if !TARGET_OS_OSX
239 case ARG_CONFIGURE_SESSION:
240 src->configure_session = g_value_get_boolean (value);
241 break;
242 #endif
243 default:
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 break;
246 }
247 }
248
249 static void
250 gst_osx_audio_src_get_property (GObject * object, guint prop_id,
251 GValue * value, GParamSpec * pspec)
252 {
253 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
254
255 switch (prop_id) {
256 case ARG_DEVICE:
257 g_value_set_int (value, src->device_id);
258 break;
259 case ARG_UNIQUE_ID:
260 g_value_set_string (value, src->unique_id);
261 break;
262 #if !TARGET_OS_OSX
263 case ARG_CONFIGURE_SESSION:
264 g_value_set_boolean (value, src->configure_session);
265 break;
266 #endif
267 default:
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 break;
270 }
271 }
272
273 static GstStateChangeReturn
274 gst_osx_audio_src_change_state (GstElement * element, GstStateChange transition)
275 {
276 GstOsxAudioSrc *osxsrc = GST_OSX_AUDIO_SRC (element);
277 GstOsxAudioRingBuffer *ringbuffer;
278 GstStateChangeReturn ret;
279
280 switch (transition) {
281 case GST_STATE_CHANGE_READY_TO_NULL:{
282 GST_OBJECT_LOCK (osxsrc);
283 osxsrc->device_id = kAudioDeviceUnknown;
284 g_clear_pointer (&osxsrc->unique_id, g_free);
285 GST_OBJECT_UNLOCK (osxsrc);
286 break;
287 }
288 #if !TARGET_OS_OSX
289 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
290 ringbuffer =
291 GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SRC (osxsrc)->ringbuffer);
292 ringbuffer->core_audio->first_sample_time = -1;
293 break;
294 #endif
295 default:
296 break;
297 }
298
299 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
300 if (ret == GST_STATE_CHANGE_FAILURE)
301 goto out;
302
303 switch (transition) {
304 case GST_STATE_CHANGE_NULL_TO_READY:
305 /* The device is open now, so fix our device_id if it changed */
306 ringbuffer =
307 GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SRC (osxsrc)->ringbuffer);
308 if (ringbuffer->core_audio->device_id != osxsrc->device_id) {
309 GST_OBJECT_LOCK (osxsrc);
310 osxsrc->device_id = ringbuffer->core_audio->device_id;
311 g_free (osxsrc->unique_id);
312 osxsrc->unique_id = g_strdup (ringbuffer->core_audio->unique_id);
313 GST_OBJECT_UNLOCK (osxsrc);
314
315 g_object_notify (G_OBJECT (osxsrc), "device");
316 }
317 break;
318
319 default:
320 break;
321 }
322
323 out:
324 return ret;
325 }
326
327 static GstCaps *
328 gst_osx_audio_src_get_caps (GstBaseSrc * src, GstCaps * filter)
329 {
330 GstOsxAudioSrc *osxsrc;
331 GstAudioRingBuffer *buf;
332 GstOsxAudioRingBuffer *osxbuf;
333 GstCaps *caps, *filtered_caps;
334
335 osxsrc = GST_OSX_AUDIO_SRC (src);
336
337 GST_OBJECT_LOCK (osxsrc);
338 buf = GST_AUDIO_BASE_SRC (src)->ringbuffer;
339 if (buf)
340 gst_object_ref (buf);
341 GST_OBJECT_UNLOCK (osxsrc);
342
343 if (!buf) {
344 GST_DEBUG_OBJECT (src, "no ring buffer, using template caps");
345 return GST_BASE_SRC_CLASS (parent_class)->get_caps (src, filter);
346 }
347
348 osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf);
349
350 /* protect against cached_caps going away */
351 GST_OBJECT_LOCK (buf);
352
353 if (osxbuf->core_audio->cached_caps_valid) {
354 GST_LOG_OBJECT (src, "Returning cached caps");
355 caps = gst_caps_ref (osxbuf->core_audio->cached_caps);
356 } else if (buf->open) {
357 GstCaps *template_caps;
358
359 /* Get template caps */
360 template_caps =
361 gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SRC_PAD (osxsrc));
362
363 /* Device is open, let's probe its caps */
364 caps = gst_core_audio_probe_caps (osxbuf->core_audio, template_caps);
365 gst_caps_replace (&osxbuf->core_audio->cached_caps, caps);
366
367 gst_caps_unref (template_caps);
368 } else {
369 GST_DEBUG_OBJECT (src, "ring buffer not open, using template caps");
370 caps = GST_BASE_SRC_CLASS (parent_class)->get_caps (src, NULL);
371 }
372
373 GST_OBJECT_UNLOCK (buf);
374
375 gst_object_unref (buf);
376
377 if (!caps)
378 return NULL;
379
380 if (!filter)
381 return caps;
382
383 /* Take care of filtered caps */
384 filtered_caps =
385 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
386 gst_caps_unref (caps);
387 return filtered_caps;
388 }
389
390 static GstAudioRingBuffer *
391 gst_osx_audio_src_create_ringbuffer (GstAudioBaseSrc * src)
392 {
393 GstOsxAudioSrc *osxsrc;
394 GstOsxAudioRingBuffer *ringbuffer;
395
396 osxsrc = GST_OSX_AUDIO_SRC (src);
397
398 GST_DEBUG_OBJECT (osxsrc, "Creating ringbuffer");
399 ringbuffer = g_object_new (GST_TYPE_OSX_AUDIO_RING_BUFFER, NULL);
400 GST_DEBUG_OBJECT (osxsrc, "osx src 0x%p element 0x%p ioproc 0x%p", osxsrc,
401 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc),
402 (void *) gst_osx_audio_src_io_proc);
403
404 ringbuffer->core_audio = g_object_new (GST_TYPE_CORE_AUDIO, "is-src", TRUE,
405 "device", osxsrc->device_id, "unique-id", osxsrc->unique_id,
406 #if !TARGET_OS_OSX
407 "configure-session", osxsrc->configure_session,
408 #endif
409 NULL);
410 ringbuffer->core_audio->osxbuf = GST_OBJECT (ringbuffer);
411 ringbuffer->core_audio->element =
412 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc);
413
414 return GST_AUDIO_RING_BUFFER (ringbuffer);
415 }
416
417 static OSStatus
418 gst_osx_audio_src_io_proc (GstOsxAudioRingBuffer * buf,
419 AudioUnitRenderActionFlags * ioActionFlags,
420 const AudioTimeStamp * inTimeStamp,
421 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
422 {
423 OSStatus status;
424 guint8 *writeptr;
425 gint writeseg;
426 gint len;
427 gint remaining;
428 UInt32 n;
429 gint offset = 0;
430 guint64 sample_position;
431 GstAudioRingBufferSpec *spec = &GST_AUDIO_RING_BUFFER (buf)->spec;
432 guint bpf = GST_AUDIO_INFO_BPF (&spec->info);
433
434 GST_LOG_OBJECT (buf, "in sample position %f frames %u",
435 inTimeStamp->mSampleTime, inNumberFrames);
436
437 /* Previous invoke of AudioUnitRender changed mDataByteSize into
438 * number of bytes actually read. Reset the members. */
439 for (n = 0; n < buf->core_audio->recBufferList->mNumberBuffers; ++n) {
440 buf->core_audio->recBufferList->mBuffers[n].mDataByteSize =
441 buf->core_audio->recBufferSize;
442 }
443
444 status = AudioUnitRender (buf->core_audio->audiounit, ioActionFlags,
445 inTimeStamp, inBusNumber, inNumberFrames, buf->core_audio->recBufferList);
446
447 if (status) {
448 GST_WARNING_OBJECT (buf, "AudioUnitRender returned %d", (int) status);
449 return status;
450 }
451
452 /* TODO: To support non-interleaved audio, go over all mBuffers,
453 * not just the first one. */
454
455 remaining = buf->core_audio->recBufferList->mBuffers[0].mDataByteSize;
456 sample_position = inTimeStamp->mSampleTime;
457
458 #if !TARGET_OS_OSX
459 /* Timestamps don't always start from 0 on iOS, have to offset */
460 if (buf->core_audio->first_sample_time == -1) {
461 GST_DEBUG ("Setting first CoreAudio timestamp to %f",
462 inTimeStamp->mSampleTime);
463 buf->core_audio->first_sample_time = inTimeStamp->mSampleTime;
464 }
465
466 sample_position -= buf->core_audio->first_sample_time;
467 #endif
468
469 if (g_atomic_int_get (&buf->core_audio->io_proc_dropping)) {
470 GST_TRACE_OBJECT (buf, "Dropped %d samples", remaining);
471 return 0;
472 }
473
474 while (remaining) {
475 if (!gst_audio_ring_buffer_prepare_read (GST_AUDIO_RING_BUFFER (buf),
476 &writeseg, &writeptr, &len))
477 return 0;
478
479 len -= buf->segoffset;
480
481 if (len > remaining)
482 len = remaining;
483
484 memcpy (writeptr + buf->segoffset,
485 (char *) buf->core_audio->recBufferList->mBuffers[0].mData + offset,
486 len);
487
488 buf->segoffset += len;
489 offset += len;
490 remaining -= len;
491 sample_position += len / bpf;
492
493 if ((gint) buf->segoffset == GST_AUDIO_RING_BUFFER (buf)->spec.segsize) {
494 /* Calculate the timestamp corresponding to the first sample in the segment */
495 guint64 seg_sample_pos = sample_position - (spec->segsize / bpf);
496 GstClockTime ts = gst_util_uint64_scale_int (seg_sample_pos, GST_SECOND,
497 GST_AUDIO_INFO_RATE (&spec->info));
498 gst_audio_ring_buffer_set_timestamp (GST_AUDIO_RING_BUFFER (buf),
499 writeseg, ts);
500
501 /* we wrote one segment */
502 CORE_AUDIO_TIMING_LOCK (buf->core_audio);
503 gst_audio_ring_buffer_advance (GST_AUDIO_RING_BUFFER (buf), 1);
504 /* FIXME: Update the timestamp and reported frames in smaller increments
505 * when the segment size is larger than the total inNumberFrames */
506 gst_core_audio_update_timing (buf->core_audio, inTimeStamp,
507 inNumberFrames);
508 CORE_AUDIO_TIMING_UNLOCK (buf->core_audio);
509
510 buf->segoffset = 0;
511 }
512 }
513 return 0;
514 }
515
516 static void
517 gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data)
518 {
519 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
520
521 iface->io_proc = (AURenderCallback) gst_osx_audio_src_io_proc;
522 }