include: Make sure __int64 is correctly defined on PPC64.
[wine.git] / dlls / mciavi32 / mciavi.c
blob7a1657f79eb3095dd05940e4ef12086f375e7582
1 /*
2 * Digital video MCI Wine Driver
4 * Copyright 1999, 2000 Eric POUECH
5 * Copyright 2003 Dmitry Timoshkov
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.
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.
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
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
40 #include <string.h>
41 #include "private_mciavi.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(mciavi);
46 static DWORD MCIAVI_mciStop(UINT, DWORD, LPMCI_GENERIC_PARMS);
48 /*======================================================================*
49 * MCI AVI implementation *
50 *======================================================================*/
52 HINSTANCE MCIAVI_hInstance = 0;
54 /***********************************************************************
55 * DllMain (MCIAVI.0)
57 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
59 switch (fdwReason) {
60 case DLL_PROCESS_ATTACH:
61 DisableThreadLibraryCalls(hInstDLL);
62 MCIAVI_hInstance = hInstDLL;
63 break;
65 return TRUE;
68 /**************************************************************************
69 * MCIAVI_drvOpen [internal]
71 static DWORD MCIAVI_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
73 WINE_MCIAVI* wma;
75 TRACE("%s, %p\n", debugstr_w(str), modp);
77 /* session instance */
78 if (!modp) return 0xFFFFFFFF;
80 if (!MCIAVI_RegisterClass()) return 0;
82 wma = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIAVI));
83 if (!wma)
84 return 0;
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, L"MCIAVI", 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);
96 return modp->wDeviceID;
99 /**************************************************************************
100 * MCIAVI_drvClose [internal]
102 static DWORD MCIAVI_drvClose(DWORD dwDevID)
104 WINE_MCIAVI *wma;
106 TRACE("%04x\n", dwDevID);
108 /* finish all outstanding things */
109 MCIAVI_mciClose(dwDevID, MCI_WAIT, NULL);
111 wma = (WINE_MCIAVI*)mciGetDriverData(dwDevID);
113 if (wma) {
114 MCIAVI_UnregisterClass();
116 EnterCriticalSection(&wma->cs);
118 mciSetDriverData(dwDevID, 0);
119 mciFreeCommandResource(wma->wCommandTable);
121 CloseHandle(wma->hStopEvent);
123 LeaveCriticalSection(&wma->cs);
124 wma->cs.DebugInfo->Spare[0] = 0;
125 DeleteCriticalSection(&wma->cs);
127 HeapFree(GetProcessHeap(), 0, wma);
128 return 1;
130 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
133 /**************************************************************************
134 * MCIAVI_drvConfigure [internal]
136 static DWORD MCIAVI_drvConfigure(DWORD dwDevID)
138 WINE_MCIAVI *wma;
140 TRACE("%04x\n", dwDevID);
142 MCIAVI_mciStop(dwDevID, MCI_WAIT, NULL);
144 wma = (WINE_MCIAVI*)mciGetDriverData(dwDevID);
146 if (wma) {
147 MessageBoxA(0, "Sample AVI Wine Driver !", "MM-Wine Driver", MB_OK);
148 return 1;
150 return 0;
153 /**************************************************************************
154 * MCIAVI_mciGetOpenDev [internal]
156 WINE_MCIAVI* MCIAVI_mciGetOpenDev(UINT wDevID)
158 WINE_MCIAVI* wma = (WINE_MCIAVI*)mciGetDriverData(wDevID);
160 if (wma == NULL || wma->nUseCount == 0) {
161 WARN("Invalid wDevID=%u\n", wDevID);
162 return 0;
164 return wma;
167 static void MCIAVI_CleanUp(WINE_MCIAVI* wma)
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;
175 HeapFree(GetProcessHeap(), 0, wma->lpFileName);
176 wma->lpFileName = NULL;
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;
197 HeapFree(GetProcessHeap(), 0, wma->lpWaveFormat);
198 wma->lpWaveFormat = 0;
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;
208 /***************************************************************************
209 * MCIAVI_mciOpen [internal]
211 static DWORD MCIAVI_mciOpen(UINT wDevID, DWORD dwFlags,
212 LPMCI_DGV_OPEN_PARMSW lpOpenParms)
214 WINE_MCIAVI *wma;
215 LRESULT dwRet = 0;
217 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpOpenParms);
219 if (lpOpenParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
221 wma = (WINE_MCIAVI *)mciGetDriverData(wDevID);
222 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
224 EnterCriticalSection(&wma->cs);
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
234 LeaveCriticalSection(&wma->cs);
235 return MCIERR_MUST_USE_SHAREABLE;
237 } else {
238 wma->nUseCount = 1;
239 wma->fShareable = dwFlags & MCI_OPEN_SHAREABLE;
242 wma->dwStatus = MCI_MODE_NOT_READY;
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 ?
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));
254 wma->lpFileName = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(lpOpenParms->lpstrElementName) + 1) * sizeof(WCHAR));
255 lstrcpyW(wma->lpFileName, lpOpenParms->lpstrElementName);
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(wcstol(lpOpenParms->lpstrElementName+1, NULL, 10)); */
261 FIXME("Using AVIFile/Stream %s NIY\n", debugstr_w(lpOpenParms->lpstrElementName));
263 wma->hFile = mmioOpenW(lpOpenParms->lpstrElementName, NULL,
264 MMIO_ALLOCBUF | MMIO_DENYWRITE | MMIO_READ);
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;
277 } else {
278 FIXME("Don't record yet\n");
279 dwRet = MCIERR_UNSUPPORTED_FUNCTION;
283 if (dwRet == 0) {
284 TRACE("lpOpenParms->wDeviceID = %04x\n", lpOpenParms->wDeviceID);
286 wma->dwStatus = MCI_MODE_STOP;
287 wma->dwMciTimeFormat = MCI_FORMAT_FRAMES;
288 } else {
289 MCIAVI_CleanUp(wma);
292 LeaveCriticalSection(&wma->cs);
294 if (!dwRet && (dwFlags & MCI_NOTIFY)) {
295 mciDriverNotify(HWND_32(LOWORD(lpOpenParms->dwCallback)),
296 wDevID, MCI_NOTIFY_SUCCESSFUL);
298 return dwRet;
301 /***************************************************************************
302 * MCIAVI_mciClose [internal]
304 DWORD MCIAVI_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
306 WINE_MCIAVI *wma;
307 DWORD dwRet = 0;
309 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
311 wma = MCIAVI_mciGetOpenDev(wDevID);
312 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
314 MCIAVI_mciStop(wDevID, MCI_WAIT, NULL);
316 EnterCriticalSection(&wma->cs);
318 if (wma->nUseCount == 1) {
319 MCIAVI_CleanUp(wma);
321 if ((dwFlags & MCI_NOTIFY) && lpParms) {
322 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
323 wDevID,
324 MCI_NOTIFY_SUCCESSFUL);
326 LeaveCriticalSection(&wma->cs);
327 return dwRet;
329 wma->nUseCount--;
331 LeaveCriticalSection(&wma->cs);
332 return dwRet;
335 static double currenttime_us(void)
337 LARGE_INTEGER lc, lf;
338 QueryPerformanceCounter(&lc);
339 QueryPerformanceFrequency(&lf);
340 return (lc.QuadPart * 1000000) / lf.QuadPart;
343 /***************************************************************************
344 * MCIAVI_player [internal]
346 static DWORD MCIAVI_player(WINE_MCIAVI *wma, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
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 BOOL wait_audio = TRUE;
356 EnterCriticalSection(&wma->cs);
358 if (wma->dwToVideoFrame <= wma->dwCurrVideoFrame)
360 dwRet = 0;
361 goto mci_play_done;
364 events[0] = wma->hStopEvent;
365 if (wma->lpWaveFormat) {
366 if (MCIAVI_OpenAudio(wma, &nHdr, &waveHdr) != 0)
368 /* can't play audio */
369 HeapFree(GetProcessHeap(), 0, wma->lpWaveFormat);
370 wma->lpWaveFormat = NULL;
372 else
374 /* fill the queue with as many wave headers as possible */
375 MCIAVI_PlayAudioBlocks(wma, nHdr, waveHdr);
376 events[1] = wma->hEvent;
377 numEvents = 2;
381 next_frame_us = currenttime_us();
382 while (wma->dwStatus == MCI_MODE_PLAY)
384 HDC hDC;
385 double tc, delta;
386 DWORD ret;
388 tc = currenttime_us();
390 hDC = wma->hWndPaint ? GetDC(wma->hWndPaint) : 0;
391 if (hDC)
393 while(next_frame_us <= tc && wma->dwCurrVideoFrame < wma->dwToVideoFrame){
394 double dur;
395 dur = MCIAVI_PaintFrame(wma, hDC);
396 ++wma->dwCurrVideoFrame;
397 if(!dur)
398 break;
399 next_frame_us += dur;
400 TRACE("next_frame: %f\n", next_frame_us);
402 ReleaseDC(wma->hWndPaint, hDC);
404 if (wma->dwCurrVideoFrame >= wma->dwToVideoFrame)
406 if (!(dwFlags & MCI_DGV_PLAY_REPEAT))
407 break;
408 TRACE("repeat media as requested\n");
409 wma->dwCurrVideoFrame = wma->dwCurrAudioBlock = 0;
412 if (wma->lpWaveFormat)
413 MCIAVI_PlayAudioBlocks(wma, nHdr, waveHdr);
415 tc = currenttime_us();
416 if (tc < next_frame_us)
417 delta = next_frame_us - tc;
418 else
419 delta = 0;
421 /* check if the playback was cancelled */
422 if ((wma->mci_break.flags & MCI_BREAK_KEY) &&
423 (GetAsyncKeyState(wma->mci_break.parms.nVirtKey) & 0x8000))
425 if (!(wma->mci_break.flags & MCI_BREAK_HWND) ||
426 GetForegroundWindow() == wma->mci_break.parms.hwndBreak)
428 /* we queue audio blocks ahead so ignore them otherwise the audio
429 * will keep playing until the buffer is empty */
430 wait_audio = FALSE;
432 TRACE("playback cancelled using break key\n");
433 break;
437 LeaveCriticalSection(&wma->cs);
438 ret = WaitForMultipleObjects(numEvents, events, FALSE, delta / 1000);
439 EnterCriticalSection(&wma->cs);
440 if (ret == WAIT_OBJECT_0 || wma->dwStatus != MCI_MODE_PLAY) break;
443 if (wma->lpWaveFormat)
445 if (wait_audio)
446 while (wma->dwEventCount != nHdr - 1)
448 LeaveCriticalSection(&wma->cs);
449 Sleep(100);
450 EnterCriticalSection(&wma->cs);
453 /* just to get rid of some race conditions between play, stop and pause */
454 LeaveCriticalSection(&wma->cs);
455 waveOutReset(wma->hWave);
456 EnterCriticalSection(&wma->cs);
458 for (i = 0; i < nHdr; i++)
459 waveOutUnprepareHeader(wma->hWave, &waveHdr[i], sizeof(WAVEHDR));
462 dwRet = 0;
464 if (wma->lpWaveFormat) {
465 HeapFree(GetProcessHeap(), 0, waveHdr);
467 if (wma->hWave) {
468 LeaveCriticalSection(&wma->cs);
469 waveOutClose(wma->hWave);
470 EnterCriticalSection(&wma->cs);
471 wma->hWave = 0;
473 CloseHandle(wma->hEvent);
476 mci_play_done:
477 wma->dwStatus = MCI_MODE_STOP;
479 if (dwFlags & MCI_NOTIFY) {
480 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
481 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
482 wma->wDevID, MCI_NOTIFY_SUCCESSFUL);
484 LeaveCriticalSection(&wma->cs);
485 return dwRet;
488 struct MCIAVI_play_data
490 WINE_MCIAVI *wma;
491 DWORD flags;
492 MCI_PLAY_PARMS params; /* FIXME: notify via wma->hCallback like the other MCI drivers */
496 * MCIAVI_mciPlay_thread
498 * FIXME: probably should use a common worker thread created at the driver
499 * load time and queue all async commands to it.
501 static DWORD WINAPI MCIAVI_mciPlay_thread(LPVOID arg)
503 struct MCIAVI_play_data *data = (struct MCIAVI_play_data *)arg;
504 DWORD ret;
506 TRACE("In thread before async play command (id %u, flags %08x)\n", data->wma->wDevID, data->flags);
507 ret = MCIAVI_player(data->wma, data->flags, &data->params);
508 TRACE("In thread after async play command (id %u, flags %08x)\n", data->wma->wDevID, data->flags);
510 HeapFree(GetProcessHeap(), 0, data);
511 return ret;
515 * MCIAVI_mciPlay_async
517 static DWORD MCIAVI_mciPlay_async(WINE_MCIAVI *wma, DWORD dwFlags, LPMCI_PLAY_PARMS lpParams)
519 HANDLE handle;
520 struct MCIAVI_play_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(struct MCIAVI_play_data));
522 if (!data) return MCIERR_OUT_OF_MEMORY;
524 data->wma = wma;
525 data->flags = dwFlags;
526 if (dwFlags & MCI_NOTIFY)
527 data->params.dwCallback = lpParams->dwCallback;
529 if (!(handle = CreateThread(NULL, 0, MCIAVI_mciPlay_thread, data, 0, NULL)))
531 WARN("Couldn't create thread for async play, playing synchronously\n");
532 return MCIAVI_mciPlay_thread(data);
534 SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
535 CloseHandle(handle);
536 return 0;
539 /***************************************************************************
540 * MCIAVI_mciPlay [internal]
542 static DWORD MCIAVI_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
544 WINE_MCIAVI *wma;
545 DWORD dwRet;
546 DWORD dwFromFrame, dwToFrame;
548 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
550 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
552 wma = MCIAVI_mciGetOpenDev(wDevID);
553 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
554 if (dwFlags & MCI_DGV_PLAY_REVERSE) return MCIERR_UNSUPPORTED_FUNCTION;
555 if (dwFlags & MCI_TEST) return 0;
557 if (dwFlags & (MCI_MCIAVI_PLAY_WINDOW|MCI_MCIAVI_PLAY_FULLBY2))
558 FIXME("Unsupported flag %08x\n", dwFlags);
560 EnterCriticalSection(&wma->cs);
562 if (!wma->hFile)
564 LeaveCriticalSection(&wma->cs);
565 return MCIERR_FILE_NOT_FOUND;
567 if (!wma->hWndPaint)
569 LeaveCriticalSection(&wma->cs);
570 return MCIERR_NO_WINDOW;
573 dwFromFrame = wma->dwCurrVideoFrame;
574 dwToFrame = wma->dwPlayableVideoFrames - 1;
576 if (dwFlags & MCI_FROM) {
577 dwFromFrame = MCIAVI_ConvertTimeFormatToFrame(wma, lpParms->dwFrom);
579 if (dwFlags & MCI_TO) {
580 dwToFrame = MCIAVI_ConvertTimeFormatToFrame(wma, lpParms->dwTo);
582 if (dwToFrame >= wma->dwPlayableVideoFrames)
583 dwToFrame = wma->dwPlayableVideoFrames - 1;
585 TRACE("Playing from frame=%u to frame=%u\n", dwFromFrame, dwToFrame);
587 wma->dwCurrVideoFrame = dwFromFrame;
588 wma->dwToVideoFrame = dwToFrame;
590 LeaveCriticalSection(&wma->cs);
592 if (dwFlags & MCI_MCIAVI_PLAY_FULLSCREEN)
594 HMONITOR mon = MonitorFromWindow(wma->hWndPaint, MONITOR_DEFAULTTONEAREST);
595 MONITORINFO mi;
596 mi.cbSize = sizeof(mi);
597 GetMonitorInfoA(mon, &mi);
598 wma->hWndPaint = CreateWindowA("STATIC", NULL, WS_POPUP | WS_VISIBLE, mi.rcMonitor.left,
599 mi.rcMonitor.top, mi.rcMonitor.right - mi.rcMonitor.left, mi.rcMonitor.bottom - mi.rcMonitor.top,
600 NULL, NULL, NULL, 0);
602 /* if not fullscreen ensure the window is visible */
603 else if (!(GetWindowLongW(wma->hWndPaint, GWL_STYLE) & WS_VISIBLE))
604 ShowWindow(wma->hWndPaint, SW_SHOWNA);
606 EnterCriticalSection(&wma->cs);
608 /* if already playing exit */
609 if (wma->dwStatus == MCI_MODE_PLAY)
611 LeaveCriticalSection(&wma->cs);
612 return 0;
615 wma->dwStatus = MCI_MODE_PLAY;
617 LeaveCriticalSection(&wma->cs);
619 if (dwFlags & MCI_WAIT)
620 return MCIAVI_player(wma, dwFlags, lpParms);
622 dwRet = MCIAVI_mciPlay_async(wma, dwFlags, lpParms);
624 if (dwRet) {
625 EnterCriticalSection(&wma->cs);
626 wma->dwStatus = MCI_MODE_STOP;
627 LeaveCriticalSection(&wma->cs);
629 return dwRet;
632 /***************************************************************************
633 * MCIAVI_mciStop [internal]
635 static DWORD MCIAVI_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
637 WINE_MCIAVI *wma;
638 DWORD dwRet = 0;
640 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
642 wma = MCIAVI_mciGetOpenDev(wDevID);
643 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
644 if (dwFlags & MCI_TEST) return 0;
646 EnterCriticalSection(&wma->cs);
648 TRACE("current status %04x\n", wma->dwStatus);
650 switch (wma->dwStatus) {
651 case MCI_MODE_PLAY:
652 case MCI_MODE_RECORD:
653 LeaveCriticalSection(&wma->cs);
654 SetEvent(wma->hStopEvent);
655 EnterCriticalSection(&wma->cs);
656 /* fall through */
657 case MCI_MODE_PAUSE:
658 /* Since our wave notification callback takes the lock,
659 * we must release it before resetting the device */
660 LeaveCriticalSection(&wma->cs);
661 dwRet = waveOutReset(wma->hWave);
662 EnterCriticalSection(&wma->cs);
663 /* fall through */
664 default:
665 do /* one more chance for an async thread to finish */
667 LeaveCriticalSection(&wma->cs);
668 Sleep(10);
669 EnterCriticalSection(&wma->cs);
670 } while (wma->dwStatus != MCI_MODE_STOP);
672 break;
674 case MCI_MODE_NOT_READY:
675 break;
678 if ((dwFlags & MCI_NOTIFY) && lpParms) {
679 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
680 wDevID, MCI_NOTIFY_SUCCESSFUL);
682 LeaveCriticalSection(&wma->cs);
683 return dwRet;
686 /***************************************************************************
687 * MCIAVI_mciPause [internal]
689 static DWORD MCIAVI_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
691 WINE_MCIAVI *wma;
693 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
695 wma = MCIAVI_mciGetOpenDev(wDevID);
696 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
697 if (dwFlags & MCI_TEST) return 0;
699 EnterCriticalSection(&wma->cs);
701 if (wma->dwStatus == MCI_MODE_PLAY)
702 wma->dwStatus = MCI_MODE_PAUSE;
704 if (wma->lpWaveFormat) {
705 LeaveCriticalSection(&wma->cs);
706 return waveOutPause(wma->hWave);
709 LeaveCriticalSection(&wma->cs);
710 return 0;
713 /***************************************************************************
714 * MCIAVI_mciResume [internal]
716 static DWORD MCIAVI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
718 WINE_MCIAVI *wma;
720 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
722 wma = MCIAVI_mciGetOpenDev(wDevID);
723 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
724 if (dwFlags & MCI_TEST) return 0;
726 EnterCriticalSection(&wma->cs);
728 if (wma->dwStatus == MCI_MODE_PAUSE)
729 wma->dwStatus = MCI_MODE_PLAY;
731 if (wma->lpWaveFormat) {
732 LeaveCriticalSection(&wma->cs);
733 return waveOutRestart(wma->hWave);
736 LeaveCriticalSection(&wma->cs);
737 return 0;
740 /***************************************************************************
741 * MCIAVI_mciSeek [internal]
743 static DWORD MCIAVI_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
745 WINE_MCIAVI *wma;
746 DWORD position;
748 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
750 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
752 wma = MCIAVI_mciGetOpenDev(wDevID);
753 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
755 position = dwFlags & (MCI_SEEK_TO_START|MCI_SEEK_TO_END|MCI_TO);
756 if (!position) return MCIERR_MISSING_PARAMETER;
757 if (position&(position-1)) return MCIERR_FLAGS_NOT_COMPATIBLE;
759 if (dwFlags & MCI_TO) {
760 position = MCIAVI_ConvertTimeFormatToFrame(wma, lpParms->dwTo);
761 if (position >= wma->dwPlayableVideoFrames)
762 return MCIERR_OUTOFRANGE;
763 } else if (dwFlags & MCI_SEEK_TO_START) {
764 position = 0;
765 } else {
766 position = wma->dwPlayableVideoFrames - 1;
768 if (dwFlags & MCI_TEST) return 0;
770 MCIAVI_mciStop(wDevID, MCI_WAIT, NULL);
772 EnterCriticalSection(&wma->cs);
774 wma->dwCurrVideoFrame = position;
775 TRACE("Seeking to frame=%u\n", wma->dwCurrVideoFrame);
777 if (dwFlags & MCI_NOTIFY) {
778 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
779 wDevID, MCI_NOTIFY_SUCCESSFUL);
781 LeaveCriticalSection(&wma->cs);
782 return 0;
785 /*****************************************************************************
786 * MCIAVI_mciLoad [internal]
788 static DWORD MCIAVI_mciLoad(UINT wDevID, DWORD dwFlags, LPMCI_DGV_LOAD_PARMSW lpParms)
790 WINE_MCIAVI *wma;
792 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
794 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
796 wma = MCIAVI_mciGetOpenDev(wDevID);
797 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
799 return MCIERR_UNSUPPORTED_FUNCTION; /* like w2k */
802 /******************************************************************************
803 * MCIAVI_mciRealize [internal]
805 static DWORD MCIAVI_mciRealize(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
807 WINE_MCIAVI *wma;
809 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
811 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
813 wma = MCIAVI_mciGetOpenDev(wDevID);
814 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
815 if (dwFlags & MCI_TEST) return 0;
817 return 0;
820 /******************************************************************************
821 * MCIAVI_mciUpdate [internal]
823 static DWORD MCIAVI_mciUpdate(UINT wDevID, DWORD dwFlags, LPMCI_DGV_UPDATE_PARMS lpParms)
825 WINE_MCIAVI *wma;
827 TRACE("%04x, %08x, %p\n", wDevID, dwFlags, lpParms);
829 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
831 wma = MCIAVI_mciGetOpenDev(wDevID);
832 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
833 /* Ignore MCI_TEST flag. */
835 EnterCriticalSection(&wma->cs);
837 if (dwFlags & MCI_DGV_UPDATE_HDC)
838 MCIAVI_PaintFrame(wma, lpParms->hDC);
840 LeaveCriticalSection(&wma->cs);
842 return 0;
845 /******************************************************************************
846 * MCIAVI_mciStep [internal]
848 static DWORD MCIAVI_mciStep(UINT wDevID, DWORD dwFlags, LPMCI_DGV_STEP_PARMS lpParms)
850 WINE_MCIAVI *wma;
851 DWORD position;
852 int delta = 1;
854 TRACE("(%04x, %08x, %p)\n", wDevID, dwFlags, lpParms);
856 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
858 wma = MCIAVI_mciGetOpenDev(wDevID);
859 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
861 if (dwFlags & MCI_DGV_STEP_FRAMES) delta = lpParms->dwFrames;
862 if (dwFlags & MCI_DGV_STEP_REVERSE) delta = -delta;
863 position = wma->dwCurrVideoFrame + delta;
864 if (position >= wma->dwPlayableVideoFrames) return MCIERR_OUTOFRANGE;
865 if (dwFlags & MCI_TEST) return 0;
867 MCIAVI_mciStop(wDevID, MCI_WAIT, NULL);
869 EnterCriticalSection(&wma->cs);
871 wma->dwCurrVideoFrame = position;
872 TRACE("Stepping to frame=%u\n", wma->dwCurrVideoFrame);
874 if (dwFlags & MCI_NOTIFY) {
875 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
876 wDevID, MCI_NOTIFY_SUCCESSFUL);
878 LeaveCriticalSection(&wma->cs);
879 return 0;
882 /******************************************************************************
883 * MCIAVI_mciCue [internal]
885 static DWORD MCIAVI_mciCue(UINT wDevID, DWORD dwFlags, LPMCI_DGV_CUE_PARMS lpParms)
887 WINE_MCIAVI *wma;
889 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
891 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
893 wma = MCIAVI_mciGetOpenDev(wDevID);
894 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
895 if (dwFlags & MCI_DGV_CUE_INPUT) return MCIERR_UNSUPPORTED_FUNCTION;
896 if (dwFlags & MCI_TEST) return 0;
898 return 0;
901 /******************************************************************************
902 * MCIAVI_mciBreak [internal]
904 static DWORD MCIAVI_mciBreak(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
906 WINE_MCIAVI *wma;
908 TRACE("(%04x, %08x, %p)\n", wDevID, dwFlags, lpParms);
910 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
912 wma = MCIAVI_mciGetOpenDev(wDevID);
913 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
915 EnterCriticalSection(&wma->cs);
917 wma->mci_break.flags = dwFlags;
918 wma->mci_break.parms = *lpParms;
920 LeaveCriticalSection(&wma->cs);
922 return 0;
925 /******************************************************************************
926 * MCIAVI_mciSetAudio [internal]
928 static DWORD MCIAVI_mciSetAudio(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SETAUDIO_PARMSW lpParms)
930 WINE_MCIAVI *wma;
932 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
934 FIXME("(%04x, %08x, %p) Item %04x: stub\n", wDevID, dwFlags, lpParms, dwFlags & MCI_DGV_SETAUDIO_ITEM ? lpParms->dwItem : 0);
936 wma = MCIAVI_mciGetOpenDev(wDevID);
937 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
939 return 0;
942 /******************************************************************************
943 * MCIAVI_mciSignal [internal]
945 static DWORD MCIAVI_mciSignal(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SIGNAL_PARMS lpParms)
947 WINE_MCIAVI *wma;
949 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
951 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
953 wma = MCIAVI_mciGetOpenDev(wDevID);
954 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
956 return 0;
959 /******************************************************************************
960 * MCIAVI_mciSetVideo [internal]
962 static DWORD MCIAVI_mciSetVideo(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SETVIDEO_PARMSW lpParms)
964 WINE_MCIAVI *wma;
966 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
968 FIXME("(%04x, %08x, %p) Item %04x: stub\n", wDevID, dwFlags, lpParms, dwFlags & MCI_DGV_SETVIDEO_ITEM ? lpParms->dwItem : 0);
970 wma = MCIAVI_mciGetOpenDev(wDevID);
971 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
973 return 0;
976 /******************************************************************************
977 * MCIAVI_mciConfigure [internal]
979 static DWORD MCIAVI_mciConfigure(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
981 WINE_MCIAVI *wma;
983 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
985 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
987 wma = MCIAVI_mciGetOpenDev(wDevID);
988 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
989 if (dwFlags & MCI_TEST) return 0;
991 return 0;
994 /*======================================================================*
995 * MCI AVI entry points *
996 *======================================================================*/
998 /**************************************************************************
999 * DriverProc (MCIAVI.@)
1001 LRESULT CALLBACK MCIAVI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1002 LPARAM dwParam1, LPARAM dwParam2)
1004 TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
1005 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1007 switch (wMsg) {
1008 case DRV_LOAD: return 1;
1009 case DRV_FREE: return 1;
1010 case DRV_OPEN: return MCIAVI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1011 case DRV_CLOSE: return MCIAVI_drvClose(dwDevID);
1012 case DRV_ENABLE: return 1;
1013 case DRV_DISABLE: return 1;
1014 case DRV_QUERYCONFIGURE: return 1;
1015 case DRV_CONFIGURE: return MCIAVI_drvConfigure(dwDevID);
1016 case DRV_INSTALL: return DRVCNF_RESTART;
1017 case DRV_REMOVE: return DRVCNF_RESTART;
1020 /* session instance */
1021 if (dwDevID == 0xFFFFFFFF) return 1;
1023 switch (wMsg) {
1024 case MCI_OPEN_DRIVER: return MCIAVI_mciOpen (dwDevID, dwParam1, (LPMCI_DGV_OPEN_PARMSW) dwParam2);
1025 case MCI_CLOSE_DRIVER: return MCIAVI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1026 case MCI_PLAY: return MCIAVI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
1027 case MCI_STOP: return MCIAVI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1028 case MCI_SET: return MCIAVI_mciSet (dwDevID, dwParam1, (LPMCI_DGV_SET_PARMS) dwParam2);
1029 case MCI_PAUSE: return MCIAVI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1030 case MCI_RESUME: return MCIAVI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1031 case MCI_STATUS: return MCIAVI_mciStatus (dwDevID, dwParam1, (LPMCI_DGV_STATUS_PARMSW) dwParam2);
1032 case MCI_GETDEVCAPS: return MCIAVI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS) dwParam2);
1033 case MCI_INFO: return MCIAVI_mciInfo (dwDevID, dwParam1, (LPMCI_DGV_INFO_PARMSW) dwParam2);
1034 case MCI_SEEK: return MCIAVI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
1035 case MCI_PUT: return MCIAVI_mciPut (dwDevID, dwParam1, (LPMCI_DGV_PUT_PARMS) dwParam2);
1036 case MCI_WINDOW: return MCIAVI_mciWindow (dwDevID, dwParam1, (LPMCI_DGV_WINDOW_PARMSW) dwParam2);
1037 case MCI_LOAD: return MCIAVI_mciLoad (dwDevID, dwParam1, (LPMCI_DGV_LOAD_PARMSW) dwParam2);
1038 case MCI_REALIZE: return MCIAVI_mciRealize (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1039 case MCI_UPDATE: return MCIAVI_mciUpdate (dwDevID, dwParam1, (LPMCI_DGV_UPDATE_PARMS) dwParam2);
1040 case MCI_WHERE: return MCIAVI_mciWhere (dwDevID, dwParam1, (LPMCI_DGV_RECT_PARMS) dwParam2);
1041 case MCI_STEP: return MCIAVI_mciStep (dwDevID, dwParam1, (LPMCI_DGV_STEP_PARMS) dwParam2);
1042 case MCI_CUE: return MCIAVI_mciCue (dwDevID, dwParam1, (LPMCI_DGV_CUE_PARMS) dwParam2);
1043 case MCI_BREAK: return MCIAVI_mciBreak (dwDevID, dwParam1, (LPMCI_BREAK_PARMS) dwParam2);
1044 /* Digital Video specific */
1045 case MCI_SETAUDIO: return MCIAVI_mciSetAudio (dwDevID, dwParam1, (LPMCI_DGV_SETAUDIO_PARMSW) dwParam2);
1046 case MCI_SIGNAL: return MCIAVI_mciSignal (dwDevID, dwParam1, (LPMCI_DGV_SIGNAL_PARMS) dwParam2);
1047 case MCI_SETVIDEO: return MCIAVI_mciSetVideo (dwDevID, dwParam1, (LPMCI_DGV_SETVIDEO_PARMSW) dwParam2);
1048 case MCI_CONFIGURE: return MCIAVI_mciConfigure (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1050 /* no editing, recording, saving, locking without inputs */
1051 case MCI_CAPTURE:
1052 case MCI_COPY:
1053 case MCI_CUT:
1054 case MCI_DELETE:
1055 case MCI_FREEZE:
1056 case MCI_LIST:
1057 case MCI_MONITOR:
1058 case MCI_PASTE:
1059 case MCI_QUALITY:
1060 case MCI_RECORD:
1061 case MCI_RESERVE:
1062 case MCI_RESTORE:
1063 case MCI_SAVE:
1064 case MCI_UNDO:
1065 case MCI_UNFREEZE:
1066 TRACE("Unsupported function [0x%x] flags=%08x\n", wMsg, (DWORD)dwParam1);
1067 return MCIERR_UNSUPPORTED_FUNCTION;
1068 case MCI_SPIN:
1069 case MCI_ESCAPE:
1070 WARN("Unsupported command [0x%x] %08x\n", wMsg, (DWORD)dwParam1);
1071 break;
1072 case MCI_OPEN:
1073 case MCI_CLOSE:
1074 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1075 break;
1076 default:
1077 TRACE("Sending msg [%u] to default driver proc\n", wMsg);
1078 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1080 return MCIERR_UNRECOGNIZED_COMMAND;