opensles_android.c 13.3 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/*****************************************************************************
 * opensles_android.c : audio output for android native code
 *****************************************************************************
 * Copyright © 2011 VideoLAN
 *
 * Authors: Dominique Martinet <asmadeus@codewreck.org>
 *          Hugo Beauzée-Luyssen <beauze.h@gmail.com>
 *
 * 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
 *****************************************************************************/

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

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

// For native audio
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

// Maximum number of buffers to enqueue.
#define BUFF_QUEUE  42

/*****************************************************************************
 * aout_sys_t: audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes the direct sound specific properties of an audio device.
 *****************************************************************************/
struct aout_sys_t
{
    SLObjectItf                     engineObject;
    SLEngineItf                     engineEngine;
    SLObjectItf                     outputMixObject;
    SLAndroidSimpleBufferQueueItf   playerBufferQueue;
    SLObjectItf                     playerObject;
    SLPlayItf                       playerPlay;
    aout_buffer_t                 * p_buffer_array[BUFF_QUEUE];
    int                             i_toclean_buffer;
    int                             i_toappend_buffer;
    SLInterfaceID                 * SL_IID_ENGINE;
    SLInterfaceID                 * SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
    SLInterfaceID                 * SL_IID_VOLUME;
    SLInterfaceID                 * SL_IID_PLAY;
    void                          * p_so_handle;
};

typedef SLresult (*slCreateEngine_t)(
        SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
        const SLInterfaceID*, const SLboolean* );

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int  Open        ( vlc_object_t * );
static void Close       ( vlc_object_t * );
static void Play        ( aout_instance_t * );
static void PlayedCallback ( SLAndroidSimpleBufferQueueItf caller,  void *pContext);

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

vlc_module_begin ()
    set_description( N_("OpenSLES audio output") )
    set_shortname( N_("OpenSLES") )
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_AOUT )

    set_capability( "audio output", 170 )
    add_shortcut( "opensles", "android" )
    set_callbacks( Open, Close )
vlc_module_end ()


#define CHECK_OPENSL_ERROR( res, msg )              \
    if( unlikely( res != SL_RESULT_SUCCESS ) )      \
    {                                               \
        msg_Err( p_aout, msg" (%lu)", res );        \
        goto error;                                 \
    }

#define OPENSL_DLSYM( dest, handle, name )                   \
    dest = (typeof(dest))dlsym( handle, name );              \
    if( dest == NULL )                                       \
    {                                                        \
        msg_Err( p_aout, "Failed to load symbol %s", name ); \
        goto error;                                          \
    }

static void Clear( aout_sys_t *p_sys )
{
    // Destroy buffer queue audio player object
    // and invalidate all associated interfaces
    if( p_sys->playerObject != NULL )
        (*p_sys->playerObject)->Destroy( p_sys->playerObject );

    // destroy output mix object, and invalidate all associated interfaces
    if( p_sys->outputMixObject != NULL )
        (*p_sys->outputMixObject)->Destroy( p_sys->outputMixObject );

    // destroy engine object, and invalidate all associated interfaces
    if( p_sys->engineObject != NULL )
        (*p_sys->engineObject)->Destroy( p_sys->engineObject );

    if( p_sys->p_so_handle != NULL )
        dlclose( p_sys->p_so_handle );

    free( p_sys );
}

/*****************************************************************************
 * Open: open a dummy audio device
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
    aout_instance_t     *p_aout = (aout_instance_t *)p_this;
    SLresult            result;

    /* Allocate structure */
142 143
    p_aout->sys = malloc( sizeof( aout_sys_t ) );
    if( unlikely( p_aout->sys == NULL ) )
144 145
        return VLC_ENOMEM;

146
    aout_sys_t * p_sys = p_aout->sys;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    p_sys->playerObject     = NULL;
    p_sys->engineObject     = NULL;
    p_sys->outputMixObject  = NULL;
    p_sys->i_toclean_buffer = 0;
    p_sys->i_toappend_buffer= 0;

    //Acquiring LibOpenSLES symbols :
    p_sys->p_so_handle = dlopen( "libOpenSLES.so", RTLD_NOW );
    if( p_sys->p_so_handle == NULL )
    {
        msg_Err( p_aout, "Failed to load libOpenSLES" );
        goto error;
    }

    slCreateEngine_t    slCreateEnginePtr = NULL;

    OPENSL_DLSYM( slCreateEnginePtr, p_sys->p_so_handle, "slCreateEngine" );
    OPENSL_DLSYM( p_sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE, p_sys->p_so_handle,
                 "SL_IID_ANDROIDSIMPLEBUFFERQUEUE" );
    OPENSL_DLSYM( p_sys->SL_IID_ENGINE, p_sys->p_so_handle, "SL_IID_ENGINE" );
    OPENSL_DLSYM( p_sys->SL_IID_PLAY, p_sys->p_so_handle, "SL_IID_PLAY" );
    OPENSL_DLSYM( p_sys->SL_IID_VOLUME, p_sys->p_so_handle, "SL_IID_VOLUME" );

    // create engine
    result = slCreateEnginePtr( &p_sys->engineObject, 0, NULL, 0, NULL, NULL );
    CHECK_OPENSL_ERROR( result, "Failed to create engine" );

    // realize the engine in synchronous mode
    result = (*p_sys->engineObject)->Realize( p_sys->engineObject,
                                             SL_BOOLEAN_FALSE );
    CHECK_OPENSL_ERROR( result, "Failed to realize engine" );

    // get the engine interface, needed to create other objects
    result = (*p_sys->engineObject)->GetInterface( p_sys->engineObject,
                                        *p_sys->SL_IID_ENGINE, &p_sys->engineEngine );
    CHECK_OPENSL_ERROR( result, "Failed to get the engine interface" );

    // create output mix, with environmental reverb specified as a non-required interface
    const SLInterfaceID ids1[] = { *p_sys->SL_IID_VOLUME };
    const SLboolean req1[] = { SL_BOOLEAN_FALSE };
    result = (*p_sys->engineEngine)->CreateOutputMix( p_sys->engineEngine,
                                        &p_sys->outputMixObject, 1, ids1, req1 );
    CHECK_OPENSL_ERROR( result, "Failed to create output mix" );

    // realize the output mix in synchronous mode
    result = (*p_sys->outputMixObject)->Realize( p_sys->outputMixObject,
                                                 SL_BOOLEAN_FALSE );
    CHECK_OPENSL_ERROR( result, "Failed to realize output mix" );


    // configure audio source - this defines the number of samples you can enqueue.
    SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
        SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
        BUFF_QUEUE
    };

    SLDataFormat_PCM format_pcm;
    format_pcm.formatType       = SL_DATAFORMAT_PCM;
    format_pcm.numChannels      = 2;
207
    format_pcm.samplesPerSec    = ((SLuint32) p_aout->format.i_rate * 1000) ;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    format_pcm.bitsPerSample    = SL_PCMSAMPLEFORMAT_FIXED_16;
    format_pcm.containerSize    = SL_PCMSAMPLEFORMAT_FIXED_16;
    format_pcm.channelMask      = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
    format_pcm.endianness       = SL_BYTEORDER_LITTLEENDIAN;

    SLDataSource audioSrc = {&loc_bufq, &format_pcm};

    // configure audio sink
    SLDataLocator_OutputMix loc_outmix = {
        SL_DATALOCATOR_OUTPUTMIX,
        p_sys->outputMixObject
    };
    SLDataSink audioSnk = {&loc_outmix, NULL};

    //create audio player
    const SLInterfaceID ids2[] = { *p_sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
    const SLboolean     req2[] = { SL_BOOLEAN_TRUE };
    result = (*p_sys->engineEngine)->CreateAudioPlayer( p_sys->engineEngine,
                                    &p_sys->playerObject, &audioSrc,
                                    &audioSnk, sizeof( ids2 ) / sizeof( *ids2 ),
                                    ids2, req2 );
    CHECK_OPENSL_ERROR( result, "Failed to create audio player" );

    // realize the player
    result = (*p_sys->playerObject)->Realize( p_sys->playerObject,
                                              SL_BOOLEAN_FALSE );
    CHECK_OPENSL_ERROR( result, "Failed to realize player object." );

    // get the play interface
    result = (*p_sys->playerObject)->GetInterface( p_sys->playerObject,
                                                  *p_sys->SL_IID_PLAY, &p_sys->playerPlay );
    CHECK_OPENSL_ERROR( result, "Failed to get player interface." );

    // get the buffer queue interface
    result = (*p_sys->playerObject)->GetInterface( p_sys->playerObject,
                                                  *p_sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
                                                  &p_sys->playerBufferQueue );
    CHECK_OPENSL_ERROR( result, "Failed to get buff queue interface" );

    result = (*p_sys->playerBufferQueue)->RegisterCallback( p_sys->playerBufferQueue,
                                                            PlayedCallback,
                                                            (void*)p_sys);
    CHECK_OPENSL_ERROR( result, "Failed to register buff queue callback." );


    // set the player's state to playing
    result = (*p_sys->playerPlay)->SetPlayState( p_sys->playerPlay,
                                                 SL_PLAYSTATE_PLAYING );
    CHECK_OPENSL_ERROR( result, "Failed to switch to playing state" );

    // we want 16bit signed data little endian.
259 260 261 262 263
    p_aout->format.i_format              = VLC_CODEC_S16L;
    p_aout->i_nb_samples                 = 2048;
    p_aout->format.i_physical_channels   = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
    p_aout->pf_play                      = Play;
    p_aout->pf_pause = NULL;
264

265
    aout_FormatPrepare( &p_aout->format );
266 267 268 269 270 271 272 273 274 275 276 277 278

    return VLC_SUCCESS;
error:
    Clear( p_sys );
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Close: close our file
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
    aout_instance_t *p_aout = (aout_instance_t *)p_this;
279
    aout_sys_t      *p_sys = p_aout->sys;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

    msg_Dbg( p_aout, "Closing OpenSLES" );

    (*p_sys->playerPlay)->SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
    //Flush remaining buffers if any.
    if( p_sys->playerBufferQueue != NULL )
        (*p_sys->playerBufferQueue)->Clear( p_sys->playerBufferQueue );
    Clear( p_sys );
}

/*****************************************************************************
 * Play: play a sound
 *****************************************************************************/
static void Play( aout_instance_t * p_aout )
{
295
    aout_sys_t * p_sys = p_aout->sys;
296 297 298 299
    aout_buffer_t *p_buffer;

    SLresult result;

300
    p_buffer = aout_FifoPop(&p_aout->fifo);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    if( p_buffer != NULL )
    {
        for (;;)
        {
            result = (*p_sys->playerBufferQueue)->Enqueue(
                            p_sys->playerBufferQueue, p_buffer->p_buffer,
                            p_buffer->i_buffer );
            if( result == SL_RESULT_SUCCESS )
                break;
            if ( result != SL_RESULT_BUFFER_INSUFFICIENT )
            {
                msg_Warn( p_aout, "Dropping invalid buffer" );
                aout_BufferFree( p_buffer );
                return ;
            }

            msg_Err( p_aout, "write error (%lu)", result );

            // Wait a bit to retry. might miss calls to *cancel
            // but this is supposed to be rare anyway
            msleep(CLOCK_FREQ);
        }
        p_sys->p_buffer_array[p_sys->i_toappend_buffer] = p_buffer;
        if( ++p_sys->i_toappend_buffer == BUFF_QUEUE )
            p_sys->i_toappend_buffer = 0;
    }
    else
        msg_Err( p_aout, "nothing to play?" );
}

static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
{
    aout_sys_t *p_sys = (aout_sys_t*)pContext;

    assert (caller == p_sys->playerBufferQueue);

    aout_BufferFree( p_sys->p_buffer_array[p_sys->i_toclean_buffer] );
    if( ++p_sys->i_toclean_buffer == BUFF_QUEUE )
        p_sys->i_toclean_buffer = 0;
}