]> git.sesse.net Git - vlc/blob - modules/audio_output/kai.c
a31355c7b2f00b532aba7f37b42cce8c7be1522a
[vlc] / modules / audio_output / kai.c
1 /*****************************************************************************
2  * kai.c : KAI audio output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2010 the VideoLAN team
5  *
6  * Authors: KO Myung-Hun <komh@chollian.net>
7  *
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.
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 General Public License for more details.
17  *
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  *****************************************************************************/
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 /*****************************************************************************
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  *****************************************************************************/
46 struct aout_sys_t
47 {
48     aout_packet_t   packet;
49     HKAI            hkai;
50     float           soft_gain;
51     bool            soft_mute;
52 };
53
54 /*****************************************************************************
55  * Local prototypes
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 * );
60
61 static ULONG APIENTRY KaiCallback ( PVOID, PVOID, ULONG );
62
63 #include "volume.h"
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 #define KAI_AUDIO_DEVICE_TEXT N_( \
69     "Device" )
70 #define KAI_AUDIO_DEVICE_LONGTEXT N_( \
71     "Select a proper audio device to be used by KAI." )
72
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 " \
77     "other audio." )
78
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" };
83
84 vlc_module_begin ()
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 )
93     add_sw_gain( )
94     add_bool( "kai-audio-exclusive-mode", false,
95               KAI_AUDIO_EXCLUSIVE_MODE_TEXT, KAI_AUDIO_EXCLUSIVE_MODE_LONGTEXT,
96               true )
97     set_callbacks( Open, Close )
98 vlc_module_end ()
99
100 /*****************************************************************************
101  * Open: open the audio device
102  *****************************************************************************/
103 static int Open ( vlc_object_t *p_this )
104 {
105     audio_output_t *p_aout = (audio_output_t *)p_this;
106     aout_sys_t *p_sys;
107     char *psz_mode;
108     ULONG i_kai_mode;
109     KAISPEC ks_wanted, ks_obtained;
110     int i_nb_channels;
111     int i_bytes_per_frame;
112     vlc_value_t val, text;
113     audio_format_t format =  p_aout->format;
114
115     /* Allocate structure */
116     p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
117
118     if( p_aout->sys == NULL )
119         return VLC_ENOMEM;
120
121     p_sys = p_aout->sys;
122
123     if( var_Get( p_aout, "audio-device", &val ) != VLC_ENOVAR )
124     {
125         /* The user has selected an audio device. */
126         if ( val.i_int == AOUT_VAR_STEREO )
127         {
128             format.i_physical_channels
129                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
130         }
131         else if ( val.i_int == AOUT_VAR_MONO )
132         {
133             format.i_physical_channels = AOUT_CHAN_CENTER;
134         }
135     }
136
137     psz_mode = var_InheritString( p_aout, "kai-audio-device" );
138     if( !psz_mode )
139         psz_mode = ( char * )ppsz_kai_audio_device[ 0 ];  // "auto"
140
141     i_kai_mode = KAIM_AUTO;
142     if( strcmp( psz_mode, "dart" ) == 0 )
143         i_kai_mode = KAIM_DART;
144     else if( strcmp( psz_mode, "uniaud" ) == 0 )
145         i_kai_mode = KAIM_UNIAUD;
146     msg_Dbg( p_aout, "selected mode = %s", psz_mode );
147
148     if( psz_mode != ppsz_kai_audio_device[ 0 ])
149         free( psz_mode );
150
151     i_nb_channels = aout_FormatNbChannels( &format );
152     if ( i_nb_channels > 2 )
153     {
154         /* KAI doesn't support more than two channels. */
155         i_nb_channels = 2;
156         format.i_physical_channels
157             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
158     }
159
160     /* Support s16l only */
161     format.i_format = VLC_CODEC_S16L;
162
163     aout_FormatPrepare( &format );
164
165     i_bytes_per_frame = format.i_bytes_per_frame;
166
167     /* Initialize library */
168     if( kaiInit( i_kai_mode ))
169     {
170         msg_Err( p_aout, "cannot initialize KAI");
171
172         goto exit_free_sys;
173     }
174
175     ks_wanted.usDeviceIndex   = 0;
176     ks_wanted.ulType          = KAIT_PLAY;
177     ks_wanted.ulBitsPerSample = BPS_16;
178     ks_wanted.ulSamplingRate  = format.i_rate;
179     ks_wanted.ulDataFormat    = MCI_WAVE_FORMAT_PCM;
180     ks_wanted.ulChannels      = i_nb_channels;
181     ks_wanted.ulNumBuffers    = 2;
182     ks_wanted.ulBufferSize    = FRAME_SIZE * i_bytes_per_frame;
183     ks_wanted.fShareable      = !var_InheritBool( p_aout,
184                                                   "kai-audio-exclusive-mode");
185     ks_wanted.pfnCallBack     = KaiCallback;
186     ks_wanted.pCallBackData   = p_aout;
187     msg_Dbg( p_aout, "requested ulBufferSize = %ld", ks_wanted.ulBufferSize );
188
189     /* Open the sound device. */
190     if( kaiOpen( &ks_wanted, &ks_obtained, &p_sys->hkai ))
191     {
192         msg_Err( p_aout, "cannot open KAI device");
193
194         goto exit_kai_done;
195     }
196
197     msg_Dbg( p_aout, "open in %s mode",
198              ks_obtained.fShareable ? "shareable" : "exclusive" );
199     msg_Dbg( p_aout, "obtained i_nb_samples = %lu",
200              ks_obtained.ulBufferSize / i_bytes_per_frame );
201     msg_Dbg( p_aout, "obtained i_bytes_per_frame = %d",
202              format.i_bytes_per_frame );
203
204     p_aout->format   = format;
205
206     p_aout->pf_play  = Play;
207     p_aout->pf_pause = aout_PacketPause;
208     p_aout->pf_flush = aout_PacketFlush;
209
210     aout_PacketInit( p_aout, &p_sys->packet,
211                      ks_obtained.ulBufferSize / i_bytes_per_frame );
212     aout_SoftVolumeInit( p_aout );
213
214     if ( var_Type( p_aout, "audio-device" ) == 0 )
215     {
216         /* First launch. */
217         var_Create( p_aout, "audio-device",
218                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
219         text.psz_string = _("Audio Device");
220         var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
221
222         val.i_int = AOUT_VAR_STEREO;
223         text.psz_string = _("Stereo");
224         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
225         val.i_int = AOUT_VAR_MONO;
226         text.psz_string = _("Mono");
227         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
228         if ( i_nb_channels == 2 )
229         {
230             val.i_int = AOUT_VAR_STEREO;
231         }
232         else
233         {
234             val.i_int = AOUT_VAR_MONO;
235         }
236         var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
237         var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
238     }
239
240     /* Prevent SIG_FPE */
241     _control87(MCW_EM, MCW_EM);
242
243     return VLC_SUCCESS;
244
245 exit_kai_done :
246     kaiDone();
247
248 exit_free_sys :
249     free( p_sys );
250
251     return VLC_EGENERIC;
252 }
253
254 /*****************************************************************************
255  * Play: play a sound samples buffer
256  *****************************************************************************/
257 static void Play (audio_output_t *p_aout, block_t *block,
258                   mtime_t *restrict drift)
259 {
260     aout_sys_t *p_sys = p_aout->sys;
261
262     kaiPlay( p_sys->hkai );
263
264     aout_PacketPlay( p_aout, block, drift );
265 }
266
267 /*****************************************************************************
268  * Close: close the audio device
269  *****************************************************************************/
270 static void Close ( vlc_object_t *p_this )
271 {
272     audio_output_t *p_aout = (audio_output_t *)p_this;
273     aout_sys_t *p_sys = p_aout->sys;
274
275     kaiClose( p_sys->hkai );
276     kaiDone();
277
278     aout_PacketDestroy( p_aout );
279     free( p_sys );
280 }
281
282 /*****************************************************************************
283  * KaiCallback: what to do once KAI has played sound samples
284  *****************************************************************************/
285 static ULONG APIENTRY KaiCallback( PVOID p_cb_data,
286                                    PVOID p_buffer,
287                                    ULONG i_buf_size )
288 {
289     audio_output_t *p_aout = (audio_output_t *)p_cb_data;
290     block_t  *p_aout_buffer;
291     mtime_t current_date, next_date;
292     ULONG i_len;
293
294     /* We have 2 buffers, and a callback function is called right after KAI
295      * runs out of a buffer. So we should get a packet to be played after the
296      * remaining buffer.
297      */
298     next_date = mdate() + ( i_buf_size * 1000000LL
299                                        / p_aout->format.i_bytes_per_frame
300                                        / p_aout->format.i_rate
301                                        * p_aout->format.i_frame_length );
302
303     for (i_len = 0; i_len < i_buf_size;)
304     {
305         current_date = mdate();
306         if( next_date < current_date )
307             next_date = current_date;
308
309         /* Get the next audio data buffer */
310         p_aout_buffer = aout_PacketNext( p_aout, next_date );
311
312         if( p_aout_buffer == NULL )
313         {
314             /* Means we are too early to request a new buffer ?
315              * Try once again.
316              */
317             msleep( AOUT_MIN_PREPARE_TIME );
318             next_date = mdate();
319             p_aout_buffer = aout_PacketNext( p_aout, next_date );
320         }
321
322         if ( p_aout_buffer != NULL )
323         {
324             memcpy( ( uint8_t * ) p_buffer + i_len,
325                         p_aout_buffer->p_buffer,
326                         p_aout_buffer->i_buffer );
327
328             i_len += p_aout_buffer->i_buffer;
329
330             next_date += p_aout_buffer->i_length;
331
332             block_Release( p_aout_buffer );
333         }
334         else
335         {
336             memset( ( uint8_t * ) p_buffer + i_len, 0, i_buf_size - i_len );
337
338             i_len = i_buf_size;
339         }
340     }
341
342     return i_buf_size;
343 }