push 92ef5b88da02911741c0a2f56030fd2e20189321
[wine/hacks.git] / dlls / wineoss.drv / audio.c
blobf3360cf60e97be7544aafb93a9f22c5fefa4b3b3
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 "mmddk.h"
66 #include "mmreg.h"
67 #include "dsound.h"
68 #include "ks.h"
69 #include "ksguid.h"
70 #include "ksmedia.h"
71 #include "initguid.h"
72 #include "dsdriver.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 OSS_DEVICE OSS_Devices[MAX_WAVEDRV];
86 WINE_WAVEOUT WOutDev[MAX_WAVEDRV];
87 WINE_WAVEIN WInDev[MAX_WAVEDRV];
88 unsigned numOutDev;
89 unsigned numInDev;
91 /* state diagram for waveOut writing:
93 * +---------+-------------+---------------+---------------------------------+
94 * | state | function | event | new state |
95 * +---------+-------------+---------------+---------------------------------+
96 * | | open() | | STOPPED |
97 * | PAUSED | write() | | PAUSED |
98 * | STOPPED | write() | <thrd create> | PLAYING |
99 * | PLAYING | write() | HEADER | PLAYING |
100 * | (other) | write() | <error> | |
101 * | (any) | pause() | PAUSING | PAUSED |
102 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
103 * | (any) | reset() | RESETTING | STOPPED |
104 * | (any) | close() | CLOSING | CLOSED |
105 * +---------+-------------+---------------+---------------------------------+
108 /* These strings used only for tracing */
109 static const char * getCmdString(enum win_wm_message msg)
111 static char unknown[32];
112 #define MSG_TO_STR(x) case x: return #x
113 switch(msg) {
114 MSG_TO_STR(WINE_WM_PAUSING);
115 MSG_TO_STR(WINE_WM_RESTARTING);
116 MSG_TO_STR(WINE_WM_RESETTING);
117 MSG_TO_STR(WINE_WM_HEADER);
118 MSG_TO_STR(WINE_WM_UPDATE);
119 MSG_TO_STR(WINE_WM_BREAKLOOP);
120 MSG_TO_STR(WINE_WM_CLOSING);
121 MSG_TO_STR(WINE_WM_STARTING);
122 MSG_TO_STR(WINE_WM_STOPPING);
124 #undef MSG_TO_STR
125 sprintf(unknown, "UNKNOWN(0x%08x)", msg);
126 return unknown;
129 int getEnables(OSS_DEVICE *ossdev)
131 return ( (ossdev->bOutputEnabled ? PCM_ENABLE_OUTPUT : 0) |
132 (ossdev->bInputEnabled ? PCM_ENABLE_INPUT : 0) );
135 static const char * getMessage(UINT msg)
137 static char unknown[32];
138 #define MSG_TO_STR(x) case x: return #x
139 switch(msg) {
140 MSG_TO_STR(DRVM_INIT);
141 MSG_TO_STR(DRVM_EXIT);
142 MSG_TO_STR(DRVM_ENABLE);
143 MSG_TO_STR(DRVM_DISABLE);
144 MSG_TO_STR(WIDM_OPEN);
145 MSG_TO_STR(WIDM_CLOSE);
146 MSG_TO_STR(WIDM_ADDBUFFER);
147 MSG_TO_STR(WIDM_PREPARE);
148 MSG_TO_STR(WIDM_UNPREPARE);
149 MSG_TO_STR(WIDM_GETDEVCAPS);
150 MSG_TO_STR(WIDM_GETNUMDEVS);
151 MSG_TO_STR(WIDM_GETPOS);
152 MSG_TO_STR(WIDM_RESET);
153 MSG_TO_STR(WIDM_START);
154 MSG_TO_STR(WIDM_STOP);
155 MSG_TO_STR(WODM_OPEN);
156 MSG_TO_STR(WODM_CLOSE);
157 MSG_TO_STR(WODM_WRITE);
158 MSG_TO_STR(WODM_PAUSE);
159 MSG_TO_STR(WODM_GETPOS);
160 MSG_TO_STR(WODM_BREAKLOOP);
161 MSG_TO_STR(WODM_PREPARE);
162 MSG_TO_STR(WODM_UNPREPARE);
163 MSG_TO_STR(WODM_GETDEVCAPS);
164 MSG_TO_STR(WODM_GETNUMDEVS);
165 MSG_TO_STR(WODM_GETPITCH);
166 MSG_TO_STR(WODM_SETPITCH);
167 MSG_TO_STR(WODM_GETPLAYBACKRATE);
168 MSG_TO_STR(WODM_SETPLAYBACKRATE);
169 MSG_TO_STR(WODM_GETVOLUME);
170 MSG_TO_STR(WODM_SETVOLUME);
171 MSG_TO_STR(WODM_RESTART);
172 MSG_TO_STR(WODM_RESET);
173 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
174 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
175 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
176 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
178 #undef MSG_TO_STR
179 sprintf(unknown, "UNKNOWN(0x%04x)", msg);
180 return unknown;
183 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
185 TRACE("(%u, %p)\n", wDevID, dwParam1);
187 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev->interface_name, -1,
188 NULL, 0 ) * sizeof(WCHAR);
189 return MMSYSERR_NOERROR;
192 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
194 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev->interface_name, -1,
195 NULL, 0 ) * sizeof(WCHAR))
197 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev->interface_name, -1,
198 dwParam1, dwParam2 / sizeof(WCHAR));
199 return MMSYSERR_NOERROR;
202 return MMSYSERR_INVALPARAM;
205 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
207 TRACE("(%u, %p)\n", wDevID, dwParam1);
209 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev->interface_name, -1,
210 NULL, 0 ) * sizeof(WCHAR);
211 return MMSYSERR_NOERROR;
214 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
216 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev->interface_name, -1,
217 NULL, 0 ) * sizeof(WCHAR))
219 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev->interface_name, -1,
220 dwParam1, dwParam2 / sizeof(WCHAR));
221 return MMSYSERR_NOERROR;
224 return MMSYSERR_INVALPARAM;
227 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
228 WAVEFORMATPCMEX* format)
230 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
231 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
232 format->Format.nChannels, format->Format.nAvgBytesPerSec);
233 TRACE("Position in bytes=%u\n", position);
235 switch (lpTime->wType) {
236 case TIME_SAMPLES:
237 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
238 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
239 break;
240 case TIME_MS:
241 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
242 TRACE("TIME_MS=%u\n", lpTime->u.ms);
243 break;
244 case TIME_SMPTE:
245 lpTime->u.smpte.fps = 30;
246 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
247 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
248 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
249 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
250 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
251 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
252 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
253 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
254 lpTime->u.smpte.fps = 30;
255 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
256 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
257 lpTime->u.smpte.hour, lpTime->u.smpte.min,
258 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
259 break;
260 default:
261 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
262 lpTime->wType = TIME_BYTES;
263 /* fall through */
264 case TIME_BYTES:
265 lpTime->u.cb = position;
266 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
267 break;
269 return MMSYSERR_NOERROR;
272 static BOOL supportedFormat(LPWAVEFORMATEX wf)
274 TRACE("(%p)\n",wf);
276 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
277 return FALSE;
279 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
280 if (wf->nChannels >= 1 && wf->nChannels <= MAX_CHANNELS) {
281 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
282 return TRUE;
284 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
285 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
287 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
288 if (wf->nChannels >=1 && wf->nChannels <= MAX_CHANNELS) {
289 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
290 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
291 return TRUE;
292 } else
293 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
295 } else
296 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
297 } else
298 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
300 return FALSE;
303 void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
305 ZeroMemory(wf2, sizeof(wf2));
306 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
307 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
308 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
309 memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
310 else
311 memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
314 /*======================================================================*
315 * Low level WAVE implementation *
316 *======================================================================*/
318 /******************************************************************
319 * OSS_RawOpenDevice
321 * Low level device opening (from values stored in ossdev)
323 static DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
325 int fd, val, rc;
326 TRACE("(%p,%d)\n",ossdev,strict_format);
328 TRACE("open_access=%s\n",
329 ossdev->open_access == O_RDONLY ? "O_RDONLY" :
330 ossdev->open_access == O_WRONLY ? "O_WRONLY" :
331 ossdev->open_access == O_RDWR ? "O_RDWR" : "Unknown");
333 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
335 WARN("Couldn't open %s (%s)\n", ossdev->dev_name, strerror(errno));
336 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
338 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
339 /* turn full duplex on if it has been requested */
340 if (ossdev->open_access == O_RDWR && ossdev->full_duplex) {
341 rc = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
342 /* on *BSD, as full duplex is always enabled by default, this ioctl
343 * will fail with EINVAL
344 * so, we don't consider EINVAL an error here
346 if (rc != 0 && errno != EINVAL) {
347 WARN("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev->dev_name, strerror(errno));
348 goto error2;
352 if (ossdev->audio_fragment) {
353 rc = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
354 if (rc != 0) {
355 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev->dev_name, strerror(errno));
356 goto error2;
360 /* First size and channels then samplerate */
361 if (ossdev->format>=0)
363 val = ossdev->format;
364 rc = ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
365 if (rc != 0 || val != ossdev->format) {
366 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
367 if (strict_format)
368 goto error;
371 if (ossdev->channels>=0)
373 val = ossdev->channels;
374 rc = ioctl(fd, SNDCTL_DSP_CHANNELS, &ossdev->channels);
375 if (rc != 0 || val != ossdev->channels) {
376 TRACE("Can't set channels to %u (returned %d)\n", val, ossdev->channels);
377 if (strict_format)
378 goto error;
381 if (ossdev->sample_rate>=0)
383 val = ossdev->sample_rate;
384 rc = ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
385 if (rc != 0 || !NEAR_MATCH(val, ossdev->sample_rate)) {
386 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
387 if (strict_format)
388 goto error;
391 ossdev->fd = fd;
393 ossdev->bOutputEnabled = TRUE; /* OSS enables by default */
394 ossdev->bInputEnabled = TRUE; /* OSS enables by default */
395 if (ossdev->open_access == O_RDONLY)
396 ossdev->bOutputEnabled = FALSE;
397 if (ossdev->open_access == O_WRONLY)
398 ossdev->bInputEnabled = FALSE;
400 if (ossdev->bTriggerSupport) {
401 int trigger;
402 trigger = getEnables(ossdev);
403 /* If we do not have full duplex, but they opened RDWR
404 ** (as you have to in order for an mmap to succeed)
405 ** then we start out with input off
407 if (ossdev->open_access == O_RDWR && !ossdev->full_duplex &&
408 ossdev->bInputEnabled && ossdev->bOutputEnabled) {
409 ossdev->bInputEnabled = FALSE;
410 trigger &= ~PCM_ENABLE_INPUT;
411 ioctl(fd, SNDCTL_DSP_SETTRIGGER, &trigger);
415 return MMSYSERR_NOERROR;
417 error:
418 close(fd);
419 return WAVERR_BADFORMAT;
420 error2:
421 close(fd);
422 return MMSYSERR_ERROR;
425 /******************************************************************
426 * OSS_OpenDevice
428 * since OSS has poor capabilities in full duplex, we try here to let a program
429 * open the device for both waveout and wavein streams...
430 * this is hackish, but it's the way OSS interface is done...
432 DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
433 int* frag, int strict_format,
434 int sample_rate, int channels, int fmt)
436 DWORD ret;
437 DWORD open_access;
438 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev,req_access,frag,strict_format,sample_rate,channels,fmt);
440 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
442 TRACE("Opening RDWR because full_duplex=%d and req_access=%d\n",
443 ossdev->full_duplex,req_access);
444 open_access = O_RDWR;
446 else
448 open_access=req_access;
451 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
452 if (ossdev->open_count == 0)
454 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
456 ossdev->audio_fragment = (frag) ? *frag : 0;
457 ossdev->sample_rate = sample_rate;
458 ossdev->channels = channels;
459 ossdev->format = fmt;
460 ossdev->open_access = open_access;
461 ossdev->owner_tid = GetCurrentThreadId();
463 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
464 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
465 (req_access == O_RDONLY || req_access == O_WRONLY))
467 int enable;
468 if (req_access == O_WRONLY)
469 ossdev->bInputEnabled=0;
470 else
471 ossdev->bOutputEnabled=0;
472 enable = getEnables(ossdev);
473 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
474 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
475 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
478 else
480 /* check we really open with the same parameters */
481 if (ossdev->open_access != open_access)
483 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
484 return WAVERR_BADFORMAT;
487 /* check if the audio parameters are the same */
488 if (ossdev->sample_rate != sample_rate ||
489 ossdev->channels != channels ||
490 ossdev->format != fmt)
492 /* This is not a fatal error because MSACM might do the remapping */
493 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
494 "OSS doesn't allow us different parameters\n"
495 "audio_frag(%x/%x) sample_rate(%d/%d) channels(%d/%d) fmt(%d/%d)\n",
496 ossdev->audio_fragment, frag ? *frag : 0,
497 ossdev->sample_rate, sample_rate,
498 ossdev->channels, channels,
499 ossdev->format, fmt);
500 return WAVERR_BADFORMAT;
502 /* check if the fragment sizes are the same */
503 if (ossdev->audio_fragment != (frag ? *frag : 0) )
505 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
506 "Please run winecfg, open \"Audio\" page and set\n"
507 "\"Hardware Acceleration\" to \"Emulation\".\n");
508 return WAVERR_BADFORMAT;
510 if (GetCurrentThreadId() != ossdev->owner_tid)
512 WARN("Another thread is trying to access audio...\n");
513 return MMSYSERR_ERROR;
515 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
516 (req_access == O_RDONLY || req_access == O_WRONLY))
518 int enable;
519 if (req_access == O_WRONLY)
520 ossdev->bOutputEnabled=1;
521 else
522 ossdev->bInputEnabled=1;
523 enable = getEnables(ossdev);
524 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
525 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
526 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
530 ossdev->open_count++;
532 return MMSYSERR_NOERROR;
535 /******************************************************************
536 * OSS_CloseDevice
540 void OSS_CloseDevice(OSS_DEVICE* ossdev)
542 TRACE("(%p)\n",ossdev);
543 if (ossdev->open_count>0) {
544 ossdev->open_count--;
545 } else {
546 WARN("OSS_CloseDevice called too many times\n");
548 if (ossdev->open_count == 0)
550 fcntl(ossdev->fd, F_SETFL, fcntl(ossdev->fd, F_GETFL) & ~O_NDELAY);
551 /* reset the device before we close it in case it is in a bad state */
552 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
553 if (close(ossdev->fd) != 0) FIXME("Cannot close %d: %s\n", ossdev->fd, strerror(errno));
557 /******************************************************************
558 * OSS_ResetDevice
560 * Resets the device. OSS Commercial requires the device to be closed
561 * after a SNDCTL_DSP_RESET ioctl call... this function implements
562 * this behavior...
563 * FIXME: This causes problems when doing full duplex so we really
564 * only reset when not doing full duplex. We need to do this better
565 * someday.
567 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
569 DWORD ret = MMSYSERR_NOERROR;
570 int old_fd = ossdev->fd;
571 TRACE("(%p)\n", ossdev);
573 if (ossdev->open_count == 1) {
574 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
576 perror("ioctl SNDCTL_DSP_RESET");
577 return -1;
579 close(ossdev->fd);
580 ret = OSS_RawOpenDevice(ossdev, 1);
581 TRACE("Changing fd from %d to %d\n", old_fd, ossdev->fd);
582 } else
583 WARN("Not resetting device because it is in full duplex mode!\n");
585 return ret;
588 static const int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
589 static const int win_std_rates[5]={96000,48000,44100,22050,11025};
590 static const int win_std_formats[2][2][5]=
591 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
592 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
593 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
594 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
595 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
596 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
597 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
598 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
601 static void OSS_Info(int fd)
603 /* Note that this only reports the formats supported by the hardware.
604 * The driver may support other formats and do the conversions in
605 * software which is why we don't use this value
607 int oss_mask, oss_caps;
608 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &oss_mask) >= 0) {
609 TRACE("Formats=%08x ( ", oss_mask);
610 if (oss_mask & AFMT_MU_LAW) TRACE("AFMT_MU_LAW ");
611 if (oss_mask & AFMT_A_LAW) TRACE("AFMT_A_LAW ");
612 if (oss_mask & AFMT_IMA_ADPCM) TRACE("AFMT_IMA_ADPCM ");
613 if (oss_mask & AFMT_U8) TRACE("AFMT_U8 ");
614 if (oss_mask & AFMT_S16_LE) TRACE("AFMT_S16_LE ");
615 if (oss_mask & AFMT_S16_BE) TRACE("AFMT_S16_BE ");
616 if (oss_mask & AFMT_S8) TRACE("AFMT_S8 ");
617 if (oss_mask & AFMT_U16_LE) TRACE("AFMT_U16_LE ");
618 if (oss_mask & AFMT_U16_BE) TRACE("AFMT_U16_BE ");
619 if (oss_mask & AFMT_MPEG) TRACE("AFMT_MPEG ");
620 #ifdef AFMT_AC3
621 if (oss_mask & AFMT_AC3) TRACE("AFMT_AC3 ");
622 #endif
623 #ifdef AFMT_VORBIS
624 if (oss_mask & AFMT_VORBIS) TRACE("AFMT_VORBIS ");
625 #endif
626 #ifdef AFMT_S32_LE
627 if (oss_mask & AFMT_S32_LE) TRACE("AFMT_S32_LE ");
628 #endif
629 #ifdef AFMT_S32_BE
630 if (oss_mask & AFMT_S32_BE) TRACE("AFMT_S32_BE ");
631 #endif
632 #ifdef AFMT_FLOAT
633 if (oss_mask & AFMT_FLOAT) TRACE("AFMT_FLOAT ");
634 #endif
635 #ifdef AFMT_S24_LE
636 if (oss_mask & AFMT_S24_LE) TRACE("AFMT_S24_LE ");
637 #endif
638 #ifdef AFMT_S24_BE
639 if (oss_mask & AFMT_S24_BE) TRACE("AFMT_S24_BE ");
640 #endif
641 #ifdef AFMT_SPDIF_RAW
642 if (oss_mask & AFMT_SPDIF_RAW) TRACE("AFMT_SPDIF_RAW ");
643 #endif
644 TRACE(")\n");
646 if (ioctl(fd, SNDCTL_DSP_GETCAPS, &oss_caps) >= 0) {
647 TRACE("Caps=%08x\n",oss_caps);
648 TRACE("\tRevision: %d\n", oss_caps&DSP_CAP_REVISION);
649 TRACE("\tDuplex: %s\n", oss_caps & DSP_CAP_DUPLEX ? "true" : "false");
650 TRACE("\tRealtime: %s\n", oss_caps & DSP_CAP_REALTIME ? "true" : "false");
651 TRACE("\tBatch: %s\n", oss_caps & DSP_CAP_BATCH ? "true" : "false");
652 TRACE("\tCoproc: %s\n", oss_caps & DSP_CAP_COPROC ? "true" : "false");
653 TRACE("\tTrigger: %s\n", oss_caps & DSP_CAP_TRIGGER ? "true" : "false");
654 TRACE("\tMmap: %s\n", oss_caps & DSP_CAP_MMAP ? "true" : "false");
655 #ifdef DSP_CAP_MULTI
656 TRACE("\tMulti: %s\n", oss_caps & DSP_CAP_MULTI ? "true" : "false");
657 #endif
658 #ifdef DSP_CAP_BIND
659 TRACE("\tBind: %s\n", oss_caps & DSP_CAP_BIND ? "true" : "false");
660 #endif
661 #ifdef DSP_CAP_INPUT
662 TRACE("\tInput: %s\n", oss_caps & DSP_CAP_INPUT ? "true" : "false");
663 #endif
664 #ifdef DSP_CAP_OUTPUT
665 TRACE("\tOutput: %s\n", oss_caps & DSP_CAP_OUTPUT ? "true" : "false");
666 #endif
667 #ifdef DSP_CAP_VIRTUAL
668 TRACE("\tVirtual: %s\n", oss_caps & DSP_CAP_VIRTUAL ? "true" : "false");
669 #endif
670 #ifdef DSP_CAP_ANALOGOUT
671 TRACE("\tAnalog Out: %s\n", oss_caps & DSP_CAP_ANALOGOUT ? "true" : "false");
672 #endif
673 #ifdef DSP_CAP_ANALOGIN
674 TRACE("\tAnalog In: %s\n", oss_caps & DSP_CAP_ANALOGIN ? "true" : "false");
675 #endif
676 #ifdef DSP_CAP_DIGITALOUT
677 TRACE("\tDigital Out: %s\n", oss_caps & DSP_CAP_DIGITALOUT ? "true" : "false");
678 #endif
679 #ifdef DSP_CAP_DIGITALIN
680 TRACE("\tDigital In: %s\n", oss_caps & DSP_CAP_DIGITALIN ? "true" : "false");
681 #endif
682 #ifdef DSP_CAP_ADMASK
683 TRACE("\tA/D Mask: %s\n", oss_caps & DSP_CAP_ADMASK ? "true" : "false");
684 #endif
685 #ifdef DSP_CAP_SHADOW
686 TRACE("\tShadow: %s\n", oss_caps & DSP_CAP_SHADOW ? "true" : "false");
687 #endif
688 #ifdef DSP_CH_MASK
689 TRACE("\tChannel Mask: %x\n", oss_caps & DSP_CH_MASK);
690 #endif
691 #ifdef DSP_CAP_SLAVE
692 TRACE("\tSlave: %s\n", oss_caps & DSP_CAP_SLAVE ? "true" : "false");
693 #endif
697 /******************************************************************
698 * OSS_WaveOutInit
702 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
704 int rc,arg;
705 int f,c,r;
706 BOOL has_mixer = FALSE;
707 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
709 if (OSS_OpenDevice(ossdev, O_WRONLY, NULL, 0,-1,-1,-1) != 0)
710 return FALSE;
712 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
714 #if defined(SNDCTL_MIXERINFO)
716 int mixer;
717 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
718 oss_mixerinfo info;
719 info.dev = 0;
720 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
721 lstrcpynA(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
722 strcpy(ossdev->ds_desc.szDrvname, "wineoss.drv");
723 MultiByteToWideChar(CP_ACP, 0, info.name, sizeof(info.name),
724 ossdev->out_caps.szPname,
725 sizeof(ossdev->out_caps.szPname) / sizeof(WCHAR));
726 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
727 has_mixer = TRUE;
728 } else {
729 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
731 close(mixer);
732 } else {
733 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
736 #elif defined(SOUND_MIXER_INFO)
738 int mixer;
739 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
740 mixer_info info;
741 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
742 lstrcpynA(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
743 strcpy(ossdev->ds_desc.szDrvname, "wineoss.drv");
744 MultiByteToWideChar(CP_ACP, 0, info.name, sizeof(info.name),
745 ossdev->out_caps.szPname,
746 sizeof(ossdev->out_caps.szPname) / sizeof(WCHAR));
747 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
748 has_mixer = TRUE;
749 } else {
750 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
751 * implement it properly, and there are probably similar issues
752 * on other platforms, so we warn but try to go ahead.
754 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
756 close(mixer);
757 } else {
758 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
761 #endif /* SOUND_MIXER_INFO */
763 if (WINE_TRACE_ON(wave))
764 OSS_Info(ossdev->fd);
766 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
767 ossdev->out_caps.wPid = 0x0001; /* Product ID */
769 ossdev->out_caps.vDriverVersion = 0x0100;
770 ossdev->out_caps.wChannels = 1;
771 ossdev->out_caps.dwFormats = 0x00000000;
772 ossdev->out_caps.wReserved1 = 0;
773 ossdev->out_caps.dwSupport = has_mixer ? WAVECAPS_VOLUME : 0;
775 /* direct sound caps */
776 ossdev->ds_caps.dwFlags = DSCAPS_CERTIFIED;
777 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY8BIT;
778 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY16BIT;
779 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYMONO;
780 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYSTEREO;
781 ossdev->ds_caps.dwFlags |= DSCAPS_CONTINUOUSRATE;
783 ossdev->ds_caps.dwPrimaryBuffers = 1;
784 ossdev->ds_caps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
785 ossdev->ds_caps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
787 /* We must first set the format and the stereo mode as some sound cards
788 * may support 44kHz mono but not 44kHz stereo. Also we must
789 * systematically check the return value of these ioctls as they will
790 * always succeed (see OSS Linux) but will modify the parameter to match
791 * whatever they support. The OSS specs also say we must first set the
792 * sample size, then the stereo and then the sample rate.
794 for (f=0;f<2;f++) {
795 arg=win_std_oss_fmts[f];
796 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
797 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
798 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
799 rc,arg,win_std_oss_fmts[f]);
800 continue;
802 if (f == 0)
803 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY8BIT;
804 else if (f == 1)
805 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY16BIT;
807 for (c = 1; c <= MAX_CHANNELS; c++) {
808 arg=c;
809 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
810 if( rc == -1) break;
811 if (rc!=0 || arg!=c) {
812 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
813 continue;
815 if (c == 1) {
816 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
817 } else if (c == 2) {
818 ossdev->out_caps.wChannels = 2;
819 if (has_mixer)
820 ossdev->out_caps.dwSupport|=WAVECAPS_LRVOLUME;
821 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
822 } else
823 ossdev->out_caps.wChannels = c;
825 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
826 arg=win_std_rates[r];
827 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
828 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
829 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
830 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
831 ossdev->out_caps.dwFormats|=win_std_formats[f][c-1][r];
836 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
837 if (arg & DSP_CAP_TRIGGER)
838 ossdev->bTriggerSupport = TRUE;
839 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
840 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
842 /* well, might as well use the DirectSound cap flag for something */
843 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
844 !(arg & DSP_CAP_BATCH)) {
845 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
846 } else {
847 ossdev->ds_caps.dwFlags |= DSCAPS_EMULDRIVER;
849 #ifdef DSP_CAP_MULTI /* not every oss has this */
850 /* check for hardware secondary buffer support (multi open) */
851 if ((arg & DSP_CAP_MULTI) &&
852 (ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
853 TRACE("hardware secondary buffer support available\n");
855 ossdev->ds_caps.dwMaxHwMixingAllBuffers = 16;
856 ossdev->ds_caps.dwMaxHwMixingStaticBuffers = 0;
857 ossdev->ds_caps.dwMaxHwMixingStreamingBuffers = 16;
859 ossdev->ds_caps.dwFreeHwMixingAllBuffers = 16;
860 ossdev->ds_caps.dwFreeHwMixingStaticBuffers = 0;
861 ossdev->ds_caps.dwFreeHwMixingStreamingBuffers = 16;
863 #endif
865 OSS_CloseDevice(ossdev);
866 TRACE("out wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
867 ossdev->out_caps.wChannels, ossdev->out_caps.dwFormats,
868 ossdev->out_caps.dwSupport);
869 return TRUE;
872 /******************************************************************
873 * OSS_WaveInInit
877 static BOOL OSS_WaveInInit(OSS_DEVICE* ossdev)
879 int rc,arg;
880 int f,c,r;
881 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
883 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0,-1,-1,-1) != 0)
884 return FALSE;
886 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
888 #if defined(SNDCTL_MIXERINFO)
890 int mixer;
891 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
892 oss_mixerinfo info;
893 info.dev = 0;
894 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
895 MultiByteToWideChar(CP_ACP, 0, info.name, -1,
896 ossdev->in_caps.szPname,
897 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
898 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
899 } else {
900 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
902 close(mixer);
903 } else {
904 WARN("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
907 #elif defined(SOUND_MIXER_INFO)
909 int mixer;
910 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
911 mixer_info info;
912 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
913 MultiByteToWideChar(CP_ACP, 0, info.name, -1,
914 ossdev->in_caps.szPname,
915 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
916 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
917 } else {
918 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
919 * implement it properly, and there are probably similar issues
920 * on other platforms, so we warn but try to go ahead.
922 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
924 close(mixer);
925 } else {
926 WARN("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
929 #endif /* SOUND_MIXER_INFO */
931 if (WINE_TRACE_ON(wave))
932 OSS_Info(ossdev->fd);
934 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
935 ossdev->in_caps.wPid = 0x0001; /* Product ID */
937 ossdev->in_caps.dwFormats = 0x00000000;
938 ossdev->in_caps.wChannels = 1;
939 ossdev->in_caps.wReserved1 = 0;
941 /* direct sound caps */
942 ossdev->dsc_caps.dwSize = sizeof(ossdev->dsc_caps);
943 ossdev->dsc_caps.dwFlags = 0;
944 ossdev->dsc_caps.dwFormats = 0x00000000;
945 ossdev->dsc_caps.dwChannels = 1;
947 /* See the comment in OSS_WaveOutInit for the loop order */
948 for (f=0;f<2;f++) {
949 arg=win_std_oss_fmts[f];
950 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
951 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
952 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
953 rc,arg,win_std_oss_fmts[f]);
954 continue;
957 for (c = 1; c <= MAX_CHANNELS; c++) {
958 arg=c;
959 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
960 if( rc == -1) break;
961 if (rc!=0 || arg!=c) {
962 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
963 continue;
965 if (c > 1) {
966 ossdev->in_caps.wChannels = c;
967 ossdev->dsc_caps.dwChannels = c;
970 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
971 arg=win_std_rates[r];
972 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
973 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
974 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
975 ossdev->in_caps.dwFormats|=win_std_formats[f][c-1][r];
976 ossdev->dsc_caps.dwFormats|=win_std_formats[f][c-1][r];
981 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
982 if (arg & DSP_CAP_TRIGGER)
983 ossdev->bTriggerSupport = TRUE;
984 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
985 !(arg & DSP_CAP_BATCH)) {
986 /* FIXME: enable the next statement if you want to work on the driver */
987 #if 0
988 ossdev->in_caps_support |= WAVECAPS_DIRECTSOUND;
989 #endif
991 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH))
992 ossdev->in_caps_support |= WAVECAPS_SAMPLEACCURATE;
994 OSS_CloseDevice(ossdev);
995 TRACE("in wChannels = %d, dwFormats = %08X, in_caps_support = %08X\n",
996 ossdev->in_caps.wChannels, ossdev->in_caps.dwFormats, ossdev->in_caps_support);
997 return TRUE;
1000 /******************************************************************
1001 * OSS_WaveFullDuplexInit
1005 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
1007 int rc,arg;
1008 int f,c,r;
1009 int caps;
1010 BOOL has_mixer = FALSE;
1011 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
1013 /* The OSS documentation says we must call SNDCTL_SETDUPLEX
1014 * *before* checking for SNDCTL_DSP_GETCAPS otherwise we may
1015 * get the wrong result. This ioctl must even be done before
1016 * setting the fragment size so that only OSS_RawOpenDevice is
1017 * in a position to do it. So we set full_duplex speculatively
1018 * and adjust right after.
1020 ossdev->full_duplex=1;
1021 rc=OSS_OpenDevice(ossdev, O_RDWR, NULL, 0,-1,-1,-1);
1022 ossdev->full_duplex=0;
1023 if (rc != 0)
1024 return;
1026 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
1028 #if defined(SNDCTL_MIXERINFO)
1030 int mixer;
1031 if ((mixer = open(ossdev->mixer_name, O_RDWR|O_NDELAY)) >= 0) {
1032 oss_mixerinfo info;
1033 info.dev = 0;
1034 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
1035 has_mixer = TRUE;
1036 } else {
1037 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
1039 close(mixer);
1040 } else {
1041 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
1044 #elif defined(SOUND_MIXER_INFO)
1046 int mixer;
1047 if ((mixer = open(ossdev->mixer_name, O_RDWR|O_NDELAY)) >= 0) {
1048 mixer_info info;
1049 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
1050 has_mixer = TRUE;
1051 } else {
1052 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
1053 * implement it properly, and there are probably similar issues
1054 * on other platforms, so we warn but try to go ahead.
1056 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
1058 close(mixer);
1059 } else {
1060 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
1063 #endif /* SOUND_MIXER_INFO */
1065 TRACE("%s\n", ossdev->ds_desc.szDesc);
1067 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
1068 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
1070 ossdev->duplex_out_caps = ossdev->out_caps;
1072 ossdev->duplex_out_caps.wChannels = 1;
1073 ossdev->duplex_out_caps.dwFormats = 0x00000000;
1074 ossdev->duplex_out_caps.dwSupport = has_mixer ? WAVECAPS_VOLUME : 0;
1076 if (WINE_TRACE_ON(wave))
1077 OSS_Info(ossdev->fd);
1079 /* See the comment in OSS_WaveOutInit for the loop order */
1080 for (f=0;f<2;f++) {
1081 arg=win_std_oss_fmts[f];
1082 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
1083 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
1084 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1085 rc,arg,win_std_oss_fmts[f]);
1086 continue;
1089 for (c = 1; c <= MAX_CHANNELS; c++) {
1090 arg=c;
1091 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
1092 if( rc == -1) break;
1093 if (rc!=0 || arg!=c) {
1094 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
1095 continue;
1097 if (c == 1) {
1098 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
1099 } else if (c == 2) {
1100 ossdev->duplex_out_caps.wChannels = 2;
1101 if (has_mixer)
1102 ossdev->duplex_out_caps.dwSupport|=WAVECAPS_LRVOLUME;
1103 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
1104 } else
1105 ossdev->duplex_out_caps.wChannels = c;
1107 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
1108 arg=win_std_rates[r];
1109 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
1110 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
1111 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
1112 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
1113 ossdev->duplex_out_caps.dwFormats|=win_std_formats[f][c-1][r];
1118 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
1119 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
1120 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
1122 /* well, might as well use the DirectSound cap flag for something */
1123 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
1124 !(arg & DSP_CAP_BATCH)) {
1125 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
1128 OSS_CloseDevice(ossdev);
1129 TRACE("duplex wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
1130 ossdev->duplex_out_caps.wChannels,
1131 ossdev->duplex_out_caps.dwFormats,
1132 ossdev->duplex_out_caps.dwSupport);
1135 static char* StrDup(const char* str, const char* def)
1137 char* dst;
1138 if (str==NULL)
1139 str=def;
1140 dst=HeapAlloc(GetProcessHeap(),0,strlen(str)+1);
1141 strcpy(dst, str);
1142 return dst;
1145 /******************************************************************
1146 * OSS_WaveInit
1148 * Initialize internal structures from OSS information
1150 LRESULT OSS_WaveInit(void)
1152 char* str;
1153 int i;
1155 TRACE("()\n");
1157 str=getenv("AUDIODEV");
1158 if (str!=NULL)
1160 OSS_Devices[0].dev_name=StrDup(str,"");
1161 OSS_Devices[0].mixer_name=StrDup(getenv("MIXERDEV"),"/dev/mixer");
1162 for (i = 1; i < MAX_WAVEDRV; ++i)
1164 OSS_Devices[i].dev_name=StrDup("",NULL);
1165 OSS_Devices[i].mixer_name=StrDup("",NULL);
1168 else
1170 OSS_Devices[0].dev_name=StrDup("/dev/dsp",NULL);
1171 OSS_Devices[0].mixer_name=StrDup("/dev/mixer",NULL);
1172 for (i = 1; i < MAX_WAVEDRV; ++i)
1174 OSS_Devices[i].dev_name=HeapAlloc(GetProcessHeap(),0,11);
1175 sprintf(OSS_Devices[i].dev_name, "/dev/dsp%d", i);
1176 OSS_Devices[i].mixer_name=HeapAlloc(GetProcessHeap(),0,13);
1177 sprintf(OSS_Devices[i].mixer_name, "/dev/mixer%d", i);
1181 for (i = 0; i < MAX_WAVEDRV; ++i)
1183 OSS_Devices[i].interface_name=HeapAlloc(GetProcessHeap(),0,9+strlen(OSS_Devices[i].dev_name)+1);
1184 sprintf(OSS_Devices[i].interface_name, "wineoss: %s", OSS_Devices[i].dev_name);
1187 /* start with output devices */
1188 for (i = 0; i < MAX_WAVEDRV; ++i)
1190 if (*OSS_Devices[i].dev_name=='\0' || OSS_WaveOutInit(&OSS_Devices[i]))
1192 WOutDev[numOutDev].state = WINE_WS_CLOSED;
1193 WOutDev[numOutDev].ossdev = &OSS_Devices[i];
1194 WOutDev[numOutDev].volume = 0xffffffff;
1195 numOutDev++;
1199 /* then do input devices */
1200 for (i = 0; i < MAX_WAVEDRV; ++i)
1202 if (*OSS_Devices[i].dev_name=='\0' || OSS_WaveInInit(&OSS_Devices[i]))
1204 WInDev[numInDev].state = WINE_WS_CLOSED;
1205 WInDev[numInDev].ossdev = &OSS_Devices[i];
1206 numInDev++;
1210 /* finish with the full duplex bits */
1211 for (i = 0; i < MAX_WAVEDRV; i++)
1212 if (*OSS_Devices[i].dev_name!='\0')
1213 OSS_WaveFullDuplexInit(&OSS_Devices[i]);
1215 TRACE("%d wave out devices\n", numOutDev);
1216 for (i = 0; i < numOutDev; i++) {
1217 TRACE("%d: %s, %s, %s\n", i, WOutDev[i].ossdev->dev_name,
1218 WOutDev[i].ossdev->mixer_name, WOutDev[i].ossdev->interface_name);
1221 TRACE("%d wave in devices\n", numInDev);
1222 for (i = 0; i < numInDev; i++) {
1223 TRACE("%d: %s, %s, %s\n", i, WInDev[i].ossdev->dev_name,
1224 WInDev[i].ossdev->mixer_name, WInDev[i].ossdev->interface_name);
1227 return 0;
1230 /******************************************************************
1231 * OSS_WaveExit
1233 * Delete/clear internal structures of OSS information
1235 LRESULT OSS_WaveExit(void)
1237 int i;
1238 TRACE("()\n");
1240 for (i = 0; i < MAX_WAVEDRV; ++i)
1242 HeapFree(GetProcessHeap(), 0, OSS_Devices[i].dev_name);
1243 HeapFree(GetProcessHeap(), 0, OSS_Devices[i].mixer_name);
1244 HeapFree(GetProcessHeap(), 0, OSS_Devices[i].interface_name);
1247 ZeroMemory(OSS_Devices, sizeof(OSS_Devices));
1248 ZeroMemory(WOutDev, sizeof(WOutDev));
1249 ZeroMemory(WInDev, sizeof(WInDev));
1251 numOutDev = 0;
1252 numInDev = 0;
1254 return 0;
1257 /******************************************************************
1258 * OSS_InitRingMessage
1260 * Initialize the ring of messages for passing between driver's caller and playback/record
1261 * thread
1263 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
1265 omr->msg_toget = 0;
1266 omr->msg_tosave = 0;
1267 #ifdef USE_PIPE_SYNC
1268 if (pipe(omr->msg_pipe) < 0) {
1269 omr->msg_pipe[0] = -1;
1270 omr->msg_pipe[1] = -1;
1271 ERR("could not create pipe, error=%s\n", strerror(errno));
1273 #else
1274 omr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1275 #endif
1276 omr->ring_buffer_size = OSS_RING_BUFFER_INCREMENT;
1277 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(OSS_MSG));
1278 InitializeCriticalSection(&omr->msg_crst);
1279 omr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": OSS_MSG_RING.msg_crst");
1280 return 0;
1283 /******************************************************************
1284 * OSS_DestroyRingMessage
1287 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
1289 #ifdef USE_PIPE_SYNC
1290 close(omr->msg_pipe[0]);
1291 close(omr->msg_pipe[1]);
1292 #else
1293 CloseHandle(omr->msg_event);
1294 #endif
1295 HeapFree(GetProcessHeap(),0,omr->messages);
1296 omr->msg_crst.DebugInfo->Spare[0] = 0;
1297 DeleteCriticalSection(&omr->msg_crst);
1298 return 0;
1301 /******************************************************************
1302 * OSS_AddRingMessage
1304 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
1306 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
1308 HANDLE hEvent = INVALID_HANDLE_VALUE;
1310 EnterCriticalSection(&omr->msg_crst);
1311 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
1313 int old_ring_buffer_size = omr->ring_buffer_size;
1314 omr->ring_buffer_size += OSS_RING_BUFFER_INCREMENT;
1315 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
1316 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(OSS_MSG));
1317 /* Now we need to rearrange the ring buffer so that the new
1318 buffers just allocated are in between omr->msg_tosave and
1319 omr->msg_toget.
1321 if (omr->msg_tosave < omr->msg_toget)
1323 memmove(&(omr->messages[omr->msg_toget + OSS_RING_BUFFER_INCREMENT]),
1324 &(omr->messages[omr->msg_toget]),
1325 sizeof(OSS_MSG)*(old_ring_buffer_size - omr->msg_toget)
1327 omr->msg_toget += OSS_RING_BUFFER_INCREMENT;
1330 if (wait)
1332 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1333 if (hEvent == INVALID_HANDLE_VALUE)
1335 ERR("can't create event !?\n");
1336 LeaveCriticalSection(&omr->msg_crst);
1337 return 0;
1339 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
1340 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1341 omr->msg_toget,getCmdString(omr->messages[omr->msg_toget].msg),
1342 omr->msg_tosave,getCmdString(omr->messages[omr->msg_tosave].msg));
1344 /* fast messages have to be added at the start of the queue */
1345 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
1346 omr->messages[omr->msg_toget].msg = msg;
1347 omr->messages[omr->msg_toget].param = param;
1348 omr->messages[omr->msg_toget].hEvent = hEvent;
1350 else
1352 omr->messages[omr->msg_tosave].msg = msg;
1353 omr->messages[omr->msg_tosave].param = param;
1354 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
1355 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
1357 LeaveCriticalSection(&omr->msg_crst);
1358 /* signal a new message */
1359 SIGNAL_OMR(omr);
1360 if (wait)
1362 /* wait for playback/record thread to have processed the message */
1363 WaitForSingleObject(hEvent, INFINITE);
1364 CloseHandle(hEvent);
1366 return 1;
1369 /******************************************************************
1370 * OSS_RetrieveRingMessage
1372 * Get a message from the ring. Should be called by the playback/record thread.
1374 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
1375 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
1377 EnterCriticalSection(&omr->msg_crst);
1379 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1381 LeaveCriticalSection(&omr->msg_crst);
1382 return 0;
1385 *msg = omr->messages[omr->msg_toget].msg;
1386 omr->messages[omr->msg_toget].msg = 0;
1387 *param = omr->messages[omr->msg_toget].param;
1388 *hEvent = omr->messages[omr->msg_toget].hEvent;
1389 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
1390 CLEAR_OMR(omr);
1391 LeaveCriticalSection(&omr->msg_crst);
1392 return 1;
1395 /******************************************************************
1396 * OSS_PeekRingMessage
1398 * Peek at a message from the ring but do not remove it.
1399 * Should be called by the playback/record thread.
1401 static int OSS_PeekRingMessage(OSS_MSG_RING* omr,
1402 enum win_wm_message *msg,
1403 DWORD *param, HANDLE *hEvent)
1405 EnterCriticalSection(&omr->msg_crst);
1407 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1409 LeaveCriticalSection(&omr->msg_crst);
1410 return 0;
1413 *msg = omr->messages[omr->msg_toget].msg;
1414 *param = omr->messages[omr->msg_toget].param;
1415 *hEvent = omr->messages[omr->msg_toget].hEvent;
1416 LeaveCriticalSection(&omr->msg_crst);
1417 return 1;
1420 /*======================================================================*
1421 * Low level WAVE OUT implementation *
1422 *======================================================================*/
1424 /**************************************************************************
1425 * wodNotifyClient [internal]
1427 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1429 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04X dwParam2 = %04X\n", wMsg,
1430 wMsg == WOM_OPEN ? "WOM_OPEN" : wMsg == WOM_CLOSE ? "WOM_CLOSE" :
1431 wMsg == WOM_DONE ? "WOM_DONE" : "Unknown", dwParam1, dwParam2);
1433 switch (wMsg) {
1434 case WOM_OPEN:
1435 case WOM_CLOSE:
1436 case WOM_DONE:
1437 if (wwo->wFlags != DCB_NULL &&
1438 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
1439 (HDRVR)wwo->waveDesc.hWave, wMsg,
1440 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
1441 WARN("can't notify client !\n");
1442 return MMSYSERR_ERROR;
1444 break;
1445 default:
1446 FIXME("Unknown callback message %u\n", wMsg);
1447 return MMSYSERR_INVALPARAM;
1449 return MMSYSERR_NOERROR;
1452 /**************************************************************************
1453 * wodUpdatePlayedTotal [internal]
1456 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
1458 audio_buf_info dspspace;
1459 DWORD notplayed;
1460 if (!info) info = &dspspace;
1462 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
1463 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
1464 return FALSE;
1467 /* GETOSPACE is not always accurate when we're down to the last fragment or two;
1468 ** we try to accommodate that here by assuming that the dsp is empty by looking
1469 ** at the clock rather than the result of GETOSPACE */
1470 notplayed = wwo->dwBufferSize - info->bytes;
1471 if (notplayed > 0 && notplayed < (info->fragsize * 2))
1473 if (wwo->dwProjectedFinishTime && GetTickCount() >= wwo->dwProjectedFinishTime)
1475 TRACE("Adjusting for a presumed OSS bug and assuming all data has been played.\n");
1476 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1477 return TRUE;
1479 else
1480 /* Some OSS drivers will clean up nicely if given a POST, so give 'em the chance... */
1481 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1484 wwo->dwPlayedTotal = wwo->dwWrittenTotal - notplayed;
1485 return TRUE;
1488 /**************************************************************************
1489 * wodPlayer_BeginWaveHdr [internal]
1491 * Makes the specified lpWaveHdr the currently playing wave header.
1492 * If the specified wave header is a begin loop and we're not already in
1493 * a loop, setup the loop.
1495 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1497 wwo->lpPlayPtr = lpWaveHdr;
1499 if (!lpWaveHdr) return;
1501 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1502 if (wwo->lpLoopPtr) {
1503 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1504 } else {
1505 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1506 wwo->lpLoopPtr = lpWaveHdr;
1507 /* Windows does not touch WAVEHDR.dwLoops,
1508 * so we need to make an internal copy */
1509 wwo->dwLoops = lpWaveHdr->dwLoops;
1512 wwo->dwPartialOffset = 0;
1515 /**************************************************************************
1516 * wodPlayer_PlayPtrNext [internal]
1518 * Advance the play pointer to the next waveheader, looping if required.
1520 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
1522 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1524 wwo->dwPartialOffset = 0;
1525 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1526 /* We're at the end of a loop, loop if required */
1527 if (--wwo->dwLoops > 0) {
1528 wwo->lpPlayPtr = wwo->lpLoopPtr;
1529 } else {
1530 /* Handle overlapping loops correctly */
1531 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1532 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1533 /* shall we consider the END flag for the closing loop or for
1534 * the opening one or for both ???
1535 * code assumes for closing loop only
1537 } else {
1538 lpWaveHdr = lpWaveHdr->lpNext;
1540 wwo->lpLoopPtr = NULL;
1541 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1543 } else {
1544 /* We're not in a loop. Advance to the next wave header */
1545 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1548 return lpWaveHdr;
1551 /**************************************************************************
1552 * wodPlayer_TicksTillEmpty [internal]
1553 * Returns the number of ticks until we think the DSP should be empty
1555 static DWORD wodPlayer_TicksTillEmpty(const WINE_WAVEOUT *wwo)
1557 return ((wwo->dwWrittenTotal - wwo->dwPlayedTotal) * 1000)
1558 / wwo->waveFormat.Format.nAvgBytesPerSec;
1561 /**************************************************************************
1562 * wodPlayer_DSPWait [internal]
1563 * Returns the number of milliseconds to wait for the DSP buffer to write
1564 * one fragment.
1566 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1568 /* time for one fragment to be played */
1569 return wwo->dwFragmentSize * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1572 /**************************************************************************
1573 * wodPlayer_NotifyWait [internal]
1574 * Returns the number of milliseconds to wait before attempting to notify
1575 * completion of the specified wavehdr.
1576 * This is based on the number of bytes remaining to be written in the
1577 * wave.
1579 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1581 DWORD dwMillis;
1583 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1584 dwMillis = 1;
1585 } else {
1586 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1587 if (!dwMillis) dwMillis = 1;
1590 return dwMillis;
1594 /**************************************************************************
1595 * wodPlayer_WriteMaxFrags [internal]
1596 * Writes the maximum number of bytes possible to the DSP and returns
1597 * TRUE iff the current playPtr has been fully played
1599 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
1601 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1602 DWORD toWrite = min(dwLength, *bytes);
1603 int written;
1604 BOOL ret = FALSE;
1606 TRACE("Writing wavehdr %p.%u[%u]/%u\n",
1607 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
1609 if (toWrite > 0)
1611 written = write(wwo->ossdev->fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
1612 if (written <= 0) {
1613 TRACE("write(%s, %p, %d) failed (%s) returned %d\n", wwo->ossdev->dev_name,
1614 wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite, strerror(errno), written);
1615 return FALSE;
1618 else
1619 written = 0;
1621 if (written >= dwLength) {
1622 /* If we wrote all current wavehdr, skip to the next one */
1623 wodPlayer_PlayPtrNext(wwo);
1624 ret = TRUE;
1625 } else {
1626 /* Remove the amount written */
1627 wwo->dwPartialOffset += written;
1629 *bytes -= written;
1630 wwo->dwWrittenTotal += written;
1631 TRACE("dwWrittenTotal=%u\n", wwo->dwWrittenTotal);
1632 return ret;
1636 /**************************************************************************
1637 * wodPlayer_NotifyCompletions [internal]
1639 * Notifies and remove from queue all wavehdrs which have been played to
1640 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1641 * we notify all wavehdrs and remove them all from the queue even if they
1642 * are unplayed or part of a loop.
1644 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1646 LPWAVEHDR lpWaveHdr;
1648 /* Start from lpQueuePtr and keep notifying until:
1649 * - we hit an unwritten wavehdr
1650 * - we hit the beginning of a running loop
1651 * - we hit a wavehdr which hasn't finished playing
1653 #if 0
1654 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1655 (force ||
1656 (lpWaveHdr != wwo->lpPlayPtr &&
1657 lpWaveHdr != wwo->lpLoopPtr &&
1658 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1660 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1662 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1663 lpWaveHdr->dwFlags |= WHDR_DONE;
1665 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1667 #else
1668 for (;;)
1670 lpWaveHdr = wwo->lpQueuePtr;
1671 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
1672 if (!force)
1674 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
1675 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
1676 if (lpWaveHdr->reserved > wwo->dwPlayedTotal) {TRACE("still playing %p (%u/%u)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
1678 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1680 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1681 lpWaveHdr->dwFlags |= WHDR_DONE;
1683 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1685 #endif
1686 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1687 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1690 /**************************************************************************
1691 * wodPlayer_Reset [internal]
1693 * wodPlayer helper. Resets current output stream.
1695 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
1697 wodUpdatePlayedTotal(wwo, NULL);
1698 /* updates current notify list */
1699 wodPlayer_NotifyCompletions(wwo, FALSE);
1701 /* flush all possible output */
1702 if (OSS_ResetDevice(wwo->ossdev) != MMSYSERR_NOERROR)
1704 wwo->hThread = 0;
1705 wwo->state = WINE_WS_STOPPED;
1706 ExitThread(-1);
1709 if (reset) {
1710 enum win_wm_message msg;
1711 DWORD param;
1712 HANDLE ev;
1714 /* remove any buffer */
1715 wodPlayer_NotifyCompletions(wwo, TRUE);
1717 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1718 wwo->state = WINE_WS_STOPPED;
1719 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1720 /* Clear partial wavehdr */
1721 wwo->dwPartialOffset = 0;
1723 /* remove any existing message in the ring */
1724 EnterCriticalSection(&wwo->msgRing.msg_crst);
1725 /* return all pending headers in queue */
1726 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1728 if (msg != WINE_WM_HEADER)
1730 FIXME("shouldn't have headers left\n");
1731 SetEvent(ev);
1732 continue;
1734 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1735 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1737 wodNotifyClient(wwo, WOM_DONE, param, 0);
1739 RESET_OMR(&wwo->msgRing);
1740 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1741 } else {
1742 if (wwo->lpLoopPtr) {
1743 /* complicated case, not handled yet (could imply modifying the loop counter */
1744 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1745 wwo->lpPlayPtr = wwo->lpLoopPtr;
1746 wwo->dwPartialOffset = 0;
1747 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1748 } else {
1749 LPWAVEHDR ptr;
1750 DWORD sz = wwo->dwPartialOffset;
1752 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1753 /* compute the max size playable from lpQueuePtr */
1754 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1755 sz += ptr->dwBufferLength;
1757 /* because the reset lpPlayPtr will be lpQueuePtr */
1758 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1759 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1760 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1761 wwo->lpPlayPtr = wwo->lpQueuePtr;
1763 wwo->state = WINE_WS_PAUSED;
1767 /**************************************************************************
1768 * wodPlayer_ProcessMessages [internal]
1770 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1772 LPWAVEHDR lpWaveHdr;
1773 enum win_wm_message msg;
1774 DWORD param;
1775 HANDLE ev;
1777 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1778 TRACE("Received %s %x\n", getCmdString(msg), param);
1779 switch (msg) {
1780 case WINE_WM_PAUSING:
1781 wodPlayer_Reset(wwo, FALSE);
1782 SetEvent(ev);
1783 break;
1784 case WINE_WM_RESTARTING:
1785 if (wwo->state == WINE_WS_PAUSED)
1787 wwo->state = WINE_WS_PLAYING;
1789 SetEvent(ev);
1790 break;
1791 case WINE_WM_HEADER:
1792 lpWaveHdr = (LPWAVEHDR)param;
1794 /* insert buffer at the end of queue */
1796 LPWAVEHDR* wh;
1797 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1798 *wh = lpWaveHdr;
1800 if (!wwo->lpPlayPtr)
1801 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1802 if (wwo->state == WINE_WS_STOPPED)
1803 wwo->state = WINE_WS_PLAYING;
1804 break;
1805 case WINE_WM_RESETTING:
1806 wodPlayer_Reset(wwo, TRUE);
1807 SetEvent(ev);
1808 break;
1809 case WINE_WM_UPDATE:
1810 wodUpdatePlayedTotal(wwo, NULL);
1811 SetEvent(ev);
1812 break;
1813 case WINE_WM_BREAKLOOP:
1814 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1815 /* ensure exit at end of current loop */
1816 wwo->dwLoops = 1;
1818 SetEvent(ev);
1819 break;
1820 case WINE_WM_CLOSING:
1821 /* sanity check: this should not happen since the device must have been reset before */
1822 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1823 wwo->hThread = 0;
1824 wwo->state = WINE_WS_CLOSED;
1825 SetEvent(ev);
1826 ExitThread(0);
1827 /* shouldn't go here */
1828 default:
1829 FIXME("unknown message %d\n", msg);
1830 break;
1835 /**************************************************************************
1836 * wodPlayer_FeedDSP [internal]
1837 * Feed as much sound data as we can into the DSP and return the number of
1838 * milliseconds before it will be necessary to feed the DSP again.
1840 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1842 audio_buf_info dspspace;
1843 DWORD availInQ;
1845 if (!wodUpdatePlayedTotal(wwo, &dspspace)) return INFINITE;
1846 availInQ = dspspace.bytes;
1847 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1848 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1850 /* no more room... no need to try to feed */
1851 if (dspspace.fragments != 0) {
1852 /* Feed from partial wavehdr */
1853 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1854 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1857 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1858 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1859 do {
1860 TRACE("Setting time to elapse for %p to %u\n",
1861 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1862 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1863 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1864 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1867 if (wwo->bNeedPost) {
1868 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1869 * if it didn't get one, we give it the other */
1870 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1871 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1872 wwo->bNeedPost = FALSE;
1876 return wodPlayer_DSPWait(wwo);
1880 /**************************************************************************
1881 * wodPlayer [internal]
1883 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1885 WORD uDevID = (DWORD)pmt;
1886 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1887 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1888 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1889 DWORD dwSleepTime;
1891 wwo->state = WINE_WS_STOPPED;
1892 SetEvent(wwo->hStartUpEvent);
1894 for (;;) {
1895 /** Wait for the shortest time before an action is required. If there
1896 * are no pending actions, wait forever for a command.
1898 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1899 TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1900 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1901 wodPlayer_ProcessMessages(wwo);
1902 if (wwo->state == WINE_WS_PLAYING) {
1903 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1904 if (dwNextFeedTime != INFINITE)
1905 wwo->dwProjectedFinishTime = GetTickCount() + wodPlayer_TicksTillEmpty(wwo);
1906 else
1907 wwo->dwProjectedFinishTime = 0;
1909 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1910 if (dwNextFeedTime == INFINITE) {
1911 /* FeedDSP ran out of data, but before flushing, */
1912 /* check that a notification didn't give us more */
1913 wodPlayer_ProcessMessages(wwo);
1914 if (!wwo->lpPlayPtr) {
1915 TRACE("flushing\n");
1916 ioctl(wwo->ossdev->fd, SNDCTL_DSP_SYNC, 0);
1917 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1918 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1919 } else {
1920 TRACE("recovering\n");
1921 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1924 } else {
1925 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1930 /**************************************************************************
1931 * wodGetDevCaps [internal]
1933 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1935 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1937 if (lpCaps == NULL) {
1938 WARN("not enabled\n");
1939 return MMSYSERR_NOTENABLED;
1942 if (wDevID >= numOutDev) {
1943 WARN("numOutDev reached !\n");
1944 return MMSYSERR_BADDEVICEID;
1947 if (WOutDev[wDevID].ossdev->open_access == O_RDWR)
1948 memcpy(lpCaps, &WOutDev[wDevID].ossdev->duplex_out_caps, min(dwSize, sizeof(*lpCaps)));
1949 else
1950 memcpy(lpCaps, &WOutDev[wDevID].ossdev->out_caps, min(dwSize, sizeof(*lpCaps)));
1952 return MMSYSERR_NOERROR;
1955 /**************************************************************************
1956 * wodOpen [internal]
1958 DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1960 int audio_fragment;
1961 WINE_WAVEOUT* wwo;
1962 audio_buf_info info;
1963 DWORD ret;
1965 TRACE("(%u, %p[cb=%08x], %08X);\n", wDevID, lpDesc, lpDesc->dwCallback, dwFlags);
1966 if (lpDesc == NULL) {
1967 WARN("Invalid Parameter !\n");
1968 return MMSYSERR_INVALPARAM;
1970 if (wDevID >= numOutDev) {
1971 TRACE("MAX_WAVOUTDRV reached !\n");
1972 return MMSYSERR_BADDEVICEID;
1975 /* only PCM format is supported so far... */
1976 if (!supportedFormat(lpDesc->lpFormat)) {
1977 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1978 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1979 lpDesc->lpFormat->nSamplesPerSec);
1980 return WAVERR_BADFORMAT;
1983 if (dwFlags & WAVE_FORMAT_QUERY) {
1984 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1985 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1986 lpDesc->lpFormat->nSamplesPerSec);
1987 return MMSYSERR_NOERROR;
1990 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
1991 lpDesc->lpFormat->nSamplesPerSec,
1992 lpDesc->lpFormat->wBitsPerSample,
1993 lpDesc->lpFormat->nChannels,
1994 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
1995 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
1996 "UNSUPPORTED");
1998 wwo = &WOutDev[wDevID];
2000 if ((dwFlags & WAVE_DIRECTSOUND) &&
2001 !(wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
2002 /* not supported, ignore it */
2003 dwFlags &= ~WAVE_DIRECTSOUND;
2005 if (dwFlags & WAVE_DIRECTSOUND) {
2006 if (wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2007 /* we have realtime DirectSound, fragments just waste our time,
2008 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2009 audio_fragment = 0x0020000B;
2010 else
2011 /* to approximate realtime, we must use small fragments,
2012 * let's try to fragment the above 64KB (256 * 2^8) */
2013 audio_fragment = 0x01000008;
2014 } else {
2015 /* A wave device must have a worst case latency of 10 ms so calculate
2016 * the largest fragment size less than 10 ms long.
2018 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2019 int shift = 0;
2020 while ((1 << shift) <= fsize)
2021 shift++;
2022 shift--;
2023 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2026 TRACE("requesting %d %d byte fragments (%d ms/fragment)\n",
2027 audio_fragment >> 16, 1 << (audio_fragment & 0xffff),
2028 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2030 if (wwo->state != WINE_WS_CLOSED) {
2031 WARN("already allocated\n");
2032 return MMSYSERR_ALLOCATED;
2035 /* we want to be able to mmap() the device, which means it must be opened readable,
2036 * otherwise mmap() will fail (at least under Linux) */
2037 ret = OSS_OpenDevice(wwo->ossdev,
2038 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
2039 &audio_fragment,
2040 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
2041 lpDesc->lpFormat->nSamplesPerSec,
2042 lpDesc->lpFormat->nChannels,
2043 (lpDesc->lpFormat->wBitsPerSample == 16)
2044 ? AFMT_S16_LE : AFMT_U8);
2045 if ((ret==MMSYSERR_NOERROR) && (dwFlags & WAVE_DIRECTSOUND)) {
2046 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev->sample_rate;
2047 lpDesc->lpFormat->nChannels=wwo->ossdev->channels;
2048 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev->format == AFMT_U8 ? 8 : 16);
2049 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
2050 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
2051 TRACE("OSS_OpenDevice returned this format: %dx%dx%d\n",
2052 lpDesc->lpFormat->nSamplesPerSec,
2053 lpDesc->lpFormat->wBitsPerSample,
2054 lpDesc->lpFormat->nChannels);
2056 if (ret != 0) return ret;
2057 wwo->state = WINE_WS_STOPPED;
2059 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2061 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2062 copy_format(lpDesc->lpFormat, &wwo->waveFormat);
2064 if (wwo->waveFormat.Format.wBitsPerSample == 0) {
2065 WARN("Resetting zeroed wBitsPerSample\n");
2066 wwo->waveFormat.Format.wBitsPerSample = 8 *
2067 (wwo->waveFormat.Format.nAvgBytesPerSec /
2068 wwo->waveFormat.Format.nSamplesPerSec) /
2069 wwo->waveFormat.Format.nChannels;
2071 /* Read output space info for future reference */
2072 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2073 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
2074 OSS_CloseDevice(wwo->ossdev);
2075 wwo->state = WINE_WS_CLOSED;
2076 return MMSYSERR_NOTENABLED;
2079 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2080 info.fragsize, (info.fragsize * 1000) / (wwo->ossdev->sample_rate *
2081 wwo->ossdev->channels * (wwo->ossdev->format == AFMT_U8 ? 1 : 2)));
2083 /* Check that fragsize is correct per our settings above */
2084 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
2085 /* we've tried to set 1K fragments or less, but it didn't work */
2086 ERR("fragment size set failed, size is now %d\n", info.fragsize);
2087 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
2088 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
2091 /* Remember fragsize and total buffer size for future use */
2092 wwo->dwFragmentSize = info.fragsize;
2093 wwo->dwBufferSize = info.fragstotal * info.fragsize;
2094 wwo->dwPlayedTotal = 0;
2095 wwo->dwWrittenTotal = 0;
2096 wwo->bNeedPost = TRUE;
2098 TRACE("fd=%d fragstotal=%d fragsize=%d BufferSize=%d\n",
2099 wwo->ossdev->fd, info.fragstotal, info.fragsize, wwo->dwBufferSize);
2100 if (wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign) {
2101 ERR("Fragment doesn't contain an integral number of data blocks fragsize=%d BlockAlign=%d\n",wwo->dwFragmentSize,wwo->waveFormat.Format.nBlockAlign);
2102 /* Some SoundBlaster 16 cards return an incorrect (odd) fragment
2103 * size for 16 bit sound. This will cause a system crash when we try
2104 * to write just the specified odd number of bytes. So if we
2105 * detect something is wrong we'd better fix it.
2107 wwo->dwFragmentSize-=wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign;
2110 OSS_InitRingMessage(&wwo->msgRing);
2112 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2113 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
2114 if (wwo->hThread)
2115 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2116 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
2117 CloseHandle(wwo->hStartUpEvent);
2118 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
2120 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2121 wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
2122 wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
2123 wwo->waveFormat.Format.nBlockAlign);
2125 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
2128 /**************************************************************************
2129 * wodClose [internal]
2131 static DWORD wodClose(WORD wDevID)
2133 DWORD ret = MMSYSERR_NOERROR;
2134 WINE_WAVEOUT* wwo;
2136 TRACE("(%u);\n", wDevID);
2138 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2139 WARN("bad device ID !\n");
2140 return MMSYSERR_BADDEVICEID;
2143 wwo = &WOutDev[wDevID];
2144 if (wwo->lpQueuePtr) {
2145 WARN("buffers still playing !\n");
2146 ret = WAVERR_STILLPLAYING;
2147 } else {
2148 if (wwo->hThread != INVALID_HANDLE_VALUE) {
2149 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
2152 OSS_DestroyRingMessage(&wwo->msgRing);
2154 OSS_CloseDevice(wwo->ossdev);
2155 wwo->state = WINE_WS_CLOSED;
2156 wwo->dwFragmentSize = 0;
2157 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
2159 return ret;
2162 /**************************************************************************
2163 * wodWrite [internal]
2166 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2168 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
2170 /* first, do the sanity checks... */
2171 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2172 WARN("bad dev ID !\n");
2173 return MMSYSERR_BADDEVICEID;
2176 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
2177 return WAVERR_UNPREPARED;
2179 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2180 return WAVERR_STILLPLAYING;
2182 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2183 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2184 lpWaveHdr->lpNext = 0;
2186 if ((lpWaveHdr->dwBufferLength & (WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1)) != 0)
2188 WARN("WaveHdr length isn't a multiple of the PCM block size: %d %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].waveFormat.Format.nBlockAlign);
2189 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1);
2192 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2194 return MMSYSERR_NOERROR;
2197 /**************************************************************************
2198 * wodPause [internal]
2200 static DWORD wodPause(WORD wDevID)
2202 TRACE("(%u);!\n", wDevID);
2204 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2205 WARN("bad device ID !\n");
2206 return MMSYSERR_BADDEVICEID;
2209 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
2211 return MMSYSERR_NOERROR;
2214 /**************************************************************************
2215 * wodRestart [internal]
2217 static DWORD wodRestart(WORD wDevID)
2219 TRACE("(%u);\n", wDevID);
2221 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2222 WARN("bad device ID !\n");
2223 return MMSYSERR_BADDEVICEID;
2226 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2228 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2229 /* FIXME: Myst crashes with this ... hmm -MM
2230 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2233 return MMSYSERR_NOERROR;
2236 /**************************************************************************
2237 * wodReset [internal]
2239 static DWORD wodReset(WORD wDevID)
2241 TRACE("(%u);\n", wDevID);
2243 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2244 WARN("bad device ID !\n");
2245 return MMSYSERR_BADDEVICEID;
2248 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2250 return MMSYSERR_NOERROR;
2253 /**************************************************************************
2254 * wodGetPosition [internal]
2256 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2258 WINE_WAVEOUT* wwo;
2260 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
2262 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2263 WARN("bad device ID !\n");
2264 return MMSYSERR_BADDEVICEID;
2267 if (lpTime == NULL) {
2268 WARN("invalid parameter: lpTime == NULL\n");
2269 return MMSYSERR_INVALPARAM;
2272 wwo = &WOutDev[wDevID];
2273 #ifdef EXACT_WODPOSITION
2274 if (wwo->ossdev->open_access == O_RDWR) {
2275 if (wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2276 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2277 } else {
2278 if (wwo->ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2279 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2281 #endif
2283 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
2286 /**************************************************************************
2287 * wodBreakLoop [internal]
2289 static DWORD wodBreakLoop(WORD wDevID)
2291 TRACE("(%u);\n", wDevID);
2293 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2294 WARN("bad device ID !\n");
2295 return MMSYSERR_BADDEVICEID;
2297 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
2298 return MMSYSERR_NOERROR;
2301 /**************************************************************************
2302 * wodGetVolume [internal]
2304 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
2306 int mixer;
2307 int volume;
2308 DWORD left, right;
2309 DWORD last_left, last_right;
2311 TRACE("(%u, %p);\n", wDevID, lpdwVol);
2313 if (lpdwVol == NULL) {
2314 WARN("not enabled\n");
2315 return MMSYSERR_NOTENABLED;
2317 if (wDevID >= numOutDev) {
2318 WARN("invalid parameter\n");
2319 return MMSYSERR_INVALPARAM;
2321 if (WOutDev[wDevID].ossdev->open_access == O_RDWR) {
2322 if (!(WOutDev[wDevID].ossdev->duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2323 TRACE("Volume not supported\n");
2324 return MMSYSERR_NOTSUPPORTED;
2326 } else {
2327 if (!(WOutDev[wDevID].ossdev->out_caps.dwSupport & WAVECAPS_VOLUME)) {
2328 TRACE("Volume not supported\n");
2329 return MMSYSERR_NOTSUPPORTED;
2333 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_RDONLY|O_NDELAY)) < 0) {
2334 WARN("mixer device not available !\n");
2335 return MMSYSERR_NOTENABLED;
2337 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
2338 close(mixer);
2339 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)\n",
2340 WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2341 return MMSYSERR_NOTENABLED;
2343 close(mixer);
2345 left = LOBYTE(volume);
2346 right = HIBYTE(volume);
2347 TRACE("left=%d right=%d !\n", left, right);
2348 last_left = (LOWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2349 last_right = (HIWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2350 TRACE("last_left=%d last_right=%d !\n", last_left, last_right);
2351 if (last_left == left && last_right == right)
2352 *lpdwVol = WOutDev[wDevID].volume;
2353 else
2354 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
2355 return MMSYSERR_NOERROR;
2358 /**************************************************************************
2359 * wodSetVolume [internal]
2361 DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
2363 int mixer;
2364 int volume;
2365 DWORD left, right;
2367 TRACE("(%u, %08X);\n", wDevID, dwParam);
2369 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
2370 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
2371 volume = left + (right << 8);
2373 if (wDevID >= numOutDev) {
2374 WARN("invalid parameter: wDevID > %d\n", numOutDev);
2375 return MMSYSERR_INVALPARAM;
2377 if (WOutDev[wDevID].ossdev->open_access == O_RDWR) {
2378 if (!(WOutDev[wDevID].ossdev->duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2379 TRACE("Volume not supported\n");
2380 return MMSYSERR_NOTSUPPORTED;
2382 } else {
2383 if (!(WOutDev[wDevID].ossdev->out_caps.dwSupport & WAVECAPS_VOLUME)) {
2384 TRACE("Volume not supported\n");
2385 return MMSYSERR_NOTSUPPORTED;
2388 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_WRONLY|O_NDELAY)) < 0) {
2389 WARN("open(%s) failed (%s)\n", WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2390 return MMSYSERR_NOTENABLED;
2392 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
2393 close(mixer);
2394 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n",
2395 WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2396 return MMSYSERR_NOTENABLED;
2398 TRACE("volume=%04x\n", (unsigned)volume);
2399 close(mixer);
2401 /* save requested volume */
2402 WOutDev[wDevID].volume = dwParam;
2404 return MMSYSERR_NOERROR;
2407 /**************************************************************************
2408 * wodMessage (WINEOSS.7)
2410 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2411 DWORD dwParam1, DWORD dwParam2)
2413 TRACE("(%u, %s, %08X, %08X, %08X);\n",
2414 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
2416 switch (wMsg) {
2417 case DRVM_INIT:
2418 case DRVM_EXIT:
2419 case DRVM_ENABLE:
2420 case DRVM_DISABLE:
2421 /* FIXME: Pretend this is supported */
2422 return 0;
2423 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2424 case WODM_CLOSE: return wodClose (wDevID);
2425 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2426 case WODM_PAUSE: return wodPause (wDevID);
2427 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
2428 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
2429 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2430 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2431 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
2432 case WODM_GETNUMDEVS: return numOutDev;
2433 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
2434 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
2435 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2436 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2437 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
2438 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
2439 case WODM_RESTART: return wodRestart (wDevID);
2440 case WODM_RESET: return wodReset (wDevID);
2442 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2443 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2444 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
2445 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2446 default:
2447 FIXME("unknown message %d!\n", wMsg);
2449 return MMSYSERR_NOTSUPPORTED;
2452 /*======================================================================*
2453 * Low level WAVE IN implementation *
2454 *======================================================================*/
2456 /**************************************************************************
2457 * widNotifyClient [internal]
2459 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2461 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04X dwParam2 = %04X\n", wMsg,
2462 wMsg == WIM_OPEN ? "WIM_OPEN" : wMsg == WIM_CLOSE ? "WIM_CLOSE" :
2463 wMsg == WIM_DATA ? "WIM_DATA" : "Unknown", dwParam1, dwParam2);
2465 switch (wMsg) {
2466 case WIM_OPEN:
2467 case WIM_CLOSE:
2468 case WIM_DATA:
2469 if (wwi->wFlags != DCB_NULL &&
2470 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2471 (HDRVR)wwi->waveDesc.hWave, wMsg,
2472 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2473 WARN("can't notify client !\n");
2474 return MMSYSERR_ERROR;
2476 break;
2477 default:
2478 FIXME("Unknown callback message %u\n", wMsg);
2479 return MMSYSERR_INVALPARAM;
2481 return MMSYSERR_NOERROR;
2484 /**************************************************************************
2485 * widGetDevCaps [internal]
2487 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
2489 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
2491 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2493 if (wDevID >= numInDev) {
2494 TRACE("numOutDev reached !\n");
2495 return MMSYSERR_BADDEVICEID;
2498 memcpy(lpCaps, &WInDev[wDevID].ossdev->in_caps, min(dwSize, sizeof(*lpCaps)));
2499 return MMSYSERR_NOERROR;
2502 /**************************************************************************
2503 * widRecorder_ReadHeaders [internal]
2505 static void widRecorder_ReadHeaders(WINE_WAVEIN * wwi)
2507 enum win_wm_message tmp_msg;
2508 DWORD tmp_param;
2509 HANDLE tmp_ev;
2510 WAVEHDR* lpWaveHdr;
2512 while (OSS_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
2513 if (tmp_msg == WINE_WM_HEADER) {
2514 LPWAVEHDR* wh;
2515 lpWaveHdr = (LPWAVEHDR)tmp_param;
2516 lpWaveHdr->lpNext = 0;
2518 if (wwi->lpQueuePtr == 0)
2519 wwi->lpQueuePtr = lpWaveHdr;
2520 else {
2521 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2522 *wh = lpWaveHdr;
2524 } else {
2525 ERR("should only have headers left\n");
2530 /**************************************************************************
2531 * widRecorder [internal]
2533 static DWORD CALLBACK widRecorder(LPVOID pmt)
2535 WORD uDevID = (DWORD)pmt;
2536 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2537 WAVEHDR* lpWaveHdr;
2538 DWORD dwSleepTime;
2539 DWORD bytesRead;
2540 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2541 char *pOffset = buffer;
2542 audio_buf_info info;
2543 int xs;
2544 enum win_wm_message msg;
2545 DWORD param;
2546 HANDLE ev;
2547 int enable;
2549 wwi->state = WINE_WS_STOPPED;
2550 wwi->dwTotalRecorded = 0;
2551 wwi->dwTotalRead = 0;
2552 wwi->lpQueuePtr = NULL;
2554 SetEvent(wwi->hStartUpEvent);
2556 /* disable input so capture will begin when triggered */
2557 wwi->ossdev->bInputEnabled = FALSE;
2558 enable = getEnables(wwi->ossdev);
2559 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2560 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2562 /* the soundblaster live needs a micro wake to get its recording started
2563 * (or GETISPACE will have 0 frags all the time)
2565 read(wwi->ossdev->fd, &xs, 4);
2567 /* make sleep time to be # of ms to output a fragment */
2568 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
2569 TRACE("sleeptime=%d ms\n", dwSleepTime);
2571 for (;;) {
2572 /* wait for dwSleepTime or an event in thread's queue */
2573 /* FIXME: could improve wait time depending on queue state,
2574 * ie, number of queued fragments
2577 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2579 lpWaveHdr = wwi->lpQueuePtr;
2581 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info);
2582 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2584 /* read all the fragments accumulated so far */
2585 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2587 info.fragments --;
2589 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2591 /* directly read fragment in wavehdr */
2592 bytesRead = read(wwi->ossdev->fd,
2593 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2594 wwi->dwFragmentSize);
2596 TRACE("bytesRead=%d (direct)\n", bytesRead);
2597 if (bytesRead != (DWORD) -1)
2599 /* update number of bytes recorded in current buffer and by this device */
2600 lpWaveHdr->dwBytesRecorded += bytesRead;
2601 wwi->dwTotalRead += bytesRead;
2602 wwi->dwTotalRecorded = wwi->dwTotalRead;
2604 /* buffer is full. notify client */
2605 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2607 /* must copy the value of next waveHdr, because we have no idea of what
2608 * will be done with the content of lpWaveHdr in callback
2610 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2612 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2613 lpWaveHdr->dwFlags |= WHDR_DONE;
2615 wwi->lpQueuePtr = lpNext;
2616 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2617 lpWaveHdr = lpNext;
2619 } else {
2620 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev->dev_name,
2621 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2622 wwi->dwFragmentSize, strerror(errno));
2625 else
2627 /* read the fragment in a local buffer */
2628 bytesRead = read(wwi->ossdev->fd, buffer, wwi->dwFragmentSize);
2629 pOffset = buffer;
2631 TRACE("bytesRead=%d (local)\n", bytesRead);
2633 if (bytesRead == (DWORD) -1) {
2634 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev->dev_name,
2635 buffer, wwi->dwFragmentSize, strerror(errno));
2636 continue;
2639 /* copy data in client buffers */
2640 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2642 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2644 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2645 pOffset,
2646 dwToCopy);
2648 /* update number of bytes recorded in current buffer and by this device */
2649 lpWaveHdr->dwBytesRecorded += dwToCopy;
2650 wwi->dwTotalRead += dwToCopy;
2651 wwi->dwTotalRecorded = wwi->dwTotalRead;
2652 bytesRead -= dwToCopy;
2653 pOffset += dwToCopy;
2655 /* client buffer is full. notify client */
2656 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2658 /* must copy the value of next waveHdr, because we have no idea of what
2659 * will be done with the content of lpWaveHdr in callback
2661 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2662 TRACE("lpNext=%p\n", lpNext);
2664 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2665 lpWaveHdr->dwFlags |= WHDR_DONE;
2667 wwi->lpQueuePtr = lpNext;
2668 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2670 lpWaveHdr = lpNext;
2671 if (!lpNext && bytesRead) {
2672 /* before we give up, check for more header messages */
2673 while (OSS_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
2675 if (msg == WINE_WM_HEADER) {
2676 LPWAVEHDR hdr;
2677 OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
2678 hdr = ((LPWAVEHDR)param);
2679 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg), hdr, ev);
2680 hdr->lpNext = 0;
2681 if (lpWaveHdr == 0) {
2682 /* new head of queue */
2683 wwi->lpQueuePtr = lpWaveHdr = hdr;
2684 } else {
2685 /* insert buffer at the end of queue */
2686 LPWAVEHDR* wh;
2687 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2688 *wh = hdr;
2690 } else
2691 break;
2694 if (lpWaveHdr == 0) {
2695 /* no more buffer to copy data to, but we did read more.
2696 * what hasn't been copied will be dropped
2698 WARN("buffer under run! %u bytes dropped.\n", bytesRead);
2699 wwi->lpQueuePtr = NULL;
2700 break;
2709 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2711 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2713 TRACE("msg=%s param=0x%x\n", getCmdString(msg), param);
2714 switch (msg) {
2715 case WINE_WM_PAUSING:
2716 wwi->state = WINE_WS_PAUSED;
2717 /*FIXME("Device should stop recording\n");*/
2718 SetEvent(ev);
2719 break;
2720 case WINE_WM_STARTING:
2721 wwi->state = WINE_WS_PLAYING;
2723 if (wwi->ossdev->bTriggerSupport)
2725 /* start the recording */
2726 wwi->ossdev->bInputEnabled = TRUE;
2727 enable = getEnables(wwi->ossdev);
2728 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2729 wwi->ossdev->bInputEnabled = FALSE;
2730 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2733 else
2735 unsigned char data[4];
2736 /* read 4 bytes to start the recording */
2737 read(wwi->ossdev->fd, data, 4);
2740 SetEvent(ev);
2741 break;
2742 case WINE_WM_HEADER:
2743 lpWaveHdr = (LPWAVEHDR)param;
2744 lpWaveHdr->lpNext = 0;
2746 /* insert buffer at the end of queue */
2748 LPWAVEHDR* wh;
2749 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2750 *wh = lpWaveHdr;
2752 break;
2753 case WINE_WM_STOPPING:
2754 if (wwi->state != WINE_WS_STOPPED)
2756 if (wwi->ossdev->bTriggerSupport)
2758 /* stop the recording */
2759 wwi->ossdev->bInputEnabled = FALSE;
2760 enable = getEnables(wwi->ossdev);
2761 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2762 wwi->ossdev->bInputEnabled = FALSE;
2763 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2767 /* read any headers in queue */
2768 widRecorder_ReadHeaders(wwi);
2770 /* return current buffer to app */
2771 lpWaveHdr = wwi->lpQueuePtr;
2772 if (lpWaveHdr)
2774 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2775 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2776 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2777 lpWaveHdr->dwFlags |= WHDR_DONE;
2778 wwi->lpQueuePtr = lpNext;
2779 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2782 wwi->state = WINE_WS_STOPPED;
2783 SetEvent(ev);
2784 break;
2785 case WINE_WM_RESETTING:
2786 if (wwi->state != WINE_WS_STOPPED)
2788 if (wwi->ossdev->bTriggerSupport)
2790 /* stop the recording */
2791 wwi->ossdev->bInputEnabled = FALSE;
2792 enable = getEnables(wwi->ossdev);
2793 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2794 wwi->ossdev->bInputEnabled = FALSE;
2795 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2799 wwi->state = WINE_WS_STOPPED;
2800 wwi->dwTotalRecorded = 0;
2801 wwi->dwTotalRead = 0;
2803 /* read any headers in queue */
2804 widRecorder_ReadHeaders(wwi);
2806 /* return all buffers to the app */
2807 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2808 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2809 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2810 lpWaveHdr->dwFlags |= WHDR_DONE;
2811 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2812 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2815 wwi->lpQueuePtr = NULL;
2816 SetEvent(ev);
2817 break;
2818 case WINE_WM_UPDATE:
2819 if (wwi->state == WINE_WS_PLAYING) {
2820 audio_buf_info tmp_info;
2821 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &tmp_info) < 0)
2822 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2823 else
2824 wwi->dwTotalRecorded = wwi->dwTotalRead + tmp_info.bytes;
2826 SetEvent(ev);
2827 break;
2828 case WINE_WM_CLOSING:
2829 wwi->hThread = 0;
2830 wwi->state = WINE_WS_CLOSED;
2831 SetEvent(ev);
2832 HeapFree(GetProcessHeap(), 0, buffer);
2833 ExitThread(0);
2834 /* shouldn't go here */
2835 default:
2836 FIXME("unknown message %d\n", msg);
2837 break;
2841 ExitThread(0);
2842 /* just for not generating compilation warnings... should never be executed */
2843 return 0;
2847 /**************************************************************************
2848 * widOpen [internal]
2850 DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2852 WINE_WAVEIN* wwi;
2853 audio_buf_info info;
2854 int audio_fragment;
2855 DWORD ret;
2857 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
2858 if (lpDesc == NULL) {
2859 WARN("Invalid Parameter !\n");
2860 return MMSYSERR_INVALPARAM;
2862 if (wDevID >= numInDev) {
2863 WARN("bad device id: %d >= %d\n", wDevID, numInDev);
2864 return MMSYSERR_BADDEVICEID;
2867 /* only PCM format is supported so far... */
2868 if (!supportedFormat(lpDesc->lpFormat)) {
2869 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2870 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2871 lpDesc->lpFormat->nSamplesPerSec);
2872 return WAVERR_BADFORMAT;
2875 if (dwFlags & WAVE_FORMAT_QUERY) {
2876 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2877 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2878 lpDesc->lpFormat->nSamplesPerSec);
2879 return MMSYSERR_NOERROR;
2882 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2883 lpDesc->lpFormat->nSamplesPerSec,
2884 lpDesc->lpFormat->wBitsPerSample,
2885 lpDesc->lpFormat->nChannels,
2886 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
2887 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
2888 "UNSUPPORTED");
2890 wwi = &WInDev[wDevID];
2892 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2894 if ((dwFlags & WAVE_DIRECTSOUND) &&
2895 !(wwi->ossdev->in_caps_support & WAVECAPS_DIRECTSOUND))
2896 /* not supported, ignore it */
2897 dwFlags &= ~WAVE_DIRECTSOUND;
2899 if (dwFlags & WAVE_DIRECTSOUND) {
2900 TRACE("has DirectSoundCapture driver\n");
2901 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
2902 /* we have realtime DirectSound, fragments just waste our time,
2903 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2904 audio_fragment = 0x0020000B;
2905 else
2906 /* to approximate realtime, we must use small fragments,
2907 * let's try to fragment the above 64KB (256 * 2^8) */
2908 audio_fragment = 0x01000008;
2909 } else {
2910 TRACE("doesn't have DirectSoundCapture driver\n");
2911 if (wwi->ossdev->open_count > 0) {
2912 TRACE("Using output device audio_fragment\n");
2913 /* FIXME: This may not be optimal for capture but it allows us
2914 * to do hardware playback without hardware capture. */
2915 audio_fragment = wwi->ossdev->audio_fragment;
2916 } else {
2917 /* A wave device must have a worst case latency of 10 ms so calculate
2918 * the largest fragment size less than 10 ms long.
2920 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2921 int shift = 0;
2922 while ((1 << shift) <= fsize)
2923 shift++;
2924 shift--;
2925 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2929 TRACE("requesting %d %d byte fragments (%d ms)\n", audio_fragment >> 16,
2930 1 << (audio_fragment & 0xffff),
2931 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2933 ret = OSS_OpenDevice(wwi->ossdev, O_RDONLY, &audio_fragment,
2935 lpDesc->lpFormat->nSamplesPerSec,
2936 lpDesc->lpFormat->nChannels,
2937 (lpDesc->lpFormat->wBitsPerSample == 16)
2938 ? AFMT_S16_LE : AFMT_U8);
2939 if (ret != 0) return ret;
2940 wwi->state = WINE_WS_STOPPED;
2942 if (wwi->lpQueuePtr) {
2943 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2944 wwi->lpQueuePtr = NULL;
2946 wwi->dwTotalRecorded = 0;
2947 wwi->dwTotalRead = 0;
2948 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2950 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2951 copy_format(lpDesc->lpFormat, &wwi->waveFormat);
2953 if (wwi->waveFormat.Format.wBitsPerSample == 0) {
2954 WARN("Resetting zeroed wBitsPerSample\n");
2955 wwi->waveFormat.Format.wBitsPerSample = 8 *
2956 (wwi->waveFormat.Format.nAvgBytesPerSec /
2957 wwi->waveFormat.Format.nSamplesPerSec) /
2958 wwi->waveFormat.Format.nChannels;
2961 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
2962 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
2963 wwi->ossdev->dev_name, strerror(errno));
2964 OSS_CloseDevice(wwi->ossdev);
2965 wwi->state = WINE_WS_CLOSED;
2966 return MMSYSERR_NOTENABLED;
2969 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2970 info.fragsize, (info.fragsize * 1000) / (wwi->ossdev->sample_rate *
2971 wwi->ossdev->channels * (wwi->ossdev->format == AFMT_U8 ? 1 : 2)));
2973 wwi->dwFragmentSize = info.fragsize;
2975 TRACE("dwFragmentSize=%u\n", wwi->dwFragmentSize);
2976 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2977 wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
2978 wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
2979 wwi->waveFormat.Format.nBlockAlign);
2981 OSS_InitRingMessage(&wwi->msgRing);
2983 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2984 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
2985 if (wwi->hThread)
2986 SetThreadPriority(wwi->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2987 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2988 CloseHandle(wwi->hStartUpEvent);
2989 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2991 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2994 /**************************************************************************
2995 * widClose [internal]
2997 static DWORD widClose(WORD wDevID)
2999 WINE_WAVEIN* wwi;
3001 TRACE("(%u);\n", wDevID);
3002 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3003 WARN("can't close !\n");
3004 return MMSYSERR_INVALHANDLE;
3007 wwi = &WInDev[wDevID];
3009 if (wwi->lpQueuePtr != NULL) {
3010 WARN("still buffers open !\n");
3011 return WAVERR_STILLPLAYING;
3014 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
3015 OSS_CloseDevice(wwi->ossdev);
3016 wwi->state = WINE_WS_CLOSED;
3017 wwi->dwFragmentSize = 0;
3018 OSS_DestroyRingMessage(&wwi->msgRing);
3019 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
3022 /**************************************************************************
3023 * widAddBuffer [internal]
3025 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3027 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
3029 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3030 WARN("can't do it !\n");
3031 return MMSYSERR_INVALHANDLE;
3033 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
3034 TRACE("never been prepared !\n");
3035 return WAVERR_UNPREPARED;
3037 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
3038 TRACE("header already in use !\n");
3039 return WAVERR_STILLPLAYING;
3042 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
3043 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3044 lpWaveHdr->dwBytesRecorded = 0;
3045 lpWaveHdr->lpNext = NULL;
3047 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
3048 return MMSYSERR_NOERROR;
3051 /**************************************************************************
3052 * widStart [internal]
3054 static DWORD widStart(WORD wDevID)
3056 TRACE("(%u);\n", wDevID);
3057 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3058 WARN("can't start recording !\n");
3059 return MMSYSERR_INVALHANDLE;
3062 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
3063 return MMSYSERR_NOERROR;
3066 /**************************************************************************
3067 * widStop [internal]
3069 static DWORD widStop(WORD wDevID)
3071 TRACE("(%u);\n", wDevID);
3072 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3073 WARN("can't stop !\n");
3074 return MMSYSERR_INVALHANDLE;
3077 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3079 return MMSYSERR_NOERROR;
3082 /**************************************************************************
3083 * widReset [internal]
3085 static DWORD widReset(WORD wDevID)
3087 TRACE("(%u);\n", wDevID);
3088 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3089 WARN("can't reset !\n");
3090 return MMSYSERR_INVALHANDLE;
3092 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3093 return MMSYSERR_NOERROR;
3096 /**************************************************************************
3097 * widGetPosition [internal]
3099 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3101 WINE_WAVEIN* wwi;
3103 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
3105 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3106 WARN("can't get pos !\n");
3107 return MMSYSERR_INVALHANDLE;
3110 if (lpTime == NULL) {
3111 WARN("invalid parameter: lpTime == NULL\n");
3112 return MMSYSERR_INVALPARAM;
3115 wwi = &WInDev[wDevID];
3116 #ifdef EXACT_WIDPOSITION
3117 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
3118 OSS_AddRingMessage(&(wwi->msgRing), WINE_WM_UPDATE, 0, TRUE);
3119 #endif
3121 return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->waveFormat);
3124 /**************************************************************************
3125 * widMessage (WINEOSS.6)
3127 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3128 DWORD dwParam1, DWORD dwParam2)
3130 TRACE("(%u, %s, %08X, %08X, %08X);\n",
3131 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
3133 switch (wMsg) {
3134 case DRVM_INIT:
3135 case DRVM_EXIT:
3136 case DRVM_ENABLE:
3137 case DRVM_DISABLE:
3138 /* FIXME: Pretend this is supported */
3139 return 0;
3140 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3141 case WIDM_CLOSE: return widClose (wDevID);
3142 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3143 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
3144 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
3145 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
3146 case WIDM_GETNUMDEVS: return numInDev;
3147 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
3148 case WIDM_RESET: return widReset (wDevID);
3149 case WIDM_START: return widStart (wDevID);
3150 case WIDM_STOP: return widStop (wDevID);
3151 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
3152 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
3153 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3154 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3155 default:
3156 FIXME("unknown message %u!\n", wMsg);
3158 return MMSYSERR_NOTSUPPORTED;
3161 #else /* !HAVE_OSS */
3163 /**************************************************************************
3164 * wodMessage (WINEOSS.7)
3166 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3167 DWORD dwParam1, DWORD dwParam2)
3169 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3170 return MMSYSERR_NOTENABLED;
3173 /**************************************************************************
3174 * widMessage (WINEOSS.6)
3176 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3177 DWORD dwParam1, DWORD dwParam2)
3179 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3180 return MMSYSERR_NOTENABLED;
3183 #endif /* HAVE_OSS */