wineesd: Update ESD driver to closer match OSS and ALSA drivers.
[wine.git] / dlls / winmm / wineesd / audio.c
blob70ddf17c27c32bfbb01a204e09f4b77d35001554
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* NOTE:
25 * with esd we cannot stop the audio that is already in
26 * the servers buffer.
28 * FIXME:
29 * pause in waveOut does not work correctly in loop mode
31 * does something need to be done in for WaveIn DirectSound?
35 #include "config.h"
37 #include <errno.h>
38 #include <math.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #include <fcntl.h>
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wingdi.h"
50 #include "winerror.h"
51 #include "wine/winuser16.h"
52 #include "mmddk.h"
53 #include "mmreg.h"
54 #include "dsound.h"
55 #include "dsdriver.h"
56 #include "ks.h"
57 #include "ksguid.h"
58 #include "ksmedia.h"
59 #include "esound.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(wave);
64 #ifdef HAVE_ESD
66 #include <esd.h>
68 /* define if you want to use esd_monitor_stream instead of
69 * esd_record_stream for waveIn stream
71 /*#define WID_USE_ESDMON*/
73 #define BUFFER_REFILL_THRESHOLD 4
75 #define MAX_WAVEOUTDRV (10)
76 #define MAX_WAVEINDRV (10)
77 #define MAX_CHANNELS 2
79 /* state diagram for waveOut writing:
81 * +---------+-------------+---------------+---------------------------------+
82 * | state | function | event | new state |
83 * +---------+-------------+---------------+---------------------------------+
84 * | | open() | | STOPPED |
85 * | PAUSED | write() | | PAUSED |
86 * | STOPPED | write() | <thrd create> | PLAYING |
87 * | PLAYING | write() | HEADER | PLAYING |
88 * | (other) | write() | <error> | |
89 * | (any) | pause() | PAUSING | PAUSED |
90 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
91 * | (any) | reset() | RESETTING | STOPPED |
92 * | (any) | close() | CLOSING | CLOSED |
93 * +---------+-------------+---------------+---------------------------------+
96 /* states of the playing device */
97 #define WINE_WS_PLAYING 0
98 #define WINE_WS_PAUSED 1
99 #define WINE_WS_STOPPED 2
100 #define WINE_WS_CLOSED 3
102 /* events to be send to device */
103 enum win_wm_message {
104 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
105 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
108 typedef struct {
109 enum win_wm_message msg; /* message identifier */
110 DWORD param; /* parameter for this message */
111 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
112 } RING_MSG;
114 /* implement an in-process message ring for better performance
115 * (compared to passing thru the server)
116 * this ring will be used by the input (resp output) record (resp playback) routine
118 #define ESD_RING_BUFFER_INCREMENT 64
119 typedef struct {
120 RING_MSG * messages;
121 int ring_buffer_size;
122 int msg_tosave;
123 int msg_toget;
124 HANDLE msg_event;
125 CRITICAL_SECTION msg_crst;
126 } ESD_MSG_RING;
128 typedef struct {
129 volatile int state; /* one of the WINE_WS_ manifest constants */
130 WAVEOPENDESC waveDesc;
131 WORD wFlags;
132 WAVEFORMATPCMEX waveFormat;
133 WAVEOUTCAPSW caps;
134 char interface_name[32];
136 DWORD dwSleepTime; /* Num of milliseconds to sleep between filling the dsp buffers */
138 /* esd information */
139 int esd_fd; /* the socket fd we get from esd when opening a stream for playing */
140 int bytes_per_frame;
141 DWORD dwBufferSize; /* size of whole buffer in bytes */
143 char* sound_buffer;
144 long buffer_size;
146 DWORD volume_left; /* volume control information */
147 DWORD volume_right;
149 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
150 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
151 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
153 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
154 DWORD dwLoops; /* private copy of loop counter */
156 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
157 DWORD dwWrittenTotal; /* number of bytes written to the audio device since opening */
159 /* synchronization stuff */
160 HANDLE hStartUpEvent;
161 HANDLE hThread;
162 DWORD dwThreadID;
163 ESD_MSG_RING msgRing;
164 } WINE_WAVEOUT;
166 typedef struct {
167 volatile int state; /* one of the WINE_WS_ manifest constants */
168 WAVEOPENDESC waveDesc;
169 WORD wFlags;
170 WAVEFORMATPCMEX waveFormat;
171 WAVEINCAPSW caps;
172 char interface_name[32];
174 /* esd information */
175 int esd_fd; /* the socket fd we get from esd when opening a stream for recording */
176 int bytes_per_frame;
178 LPWAVEHDR lpQueuePtr;
179 DWORD dwRecordedTotal;
181 /* synchronization stuff */
182 HANDLE hStartUpEvent;
183 HANDLE hThread;
184 DWORD dwThreadID;
185 ESD_MSG_RING msgRing;
186 } WINE_WAVEIN;
188 static char* esd_host; /* the esd host */
190 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
191 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
193 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
194 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
196 /* These strings used only for tracing */
197 static const char *wodPlayerCmdString[] = {
198 "WINE_WM_PAUSING",
199 "WINE_WM_RESTARTING",
200 "WINE_WM_RESETTING",
201 "WINE_WM_HEADER",
202 "WINE_WM_UPDATE",
203 "WINE_WM_BREAKLOOP",
204 "WINE_WM_CLOSING",
205 "WINE_WM_STARTING",
206 "WINE_WM_STOPPING",
209 /*======================================================================*
210 * Low level WAVE implementation *
211 *======================================================================*/
213 /* Volume functions derived from Alsaplayer source */
214 /* length is the number of 16 bit samples */
215 void volume_effect16(void *bufin, void* bufout, int length, int left,
216 int right, int nChannels)
218 short *d_out = (short *)bufout;
219 short *d_in = (short *)bufin;
220 int i, v;
223 TRACE("length == %d, nChannels == %d\n", length, nChannels);
226 if (right == -1) right = left;
228 for(i = 0; i < length; i+=(nChannels))
230 v = (int) ((*(d_in++) * left) / 100);
231 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
232 if(nChannels == 2)
234 v = (int) ((*(d_in++) * right) / 100);
235 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
240 /* length is the number of 8 bit samples */
241 void volume_effect8(void *bufin, void* bufout, int length, int left,
242 int right, int nChannels)
244 BYTE *d_out = (BYTE *)bufout;
245 BYTE *d_in = (BYTE *)bufin;
246 int i, v;
249 TRACE("length == %d, nChannels == %d\n", length, nChannels);
252 if (right == -1) right = left;
254 for(i = 0; i < length; i+=(nChannels))
256 v = (BYTE) ((*(d_in++) * left) / 100);
257 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
258 if(nChannels == 2)
260 v = (BYTE) ((*(d_in++) * right) / 100);
261 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
266 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
267 WAVEFORMATPCMEX* format)
269 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
270 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
271 format->Format.nChannels, format->Format.nAvgBytesPerSec);
272 TRACE("Position in bytes=%lu\n", position);
274 switch (lpTime->wType) {
275 case TIME_SAMPLES:
276 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
277 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
278 break;
279 case TIME_MS:
280 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
281 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
282 break;
283 case TIME_SMPTE:
284 lpTime->u.smpte.fps = 30;
285 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
286 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
287 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
288 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
289 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
290 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
291 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
292 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
293 lpTime->u.smpte.fps = 30;
294 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
295 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
296 lpTime->u.smpte.hour, lpTime->u.smpte.min,
297 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
298 break;
299 default:
300 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
301 lpTime->wType = TIME_BYTES;
302 /* fall through */
303 case TIME_BYTES:
304 lpTime->u.cb = position;
305 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
306 break;
308 return MMSYSERR_NOERROR;
311 static BOOL supportedFormat(LPWAVEFORMATEX wf)
313 TRACE("(%p)\n",wf);
315 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
316 return FALSE;
318 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
319 if (wf->nChannels >= 1 && wf->nChannels <= MAX_CHANNELS) {
320 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
321 return TRUE;
323 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
324 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
326 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
327 if (wf->nChannels >=1 && wf->nChannels <= MAX_CHANNELS) {
328 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
329 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
330 return TRUE;
331 } else
332 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
334 } else
335 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
336 } else
337 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
339 return FALSE;
342 void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
344 ZeroMemory(wf2, sizeof(wf2));
345 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
346 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
347 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
348 memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
349 else
350 memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
353 /******************************************************************
354 * ESD_CloseWaveOutDevice
357 void ESD_CloseWaveOutDevice(WINE_WAVEOUT* wwo)
359 esd_close(wwo->esd_fd); /* close the esd socket fd */
360 wwo->esd_fd = -1;
362 /* free up the buffer we use for volume and reset the size */
363 if(wwo->sound_buffer)
365 HeapFree(GetProcessHeap(), 0, wwo->sound_buffer);
366 wwo->sound_buffer = NULL;
369 wwo->buffer_size = 0;
372 /******************************************************************
373 * ESD_CloseWaveInDevice
376 void ESD_CloseWaveInDevice(WINE_WAVEIN* wwi)
378 esd_close(wwi->esd_fd); /* close the esd socket fd */
379 wwi->esd_fd = -1;
382 /******************************************************************
383 * ESD_WaveClose
385 LONG ESD_WaveClose(void)
387 int iDevice;
389 /* close all open devices */
390 for(iDevice = 0; iDevice < MAX_WAVEOUTDRV; iDevice++)
392 if(WOutDev[iDevice].esd_fd != -1)
394 ESD_CloseWaveOutDevice(&WOutDev[iDevice]);
398 for(iDevice = 0; iDevice < MAX_WAVEINDRV; iDevice++)
400 if(WInDev[iDevice].esd_fd != -1)
402 ESD_CloseWaveInDevice(&WInDev[iDevice]);
406 return 1;
409 /******************************************************************
410 * ESD_WaveInit
412 * Initialize internal structures from ESD server info
414 LONG ESD_WaveInit(void)
416 int i;
417 int fd;
419 TRACE("called\n");
421 /* FIXME: Maybe usefully to set the esd host. */
422 esd_host = NULL;
424 /* Testing whether the esd host is alive. */
425 if ((fd = esd_open_sound(esd_host)) < 0)
427 WARN("esd_open_sound() failed (%d)\n", errno);
428 return -1;
430 esd_close(fd);
432 /* initialize all device handles to -1 */
433 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
435 static const WCHAR ini[] = {'E','s','o','u','n','D',' ','W','a','v','e','O','u','t','D','r','i','v','e','r',0};
437 WOutDev[i].esd_fd = -1;
438 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out
439 caps values */
440 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
441 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
442 lstrcpyW(WOutDev[i].caps.szPname, ini);
443 snprintf(WOutDev[i].interface_name, sizeof(WOutDev[i].interface_name), "wineesd: %d", i);
445 WOutDev[i].caps.vDriverVersion = 0x0100;
446 WOutDev[i].caps.dwFormats = 0x00000000;
447 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
449 WOutDev[i].caps.wChannels = 2;
450 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
452 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
453 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
454 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
455 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
456 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
457 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
458 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
459 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
460 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
461 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
462 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
463 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
466 for (i = 0; i < MAX_WAVEINDRV; ++i)
468 static const WCHAR ini[] = {'E','s','o','u','n','D',' ','W','a','v','e','I','n','D','r','i','v','e','r',0};
470 WInDev[i].esd_fd = -1;
471 memset(&WInDev[i].caps, 0, sizeof(WInDev[i].caps)); /* zero out
472 caps values */
473 WInDev[i].caps.wMid = 0x00FF;
474 WInDev[i].caps.wPid = 0x0001;
475 lstrcpyW(WInDev[i].caps.szPname, ini);
476 snprintf(WInDev[i].interface_name, sizeof(WInDev[i].interface_name), "wineesd: %d", i);
478 WInDev[i].caps.vDriverVersion = 0x0100;
479 WInDev[i].caps.dwFormats = 0x00000000;
481 WInDev[i].caps.wChannels = 2;
483 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
484 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
485 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
486 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
487 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
488 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
489 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
490 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
491 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
492 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
493 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
494 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
496 WInDev[i].caps.wReserved1 = 0;
498 return 0;
501 /******************************************************************
502 * ESD_InitRingMessage
504 * Initialize the ring of messages for passing between driver's caller and playback/record
505 * thread
507 static int ESD_InitRingMessage(ESD_MSG_RING* mr)
509 mr->msg_toget = 0;
510 mr->msg_tosave = 0;
511 mr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
512 mr->ring_buffer_size = ESD_RING_BUFFER_INCREMENT;
513 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
514 InitializeCriticalSection(&mr->msg_crst);
515 mr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)"WINEESD_msg_crst";
516 return 0;
519 /******************************************************************
520 * ESD_DestroyRingMessage
523 static int ESD_DestroyRingMessage(ESD_MSG_RING* mr)
525 CloseHandle(mr->msg_event);
526 HeapFree(GetProcessHeap(),0,mr->messages);
527 mr->messages=NULL;
528 DeleteCriticalSection(&mr->msg_crst);
529 return 0;
532 /******************************************************************
533 * ESD_AddRingMessage
535 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
537 static int ESD_AddRingMessage(ESD_MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
539 HANDLE hEvent = INVALID_HANDLE_VALUE;
541 EnterCriticalSection(&mr->msg_crst);
542 if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
544 int old_ring_buffer_size = mr->ring_buffer_size;
545 mr->ring_buffer_size += ESD_RING_BUFFER_INCREMENT;
546 TRACE("mr->ring_buffer_size=%d\n",mr->ring_buffer_size);
547 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
548 /* Now we need to rearrange the ring buffer so that the new
549 buffers just allocated are in between mr->msg_tosave and
550 mr->msg_toget.
552 if (mr->msg_tosave < mr->msg_toget)
554 memmove(&(mr->messages[mr->msg_toget + ESD_RING_BUFFER_INCREMENT]),
555 &(mr->messages[mr->msg_toget]),
556 sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
558 mr->msg_toget += ESD_RING_BUFFER_INCREMENT;
561 if (wait)
563 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
564 if (hEvent == INVALID_HANDLE_VALUE)
566 ERR("can't create event !?\n");
567 LeaveCriticalSection(&mr->msg_crst);
568 return 0;
570 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
571 FIXME("two fast messages in the queue!!!!\n");
573 /* fast messages have to be added at the start of the queue */
574 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
576 mr->messages[mr->msg_toget].msg = msg;
577 mr->messages[mr->msg_toget].param = param;
578 mr->messages[mr->msg_toget].hEvent = hEvent;
580 else
582 mr->messages[mr->msg_tosave].msg = msg;
583 mr->messages[mr->msg_tosave].param = param;
584 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
585 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
588 LeaveCriticalSection(&mr->msg_crst);
590 SetEvent(mr->msg_event); /* signal a new message */
592 if (wait)
594 /* wait for playback/record thread to have processed the message */
595 WaitForSingleObject(hEvent, INFINITE);
596 CloseHandle(hEvent);
599 return 1;
602 /******************************************************************
603 * ESD_RetrieveRingMessage
605 * Get a message from the ring. Should be called by the playback/record thread.
607 static int ESD_RetrieveRingMessage(ESD_MSG_RING* mr,
608 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
610 EnterCriticalSection(&mr->msg_crst);
612 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
614 LeaveCriticalSection(&mr->msg_crst);
615 return 0;
618 *msg = mr->messages[mr->msg_toget].msg;
619 mr->messages[mr->msg_toget].msg = 0;
620 *param = mr->messages[mr->msg_toget].param;
621 *hEvent = mr->messages[mr->msg_toget].hEvent;
622 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
623 LeaveCriticalSection(&mr->msg_crst);
624 return 1;
627 /*======================================================================*
628 * Low level WAVE OUT implementation *
629 *======================================================================*/
631 /**************************************************************************
632 * wodNotifyClient [internal]
634 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
636 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
638 switch (wMsg) {
639 case WOM_OPEN:
640 case WOM_CLOSE:
641 case WOM_DONE:
642 if (wwo->wFlags != DCB_NULL &&
643 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
644 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
645 WARN("can't notify client !\n");
646 return MMSYSERR_ERROR;
648 break;
649 default:
650 FIXME("Unknown callback message %u\n", wMsg);
651 return MMSYSERR_INVALPARAM;
653 return MMSYSERR_NOERROR;
656 /**************************************************************************
657 * wodUpdatePlayedTotal [internal]
660 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
662 /* total played is the bytes written less the bytes to write ;-) */
663 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
665 return TRUE;
668 /**************************************************************************
669 * wodPlayer_BeginWaveHdr [internal]
671 * Makes the specified lpWaveHdr the currently playing wave header.
672 * If the specified wave header is a begin loop and we're not already in
673 * a loop, setup the loop.
675 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
677 wwo->lpPlayPtr = lpWaveHdr;
679 if (!lpWaveHdr) return;
681 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
682 if (wwo->lpLoopPtr) {
683 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
684 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
685 } else {
686 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
687 wwo->lpLoopPtr = lpWaveHdr;
688 /* Windows does not touch WAVEHDR.dwLoops,
689 * so we need to make an internal copy */
690 wwo->dwLoops = lpWaveHdr->dwLoops;
693 wwo->dwPartialOffset = 0;
696 /**************************************************************************
697 * wodPlayer_PlayPtrNext [internal]
699 * Advance the play pointer to the next waveheader, looping if required.
701 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
703 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
705 wwo->dwPartialOffset = 0;
706 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
707 /* We're at the end of a loop, loop if required */
708 if (--wwo->dwLoops > 0) {
709 wwo->lpPlayPtr = wwo->lpLoopPtr;
710 } else {
711 /* Handle overlapping loops correctly */
712 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
713 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
714 /* shall we consider the END flag for the closing loop or for
715 * the opening one or for both ???
716 * code assumes for closing loop only
718 } else {
719 lpWaveHdr = lpWaveHdr->lpNext;
721 wwo->lpLoopPtr = NULL;
722 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
724 } else {
725 /* We're not in a loop. Advance to the next wave header */
726 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
729 return lpWaveHdr;
732 /**************************************************************************
733 * wodPlayer_NotifyWait [internal]
734 * Returns the number of milliseconds to wait before attempting to notify
735 * completion of the specified wavehdr.
736 * This is based on the number of bytes remaining to be written in the
737 * wave.
739 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
741 DWORD dwMillis;
743 if(lpWaveHdr->reserved < wwo->dwPlayedTotal)
745 dwMillis = 1;
747 else
749 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
750 if(!dwMillis) dwMillis = 1;
753 TRACE("dwMillis = %ld\n", dwMillis);
755 return dwMillis;
759 /**************************************************************************
760 * wodPlayer_WriteMaxFrags [internal]
761 * Writes the maximum number of bytes possible to the DSP and returns
762 * the number of bytes written.
764 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
766 /* Only attempt to write to free bytes */
767 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
768 int toWrite = min(dwLength, *bytes);
769 int written;
771 TRACE("Writing wavehdr %p.%lu[%lu]\n",
772 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength);
774 /* see if our buffer isn't large enough for the data we are writing */
775 if(wwo->buffer_size < toWrite)
777 if(wwo->sound_buffer)
779 wwo->sound_buffer = HeapReAlloc(GetProcessHeap(), 0, wwo->sound_buffer, toWrite);
780 wwo->buffer_size = toWrite;
784 /* if we don't have a buffer then get one */
785 if(!wwo->sound_buffer)
787 /* allocate some memory for the buffer */
788 wwo->sound_buffer = HeapAlloc(GetProcessHeap(), 0, toWrite);
789 wwo->buffer_size = toWrite;
792 /* if we don't have a buffer then error out */
793 if(!wwo->sound_buffer)
795 ERR("error allocating sound_buffer memory\n");
796 return 0;
799 TRACE("toWrite == %d\n", toWrite);
801 /* apply volume to the bits */
802 /* for single channel audio streams we only use the LEFT volume */
803 if(wwo->waveFormat.Format.wBitsPerSample == 16)
805 /* apply volume to the buffer we are about to send */
806 /* divide toWrite(bytes) by 2 as volume processes by 16 bits */
807 volume_effect16(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
808 wwo->sound_buffer, toWrite>>1, wwo->volume_left,
809 wwo->volume_right, wwo->waveFormat.Format.nChannels);
810 } else if(wwo->waveFormat.Format.wBitsPerSample == 8)
812 /* apply volume to the buffer we are about to send */
813 volume_effect8(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
814 wwo->sound_buffer, toWrite, wwo->volume_left,
815 wwo->volume_right, wwo->waveFormat.Format.nChannels);
816 } else
818 FIXME("unsupported wwo->format.wBitsPerSample of %d\n",
819 wwo->waveFormat.Format.wBitsPerSample);
822 /* send the audio data to esd for playing */
823 written = write(wwo->esd_fd, wwo->sound_buffer, toWrite);
825 TRACE("written = %d\n", written);
827 if (written <= 0)
829 *bytes = 0; /* apparently esd is actually full */
830 return written; /* if we wrote nothing just return */
833 if (written >= dwLength)
834 wodPlayer_PlayPtrNext(wwo); /* If we wrote all current wavehdr, skip to the next one */
835 else
836 wwo->dwPartialOffset += written; /* Remove the amount written */
838 if (written < toWrite)
839 *bytes = 0;
840 else
841 *bytes -= written;
843 wwo->dwWrittenTotal += written; /* update stats on this wave device */
845 return written; /* return the number of bytes written */
849 /**************************************************************************
850 * wodPlayer_NotifyCompletions [internal]
852 * Notifies and remove from queue all wavehdrs which have been played to
853 * the speaker (ie. they have cleared the audio device). If force is true,
854 * we notify all wavehdrs and remove them all from the queue even if they
855 * are unplayed or part of a loop.
857 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
859 LPWAVEHDR lpWaveHdr;
861 if (wwo->lpQueuePtr) {
862 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), reserved=(%ld), dwWrittenTotal=(%ld), force=(%d)\n",
863 wwo->lpQueuePtr,
864 wwo->lpPlayPtr,
865 wwo->lpLoopPtr,
866 wwo->lpQueuePtr->reserved,
867 wwo->dwWrittenTotal,
868 force);
869 } else {
870 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), dwWrittenTotal=(%ld), force=(%d)\n",
871 wwo->lpQueuePtr,
872 wwo->lpPlayPtr,
873 wwo->lpLoopPtr,
874 wwo->dwWrittenTotal,
875 force);
878 /* Start from lpQueuePtr and keep notifying until:
879 * - we hit an unwritten wavehdr
880 * - we hit the beginning of a running loop
881 * - we hit a wavehdr which hasn't finished playing
883 while ((lpWaveHdr = wwo->lpQueuePtr) &&
884 (force ||
885 (lpWaveHdr != wwo->lpPlayPtr &&
886 lpWaveHdr != wwo->lpLoopPtr &&
887 lpWaveHdr->reserved <= wwo->dwWrittenTotal))) {
889 wwo->lpQueuePtr = lpWaveHdr->lpNext;
891 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
892 lpWaveHdr->dwFlags |= WHDR_DONE;
894 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
896 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
897 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
900 /**************************************************************************
901 * wodPlayer_Reset [internal]
903 * wodPlayer helper. Resets current output stream.
905 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
907 wodUpdatePlayedTotal(wwo);
909 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
911 /* we aren't able to flush any data that has already been written */
912 /* to esd, otherwise we would do the flushing here */
914 if (reset) {
915 enum win_wm_message msg;
916 DWORD param;
917 HANDLE ev;
919 /* remove any buffer */
920 wodPlayer_NotifyCompletions(wwo, TRUE);
922 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
923 wwo->state = WINE_WS_STOPPED;
924 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
926 wwo->dwPartialOffset = 0; /* Clear partial wavehdr */
928 /* remove any existing message in the ring */
929 EnterCriticalSection(&wwo->msgRing.msg_crst);
931 /* return all pending headers in queue */
932 while (ESD_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
934 TRACE("flushing msg\n");
935 if (msg != WINE_WM_HEADER)
937 FIXME("shouldn't have headers left\n");
938 SetEvent(ev);
939 continue;
941 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
942 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
944 wodNotifyClient(wwo, WOM_DONE, param, 0);
946 ResetEvent(wwo->msgRing.msg_event);
947 LeaveCriticalSection(&wwo->msgRing.msg_crst);
948 } else {
949 if (wwo->lpLoopPtr) {
950 /* complicated case, not handled yet (could imply modifying the loop counter */
951 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
952 wwo->lpPlayPtr = wwo->lpLoopPtr;
953 wwo->dwPartialOffset = 0;
954 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
955 } else {
956 /* the data already written is going to be played, so take */
957 /* this fact into account here */
958 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
960 wwo->state = WINE_WS_PAUSED;
964 /**************************************************************************
965 * wodPlayer_ProcessMessages [internal]
967 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
969 LPWAVEHDR lpWaveHdr;
970 enum win_wm_message msg;
971 DWORD param;
972 HANDLE ev;
974 while (ESD_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
975 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
976 switch (msg) {
977 case WINE_WM_PAUSING:
978 wodPlayer_Reset(wwo, FALSE);
979 SetEvent(ev);
980 break;
981 case WINE_WM_RESTARTING:
982 wwo->state = WINE_WS_PLAYING;
983 SetEvent(ev);
984 break;
985 case WINE_WM_HEADER:
986 lpWaveHdr = (LPWAVEHDR)param;
988 /* insert buffer at the end of queue */
990 LPWAVEHDR* wh;
991 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
992 *wh = lpWaveHdr;
994 if (!wwo->lpPlayPtr)
995 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
996 if (wwo->state == WINE_WS_STOPPED)
997 wwo->state = WINE_WS_PLAYING;
998 break;
999 case WINE_WM_RESETTING:
1000 wodPlayer_Reset(wwo, TRUE);
1001 SetEvent(ev);
1002 break;
1003 case WINE_WM_UPDATE:
1004 wodUpdatePlayedTotal(wwo);
1005 SetEvent(ev);
1006 break;
1007 case WINE_WM_BREAKLOOP:
1008 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1009 /* ensure exit at end of current loop */
1010 wwo->dwLoops = 1;
1012 SetEvent(ev);
1013 break;
1014 case WINE_WM_CLOSING:
1015 /* sanity check: this should not happen since the device must have been reset before */
1016 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1017 wwo->hThread = 0;
1018 wwo->state = WINE_WS_CLOSED;
1019 SetEvent(ev);
1020 ExitThread(0);
1021 /* shouldn't go here */
1022 default:
1023 FIXME("unknown message %d\n", msg);
1024 break;
1029 /**************************************************************************
1030 * wodPlayer_FeedDSP [internal]
1031 * Feed as much sound data as we can into the DSP and return the number of
1032 * milliseconds before it will be necessary to feed the DSP again.
1034 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1036 DWORD availInQ;
1038 wodUpdatePlayedTotal(wwo);
1039 /* better way to set availInQ? */
1040 availInQ = ESD_BUF_SIZE;
1041 TRACE("availInQ = %ld\n", availInQ);
1043 /* input queue empty */
1044 if (!wwo->lpPlayPtr) {
1045 TRACE("Run out of wavehdr:s... flushing\n");
1046 return INFINITE;
1049 #if 0
1050 /* no more room... no need to try to feed */
1051 if(!availInQ)
1053 TRACE("no more room, no need to try to feed\n");
1054 return wwo->dwSleepTime;
1056 #endif
1058 /* Feed from partial wavehdr */
1059 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0)
1061 TRACE("feeding from partial wavehdr\n");
1062 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1065 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1066 if (!wwo->dwPartialOffset)
1068 while(wwo->lpPlayPtr && availInQ)
1070 TRACE("feeding waveheaders until we run out of space\n");
1071 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1072 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1073 TRACE("reserved=(%ld) dwWrittenTotal=(%ld) dwBufferLength=(%ld)\n",
1074 wwo->lpPlayPtr->reserved,
1075 wwo->dwWrittenTotal,
1076 wwo->lpPlayPtr->dwBufferLength
1078 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1082 if (!wwo->lpPlayPtr) {
1083 TRACE("Ran out of wavehdrs\n");
1084 return INFINITE;
1087 return wwo->dwSleepTime;
1091 /**************************************************************************
1092 * wodPlayer [internal]
1094 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1096 WORD uDevID = (DWORD)pmt;
1097 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1098 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1099 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1100 DWORD dwSleepTime;
1102 wwo->state = WINE_WS_STOPPED;
1103 SetEvent(wwo->hStartUpEvent);
1105 for (;;) {
1106 /** Wait for the shortest time before an action is required. If there
1107 * are no pending actions, wait forever for a command.
1109 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1110 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1111 WaitForSingleObject(wwo->msgRing.msg_event, dwSleepTime);
1112 wodPlayer_ProcessMessages(wwo);
1113 if (wwo->state == WINE_WS_PLAYING) {
1114 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1115 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1116 } else {
1117 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1122 /**************************************************************************
1123 * wodGetDevCaps [internal]
1125 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1127 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1129 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1131 if (wDevID >= MAX_WAVEOUTDRV) {
1132 TRACE("MAX_WAVOUTDRV reached !\n");
1133 return MMSYSERR_BADDEVICEID;
1136 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1137 return MMSYSERR_NOERROR;
1140 /**************************************************************************
1141 * wodOpen [internal]
1143 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1145 WINE_WAVEOUT* wwo;
1146 /* output to esound... */
1147 int out_bits = ESD_BITS8, out_channels = ESD_MONO, out_rate;
1148 int out_mode = ESD_STREAM, out_func = ESD_PLAY;
1149 esd_format_t out_format;
1151 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1152 if (lpDesc == NULL) {
1153 WARN("Invalid Parameter !\n");
1154 return MMSYSERR_INVALPARAM;
1156 if (wDevID >= MAX_WAVEOUTDRV) {
1157 TRACE("MAX_WAVOUTDRV reached !\n");
1158 return MMSYSERR_BADDEVICEID;
1161 /* if this device is already open tell the app that it is allocated */
1162 if(WOutDev[wDevID].esd_fd != -1)
1164 TRACE("device already allocated\n");
1165 return MMSYSERR_ALLOCATED;
1168 /* only PCM format is supported so far... */
1169 if (!supportedFormat(lpDesc->lpFormat)) {
1170 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1171 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1172 lpDesc->lpFormat->nSamplesPerSec);
1173 return WAVERR_BADFORMAT;
1176 if (dwFlags & WAVE_FORMAT_QUERY) {
1177 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1178 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1179 lpDesc->lpFormat->nSamplesPerSec);
1180 return MMSYSERR_NOERROR;
1183 wwo = &WOutDev[wDevID];
1185 /* direct sound not supported, ignore the flag */
1186 dwFlags &= ~WAVE_DIRECTSOUND;
1188 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1190 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1191 copy_format(lpDesc->lpFormat, &wwo->waveFormat);
1193 if (wwo->waveFormat.Format.wBitsPerSample == 0) {
1194 WARN("Resetting zeroed wBitsPerSample\n");
1195 wwo->waveFormat.Format.wBitsPerSample = 8 *
1196 (wwo->waveFormat.Format.nAvgBytesPerSec /
1197 wwo->waveFormat.Format.nSamplesPerSec) /
1198 wwo->waveFormat.Format.nChannels;
1201 if (wwo->waveFormat.Format.wBitsPerSample == 8)
1202 out_bits = ESD_BITS8;
1203 else if (wwo->waveFormat.Format.wBitsPerSample == 16)
1204 out_bits = ESD_BITS16;
1206 wwo->bytes_per_frame = (wwo->waveFormat.Format.wBitsPerSample * wwo->waveFormat.Format.nChannels) / 8;
1208 if (wwo->waveFormat.Format.nChannels == 1)
1209 out_channels = ESD_MONO;
1210 else if (wwo->waveFormat.Format.nChannels == 2)
1211 out_channels = ESD_STEREO;
1213 out_format = out_bits | out_channels | out_mode | out_func;
1214 out_rate = (int) wwo->waveFormat.Format.nSamplesPerSec;
1215 TRACE("esd output format = 0x%08x, rate = %d\n", out_format, out_rate);
1217 wwo->esd_fd = esd_play_stream(out_format, out_rate, esd_host, "wineesd");
1219 /* clear these so we don't have any confusion ;-) */
1220 wwo->sound_buffer = 0;
1221 wwo->buffer_size = 0;
1223 if(wwo->esd_fd < 0) return MMSYSERR_ALLOCATED;
1225 wwo->dwBufferSize = ESD_BUF_SIZE;
1226 TRACE("Buffer size is now (%ld)\n",wwo->dwBufferSize);
1228 wwo->dwPlayedTotal = 0;
1229 wwo->dwWrittenTotal = 0;
1231 wwo->dwSleepTime = (1024 * 1000 * BUFFER_REFILL_THRESHOLD) / wwo->waveFormat.Format.nAvgBytesPerSec;
1233 /* Initialize volume to full level */
1234 wwo->volume_left = 100;
1235 wwo->volume_right = 100;
1237 ESD_InitRingMessage(&wwo->msgRing);
1239 /* create player thread */
1240 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1241 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1242 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1243 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1244 CloseHandle(wwo->hStartUpEvent);
1245 } else {
1246 wwo->hThread = INVALID_HANDLE_VALUE;
1247 wwo->dwThreadID = 0;
1249 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1251 TRACE("esd=0x%lx, dwBufferSize=%ld\n",
1252 (long)wwo->esd_fd, wwo->dwBufferSize);
1254 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1255 wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
1256 wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
1257 wwo->waveFormat.Format.nBlockAlign);
1259 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1262 /**************************************************************************
1263 * wodClose [internal]
1265 static DWORD wodClose(WORD wDevID)
1267 DWORD ret = MMSYSERR_NOERROR;
1268 WINE_WAVEOUT* wwo;
1270 TRACE("(%u);\n", wDevID);
1272 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1273 WARN("bad device ID !\n");
1274 return MMSYSERR_BADDEVICEID;
1277 wwo = &WOutDev[wDevID];
1278 if (wwo->lpQueuePtr) {
1279 WARN("buffers still playing !\n");
1280 ret = WAVERR_STILLPLAYING;
1281 } else {
1282 TRACE("imhere[3-close]\n");
1283 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1284 ESD_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1287 ESD_DestroyRingMessage(&wwo->msgRing);
1289 ESD_CloseWaveOutDevice(wwo); /* close the stream and clean things up */
1291 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1293 return ret;
1296 /**************************************************************************
1297 * wodWrite [internal]
1300 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1302 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1304 /* first, do the sanity checks... */
1305 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1306 WARN("bad dev ID !\n");
1307 return MMSYSERR_BADDEVICEID;
1310 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1312 TRACE("unprepared\n");
1313 return WAVERR_UNPREPARED;
1316 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1318 TRACE("still playing\n");
1319 return WAVERR_STILLPLAYING;
1322 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1323 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1324 lpWaveHdr->lpNext = 0;
1326 TRACE("adding ring message\n");
1327 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1329 return MMSYSERR_NOERROR;
1332 /**************************************************************************
1333 * wodPause [internal]
1335 static DWORD wodPause(WORD wDevID)
1337 TRACE("(%u);!\n", wDevID);
1339 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1340 WARN("bad device ID !\n");
1341 return MMSYSERR_BADDEVICEID;
1344 TRACE("imhere[3-PAUSING]\n");
1345 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1347 return MMSYSERR_NOERROR;
1350 /**************************************************************************
1351 * wodRestart [internal]
1353 static DWORD wodRestart(WORD wDevID)
1355 TRACE("(%u);\n", wDevID);
1357 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1358 WARN("bad device ID !\n");
1359 return MMSYSERR_BADDEVICEID;
1362 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1363 TRACE("imhere[3-RESTARTING]\n");
1364 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1367 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1368 /* FIXME: Myst crashes with this ... hmm -MM
1369 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1372 return MMSYSERR_NOERROR;
1375 /**************************************************************************
1376 * wodReset [internal]
1378 static DWORD wodReset(WORD wDevID)
1380 TRACE("(%u);\n", wDevID);
1382 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1383 WARN("bad device ID !\n");
1384 return MMSYSERR_BADDEVICEID;
1387 TRACE("imhere[3-RESET]\n");
1388 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1390 return MMSYSERR_NOERROR;
1393 /**************************************************************************
1394 * wodGetPosition [internal]
1396 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1398 WINE_WAVEOUT* wwo;
1400 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1402 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1403 WARN("bad device ID !\n");
1404 return MMSYSERR_BADDEVICEID;
1407 if (lpTime == NULL) {
1408 WARN("invalid parameter: lpTime == NULL\n");
1409 return MMSYSERR_INVALPARAM;
1412 wwo = &WOutDev[wDevID];
1413 ESD_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1415 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
1418 /**************************************************************************
1419 * wodBreakLoop [internal]
1421 static DWORD wodBreakLoop(WORD wDevID)
1423 TRACE("(%u);\n", wDevID);
1425 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1426 WARN("bad device ID !\n");
1427 return MMSYSERR_BADDEVICEID;
1429 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1430 return MMSYSERR_NOERROR;
1433 /**************************************************************************
1434 * wodGetVolume [internal]
1436 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1438 DWORD left, right;
1440 left = WOutDev[wDevID].volume_left;
1441 right = WOutDev[wDevID].volume_right;
1443 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1445 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) <<
1446 16);
1448 return MMSYSERR_NOERROR;
1451 /**************************************************************************
1452 * wodSetVolume [internal]
1454 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1456 DWORD left, right;
1458 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1459 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1461 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1463 WOutDev[wDevID].volume_left = left;
1464 WOutDev[wDevID].volume_right = right;
1466 return MMSYSERR_NOERROR;
1469 /**************************************************************************
1470 * wodGetNumDevs [internal]
1472 static DWORD wodGetNumDevs(void)
1474 return MAX_WAVEOUTDRV;
1477 /**************************************************************************
1478 * wodDevInterfaceSize [internal]
1480 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1482 TRACE("(%u, %p)\n", wDevID, dwParam1);
1484 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1485 NULL, 0 ) * sizeof(WCHAR);
1486 return MMSYSERR_NOERROR;
1489 /**************************************************************************
1490 * wodDevInterface [internal]
1492 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1494 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1495 NULL, 0 ) * sizeof(WCHAR))
1497 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1498 dwParam1, dwParam2 / sizeof(WCHAR));
1499 return MMSYSERR_NOERROR;
1501 return MMSYSERR_INVALPARAM;
1504 /**************************************************************************
1505 * wodMessage (WINEESD.@)
1507 DWORD WINAPI ESD_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1508 DWORD dwParam1, DWORD dwParam2)
1510 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1511 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1513 switch (wMsg) {
1514 case DRVM_INIT:
1515 case DRVM_EXIT:
1516 case DRVM_ENABLE:
1517 case DRVM_DISABLE:
1518 /* FIXME: Pretend this is supported */
1519 return 0;
1520 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1521 case WODM_CLOSE: return wodClose (wDevID);
1522 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1523 case WODM_PAUSE: return wodPause (wDevID);
1524 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1525 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1526 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
1527 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
1528 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
1529 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1530 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1531 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1532 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1533 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1534 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1535 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1536 case WODM_RESTART: return wodRestart (wDevID);
1537 case WODM_RESET: return wodReset (wDevID);
1539 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1540 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1541 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1542 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1543 default:
1544 FIXME("unknown message %d!\n", wMsg);
1546 return MMSYSERR_NOTSUPPORTED;
1549 /*======================================================================*
1550 * Low level WAVE IN implementation *
1551 *======================================================================*/
1553 /**************************************************************************
1554 * widGetNumDevs [internal]
1556 static DWORD widGetNumDevs(void)
1558 TRACE("%d\n", MAX_WAVEINDRV);
1559 return MAX_WAVEINDRV;
1562 /**************************************************************************
1563 * widDevInterfaceSize [internal]
1565 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1567 TRACE("(%u, %p)\n", wDevID, dwParam1);
1570 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1571 NULL, 0 ) * sizeof(WCHAR);
1572 return MMSYSERR_NOERROR;
1575 /**************************************************************************
1576 * widDevInterface [internal]
1578 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1580 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1581 NULL, 0 ) * sizeof(WCHAR))
1583 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1584 dwParam1, dwParam2 / sizeof(WCHAR));
1585 return MMSYSERR_NOERROR;
1587 return MMSYSERR_INVALPARAM;
1590 /**************************************************************************
1591 * widNotifyClient [internal]
1593 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1595 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
1597 switch (wMsg) {
1598 case WIM_OPEN:
1599 case WIM_CLOSE:
1600 case WIM_DATA:
1601 if (wwi->wFlags != DCB_NULL &&
1602 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
1603 (HDRVR)wwi->waveDesc.hWave, wMsg,
1604 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
1605 WARN("can't notify client !\n");
1606 return MMSYSERR_ERROR;
1608 break;
1609 default:
1610 FIXME("Unknown callback message %u\n", wMsg);
1611 return MMSYSERR_INVALPARAM;
1613 return MMSYSERR_NOERROR;
1616 /**************************************************************************
1617 * widGetDevCaps [internal]
1619 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
1621 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1623 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1625 if (wDevID >= MAX_WAVEINDRV) {
1626 TRACE("MAX_WAVINDRV reached !\n");
1627 return MMSYSERR_BADDEVICEID;
1630 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1631 return MMSYSERR_NOERROR;
1634 /**************************************************************************
1635 * widRecorder [internal]
1637 static DWORD CALLBACK widRecorder(LPVOID pmt)
1639 WORD uDevID = (DWORD)pmt;
1640 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
1641 WAVEHDR* lpWaveHdr;
1642 DWORD dwSleepTime;
1643 DWORD bytesRead;
1644 enum win_wm_message msg;
1645 DWORD param;
1646 HANDLE ev;
1648 SetEvent(wwi->hStartUpEvent);
1650 /* make sleep time to be # of ms to record one packet */
1651 dwSleepTime = (1024 * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
1652 TRACE("sleeptime=%ld ms\n", dwSleepTime);
1654 for(;;) {
1655 TRACE("wwi->lpQueuePtr=(%p), wwi->state=(%d)\n",wwi->lpQueuePtr,wwi->state);
1657 /* read all data is esd input buffer. */
1658 if ((wwi->lpQueuePtr != NULL) && (wwi->state == WINE_WS_PLAYING))
1660 lpWaveHdr = wwi->lpQueuePtr;
1662 TRACE("read as much as we can\n");
1663 while(wwi->lpQueuePtr)
1665 TRACE("attempt to read %ld bytes\n",lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1666 bytesRead = read(wwi->esd_fd,
1667 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1668 lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1669 TRACE("bytesRead=%ld\n",bytesRead);
1670 if (bytesRead == -1 && errno == EAGAIN)
1671 bytesRead = 0;
1672 if (bytesRead==0) break; /* So we can stop recording smoothly */
1673 if (bytesRead < 0)
1674 bytesRead = 0;
1676 lpWaveHdr->dwBytesRecorded += bytesRead;
1677 wwi->dwRecordedTotal += bytesRead;
1679 /* buffer full. notify client */
1680 if (lpWaveHdr->dwBytesRecorded >= lpWaveHdr->dwBufferLength)
1682 /* must copy the value of next waveHdr, because we have no idea of what
1683 * will be done with the content of lpWaveHdr in callback
1685 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1687 TRACE("waveHdr full.\n");
1689 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1690 lpWaveHdr->dwFlags |= WHDR_DONE;
1692 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1693 lpWaveHdr = wwi->lpQueuePtr = lpNext;
1698 /* wait for dwSleepTime or an event in thread's queue */
1699 WaitForSingleObject(wwi->msgRing.msg_event, dwSleepTime);
1701 while (ESD_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
1703 TRACE("msg=%s param=0x%lx\n",wodPlayerCmdString[msg - WM_USER - 1], param);
1704 switch(msg) {
1705 case WINE_WM_PAUSING:
1706 wwi->state = WINE_WS_PAUSED;
1708 /* Put code here to "pause" esd recording
1711 SetEvent(ev);
1712 break;
1713 case WINE_WM_STARTING:
1714 wwi->state = WINE_WS_PLAYING;
1716 /* Put code here to "start" esd recording
1719 SetEvent(ev);
1720 break;
1721 case WINE_WM_HEADER:
1722 lpWaveHdr = (LPWAVEHDR)param;
1723 /* insert buffer at end of queue */
1725 LPWAVEHDR* wh;
1726 int num_headers = 0;
1727 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
1729 num_headers++;
1732 *wh=lpWaveHdr;
1734 break;
1735 case WINE_WM_STOPPING:
1736 if (wwi->state != WINE_WS_STOPPED)
1739 /* Put code here to "stop" esd recording
1742 /* return current buffer to app */
1743 lpWaveHdr = wwi->lpQueuePtr;
1744 if (lpWaveHdr)
1746 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1747 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1748 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1749 lpWaveHdr->dwFlags |= WHDR_DONE;
1750 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1751 wwi->lpQueuePtr = lpNext;
1754 wwi->state = WINE_WS_STOPPED;
1755 SetEvent(ev);
1756 break;
1757 case WINE_WM_RESETTING:
1758 wwi->state = WINE_WS_STOPPED;
1759 wwi->dwRecordedTotal = 0;
1761 /* return all buffers to the app */
1762 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
1763 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1764 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1765 lpWaveHdr->dwFlags |= WHDR_DONE;
1767 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1769 wwi->lpQueuePtr = NULL;
1770 SetEvent(ev);
1771 break;
1772 case WINE_WM_CLOSING:
1773 wwi->hThread = 0;
1774 wwi->state = WINE_WS_CLOSED;
1775 SetEvent(ev);
1776 ExitThread(0);
1777 /* shouldn't go here */
1778 default:
1779 FIXME("unknown message %d\n", msg);
1780 break;
1784 ExitThread(0);
1785 /* just for not generating compilation warnings... should never be executed */
1786 return 0;
1789 /**************************************************************************
1790 * widOpen [internal]
1792 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1794 WINE_WAVEIN* wwi;
1795 /* input esound... */
1796 int in_bits = ESD_BITS16, in_channels = ESD_STEREO, in_rate;
1797 #ifdef WID_USE_ESDMON
1798 int in_mode = ESD_STREAM, in_func = ESD_PLAY;
1799 #else
1800 int in_mode = ESD_STREAM, in_func = ESD_RECORD;
1801 #endif
1802 esd_format_t in_format;
1803 int mode;
1805 TRACE("(%u, %p %08lX);\n",wDevID, lpDesc, dwFlags);
1806 if (lpDesc == NULL) {
1807 WARN("Invalid Parametr (lpDesc == NULL)!\n");
1808 return MMSYSERR_INVALPARAM;
1811 if (wDevID >= MAX_WAVEINDRV) {
1812 TRACE ("MAX_WAVEINDRV reached !\n");
1813 return MMSYSERR_BADDEVICEID;
1816 /* if this device is already open tell the app that it is allocated */
1817 if(WInDev[wDevID].esd_fd != -1)
1819 TRACE("device already allocated\n");
1820 return MMSYSERR_ALLOCATED;
1823 /* only PCM format is support so far... */
1824 if (!supportedFormat(lpDesc->lpFormat)) {
1825 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1826 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1827 lpDesc->lpFormat->nSamplesPerSec);
1828 return WAVERR_BADFORMAT;
1831 if (dwFlags & WAVE_FORMAT_QUERY) {
1832 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1833 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1834 lpDesc->lpFormat->nSamplesPerSec);
1835 return MMSYSERR_NOERROR;
1838 wwi = &WInDev[wDevID];
1840 /* direct sound not supported, ignore the flag */
1841 dwFlags &= ~WAVE_DIRECTSOUND;
1843 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1845 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1846 copy_format(lpDesc->lpFormat, &wwi->waveFormat);
1848 if (wwi->waveFormat.Format.wBitsPerSample == 0) {
1849 WARN("Resetting zerod wBitsPerSample\n");
1850 wwi->waveFormat.Format.wBitsPerSample = 8 *
1851 (wwi->waveFormat.Format.nAvgBytesPerSec /
1852 wwi->waveFormat.Format.nSamplesPerSec) /
1853 wwi->waveFormat.Format.nChannels;
1856 if (wwi->waveFormat.Format.wBitsPerSample == 8)
1857 in_bits = ESD_BITS8;
1858 else if (wwi->waveFormat.Format.wBitsPerSample == 16)
1859 in_bits = ESD_BITS16;
1861 wwi->bytes_per_frame = (wwi->waveFormat.Format.wBitsPerSample * wwi->waveFormat.Format.nChannels) / 8;
1863 if (wwi->waveFormat.Format.nChannels == 1)
1864 in_channels = ESD_MONO;
1865 else if (wwi->waveFormat.Format.nChannels == 2)
1866 in_channels = ESD_STEREO;
1868 in_format = in_bits | in_channels | in_mode | in_func;
1869 in_rate = (int) wwi->waveFormat.Format.nSamplesPerSec;
1870 TRACE("esd input format = 0x%08x, rate = %d\n", in_format, in_rate);
1872 #ifdef WID_USE_ESDMON
1873 wwi->esd_fd = esd_monitor_stream(in_format, in_rate, esd_host, "wineesd");
1874 #else
1875 wwi->esd_fd = esd_record_stream(in_format, in_rate, esd_host, "wineesd");
1876 #endif
1877 TRACE("(wwi->esd_fd=%d)\n",wwi->esd_fd);
1878 wwi->state = WINE_WS_STOPPED;
1880 if (wwi->lpQueuePtr) {
1881 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
1882 wwi->lpQueuePtr = NULL;
1885 if(wwi->esd_fd < 0) return MMSYSERR_ALLOCATED;
1887 /* Set the esd socket O_NONBLOCK, so we can stop recording smoothly */
1888 mode = fcntl(wwi->esd_fd, F_GETFL);
1889 mode |= O_NONBLOCK;
1890 fcntl(wwi->esd_fd, F_SETFL, mode);
1892 wwi->dwRecordedTotal = 0;
1893 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1895 ESD_InitRingMessage(&wwi->msgRing);
1897 /* create recorder thread */
1898 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1899 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1900 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
1901 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
1902 CloseHandle(wwi->hStartUpEvent);
1903 } else {
1904 wwi->hThread = INVALID_HANDLE_VALUE;
1905 wwi->dwThreadID = 0;
1907 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
1909 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1910 wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
1911 wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
1912 wwi->waveFormat.Format.nBlockAlign);
1913 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
1916 /**************************************************************************
1917 * widClose [internal]
1919 static DWORD widClose(WORD wDevID)
1921 WINE_WAVEIN* wwi;
1923 TRACE("(%u);\n", wDevID);
1924 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1925 WARN("can't close !\n");
1926 return MMSYSERR_INVALHANDLE;
1929 wwi = &WInDev[wDevID];
1931 if (wwi->lpQueuePtr != NULL) {
1932 WARN("still buffers open !\n");
1933 return WAVERR_STILLPLAYING;
1936 ESD_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
1937 ESD_CloseWaveInDevice(wwi);
1938 wwi->state = WINE_WS_CLOSED;
1939 ESD_DestroyRingMessage(&wwi->msgRing);
1940 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
1943 /**************************************************************************
1944 * widAddBuffer [internal]
1946 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1948 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1950 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1951 WARN("can't do it !\n");
1952 return MMSYSERR_INVALHANDLE;
1954 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
1955 TRACE("never been prepared !\n");
1956 return WAVERR_UNPREPARED;
1958 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
1959 TRACE("header already in use !\n");
1960 return WAVERR_STILLPLAYING;
1963 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1964 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1965 lpWaveHdr->dwBytesRecorded = 0;
1966 lpWaveHdr->lpNext = NULL;
1968 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1969 return MMSYSERR_NOERROR;
1972 /**************************************************************************
1973 * widStart [internal]
1975 static DWORD widStart(WORD wDevID)
1977 TRACE("(%u);\n", wDevID);
1978 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1979 WARN("can't start recording !\n");
1980 return MMSYSERR_INVALHANDLE;
1983 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
1984 return MMSYSERR_NOERROR;
1987 /**************************************************************************
1988 * widStop [internal]
1990 static DWORD widStop(WORD wDevID)
1992 TRACE("(%u);\n", wDevID);
1993 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1994 WARN("can't stop !\n");
1995 return MMSYSERR_INVALHANDLE;
1998 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
2000 return MMSYSERR_NOERROR;
2003 /**************************************************************************
2004 * widReset [internal]
2006 static DWORD widReset(WORD wDevID)
2008 TRACE("(%u);\n", wDevID);
2009 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2010 WARN("can't reset !\n");
2011 return MMSYSERR_INVALHANDLE;
2013 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2014 return MMSYSERR_NOERROR;
2017 /**************************************************************************
2018 * widMessage (WINEESD.6)
2020 DWORD WINAPI ESD_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2021 DWORD dwParam1, DWORD dwParam2)
2023 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2024 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2025 switch (wMsg) {
2026 case DRVM_INIT:
2027 case DRVM_EXIT:
2028 case DRVM_ENABLE:
2029 case DRVM_DISABLE:
2030 /* FIXME: Pretend this is supported */
2031 return 0;
2032 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2033 case WIDM_CLOSE: return widClose (wDevID);
2034 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2035 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2036 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2037 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
2038 case WIDM_GETNUMDEVS: return widGetNumDevs ();
2039 case WIDM_RESET: return widReset (wDevID);
2040 case WIDM_START: return widStart (wDevID);
2041 case WIDM_STOP: return widStop (wDevID);
2042 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2043 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2044 default:
2045 FIXME("unknown message %d!\n", wMsg);
2047 return MMSYSERR_NOTSUPPORTED;
2050 /*======================================================================*
2051 * Low level DSOUND implementation *
2052 *======================================================================*/
2053 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2055 /* we can't perform memory mapping as we don't have a file stream
2056 interface with esd like we do with oss */
2057 MESSAGE("This sound card's driver does not support direct access\n");
2058 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2059 return MMSYSERR_NOTSUPPORTED;
2062 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2064 memset(desc, 0, sizeof(*desc));
2065 strcpy(desc->szDesc, "Wine EsounD DirectSound Driver");
2066 strcpy(desc->szDrvname, "wineesd.drv");
2067 return MMSYSERR_NOERROR;
2070 #else /* !HAVE_ESD */
2072 /**************************************************************************
2073 * wodMessage (WINEESD.@)
2075 DWORD WINAPI ESD_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2076 DWORD dwParam1, DWORD dwParam2)
2078 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2079 return MMSYSERR_NOTENABLED;
2082 /**************************************************************************
2083 * widMessage (WINEESD.6)
2085 DWORD WINAPI ESD_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2086 DWORD dwParam1, DWORD dwParam2)
2088 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2089 return MMSYSERR_NOTENABLED;
2092 #endif /* HAVE_ESD */