[vlc-commits] wasapi: factorize/check COM initialization

Rémi Denis-Courmont git at videolan.org
Wed May 16 22:46:38 CEST 2012


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed May 16 23:46:17 2012 +0300| [e86fa9dd9c85ae34ab90ca912d5256f6e39828d0] | committer: Rémi Denis-Courmont

wasapi: factorize/check COM initialization

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=e86fa9dd9c85ae34ab90ca912d5256f6e39828d0
---

 modules/audio_output/wasapi.c |   53 +++++++++++++++++++++++++++--------------
 1 file changed, 35 insertions(+), 18 deletions(-)

diff --git a/modules/audio_output/wasapi.c b/modules/audio_output/wasapi.c
index 29b26d7..3ccd59b 100644
--- a/modules/audio_output/wasapi.c
+++ b/modules/audio_output/wasapi.c
@@ -46,6 +46,26 @@ vlc_module_begin()
     set_callbacks(Open, Close)
 vlc_module_end()
 
+static bool TryEnter(void)
+{
+    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
+    return SUCCEEDED(hr);
+}
+
+static void Enter(void)
+{
+    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
+    if (unlikely(FAILED(hr)))
+        abort();
+}
+
+static void Leave(void)
+{
+    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
+    if (unlikely(FAILED(hr)))
+        abort();
+}
+
 struct aout_sys_t
 {
     IAudioClient *client;
@@ -65,7 +85,7 @@ static void Play(audio_output_t *aout, block_t *block)
     aout_sys_t *sys = aout->sys;
     HRESULT hr;
 
-    CoInitializeEx(NULL, COINIT_MULTITHREADED);
+    Enter();
     if (likely(sys->clock != NULL))
     {
         UINT64 pos, qpcpos;
@@ -121,7 +141,7 @@ static void Play(audio_output_t *aout, block_t *block)
              + block->i_nb_samples * CLOCK_FREQ / aout->format.i_rate);
     }
 
-    CoUninitialize();
+    Leave();
     block_Release(block);
 }
 
@@ -130,7 +150,7 @@ static void Pause(audio_output_t *aout, bool paused, mtime_t date)
     aout_sys_t *sys = aout->sys;
     HRESULT hr;
 
-    CoInitializeEx(NULL, COINIT_MULTITHREADED);
+    Enter();
     if (paused)
         hr = IAudioClient_Stop(sys->client);
     else
@@ -138,7 +158,7 @@ static void Pause(audio_output_t *aout, bool paused, mtime_t date)
     if (FAILED(hr))
         msg_Warn(aout, "cannot %s stream (error 0x%lx)",
                  paused ? "stop" : "start", hr);
-    CoUninitialize();
+    Leave();
     (void) date;
 }
 
@@ -150,12 +170,12 @@ static void Flush(audio_output_t *aout, bool wait)
     if (wait)
         return; /* Not drain implemented */
 
-    CoInitializeEx(NULL, COINIT_MULTITHREADED);
+    Enter();
     IAudioClient_Stop(sys->client);
     hr = IAudioClient_Reset(sys->client);
     if (FAILED(hr))
         msg_Warn(aout, "cannot reset stream (error 0x%lx)", hr);
-    CoUninitialize();
+    Leave();
 }
 
 static int SimpleVolumeSet(audio_output_t *aout, float vol, bool mute)
@@ -166,8 +186,8 @@ static int SimpleVolumeSet(audio_output_t *aout, float vol, bool mute)
     if (vol > 1.)
         vol = 1.;
 
+    Enter();
     /* NOTE: better change volume while muted (if mute is toggled) */
-    CoInitializeEx(NULL, COINIT_MULTITHREADED);
     if (mute)
     {
         hr = ISimpleAudioVolume_SetMute(sys->volume.simple, true, NULL);
@@ -185,7 +205,7 @@ static int SimpleVolumeSet(audio_output_t *aout, float vol, bool mute)
         if (FAILED(hr))
             msg_Warn(aout, "cannot unmute session (error 0x%lx)", hr);
     }
-    CoUninitialize();
+    Leave();
     return 0;
 }
 
@@ -268,9 +288,7 @@ static void MTAThread(void *data)
     aout_sys_t *sys = aout->sys;
     HRESULT hr;
 
-    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
-    if (unlikely(FAILED(hr)))
-        abort();
+    Enter();
 
     hr = IAudioClient_GetService(sys->client, &IID_IAudioRenderClient,
                                  (void **)&sys->render);
@@ -299,7 +317,7 @@ static void MTAThread(void *data)
         IAudioClock_Release(sys->clock);
     IAudioRenderClient_Release(sys->render);
 fail:
-    CoUninitialize();
+    Leave();
     ReleaseSemaphore(sys->ready, 1, NULL);
 }
 
@@ -323,8 +341,7 @@ static int Open(vlc_object_t *obj)
     sys->done = NULL;
     aout->sys = sys;
 
-    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
-    if (FAILED(hr))
+    if (TryEnter())
     {
         free(sys);
         return VLC_EGENERIC;
@@ -430,7 +447,7 @@ static int Open(vlc_object_t *obj)
     aout->pf_flush = Flush;
     /*if (AOUT_FMT_LINEAR(&format) && !exclusive)*/
         aout_VolumeHardInit(aout, SimpleVolumeSet, false);
-    CoUninitialize();
+    Leave();
     return VLC_SUCCESS;
 error:
     if (sys->done != NULL)
@@ -439,7 +456,7 @@ error:
         CloseHandle(sys->done);
     if (sys->client != NULL)
         IAudioClient_Release(sys->client);
-    CoUninitialize();
+    Leave();
     free(sys);
     return VLC_EGENERIC;
 }
@@ -449,12 +466,12 @@ static void Close (vlc_object_t *obj)
     audio_output_t *aout = (audio_output_t *)obj;
     aout_sys_t *sys = aout->sys;
 
-    CoInitializeEx(NULL, COINIT_MULTITHREADED);
+    Enter();
     ReleaseSemaphore(sys->done, 1, NULL); /* tell MTA thread to finish */
     WaitForSingleObject(sys->ready, INFINITE); /* wait for that ^ */
     IAudioClient_Stop(sys->client); /* should not be needed */
     IAudioClient_Release(sys->client);
-    CoUninitialize();
+    Leave();
 
     CloseHandle(sys->done);
     CloseHandle(sys->ready);



More information about the vlc-commits mailing list