hd1000a.cpp 8.02 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 142 143 144 145 146 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 207 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
/*****************************************************************************
 * hd1000a.cpp : Roku HD1000 audio output
 *****************************************************************************
 * Copyright (C) 2004 VideoLAN
 * $Id$
 *
 * Author: Jon Lech Johansen <jon-vl@nanocrew.net>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
extern "C"
{
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <vlc/vlc.h>
#include <vlc/aout.h>

#include "aout_internal.h"
}

#include <deschutes/libraries/hdmachinex225/PCMAudioPlayer.h>

#define FRAME_SIZE 4096

/*****************************************************************************
 * 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
{
    u32 nAlignment;
    u32 nSizeMultiple;
    u32 nBuffers;
    u32 nBufferSize;
    void ** ppBuffers;
    u32 nNextBufferIndex;
    PCMAudioPlayer * pPlayer;
};

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int     Open        ( vlc_object_t * );
static void    Close       ( vlc_object_t * );

static void    Play        ( aout_instance_t * );
static int     Thread      ( aout_instance_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( N_("HD1000 audio output") );
    set_capability( "audio output", 100 );
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * Open: open a dummy audio device
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
    aout_instance_t * p_aout = (aout_instance_t *)p_this;
    struct aout_sys_t * p_sys;
    PCMAudioPlayer * pPlayer;
    int i_volume;

    /* Allocate structure */
    p_aout->output.p_sys = p_sys =
        (aout_sys_t *)malloc( sizeof( aout_sys_t ) );
    if( p_aout->output.p_sys == NULL )
    {
        msg_Err( p_aout, "out of memory" );
        return VLC_EGENERIC;
    }

    /* New PCMAudioPlayer */
    p_sys->pPlayer = pPlayer = new PCMAudioPlayer();
    if( p_sys->pPlayer == NULL )
    {
        msg_Err( p_aout, "out of memory" );
        free( p_sys );
        return VLC_EGENERIC;
    }

    /* Get Buffer Requirements */
    if( !pPlayer->GetBufferRequirements( p_sys->nAlignment,
                                         p_sys->nSizeMultiple,
                                         p_sys->nBuffers ) )
    {
        msg_Err( p_aout, "GetBufferRequirements failed" );
        delete pPlayer;
        free( p_sys );
        return VLC_EGENERIC;
    } 

    p_sys->nBuffers = __MIN( p_sys->nBuffers, 4 );

    p_sys->ppBuffers = (void **)malloc( p_sys->nBuffers * sizeof( void * ) );
    if( p_sys->ppBuffers == NULL )
    {
        msg_Err( p_aout, "out of memory" );
        delete pPlayer;
        free( p_sys );
        return VLC_EGENERIC;
    }

    /* Open PCMAudioPlayer */
    p_sys->nBufferSize = FRAME_SIZE * 4;
    if( !pPlayer->Open( p_sys->nBuffers, p_sys->nBufferSize,
                        p_sys->ppBuffers ) )
    {
        msg_Err( p_aout, "Open failed" );
        delete pPlayer;
        free( p_sys->ppBuffers );
        free( p_sys );
        return VLC_EGENERIC;
    }

    p_sys->nNextBufferIndex = 0;

    if( !pPlayer->SetSampleRate( p_aout->output.output.i_rate ) )
    {
        p_aout->output.output.i_rate = 44100;
        if( !pPlayer->SetSampleRate( p_aout->output.output.i_rate ) )
        {
            msg_Err( p_aout, "SetSampleRate failed" );
            pPlayer->Close();
            delete pPlayer;
            free( p_sys->ppBuffers );
            free( p_sys );
            return VLC_EGENERIC;
        }
    }

    p_aout->output.output.i_format = AOUT_FMT_S16_NE;
    p_aout->output.i_nb_samples = FRAME_SIZE;
    p_aout->output.output.i_physical_channels
            = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
    p_aout->output.pf_play = Play;
    aout_VolumeSoftInit( p_aout );

    i_volume = config_GetInt( p_aout->p_vlc, "volume" );
    pPlayer->SetVolume( (u32)__MIN( i_volume * 64, 0xFFFF ) );

    /* Create thread and wait for its readiness. */
    if( vlc_thread_create( p_aout, "aout", Thread,
                           VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
    {
        msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
        pPlayer->Close();
        delete pPlayer;
        free( p_sys->ppBuffers );
        free( p_sys );
        return VLC_ETHREAD;
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: close our file
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
    u32 i;
    aout_instance_t * p_aout = (aout_instance_t *)p_this;
    struct aout_sys_t * p_sys = p_aout->output.p_sys;

    p_aout->b_die = VLC_TRUE;
    vlc_thread_join( p_aout );
    p_aout->b_die = VLC_FALSE;

    do
    {
        i = p_sys->pPlayer->WaitForBuffer();
    } while( i != 0 && i != p_sys->nBuffers );

    p_sys->pPlayer->Close();
    delete p_sys->pPlayer;

    free( p_sys->ppBuffers );
    free( p_sys );
}

/*****************************************************************************
 * Play: do nothing
 *****************************************************************************/
static void Play( aout_instance_t * p_aout )
{
}

/*****************************************************************************
 * Thread: thread used to DMA the data to the device
 *****************************************************************************/
static int Thread( aout_instance_t * p_aout )
{
    aout_buffer_t * p_buffer;
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
    PCMAudioPlayer * pPlayer = p_sys->pPlayer;

    while( !p_aout->b_die )
    {
        pPlayer->WaitForBuffer();

        vlc_mutex_lock( &p_aout->output_fifo_lock );
        p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
        vlc_mutex_unlock( &p_aout->output_fifo_lock );

#define i p_sys->nNextBufferIndex
        if( p_buffer == NULL )
        {
            p_aout->p_vlc->pf_memset( p_sys->ppBuffers[ i ], 0,
                                      p_sys->nBufferSize ); 
        }
        else
        {
            p_aout->p_vlc->pf_memcpy( p_sys->ppBuffers[ i ],
                                      p_buffer->p_buffer,
                                      p_sys->nBufferSize );
            aout_BufferFree( p_buffer );
        }

        if( !pPlayer->QueueBuffer( (s16 *)p_sys->ppBuffers[ i ],
                                   p_sys->nBufferSize / 2 ) )
        {
            msg_Err( p_aout, "QueueBuffer failed" );
        } 

        i = (i + 1) % p_sys->nBuffers;
#undef i
    }

    return VLC_SUCCESS;
}