]> git.sesse.net Git - vlc/blob - modules/audio_output/audioqueue.c
AudioTrack: blind fix for compilation
[vlc] / modules / audio_output / audioqueue.c
1 /*****************************************************************************
2  * audioqueue.c : AudioQueue audio output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2010 VideoLAN and AUTHORS
5  *
6  * Authors: Romain Goyet <romain.goyet@likid.org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include <unistd.h>                                      /* write(), close() */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_aout.h>
35
36 #include <AudioToolBox/AudioToolBox.h>
37
38 #define NUMBER_OF_BUFFERS 3
39 #define FRAME_SIZE 2048
40
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  *****************************************************************************/
47 struct aout_sys_t
48 {
49     aout_packet_t packet;
50     AudioQueueRef audioQueue;
51     float soft_gain;
52     bool soft_mute;
53 };
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  Open               ( vlc_object_t * );
59 static void Close              ( vlc_object_t * );
60 static void Play               ( audio_output_t *, block_t *, mtime_t * );
61 static void AudioQueueCallback (void *, AudioQueueRef, AudioQueueBufferRef);
62
63 #include "volume.h"
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 vlc_module_begin ()
69     set_shortname( "AudioQueue" )
70     set_description( N_("AudioQueue (iOS / Mac OS) audio output") )
71     set_capability( "audio output", 40 )
72     set_category( CAT_AUDIO )
73     set_subcategory( SUBCAT_AUDIO_AOUT )
74     add_shortcut( "audioqueue" )
75     set_callbacks( Open, Close )
76 vlc_module_end ()
77
78 /*****************************************************************************
79  * Open: open the audio device
80  *****************************************************************************/
81
82 static int Open ( vlc_object_t *p_this )
83 {
84     audio_output_t *p_aout = (audio_output_t *)p_this;
85     struct aout_sys_t *p_sys = malloc(sizeof(aout_sys_t));
86     p_aout->sys = p_sys;
87
88     OSStatus status = 0;
89
90     // Setup the audio device.
91     AudioStreamBasicDescription deviceFormat;
92     deviceFormat.mSampleRate = 44100;
93     deviceFormat.mFormatID = kAudioFormatLinearPCM;
94     deviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger; // Signed integer, little endian
95     deviceFormat.mBytesPerPacket = 4;
96     deviceFormat.mFramesPerPacket = 1;
97     deviceFormat.mBytesPerFrame = 4;
98     deviceFormat.mChannelsPerFrame = 2;
99     deviceFormat.mBitsPerChannel = 16;
100     deviceFormat.mReserved = 0;
101
102     // Create a new output AudioQueue for the device.
103     status = AudioQueueNewOutput(&deviceFormat,         // Format
104                                  AudioQueueCallback,    // Callback
105                                  p_aout,                // User data, passed to the callback
106                                  CFRunLoopGetMain(),    // RunLoop
107                                  kCFRunLoopDefaultMode, // RunLoop mode
108                                  0,                     // Flags ; must be zero (per documentation)...
109                                  &(p_sys->audioQueue)); // Output
110
111     // This will be used for boosting the audio without the need of a mixer (floating-point conversion is expensive on ARM)
112     // AudioQueueSetParameter(p_sys->audioQueue, kAudioQueueParam_Volume, 12.0); // Defaults to 1.0
113
114     msg_Dbg(p_aout, "New AudioQueue output created (status = %i)", status);
115
116     // Allocate buffers for the AudioQueue, and pre-fill them.
117     for (int i = 0; i < NUMBER_OF_BUFFERS; ++i) {
118         AudioQueueBufferRef buffer = NULL;
119         status = AudioQueueAllocateBuffer(p_sys->audioQueue, FRAME_SIZE * 4, &buffer);
120         AudioQueueCallback(NULL, p_sys->audioQueue, buffer);
121     }
122
123     /* Volume is entirely done in software. */
124     aout_SoftVolumeInit( p_aout );
125
126     p_aout->format.i_format = VLC_CODEC_S16L;
127     p_aout->format.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
128     p_aout->format.i_rate = 44100;
129     aout_PacketInit(p_aout, &p_sys->packet, FRAME_SIZE);
130     p_aout->pf_play = aout_PacketPlay;
131     p_aout->pf_pause = aout_PacketPause;
132     p_aout->pf_flush = aout_PacketFlush;
133
134     msg_Dbg(p_aout, "Starting AudioQueue (status = %i)", status);
135     status = AudioQueueStart(p_sys->audioQueue, NULL);
136
137     return VLC_SUCCESS;
138 }
139
140 /*****************************************************************************
141   * aout_FifoPop : get the next buffer out of the FIFO
142   *****************************************************************************/
143 static block_t *aout_FifoPop2( aout_fifo_t * p_fifo )
144  {
145      block_t *p_buffer = p_fifo->p_first;
146      if( p_buffer != NULL )
147      {
148          p_fifo->p_first = p_buffer->p_next;
149          if( p_fifo->p_first == NULL )
150              p_fifo->pp_last = &p_fifo->p_first;
151      }
152      return p_buffer;
153  }
154
155
156 /*****************************************************************************
157  * Close: close the audio device
158  *****************************************************************************/
159 static void Close ( vlc_object_t *p_this )
160 {
161     audio_output_t *p_aout = (audio_output_t *)p_this;
162     struct aout_sys_t * p_sys = p_aout->sys;
163
164     msg_Dbg(p_aout, "Stopping AudioQueue");
165     AudioQueueStop(p_sys->audioQueue, false);
166     msg_Dbg(p_aout, "Disposing of AudioQueue");
167     AudioQueueDispose(p_sys->audioQueue, false);
168     aout_PacketDestroy(p_aout);
169     free (p_sys);
170 }
171
172 void AudioQueueCallback(void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
173     audio_output_t * p_aout = (audio_output_t *)inUserData;
174     block_t *   p_buffer = NULL;
175
176     if (p_aout) {
177         struct aout_sys_t * p_sys = p_aout->sys;
178         aout_packet_t * packet = &p_sys->packet;
179
180         if (packet)
181         {
182             vlc_mutex_lock( &packet->lock );
183             p_buffer = aout_FifoPop2( &packet->fifo );
184             vlc_mutex_unlock( &packet->lock );
185         }
186     }
187
188     if ( p_buffer != NULL ) {
189         memcpy( inBuffer->mAudioData, p_buffer->p_buffer, p_buffer->i_buffer );
190         inBuffer->mAudioDataByteSize = p_buffer->i_buffer;
191         block_Release( p_buffer );
192     } else {
193         memset( inBuffer->mAudioData, 0, inBuffer->mAudioDataBytesCapacity );
194         inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
195     }
196     AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
197 }