scrrun: Add IFileSystem3 support.
[wine/multimedia.git] / dlls / mciavi32 / mciavi.c
blobc78ddc058be0053ab4a56262820f05d85bf8e09c
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"
43 #include "wine/unicode.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(mciavi);
47 static DWORD MCIAVI_mciStop(UINT, DWORD, LPMCI_GENERIC_PARMS);
49 /*======================================================================*
50 * MCI AVI implementation *
51 *======================================================================*/
53 HINSTANCE MCIAVI_hInstance = 0;
55 /***********************************************************************
56 * DllMain (MCIAVI.0)
58 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
60 switch (fdwReason) {
61 case DLL_PROCESS_ATTACH:
62 DisableThreadLibraryCalls(hInstDLL);
63 MCIAVI_hInstance = hInstDLL;
64 break;
66 return TRUE;
69 /**************************************************************************
70 * MCIAVI_drvOpen [internal]
72 static DWORD MCIAVI_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
74 WINE_MCIAVI* wma;
75 static const WCHAR mciAviWStr[] = {'M','C','I','A','V','I',0};
77 TRACE("%s, %p\n", debugstr_w(str), modp);
79 /* session instance */
80 if (!modp) return 0xFFFFFFFF;
82 if (!MCIAVI_RegisterClass()) return 0;
84 wma = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIAVI));
85 if (!wma)
86 return 0;
88 InitializeCriticalSection(&wma->cs);
89 wma->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINE_MCIAVI.cs");
90 wma->ack_event = CreateEventW(NULL, FALSE, FALSE, NULL);
91 wma->hStopEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
92 wma->wDevID = modp->wDeviceID;
93 wma->wCommandTable = mciLoadCommandResource(MCIAVI_hInstance, mciAviWStr, 0);
94 wma->dwStatus = MCI_MODE_NOT_READY;
95 modp->wCustomCommandTable = wma->wCommandTable;
96 modp->wType = MCI_DEVTYPE_DIGITAL_VIDEO;
97 mciSetDriverData(wma->wDevID, (DWORD_PTR)wma);
99 return modp->wDeviceID;
102 /**************************************************************************
103 * MCIAVI_drvClose [internal]
105 static DWORD MCIAVI_drvClose(DWORD dwDevID)
107 WINE_MCIAVI *wma;
109 TRACE("%04x\n", dwDevID);
111 /* finish all outstanding things */
112 MCIAVI_mciClose(dwDevID, MCI_WAIT, NULL);
114 wma = (WINE_MCIAVI*)mciGetDriverData(dwDevID);
116 if (wma) {
117 MCIAVI_UnregisterClass();
119 EnterCriticalSection(&wma->cs);
121 mciSetDriverData(dwDevID, 0);
122 mciFreeCommandResource(wma->wCommandTable);
124 CloseHandle(wma->ack_event);
125 CloseHandle(wma->hStopEvent);
127 LeaveCriticalSection(&wma->cs);
128 wma->cs.DebugInfo->Spare[0] = 0;
129 DeleteCriticalSection(&wma->cs);
131 HeapFree(GetProcessHeap(), 0, wma);
132 return 1;
134 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
137 /**************************************************************************
138 * MCIAVI_drvConfigure [internal]
140 static DWORD MCIAVI_drvConfigure(DWORD dwDevID)
142 WINE_MCIAVI *wma;
144 TRACE("%04x\n", dwDevID);
146 MCIAVI_mciStop(dwDevID, MCI_WAIT, NULL);
148 wma = (WINE_MCIAVI*)mciGetDriverData(dwDevID);
150 if (wma) {
151 MessageBoxA(0, "Sample AVI Wine Driver !", "MM-Wine Driver", MB_OK);
152 return 1;
154 return 0;
157 /**************************************************************************
158 * MCIAVI_mciGetOpenDev [internal]
160 WINE_MCIAVI* MCIAVI_mciGetOpenDev(UINT wDevID)
162 WINE_MCIAVI* wma = (WINE_MCIAVI*)mciGetDriverData(wDevID);
164 if (wma == NULL || wma->nUseCount == 0) {
165 WARN("Invalid wDevID=%u\n", wDevID);
166 return 0;
168 return wma;
171 static void MCIAVI_CleanUp(WINE_MCIAVI* wma)
173 /* to prevent handling in WindowProc */
174 wma->dwStatus = MCI_MODE_NOT_READY;
175 if (wma->hFile) {
176 mmioClose(wma->hFile, 0);
177 wma->hFile = 0;
179 HeapFree(GetProcessHeap(), 0, wma->lpFileName);
180 wma->lpFileName = NULL;
182 HeapFree(GetProcessHeap(), 0, wma->lpVideoIndex);
183 wma->lpVideoIndex = NULL;
184 HeapFree(GetProcessHeap(), 0, wma->lpAudioIndex);
185 wma->lpAudioIndex = NULL;
186 if (wma->hic) ICClose(wma->hic);
187 wma->hic = 0;
188 HeapFree(GetProcessHeap(), 0, wma->inbih);
189 wma->inbih = NULL;
190 HeapFree(GetProcessHeap(), 0, wma->outbih);
191 wma->outbih = NULL;
192 HeapFree(GetProcessHeap(), 0, wma->indata);
193 wma->indata = NULL;
194 HeapFree(GetProcessHeap(), 0, wma->outdata);
195 wma->outdata = NULL;
196 if (wma->hbmFrame) DeleteObject(wma->hbmFrame);
197 wma->hbmFrame = 0;
198 if (wma->hWnd) DestroyWindow(wma->hWnd);
199 wma->hWnd = 0;
201 HeapFree(GetProcessHeap(), 0, wma->lpWaveFormat);
202 wma->lpWaveFormat = 0;
204 memset(&wma->mah, 0, sizeof(wma->mah));
205 memset(&wma->ash_video, 0, sizeof(wma->ash_video));
206 memset(&wma->ash_audio, 0, sizeof(wma->ash_audio));
207 wma->dwCurrVideoFrame = wma->dwCurrAudioBlock = 0;
208 wma->dwCachedFrame = -1;
212 /***************************************************************************
213 * MCIAVI_mciOpen [internal]
215 static DWORD MCIAVI_mciOpen(UINT wDevID, DWORD dwFlags,
216 LPMCI_DGV_OPEN_PARMSW lpOpenParms)
218 WINE_MCIAVI *wma;
219 LRESULT dwRet = 0;
221 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpOpenParms);
223 if (lpOpenParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
225 wma = (WINE_MCIAVI *)mciGetDriverData(wDevID);
226 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
228 EnterCriticalSection(&wma->cs);
230 if (wma->nUseCount > 0) {
231 /* The driver is already open on this channel */
232 /* If the driver was opened shareable before and this open specifies */
233 /* shareable then increment the use count */
234 if (wma->fShareable && (dwFlags & MCI_OPEN_SHAREABLE))
235 ++wma->nUseCount;
236 else
238 LeaveCriticalSection(&wma->cs);
239 return MCIERR_MUST_USE_SHAREABLE;
241 } else {
242 wma->nUseCount = 1;
243 wma->fShareable = dwFlags & MCI_OPEN_SHAREABLE;
246 wma->dwStatus = MCI_MODE_NOT_READY;
248 if (dwFlags & MCI_OPEN_ELEMENT) {
249 if (dwFlags & MCI_OPEN_ELEMENT_ID) {
250 /* could it be that (DWORD)lpOpenParms->lpstrElementName
251 * contains the hFile value ?
253 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
254 } else if (lpOpenParms->lpstrElementName && lpOpenParms->lpstrElementName[0]) {
255 /* FIXME : what should be done id wma->hFile is already != 0, or the driver is playin' */
256 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpOpenParms->lpstrElementName));
258 wma->lpFileName = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpOpenParms->lpstrElementName) + 1) * sizeof(WCHAR));
259 strcpyW(wma->lpFileName, lpOpenParms->lpstrElementName);
261 if (lpOpenParms->lpstrElementName[0] == '@') {
262 /* The file name @11223344 encodes an AVIFile handle in decimal notation
263 * in Win3.1 and w2k/NT, but this feature is absent in win95 (KB140750).
264 * wma->hFile = LongToHandle(strtolW(lpOpenParms->lpstrElementName+1, NULL, 10)); */
265 FIXME("Using AVIFile/Stream %s NIY\n", debugstr_w(lpOpenParms->lpstrElementName));
267 wma->hFile = mmioOpenW(lpOpenParms->lpstrElementName, NULL,
268 MMIO_ALLOCBUF | MMIO_DENYWRITE | MMIO_READ);
270 if (wma->hFile == 0) {
271 WARN("can't find file=%s!\n", debugstr_w(lpOpenParms->lpstrElementName));
272 dwRet = MCIERR_FILE_NOT_FOUND;
273 } else {
274 if (!MCIAVI_GetInfo(wma))
275 dwRet = MCIERR_INVALID_FILE;
276 else if (!MCIAVI_OpenVideo(wma))
277 dwRet = MCIERR_CANNOT_LOAD_DRIVER;
278 else if (!MCIAVI_CreateWindow(wma, dwFlags, lpOpenParms))
279 dwRet = MCIERR_CREATEWINDOW;
281 } else {
282 FIXME("Don't record yet\n");
283 dwRet = MCIERR_UNSUPPORTED_FUNCTION;
287 if (dwRet == 0) {
288 TRACE("lpOpenParms->wDeviceID = %04x\n", lpOpenParms->wDeviceID);
290 wma->dwStatus = MCI_MODE_STOP;
291 wma->dwMciTimeFormat = MCI_FORMAT_FRAMES;
292 } else {
293 MCIAVI_CleanUp(wma);
296 LeaveCriticalSection(&wma->cs);
298 if (!dwRet && (dwFlags & MCI_NOTIFY)) {
299 mciDriverNotify(HWND_32(LOWORD(lpOpenParms->dwCallback)),
300 wDevID, MCI_NOTIFY_SUCCESSFUL);
302 return dwRet;
305 /***************************************************************************
306 * MCIAVI_mciClose [internal]
308 DWORD MCIAVI_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
310 WINE_MCIAVI *wma;
311 DWORD dwRet = 0;
313 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
315 wma = MCIAVI_mciGetOpenDev(wDevID);
316 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
318 MCIAVI_mciStop(wDevID, MCI_WAIT, NULL);
320 EnterCriticalSection(&wma->cs);
322 if (wma->nUseCount == 1) {
323 MCIAVI_CleanUp(wma);
325 if ((dwFlags & MCI_NOTIFY) && lpParms) {
326 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
327 wDevID,
328 MCI_NOTIFY_SUCCESSFUL);
330 LeaveCriticalSection(&wma->cs);
331 return dwRet;
333 wma->nUseCount--;
335 LeaveCriticalSection(&wma->cs);
336 return dwRet;
339 static DWORD MCIAVI_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms);
341 struct MCIAVI_play_data
343 MCIDEVICEID wDevID;
344 DWORD flags;
345 MCI_PLAY_PARMS params;
349 * MCIAVI_mciPlay_thread
351 * FIXME: probably should use a common worker thread created at the driver
352 * load time and queue all async commands to it.
354 static DWORD WINAPI MCIAVI_mciPlay_thread(LPVOID arg)
356 struct MCIAVI_play_data *data = (struct MCIAVI_play_data *)arg;
357 DWORD ret;
359 TRACE("In thread before async play command (id %08x, flags %08x)\n", data->wDevID, data->flags);
360 ret = MCIAVI_mciPlay(data->wDevID, data->flags | MCI_WAIT, &data->params);
361 TRACE("In thread after async play command (id %08x, flags %08x)\n", data->wDevID, data->flags);
363 HeapFree(GetProcessHeap(), 0, data);
364 return ret;
368 * MCIAVI_mciPlay_async
370 static DWORD MCIAVI_mciPlay_async(WINE_MCIAVI *wma, DWORD dwFlags, LPMCI_PLAY_PARMS lpParams)
372 HANDLE handle, ack_event = wma->ack_event;
373 struct MCIAVI_play_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(struct MCIAVI_play_data));
375 if (!data) return MCIERR_OUT_OF_MEMORY;
377 data->wDevID = wma->wDevID;
378 data->flags = dwFlags;
379 data->params = *lpParams;
381 if (!(handle = CreateThread(NULL, 0, MCIAVI_mciPlay_thread, data, 0, NULL)))
383 WARN("Couldn't create thread for async play, playing synchronously\n");
384 return MCIAVI_mciPlay_thread(data);
386 SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
387 CloseHandle(handle);
388 /* wait until the thread starts up, so the app could see a changed status */
389 WaitForSingleObject(ack_event, INFINITE);
390 TRACE("Async play has started\n");
391 return 0;
394 /***************************************************************************
395 * MCIAVI_mciPlay [internal]
397 static DWORD MCIAVI_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
399 WINE_MCIAVI *wma;
400 DWORD frameTime;
401 DWORD dwRet;
402 LPWAVEHDR waveHdr = NULL;
403 unsigned i, nHdr = 0;
404 DWORD dwFromFrame, dwToFrame;
405 DWORD numEvents = 1;
406 HANDLE events[2];
408 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
410 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
412 wma = MCIAVI_mciGetOpenDev(wDevID);
413 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
414 if (dwFlags & MCI_DGV_PLAY_REVERSE) return MCIERR_UNSUPPORTED_FUNCTION;
415 if (dwFlags & MCI_TEST) return 0;
417 EnterCriticalSection(&wma->cs);
419 if (!wma->hFile)
421 LeaveCriticalSection(&wma->cs);
422 return MCIERR_FILE_NOT_FOUND;
424 if (!wma->hWndPaint)
426 LeaveCriticalSection(&wma->cs);
427 return MCIERR_NO_WINDOW;
430 LeaveCriticalSection(&wma->cs);
432 if (!(dwFlags & MCI_WAIT))
433 return MCIAVI_mciPlay_async(wma, dwFlags, lpParms);
435 if (!(GetWindowLongW(wma->hWndPaint, GWL_STYLE) & WS_VISIBLE))
436 ShowWindow(wma->hWndPaint, SW_SHOWNA);
438 EnterCriticalSection(&wma->cs);
440 dwFromFrame = wma->dwCurrVideoFrame;
441 dwToFrame = wma->dwPlayableVideoFrames - 1;
443 if (dwFlags & MCI_FROM) {
444 dwFromFrame = MCIAVI_ConvertTimeFormatToFrame(wma, lpParms->dwFrom);
446 if (dwFlags & MCI_TO) {
447 dwToFrame = MCIAVI_ConvertTimeFormatToFrame(wma, lpParms->dwTo);
449 if (dwToFrame >= wma->dwPlayableVideoFrames)
450 dwToFrame = wma->dwPlayableVideoFrames - 1;
452 TRACE("Playing from frame=%u to frame=%u\n", dwFromFrame, dwToFrame);
454 wma->dwCurrVideoFrame = dwFromFrame;
455 wma->dwToVideoFrame = dwToFrame;
457 /* if already playing exit */
458 if (wma->dwStatus == MCI_MODE_PLAY)
460 LeaveCriticalSection(&wma->cs);
461 SetEvent(wma->ack_event);
462 return 0;
465 if (wma->dwToVideoFrame <= wma->dwCurrVideoFrame)
467 dwRet = 0;
468 SetEvent(wma->ack_event);
469 goto mci_play_done;
472 wma->dwStatus = MCI_MODE_PLAY;
473 /* signal the state change */
474 SetEvent(wma->ack_event);
476 if (dwFlags & (MCI_DGV_PLAY_REPEAT|MCI_MCIAVI_PLAY_WINDOW|MCI_MCIAVI_PLAY_FULLSCREEN))
477 FIXME("Unsupported flag %08x\n", dwFlags);
479 /* time is in microseconds, we should convert it to milliseconds */
480 frameTime = (wma->mah.dwMicroSecPerFrame + 500) / 1000;
482 events[0] = wma->hStopEvent;
483 if (wma->lpWaveFormat) {
484 if (MCIAVI_OpenAudio(wma, &nHdr, &waveHdr) != 0)
486 /* can't play audio */
487 HeapFree(GetProcessHeap(), 0, wma->lpWaveFormat);
488 wma->lpWaveFormat = NULL;
490 else
492 /* fill the queue with as many wave headers as possible */
493 MCIAVI_PlayAudioBlocks(wma, nHdr, waveHdr);
494 events[1] = wma->hEvent;
495 numEvents = 2;
499 while (wma->dwStatus == MCI_MODE_PLAY)
501 HDC hDC;
502 DWORD tc, delta;
503 DWORD ret;
505 tc = GetTickCount();
507 hDC = wma->hWndPaint ? GetDC(wma->hWndPaint) : 0;
508 if (hDC)
510 MCIAVI_PaintFrame(wma, hDC);
511 ReleaseDC(wma->hWndPaint, hDC);
514 if (wma->lpWaveFormat)
515 MCIAVI_PlayAudioBlocks(wma, nHdr, waveHdr);
517 delta = GetTickCount() - tc;
518 if (delta < frameTime)
519 delta = frameTime - delta;
520 else
521 delta = 0;
523 LeaveCriticalSection(&wma->cs);
524 ret = WaitForMultipleObjects(numEvents, events, FALSE, delta);
525 EnterCriticalSection(&wma->cs);
526 if (ret == WAIT_OBJECT_0 || wma->dwStatus != MCI_MODE_PLAY) break;
528 if (wma->dwCurrVideoFrame < dwToFrame)
529 wma->dwCurrVideoFrame++;
530 else
531 break;
534 if (wma->lpWaveFormat) {
535 while (wma->dwEventCount != nHdr - 1)
537 LeaveCriticalSection(&wma->cs);
538 Sleep(100);
539 EnterCriticalSection(&wma->cs);
542 /* just to get rid of some race conditions between play, stop and pause */
543 LeaveCriticalSection(&wma->cs);
544 waveOutReset(wma->hWave);
545 EnterCriticalSection(&wma->cs);
547 for (i = 0; i < nHdr; i++)
548 waveOutUnprepareHeader(wma->hWave, &waveHdr[i], sizeof(WAVEHDR));
551 dwRet = 0;
553 if (wma->lpWaveFormat) {
554 HeapFree(GetProcessHeap(), 0, waveHdr);
556 if (wma->hWave) {
557 LeaveCriticalSection(&wma->cs);
558 waveOutClose(wma->hWave);
559 EnterCriticalSection(&wma->cs);
560 wma->hWave = 0;
562 CloseHandle(wma->hEvent);
565 mci_play_done:
566 wma->dwStatus = MCI_MODE_STOP;
568 if (dwFlags & MCI_NOTIFY) {
569 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
570 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
571 wDevID, MCI_NOTIFY_SUCCESSFUL);
573 LeaveCriticalSection(&wma->cs);
574 return dwRet;
577 /***************************************************************************
578 * MCIAVI_mciStop [internal]
580 static DWORD MCIAVI_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
582 WINE_MCIAVI *wma;
583 DWORD dwRet = 0;
585 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
587 wma = MCIAVI_mciGetOpenDev(wDevID);
588 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
589 if (dwFlags & MCI_TEST) return 0;
591 EnterCriticalSection(&wma->cs);
593 TRACE("current status %04x\n", wma->dwStatus);
595 switch (wma->dwStatus) {
596 case MCI_MODE_PLAY:
597 case MCI_MODE_RECORD:
598 LeaveCriticalSection(&wma->cs);
599 SetEvent(wma->hStopEvent);
600 EnterCriticalSection(&wma->cs);
601 /* fall through */
602 case MCI_MODE_PAUSE:
603 /* Since our wave notification callback takes the lock,
604 * we must release it before resetting the device */
605 LeaveCriticalSection(&wma->cs);
606 dwRet = waveOutReset(wma->hWave);
607 EnterCriticalSection(&wma->cs);
608 /* fall through */
609 default:
610 do /* one more chance for an async thread to finish */
612 LeaveCriticalSection(&wma->cs);
613 Sleep(10);
614 EnterCriticalSection(&wma->cs);
615 } while (wma->dwStatus != MCI_MODE_STOP);
617 break;
619 case MCI_MODE_NOT_READY:
620 break;
623 if ((dwFlags & MCI_NOTIFY) && lpParms) {
624 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
625 wDevID, MCI_NOTIFY_SUCCESSFUL);
627 LeaveCriticalSection(&wma->cs);
628 return dwRet;
631 /***************************************************************************
632 * MCIAVI_mciPause [internal]
634 static DWORD MCIAVI_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
636 WINE_MCIAVI *wma;
638 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
640 wma = MCIAVI_mciGetOpenDev(wDevID);
641 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
642 if (dwFlags & MCI_TEST) return 0;
644 EnterCriticalSection(&wma->cs);
646 if (wma->dwStatus == MCI_MODE_PLAY)
647 wma->dwStatus = MCI_MODE_PAUSE;
649 if (wma->lpWaveFormat) {
650 LeaveCriticalSection(&wma->cs);
651 return waveOutPause(wma->hWave);
654 LeaveCriticalSection(&wma->cs);
655 return 0;
658 /***************************************************************************
659 * MCIAVI_mciResume [internal]
661 static DWORD MCIAVI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
663 WINE_MCIAVI *wma;
665 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
667 wma = MCIAVI_mciGetOpenDev(wDevID);
668 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
669 if (dwFlags & MCI_TEST) return 0;
671 EnterCriticalSection(&wma->cs);
673 if (wma->dwStatus == MCI_MODE_PAUSE)
674 wma->dwStatus = MCI_MODE_PLAY;
676 if (wma->lpWaveFormat) {
677 LeaveCriticalSection(&wma->cs);
678 return waveOutRestart(wma->hWave);
681 LeaveCriticalSection(&wma->cs);
682 return 0;
685 /***************************************************************************
686 * MCIAVI_mciSeek [internal]
688 static DWORD MCIAVI_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
690 WINE_MCIAVI *wma;
691 DWORD position;
693 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
695 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
697 wma = MCIAVI_mciGetOpenDev(wDevID);
698 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
700 position = dwFlags & (MCI_SEEK_TO_START|MCI_SEEK_TO_END|MCI_TO);
701 if (!position) return MCIERR_MISSING_PARAMETER;
702 if (position&(position-1)) return MCIERR_FLAGS_NOT_COMPATIBLE;
704 if (dwFlags & MCI_TO) {
705 position = MCIAVI_ConvertTimeFormatToFrame(wma, lpParms->dwTo);
706 if (position >= wma->dwPlayableVideoFrames)
707 return MCIERR_OUTOFRANGE;
708 } else if (dwFlags & MCI_SEEK_TO_START) {
709 position = 0;
710 } else {
711 position = wma->dwPlayableVideoFrames - 1;
713 if (dwFlags & MCI_TEST) return 0;
715 MCIAVI_mciStop(wDevID, MCI_WAIT, NULL);
717 EnterCriticalSection(&wma->cs);
719 wma->dwCurrVideoFrame = position;
720 TRACE("Seeking to frame=%u\n", wma->dwCurrVideoFrame);
722 if (dwFlags & MCI_NOTIFY) {
723 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
724 wDevID, MCI_NOTIFY_SUCCESSFUL);
726 LeaveCriticalSection(&wma->cs);
727 return 0;
730 /*****************************************************************************
731 * MCIAVI_mciLoad [internal]
733 static DWORD MCIAVI_mciLoad(UINT wDevID, DWORD dwFlags, LPMCI_DGV_LOAD_PARMSW lpParms)
735 WINE_MCIAVI *wma;
737 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
739 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
741 wma = MCIAVI_mciGetOpenDev(wDevID);
742 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
744 return MCIERR_UNSUPPORTED_FUNCTION; /* like w2k */
747 /******************************************************************************
748 * MCIAVI_mciRealize [internal]
750 static DWORD MCIAVI_mciRealize(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
752 WINE_MCIAVI *wma;
754 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
756 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
758 wma = MCIAVI_mciGetOpenDev(wDevID);
759 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
760 if (dwFlags & MCI_TEST) return 0;
762 return 0;
765 /******************************************************************************
766 * MCIAVI_mciUpdate [internal]
768 static DWORD MCIAVI_mciUpdate(UINT wDevID, DWORD dwFlags, LPMCI_DGV_UPDATE_PARMS lpParms)
770 WINE_MCIAVI *wma;
772 TRACE("%04x, %08x, %p\n", wDevID, dwFlags, lpParms);
774 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
776 wma = MCIAVI_mciGetOpenDev(wDevID);
777 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
778 /* Ignore MCI_TEST flag. */
780 EnterCriticalSection(&wma->cs);
782 if (dwFlags & MCI_DGV_UPDATE_HDC)
783 MCIAVI_PaintFrame(wma, lpParms->hDC);
785 LeaveCriticalSection(&wma->cs);
787 return 0;
790 /******************************************************************************
791 * MCIAVI_mciStep [internal]
793 static DWORD MCIAVI_mciStep(UINT wDevID, DWORD dwFlags, LPMCI_DGV_STEP_PARMS lpParms)
795 WINE_MCIAVI *wma;
796 DWORD position;
797 int delta = 1;
799 TRACE("(%04x, %08x, %p)\n", wDevID, dwFlags, lpParms);
801 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
803 wma = MCIAVI_mciGetOpenDev(wDevID);
804 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
806 if (dwFlags & MCI_DGV_STEP_FRAMES) delta = lpParms->dwFrames;
807 if (dwFlags & MCI_DGV_STEP_REVERSE) delta = -delta;
808 position = wma->dwCurrVideoFrame + delta;
809 if (position >= wma->dwPlayableVideoFrames) return MCIERR_OUTOFRANGE;
810 if (dwFlags & MCI_TEST) return 0;
812 MCIAVI_mciStop(wDevID, MCI_WAIT, NULL);
814 EnterCriticalSection(&wma->cs);
816 wma->dwCurrVideoFrame = position;
817 TRACE("Stepping to frame=%u\n", wma->dwCurrVideoFrame);
819 if (dwFlags & MCI_NOTIFY) {
820 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
821 wDevID, MCI_NOTIFY_SUCCESSFUL);
823 LeaveCriticalSection(&wma->cs);
824 return 0;
827 /******************************************************************************
828 * MCIAVI_mciCue [internal]
830 static DWORD MCIAVI_mciCue(UINT wDevID, DWORD dwFlags, LPMCI_DGV_CUE_PARMS lpParms)
832 WINE_MCIAVI *wma;
834 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
836 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
838 wma = MCIAVI_mciGetOpenDev(wDevID);
839 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
840 if (dwFlags & MCI_DGV_CUE_INPUT) return MCIERR_UNSUPPORTED_FUNCTION;
841 if (dwFlags & MCI_TEST) return 0;
843 return 0;
846 /******************************************************************************
847 * MCIAVI_mciSetAudio [internal]
849 static DWORD MCIAVI_mciSetAudio(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SETAUDIO_PARMSW lpParms)
851 WINE_MCIAVI *wma;
853 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
855 FIXME("(%04x, %08x, %p) Item %04x: stub\n", wDevID, dwFlags, lpParms, dwFlags & MCI_DGV_SETAUDIO_ITEM ? lpParms->dwItem : 0);
857 wma = MCIAVI_mciGetOpenDev(wDevID);
858 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
860 return 0;
863 /******************************************************************************
864 * MCIAVI_mciSignal [internal]
866 static DWORD MCIAVI_mciSignal(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SIGNAL_PARMS lpParms)
868 WINE_MCIAVI *wma;
870 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
872 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
874 wma = MCIAVI_mciGetOpenDev(wDevID);
875 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
877 return 0;
880 /******************************************************************************
881 * MCIAVI_mciSetVideo [internal]
883 static DWORD MCIAVI_mciSetVideo(UINT wDevID, DWORD dwFlags, LPMCI_DGV_SETVIDEO_PARMSW lpParms)
885 WINE_MCIAVI *wma;
887 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
889 FIXME("(%04x, %08x, %p) Item %04x: stub\n", wDevID, dwFlags, lpParms, dwFlags & MCI_DGV_SETVIDEO_ITEM ? lpParms->dwItem : 0);
891 wma = MCIAVI_mciGetOpenDev(wDevID);
892 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
894 return 0;
897 /******************************************************************************
898 * MCIAVI_mciConfigure [internal]
900 static DWORD MCIAVI_mciConfigure(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
902 WINE_MCIAVI *wma;
904 FIXME("(%04x, %08x, %p) : stub\n", wDevID, dwFlags, lpParms);
906 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
908 wma = MCIAVI_mciGetOpenDev(wDevID);
909 if (wma == NULL) return MCIERR_INVALID_DEVICE_ID;
910 if (dwFlags & MCI_TEST) return 0;
912 return 0;
915 /*======================================================================*
916 * MCI AVI entry points *
917 *======================================================================*/
919 /**************************************************************************
920 * DriverProc (MCIAVI.@)
922 LRESULT CALLBACK MCIAVI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
923 LPARAM dwParam1, LPARAM dwParam2)
925 TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
926 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
928 switch (wMsg) {
929 case DRV_LOAD: return 1;
930 case DRV_FREE: return 1;
931 case DRV_OPEN: return MCIAVI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
932 case DRV_CLOSE: return MCIAVI_drvClose(dwDevID);
933 case DRV_ENABLE: return 1;
934 case DRV_DISABLE: return 1;
935 case DRV_QUERYCONFIGURE: return 1;
936 case DRV_CONFIGURE: return MCIAVI_drvConfigure(dwDevID);
937 case DRV_INSTALL: return DRVCNF_RESTART;
938 case DRV_REMOVE: return DRVCNF_RESTART;
941 /* session instance */
942 if (dwDevID == 0xFFFFFFFF) return 1;
944 switch (wMsg) {
945 case MCI_OPEN_DRIVER: return MCIAVI_mciOpen (dwDevID, dwParam1, (LPMCI_DGV_OPEN_PARMSW) dwParam2);
946 case MCI_CLOSE_DRIVER: return MCIAVI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
947 case MCI_PLAY: return MCIAVI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
948 case MCI_STOP: return MCIAVI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
949 case MCI_SET: return MCIAVI_mciSet (dwDevID, dwParam1, (LPMCI_DGV_SET_PARMS) dwParam2);
950 case MCI_PAUSE: return MCIAVI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
951 case MCI_RESUME: return MCIAVI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
952 case MCI_STATUS: return MCIAVI_mciStatus (dwDevID, dwParam1, (LPMCI_DGV_STATUS_PARMSW) dwParam2);
953 case MCI_GETDEVCAPS: return MCIAVI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS) dwParam2);
954 case MCI_INFO: return MCIAVI_mciInfo (dwDevID, dwParam1, (LPMCI_DGV_INFO_PARMSW) dwParam2);
955 case MCI_SEEK: return MCIAVI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
956 case MCI_PUT: return MCIAVI_mciPut (dwDevID, dwParam1, (LPMCI_DGV_PUT_PARMS) dwParam2);
957 case MCI_WINDOW: return MCIAVI_mciWindow (dwDevID, dwParam1, (LPMCI_DGV_WINDOW_PARMSW) dwParam2);
958 case MCI_LOAD: return MCIAVI_mciLoad (dwDevID, dwParam1, (LPMCI_DGV_LOAD_PARMSW) dwParam2);
959 case MCI_REALIZE: return MCIAVI_mciRealize (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
960 case MCI_UPDATE: return MCIAVI_mciUpdate (dwDevID, dwParam1, (LPMCI_DGV_UPDATE_PARMS) dwParam2);
961 case MCI_WHERE: return MCIAVI_mciWhere (dwDevID, dwParam1, (LPMCI_DGV_RECT_PARMS) dwParam2);
962 case MCI_STEP: return MCIAVI_mciStep (dwDevID, dwParam1, (LPMCI_DGV_STEP_PARMS) dwParam2);
963 case MCI_CUE: return MCIAVI_mciCue (dwDevID, dwParam1, (LPMCI_DGV_CUE_PARMS) dwParam2);
964 /* Digital Video specific */
965 case MCI_SETAUDIO: return MCIAVI_mciSetAudio (dwDevID, dwParam1, (LPMCI_DGV_SETAUDIO_PARMSW) dwParam2);
966 case MCI_SIGNAL: return MCIAVI_mciSignal (dwDevID, dwParam1, (LPMCI_DGV_SIGNAL_PARMS) dwParam2);
967 case MCI_SETVIDEO: return MCIAVI_mciSetVideo (dwDevID, dwParam1, (LPMCI_DGV_SETVIDEO_PARMSW) dwParam2);
968 case MCI_CONFIGURE: return MCIAVI_mciConfigure (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
970 /* no editing, recording, saving, locking without inputs */
971 case MCI_CAPTURE:
972 case MCI_COPY:
973 case MCI_CUT:
974 case MCI_DELETE:
975 case MCI_FREEZE:
976 case MCI_LIST:
977 case MCI_MONITOR:
978 case MCI_PASTE:
979 case MCI_QUALITY:
980 case MCI_RECORD:
981 case MCI_RESERVE:
982 case MCI_RESTORE:
983 case MCI_SAVE:
984 case MCI_UNDO:
985 case MCI_UNFREEZE:
986 TRACE("Unsupported function [0x%x] flags=%08x\n", wMsg, (DWORD)dwParam1);
987 return MCIERR_UNSUPPORTED_FUNCTION;
988 case MCI_SPIN:
989 case MCI_ESCAPE:
990 WARN("Unsupported command [0x%x] %08x\n", wMsg, (DWORD)dwParam1);
991 break;
992 case MCI_OPEN:
993 case MCI_CLOSE:
994 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
995 break;
996 default:
997 TRACE("Sending msg [%u] to default driver proc\n", wMsg);
998 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1000 return MCIERR_UNRECOGNIZED_COMMAND;