1 /*****************************************************************************
2 * kai.c : KAI audio output plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2010-2013 VLC authors and VideoLAN
6 * Authors: KO Myung-Hun <komh@chollian.net>
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
38 #define FRAME_SIZE 2048
40 #define AUDIO_BUFFER_SIZE_IN_SECONDS ( AOUT_MAX_ADVANCE_TIME / CLOCK_FREQ )
53 typedef struct audio_buffer_t audio_buffer_t;
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 *****************************************************************************/
63 audio_buffer_t *buffer;
67 audio_sample_format_t format;
70 /*****************************************************************************
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 );
80 static ULONG APIENTRY KaiCallback ( PVOID, PVOID, ULONG );
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 );
87 #include "audio_output/volume.h"
89 /*****************************************************************************
91 *****************************************************************************/
92 #define KAI_AUDIO_DEVICE_TEXT N_( \
94 #define KAI_AUDIO_DEVICE_LONGTEXT N_( \
95 "Select a proper audio device to be used by KAI." )
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 " \
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" };
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 )
118 add_bool( "kai-audio-exclusive-mode", false,
119 KAI_AUDIO_EXCLUSIVE_MODE_TEXT, KAI_AUDIO_EXCLUSIVE_MODE_LONGTEXT,
121 set_callbacks( Open, Close )
124 /*****************************************************************************
125 * Open: open the audio device
126 *****************************************************************************/
127 static int Start ( audio_output_t *p_aout, audio_sample_format_t *fmt )
129 aout_sys_t *p_sys = p_aout->sys;
132 KAISPEC ks_wanted, ks_obtained;
134 int i_bytes_per_frame;
135 vlc_value_t val, text;
136 audio_sample_format_t format = *fmt;
138 psz_mode = var_InheritString( p_aout, "kai-audio-device" );
140 psz_mode = ( char * )ppsz_kai_audio_device[ 0 ]; // "auto"
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 );
149 if( psz_mode != ppsz_kai_audio_device[ 0 ])
152 i_nb_channels = aout_FormatNbChannels( &format );
153 if ( i_nb_channels >= 2 )
155 /* KAI doesn't support more than two channels. */
157 format.i_physical_channels = AOUT_CHANS_STEREO;
160 format.i_physical_channels = AOUT_CHAN_CENTER;
162 /* Support S16 only */
163 format.i_format = VLC_CODEC_S16N;
165 aout_FormatPrepare( &format );
167 i_bytes_per_frame = format.i_bytes_per_frame;
169 /* Initialize library */
170 if( kaiInit( i_kai_mode ))
172 msg_Err( p_aout, "cannot initialize KAI");
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 );
191 /* Open the sound device. */
192 if( kaiOpen( &ks_wanted, &ks_obtained, &p_sys->hkai ))
194 msg_Err( p_aout, "cannot open KAI device");
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 );
206 p_sys->format = *fmt = format;
208 p_aout->time_get = TimeGet;
210 p_aout->pause = Pause;
211 p_aout->flush = Flush;
213 aout_SoftVolumeStart( p_aout );
215 CreateBuffer( p_aout, AUDIO_BUFFER_SIZE_IN_SECONDS *
216 format.i_rate * format.i_bytes_per_frame );
218 /* Prevent SIG_FPE */
219 _control87(MCW_EM, MCW_EM);
229 /*****************************************************************************
230 * Play: play a sound samples buffer
231 *****************************************************************************/
232 static void Play (audio_output_t *p_aout, block_t *block)
234 aout_sys_t *p_sys = p_aout->sys;
236 kaiPlay( p_sys->hkai );
238 WriteBuffer( p_aout, block->p_buffer, block->i_buffer );
240 block_Release( block );
243 /*****************************************************************************
244 * Close: close the audio device
245 *****************************************************************************/
246 static void Stop ( audio_output_t *p_aout )
248 aout_sys_t *p_sys = p_aout->sys;
250 kaiClose( p_sys->hkai );
253 DestroyBuffer( p_aout );
256 /*****************************************************************************
257 * KaiCallback: what to do once KAI has played sound samples
258 *****************************************************************************/
259 static ULONG APIENTRY KaiCallback( PVOID p_cb_data,
263 audio_output_t *p_aout = (audio_output_t *)p_cb_data;
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 );
273 static int Open (vlc_object_t *obj)
275 audio_output_t *aout = (audio_output_t *)obj;
276 aout_sys_t *sys = calloc( 1, sizeof( aout_sys_t ) );
278 if( unlikely( sys == NULL ))
284 aout_SoftVolumeInit( aout );
288 static void Close( vlc_object_t *obj )
290 audio_output_t *aout = (audio_output_t *)obj;
291 aout_sys_t *sys = aout->sys;
296 static void Pause( audio_output_t *aout, bool pause, mtime_t date )
300 aout_sys_t *sys = aout->sys;
303 kaiPause( sys->hkai );
305 kaiResume( sys->hkai );
308 static void Flush( audio_output_t *aout, bool drain )
310 audio_buffer_t *buffer = aout->sys->buffer;
312 vlc_mutex_lock( &buffer->mutex );
316 while( buffer->length > 0 )
317 vlc_cond_wait( &buffer->cond, &buffer->mutex );
321 buffer->read_pos = buffer->write_pos;
325 vlc_mutex_unlock( &buffer->mutex );
328 static int TimeGet( audio_output_t *aout, mtime_t *restrict delay )
330 aout_sys_t *sys = aout->sys;
331 audio_sample_format_t *format = &sys->format;
332 audio_buffer_t *buffer = sys->buffer;
334 vlc_mutex_lock( &buffer->mutex );
336 *delay = ( buffer->length / format->i_bytes_per_frame ) * CLOCK_FREQ /
339 vlc_mutex_unlock( &buffer->mutex );
344 static int CreateBuffer( audio_output_t *aout, int size )
346 audio_buffer_t *buffer;
348 buffer = calloc( 1, sizeof( *buffer ));
352 buffer->data = malloc( size );
362 vlc_mutex_init( &buffer->mutex );
363 vlc_cond_init( &buffer->cond );
365 aout->sys->buffer = buffer;
370 static void DestroyBuffer( audio_output_t *aout )
372 audio_buffer_t *buffer = aout->sys->buffer;
374 vlc_mutex_destroy( &buffer->mutex );
375 vlc_cond_destroy( &buffer->cond );
377 free( buffer->data );
381 static int ReadBuffer( audio_output_t *aout, uint8_t *data, int size )
383 audio_buffer_t *buffer = aout->sys->buffer;
387 vlc_mutex_lock( &buffer->mutex );
389 len = MIN( buffer->length, size );
390 if( buffer->read_pos + len > buffer->size )
393 len = buffer->size - buffer->read_pos;
397 memcpy( data, buffer->data + buffer->read_pos, len );
399 memcpy( data + len, buffer->data, remain_len );
403 buffer->read_pos += len;
404 buffer->read_pos %= buffer->size;
406 buffer->length -= len;
408 vlc_cond_signal( &buffer->cond );
410 vlc_mutex_unlock( &buffer->mutex );
415 static int WriteBuffer( audio_output_t *aout, uint8_t *data, int size )
417 audio_buffer_t *buffer = aout->sys->buffer;
421 vlc_mutex_lock( &buffer->mutex );
424 * If size is larger than buffer->size, this is locked indefinitely.
426 while( buffer->length + size > buffer->size )
427 vlc_cond_wait( &buffer->cond, &buffer->mutex );
430 if( buffer->write_pos + len > buffer->size )
433 len = buffer->size - buffer->write_pos;
437 memcpy( buffer->data + buffer->write_pos, data, len );
439 memcpy( buffer->data, data + len, remain_len );
441 buffer->write_pos += size;
442 buffer->write_pos %= buffer->size;
444 buffer->length += size;
446 vlc_mutex_unlock( &buffer->mutex );