1 /*****************************************************************************
2 * audioqueue.c : AudioQueue audio output plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2010 VideoLAN and AUTHORS
6 * Authors: Romain Goyet <romain.goyet@likid.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <unistd.h> /* write(), close() */
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
36 #include <AudioToolBox/AudioToolBox.h>
38 #define NUMBER_OF_BUFFERS 3
39 #define FRAME_SIZE 2048
41 /*****************************************************************************
42 * aout_sys_t: AudioQueue audio output method descriptor
43 *****************************************************************************
44 * This structure is part of the audio output thread descriptor.
45 * It describes the specific properties of an audio device.
46 *****************************************************************************/
50 AudioQueueRef audioQueue;
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close ( vlc_object_t * );
58 static void Play ( audio_output_t *, block_t * );
59 static void AudioQueueCallback (void *, AudioQueueRef, AudioQueueBufferRef);
61 /*****************************************************************************
63 *****************************************************************************/
65 set_shortname( "AudioQueue" )
66 set_description( N_("AudioQueue (iOS / Mac OS) audio output") )
67 set_capability( "audio output", 40 )
68 set_category( CAT_AUDIO )
69 set_subcategory( SUBCAT_AUDIO_AOUT )
70 add_shortcut( "audioqueue" )
71 set_callbacks( Open, Close )
74 /*****************************************************************************
75 * Open: open the audio device
76 *****************************************************************************/
78 static int Open ( vlc_object_t *p_this )
80 audio_output_t *p_aout = (audio_output_t *)p_this;
81 struct aout_sys_t *p_sys = malloc(sizeof(aout_sys_t));
86 // Setup the audio device.
87 AudioStreamBasicDescription deviceFormat;
88 deviceFormat.mSampleRate = 44100;
89 deviceFormat.mFormatID = kAudioFormatLinearPCM;
90 deviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger; // Signed integer, little endian
91 deviceFormat.mBytesPerPacket = 4;
92 deviceFormat.mFramesPerPacket = 1;
93 deviceFormat.mBytesPerFrame = 4;
94 deviceFormat.mChannelsPerFrame = 2;
95 deviceFormat.mBitsPerChannel = 16;
96 deviceFormat.mReserved = 0;
98 // Create a new output AudioQueue for the device.
99 status = AudioQueueNewOutput(&deviceFormat, // Format
100 AudioQueueCallback, // Callback
101 p_aout, // User data, passed to the callback
102 CFRunLoopGetMain(), // RunLoop
103 kCFRunLoopDefaultMode, // RunLoop mode
104 0, // Flags ; must be zero (per documentation)...
105 &(p_sys->audioQueue)); // Output
107 // This will be used for boosting the audio without the need of a mixer (floating-point conversion is expensive on ARM)
108 // AudioQueueSetParameter(p_sys->audioQueue, kAudioQueueParam_Volume, 12.0); // Defaults to 1.0
110 msg_Dbg(p_aout, "New AudioQueue output created (status = %i)", status);
112 // Allocate buffers for the AudioQueue, and pre-fill them.
113 for (int i = 0; i < NUMBER_OF_BUFFERS; ++i) {
114 AudioQueueBufferRef buffer = NULL;
115 status = AudioQueueAllocateBuffer(p_sys->audioQueue, FRAME_SIZE * 4, &buffer);
116 AudioQueueCallback(NULL, p_sys->audioQueue, buffer);
119 /* Volume is entirely done in software. */
120 aout_VolumeSoftInit( p_aout );
122 p_aout->format.i_format = VLC_CODEC_S16L;
123 p_aout->format.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
124 p_aout->format.i_rate = 44100;
125 aout_PacketInit(p_aout, &p_sys->packet, FRAME_SIZE);
126 p_aout->pf_play = aout_PacketPlay;
127 p_aout->pf_pause = aout_PacketPause;
128 p_aout->pf_flush = aout_PacketFlush;
130 msg_Dbg(p_aout, "Starting AudioQueue (status = %i)", status);
131 status = AudioQueueStart(p_sys->audioQueue, NULL);
136 /*****************************************************************************
137 * aout_FifoPop : get the next buffer out of the FIFO
138 *****************************************************************************/
139 static aout_buffer_t *aout_FifoPop2( aout_fifo_t * p_fifo )
141 aout_buffer_t *p_buffer = p_fifo->p_first;
142 if( p_buffer != NULL )
144 p_fifo->p_first = p_buffer->p_next;
145 if( p_fifo->p_first == NULL )
146 p_fifo->pp_last = &p_fifo->p_first;
152 /*****************************************************************************
153 * Close: close the audio device
154 *****************************************************************************/
155 static void Close ( vlc_object_t *p_this )
157 audio_output_t *p_aout = (audio_output_t *)p_this;
158 struct aout_sys_t * p_sys = p_aout->sys;
160 msg_Dbg(p_aout, "Stopping AudioQueue");
161 AudioQueueStop(p_sys->audioQueue, false);
162 msg_Dbg(p_aout, "Disposing of AudioQueue");
163 AudioQueueDispose(p_sys->audioQueue, false);
164 aout_PacketDestroy(p_aout);
168 void AudioQueueCallback(void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
169 audio_output_t * p_aout = (audio_output_t *)inUserData;
170 aout_buffer_t * p_buffer = NULL;
173 struct aout_sys_t * p_sys = p_aout->sys;
174 aout_packet_t * packet = &p_sys->packet;
178 vlc_mutex_lock( &packet->lock );
179 p_buffer = aout_FifoPop2( &packet->fifo );
180 vlc_mutex_unlock( &packet->lock );
184 if ( p_buffer != NULL ) {
185 vlc_memcpy( inBuffer->mAudioData, p_buffer->p_buffer, p_buffer->i_buffer );
186 inBuffer->mAudioDataByteSize = p_buffer->i_buffer;
187 aout_BufferFree( p_buffer );
189 vlc_memset( inBuffer->mAudioData, 0, inBuffer->mAudioDataBytesCapacity );
190 inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
192 AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);