[CMAKE]
[reactos.git] / lib / drivers / sound / mmebuddy / wave / streaming.c
1 /*
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
5 *
6 * PURPOSE: Wave streaming
7 *
8 * PROGRAMMERS: Andrew Greenwood (silverblade@reactos.org)
9 */
10
11 #include <windows.h>
12 #include <mmsystem.h>
13 #include <mmddk.h>
14 #include <ntddsnd.h>
15 #include <mmebuddy.h>
16 #include <sndtypes.h>
17
18
19 /*
20 DoWaveStreaming
21 Check if there is streaming to be done, and if so, do it.
22 */
23
24 VOID
25 DoWaveStreaming(
26 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance)
27 {
28 MMRESULT Result;
29 MMDEVICE_TYPE DeviceType;
30 PSOUND_DEVICE SoundDevice;
31 PMMFUNCTION_TABLE FunctionTable;
32 PWAVEHDR Header;
33 PWAVEHDR_EXTENSION HeaderExtension;
34
35 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
36 SND_ASSERT( MMSUCCESS(Result) );
37
38 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
39 SND_ASSERT( MMSUCCESS(Result) );
40
41 Result = GetSoundDeviceFunctionTable(SoundDevice, &FunctionTable);
42 SND_ASSERT( MMSUCCESS(Result) );
43 SND_ASSERT( FunctionTable );
44 SND_ASSERT( FunctionTable->CommitWaveBuffer );
45
46 /* No point in doing anything if no resources available to use */
47 if ( SoundDeviceInstance->OutstandingBuffers >= SoundDeviceInstance->BufferCount )
48 {
49 SND_TRACE(L"DoWaveStreaming: No available buffers to stream with - doing nothing\n");
50 return;
51 }
52
53 /* Is there any work to do? */
54 Header = SoundDeviceInstance->HeadWaveHeader;
55
56 if ( ! Header )
57 {
58 SND_TRACE(L"DoWaveStreaming: No work to do - doing nothing\n");
59 return;
60 }
61
62 while ( ( SoundDeviceInstance->OutstandingBuffers < SoundDeviceInstance->BufferCount ) &&
63 ( Header ) && SoundDeviceInstance->ResetInProgress == FALSE)
64 {
65 HeaderExtension = (PWAVEHDR_EXTENSION) Header->reserved;
66 SND_ASSERT( HeaderExtension );
67
68 /* Saniy checks */
69 SND_ASSERT(Header->dwFlags & WHDR_PREPARED);
70 SND_ASSERT(Header->dwFlags & WHDR_INQUEUE);
71
72 /* Can never be *above* the length */
73 SND_ASSERT( HeaderExtension->BytesCommitted <= Header->dwBufferLength );
74
75 /* Is this header entirely committed? */
76 if ( HeaderExtension->BytesCommitted == Header->dwBufferLength )
77 {
78 {
79 /* Move on to the next header */
80 SND_ASSERT(Header != Header->lpNext);
81 Header = Header->lpNext;
82 }
83 }
84 else
85 {
86 PSOUND_OVERLAPPED Overlap;
87 LPVOID OffsetPtr;
88 DWORD BytesRemaining, BytesToCommit;
89 BOOL OK;
90
91 /* Where within the header buffer to stream from */
92 OffsetPtr = Header->lpData + HeaderExtension->BytesCommitted;
93
94 /* How much of this header has not been committed */
95 BytesRemaining = Header->dwBufferLength - HeaderExtension->BytesCommitted;
96
97 /* We can commit anything up to the buffer size limit */
98 BytesToCommit = BytesRemaining > SoundDeviceInstance->FrameSize ?
99 SoundDeviceInstance->FrameSize :
100 BytesRemaining;
101
102 /* Should always have something to commit by this point */
103 SND_ASSERT( BytesToCommit > 0 );
104
105 /* We need a new overlapped info structure for each buffer */
106 Overlap = AllocateStruct(SOUND_OVERLAPPED);
107
108 if ( Overlap )
109 {
110 ZeroMemory(Overlap, sizeof(SOUND_OVERLAPPED));
111 Overlap->SoundDeviceInstance = SoundDeviceInstance;
112 Overlap->Header = Header;
113
114 /* Don't complete this header if it's part of a loop */
115 Overlap->PerformCompletion = TRUE;
116 // ( SoundDeviceInstance->LoopsRemaining > 0 );
117
118 /* Adjust the commit-related counters */
119 HeaderExtension->BytesCommitted += BytesToCommit;
120 ++ SoundDeviceInstance->OutstandingBuffers;
121
122 OK = MMSUCCESS(FunctionTable->CommitWaveBuffer(SoundDeviceInstance,
123 OffsetPtr,
124 BytesToCommit,
125 Overlap,
126 CompleteIO));
127
128 if ( ! OK )
129 {
130 /* Clean-up and try again on the next iteration (is this OK?) */
131 SND_WARN(L"FAILED\n");
132
133 FreeMemory(Overlap);
134 HeaderExtension->BytesCommitted -= BytesToCommit;
135 -- SoundDeviceInstance->OutstandingBuffers;
136 }
137 }
138 }
139 }
140 }
141
142
143 /*
144 CompleteIO
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.
149
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.
156
157 WriteFileEx_Committer
158 Commit buffers using the WriteFileEx API.
159 */
160
161 VOID CALLBACK
162 CompleteIO(
163 IN DWORD dwErrorCode,
164 IN DWORD dwNumberOfBytesTransferred,
165 IN LPOVERLAPPED lpOverlapped)
166 {
167 MMDEVICE_TYPE DeviceType;
168 PSOUND_DEVICE SoundDevice;
169 PSOUND_DEVICE_INSTANCE SoundDeviceInstance;
170 PSOUND_OVERLAPPED SoundOverlapped = (PSOUND_OVERLAPPED) lpOverlapped;
171 PWAVEHDR WaveHdr;
172 PWAVEHDR_EXTENSION HdrExtension;
173 MMRESULT Result;
174 DWORD Bytes;
175
176 WaveHdr = (PWAVEHDR) SoundOverlapped->Header;
177 SND_ASSERT( WaveHdr );
178
179 HdrExtension = (PWAVEHDR_EXTENSION) WaveHdr->reserved;
180 SND_ASSERT( HdrExtension );
181
182 SoundDeviceInstance = SoundOverlapped->SoundDeviceInstance;
183
184 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
185 SND_ASSERT( MMSUCCESS(Result) );
186
187 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
188 SND_ASSERT( MMSUCCESS(Result) );
189
190 do
191 {
192
193 /* We have an available buffer now */
194 -- SoundDeviceInstance->OutstandingBuffers;
195
196 /* Did we finish a WAVEHDR and aren't looping? */
197 if ( HdrExtension->BytesCompleted + dwNumberOfBytesTransferred >= WaveHdr->dwBufferLength &&
198 SoundOverlapped->PerformCompletion )
199 {
200 /* Wave buffer fully completed */
201 Bytes = WaveHdr->dwBufferLength - HdrExtension->BytesCompleted;
202
203 HdrExtension->BytesCompleted += Bytes;
204 dwNumberOfBytesTransferred -= Bytes;
205
206 CompleteWaveHeader(SoundDeviceInstance, WaveHdr);
207 SND_TRACE(L"%d/%d bytes of wavehdr completed\n", HdrExtension->BytesCompleted, WaveHdr->dwBufferLength);
208 }
209 else
210 {
211 /* Partially completed */
212 HdrExtension->BytesCompleted += dwNumberOfBytesTransferred;
213 SND_TRACE(L"%d/%d bytes of wavehdr completed\n", HdrExtension->BytesCompleted, WaveHdr->dwBufferLength);
214 break;
215 }
216
217 /* Move to next wave header */
218 WaveHdr = WaveHdr->lpNext;
219
220 if (!WaveHdr)
221 {
222 /* No following WaveHdr */
223 SND_ASSERT(dwNumberOfBytesTransferred == 0);
224 break;
225 }
226
227 HdrExtension = (PWAVEHDR_EXTENSION) WaveHdr->reserved;
228 SND_ASSERT( HdrExtension );
229
230
231 }while(dwNumberOfBytesTransferred);
232
233 // AUDIO-BRANCH DIFF
234 // completion callback is performed in a thread
235 DoWaveStreaming(SoundDeviceInstance);
236
237 //CompleteWavePortion(SoundDeviceInstance, dwNumberOfBytesTransferred);
238
239 FreeMemory(lpOverlapped);
240 }
241
242 MMRESULT
243 WriteFileEx_Committer(
244 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
245 IN PVOID OffsetPtr,
246 IN DWORD Length,
247 IN PSOUND_OVERLAPPED Overlap,
248 IN LPOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine)
249 {
250 HANDLE Handle;
251
252 VALIDATE_MMSYS_PARAMETER( SoundDeviceInstance );
253 VALIDATE_MMSYS_PARAMETER( OffsetPtr );
254 VALIDATE_MMSYS_PARAMETER( Overlap );
255 VALIDATE_MMSYS_PARAMETER( CompletionRoutine );
256
257 GetSoundDeviceInstanceHandle(SoundDeviceInstance, &Handle);
258
259 if ( ! WriteFileEx(Handle, OffsetPtr, Length, (LPOVERLAPPED)Overlap, CompletionRoutine) )
260 {
261 // TODO
262 }
263
264 return MMSYSERR_NOERROR;
265 }
266
267
268 /*
269 Stream control functions
270 (External/internal thread pairs)
271
272 TODO - Move elsewhere as these shouldn't be wave specific!
273 */
274
275 MMRESULT
276 StopStreamingInSoundThread(
277 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
278 IN PVOID Parameter)
279 {
280 MMDEVICE_TYPE DeviceType;
281 PMMFUNCTION_TABLE FunctionTable;
282 MMRESULT Result;
283 PSOUND_DEVICE SoundDevice;
284
285 /* set state reset in progress */
286 SoundDeviceInstance->ResetInProgress = TRUE;
287
288 /* Get sound device */
289 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
290 SND_ASSERT( Result == MMSYSERR_NOERROR );
291
292 /* Obtain the function table */
293 Result = GetSoundDeviceFunctionTable(SoundDevice, &FunctionTable);
294 SND_ASSERT( Result == MMSYSERR_NOERROR );
295
296 /* Obtain device instance type */
297 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
298 SND_ASSERT( Result == MMSYSERR_NOERROR );
299
300 /* Check if reset function is supported */
301 if (FunctionTable->ResetStream)
302 {
303 /* cancel all current audio buffers */
304 FunctionTable->ResetStream(SoundDeviceInstance, DeviceType, TRUE);
305 }
306 while(SoundDeviceInstance->OutstandingBuffers)
307 {
308 SND_TRACE(L"StopStreamingInSoundThread OutStandingBufferCount %lu\n", SoundDeviceInstance->OutstandingBuffers);
309 /* wait until pending i/o has completed */
310 SleepEx(10, TRUE);
311 }
312
313 /* complete all current headers */
314 while( SoundDeviceInstance->HeadWaveHeader )
315 {
316 SND_TRACE(L"StopStreamingInSoundThread: Completing Header %p\n", SoundDeviceInstance->HeadWaveHeader);
317 CompleteWaveHeader( SoundDeviceInstance, SoundDeviceInstance->HeadWaveHeader );
318 }
319
320 /* there should be no oustanding buffers now */
321 SND_ASSERT(SoundDeviceInstance->OutstandingBuffers == 0);
322
323
324 /* Check if reset function is supported */
325 if (FunctionTable->ResetStream)
326 {
327 /* finish the reset */
328 FunctionTable->ResetStream(SoundDeviceInstance, DeviceType, FALSE);
329 }
330
331 /* clear state reset in progress */
332 SoundDeviceInstance->ResetInProgress = FALSE;
333
334
335 return MMSYSERR_NOERROR;
336 }
337
338 MMRESULT
339 StopStreaming(
340 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance)
341 {
342 MMRESULT Result;
343 PSOUND_DEVICE SoundDevice;
344 MMDEVICE_TYPE DeviceType;
345
346 if ( ! IsValidSoundDeviceInstance(SoundDeviceInstance) )
347 return MMSYSERR_INVALHANDLE;
348
349 Result = GetSoundDeviceFromInstance(SoundDeviceInstance, &SoundDevice);
350 if ( ! MMSUCCESS(Result) )
351 return TranslateInternalMmResult(Result);
352
353 Result = GetSoundDeviceType(SoundDevice, &DeviceType);
354 if ( ! MMSUCCESS(Result) )
355 return TranslateInternalMmResult(Result);
356
357 if ( DeviceType != WAVE_OUT_DEVICE_TYPE && DeviceType != WAVE_IN_DEVICE_TYPE )
358 return MMSYSERR_NOTSUPPORTED;
359
360 return CallSoundThread(SoundDeviceInstance,
361 StopStreamingInSoundThread,
362 NULL);
363 }
364
365 MMRESULT
366 PerformWaveStreaming(
367 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance,
368 IN PVOID Parameter)
369 {
370 DoWaveStreaming(SoundDeviceInstance);
371
372 return MMSYSERR_NOERROR;
373 }
374
375 DWORD
376 WINAPI
377 WaveActivateSoundStreaming(
378 IN PVOID lpParameter)
379 {
380 CallSoundThread((PSOUND_DEVICE_INSTANCE)lpParameter,
381 PerformWaveStreaming,
382 NULL);
383
384 ExitThread(0);
385 }
386
387 VOID
388 InitiateSoundStreaming(
389 IN PSOUND_DEVICE_INSTANCE SoundDeviceInstance)
390 {
391 HANDLE hThread;
392
393 hThread = CreateThread(NULL, 0, WaveActivateSoundStreaming, (PVOID)SoundDeviceInstance, 0, NULL);
394
395 if (hThread != NULL)
396 CloseHandle(hThread);
397 }