/*****************************************************************************
- * audioqueue.c : AudioQueue audio output plugin for vlc
- *****************************************************************************
- * Copyright (C) 2010 VideoLAN and AUTHORS
+ * Copyright (C) 2000-2013 VLC authors and VideoLAN
+ * $Id$
*
- * Authors: Romain Goyet <romain.goyet@likid.org>
+ * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
+ * Rémi Denis-Courmont
+ * Rafaël Carré <funman at videolan dot org>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
-/*****************************************************************************
- * Preamble
- *****************************************************************************/
-#include <unistd.h> /* write(), close() */
+#pragma mark includes
#ifdef HAVE_CONFIG_H
-# include "config.h"
+# import "config.h"
+#endif
+#import <vlc_common.h>
+#import <vlc_plugin.h>
+#import <vlc_aout.h>
+#import <AudioToolBox/AudioQueue.h>
+#import <TargetConditionals.h>
+#if TARGET_OS_IPHONE
+#import <AudioToolBox/AudioSession.h>
+#else
+#define AudioSessionSetActive(x)
#endif
-#include <vlc_common.h>
-#include <vlc_plugin.h>
-#include <vlc_aout.h>
-
-#include <AudioToolBox/AudioToolBox.h>
-
-#define NUMBER_OF_BUFFERS 3
-#define FRAME_SIZE 2048
+#pragma mark -
+#pragma mark private declarations
-/*****************************************************************************
- * aout_sys_t: AudioQueue audio output method descriptor
- *****************************************************************************
- * This structure is part of the audio output thread descriptor.
- * It describes the specific properties of an audio device.
- *****************************************************************************/
struct aout_sys_t
{
- aout_packet_t packet;
- AudioQueueRef audioQueue;
- float soft_gain;
- bool soft_mute;
-};
+ AudioQueueRef audioQueueRef;
+ AudioQueueTimelineRef timelineRef;
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static int Open ( vlc_object_t * );
-static void Close ( vlc_object_t * );
-static void Play ( audio_output_t *, block_t *, mtime_t * );
-static void AudioQueueCallback (void *, AudioQueueRef, AudioQueueBufferRef);
+ bool b_started;
-#include "volume.h"
-
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
+ mtime_t i_played_length;
+ int i_rate;
+ float f_volume;
+};
+static int Open (vlc_object_t *);
+static void Close (vlc_object_t *);
+static void Play (audio_output_t *, block_t *);
+static void Pause (audio_output_t *p_aout, bool pause, mtime_t date);
+static void Flush (audio_output_t *p_aout, bool wait);
+static int TimeGet (audio_output_t *aout, mtime_t *);
+static void UnusedAudioQueueCallback (void *, AudioQueueRef, AudioQueueBufferRef);
+static int Start(audio_output_t *, audio_sample_format_t *);
+static void Stop(audio_output_t *);
+static int VolumeSet(audio_output_t *, float );
vlc_module_begin ()
- set_shortname( "AudioQueue" )
- set_description( N_("AudioQueue (iOS / Mac OS) audio output") )
- set_capability( "audio output", 40 )
- set_category( CAT_AUDIO )
- set_subcategory( SUBCAT_AUDIO_AOUT )
- add_shortcut( "audioqueue" )
- set_callbacks( Open, Close )
+set_shortname("AudioQueue")
+set_description(N_("AudioQueue (iOS / Mac OS) audio output"))
+set_capability("audio output", 40)
+set_category(CAT_AUDIO)
+set_subcategory(SUBCAT_AUDIO_AOUT)
+add_shortcut("audioqueue")
+set_callbacks(Open, Close)
vlc_module_end ()
-/*****************************************************************************
- * Open: open the audio device
- *****************************************************************************/
+#pragma mark -
+#pragma mark initialization
-static int Start( audio_output_t *aout, audio_sample_format_t *restrict fmt )
+static int Open(vlc_object_t *obj)
{
- audio_output_t *p_aout = (audio_output_t *)p_this;
- aout_sys_t *p_sys = aout->sys;
-
- OSStatus status = 0;
-
- // Setup the audio device.
- AudioStreamBasicDescription deviceFormat;
- deviceFormat.mSampleRate = 44100;
- deviceFormat.mFormatID = kAudioFormatLinearPCM;
- deviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger; // Signed integer, little endian
- deviceFormat.mBytesPerPacket = 4;
- deviceFormat.mFramesPerPacket = 1;
- deviceFormat.mBytesPerFrame = 4;
- deviceFormat.mChannelsPerFrame = 2;
- deviceFormat.mBitsPerChannel = 16;
- deviceFormat.mReserved = 0;
-
- // Create a new output AudioQueue for the device.
- status = AudioQueueNewOutput(&deviceFormat, // Format
- AudioQueueCallback, // Callback
- p_aout, // User data, passed to the callback
- CFRunLoopGetMain(), // RunLoop
- kCFRunLoopDefaultMode, // RunLoop mode
- 0, // Flags ; must be zero (per documentation)...
- &(p_sys->audioQueue)); // Output
-
- // This will be used for boosting the audio without the need of a mixer (floating-point conversion is expensive on ARM)
- // AudioQueueSetParameter(p_sys->audioQueue, kAudioQueueParam_Volume, 12.0); // Defaults to 1.0
-
- msg_Dbg(p_aout, "New AudioQueue output created (status = %i)", status);
-
- // Allocate buffers for the AudioQueue, and pre-fill them.
- for (int i = 0; i < NUMBER_OF_BUFFERS; ++i) {
- AudioQueueBufferRef buffer = NULL;
- status = AudioQueueAllocateBuffer(p_sys->audioQueue, FRAME_SIZE * 4, &buffer);
- AudioQueueCallback(NULL, p_sys->audioQueue, buffer);
- }
+ audio_output_t *aout = (audio_output_t *)obj;
+ aout_sys_t *sys = malloc(sizeof (*sys));
- fmt->i_format = VLC_CODEC_S16L;
- fmt->i_physical_channels = AOUT_CHANS_STEREO;
- fmt->i_rate = 44100;
- aout_PacketInit(p_aout, &p_sys->packet, FRAME_SIZE, fmt);
- p_aout->play = aout_PacketPlay;
- p_aout->pause = aout_PacketPause;
- p_aout->flush = aout_PacketFlush;
+ if (unlikely(sys == NULL))
+ return VLC_ENOMEM;
+
+ aout->sys = sys;
+ aout->start = Start;
+ aout->stop = Stop;
+ aout->volume_set = VolumeSet;
- msg_Dbg(p_aout, "Starting AudioQueue (status = %i)", status);
- status = AudioQueueStart(p_sys->audioQueue, NULL);
+ /* reset volume */
+ aout_VolumeReport(aout, 1.0);
return VLC_SUCCESS;
}
-/*****************************************************************************
- * aout_FifoPop : get the next buffer out of the FIFO
- *****************************************************************************/
-static block_t *aout_FifoPop2( aout_fifo_t * p_fifo )
- {
- block_t *p_buffer = p_fifo->p_first;
- if( p_buffer != NULL )
- {
- p_fifo->p_first = p_buffer->p_next;
- if( p_fifo->p_first == NULL )
- p_fifo->pp_last = &p_fifo->p_first;
- }
- return p_buffer;
- }
+static void Close(vlc_object_t *obj)
+{
+ audio_output_t *aout = (audio_output_t *)obj;
+ msg_Dbg( aout, "audioqueue: Close");
+ aout_sys_t *sys = aout->sys;
+ free(sys);
+}
-/*****************************************************************************
- * Close: close the audio device
- *****************************************************************************/
-static void Stop ( audio_output_t *p_aout )
+static int VolumeSet(audio_output_t * p_aout, float volume)
+{
+ struct aout_sys_t *p_sys = p_aout->sys;
+ OSStatus ostatus;
+
+ aout_VolumeReport(p_aout, volume);
+ p_sys->f_volume = volume;
+
+ /* Set volume for output unit */
+ ostatus = AudioQueueSetParameter(p_sys->audioQueueRef, kAudioQueueParam_Volume, volume * volume * volume);
+
+ return ostatus;
+}
+
+static int Start(audio_output_t *p_aout, audio_sample_format_t *restrict fmt)
{
- struct aout_sys_t * p_sys = p_aout->sys;
+ aout_sys_t *p_sys = p_aout->sys;
+ OSStatus error = 0;
+
+ // prepare the format description for our output
+ AudioStreamBasicDescription streamDescription;
+ streamDescription.mSampleRate = fmt->i_rate;
+ streamDescription.mFormatID = kAudioFormatLinearPCM;
+ streamDescription.mFormatFlags = kAudioFormatFlagsNativeFloatPacked; // FL32
+ streamDescription.mFramesPerPacket = 1;
+ streamDescription.mChannelsPerFrame = 2;
+ streamDescription.mBitsPerChannel = 32;
+ streamDescription.mBytesPerFrame = streamDescription.mBitsPerChannel * streamDescription.mChannelsPerFrame / 8;
+ streamDescription.mBytesPerPacket = streamDescription.mBytesPerFrame * streamDescription.mFramesPerPacket;
+
+ // init new output instance
+ error = AudioQueueNewOutput(&streamDescription, // Format
+ UnusedAudioQueueCallback, // Unused Callback, which needs to be provided to have a proper instance
+ NULL, // User data, passed to the callback
+ NULL, // RunLoop
+ kCFRunLoopCommonModes, // RunLoop mode
+ 0, // Flags ; must be zero (per documentation)...
+ &(p_sys->audioQueueRef)); // Output
+ msg_Dbg(p_aout, "New AudioQueue instance created (status = %li)", error);
+ if (error != noErr)
+ return VLC_EGENERIC;
+ fmt->i_format = VLC_CODEC_FL32;
+ fmt->i_physical_channels = AOUT_CHANS_STEREO;
+ aout_FormatPrepare(fmt);
+ p_aout->sys->i_rate = fmt->i_rate;
+
+ // start queue
+ error = AudioQueueStart(p_sys->audioQueueRef, NULL);
+ msg_Dbg(p_aout, "Starting AudioQueue (status = %li)", error);
+
+ // start timeline for synchro
+ error = AudioQueueCreateTimeline(p_sys->audioQueueRef, &p_sys->timelineRef);
+ msg_Dbg(p_aout, "AudioQueue Timeline started (status = %li)", error);
+ if (error != noErr)
+ return VLC_EGENERIC;
+
+#if TARGET_OS_IPHONE
+ // start audio session so playback continues if mute switch is on
+ AudioSessionInitialize (NULL,
+ kCFRunLoopCommonModes,
+ NULL,
+ NULL);
+
+ // Set audio session to mediaplayback
+ UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
+ AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory),&sessionCategory);
+ AudioSessionSetActive(true);
+#endif
+
+ p_aout->sys->b_started = true;
- msg_Dbg(p_aout, "Stopping AudioQueue");
- AudioQueueStop(p_sys->audioQueue, false);
- msg_Dbg(p_aout, "Disposing of AudioQueue");
- AudioQueueDispose(p_sys->audioQueue, false);
- aout_PacketDestroy(p_aout);
+ p_aout->time_get = TimeGet;
+ p_aout->play = Play;
+ p_aout->pause = Pause;
+ p_aout->flush = Flush;
+ return VLC_SUCCESS;
}
-void AudioQueueCallback(void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
- audio_output_t * p_aout = (audio_output_t *)inUserData;
- block_t * p_buffer = NULL;
+static void Stop(audio_output_t *p_aout)
+{
+ AudioSessionSetActive(false);
- if (p_aout) {
- struct aout_sys_t * p_sys = p_aout->sys;
- aout_packet_t * packet = &p_sys->packet;
+ p_aout->sys->i_played_length = 0;
+ AudioQueueDisposeTimeline(p_aout->sys->audioQueueRef, p_aout->sys->timelineRef);
+ AudioQueueStop(p_aout->sys->audioQueueRef, true);
+ AudioQueueDispose(p_aout->sys->audioQueueRef, true);
+ msg_Dbg(p_aout, "audioqueue stopped and disposed");
+}
- if (packet)
- {
- vlc_mutex_lock( &packet->lock );
- p_buffer = aout_FifoPop2( &packet->fifo );
- vlc_mutex_unlock( &packet->lock );
- }
- }
+#pragma mark -
+#pragma mark actual playback
- if ( p_buffer != NULL ) {
- memcpy( inBuffer->mAudioData, p_buffer->p_buffer, p_buffer->i_buffer );
- inBuffer->mAudioDataByteSize = p_buffer->i_buffer;
- block_Release( p_buffer );
+static void Play(audio_output_t *p_aout, block_t *p_block)
+{
+ AudioQueueBufferRef inBuffer = NULL;
+ OSStatus status;
+
+ status = AudioQueueAllocateBuffer(p_aout->sys->audioQueueRef, p_block->i_buffer, &inBuffer);
+ if (status == noErr) {
+ memcpy(inBuffer->mAudioData, p_block->p_buffer, p_block->i_buffer);
+ inBuffer->mAudioDataByteSize = p_block->i_buffer;
+
+ status = AudioQueueEnqueueBuffer(p_aout->sys->audioQueueRef, inBuffer, 0, NULL);
+ if (status == noErr)
+ p_aout->sys->i_played_length += p_block->i_length;
+ else
+ msg_Err(p_aout, "enqueuing buffer failed (%li)", status);
+ } else
+ msg_Err(p_aout, "buffer alloction failed (%li)", status);
+
+ block_Release(p_block);
+}
+
+void UnusedAudioQueueCallback(void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
+ /* this function does nothing, but needs to be here to make the AudioQueue API happy.
+ * additionally, we clean-up after empty buffers */
+ VLC_UNUSED(inUserData);
+ AudioQueueFreeBuffer(inAQ, inBuffer);
+}
+
+static void Pause(audio_output_t *p_aout, bool pause, mtime_t date)
+{
+ VLC_UNUSED(date);
+
+ if (pause) {
+ AudioQueuePause(p_aout->sys->audioQueueRef);
+ AudioSessionSetActive(false);
} else {
- memset( inBuffer->mAudioData, 0, inBuffer->mAudioDataBytesCapacity );
- inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
+ AudioQueueStart(p_aout->sys->audioQueueRef, NULL);
+ AudioSessionSetActive(true);
}
- AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
}
-static int Open(vlc_object_t *obj)
+static void Flush(audio_output_t *p_aout, bool wait)
{
- audio_output_t *aout = (audio_output_t *)obj;
- aout_sys_t *sys = malloc(sizeof (*sys));
+ if (!p_aout->sys->audioQueueRef)
+ return;
- if (unlikely(sys == NULL))
- return VLC_ENOMEM;
- aout->sys = sys;
- aout->start = Start;
- aout->stop = Stop;
- aout_SoftVolumeInit(aout);
- return VLC_SUCCESS;
+ AudioQueueDisposeTimeline(p_aout->sys->audioQueueRef, p_aout->sys->timelineRef);
+
+ if (wait)
+ AudioQueueStop(p_aout->sys->audioQueueRef, false);
+ else
+ AudioQueueStop(p_aout->sys->audioQueueRef, true);
+
+ p_aout->sys->b_started = false;
+ p_aout->sys->i_played_length = 0;
+ AudioQueueStart(p_aout->sys->audioQueueRef, NULL);
+ AudioQueueCreateTimeline(p_aout->sys->audioQueueRef, &p_aout->sys->timelineRef);
+ p_aout->sys->b_started = true;
}
-static void Close(vlc_object_t *obj)
+static int TimeGet(audio_output_t *p_aout, mtime_t *restrict delay)
{
- audio_output_t *aout = (audio_output_t *)obj;
- aout_sys_t *sys = aout->sys;
+ AudioTimeStamp outTimeStamp;
+ Boolean b_discontinuity;
+ OSStatus status = AudioQueueGetCurrentTime(p_aout->sys->audioQueueRef, p_aout->sys->timelineRef, &outTimeStamp, &b_discontinuity);
- free(sys);
+ if (status != noErr)
+ return -1;
+
+ bool b_started = p_aout->sys->b_started;
+
+ if (!b_started)
+ return -1;
+
+ if (b_discontinuity) {
+ msg_Dbg(p_aout, "detected output discontinuity");
+ return -1;
+ }
+
+ mtime_t i_pos = (mtime_t) outTimeStamp.mSampleTime * CLOCK_FREQ / p_aout->sys->i_rate;
+ if (i_pos > 0) {
+ *delay = p_aout->sys->i_played_length - i_pos;
+ return 0;
+ } else
+ return -1;
}