| 1 |
/* |
| 2 |
* GStreamer |
| 3 |
* Copyright (C) 2006 Zaheer Abbas Merali <zaheerabbas at merali dot org> |
| 4 |
* Copyright (C) 2008 Pioneers of the Inevitable <[email protected]> |
| 5 |
* Copyright (C) 2012 Fluendo S.A. <[email protected]> |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
* copy of this software and associated documentation files (the "Software"), |
| 9 |
* to deal in the Software without restriction, including without limitation |
| 10 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in |
| 15 |
* all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 |
* DEALINGS IN THE SOFTWARE. |
| 24 |
* |
| 25 |
* Alternatively, the contents of this file may be used under the |
| 26 |
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in |
| 27 |
* which case the following provisions apply instead of the ones |
| 28 |
* mentioned above: |
| 29 |
* |
| 30 |
* This library is free software; you can redistribute it and/or |
| 31 |
* modify it under the terms of the GNU Library General Public |
| 32 |
* License as published by the Free Software Foundation; either |
| 33 |
* version 2 of the License, or (at your option) any later version. |
| 34 |
* |
| 35 |
* This library is distributed in the hope that it will be useful, |
| 36 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 38 |
* Library General Public License for more details. |
| 39 |
* |
| 40 |
* You should have received a copy of the GNU Library General Public |
| 41 |
* License along with this library; if not, write to the |
| 42 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 43 |
* Boston, MA 02110-1301, USA. |
| 44 |
*/ |
| 45 |
|
| 46 |
#ifdef HAVE_CONFIG_H |
| 47 |
# include <config.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#include <gst/gst.h> |
| 51 |
#include <glib/gi18n-lib.h> |
| 52 |
#include <gst/audio/audio-channels.h> |
| 53 |
#include "gstosxaudioringbuffer.h" |
| 54 |
#include "gstosxaudiosink.h" |
| 55 |
#include "gstosxaudiosrc.h" |
| 56 |
|
| 57 |
#include <unistd.h> /* for getpid() */ |
| 58 |
|
| 59 |
GST_DEBUG_CATEGORY_STATIC (osx_audio_debug); |
| 60 |
#define GST_CAT_DEFAULT osx_audio_debug |
| 61 |
|
| 62 |
#include "gstosxcoreaudio.h" |
| 63 |
|
| 64 |
static void gst_osx_audio_ring_buffer_dispose (GObject * object); |
| 65 |
static gboolean gst_osx_audio_ring_buffer_open_device (GstAudioRingBuffer * |
| 66 |
buf); |
| 67 |
static gboolean gst_osx_audio_ring_buffer_close_device (GstAudioRingBuffer * |
| 68 |
buf); |
| 69 |
|
| 70 |
static gboolean gst_osx_audio_ring_buffer_acquire (GstAudioRingBuffer * buf, |
| 71 |
GstAudioRingBufferSpec * spec); |
| 72 |
static gboolean gst_osx_audio_ring_buffer_release (GstAudioRingBuffer * buf); |
| 73 |
|
| 74 |
static gboolean gst_osx_audio_ring_buffer_start (GstAudioRingBuffer * buf); |
| 75 |
static gboolean gst_osx_audio_ring_buffer_pause (GstAudioRingBuffer * buf); |
| 76 |
static gboolean gst_osx_audio_ring_buffer_stop (GstAudioRingBuffer * buf); |
| 77 |
static guint gst_osx_audio_ring_buffer_delay (GstAudioRingBuffer * buf); |
| 78 |
static GstAudioRingBufferClass *ring_parent_class = NULL; |
| 79 |
|
| 80 |
#define gst_osx_audio_ring_buffer_do_init \ |
| 81 |
GST_DEBUG_CATEGORY_INIT (osx_audio_debug, "osxaudio", 0, "OSX Audio Elements"); |
| 82 |
|
| 83 |
G_DEFINE_TYPE_WITH_CODE (GstOsxAudioRingBuffer, gst_osx_audio_ring_buffer, |
| 84 |
GST_TYPE_AUDIO_RING_BUFFER, gst_osx_audio_ring_buffer_do_init); |
| 85 |
|
| 86 |
static void |
| 87 |
gst_osx_audio_ring_buffer_class_init (GstOsxAudioRingBufferClass * klass) |
| 88 |
{ |
| 89 |
GObjectClass *gobject_class; |
| 90 |
GstAudioRingBufferClass *gstringbuffer_class; |
| 91 |
|
| 92 |
gobject_class = (GObjectClass *) klass; |
| 93 |
gstringbuffer_class = (GstAudioRingBufferClass *) klass; |
| 94 |
|
| 95 |
ring_parent_class = g_type_class_peek_parent (klass); |
| 96 |
|
| 97 |
gobject_class->dispose = gst_osx_audio_ring_buffer_dispose; |
| 98 |
|
| 99 |
gstringbuffer_class->open_device = |
| 100 |
GST_DEBUG_FUNCPTR (gst_osx_audio_ring_buffer_open_device); |
| 101 |
gstringbuffer_class->close_device = |
| 102 |
GST_DEBUG_FUNCPTR (gst_osx_audio_ring_buffer_close_device); |
| 103 |
gstringbuffer_class->acquire = |
| 104 |
GST_DEBUG_FUNCPTR (gst_osx_audio_ring_buffer_acquire); |
| 105 |
gstringbuffer_class->release = |
| 106 |
GST_DEBUG_FUNCPTR (gst_osx_audio_ring_buffer_release); |
| 107 |
gstringbuffer_class->start = |
| 108 |
GST_DEBUG_FUNCPTR (gst_osx_audio_ring_buffer_start); |
| 109 |
gstringbuffer_class->pause = |
| 110 |
GST_DEBUG_FUNCPTR (gst_osx_audio_ring_buffer_pause); |
| 111 |
gstringbuffer_class->resume = |
| 112 |
GST_DEBUG_FUNCPTR (gst_osx_audio_ring_buffer_start); |
| 113 |
gstringbuffer_class->stop = |
| 114 |
GST_DEBUG_FUNCPTR (gst_osx_audio_ring_buffer_stop); |
| 115 |
gstringbuffer_class->delay = |
| 116 |
GST_DEBUG_FUNCPTR (gst_osx_audio_ring_buffer_delay); |
| 117 |
|
| 118 |
GST_DEBUG ("osx audio ring buffer class init"); |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
gst_osx_audio_ring_buffer_init (GstOsxAudioRingBuffer * ringbuffer) |
| 123 |
{ |
| 124 |
} |
| 125 |
|
| 126 |
static void |
| 127 |
gst_osx_audio_ring_buffer_dispose (GObject * object) |
| 128 |
{ |
| 129 |
GstOsxAudioRingBuffer *osxbuf; |
| 130 |
|
| 131 |
osxbuf = GST_OSX_AUDIO_RING_BUFFER (object); |
| 132 |
|
| 133 |
g_clear_object (&osxbuf->core_audio); |
| 134 |
G_OBJECT_CLASS (ring_parent_class)->dispose (object); |
| 135 |
} |
| 136 |
|
| 137 |
static gboolean |
| 138 |
gst_osx_audio_ring_buffer_open_device (GstAudioRingBuffer * buf) |
| 139 |
{ |
| 140 |
GstObject *osxel = GST_OBJECT_PARENT (buf); |
| 141 |
GstOsxAudioRingBuffer *osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf); |
| 142 |
|
| 143 |
if (!gst_core_audio_select_device (osxbuf->core_audio)) { |
| 144 |
GST_ELEMENT_ERROR (osxel, RESOURCE, NOT_FOUND, |
| 145 |
(_("CoreAudio device not found")), (NULL)); |
| 146 |
return FALSE; |
| 147 |
} |
| 148 |
|
| 149 |
if (!gst_core_audio_open (osxbuf->core_audio)) { |
| 150 |
GST_ELEMENT_ERROR (osxel, RESOURCE, OPEN_READ, |
| 151 |
(_("CoreAudio device could not be opened")), (NULL)); |
| 152 |
return FALSE; |
| 153 |
} |
| 154 |
|
| 155 |
return TRUE; |
| 156 |
} |
| 157 |
|
| 158 |
static gboolean |
| 159 |
gst_osx_audio_ring_buffer_close_device (GstAudioRingBuffer * buf) |
| 160 |
{ |
| 161 |
GstOsxAudioRingBuffer *osxbuf; |
| 162 |
osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf); |
| 163 |
|
| 164 |
return gst_core_audio_close (osxbuf->core_audio); |
| 165 |
} |
| 166 |
|
| 167 |
static gboolean |
| 168 |
gst_osx_audio_ring_buffer_acquire (GstAudioRingBuffer * buf, |
| 169 |
GstAudioRingBufferSpec * spec) |
| 170 |
{ |
| 171 |
gboolean ret = FALSE, is_passthrough = FALSE; |
| 172 |
GstOsxAudioRingBuffer *osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf); |
| 173 |
AudioStreamBasicDescription format = { 0 }; |
| 174 |
guint32 frames_per_packet = 0; |
| 175 |
|
| 176 |
if (RINGBUFFER_IS_SPDIF (spec->type)) { |
| 177 |
format.mFormatID = kAudioFormat60958AC3; |
| 178 |
format.mSampleRate = (double) GST_AUDIO_INFO_RATE (&spec->info); |
| 179 |
format.mChannelsPerFrame = 2; |
| 180 |
format.mFormatFlags = kAudioFormatFlagIsSignedInteger | |
| 181 |
kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonMixable; |
| 182 |
format.mBytesPerFrame = 0; |
| 183 |
format.mBitsPerChannel = 16; |
| 184 |
format.mBytesPerPacket = 6144; |
| 185 |
format.mFramesPerPacket = 1536; |
| 186 |
format.mReserved = 0; |
| 187 |
spec->segsize = 6144; |
| 188 |
spec->segtotal = 10; |
| 189 |
is_passthrough = TRUE; |
| 190 |
} else { |
| 191 |
int width, depth; |
| 192 |
/* Fill out the audio description we're going to be using */ |
| 193 |
format.mFormatID = kAudioFormatLinearPCM; |
| 194 |
format.mSampleRate = (double) GST_AUDIO_INFO_RATE (&spec->info); |
| 195 |
format.mChannelsPerFrame = GST_AUDIO_INFO_CHANNELS (&spec->info); |
| 196 |
if (GST_AUDIO_INFO_IS_FLOAT (&spec->info)) { |
| 197 |
format.mFormatFlags = kAudioFormatFlagsNativeFloatPacked; |
| 198 |
width = depth = GST_AUDIO_INFO_WIDTH (&spec->info); |
| 199 |
} else { |
| 200 |
format.mFormatFlags = kAudioFormatFlagIsSignedInteger; |
| 201 |
width = GST_AUDIO_INFO_WIDTH (&spec->info); |
| 202 |
depth = GST_AUDIO_INFO_DEPTH (&spec->info); |
| 203 |
if (width == depth) { |
| 204 |
format.mFormatFlags |= kAudioFormatFlagIsPacked; |
| 205 |
} else { |
| 206 |
format.mFormatFlags |= kAudioFormatFlagIsAlignedHigh; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (GST_AUDIO_INFO_IS_BIG_ENDIAN (&spec->info)) { |
| 211 |
format.mFormatFlags |= kAudioFormatFlagIsBigEndian; |
| 212 |
} |
| 213 |
|
| 214 |
format.mBytesPerFrame = GST_AUDIO_INFO_BPF (&spec->info); |
| 215 |
format.mBitsPerChannel = depth; |
| 216 |
format.mBytesPerPacket = GST_AUDIO_INFO_BPF (&spec->info); |
| 217 |
format.mFramesPerPacket = 1; |
| 218 |
format.mReserved = 0; |
| 219 |
spec->segsize = |
| 220 |
(spec->latency_time * GST_AUDIO_INFO_RATE (&spec->info) / |
| 221 |
G_USEC_PER_SEC) * GST_AUDIO_INFO_BPF (&spec->info); |
| 222 |
spec->segtotal = spec->buffer_time / spec->latency_time; |
| 223 |
frames_per_packet = spec->segsize / GST_AUDIO_INFO_BPF (&spec->info); |
| 224 |
|
| 225 |
is_passthrough = FALSE; |
| 226 |
} |
| 227 |
|
| 228 |
GST_DEBUG_OBJECT (osxbuf, "Format: " CORE_AUDIO_FORMAT, |
| 229 |
CORE_AUDIO_FORMAT_ARGS (format)); |
| 230 |
|
| 231 |
/* gst_audio_ring_buffer_set_channel_positions is not called |
| 232 |
* since the AUs perform channel reordering themselves. |
| 233 |
* (see gst_core_audio_set_channel_layout) */ |
| 234 |
|
| 235 |
buf->size = spec->segtotal * spec->segsize; |
| 236 |
buf->memory = g_malloc0 (buf->size); |
| 237 |
|
| 238 |
ret = gst_core_audio_initialize (osxbuf->core_audio, format, spec->caps, |
| 239 |
frames_per_packet, is_passthrough); |
| 240 |
|
| 241 |
if (!ret) { |
| 242 |
g_free (buf->memory); |
| 243 |
buf->memory = NULL; |
| 244 |
buf->size = 0; |
| 245 |
} |
| 246 |
|
| 247 |
osxbuf->segoffset = 0; |
| 248 |
|
| 249 |
return ret; |
| 250 |
} |
| 251 |
|
| 252 |
static gboolean |
| 253 |
gst_osx_audio_ring_buffer_release (GstAudioRingBuffer * buf) |
| 254 |
{ |
| 255 |
GstOsxAudioRingBuffer *osxbuf; |
| 256 |
|
| 257 |
osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf); |
| 258 |
|
| 259 |
gst_core_audio_uninitialize (osxbuf->core_audio); |
| 260 |
|
| 261 |
g_free (buf->memory); |
| 262 |
buf->memory = NULL; |
| 263 |
buf->size = 0; |
| 264 |
|
| 265 |
return TRUE; |
| 266 |
} |
| 267 |
|
| 268 |
static gboolean |
| 269 |
gst_osx_audio_ring_buffer_start (GstAudioRingBuffer * buf) |
| 270 |
{ |
| 271 |
GstOsxAudioRingBuffer *osxbuf; |
| 272 |
|
| 273 |
osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf); |
| 274 |
|
| 275 |
return gst_core_audio_start_processing (osxbuf->core_audio); |
| 276 |
} |
| 277 |
|
| 278 |
static gboolean |
| 279 |
gst_osx_audio_ring_buffer_pause (GstAudioRingBuffer * buf) |
| 280 |
{ |
| 281 |
GstOsxAudioRingBuffer *osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf); |
| 282 |
|
| 283 |
return gst_core_audio_pause_processing (osxbuf->core_audio); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
static gboolean |
| 288 |
gst_osx_audio_ring_buffer_stop (GstAudioRingBuffer * buf) |
| 289 |
{ |
| 290 |
GstOsxAudioRingBuffer *osxbuf; |
| 291 |
|
| 292 |
osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf); |
| 293 |
|
| 294 |
gst_core_audio_stop_processing (osxbuf->core_audio); |
| 295 |
|
| 296 |
return TRUE; |
| 297 |
} |
| 298 |
|
| 299 |
static guint |
| 300 |
gst_osx_audio_ring_buffer_delay (GstAudioRingBuffer * buf) |
| 301 |
{ |
| 302 |
GstOsxAudioRingBuffer *osxbuf; |
| 303 |
double latency; |
| 304 |
guint samples; |
| 305 |
|
| 306 |
osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf); |
| 307 |
|
| 308 |
if (!gst_core_audio_get_samples_and_latency (osxbuf->core_audio, |
| 309 |
GST_AUDIO_INFO_RATE (&buf->spec.info), &samples, &latency)) { |
| 310 |
return 0; |
| 311 |
} |
| 312 |
return samples; |
| 313 |
} |