]> git.sesse.net Git - vlc/blob - modules/audio_output/audioqueue.c
Use var_InheritString for --decklink-video-connection.
[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 FRAME_SIZE 2048
39 #define NUMBER_OF_BUFFERS 3
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     AudioQueueRef audioQueue;
50 };
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  Open               ( vlc_object_t * );
56 static void Close              ( vlc_object_t * );
57 static void Play               ( aout_instance_t * );
58 static void AudioQueueCallback (void *, AudioQueueRef, AudioQueueBufferRef);
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 vlc_module_begin ()
64     set_shortname( "AudioQueue" )
65     set_description( N_("AudioQueue (iOS / Mac OS) audio output") )
66     set_capability( "audio output", 40 )
67     set_category( CAT_AUDIO )
68     set_subcategory( SUBCAT_AUDIO_AOUT )
69     add_shortcut( "audioqueue" )
70     set_callbacks( Open, Close )
71 vlc_module_end ()
72
73 /*****************************************************************************
74  * Open: open the audio device
75  *****************************************************************************/
76
77 static int Open ( vlc_object_t *p_this )
78 {
79     aout_instance_t *p_aout = (aout_instance_t *)p_this;
80     struct aout_sys_t *p_sys = malloc(sizeof(aout_sys_t));
81     p_aout->output.p_sys = p_sys;
82
83     OSStatus status = 0;
84
85     // Setup the audio device.
86     AudioStreamBasicDescription deviceFormat;
87     deviceFormat.mSampleRate = 44100;
88     deviceFormat.mFormatID = kAudioFormatLinearPCM;
89     deviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger; // Signed integer, little endian
90     deviceFormat.mBytesPerPacket = 4;
91     deviceFormat.mFramesPerPacket = 1;
92     deviceFormat.mBytesPerFrame = 4;
93     deviceFormat.mChannelsPerFrame = 2;
94     deviceFormat.mBitsPerChannel = 16;
95     deviceFormat.mReserved = 0;
96
97     // Create a new output AudioQueue for the device.
98     status = AudioQueueNewOutput(&deviceFormat,         // Format
99                                  AudioQueueCallback,    // Callback
100                                  p_aout,                // User data, passed to the callback
101                                  CFRunLoopGetMain(),    // RunLoop
102                                  kCFRunLoopDefaultMode, // RunLoop mode
103                                  0,                     // Flags ; must be zero (per documentation)...
104                                  &(p_sys->audioQueue)); // Output
105
106     // This will be used for boosting the audio without the need of a mixer (floating-point conversion is expensive on ARM)
107     // AudioQueueSetParameter(p_sys->audioQueue, kAudioQueueParam_Volume, 12.0); // Defaults to 1.0
108
109     msg_Dbg(p_aout, "New AudioQueue output created (status = %ld)", status);
110
111     // Allocate buffers for the AudioQueue, and pre-fill them.
112     for (int i = 0; i < NUMBER_OF_BUFFERS; ++i) {
113         AudioQueueBufferRef buffer = NULL;
114         status = AudioQueueAllocateBuffer(p_sys->audioQueue, FRAME_SIZE * 4, &buffer);
115         AudioQueueCallback(NULL, p_sys->audioQueue, buffer);
116     }
117
118     /* Volume is entirely done in software. */
119     aout_VolumeSoftInit( p_aout );
120
121     p_aout->output.output.i_format = VLC_CODEC_S16L;
122     p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
123     p_aout->output.output.i_rate = 44100;
124     p_aout->output.i_nb_samples = FRAME_SIZE;
125     p_aout->output.pf_play = Play;
126
127     msg_Dbg(p_aout, "Starting AudioQueue (status = %ld)", status);
128     status = AudioQueueStart(p_sys->audioQueue, NULL);
129
130     return VLC_SUCCESS;
131 }
132
133 /*****************************************************************************
134  * Play: play a sound samples buffer
135  *****************************************************************************/
136 static void Play( aout_instance_t * p_aout )
137 {
138     VLC_UNUSED(p_aout);
139 }
140
141 /*****************************************************************************
142  * Close: close the audio device
143  *****************************************************************************/
144 static void Close ( vlc_object_t *p_this )
145 {
146     aout_instance_t *p_aout = (aout_instance_t *)p_this;
147     struct aout_sys_t * p_sys = p_aout->output.p_sys;
148
149     msg_Dbg(p_aout, "Stopping AudioQueue");
150     AudioQueueStop(p_sys->audioQueue, false);
151     msg_Dbg(p_aout, "Disposing of AudioQueue");
152     AudioQueueDispose(p_sys->audioQueue, false);
153     free (p_sys);
154 }
155
156 void AudioQueueCallback(void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
157     aout_instance_t * p_aout = (aout_instance_t *)inUserData;
158     aout_buffer_t *   p_buffer = NULL;
159
160     if (p_aout) {
161         vlc_mutex_lock( &p_aout->output_fifo_lock );
162         p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
163         vlc_mutex_unlock( &p_aout->output_fifo_lock );
164     }
165
166     if ( p_buffer != NULL ) {
167         vlc_memcpy( inBuffer->mAudioData, p_buffer->p_buffer, p_buffer->i_buffer );
168         inBuffer->mAudioDataByteSize = p_buffer->i_buffer;
169         aout_BufferFree( p_buffer );
170     } else {
171         vlc_memset( inBuffer->mAudioData, 0, inBuffer->mAudioDataBytesCapacity );
172         inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
173     }
174     AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
175 }