2 * PROJECT: ReactOS Sound System "MME Buddy" Library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: lib/drivers/sound/mmebuddy/wave/streaming.c
6 * PURPOSE: Wave streaming
8 * PROGRAMMERS: Andrew Greenwood (silverblade@reactos.org)
21 Check if there is streaming to be done, and if so, do it.
26 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance
)
29 MMDEVICE_TYPE DeviceType
;
30 PSOUND_DEVICE SoundDevice
;
31 PMMFUNCTION_TABLE FunctionTable
;
33 PWAVEHDR_EXTENSION HeaderExtension
;
35 Result
= GetSoundDeviceFromInstance(SoundDeviceInstance
, &SoundDevice
);
36 SND_ASSERT( MMSUCCESS(Result
) );
38 Result
= GetSoundDeviceType(SoundDevice
, &DeviceType
);
39 SND_ASSERT( MMSUCCESS(Result
) );
41 Result
= GetSoundDeviceFunctionTable(SoundDevice
, &FunctionTable
);
42 SND_ASSERT( MMSUCCESS(Result
) );
43 SND_ASSERT( FunctionTable
);
44 SND_ASSERT( FunctionTable
->CommitWaveBuffer
);
46 /* No point in doing anything if no resources available to use */
47 if ( SoundDeviceInstance
->OutstandingBuffers
>= SoundDeviceInstance
->BufferCount
)
49 SND_TRACE(L
"DoWaveStreaming: No available buffers to stream with - doing nothing\n");
53 /* Is there any work to do? */
54 Header
= SoundDeviceInstance
->HeadWaveHeader
;
58 SND_TRACE(L
"DoWaveStreaming: No work to do - doing nothing\n");
62 while ( ( SoundDeviceInstance
->OutstandingBuffers
< SoundDeviceInstance
->BufferCount
) &&
65 HeaderExtension
= (PWAVEHDR_EXTENSION
) Header
->reserved
;
66 SND_ASSERT( HeaderExtension
);
69 SND_ASSERT(Header
->dwFlags
& WHDR_PREPARED
);
70 SND_ASSERT(Header
->dwFlags
& WHDR_INQUEUE
);
72 /* Can never be *above* the length */
73 SND_ASSERT( HeaderExtension
->BytesCommitted
<= Header
->dwBufferLength
);
75 /* Is this header entirely committed? */
76 if ( HeaderExtension
->BytesCommitted
== Header
->dwBufferLength
)
79 /* Move on to the next header */
80 SND_ASSERT(Header
!= Header
->lpNext
);
81 Header
= Header
->lpNext
;
86 PSOUND_OVERLAPPED Overlap
;
88 DWORD BytesRemaining
, BytesToCommit
;
91 /* Where within the header buffer to stream from */
92 OffsetPtr
= Header
->lpData
+ HeaderExtension
->BytesCommitted
;
94 /* How much of this header has not been committed */
95 BytesRemaining
= Header
->dwBufferLength
- HeaderExtension
->BytesCommitted
;
97 /* We can commit anything up to the buffer size limit */
98 BytesToCommit
= BytesRemaining
> SoundDeviceInstance
->FrameSize
?
99 SoundDeviceInstance
->FrameSize
:
102 /* Should always have something to commit by this point */
103 SND_ASSERT( BytesToCommit
> 0 );
105 /* We need a new overlapped info structure for each buffer */
106 Overlap
= AllocateStruct(SOUND_OVERLAPPED
);
110 ZeroMemory(Overlap
, sizeof(SOUND_OVERLAPPED
));
111 Overlap
->SoundDeviceInstance
= SoundDeviceInstance
;
112 Overlap
->Header
= Header
;
114 /* Don't complete this header if it's part of a loop */
115 Overlap
->PerformCompletion
= TRUE
;
116 // ( SoundDeviceInstance->LoopsRemaining > 0 );
118 /* Adjust the commit-related counters */
119 HeaderExtension
->BytesCommitted
+= BytesToCommit
;
120 ++ SoundDeviceInstance
->OutstandingBuffers
;
122 OK
= MMSUCCESS(FunctionTable
->CommitWaveBuffer(SoundDeviceInstance
,
130 /* Clean-up and try again on the next iteration (is this OK?) */
131 SND_WARN(L
"FAILED\n");
134 HeaderExtension
->BytesCommitted
-= BytesToCommit
;
135 -- SoundDeviceInstance
->OutstandingBuffers
;
145 An APC called as a result of a call to CommitWaveHeaderToKernelDevice.
146 This will count up the number of bytes which have been dealt with,
147 and when the entire wave header has been dealt with, will call
148 CompleteWaveHeader to have the wave header returned to the client.
150 CommitWaveHeaderToKernelDevice
151 Sends portions of the buffer described by the wave header to a kernel
152 device. This must only be called from within the context of the sound
153 thread. The caller supplies either their own commit routine, or uses
154 WriteFileEx_Committer. The committer is called with portions of the
155 buffer specified in the wave header.
157 WriteFileEx_Committer
158 Commit buffers using the WriteFileEx API.
163 IN DWORD dwErrorCode
,
164 IN DWORD dwNumberOfBytesTransferred
,
165 IN LPOVERLAPPED lpOverlapped
)
167 MMDEVICE_TYPE DeviceType
;
168 PSOUND_DEVICE SoundDevice
;
169 PSOUND_DEVICE_INSTANCE SoundDeviceInstance
;
170 PSOUND_OVERLAPPED SoundOverlapped
= (PSOUND_OVERLAPPED
) lpOverlapped
;
172 PWAVEHDR_EXTENSION HdrExtension
;
176 WaveHdr
= (PWAVEHDR
) SoundOverlapped
->Header
;
177 SND_ASSERT( WaveHdr
);
179 SND_ASSERT( ERROR_SUCCESS
== dwErrorCode
);
181 HdrExtension
= (PWAVEHDR_EXTENSION
) WaveHdr
->reserved
;
182 SND_ASSERT( HdrExtension
);
184 SoundDeviceInstance
= SoundOverlapped
->SoundDeviceInstance
;
186 Result
= GetSoundDeviceFromInstance(SoundDeviceInstance
, &SoundDevice
);
187 SND_ASSERT( MMSUCCESS(Result
) );
189 Result
= GetSoundDeviceType(SoundDevice
, &DeviceType
);
190 SND_ASSERT( MMSUCCESS(Result
) );
195 /* We have an available buffer now */
196 -- SoundDeviceInstance
->OutstandingBuffers
;
198 /* Did we finish a WAVEHDR and aren't looping? */
199 if ( HdrExtension
->BytesCompleted
+ dwNumberOfBytesTransferred
>= WaveHdr
->dwBufferLength
&&
200 SoundOverlapped
->PerformCompletion
)
202 /* Wave buffer fully completed */
203 Bytes
= WaveHdr
->dwBufferLength
- HdrExtension
->BytesCompleted
;
205 HdrExtension
->BytesCompleted
+= Bytes
;
206 dwNumberOfBytesTransferred
-= Bytes
;
208 CompleteWaveHeader(SoundDeviceInstance
, WaveHdr
);
209 SND_TRACE(L
"%d/%d bytes of wavehdr completed\n", HdrExtension
->BytesCompleted
, WaveHdr
->dwBufferLength
);
213 /* Partially completed */
214 HdrExtension
->BytesCompleted
+= dwNumberOfBytesTransferred
;
215 SND_TRACE(L
"%d/%d bytes of wavehdr completed\n", HdrExtension
->BytesCompleted
, WaveHdr
->dwBufferLength
);
219 /* Move to next wave header */
220 WaveHdr
= WaveHdr
->lpNext
;
224 /* No following WaveHdr */
225 SND_ASSERT(dwNumberOfBytesTransferred
== 0);
229 HdrExtension
= (PWAVEHDR_EXTENSION
) WaveHdr
->reserved
;
230 SND_ASSERT( HdrExtension
);
233 }while(dwNumberOfBytesTransferred
);
235 //DoWaveStreaming(SoundDeviceInstance);
237 //CompleteWavePortion(SoundDeviceInstance, dwNumberOfBytesTransferred);
239 FreeMemory(lpOverlapped
);
243 WriteFileEx_Committer(
244 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance
,
247 IN PSOUND_OVERLAPPED Overlap
,
248 IN LPOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine
)
252 VALIDATE_MMSYS_PARAMETER( SoundDeviceInstance
);
253 VALIDATE_MMSYS_PARAMETER( OffsetPtr
);
254 VALIDATE_MMSYS_PARAMETER( Overlap
);
255 VALIDATE_MMSYS_PARAMETER( CompletionRoutine
);
257 GetSoundDeviceInstanceHandle(SoundDeviceInstance
, &Handle
);
259 if ( ! WriteFileEx(Handle
, OffsetPtr
, Length
, (LPOVERLAPPED
)Overlap
, CompletionRoutine
) )
264 return MMSYSERR_NOERROR
;
269 Stream control functions
270 (External/internal thread pairs)
272 TODO - Move elsewhere as these shouldn't be wave specific!
276 StopStreamingInSoundThread(
277 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance
,
280 MMDEVICE_TYPE DeviceType
;
281 PMMFUNCTION_TABLE FunctionTable
;
283 PSOUND_DEVICE SoundDevice
;
285 /* set state reset in progress */
286 SoundDeviceInstance
->ResetInProgress
= TRUE
;
288 /* Get sound device */
289 Result
= GetSoundDeviceFromInstance(SoundDeviceInstance
, &SoundDevice
);
290 SND_ASSERT( Result
== MMSYSERR_NOERROR
);
292 /* Obtain the function table */
293 Result
= GetSoundDeviceFunctionTable(SoundDevice
, &FunctionTable
);
294 SND_ASSERT( Result
== MMSYSERR_NOERROR
);
296 /* Obtain device instance type */
297 Result
= GetSoundDeviceType(SoundDevice
, &DeviceType
);
298 SND_ASSERT( Result
== MMSYSERR_NOERROR
);
300 /* Check if reset function is supported */
301 if (FunctionTable
->ResetStream
)
303 /* cancel all current audio buffers */
304 FunctionTable
->ResetStream(SoundDeviceInstance
, DeviceType
, TRUE
);
307 /* complete all current headers */
308 while( SoundDeviceInstance
->HeadWaveHeader
)
310 SND_TRACE(L
"StopStreamingInSoundThread: Completing Header %p\n", SoundDeviceInstance
->HeadWaveHeader
);
311 CompleteWaveHeader( SoundDeviceInstance
, SoundDeviceInstance
->HeadWaveHeader
);
314 /* there should be no oustanding buffers now */
315 SND_ASSERT(SoundDeviceInstance
->OutstandingBuffers
== 0);
317 while(SoundDeviceInstance
->OutstandingBuffers
)
319 SND_ERR("StopStreamingInSoundThread OutStandingBufferCount %lu\n", SoundDeviceInstance
->OutstandingBuffers
);
320 /* my hack of doom */
324 /* Check if reset function is supported */
325 if (FunctionTable
->ResetStream
)
327 /* finish the reset */
328 FunctionTable
->ResetStream(SoundDeviceInstance
, DeviceType
, FALSE
);
331 /* clear state reset in progress */
332 SoundDeviceInstance
->ResetInProgress
= FALSE
;
335 return MMSYSERR_NOERROR
;
340 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance
)
343 PSOUND_DEVICE SoundDevice
;
344 MMDEVICE_TYPE DeviceType
;
346 if ( ! IsValidSoundDeviceInstance(SoundDeviceInstance
) )
347 return MMSYSERR_INVALHANDLE
;
349 Result
= GetSoundDeviceFromInstance(SoundDeviceInstance
, &SoundDevice
);
350 if ( ! MMSUCCESS(Result
) )
351 return TranslateInternalMmResult(Result
);
353 Result
= GetSoundDeviceType(SoundDevice
, &DeviceType
);
354 if ( ! MMSUCCESS(Result
) )
355 return TranslateInternalMmResult(Result
);
357 if ( DeviceType
!= WAVE_OUT_DEVICE_TYPE
&& DeviceType
!= WAVE_IN_DEVICE_TYPE
)
358 return MMSYSERR_NOTSUPPORTED
;
360 return CallSoundThread(SoundDeviceInstance
,
361 StopStreamingInSoundThread
,