d3dxof: Small cleanup.
[wine/wine64.git] / dlls / wineoss.drv / audio.c
blob111b9d126588b080d1aa27fca88d30b8ec07733a
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 WINE_WAVEOUT WOutDev[MAX_WAVEDRV];
86 WINE_WAVEIN WInDev[MAX_WAVEDRV];
87 unsigned numOutDev;
88 unsigned numInDev;
90 /* state diagram for waveOut writing:
92 * +---------+-------------+---------------+---------------------------------+
93 * | state | function | event | new state |
94 * +---------+-------------+---------------+---------------------------------+
95 * | | open() | | STOPPED |
96 * | PAUSED | write() | | PAUSED |
97 * | STOPPED | write() | <thrd create> | PLAYING |
98 * | PLAYING | write() | HEADER | PLAYING |
99 * | (other) | write() | <error> | |
100 * | (any) | pause() | PAUSING | PAUSED |
101 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
102 * | (any) | reset() | RESETTING | STOPPED |
103 * | (any) | close() | CLOSING | CLOSED |
104 * +---------+-------------+---------------+---------------------------------+
107 /* These strings used only for tracing */
108 static const char * getCmdString(enum win_wm_message msg)
110 static char unknown[32];
111 #define MSG_TO_STR(x) case x: return #x
112 switch(msg) {
113 MSG_TO_STR(WINE_WM_PAUSING);
114 MSG_TO_STR(WINE_WM_RESTARTING);
115 MSG_TO_STR(WINE_WM_RESETTING);
116 MSG_TO_STR(WINE_WM_HEADER);
117 MSG_TO_STR(WINE_WM_UPDATE);
118 MSG_TO_STR(WINE_WM_BREAKLOOP);
119 MSG_TO_STR(WINE_WM_CLOSING);
120 MSG_TO_STR(WINE_WM_STARTING);
121 MSG_TO_STR(WINE_WM_STOPPING);
123 #undef MSG_TO_STR
124 sprintf(unknown, "UNKNOWN(0x%08x)", msg);
125 return unknown;
128 int getEnables(OSS_DEVICE *ossdev)
130 return ( (ossdev->bOutputEnabled ? PCM_ENABLE_OUTPUT : 0) |
131 (ossdev->bInputEnabled ? PCM_ENABLE_INPUT : 0) );
134 static const char * getMessage(UINT msg)
136 static char unknown[32];
137 #define MSG_TO_STR(x) case x: return #x
138 switch(msg) {
139 MSG_TO_STR(DRVM_INIT);
140 MSG_TO_STR(DRVM_EXIT);
141 MSG_TO_STR(DRVM_ENABLE);
142 MSG_TO_STR(DRVM_DISABLE);
143 MSG_TO_STR(WIDM_OPEN);
144 MSG_TO_STR(WIDM_CLOSE);
145 MSG_TO_STR(WIDM_ADDBUFFER);
146 MSG_TO_STR(WIDM_PREPARE);
147 MSG_TO_STR(WIDM_UNPREPARE);
148 MSG_TO_STR(WIDM_GETDEVCAPS);
149 MSG_TO_STR(WIDM_GETNUMDEVS);
150 MSG_TO_STR(WIDM_GETPOS);
151 MSG_TO_STR(WIDM_RESET);
152 MSG_TO_STR(WIDM_START);
153 MSG_TO_STR(WIDM_STOP);
154 MSG_TO_STR(WODM_OPEN);
155 MSG_TO_STR(WODM_CLOSE);
156 MSG_TO_STR(WODM_WRITE);
157 MSG_TO_STR(WODM_PAUSE);
158 MSG_TO_STR(WODM_GETPOS);
159 MSG_TO_STR(WODM_BREAKLOOP);
160 MSG_TO_STR(WODM_PREPARE);
161 MSG_TO_STR(WODM_UNPREPARE);
162 MSG_TO_STR(WODM_GETDEVCAPS);
163 MSG_TO_STR(WODM_GETNUMDEVS);
164 MSG_TO_STR(WODM_GETPITCH);
165 MSG_TO_STR(WODM_SETPITCH);
166 MSG_TO_STR(WODM_GETPLAYBACKRATE);
167 MSG_TO_STR(WODM_SETPLAYBACKRATE);
168 MSG_TO_STR(WODM_GETVOLUME);
169 MSG_TO_STR(WODM_SETVOLUME);
170 MSG_TO_STR(WODM_RESTART);
171 MSG_TO_STR(WODM_RESET);
172 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
173 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
174 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
175 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
177 #undef MSG_TO_STR
178 sprintf(unknown, "UNKNOWN(0x%04x)", msg);
179 return unknown;
182 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
184 TRACE("(%u, %p)\n", wDevID, dwParam1);
186 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev.interface_name, -1,
187 NULL, 0 ) * sizeof(WCHAR);
188 return MMSYSERR_NOERROR;
191 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
193 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev.interface_name, -1,
194 NULL, 0 ) * sizeof(WCHAR))
196 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev.interface_name, -1,
197 dwParam1, dwParam2 / sizeof(WCHAR));
198 return MMSYSERR_NOERROR;
201 return MMSYSERR_INVALPARAM;
204 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
206 TRACE("(%u, %p)\n", wDevID, dwParam1);
208 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev.interface_name, -1,
209 NULL, 0 ) * sizeof(WCHAR);
210 return MMSYSERR_NOERROR;
213 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
215 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev.interface_name, -1,
216 NULL, 0 ) * sizeof(WCHAR))
218 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev.interface_name, -1,
219 dwParam1, dwParam2 / sizeof(WCHAR));
220 return MMSYSERR_NOERROR;
223 return MMSYSERR_INVALPARAM;
226 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
227 WAVEFORMATPCMEX* format)
229 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
230 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
231 format->Format.nChannels, format->Format.nAvgBytesPerSec);
232 TRACE("Position in bytes=%u\n", position);
234 switch (lpTime->wType) {
235 case TIME_SAMPLES:
236 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
237 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
238 break;
239 case TIME_MS:
240 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
241 TRACE("TIME_MS=%u\n", lpTime->u.ms);
242 break;
243 case TIME_SMPTE:
244 lpTime->u.smpte.fps = 30;
245 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
246 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
247 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
248 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
249 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
250 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
251 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
252 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
253 lpTime->u.smpte.fps = 30;
254 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
255 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
256 lpTime->u.smpte.hour, lpTime->u.smpte.min,
257 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
258 break;
259 default:
260 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
261 lpTime->wType = TIME_BYTES;
262 /* fall through */
263 case TIME_BYTES:
264 lpTime->u.cb = position;
265 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
266 break;
268 return MMSYSERR_NOERROR;
271 static BOOL supportedFormat(LPWAVEFORMATEX wf)
273 TRACE("(%p)\n",wf);
275 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
276 return FALSE;
278 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
279 if (wf->nChannels >= 1 && wf->nChannels <= MAX_CHANNELS) {
280 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
281 return TRUE;
283 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
284 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
286 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
287 if (wf->nChannels >=1 && wf->nChannels <= MAX_CHANNELS) {
288 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
289 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
290 return TRUE;
291 } else
292 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
294 } else
295 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
296 } else
297 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
299 return FALSE;
302 void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
304 ZeroMemory(wf2, sizeof(wf2));
305 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
306 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
307 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
308 memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
309 else
310 memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
313 /*======================================================================*
314 * Low level WAVE implementation *
315 *======================================================================*/
317 /******************************************************************
318 * OSS_RawOpenDevice
320 * Low level device opening (from values stored in ossdev)
322 static DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
324 int fd, val, rc;
325 TRACE("(%p,%d)\n",ossdev,strict_format);
327 TRACE("open_access=%s\n",
328 ossdev->open_access == O_RDONLY ? "O_RDONLY" :
329 ossdev->open_access == O_WRONLY ? "O_WRONLY" :
330 ossdev->open_access == O_RDWR ? "O_RDWR" : "Unknown");
332 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
334 WARN("Couldn't open %s (%s)\n", ossdev->dev_name, strerror(errno));
335 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
337 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
338 /* turn full duplex on if it has been requested */
339 if (ossdev->open_access == O_RDWR && ossdev->full_duplex) {
340 rc = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
341 /* on *BSD, as full duplex is always enabled by default, this ioctl
342 * will fail with EINVAL
343 * so, we don't consider EINVAL an error here
345 if (rc != 0 && errno != EINVAL) {
346 WARN("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev->dev_name, strerror(errno));
347 goto error2;
351 if (ossdev->audio_fragment) {
352 rc = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
353 if (rc != 0) {
354 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev->dev_name, strerror(errno));
355 goto error2;
359 /* First size and channels then samplerate */
360 if (ossdev->format>=0)
362 val = ossdev->format;
363 rc = ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
364 if (rc != 0 || val != ossdev->format) {
365 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
366 if (strict_format)
367 goto error;
370 if (ossdev->channels>=0)
372 val = ossdev->channels;
373 rc = ioctl(fd, SNDCTL_DSP_CHANNELS, &ossdev->channels);
374 if (rc != 0 || val != ossdev->channels) {
375 TRACE("Can't set channels to %u (returned %d)\n", val, ossdev->channels);
376 if (strict_format)
377 goto error;
380 if (ossdev->sample_rate>=0)
382 val = ossdev->sample_rate;
383 rc = ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
384 if (rc != 0 || !NEAR_MATCH(val, ossdev->sample_rate)) {
385 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
386 if (strict_format)
387 goto error;
390 ossdev->fd = fd;
392 ossdev->bOutputEnabled = TRUE; /* OSS enables by default */
393 ossdev->bInputEnabled = TRUE; /* OSS enables by default */
394 if (ossdev->open_access == O_RDONLY)
395 ossdev->bOutputEnabled = FALSE;
396 if (ossdev->open_access == O_WRONLY)
397 ossdev->bInputEnabled = FALSE;
399 if (ossdev->bTriggerSupport) {
400 int trigger;
401 trigger = getEnables(ossdev);
402 /* If we do not have full duplex, but they opened RDWR
403 ** (as you have to in order for an mmap to succeed)
404 ** then we start out with input off
406 if (ossdev->open_access == O_RDWR && !ossdev->full_duplex &&
407 ossdev->bInputEnabled && ossdev->bOutputEnabled) {
408 ossdev->bInputEnabled = FALSE;
409 trigger &= ~PCM_ENABLE_INPUT;
410 ioctl(fd, SNDCTL_DSP_SETTRIGGER, &trigger);
414 return MMSYSERR_NOERROR;
416 error:
417 close(fd);
418 return WAVERR_BADFORMAT;
419 error2:
420 close(fd);
421 return MMSYSERR_ERROR;
424 /******************************************************************
425 * OSS_OpenDevice
427 * since OSS has poor capabilities in full duplex, we try here to let a program
428 * open the device for both waveout and wavein streams...
429 * this is hackish, but it's the way OSS interface is done...
431 DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
432 int* frag, int strict_format,
433 int sample_rate, int channels, int fmt)
435 DWORD ret;
436 DWORD open_access;
437 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev,req_access,frag,strict_format,sample_rate,channels,fmt);
439 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
441 TRACE("Opening RDWR because full_duplex=%d and req_access=%d\n",
442 ossdev->full_duplex,req_access);
443 open_access = O_RDWR;
445 else
447 open_access=req_access;
450 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
451 if (ossdev->open_count == 0)
453 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
455 ossdev->audio_fragment = (frag) ? *frag : 0;
456 ossdev->sample_rate = sample_rate;
457 ossdev->channels = channels;
458 ossdev->format = fmt;
459 ossdev->open_access = open_access;
460 ossdev->owner_tid = GetCurrentThreadId();
462 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
463 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
464 (req_access == O_RDONLY || req_access == O_WRONLY))
466 int enable;
467 if (req_access == O_WRONLY)
468 ossdev->bInputEnabled=0;
469 else
470 ossdev->bOutputEnabled=0;
471 enable = getEnables(ossdev);
472 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
473 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
474 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
477 else
479 /* check we really open with the same parameters */
480 if (ossdev->open_access != open_access)
482 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
483 return WAVERR_BADFORMAT;
486 /* check if the audio parameters are the same */
487 if (ossdev->sample_rate != sample_rate ||
488 ossdev->channels != channels ||
489 ossdev->format != fmt)
491 /* This is not a fatal error because MSACM might do the remapping */
492 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
493 "OSS doesn't allow us different parameters\n"
494 "audio_frag(%x/%x) sample_rate(%d/%d) channels(%d/%d) fmt(%d/%d)\n",
495 ossdev->audio_fragment, frag ? *frag : 0,
496 ossdev->sample_rate, sample_rate,
497 ossdev->channels, channels,
498 ossdev->format, fmt);
499 return WAVERR_BADFORMAT;
501 /* check if the fragment sizes are the same */
502 if (ossdev->audio_fragment != (frag ? *frag : 0) )
504 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
505 "Please run winecfg, open \"Audio\" page and set\n"
506 "\"Hardware Acceleration\" to \"Emulation\".\n");
507 return WAVERR_BADFORMAT;
509 if (GetCurrentThreadId() != ossdev->owner_tid)
511 WARN("Another thread is trying to access audio...\n");
512 return MMSYSERR_ERROR;
514 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
515 (req_access == O_RDONLY || req_access == O_WRONLY))
517 int enable;
518 if (req_access == O_WRONLY)
519 ossdev->bOutputEnabled=1;
520 else
521 ossdev->bInputEnabled=1;
522 enable = getEnables(ossdev);
523 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
524 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
525 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
529 ossdev->open_count++;
531 return MMSYSERR_NOERROR;
534 /******************************************************************
535 * OSS_CloseDevice
539 void OSS_CloseDevice(OSS_DEVICE* ossdev)
541 TRACE("(%p)\n",ossdev);
542 if (ossdev->open_count>0) {
543 ossdev->open_count--;
544 } else {
545 WARN("OSS_CloseDevice called too many times\n");
547 if (ossdev->open_count == 0)
549 fcntl(ossdev->fd, F_SETFL, fcntl(ossdev->fd, F_GETFL) & ~O_NDELAY);
550 /* reset the device before we close it in case it is in a bad state */
551 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
552 if (close(ossdev->fd) != 0) FIXME("Cannot close %d: %s\n", ossdev->fd, strerror(errno));
556 /******************************************************************
557 * OSS_ResetDevice
559 * Resets the device. OSS Commercial requires the device to be closed
560 * after a SNDCTL_DSP_RESET ioctl call... this function implements
561 * this behavior...
562 * FIXME: This causes problems when doing full duplex so we really
563 * only reset when not doing full duplex. We need to do this better
564 * someday.
566 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
568 DWORD ret = MMSYSERR_NOERROR;
569 int old_fd = ossdev->fd;
570 TRACE("(%p)\n", ossdev);
572 if (ossdev->open_count == 1) {
573 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
575 perror("ioctl SNDCTL_DSP_RESET");
576 return -1;
578 close(ossdev->fd);
579 ret = OSS_RawOpenDevice(ossdev, 1);
580 TRACE("Changing fd from %d to %d\n", old_fd, ossdev->fd);
581 } else
582 WARN("Not resetting device because it is in full duplex mode!\n");
584 return ret;
587 static const int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
588 static const int win_std_rates[5]={96000,48000,44100,22050,11025};
589 static const int win_std_formats[2][2][5]=
590 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
591 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
592 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
593 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
594 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
595 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
596 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
597 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
600 static void OSS_Info(int fd)
602 /* Note that this only reports the formats supported by the hardware.
603 * The driver may support other formats and do the conversions in
604 * software which is why we don't use this value
606 int oss_mask, oss_caps;
607 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &oss_mask) >= 0) {
608 TRACE("Formats=%08x ( ", oss_mask);
609 if (oss_mask & AFMT_MU_LAW) TRACE("AFMT_MU_LAW ");
610 if (oss_mask & AFMT_A_LAW) TRACE("AFMT_A_LAW ");
611 if (oss_mask & AFMT_IMA_ADPCM) TRACE("AFMT_IMA_ADPCM ");
612 if (oss_mask & AFMT_U8) TRACE("AFMT_U8 ");
613 if (oss_mask & AFMT_S16_LE) TRACE("AFMT_S16_LE ");
614 if (oss_mask & AFMT_S16_BE) TRACE("AFMT_S16_BE ");
615 if (oss_mask & AFMT_S8) TRACE("AFMT_S8 ");
616 if (oss_mask & AFMT_U16_LE) TRACE("AFMT_U16_LE ");
617 if (oss_mask & AFMT_U16_BE) TRACE("AFMT_U16_BE ");
618 if (oss_mask & AFMT_MPEG) TRACE("AFMT_MPEG ");
619 #ifdef AFMT_AC3
620 if (oss_mask & AFMT_AC3) TRACE("AFMT_AC3 ");
621 #endif
622 #ifdef AFMT_VORBIS
623 if (oss_mask & AFMT_VORBIS) TRACE("AFMT_VORBIS ");
624 #endif
625 #ifdef AFMT_S32_LE
626 if (oss_mask & AFMT_S32_LE) TRACE("AFMT_S32_LE ");
627 #endif
628 #ifdef AFMT_S32_BE
629 if (oss_mask & AFMT_S32_BE) TRACE("AFMT_S32_BE ");
630 #endif
631 #ifdef AFMT_FLOAT
632 if (oss_mask & AFMT_FLOAT) TRACE("AFMT_FLOAT ");
633 #endif
634 #ifdef AFMT_S24_LE
635 if (oss_mask & AFMT_S24_LE) TRACE("AFMT_S24_LE ");
636 #endif
637 #ifdef AFMT_S24_BE
638 if (oss_mask & AFMT_S24_BE) TRACE("AFMT_S24_BE ");
639 #endif
640 #ifdef AFMT_SPDIF_RAW
641 if (oss_mask & AFMT_SPDIF_RAW) TRACE("AFMT_SPDIF_RAW ");
642 #endif
643 TRACE(")\n");
645 if (ioctl(fd, SNDCTL_DSP_GETCAPS, &oss_caps) >= 0) {
646 TRACE("Caps=%08x\n",oss_caps);
647 TRACE("\tRevision: %d\n", oss_caps&DSP_CAP_REVISION);
648 TRACE("\tDuplex: %s\n", oss_caps & DSP_CAP_DUPLEX ? "true" : "false");
649 TRACE("\tRealtime: %s\n", oss_caps & DSP_CAP_REALTIME ? "true" : "false");
650 TRACE("\tBatch: %s\n", oss_caps & DSP_CAP_BATCH ? "true" : "false");
651 TRACE("\tCoproc: %s\n", oss_caps & DSP_CAP_COPROC ? "true" : "false");
652 TRACE("\tTrigger: %s\n", oss_caps & DSP_CAP_TRIGGER ? "true" : "false");
653 TRACE("\tMmap: %s\n", oss_caps & DSP_CAP_MMAP ? "true" : "false");
654 #ifdef DSP_CAP_MULTI
655 TRACE("\tMulti: %s\n", oss_caps & DSP_CAP_MULTI ? "true" : "false");
656 #endif
657 #ifdef DSP_CAP_BIND
658 TRACE("\tBind: %s\n", oss_caps & DSP_CAP_BIND ? "true" : "false");
659 #endif
660 #ifdef DSP_CAP_INPUT
661 TRACE("\tInput: %s\n", oss_caps & DSP_CAP_INPUT ? "true" : "false");
662 #endif
663 #ifdef DSP_CAP_OUTPUT
664 TRACE("\tOutput: %s\n", oss_caps & DSP_CAP_OUTPUT ? "true" : "false");
665 #endif
666 #ifdef DSP_CAP_VIRTUAL
667 TRACE("\tVirtual: %s\n", oss_caps & DSP_CAP_VIRTUAL ? "true" : "false");
668 #endif
669 #ifdef DSP_CAP_ANALOGOUT
670 TRACE("\tAnalog Out: %s\n", oss_caps & DSP_CAP_ANALOGOUT ? "true" : "false");
671 #endif
672 #ifdef DSP_CAP_ANALOGIN
673 TRACE("\tAnalog In: %s\n", oss_caps & DSP_CAP_ANALOGIN ? "true" : "false");
674 #endif
675 #ifdef DSP_CAP_DIGITALOUT
676 TRACE("\tDigital Out: %s\n", oss_caps & DSP_CAP_DIGITALOUT ? "true" : "false");
677 #endif
678 #ifdef DSP_CAP_DIGITALIN
679 TRACE("\tDigital In: %s\n", oss_caps & DSP_CAP_DIGITALIN ? "true" : "false");
680 #endif
681 #ifdef DSP_CAP_ADMASK
682 TRACE("\tA/D Mask: %s\n", oss_caps & DSP_CAP_ADMASK ? "true" : "false");
683 #endif
684 #ifdef DSP_CAP_SHADOW
685 TRACE("\tShadow: %s\n", oss_caps & DSP_CAP_SHADOW ? "true" : "false");
686 #endif
687 #ifdef DSP_CH_MASK
688 TRACE("\tChannel Mask: %x\n", oss_caps & DSP_CH_MASK);
689 #endif
690 #ifdef DSP_CAP_SLAVE
691 TRACE("\tSlave: %s\n", oss_caps & DSP_CAP_SLAVE ? "true" : "false");
692 #endif
696 /******************************************************************
697 * OSS_WaveOutInit
701 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
703 int rc,arg;
704 int f,c;
705 unsigned int 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;
881 unsigned int r;
882 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
884 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0,-1,-1,-1) != 0)
885 return FALSE;
887 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
889 #if defined(SNDCTL_MIXERINFO)
891 int mixer;
892 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
893 oss_mixerinfo info;
894 info.dev = 0;
895 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
896 MultiByteToWideChar(CP_ACP, 0, info.name, -1,
897 ossdev->in_caps.szPname,
898 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
899 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
900 } else {
901 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
903 close(mixer);
904 } else {
905 WARN("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
908 #elif defined(SOUND_MIXER_INFO)
910 int mixer;
911 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
912 mixer_info info;
913 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
914 MultiByteToWideChar(CP_ACP, 0, info.name, -1,
915 ossdev->in_caps.szPname,
916 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
917 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
918 } else {
919 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
920 * implement it properly, and there are probably similar issues
921 * on other platforms, so we warn but try to go ahead.
923 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
925 close(mixer);
926 } else {
927 WARN("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
930 #endif /* SOUND_MIXER_INFO */
932 if (WINE_TRACE_ON(wave))
933 OSS_Info(ossdev->fd);
935 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
936 ossdev->in_caps.wPid = 0x0001; /* Product ID */
938 ossdev->in_caps.dwFormats = 0x00000000;
939 ossdev->in_caps.wChannels = 1;
940 ossdev->in_caps.wReserved1 = 0;
942 /* direct sound caps */
943 ossdev->dsc_caps.dwSize = sizeof(ossdev->dsc_caps);
944 ossdev->dsc_caps.dwFlags = 0;
945 ossdev->dsc_caps.dwFormats = 0x00000000;
946 ossdev->dsc_caps.dwChannels = 1;
948 /* See the comment in OSS_WaveOutInit for the loop order */
949 for (f=0;f<2;f++) {
950 arg=win_std_oss_fmts[f];
951 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
952 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
953 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
954 rc,arg,win_std_oss_fmts[f]);
955 continue;
958 for (c = 1; c <= MAX_CHANNELS; c++) {
959 arg=c;
960 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
961 if( rc == -1) break;
962 if (rc!=0 || arg!=c) {
963 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
964 continue;
966 if (c > 1) {
967 ossdev->in_caps.wChannels = c;
968 ossdev->dsc_caps.dwChannels = c;
971 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
972 arg=win_std_rates[r];
973 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
974 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
975 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
976 ossdev->in_caps.dwFormats|=win_std_formats[f][c-1][r];
977 ossdev->dsc_caps.dwFormats|=win_std_formats[f][c-1][r];
982 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
983 if (arg & DSP_CAP_TRIGGER)
984 ossdev->bTriggerSupport = TRUE;
985 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
986 !(arg & DSP_CAP_BATCH)) {
987 /* FIXME: enable the next statement if you want to work on the driver */
988 #if 0
989 ossdev->in_caps_support |= WAVECAPS_DIRECTSOUND;
990 #endif
992 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH))
993 ossdev->in_caps_support |= WAVECAPS_SAMPLEACCURATE;
995 OSS_CloseDevice(ossdev);
996 TRACE("in wChannels = %d, dwFormats = %08X, in_caps_support = %08X\n",
997 ossdev->in_caps.wChannels, ossdev->in_caps.dwFormats, ossdev->in_caps_support);
998 return TRUE;
1001 /******************************************************************
1002 * OSS_WaveFullDuplexInit
1006 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
1008 int rc,arg;
1009 int f,c;
1010 unsigned int r;
1011 int caps;
1012 BOOL has_mixer = FALSE;
1013 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
1015 /* The OSS documentation says we must call SNDCTL_SETDUPLEX
1016 * *before* checking for SNDCTL_DSP_GETCAPS otherwise we may
1017 * get the wrong result. This ioctl must even be done before
1018 * setting the fragment size so that only OSS_RawOpenDevice is
1019 * in a position to do it. So we set full_duplex speculatively
1020 * and adjust right after.
1022 ossdev->full_duplex=1;
1023 rc=OSS_OpenDevice(ossdev, O_RDWR, NULL, 0,-1,-1,-1);
1024 ossdev->full_duplex=0;
1025 if (rc != 0)
1026 return;
1028 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
1030 #if defined(SNDCTL_MIXERINFO)
1032 int mixer;
1033 if ((mixer = open(ossdev->mixer_name, O_RDWR|O_NDELAY)) >= 0) {
1034 oss_mixerinfo info;
1035 info.dev = 0;
1036 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
1037 has_mixer = TRUE;
1038 } else {
1039 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
1041 close(mixer);
1042 } else {
1043 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
1046 #elif defined(SOUND_MIXER_INFO)
1048 int mixer;
1049 if ((mixer = open(ossdev->mixer_name, O_RDWR|O_NDELAY)) >= 0) {
1050 mixer_info info;
1051 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
1052 has_mixer = TRUE;
1053 } else {
1054 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
1055 * implement it properly, and there are probably similar issues
1056 * on other platforms, so we warn but try to go ahead.
1058 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
1060 close(mixer);
1061 } else {
1062 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
1065 #endif /* SOUND_MIXER_INFO */
1067 TRACE("%s\n", ossdev->ds_desc.szDesc);
1069 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
1070 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
1072 ossdev->duplex_out_caps = ossdev->out_caps;
1074 ossdev->duplex_out_caps.wChannels = 1;
1075 ossdev->duplex_out_caps.dwFormats = 0x00000000;
1076 ossdev->duplex_out_caps.dwSupport = has_mixer ? WAVECAPS_VOLUME : 0;
1078 if (WINE_TRACE_ON(wave))
1079 OSS_Info(ossdev->fd);
1081 /* See the comment in OSS_WaveOutInit for the loop order */
1082 for (f=0;f<2;f++) {
1083 arg=win_std_oss_fmts[f];
1084 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
1085 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
1086 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1087 rc,arg,win_std_oss_fmts[f]);
1088 continue;
1091 for (c = 1; c <= MAX_CHANNELS; c++) {
1092 arg=c;
1093 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
1094 if( rc == -1) break;
1095 if (rc!=0 || arg!=c) {
1096 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
1097 continue;
1099 if (c == 1) {
1100 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
1101 } else if (c == 2) {
1102 ossdev->duplex_out_caps.wChannels = 2;
1103 if (has_mixer)
1104 ossdev->duplex_out_caps.dwSupport|=WAVECAPS_LRVOLUME;
1105 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
1106 } else
1107 ossdev->duplex_out_caps.wChannels = c;
1109 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
1110 arg=win_std_rates[r];
1111 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
1112 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
1113 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
1114 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
1115 ossdev->duplex_out_caps.dwFormats|=win_std_formats[f][c-1][r];
1120 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
1121 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
1122 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
1124 /* well, might as well use the DirectSound cap flag for something */
1125 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
1126 !(arg & DSP_CAP_BATCH)) {
1127 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
1130 OSS_CloseDevice(ossdev);
1131 TRACE("duplex wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
1132 ossdev->duplex_out_caps.wChannels,
1133 ossdev->duplex_out_caps.dwFormats,
1134 ossdev->duplex_out_caps.dwSupport);
1137 static char* StrDup(const char* str, const char* def)
1139 char* dst;
1140 if (str==NULL)
1141 str=def;
1142 dst=HeapAlloc(GetProcessHeap(),0,strlen(str)+1);
1143 strcpy(dst, str);
1144 return dst;
1147 /******************************************************************
1148 * OSS_WaveInit
1150 * Initialize internal structures from OSS information
1152 LRESULT OSS_WaveInit(void)
1154 char* str;
1155 unsigned int i;
1157 /* FIXME: Remove unneeded members of WOutDev and WInDev */
1158 TRACE("()\n");
1160 str=getenv("AUDIODEV");
1161 if (str!=NULL)
1163 WOutDev[0].ossdev.dev_name = WInDev[0].ossdev.dev_name = StrDup(str,"");
1164 WOutDev[0].ossdev.mixer_name = WInDev[0].ossdev.mixer_name = StrDup(getenv("MIXERDEV"),"/dev/mixer");
1165 for (i = 1; i < MAX_WAVEDRV; ++i)
1167 WOutDev[i].ossdev.dev_name = WInDev[i].ossdev.dev_name = StrDup("",NULL);
1168 WOutDev[i].ossdev.mixer_name = WInDev[i].ossdev.mixer_name = StrDup("",NULL);
1171 else
1173 WOutDev[0].ossdev.dev_name = WInDev[0].ossdev.dev_name = StrDup("/dev/dsp",NULL);
1174 WOutDev[0].ossdev.mixer_name = WInDev[0].ossdev.mixer_name = StrDup("/dev/mixer",NULL);
1175 for (i = 1; i < MAX_WAVEDRV; ++i)
1177 WOutDev[i].ossdev.dev_name = WInDev[i].ossdev.dev_name = HeapAlloc(GetProcessHeap(),0,11);
1178 sprintf(WOutDev[i].ossdev.dev_name, "/dev/dsp%u", i);
1179 WOutDev[i].ossdev.mixer_name = WInDev[i].ossdev.mixer_name = HeapAlloc(GetProcessHeap(),0,13);
1180 sprintf(WOutDev[i].ossdev.mixer_name, "/dev/mixer%u", i);
1184 for (i = 0; i < MAX_WAVEDRV; ++i)
1186 WOutDev[i].ossdev.interface_name = WInDev[i].ossdev.interface_name =
1187 HeapAlloc(GetProcessHeap(),0,9+strlen(WOutDev[i].ossdev.dev_name)+1);
1188 sprintf(WOutDev[i].ossdev.interface_name, "wineoss: %s", WOutDev[i].ossdev.dev_name);
1191 /* start with output devices */
1192 for (i = 0; i < MAX_WAVEDRV; ++i)
1194 if (*WOutDev[i].ossdev.dev_name == '\0' || OSS_WaveOutInit(&WOutDev[i].ossdev))
1196 WOutDev[numOutDev].state = WINE_WS_CLOSED;
1197 WOutDev[numOutDev].volume = 0xffffffff;
1198 numOutDev++;
1202 /* then do input devices */
1203 for (i = 0; i < MAX_WAVEDRV; ++i)
1205 if (*WInDev[i].ossdev.dev_name=='\0' || OSS_WaveInInit(&WInDev[i].ossdev))
1207 WInDev[numInDev].state = WINE_WS_CLOSED;
1208 numInDev++;
1212 /* finish with the full duplex bits */
1213 for (i = 0; i < MAX_WAVEDRV; i++)
1214 if (*WOutDev[i].ossdev.dev_name!='\0')
1215 OSS_WaveFullDuplexInit(&WOutDev[i].ossdev);
1217 TRACE("%d wave out devices\n", numOutDev);
1218 for (i = 0; i < numOutDev; i++) {
1219 TRACE("%u: %s, %s, %s\n", i, WOutDev[i].ossdev.dev_name,
1220 WOutDev[i].ossdev.mixer_name, WOutDev[i].ossdev.interface_name);
1223 TRACE("%d wave in devices\n", numInDev);
1224 for (i = 0; i < numInDev; i++) {
1225 TRACE("%u: %s, %s, %s\n", i, WInDev[i].ossdev.dev_name,
1226 WInDev[i].ossdev.mixer_name, WInDev[i].ossdev.interface_name);
1229 return 0;
1232 /******************************************************************
1233 * OSS_WaveExit
1235 * Delete/clear internal structures of OSS information
1237 LRESULT OSS_WaveExit(void)
1239 int i;
1240 TRACE("()\n");
1242 for (i = 0; i < MAX_WAVEDRV; ++i)
1244 HeapFree(GetProcessHeap(), 0, WOutDev[i].ossdev.dev_name);
1245 HeapFree(GetProcessHeap(), 0, WOutDev[i].ossdev.mixer_name);
1246 HeapFree(GetProcessHeap(), 0, WOutDev[i].ossdev.interface_name);
1249 ZeroMemory(WOutDev, sizeof(WOutDev));
1250 ZeroMemory(WInDev, sizeof(WInDev));
1252 numOutDev = 0;
1253 numInDev = 0;
1255 return 0;
1258 /******************************************************************
1259 * OSS_InitRingMessage
1261 * Initialize the ring of messages for passing between driver's caller and playback/record
1262 * thread
1264 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
1266 omr->msg_toget = 0;
1267 omr->msg_tosave = 0;
1268 #ifdef USE_PIPE_SYNC
1269 if (pipe(omr->msg_pipe) < 0) {
1270 omr->msg_pipe[0] = -1;
1271 omr->msg_pipe[1] = -1;
1272 ERR("could not create pipe, error=%s\n", strerror(errno));
1274 #else
1275 omr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1276 #endif
1277 omr->ring_buffer_size = OSS_RING_BUFFER_INCREMENT;
1278 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(OSS_MSG));
1279 InitializeCriticalSection(&omr->msg_crst);
1280 omr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": OSS_MSG_RING.msg_crst");
1281 return 0;
1284 /******************************************************************
1285 * OSS_DestroyRingMessage
1288 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
1290 #ifdef USE_PIPE_SYNC
1291 close(omr->msg_pipe[0]);
1292 close(omr->msg_pipe[1]);
1293 #else
1294 CloseHandle(omr->msg_event);
1295 #endif
1296 HeapFree(GetProcessHeap(),0,omr->messages);
1297 omr->msg_crst.DebugInfo->Spare[0] = 0;
1298 DeleteCriticalSection(&omr->msg_crst);
1299 return 0;
1302 /******************************************************************
1303 * OSS_AddRingMessage
1305 * Inserts a new message into the ring (should be called from DriverProc derived routines)
1307 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
1309 HANDLE hEvent = INVALID_HANDLE_VALUE;
1311 EnterCriticalSection(&omr->msg_crst);
1312 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
1314 int old_ring_buffer_size = omr->ring_buffer_size;
1315 omr->ring_buffer_size += OSS_RING_BUFFER_INCREMENT;
1316 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
1317 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(OSS_MSG));
1318 /* Now we need to rearrange the ring buffer so that the new
1319 buffers just allocated are in between omr->msg_tosave and
1320 omr->msg_toget.
1322 if (omr->msg_tosave < omr->msg_toget)
1324 memmove(&(omr->messages[omr->msg_toget + OSS_RING_BUFFER_INCREMENT]),
1325 &(omr->messages[omr->msg_toget]),
1326 sizeof(OSS_MSG)*(old_ring_buffer_size - omr->msg_toget)
1328 omr->msg_toget += OSS_RING_BUFFER_INCREMENT;
1331 if (wait)
1333 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1334 if (hEvent == INVALID_HANDLE_VALUE)
1336 ERR("can't create event !?\n");
1337 LeaveCriticalSection(&omr->msg_crst);
1338 return 0;
1340 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
1341 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1342 omr->msg_toget,getCmdString(omr->messages[omr->msg_toget].msg),
1343 omr->msg_tosave,getCmdString(omr->messages[omr->msg_tosave].msg));
1345 /* fast messages have to be added at the start of the queue */
1346 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
1347 omr->messages[omr->msg_toget].msg = msg;
1348 omr->messages[omr->msg_toget].param = param;
1349 omr->messages[omr->msg_toget].hEvent = hEvent;
1351 else
1353 omr->messages[omr->msg_tosave].msg = msg;
1354 omr->messages[omr->msg_tosave].param = param;
1355 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
1356 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
1358 LeaveCriticalSection(&omr->msg_crst);
1359 /* signal a new message */
1360 SIGNAL_OMR(omr);
1361 if (wait)
1363 /* wait for playback/record thread to have processed the message */
1364 WaitForSingleObject(hEvent, INFINITE);
1365 CloseHandle(hEvent);
1367 return 1;
1370 /******************************************************************
1371 * OSS_RetrieveRingMessage
1373 * Get a message from the ring. Should be called by the playback/record thread.
1375 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
1376 enum win_wm_message *msg, DWORD_PTR *param, HANDLE *hEvent)
1378 EnterCriticalSection(&omr->msg_crst);
1380 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1382 LeaveCriticalSection(&omr->msg_crst);
1383 return 0;
1386 *msg = omr->messages[omr->msg_toget].msg;
1387 omr->messages[omr->msg_toget].msg = 0;
1388 *param = omr->messages[omr->msg_toget].param;
1389 *hEvent = omr->messages[omr->msg_toget].hEvent;
1390 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
1391 CLEAR_OMR(omr);
1392 LeaveCriticalSection(&omr->msg_crst);
1393 return 1;
1396 /******************************************************************
1397 * OSS_PeekRingMessage
1399 * Peek at a message from the ring but do not remove it.
1400 * Should be called by the playback/record thread.
1402 static int OSS_PeekRingMessage(OSS_MSG_RING* omr,
1403 enum win_wm_message *msg,
1404 DWORD_PTR *param, HANDLE *hEvent)
1406 EnterCriticalSection(&omr->msg_crst);
1408 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1410 LeaveCriticalSection(&omr->msg_crst);
1411 return 0;
1414 *msg = omr->messages[omr->msg_toget].msg;
1415 *param = omr->messages[omr->msg_toget].param;
1416 *hEvent = omr->messages[omr->msg_toget].hEvent;
1417 LeaveCriticalSection(&omr->msg_crst);
1418 return 1;
1421 /*======================================================================*
1422 * Low level WAVE OUT implementation *
1423 *======================================================================*/
1425 /**************************************************************************
1426 * wodNotifyClient [internal]
1428 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1430 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lx dwParam2 = %04lx\n", wMsg,
1431 wMsg == WOM_OPEN ? "WOM_OPEN" : wMsg == WOM_CLOSE ? "WOM_CLOSE" :
1432 wMsg == WOM_DONE ? "WOM_DONE" : "Unknown", dwParam1, dwParam2);
1434 switch (wMsg) {
1435 case WOM_OPEN:
1436 case WOM_CLOSE:
1437 case WOM_DONE:
1438 if (wwo->wFlags != DCB_NULL &&
1439 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
1440 (HDRVR)wwo->waveDesc.hWave, wMsg,
1441 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
1442 WARN("can't notify client !\n");
1443 return MMSYSERR_ERROR;
1445 break;
1446 default:
1447 FIXME("Unknown callback message %u\n", wMsg);
1448 return MMSYSERR_INVALPARAM;
1450 return MMSYSERR_NOERROR;
1453 /**************************************************************************
1454 * wodUpdatePlayedTotal [internal]
1457 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
1459 audio_buf_info dspspace;
1460 DWORD notplayed;
1461 if (!info) info = &dspspace;
1463 if (ioctl(wwo->ossdev.fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
1464 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev.dev_name, strerror(errno));
1465 return FALSE;
1468 /* GETOSPACE is not always accurate when we're down to the last fragment or two;
1469 ** we try to accommodate that here by assuming that the dsp is empty by looking
1470 ** at the clock rather than the result of GETOSPACE */
1471 notplayed = wwo->dwBufferSize - info->bytes;
1472 if (notplayed > 0 && notplayed < (info->fragsize * 2))
1474 if (wwo->dwProjectedFinishTime && GetTickCount() >= wwo->dwProjectedFinishTime)
1476 TRACE("Adjusting for a presumed OSS bug and assuming all data has been played.\n");
1477 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1478 return TRUE;
1480 else
1481 /* Some OSS drivers will clean up nicely if given a POST, so give 'em the chance... */
1482 ioctl(wwo->ossdev.fd, SNDCTL_DSP_POST, 0);
1485 wwo->dwPlayedTotal = wwo->dwWrittenTotal - notplayed;
1486 return TRUE;
1489 /**************************************************************************
1490 * wodPlayer_BeginWaveHdr [internal]
1492 * Makes the specified lpWaveHdr the currently playing wave header.
1493 * If the specified wave header is a begin loop and we're not already in
1494 * a loop, setup the loop.
1496 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1498 wwo->lpPlayPtr = lpWaveHdr;
1500 if (!lpWaveHdr) return;
1502 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1503 if (wwo->lpLoopPtr) {
1504 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1505 } else {
1506 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1507 wwo->lpLoopPtr = lpWaveHdr;
1508 /* Windows does not touch WAVEHDR.dwLoops,
1509 * so we need to make an internal copy */
1510 wwo->dwLoops = lpWaveHdr->dwLoops;
1513 wwo->dwPartialOffset = 0;
1516 /**************************************************************************
1517 * wodPlayer_PlayPtrNext [internal]
1519 * Advance the play pointer to the next waveheader, looping if required.
1521 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
1523 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1525 wwo->dwPartialOffset = 0;
1526 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1527 /* We're at the end of a loop, loop if required */
1528 if (--wwo->dwLoops > 0) {
1529 wwo->lpPlayPtr = wwo->lpLoopPtr;
1530 } else {
1531 /* Handle overlapping loops correctly */
1532 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1533 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1534 /* shall we consider the END flag for the closing loop or for
1535 * the opening one or for both ???
1536 * code assumes for closing loop only
1538 } else {
1539 lpWaveHdr = lpWaveHdr->lpNext;
1541 wwo->lpLoopPtr = NULL;
1542 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1544 } else {
1545 /* We're not in a loop. Advance to the next wave header */
1546 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1549 return lpWaveHdr;
1552 /**************************************************************************
1553 * wodPlayer_TicksTillEmpty [internal]
1554 * Returns the number of ticks until we think the DSP should be empty
1556 static DWORD wodPlayer_TicksTillEmpty(const WINE_WAVEOUT *wwo)
1558 return ((wwo->dwWrittenTotal - wwo->dwPlayedTotal) * 1000)
1559 / wwo->waveFormat.Format.nAvgBytesPerSec;
1562 /**************************************************************************
1563 * wodPlayer_DSPWait [internal]
1564 * Returns the number of milliseconds to wait for the DSP buffer to write
1565 * one fragment.
1567 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1569 /* time for one fragment to be played */
1570 return wwo->dwFragmentSize * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1573 /**************************************************************************
1574 * wodPlayer_NotifyWait [internal]
1575 * Returns the number of milliseconds to wait before attempting to notify
1576 * completion of the specified wavehdr.
1577 * This is based on the number of bytes remaining to be written in the
1578 * wave.
1580 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1582 DWORD dwMillis;
1584 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1585 dwMillis = 1;
1586 } else {
1587 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1588 if (!dwMillis) dwMillis = 1;
1591 return dwMillis;
1595 /**************************************************************************
1596 * wodPlayer_WriteMaxFrags [internal]
1597 * Writes the maximum number of bytes possible to the DSP and returns
1598 * TRUE iff the current playPtr has been fully played
1600 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
1602 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1603 DWORD toWrite = min(dwLength, *bytes);
1604 int written;
1605 BOOL ret = FALSE;
1607 TRACE("Writing wavehdr %p.%u[%u]/%u\n",
1608 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
1610 if (toWrite > 0)
1612 written = write(wwo->ossdev.fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
1613 if (written <= 0) {
1614 TRACE("write(%s, %p, %d) failed (%s) returned %d\n", wwo->ossdev.dev_name,
1615 wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite, strerror(errno), written);
1616 return FALSE;
1619 else
1620 written = 0;
1622 if (written >= dwLength) {
1623 /* If we wrote all current wavehdr, skip to the next one */
1624 wodPlayer_PlayPtrNext(wwo);
1625 ret = TRUE;
1626 } else {
1627 /* Remove the amount written */
1628 wwo->dwPartialOffset += written;
1630 *bytes -= written;
1631 wwo->dwWrittenTotal += written;
1632 TRACE("dwWrittenTotal=%u\n", wwo->dwWrittenTotal);
1633 return ret;
1637 /**************************************************************************
1638 * wodPlayer_NotifyCompletions [internal]
1640 * Notifies and remove from queue all wavehdrs which have been played to
1641 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1642 * we notify all wavehdrs and remove them all from the queue even if they
1643 * are unplayed or part of a loop.
1645 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1647 LPWAVEHDR lpWaveHdr;
1649 /* Start from lpQueuePtr and keep notifying until:
1650 * - we hit an unwritten wavehdr
1651 * - we hit the beginning of a running loop
1652 * - we hit a wavehdr which hasn't finished playing
1654 #if 0
1655 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1656 (force ||
1657 (lpWaveHdr != wwo->lpPlayPtr &&
1658 lpWaveHdr != wwo->lpLoopPtr &&
1659 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1661 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1663 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1664 lpWaveHdr->dwFlags |= WHDR_DONE;
1666 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1668 #else
1669 for (;;)
1671 lpWaveHdr = wwo->lpQueuePtr;
1672 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
1673 if (!force)
1675 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
1676 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
1677 if (lpWaveHdr->reserved > wwo->dwPlayedTotal) {TRACE("still playing %p (%u/%u)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
1679 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1681 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1682 lpWaveHdr->dwFlags |= WHDR_DONE;
1684 wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
1686 #endif
1687 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1688 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1691 /**************************************************************************
1692 * wodPlayer_Reset [internal]
1694 * wodPlayer helper. Resets current output stream.
1696 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
1698 wodUpdatePlayedTotal(wwo, NULL);
1699 /* updates current notify list */
1700 wodPlayer_NotifyCompletions(wwo, FALSE);
1702 /* flush all possible output */
1703 if (OSS_ResetDevice(&wwo->ossdev) != MMSYSERR_NOERROR)
1705 wwo->hThread = 0;
1706 wwo->state = WINE_WS_STOPPED;
1707 ExitThread(-1);
1710 if (reset) {
1711 enum win_wm_message msg;
1712 DWORD_PTR param;
1713 HANDLE ev;
1715 /* remove any buffer */
1716 wodPlayer_NotifyCompletions(wwo, TRUE);
1718 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1719 wwo->state = WINE_WS_STOPPED;
1720 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1721 /* Clear partial wavehdr */
1722 wwo->dwPartialOffset = 0;
1724 /* remove any existing message in the ring */
1725 EnterCriticalSection(&wwo->msgRing.msg_crst);
1726 /* return all pending headers in queue */
1727 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1729 if (msg != WINE_WM_HEADER)
1731 FIXME("shouldn't have headers left\n");
1732 SetEvent(ev);
1733 continue;
1735 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1736 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1738 wodNotifyClient(wwo, WOM_DONE, param, 0);
1740 RESET_OMR(&wwo->msgRing);
1741 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1742 } else {
1743 if (wwo->lpLoopPtr) {
1744 /* complicated case, not handled yet (could imply modifying the loop counter */
1745 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1746 wwo->lpPlayPtr = wwo->lpLoopPtr;
1747 wwo->dwPartialOffset = 0;
1748 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1749 } else {
1750 LPWAVEHDR ptr;
1751 DWORD sz = wwo->dwPartialOffset;
1753 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1754 /* compute the max size playable from lpQueuePtr */
1755 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1756 sz += ptr->dwBufferLength;
1758 /* because the reset lpPlayPtr will be lpQueuePtr */
1759 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1760 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1761 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1762 wwo->lpPlayPtr = wwo->lpQueuePtr;
1764 wwo->state = WINE_WS_PAUSED;
1768 /**************************************************************************
1769 * wodPlayer_ProcessMessages [internal]
1771 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1773 LPWAVEHDR lpWaveHdr;
1774 enum win_wm_message msg;
1775 DWORD_PTR param;
1776 HANDLE ev;
1778 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1779 TRACE("Received %s %lx\n", getCmdString(msg), param);
1780 switch (msg) {
1781 case WINE_WM_PAUSING:
1782 wodPlayer_Reset(wwo, FALSE);
1783 SetEvent(ev);
1784 break;
1785 case WINE_WM_RESTARTING:
1786 if (wwo->state == WINE_WS_PAUSED)
1788 wwo->state = WINE_WS_PLAYING;
1790 SetEvent(ev);
1791 break;
1792 case WINE_WM_HEADER:
1793 lpWaveHdr = (LPWAVEHDR)param;
1795 /* insert buffer at the end of queue */
1797 LPWAVEHDR* wh;
1798 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1799 *wh = lpWaveHdr;
1801 if (!wwo->lpPlayPtr)
1802 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1803 if (wwo->state == WINE_WS_STOPPED)
1804 wwo->state = WINE_WS_PLAYING;
1805 break;
1806 case WINE_WM_RESETTING:
1807 wodPlayer_Reset(wwo, TRUE);
1808 SetEvent(ev);
1809 break;
1810 case WINE_WM_UPDATE:
1811 wodUpdatePlayedTotal(wwo, NULL);
1812 SetEvent(ev);
1813 break;
1814 case WINE_WM_BREAKLOOP:
1815 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1816 /* ensure exit at end of current loop */
1817 wwo->dwLoops = 1;
1819 SetEvent(ev);
1820 break;
1821 case WINE_WM_CLOSING:
1822 /* sanity check: this should not happen since the device must have been reset before */
1823 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1824 wwo->hThread = 0;
1825 wwo->state = WINE_WS_CLOSED;
1826 SetEvent(ev);
1827 ExitThread(0);
1828 /* shouldn't go here */
1829 default:
1830 FIXME("unknown message %d\n", msg);
1831 break;
1836 /**************************************************************************
1837 * wodPlayer_FeedDSP [internal]
1838 * Feed as much sound data as we can into the DSP and return the number of
1839 * milliseconds before it will be necessary to feed the DSP again.
1841 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1843 audio_buf_info dspspace;
1844 DWORD availInQ;
1846 if (!wodUpdatePlayedTotal(wwo, &dspspace)) return INFINITE;
1847 availInQ = dspspace.bytes;
1848 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1849 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1851 /* no more room... no need to try to feed */
1852 if (dspspace.fragments != 0) {
1853 /* Feed from partial wavehdr */
1854 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1855 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1858 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1859 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1860 do {
1861 TRACE("Setting time to elapse for %p to %u\n",
1862 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1863 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1864 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1865 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1868 if (wwo->bNeedPost) {
1869 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1870 * if it didn't get one, we give it the other */
1871 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1872 ioctl(wwo->ossdev.fd, SNDCTL_DSP_POST, 0);
1873 wwo->bNeedPost = FALSE;
1877 return wodPlayer_DSPWait(wwo);
1881 /**************************************************************************
1882 * wodPlayer [internal]
1884 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1886 WORD uDevID = (DWORD_PTR)pmt;
1887 WINE_WAVEOUT* wwo = &WOutDev[uDevID];
1888 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1889 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1890 DWORD dwSleepTime;
1892 wwo->state = WINE_WS_STOPPED;
1893 SetEvent(wwo->hStartUpEvent);
1895 for (;;) {
1896 /** Wait for the shortest time before an action is required. If there
1897 * are no pending actions, wait forever for a command.
1899 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1900 TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1901 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1902 wodPlayer_ProcessMessages(wwo);
1903 if (wwo->state == WINE_WS_PLAYING) {
1904 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1905 if (dwNextFeedTime != INFINITE)
1906 wwo->dwProjectedFinishTime = GetTickCount() + wodPlayer_TicksTillEmpty(wwo);
1907 else
1908 wwo->dwProjectedFinishTime = 0;
1910 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1911 if (dwNextFeedTime == INFINITE) {
1912 /* FeedDSP ran out of data, but before flushing, */
1913 /* check that a notification didn't give us more */
1914 wodPlayer_ProcessMessages(wwo);
1915 if (!wwo->lpPlayPtr) {
1916 TRACE("flushing\n");
1917 ioctl(wwo->ossdev.fd, SNDCTL_DSP_SYNC, 0);
1918 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1919 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1920 } else {
1921 TRACE("recovering\n");
1922 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1925 } else {
1926 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1930 return 0;
1933 /**************************************************************************
1934 * wodGetDevCaps [internal]
1936 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1938 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1940 if (lpCaps == NULL) {
1941 WARN("not enabled\n");
1942 return MMSYSERR_NOTENABLED;
1945 if (wDevID >= numOutDev) {
1946 WARN("numOutDev reached !\n");
1947 return MMSYSERR_BADDEVICEID;
1950 if (WOutDev[wDevID].ossdev.open_access == O_RDWR)
1951 memcpy(lpCaps, &WOutDev[wDevID].ossdev.duplex_out_caps, min(dwSize, sizeof(*lpCaps)));
1952 else
1953 memcpy(lpCaps, &WOutDev[wDevID].ossdev.out_caps, min(dwSize, sizeof(*lpCaps)));
1955 return MMSYSERR_NOERROR;
1958 /**************************************************************************
1959 * wodOpen [internal]
1961 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1963 int audio_fragment;
1964 WINE_WAVEOUT* wwo;
1965 audio_buf_info info;
1966 DWORD ret;
1968 TRACE("(%u, %p[cb=%08x], %08X);\n", wDevID, lpDesc, lpDesc->dwCallback, dwFlags);
1969 if (lpDesc == NULL) {
1970 WARN("Invalid Parameter !\n");
1971 return MMSYSERR_INVALPARAM;
1973 if (wDevID >= numOutDev) {
1974 TRACE("MAX_WAVOUTDRV reached !\n");
1975 return MMSYSERR_BADDEVICEID;
1978 /* only PCM format is supported so far... */
1979 if (!supportedFormat(lpDesc->lpFormat)) {
1980 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1981 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1982 lpDesc->lpFormat->nSamplesPerSec);
1983 return WAVERR_BADFORMAT;
1986 if (dwFlags & WAVE_FORMAT_QUERY) {
1987 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1988 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1989 lpDesc->lpFormat->nSamplesPerSec);
1990 return MMSYSERR_NOERROR;
1993 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
1994 lpDesc->lpFormat->nSamplesPerSec,
1995 lpDesc->lpFormat->wBitsPerSample,
1996 lpDesc->lpFormat->nChannels,
1997 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
1998 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
1999 "UNSUPPORTED");
2001 wwo = &WOutDev[wDevID];
2003 if ((dwFlags & WAVE_DIRECTSOUND) &&
2004 !(wwo->ossdev.duplex_out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
2005 /* not supported, ignore it */
2006 dwFlags &= ~WAVE_DIRECTSOUND;
2008 if (dwFlags & WAVE_DIRECTSOUND) {
2009 if (wwo->ossdev.duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2010 /* we have realtime DirectSound, fragments just waste our time,
2011 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2012 audio_fragment = 0x0020000B;
2013 else
2014 /* to approximate realtime, we must use small fragments,
2015 * let's try to fragment the above 64KB (256 * 2^8) */
2016 audio_fragment = 0x01000008;
2017 } else {
2018 /* A wave device must have a worst case latency of 10 ms so calculate
2019 * the largest fragment size less than 10 ms long.
2021 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2022 int shift = 0;
2023 while ((1 << shift) <= fsize)
2024 shift++;
2025 shift--;
2026 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2029 TRACE("requesting %d %d byte fragments (%d ms/fragment)\n",
2030 audio_fragment >> 16, 1 << (audio_fragment & 0xffff),
2031 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2033 if (wwo->state != WINE_WS_CLOSED) {
2034 WARN("already allocated\n");
2035 return MMSYSERR_ALLOCATED;
2038 /* we want to be able to mmap() the device, which means it must be opened readable,
2039 * otherwise mmap() will fail (at least under Linux) */
2040 ret = OSS_OpenDevice(&wwo->ossdev,
2041 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
2042 &audio_fragment,
2043 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
2044 lpDesc->lpFormat->nSamplesPerSec,
2045 lpDesc->lpFormat->nChannels,
2046 (lpDesc->lpFormat->wBitsPerSample == 16)
2047 ? AFMT_S16_LE : AFMT_U8);
2048 if ((ret==MMSYSERR_NOERROR) && (dwFlags & WAVE_DIRECTSOUND)) {
2049 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev.sample_rate;
2050 lpDesc->lpFormat->nChannels=wwo->ossdev.channels;
2051 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev.format == AFMT_U8 ? 8 : 16);
2052 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
2053 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
2054 TRACE("OSS_OpenDevice returned this format: %dx%dx%d\n",
2055 lpDesc->lpFormat->nSamplesPerSec,
2056 lpDesc->lpFormat->wBitsPerSample,
2057 lpDesc->lpFormat->nChannels);
2059 if (ret != 0) return ret;
2060 wwo->state = WINE_WS_STOPPED;
2062 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2064 wwo->waveDesc = *lpDesc;
2065 copy_format(lpDesc->lpFormat, &wwo->waveFormat);
2067 if (wwo->waveFormat.Format.wBitsPerSample == 0) {
2068 WARN("Resetting zeroed wBitsPerSample\n");
2069 wwo->waveFormat.Format.wBitsPerSample = 8 *
2070 (wwo->waveFormat.Format.nAvgBytesPerSec /
2071 wwo->waveFormat.Format.nSamplesPerSec) /
2072 wwo->waveFormat.Format.nChannels;
2074 /* Read output space info for future reference */
2075 if (ioctl(wwo->ossdev.fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2076 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev.dev_name, strerror(errno));
2077 OSS_CloseDevice(&wwo->ossdev);
2078 wwo->state = WINE_WS_CLOSED;
2079 return MMSYSERR_NOTENABLED;
2082 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2083 info.fragsize, (info.fragsize * 1000) / (wwo->ossdev.sample_rate *
2084 wwo->ossdev.channels * (wwo->ossdev.format == AFMT_U8 ? 1 : 2)));
2086 /* Check that fragsize is correct per our settings above */
2087 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
2088 /* we've tried to set 1K fragments or less, but it didn't work */
2089 WARN("fragment size set failed, size is now %d\n", info.fragsize);
2092 /* Remember fragsize and total buffer size for future use */
2093 wwo->dwFragmentSize = info.fragsize;
2094 wwo->dwBufferSize = info.fragstotal * info.fragsize;
2095 wwo->dwPlayedTotal = 0;
2096 wwo->dwWrittenTotal = 0;
2097 wwo->bNeedPost = TRUE;
2099 TRACE("fd=%d fragstotal=%d fragsize=%d BufferSize=%d\n",
2100 wwo->ossdev.fd, info.fragstotal, info.fragsize, wwo->dwBufferSize);
2101 if (wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign) {
2102 ERR("Fragment doesn't contain an integral number of data blocks fragsize=%d BlockAlign=%d\n",wwo->dwFragmentSize,wwo->waveFormat.Format.nBlockAlign);
2103 /* Some SoundBlaster 16 cards return an incorrect (odd) fragment
2104 * size for 16 bit sound. This will cause a system crash when we try
2105 * to write just the specified odd number of bytes. So if we
2106 * detect something is wrong we'd better fix it.
2108 wwo->dwFragmentSize-=wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign;
2111 OSS_InitRingMessage(&wwo->msgRing);
2113 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2114 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwo->dwThreadID));
2115 if (wwo->hThread)
2116 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2117 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
2118 CloseHandle(wwo->hStartUpEvent);
2119 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
2121 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2122 wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
2123 wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
2124 wwo->waveFormat.Format.nBlockAlign);
2126 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
2129 /**************************************************************************
2130 * wodClose [internal]
2132 static DWORD wodClose(WORD wDevID)
2134 DWORD ret = MMSYSERR_NOERROR;
2135 WINE_WAVEOUT* wwo;
2137 TRACE("(%u);\n", wDevID);
2139 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2140 WARN("bad device ID !\n");
2141 return MMSYSERR_BADDEVICEID;
2144 wwo = &WOutDev[wDevID];
2145 if (wwo->lpQueuePtr) {
2146 WARN("buffers still playing !\n");
2147 ret = WAVERR_STILLPLAYING;
2148 } else {
2149 if (wwo->hThread != INVALID_HANDLE_VALUE) {
2150 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
2153 OSS_DestroyRingMessage(&wwo->msgRing);
2155 OSS_CloseDevice(&wwo->ossdev);
2156 wwo->state = WINE_WS_CLOSED;
2157 wwo->dwFragmentSize = 0;
2158 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
2160 return ret;
2163 /**************************************************************************
2164 * wodWrite [internal]
2167 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2169 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
2171 /* first, do the sanity checks... */
2172 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2173 WARN("bad dev ID !\n");
2174 return MMSYSERR_BADDEVICEID;
2177 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
2178 return WAVERR_UNPREPARED;
2180 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2181 return WAVERR_STILLPLAYING;
2183 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2184 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2185 lpWaveHdr->lpNext = 0;
2187 if ((lpWaveHdr->dwBufferLength & (WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1)) != 0)
2189 WARN("WaveHdr length isn't a multiple of the PCM block size: %d %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].waveFormat.Format.nBlockAlign);
2190 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1);
2193 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
2195 return MMSYSERR_NOERROR;
2198 /**************************************************************************
2199 * wodPause [internal]
2201 static DWORD wodPause(WORD wDevID)
2203 TRACE("(%u);!\n", wDevID);
2205 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2206 WARN("bad device ID !\n");
2207 return MMSYSERR_BADDEVICEID;
2210 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
2212 return MMSYSERR_NOERROR;
2215 /**************************************************************************
2216 * wodRestart [internal]
2218 static DWORD wodRestart(WORD wDevID)
2220 TRACE("(%u);\n", wDevID);
2222 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2223 WARN("bad device ID !\n");
2224 return MMSYSERR_BADDEVICEID;
2227 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2229 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2230 /* FIXME: Myst crashes with this ... hmm -MM
2231 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2234 return MMSYSERR_NOERROR;
2237 /**************************************************************************
2238 * wodReset [internal]
2240 static DWORD wodReset(WORD wDevID)
2242 TRACE("(%u);\n", wDevID);
2244 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2245 WARN("bad device ID !\n");
2246 return MMSYSERR_BADDEVICEID;
2249 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2251 return MMSYSERR_NOERROR;
2254 /**************************************************************************
2255 * wodGetPosition [internal]
2257 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2259 WINE_WAVEOUT* wwo;
2261 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
2263 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2264 WARN("bad device ID !\n");
2265 return MMSYSERR_BADDEVICEID;
2268 if (lpTime == NULL) {
2269 WARN("invalid parameter: lpTime == NULL\n");
2270 return MMSYSERR_INVALPARAM;
2273 wwo = &WOutDev[wDevID];
2274 #ifdef EXACT_WODPOSITION
2275 if (wwo->ossdev.open_access == O_RDWR) {
2276 if (wwo->ossdev.duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2277 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2278 } else {
2279 if (wwo->ossdev.out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2280 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2282 #endif
2284 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
2287 /**************************************************************************
2288 * wodBreakLoop [internal]
2290 static DWORD wodBreakLoop(WORD wDevID)
2292 TRACE("(%u);\n", wDevID);
2294 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2295 WARN("bad device ID !\n");
2296 return MMSYSERR_BADDEVICEID;
2298 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
2299 return MMSYSERR_NOERROR;
2302 /**************************************************************************
2303 * wodGetVolume [internal]
2305 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
2307 int mixer;
2308 int volume;
2309 DWORD left, right;
2310 DWORD last_left, last_right;
2312 TRACE("(%u, %p);\n", wDevID, lpdwVol);
2314 if (lpdwVol == NULL) {
2315 WARN("not enabled\n");
2316 return MMSYSERR_NOTENABLED;
2318 if (wDevID >= numOutDev) {
2319 WARN("invalid parameter\n");
2320 return MMSYSERR_INVALPARAM;
2322 if (WOutDev[wDevID].ossdev.open_access == O_RDWR) {
2323 if (!(WOutDev[wDevID].ossdev.duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2324 TRACE("Volume not supported\n");
2325 return MMSYSERR_NOTSUPPORTED;
2327 } else {
2328 if (!(WOutDev[wDevID].ossdev.out_caps.dwSupport & WAVECAPS_VOLUME)) {
2329 TRACE("Volume not supported\n");
2330 return MMSYSERR_NOTSUPPORTED;
2334 if ((mixer = open(WOutDev[wDevID].ossdev.mixer_name, O_RDONLY|O_NDELAY)) < 0) {
2335 WARN("mixer device not available !\n");
2336 return MMSYSERR_NOTENABLED;
2338 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
2339 close(mixer);
2340 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)\n",
2341 WOutDev[wDevID].ossdev.mixer_name, strerror(errno));
2342 return MMSYSERR_NOTENABLED;
2344 close(mixer);
2346 left = LOBYTE(volume);
2347 right = HIBYTE(volume);
2348 TRACE("left=%d right=%d !\n", left, right);
2349 last_left = (LOWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2350 last_right = (HIWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2351 TRACE("last_left=%d last_right=%d !\n", last_left, last_right);
2352 if (last_left == left && last_right == right)
2353 *lpdwVol = WOutDev[wDevID].volume;
2354 else
2355 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
2356 return MMSYSERR_NOERROR;
2359 /**************************************************************************
2360 * wodSetVolume [internal]
2362 DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
2364 int mixer;
2365 int volume;
2366 DWORD left, right;
2368 TRACE("(%u, %08X);\n", wDevID, dwParam);
2370 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
2371 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
2372 volume = left + (right << 8);
2374 if (wDevID >= numOutDev) {
2375 WARN("invalid parameter: wDevID > %d\n", numOutDev);
2376 return MMSYSERR_INVALPARAM;
2378 if (WOutDev[wDevID].ossdev.open_access == O_RDWR) {
2379 if (!(WOutDev[wDevID].ossdev.duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2380 TRACE("Volume not supported\n");
2381 return MMSYSERR_NOTSUPPORTED;
2383 } else {
2384 if (!(WOutDev[wDevID].ossdev.out_caps.dwSupport & WAVECAPS_VOLUME)) {
2385 TRACE("Volume not supported\n");
2386 return MMSYSERR_NOTSUPPORTED;
2389 if ((mixer = open(WOutDev[wDevID].ossdev.mixer_name, O_WRONLY|O_NDELAY)) < 0) {
2390 WARN("open(%s) failed (%s)\n", WOutDev[wDevID].ossdev.mixer_name, strerror(errno));
2391 return MMSYSERR_NOTENABLED;
2393 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
2394 close(mixer);
2395 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n",
2396 WOutDev[wDevID].ossdev.mixer_name, strerror(errno));
2397 return MMSYSERR_NOTENABLED;
2399 TRACE("volume=%04x\n", (unsigned)volume);
2400 close(mixer);
2402 /* save requested volume */
2403 WOutDev[wDevID].volume = dwParam;
2405 return MMSYSERR_NOERROR;
2408 /**************************************************************************
2409 * wodMessage (WINEOSS.7)
2411 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
2412 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2414 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
2415 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
2417 switch (wMsg) {
2418 case DRVM_INIT:
2419 case DRVM_EXIT:
2420 case DRVM_ENABLE:
2421 case DRVM_DISABLE:
2422 /* FIXME: Pretend this is supported */
2423 return 0;
2424 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2425 case WODM_CLOSE: return wodClose (wDevID);
2426 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2427 case WODM_PAUSE: return wodPause (wDevID);
2428 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
2429 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
2430 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2431 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2432 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
2433 case WODM_GETNUMDEVS: return numOutDev;
2434 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
2435 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
2436 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2437 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2438 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
2439 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
2440 case WODM_RESTART: return wodRestart (wDevID);
2441 case WODM_RESET: return wodReset (wDevID);
2443 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2444 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2445 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
2446 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2447 default:
2448 FIXME("unknown message %d!\n", wMsg);
2450 return MMSYSERR_NOTSUPPORTED;
2453 /*======================================================================*
2454 * Low level WAVE IN implementation *
2455 *======================================================================*/
2457 /**************************************************************************
2458 * widNotifyClient [internal]
2460 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2462 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lx dwParam2 = %04lx\n", wMsg,
2463 wMsg == WIM_OPEN ? "WIM_OPEN" : wMsg == WIM_CLOSE ? "WIM_CLOSE" :
2464 wMsg == WIM_DATA ? "WIM_DATA" : "Unknown", dwParam1, dwParam2);
2466 switch (wMsg) {
2467 case WIM_OPEN:
2468 case WIM_CLOSE:
2469 case WIM_DATA:
2470 if (wwi->wFlags != DCB_NULL &&
2471 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2472 (HDRVR)wwi->waveDesc.hWave, wMsg,
2473 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2474 WARN("can't notify client !\n");
2475 return MMSYSERR_ERROR;
2477 break;
2478 default:
2479 FIXME("Unknown callback message %u\n", wMsg);
2480 return MMSYSERR_INVALPARAM;
2482 return MMSYSERR_NOERROR;
2485 /**************************************************************************
2486 * widGetDevCaps [internal]
2488 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
2490 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
2492 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2494 if (wDevID >= numInDev) {
2495 TRACE("numOutDev reached !\n");
2496 return MMSYSERR_BADDEVICEID;
2499 memcpy(lpCaps, &WInDev[wDevID].ossdev.in_caps, min(dwSize, sizeof(*lpCaps)));
2500 return MMSYSERR_NOERROR;
2503 /**************************************************************************
2504 * widRecorder_ReadHeaders [internal]
2506 static void widRecorder_ReadHeaders(WINE_WAVEIN * wwi)
2508 enum win_wm_message tmp_msg;
2509 DWORD_PTR tmp_param;
2510 HANDLE tmp_ev;
2511 WAVEHDR* lpWaveHdr;
2513 while (OSS_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
2514 if (tmp_msg == WINE_WM_HEADER) {
2515 LPWAVEHDR* wh;
2516 lpWaveHdr = (LPWAVEHDR)tmp_param;
2517 lpWaveHdr->lpNext = 0;
2519 if (wwi->lpQueuePtr == 0)
2520 wwi->lpQueuePtr = lpWaveHdr;
2521 else {
2522 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2523 *wh = lpWaveHdr;
2525 } else {
2526 ERR("should only have headers left\n");
2531 /**************************************************************************
2532 * widRecorder [internal]
2534 static DWORD CALLBACK widRecorder(LPVOID pmt)
2536 WORD uDevID = (DWORD_PTR)pmt;
2537 WINE_WAVEIN* wwi = &WInDev[uDevID];
2538 WAVEHDR* lpWaveHdr;
2539 DWORD dwSleepTime;
2540 DWORD bytesRead;
2541 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2542 char *pOffset = buffer;
2543 audio_buf_info info;
2544 int xs;
2545 enum win_wm_message msg;
2546 DWORD_PTR param;
2547 HANDLE ev;
2548 int enable;
2550 wwi->state = WINE_WS_STOPPED;
2551 wwi->dwTotalRecorded = 0;
2552 wwi->dwTotalRead = 0;
2553 wwi->lpQueuePtr = NULL;
2555 SetEvent(wwi->hStartUpEvent);
2557 /* disable input so capture will begin when triggered */
2558 wwi->ossdev.bInputEnabled = FALSE;
2559 enable = getEnables(&wwi->ossdev);
2560 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2561 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2563 /* the soundblaster live needs a micro wake to get its recording started
2564 * (or GETISPACE will have 0 frags all the time)
2566 read(wwi->ossdev.fd, &xs, 4);
2568 /* make sleep time to be # of ms to output a fragment */
2569 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
2570 TRACE("sleeptime=%d ms\n", dwSleepTime);
2572 for (;;) {
2573 /* wait for dwSleepTime or an event in thread's queue */
2574 /* FIXME: could improve wait time depending on queue state,
2575 * ie, number of queued fragments
2578 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2580 lpWaveHdr = wwi->lpQueuePtr;
2582 ioctl(wwi->ossdev.fd, SNDCTL_DSP_GETISPACE, &info);
2583 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2585 /* read all the fragments accumulated so far */
2586 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2588 info.fragments --;
2590 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2592 /* directly read fragment in wavehdr */
2593 bytesRead = read(wwi->ossdev.fd,
2594 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2595 wwi->dwFragmentSize);
2597 TRACE("bytesRead=%d (direct)\n", bytesRead);
2598 if (bytesRead != (DWORD) -1)
2600 /* update number of bytes recorded in current buffer and by this device */
2601 lpWaveHdr->dwBytesRecorded += bytesRead;
2602 wwi->dwTotalRead += bytesRead;
2603 wwi->dwTotalRecorded = wwi->dwTotalRead;
2605 /* buffer is full. notify client */
2606 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2608 /* must copy the value of next waveHdr, because we have no idea of what
2609 * will be done with the content of lpWaveHdr in callback
2611 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2613 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2614 lpWaveHdr->dwFlags |= WHDR_DONE;
2616 wwi->lpQueuePtr = lpNext;
2617 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2618 lpWaveHdr = lpNext;
2620 } else {
2621 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev.dev_name,
2622 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2623 wwi->dwFragmentSize, strerror(errno));
2626 else
2628 /* read the fragment in a local buffer */
2629 bytesRead = read(wwi->ossdev.fd, buffer, wwi->dwFragmentSize);
2630 pOffset = buffer;
2632 TRACE("bytesRead=%d (local)\n", bytesRead);
2634 if (bytesRead == (DWORD) -1) {
2635 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev.dev_name,
2636 buffer, wwi->dwFragmentSize, strerror(errno));
2637 continue;
2640 /* copy data in client buffers */
2641 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2643 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2645 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2646 pOffset,
2647 dwToCopy);
2649 /* update number of bytes recorded in current buffer and by this device */
2650 lpWaveHdr->dwBytesRecorded += dwToCopy;
2651 wwi->dwTotalRead += dwToCopy;
2652 wwi->dwTotalRecorded = wwi->dwTotalRead;
2653 bytesRead -= dwToCopy;
2654 pOffset += dwToCopy;
2656 /* client buffer is full. notify client */
2657 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2659 /* must copy the value of next waveHdr, because we have no idea of what
2660 * will be done with the content of lpWaveHdr in callback
2662 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2663 TRACE("lpNext=%p\n", lpNext);
2665 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2666 lpWaveHdr->dwFlags |= WHDR_DONE;
2668 wwi->lpQueuePtr = lpNext;
2669 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2671 lpWaveHdr = lpNext;
2672 if (!lpNext && bytesRead) {
2673 /* before we give up, check for more header messages */
2674 while (OSS_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
2676 if (msg == WINE_WM_HEADER) {
2677 LPWAVEHDR hdr;
2678 OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
2679 hdr = ((LPWAVEHDR)param);
2680 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg), hdr, ev);
2681 hdr->lpNext = 0;
2682 if (lpWaveHdr == 0) {
2683 /* new head of queue */
2684 wwi->lpQueuePtr = lpWaveHdr = hdr;
2685 } else {
2686 /* insert buffer at the end of queue */
2687 LPWAVEHDR* wh;
2688 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2689 *wh = hdr;
2691 } else
2692 break;
2695 if (lpWaveHdr == 0) {
2696 /* no more buffer to copy data to, but we did read more.
2697 * what hasn't been copied will be dropped
2699 WARN("buffer under run! %u bytes dropped.\n", bytesRead);
2700 wwi->lpQueuePtr = NULL;
2701 break;
2710 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2712 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2714 TRACE("msg=%s param=0x%lx\n", getCmdString(msg), param);
2715 switch (msg) {
2716 case WINE_WM_PAUSING:
2717 wwi->state = WINE_WS_PAUSED;
2718 /*FIXME("Device should stop recording\n");*/
2719 SetEvent(ev);
2720 break;
2721 case WINE_WM_STARTING:
2722 wwi->state = WINE_WS_PLAYING;
2724 if (wwi->ossdev.bTriggerSupport)
2726 /* start the recording */
2727 wwi->ossdev.bInputEnabled = TRUE;
2728 enable = getEnables(&wwi->ossdev);
2729 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2730 wwi->ossdev.bInputEnabled = FALSE;
2731 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2734 else
2736 unsigned char data[4];
2737 /* read 4 bytes to start the recording */
2738 read(wwi->ossdev.fd, data, 4);
2741 SetEvent(ev);
2742 break;
2743 case WINE_WM_HEADER:
2744 lpWaveHdr = (LPWAVEHDR)param;
2745 lpWaveHdr->lpNext = 0;
2747 /* insert buffer at the end of queue */
2749 LPWAVEHDR* wh;
2750 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2751 *wh = lpWaveHdr;
2753 break;
2754 case WINE_WM_STOPPING:
2755 if (wwi->state != WINE_WS_STOPPED)
2757 if (wwi->ossdev.bTriggerSupport)
2759 /* stop the recording */
2760 wwi->ossdev.bInputEnabled = FALSE;
2761 enable = getEnables(&wwi->ossdev);
2762 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2763 wwi->ossdev.bInputEnabled = FALSE;
2764 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2768 /* read any headers in queue */
2769 widRecorder_ReadHeaders(wwi);
2771 /* return current buffer to app */
2772 lpWaveHdr = wwi->lpQueuePtr;
2773 if (lpWaveHdr)
2775 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2776 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2777 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2778 lpWaveHdr->dwFlags |= WHDR_DONE;
2779 wwi->lpQueuePtr = lpNext;
2780 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2783 wwi->state = WINE_WS_STOPPED;
2784 SetEvent(ev);
2785 break;
2786 case WINE_WM_RESETTING:
2787 if (wwi->state != WINE_WS_STOPPED)
2789 if (wwi->ossdev.bTriggerSupport)
2791 /* stop the recording */
2792 wwi->ossdev.bInputEnabled = FALSE;
2793 enable = getEnables(&wwi->ossdev);
2794 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2795 wwi->ossdev.bInputEnabled = FALSE;
2796 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2800 wwi->state = WINE_WS_STOPPED;
2801 wwi->dwTotalRecorded = 0;
2802 wwi->dwTotalRead = 0;
2804 /* read any headers in queue */
2805 widRecorder_ReadHeaders(wwi);
2807 /* return all buffers to the app */
2808 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2809 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2810 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2811 lpWaveHdr->dwFlags |= WHDR_DONE;
2812 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2813 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2816 wwi->lpQueuePtr = NULL;
2817 SetEvent(ev);
2818 break;
2819 case WINE_WM_UPDATE:
2820 if (wwi->state == WINE_WS_PLAYING) {
2821 audio_buf_info tmp_info;
2822 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_GETISPACE, &tmp_info) < 0)
2823 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2824 else
2825 wwi->dwTotalRecorded = wwi->dwTotalRead + tmp_info.bytes;
2827 SetEvent(ev);
2828 break;
2829 case WINE_WM_CLOSING:
2830 wwi->hThread = 0;
2831 wwi->state = WINE_WS_CLOSED;
2832 SetEvent(ev);
2833 HeapFree(GetProcessHeap(), 0, buffer);
2834 ExitThread(0);
2835 /* shouldn't go here */
2836 default:
2837 FIXME("unknown message %d\n", msg);
2838 break;
2842 ExitThread(0);
2843 /* just for not generating compilation warnings... should never be executed */
2844 return 0;
2848 /**************************************************************************
2849 * widOpen [internal]
2851 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2853 WINE_WAVEIN* wwi;
2854 audio_buf_info info;
2855 int audio_fragment;
2856 DWORD ret;
2858 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
2859 if (lpDesc == NULL) {
2860 WARN("Invalid Parameter !\n");
2861 return MMSYSERR_INVALPARAM;
2863 if (wDevID >= numInDev) {
2864 WARN("bad device id: %d >= %d\n", wDevID, numInDev);
2865 return MMSYSERR_BADDEVICEID;
2868 /* only PCM format is supported so far... */
2869 if (!supportedFormat(lpDesc->lpFormat)) {
2870 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2871 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2872 lpDesc->lpFormat->nSamplesPerSec);
2873 return WAVERR_BADFORMAT;
2876 if (dwFlags & WAVE_FORMAT_QUERY) {
2877 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2878 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2879 lpDesc->lpFormat->nSamplesPerSec);
2880 return MMSYSERR_NOERROR;
2883 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2884 lpDesc->lpFormat->nSamplesPerSec,
2885 lpDesc->lpFormat->wBitsPerSample,
2886 lpDesc->lpFormat->nChannels,
2887 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
2888 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
2889 "UNSUPPORTED");
2891 wwi = &WInDev[wDevID];
2893 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2895 if ((dwFlags & WAVE_DIRECTSOUND) &&
2896 !(wwi->ossdev.in_caps_support & WAVECAPS_DIRECTSOUND))
2897 /* not supported, ignore it */
2898 dwFlags &= ~WAVE_DIRECTSOUND;
2900 if (dwFlags & WAVE_DIRECTSOUND) {
2901 TRACE("has DirectSoundCapture driver\n");
2902 if (wwi->ossdev.in_caps_support & WAVECAPS_SAMPLEACCURATE)
2903 /* we have realtime DirectSound, fragments just waste our time,
2904 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2905 audio_fragment = 0x0020000B;
2906 else
2907 /* to approximate realtime, we must use small fragments,
2908 * let's try to fragment the above 64KB (256 * 2^8) */
2909 audio_fragment = 0x01000008;
2910 } else {
2911 TRACE("doesn't have DirectSoundCapture driver\n");
2912 if (wwi->ossdev.open_count > 0) {
2913 TRACE("Using output device audio_fragment\n");
2914 /* FIXME: This may not be optimal for capture but it allows us
2915 * to do hardware playback without hardware capture. */
2916 audio_fragment = wwi->ossdev.audio_fragment;
2917 } else {
2918 /* A wave device must have a worst case latency of 10 ms so calculate
2919 * the largest fragment size less than 10 ms long.
2921 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2922 int shift = 0;
2923 while ((1 << shift) <= fsize)
2924 shift++;
2925 shift--;
2926 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2930 TRACE("requesting %d %d byte fragments (%d ms)\n", audio_fragment >> 16,
2931 1 << (audio_fragment & 0xffff),
2932 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2934 ret = OSS_OpenDevice(&wwi->ossdev, O_RDONLY, &audio_fragment,
2936 lpDesc->lpFormat->nSamplesPerSec,
2937 lpDesc->lpFormat->nChannels,
2938 (lpDesc->lpFormat->wBitsPerSample == 16)
2939 ? AFMT_S16_LE : AFMT_U8);
2940 if (ret != 0) return ret;
2941 wwi->state = WINE_WS_STOPPED;
2943 if (wwi->lpQueuePtr) {
2944 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2945 wwi->lpQueuePtr = NULL;
2947 wwi->dwTotalRecorded = 0;
2948 wwi->dwTotalRead = 0;
2949 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2951 wwi->waveDesc = *lpDesc;
2952 copy_format(lpDesc->lpFormat, &wwi->waveFormat);
2954 if (wwi->waveFormat.Format.wBitsPerSample == 0) {
2955 WARN("Resetting zeroed wBitsPerSample\n");
2956 wwi->waveFormat.Format.wBitsPerSample = 8 *
2957 (wwi->waveFormat.Format.nAvgBytesPerSec /
2958 wwi->waveFormat.Format.nSamplesPerSec) /
2959 wwi->waveFormat.Format.nChannels;
2962 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
2963 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
2964 wwi->ossdev.dev_name, strerror(errno));
2965 OSS_CloseDevice(&wwi->ossdev);
2966 wwi->state = WINE_WS_CLOSED;
2967 return MMSYSERR_NOTENABLED;
2970 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2971 info.fragsize, (info.fragsize * 1000) / (wwi->ossdev.sample_rate *
2972 wwi->ossdev.channels * (wwi->ossdev.format == AFMT_U8 ? 1 : 2)));
2974 wwi->dwFragmentSize = info.fragsize;
2976 TRACE("dwFragmentSize=%u\n", wwi->dwFragmentSize);
2977 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2978 wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
2979 wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
2980 wwi->waveFormat.Format.nBlockAlign);
2982 OSS_InitRingMessage(&wwi->msgRing);
2984 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2985 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwi->dwThreadID));
2986 if (wwi->hThread)
2987 SetThreadPriority(wwi->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2988 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2989 CloseHandle(wwi->hStartUpEvent);
2990 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2992 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2995 /**************************************************************************
2996 * widClose [internal]
2998 static DWORD widClose(WORD wDevID)
3000 WINE_WAVEIN* wwi;
3002 TRACE("(%u);\n", wDevID);
3003 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3004 WARN("can't close !\n");
3005 return MMSYSERR_INVALHANDLE;
3008 wwi = &WInDev[wDevID];
3010 if (wwi->lpQueuePtr != NULL) {
3011 WARN("still buffers open !\n");
3012 return WAVERR_STILLPLAYING;
3015 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
3016 OSS_CloseDevice(&wwi->ossdev);
3017 wwi->state = WINE_WS_CLOSED;
3018 wwi->dwFragmentSize = 0;
3019 OSS_DestroyRingMessage(&wwi->msgRing);
3020 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
3023 /**************************************************************************
3024 * widAddBuffer [internal]
3026 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3028 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
3030 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3031 WARN("can't do it !\n");
3032 return MMSYSERR_INVALHANDLE;
3034 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
3035 TRACE("never been prepared !\n");
3036 return WAVERR_UNPREPARED;
3038 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
3039 TRACE("header already in use !\n");
3040 return WAVERR_STILLPLAYING;
3043 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
3044 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3045 lpWaveHdr->dwBytesRecorded = 0;
3046 lpWaveHdr->lpNext = NULL;
3048 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
3049 return MMSYSERR_NOERROR;
3052 /**************************************************************************
3053 * widStart [internal]
3055 static DWORD widStart(WORD wDevID)
3057 TRACE("(%u);\n", wDevID);
3058 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3059 WARN("can't start recording !\n");
3060 return MMSYSERR_INVALHANDLE;
3063 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
3064 return MMSYSERR_NOERROR;
3067 /**************************************************************************
3068 * widStop [internal]
3070 static DWORD widStop(WORD wDevID)
3072 TRACE("(%u);\n", wDevID);
3073 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3074 WARN("can't stop !\n");
3075 return MMSYSERR_INVALHANDLE;
3078 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3080 return MMSYSERR_NOERROR;
3083 /**************************************************************************
3084 * widReset [internal]
3086 static DWORD widReset(WORD wDevID)
3088 TRACE("(%u);\n", wDevID);
3089 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3090 WARN("can't reset !\n");
3091 return MMSYSERR_INVALHANDLE;
3093 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3094 return MMSYSERR_NOERROR;
3097 /**************************************************************************
3098 * widGetPosition [internal]
3100 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3102 WINE_WAVEIN* wwi;
3104 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
3106 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3107 WARN("can't get pos !\n");
3108 return MMSYSERR_INVALHANDLE;
3111 if (lpTime == NULL) {
3112 WARN("invalid parameter: lpTime == NULL\n");
3113 return MMSYSERR_INVALPARAM;
3116 wwi = &WInDev[wDevID];
3117 #ifdef EXACT_WIDPOSITION
3118 if (wwi->ossdev.in_caps_support & WAVECAPS_SAMPLEACCURATE)
3119 OSS_AddRingMessage(&(wwi->msgRing), WINE_WM_UPDATE, 0, TRUE);
3120 #endif
3122 return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->waveFormat);
3125 /**************************************************************************
3126 * widMessage (WINEOSS.6)
3128 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD_PTR dwUser,
3129 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3131 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
3132 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
3134 switch (wMsg) {
3135 case DRVM_INIT:
3136 case DRVM_EXIT:
3137 case DRVM_ENABLE:
3138 case DRVM_DISABLE:
3139 /* FIXME: Pretend this is supported */
3140 return 0;
3141 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3142 case WIDM_CLOSE: return widClose (wDevID);
3143 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3144 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
3145 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
3146 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
3147 case WIDM_GETNUMDEVS: return numInDev;
3148 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
3149 case WIDM_RESET: return widReset (wDevID);
3150 case WIDM_START: return widStart (wDevID);
3151 case WIDM_STOP: return widStop (wDevID);
3152 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
3153 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
3154 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3155 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3156 default:
3157 FIXME("unknown message %u!\n", wMsg);
3159 return MMSYSERR_NOTSUPPORTED;
3162 #else /* !HAVE_OSS */
3164 /**************************************************************************
3165 * wodMessage (WINEOSS.7)
3167 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD_PTR dwUser,
3168 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3170 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3171 return MMSYSERR_NOTENABLED;
3174 /**************************************************************************
3175 * widMessage (WINEOSS.6)
3177 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD_PTR dwUser,
3178 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3180 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3181 return MMSYSERR_NOTENABLED;
3184 #endif /* HAVE_OSS */