1 /*****************************************************************************
2 * hd1000a.cpp : Roku HD1000 audio output
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Author: Jon Lech Johansen <jon-vl@nanocrew.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include "aout_internal.h"
38 #include <deschutes/libraries/hdmachinex225/PCMAudioPlayer.h>
40 #define FRAME_SIZE 4096
42 /*****************************************************************************
43 * aout_sys_t: audio output method descriptor
44 *****************************************************************************
45 * This structure is part of the audio output thread descriptor.
46 * It describes the direct sound specific properties of an audio device.
47 *****************************************************************************/
56 PCMAudioPlayer * pPlayer;
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Open ( vlc_object_t * );
63 static void Close ( vlc_object_t * );
65 static void Play ( aout_instance_t * );
66 static int Thread ( aout_instance_t * );
68 static void InterleaveS16( int16_t *, int16_t * );
70 /*****************************************************************************
72 *****************************************************************************/
74 set_shortname( "Roku HD1000" );
75 set_description( _("Roku HD1000 audio output") );
76 set_capability( "audio output", 100 );
77 set_category( CAT_AUDIO );
78 set_subcategory( SUBCAT_AUDIO_AOUT );
79 set_callbacks( Open, Close );
82 /*****************************************************************************
83 * Open: open a dummy audio device
84 *****************************************************************************/
85 static int Open( vlc_object_t * p_this )
87 aout_instance_t * p_aout = (aout_instance_t *)p_this;
88 struct aout_sys_t * p_sys;
89 PCMAudioPlayer * pPlayer;
92 /* Allocate structure */
93 p_aout->output.p_sys = p_sys =
94 (aout_sys_t *)malloc( sizeof( aout_sys_t ) );
95 if( p_aout->output.p_sys == NULL )
97 msg_Err( p_aout, "out of memory" );
101 /* New PCMAudioPlayer */
102 p_sys->pPlayer = pPlayer = new PCMAudioPlayer();
103 if( p_sys->pPlayer == NULL )
105 msg_Err( p_aout, "out of memory" );
110 /* Get Buffer Requirements */
111 if( !pPlayer->GetBufferRequirements( p_sys->nAlignment,
112 p_sys->nSizeMultiple,
115 msg_Err( p_aout, "GetBufferRequirements failed" );
121 p_sys->nBuffers = __MIN( p_sys->nBuffers, 4 );
123 p_sys->ppBuffers = (void **)malloc( p_sys->nBuffers * sizeof( void * ) );
124 if( p_sys->ppBuffers == NULL )
126 msg_Err( p_aout, "out of memory" );
132 /* Open PCMAudioPlayer */
133 p_sys->nBufferSize = FRAME_SIZE * 4;
134 if( !pPlayer->Open( p_sys->nBuffers, p_sys->nBufferSize,
137 msg_Err( p_aout, "Open failed" );
139 free( p_sys->ppBuffers );
144 p_sys->nNextBufferIndex = 0;
146 if( !pPlayer->SetSampleRate( p_aout->output.output.i_rate ) )
148 p_aout->output.output.i_rate = 44100;
149 if( !pPlayer->SetSampleRate( p_aout->output.output.i_rate ) )
151 msg_Err( p_aout, "SetSampleRate failed" );
154 free( p_sys->ppBuffers );
160 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
161 p_aout->output.i_nb_samples = FRAME_SIZE;
162 p_aout->output.output.i_physical_channels
163 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
164 p_aout->output.pf_play = Play;
165 aout_VolumeSoftInit( p_aout );
167 i_volume = config_GetInt( p_aout->p_libvlc, "volume" );
168 pPlayer->SetVolume( (u32)__MIN( i_volume * 64, 0xFFFF ) );
170 /* Create thread and wait for its readiness. */
171 if( vlc_thread_create( p_aout, "aout", Thread,
172 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
174 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
177 free( p_sys->ppBuffers );
185 /*****************************************************************************
186 * Close: close our file
187 *****************************************************************************/
188 static void Close( vlc_object_t * p_this )
191 aout_instance_t * p_aout = (aout_instance_t *)p_this;
192 struct aout_sys_t * p_sys = p_aout->output.p_sys;
194 vlc_object_kill( p_aout );
195 vlc_thread_join( p_aout );
196 p_aout->b_die = VLC_FALSE;
200 i = p_sys->pPlayer->WaitForBuffer();
201 } while( i != 0 && i != p_sys->nBuffers );
203 p_sys->pPlayer->Close();
204 delete p_sys->pPlayer;
206 free( p_sys->ppBuffers );
210 /*****************************************************************************
212 *****************************************************************************/
213 static void Play( aout_instance_t * p_aout )
217 /*****************************************************************************
218 * Thread: thread used to DMA the data to the device
219 *****************************************************************************/
220 static int Thread( aout_instance_t * p_aout )
222 aout_buffer_t * p_buffer;
223 struct aout_sys_t * p_sys = p_aout->output.p_sys;
224 PCMAudioPlayer * pPlayer = p_sys->pPlayer;
226 while( !p_aout->b_die )
228 pPlayer->WaitForBuffer();
230 vlc_mutex_lock( &p_aout->output_fifo_lock );
231 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
232 vlc_mutex_unlock( &p_aout->output_fifo_lock );
234 #define i p_sys->nNextBufferIndex
235 if( p_buffer == NULL )
237 p_aout->p_libvlc->pf_memset( p_sys->ppBuffers[ i ], 0,
238 p_sys->nBufferSize );
242 InterleaveS16( (int16_t *)p_buffer->p_buffer,
243 (int16_t *)p_sys->ppBuffers[ i ] );
244 aout_BufferFree( p_buffer );
247 if( !pPlayer->QueueBuffer( (s16 *)p_sys->ppBuffers[ i ],
248 p_sys->nBufferSize / 2 ) )
250 msg_Err( p_aout, "QueueBuffer failed" );
253 i = (i + 1) % p_sys->nBuffers;
260 /*****************************************************************************
261 * InterleaveS16: interleave samples
262 *****************************************************************************/
263 static void InterleaveS16( int16_t * p_in, int16_t * p_out )
265 for( int i = 0; i < FRAME_SIZE; i++ )
267 p_out[ i * 2 + 0 ] = p_in[ i * 2 + 1 ];
268 p_out[ i * 2 + 1 ] = p_in[ i * 2 + 0 ];