]> git.sesse.net Git - vlc/blob - modules/audio_output/kai.c
direct3d11: catch texture mapping errors
[vlc] / modules / audio_output / kai.c
1 /*****************************************************************************
2  * kai.c : KAI audio output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2010-2013 VLC authors and VideoLAN
5  *
6  * Authors: KO Myung-Hun <komh@chollian.net>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_aout.h>
33
34 #include <float.h>
35
36 #include <kai.h>
37
38 #define FRAME_SIZE 2048
39
40 #define AUDIO_BUFFER_SIZE_IN_SECONDS ( AOUT_MAX_ADVANCE_TIME / CLOCK_FREQ )
41
42 struct audio_buffer_t
43 {
44     uint8_t    *data;
45     int         read_pos;
46     int         write_pos;
47     int         length;
48     int         size;
49     vlc_mutex_t mutex;
50     vlc_cond_t  cond;
51 };
52
53 typedef struct audio_buffer_t audio_buffer_t;
54
55 /*****************************************************************************
56  * aout_sys_t: KAI audio output method descriptor
57  *****************************************************************************
58  * This structure is part of the audio output thread descriptor.
59  * It describes the specific properties of an audio device.
60  *****************************************************************************/
61 struct aout_sys_t
62 {
63     audio_buffer_t *buffer;
64     HKAI            hkai;
65     float           soft_gain;
66     bool            soft_mute;
67     audio_sample_format_t format;
68 };
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static int  Open    ( vlc_object_t * );
74 static void Close   ( vlc_object_t * );
75 static void Play    ( audio_output_t *_p_aout, block_t *block );
76 static void Pause   ( audio_output_t *, bool, mtime_t );
77 static void Flush   ( audio_output_t *, bool );
78 static int  TimeGet ( audio_output_t *, mtime_t *restrict );
79
80 static ULONG APIENTRY KaiCallback ( PVOID, PVOID, ULONG );
81
82 static int  CreateBuffer ( audio_output_t *, int );
83 static void DestroyBuffer( audio_output_t * );
84 static int  ReadBuffer   ( audio_output_t *, uint8_t *, int );
85 static int  WriteBuffer  ( audio_output_t *, uint8_t *, int );
86
87 #include "audio_output/volume.h"
88
89 /*****************************************************************************
90  * Module descriptor
91  *****************************************************************************/
92 #define KAI_AUDIO_DEVICE_TEXT N_( \
93     "Device" )
94 #define KAI_AUDIO_DEVICE_LONGTEXT N_( \
95     "Select a proper audio device to be used by KAI." )
96
97 #define KAI_AUDIO_EXCLUSIVE_MODE_TEXT N_( \
98     "Open audio in exclusive mode." )
99 #define KAI_AUDIO_EXCLUSIVE_MODE_LONGTEXT N_( \
100     "Enable this option if you want your audio not to be interrupted by the " \
101     "other audio." )
102
103 static const char *const ppsz_kai_audio_device[] = {
104     "auto", "dart", "uniaud" };
105 static const char *const ppsz_kai_audio_device_text[] = {
106     N_("Auto"), "DART", "UNIAUD" };
107
108 vlc_module_begin ()
109     set_shortname( "KAI" )
110     set_description( N_("K Audio Interface audio output") )
111     set_capability( "audio output", 100 )
112     set_category( CAT_AUDIO )
113     set_subcategory( SUBCAT_AUDIO_AOUT )
114     add_string( "kai-audio-device", ppsz_kai_audio_device[0],
115                 KAI_AUDIO_DEVICE_TEXT, KAI_AUDIO_DEVICE_LONGTEXT, false )
116         change_string_list( ppsz_kai_audio_device, ppsz_kai_audio_device_text )
117     add_sw_gain( )
118     add_bool( "kai-audio-exclusive-mode", false,
119               KAI_AUDIO_EXCLUSIVE_MODE_TEXT, KAI_AUDIO_EXCLUSIVE_MODE_LONGTEXT,
120               true )
121     set_callbacks( Open, Close )
122 vlc_module_end ()
123
124 /*****************************************************************************
125  * Open: open the audio device
126  *****************************************************************************/
127 static int Start ( audio_output_t *p_aout, audio_sample_format_t *fmt )
128 {
129     aout_sys_t *p_sys = p_aout->sys;
130     char *psz_mode;
131     ULONG i_kai_mode;
132     KAISPEC ks_wanted, ks_obtained;
133     int i_nb_channels;
134     int i_bytes_per_frame;
135     vlc_value_t val, text;
136     audio_sample_format_t format = *fmt;
137
138     psz_mode = var_InheritString( p_aout, "kai-audio-device" );
139     if( !psz_mode )
140         psz_mode = ( char * )ppsz_kai_audio_device[ 0 ];  // "auto"
141
142     i_kai_mode = KAIM_AUTO;
143     if( strcmp( psz_mode, "dart" ) == 0 )
144         i_kai_mode = KAIM_DART;
145     else if( strcmp( psz_mode, "uniaud" ) == 0 )
146         i_kai_mode = KAIM_UNIAUD;
147     msg_Dbg( p_aout, "selected mode = %s", psz_mode );
148
149     if( psz_mode != ppsz_kai_audio_device[ 0 ])
150         free( psz_mode );
151
152     i_nb_channels = aout_FormatNbChannels( &format );
153     if ( i_nb_channels >= 2 )
154     {
155         /* KAI doesn't support more than two channels. */
156         i_nb_channels = 2;
157         format.i_physical_channels = AOUT_CHANS_STEREO;
158     }
159     else
160         format.i_physical_channels = AOUT_CHAN_CENTER;
161
162     /* Support S16 only */
163     format.i_format = VLC_CODEC_S16N;
164
165     aout_FormatPrepare( &format );
166
167     i_bytes_per_frame = format.i_bytes_per_frame;
168
169     /* Initialize library */
170     if( kaiInit( i_kai_mode ))
171     {
172         msg_Err( p_aout, "cannot initialize KAI");
173
174         return VLC_EGENERIC;
175     }
176
177     ks_wanted.usDeviceIndex   = 0;
178     ks_wanted.ulType          = KAIT_PLAY;
179     ks_wanted.ulBitsPerSample = BPS_16;
180     ks_wanted.ulSamplingRate  = format.i_rate;
181     ks_wanted.ulDataFormat    = MCI_WAVE_FORMAT_PCM;
182     ks_wanted.ulChannels      = i_nb_channels;
183     ks_wanted.ulNumBuffers    = 2;
184     ks_wanted.ulBufferSize    = FRAME_SIZE * i_bytes_per_frame;
185     ks_wanted.fShareable      = !var_InheritBool( p_aout,
186                                                   "kai-audio-exclusive-mode");
187     ks_wanted.pfnCallBack     = KaiCallback;
188     ks_wanted.pCallBackData   = p_aout;
189     msg_Dbg( p_aout, "requested ulBufferSize = %ld", ks_wanted.ulBufferSize );
190
191     /* Open the sound device. */
192     if( kaiOpen( &ks_wanted, &ks_obtained, &p_sys->hkai ))
193     {
194         msg_Err( p_aout, "cannot open KAI device");
195
196         goto exit_kai_done;
197     }
198
199     msg_Dbg( p_aout, "open in %s mode",
200              ks_obtained.fShareable ? "shareable" : "exclusive" );
201     msg_Dbg( p_aout, "obtained i_nb_samples = %lu",
202              ks_obtained.ulBufferSize / i_bytes_per_frame );
203     msg_Dbg( p_aout, "obtained i_bytes_per_frame = %d",
204              format.i_bytes_per_frame );
205
206     p_sys->format = *fmt = format;
207
208     p_aout->time_get = TimeGet;
209     p_aout->play     = Play;
210     p_aout->pause    = Pause;
211     p_aout->flush    = Flush;
212
213     aout_SoftVolumeStart( p_aout );
214
215     CreateBuffer( p_aout, AUDIO_BUFFER_SIZE_IN_SECONDS *
216                           format.i_rate * format.i_bytes_per_frame );
217
218     /* Prevent SIG_FPE */
219     _control87(MCW_EM, MCW_EM);
220
221     return VLC_SUCCESS;
222
223 exit_kai_done :
224     kaiDone();
225
226     return VLC_EGENERIC;
227 }
228
229 /*****************************************************************************
230  * Play: play a sound samples buffer
231  *****************************************************************************/
232 static void Play (audio_output_t *p_aout, block_t *block)
233 {
234     aout_sys_t *p_sys = p_aout->sys;
235
236     kaiPlay( p_sys->hkai );
237
238     WriteBuffer( p_aout, block->p_buffer, block->i_buffer );
239
240     block_Release( block );
241 }
242
243 /*****************************************************************************
244  * Close: close the audio device
245  *****************************************************************************/
246 static void Stop ( audio_output_t *p_aout )
247 {
248     aout_sys_t *p_sys = p_aout->sys;
249
250     kaiClose( p_sys->hkai );
251     kaiDone();
252
253     DestroyBuffer( p_aout );
254 }
255
256 /*****************************************************************************
257  * KaiCallback: what to do once KAI has played sound samples
258  *****************************************************************************/
259 static ULONG APIENTRY KaiCallback( PVOID p_cb_data,
260                                    PVOID p_buffer,
261                                    ULONG i_buf_size )
262 {
263     audio_output_t *p_aout = (audio_output_t *)p_cb_data;
264     int i_len;
265
266     i_len = ReadBuffer( p_aout, p_buffer, i_buf_size );
267     if(( ULONG )i_len < i_buf_size )
268         memset(( uint8_t * )p_buffer + i_len, 0, i_buf_size - i_len );
269
270     return i_buf_size;
271 }
272
273 static int Open (vlc_object_t *obj)
274 {
275     audio_output_t *aout = (audio_output_t *)obj;
276     aout_sys_t *sys = calloc( 1, sizeof( aout_sys_t ) );
277
278     if( unlikely( sys == NULL ))
279         return VLC_ENOMEM;
280
281     aout->sys = sys;
282     aout->start = Start;
283     aout->stop = Stop;
284     aout_SoftVolumeInit( aout );
285     return VLC_SUCCESS;
286 }
287
288 static void Close( vlc_object_t *obj )
289 {
290     audio_output_t *aout = (audio_output_t *)obj;
291     aout_sys_t *sys = aout->sys;
292
293     free(sys);
294 }
295
296 static void Pause( audio_output_t *aout, bool pause, mtime_t date )
297 {
298     VLC_UNUSED( date );
299
300     aout_sys_t *sys = aout->sys;
301
302     if( pause )
303         kaiPause( sys->hkai );
304     else
305         kaiResume( sys->hkai );
306 }
307
308 static void Flush( audio_output_t *aout, bool drain )
309 {
310     audio_buffer_t *buffer = aout->sys->buffer;
311
312     vlc_mutex_lock( &buffer->mutex );
313
314     if( drain )
315     {
316         while( buffer->length > 0 )
317             vlc_cond_wait( &buffer->cond, &buffer->mutex );
318     }
319     else
320     {
321         buffer->read_pos = buffer->write_pos;
322         buffer->length   = 0;
323     }
324
325     vlc_mutex_unlock( &buffer->mutex );
326 }
327
328 static int TimeGet( audio_output_t *aout, mtime_t *restrict delay )
329 {
330     aout_sys_t            *sys = aout->sys;
331     audio_sample_format_t *format = &sys->format;
332     audio_buffer_t        *buffer = sys->buffer;
333
334     vlc_mutex_lock( &buffer->mutex );
335
336     *delay = ( buffer->length / format->i_bytes_per_frame ) * CLOCK_FREQ /
337              format->i_rate;
338
339     vlc_mutex_unlock( &buffer->mutex );
340
341     return 0;
342 }
343
344 static int CreateBuffer( audio_output_t *aout, int size )
345 {
346     audio_buffer_t *buffer;
347
348     buffer = calloc( 1, sizeof( *buffer ));
349     if( !buffer )
350         return -1;
351
352     buffer->data = malloc( size );
353     if( !buffer->data )
354     {
355         free( buffer );
356
357         return -1;
358     }
359
360     buffer->size = size;
361
362     vlc_mutex_init( &buffer->mutex );
363     vlc_cond_init( &buffer->cond );
364
365     aout->sys->buffer = buffer;
366
367     return 0;
368 }
369
370 static void DestroyBuffer( audio_output_t *aout )
371 {
372     audio_buffer_t *buffer = aout->sys->buffer;
373
374     vlc_mutex_destroy( &buffer->mutex );
375     vlc_cond_destroy( &buffer->cond );
376
377     free( buffer->data );
378     free( buffer );
379 }
380
381 static int ReadBuffer( audio_output_t *aout, uint8_t *data, int size )
382 {
383     audio_buffer_t *buffer = aout->sys->buffer;
384     int             len;
385     int             remain_len = 0;
386
387     vlc_mutex_lock( &buffer->mutex );
388
389     len = MIN( buffer->length, size );
390     if( buffer->read_pos + len > buffer->size )
391     {
392         remain_len  = len;
393         len         = buffer->size - buffer->read_pos;
394         remain_len -= len;
395     }
396
397     memcpy( data, buffer->data + buffer->read_pos, len );
398     if( remain_len )
399         memcpy( data + len, buffer->data, remain_len );
400
401     len += remain_len;
402
403     buffer->read_pos += len;
404     buffer->read_pos %= buffer->size;
405
406     buffer->length -= len;
407
408     vlc_cond_signal( &buffer->cond );
409
410     vlc_mutex_unlock( &buffer->mutex );
411
412     return len;
413 }
414
415 static int WriteBuffer( audio_output_t *aout, uint8_t *data, int size )
416 {
417     audio_buffer_t *buffer = aout->sys->buffer;
418     int             len;
419     int             remain_len = 0;
420
421     vlc_mutex_lock( &buffer->mutex );
422
423     /* FIXME :
424      * If size is larger than buffer->size, this is locked indefinitely.
425      */
426     while( buffer->length + size > buffer->size )
427         vlc_cond_wait( &buffer->cond, &buffer->mutex );
428
429     len = size;
430     if( buffer->write_pos + len > buffer->size )
431     {
432         remain_len  = len;
433         len         = buffer->size - buffer->write_pos;
434         remain_len -= len;
435     }
436
437     memcpy( buffer->data + buffer->write_pos, data, len );
438     if( remain_len )
439         memcpy( buffer->data, data + len, remain_len );
440
441     buffer->write_pos += size;
442     buffer->write_pos %= buffer->size;
443
444     buffer->length += size;
445
446     vlc_mutex_unlock( &buffer->mutex );
447
448     return size;
449 }