Debian Codegraph @

sys/osxaudio/gstosxcoreaudioremoteio.c

raw
1 /*
2 * GStreamer
3 * Copyright (C) 2012 Fluendo S.A. <[email protected]>
4 * Authors: Andoni Morales Alastruey <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 *
21 */
22
23 #import <AudioUnit/AUComponent.h>
24 #import "gstiosaudiosession.h"
25
26 static gboolean
27 gst_core_audio_open_impl (GstCoreAudio * core_audio)
28 {
29 /* On iOS, an AVAudioSession needs to be set up to 1) avoid playback being silenced
30 * by silent mode and 2) to allow audio to be captured from the microphone.
31 * However, AVAudioSession has a lot of settings and in more complicated scenarios,
32 * apps/users should handle that themselves. Disable auto-config through the
33 * configure-session property on osxaudiosrc/sink in those cases. */
34 if (core_audio->configure_session)
35 gst_ios_audio_session_setup (core_audio->is_src);
36
37 return gst_core_audio_open_device (core_audio, kAudioUnitSubType_RemoteIO,
38 "RemoteIO");
39 }
40
41 static gboolean
42 gst_core_audio_start_processing_impl (GstCoreAudio * core_audio)
43 {
44 return gst_core_audio_io_proc_start (core_audio);
45 }
46
47 static gboolean
48 gst_core_audio_pause_processing_impl (GstCoreAudio * core_audio)
49 {
50 GST_DEBUG_OBJECT (core_audio,
51 "osx ring buffer pause ioproc: %p device_id %lu",
52 core_audio->element->io_proc, (gulong) core_audio->device_id);
53
54 if (core_audio->io_proc_active) {
55 /* CoreAudio isn't threadsafe enough to remove the render callback in the
56 * render notify callback, so we pause by having the callback drop
57 * samples or feed silence. See:
58 * http://lists.apple.com/archives/Coreaudio-api/2006/Mar/msg00010.html
59 * https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/4155
60 */
61 g_atomic_int_set (&core_audio->io_proc_dropping, TRUE);
62 }
63 return TRUE;
64 }
65
66 static gboolean
67 gst_core_audio_stop_processing_impl (GstCoreAudio * core_audio)
68 {
69 gst_core_audio_io_proc_stop (core_audio);
70 return TRUE;
71 }
72
73 static gboolean
74 gst_core_audio_get_samples_and_latency_impl (GstCoreAudio * core_audio,
75 gdouble rate, guint * samples, gdouble * latency)
76 {
77 OSStatus status;
78 UInt32 size = sizeof (double);
79
80 status = AudioUnitGetProperty (core_audio->audiounit, kAudioUnitProperty_Latency, kAudioUnitScope_Global, 0, /* N/A for global */
81 latency, &size);
82
83 if (status) {
84 GST_WARNING_OBJECT (core_audio, "Failed to get latency: %d", (int) status);
85 *samples = 0;
86 return FALSE;
87 }
88
89 *samples = *latency * rate;
90 return TRUE;
91 }
92
93 static gboolean
94 gst_core_audio_initialize_impl (GstCoreAudio * core_audio,
95 AudioStreamBasicDescription format, GstCaps * caps,
96 gboolean is_passthrough, guint32 * frame_size)
97 {
98 gboolean ret = FALSE;
99 OSStatus status;
100
101 /* Uninitialize the AudioUnit before changing formats */
102 status = AudioUnitUninitialize (core_audio->audiounit);
103 if (status) {
104 GST_ERROR_OBJECT (core_audio, "Failed to uninitialize AudioUnit: %d",
105 (int) status);
106 return FALSE;
107 }
108
109 core_audio->is_passthrough = is_passthrough;
110 core_audio->stream_idx = 0;
111
112 if (!gst_core_audio_set_format (core_audio, format))
113 goto done;
114
115 /* FIXME: Use kAudioSessionProperty_CurrentHardwareSampleRate and
116 * kAudioSessionProperty_CurrentHardwareIOBufferDuration with property
117 * listeners to detect changes in screen locks.
118 * For now use the maximum value */
119 *frame_size = 4196;
120
121 GST_DEBUG_OBJECT (core_audio, "osxbuf ring buffer acquired");
122 ret = TRUE;
123
124 done:
125 /* Format changed, initialise the AudioUnit again */
126 status = AudioUnitInitialize (core_audio->audiounit);
127 if (status) {
128 GST_ERROR_OBJECT (core_audio, "Failed to initialize AudioUnit: %d",
129 (int) status);
130 ret = FALSE;
131 }
132
133 return ret;
134 }
135
136 static gboolean
137 gst_core_audio_select_device_impl (GstCoreAudio * core_audio)
138 {
139 /* No device selection in iOS */
140 core_audio->is_default = TRUE;
141 return TRUE;
142 }
143
144 static gboolean
145 gst_core_audio_audio_device_is_spdif_avail_impl (AudioDeviceID device_id)
146 {
147 /* No SPDIF in iOS */
148 return FALSE;
149 }