]> git.sesse.net Git - vlc/blob - modules/audio_output/kai.c
kai: remove aout_Packet*()
[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 ( 64 * 1024 )
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 "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     if( var_Get( p_aout, "audio-device", &val ) != VLC_ENOVAR )
139     {
140         /* The user has selected an audio device. */
141         if ( val.i_int == AOUT_VAR_STEREO )
142         {
143             format.i_physical_channels
144                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
145         }
146         else if ( val.i_int == AOUT_VAR_MONO )
147         {
148             format.i_physical_channels = AOUT_CHAN_CENTER;
149         }
150     }
151
152     psz_mode = var_InheritString( p_aout, "kai-audio-device" );
153     if( !psz_mode )
154         psz_mode = ( char * )ppsz_kai_audio_device[ 0 ];  // "auto"
155
156     i_kai_mode = KAIM_AUTO;
157     if( strcmp( psz_mode, "dart" ) == 0 )
158         i_kai_mode = KAIM_DART;
159     else if( strcmp( psz_mode, "uniaud" ) == 0 )
160         i_kai_mode = KAIM_UNIAUD;
161     msg_Dbg( p_aout, "selected mode = %s", psz_mode );
162
163     if( psz_mode != ppsz_kai_audio_device[ 0 ])
164         free( psz_mode );
165
166     i_nb_channels = aout_FormatNbChannels( &format );
167     if ( i_nb_channels > 2 )
168     {
169         /* KAI doesn't support more than two channels. */
170         i_nb_channels = 2;
171         format.i_physical_channels
172             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
173     }
174
175     /* Support S16 only */
176     format.i_format = VLC_CODEC_S16N;
177
178     aout_FormatPrepare( &format );
179
180     i_bytes_per_frame = format.i_bytes_per_frame;
181
182     /* Initialize library */
183     if( kaiInit( i_kai_mode ))
184     {
185         msg_Err( p_aout, "cannot initialize KAI");
186
187         return VLC_EGENERIC;
188     }
189
190     ks_wanted.usDeviceIndex   = 0;
191     ks_wanted.ulType          = KAIT_PLAY;
192     ks_wanted.ulBitsPerSample = BPS_16;
193     ks_wanted.ulSamplingRate  = format.i_rate;
194     ks_wanted.ulDataFormat    = MCI_WAVE_FORMAT_PCM;
195     ks_wanted.ulChannels      = i_nb_channels;
196     ks_wanted.ulNumBuffers    = 2;
197     ks_wanted.ulBufferSize    = FRAME_SIZE * i_bytes_per_frame;
198     ks_wanted.fShareable      = !var_InheritBool( p_aout,
199                                                   "kai-audio-exclusive-mode");
200     ks_wanted.pfnCallBack     = KaiCallback;
201     ks_wanted.pCallBackData   = p_aout;
202     msg_Dbg( p_aout, "requested ulBufferSize = %ld", ks_wanted.ulBufferSize );
203
204     /* Open the sound device. */
205     if( kaiOpen( &ks_wanted, &ks_obtained, &p_sys->hkai ))
206     {
207         msg_Err( p_aout, "cannot open KAI device");
208
209         goto exit_kai_done;
210     }
211
212     msg_Dbg( p_aout, "open in %s mode",
213              ks_obtained.fShareable ? "shareable" : "exclusive" );
214     msg_Dbg( p_aout, "obtained i_nb_samples = %lu",
215              ks_obtained.ulBufferSize / i_bytes_per_frame );
216     msg_Dbg( p_aout, "obtained i_bytes_per_frame = %d",
217              format.i_bytes_per_frame );
218
219     p_sys->format = *fmt = format;
220
221     p_aout->time_get = TimeGet;
222     p_aout->play     = Play;
223     p_aout->pause    = Pause;
224     p_aout->flush    = Flush;
225
226     aout_SoftVolumeStart( p_aout );
227
228     CreateBuffer( p_aout, AUDIO_BUFFER_SIZE );
229
230     if ( var_Type( p_aout, "audio-device" ) == 0 )
231     {
232         /* First launch. */
233         var_Create( p_aout, "audio-device",
234                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
235         text.psz_string = _("Audio Device");
236         var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
237
238         val.i_int = AOUT_VAR_STEREO;
239         text.psz_string = _("Stereo");
240         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
241         val.i_int = AOUT_VAR_MONO;
242         text.psz_string = _("Mono");
243         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
244         if ( i_nb_channels == 2 )
245         {
246             val.i_int = AOUT_VAR_STEREO;
247         }
248         else
249         {
250             val.i_int = AOUT_VAR_MONO;
251         }
252         var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
253         var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
254     }
255
256     /* Prevent SIG_FPE */
257     _control87(MCW_EM, MCW_EM);
258
259     return VLC_SUCCESS;
260
261 exit_kai_done :
262     kaiDone();
263
264     return VLC_EGENERIC;
265 }
266
267 /*****************************************************************************
268  * Play: play a sound samples buffer
269  *****************************************************************************/
270 static void Play (audio_output_t *p_aout, block_t *block)
271 {
272     aout_sys_t *p_sys = p_aout->sys;
273
274     kaiPlay( p_sys->hkai );
275
276     WriteBuffer( p_aout, block->p_buffer, block->i_buffer );
277
278     block_Release( block );
279 }
280
281 /*****************************************************************************
282  * Close: close the audio device
283  *****************************************************************************/
284 static void Stop ( audio_output_t *p_aout )
285 {
286     aout_sys_t *p_sys = p_aout->sys;
287
288     kaiClose( p_sys->hkai );
289     kaiDone();
290
291     DestroyBuffer( p_aout );
292 }
293
294 /*****************************************************************************
295  * KaiCallback: what to do once KAI has played sound samples
296  *****************************************************************************/
297 static ULONG APIENTRY KaiCallback( PVOID p_cb_data,
298                                    PVOID p_buffer,
299                                    ULONG i_buf_size )
300 {
301     audio_output_t *p_aout = (audio_output_t *)p_cb_data;
302     int i_len;
303
304     i_len = ReadBuffer( p_aout, p_buffer, i_buf_size );
305     if(( ULONG )i_len < i_buf_size )
306         memset(( uint8_t * )p_buffer + i_len, 0, i_buf_size - i_len );
307
308     return i_buf_size;
309 }
310
311 static int Open (vlc_object_t *obj)
312 {
313     audio_output_t *aout = (audio_output_t *)obj;
314     aout_sys_t *sys = calloc( 1, sizeof( aout_sys_t ) );
315
316     if( unlikely( sys == NULL ))
317         return VLC_ENOMEM;
318
319     aout->sys = sys;
320     aout->start = Start;
321     aout->stop = Stop;
322     aout_SoftVolumeInit( aout );
323     return VLC_SUCCESS;
324 }
325
326 static void Close( vlc_object_t *obj )
327 {
328     audio_output_t *aout = (audio_output_t *)obj;
329     aout_sys_t *sys = aout->sys;
330
331     free(sys);
332 }
333
334 static void Pause( audio_output_t *aout, bool pause, mtime_t date )
335 {
336     VLC_UNUSED( date );
337
338     aout_sys_t *sys = aout->sys;
339
340     if( pause )
341         kaiPause( sys->hkai );
342     else
343         kaiResume( sys->hkai );
344 }
345
346 static void Flush( audio_output_t *aout, bool drain )
347 {
348     audio_buffer_t *buffer = aout->sys->buffer;
349
350     vlc_mutex_lock( &buffer->mutex );
351
352     if( drain )
353     {
354         while( buffer->length > 0 )
355             vlc_cond_wait( &buffer->cond, &buffer->mutex );
356     }
357     else
358     {
359         buffer->read_pos = buffer->write_pos;
360         buffer->length   = 0;
361     }
362
363     vlc_mutex_unlock( &buffer->mutex );
364 }
365
366 static int TimeGet( audio_output_t *aout, mtime_t *restrict delay )
367 {
368     aout_sys_t            *sys = aout->sys;
369     audio_sample_format_t *format = &sys->format;
370     audio_buffer_t        *buffer = sys->buffer;
371
372     vlc_mutex_lock( &buffer->mutex );
373
374     *delay = ( buffer->length / format->i_bytes_per_frame ) * CLOCK_FREQ /
375              format->i_rate;
376
377     vlc_mutex_unlock( &buffer->mutex );
378
379     return 0;
380 }
381
382 static int CreateBuffer( audio_output_t *aout, int size )
383 {
384     audio_buffer_t *buffer;
385
386     buffer = calloc( 1, sizeof( *buffer ));
387     if( !buffer )
388         return -1;
389
390     buffer->data = malloc( size );
391     if( !buffer->data )
392     {
393         free( buffer );
394
395         return -1;
396     }
397
398     buffer->size = size;
399
400     vlc_mutex_init( &buffer->mutex );
401     vlc_cond_init( &buffer->cond );
402
403     aout->sys->buffer = buffer;
404
405     return 0;
406 }
407
408 static void DestroyBuffer( audio_output_t *aout )
409 {
410     audio_buffer_t *buffer = aout->sys->buffer;
411
412     vlc_mutex_destroy( &buffer->mutex );
413     vlc_cond_destroy( &buffer->cond );
414
415     free( buffer->data );
416     free( buffer );
417 }
418
419 static int ReadBuffer( audio_output_t *aout, uint8_t *data, int size )
420 {
421     audio_buffer_t *buffer = aout->sys->buffer;
422     int             len;
423     int             remain_len = 0;
424
425     vlc_mutex_lock( &buffer->mutex );
426
427     len = MIN( buffer->length, size );
428     if( buffer->read_pos + len > buffer->size )
429     {
430         remain_len  = len;
431         len         = buffer->size - buffer->read_pos;
432         remain_len -= len;
433     }
434
435     memcpy( data, buffer->data + buffer->read_pos, len );
436     if( remain_len )
437         memcpy( data + len, buffer->data, remain_len );
438
439     len += remain_len;
440
441     buffer->read_pos += len;
442     buffer->read_pos %= buffer->size;
443
444     buffer->length -= len;
445
446     vlc_cond_signal( &buffer->cond );
447
448     vlc_mutex_unlock( &buffer->mutex );
449
450     return len;
451 }
452
453 static int WriteBuffer( audio_output_t *aout, uint8_t *data, int size )
454 {
455     audio_buffer_t *buffer = aout->sys->buffer;
456     int             len;
457     int             remain_len = 0;
458
459     vlc_mutex_lock( &buffer->mutex );
460
461     while( buffer->length + size > buffer->size )
462         vlc_cond_wait( &buffer->cond, &buffer->mutex );
463
464     len = size;
465     if( buffer->write_pos + len > buffer->size )
466     {
467         remain_len  = len;
468         len         = buffer->size - buffer->write_pos;
469         remain_len -= len;
470     }
471
472     memcpy( buffer->data + buffer->write_pos, data, len );
473     if( remain_len )
474         memcpy( buffer->data, data + len, remain_len );
475
476     buffer->write_pos += size;
477     buffer->write_pos %= buffer->size;
478
479     buffer->length += size;
480
481     vlc_mutex_unlock( &buffer->mutex );
482
483     return size;
484 }