1 /*****************************************************************************
2 * kai.c : KAI audio output plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2010 the VideoLAN team
6 * Authors: KO Myung-Hun <komh@chollian.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, 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 /*****************************************************************************
41 * aout_sys_t: KAI audio output method descriptor
42 *****************************************************************************
43 * This structure is part of the audio output thread descriptor.
44 * It describes the specific properties of an audio device.
45 *****************************************************************************/
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close ( vlc_object_t * );
59 static void Play ( audio_output_t *_p_aout, block_t *block, mtime_t * );
61 static ULONG APIENTRY KaiCallback ( PVOID, PVOID, ULONG );
65 /*****************************************************************************
67 *****************************************************************************/
68 #define KAI_AUDIO_DEVICE_TEXT N_( \
70 #define KAI_AUDIO_DEVICE_LONGTEXT N_( \
71 "Select a proper audio device to be used by KAI." )
73 #define KAI_AUDIO_EXCLUSIVE_MODE_TEXT N_( \
74 "Open audio in exclusive mode." )
75 #define KAI_AUDIO_EXCLUSIVE_MODE_LONGTEXT N_( \
76 "Enable this option if you want your audio not to be interrupted by the " \
79 static const char *const ppsz_kai_audio_device[] = {
80 "auto", "dart", "uniaud" };
81 static const char *const ppsz_kai_audio_device_text[] = {
82 N_("Auto"), "DART", "UNIAUD" };
85 set_shortname( "KAI" )
86 set_description( N_("K Audio Interface audio output") )
87 set_capability( "audio output", 100 )
88 set_category( CAT_AUDIO )
89 set_subcategory( SUBCAT_AUDIO_AOUT )
90 add_string( "kai-audio-device", ppsz_kai_audio_device[0],
91 KAI_AUDIO_DEVICE_TEXT, KAI_AUDIO_DEVICE_LONGTEXT, false )
92 change_string_list( ppsz_kai_audio_device, ppsz_kai_audio_device_text,
95 add_bool( "kai-audio-exclusive-mode", false,
96 KAI_AUDIO_EXCLUSIVE_MODE_TEXT, KAI_AUDIO_EXCLUSIVE_MODE_LONGTEXT,
98 set_callbacks( Open, Close )
101 /*****************************************************************************
102 * Open: open the audio device
103 *****************************************************************************/
104 static int Open ( vlc_object_t *p_this )
106 audio_output_t *p_aout = (audio_output_t *)p_this;
110 KAISPEC ks_wanted, ks_obtained;
112 int i_bytes_per_frame;
113 vlc_value_t val, text;
114 audio_format_t format = p_aout->format;
116 /* Allocate structure */
117 p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
119 if( p_aout->sys == NULL )
124 if( var_Get( p_aout, "audio-device", &val ) != VLC_ENOVAR )
126 /* The user has selected an audio device. */
127 if ( val.i_int == AOUT_VAR_STEREO )
129 format.i_physical_channels
130 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
132 else if ( val.i_int == AOUT_VAR_MONO )
134 format.i_physical_channels = AOUT_CHAN_CENTER;
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
158 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
161 /* Support s16l only */
162 format.i_format = VLC_CODEC_S16L;
164 aout_FormatPrepare( &format );
166 i_bytes_per_frame = format.i_bytes_per_frame;
168 /* Initialize library */
169 if( kaiInit( i_kai_mode ))
171 msg_Err( p_aout, "cannot initialize KAI");
176 ks_wanted.usDeviceIndex = 0;
177 ks_wanted.ulType = KAIT_PLAY;
178 ks_wanted.ulBitsPerSample = BPS_16;
179 ks_wanted.ulSamplingRate = format.i_rate;
180 ks_wanted.ulDataFormat = MCI_WAVE_FORMAT_PCM;
181 ks_wanted.ulChannels = i_nb_channels;
182 ks_wanted.ulNumBuffers = 2;
183 ks_wanted.ulBufferSize = FRAME_SIZE * i_bytes_per_frame;
184 ks_wanted.fShareable = !var_InheritBool( p_aout,
185 "kai-audio-exclusive-mode");
186 ks_wanted.pfnCallBack = KaiCallback;
187 ks_wanted.pCallBackData = p_aout;
188 msg_Dbg( p_aout, "requested ulBufferSize = %ld", ks_wanted.ulBufferSize );
190 /* Open the sound device. */
191 if( kaiOpen( &ks_wanted, &ks_obtained, &p_sys->hkai ))
193 msg_Err( p_aout, "cannot open KAI device");
198 msg_Dbg( p_aout, "open in %s mode",
199 ks_obtained.fShareable ? "shareable" : "exclusive" );
200 msg_Dbg( p_aout, "obtained i_nb_samples = %lu",
201 ks_obtained.ulBufferSize / i_bytes_per_frame );
202 msg_Dbg( p_aout, "obtained i_bytes_per_frame = %d",
203 format.i_bytes_per_frame );
205 p_aout->format = format;
207 p_aout->pf_play = Play;
208 p_aout->pf_pause = aout_PacketPause;
209 p_aout->pf_flush = aout_PacketFlush;
211 aout_PacketInit( p_aout, &p_sys->packet,
212 ks_obtained.ulBufferSize / i_bytes_per_frame );
213 aout_SoftVolumeInit( p_aout );
215 if ( var_Type( p_aout, "audio-device" ) == 0 )
218 var_Create( p_aout, "audio-device",
219 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
220 text.psz_string = _("Audio Device");
221 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
223 val.i_int = AOUT_VAR_STEREO;
224 text.psz_string = _("Stereo");
225 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
226 val.i_int = AOUT_VAR_MONO;
227 text.psz_string = _("Mono");
228 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
229 if ( i_nb_channels == 2 )
231 val.i_int = AOUT_VAR_STEREO;
235 val.i_int = AOUT_VAR_MONO;
237 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
238 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
241 /* Prevent SIG_FPE */
242 _control87(MCW_EM, MCW_EM);
255 /*****************************************************************************
256 * Play: play a sound samples buffer
257 *****************************************************************************/
258 static void Play (audio_output_t *p_aout, block_t *block,
259 mtime_t *restrict drift)
261 aout_sys_t *p_sys = p_aout->sys;
263 kaiPlay( p_sys->hkai );
265 aout_PacketPlay( p_aout, block, drift );
268 /*****************************************************************************
269 * Close: close the audio device
270 *****************************************************************************/
271 static void Close ( vlc_object_t *p_this )
273 audio_output_t *p_aout = (audio_output_t *)p_this;
274 aout_sys_t *p_sys = p_aout->sys;
276 kaiClose( p_sys->hkai );
279 aout_PacketDestroy( p_aout );
283 /*****************************************************************************
284 * KaiCallback: what to do once KAI has played sound samples
285 *****************************************************************************/
286 static ULONG APIENTRY KaiCallback( PVOID p_cb_data,
290 audio_output_t *p_aout = (audio_output_t *)p_cb_data;
291 block_t *p_aout_buffer;
292 mtime_t current_date, next_date;
295 /* We have 2 buffers, and a callback function is called right after KAI
296 * runs out of a buffer. So we should get a packet to be played after the
299 next_date = mdate() + ( i_buf_size * 1000000LL
300 / p_aout->format.i_bytes_per_frame
301 / p_aout->format.i_rate
302 * p_aout->format.i_frame_length );
304 for (i_len = 0; i_len < i_buf_size;)
306 current_date = mdate();
307 if( next_date < current_date )
308 next_date = current_date;
310 /* Get the next audio data buffer */
311 p_aout_buffer = aout_PacketNext( p_aout, next_date );
313 if( p_aout_buffer == NULL )
315 /* Means we are too early to request a new buffer ?
318 msleep( AOUT_MIN_PREPARE_TIME );
320 p_aout_buffer = aout_PacketNext( p_aout, next_date );
323 if ( p_aout_buffer != NULL )
325 memcpy( ( uint8_t * ) p_buffer + i_len,
326 p_aout_buffer->p_buffer,
327 p_aout_buffer->i_buffer );
329 i_len += p_aout_buffer->i_buffer;
331 next_date += p_aout_buffer->i_length;
333 block_Release( p_aout_buffer );
337 memset( ( uint8_t * ) p_buffer + i_len, 0, i_buf_size - i_len );