2 * Wine Driver for EsounD Sound Server
3 * http://www.tux.org/~ricdude/EsounD.html
5 * Copyright 1994 Martin Ayotte
6 * 1999 Eric Pouech (async playing in waveOut/waveIn)
7 * 2000 Eric Pouech (loops in waveOut)
8 * 2004 Zhangrong Huang (EsounD version of this file)
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * with esd we cannot stop the audio that is already in
26 * the server's buffer.
29 * pause in waveOut does not work correctly in loop mode
31 * does something need to be done in for WaveIn DirectSound?
50 #ifdef HAVE_SYS_POLL_H
51 # include <sys/poll.h>
58 #include "wine/winuser16.h"
66 #include "wine/debug.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(wave);
70 #ifdef HAVE_SYS_ERRNO_H
71 #include <sys/errno.h>
76 /* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
79 /* define if you want to use esd_monitor_stream instead of
80 * esd_record_stream for waveIn stream
82 /*#define WID_USE_ESDMON*/
84 #define BUFFER_REFILL_THRESHOLD 4
86 #define MAX_WAVEOUTDRV (10)
87 #define MAX_WAVEINDRV (10)
88 #define MAX_CHANNELS 2
90 /* state diagram for waveOut writing:
92 * +---------+-------------+---------------+---------------------------------+
93 * | state | function | event | new state |
94 * +---------+-------------+---------------+---------------------------------+
95 * | | open() | | STOPPED |
96 * | PAUSED | write() | | PAUSED |
97 * | STOPPED | write() | <thrd create> | PLAYING |
98 * | PLAYING | write() | HEADER | PLAYING |
99 * | (other) | write() | <error> | |
100 * | (any) | pause() | PAUSING | PAUSED |
101 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
102 * | (any) | reset() | RESETTING | STOPPED |
103 * | (any) | close() | CLOSING | CLOSED |
104 * +---------+-------------+---------------+---------------------------------+
107 /* states of the playing device */
108 #define WINE_WS_PLAYING 0
109 #define WINE_WS_PAUSED 1
110 #define WINE_WS_STOPPED 2
111 #define WINE_WS_CLOSED 3
113 /* events to be send to device */
114 enum win_wm_message {
115 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
116 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
120 #define SIGNAL_OMR(mr) do { int x = 0; write((mr)->msg_pipe[1], &x, sizeof(x)); } while (0)
121 #define CLEAR_OMR(mr) do { int x = 0; read((mr)->msg_pipe[0], &x, sizeof(x)); } while (0)
122 #define RESET_OMR(mr) do { } while (0)
123 #define WAIT_OMR(mr, sleep) \
124 do { struct pollfd pfd; pfd.fd = (mr)->msg_pipe[0]; \
125 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
127 #define SIGNAL_OMR(mr) do { SetEvent((mr)->msg_event); } while (0)
128 #define CLEAR_OMR(mr) do { } while (0)
129 #define RESET_OMR(mr) do { ResetEvent((mr)->msg_event); } while (0)
130 #define WAIT_OMR(mr, sleep) \
131 do { WaitForSingleObject((mr)->msg_event, sleep); } while (0)
135 enum win_wm_message msg; /* message identifier */
136 DWORD_PTR param; /* parameter for this message */
137 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
140 /* implement an in-process message ring for better performance
141 * (compared to passing thru the server)
142 * this ring will be used by the input (resp output) record (resp playback) routine
144 #define ESD_RING_BUFFER_INCREMENT 64
147 int ring_buffer_size;
155 CRITICAL_SECTION msg_crst;
159 volatile int state; /* one of the WINE_WS_ manifest constants */
160 WAVEOPENDESC waveDesc;
162 WAVEFORMATPCMEX waveFormat;
164 char interface_name[32];
166 DWORD dwSleepTime; /* Num of milliseconds to sleep between filling the dsp buffers */
168 /* esd information */
169 char* stream_name; /* a unique name identifying the esd stream */
170 int stream_fd; /* the socket fd we get from esd when opening a stream for playing */
171 int stream_id; /* the stream id (to change the volume) */
172 int esd_fd; /* a socket to communicate with the sound server */
174 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
175 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
176 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
178 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
179 DWORD dwLoops; /* private copy of loop counter */
181 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
182 DWORD dwWrittenTotal; /* number of bytes written to the audio device since opening */
183 DWORD dwLastWrite; /* Time of last write */
184 DWORD dwLatency; /* Num of milliseconds between when data is sent to the server and when it is played */
186 /* synchronization stuff */
187 HANDLE hStartUpEvent;
189 ESD_MSG_RING msgRing;
193 volatile int state; /* one of the WINE_WS_ manifest constants */
194 WAVEOPENDESC waveDesc;
196 WAVEFORMATPCMEX waveFormat;
198 char interface_name[32];
200 /* esd information */
201 char* stream_name; /* a unique name identifying the esd stream */
202 int stream_fd; /* the socket fd we get from esd when opening a stream for recording */
204 LPWAVEHDR lpQueuePtr;
205 DWORD dwRecordedTotal;
207 /* synchronization stuff */
208 HANDLE hStartUpEvent;
210 ESD_MSG_RING msgRing;
213 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
214 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
216 /* These strings used only for tracing */
217 static const char *wodPlayerCmdString[] = {
219 "WINE_WM_RESTARTING",
230 /*======================================================================*
231 * Low level DSOUND implementation *
232 *======================================================================*/
234 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
236 /* we can't perform memory mapping as we don't have a file stream
237 interface with esd like we do with oss */
238 MESSAGE("This sound card's driver does not support direct access\n");
239 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
240 return MMSYSERR_NOTSUPPORTED;
243 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
245 memset(desc, 0, sizeof(*desc));
246 strcpy(desc->szDesc, "Wine EsounD DirectSound Driver");
247 strcpy(desc->szDrvname, "wineesd.drv");
248 return MMSYSERR_NOERROR;
251 /*======================================================================*
252 * Low level WAVE implementation *
253 *======================================================================*/
255 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
256 WAVEFORMATPCMEX* format)
258 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
259 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
260 format->Format.nChannels, format->Format.nAvgBytesPerSec);
261 TRACE("Position in bytes=%u\n", position);
263 switch (lpTime->wType) {
265 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
266 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
269 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
270 TRACE("TIME_MS=%u\n", lpTime->u.ms);
273 lpTime->u.smpte.fps = 30;
274 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
275 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
276 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
277 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
278 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
279 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
280 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
281 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
282 lpTime->u.smpte.fps = 30;
283 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
284 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
285 lpTime->u.smpte.hour, lpTime->u.smpte.min,
286 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
289 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
290 lpTime->wType = TIME_BYTES;
293 lpTime->u.cb = position;
294 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
297 return MMSYSERR_NOERROR;
300 static BOOL supportedFormat(LPWAVEFORMATEX wf)
304 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
307 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
308 if (wf->nChannels >= 1 && wf->nChannels <= MAX_CHANNELS) {
309 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
312 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
313 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
315 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
316 if (wf->nChannels >=1 && wf->nChannels <= MAX_CHANNELS) {
317 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
318 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
321 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
324 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
326 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
331 static void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
333 ZeroMemory(wf2, sizeof(wf2));
334 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
335 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
336 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
337 memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
339 memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
342 static char* get_stream_name(const char* direction, unsigned int dev_id)
344 char exename[MAX_PATH];
348 GetModuleFileNameA(NULL, exename, sizeof(exename));
349 exename[sizeof(exename)-1]='\0';
350 basename = s = exename;
353 if (*s == '/' || *s == '\\')
358 stream_name = HeapAlloc(GetProcessHeap(), 0, 4+strlen(basename)+10+strlen(direction)+10+1);
359 sprintf(stream_name, "%s (%lu:%s%u)", basename, (unsigned long)getpid(), direction, dev_id);
364 /******************************************************************
365 * ESD_CloseWaveOutDevice
368 static void ESD_CloseWaveOutDevice(WINE_WAVEOUT* wwo)
370 HeapFree(GetProcessHeap(), 0, wwo->stream_name);
371 esd_close(wwo->stream_fd);
374 esd_close(wwo->esd_fd);
377 /******************************************************************
378 * ESD_CloseWaveInDevice
381 static void ESD_CloseWaveInDevice(WINE_WAVEIN* wwi)
383 HeapFree(GetProcessHeap(), 0, wwi->stream_name);
384 esd_close(wwi->stream_fd);
388 static int WAVE_loadcount;
389 /******************************************************************
392 static LONG ESD_WaveClose(void)
396 if (--WAVE_loadcount)
399 /* close all open devices */
400 for(iDevice = 0; iDevice < MAX_WAVEOUTDRV; iDevice++)
402 if(WOutDev[iDevice].stream_fd != -1)
404 ESD_CloseWaveOutDevice(&WOutDev[iDevice]);
408 for(iDevice = 0; iDevice < MAX_WAVEINDRV; iDevice++)
410 if(WInDev[iDevice].stream_fd != -1)
412 ESD_CloseWaveInDevice(&WInDev[iDevice]);
419 /******************************************************************
422 * Initialize internal structures from ESD server info
424 static LONG ESD_WaveInit(void)
430 if (WAVE_loadcount++)
433 /* Testing whether the esd host is alive. */
434 if ((fd = esd_open_sound(NULL)) < 0)
436 WARN("esd_open_sound() failed (%d)\n", errno);
441 /* initialize all device handles to -1 */
442 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
444 static const WCHAR ini[] = {'E','s','o','u','n','D',' ','W','a','v','e','O','u','t','D','r','i','v','e','r',0};
446 WOutDev[i].stream_fd = -1;
447 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out
449 WOutDev[i].caps.wMid = 0x00FF; /* Manufacturer ID */
450 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
451 lstrcpyW(WOutDev[i].caps.szPname, ini);
452 snprintf(WOutDev[i].interface_name, sizeof(WOutDev[i].interface_name), "wineesd: %d", i);
454 WOutDev[i].caps.vDriverVersion = 0x0100;
455 WOutDev[i].caps.dwFormats = 0x00000000;
456 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
458 WOutDev[i].caps.wChannels = 2;
459 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
461 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
462 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
463 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
464 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
465 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
466 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
467 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
468 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
469 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
470 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
471 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
472 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
475 for (i = 0; i < MAX_WAVEINDRV; ++i)
477 static const WCHAR ini[] = {'E','s','o','u','n','D',' ','W','a','v','e','I','n','D','r','i','v','e','r',0};
479 WInDev[i].stream_fd = -1;
480 memset(&WInDev[i].caps, 0, sizeof(WInDev[i].caps)); /* zero out
482 WInDev[i].caps.wMid = 0x00FF;
483 WInDev[i].caps.wPid = 0x0001;
484 lstrcpyW(WInDev[i].caps.szPname, ini);
485 snprintf(WInDev[i].interface_name, sizeof(WInDev[i].interface_name), "wineesd: %d", i);
487 WInDev[i].caps.vDriverVersion = 0x0100;
488 WInDev[i].caps.dwFormats = 0x00000000;
490 WInDev[i].caps.wChannels = 2;
492 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
493 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
494 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
495 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
496 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
497 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
498 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
499 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
500 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
501 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
502 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
503 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
505 WInDev[i].caps.wReserved1 = 0;
510 /******************************************************************
511 * ESD_InitRingMessage
513 * Initialize the ring of messages for passing between driver's caller and playback/record
516 static int ESD_InitRingMessage(ESD_MSG_RING* mr)
521 if (pipe(mr->msg_pipe) < 0) {
522 mr->msg_pipe[0] = -1;
523 mr->msg_pipe[1] = -1;
524 ERR("could not create pipe, error=%s\n", strerror(errno));
527 mr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
529 mr->ring_buffer_size = ESD_RING_BUFFER_INCREMENT;
530 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
531 InitializeCriticalSection(&mr->msg_crst);
532 mr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ESD_MSG_RING.msg_crst");
536 /******************************************************************
537 * ESD_DestroyRingMessage
540 static int ESD_DestroyRingMessage(ESD_MSG_RING* mr)
543 close(mr->msg_pipe[0]);
544 close(mr->msg_pipe[1]);
546 CloseHandle(mr->msg_event);
548 HeapFree(GetProcessHeap(),0,mr->messages);
550 mr->msg_crst.DebugInfo->Spare[0] = 0;
551 DeleteCriticalSection(&mr->msg_crst);
555 /******************************************************************
558 * Inserts a new message into the ring (should be called from DriverProc derived routines)
560 static int ESD_AddRingMessage(ESD_MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
562 HANDLE hEvent = INVALID_HANDLE_VALUE;
564 EnterCriticalSection(&mr->msg_crst);
565 if (mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size))
567 int old_ring_buffer_size = mr->ring_buffer_size;
568 mr->ring_buffer_size += ESD_RING_BUFFER_INCREMENT;
569 TRACE("mr->ring_buffer_size=%d\n",mr->ring_buffer_size);
570 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
571 /* Now we need to rearrange the ring buffer so that the new
572 buffers just allocated are in between mr->msg_tosave and
575 if (mr->msg_tosave < mr->msg_toget)
577 memmove(&(mr->messages[mr->msg_toget + ESD_RING_BUFFER_INCREMENT]),
578 &(mr->messages[mr->msg_toget]),
579 sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
581 mr->msg_toget += ESD_RING_BUFFER_INCREMENT;
586 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
587 if (hEvent == INVALID_HANDLE_VALUE)
589 ERR("can't create event !?\n");
590 LeaveCriticalSection(&mr->msg_crst);
593 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
594 FIXME("two fast messages in the queue!!!!\n");
596 /* fast messages have to be added at the start of the queue */
597 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
599 mr->messages[mr->msg_toget].msg = msg;
600 mr->messages[mr->msg_toget].param = param;
601 mr->messages[mr->msg_toget].hEvent = hEvent;
605 mr->messages[mr->msg_tosave].msg = msg;
606 mr->messages[mr->msg_tosave].param = param;
607 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
608 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
611 LeaveCriticalSection(&mr->msg_crst);
613 /* signal a new message */
617 /* wait for playback/record thread to have processed the message */
618 WaitForSingleObject(hEvent, INFINITE);
625 /******************************************************************
626 * ESD_RetrieveRingMessage
628 * Get a message from the ring. Should be called by the playback/record thread.
630 static int ESD_RetrieveRingMessage(ESD_MSG_RING* mr, enum win_wm_message *msg,
631 DWORD_PTR *param, HANDLE *hEvent)
633 EnterCriticalSection(&mr->msg_crst);
635 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
637 LeaveCriticalSection(&mr->msg_crst);
641 *msg = mr->messages[mr->msg_toget].msg;
642 mr->messages[mr->msg_toget].msg = 0;
643 *param = mr->messages[mr->msg_toget].param;
644 *hEvent = mr->messages[mr->msg_toget].hEvent;
645 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
647 LeaveCriticalSection(&mr->msg_crst);
651 /*======================================================================*
652 * Low level WAVE OUT implementation *
653 *======================================================================*/
655 /**************************************************************************
656 * wodNotifyClient [internal]
658 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD_PTR dwParam1,
661 TRACE("wMsg = 0x%04x dwParm1 = %08lX dwParam2 = %08lX\n", wMsg, dwParam1, dwParam2);
667 if (wwo->wFlags != DCB_NULL &&
668 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
669 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
670 WARN("can't notify client !\n");
671 return MMSYSERR_ERROR;
675 FIXME("Unknown callback message %u\n", wMsg);
676 return MMSYSERR_INVALPARAM;
678 return MMSYSERR_NOERROR;
681 /**************************************************************************
682 * wodUpdatePlayedTotal [internal]
684 * dwPlayedTotal is used for wodPlayer_NotifyCompletions() and
685 * wodGetPosition(), so a byte must only be reported as played once it has
686 * reached the speakers. So give our best estimate based on the latency
687 * reported by the esd server, and on the elapsed time since the last byte
688 * was sent to the server.
690 static void wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
694 if (wwo->dwPlayedTotal == wwo->dwWrittenTotal)
697 /* GetTickCount() wraps every now and then, but these being all unsigned it's ok */
698 elapsed = GetTickCount() - wwo->dwLastWrite;
699 if (elapsed < wwo->dwLatency)
701 wwo->dwPlayedTotal = wwo->dwWrittenTotal - (wwo->dwLatency - elapsed) * wwo->waveFormat.Format.nAvgBytesPerSec / 1000;
702 TRACE("written=%u - elapsed=%u -> played=%u\n", wwo->dwWrittenTotal, elapsed, wwo->dwPlayedTotal);
706 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
707 TRACE("elapsed=%u -> played=written=%u\n", elapsed, wwo->dwPlayedTotal);
711 /**************************************************************************
712 * wodPlayer_BeginWaveHdr [internal]
714 * Makes the specified lpWaveHdr the currently playing wave header.
715 * If the specified wave header is a begin loop and we're not already in
716 * a loop, setup the loop.
718 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
720 wwo->lpPlayPtr = lpWaveHdr;
722 if (!lpWaveHdr) return;
724 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
725 if (wwo->lpLoopPtr) {
726 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
727 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
729 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
730 wwo->lpLoopPtr = lpWaveHdr;
731 /* Windows does not touch WAVEHDR.dwLoops,
732 * so we need to make an internal copy */
733 wwo->dwLoops = lpWaveHdr->dwLoops;
736 wwo->dwPartialOffset = 0;
739 /**************************************************************************
740 * wodPlayer_PlayPtrNext [internal]
742 * Advance the play pointer to the next waveheader, looping if required.
744 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
746 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
748 wwo->dwPartialOffset = 0;
749 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
750 /* We're at the end of a loop, loop if required */
751 if (--wwo->dwLoops > 0) {
752 wwo->lpPlayPtr = wwo->lpLoopPtr;
754 /* Handle overlapping loops correctly */
755 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
756 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
757 /* shall we consider the END flag for the closing loop or for
758 * the opening one or for both ???
759 * code assumes for closing loop only
762 lpWaveHdr = lpWaveHdr->lpNext;
764 wwo->lpLoopPtr = NULL;
765 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
768 /* We're not in a loop. Advance to the next wave header */
769 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
775 /**************************************************************************
776 * wodPlayer_NotifyWait [internal]
777 * Returns the number of milliseconds to wait before attempting to notify
778 * completion of the specified wavehdr.
779 * This is based on the number of bytes remaining to be written in the
782 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
786 if(lpWaveHdr->reserved < wwo->dwPlayedTotal)
792 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
793 if(!dwMillis) dwMillis = 1;
796 TRACE("dwMillis = %d\n", dwMillis);
802 /**************************************************************************
803 * wodPlayer_WriteMaxFrags [internal]
805 * Esdlib will have set the stream socket buffer size to an appropriate
806 * value, so now our job is to keep it full. So write what we can, and
807 * return 1 if more can be written and 0 otherwise.
809 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo)
811 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
815 TRACE("Writing wavehdr %p.%u[%u]\n",
816 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength);
818 written = write(wwo->stream_fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, dwLength);
821 /* the esd buffer is full or some error occurred */
822 TRACE("write(%u) failed, errno=%d\n", dwLength, errno);
825 now = GetTickCount();
826 TRACE("Wrote %d bytes out of %u, %ums since last\n", written, dwLength, now-wwo->dwLastWrite);
828 wwo->dwLastWrite = now;
829 wwo->dwWrittenTotal += written; /* update stats on this wave device */
830 if (written == dwLength)
832 /* We're done with this wavehdr, skip to the next one */
833 wodPlayer_PlayPtrNext(wwo);
837 /* Remove the amount written and wait a bit before trying to write more */
838 wwo->dwPartialOffset += written;
843 /**************************************************************************
844 * wodPlayer_NotifyCompletions [internal]
846 * Notifies and remove from queue all wavehdrs which have been played to
847 * the speaker (ie. they have cleared the audio device). If force is true,
848 * we notify all wavehdrs and remove them all from the queue even if they
849 * are unplayed or part of a loop.
851 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
855 if (wwo->lpQueuePtr) {
856 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), reserved=(%ld), dwWrittenTotal=(%d), force=(%d)\n",
860 wwo->lpQueuePtr->reserved,
864 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), dwWrittenTotal=(%d), force=(%d)\n",
872 /* Start from lpQueuePtr and keep notifying until:
873 * - we hit an unwritten wavehdr
874 * - we hit the beginning of a running loop
875 * - we hit a wavehdr which hasn't finished playing
877 while ((lpWaveHdr = wwo->lpQueuePtr) &&
879 (lpWaveHdr != wwo->lpPlayPtr &&
880 lpWaveHdr != wwo->lpLoopPtr &&
881 lpWaveHdr->reserved <= wwo->dwWrittenTotal))) {
883 wwo->lpQueuePtr = lpWaveHdr->lpNext;
885 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
886 lpWaveHdr->dwFlags |= WHDR_DONE;
888 wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
890 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
891 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
894 /**************************************************************************
895 * wodPlayer_Reset [internal]
897 * wodPlayer helper. Resets current output stream.
899 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
901 wodUpdatePlayedTotal(wwo);
903 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
905 /* we aren't able to flush any data that has already been written */
906 /* to esd, otherwise we would do the flushing here */
909 enum win_wm_message msg;
913 /* remove any buffer */
914 wodPlayer_NotifyCompletions(wwo, TRUE);
916 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
917 wwo->state = WINE_WS_STOPPED;
918 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
920 wwo->dwPartialOffset = 0; /* Clear partial wavehdr */
922 /* remove any existing message in the ring */
923 EnterCriticalSection(&wwo->msgRing.msg_crst);
925 /* return all pending headers in queue */
926 while (ESD_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev))
928 TRACE("flushing msg\n");
929 if (msg != WINE_WM_HEADER)
931 FIXME("shouldn't have headers left\n");
935 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
936 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
938 wodNotifyClient(wwo, WOM_DONE, param, 0);
940 RESET_OMR(&wwo->msgRing);
941 LeaveCriticalSection(&wwo->msgRing.msg_crst);
943 if (wwo->lpLoopPtr) {
944 /* complicated case, not handled yet (could imply modifying the loop counter */
945 FIXME("Pausing while in loop isn't correctly handled yet, expect strange results\n");
946 wwo->lpPlayPtr = wwo->lpLoopPtr;
947 wwo->dwPartialOffset = 0;
948 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
950 /* the data already written is going to be played, so take */
951 /* this fact into account here */
952 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
954 wwo->state = WINE_WS_PAUSED;
958 /**************************************************************************
959 * wodPlayer_ProcessMessages [internal]
961 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
964 enum win_wm_message msg;
968 while (ESD_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev)) {
969 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
971 case WINE_WM_PAUSING:
972 wodPlayer_Reset(wwo, FALSE);
975 case WINE_WM_RESTARTING:
976 wwo->state = WINE_WS_PLAYING;
980 lpWaveHdr = (LPWAVEHDR)param;
982 /* insert buffer at the end of queue */
985 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
989 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
990 if (wwo->state == WINE_WS_STOPPED)
991 wwo->state = WINE_WS_PLAYING;
993 case WINE_WM_RESETTING:
994 wodPlayer_Reset(wwo, TRUE);
998 wodUpdatePlayedTotal(wwo);
1001 case WINE_WM_BREAKLOOP:
1002 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1003 /* ensure exit at end of current loop */
1008 case WINE_WM_CLOSING:
1009 /* sanity check: this should not happen since the device must have been reset before */
1010 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1012 wwo->state = WINE_WS_CLOSED;
1015 /* shouldn't go here */
1017 FIXME("unknown message %d\n", msg);
1023 /**************************************************************************
1024 * wodPlayer_FeedDSP [internal]
1025 * Feed as much sound data as we can into the DSP and return the number of
1026 * milliseconds before it will be necessary to feed the DSP again.
1028 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1030 wodUpdatePlayedTotal(wwo);
1032 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1033 while (wwo->lpPlayPtr)
1035 if (wwo->dwPartialOffset != 0)
1036 TRACE("feeding from partial wavehdr\n");
1039 /* Note the value that dwPlayedTotal will return when this
1040 * wavehdr finishes playing, for the completion notifications.
1042 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1043 TRACE("new wavehdr: reserved=(%ld) dwWrittenTotal=(%d) dwBufferLength=(%d)\n",
1044 wwo->lpPlayPtr->reserved, wwo->dwWrittenTotal,
1045 wwo->lpPlayPtr->dwBufferLength);
1047 if (!wodPlayer_WriteMaxFrags(wwo))
1049 /* the buffer is full, wait a bit */
1050 return wwo->dwSleepTime;
1054 TRACE("Ran out of wavehdrs or nothing to play\n");
1059 /**************************************************************************
1060 * wodPlayer [internal]
1062 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1064 WORD uDevID = (DWORD_PTR)pmt;
1065 WINE_WAVEOUT* wwo = &WOutDev[uDevID];
1066 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1067 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1070 wwo->state = WINE_WS_STOPPED;
1071 SetEvent(wwo->hStartUpEvent);
1074 /** Wait for the shortest time before an action is required. If there
1075 * are no pending actions, wait forever for a command.
1077 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1078 TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1079 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1080 wodPlayer_ProcessMessages(wwo);
1081 if (wwo->state == WINE_WS_PLAYING) {
1082 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1083 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1085 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1092 /**************************************************************************
1093 * wodGetDevCaps [internal]
1095 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1097 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1099 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1101 if (wDevID >= MAX_WAVEOUTDRV) {
1102 TRACE("MAX_WAVOUTDRV reached !\n");
1103 return MMSYSERR_BADDEVICEID;
1106 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1107 return MMSYSERR_NOERROR;
1110 /**************************************************************************
1111 * wodOpen [internal]
1113 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1116 /* output to esound... */
1117 int out_bits = ESD_BITS8, out_channels = ESD_MONO, out_rate;
1118 int out_mode = ESD_STREAM, out_func = ESD_PLAY;
1119 esd_format_t out_format;
1122 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1123 if (lpDesc == NULL) {
1124 WARN("Invalid Parameter !\n");
1125 return MMSYSERR_INVALPARAM;
1127 if (wDevID >= MAX_WAVEOUTDRV) {
1128 TRACE("MAX_WAVOUTDRV reached !\n");
1129 return MMSYSERR_BADDEVICEID;
1132 /* if this device is already open tell the app that it is allocated */
1133 if(WOutDev[wDevID].stream_fd != -1)
1135 TRACE("device already allocated\n");
1136 return MMSYSERR_ALLOCATED;
1139 /* only PCM format is supported so far... */
1140 if (!supportedFormat(lpDesc->lpFormat)) {
1141 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1142 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1143 lpDesc->lpFormat->nSamplesPerSec);
1144 return WAVERR_BADFORMAT;
1147 if (dwFlags & WAVE_FORMAT_QUERY) {
1148 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1149 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1150 lpDesc->lpFormat->nSamplesPerSec);
1151 return MMSYSERR_NOERROR;
1154 wwo = &WOutDev[wDevID];
1156 /* direct sound not supported, ignore the flag */
1157 dwFlags &= ~WAVE_DIRECTSOUND;
1159 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1161 wwo->waveDesc = *lpDesc;
1162 copy_format(lpDesc->lpFormat, &wwo->waveFormat);
1164 if (wwo->waveFormat.Format.wBitsPerSample == 0) {
1165 WARN("Resetting zeroed wBitsPerSample\n");
1166 wwo->waveFormat.Format.wBitsPerSample = 8 *
1167 (wwo->waveFormat.Format.nAvgBytesPerSec /
1168 wwo->waveFormat.Format.nSamplesPerSec) /
1169 wwo->waveFormat.Format.nChannels;
1172 if (wwo->waveFormat.Format.wBitsPerSample == 8)
1173 out_bits = ESD_BITS8;
1174 else if (wwo->waveFormat.Format.wBitsPerSample == 16)
1175 out_bits = ESD_BITS16;
1177 if (wwo->waveFormat.Format.nChannels == 1)
1178 out_channels = ESD_MONO;
1179 else if (wwo->waveFormat.Format.nChannels == 2)
1180 out_channels = ESD_STEREO;
1182 out_format = out_bits | out_channels | out_mode | out_func;
1183 out_rate = (int) wwo->waveFormat.Format.nSamplesPerSec;
1184 TRACE("esd output format = 0x%08x, rate = %d\n", out_format, out_rate);
1186 wwo->stream_name = get_stream_name("out", wDevID);
1187 wwo->stream_fd = esd_play_stream(out_format, out_rate, NULL, wwo->stream_name);
1188 TRACE("wwo->stream_fd=%d\n", wwo->stream_fd);
1189 if(wwo->stream_fd < 0)
1191 HeapFree(GetProcessHeap(), 0, wwo->stream_name);
1192 return MMSYSERR_ALLOCATED;
1196 wwo->dwPlayedTotal = 0;
1197 wwo->dwWrittenTotal = 0;
1199 wwo->esd_fd = esd_open_sound(NULL);
1200 if (wwo->esd_fd >= 0)
1202 wwo->dwLatency = 1000 * esd_get_latency(wwo->esd_fd) * 4 / wwo->waveFormat.Format.nAvgBytesPerSec;
1206 WARN("esd_open_sound() failed\n");
1207 /* just do a rough guess at the latency and continue anyway */
1208 wwo->dwLatency = 1000 * (2 * ESD_BUF_SIZE) / out_rate;
1210 TRACE("dwLatency = %ums\n", wwo->dwLatency);
1212 /* ESD_BUF_SIZE is the socket buffer size in samples. Set dwSleepTime
1213 * to a fraction of that so it never get empty.
1215 wwo->dwSleepTime = 1000 * ESD_BUF_SIZE / out_rate / 3;
1217 /* Set the stream socket to O_NONBLOCK, so we can stop playing smoothly */
1218 mode = fcntl(wwo->stream_fd, F_GETFL);
1220 fcntl(wwo->stream_fd, F_SETFL, mode);
1222 ESD_InitRingMessage(&wwo->msgRing);
1224 /* create player thread */
1225 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1226 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1227 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID,
1229 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1230 CloseHandle(wwo->hStartUpEvent);
1232 wwo->hThread = INVALID_HANDLE_VALUE;
1234 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1236 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
1237 wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
1238 wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
1239 wwo->waveFormat.Format.nBlockAlign);
1241 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1244 /**************************************************************************
1245 * wodClose [internal]
1247 static DWORD wodClose(WORD wDevID)
1249 DWORD ret = MMSYSERR_NOERROR;
1252 TRACE("(%u);\n", wDevID);
1254 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1255 WARN("bad device ID !\n");
1256 return MMSYSERR_BADDEVICEID;
1259 wwo = &WOutDev[wDevID];
1260 if (wwo->lpQueuePtr) {
1261 WARN("buffers still playing !\n");
1262 ret = WAVERR_STILLPLAYING;
1264 TRACE("imhere[3-close]\n");
1265 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1266 ESD_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1269 ESD_DestroyRingMessage(&wwo->msgRing);
1271 ESD_CloseWaveOutDevice(wwo); /* close the stream and clean things up */
1273 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1278 /**************************************************************************
1279 * wodWrite [internal]
1282 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1284 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
1286 /* first, do the sanity checks... */
1287 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1288 WARN("bad dev ID !\n");
1289 return MMSYSERR_BADDEVICEID;
1292 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1294 TRACE("unprepared\n");
1295 return WAVERR_UNPREPARED;
1298 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1300 TRACE("still playing\n");
1301 return WAVERR_STILLPLAYING;
1304 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1305 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1306 lpWaveHdr->lpNext = 0;
1308 TRACE("adding ring message\n");
1309 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER,
1310 (DWORD_PTR)lpWaveHdr, FALSE);
1312 return MMSYSERR_NOERROR;
1315 /**************************************************************************
1316 * wodPause [internal]
1318 static DWORD wodPause(WORD wDevID)
1320 TRACE("(%u);!\n", wDevID);
1322 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1323 WARN("bad device ID !\n");
1324 return MMSYSERR_BADDEVICEID;
1327 TRACE("imhere[3-PAUSING]\n");
1328 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1330 return MMSYSERR_NOERROR;
1333 /**************************************************************************
1334 * wodRestart [internal]
1336 static DWORD wodRestart(WORD wDevID)
1338 TRACE("(%u);\n", wDevID);
1340 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1341 WARN("bad device ID !\n");
1342 return MMSYSERR_BADDEVICEID;
1345 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1346 TRACE("imhere[3-RESTARTING]\n");
1347 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1350 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1351 /* FIXME: Myst crashes with this ... hmm -MM
1352 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1355 return MMSYSERR_NOERROR;
1358 /**************************************************************************
1359 * wodReset [internal]
1361 static DWORD wodReset(WORD wDevID)
1363 TRACE("(%u);\n", wDevID);
1365 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1366 WARN("bad device ID !\n");
1367 return MMSYSERR_BADDEVICEID;
1370 TRACE("imhere[3-RESET]\n");
1371 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1373 return MMSYSERR_NOERROR;
1376 /**************************************************************************
1377 * wodGetPosition [internal]
1379 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1383 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
1385 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1386 WARN("bad device ID !\n");
1387 return MMSYSERR_BADDEVICEID;
1390 if (lpTime == NULL) {
1391 WARN("invalid parameter: lpTime == NULL\n");
1392 return MMSYSERR_INVALPARAM;
1395 wwo = &WOutDev[wDevID];
1396 ESD_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1398 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
1401 /**************************************************************************
1402 * wodBreakLoop [internal]
1404 static DWORD wodBreakLoop(WORD wDevID)
1406 TRACE("(%u);\n", wDevID);
1408 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1409 WARN("bad device ID !\n");
1410 return MMSYSERR_BADDEVICEID;
1412 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1413 return MMSYSERR_NOERROR;
1416 static esd_player_info_t* wod_get_player(WINE_WAVEOUT* wwo, esd_info_t** esd_all_info)
1418 esd_player_info_t* player;
1420 if (wwo->esd_fd == -1)
1422 wwo->esd_fd = esd_open_sound(NULL);
1423 if (wwo->esd_fd < 0)
1425 WARN("esd_open_sound() failed (%d)\n", errno);
1426 *esd_all_info = NULL;
1431 *esd_all_info = esd_get_all_info(wwo->esd_fd);
1434 WARN("esd_get_all_info() failed (%d)\n", errno);
1438 for (player = (*esd_all_info)->player_list; player != NULL; player = player->next)
1440 if (strcmp(player->name, wwo->stream_name) == 0)
1442 wwo->stream_id = player->source_id;
1450 /**************************************************************************
1451 * wodGetVolume [internal]
1453 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1455 esd_info_t* esd_all_info;
1456 esd_player_info_t* player;
1459 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1)
1461 WARN("bad device ID !\n");
1462 return MMSYSERR_BADDEVICEID;
1465 ret = MMSYSERR_ERROR;
1466 player = wod_get_player(WOutDev+wDevID, &esd_all_info);
1470 left = (player->left_vol_scale * 0xFFFF) / ESD_VOLUME_BASE;
1471 right = (player->right_vol_scale * 0xFFFF) / ESD_VOLUME_BASE;
1472 TRACE("volume = %u / %u\n", left, right);
1473 *lpdwVol = left + (right << 16);
1474 ret = MMSYSERR_NOERROR;
1478 esd_free_all_info(esd_all_info);
1482 /**************************************************************************
1483 * wodSetVolume [internal]
1485 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1487 WINE_WAVEOUT* wwo = WOutDev+wDevID;
1489 if (wDevID >= MAX_WAVEOUTDRV || wwo->stream_fd == -1)
1491 WARN("bad device ID !\n");
1492 return MMSYSERR_BADDEVICEID;
1495 /* stream_id is the ESD's file descriptor for our stream so it's should
1496 * be non-zero if set.
1498 if (!wwo->stream_id)
1500 esd_info_t* esd_all_info;
1501 /* wod_get_player sets the stream_id as a side effect */
1502 wod_get_player(wwo, &esd_all_info);
1504 esd_free_all_info(esd_all_info);
1506 if (!wwo->stream_id)
1507 return MMSYSERR_ERROR;
1509 esd_set_stream_pan(wwo->esd_fd, wwo->stream_id,
1510 LOWORD(dwParam) * ESD_VOLUME_BASE / 0xFFFF,
1511 HIWORD(dwParam) * ESD_VOLUME_BASE / 0xFFFF);
1513 return MMSYSERR_NOERROR;
1516 /**************************************************************************
1517 * wodGetNumDevs [internal]
1519 static DWORD wodGetNumDevs(void)
1521 return MAX_WAVEOUTDRV;
1524 /**************************************************************************
1525 * wodDevInterfaceSize [internal]
1527 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1529 TRACE("(%u, %p)\n", wDevID, dwParam1);
1531 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1532 NULL, 0 ) * sizeof(WCHAR);
1533 return MMSYSERR_NOERROR;
1536 /**************************************************************************
1537 * wodDevInterface [internal]
1539 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1541 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1542 NULL, 0 ) * sizeof(WCHAR))
1544 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1545 dwParam1, dwParam2 / sizeof(WCHAR));
1546 return MMSYSERR_NOERROR;
1548 return MMSYSERR_INVALPARAM;
1551 /**************************************************************************
1552 * wodMessage (WINEESD.@)
1554 DWORD WINAPI ESD_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1555 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1557 TRACE("(%u, %04X, %08X, %08lX, %08lX);\n",
1558 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1562 return ESD_WaveInit();
1564 return ESD_WaveClose();
1567 /* FIXME: Pretend this is supported */
1569 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1570 case WODM_CLOSE: return wodClose (wDevID);
1571 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1572 case WODM_PAUSE: return wodPause (wDevID);
1573 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1574 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1575 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
1576 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
1577 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
1578 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1579 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1580 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1581 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1582 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1583 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1584 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1585 case WODM_RESTART: return wodRestart (wDevID);
1586 case WODM_RESET: return wodReset (wDevID);
1588 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1589 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1590 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1591 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1593 FIXME("unknown message %d!\n", wMsg);
1595 return MMSYSERR_NOTSUPPORTED;
1598 /*======================================================================*
1599 * Low level WAVE IN implementation *
1600 *======================================================================*/
1602 /**************************************************************************
1603 * widGetNumDevs [internal]
1605 static DWORD widGetNumDevs(void)
1607 TRACE("%d\n", MAX_WAVEINDRV);
1608 return MAX_WAVEINDRV;
1611 /**************************************************************************
1612 * widDevInterfaceSize [internal]
1614 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1616 TRACE("(%u, %p)\n", wDevID, dwParam1);
1619 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1620 NULL, 0 ) * sizeof(WCHAR);
1621 return MMSYSERR_NOERROR;
1624 /**************************************************************************
1625 * widDevInterface [internal]
1627 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1629 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1630 NULL, 0 ) * sizeof(WCHAR))
1632 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1633 dwParam1, dwParam2 / sizeof(WCHAR));
1634 return MMSYSERR_NOERROR;
1636 return MMSYSERR_INVALPARAM;
1639 /**************************************************************************
1640 * widNotifyClient [internal]
1642 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD_PTR dwParam1,
1645 TRACE("wMsg = 0x%04x dwParm1 = %08lX dwParam2 = %08lX\n", wMsg, dwParam1, dwParam2);
1651 if (wwi->wFlags != DCB_NULL &&
1652 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
1653 (HDRVR)wwi->waveDesc.hWave, wMsg,
1654 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
1655 WARN("can't notify client !\n");
1656 return MMSYSERR_ERROR;
1660 FIXME("Unknown callback message %u\n", wMsg);
1661 return MMSYSERR_INVALPARAM;
1663 return MMSYSERR_NOERROR;
1666 /**************************************************************************
1667 * widGetDevCaps [internal]
1669 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
1671 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1673 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1675 if (wDevID >= MAX_WAVEINDRV) {
1676 TRACE("MAX_WAVINDRV reached !\n");
1677 return MMSYSERR_BADDEVICEID;
1680 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1681 return MMSYSERR_NOERROR;
1684 /**************************************************************************
1685 * widRecorder [internal]
1687 static DWORD CALLBACK widRecorder(LPVOID pmt)
1689 WORD uDevID = (DWORD_PTR)pmt;
1690 WINE_WAVEIN* wwi = &WInDev[uDevID];
1694 enum win_wm_message msg;
1698 SetEvent(wwi->hStartUpEvent);
1700 /* make sleep time to be # of ms to record one packet */
1701 dwSleepTime = (1024 * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
1702 TRACE("sleeptime=%d ms\n", dwSleepTime);
1705 TRACE("wwi->lpQueuePtr=(%p), wwi->state=(%d)\n",wwi->lpQueuePtr,wwi->state);
1707 /* read all data is esd input buffer. */
1708 if ((wwi->lpQueuePtr != NULL) && (wwi->state == WINE_WS_PLAYING))
1710 lpWaveHdr = wwi->lpQueuePtr;
1712 TRACE("read as much as we can\n");
1713 while(wwi->lpQueuePtr)
1715 TRACE("attempt to read %d bytes\n",lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1716 bytesRead = read(wwi->stream_fd,
1717 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1718 lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1719 TRACE("bytesRead=%d\n",bytesRead);
1720 if (bytesRead <= 0) break; /* So we can stop recording smoothly */
1722 lpWaveHdr->dwBytesRecorded += bytesRead;
1723 wwi->dwRecordedTotal += bytesRead;
1725 /* buffer full. notify client */
1726 if (lpWaveHdr->dwBytesRecorded >= lpWaveHdr->dwBufferLength)
1728 /* must copy the value of next waveHdr, because we have no idea of what
1729 * will be done with the content of lpWaveHdr in callback
1731 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1733 TRACE("waveHdr full.\n");
1735 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1736 lpWaveHdr->dwFlags |= WHDR_DONE;
1738 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
1739 lpWaveHdr = wwi->lpQueuePtr = lpNext;
1744 /* wait for dwSleepTime or an event in thread's queue */
1745 WAIT_OMR(&wwi->msgRing, dwSleepTime);
1747 while (ESD_RetrieveRingMessage(&wwi->msgRing, &msg, ¶m, &ev))
1749 TRACE("msg=%s param=0x%lx\n",wodPlayerCmdString[msg - WM_USER - 1], param);
1751 case WINE_WM_PAUSING:
1752 wwi->state = WINE_WS_PAUSED;
1754 /* Put code here to "pause" esd recording
1759 case WINE_WM_STARTING:
1760 wwi->state = WINE_WS_PLAYING;
1762 /* Put code here to "start" esd recording
1767 case WINE_WM_HEADER:
1768 lpWaveHdr = (LPWAVEHDR)param;
1769 /* insert buffer at end of queue */
1772 int num_headers = 0;
1773 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
1781 case WINE_WM_STOPPING:
1782 if (wwi->state != WINE_WS_STOPPED)
1785 /* Put code here to "stop" esd recording
1788 /* return current buffer to app */
1789 lpWaveHdr = wwi->lpQueuePtr;
1792 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1793 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1794 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1795 lpWaveHdr->dwFlags |= WHDR_DONE;
1796 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
1797 wwi->lpQueuePtr = lpNext;
1800 wwi->state = WINE_WS_STOPPED;
1803 case WINE_WM_RESETTING:
1804 wwi->state = WINE_WS_STOPPED;
1805 wwi->dwRecordedTotal = 0;
1807 /* return all buffers to the app */
1808 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
1809 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1810 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1811 lpWaveHdr->dwFlags |= WHDR_DONE;
1813 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
1815 wwi->lpQueuePtr = NULL;
1818 case WINE_WM_CLOSING:
1820 wwi->state = WINE_WS_CLOSED;
1823 /* shouldn't go here */
1825 FIXME("unknown message %d\n", msg);
1831 /* just for not generating compilation warnings... should never be executed */
1835 /**************************************************************************
1836 * widOpen [internal]
1838 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1841 /* input esound... */
1842 int in_bits = ESD_BITS16, in_channels = ESD_STEREO, in_rate;
1843 #ifdef WID_USE_ESDMON
1844 int in_mode = ESD_STREAM, in_func = ESD_PLAY;
1846 int in_mode = ESD_STREAM, in_func = ESD_RECORD;
1848 esd_format_t in_format;
1851 TRACE("(%u, %p %08X);\n",wDevID, lpDesc, dwFlags);
1852 if (lpDesc == NULL) {
1853 WARN("Invalid Parametr (lpDesc == NULL)!\n");
1854 return MMSYSERR_INVALPARAM;
1857 if (wDevID >= MAX_WAVEINDRV) {
1858 TRACE ("MAX_WAVEINDRV reached !\n");
1859 return MMSYSERR_BADDEVICEID;
1862 /* if this device is already open tell the app that it is allocated */
1863 if(WInDev[wDevID].stream_fd != -1)
1865 TRACE("device already allocated\n");
1866 return MMSYSERR_ALLOCATED;
1869 /* only PCM format is support so far... */
1870 if (!supportedFormat(lpDesc->lpFormat)) {
1871 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1872 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1873 lpDesc->lpFormat->nSamplesPerSec);
1874 return WAVERR_BADFORMAT;
1877 if (dwFlags & WAVE_FORMAT_QUERY) {
1878 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1879 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1880 lpDesc->lpFormat->nSamplesPerSec);
1881 return MMSYSERR_NOERROR;
1884 wwi = &WInDev[wDevID];
1886 /* direct sound not supported, ignore the flag */
1887 dwFlags &= ~WAVE_DIRECTSOUND;
1889 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1891 wwi->waveDesc = *lpDesc;
1892 copy_format(lpDesc->lpFormat, &wwi->waveFormat);
1894 if (wwi->waveFormat.Format.wBitsPerSample == 0) {
1895 WARN("Resetting zerod wBitsPerSample\n");
1896 wwi->waveFormat.Format.wBitsPerSample = 8 *
1897 (wwi->waveFormat.Format.nAvgBytesPerSec /
1898 wwi->waveFormat.Format.nSamplesPerSec) /
1899 wwi->waveFormat.Format.nChannels;
1902 if (wwi->waveFormat.Format.wBitsPerSample == 8)
1903 in_bits = ESD_BITS8;
1904 else if (wwi->waveFormat.Format.wBitsPerSample == 16)
1905 in_bits = ESD_BITS16;
1907 if (wwi->waveFormat.Format.nChannels == 1)
1908 in_channels = ESD_MONO;
1909 else if (wwi->waveFormat.Format.nChannels == 2)
1910 in_channels = ESD_STEREO;
1912 in_format = in_bits | in_channels | in_mode | in_func;
1913 in_rate = (int) wwi->waveFormat.Format.nSamplesPerSec;
1914 TRACE("esd input format = 0x%08x, rate = %d\n", in_format, in_rate);
1916 wwi->stream_name = get_stream_name("in", wDevID);
1917 #ifdef WID_USE_ESDMON
1918 wwi->stream_fd = esd_monitor_stream(in_format, in_rate, NULL, wwi->stream_name);
1920 wwi->stream_fd = esd_record_stream(in_format, in_rate, NULL, wwi->stream_name);
1922 TRACE("wwi->stream_fd=%d\n",wwi->stream_fd);
1923 if(wwi->stream_fd < 0)
1925 HeapFree(GetProcessHeap(), 0, wwi->stream_name);
1926 return MMSYSERR_ALLOCATED;
1928 wwi->state = WINE_WS_STOPPED;
1930 if (wwi->lpQueuePtr) {
1931 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
1932 wwi->lpQueuePtr = NULL;
1935 /* Set the socket to O_NONBLOCK, so we can stop recording smoothly */
1936 mode = fcntl(wwi->stream_fd, F_GETFL);
1938 fcntl(wwi->stream_fd, F_SETFL, mode);
1940 wwi->dwRecordedTotal = 0;
1941 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1943 ESD_InitRingMessage(&wwi->msgRing);
1945 /* create recorder thread */
1946 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1947 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1948 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD_PTR)wDevID,
1950 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
1951 CloseHandle(wwi->hStartUpEvent);
1953 wwi->hThread = INVALID_HANDLE_VALUE;
1955 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
1957 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
1958 wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
1959 wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
1960 wwi->waveFormat.Format.nBlockAlign);
1961 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
1964 /**************************************************************************
1965 * widClose [internal]
1967 static DWORD widClose(WORD wDevID)
1971 TRACE("(%u);\n", wDevID);
1972 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1973 WARN("can't close !\n");
1974 return MMSYSERR_INVALHANDLE;
1977 wwi = &WInDev[wDevID];
1979 if (wwi->lpQueuePtr != NULL) {
1980 WARN("still buffers open !\n");
1981 return WAVERR_STILLPLAYING;
1984 ESD_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
1985 ESD_CloseWaveInDevice(wwi);
1986 wwi->state = WINE_WS_CLOSED;
1987 ESD_DestroyRingMessage(&wwi->msgRing);
1988 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
1991 /**************************************************************************
1992 * widAddBuffer [internal]
1994 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1996 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
1998 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1999 WARN("can't do it !\n");
2000 return MMSYSERR_INVALHANDLE;
2002 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2003 TRACE("never been prepared !\n");
2004 return WAVERR_UNPREPARED;
2006 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2007 TRACE("header already in use !\n");
2008 return WAVERR_STILLPLAYING;
2011 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2012 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2013 lpWaveHdr->dwBytesRecorded = 0;
2014 lpWaveHdr->lpNext = NULL;
2016 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER,
2017 (DWORD_PTR)lpWaveHdr, FALSE);
2018 return MMSYSERR_NOERROR;
2021 /**************************************************************************
2022 * widStart [internal]
2024 static DWORD widStart(WORD wDevID)
2026 TRACE("(%u);\n", wDevID);
2027 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2028 WARN("can't start recording !\n");
2029 return MMSYSERR_INVALHANDLE;
2032 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
2033 return MMSYSERR_NOERROR;
2036 /**************************************************************************
2037 * widStop [internal]
2039 static DWORD widStop(WORD wDevID)
2041 TRACE("(%u);\n", wDevID);
2042 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2043 WARN("can't stop !\n");
2044 return MMSYSERR_INVALHANDLE;
2047 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
2049 return MMSYSERR_NOERROR;
2052 /**************************************************************************
2053 * widReset [internal]
2055 static DWORD widReset(WORD wDevID)
2057 TRACE("(%u);\n", wDevID);
2058 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2059 WARN("can't reset !\n");
2060 return MMSYSERR_INVALHANDLE;
2062 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2063 return MMSYSERR_NOERROR;
2066 /**************************************************************************
2067 * widMessage (WINEESD.6)
2069 DWORD WINAPI ESD_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2070 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2072 TRACE("(%u, %04X, %08X, %08lX, %08lX);\n",
2073 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2076 return ESD_WaveInit();
2078 return ESD_WaveClose();
2081 /* FIXME: Pretend this is supported */
2083 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2084 case WIDM_CLOSE: return widClose (wDevID);
2085 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2086 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2087 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2088 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
2089 case WIDM_GETNUMDEVS: return widGetNumDevs ();
2090 case WIDM_RESET: return widReset (wDevID);
2091 case WIDM_START: return widStart (wDevID);
2092 case WIDM_STOP: return widStop (wDevID);
2093 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2094 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2096 FIXME("unknown message %d!\n", wMsg);
2098 return MMSYSERR_NOTSUPPORTED;
2101 /**************************************************************************
2102 * DriverProc (WINEESD.@)
2104 LRESULT CALLBACK ESD_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
2105 LPARAM dwParam1, LPARAM dwParam2)
2116 case DRV_QUERYCONFIGURE:
2118 case DRV_CONFIGURE: MessageBoxA(0, "EsounD MultiMedia Driver!", "EsounD Driver", MB_OK); return 1;