Sync with trunk revision 64099.
[reactos.git] / dll / win32 / mciavi32 / mciavi.c
1 /*
2 * Digital video MCI Wine Driver
3 *
4 * Copyright 1999, 2000 Eric POUECH
5 * Copyright 2003 Dmitry Timoshkov
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 /* TODO list :
23 * - handling of palettes
24 * - recording (which input devices ?), a cam recorder ?
25 * - lots of messages still need to be handled (cf FIXME)
26 * - synchronization between audio and video (especially for interleaved
27 * files)
28 * - robustness when reading file can be enhanced
29 * - reimplement the AVI handling part with avifile DLL because
30 * "open @1122334 type avivideo alias a" expects an AVIFile/Stream
31 * and MCI_DGV_SET|STATUS_SPEED maps to Rate/Scale
32 * - some files appear to have more than one audio stream (we only play the
33 * first one)
34 * - some files contain an index of audio/video frame. Better use it,
35 * instead of rebuilding it (AVIFile does that already)
36 * - stopping while playing a file with sound blocks until all buffered
37 * audio is played... still should be stopped ASAP
38 */
39
40 #include "private_mciavi.h"
41
42 #include <mciavi.h>
43 #include <wine/unicode.h>
44
45 static DWORD MCIAVI_mciStop(UINT, DWORD, LPMCI_GENERIC_PARMS);
46
47 /*======================================================================*
48 * MCI AVI implementation *
49 *======================================================================*/
50
51 HINSTANCE MCIAVI_hInstance = 0;
52
53 /***********************************************************************
54 * DllMain (MCIAVI.0)
55 */
56 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
57 {
58 switch (fdwReason) {
59 case DLL_PROCESS_ATTACH:
60 DisableThreadLibraryCalls(hInstDLL);
61 MCIAVI_hInstance = hInstDLL;
62 break;
63 }
64 return TRUE;
65 }
66
67 /**************************************************************************
68 * MCIAVI_drvOpen [internal]
69 */
70 static DWORD MCIAVI_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
71 {
72 WINE_MCIAVI* wma;
73 static const WCHAR mciAviWStr[] = {'M','C','I','A','V','I',0};
74
75 TRACE("%s, %p\n", debugstr_w(str), modp);
76
77 /* session instance */
78 if (!modp) return 0xFFFFFFFF;
79
80 if (!MCIAVI_RegisterClass()) return 0;
81
82 wma = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIAVI));
83 if (!wma)
84 return 0;
85
86 InitializeCriticalSection(&wma->cs);
87 wma->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINE_MCIAVI.cs");
88 wma->hStopEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
89 wma->wDevID = modp->wDeviceID;
90 wma->wCommandTable = mciLoadCommandResource(MCIAVI_hInstance, mciAviWStr, 0);
91 wma->dwStatus = MCI_MODE_NOT_READY;
92 modp->wCustomCommandTable = wma->wCommandTable;
93 modp->wType = MCI_DEVTYPE_DIGITAL_VIDEO;
94 mciSetDriverData(wma->wDevID, (DWORD_PTR)wma);
95
96 return modp->wDeviceID;
97 }
98
99 /**************************************************************************
100 * MCIAVI_drvClose [internal]
101 */
102 static DWORD MCIAVI_drvClose(DWORD dwDevID)
103 {
104 WINE_MCIAVI *wma;
105
106 TRACE("%04x\n", dwDevID);
107
108 /* finish all outstanding things */
109 MCIAVI_mciClose(dwDevID, MCI_WAIT, NULL);
110
111 wma = (WINE_MCIAVI*)mciGetDriverData(dwDevID);
112
113 if (wma) {
114 MCIAVI_UnregisterClass();
115
116 EnterCriticalSection(&wma->cs);
117
118 mciSetDriverData(dwDevID, 0);
119 mciFreeCommandResource(wma->wCommandTable);
120
121 CloseHandle(wma->hStopEvent);
122
123 LeaveCriticalSection(&wma->cs);
124 wma->cs.DebugInfo->Spare[0] = 0;
125 DeleteCriticalSection(&wma->cs);
126
127 HeapFree(GetProcessHeap(), 0, wma);
128 return 1;
129 }
130 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
131 }
132
133 /**************************************************************************
134 * MCIAVI_drvConfigure [internal]
135 */
136 static DWORD MCIAVI_drvConfigure(DWORD dwDevID)
137 {
138 WINE_MCIAVI *wma;
139
140 TRACE("%04x\n", dwDevID);
141
142 MCIAVI_mciStop(dwDevID, MCI_WAIT, NULL);
143
144 wma = (WINE_MCIAVI*)mciGetDriverData(dwDevID);
145
146 if (wma) {
147 MessageBoxA(0, "Sample AVI Wine Driver !", "MM-Wine Driver", MB_OK);
148 return 1;
149 }
150 return 0;
151 }
152
153 /**************************************************************************
154 * MCIAVI_mciGetOpenDev [internal]
155 */
156 WINE_MCIAVI* MCIAVI_mciGetOpenDev(UINT wDevID)
157 {
158 WINE_MCIAVI* wma = (WINE_MCIAVI*)mciGetDriverData(wDevID);
159
160 if (wma == NULL || wma->nUseCount == 0) {
161 WARN("Invalid wDevID=%u\n", wDevID);
162 return 0;
163 }
164 return wma;
165 }
166
167 static void MCIAVI_CleanUp(WINE_MCIAVI* wma)
168 {
169 /* to prevent handling in WindowProc */
170 wma->dwStatus = MCI_MODE_NOT_READY;
171 if (wma->hFile) {
172 mmioClose(wma->hFile, 0);
173 wma->hFile = 0;
174
175 HeapFree(GetProcessHeap(), 0, wma->lpFileName);
176 wma->lpFileName = NULL;
177
178 HeapFree(GetProcessHeap(), 0, wma->lpVideoIndex);
179 wma->lpVideoIndex = NULL;
180 HeapFree(GetProcessHeap(), 0, wma->lpAudioIndex);
181 wma->lpAudioIndex = NULL;
182 if (wma->hic) ICClose(wma->hic);
183 wma->hic = 0;
184 HeapFree(GetProcessHeap(), 0, wma->inbih);
185 wma->inbih = NULL;
186 HeapFree(GetProcessHeap(), 0, wma->outbih);
187 wma->outbih = NULL;
188 HeapFree(GetProcessHeap(), 0, wma->indata);
189 wma->indata = NULL;
190 HeapFree(GetProcessHeap(), 0, wma->outdata);
191 wma->outdata = NULL;
192 if (wma->hbmFrame) DeleteObject(wma->hbmFrame);
193 wma->hbmFrame = 0;
194 if (wma->hWnd) DestroyWindow(wma->hWnd);
195 wma->hWnd = 0;
196
197 HeapFree(GetProcessHeap(), 0, wma->lpWaveFormat);
198 wma->lpWaveFormat = 0;
199
200 memset(&wma->mah, 0, sizeof(wma->mah));
201 memset(&wma->ash_video, 0, sizeof(wma->ash_video));
202 memset(&wma->ash_audio, 0, sizeof(wma->ash_audio));
203 wma->dwCurrVideoFrame = wma->dwCurrAudioBlock = 0;
204 wma->dwCachedFrame = -1;
205 }
206 }
207
208 /***************************************************************************
209 * MCIAVI_mciOpen [internal]
210 */
211 static DWORD MCIAVI_mciOpen(UINT wDevID, DWORD dwFlags,
212 LPMCI_DGV_OPEN_PARMSW lpOpenParms)
213 {
214 WINE_MCIAVI *wma;
215 LRESULT dwRet = 0;
216
217 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpOpenParms);
218
219 if (lpOpenParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
220
221 wma = (WINE_MCIAVI *)mciGetDriverData(wDevID);
222 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
223
224 EnterCriticalSection(&wma->cs);
225
226 if (wma->nUseCount > 0) {
227 /* The driver is already open on this channel */
228 /* If the driver was opened shareable before and this open specifies */
229 /* shareable then increment the use count */
230 if (wma->fShareable && (dwFlags & MCI_OPEN_SHAREABLE))
231 ++wma->nUseCount;
232 else
233 {
234 LeaveCriticalSection(&wma->cs);
235 return MCIERR_MUST_USE_SHAREABLE;
236 }
237 } else {
238 wma->nUseCount = 1;
239 wma->fShareable = dwFlags & MCI_OPEN_SHAREABLE;
240 }
241
242 wma->dwStatus = MCI_MODE_NOT_READY;
243
244 if (dwFlags & MCI_OPEN_ELEMENT) {
245 if (dwFlags & MCI_OPEN_ELEMENT_ID) {
246 /* could it be that (DWORD)lpOpenParms->lpstrElementName
247 * contains the hFile value ?
248 */
249 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
250 } else if (lpOpenParms->lpstrElementName && lpOpenParms->lpstrElementName[0]) {
251 /* FIXME : what should be done id wma->hFile is already != 0, or the driver is playin' */
252 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpOpenParms->lpstrElementName));
253
254 wma->lpFileName = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpOpenParms->lpstrElementName) + 1) * sizeof(WCHAR));
255 strcpyW(wma->lpFileName, lpOpenParms->lpstrElementName);
256
257 if (lpOpenParms->lpstrElementName[0] == '@') {
258 /* The file name @11223344 encodes an AVIFile handle in decimal notation
259 * in Win3.1 and w2k/NT, but this feature is absent in win95 (KB140750).
260 * wma->hFile = LongToHandle(strtolW(lpOpenParms->lpstrElementName+1, NULL, 10)); */
261 FIXME("Using AVIFile/Stream %s NIY\n", debugstr_w(lpOpenParms->lpstrElementName));
262 }
263 wma->hFile = mmioOpenW(lpOpenParms->lpstrElementName, NULL,
264 MMIO_ALLOCBUF | MMIO_DENYWRITE | MMIO_READ);
265
266 if (wma->hFile == 0) {
267 WARN("can't find file=%s!\n", debugstr_w(lpOpenParms->lpstrElementName));
268 dwRet = MCIERR_FILE_NOT_FOUND;
269 } else {
270 if (!MCIAVI_GetInfo(wma))
271 dwRet = MCIERR_INVALID_FILE;
272 else if (!MCIAVI_OpenVideo(wma))
273 dwRet = MCIERR_CANNOT_LOAD_DRIVER;
274 else if (!MCIAVI_CreateWindow(wma, dwFlags, lpOpenParms))
275 dwRet = MCIERR_CREATEWINDOW;
276 }
277 } else {
278 FIXME("Don't record yet\n");
279 dwRet = MCIERR_UNSUPPORTED_FUNCTION;
280 }
281 }
282
283 if (dwRet == 0) {
284 TRACE("lpOpenParms->wDeviceID = %04x\n", lpOpenParms->wDeviceID);
285
286 wma->dwStatus = MCI_MODE_STOP;
287 wma->dwMciTimeFormat = MCI_FORMAT_FRAMES;
288 } else {
289 MCIAVI_CleanUp(wma);
290 }
291
292 LeaveCriticalSection(&wma->cs);
293
294 if (!dwRet && (dwFlags & MCI_NOTIFY)) {
295 mciDriverNotify(HWND_32(LOWORD(lpOpenParms->dwCallback)),
296 wDevID, MCI_NOTIFY_SUCCESSFUL);
297 }
298 return dwRet;
299 }
300
301 /***************************************************************************
302 * MCIAVI_mciClose [internal]
303 */
304 DWORD MCIAVI_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
305 {
306 WINE_MCIAVI *wma;
307 DWORD dwRet = 0;
308
309 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
310
311 wma = MCIAVI_mciGetOpenDev(wDevID);
312 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
313
314 MCIAVI_mciStop(wDevID, MCI_WAIT, NULL);
315
316 EnterCriticalSection(&wma->cs);
317
318 if (wma->nUseCount == 1) {
319 MCIAVI_CleanUp(wma);
320
321 if ((dwFlags & MCI_NOTIFY) && lpParms) {
322 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
323 wDevID,
324 MCI_NOTIFY_SUCCESSFUL);
325 }
326 LeaveCriticalSection(&wma->cs);
327 return dwRet;
328 }
329 wma->nUseCount--;
330
331 LeaveCriticalSection(&wma->cs);
332 return dwRet;
333 }
334
335 static double currenttime_us(void)
336 {
337 LARGE_INTEGER lc, lf;
338 QueryPerformanceCounter(&lc);
339 QueryPerformanceFrequency(&lf);
340 return (lc.QuadPart * 1000000) / lf.QuadPart;
341 }
342
343 /***************************************************************************
344 * MCIAVI_player [internal]
345 */
346 static DWORD MCIAVI_player(WINE_MCIAVI *wma, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
347 {
348 DWORD dwRet;
349 LPWAVEHDR waveHdr = NULL;
350 unsigned i, nHdr = 0;
351 DWORD numEvents = 1;
352 HANDLE events[2];
353 double next_frame_us;
354
355 EnterCriticalSection(&wma->cs);
356
357 if (wma->dwToVideoFrame <= wma->dwCurrVideoFrame)
358 {
359 dwRet = 0;
360 goto mci_play_done;
361 }
362
363 events[0] = wma->hStopEvent;
364 if (wma->lpWaveFormat) {
365 if (MCIAVI_OpenAudio(wma, &nHdr, &waveHdr) != 0)
366 {
367 /* can't play audio */
368 HeapFree(GetProcessHeap(), 0, wma->lpWaveFormat);
369 wma->lpWaveFormat = NULL;
370 }
371 else
372 {
373 /* fill the queue with as many wave headers as possible */
374 MCIAVI_PlayAudioBlocks(wma, nHdr, waveHdr);
375 events[1] = wma->hEvent;
376 numEvents = 2;
377 }
378 }
379
380 next_frame_us = currenttime_us();
381 while (wma->dwStatus == MCI_MODE_PLAY)
382 {
383 HDC hDC;
384 double tc, delta;
385 DWORD ret;
386
387 tc = currenttime_us();
388
389 hDC = wma->hWndPaint ? GetDC(wma->hWndPaint) : 0;
390 if (hDC)
391 {
392 while(next_frame_us <= tc && wma->dwCurrVideoFrame < wma->dwToVideoFrame){
393 double dur;
394 ++wma->dwCurrVideoFrame;
395 dur = MCIAVI_PaintFrame(wma, hDC);
396 if(!dur)
397 break;
398 next_frame_us += dur;
399 TRACE("next_frame: %f\n", next_frame_us);
400 }
401 ReleaseDC(wma->hWndPaint, hDC);
402 }
403 if(wma->dwCurrVideoFrame >= wma->dwToVideoFrame)
404 break;
405
406 if (wma->lpWaveFormat)
407 MCIAVI_PlayAudioBlocks(wma, nHdr, waveHdr);
408
409 tc = currenttime_us();
410 if(tc < next_frame_us)
411 delta = next_frame_us - tc;
412 else
413 delta = 0;
414
415 LeaveCriticalSection(&wma->cs);
416 ret = WaitForMultipleObjects(numEvents, events, FALSE, delta / 1000);
417 EnterCriticalSection(&wma->cs);
418 if (ret == WAIT_OBJECT_0 || wma->dwStatus != MCI_MODE_PLAY) break;
419 }
420
421 if (wma->lpWaveFormat) {
422 while (wma->dwEventCount != nHdr - 1)
423 {
424 LeaveCriticalSection(&wma->cs);
425 Sleep(100);
426 EnterCriticalSection(&wma->cs);
427 }
428
429 /* just to get rid of some race conditions between play, stop and pause */
430 LeaveCriticalSection(&wma->cs);
431 waveOutReset(wma->hWave);
432 EnterCriticalSection(&wma->cs);
433
434 for (i = 0; i < nHdr; i++)
435 waveOutUnprepareHeader(wma->hWave, &waveHdr[i], sizeof(WAVEHDR));
436 }
437
438 dwRet = 0;
439
440 if (wma->lpWaveFormat) {
441 HeapFree(GetProcessHeap(), 0, waveHdr);
442
443 if (wma->hWave) {
444 LeaveCriticalSection(&wma->cs);
445 waveOutClose(wma->hWave);
446 EnterCriticalSection(&wma->cs);
447 wma->hWave = 0;
448 }
449 CloseHandle(wma->hEvent);
450 }
451
452 mci_play_done:
453 wma->dwStatus = MCI_MODE_STOP;
454
455 if (dwFlags & MCI_NOTIFY) {
456 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
457 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
458 wma->wDevID, MCI_NOTIFY_SUCCESSFUL);
459 }
460 LeaveCriticalSection(&wma->cs);
461 return dwRet;
462 }
463
464 struct MCIAVI_play_data
465 {
466 WINE_MCIAVI *wma;
467 DWORD flags;
468 MCI_PLAY_PARMS params; /* FIXME: notify via wma->hCallback like the other MCI drivers */
469 };
470
471 /*
472 * MCIAVI_mciPlay_thread
473 *
474 * FIXME: probably should use a common worker thread created at the driver
475 * load time and queue all async commands to it.
476 */
477 static DWORD WINAPI MCIAVI_mciPlay_thread(LPVOID arg)
478 {
479 struct MCIAVI_play_data *data = (struct MCIAVI_play_data *)arg;
480 DWORD ret;
481
482 TRACE("In thread before async play command (id %u, flags %08x)\n", data->wma->wDevID, data->flags);
483 ret = MCIAVI_player(data->wma, data->flags, &data->params);
484 TRACE("In thread after async play command (id %u, flags %08x)\n", data->wma->wDevID, data->flags);
485
486 HeapFree(GetProcessHeap(), 0, data);
487 return ret;
488 }
489
490 /*
491 * MCIAVI_mciPlay_async
492 */
493 static DWORD MCIAVI_mciPlay_async(WINE_MCIAVI *wma, DWORD dwFlags, LPMCI_PLAY_PARMS lpParams)
494 {
495 HANDLE handle;
496 struct MCIAVI_play_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(struct MCIAVI_play_data));
497
498 if (!data) return MCIERR_OUT_OF_MEMORY;
499
500 data->wma = wma;
501 data->flags = dwFlags;
502 if (dwFlags & MCI_NOTIFY)
503 data->params.dwCallback = lpParams->dwCallback;
504
505 if (!(handle = CreateThread(NULL, 0, MCIAVI_mciPlay_thread, data, 0, NULL)))
506 {
507 WARN("Couldn't create thread for async play, playing synchronously\n");
508 return MCIAVI_mciPlay_thread(data);
509 }
510 SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
511 CloseHandle(handle);
512 return 0;
513 }
514
515 /***************************************************************************
516 * MCIAVI_mciPlay [internal]
517 */
518 static DWORD MCIAVI_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
519 {
520 WINE_MCIAVI *wma;
521 DWORD dwRet;
522 DWORD dwFromFrame, dwToFrame;
523
524 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
525
526 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
527
528 wma = MCIAVI_mciGetOpenDev(wDevID);
529 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
530 if (dwFlags & MCI_DGV_PLAY_REVERSE) return MCIERR_UNSUPPORTED_FUNCTION;
531 if (dwFlags & MCI_TEST) return 0;
532
533 if (dwFlags & (MCI_DGV_PLAY_REPEAT|MCI_MCIAVI_PLAY_WINDOW|MCI_MCIAVI_PLAY_FULLSCREEN|MCI_MCIAVI_PLAY_FULLBY2))
534 FIXME("Unsupported flag %08x\n", dwFlags);
535
536 EnterCriticalSection(&wma->cs);
537
538 if (!wma->hFile)
539 {
540 LeaveCriticalSection(&wma->cs);
541 return MCIERR_FILE_NOT_FOUND;
542 }
543 if (!wma->hWndPaint)
544 {
545 LeaveCriticalSection(&wma->cs);
546 return MCIERR_NO_WINDOW;
547 }
548
549 dwFromFrame = wma->dwCurrVideoFrame;
550 dwToFrame = wma->dwPlayableVideoFrames - 1;
551
552 if (dwFlags & MCI_FROM) {
553 dwFromFrame = MCIAVI_ConvertTimeFormatToFrame(wma, lpParms->dwFrom);
554 }
555 if (dwFlags & MCI_TO) {
556 dwToFrame = MCIAVI_ConvertTimeFormatToFrame(wma, lpParms->dwTo);
557 }
558 if (dwToFrame >= wma->dwPlayableVideoFrames)
559 dwToFrame = wma->dwPlayableVideoFrames - 1;
560
561 TRACE("Playing from frame=%u to frame=%u\n", dwFromFrame, dwToFrame);
562
563 wma->dwCurrVideoFrame = dwFromFrame;
564 wma->dwToVideoFrame = dwToFrame;
565
566 LeaveCriticalSection(&wma->cs);
567
568 if (!(GetWindowLongW(wma->hWndPaint, GWL_STYLE) & WS_VISIBLE))
569 ShowWindow(wma->hWndPaint, SW_SHOWNA);
570
571 EnterCriticalSection(&wma->cs);
572
573 /* if already playing exit */
574 if (wma->dwStatus == MCI_MODE_PLAY)
575 {
576 LeaveCriticalSection(&wma->cs);
577 return 0;
578 }
579
580 wma->dwStatus = MCI_MODE_PLAY;
581
582 LeaveCriticalSection(&wma->cs);
583
584 if (dwFlags & MCI_WAIT)
585 return MCIAVI_player(wma, dwFlags, lpParms);
586
587 dwRet = MCIAVI_mciPlay_async(wma, dwFlags, lpParms);
588
589 if (dwRet) {
590 EnterCriticalSection(&wma->cs);
591 wma->dwStatus = MCI_MODE_STOP;
592 LeaveCriticalSection(&wma->cs);
593 }
594 return dwRet;
595 }
596
597 /***************************************************************************
598 * MCIAVI_mciStop [internal]
599 */
600 static DWORD MCIAVI_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
601 {
602 WINE_MCIAVI *wma;
603 DWORD dwRet = 0;
604
605 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
606
607 wma = MCIAVI_mciGetOpenDev(wDevID);
608 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
609 if (dwFlags & MCI_TEST) return 0;
610
611 EnterCriticalSection(&wma->cs);
612
613 TRACE("current status %04x\n", wma->dwStatus);
614
615 switch (wma->dwStatus) {
616 case MCI_MODE_PLAY:
617 case MCI_MODE_RECORD:
618 LeaveCriticalSection(&wma->cs);
619 SetEvent(wma->hStopEvent);
620 EnterCriticalSection(&wma->cs);
621 /* fall through */
622 case MCI_MODE_PAUSE:
623 /* Since our wave notification callback takes the lock,
624 * we must release it before resetting the device */
625 LeaveCriticalSection(&wma->cs);
626 dwRet = waveOutReset(wma->hWave);
627 EnterCriticalSection(&wma->cs);
628 /* fall through */
629 default:
630 do /* one more chance for an async thread to finish */
631 {
632 LeaveCriticalSection(&wma->cs);
633 Sleep(10);
634 EnterCriticalSection(&wma->cs);
635 } while (wma->dwStatus != MCI_MODE_STOP);
636
637 break;
638
639 case MCI_MODE_NOT_READY:
640 break;
641 }
642
643 if ((dwFlags & MCI_NOTIFY) && lpParms) {
644 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
645 wDevID, MCI_NOTIFY_SUCCESSFUL);
646 }
647 LeaveCriticalSection(&wma->cs);
648 return dwRet;
649 }
650
651 /***************************************************************************
652 * MCIAVI_mciPause [internal]
653 */
654 static DWORD MCIAVI_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
655 {
656 WINE_MCIAVI *wma;
657
658 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
659
660 wma = MCIAVI_mciGetOpenDev(wDevID);
661 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
662 if (dwFlags & MCI_TEST) return 0;
663
664 EnterCriticalSection(&wma->cs);
665
666 if (wma->dwStatus == MCI_MODE_PLAY)
667 wma->dwStatus = MCI_MODE_PAUSE;
668
669 if (wma->lpWaveFormat) {
670 LeaveCriticalSection(&wma->cs);
671 return waveOutPause(wma->hWave);
672 }
673
674 LeaveCriticalSection(&wma->cs);
675 return 0;
676 }
677
678 /***************************************************************************
679 * MCIAVI_mciResume [internal]
680 */
681 static DWORD MCIAVI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
682 {
683 WINE_MCIAVI *wma;
684
685 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
686
687 wma = MCIAVI_mciGetOpenDev(wDevID);
688 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
689 if (dwFlags & MCI_TEST) return 0;
690
691 EnterCriticalSection(&wma->cs);
692
693 if (wma->dwStatus == MCI_MODE_PAUSE)
694 wma->dwStatus = MCI_MODE_PLAY;
695
696 if (wma->lpWaveFormat) {
697 LeaveCriticalSection(&wma->cs);
698 return waveOutRestart(wma->hWave);
699 }
700
701 LeaveCriticalSection(&wma->cs);
702 return 0;
703 }
704
705 /***************************************************************************
706 * MCIAVI_mciSeek [internal]
707 */
708 static DWORD MCIAVI_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
709 {
710 WINE_MCIAVI *wma;
711 DWORD position;
712
713 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
714
715 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
716
717 wma = MCIAVI_mciGetOpenDev(wDevID);
718 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
719
720 position = dwFlags & (MCI_SEEK_TO_START|MCI_SEEK_TO_END|MCI_TO);
721 if (!position) return MCIERR_MISSING_PARAMETER;
722 if (position&(position-1)) return MCIERR_FLAGS_NOT_COMPATIBLE;
723
724 if (dwFlags & MCI_TO) {
725 position = MCIAVI_ConvertTimeFormatToFrame(wma, lpParms->dwTo);
726 if (position >= wma->dwPlayableVideoFrames)
727 return MCIERR_OUTOFRANGE;
728 } else if (dwFlags & MCI_SEEK_TO_START) {
729 position = 0;
730 } else {
731 position = wma->dwPlayableVideoFrames - 1;
732 }
733 if (dwFlags & MCI_TEST) return 0;
734
735 MCIAVI_mciStop(wDevID, MCI_WAIT, NULL);
736
737 EnterCriticalSection(&wma->cs);
738
739 wma->dwCurrVideoFrame = position;
740 TRACE("Seeking to frame=%u\n", wma->dwCurrVideoFrame);
741
742 if (dwFlags & MCI_NOTIFY) {
743 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
744 wDevID, MCI_NOTIFY_SUCCESSFUL);
745 }
746 LeaveCriticalSection(&wma->cs);
747 return 0;
748 }
749
750 /*****************************************************************************
751 * MCIAVI_mciLoad [internal]
752 */
753 static DWORD MCIAVI_mciLoad(UINT wDevID, DWORD dwFlags, LPMCI_DGV_LOAD_PARMSW lpParms)
754 {
755 WINE_MCIAVI *wma;
756
757 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
758
759 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
760
761 wma = MCIAVI_mciGetOpenDev(wDevID);
762 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
763
764 return MCIERR_UNSUPPORTED_FUNCTION; /* like w2k */
765 }
766
767 /******************************************************************************
768 * MCIAVI_mciRealize [internal]
769 */
770 static DWORD MCIAVI_mciRealize(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
771 {
772 WINE_MCIAVI *wma;
773
774 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
775
776 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
777
778 wma = MCIAVI_mciGetOpenDev(wDevID);
779 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
780 if (dwFlags & MCI_TEST) return 0;
781
782 return 0;
783 }
784
785 /******************************************************************************
786 * MCIAVI_mciUpdate [internal]
787 */
788 static DWORD MCIAVI_mciUpdate(UINT wDevID, DWORD dwFlags, LPMCI_DGV_UPDATE_PARMS lpParms)
789 {
790 WINE_MCIAVI *wma;
791
792 TRACE("%04x, %08x, %p\n", wDevID, dwFlags, lpParms);
793
794 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
795
796 wma = MCIAVI_mciGetOpenDev(wDevID);
797 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
798 /* Ignore MCI_TEST flag. */
799
800 EnterCriticalSection(&wma->cs);
801
802 if (dwFlags & MCI_DGV_UPDATE_HDC)
803 MCIAVI_PaintFrame(wma, lpParms->hDC);
804
805 LeaveCriticalSection(&wma->cs);
806
807 return 0;
808 }
809
810 /******************************************************************************
811 * MCIAVI_mciStep [internal]
812 */
813 static DWORD MCIAVI_mciStep(UINT wDevID, DWORD dwFlags, LPMCI_DGV_STEP_PARMS lpParms)
814 {
815 WINE_MCIAVI *wma;
816 DWORD position;
817 int delta = 1;
818
819 TRACE("(%04x, %08x, %p)\n", wDevID, dwFlags, lpParms);
820
821 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
822
823 wma = MCIAVI_mciGetOpenDev(wDevID);
824 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
825
826 if (dwFlags & MCI_DGV_STEP_FRAMES) delta = lpParms->dwFrames;
827 if (dwFlags & MCI_DGV_STEP_REVERSE) delta = -delta;
828 position = wma->dwCurrVideoFrame + delta;
829 if (position >= wma->dwPlayableVideoFrames) return MCIERR_OUTOFRANGE;
830 if (dwFlags & MCI_TEST) return 0;
831
832 MCIAVI_mciStop(wDevID, MCI_WAIT, NULL);
833
834 EnterCriticalSection(&wma->cs);
835
836 wma->dwCurrVideoFrame = position;
837 TRACE("Stepping to frame=%u\n", wma->dwCurrVideoFrame);
838
839 if (dwFlags & MCI_NOTIFY) {
840 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
841 wDevID, MCI_NOTIFY_SUCCESSFUL);
842 }
843 LeaveCriticalSection(&wma->cs);
844 return 0;
845 }
846
847 /******************************************************************************
848 * MCIAVI_mciCue [internal]
849 */
850 static DWORD MCIAVI_mciCue(UINT wDevID, DWORD dwFlags, LPMCI_DGV_CUE_PARMS lpParms)
851 {
852 WINE_MCIAVI *wma;
853
854 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
855
856 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
857
858 wma = MCIAVI_mciGetOpenDev(wDevID);
859 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
860 if (dwFlags & MCI_DGV_CUE_INPUT) return MCIERR_UNSUPPORTED_FUNCTION;
861 if (dwFlags & MCI_TEST) return 0;
862
863 return 0;
864 }
865
866 /******************************************************************************
867 * MCIAVI_mciSetAudio [internal]
868 */
869 static DWORD MCIAVI_mciSetAudio(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SETAUDIO_PARMSW lpParms)
870 {
871 WINE_MCIAVI *wma;
872
873 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
874
875 FIXME("(%04x, %08x, %p) Item %04x: stub\n", wDevID, dwFlags, lpParms, dwFlags & MCI_DGV_SETAUDIO_ITEM ? lpParms->dwItem : 0);
876
877 wma = MCIAVI_mciGetOpenDev(wDevID);
878 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
879
880 return 0;
881 }
882
883 /******************************************************************************
884 * MCIAVI_mciSignal [internal]
885 */
886 static DWORD MCIAVI_mciSignal(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SIGNAL_PARMS lpParms)
887 {
888 WINE_MCIAVI *wma;
889
890 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
891
892 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
893
894 wma = MCIAVI_mciGetOpenDev(wDevID);
895 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
896
897 return 0;
898 }
899
900 /******************************************************************************
901 * MCIAVI_mciSetVideo [internal]
902 */
903 static DWORD MCIAVI_mciSetVideo(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SETVIDEO_PARMSW lpParms)
904 {
905 WINE_MCIAVI *wma;
906
907 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
908
909 FIXME("(%04x, %08x, %p) Item %04x: stub\n", wDevID, dwFlags, lpParms, dwFlags & MCI_DGV_SETVIDEO_ITEM ? lpParms->dwItem : 0);
910
911 wma = MCIAVI_mciGetOpenDev(wDevID);
912 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
913
914 return 0;
915 }
916
917 /******************************************************************************
918 * MCIAVI_mciConfigure [internal]
919 */
920 static DWORD MCIAVI_mciConfigure(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
921 {
922 WINE_MCIAVI *wma;
923
924 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
925
926 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
927
928 wma = MCIAVI_mciGetOpenDev(wDevID);
929 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
930 if (dwFlags & MCI_TEST) return 0;
931
932 return 0;
933 }
934
935 /*======================================================================*
936 * MCI AVI entry points *
937 *======================================================================*/
938
939 /**************************************************************************
940 * DriverProc (MCIAVI.@)
941 */
942 LRESULT CALLBACK MCIAVI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
943 LPARAM dwParam1, LPARAM dwParam2)
944 {
945 TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
946 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
947
948 switch (wMsg) {
949 case DRV_LOAD: return 1;
950 case DRV_FREE: return 1;
951 case DRV_OPEN: return MCIAVI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
952 case DRV_CLOSE: return MCIAVI_drvClose(dwDevID);
953 case DRV_ENABLE: return 1;
954 case DRV_DISABLE: return 1;
955 case DRV_QUERYCONFIGURE: return 1;
956 case DRV_CONFIGURE: return MCIAVI_drvConfigure(dwDevID);
957 case DRV_INSTALL: return DRVCNF_RESTART;
958 case DRV_REMOVE: return DRVCNF_RESTART;
959 }
960
961 /* session instance */
962 if (dwDevID == 0xFFFFFFFF) return 1;
963
964 switch (wMsg) {
965 case MCI_OPEN_DRIVER: return MCIAVI_mciOpen (dwDevID, dwParam1, (LPMCI_DGV_OPEN_PARMSW) dwParam2);
966 case MCI_CLOSE_DRIVER: return MCIAVI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
967 case MCI_PLAY: return MCIAVI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
968 case MCI_STOP: return MCIAVI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
969 case MCI_SET: return MCIAVI_mciSet (dwDevID, dwParam1, (LPMCI_DGV_SET_PARMS) dwParam2);
970 case MCI_PAUSE: return MCIAVI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
971 case MCI_RESUME: return MCIAVI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
972 case MCI_STATUS: return MCIAVI_mciStatus (dwDevID, dwParam1, (LPMCI_DGV_STATUS_PARMSW) dwParam2);
973 case MCI_GETDEVCAPS: return MCIAVI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS) dwParam2);
974 case MCI_INFO: return MCIAVI_mciInfo (dwDevID, dwParam1, (LPMCI_DGV_INFO_PARMSW) dwParam2);
975 case MCI_SEEK: return MCIAVI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
976 case MCI_PUT: return MCIAVI_mciPut (dwDevID, dwParam1, (LPMCI_DGV_PUT_PARMS) dwParam2);
977 case MCI_WINDOW: return MCIAVI_mciWindow (dwDevID, dwParam1, (LPMCI_DGV_WINDOW_PARMSW) dwParam2);
978 case MCI_LOAD: return MCIAVI_mciLoad (dwDevID, dwParam1, (LPMCI_DGV_LOAD_PARMSW) dwParam2);
979 case MCI_REALIZE: return MCIAVI_mciRealize (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
980 case MCI_UPDATE: return MCIAVI_mciUpdate (dwDevID, dwParam1, (LPMCI_DGV_UPDATE_PARMS) dwParam2);
981 case MCI_WHERE: return MCIAVI_mciWhere (dwDevID, dwParam1, (LPMCI_DGV_RECT_PARMS) dwParam2);
982 case MCI_STEP: return MCIAVI_mciStep (dwDevID, dwParam1, (LPMCI_DGV_STEP_PARMS) dwParam2);
983 case MCI_CUE: return MCIAVI_mciCue (dwDevID, dwParam1, (LPMCI_DGV_CUE_PARMS) dwParam2);
984 /* Digital Video specific */
985 case MCI_SETAUDIO: return MCIAVI_mciSetAudio (dwDevID, dwParam1, (LPMCI_DGV_SETAUDIO_PARMSW) dwParam2);
986 case MCI_SIGNAL: return MCIAVI_mciSignal (dwDevID, dwParam1, (LPMCI_DGV_SIGNAL_PARMS) dwParam2);
987 case MCI_SETVIDEO: return MCIAVI_mciSetVideo (dwDevID, dwParam1, (LPMCI_DGV_SETVIDEO_PARMSW) dwParam2);
988 case MCI_CONFIGURE: return MCIAVI_mciConfigure (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
989
990 /* no editing, recording, saving, locking without inputs */
991 case MCI_CAPTURE:
992 case MCI_COPY:
993 case MCI_CUT:
994 case MCI_DELETE:
995 case MCI_FREEZE:
996 case MCI_LIST:
997 case MCI_MONITOR:
998 case MCI_PASTE:
999 case MCI_QUALITY:
1000 case MCI_RECORD:
1001 case MCI_RESERVE:
1002 case MCI_RESTORE:
1003 case MCI_SAVE:
1004 case MCI_UNDO:
1005 case MCI_UNFREEZE:
1006 TRACE("Unsupported function [0x%x] flags=%08x\n", wMsg, (DWORD)dwParam1);
1007 return MCIERR_UNSUPPORTED_FUNCTION;
1008 case MCI_SPIN:
1009 case MCI_ESCAPE:
1010 WARN("Unsupported command [0x%x] %08x\n", wMsg, (DWORD)dwParam1);
1011 break;
1012 case MCI_OPEN:
1013 case MCI_CLOSE:
1014 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1015 break;
1016 default:
1017 TRACE("Sending msg [%u] to default driver proc\n", wMsg);
1018 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1019 }
1020 return MCIERR_UNRECOGNIZED_COMMAND;
1021 }