]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/waveout.c
Made vout_display_opengl_t private.
[vlc] / modules / audio_output / waveout.c
index d23e9335583a62e4539546795b6d42b9203cf5f1..6a6ebc370ddfc20f5b1edd4d4947870c090e991b 100644 (file)
@@ -18,8 +18,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_aout.h>
-#include <vlc_charset.h>
+#include <vlc_charset.h>                        /* FromLocaleDup, LocaleFree */
+#include <vlc_atomic.h>
 
-#include <windows.h>
-#include <mmsystem.h>
+#include "windows_audio_common.h"
 
 #define FRAME_SIZE 4096              /* The size is in samples, not in bytes */
-#define FRAMES_NUM 8
-
-/*****************************************************************************
- * Useful macros
- *****************************************************************************/
-#ifdef UNDER_CE
-#   define DWORD_PTR DWORD
-#   ifdef waveOutGetDevCaps
-#       undef waveOutGetDevCaps
-        MMRESULT WINAPI waveOutGetDevCaps(UINT, LPWAVEOUTCAPS, UINT);
-#   endif
-#endif
-
-#ifndef WAVE_FORMAT_IEEE_FLOAT
-#   define WAVE_FORMAT_IEEE_FLOAT 0x0003
-#endif
-
-#ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
-#   define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
-#endif
-
-#ifndef WAVE_FORMAT_EXTENSIBLE
-#define  WAVE_FORMAT_EXTENSIBLE   0xFFFE
-#endif
-
-#ifndef SPEAKER_FRONT_LEFT
-#   define SPEAKER_FRONT_LEFT             0x1
-#   define SPEAKER_FRONT_RIGHT            0x2
-#   define SPEAKER_FRONT_CENTER           0x4
-#   define SPEAKER_LOW_FREQUENCY          0x8
-#   define SPEAKER_BACK_LEFT              0x10
-#   define SPEAKER_BACK_RIGHT             0x20
-#   define SPEAKER_FRONT_LEFT_OF_CENTER   0x40
-#   define SPEAKER_FRONT_RIGHT_OF_CENTER  0x80
-#   define SPEAKER_BACK_CENTER            0x100
-#   define SPEAKER_SIDE_LEFT              0x200
-#   define SPEAKER_SIDE_RIGHT             0x400
-#   define SPEAKER_TOP_CENTER             0x800
-#   define SPEAKER_TOP_FRONT_LEFT         0x1000
-#   define SPEAKER_TOP_FRONT_CENTER       0x2000
-#   define SPEAKER_TOP_FRONT_RIGHT        0x4000
-#   define SPEAKER_TOP_BACK_LEFT          0x8000
-#   define SPEAKER_TOP_BACK_CENTER        0x10000
-#   define SPEAKER_TOP_BACK_RIGHT         0x20000
-#   define SPEAKER_RESERVED               0x80000000
-#endif
-
-#ifndef _WAVEFORMATEXTENSIBLE_
-typedef struct {
-    WAVEFORMATEX    Format;
-    union {
-        WORD wValidBitsPerSample;       /* bits of precision  */
-        WORD wSamplesPerBlock;          /* valid if wBitsPerSample==0 */
-        WORD wReserved;                 /* If neither applies, set to zero. */
-    } Samples;
-    DWORD           dwChannelMask;      /* which channels are */
-                                        /* present in stream  */
-    GUID            SubFormat;
-} WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
-#endif
-
-static const GUID __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = {WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
-static const GUID __KSDATAFORMAT_SUBTYPE_PCM = {WAVE_FORMAT_PCM, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
-static const GUID __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF = {WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
 
 /*****************************************************************************
  * Local prototypes
@@ -114,13 +50,6 @@ static void Play         ( aout_instance_t * );
 /*****************************************************************************
  * notification_thread_t: waveOut event thread
  *****************************************************************************/
-typedef struct notification_thread_t
-{
-    VLC_COMMON_MEMBERS
-    aout_instance_t *p_aout;
-
-} notification_thread_t;
-
 /* local functions */
 static void Probe        ( aout_instance_t * );
 static int OpenWaveOut   ( aout_instance_t *, uint32_t,
@@ -131,10 +60,9 @@ static int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
                            aout_buffer_t *, bool );
 
 static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
-static void* WaveOutThread( vlc_object_t * );
+static void* WaveOutThread( void * );
 
-static int VolumeGet( aout_instance_t *, audio_volume_t * );
-static int VolumeSet( aout_instance_t *, audio_volume_t );
+static int VolumeSet( aout_instance_t *, audio_volume_t, bool );
 
 static int WaveOutClearDoneBuffers(aout_sys_t *p_sys);
 
@@ -148,14 +76,9 @@ static const char *const ppsz_adev[] = { "wavemapper", };
 static const char *const ppsz_adev_text[] = { N_("Microsoft Soundmapper") };
 
 
-
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-#define FLOAT_TEXT N_("Use float32 output")
-#define FLOAT_LONGTEXT N_( \
-    "The option allows you to enable or disable the high-quality float32 " \
-    "audio output mode (which is not well supported by some soundcards)." )
 #define DEVICE_TEXT N_("Select Audio Device")
 #define DEVICE_LONG N_("Select special Audio device, or let windows "\
                        "decide (default), change needs VLC restart "\
@@ -168,16 +91,14 @@ vlc_module_begin ()
     set_capability( "audio output", 50 )
     set_category( CAT_AUDIO )
     set_subcategory( SUBCAT_AUDIO_AOUT )
-    add_bool( "waveout-float32", 1, 0, FLOAT_TEXT, FLOAT_LONGTEXT, true )
+    add_bool( "waveout-float32", true, FLOAT_TEXT, FLOAT_LONGTEXT, true )
 
-    add_string( "waveout-audio-device", "wavemapper", NULL,
+    add_string( "waveout-audio-device", "wavemapper",
                  DEVICE_TEXT, DEVICE_LONG, false )
        add_deprecated_alias( "waveout-dev" )   /* deprecated since 0.9.3 */
        change_string_list( ppsz_adev, ppsz_adev_text, ReloadWaveoutDevices )
-       change_need_restart ()
        change_action_add( ReloadWaveoutDevices, N_("Refresh list") )
 
-
     set_callbacks( Open, Close )
 vlc_module_end ()
 
@@ -197,7 +118,8 @@ struct aout_sys_t
 
     WAVEHDR waveheader[FRAMES_NUM];
 
-    notification_thread_t *p_notif;                      /* WaveOutThread id */
+    vlc_thread_t thread;
+    vlc_atomic_t abort;
     HANDLE event;
     HANDLE new_buffer_event;
 
@@ -215,23 +137,6 @@ struct aout_sys_t
     int pi_chan_table[AOUT_CHAN_MAX];
 };
 
-static const uint32_t pi_channels_src[] =
-    { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
-      AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
-      AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
-      AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
-static const uint32_t pi_channels_in[] =
-    { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
-      SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
-      SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT, SPEAKER_BACK_CENTER,
-      SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
-static const uint32_t pi_channels_out[] =
-    { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
-      SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
-      SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
-      SPEAKER_BACK_CENTER,
-      SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
-
 /*****************************************************************************
  * Open: open the audio device
  *****************************************************************************
@@ -241,7 +146,6 @@ static int Open( vlc_object_t *p_this )
 {
     aout_instance_t *p_aout = (aout_instance_t *)p_this;
     vlc_value_t val;
-    int i;
 
     /* Allocate structure */
     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
@@ -392,7 +296,6 @@ static int Open( vlc_object_t *p_this )
             if( waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_dummy )
                 == MMSYSERR_NOERROR )
             {
-                p_aout->output.pf_volume_get = VolumeGet;
                 p_aout->output.pf_volume_set = VolumeSet;
             }
         }
@@ -417,9 +320,6 @@ static int Open( vlc_object_t *p_this )
             p_aout->output.p_sys->i_buffer_size );
 
     /* Now we need to setup our waveOut play notification structure */
-    p_aout->output.p_sys->p_notif =
-        vlc_object_create( p_aout, sizeof(notification_thread_t) );
-    p_aout->output.p_sys->p_notif->p_aout = p_aout;
     p_aout->output.p_sys->event = CreateEvent( NULL, FALSE, FALSE, NULL );
     p_aout->output.p_sys->new_buffer_event = CreateEvent( NULL, FALSE, FALSE, NULL );
 
@@ -430,16 +330,16 @@ static int Open( vlc_object_t *p_this )
 
 
     /* Then launch the notification thread */
-    if( vlc_thread_create( p_aout->output.p_sys->p_notif,
-                           "waveOut Notification Thread", WaveOutThread,
-                           VLC_THREAD_PRIORITY_OUTPUT ) )
+    vlc_atomic_set( &p_aout->output.p_sys->abort, 0);
+    if( vlc_clone( &p_aout->output.p_sys->thread,
+                   WaveOutThread, p_aout, VLC_THREAD_PRIORITY_OUTPUT ) )
     {
         msg_Err( p_aout, "cannot create WaveOutThread" );
     }
 
     /* We need to kick off the playback in order to have the callback properly
      * working */
-    for( i = 0; i < FRAMES_NUM; i++ )
+    for( int i = 0; i < FRAMES_NUM; i++ )
     {
         p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
         p_aout->output.p_sys->waveheader[i].dwUser = 0;
@@ -549,7 +449,7 @@ static void Probe( aout_instance_t * p_aout )
             text.psz_string = (char *)_("A/52 over S/PDIF");
             var_Change( p_aout, "audio-device",
                         VLC_VAR_ADDCHOICE, &val, &text );
-            if( config_GetInt( p_aout, "spdif" ) )
+            if( var_InheritBool( p_aout, "spdif" ) )
                 var_Set( p_aout, "audio-device", val );
         }
     }
@@ -563,7 +463,7 @@ static void Probe( aout_instance_t * p_aout )
     }
 
     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
-    var_SetBool( p_aout, "intf-change", true );
+    var_TriggerCallback( p_aout, "intf-change" );
 }
 
 /*****************************************************************************
@@ -600,14 +500,13 @@ static void Close( vlc_object_t *p_this )
     aout_sys_t *p_sys = p_aout->output.p_sys;
 
     /* Before calling waveOutClose we must reset the device */
-    vlc_object_kill( p_aout );
+    vlc_atomic_set( &p_sys->abort, 1);
 
     /* wake up the audio thread, to recognize that p_aout died */
     SetEvent( p_sys->event );
     SetEvent( p_sys->new_buffer_event );
 
-    vlc_thread_join( p_sys->p_notif );
-    vlc_object_release( p_sys->p_notif );
+    vlc_join( p_sys->thread, NULL );
 
     /*
       kill the real output then - when the feed thread
@@ -676,14 +575,13 @@ static int OpenWaveOut( aout_instance_t *p_aout, uint32_t i_device_id, int i_for
                         bool b_probe )
 {
     MMRESULT result;
-    unsigned int i;
 
     /* Set sound format */
 
 #define waveformat p_aout->output.p_sys->waveformat
 
     waveformat.dwChannelMask = 0;
-    for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
+    for( unsigned i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
     {
         if( i_channels & pi_channels_src[i] )
             waveformat.dwChannelMask |= pi_channels_in[i];
@@ -844,7 +742,7 @@ static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
     /* Prepare the buffer */
     if( p_buffer != NULL )
     {
-        p_waveheader->lpData = p_buffer->p_buffer;
+        p_waveheader->lpData = (LPSTR)p_buffer->p_buffer;
         /*
           copy the buffer to the silence buffer :) so in case we don't
           get the next buffer fast enough (I will repeat this one a time
@@ -869,7 +767,7 @@ static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
                            0x00, p_aout->output.p_sys->i_buffer_size );
            }
         }
-        p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
+        p_waveheader->lpData = (LPSTR)p_aout->output.p_sys->p_silence_buffer;
     }
 
     p_waveheader->dwUser = p_buffer ? (DWORD_PTR)p_buffer : (DWORD_PTR)1;
@@ -903,14 +801,14 @@ static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
 {
     (void)h_waveout;    (void)dwParam1;    (void)dwParam2;
     aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
-    int i, i_queued_frames = 0;
+    int i_queued_frames = 0;
 
     if( uMsg != WOM_DONE ) return;
 
-    if( !vlc_object_alive (p_aout) ) return;
+    if( vlc_atomic_get(&p_aout->output.p_sys->abort) ) return;
 
     /* Find out the current latency */
-    for( i = 0; i < FRAMES_NUM; i++ )
+    for( int i = 0; i < FRAMES_NUM; i++ )
     {
         /* Check if frame buf is available */
         if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
@@ -968,10 +866,9 @@ static int WaveOutClearDoneBuffers(aout_sys_t *p_sys)
  * we are not authorized to use waveOutWrite() directly in the waveout
  * callback.
  *****************************************************************************/
-static void* WaveOutThread( vlc_object_t *p_this )
+static void* WaveOutThread( void *data )
 {
-    notification_thread_t *p_notif = (notification_thread_t*)p_this;
-    aout_instance_t *p_aout = p_notif->p_aout;
+    aout_instance_t *p_aout = data;
     aout_sys_t *p_sys = p_aout->output.p_sys;
     aout_buffer_t *p_buffer = NULL;
     WAVEHDR *p_waveheader = p_sys->waveheader;
@@ -985,9 +882,9 @@ static void* WaveOutThread( vlc_object_t *p_this )
     b_sleek = p_aout->output.output.i_format == VLC_CODEC_SPDIFL;
 
     // wait for first call to "play()"
-    while( !p_sys->start_date && vlc_object_alive (p_aout) )
+    while( !p_sys->start_date && !vlc_atomic_get(&p_aout->output.p_sys->abort) )
            WaitForSingleObject( p_sys->event, INFINITE );
-    if( !vlc_object_alive (p_aout) )
+    if( vlc_atomic_get(&p_aout->output.p_sys->abort) )
         return NULL;
 
     msg_Dbg( p_aout, "will start to play in %"PRId64" us",
@@ -1004,12 +901,12 @@ static void* WaveOutThread( vlc_object_t *p_this )
                            p_aout->output.b_starving, msg);
     next_date = mdate();
 
-    while( vlc_object_alive (p_aout) )
+    while( !vlc_atomic_get(&p_aout->output.p_sys->abort) )
     {
         /* Cleanup and find out the current latency */
         i_queued_frames = WaveOutClearDoneBuffers( p_sys );
 
-        if( !vlc_object_alive (p_aout) ) return NULL;
+        if( vlc_atomic_get(&p_aout->output.p_sys->abort) ) return NULL;
 
         /* Try to fill in as many frame buffers as possible */
         for( i = 0; i < FRAMES_NUM; i++ )
@@ -1062,8 +959,7 @@ static void* WaveOutThread( vlc_object_t *p_this )
 
                 if( p_buffer )
                 {
-                    mtime_t buffer_length = (p_buffer->end_date
-                                             - p_buffer->start_date);
+                    mtime_t buffer_length = p_buffer->i_length;
                     next_date = next_date + buffer_length;
                     i_buffer_length = buffer_length/1000;
                 }
@@ -1072,7 +968,7 @@ static void* WaveOutThread( vlc_object_t *p_this )
                 if( p_buffer && p_sys->b_chan_reorder )
                 {
                     aout_ChannelReorder( p_buffer->p_buffer,
-                        p_buffer->i_nb_bytes,
+                        p_buffer->i_buffer,
                         p_sys->waveformat.Format.nChannels,
                         p_sys->pi_chan_table,
                         p_sys->waveformat.Format.wBitsPerSample );
@@ -1085,7 +981,7 @@ static void* WaveOutThread( vlc_object_t *p_this )
             }
         }
 
-        if( !vlc_object_alive (p_aout) ) return NULL;
+        if( vlc_atomic_get(&p_aout->output.p_sys->abort) ) return NULL;
 
         /*
           deal with the case that the loop didn't fillup the buffer to the
@@ -1110,24 +1006,12 @@ static void* WaveOutThread( vlc_object_t *p_this )
     return NULL;
 }
 
-static int VolumeGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
+static int VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume,
+                      bool mute )
 {
-    DWORD i_waveout_vol;
-
-#ifdef UNDER_CE
-    waveOutGetVolume( 0, &i_waveout_vol );
-#else
-    waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_waveout_vol );
-#endif
+    if( mute )
+        i_volume = AOUT_VOLUME_MIN;
 
-    i_waveout_vol &= 0xFFFF;
-    *pi_volume = p_aout->output.i_volume =
-        (i_waveout_vol * AOUT_VOLUME_MAX + 0xFFFF /*rounding*/) / 2 / 0xFFFF;
-    return 0;
-}
-
-static int VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
-{
     unsigned long i_waveout_vol = i_volume * 0xFFFF * 2 / AOUT_VOLUME_MAX;
     i_waveout_vol |= (i_waveout_vol << 16);
 
@@ -1136,8 +1020,6 @@ static int VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
 #else
     waveOutSetVolume( p_aout->output.p_sys->h_waveout, i_waveout_vol );
 #endif
-
-    p_aout->output.i_volume = i_volume;
     return 0;
 }
 
@@ -1148,7 +1030,7 @@ static int VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
 static int ReloadWaveoutDevices( vlc_object_t *p_this, char const *psz_name,
                                  vlc_value_t newval, vlc_value_t oldval, void *data )
 {
-    int i;
+    VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( data );
 
     module_config_t *p_item = config_FindConfig( p_this, psz_name );
     if( !p_item ) return VLC_SUCCESS;
@@ -1156,6 +1038,8 @@ static int ReloadWaveoutDevices( vlc_object_t *p_this, char const *psz_name,
     /* Clear-up the current list */
     if( p_item->i_list )
     {
+        int i;
+
         /* Keep the first entry */
         for( i = 1; i < p_item->i_list; i++ )
         {
@@ -1170,11 +1054,9 @@ static int ReloadWaveoutDevices( vlc_object_t *p_this, char const *psz_name,
 
     int wave_devices = waveOutGetNumDevs();
 
-    p_item->ppsz_list =
-        (char **)realloc( p_item->ppsz_list,
+    p_item->ppsz_list = xrealloc( p_item->ppsz_list,
                           (wave_devices+2) * sizeof(char *) );
-    p_item->ppsz_list_text =
-        (char **)realloc( p_item->ppsz_list_text,
+    p_item->ppsz_list_text = xrealloc( p_item->ppsz_list_text,
                           (wave_devices+2) * sizeof(char *) );
 
     WAVEOUTCAPS caps;
@@ -1213,18 +1095,19 @@ static int ReloadWaveoutDevices( vlc_object_t *p_this, char const *psz_name,
 */
 static uint32_t findDeviceID(char *psz_device_name)
 {
-    if(!psz_device_name)
+    if( !psz_device_name )
        return WAVE_MAPPER;
 
     uint32_t wave_devices = waveOutGetNumDevs();
     WAVEOUTCAPS caps;
     char sz_dev_name[MAXPNAMELEN+32];
-    for(uint32_t i=0; i<wave_devices; i++)
+
+    for( uint32_t i = 0; i < wave_devices; i++ )
     {
-        if(waveOutGetDevCaps(i, &caps, sizeof(WAVEOUTCAPS))
+        if( waveOutGetDevCaps( i, &caps, sizeof(WAVEOUTCAPS) )
            == MMSYSERR_NOERROR)
         {
-            sprintf(sz_dev_name, psz_device_name_fmt, caps.szPname,
+            sprintf( sz_dev_name, psz_device_name_fmt, caps.szPname,
                                                caps.wMid,
                                                caps.wPid
                                               );