[PATCH 4/6] dsound: Use event based threads

Maarten Lankhorst m.b.lankhorst at gmail.com
Tue Oct 16 07:06:28 CDT 2012


From: Maarten Lankhorst <maarten.lankhorst at canonical.com>

---
 dlls/dsound/dsound.c         | 21 +++++++++++----------
 dlls/dsound/dsound_private.h |  7 ++++---
 dlls/dsound/mixer.c          | 40 ++++++++++++++++++++++------------------
 dlls/dsound/primary.c        | 22 +++++++++++++++++++---
 4 files changed, 56 insertions(+), 34 deletions(-)

diff --git a/dlls/dsound/dsound.c b/dlls/dsound/dsound.c
index 90c0be8..d2a8ed5 100644
--- a/dlls/dsound/dsound.c
+++ b/dlls/dsound/dsound.c
@@ -670,14 +670,13 @@ ULONG DirectSoundDevice_Release(DirectSoundDevice * device)
     TRACE("(%p) ref was %u\n", device, ref + 1);
     if (!ref) {
         int i;
-        timeKillEvent(device->timerID);
-        timeEndPeriod(DS_TIME_RES);
 
-        /* The kill event should have allowed the timer process to expire
-         * but try to grab the lock just in case. Can't hold lock because
-         * secondarybuffer_destroy also grabs the lock */
-        RtlAcquireResourceShared(&(device->buffer_list_lock), TRUE);
-        RtlReleaseResource(&(device->buffer_list_lock));
+        SetEvent(device->sleepev);
+        if (device->thread) {
+            WaitForSingleObject(device->thread, INFINITE);
+            CloseHandle(device->thread);
+        }
+        CloseHandle(device->sleepev);
 
         EnterCriticalSection(&DSOUND_renderers_lock);
         list_remove(&device->entry);
@@ -813,6 +812,7 @@ HRESULT DirectSoundDevice_Initialize(DirectSoundDevice ** ppDevice, LPCGUID lpcG
 
     device->mmdevice = mmdevice;
     device->guid = devGUID;
+    device->sleepev = CreateEventW(0, 0, 0, 0);
 
     hr = DSOUND_ReopenDevice(device, FALSE);
     if (FAILED(hr))
@@ -869,9 +869,10 @@ HRESULT DirectSoundDevice_Initialize(DirectSoundDevice ** ppDevice, LPCGUID lpcG
     ZeroMemory(&device->volpan, sizeof(device->volpan));
 
     hr = DSOUND_PrimaryCreate(device);
-    if (hr == DS_OK)
-        device->timerID = DSOUND_create_timer(DSOUND_timer, (DWORD_PTR)device);
-    else
+    if (hr == DS_OK) {
+        device->thread = CreateThread(0, 0, DSOUND_mixthread, device, 0, 0);
+        SetThreadPriority(device->thread, THREAD_PRIORITY_TIME_CRITICAL);
+    } else
         WARN("DSOUND_PrimaryCreate failed: %08x\n", hr);
 
     *ppDevice = device;
diff --git a/dlls/dsound/dsound_private.h b/dlls/dsound/dsound_private.h
index 4c4e04e..feef787 100644
--- a/dlls/dsound/dsound_private.h
+++ b/dlls/dsound/dsound_private.h
@@ -71,9 +71,9 @@ struct DirectSoundDevice
 
     GUID                        guid;
     DSCAPS                      drvcaps;
-    DWORD                       priolevel;
+    DWORD                       priolevel, sleeptime;
     PWAVEFORMATEX               pwfx, primary_pwfx;
-    UINT                        timerID, playing_offs_bytes, in_mmdev_bytes, prebuf, helfrags;
+    UINT                        playing_offs_bytes, in_mmdev_bytes, prebuf, helfrags;
     DWORD                       fraglen;
     LPBYTE                      buffer;
     DWORD                       writelead, buflen, state, playpos, mixpos;
@@ -100,6 +100,7 @@ struct DirectSoundDevice
     IAudioStreamVolume *volume;
     IAudioRenderClient *render;
 
+    HANDLE sleepev, thread;
     struct list entry;
 };
 
@@ -229,7 +230,7 @@ void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
 DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, float *overshot) DECLSPEC_HIDDEN;
 
-void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) DECLSPEC_HIDDEN;
+DWORD CALLBACK DSOUND_mixthread(void *ptr) DECLSPEC_HIDDEN;
 
 /* sound3d.c */
 
diff --git a/dlls/dsound/mixer.c b/dlls/dsound/mixer.c
index 84e029b..f1cabbd 100644
--- a/dlls/dsound/mixer.c
+++ b/dlls/dsound/mixer.c
@@ -820,7 +820,7 @@ static void DSOUND_PerformMix(DirectSoundDevice *device)
 			DSOUND_PrimaryStop(device);
 		}
 
-	} else {
+	} else if (device->state != STATE_STOPPED) {
 
 		DSOUND_WaveQueue(device, TRUE);
 
@@ -843,22 +843,26 @@ static void DSOUND_PerformMix(DirectSoundDevice *device)
 	/* **** */
 }
 
-void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser,
-                           DWORD_PTR dw1, DWORD_PTR dw2)
+DWORD CALLBACK DSOUND_mixthread(void *p)
 {
-	DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
-	DWORD start_time =  GetTickCount();
-	DWORD end_time;
-	TRACE("(%d,%d,0x%lx,0x%lx,0x%lx)\n",timerID,msg,dwUser,dw1,dw2);
-	TRACE("entering at %d\n", start_time);
-
-	RtlAcquireResourceShared(&(device->buffer_list_lock), TRUE);
-
-	if (device->ref)
-		DSOUND_PerformMix(device);
-
-	RtlReleaseResource(&(device->buffer_list_lock));
-
-	end_time = GetTickCount();
-	TRACE("completed processing at %d, duration = %d\n", end_time, end_time - start_time);
+	DirectSoundDevice *dev = p;
+	TRACE("(%p)\n", dev);
+
+	while (dev->ref) {
+		DWORD ret;
+
+		/* ALSA is retarded, add a timeout.. */
+		ret = WaitForSingleObject(dev->sleepev, dev->sleeptime);
+		if (ret == WAIT_FAILED)
+			WARN("wait returned error %u %08x!\n", GetLastError(), GetLastError());
+		else if (ret)
+			WARN("wait returned %08x!\n", ret);
+		if (!dev->ref)
+			break;
+
+		RtlAcquireResourceShared(&(dev->buffer_list_lock), TRUE);
+		DSOUND_PerformMix(dev);
+		RtlReleaseResource(&(dev->buffer_list_lock));
+	}
+	return 0;
 }
diff --git a/dlls/dsound/primary.c b/dlls/dsound/primary.c
index 94bdf9c..9d9fa27 100644
--- a/dlls/dsound/primary.c
+++ b/dlls/dsound/primary.c
@@ -64,6 +64,8 @@ HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
     REFERENCE_TIME prebuf_rt;
     WAVEFORMATEXTENSIBLE *wfe2 = NULL;
     HRESULT hres;
+    REFERENCE_TIME period;
+    DWORD period_ms;
 
     TRACE("(%p, %d)\n", device, forcewave);
 
@@ -155,8 +157,8 @@ HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
     prebuf_rt = (10000000 * (UINT64)prebuf_frames) / device->pwfx->nSamplesPerSec;
 
     hres = IAudioClient_Initialize(device->client,
-            AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST,
-            prebuf_rt, 0, device->pwfx, NULL);
+            AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST |
+            AUDCLNT_STREAMFLAGS_EVENTCALLBACK, prebuf_rt, 0, device->pwfx, NULL);
     if(FAILED(hres)){
         IAudioClient_Release(device->client);
         device->client = NULL;
@@ -170,6 +172,7 @@ HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
             device->pwfx->wFormatTag = WAVE_FORMAT_PCM;
         device->pwfx->cbSize = 0;
     }
+    IAudioClient_SetEventHandle(device->client, device->sleepev);
 
     hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
             (void**)&device->render);
@@ -203,6 +206,19 @@ HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
         WARN("GetService failed: %08x\n", hres);
         return hres;
     }
+    /* Now kick off the timer so the event fires periodically */
+    IAudioClient_Start(device->client);
+
+    IAudioClient_GetStreamLatency(device->client, &period);
+    period_ms = (period + 9999) / 10000;
+    TRACE("period %u ms fraglen %u prebuf %u\n", period_ms, device->fraglen, device->prebuf);
+
+    if (period_ms <= 15)
+        device->sleeptime = period_ms * 5 / 2;
+    else
+        device->sleeptime = period_ms * 3 / 2;
+    if (device->sleeptime < 10)
+        device->sleeptime = 10;
 
     return S_OK;
 }
@@ -345,7 +361,7 @@ HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
     TRACE("(%p)\n", device);
 
     hr = IAudioClient_Start(device->client);
-    if(FAILED(hr)){
+    if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
         WARN("Start failed: %08x\n", hr);
         return hr;
     }
-- 
1.7.11.3




More information about the wine-patches mailing list