]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/directx.c
OpenSL ES audio output for Android
[vlc] / modules / audio_output / directx.c
index ce57d5bc5d3c1c8d30881110a2055c7368b58e4d..97e659d0dfd593bdba999d10a2f3e388543d5013 100644 (file)
 #include <vlc_plugin.h>
 #include <vlc_aout.h>
 #include <vlc_charset.h>
+#include <vlc_atomic.h>
 
 #include "windows_audio_common.h"
 
-#include <dsound.h>
-
 #define FRAME_SIZE ((int)p_aout->output.output.i_rate/20) /* Size in samples */
 
 /*****************************************************************************
@@ -45,8 +44,6 @@
  *****************************************************************************/
 typedef struct notification_thread_t
 {
-    VLC_COMMON_MEMBERS
-
     aout_instance_t *p_aout;
     int i_frame_size;                          /* size in bytes of one frame */
     int i_write_slot;       /* current write position in our circular buffer */
@@ -54,6 +51,9 @@ typedef struct notification_thread_t
     mtime_t start_date;
     HANDLE event;
 
+    vlc_thread_t thread;
+    vlc_atomic_t abort;
+
 } notification_thread_t;
 
 /*****************************************************************************
@@ -102,7 +102,7 @@ static int  InitDirectSound   ( aout_instance_t * );
 static int  CreateDSBuffer    ( aout_instance_t *, int, int, int, int, int, bool );
 static int  CreateDSBufferPCM ( aout_instance_t *, vlc_fourcc_t*, int, int, int, bool );
 static void DestroyDSBuffer   ( aout_instance_t * );
-static void* DirectSoundThread( vlc_object_t * );
+static void* DirectSoundThread( void * );
 static int  FillBuffer        ( aout_instance_t *, int, aout_buffer_t * );
 
 static int ReloadDirectXDevices( vlc_object_t *, char const *,
@@ -130,21 +130,18 @@ vlc_module_begin ()
     set_capability( "audio output", 100 )
     set_category( CAT_AUDIO )
     set_subcategory( SUBCAT_AUDIO_AOUT )
-    add_shortcut( "directx" )
-    add_shortcut( "directsound" )
+    add_shortcut( "directx", "directsound" )
 
-    add_string( "directx-audio-device-name", "default", NULL,
+    add_string( "directx-audio-device-name", "default",
              DEVICE_TEXT, DEVICE_LONGTEXT, false )
         add_deprecated_alias( "directx-audio-device" ) /* Since 1.1.0 */
         change_string_list( ppsz_adev, ppsz_adev_text, ReloadDirectXDevices )
         change_action_add( ReloadDirectXDevices, N_("Refresh list") )
-        change_need_restart ()
-    add_bool( "directx-audio-float32", false, NULL, FLOAT_TEXT,
+    add_bool( "directx-audio-float32", false, FLOAT_TEXT,
               FLOAT_LONGTEXT, true )
-    add_string( "directx-audio-speaker", "Windows default", NULL,
+    add_string( "directx-audio-speaker", "Windows default",
                  SPEAKER_TEXT, SPEAKER_LONGTEXT, true )
         change_string_list( speaker_list, 0, 0 )
-        change_need_restart ()
 
     set_callbacks( OpenAudio, CloseAudio )
 vlc_module_end ()
@@ -305,30 +302,27 @@ static int OpenAudio( vlc_object_t *p_this )
     }
 
     /* Now we need to setup our DirectSound 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 = calloc( 1, sizeof( *p_aout->output.p_sys->p_notif ) );
     p_aout->output.p_sys->p_notif->p_aout = p_aout;
 
+    vlc_atomic_set(&p_aout->output.p_sys->p_notif->abort, 0);
     p_aout->output.p_sys->p_notif->event = CreateEvent( 0, FALSE, FALSE, 0 );
     p_aout->output.p_sys->p_notif->i_frame_size =
         p_aout->output.p_sys->i_frame_size;
 
     /* then launch the notification thread */
     msg_Dbg( p_aout, "creating DirectSoundThread" );
-    if( vlc_thread_create( p_aout->output.p_sys->p_notif,
-                           "DirectSound Notification Thread",
-                           DirectSoundThread,
-                           VLC_THREAD_PRIORITY_HIGHEST ) )
+    if( vlc_clone( &p_aout->output.p_sys->p_notif->thread,
+                   DirectSoundThread, p_aout->output.p_sys->p_notif,
+                   VLC_THREAD_PRIORITY_HIGHEST ) )
     {
         msg_Err( p_aout, "cannot create DirectSoundThread" );
         CloseHandle( p_aout->output.p_sys->p_notif->event );
-        vlc_object_release( p_aout->output.p_sys->p_notif );
+        free( p_aout->output.p_sys->p_notif );
         p_aout->output.p_sys->p_notif = NULL;
         goto error;
     }
 
-    vlc_object_attach( p_aout->output.p_sys->p_notif, p_aout );
-
     return VLC_SUCCESS;
 
  error:
@@ -553,7 +547,7 @@ static void Probe( aout_instance_t * p_aout )
             text.psz_string = _("A/52 over S/PDIF");
             var_Change( p_aout, "audio-device",
                         VLC_VAR_ADDCHOICE, &val, &text );
-            if( var_InheritInteger( p_aout, "spdif" ) )
+            if( var_InheritBool( p_aout, "spdif" ) )
                 var_Set( p_aout, "audio-device", val );
         }
     }
@@ -567,7 +561,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" );
 }
 
 /*****************************************************************************
@@ -585,12 +579,12 @@ static void Play( aout_instance_t *p_aout )
 
         /* get the playing date of the first aout buffer */
         p_aout->output.p_sys->p_notif->start_date =
-            aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
+            aout_FifoFirstDate( &p_aout->output.fifo );
 
         /* fill in the first samples */
         for( int i = 0; i < FRAMES_NUM; i++ )
         {
-            p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
+            p_buffer = aout_FifoPop( &p_aout->output.fifo );
             if( !p_buffer ) break;
             FillBuffer( p_aout, i, p_buffer );
         }
@@ -613,13 +607,12 @@ static void CloseAudio( vlc_object_t *p_this )
     /* kill the position notification thread, if any */
     if( p_sys->p_notif )
     {
-        vlc_object_detach( p_sys->p_notif );
-        vlc_object_kill( p_sys->p_notif );
+        vlc_atomic_set(&p_aout->output.p_sys->p_notif->abort, 1);
         /* wake up the audio thread if needed */
         if( !p_sys->b_playing ) SetEvent( p_sys->p_notif->event );
 
-        vlc_thread_join( p_sys->p_notif );
-        vlc_object_release( p_sys->p_notif );
+        vlc_join( p_sys->p_notif->thread, NULL );
+        free( p_sys->p_notif );
     }
 
     /* release the secondary buffer */
@@ -961,7 +954,7 @@ static int FillBuffer( aout_instance_t *p_aout, int i_frame,
                 p_notif->i_frame_size,                    /* Number of bytes */
                 &p_write_position,                  /* Address of lock start */
                 &l_bytes1,       /* Count of bytes locked before wrap around */
-                &p_wrap_around,            /* Buffer adress (if wrap around) */
+                &p_wrap_around,           /* Buffer address (if wrap around) */
                 &l_bytes2,               /* Count of bytes after wrap around */
                 0 );                                                /* Flags */
     if( dsresult == DSERR_BUFFERLOST )
@@ -979,7 +972,7 @@ static int FillBuffer( aout_instance_t *p_aout, int i_frame,
     }
     if( dsresult != DS_OK )
     {
-        msg_Warn( p_notif, "cannot lock buffer" );
+        msg_Warn( p_aout, "cannot lock buffer" );
         if( p_buffer ) aout_BufferFree( p_buffer );
         return VLC_EGENERIC;
     }
@@ -1016,9 +1009,9 @@ static int FillBuffer( aout_instance_t *p_aout, int i_frame,
  * We use this thread to emulate a callback mechanism. The thread probes for
  * event notification and fills up the DS secondary buffer when needed.
  *****************************************************************************/
-static void* DirectSoundThread( vlc_object_t *p_this )
+static void* DirectSoundThread( void *data )
 {
-    notification_thread_t *p_notif = (notification_thread_t*)p_this;
+    notification_thread_t *p_notif = (notification_thread_t*)data;
     aout_instance_t *p_aout = p_notif->p_aout;
     mtime_t last_time;
     int canc = vlc_savecancel ();
@@ -1026,12 +1019,12 @@ static void* DirectSoundThread( vlc_object_t *p_this )
     /* We don't want any resampling when using S/PDIF output */
     bool b_sleek = (p_aout->output.output.i_format == VLC_CODEC_SPDIFL);
 
-    msg_Dbg( p_notif, "DirectSoundThread ready" );
+    msg_Dbg( p_aout, "DirectSoundThread ready" );
 
     /* Wait here until Play() is called */
     WaitForSingleObject( p_notif->event, INFINITE );
 
-    if( vlc_object_alive (p_notif) )
+    if( !vlc_atomic_get( &p_notif->abort) )
     {
         HRESULT dsresult;
         mwait( p_notif->start_date - AOUT_PTS_TOLERANCE / 2 );
@@ -1057,7 +1050,7 @@ static void* DirectSoundThread( vlc_object_t *p_this )
     }
     last_time = mdate();
 
-    while( vlc_object_alive (p_notif) )
+    while( !vlc_atomic_get( &p_notif->abort ) )
     {
         DWORD l_read;
         int l_queued = 0, l_free_slots;
@@ -1118,7 +1111,7 @@ static void* DirectSoundThread( vlc_object_t *p_this )
     CloseHandle( p_notif->event );
 
     vlc_restorecancel (canc);
-    msg_Dbg( p_notif, "DirectSoundThread exiting" );
+    msg_Dbg( p_aout, "DirectSoundThread exiting" );
     return NULL;
 }