Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / wineoss.drv / audio.c
bloba00fa6596acb413693f8a8e18553ab41369ba944
1 /*
2 * Sample Wine Driver for Open Sound System (featured in Linux and FreeBSD)
4 * Copyright 1994 Martin Ayotte
5 * 1999 Eric Pouech (async playing in waveOut/waveIn)
6 * 2000 Eric Pouech (loops in waveOut)
7 * 2002 Eric Pouech (full duplex)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * FIXME:
25 * pause in waveOut does not work correctly in loop mode
26 * Direct Sound Capture driver does not work (not complete yet)
29 /* an exact wodGetPosition is usually not worth the extra context switches,
30 * as we're going to have near fragment accuracy anyway */
31 #define EXACT_WODPOSITION
32 #define EXACT_WIDPOSITION
34 #include "config.h"
35 #include "wine/port.h"
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <string.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 #include <errno.h>
45 #include <fcntl.h>
46 #ifdef HAVE_SYS_IOCTL_H
47 # include <sys/ioctl.h>
48 #endif
49 #ifdef HAVE_SYS_MMAN_H
50 # include <sys/mman.h>
51 #endif
52 #ifdef HAVE_POLL_H
53 #include <poll.h>
54 #endif
55 #ifdef HAVE_SYS_POLL_H
56 # include <sys/poll.h>
57 #endif
59 #include "windef.h"
60 #include "winbase.h"
61 #include "wingdi.h"
62 #include "winuser.h"
63 #include "winnls.h"
64 #include "winerror.h"
65 #include "winreg.h"
66 #include "mmddk.h"
67 #include "mmreg.h"
68 #include "dsound.h"
69 #include "dsdriver.h"
70 #include "ks.h"
71 #include "ksguid.h"
72 #include "ksmedia.h"
73 #include "oss.h"
74 #include "wine/debug.h"
76 #include "audio.h"
78 WINE_DEFAULT_DEBUG_CHANNEL(wave);
80 /* Allow 1% deviation for sample rates (some ES137x cards) */
81 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
83 #ifdef HAVE_OSS
85 static int oss_locked_format=-1;
86 static int oss_locked_stereo=-1;
87 static int oss_locked_rate=-1;
88 static int oss_fullduplex=-1;
90 OSS_DEVICE OSS_Devices[MAX_WAVEDRV];
91 WINE_WAVEOUT WOutDev[MAX_WAVEDRV];
92 WINE_WAVEIN WInDev[MAX_WAVEDRV];
93 unsigned numOutDev;
94 unsigned numInDev;
96 /* state diagram for waveOut writing:
98 * +---------+-------------+---------------+---------------------------------+
99 * | state | function | event | new state |
100 * +---------+-------------+---------------+---------------------------------+
101 * | | open() | | STOPPED |
102 * | PAUSED | write() | | PAUSED |
103 * | STOPPED | write() | <thrd create> | PLAYING |
104 * | PLAYING | write() | HEADER | PLAYING |
105 * | (other) | write() | <error> | |
106 * | (any) | pause() | PAUSING | PAUSED |
107 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
108 * | (any) | reset() | RESETTING | STOPPED |
109 * | (any) | close() | CLOSING | CLOSED |
110 * +---------+-------------+---------------+---------------------------------+
113 /* These strings used only for tracing */
114 static const char * getCmdString(enum win_wm_message msg)
116 static char unknown[32];
117 #define MSG_TO_STR(x) case x: return #x
118 switch(msg) {
119 MSG_TO_STR(WINE_WM_PAUSING);
120 MSG_TO_STR(WINE_WM_RESTARTING);
121 MSG_TO_STR(WINE_WM_RESETTING);
122 MSG_TO_STR(WINE_WM_HEADER);
123 MSG_TO_STR(WINE_WM_UPDATE);
124 MSG_TO_STR(WINE_WM_BREAKLOOP);
125 MSG_TO_STR(WINE_WM_CLOSING);
126 MSG_TO_STR(WINE_WM_STARTING);
127 MSG_TO_STR(WINE_WM_STOPPING);
129 #undef MSG_TO_STR
130 sprintf(unknown, "UNKNOWN(0x%08x)", msg);
131 return unknown;
134 int getEnables(OSS_DEVICE *ossdev)
136 return ( (ossdev->bOutputEnabled ? PCM_ENABLE_OUTPUT : 0) |
137 (ossdev->bInputEnabled ? PCM_ENABLE_INPUT : 0) );
140 static const char * getMessage(UINT msg)
142 static char unknown[32];
143 #define MSG_TO_STR(x) case x: return #x
144 switch(msg) {
145 MSG_TO_STR(DRVM_INIT);
146 MSG_TO_STR(DRVM_EXIT);
147 MSG_TO_STR(DRVM_ENABLE);
148 MSG_TO_STR(DRVM_DISABLE);
149 MSG_TO_STR(WIDM_OPEN);
150 MSG_TO_STR(WIDM_CLOSE);
151 MSG_TO_STR(WIDM_ADDBUFFER);
152 MSG_TO_STR(WIDM_PREPARE);
153 MSG_TO_STR(WIDM_UNPREPARE);
154 MSG_TO_STR(WIDM_GETDEVCAPS);
155 MSG_TO_STR(WIDM_GETNUMDEVS);
156 MSG_TO_STR(WIDM_GETPOS);
157 MSG_TO_STR(WIDM_RESET);
158 MSG_TO_STR(WIDM_START);
159 MSG_TO_STR(WIDM_STOP);
160 MSG_TO_STR(WODM_OPEN);
161 MSG_TO_STR(WODM_CLOSE);
162 MSG_TO_STR(WODM_WRITE);
163 MSG_TO_STR(WODM_PAUSE);
164 MSG_TO_STR(WODM_GETPOS);
165 MSG_TO_STR(WODM_BREAKLOOP);
166 MSG_TO_STR(WODM_PREPARE);
167 MSG_TO_STR(WODM_UNPREPARE);
168 MSG_TO_STR(WODM_GETDEVCAPS);
169 MSG_TO_STR(WODM_GETNUMDEVS);
170 MSG_TO_STR(WODM_GETPITCH);
171 MSG_TO_STR(WODM_SETPITCH);
172 MSG_TO_STR(WODM_GETPLAYBACKRATE);
173 MSG_TO_STR(WODM_SETPLAYBACKRATE);
174 MSG_TO_STR(WODM_GETVOLUME);
175 MSG_TO_STR(WODM_SETVOLUME);
176 MSG_TO_STR(WODM_RESTART);
177 MSG_TO_STR(WODM_RESET);
178 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
179 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
180 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
181 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
183 #undef MSG_TO_STR
184 sprintf(unknown, "UNKNOWN(0x%04x)", msg);
185 return unknown;
188 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
190 TRACE("(%u, %p)\n", wDevID, dwParam1);
192 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev->interface_name, -1,
193 NULL, 0 ) * sizeof(WCHAR);
194 return MMSYSERR_NOERROR;
197 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
199 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev->interface_name, -1,
200 NULL, 0 ) * sizeof(WCHAR))
202 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev->interface_name, -1,
203 dwParam1, dwParam2 / sizeof(WCHAR));
204 return MMSYSERR_NOERROR;
207 return MMSYSERR_INVALPARAM;
210 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
212 TRACE("(%u, %p)\n", wDevID, dwParam1);
214 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev->interface_name, -1,
215 NULL, 0 ) * sizeof(WCHAR);
216 return MMSYSERR_NOERROR;
219 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
221 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev->interface_name, -1,
222 NULL, 0 ) * sizeof(WCHAR))
224 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev->interface_name, -1,
225 dwParam1, dwParam2 / sizeof(WCHAR));
226 return MMSYSERR_NOERROR;
229 return MMSYSERR_INVALPARAM;
232 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
233 WAVEFORMATPCMEX* format)
235 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
236 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
237 format->Format.nChannels, format->Format.nAvgBytesPerSec);
238 TRACE("Position in bytes=%u\n", position);
240 switch (lpTime->wType) {
241 case TIME_SAMPLES:
242 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
243 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
244 break;
245 case TIME_MS:
246 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
247 TRACE("TIME_MS=%u\n", lpTime->u.ms);
248 break;
249 case TIME_SMPTE:
250 lpTime->u.smpte.fps = 30;
251 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
252 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
253 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
254 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
255 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
256 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
257 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
258 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
259 lpTime->u.smpte.fps = 30;
260 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
261 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
262 lpTime->u.smpte.hour, lpTime->u.smpte.min,
263 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
264 break;
265 default:
266 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
267 lpTime->wType = TIME_BYTES;
268 /* fall through */
269 case TIME_BYTES:
270 lpTime->u.cb = position;
271 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
272 break;
274 return MMSYSERR_NOERROR;
277 static BOOL supportedFormat(LPWAVEFORMATEX wf)
279 TRACE("(%p)\n",wf);
281 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
282 return FALSE;
284 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
285 if (wf->nChannels >= 1 && wf->nChannels <= MAX_CHANNELS) {
286 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
287 return TRUE;
289 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
290 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
292 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
293 if (wf->nChannels >=1 && wf->nChannels <= MAX_CHANNELS) {
294 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
295 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
296 return TRUE;
297 } else
298 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
300 } else
301 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
302 } else
303 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
305 return FALSE;
308 void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
310 ZeroMemory(wf2, sizeof(wf2));
311 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
312 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
313 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
314 memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
315 else
316 memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
319 /*======================================================================*
320 * Low level WAVE implementation *
321 *======================================================================*/
323 /******************************************************************
324 * OSS_RawOpenDevice
326 * Low level device opening (from values stored in ossdev)
328 static DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
330 int fd, val, rc;
331 TRACE("(%p,%d)\n",ossdev,strict_format);
333 TRACE("open_access=%s\n",
334 ossdev->open_access == O_RDONLY ? "O_RDONLY" :
335 ossdev->open_access == O_WRONLY ? "O_WRONLY" :
336 ossdev->open_access == O_RDWR ? "O_RDWR" : "Unknown");
338 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
340 WARN("Couldn't open %s (%s)\n", ossdev->dev_name, strerror(errno));
341 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
343 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
344 /* turn full duplex on if it has been requested */
345 if (ossdev->open_access == O_RDWR && ossdev->full_duplex) {
346 rc = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
347 /* on *BSD, as full duplex is always enabled by default, this ioctl
348 * will fail with EINVAL
349 * so, we don't consider EINVAL an error here
351 if (rc != 0 && errno != EINVAL) {
352 WARN("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev->dev_name, strerror(errno));
353 goto error2;
357 if (ossdev->audio_fragment) {
358 rc = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
359 if (rc != 0) {
360 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev->dev_name, strerror(errno));
361 goto error2;
365 /* First size and channels then samplerate */
366 if (ossdev->format>=0)
368 val = ossdev->format;
369 if (oss_locked_format>=0 && oss_locked_format!=ossdev->format) {
370 TRACE("OSS locked format is 0x%x, overriding 0x%x\n",
371 oss_locked_format,ossdev->format);
372 ossdev->format = oss_locked_format;
374 rc = ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
375 if (rc != 0 || val != ossdev->format) {
376 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
377 if (strict_format)
378 goto error;
381 if (ossdev->channels>=0)
383 val = ossdev->channels;
384 rc = ioctl(fd, SNDCTL_DSP_CHANNELS, &ossdev->channels);
385 if (rc != 0 || val != ossdev->channels) {
386 TRACE("Can't set channels to %u (returned %d)\n", val, ossdev->channels);
387 if (strict_format)
388 goto error;
391 if (ossdev->sample_rate>=0)
393 val = ossdev->sample_rate;
394 if (oss_locked_rate>=0 && oss_locked_rate!=ossdev->sample_rate) {
395 TRACE("OSS locked rate is %d, overriding %d\n",
396 oss_locked_rate,ossdev->sample_rate);
397 ossdev->sample_rate = oss_locked_rate;
399 rc = ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
400 if (rc != 0 || !NEAR_MATCH(val, ossdev->sample_rate)) {
401 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
402 if (strict_format)
403 goto error;
406 ossdev->fd = fd;
408 if (ossdev->bTriggerSupport) {
409 int trigger;
410 rc = ioctl(fd, SNDCTL_DSP_GETTRIGGER, &trigger);
411 if (rc != 0) {
412 ERR("ioctl(%s, SNDCTL_DSP_GETTRIGGER) failed (%s)\n",
413 ossdev->dev_name, strerror(errno));
414 goto error;
417 ossdev->bOutputEnabled = ((trigger & PCM_ENABLE_OUTPUT) == PCM_ENABLE_OUTPUT);
418 ossdev->bInputEnabled = ((trigger & PCM_ENABLE_INPUT) == PCM_ENABLE_INPUT);
420 /* If we do not have full duplex, but they opened RDWR
421 ** (as you have to in order for an mmap to succeed)
422 ** then we start out with input off
424 if (ossdev->open_access == O_RDWR && !ossdev->full_duplex &&
425 ossdev->bInputEnabled && ossdev->bOutputEnabled) {
426 ossdev->bInputEnabled = FALSE;
427 trigger &= ~PCM_ENABLE_INPUT;
428 ioctl(fd, SNDCTL_DSP_SETTRIGGER, &trigger);
430 } else {
431 ossdev->bOutputEnabled = TRUE; /* OSS enables by default */
432 ossdev->bInputEnabled = TRUE; /* OSS enables by default */
435 if (ossdev->open_access == O_RDONLY)
436 ossdev->bOutputEnabled = FALSE;
437 if (ossdev->open_access == O_WRONLY)
438 ossdev->bInputEnabled = FALSE;
440 return MMSYSERR_NOERROR;
442 error:
443 close(fd);
444 return WAVERR_BADFORMAT;
445 error2:
446 close(fd);
447 return MMSYSERR_ERROR;
450 /******************************************************************
451 * OSS_OpenDevice
453 * since OSS has poor capabilities in full duplex, we try here to let a program
454 * open the device for both waveout and wavein streams...
455 * this is hackish, but it's the way OSS interface is done...
457 DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
458 int* frag, int strict_format,
459 int sample_rate, int channels, int fmt)
461 DWORD ret;
462 DWORD open_access;
463 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev,req_access,frag,strict_format,sample_rate,channels,fmt);
465 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
467 TRACE("Opening RDWR because full_duplex=%d and req_access=%d\n",
468 ossdev->full_duplex,req_access);
469 open_access = O_RDWR;
471 else
473 open_access=req_access;
476 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
477 if (ossdev->open_count == 0)
479 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
481 ossdev->audio_fragment = (frag) ? *frag : 0;
482 ossdev->sample_rate = sample_rate;
483 ossdev->channels = channels;
484 ossdev->format = fmt;
485 ossdev->open_access = open_access;
486 ossdev->owner_tid = GetCurrentThreadId();
488 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
489 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
490 (req_access == O_RDONLY || req_access == O_WRONLY))
492 int enable;
493 if (req_access == O_WRONLY)
494 ossdev->bInputEnabled=0;
495 else
496 ossdev->bOutputEnabled=0;
497 enable = getEnables(ossdev);
498 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
499 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
500 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
503 else
505 /* check we really open with the same parameters */
506 if (ossdev->open_access != open_access)
508 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
509 return WAVERR_BADFORMAT;
512 /* check if the audio parameters are the same */
513 if (ossdev->sample_rate != sample_rate ||
514 ossdev->channels != channels ||
515 ossdev->format != fmt)
517 /* This is not a fatal error because MSACM might do the remapping */
518 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
519 "OSS doesn't allow us different parameters\n"
520 "audio_frag(%x/%x) sample_rate(%d/%d) channels(%d/%d) fmt(%d/%d)\n",
521 ossdev->audio_fragment, frag ? *frag : 0,
522 ossdev->sample_rate, sample_rate,
523 ossdev->channels, channels,
524 ossdev->format, fmt);
525 return WAVERR_BADFORMAT;
527 /* check if the fragment sizes are the same */
528 if (ossdev->audio_fragment != (frag ? *frag : 0) )
530 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
531 "Please run winecfg, open \"Audio\" page and set\n"
532 "\"Hardware Acceleration\" to \"Emulation\".\n");
533 return WAVERR_BADFORMAT;
535 if (GetCurrentThreadId() != ossdev->owner_tid)
537 WARN("Another thread is trying to access audio...\n");
538 return MMSYSERR_ERROR;
540 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
541 (req_access == O_RDONLY || req_access == O_WRONLY))
543 int enable;
544 if (req_access == O_WRONLY)
545 ossdev->bOutputEnabled=1;
546 else
547 ossdev->bInputEnabled=1;
548 enable = getEnables(ossdev);
549 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
550 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
551 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
555 ossdev->open_count++;
557 return MMSYSERR_NOERROR;
560 /******************************************************************
561 * OSS_CloseDevice
565 void OSS_CloseDevice(OSS_DEVICE* ossdev)
567 TRACE("(%p)\n",ossdev);
568 if (ossdev->open_count>0) {
569 ossdev->open_count--;
570 } else {
571 WARN("OSS_CloseDevice called too many times\n");
573 if (ossdev->open_count == 0)
575 fcntl(ossdev->fd, F_SETFL, fcntl(ossdev->fd, F_GETFL) & ~O_NDELAY);
576 /* reset the device before we close it in case it is in a bad state */
577 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
578 if (close(ossdev->fd) != 0) FIXME("Cannot close %d: %s\n", ossdev->fd, strerror(errno));
582 /******************************************************************
583 * OSS_ResetDevice
585 * Resets the device. OSS Commercial requires the device to be closed
586 * after a SNDCTL_DSP_RESET ioctl call... this function implements
587 * this behavior...
588 * FIXME: This causes problems when doing full duplex so we really
589 * only reset when not doing full duplex. We need to do this better
590 * someday.
592 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
594 DWORD ret = MMSYSERR_NOERROR;
595 int old_fd = ossdev->fd;
596 TRACE("(%p)\n", ossdev);
598 if (ossdev->open_count == 1) {
599 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
601 perror("ioctl SNDCTL_DSP_RESET");
602 return -1;
604 close(ossdev->fd);
605 ret = OSS_RawOpenDevice(ossdev, 1);
606 TRACE("Changing fd from %d to %d\n", old_fd, ossdev->fd);
607 } else
608 WARN("Not resetting device because it is in full duplex mode!\n");
610 return ret;
613 static const int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
614 static const int win_std_rates[5]={96000,48000,44100,22050,11025};
615 static const int win_std_formats[2][2][5]=
616 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
617 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
618 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
619 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
620 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
621 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
622 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
623 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
626 static void OSS_Info(int fd)
628 /* Note that this only reports the formats supported by the hardware.
629 * The driver may support other formats and do the conversions in
630 * software which is why we don't use this value
632 int oss_mask, oss_caps;
633 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &oss_mask) >= 0) {
634 TRACE("Formats=%08x ( ", oss_mask);
635 if (oss_mask & AFMT_MU_LAW) TRACE("AFMT_MU_LAW ");
636 if (oss_mask & AFMT_A_LAW) TRACE("AFMT_A_LAW ");
637 if (oss_mask & AFMT_IMA_ADPCM) TRACE("AFMT_IMA_ADPCM ");
638 if (oss_mask & AFMT_U8) TRACE("AFMT_U8 ");
639 if (oss_mask & AFMT_S16_LE) TRACE("AFMT_S16_LE ");
640 if (oss_mask & AFMT_S16_BE) TRACE("AFMT_S16_BE ");
641 if (oss_mask & AFMT_S8) TRACE("AFMT_S8 ");
642 if (oss_mask & AFMT_U16_LE) TRACE("AFMT_U16_LE ");
643 if (oss_mask & AFMT_U16_BE) TRACE("AFMT_U16_BE ");
644 if (oss_mask & AFMT_MPEG) TRACE("AFMT_MPEG ");
645 #ifdef AFMT_AC3
646 if (oss_mask & AFMT_AC3) TRACE("AFMT_AC3 ");
647 #endif
648 #ifdef AFMT_VORBIS
649 if (oss_mask & AFMT_VORBIS) TRACE("AFMT_VORBIS ");
650 #endif
651 #ifdef AFMT_S32_LE
652 if (oss_mask & AFMT_S32_LE) TRACE("AFMT_S32_LE ");
653 #endif
654 #ifdef AFMT_S32_BE
655 if (oss_mask & AFMT_S32_BE) TRACE("AFMT_S32_BE ");
656 #endif
657 #ifdef AFMT_FLOAT
658 if (oss_mask & AFMT_FLOAT) TRACE("AFMT_FLOAT ");
659 #endif
660 #ifdef AFMT_S24_LE
661 if (oss_mask & AFMT_S24_LE) TRACE("AFMT_S24_LE ");
662 #endif
663 #ifdef AFMT_S24_BE
664 if (oss_mask & AFMT_S24_BE) TRACE("AFMT_S24_BE ");
665 #endif
666 #ifdef AFMT_SPDIF_RAW
667 if (oss_mask & AFMT_SPDIF_RAW) TRACE("AFMT_SPDIF_RAW ");
668 #endif
669 TRACE(")\n");
671 if (ioctl(fd, SNDCTL_DSP_GETCAPS, &oss_caps) >= 0) {
672 TRACE("Caps=%08x\n",oss_caps);
673 TRACE("\tRevision: %d\n", oss_caps&DSP_CAP_REVISION);
674 TRACE("\tDuplex: %s\n", oss_caps & DSP_CAP_DUPLEX ? "true" : "false");
675 TRACE("\tRealtime: %s\n", oss_caps & DSP_CAP_REALTIME ? "true" : "false");
676 TRACE("\tBatch: %s\n", oss_caps & DSP_CAP_BATCH ? "true" : "false");
677 TRACE("\tCoproc: %s\n", oss_caps & DSP_CAP_COPROC ? "true" : "false");
678 TRACE("\tTrigger: %s\n", oss_caps & DSP_CAP_TRIGGER ? "true" : "false");
679 TRACE("\tMmap: %s\n", oss_caps & DSP_CAP_MMAP ? "true" : "false");
680 #ifdef DSP_CAP_MULTI
681 TRACE("\tMulti: %s\n", oss_caps & DSP_CAP_MULTI ? "true" : "false");
682 #endif
683 #ifdef DSP_CAP_BIND
684 TRACE("\tBind: %s\n", oss_caps & DSP_CAP_BIND ? "true" : "false");
685 #endif
686 #ifdef DSP_CAP_INPUT
687 TRACE("\tInput: %s\n", oss_caps & DSP_CAP_INPUT ? "true" : "false");
688 #endif
689 #ifdef DSP_CAP_OUTPUT
690 TRACE("\tOutput: %s\n", oss_caps & DSP_CAP_OUTPUT ? "true" : "false");
691 #endif
692 #ifdef DSP_CAP_VIRTUAL
693 TRACE("\tVirtual: %s\n", oss_caps & DSP_CAP_VIRTUAL ? "true" : "false");
694 #endif
695 #ifdef DSP_CAP_ANALOGOUT
696 TRACE("\tAnalog Out: %s\n", oss_caps & DSP_CAP_ANALOGOUT ? "true" : "false");
697 #endif
698 #ifdef DSP_CAP_ANALOGIN
699 TRACE("\tAnalog In: %s\n", oss_caps & DSP_CAP_ANALOGIN ? "true" : "false");
700 #endif
701 #ifdef DSP_CAP_DIGITALOUT
702 TRACE("\tDigital Out: %s\n", oss_caps & DSP_CAP_DIGITALOUT ? "true" : "false");
703 #endif
704 #ifdef DSP_CAP_DIGITALIN
705 TRACE("\tDigital In: %s\n", oss_caps & DSP_CAP_DIGITALIN ? "true" : "false");
706 #endif
707 #ifdef DSP_CAP_ADMASK
708 TRACE("\tA/D Mask: %s\n", oss_caps & DSP_CAP_ADMASK ? "true" : "false");
709 #endif
710 #ifdef DSP_CAP_SHADOW
711 TRACE("\tShadow: %s\n", oss_caps & DSP_CAP_SHADOW ? "true" : "false");
712 #endif
713 #ifdef DSP_CH_MASK
714 TRACE("\tChannel Mask: %x\n", oss_caps & DSP_CH_MASK);
715 #endif
716 #ifdef DSP_CAP_SLAVE
717 TRACE("\tSlave: %s\n", oss_caps & DSP_CAP_SLAVE ? "true" : "false");
718 #endif
722 /******************************************************************
723 * OSS_WaveOutInit
727 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
729 int rc,arg;
730 int f,c,r;
731 BOOL has_mixer = FALSE;
732 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
734 if (OSS_OpenDevice(ossdev, O_WRONLY, NULL, 0,-1,-1,-1) != 0)
735 return FALSE;
737 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
739 #ifdef SOUND_MIXER_INFO
741 int mixer;
742 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
743 mixer_info info;
744 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
745 lstrcpynA(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
746 strcpy(ossdev->ds_desc.szDrvname, "wineoss.drv");
747 MultiByteToWideChar(CP_ACP, 0, info.name, sizeof(info.name),
748 ossdev->out_caps.szPname,
749 sizeof(ossdev->out_caps.szPname) / sizeof(WCHAR));
750 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
751 has_mixer = TRUE;
752 } else {
753 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
754 * implement it properly, and there are probably similar issues
755 * on other platforms, so we warn but try to go ahead.
757 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
759 close(mixer);
760 } else {
761 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
764 #endif /* SOUND_MIXER_INFO */
766 if (WINE_TRACE_ON(wave))
767 OSS_Info(ossdev->fd);
769 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
770 ossdev->out_caps.wPid = 0x0001; /* Product ID */
772 ossdev->out_caps.vDriverVersion = 0x0100;
773 ossdev->out_caps.wChannels = 1;
774 ossdev->out_caps.dwFormats = 0x00000000;
775 ossdev->out_caps.wReserved1 = 0;
776 ossdev->out_caps.dwSupport = has_mixer ? WAVECAPS_VOLUME : 0;
778 /* direct sound caps */
779 ossdev->ds_caps.dwFlags = DSCAPS_CERTIFIED;
780 ossdev->ds_caps.dwPrimaryBuffers = 1;
781 ossdev->ds_caps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
782 ossdev->ds_caps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
784 /* We must first set the format and the stereo mode as some sound cards
785 * may support 44kHz mono but not 44kHz stereo. Also we must
786 * systematically check the return value of these ioctls as they will
787 * always succeed (see OSS Linux) but will modify the parameter to match
788 * whatever they support. The OSS specs also say we must first set the
789 * sample size, then the stereo and then the sample rate.
791 for (f=0;f<2;f++) {
792 arg=win_std_oss_fmts[f];
793 if (oss_locked_format>=0 && oss_locked_format!=arg) {
794 TRACE("OSS locked format is 0x%x, skipping 0x%x\n",
795 oss_locked_format,arg);
796 continue;
798 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
799 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
800 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
801 rc,arg,win_std_oss_fmts[f]);
802 continue;
804 if (f == 0)
805 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY8BIT;
806 else if (f == 1)
807 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY16BIT;
809 for (c = 1; c <= MAX_CHANNELS; c++) {
810 arg=c;
811 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
812 if( rc == -1) break;
813 if (rc!=0 || arg!=c) {
814 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
815 continue;
817 if (c == 1) {
818 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
819 } else if (c == 2) {
820 ossdev->out_caps.wChannels = 2;
821 if (has_mixer)
822 ossdev->out_caps.dwSupport|=WAVECAPS_LRVOLUME;
823 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
824 } else
825 ossdev->out_caps.wChannels = c;
827 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
828 arg=win_std_rates[r];
829 if (oss_locked_rate>=0 && oss_locked_rate!=arg) {
830 TRACE("OSS locked rate is %d, skipping %d\n",
831 oss_locked_rate,arg);
832 continue;
834 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
835 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
836 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
837 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
838 ossdev->out_caps.dwFormats|=win_std_formats[f][c-1][r];
843 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
844 if (arg & DSP_CAP_TRIGGER)
845 ossdev->bTriggerSupport = TRUE;
846 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
847 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
849 /* well, might as well use the DirectSound cap flag for something */
850 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
851 !(arg & DSP_CAP_BATCH)) {
852 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
853 } else {
854 ossdev->ds_caps.dwFlags |= DSCAPS_EMULDRIVER;
856 #ifdef DSP_CAP_MULTI /* not every oss has this */
857 /* check for hardware secondary buffer support (multi open) */
858 if ((arg & DSP_CAP_MULTI) &&
859 (ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
860 TRACE("hardware secondary buffer support available\n");
861 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARY8BIT)
862 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY8BIT;
863 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARY16BIT)
864 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY16BIT;
865 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARYMONO)
866 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYMONO;
867 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARYSTEREO)
868 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYSTEREO;
870 ossdev->ds_caps.dwMaxHwMixingAllBuffers = 16;
871 ossdev->ds_caps.dwMaxHwMixingStaticBuffers = 0;
872 ossdev->ds_caps.dwMaxHwMixingStreamingBuffers = 16;
874 ossdev->ds_caps.dwFreeHwMixingAllBuffers = 16;
875 ossdev->ds_caps.dwFreeHwMixingStaticBuffers = 0;
876 ossdev->ds_caps.dwFreeHwMixingStreamingBuffers = 16;
878 #endif
880 OSS_CloseDevice(ossdev);
881 TRACE("out wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
882 ossdev->out_caps.wChannels, ossdev->out_caps.dwFormats,
883 ossdev->out_caps.dwSupport);
884 return TRUE;
887 /******************************************************************
888 * OSS_WaveInInit
892 static BOOL OSS_WaveInInit(OSS_DEVICE* ossdev)
894 int rc,arg;
895 int f,c,r;
896 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
898 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0,-1,-1,-1) != 0)
899 return FALSE;
901 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
903 #ifdef SOUND_MIXER_INFO
905 int mixer;
906 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
907 mixer_info info;
908 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
909 MultiByteToWideChar(CP_ACP, 0, info.name, -1,
910 ossdev->in_caps.szPname,
911 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
912 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
913 } else {
914 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
915 * implement it properly, and there are probably similar issues
916 * on other platforms, so we warn but try to go ahead.
918 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
920 close(mixer);
921 } else {
922 WARN("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
925 #endif /* SOUND_MIXER_INFO */
927 if (WINE_TRACE_ON(wave))
928 OSS_Info(ossdev->fd);
930 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
931 ossdev->in_caps.wPid = 0x0001; /* Product ID */
933 ossdev->in_caps.dwFormats = 0x00000000;
934 ossdev->in_caps.wChannels = 1;
935 ossdev->in_caps.wReserved1 = 0;
937 /* direct sound caps */
938 ossdev->dsc_caps.dwSize = sizeof(ossdev->dsc_caps);
939 ossdev->dsc_caps.dwFlags = 0;
940 ossdev->dsc_caps.dwFormats = 0x00000000;
941 ossdev->dsc_caps.dwChannels = 1;
943 /* See the comment in OSS_WaveOutInit for the loop order */
944 for (f=0;f<2;f++) {
945 arg=win_std_oss_fmts[f];
946 if (oss_locked_format>=0 && oss_locked_format!=arg) {
947 TRACE("OSS locked format is 0x%x, skipping 0x%x\n",
948 oss_locked_format,arg);
949 continue;
951 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
952 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
953 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
954 rc,arg,win_std_oss_fmts[f]);
955 continue;
958 for (c = 1; c <= MAX_CHANNELS; c++) {
959 arg=c;
960 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
961 if( rc == -1) break;
962 if (rc!=0 || arg!=c) {
963 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
964 continue;
966 if (c > 1) {
967 ossdev->in_caps.wChannels = c;
968 ossdev->dsc_caps.dwChannels = c;
971 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
972 arg=win_std_rates[r];
973 if (oss_locked_rate>=0 && oss_locked_rate!=arg) {
974 TRACE("OSS locked rate is %d, skipping %d\n",
975 oss_locked_rate,arg);
976 continue;
978 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
979 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
980 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
981 ossdev->in_caps.dwFormats|=win_std_formats[f][c-1][r];
982 ossdev->dsc_caps.dwFormats|=win_std_formats[f][c-1][r];
987 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
988 if (arg & DSP_CAP_TRIGGER)
989 ossdev->bTriggerSupport = TRUE;
990 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
991 !(arg & DSP_CAP_BATCH)) {
992 /* FIXME: enable the next statement if you want to work on the driver */
993 #if 0
994 ossdev->in_caps_support |= WAVECAPS_DIRECTSOUND;
995 #endif
997 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH))
998 ossdev->in_caps_support |= WAVECAPS_SAMPLEACCURATE;
1000 OSS_CloseDevice(ossdev);
1001 TRACE("in wChannels = %d, dwFormats = %08X, in_caps_support = %08X\n",
1002 ossdev->in_caps.wChannels, ossdev->in_caps.dwFormats, ossdev->in_caps_support);
1003 return TRUE;
1006 /******************************************************************
1007 * OSS_WaveFullDuplexInit
1011 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
1013 int rc,arg;
1014 int f,c,r;
1015 int caps;
1016 BOOL has_mixer = FALSE;
1017 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
1019 if (!oss_fullduplex)
1021 TRACE("FullDuplex has been disabled.\n");
1022 ossdev->full_duplex=0;
1023 return;
1026 /* The OSS documentation says we must call SNDCTL_SETDUPLEX
1027 * *before* checking for SNDCTL_DSP_GETCAPS otherwise we may
1028 * get the wrong result. This ioctl must even be done before
1029 * setting the fragment size so that only OSS_RawOpenDevice is
1030 * in a position to do it. So we set full_duplex speculatively
1031 * and adjust right after.
1033 ossdev->full_duplex=1;
1034 rc=OSS_OpenDevice(ossdev, O_RDWR, NULL, 0,-1,-1,-1);
1035 ossdev->full_duplex=0;
1036 if (rc != 0)
1037 return;
1039 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
1041 #ifdef SOUND_MIXER_INFO
1043 int mixer;
1044 if ((mixer = open(ossdev->mixer_name, O_RDWR|O_NDELAY)) >= 0) {
1045 mixer_info info;
1046 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
1047 has_mixer = TRUE;
1048 } else {
1049 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
1050 * implement it properly, and there are probably similar issues
1051 * on other platforms, so we warn but try to go ahead.
1053 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
1055 close(mixer);
1056 } else {
1057 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
1060 #endif /* SOUND_MIXER_INFO */
1062 TRACE("%s\n", ossdev->ds_desc.szDesc);
1064 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
1065 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
1067 ossdev->duplex_out_caps = ossdev->out_caps;
1069 ossdev->duplex_out_caps.wChannels = 1;
1070 ossdev->duplex_out_caps.dwFormats = 0x00000000;
1071 ossdev->duplex_out_caps.dwSupport = has_mixer ? WAVECAPS_VOLUME : 0;
1073 if (WINE_TRACE_ON(wave))
1074 OSS_Info(ossdev->fd);
1076 /* See the comment in OSS_WaveOutInit for the loop order */
1077 for (f=0;f<2;f++) {
1078 arg=win_std_oss_fmts[f];
1079 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
1080 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
1081 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1082 rc,arg,win_std_oss_fmts[f]);
1083 continue;
1086 for (c = 1; c <= MAX_CHANNELS; c++) {
1087 arg=c;
1088 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
1089 if( rc == -1) break;
1090 if (rc!=0 || arg!=c) {
1091 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
1092 continue;
1094 if (c == 1) {
1095 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
1096 } else if (c == 2) {
1097 ossdev->duplex_out_caps.wChannels = 2;
1098 if (has_mixer)
1099 ossdev->duplex_out_caps.dwSupport|=WAVECAPS_LRVOLUME;
1100 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
1101 } else
1102 ossdev->duplex_out_caps.wChannels = c;
1104 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
1105 arg=win_std_rates[r];
1106 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
1107 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
1108 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
1109 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
1110 ossdev->duplex_out_caps.dwFormats|=win_std_formats[f][c-1][r];
1115 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
1116 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
1117 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
1119 /* well, might as well use the DirectSound cap flag for something */
1120 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
1121 !(arg & DSP_CAP_BATCH)) {
1122 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
1125 OSS_CloseDevice(ossdev);
1126 TRACE("duplex wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
1127 ossdev->duplex_out_caps.wChannels,
1128 ossdev->duplex_out_caps.dwFormats,
1129 ossdev->duplex_out_caps.dwSupport);
1132 static char* StrDup(const char* str, const char* def)
1134 char* dst;
1135 if (str==NULL)
1136 str=def;
1137 dst=HeapAlloc(GetProcessHeap(),0,strlen(str)+1);
1138 strcpy(dst, str);
1139 return dst;
1142 /******************************************************************
1143 * OSS_WaveInit
1145 * Initialize internal structures from OSS information
1147 LRESULT OSS_WaveInit(void)
1149 HKEY hKey;
1150 char* str;
1151 int i;
1153 TRACE("()\n");
1155 /* @@ Wine registry key: HKCU\Software\Wine\OSS Driver */
1156 if (!RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\OSS Driver", &hKey)) {
1157 char buffer[256];
1158 DWORD type,size;
1160 size = sizeof(buffer);
1161 if (!RegQueryValueExA(hKey, "LockFormat", 0, &type, (LPVOID)buffer, &size))
1162 oss_locked_format=atoi(buffer);
1163 size = sizeof(buffer);
1164 if (!RegQueryValueExA(hKey, "LockStereo", 0, &type, (LPVOID)buffer, &size))
1165 oss_locked_stereo=atoi(buffer);
1166 size = sizeof(buffer);
1167 if (!RegQueryValueExA(hKey, "LockRate", 0, &type, (LPVOID)buffer, &size))
1168 oss_locked_rate=atoi(buffer);
1169 size = sizeof(buffer);
1170 if (!RegQueryValueExA(hKey, "FullDuplex", 0, &type, (LPVOID)buffer, &size))
1171 oss_fullduplex=atoi(buffer);
1172 RegCloseKey(hKey);
1175 str=getenv("AUDIODEV");
1176 if (str!=NULL)
1178 OSS_Devices[0].dev_name=StrDup(str,"");
1179 OSS_Devices[0].mixer_name=StrDup(getenv("MIXERDEV"),"/dev/mixer");
1180 for (i = 1; i < MAX_WAVEDRV; ++i)
1182 OSS_Devices[i].dev_name=StrDup("",NULL);
1183 OSS_Devices[i].mixer_name=StrDup("",NULL);
1186 else
1188 OSS_Devices[0].dev_name=StrDup("/dev/dsp",NULL);
1189 OSS_Devices[0].mixer_name=StrDup("/dev/mixer",NULL);
1190 for (i = 1; i < MAX_WAVEDRV; ++i)
1192 OSS_Devices[i].dev_name=HeapAlloc(GetProcessHeap(),0,11);
1193 sprintf(OSS_Devices[i].dev_name, "/dev/dsp%d", i);
1194 OSS_Devices[i].mixer_name=HeapAlloc(GetProcessHeap(),0,13);
1195 sprintf(OSS_Devices[i].mixer_name, "/dev/mixer%d", i);
1199 for (i = 0; i < MAX_WAVEDRV; ++i)
1201 OSS_Devices[i].interface_name=HeapAlloc(GetProcessHeap(),0,9+strlen(OSS_Devices[i].dev_name)+1);
1202 sprintf(OSS_Devices[i].interface_name, "wineoss: %s", OSS_Devices[i].dev_name);
1205 /* start with output devices */
1206 for (i = 0; i < MAX_WAVEDRV; ++i)
1208 if (*OSS_Devices[i].dev_name=='\0' || OSS_WaveOutInit(&OSS_Devices[i]))
1210 WOutDev[numOutDev].state = WINE_WS_CLOSED;
1211 WOutDev[numOutDev].ossdev = &OSS_Devices[i];
1212 WOutDev[numOutDev].volume = 0xffffffff;
1213 numOutDev++;
1217 /* then do input devices */
1218 for (i = 0; i < MAX_WAVEDRV; ++i)
1220 if (*OSS_Devices[i].dev_name=='\0' || OSS_WaveInInit(&OSS_Devices[i]))
1222 WInDev[numInDev].state = WINE_WS_CLOSED;
1223 WInDev[numInDev].ossdev = &OSS_Devices[i];
1224 numInDev++;
1228 /* finish with the full duplex bits */
1229 for (i = 0; i < MAX_WAVEDRV; i++)
1230 if (*OSS_Devices[i].dev_name!='\0')
1231 OSS_WaveFullDuplexInit(&OSS_Devices[i]);
1233 TRACE("%d wave out devices\n", numOutDev);
1234 for (i = 0; i < numOutDev; i++) {
1235 TRACE("%d: %s, %s, %s\n", i, WOutDev[i].ossdev->dev_name,
1236 WOutDev[i].ossdev->mixer_name, WOutDev[i].ossdev->interface_name);
1239 TRACE("%d wave in devices\n", numInDev);
1240 for (i = 0; i < numInDev; i++) {
1241 TRACE("%d: %s, %s, %s\n", i, WInDev[i].ossdev->dev_name,
1242 WInDev[i].ossdev->mixer_name, WInDev[i].ossdev->interface_name);
1245 return 0;
1248 /******************************************************************
1249 * OSS_WaveExit
1251 * Delete/clear internal structures of OSS information
1253 LRESULT OSS_WaveExit(void)
1255 int i;
1256 TRACE("()\n");
1258 for (i = 0; i < MAX_WAVEDRV; ++i)
1260 HeapFree(GetProcessHeap(), 0, OSS_Devices[i].dev_name);
1261 HeapFree(GetProcessHeap(), 0, OSS_Devices[i].mixer_name);
1262 HeapFree(GetProcessHeap(), 0, OSS_Devices[i].interface_name);
1265 ZeroMemory(OSS_Devices, sizeof(OSS_Devices));
1266 ZeroMemory(WOutDev, sizeof(WOutDev));
1267 ZeroMemory(WInDev, sizeof(WInDev));
1269 numOutDev = 0;
1270 numInDev = 0;
1272 return 0;
1275 /******************************************************************
1276 * OSS_InitRingMessage
1278 * Initialize the ring of messages for passing between driver's caller and playback/record
1279 * thread
1281 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
1283 omr->msg_toget = 0;
1284 omr->msg_tosave = 0;
1285 #ifdef USE_PIPE_SYNC
1286 if (pipe(omr->msg_pipe) < 0) {
1287 omr->msg_pipe[0] = -1;
1288 omr->msg_pipe[1] = -1;
1289 ERR("could not create pipe, error=%s\n", strerror(errno));
1291 #else
1292 omr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1293 #endif
1294 omr->ring_buffer_size = OSS_RING_BUFFER_INCREMENT;
1295 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(OSS_MSG));
1296 InitializeCriticalSection(&omr->msg_crst);
1297 omr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": OSS_MSG_RING.msg_crst");
1298 return 0;
1301 /******************************************************************
1302 * OSS_DestroyRingMessage
1305 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
1307 #ifdef USE_PIPE_SYNC
1308 close(omr->msg_pipe[0]);
1309 close(omr->msg_pipe[1]);
1310 #else
1311 CloseHandle(omr->msg_event);
1312 #endif
1313 HeapFree(GetProcessHeap(),0,omr->messages);
1314 omr->msg_crst.DebugInfo->Spare[0] = 0;
1315 DeleteCriticalSection(&omr->msg_crst);
1316 return 0;
1319 /******************************************************************
1320 * OSS_AddRingMessage
1322 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
1324 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
1326 HANDLE hEvent = INVALID_HANDLE_VALUE;
1328 EnterCriticalSection(&omr->msg_crst);
1329 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
1331 int old_ring_buffer_size = omr->ring_buffer_size;
1332 omr->ring_buffer_size += OSS_RING_BUFFER_INCREMENT;
1333 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
1334 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(OSS_MSG));
1335 /* Now we need to rearrange the ring buffer so that the new
1336 buffers just allocated are in between omr->msg_tosave and
1337 omr->msg_toget.
1339 if (omr->msg_tosave < omr->msg_toget)
1341 memmove(&(omr->messages[omr->msg_toget + OSS_RING_BUFFER_INCREMENT]),
1342 &(omr->messages[omr->msg_toget]),
1343 sizeof(OSS_MSG)*(old_ring_buffer_size - omr->msg_toget)
1345 omr->msg_toget += OSS_RING_BUFFER_INCREMENT;
1348 if (wait)
1350 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1351 if (hEvent == INVALID_HANDLE_VALUE)
1353 ERR("can't create event !?\n");
1354 LeaveCriticalSection(&omr->msg_crst);
1355 return 0;
1357 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
1358 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1359 omr->msg_toget,getCmdString(omr->messages[omr->msg_toget].msg),
1360 omr->msg_tosave,getCmdString(omr->messages[omr->msg_tosave].msg));
1362 /* fast messages have to be added at the start of the queue */
1363 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
1364 omr->messages[omr->msg_toget].msg = msg;
1365 omr->messages[omr->msg_toget].param = param;
1366 omr->messages[omr->msg_toget].hEvent = hEvent;
1368 else
1370 omr->messages[omr->msg_tosave].msg = msg;
1371 omr->messages[omr->msg_tosave].param = param;
1372 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
1373 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
1375 LeaveCriticalSection(&omr->msg_crst);
1376 /* signal a new message */
1377 SIGNAL_OMR(omr);
1378 if (wait)
1380 /* wait for playback/record thread to have processed the message */
1381 WaitForSingleObject(hEvent, INFINITE);
1382 CloseHandle(hEvent);
1384 return 1;
1387 /******************************************************************
1388 * OSS_RetrieveRingMessage
1390 * Get a message from the ring. Should be called by the playback/record thread.
1392 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
1393 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
1395 EnterCriticalSection(&omr->msg_crst);
1397 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1399 LeaveCriticalSection(&omr->msg_crst);
1400 return 0;
1403 *msg = omr->messages[omr->msg_toget].msg;
1404 omr->messages[omr->msg_toget].msg = 0;
1405 *param = omr->messages[omr->msg_toget].param;
1406 *hEvent = omr->messages[omr->msg_toget].hEvent;
1407 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
1408 CLEAR_OMR(omr);
1409 LeaveCriticalSection(&omr->msg_crst);
1410 return 1;
1413 /******************************************************************
1414 * OSS_PeekRingMessage
1416 * Peek at a message from the ring but do not remove it.
1417 * Should be called by the playback/record thread.
1419 static int OSS_PeekRingMessage(OSS_MSG_RING* omr,
1420 enum win_wm_message *msg,
1421 DWORD *param, HANDLE *hEvent)
1423 EnterCriticalSection(&omr->msg_crst);
1425 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1427 LeaveCriticalSection(&omr->msg_crst);
1428 return 0;
1431 *msg = omr->messages[omr->msg_toget].msg;
1432 *param = omr->messages[omr->msg_toget].param;
1433 *hEvent = omr->messages[omr->msg_toget].hEvent;
1434 LeaveCriticalSection(&omr->msg_crst);
1435 return 1;
1438 /*======================================================================*
1439 * Low level WAVE OUT implementation *
1440 *======================================================================*/
1442 /**************************************************************************
1443 * wodNotifyClient [internal]
1445 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1447 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04X dwParam2 = %04X\n", wMsg,
1448 wMsg == WOM_OPEN ? "WOM_OPEN" : wMsg == WOM_CLOSE ? "WOM_CLOSE" :
1449 wMsg == WOM_DONE ? "WOM_DONE" : "Unknown", dwParam1, dwParam2);
1451 switch (wMsg) {
1452 case WOM_OPEN:
1453 case WOM_CLOSE:
1454 case WOM_DONE:
1455 if (wwo->wFlags != DCB_NULL &&
1456 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
1457 (HDRVR)wwo->waveDesc.hWave, wMsg,
1458 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
1459 WARN("can't notify client !\n");
1460 return MMSYSERR_ERROR;
1462 break;
1463 default:
1464 FIXME("Unknown callback message %u\n", wMsg);
1465 return MMSYSERR_INVALPARAM;
1467 return MMSYSERR_NOERROR;
1470 /**************************************************************************
1471 * wodUpdatePlayedTotal [internal]
1474 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
1476 audio_buf_info dspspace;
1477 DWORD notplayed;
1478 if (!info) info = &dspspace;
1480 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
1481 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
1482 return FALSE;
1485 /* GETOSPACE is not always accurate when we're down to the last fragment or two;
1486 ** we try to accommodate that here by assuming that the dsp is empty by looking
1487 ** at the clock rather than the result of GETOSPACE */
1488 notplayed = wwo->dwBufferSize - info->bytes;
1489 if (notplayed > 0 && notplayed < (info->fragsize * 2))
1491 if (wwo->dwProjectedFinishTime && GetTickCount() >= wwo->dwProjectedFinishTime)
1493 TRACE("Adjusting for a presumed OSS bug and assuming all data has been played.\n");
1494 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1495 return TRUE;
1497 else
1498 /* Some OSS drivers will clean up nicely if given a POST, so give 'em the chance... */
1499 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1502 wwo->dwPlayedTotal = wwo->dwWrittenTotal - notplayed;
1503 return TRUE;
1506 /**************************************************************************
1507 * wodPlayer_BeginWaveHdr [internal]
1509 * Makes the specified lpWaveHdr the currently playing wave header.
1510 * If the specified wave header is a begin loop and we're not already in
1511 * a loop, setup the loop.
1513 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1515 wwo->lpPlayPtr = lpWaveHdr;
1517 if (!lpWaveHdr) return;
1519 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1520 if (wwo->lpLoopPtr) {
1521 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1522 } else {
1523 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1524 wwo->lpLoopPtr = lpWaveHdr;
1525 /* Windows does not touch WAVEHDR.dwLoops,
1526 * so we need to make an internal copy */
1527 wwo->dwLoops = lpWaveHdr->dwLoops;
1530 wwo->dwPartialOffset = 0;
1533 /**************************************************************************
1534 * wodPlayer_PlayPtrNext [internal]
1536 * Advance the play pointer to the next waveheader, looping if required.
1538 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
1540 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1542 wwo->dwPartialOffset = 0;
1543 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1544 /* We're at the end of a loop, loop if required */
1545 if (--wwo->dwLoops > 0) {
1546 wwo->lpPlayPtr = wwo->lpLoopPtr;
1547 } else {
1548 /* Handle overlapping loops correctly */
1549 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1550 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1551 /* shall we consider the END flag for the closing loop or for
1552 * the opening one or for both ???
1553 * code assumes for closing loop only
1555 } else {
1556 lpWaveHdr = lpWaveHdr->lpNext;
1558 wwo->lpLoopPtr = NULL;
1559 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1561 } else {
1562 /* We're not in a loop. Advance to the next wave header */
1563 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1566 return lpWaveHdr;
1569 /**************************************************************************
1570 * wodPlayer_TicksTillEmpty [internal]
1571 * Returns the number of ticks until we think the DSP should be empty
1573 static DWORD wodPlayer_TicksTillEmpty(const WINE_WAVEOUT *wwo)
1575 return ((wwo->dwWrittenTotal - wwo->dwPlayedTotal) * 1000)
1576 / wwo->waveFormat.Format.nAvgBytesPerSec;
1579 /**************************************************************************
1580 * wodPlayer_DSPWait [internal]
1581 * Returns the number of milliseconds to wait for the DSP buffer to write
1582 * one fragment.
1584 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1586 /* time for one fragment to be played */
1587 return wwo->dwFragmentSize * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1590 /**************************************************************************
1591 * wodPlayer_NotifyWait [internal]
1592 * Returns the number of milliseconds to wait before attempting to notify
1593 * completion of the specified wavehdr.
1594 * This is based on the number of bytes remaining to be written in the
1595 * wave.
1597 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1599 DWORD dwMillis;
1601 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1602 dwMillis = 1;
1603 } else {
1604 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1605 if (!dwMillis) dwMillis = 1;
1608 return dwMillis;
1612 /**************************************************************************
1613 * wodPlayer_WriteMaxFrags [internal]
1614 * Writes the maximum number of bytes possible to the DSP and returns
1615 * TRUE iff the current playPtr has been fully played
1617 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
1619 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1620 DWORD toWrite = min(dwLength, *bytes);
1621 int written;
1622 BOOL ret = FALSE;
1624 TRACE("Writing wavehdr %p.%u[%u]/%u\n",
1625 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
1627 if (toWrite > 0)
1629 written = write(wwo->ossdev->fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
1630 if (written <= 0) {
1631 TRACE("write(%s, %p, %d) failed (%s) returned %d\n", wwo->ossdev->dev_name,
1632 wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite, strerror(errno), written);
1633 return FALSE;
1636 else
1637 written = 0;
1639 if (written >= dwLength) {
1640 /* If we wrote all current wavehdr, skip to the next one */
1641 wodPlayer_PlayPtrNext(wwo);
1642 ret = TRUE;
1643 } else {
1644 /* Remove the amount written */
1645 wwo->dwPartialOffset += written;
1647 *bytes -= written;
1648 wwo->dwWrittenTotal += written;
1649 TRACE("dwWrittenTotal=%u\n", wwo->dwWrittenTotal);
1650 return ret;
1654 /**************************************************************************
1655 * wodPlayer_NotifyCompletions [internal]
1657 * Notifies and remove from queue all wavehdrs which have been played to
1658 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1659 * we notify all wavehdrs and remove them all from the queue even if they
1660 * are unplayed or part of a loop.
1662 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1664 LPWAVEHDR lpWaveHdr;
1666 /* Start from lpQueuePtr and keep notifying until:
1667 * - we hit an unwritten wavehdr
1668 * - we hit the beginning of a running loop
1669 * - we hit a wavehdr which hasn't finished playing
1671 #if 0
1672 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1673 (force ||
1674 (lpWaveHdr != wwo->lpPlayPtr &&
1675 lpWaveHdr != wwo->lpLoopPtr &&
1676 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1678 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1680 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1681 lpWaveHdr->dwFlags |= WHDR_DONE;
1683 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1685 #else
1686 for (;;)
1688 lpWaveHdr = wwo->lpQueuePtr;
1689 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
1690 if (!force)
1692 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
1693 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
1694 if (lpWaveHdr->reserved > wwo->dwPlayedTotal) {TRACE("still playing %p (%u/%u)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
1696 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1698 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1699 lpWaveHdr->dwFlags |= WHDR_DONE;
1701 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1703 #endif
1704 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1705 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1708 /**************************************************************************
1709 * wodPlayer_Reset [internal]
1711 * wodPlayer helper. Resets current output stream.
1713 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
1715 wodUpdatePlayedTotal(wwo, NULL);
1716 /* updates current notify list */
1717 wodPlayer_NotifyCompletions(wwo, FALSE);
1719 /* flush all possible output */
1720 if (OSS_ResetDevice(wwo->ossdev) != MMSYSERR_NOERROR)
1722 wwo->hThread = 0;
1723 wwo->state = WINE_WS_STOPPED;
1724 ExitThread(-1);
1727 if (reset) {
1728 enum win_wm_message msg;
1729 DWORD param;
1730 HANDLE ev;
1732 /* remove any buffer */
1733 wodPlayer_NotifyCompletions(wwo, TRUE);
1735 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1736 wwo->state = WINE_WS_STOPPED;
1737 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1738 /* Clear partial wavehdr */
1739 wwo->dwPartialOffset = 0;
1741 /* remove any existing message in the ring */
1742 EnterCriticalSection(&wwo->msgRing.msg_crst);
1743 /* return all pending headers in queue */
1744 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1746 if (msg != WINE_WM_HEADER)
1748 FIXME("shouldn't have headers left\n");
1749 SetEvent(ev);
1750 continue;
1752 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1753 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1755 wodNotifyClient(wwo, WOM_DONE, param, 0);
1757 RESET_OMR(&wwo->msgRing);
1758 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1759 } else {
1760 if (wwo->lpLoopPtr) {
1761 /* complicated case, not handled yet (could imply modifying the loop counter */
1762 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1763 wwo->lpPlayPtr = wwo->lpLoopPtr;
1764 wwo->dwPartialOffset = 0;
1765 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1766 } else {
1767 LPWAVEHDR ptr;
1768 DWORD sz = wwo->dwPartialOffset;
1770 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1771 /* compute the max size playable from lpQueuePtr */
1772 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1773 sz += ptr->dwBufferLength;
1775 /* because the reset lpPlayPtr will be lpQueuePtr */
1776 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1777 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1778 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1779 wwo->lpPlayPtr = wwo->lpQueuePtr;
1781 wwo->state = WINE_WS_PAUSED;
1785 /**************************************************************************
1786 * wodPlayer_ProcessMessages [internal]
1788 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1790 LPWAVEHDR lpWaveHdr;
1791 enum win_wm_message msg;
1792 DWORD param;
1793 HANDLE ev;
1795 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1796 TRACE("Received %s %x\n", getCmdString(msg), param);
1797 switch (msg) {
1798 case WINE_WM_PAUSING:
1799 wodPlayer_Reset(wwo, FALSE);
1800 SetEvent(ev);
1801 break;
1802 case WINE_WM_RESTARTING:
1803 if (wwo->state == WINE_WS_PAUSED)
1805 wwo->state = WINE_WS_PLAYING;
1807 SetEvent(ev);
1808 break;
1809 case WINE_WM_HEADER:
1810 lpWaveHdr = (LPWAVEHDR)param;
1812 /* insert buffer at the end of queue */
1814 LPWAVEHDR* wh;
1815 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1816 *wh = lpWaveHdr;
1818 if (!wwo->lpPlayPtr)
1819 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1820 if (wwo->state == WINE_WS_STOPPED)
1821 wwo->state = WINE_WS_PLAYING;
1822 break;
1823 case WINE_WM_RESETTING:
1824 wodPlayer_Reset(wwo, TRUE);
1825 SetEvent(ev);
1826 break;
1827 case WINE_WM_UPDATE:
1828 wodUpdatePlayedTotal(wwo, NULL);
1829 SetEvent(ev);
1830 break;
1831 case WINE_WM_BREAKLOOP:
1832 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1833 /* ensure exit at end of current loop */
1834 wwo->dwLoops = 1;
1836 SetEvent(ev);
1837 break;
1838 case WINE_WM_CLOSING:
1839 /* sanity check: this should not happen since the device must have been reset before */
1840 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1841 wwo->hThread = 0;
1842 wwo->state = WINE_WS_CLOSED;
1843 SetEvent(ev);
1844 ExitThread(0);
1845 /* shouldn't go here */
1846 default:
1847 FIXME("unknown message %d\n", msg);
1848 break;
1853 /**************************************************************************
1854 * wodPlayer_FeedDSP [internal]
1855 * Feed as much sound data as we can into the DSP and return the number of
1856 * milliseconds before it will be necessary to feed the DSP again.
1858 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1860 audio_buf_info dspspace;
1861 DWORD availInQ;
1863 if (!wodUpdatePlayedTotal(wwo, &dspspace)) return INFINITE;
1864 availInQ = dspspace.bytes;
1865 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1866 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1868 /* no more room... no need to try to feed */
1869 if (dspspace.fragments != 0) {
1870 /* Feed from partial wavehdr */
1871 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1872 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1875 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1876 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1877 do {
1878 TRACE("Setting time to elapse for %p to %u\n",
1879 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1880 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1881 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1882 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1885 if (wwo->bNeedPost) {
1886 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1887 * if it didn't get one, we give it the other */
1888 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1889 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1890 wwo->bNeedPost = FALSE;
1894 return wodPlayer_DSPWait(wwo);
1898 /**************************************************************************
1899 * wodPlayer [internal]
1901 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1903 WORD uDevID = (DWORD)pmt;
1904 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1905 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1906 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1907 DWORD dwSleepTime;
1909 wwo->state = WINE_WS_STOPPED;
1910 SetEvent(wwo->hStartUpEvent);
1912 for (;;) {
1913 /** Wait for the shortest time before an action is required. If there
1914 * are no pending actions, wait forever for a command.
1916 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1917 TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1918 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1919 wodPlayer_ProcessMessages(wwo);
1920 if (wwo->state == WINE_WS_PLAYING) {
1921 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1922 if (dwNextFeedTime != INFINITE)
1923 wwo->dwProjectedFinishTime = GetTickCount() + wodPlayer_TicksTillEmpty(wwo);
1924 else
1925 wwo->dwProjectedFinishTime = 0;
1927 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1928 if (dwNextFeedTime == INFINITE) {
1929 /* FeedDSP ran out of data, but before flushing, */
1930 /* check that a notification didn't give us more */
1931 wodPlayer_ProcessMessages(wwo);
1932 if (!wwo->lpPlayPtr) {
1933 TRACE("flushing\n");
1934 ioctl(wwo->ossdev->fd, SNDCTL_DSP_SYNC, 0);
1935 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1936 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1937 } else {
1938 TRACE("recovering\n");
1939 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1942 } else {
1943 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1948 /**************************************************************************
1949 * wodGetDevCaps [internal]
1951 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1953 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1955 if (lpCaps == NULL) {
1956 WARN("not enabled\n");
1957 return MMSYSERR_NOTENABLED;
1960 if (wDevID >= numOutDev) {
1961 WARN("numOutDev reached !\n");
1962 return MMSYSERR_BADDEVICEID;
1965 if (WOutDev[wDevID].ossdev->open_access == O_RDWR)
1966 memcpy(lpCaps, &WOutDev[wDevID].ossdev->duplex_out_caps, min(dwSize, sizeof(*lpCaps)));
1967 else
1968 memcpy(lpCaps, &WOutDev[wDevID].ossdev->out_caps, min(dwSize, sizeof(*lpCaps)));
1970 return MMSYSERR_NOERROR;
1973 /**************************************************************************
1974 * wodOpen [internal]
1976 DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1978 int audio_fragment;
1979 WINE_WAVEOUT* wwo;
1980 audio_buf_info info;
1981 DWORD ret;
1983 TRACE("(%u, %p[cb=%08x], %08X);\n", wDevID, lpDesc, lpDesc->dwCallback, dwFlags);
1984 if (lpDesc == NULL) {
1985 WARN("Invalid Parameter !\n");
1986 return MMSYSERR_INVALPARAM;
1988 if (wDevID >= numOutDev) {
1989 TRACE("MAX_WAVOUTDRV reached !\n");
1990 return MMSYSERR_BADDEVICEID;
1993 /* only PCM format is supported so far... */
1994 if (!supportedFormat(lpDesc->lpFormat)) {
1995 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1996 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1997 lpDesc->lpFormat->nSamplesPerSec);
1998 return WAVERR_BADFORMAT;
2001 if (dwFlags & WAVE_FORMAT_QUERY) {
2002 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2003 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2004 lpDesc->lpFormat->nSamplesPerSec);
2005 return MMSYSERR_NOERROR;
2008 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2009 lpDesc->lpFormat->nSamplesPerSec,
2010 lpDesc->lpFormat->wBitsPerSample,
2011 lpDesc->lpFormat->nChannels,
2012 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
2013 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
2014 "UNSUPPORTED");
2016 wwo = &WOutDev[wDevID];
2018 if ((dwFlags & WAVE_DIRECTSOUND) &&
2019 !(wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
2020 /* not supported, ignore it */
2021 dwFlags &= ~WAVE_DIRECTSOUND;
2023 if (dwFlags & WAVE_DIRECTSOUND) {
2024 if (wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2025 /* we have realtime DirectSound, fragments just waste our time,
2026 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2027 audio_fragment = 0x0020000B;
2028 else
2029 /* to approximate realtime, we must use small fragments,
2030 * let's try to fragment the above 64KB (256 * 2^8) */
2031 audio_fragment = 0x01000008;
2032 } else {
2033 /* A wave device must have a worst case latency of 10 ms so calculate
2034 * the largest fragment size less than 10 ms long.
2036 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2037 int shift = 0;
2038 while ((1 << shift) <= fsize)
2039 shift++;
2040 shift--;
2041 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2044 TRACE("requesting %d %d byte fragments (%d ms/fragment)\n",
2045 audio_fragment >> 16, 1 << (audio_fragment & 0xffff),
2046 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2048 if (wwo->state != WINE_WS_CLOSED) {
2049 WARN("already allocated\n");
2050 return MMSYSERR_ALLOCATED;
2053 /* we want to be able to mmap() the device, which means it must be opened readable,
2054 * otherwise mmap() will fail (at least under Linux) */
2055 ret = OSS_OpenDevice(wwo->ossdev,
2056 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
2057 &audio_fragment,
2058 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
2059 lpDesc->lpFormat->nSamplesPerSec,
2060 lpDesc->lpFormat->nChannels,
2061 (lpDesc->lpFormat->wBitsPerSample == 16)
2062 ? AFMT_S16_LE : AFMT_U8);
2063 if ((ret==MMSYSERR_NOERROR) && (dwFlags & WAVE_DIRECTSOUND)) {
2064 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev->sample_rate;
2065 lpDesc->lpFormat->nChannels=wwo->ossdev->channels;
2066 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev->format == AFMT_U8 ? 8 : 16);
2067 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
2068 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
2069 TRACE("OSS_OpenDevice returned this format: %dx%dx%d\n",
2070 lpDesc->lpFormat->nSamplesPerSec,
2071 lpDesc->lpFormat->wBitsPerSample,
2072 lpDesc->lpFormat->nChannels);
2074 if (ret != 0) return ret;
2075 wwo->state = WINE_WS_STOPPED;
2077 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2079 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2080 copy_format(lpDesc->lpFormat, &wwo->waveFormat);
2082 if (wwo->waveFormat.Format.wBitsPerSample == 0) {
2083 WARN("Resetting zeroed wBitsPerSample\n");
2084 wwo->waveFormat.Format.wBitsPerSample = 8 *
2085 (wwo->waveFormat.Format.nAvgBytesPerSec /
2086 wwo->waveFormat.Format.nSamplesPerSec) /
2087 wwo->waveFormat.Format.nChannels;
2089 /* Read output space info for future reference */
2090 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2091 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
2092 OSS_CloseDevice(wwo->ossdev);
2093 wwo->state = WINE_WS_CLOSED;
2094 return MMSYSERR_NOTENABLED;
2097 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2098 info.fragsize, (info.fragsize * 1000) / (wwo->ossdev->sample_rate *
2099 wwo->ossdev->channels * (wwo->ossdev->format == AFMT_U8 ? 1 : 2)));
2101 /* Check that fragsize is correct per our settings above */
2102 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
2103 /* we've tried to set 1K fragments or less, but it didn't work */
2104 ERR("fragment size set failed, size is now %d\n", info.fragsize);
2105 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
2106 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
2109 /* Remember fragsize and total buffer size for future use */
2110 wwo->dwFragmentSize = info.fragsize;
2111 wwo->dwBufferSize = info.fragstotal * info.fragsize;
2112 wwo->dwPlayedTotal = 0;
2113 wwo->dwWrittenTotal = 0;
2114 wwo->bNeedPost = TRUE;
2116 TRACE("fd=%d fragstotal=%d fragsize=%d BufferSize=%d\n",
2117 wwo->ossdev->fd, info.fragstotal, info.fragsize, wwo->dwBufferSize);
2118 if (wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign) {
2119 ERR("Fragment doesn't contain an integral number of data blocks fragsize=%d BlockAlign=%d\n",wwo->dwFragmentSize,wwo->waveFormat.Format.nBlockAlign);
2120 /* Some SoundBlaster 16 cards return an incorrect (odd) fragment
2121 * size for 16 bit sound. This will cause a system crash when we try
2122 * to write just the specified odd number of bytes. So if we
2123 * detect something is wrong we'd better fix it.
2125 wwo->dwFragmentSize-=wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign;
2128 OSS_InitRingMessage(&wwo->msgRing);
2130 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2131 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
2132 if (wwo->hThread)
2133 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2134 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
2135 CloseHandle(wwo->hStartUpEvent);
2136 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
2138 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2139 wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
2140 wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
2141 wwo->waveFormat.Format.nBlockAlign);
2143 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
2146 /**************************************************************************
2147 * wodClose [internal]
2149 static DWORD wodClose(WORD wDevID)
2151 DWORD ret = MMSYSERR_NOERROR;
2152 WINE_WAVEOUT* wwo;
2154 TRACE("(%u);\n", wDevID);
2156 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2157 WARN("bad device ID !\n");
2158 return MMSYSERR_BADDEVICEID;
2161 wwo = &WOutDev[wDevID];
2162 if (wwo->lpQueuePtr) {
2163 WARN("buffers still playing !\n");
2164 ret = WAVERR_STILLPLAYING;
2165 } else {
2166 if (wwo->hThread != INVALID_HANDLE_VALUE) {
2167 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
2170 OSS_DestroyRingMessage(&wwo->msgRing);
2172 OSS_CloseDevice(wwo->ossdev);
2173 wwo->state = WINE_WS_CLOSED;
2174 wwo->dwFragmentSize = 0;
2175 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
2177 return ret;
2180 /**************************************************************************
2181 * wodWrite [internal]
2184 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2186 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
2188 /* first, do the sanity checks... */
2189 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2190 WARN("bad dev ID !\n");
2191 return MMSYSERR_BADDEVICEID;
2194 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
2195 return WAVERR_UNPREPARED;
2197 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2198 return WAVERR_STILLPLAYING;
2200 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2201 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2202 lpWaveHdr->lpNext = 0;
2204 if ((lpWaveHdr->dwBufferLength & (WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1)) != 0)
2206 WARN("WaveHdr length isn't a multiple of the PCM block size: %d %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].waveFormat.Format.nBlockAlign);
2207 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1);
2210 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2212 return MMSYSERR_NOERROR;
2215 /**************************************************************************
2216 * wodPause [internal]
2218 static DWORD wodPause(WORD wDevID)
2220 TRACE("(%u);!\n", wDevID);
2222 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2223 WARN("bad device ID !\n");
2224 return MMSYSERR_BADDEVICEID;
2227 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
2229 return MMSYSERR_NOERROR;
2232 /**************************************************************************
2233 * wodRestart [internal]
2235 static DWORD wodRestart(WORD wDevID)
2237 TRACE("(%u);\n", wDevID);
2239 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2240 WARN("bad device ID !\n");
2241 return MMSYSERR_BADDEVICEID;
2244 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2246 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2247 /* FIXME: Myst crashes with this ... hmm -MM
2248 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2251 return MMSYSERR_NOERROR;
2254 /**************************************************************************
2255 * wodReset [internal]
2257 static DWORD wodReset(WORD wDevID)
2259 TRACE("(%u);\n", wDevID);
2261 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2262 WARN("bad device ID !\n");
2263 return MMSYSERR_BADDEVICEID;
2266 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2268 return MMSYSERR_NOERROR;
2271 /**************************************************************************
2272 * wodGetPosition [internal]
2274 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2276 WINE_WAVEOUT* wwo;
2278 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
2280 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2281 WARN("bad device ID !\n");
2282 return MMSYSERR_BADDEVICEID;
2285 if (lpTime == NULL) {
2286 WARN("invalid parameter: lpTime == NULL\n");
2287 return MMSYSERR_INVALPARAM;
2290 wwo = &WOutDev[wDevID];
2291 #ifdef EXACT_WODPOSITION
2292 if (wwo->ossdev->open_access == O_RDWR) {
2293 if (wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2294 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2295 } else {
2296 if (wwo->ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2297 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2299 #endif
2301 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
2304 /**************************************************************************
2305 * wodBreakLoop [internal]
2307 static DWORD wodBreakLoop(WORD wDevID)
2309 TRACE("(%u);\n", wDevID);
2311 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2312 WARN("bad device ID !\n");
2313 return MMSYSERR_BADDEVICEID;
2315 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
2316 return MMSYSERR_NOERROR;
2319 /**************************************************************************
2320 * wodGetVolume [internal]
2322 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
2324 int mixer;
2325 int volume;
2326 DWORD left, right;
2327 DWORD last_left, last_right;
2329 TRACE("(%u, %p);\n", wDevID, lpdwVol);
2331 if (lpdwVol == NULL) {
2332 WARN("not enabled\n");
2333 return MMSYSERR_NOTENABLED;
2335 if (wDevID >= numOutDev) {
2336 WARN("invalid parameter\n");
2337 return MMSYSERR_INVALPARAM;
2339 if (WOutDev[wDevID].ossdev->open_access == O_RDWR) {
2340 if (!(WOutDev[wDevID].ossdev->duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2341 TRACE("Volume not supported\n");
2342 return MMSYSERR_NOTSUPPORTED;
2344 } else {
2345 if (!(WOutDev[wDevID].ossdev->out_caps.dwSupport & WAVECAPS_VOLUME)) {
2346 TRACE("Volume not supported\n");
2347 return MMSYSERR_NOTSUPPORTED;
2351 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_RDONLY|O_NDELAY)) < 0) {
2352 WARN("mixer device not available !\n");
2353 return MMSYSERR_NOTENABLED;
2355 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
2356 close(mixer);
2357 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)\n",
2358 WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2359 return MMSYSERR_NOTENABLED;
2361 close(mixer);
2363 left = LOBYTE(volume);
2364 right = HIBYTE(volume);
2365 TRACE("left=%d right=%d !\n", left, right);
2366 last_left = (LOWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2367 last_right = (HIWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2368 TRACE("last_left=%d last_right=%d !\n", last_left, last_right);
2369 if (last_left == left && last_right == right)
2370 *lpdwVol = WOutDev[wDevID].volume;
2371 else
2372 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
2373 return MMSYSERR_NOERROR;
2376 /**************************************************************************
2377 * wodSetVolume [internal]
2379 DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
2381 int mixer;
2382 int volume;
2383 DWORD left, right;
2385 TRACE("(%u, %08X);\n", wDevID, dwParam);
2387 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
2388 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
2389 volume = left + (right << 8);
2391 if (wDevID >= numOutDev) {
2392 WARN("invalid parameter: wDevID > %d\n", numOutDev);
2393 return MMSYSERR_INVALPARAM;
2395 if (WOutDev[wDevID].ossdev->open_access == O_RDWR) {
2396 if (!(WOutDev[wDevID].ossdev->duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2397 TRACE("Volume not supported\n");
2398 return MMSYSERR_NOTSUPPORTED;
2400 } else {
2401 if (!(WOutDev[wDevID].ossdev->out_caps.dwSupport & WAVECAPS_VOLUME)) {
2402 TRACE("Volume not supported\n");
2403 return MMSYSERR_NOTSUPPORTED;
2406 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_WRONLY|O_NDELAY)) < 0) {
2407 WARN("open(%s) failed (%s)\n", WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2408 return MMSYSERR_NOTENABLED;
2410 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
2411 close(mixer);
2412 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n",
2413 WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2414 return MMSYSERR_NOTENABLED;
2416 TRACE("volume=%04x\n", (unsigned)volume);
2417 close(mixer);
2419 /* save requested volume */
2420 WOutDev[wDevID].volume = dwParam;
2422 return MMSYSERR_NOERROR;
2425 /**************************************************************************
2426 * wodMessage (WINEOSS.7)
2428 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2429 DWORD dwParam1, DWORD dwParam2)
2431 TRACE("(%u, %s, %08X, %08X, %08X);\n",
2432 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
2434 switch (wMsg) {
2435 case DRVM_INIT:
2436 case DRVM_EXIT:
2437 case DRVM_ENABLE:
2438 case DRVM_DISABLE:
2439 /* FIXME: Pretend this is supported */
2440 return 0;
2441 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2442 case WODM_CLOSE: return wodClose (wDevID);
2443 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2444 case WODM_PAUSE: return wodPause (wDevID);
2445 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
2446 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
2447 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2448 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2449 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
2450 case WODM_GETNUMDEVS: return numOutDev;
2451 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
2452 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
2453 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2454 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2455 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
2456 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
2457 case WODM_RESTART: return wodRestart (wDevID);
2458 case WODM_RESET: return wodReset (wDevID);
2460 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2461 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2462 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
2463 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2464 default:
2465 FIXME("unknown message %d!\n", wMsg);
2467 return MMSYSERR_NOTSUPPORTED;
2470 /*======================================================================*
2471 * Low level WAVE IN implementation *
2472 *======================================================================*/
2474 /**************************************************************************
2475 * widNotifyClient [internal]
2477 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2479 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04X dwParam2 = %04X\n", wMsg,
2480 wMsg == WIM_OPEN ? "WIM_OPEN" : wMsg == WIM_CLOSE ? "WIM_CLOSE" :
2481 wMsg == WIM_DATA ? "WIM_DATA" : "Unknown", dwParam1, dwParam2);
2483 switch (wMsg) {
2484 case WIM_OPEN:
2485 case WIM_CLOSE:
2486 case WIM_DATA:
2487 if (wwi->wFlags != DCB_NULL &&
2488 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2489 (HDRVR)wwi->waveDesc.hWave, wMsg,
2490 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2491 WARN("can't notify client !\n");
2492 return MMSYSERR_ERROR;
2494 break;
2495 default:
2496 FIXME("Unknown callback message %u\n", wMsg);
2497 return MMSYSERR_INVALPARAM;
2499 return MMSYSERR_NOERROR;
2502 /**************************************************************************
2503 * widGetDevCaps [internal]
2505 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
2507 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
2509 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2511 if (wDevID >= numInDev) {
2512 TRACE("numOutDev reached !\n");
2513 return MMSYSERR_BADDEVICEID;
2516 memcpy(lpCaps, &WInDev[wDevID].ossdev->in_caps, min(dwSize, sizeof(*lpCaps)));
2517 return MMSYSERR_NOERROR;
2520 /**************************************************************************
2521 * widRecorder_ReadHeaders [internal]
2523 static void widRecorder_ReadHeaders(WINE_WAVEIN * wwi)
2525 enum win_wm_message tmp_msg;
2526 DWORD tmp_param;
2527 HANDLE tmp_ev;
2528 WAVEHDR* lpWaveHdr;
2530 while (OSS_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
2531 if (tmp_msg == WINE_WM_HEADER) {
2532 LPWAVEHDR* wh;
2533 lpWaveHdr = (LPWAVEHDR)tmp_param;
2534 lpWaveHdr->lpNext = 0;
2536 if (wwi->lpQueuePtr == 0)
2537 wwi->lpQueuePtr = lpWaveHdr;
2538 else {
2539 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2540 *wh = lpWaveHdr;
2542 } else {
2543 ERR("should only have headers left\n");
2548 /**************************************************************************
2549 * widRecorder [internal]
2551 static DWORD CALLBACK widRecorder(LPVOID pmt)
2553 WORD uDevID = (DWORD)pmt;
2554 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2555 WAVEHDR* lpWaveHdr;
2556 DWORD dwSleepTime;
2557 DWORD bytesRead;
2558 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2559 char *pOffset = buffer;
2560 audio_buf_info info;
2561 int xs;
2562 enum win_wm_message msg;
2563 DWORD param;
2564 HANDLE ev;
2565 int enable;
2567 wwi->state = WINE_WS_STOPPED;
2568 wwi->dwTotalRecorded = 0;
2569 wwi->dwTotalRead = 0;
2570 wwi->lpQueuePtr = NULL;
2572 SetEvent(wwi->hStartUpEvent);
2574 /* disable input so capture will begin when triggered */
2575 wwi->ossdev->bInputEnabled = FALSE;
2576 enable = getEnables(wwi->ossdev);
2577 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2578 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2580 /* the soundblaster live needs a micro wake to get its recording started
2581 * (or GETISPACE will have 0 frags all the time)
2583 read(wwi->ossdev->fd, &xs, 4);
2585 /* make sleep time to be # of ms to output a fragment */
2586 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
2587 TRACE("sleeptime=%d ms\n", dwSleepTime);
2589 for (;;) {
2590 /* wait for dwSleepTime or an event in thread's queue */
2591 /* FIXME: could improve wait time depending on queue state,
2592 * ie, number of queued fragments
2595 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2597 lpWaveHdr = wwi->lpQueuePtr;
2599 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info);
2600 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2602 /* read all the fragments accumulated so far */
2603 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2605 info.fragments --;
2607 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2609 /* directly read fragment in wavehdr */
2610 bytesRead = read(wwi->ossdev->fd,
2611 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2612 wwi->dwFragmentSize);
2614 TRACE("bytesRead=%d (direct)\n", bytesRead);
2615 if (bytesRead != (DWORD) -1)
2617 /* update number of bytes recorded in current buffer and by this device */
2618 lpWaveHdr->dwBytesRecorded += bytesRead;
2619 wwi->dwTotalRead += bytesRead;
2620 wwi->dwTotalRecorded = wwi->dwTotalRead;
2622 /* buffer is full. notify client */
2623 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2625 /* must copy the value of next waveHdr, because we have no idea of what
2626 * will be done with the content of lpWaveHdr in callback
2628 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2630 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2631 lpWaveHdr->dwFlags |= WHDR_DONE;
2633 wwi->lpQueuePtr = lpNext;
2634 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2635 lpWaveHdr = lpNext;
2637 } else {
2638 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev->dev_name,
2639 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2640 wwi->dwFragmentSize, strerror(errno));
2643 else
2645 /* read the fragment in a local buffer */
2646 bytesRead = read(wwi->ossdev->fd, buffer, wwi->dwFragmentSize);
2647 pOffset = buffer;
2649 TRACE("bytesRead=%d (local)\n", bytesRead);
2651 if (bytesRead == (DWORD) -1) {
2652 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev->dev_name,
2653 buffer, wwi->dwFragmentSize, strerror(errno));
2654 continue;
2657 /* copy data in client buffers */
2658 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2660 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2662 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2663 pOffset,
2664 dwToCopy);
2666 /* update number of bytes recorded in current buffer and by this device */
2667 lpWaveHdr->dwBytesRecorded += dwToCopy;
2668 wwi->dwTotalRead += dwToCopy;
2669 wwi->dwTotalRecorded = wwi->dwTotalRead;
2670 bytesRead -= dwToCopy;
2671 pOffset += dwToCopy;
2673 /* client buffer is full. notify client */
2674 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2676 /* must copy the value of next waveHdr, because we have no idea of what
2677 * will be done with the content of lpWaveHdr in callback
2679 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2680 TRACE("lpNext=%p\n", lpNext);
2682 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2683 lpWaveHdr->dwFlags |= WHDR_DONE;
2685 wwi->lpQueuePtr = lpNext;
2686 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2688 lpWaveHdr = lpNext;
2689 if (!lpNext && bytesRead) {
2690 /* before we give up, check for more header messages */
2691 while (OSS_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
2693 if (msg == WINE_WM_HEADER) {
2694 LPWAVEHDR hdr;
2695 OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
2696 hdr = ((LPWAVEHDR)param);
2697 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg), hdr, ev);
2698 hdr->lpNext = 0;
2699 if (lpWaveHdr == 0) {
2700 /* new head of queue */
2701 wwi->lpQueuePtr = lpWaveHdr = hdr;
2702 } else {
2703 /* insert buffer at the end of queue */
2704 LPWAVEHDR* wh;
2705 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2706 *wh = hdr;
2708 } else
2709 break;
2712 if (lpWaveHdr == 0) {
2713 /* no more buffer to copy data to, but we did read more.
2714 * what hasn't been copied will be dropped
2716 WARN("buffer under run! %u bytes dropped.\n", bytesRead);
2717 wwi->lpQueuePtr = NULL;
2718 break;
2727 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2729 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2731 TRACE("msg=%s param=0x%x\n", getCmdString(msg), param);
2732 switch (msg) {
2733 case WINE_WM_PAUSING:
2734 wwi->state = WINE_WS_PAUSED;
2735 /*FIXME("Device should stop recording\n");*/
2736 SetEvent(ev);
2737 break;
2738 case WINE_WM_STARTING:
2739 wwi->state = WINE_WS_PLAYING;
2741 if (wwi->ossdev->bTriggerSupport)
2743 /* start the recording */
2744 wwi->ossdev->bInputEnabled = TRUE;
2745 enable = getEnables(wwi->ossdev);
2746 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2747 wwi->ossdev->bInputEnabled = FALSE;
2748 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2751 else
2753 unsigned char data[4];
2754 /* read 4 bytes to start the recording */
2755 read(wwi->ossdev->fd, data, 4);
2758 SetEvent(ev);
2759 break;
2760 case WINE_WM_HEADER:
2761 lpWaveHdr = (LPWAVEHDR)param;
2762 lpWaveHdr->lpNext = 0;
2764 /* insert buffer at the end of queue */
2766 LPWAVEHDR* wh;
2767 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2768 *wh = lpWaveHdr;
2770 break;
2771 case WINE_WM_STOPPING:
2772 if (wwi->state != WINE_WS_STOPPED)
2774 if (wwi->ossdev->bTriggerSupport)
2776 /* stop the recording */
2777 wwi->ossdev->bInputEnabled = FALSE;
2778 enable = getEnables(wwi->ossdev);
2779 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2780 wwi->ossdev->bInputEnabled = FALSE;
2781 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2785 /* read any headers in queue */
2786 widRecorder_ReadHeaders(wwi);
2788 /* return current buffer to app */
2789 lpWaveHdr = wwi->lpQueuePtr;
2790 if (lpWaveHdr)
2792 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2793 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2794 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2795 lpWaveHdr->dwFlags |= WHDR_DONE;
2796 wwi->lpQueuePtr = lpNext;
2797 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2800 wwi->state = WINE_WS_STOPPED;
2801 SetEvent(ev);
2802 break;
2803 case WINE_WM_RESETTING:
2804 if (wwi->state != WINE_WS_STOPPED)
2806 if (wwi->ossdev->bTriggerSupport)
2808 /* stop the recording */
2809 wwi->ossdev->bInputEnabled = FALSE;
2810 enable = getEnables(wwi->ossdev);
2811 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2812 wwi->ossdev->bInputEnabled = FALSE;
2813 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2817 wwi->state = WINE_WS_STOPPED;
2818 wwi->dwTotalRecorded = 0;
2819 wwi->dwTotalRead = 0;
2821 /* read any headers in queue */
2822 widRecorder_ReadHeaders(wwi);
2824 /* return all buffers to the app */
2825 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2826 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2827 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2828 lpWaveHdr->dwFlags |= WHDR_DONE;
2829 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2830 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2833 wwi->lpQueuePtr = NULL;
2834 SetEvent(ev);
2835 break;
2836 case WINE_WM_UPDATE:
2837 if (wwi->state == WINE_WS_PLAYING) {
2838 audio_buf_info tmp_info;
2839 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &tmp_info) < 0)
2840 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2841 else
2842 wwi->dwTotalRecorded = wwi->dwTotalRead + tmp_info.bytes;
2844 SetEvent(ev);
2845 break;
2846 case WINE_WM_CLOSING:
2847 wwi->hThread = 0;
2848 wwi->state = WINE_WS_CLOSED;
2849 SetEvent(ev);
2850 HeapFree(GetProcessHeap(), 0, buffer);
2851 ExitThread(0);
2852 /* shouldn't go here */
2853 default:
2854 FIXME("unknown message %d\n", msg);
2855 break;
2859 ExitThread(0);
2860 /* just for not generating compilation warnings... should never be executed */
2861 return 0;
2865 /**************************************************************************
2866 * widOpen [internal]
2868 DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2870 WINE_WAVEIN* wwi;
2871 audio_buf_info info;
2872 int audio_fragment;
2873 DWORD ret;
2875 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
2876 if (lpDesc == NULL) {
2877 WARN("Invalid Parameter !\n");
2878 return MMSYSERR_INVALPARAM;
2880 if (wDevID >= numInDev) {
2881 WARN("bad device id: %d >= %d\n", wDevID, numInDev);
2882 return MMSYSERR_BADDEVICEID;
2885 /* only PCM format is supported so far... */
2886 if (!supportedFormat(lpDesc->lpFormat)) {
2887 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2888 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2889 lpDesc->lpFormat->nSamplesPerSec);
2890 return WAVERR_BADFORMAT;
2893 if (dwFlags & WAVE_FORMAT_QUERY) {
2894 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2895 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2896 lpDesc->lpFormat->nSamplesPerSec);
2897 return MMSYSERR_NOERROR;
2900 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2901 lpDesc->lpFormat->nSamplesPerSec,
2902 lpDesc->lpFormat->wBitsPerSample,
2903 lpDesc->lpFormat->nChannels,
2904 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
2905 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
2906 "UNSUPPORTED");
2908 wwi = &WInDev[wDevID];
2910 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2912 if ((dwFlags & WAVE_DIRECTSOUND) &&
2913 !(wwi->ossdev->in_caps_support & WAVECAPS_DIRECTSOUND))
2914 /* not supported, ignore it */
2915 dwFlags &= ~WAVE_DIRECTSOUND;
2917 if (dwFlags & WAVE_DIRECTSOUND) {
2918 TRACE("has DirectSoundCapture driver\n");
2919 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
2920 /* we have realtime DirectSound, fragments just waste our time,
2921 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2922 audio_fragment = 0x0020000B;
2923 else
2924 /* to approximate realtime, we must use small fragments,
2925 * let's try to fragment the above 64KB (256 * 2^8) */
2926 audio_fragment = 0x01000008;
2927 } else {
2928 TRACE("doesn't have DirectSoundCapture driver\n");
2929 if (wwi->ossdev->open_count > 0) {
2930 TRACE("Using output device audio_fragment\n");
2931 /* FIXME: This may not be optimal for capture but it allows us
2932 * to do hardware playback without hardware capture. */
2933 audio_fragment = wwi->ossdev->audio_fragment;
2934 } else {
2935 /* A wave device must have a worst case latency of 10 ms so calculate
2936 * the largest fragment size less than 10 ms long.
2938 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2939 int shift = 0;
2940 while ((1 << shift) <= fsize)
2941 shift++;
2942 shift--;
2943 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2947 TRACE("requesting %d %d byte fragments (%d ms)\n", audio_fragment >> 16,
2948 1 << (audio_fragment & 0xffff),
2949 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2951 ret = OSS_OpenDevice(wwi->ossdev, O_RDONLY, &audio_fragment,
2953 lpDesc->lpFormat->nSamplesPerSec,
2954 lpDesc->lpFormat->nChannels,
2955 (lpDesc->lpFormat->wBitsPerSample == 16)
2956 ? AFMT_S16_LE : AFMT_U8);
2957 if (ret != 0) return ret;
2958 wwi->state = WINE_WS_STOPPED;
2960 if (wwi->lpQueuePtr) {
2961 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2962 wwi->lpQueuePtr = NULL;
2964 wwi->dwTotalRecorded = 0;
2965 wwi->dwTotalRead = 0;
2966 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2968 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2969 copy_format(lpDesc->lpFormat, &wwi->waveFormat);
2971 if (wwi->waveFormat.Format.wBitsPerSample == 0) {
2972 WARN("Resetting zeroed wBitsPerSample\n");
2973 wwi->waveFormat.Format.wBitsPerSample = 8 *
2974 (wwi->waveFormat.Format.nAvgBytesPerSec /
2975 wwi->waveFormat.Format.nSamplesPerSec) /
2976 wwi->waveFormat.Format.nChannels;
2979 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
2980 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
2981 wwi->ossdev->dev_name, strerror(errno));
2982 OSS_CloseDevice(wwi->ossdev);
2983 wwi->state = WINE_WS_CLOSED;
2984 return MMSYSERR_NOTENABLED;
2987 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2988 info.fragsize, (info.fragsize * 1000) / (wwi->ossdev->sample_rate *
2989 wwi->ossdev->channels * (wwi->ossdev->format == AFMT_U8 ? 1 : 2)));
2991 wwi->dwFragmentSize = info.fragsize;
2993 TRACE("dwFragmentSize=%u\n", wwi->dwFragmentSize);
2994 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2995 wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
2996 wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
2997 wwi->waveFormat.Format.nBlockAlign);
2999 OSS_InitRingMessage(&wwi->msgRing);
3001 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
3002 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
3003 if (wwi->hThread)
3004 SetThreadPriority(wwi->hThread, THREAD_PRIORITY_TIME_CRITICAL);
3005 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
3006 CloseHandle(wwi->hStartUpEvent);
3007 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
3009 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
3012 /**************************************************************************
3013 * widClose [internal]
3015 static DWORD widClose(WORD wDevID)
3017 WINE_WAVEIN* wwi;
3019 TRACE("(%u);\n", wDevID);
3020 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3021 WARN("can't close !\n");
3022 return MMSYSERR_INVALHANDLE;
3025 wwi = &WInDev[wDevID];
3027 if (wwi->lpQueuePtr != NULL) {
3028 WARN("still buffers open !\n");
3029 return WAVERR_STILLPLAYING;
3032 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
3033 OSS_CloseDevice(wwi->ossdev);
3034 wwi->state = WINE_WS_CLOSED;
3035 wwi->dwFragmentSize = 0;
3036 OSS_DestroyRingMessage(&wwi->msgRing);
3037 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
3040 /**************************************************************************
3041 * widAddBuffer [internal]
3043 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3045 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
3047 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3048 WARN("can't do it !\n");
3049 return MMSYSERR_INVALHANDLE;
3051 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
3052 TRACE("never been prepared !\n");
3053 return WAVERR_UNPREPARED;
3055 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
3056 TRACE("header already in use !\n");
3057 return WAVERR_STILLPLAYING;
3060 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
3061 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3062 lpWaveHdr->dwBytesRecorded = 0;
3063 lpWaveHdr->lpNext = NULL;
3065 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
3066 return MMSYSERR_NOERROR;
3069 /**************************************************************************
3070 * widStart [internal]
3072 static DWORD widStart(WORD wDevID)
3074 TRACE("(%u);\n", wDevID);
3075 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3076 WARN("can't start recording !\n");
3077 return MMSYSERR_INVALHANDLE;
3080 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
3081 return MMSYSERR_NOERROR;
3084 /**************************************************************************
3085 * widStop [internal]
3087 static DWORD widStop(WORD wDevID)
3089 TRACE("(%u);\n", wDevID);
3090 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3091 WARN("can't stop !\n");
3092 return MMSYSERR_INVALHANDLE;
3095 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3097 return MMSYSERR_NOERROR;
3100 /**************************************************************************
3101 * widReset [internal]
3103 static DWORD widReset(WORD wDevID)
3105 TRACE("(%u);\n", wDevID);
3106 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3107 WARN("can't reset !\n");
3108 return MMSYSERR_INVALHANDLE;
3110 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3111 return MMSYSERR_NOERROR;
3114 /**************************************************************************
3115 * widGetPosition [internal]
3117 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3119 WINE_WAVEIN* wwi;
3121 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
3123 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3124 WARN("can't get pos !\n");
3125 return MMSYSERR_INVALHANDLE;
3128 if (lpTime == NULL) {
3129 WARN("invalid parameter: lpTime == NULL\n");
3130 return MMSYSERR_INVALPARAM;
3133 wwi = &WInDev[wDevID];
3134 #ifdef EXACT_WIDPOSITION
3135 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
3136 OSS_AddRingMessage(&(wwi->msgRing), WINE_WM_UPDATE, 0, TRUE);
3137 #endif
3139 return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->waveFormat);
3142 /**************************************************************************
3143 * widMessage (WINEOSS.6)
3145 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3146 DWORD dwParam1, DWORD dwParam2)
3148 TRACE("(%u, %s, %08X, %08X, %08X);\n",
3149 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
3151 switch (wMsg) {
3152 case DRVM_INIT:
3153 case DRVM_EXIT:
3154 case DRVM_ENABLE:
3155 case DRVM_DISABLE:
3156 /* FIXME: Pretend this is supported */
3157 return 0;
3158 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3159 case WIDM_CLOSE: return widClose (wDevID);
3160 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3161 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
3162 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
3163 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
3164 case WIDM_GETNUMDEVS: return numInDev;
3165 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
3166 case WIDM_RESET: return widReset (wDevID);
3167 case WIDM_START: return widStart (wDevID);
3168 case WIDM_STOP: return widStop (wDevID);
3169 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
3170 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
3171 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3172 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3173 default:
3174 FIXME("unknown message %u!\n", wMsg);
3176 return MMSYSERR_NOTSUPPORTED;
3179 #else /* !HAVE_OSS */
3181 /**************************************************************************
3182 * wodMessage (WINEOSS.7)
3184 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3185 DWORD dwParam1, DWORD dwParam2)
3187 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3188 return MMSYSERR_NOTENABLED;
3191 /**************************************************************************
3192 * widMessage (WINEOSS.6)
3194 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3195 DWORD dwParam1, DWORD dwParam2)
3197 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3198 return MMSYSERR_NOTENABLED;
3201 #endif /* HAVE_OSS */