1 /*
2  * GStreamer
3  * Copyright (C) 2012 Fluendo S.A. <support@fluendo.com>
4  *   Authors: Andoni Morales Alastruey <amorales@fluendo.com>
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 
25 static gboolean
gst_core_audio_open_impl(GstCoreAudio * core_audio)26 gst_core_audio_open_impl (GstCoreAudio * core_audio)
27 {
28   return gst_core_audio_open_device (core_audio, kAudioUnitSubType_RemoteIO,
29       "RemoteIO");
30 }
31 
32 static gboolean
gst_core_audio_start_processing_impl(GstCoreAudio * core_audio)33 gst_core_audio_start_processing_impl (GstCoreAudio * core_audio)
34 {
35   return gst_core_audio_io_proc_start (core_audio);
36 }
37 
38 static gboolean
gst_core_audio_pause_processing_impl(GstCoreAudio * core_audio)39 gst_core_audio_pause_processing_impl (GstCoreAudio * core_audio)
40 {
41   GST_DEBUG_OBJECT (core_audio,
42       "osx ring buffer pause ioproc: %p device_id %lu",
43       core_audio->element->io_proc, (gulong) core_audio->device_id);
44 
45   if (core_audio->io_proc_active) {
46     /* CoreAudio isn't threadsafe enough to do this here;
47      * we must deactivate the render callback elsewhere. See:
48      * http://lists.apple.com/archives/Coreaudio-api/2006/Mar/msg00010.html
49      */
50     core_audio->io_proc_needs_deactivation = TRUE;
51   }
52   return TRUE;
53 }
54 
55 static gboolean
gst_core_audio_stop_processing_impl(GstCoreAudio * core_audio)56 gst_core_audio_stop_processing_impl (GstCoreAudio * core_audio)
57 {
58   gst_core_audio_io_proc_stop (core_audio);
59   return TRUE;
60 }
61 
62 static gboolean
gst_core_audio_get_samples_and_latency_impl(GstCoreAudio * core_audio,gdouble rate,guint * samples,gdouble * latency)63 gst_core_audio_get_samples_and_latency_impl (GstCoreAudio * core_audio,
64     gdouble rate, guint * samples, gdouble * latency)
65 {
66   OSStatus status;
67   UInt32 size = sizeof (double);
68 
69   status = AudioUnitGetProperty (core_audio->audiounit, kAudioUnitProperty_Latency, kAudioUnitScope_Global, 0,  /* N/A for global */
70       latency, &size);
71 
72   if (status) {
73     GST_WARNING_OBJECT (core_audio, "Failed to get latency: %d", (int) status);
74     *samples = 0;
75     return FALSE;
76   }
77 
78   *samples = *latency * rate;
79   return TRUE;
80 }
81 
82 static gboolean
gst_core_audio_initialize_impl(GstCoreAudio * core_audio,AudioStreamBasicDescription format,GstCaps * caps,gboolean is_passthrough,guint32 * frame_size)83 gst_core_audio_initialize_impl (GstCoreAudio * core_audio,
84     AudioStreamBasicDescription format, GstCaps * caps,
85     gboolean is_passthrough, guint32 * frame_size)
86 {
87   gboolean ret = FALSE;
88   OSStatus status;
89 
90   /* Uninitialize the AudioUnit before changing formats */
91   status = AudioUnitUninitialize (core_audio->audiounit);
92   if (status) {
93     GST_ERROR_OBJECT (core_audio, "Failed to uninitialize AudioUnit: %d",
94         (int) status);
95     return FALSE;
96   }
97 
98   core_audio->is_passthrough = is_passthrough;
99   core_audio->stream_idx = 0;
100 
101   if (!gst_core_audio_set_format (core_audio, format))
102     goto done;
103 
104   /* FIXME: Use kAudioSessionProperty_CurrentHardwareSampleRate and
105    * kAudioSessionProperty_CurrentHardwareIOBufferDuration with property
106    * listeners to detect changes in screen locks.
107    * For now use the maximum value */
108   *frame_size = 4196;
109 
110   GST_DEBUG_OBJECT (core_audio, "osxbuf ring buffer acquired");
111   ret = TRUE;
112 
113 done:
114   /* Format changed, initialise the AudioUnit again */
115   status = AudioUnitInitialize (core_audio->audiounit);
116   if (status) {
117     GST_ERROR_OBJECT (core_audio, "Failed to initialize AudioUnit: %d",
118         (int) status);
119     ret = FALSE;
120   }
121 
122   return ret;
123 }
124 
125 static gboolean
gst_core_audio_select_device_impl(GstCoreAudio * core_audio)126 gst_core_audio_select_device_impl (GstCoreAudio * core_audio)
127 {
128   /* No device selection in iOS */
129   return TRUE;
130 }
131 
132 static gboolean
gst_core_audio_audio_device_is_spdif_avail_impl(AudioDeviceID device_id)133 gst_core_audio_audio_device_is_spdif_avail_impl (AudioDeviceID device_id)
134 {
135   /* No SPDIF in iOS */
136   return FALSE;
137 }
138