WaveHdr prepare/unprepare/submit now gets handled within the context of the
authorAndrew Greenwood <silverblade@reactos.org>
Sun, 15 Feb 2009 15:19:58 +0000 (15:19 +0000)
committerAndrew Greenwood <silverblade@reactos.org>
Sun, 15 Feb 2009 15:19:58 +0000 (15:19 +0000)
appropriate sound thread. This removes some responsibility of the sound
threading from the actual usermode sound component implementations. Minor
cleanup to CallSoundThread as we can deduce the thread handle from the
sound device instance.

svn path=/trunk/; revision=39608

reactos/include/reactos/libs/sound/mmebuddy.h
reactos/lib/drivers/sound/mmebuddy/deviceinstance.c
reactos/lib/drivers/sound/mmebuddy/functiontable.c
reactos/lib/drivers/sound/mmebuddy/thread.c
reactos/lib/drivers/sound/mmebuddy/wave/header.c

index 178ccb7..99c5894 100644 (file)
@@ -376,7 +376,7 @@ GetSoundDeviceType(
 MMRESULT
 SetSoundDeviceFunctionTable(
     IN  PSOUND_DEVICE SoundDevice,
-    IN  PMMFUNCTION_TABLE FunctionTable OPTIONAL);
+    IN  PMMFUNCTION_TABLE FunctionTable);
 
 MMRESULT
 GetSoundDeviceFunctionTable(
@@ -438,9 +438,8 @@ DestroySoundThread(
 
 MMRESULT
 CallSoundThread(
-    IN  PSOUND_THREAD Thread,
+    IN  PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
     IN  SOUND_THREAD_REQUEST_HANDLER RequestHandler,
-    IN  PSOUND_DEVICE_INSTANCE SoundDeviceInstance OPTIONAL,
     IN  PVOID Parameter OPTIONAL);
 
 
index e6d0029..64dcbff 100644 (file)
@@ -243,7 +243,7 @@ DestroySoundDeviceInstance(
     SND_ASSERT( FunctionTable->Close );
     if ( FunctionTable->Close == NULL )
     {
-        /* Bad practice, really! If you can open, why not close?! */
+        /* This indicates bad practice, really! If you can open, why not close?! */
         return MMSYSERR_NOTSUPPORTED;
     }
 
index bed72db..18d7d76 100644 (file)
 /*
     Attaches a function table to a sound device. Any NULL entries in this
     table are automatically set to point to a default routine to handle
-    the appropriate function. If NULL is passed as the function table itself,
-    the entire function table will use only the default routines.
+    the appropriate function.
 */
 MMRESULT
 SetSoundDeviceFunctionTable(
     IN  PSOUND_DEVICE SoundDevice,
-    IN  PMMFUNCTION_TABLE FunctionTable OPTIONAL)
+    IN  PMMFUNCTION_TABLE FunctionTable)
 {
     VALIDATE_MMSYS_PARAMETER( IsValidSoundDevice(SoundDevice) );
+    VALIDATE_MMSYS_PARAMETER( FunctionTable );
 
     /* Zero out the existing function table (if present) */
     ZeroMemory(&SoundDevice->FunctionTable, sizeof(MMFUNCTION_TABLE));
 
-    if ( FunctionTable )
-    {
-        /* Fill in the client-supplied functions */
-        CopyMemory(&SoundDevice->FunctionTable,
-                   FunctionTable,
-                   sizeof(MMFUNCTION_TABLE));
-    }
+    if ( FunctionTable )
+        return MMSYSERR_INVALPARAM;
+
+    /* Fill in the client-supplied functions */
+    CopyMemory(&SoundDevice->FunctionTable,
+                FunctionTable,
+                sizeof(MMFUNCTION_TABLE));
 
     return MMSYSERR_NOERROR;
 }
index cc8ae7e..273413f 100644 (file)
@@ -175,6 +175,7 @@ CreateSoundThread(
     /* Wake the thread up */
     if ( ResumeThread(NewThread->Handle) == -1 )
     {
+        SND_ERR(L"Failed to resume thread!\n");
         CloseHandle(NewThread->Handle);
         DestroySoundThreadEvents(NewThread->Events.Ready,
                                  NewThread->Events.Request,
@@ -199,14 +200,16 @@ DestroySoundThread(
 
 MMRESULT
 CallSoundThread(
-    IN  PSOUND_THREAD Thread,
+    IN  PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
     IN  SOUND_THREAD_REQUEST_HANDLER RequestHandler,
-    IN  PSOUND_DEVICE_INSTANCE SoundDeviceInstance OPTIONAL,
     IN  PVOID Parameter OPTIONAL)
 {
-    VALIDATE_MMSYS_PARAMETER( Thread );
+    VALIDATE_MMSYS_PARAMETER( SoundDeviceInstance );
     VALIDATE_MMSYS_PARAMETER( RequestHandler );
 
+    /* TODO: Don't call this directly? */
+    PSOUND_THREAD Thread = SoundDeviceInstance->Thread;
+
     SND_TRACE(L"Waiting for READY event\n");
     WaitForSingleObject(Thread->Events.Ready, INFINITE);
 
index dbaad8e..054e3c7 100644 (file)
 #include <ntddsnd.h>
 #include <mmebuddy.h>
 
+
+/*
+    This structure gets used locally within functions as a way to shuttle data
+    to the sound thread. It's safe to use locally since CallSoundThread will
+    not return until the operation has been carried out.
+*/
+
+typedef struct
+{
+    MMWAVEHEADER_FUNC Function;
+    PWAVEHDR Header;
+} THREADED_WAVEHEADER_PARAMETERS;
+
+
+/*
+    Helper routines to simplify the call to the sound thread for the header
+    functions.
+*/
+
+MMRESULT
+WaveHeaderOperationInSoundThread(
+    PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
+    IN  PVOID Parameter)
+{
+    THREADED_WAVEHEADER_PARAMETERS* Parameters = (THREADED_WAVEHEADER_PARAMETERS*) Parameter;
+    return Parameters->Function(SoundDeviceInstance, Parameters->Header);
+}
+
+MMRESULT
+WaveHeaderOperation(
+    MMWAVEHEADER_FUNC Function,
+    PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
+    PWAVEHDR Header)
+{
+    THREADED_WAVEHEADER_PARAMETERS Parameters;
+
+    Parameters.Function = Function;
+    Parameters.Header = Header;
+
+    return CallSoundThread(SoundDeviceInstance,
+                           WaveHeaderOperationInSoundThread,
+                           &Parameters);
+}
+
+
+/*
+    The following routines are basically handlers for:
+    - WODM_PREPARE
+    - WODM_UNPREPARE
+    - WODM_WRITE
+
+    All of these calls are ultimately dealt with in the context of the
+    appropriate sound thread, so the implementation should expect itself to
+    be running in this other thread when any of these operations take place.
+*/
+
 MMRESULT
 PrepareWaveHeader(
     IN  PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
@@ -39,7 +95,9 @@ PrepareWaveHeader(
     if ( ! FunctionTable->PrepareWaveHeader )
         return MMSYSERR_NOTSUPPORTED;
 
-    return FunctionTable->PrepareWaveHeader(SoundDeviceInstance, Header);
+    return WaveHeaderOperation(FunctionTable->PrepareWaveHeader,
+                               SoundDeviceInstance,
+                               Header);
 }
 
 MMRESULT
@@ -67,7 +125,9 @@ UnprepareWaveHeader(
     if ( ! FunctionTable->UnprepareWaveHeader )
         return MMSYSERR_NOTSUPPORTED;
 
-    return FunctionTable->UnprepareWaveHeader(SoundDeviceInstance, Header);
+    return WaveHeaderOperation(FunctionTable->UnprepareWaveHeader,
+                               SoundDeviceInstance,
+                               Header);
 }
 
 MMRESULT
@@ -95,6 +155,8 @@ SubmitWaveHeader(
     if ( ! FunctionTable->SubmitWaveHeader )
         return MMSYSERR_NOTSUPPORTED;
 
-    return FunctionTable->SubmitWaveHeader(SoundDeviceInstance, Header);
+    return WaveHeaderOperation(FunctionTable->SubmitWaveHeader,
+                               SoundDeviceInstance,
+                               Header);
 }