00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <errno.h>
00028 #include <fcntl.h>
00029 #include <string.h>
00030
00031 #include <stdlib.h>
00032
00033 #include <vlc/vlc.h>
00034 #include <vlc/aout.h>
00035 #include "aout_internal.h"
00036
00037 #include <windows.h>
00038 #include <mmsystem.h>
00039 #include <dsound.h>
00040
00041 #define FRAME_SIZE ((int)p_aout->output.output.i_rate/20)
00042 #define FRAMES_NUM 8
00043
00044
00045
00046
00047
00048
00049 #include <initguid.h>
00050
00051
00052
00053
00054 #ifndef WAVE_FORMAT_IEEE_FLOAT
00055 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
00056 #endif
00057
00058 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
00059 # define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
00060 #endif
00061
00062 #ifndef WAVE_FORMAT_EXTENSIBLE
00063 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
00064 #endif
00065
00066 #ifndef SPEAKER_FRONT_LEFT
00067 # define SPEAKER_FRONT_LEFT 0x1
00068 # define SPEAKER_FRONT_RIGHT 0x2
00069 # define SPEAKER_FRONT_CENTER 0x4
00070 # define SPEAKER_LOW_FREQUENCY 0x8
00071 # define SPEAKER_BACK_LEFT 0x10
00072 # define SPEAKER_BACK_RIGHT 0x20
00073 # define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
00074 # define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
00075 # define SPEAKER_BACK_CENTER 0x100
00076 # define SPEAKER_SIDE_LEFT 0x200
00077 # define SPEAKER_SIDE_RIGHT 0x400
00078 # define SPEAKER_TOP_CENTER 0x800
00079 # define SPEAKER_TOP_FRONT_LEFT 0x1000
00080 # define SPEAKER_TOP_FRONT_CENTER 0x2000
00081 # define SPEAKER_TOP_FRONT_RIGHT 0x4000
00082 # define SPEAKER_TOP_BACK_LEFT 0x8000
00083 # define SPEAKER_TOP_BACK_CENTER 0x10000
00084 # define SPEAKER_TOP_BACK_RIGHT 0x20000
00085 # define SPEAKER_RESERVED 0x80000000
00086 #endif
00087
00088 #ifndef DSSPEAKER_HEADPHONE
00089 # define DSSPEAKER_HEADPHONE 0x00000001
00090 #endif
00091 #ifndef DSSPEAKER_MONO
00092 # define DSSPEAKER_MONO 0x00000002
00093 #endif
00094 #ifndef DSSPEAKER_QUAD
00095 # define DSSPEAKER_QUAD 0x00000003
00096 #endif
00097 #ifndef DSSPEAKER_STEREO
00098 # define DSSPEAKER_STEREO 0x00000004
00099 #endif
00100 #ifndef DSSPEAKER_SURROUND
00101 # define DSSPEAKER_SURROUND 0x00000005
00102 #endif
00103 #ifndef DSSPEAKER_5POINT1
00104 # define DSSPEAKER_5POINT1 0x00000006
00105 #endif
00106
00107 #ifndef _WAVEFORMATEXTENSIBLE_
00108 typedef struct {
00109 WAVEFORMATEX Format;
00110 union {
00111 WORD wValidBitsPerSample;
00112 WORD wSamplesPerBlock;
00113 WORD wReserved;
00114 } Samples;
00115 DWORD dwChannelMask;
00116
00117 GUID SubFormat;
00118 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
00119 #endif
00120
00121 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
00122 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_PCM, WAVE_FORMAT_PCM, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
00123 DEFINE_GUID( _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF, WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
00124
00125
00126
00127
00128 typedef struct notification_thread_t
00129 {
00130 VLC_COMMON_MEMBERS
00131
00132 aout_instance_t *p_aout;
00133 int i_frame_size;
00134 int i_write_slot;
00135
00136 mtime_t start_date;
00137 HANDLE event;
00138
00139 } notification_thread_t;
00140
00141
00142
00143
00144
00145
00146
00147 struct aout_sys_t
00148 {
00149 HINSTANCE hdsound_dll;
00150
00151 int i_device_id;
00152 LPGUID p_device_guid;
00153
00154 LPDIRECTSOUND p_dsobject;
00155 LPDIRECTSOUNDBUFFER p_dsbuffer;
00156
00157
00158
00159 notification_thread_t *p_notif;
00160
00161 int b_playing;
00162
00163 int i_frame_size;
00164
00165 vlc_bool_t b_chan_reorder;
00166 int pi_chan_table[AOUT_CHAN_MAX];
00167 uint32_t i_channel_mask;
00168 uint32_t i_bits_per_sample;
00169 uint32_t i_channels;
00170 };
00171
00172 static const uint32_t pi_channels_src[] =
00173 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
00174 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
00175 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
00176 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
00177 static const uint32_t pi_channels_in[] =
00178 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
00179 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
00180 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
00181 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
00182 static const uint32_t pi_channels_out[] =
00183 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
00184 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
00185 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
00186 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
00187
00188
00189
00190
00191 static int OpenAudio ( vlc_object_t * );
00192 static void CloseAudio ( vlc_object_t * );
00193 static void Play ( aout_instance_t * );
00194
00195
00196 static void Probe ( aout_instance_t * );
00197 static int InitDirectSound ( aout_instance_t * );
00198 static int CreateDSBuffer ( aout_instance_t *, int, int, int, int, int, vlc_bool_t );
00199 static int CreateDSBufferPCM ( aout_instance_t *, int*, int, int, int, vlc_bool_t );
00200 static void DestroyDSBuffer ( aout_instance_t * );
00201 static void DirectSoundThread ( notification_thread_t * );
00202 static int FillBuffer ( aout_instance_t *, int, aout_buffer_t * );
00203
00204
00205
00206
00207 #define DEVICE_TEXT N_("Output device")
00208 #define DEVICE_LONGTEXT N_( \
00209 "DirectX device number: 0 default device, 1..N device by number" \
00210 "(Note that the default device appears as 0 AND another number)." )
00211 #define FLOAT_TEXT N_("Use float32 output")
00212 #define FLOAT_LONGTEXT N_( \
00213 "The option allows you to enable or disable the high-quality float32 " \
00214 "audio output mode (which is not well supported by some soundcards)." )
00215
00216 vlc_module_begin();
00217 set_description( _("DirectX audio output") );
00218 set_shortname( "DirectX" );
00219 set_capability( "audio output", 100 );
00220 set_category( CAT_AUDIO );
00221 set_subcategory( SUBCAT_AUDIO_AOUT );
00222 add_shortcut( "directx" );
00223 add_integer( "directx-audio-device", 0, NULL, DEVICE_TEXT,
00224 DEVICE_LONGTEXT, VLC_TRUE );
00225 add_bool( "directx-audio-float32", 0, 0, FLOAT_TEXT,
00226 FLOAT_LONGTEXT, VLC_TRUE );
00227 set_callbacks( OpenAudio, CloseAudio );
00228 vlc_module_end();
00229
00230
00231
00232
00233
00234
00235 static int OpenAudio( vlc_object_t *p_this )
00236 {
00237 aout_instance_t * p_aout = (aout_instance_t *)p_this;
00238 vlc_value_t val;
00239
00240 msg_Dbg( p_aout, "OpenAudio" );
00241
00242
00243 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
00244 if( p_aout->output.p_sys == NULL )
00245 {
00246 msg_Err( p_aout, "out of memory" );
00247 return VLC_EGENERIC;
00248 }
00249
00250
00251 p_aout->output.p_sys->p_dsobject = NULL;
00252 p_aout->output.p_sys->p_dsbuffer = NULL;
00253 p_aout->output.p_sys->p_notif = NULL;
00254 p_aout->output.p_sys->b_playing = 0;
00255
00256 p_aout->output.pf_play = Play;
00257 aout_VolumeSoftInit( p_aout );
00258
00259
00260 var_Create( p_aout, "directx-audio-float32",
00261 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
00262 var_Create( p_aout, "directx-audio-device",
00263 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
00264 var_Get( p_aout, "directx-audio-device", &val );
00265 p_aout->output.p_sys->i_device_id = val.i_int;
00266 p_aout->output.p_sys->p_device_guid = 0;
00267
00268
00269 if( InitDirectSound( p_aout ) )
00270 {
00271 msg_Err( p_aout, "cannot initialize DirectSound" );
00272 goto error;
00273 }
00274
00275 if( var_Type( p_aout, "audio-device" ) == 0 )
00276 {
00277 Probe( p_aout );
00278 }
00279
00280 if( var_Get( p_aout, "audio-device", &val ) < 0 )
00281 {
00282
00283 goto error;
00284 }
00285
00286
00287 if( val.i_int == AOUT_VAR_SPDIF )
00288 {
00289 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
00290
00291
00292 p_aout->output.i_nb_samples = A52_FRAME_NB;
00293 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
00294 p_aout->output.output.i_frame_length = A52_FRAME_NB;
00295 p_aout->output.p_sys->i_frame_size =
00296 p_aout->output.output.i_bytes_per_frame;
00297
00298 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
00299 p_aout->output.output.i_physical_channels,
00300 aout_FormatNbChannels( &p_aout->output.output ),
00301 p_aout->output.output.i_rate,
00302 p_aout->output.p_sys->i_frame_size, VLC_FALSE )
00303 != VLC_SUCCESS )
00304 {
00305 msg_Err( p_aout, "cannot open directx audio device" );
00306 free( p_aout->output.p_sys );
00307 return VLC_EGENERIC;
00308 }
00309
00310 aout_VolumeNoneInit( p_aout );
00311 }
00312 else
00313 {
00314 if( val.i_int == AOUT_VAR_5_1 )
00315 {
00316 p_aout->output.output.i_physical_channels
00317 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
00318 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
00319 | AOUT_CHAN_LFE;
00320 }
00321 else if( val.i_int == AOUT_VAR_3F2R )
00322 {
00323 p_aout->output.output.i_physical_channels
00324 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
00325 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
00326 }
00327 else if( val.i_int == AOUT_VAR_2F2R )
00328 {
00329 p_aout->output.output.i_physical_channels
00330 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
00331 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
00332 }
00333 else if( val.i_int == AOUT_VAR_MONO )
00334 {
00335 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
00336 }
00337 else
00338 {
00339 p_aout->output.output.i_physical_channels
00340 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
00341 }
00342
00343 if( CreateDSBufferPCM( p_aout, &p_aout->output.output.i_format,
00344 p_aout->output.output.i_physical_channels,
00345 aout_FormatNbChannels( &p_aout->output.output ),
00346 p_aout->output.output.i_rate, VLC_FALSE )
00347 != VLC_SUCCESS )
00348 {
00349 msg_Err( p_aout, "cannot open directx audio device" );
00350 free( p_aout->output.p_sys );
00351 return VLC_EGENERIC;
00352 }
00353
00354
00355 p_aout->output.i_nb_samples = FRAME_SIZE;
00356 aout_FormatPrepare( &p_aout->output.output );
00357 aout_VolumeSoftInit( p_aout );
00358 }
00359
00360
00361 p_aout->output.p_sys->p_notif =
00362 vlc_object_create( p_aout, sizeof(notification_thread_t) );
00363 p_aout->output.p_sys->p_notif->p_aout = p_aout;
00364
00365 p_aout->output.p_sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
00366 p_aout->output.p_sys->p_notif->i_frame_size =
00367 p_aout->output.p_sys->i_frame_size;
00368
00369
00370 msg_Dbg( p_aout, "creating DirectSoundThread" );
00371 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
00372 "DirectSound Notification Thread",
00373 DirectSoundThread,
00374 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
00375 {
00376 msg_Err( p_aout, "cannot create DirectSoundThread" );
00377 CloseHandle( p_aout->output.p_sys->p_notif->event );
00378 vlc_object_destroy( p_aout->output.p_sys->p_notif );
00379 p_aout->output.p_sys->p_notif = 0;
00380 goto error;
00381 }
00382
00383 vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
00384
00385 return VLC_SUCCESS;
00386
00387 error:
00388 CloseAudio( VLC_OBJECT(p_aout) );
00389 return VLC_EGENERIC;
00390 }
00391
00392
00393
00394
00395 static void Probe( aout_instance_t * p_aout )
00396 {
00397 vlc_value_t val, text;
00398 int i_format;
00399 unsigned int i_physical_channels;
00400 DWORD ui_speaker_config;
00401
00402 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
00403 text.psz_string = _("Audio Device");
00404 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
00405
00406
00407 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
00408 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
00409 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
00410 if( p_aout->output.output.i_physical_channels == i_physical_channels )
00411 {
00412 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 6,
00413 p_aout->output.output.i_rate, VLC_TRUE )
00414 == VLC_SUCCESS )
00415 {
00416 val.i_int = AOUT_VAR_5_1;
00417 text.psz_string = N_("5.1");
00418 var_Change( p_aout, "audio-device",
00419 VLC_VAR_ADDCHOICE, &val, &text );
00420 msg_Dbg( p_aout, "device supports 5.1 channels" );
00421 }
00422 }
00423
00424
00425 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
00426 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
00427 AOUT_CHAN_REARRIGHT;
00428 if( p_aout->output.output.i_physical_channels == i_physical_channels )
00429 {
00430 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 5,
00431 p_aout->output.output.i_rate, VLC_TRUE )
00432 == VLC_SUCCESS )
00433 {
00434 val.i_int = AOUT_VAR_3F2R;
00435 text.psz_string = N_("3 Front 2 Rear");
00436 var_Change( p_aout, "audio-device",
00437 VLC_VAR_ADDCHOICE, &val, &text );
00438 msg_Dbg( p_aout, "device supports 5 channels" );
00439 }
00440 }
00441
00442
00443 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
00444 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
00445 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
00446 == i_physical_channels )
00447 {
00448 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 4,
00449 p_aout->output.output.i_rate, VLC_TRUE )
00450 == VLC_SUCCESS )
00451 {
00452 val.i_int = AOUT_VAR_2F2R;
00453 text.psz_string = N_("2 Front 2 Rear");
00454 var_Change( p_aout, "audio-device",
00455 VLC_VAR_ADDCHOICE, &val, &text );
00456 msg_Dbg( p_aout, "device supports 4 channels" );
00457 }
00458 }
00459
00460
00461 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
00462 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 2,
00463 p_aout->output.output.i_rate, VLC_TRUE )
00464 == VLC_SUCCESS )
00465 {
00466 val.i_int = AOUT_VAR_STEREO;
00467 text.psz_string = N_("Stereo");
00468 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
00469 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
00470 msg_Dbg( p_aout, "device supports 2 channels" );
00471 }
00472
00473
00474 i_physical_channels = AOUT_CHAN_CENTER;
00475 if( CreateDSBufferPCM( p_aout, &i_format, i_physical_channels, 1,
00476 p_aout->output.output.i_rate, VLC_TRUE )
00477 == VLC_SUCCESS )
00478 {
00479 val.i_int = AOUT_VAR_MONO;
00480 text.psz_string = N_("Mono");
00481 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
00482 msg_Dbg( p_aout, "device supports 1 channel" );
00483 }
00484
00485
00486
00487 if FAILED( IDirectSound_GetSpeakerConfig( p_aout->output.p_sys->p_dsobject,
00488 &ui_speaker_config ) )
00489 {
00490 ui_speaker_config = DSSPEAKER_STEREO;
00491 }
00492 switch( DSSPEAKER_CONFIG(ui_speaker_config) )
00493 {
00494 case DSSPEAKER_5POINT1:
00495 val.i_int = AOUT_VAR_5_1;
00496 break;
00497 case DSSPEAKER_QUAD:
00498 val.i_int = AOUT_VAR_2F2R;
00499 break;
00500 #if 0
00501
00502 case DSSPEAKER_MONO:
00503 val.i_int = AOUT_VAR_MONO;
00504 break;
00505 #endif
00506 case DSSPEAKER_SURROUND:
00507 case DSSPEAKER_STEREO:
00508 default:
00509 val.i_int = AOUT_VAR_STEREO;
00510 break;
00511 }
00512 var_Set( p_aout, "audio-device", val );
00513
00514
00515 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
00516 {
00517 if( CreateDSBuffer( p_aout, VLC_FOURCC('s','p','d','i'),
00518 p_aout->output.output.i_physical_channels,
00519 aout_FormatNbChannels( &p_aout->output.output ),
00520 p_aout->output.output.i_rate,
00521 AOUT_SPDIF_SIZE, VLC_TRUE )
00522 == VLC_SUCCESS )
00523 {
00524 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
00525 val.i_int = AOUT_VAR_SPDIF;
00526 text.psz_string = N_("A/52 over S/PDIF");
00527 var_Change( p_aout, "audio-device",
00528 VLC_VAR_ADDCHOICE, &val, &text );
00529 if( config_GetInt( p_aout, "spdif" ) )
00530 var_Set( p_aout, "audio-device", val );
00531 }
00532 }
00533
00534 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
00535 if( val.i_int <= 0 )
00536 {
00537
00538 var_Destroy( p_aout, "audio-device" );
00539 return;
00540 }
00541
00542 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
00543
00544 val.b_bool = VLC_TRUE;
00545 var_Set( p_aout, "intf-change", val );
00546 }
00547
00548
00549
00550
00551
00552
00553 static void Play( aout_instance_t *p_aout )
00554 {
00555 if( !p_aout->output.p_sys->b_playing )
00556 {
00557 aout_buffer_t *p_buffer;
00558 int i;
00559
00560 p_aout->output.p_sys->b_playing = 1;
00561
00562
00563 p_aout->output.p_sys->p_notif->start_date =
00564 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
00565
00566
00567 for( i = 0; i < FRAMES_NUM; i++ )
00568 {
00569 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
00570 if( !p_buffer ) break;
00571 FillBuffer( p_aout, i, p_buffer );
00572 }
00573
00574
00575 SetEvent( p_aout->output.p_sys->p_notif->event );
00576 }
00577 }
00578
00579
00580
00581
00582 static void CloseAudio( vlc_object_t *p_this )
00583 {
00584 aout_instance_t * p_aout = (aout_instance_t *)p_this;
00585 aout_sys_t *p_sys = p_aout->output.p_sys;
00586
00587 msg_Dbg( p_aout, "CloseAudio" );
00588
00589
00590 if( p_sys->p_notif )
00591 {
00592 vlc_object_detach( p_sys->p_notif );
00593 if( p_sys->p_notif->b_thread )
00594 {
00595 p_sys->p_notif->b_die = 1;
00596
00597
00598 if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
00599
00600 vlc_thread_join( p_sys->p_notif );
00601 }
00602 vlc_object_destroy( p_sys->p_notif );
00603 }
00604
00605
00606 DestroyDSBuffer( p_aout );
00607
00608
00609 if( p_sys->p_dsobject ) IDirectSound_Release( p_sys->p_dsobject );
00610
00611
00612 if( p_sys->hdsound_dll ) FreeLibrary( p_sys->hdsound_dll );
00613
00614 if( p_aout->output.p_sys->p_device_guid )
00615 free( p_aout->output.p_sys->p_device_guid );
00616
00617 free( p_sys );
00618 }
00619
00620
00621
00622
00623 static int CALLBACK CallBackDirectSoundEnum( LPGUID p_guid, LPCSTR psz_desc,
00624 LPCSTR psz_mod, LPVOID _p_aout )
00625 {
00626 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
00627
00628 msg_Dbg( p_aout, "found device: %s", psz_desc );
00629
00630 if( p_aout->output.p_sys->i_device_id == 0 && p_guid )
00631 {
00632 p_aout->output.p_sys->p_device_guid = malloc( sizeof( GUID ) );
00633 *p_aout->output.p_sys->p_device_guid = *p_guid;
00634 msg_Dbg( p_aout, "using device: %s", psz_desc );
00635 }
00636
00637 p_aout->output.p_sys->i_device_id--;
00638 return 1;
00639 }
00640
00641
00642
00643
00644 static int InitDirectSound( aout_instance_t *p_aout )
00645 {
00646 HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
00647 HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACK, LPVOID);
00648
00649 p_aout->output.p_sys->hdsound_dll = LoadLibrary("DSOUND.DLL");
00650 if( p_aout->output.p_sys->hdsound_dll == NULL )
00651 {
00652 msg_Warn( p_aout, "cannot open DSOUND.DLL" );
00653 goto error;
00654 }
00655
00656 OurDirectSoundCreate = (void *)
00657 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
00658 "DirectSoundCreate" );
00659 if( OurDirectSoundCreate == NULL )
00660 {
00661 msg_Warn( p_aout, "GetProcAddress FAILED" );
00662 goto error;
00663 }
00664
00665
00666 OurDirectSoundEnumerate = (void *)
00667 GetProcAddress( p_aout->output.p_sys->hdsound_dll,
00668 "DirectSoundEnumerateA" );
00669 if( OurDirectSoundEnumerate )
00670 {
00671
00672 if( FAILED( OurDirectSoundEnumerate( CallBackDirectSoundEnum,
00673 p_aout ) ) )
00674 {
00675 msg_Dbg( p_aout, "enumeration of DirectSound devices failed" );
00676 }
00677 }
00678
00679
00680 if FAILED( OurDirectSoundCreate( p_aout->output.p_sys->p_device_guid,
00681 &p_aout->output.p_sys->p_dsobject,
00682 NULL ) )
00683 {
00684 msg_Warn( p_aout, "cannot create a direct sound device" );
00685 goto error;
00686 }
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698 if( IDirectSound_SetCooperativeLevel( p_aout->output.p_sys->p_dsobject,
00699 GetDesktopWindow(),
00700 DSSCL_EXCLUSIVE) )
00701 {
00702 msg_Warn( p_aout, "cannot set direct sound cooperative level" );
00703 }
00704
00705 return VLC_SUCCESS;
00706
00707 error:
00708 p_aout->output.p_sys->p_dsobject = NULL;
00709 if( p_aout->output.p_sys->hdsound_dll )
00710 {
00711 FreeLibrary( p_aout->output.p_sys->hdsound_dll );
00712 p_aout->output.p_sys->hdsound_dll = NULL;
00713 }
00714 return VLC_EGENERIC;
00715
00716 }
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730 static int CreateDSBuffer( aout_instance_t *p_aout, int i_format,
00731 int i_channels, int i_nb_channels, int i_rate,
00732 int i_bytes_per_frame, vlc_bool_t b_probe )
00733 {
00734 WAVEFORMATEXTENSIBLE waveformat;
00735 DSBUFFERDESC dsbdesc;
00736 unsigned int i;
00737
00738
00739 waveformat.dwChannelMask = 0;
00740 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
00741 {
00742 if( i_channels & pi_channels_src[i] )
00743 waveformat.dwChannelMask |= pi_channels_in[i];
00744 }
00745
00746 switch( i_format )
00747 {
00748 case VLC_FOURCC('s','p','d','i'):
00749 i_nb_channels = 2;
00750
00751 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
00752 waveformat.Format.wBitsPerSample = 16;
00753 waveformat.Samples.wValidBitsPerSample =
00754 waveformat.Format.wBitsPerSample;
00755 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
00756 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
00757 break;
00758
00759 case VLC_FOURCC('f','l','3','2'):
00760 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
00761 waveformat.Samples.wValidBitsPerSample =
00762 waveformat.Format.wBitsPerSample;
00763 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
00764 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
00765 break;
00766
00767 case VLC_FOURCC('s','1','6','l'):
00768 waveformat.Format.wBitsPerSample = 16;
00769 waveformat.Samples.wValidBitsPerSample =
00770 waveformat.Format.wBitsPerSample;
00771 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
00772 waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
00773 break;
00774 }
00775
00776 waveformat.Format.nChannels = i_nb_channels;
00777 waveformat.Format.nSamplesPerSec = i_rate;
00778 waveformat.Format.nBlockAlign =
00779 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
00780 waveformat.Format.nAvgBytesPerSec =
00781 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
00782
00783 p_aout->output.p_sys->i_bits_per_sample = waveformat.Format.wBitsPerSample;
00784 p_aout->output.p_sys->i_channels = i_nb_channels;
00785
00786
00787 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
00788 dsbdesc.dwSize = sizeof(DSBUFFERDESC);
00789 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2
00790 | DSBCAPS_GLOBALFOCUS;
00791
00792
00793 if( i_nb_channels <= 2 )
00794 {
00795 waveformat.Format.cbSize = 0;
00796 }
00797 else
00798 {
00799 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
00800 waveformat.Format.cbSize =
00801 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
00802
00803
00804 dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
00805 }
00806
00807 dsbdesc.dwBufferBytes = FRAMES_NUM * i_bytes_per_frame;
00808 dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&waveformat;
00809
00810 if FAILED( IDirectSound_CreateSoundBuffer(
00811 p_aout->output.p_sys->p_dsobject, &dsbdesc,
00812 &p_aout->output.p_sys->p_dsbuffer, NULL) )
00813 {
00814 if( dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE )
00815 {
00816
00817 dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
00818 if FAILED( IDirectSound_CreateSoundBuffer(
00819 p_aout->output.p_sys->p_dsobject, &dsbdesc,
00820 &p_aout->output.p_sys->p_dsbuffer, NULL) )
00821 {
00822 return VLC_EGENERIC;
00823 }
00824 if( !b_probe )
00825 msg_Dbg( p_aout, "couldn't use hardware sound buffer" );
00826 }
00827 else
00828 {
00829 return VLC_EGENERIC;
00830 }
00831 }
00832
00833
00834 if( b_probe )
00835 {
00836 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
00837 p_aout->output.p_sys->p_dsbuffer = NULL;
00838 return VLC_SUCCESS;
00839 }
00840
00841 p_aout->output.p_sys->i_frame_size = i_bytes_per_frame;
00842 p_aout->output.p_sys->i_channel_mask = waveformat.dwChannelMask;
00843 p_aout->output.p_sys->b_chan_reorder =
00844 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
00845 waveformat.dwChannelMask, i_nb_channels,
00846 p_aout->output.p_sys->pi_chan_table );
00847
00848 if( p_aout->output.p_sys->b_chan_reorder )
00849 {
00850 msg_Dbg( p_aout, "channel reordering needed" );
00851 }
00852
00853 return VLC_SUCCESS;
00854 }
00855
00856
00857
00858
00859
00860
00861
00862 static int CreateDSBufferPCM( aout_instance_t *p_aout, int *i_format,
00863 int i_channels, int i_nb_channels, int i_rate,
00864 vlc_bool_t b_probe )
00865 {
00866 vlc_value_t val;
00867
00868 var_Get( p_aout, "directx-audio-float32", &val );
00869
00870
00871
00872 if( !val.b_bool || i_nb_channels > 2 ||
00873 CreateDSBuffer( p_aout, VLC_FOURCC('f','l','3','2'),
00874 i_channels, i_nb_channels, i_rate,
00875 FRAME_SIZE * 4 * i_nb_channels, b_probe )
00876 != VLC_SUCCESS )
00877 {
00878 if ( CreateDSBuffer( p_aout, VLC_FOURCC('s','1','6','l'),
00879 i_channels, i_nb_channels, i_rate,
00880 FRAME_SIZE * 2 * i_nb_channels, b_probe )
00881 != VLC_SUCCESS )
00882 {
00883 return VLC_EGENERIC;
00884 }
00885 else
00886 {
00887 *i_format = VLC_FOURCC('s','1','6','l');
00888 return VLC_SUCCESS;
00889 }
00890 }
00891 else
00892 {
00893 *i_format = VLC_FOURCC('f','l','3','2');
00894 return VLC_SUCCESS;
00895 }
00896 }
00897
00898
00899
00900
00901
00902
00903 static void DestroyDSBuffer( aout_instance_t *p_aout )
00904 {
00905 if( p_aout->output.p_sys->p_dsbuffer )
00906 {
00907 IDirectSoundBuffer_Release( p_aout->output.p_sys->p_dsbuffer );
00908 p_aout->output.p_sys->p_dsbuffer = NULL;
00909 }
00910 }
00911
00912
00913
00914
00915
00916
00917 static int FillBuffer( aout_instance_t *p_aout, int i_frame,
00918 aout_buffer_t *p_buffer )
00919 {
00920 notification_thread_t *p_notif = p_aout->output.p_sys->p_notif;
00921 aout_sys_t *p_sys = p_aout->output.p_sys;
00922 void *p_write_position, *p_wrap_around;
00923 long l_bytes1, l_bytes2;
00924 HRESULT dsresult;
00925
00926
00927 dsresult = IDirectSoundBuffer_Lock(
00928 p_sys->p_dsbuffer,
00929 i_frame * p_notif->i_frame_size,
00930 p_notif->i_frame_size,
00931 &p_write_position,
00932 &l_bytes1,
00933 &p_wrap_around,
00934 &l_bytes2,
00935 0 );
00936 if( dsresult == DSERR_BUFFERLOST )
00937 {
00938 IDirectSoundBuffer_Restore( p_sys->p_dsbuffer );
00939 dsresult = IDirectSoundBuffer_Lock(
00940 p_sys->p_dsbuffer,
00941 i_frame * p_notif->i_frame_size,
00942 p_notif->i_frame_size,
00943 &p_write_position,
00944 &l_bytes1,
00945 &p_wrap_around,
00946 &l_bytes2,
00947 0 );
00948 }
00949 if( dsresult != DS_OK )
00950 {
00951 msg_Warn( p_notif, "cannot lock buffer" );
00952 if( p_buffer ) aout_BufferFree( p_buffer );
00953 return VLC_EGENERIC;
00954 }
00955
00956 if( p_buffer == NULL )
00957 {
00958 memset( p_write_position, 0, l_bytes1 );
00959 }
00960 else
00961 {
00962 if( p_sys->b_chan_reorder )
00963 {
00964
00965 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
00966 p_sys->i_channels, p_sys->pi_chan_table,
00967 p_sys->i_bits_per_sample );
00968 }
00969
00970 p_aout->p_vlc->pf_memcpy( p_write_position, p_buffer->p_buffer,
00971 l_bytes1 );
00972 aout_BufferFree( p_buffer );
00973 }
00974
00975
00976 IDirectSoundBuffer_Unlock( p_sys->p_dsbuffer, p_write_position, l_bytes1,
00977 p_wrap_around, l_bytes2 );
00978
00979 p_notif->i_write_slot = (i_frame + 1) % FRAMES_NUM;
00980 return VLC_SUCCESS;
00981 }
00982
00983
00984
00985
00986
00987
00988
00989 static void DirectSoundThread( notification_thread_t *p_notif )
00990 {
00991 aout_instance_t *p_aout = p_notif->p_aout;
00992 vlc_bool_t b_sleek;
00993 mtime_t last_time;
00994 HRESULT dsresult;
00995 long l_queued = 0;
00996
00997
00998 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
00999
01000
01001 vlc_thread_ready( p_notif );
01002
01003 msg_Dbg( p_notif, "DirectSoundThread ready" );
01004
01005
01006 WaitForSingleObject( p_notif->event, INFINITE );
01007
01008 if( !p_notif->b_die )
01009 {
01010 mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
01011
01012
01013 dsresult = IDirectSoundBuffer_Play( p_aout->output.p_sys->p_dsbuffer,
01014 0,
01015 0,
01016 DSBPLAY_LOOPING );
01017 if( dsresult == DSERR_BUFFERLOST )
01018 {
01019 IDirectSoundBuffer_Restore( p_aout->output.p_sys->p_dsbuffer );
01020 dsresult = IDirectSoundBuffer_Play(
01021 p_aout->output.p_sys->p_dsbuffer,
01022 0,
01023 0,
01024 DSBPLAY_LOOPING );
01025 }
01026 if( dsresult != DS_OK )
01027 {
01028 msg_Err( p_aout, "cannot start playing buffer" );
01029 }
01030 }
01031 last_time = mdate();
01032
01033 while( !p_notif->b_die )
01034 {
01035 long l_read, l_free_slots;
01036 mtime_t mtime = mdate();
01037 int i;
01038
01039
01040
01041
01042
01043
01044 if FAILED( IDirectSoundBuffer_GetCurrentPosition(
01045 p_aout->output.p_sys->p_dsbuffer, &l_read, NULL ) )
01046 {
01047 msg_Err( p_aout, "GetCurrentPosition() failed!" );
01048 l_read = 0;
01049 }
01050
01051
01052 if( l_queued && mtime - last_time >
01053 I64C(1000000) * l_queued / p_aout->output.output.i_rate )
01054 {
01055 msg_Dbg( p_aout, "detected underrun!" );
01056 }
01057 last_time = mtime;
01058
01059
01060 l_read /= p_aout->output.output.i_bytes_per_frame;
01061 l_queued = p_notif->i_write_slot * FRAME_SIZE - l_read;
01062 if( l_queued < 0 ) l_queued += (FRAME_SIZE * FRAMES_NUM);
01063 l_free_slots = (FRAMES_NUM * FRAME_SIZE - l_queued) / FRAME_SIZE;
01064
01065 for( i = 0; i < l_free_slots; i++ )
01066 {
01067 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout,
01068 mtime + I64C(1000000) * (i * FRAME_SIZE + l_queued) /
01069 p_aout->output.output.i_rate, b_sleek );
01070
01071
01072
01073 if( !p_buffer && (i || l_queued / FRAME_SIZE) ) break;
01074
01075 if( FillBuffer( p_aout, p_notif->i_write_slot % FRAMES_NUM,
01076 p_buffer ) != VLC_SUCCESS ) break;
01077 }
01078
01079
01080 l_queued += (i * FRAME_SIZE);
01081 msleep( I64C(1000000) * l_queued / p_aout->output.output.i_rate / 2 );
01082 }
01083
01084
01085 IDirectSoundBuffer_Stop( p_aout->output.p_sys->p_dsbuffer );
01086
01087
01088 CloseHandle( p_notif->event );
01089
01090 msg_Dbg( p_notif, "DirectSoundThread exiting" );
01091 }