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