]> git.sesse.net Git - vlc/blob - modules/audio_output/hd1000a.cpp
16f3a933cf549420082e7223a8e3dfd90bff1048
[vlc] / modules / audio_output / hd1000a.cpp
1 /*****************************************************************************
2  * hd1000a.cpp : Roku HD1000 audio output
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Author: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 extern "C"
28 {
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_aout.h>
33
34 #include "aout_internal.h"
35 }
36
37 #include <deschutes/libraries/hdmachinex225/PCMAudioPlayer.h>
38
39 #define FRAME_SIZE 4096
40
41 /*****************************************************************************
42  * aout_sys_t: audio output method descriptor
43  *****************************************************************************
44  * This structure is part of the audio output thread descriptor.
45  * It describes the direct sound specific properties of an audio device.
46  *****************************************************************************/
47 struct aout_sys_t
48 {
49     u32 nAlignment;
50     u32 nSizeMultiple;
51     u32 nBuffers;
52     u32 nBufferSize;
53     void ** ppBuffers;
54     u32 nNextBufferIndex;
55     PCMAudioPlayer * pPlayer;
56 };
57
58 /*****************************************************************************
59  * Local prototypes.
60  *****************************************************************************/
61 static int     Open        ( vlc_object_t * );
62 static void    Close       ( vlc_object_t * );
63
64 static void    Play        ( aout_instance_t * );
65 static int     Thread      ( aout_instance_t * );
66
67 static void    InterleaveS16( int16_t *, int16_t * );
68
69 /*****************************************************************************
70  * Module descriptor
71  *****************************************************************************/
72 vlc_module_begin();
73     set_shortname( "Roku HD1000" );
74     set_description( _("Roku HD1000 audio output") );
75     set_capability( "audio output", 100 );
76     set_category( CAT_AUDIO );
77     set_subcategory( SUBCAT_AUDIO_AOUT );
78     set_callbacks( Open, Close );
79 vlc_module_end();
80
81 /*****************************************************************************
82  * Open: open a dummy audio device
83  *****************************************************************************/
84 static int Open( vlc_object_t * p_this )
85 {
86     aout_instance_t * p_aout = (aout_instance_t *)p_this;
87     struct aout_sys_t * p_sys;
88     PCMAudioPlayer * pPlayer;
89     int i_volume;
90
91     /* Allocate structure */
92     p_aout->output.p_sys = p_sys =
93         (aout_sys_t *)malloc( sizeof( aout_sys_t ) );
94     if( p_aout->output.p_sys == NULL )
95     {
96         msg_Err( p_aout, "out of memory" );
97         return VLC_EGENERIC;
98     }
99
100     /* New PCMAudioPlayer */
101     p_sys->pPlayer = pPlayer = new PCMAudioPlayer();
102     if( p_sys->pPlayer == NULL )
103     {
104         msg_Err( p_aout, "out of memory" );
105         free( p_sys );
106         return VLC_EGENERIC;
107     }
108
109     /* Get Buffer Requirements */
110     if( !pPlayer->GetBufferRequirements( p_sys->nAlignment,
111                                          p_sys->nSizeMultiple,
112                                          p_sys->nBuffers ) )
113     {
114         msg_Err( p_aout, "GetBufferRequirements failed" );
115         delete pPlayer;
116         free( p_sys );
117         return VLC_EGENERIC;
118     } 
119
120     p_sys->nBuffers = __MIN( p_sys->nBuffers, 4 );
121
122     p_sys->ppBuffers = (void **)malloc( p_sys->nBuffers * sizeof( void * ) );
123     if( p_sys->ppBuffers == NULL )
124     {
125         msg_Err( p_aout, "out of memory" );
126         delete pPlayer;
127         free( p_sys );
128         return VLC_EGENERIC;
129     }
130
131     /* Open PCMAudioPlayer */
132     p_sys->nBufferSize = FRAME_SIZE * 4;
133     if( !pPlayer->Open( p_sys->nBuffers, p_sys->nBufferSize,
134                         p_sys->ppBuffers ) )
135     {
136         msg_Err( p_aout, "Open failed" );
137         delete pPlayer;
138         free( p_sys->ppBuffers );
139         free( p_sys );
140         return VLC_EGENERIC;
141     }
142
143     p_sys->nNextBufferIndex = 0;
144
145     if( !pPlayer->SetSampleRate( p_aout->output.output.i_rate ) )
146     {
147         p_aout->output.output.i_rate = 44100;
148         if( !pPlayer->SetSampleRate( p_aout->output.output.i_rate ) )
149         {
150             msg_Err( p_aout, "SetSampleRate failed" );
151             pPlayer->Close();
152             delete pPlayer;
153             free( p_sys->ppBuffers );
154             free( p_sys );
155             return VLC_EGENERIC;
156         }
157     }
158
159     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
160     p_aout->output.i_nb_samples = FRAME_SIZE;
161     p_aout->output.output.i_physical_channels
162             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
163     p_aout->output.pf_play = Play;
164     aout_VolumeSoftInit( p_aout );
165
166     i_volume = config_GetInt( p_aout->p_libvlc, "volume" );
167     pPlayer->SetVolume( (u32)__MIN( i_volume * 64, 0xFFFF ) );
168
169     /* Create thread and wait for its readiness. */
170     if( vlc_thread_create( p_aout, "aout", Thread,
171                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
172     {
173         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
174         pPlayer->Close();
175         delete pPlayer;
176         free( p_sys->ppBuffers );
177         free( p_sys );
178         return VLC_ETHREAD;
179     }
180
181     return VLC_SUCCESS;
182 }
183
184 /*****************************************************************************
185  * Close: close our file
186  *****************************************************************************/
187 static void Close( vlc_object_t * p_this )
188 {
189     u32 i;
190     aout_instance_t * p_aout = (aout_instance_t *)p_this;
191     struct aout_sys_t * p_sys = p_aout->output.p_sys;
192
193     vlc_object_kill( p_aout );
194     vlc_thread_join( p_aout );
195     p_aout->b_die = VLC_FALSE;
196
197     do
198     {
199         i = p_sys->pPlayer->WaitForBuffer();
200     } while( i != 0 && i != p_sys->nBuffers );
201
202     p_sys->pPlayer->Close();
203     delete p_sys->pPlayer;
204
205     free( p_sys->ppBuffers );
206     free( p_sys );
207 }
208
209 /*****************************************************************************
210  * Play: do nothing
211  *****************************************************************************/
212 static void Play( aout_instance_t * p_aout )
213 {
214 }
215
216 /*****************************************************************************
217  * Thread: thread used to DMA the data to the device
218  *****************************************************************************/
219 static int Thread( aout_instance_t * p_aout )
220 {
221     aout_buffer_t * p_buffer;
222     struct aout_sys_t * p_sys = p_aout->output.p_sys;
223     PCMAudioPlayer * pPlayer = p_sys->pPlayer;
224
225     while( !p_aout->b_die )
226     {
227         pPlayer->WaitForBuffer();
228
229         vlc_mutex_lock( &p_aout->output_fifo_lock );
230         p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
231         vlc_mutex_unlock( &p_aout->output_fifo_lock );
232
233 #define i p_sys->nNextBufferIndex
234         if( p_buffer == NULL )
235         {
236             p_aout->p_libvlc->pf_memset( p_sys->ppBuffers[ i ], 0,
237                                       p_sys->nBufferSize ); 
238         }
239         else
240         {
241             InterleaveS16( (int16_t *)p_buffer->p_buffer,
242                            (int16_t *)p_sys->ppBuffers[ i ] );
243             aout_BufferFree( p_buffer );
244         }
245
246         if( !pPlayer->QueueBuffer( (s16 *)p_sys->ppBuffers[ i ],
247                                    p_sys->nBufferSize / 2 ) )
248         {
249             msg_Err( p_aout, "QueueBuffer failed" );
250         } 
251
252         i = (i + 1) % p_sys->nBuffers;
253 #undef i
254     }
255
256     return VLC_SUCCESS;
257 }
258
259 /*****************************************************************************
260  * InterleaveS16: interleave samples
261  *****************************************************************************/
262 static void InterleaveS16( int16_t * p_in, int16_t * p_out )
263 {
264     for( int i = 0; i < FRAME_SIZE; i++ )
265     {
266         p_out[ i * 2 + 0 ] = p_in[ i * 2 + 1 ];
267         p_out[ i * 2 + 1 ] = p_in[ i * 2 + 0 ];
268     }
269 }