audioqueue.c 7.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*****************************************************************************
 * audioqueue.c : AudioQueue audio output plugin for vlc
 *****************************************************************************
 * Copyright (C) 2010 VideoLAN and AUTHORS
 *
 * Authors: Romain Goyet <romain.goyet@likid.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
 * (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.
 *
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <unistd.h>                                      /* write(), close() */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_aout.h>

#include <AudioToolBox/AudioToolBox.h>

#define NUMBER_OF_BUFFERS 3
39
#define FRAME_SIZE 2048
40 41 42 43 44 45 46 47 48

/*****************************************************************************
 * 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
{
49
    aout_packet_t packet;
50
    AudioQueueRef audioQueue;
51 52
    float soft_gain;
    bool soft_mute;
53 54 55 56 57 58 59
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open               ( vlc_object_t * );
static void Close              ( vlc_object_t * );
60
static void Play               ( audio_output_t *, block_t *, mtime_t * );
61 62
static void AudioQueueCallback (void *, AudioQueueRef, AudioQueueBufferRef);

63 64
#include "volume.h"

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
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 )
vlc_module_end ()

/*****************************************************************************
 * Open: open the audio device
 *****************************************************************************/

static int Open ( vlc_object_t *p_this )
{
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    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

114
    msg_Dbg(p_aout, "New AudioQueue output created (status = %i)", status);
115 116 117 118 119 120 121 122 123

    // 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);
    }

    /* Volume is entirely done in software. */
124
    aout_SoftVolumeInit( p_aout );
125

126 127 128
    p_aout->format.i_format = VLC_CODEC_S16L;
    p_aout->format.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
    p_aout->format.i_rate = 44100;
129
    aout_PacketInit(p_aout, &p_sys->packet, FRAME_SIZE);
130 131 132
    p_aout->pf_play = aout_PacketPlay;
    p_aout->pf_pause = aout_PacketPause;
    p_aout->pf_flush = aout_PacketFlush;
133

134
    msg_Dbg(p_aout, "Starting AudioQueue (status = %i)", status);
135 136 137 138 139
    status = AudioQueueStart(p_sys->audioQueue, NULL);

    return VLC_SUCCESS;
}

140 141 142
/*****************************************************************************
  * aout_FifoPop : get the next buffer out of the FIFO
  *****************************************************************************/
143
static block_t *aout_FifoPop2( aout_fifo_t * p_fifo )
144
 {
145
     block_t *p_buffer = p_fifo->p_first;
146 147 148 149 150 151 152 153 154 155
     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;
 }


156 157 158 159 160
/*****************************************************************************
 * Close: close the audio device
 *****************************************************************************/
static void Close ( vlc_object_t *p_this )
{
161
    audio_output_t *p_aout = (audio_output_t *)p_this;
162
    struct aout_sys_t * p_sys = p_aout->sys;
163 164 165 166 167

    msg_Dbg(p_aout, "Stopping AudioQueue");
    AudioQueueStop(p_sys->audioQueue, false);
    msg_Dbg(p_aout, "Disposing of AudioQueue");
    AudioQueueDispose(p_sys->audioQueue, false);
168
    aout_PacketDestroy(p_aout);
169 170 171 172
    free (p_sys);
}

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 178 179 180 181 182 183 184 185
        struct aout_sys_t * p_sys = p_aout->sys;
        aout_packet_t * packet = &p_sys->packet;

        if (packet)
        {
            vlc_mutex_lock( &packet->lock );
            p_buffer = aout_FifoPop2( &packet->fifo );
            vlc_mutex_unlock( &packet->lock );
        }
186 187 188
    }

    if ( p_buffer != NULL ) {
Rafaël Carré's avatar
Rafaël Carré committed
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 {
Rafaël Carré's avatar
Rafaël Carré committed
193
        memset( inBuffer->mAudioData, 0, inBuffer->mAudioDataBytesCapacity );
194 195 196 197
        inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
    }
    AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
}