]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/waveout.c
Fix potential memleak.
[vlc] / modules / audio_output / waveout.c
index f6b7382e4b70bcc6948e351d817c218a21f3cba0..bf9ac039186fd44a0fb4954c82d4906ccbb77b7e 100644 (file)
@@ -29,7 +29,8 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_aout.h>
 #include <vlc_charset.h>
 
@@ -122,11 +123,11 @@ typedef struct notification_thread_t
 /* local functions */
 static void Probe        ( aout_instance_t * );
 static int OpenWaveOut   ( aout_instance_t *, uint32_t,
-                           int, int, int, int, vlc_bool_t );
+                           int, int, int, int, bool );
 static int OpenWaveOutPCM( aout_instance_t *, uint32_t,
-                           int*, int, int, int, vlc_bool_t );
+                           int*, int, int, int, bool );
 static int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
-                           aout_buffer_t *, vlc_bool_t );
+                           aout_buffer_t *, bool );
 
 static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
 static void WaveOutThread( notification_thread_t * );
@@ -143,8 +144,8 @@ static uint32_t findDeviceID(char *);
 
 static const char psz_device_name_fmt[] = "%s ($%x,$%x)";
 
-static const char *ppsz_adev[] = { "wavemapper", };
-static const char *ppsz_adev_text[] = { N_("Microsoft Soundmapper") };
+static const char *const ppsz_adev[] = { "wavemapper", };
+static const char *const ppsz_adev_text[] = { N_("Microsoft Soundmapper") };
 
 
 
@@ -163,14 +164,14 @@ static const char *ppsz_adev_text[] = { N_("Microsoft Soundmapper") };
 
 vlc_module_begin();
     set_shortname( "WaveOut" );
-    set_description( _("Win32 waveOut extension output") );
+    set_description( N_("Win32 waveOut extension output") );
     set_capability( "audio output", 50 );
     set_category( CAT_AUDIO );
     set_subcategory( SUBCAT_AUDIO_AOUT );
-    add_bool( "waveout-float32", 1, 0, FLOAT_TEXT, FLOAT_LONGTEXT, VLC_TRUE );
+    add_bool( "waveout-float32", 1, 0, FLOAT_TEXT, FLOAT_LONGTEXT, true );
 
     add_string( "waveout-dev", "wavemapper", NULL,
-                 DEVICE_TEXT, DEVICE_LONG, VLC_FALSE );
+                 DEVICE_TEXT, DEVICE_LONG, false );
        change_string_list( ppsz_adev, ppsz_adev_text, ReloadWaveoutDevices );
        change_need_restart();
        change_action_add( ReloadWaveoutDevices, N_("Refresh list") );
@@ -207,9 +208,9 @@ struct aout_sys_t
 
     int i_buffer_size;
 
-    byte_t *p_silence_buffer;               /* buffer we use to play silence */
+    uint8_t *p_silence_buffer;              /* buffer we use to play silence */
 
-    vlc_bool_t b_chan_reorder;              /* do we need channel reordering */
+    bool b_chan_reorder;              /* do we need channel reordering */
     int pi_chan_table[AOUT_CHAN_MAX];
 };
 
@@ -244,13 +245,10 @@ static int Open( vlc_object_t *p_this )
     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
 
     if( p_aout->output.p_sys == NULL )
-    {
-        msg_Err( p_aout, "out of memory" );
-        return VLC_EGENERIC;
-    }
+        return VLC_ENOMEM;
 
     p_aout->output.pf_play = Play;
-    p_aout->b_die = VLC_FALSE;
+    p_aout->b_die = false;
 
 
     /*
@@ -276,7 +274,7 @@ static int Open( vlc_object_t *p_this )
                          "use default instead", psz_waveout_dev );
        }
     }
-    if(psz_waveout_dev) free( psz_waveout_dev );
+    free( psz_waveout_dev );
 
 
     WAVEOUTCAPS waveoutcaps;
@@ -320,7 +318,7 @@ static int Open( vlc_object_t *p_this )
                          VLC_FOURCC('s','p','d','i'),
                          p_aout->output.output.i_physical_channels,
                          aout_FormatNbChannels( &p_aout->output.output ),
-                         p_aout->output.output.i_rate, VLC_FALSE )
+                         p_aout->output.output.i_rate, false )
             != VLC_SUCCESS )
         {
             msg_Err( p_aout, "cannot open waveout audio device" );
@@ -369,7 +367,7 @@ static int Open( vlc_object_t *p_this )
                             &p_aout->output.output.i_format,
                             p_aout->output.output.i_physical_channels,
                             aout_FormatNbChannels( &p_aout->output.output ),
-                            p_aout->output.output.i_rate, VLC_FALSE )
+                            p_aout->output.output.i_rate, false )
             != VLC_SUCCESS )
         {
             msg_Err( p_aout, "cannot open waveout audio device" );
@@ -410,7 +408,6 @@ static int Open( vlc_object_t *p_this )
     if( p_aout->output.p_sys->p_silence_buffer == NULL )
     {
         free( p_aout->output.p_sys );
-        msg_Err( p_aout, "out of memory" );
         return 1;
     }
     p_aout->output.p_sys->i_repeat_counter = 0;
@@ -436,7 +433,7 @@ 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_FALSE ) )
+                           VLC_THREAD_PRIORITY_OUTPUT, false ) )
     {
         msg_Err( p_aout, "cannot create WaveOutThread" );
     }
@@ -475,7 +472,7 @@ static void Probe( aout_instance_t * p_aout )
                             p_aout->output.p_sys->i_wave_device_id,
                             &i_format,
                             i_physical_channels, 6,
-                            p_aout->output.output.i_rate, VLC_TRUE )
+                            p_aout->output.output.i_rate, true )
             == VLC_SUCCESS )
         {
             val.i_int = AOUT_VAR_5_1;
@@ -496,7 +493,7 @@ static void Probe( aout_instance_t * p_aout )
                             p_aout->output.p_sys->i_wave_device_id,
                             &i_format,
                             i_physical_channels, 4,
-                            p_aout->output.output.i_rate, VLC_TRUE )
+                            p_aout->output.output.i_rate, true )
             == VLC_SUCCESS )
         {
             val.i_int = AOUT_VAR_2F2R;
@@ -513,7 +510,7 @@ static void Probe( aout_instance_t * p_aout )
                         p_aout->output.p_sys->i_wave_device_id,
                         &i_format,
                         i_physical_channels, 2,
-                        p_aout->output.output.i_rate, VLC_TRUE )
+                        p_aout->output.output.i_rate, true )
         == VLC_SUCCESS )
     {
         val.i_int = AOUT_VAR_STEREO;
@@ -528,7 +525,7 @@ static void Probe( aout_instance_t * p_aout )
                         p_aout->output.p_sys->i_wave_device_id,
                         &i_format,
                         i_physical_channels, 1,
-                        p_aout->output.output.i_rate, VLC_TRUE )
+                        p_aout->output.output.i_rate, true )
         == VLC_SUCCESS )
     {
         val.i_int = AOUT_VAR_MONO;
@@ -545,7 +542,7 @@ static void Probe( aout_instance_t * p_aout )
                          VLC_FOURCC('s','p','d','i'),
                          p_aout->output.output.i_physical_channels,
                          aout_FormatNbChannels( &p_aout->output.output ),
-                         p_aout->output.output.i_rate, VLC_TRUE )
+                         p_aout->output.output.i_rate, true )
             == VLC_SUCCESS )
         {
             msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
@@ -568,7 +565,7 @@ static void Probe( aout_instance_t * p_aout )
 
     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
 
-    val.b_bool = VLC_TRUE;
+    val.b_bool = true;
     var_Set( p_aout, "intf-change", val );
 }
 
@@ -679,7 +676,7 @@ static void Close( vlc_object_t *p_this )
  ****************************************************************************/
 static int OpenWaveOut( aout_instance_t *p_aout, uint32_t i_device_id, int i_format,
                         int i_channels, int i_nb_channels, int i_rate,
-                        vlc_bool_t b_probe )
+                        bool b_probe )
 {
     MMRESULT result;
     unsigned int i;
@@ -810,9 +807,9 @@ static int OpenWaveOut( aout_instance_t *p_aout, uint32_t i_device_id, int i_for
  ****************************************************************************/
 static int OpenWaveOutPCM( aout_instance_t *p_aout, uint32_t i_device_id, int *i_format,
                            int i_channels, int i_nb_channels, int i_rate,
-                           vlc_bool_t b_probe )
+                           bool b_probe )
 {
-    vlc_bool_t b_use_float32 = var_CreateGetBool( p_aout, "waveout-float32");
+    bool b_use_float32 = var_CreateGetBool( p_aout, "waveout-float32");
 
     if( !b_use_float32 || OpenWaveOut( p_aout, i_device_id, VLC_FOURCC('f','l','3','2'),
                                    i_channels, i_nb_channels, i_rate, b_probe )
@@ -842,7 +839,7 @@ static int OpenWaveOutPCM( aout_instance_t *p_aout, uint32_t i_device_id, int *i
  *****************************************************************************/
 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
                         WAVEHDR *p_waveheader, aout_buffer_t *p_buffer,
-                        vlc_bool_t b_spdif)
+                        bool b_spdif)
 {
     MMRESULT result;
 
@@ -858,9 +855,9 @@ static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
         */
         if(b_spdif)
         {
-           p_aout->p_libvlc->pf_memcpy( p_aout->output.p_sys->p_silence_buffer,
-                                     p_buffer->p_buffer,
-                                     p_aout->output.p_sys->i_buffer_size );
+           vlc_memcpy( p_aout->output.p_sys->p_silence_buffer,
+                       p_buffer->p_buffer,
+                       p_aout->output.p_sys->i_buffer_size );
            p_aout->output.p_sys->i_repeat_counter = 2;
         }
     } else {
@@ -870,10 +867,8 @@ static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
            p_aout->output.p_sys->i_repeat_counter--;
            if(!p_aout->output.p_sys->i_repeat_counter)
            {
-               p_aout->p_libvlc->pf_memset( p_aout->output.p_sys->p_silence_buffer,
-                                            0x00,
-                                            p_aout->output.p_sys->i_buffer_size
-                                          );
+               vlc_memset( p_aout->output.p_sys->p_silence_buffer,
+                           0x00, p_aout->output.p_sys->i_buffer_size );
            }
         }
         p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
@@ -913,7 +908,7 @@ static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
 
     if( uMsg != WOM_DONE ) return;
 
-    if( p_aout->b_die ) return;
+    if( !vlc_object_alive (p_aout) ) return;
 
     /* Find out the current latency */
     for( i = 0; i < FRAMES_NUM; i++ )
@@ -981,7 +976,7 @@ static void WaveOutThread( notification_thread_t *p_notif )
     aout_buffer_t *p_buffer = NULL;
     WAVEHDR *p_waveheader = p_sys->waveheader;
     int i, i_queued_frames;
-    vlc_bool_t b_sleek;
+    bool b_sleek;
     mtime_t next_date;
     uint32_t i_buffer_length = 64;
 
@@ -989,12 +984,12 @@ static void WaveOutThread( notification_thread_t *p_notif )
     b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
 
     // wait for first call to "play()"
-    while( !p_sys->start_date && !p_aout->b_die )
+    while( !p_sys->start_date && vlc_object_alive (p_aout) )
            WaitForSingleObject( p_sys->event, INFINITE );
-    if( p_aout->b_die )
+    if( !vlc_object_alive (p_aout) )
         return;
 
-    msg_Dbg( p_aout, "will start to play in "I64Fd" us",
+    msg_Dbg( p_aout, "will start to play in %"PRId64" us",
              (p_sys->start_date - AOUT_PTS_TOLERANCE/4)-mdate());
 
     // than wait a short time... before grabbing first frames
@@ -1008,12 +1003,12 @@ static void WaveOutThread( notification_thread_t *p_notif )
                            p_aout->output.b_starving, msg);
     next_date = mdate();
 
-    while( !p_aout->b_die )
+    while( vlc_object_alive (p_aout) )
     {
         /* Cleanup and find out the current latency */
         i_queued_frames = WaveOutClearDoneBuffers( p_sys );
 
-        if( p_aout->b_die ) return;
+        if( !vlc_object_alive (p_aout) ) return;
 
         /* Try to fill in as many frame buffers as possible */
         for( i = 0; i < FRAMES_NUM; i++ )
@@ -1089,7 +1084,7 @@ static void WaveOutThread( notification_thread_t *p_notif )
             }
         }
 
-        if( p_aout->b_die ) return;
+        if( !vlc_object_alive (p_aout) ) return;
 
         /*
           deal with the case that the loop didn't fillup the buffer to the
@@ -1193,11 +1188,11 @@ static int ReloadWaveoutDevices( vlc_object_t *p_this, char const *psz_name,
         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
                                               );
-          p_item->ppsz_list[j] = strdup( sz_dev_name );
+          p_item->ppsz_list[j] = FromLocaleDup( sz_dev_name );
           p_item->ppsz_list_text[j] = FromLocaleDup( sz_dev_name );
           p_item->i_list++;
           j++;
@@ -1208,7 +1203,7 @@ static int ReloadWaveoutDevices( vlc_object_t *p_this, char const *psz_name,
     p_item->ppsz_list_text[j] = NULL;
 
     /* Signal change to the interface */
-    p_item->b_dirty = VLC_TRUE;
+    p_item->b_dirty = true;
 
     return VLC_SUCCESS;
 }
@@ -1216,7 +1211,7 @@ static int ReloadWaveoutDevices( vlc_object_t *p_this, char const *psz_name,
 /*
   convert devicename to device ID for output
   if device not found return WAVE_MAPPER, so let
-  windows decide which prefered audio device
+  windows decide which preferred audio device
   should be used.
 */
 static uint32_t findDeviceID(char *psz_device_name)
@@ -1232,12 +1227,18 @@ static uint32_t findDeviceID(char *psz_device_name)
         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
                                               );
-            if(!stricmp(sz_dev_name,psz_device_name))
-               return i;
+            char *psz_temp = FromLocaleDup(sz_dev_name);
+
+            if( !stricmp(psz_temp, psz_device_name) )
+            {
+                LocaleFree( psz_temp );
+                return i;
+            }
+            LocaleFree( psz_temp );
         }
     }