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