Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / wineoss.drv / audio.c
blobcd89cb7687b027734f3eb340c3d34805f6848f11
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 #define MSG_TO_STR(x) case x: return #x
111 switch(msg) {
112 MSG_TO_STR(WINE_WM_PAUSING);
113 MSG_TO_STR(WINE_WM_RESTARTING);
114 MSG_TO_STR(WINE_WM_RESETTING);
115 MSG_TO_STR(WINE_WM_HEADER);
116 MSG_TO_STR(WINE_WM_UPDATE);
117 MSG_TO_STR(WINE_WM_BREAKLOOP);
118 MSG_TO_STR(WINE_WM_CLOSING);
119 MSG_TO_STR(WINE_WM_STARTING);
120 MSG_TO_STR(WINE_WM_STOPPING);
122 #undef MSG_TO_STR
123 return wine_dbg_sprintf("UNKNOWN(0x%08x)", msg);
126 int getEnables(OSS_DEVICE *ossdev)
128 return ( (ossdev->bOutputEnabled ? PCM_ENABLE_OUTPUT : 0) |
129 (ossdev->bInputEnabled ? PCM_ENABLE_INPUT : 0) );
132 static const char * getMessage(UINT msg)
134 #define MSG_TO_STR(x) case x: return #x
135 switch(msg) {
136 MSG_TO_STR(DRVM_INIT);
137 MSG_TO_STR(DRVM_EXIT);
138 MSG_TO_STR(DRVM_ENABLE);
139 MSG_TO_STR(DRVM_DISABLE);
140 MSG_TO_STR(WIDM_OPEN);
141 MSG_TO_STR(WIDM_CLOSE);
142 MSG_TO_STR(WIDM_ADDBUFFER);
143 MSG_TO_STR(WIDM_PREPARE);
144 MSG_TO_STR(WIDM_UNPREPARE);
145 MSG_TO_STR(WIDM_GETDEVCAPS);
146 MSG_TO_STR(WIDM_GETNUMDEVS);
147 MSG_TO_STR(WIDM_GETPOS);
148 MSG_TO_STR(WIDM_RESET);
149 MSG_TO_STR(WIDM_START);
150 MSG_TO_STR(WIDM_STOP);
151 MSG_TO_STR(WODM_OPEN);
152 MSG_TO_STR(WODM_CLOSE);
153 MSG_TO_STR(WODM_WRITE);
154 MSG_TO_STR(WODM_PAUSE);
155 MSG_TO_STR(WODM_GETPOS);
156 MSG_TO_STR(WODM_BREAKLOOP);
157 MSG_TO_STR(WODM_PREPARE);
158 MSG_TO_STR(WODM_UNPREPARE);
159 MSG_TO_STR(WODM_GETDEVCAPS);
160 MSG_TO_STR(WODM_GETNUMDEVS);
161 MSG_TO_STR(WODM_GETPITCH);
162 MSG_TO_STR(WODM_SETPITCH);
163 MSG_TO_STR(WODM_GETPLAYBACKRATE);
164 MSG_TO_STR(WODM_SETPLAYBACKRATE);
165 MSG_TO_STR(WODM_GETVOLUME);
166 MSG_TO_STR(WODM_SETVOLUME);
167 MSG_TO_STR(WODM_RESTART);
168 MSG_TO_STR(WODM_RESET);
169 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
170 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
171 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
172 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
174 #undef MSG_TO_STR
175 return wine_dbg_sprintf("UNKNOWN(0x%04x)", msg);
178 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
180 TRACE("(%u, %p)\n", wDevID, dwParam1);
182 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].ossdev.interface_name, -1,
183 NULL, 0 ) * sizeof(WCHAR);
184 return MMSYSERR_NOERROR;
187 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
189 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].ossdev.interface_name, -1,
190 NULL, 0 ) * sizeof(WCHAR))
192 MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].ossdev.interface_name, -1,
193 dwParam1, dwParam2 / sizeof(WCHAR));
194 return MMSYSERR_NOERROR;
197 return MMSYSERR_INVALPARAM;
200 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
202 TRACE("(%u, %p)\n", wDevID, dwParam1);
204 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].ossdev.interface_name, -1,
205 NULL, 0 ) * sizeof(WCHAR);
206 return MMSYSERR_NOERROR;
209 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
211 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].ossdev.interface_name, -1,
212 NULL, 0 ) * sizeof(WCHAR))
214 MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].ossdev.interface_name, -1,
215 dwParam1, dwParam2 / sizeof(WCHAR));
216 return MMSYSERR_NOERROR;
219 return MMSYSERR_INVALPARAM;
222 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
223 WAVEFORMATPCMEX* format)
225 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
226 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
227 format->Format.nChannels, format->Format.nAvgBytesPerSec);
228 TRACE("Position in bytes=%u\n", position);
230 switch (lpTime->wType) {
231 case TIME_SAMPLES:
232 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
233 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
234 break;
235 case TIME_MS:
236 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
237 TRACE("TIME_MS=%u\n", lpTime->u.ms);
238 break;
239 case TIME_SMPTE:
240 lpTime->u.smpte.fps = 30;
241 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
242 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
243 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
244 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
245 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
246 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
247 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
248 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
249 lpTime->u.smpte.fps = 30;
250 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
251 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
252 lpTime->u.smpte.hour, lpTime->u.smpte.min,
253 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
254 break;
255 default:
256 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
257 lpTime->wType = TIME_BYTES;
258 /* fall through */
259 case TIME_BYTES:
260 lpTime->u.cb = position;
261 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
262 break;
264 return MMSYSERR_NOERROR;
267 static BOOL supportedFormat(LPWAVEFORMATEX wf)
269 TRACE("(%p)\n",wf);
271 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
272 return FALSE;
274 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
275 if (wf->nChannels >= 1 && wf->nChannels <= MAX_CHANNELS) {
276 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
277 return TRUE;
279 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
280 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
282 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
283 if (wf->nChannels >=1 && wf->nChannels <= MAX_CHANNELS) {
284 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
285 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
286 return TRUE;
287 } else
288 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
290 } else
291 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
292 } else
293 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
295 return FALSE;
298 void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
300 ZeroMemory(wf2, sizeof(wf2));
301 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
302 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
303 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
304 memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
305 else
306 memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
309 /*======================================================================*
310 * Low level WAVE implementation *
311 *======================================================================*/
313 /******************************************************************
314 * OSS_RawOpenDevice
316 * Low level device opening (from values stored in ossdev)
318 static DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
320 int fd, val, rc;
321 TRACE("(%p,%d)\n",ossdev,strict_format);
323 TRACE("open_access=%s\n",
324 ossdev->open_access == O_RDONLY ? "O_RDONLY" :
325 ossdev->open_access == O_WRONLY ? "O_WRONLY" :
326 ossdev->open_access == O_RDWR ? "O_RDWR" : "Unknown");
328 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
330 WARN("Couldn't open %s (%s)\n", ossdev->dev_name, strerror(errno));
331 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
333 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
334 /* turn full duplex on if it has been requested */
335 if (ossdev->open_access == O_RDWR && ossdev->full_duplex) {
336 rc = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
337 /* on *BSD, as full duplex is always enabled by default, this ioctl
338 * will fail with EINVAL
339 * so, we don't consider EINVAL an error here
341 if (rc != 0 && errno != EINVAL) {
342 WARN("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev->dev_name, strerror(errno));
343 goto error2;
347 if (ossdev->audio_fragment) {
348 rc = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
349 if (rc != 0) {
350 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev->dev_name, strerror(errno));
351 goto error2;
355 /* First size and channels then samplerate */
356 if (ossdev->format>=0)
358 val = ossdev->format;
359 rc = ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
360 if (rc != 0 || val != ossdev->format) {
361 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
362 if (strict_format)
363 goto error;
366 if (ossdev->channels>=0)
368 val = ossdev->channels;
369 rc = ioctl(fd, SNDCTL_DSP_CHANNELS, &ossdev->channels);
370 if (rc != 0 || val != ossdev->channels) {
371 TRACE("Can't set channels to %u (returned %d)\n", val, ossdev->channels);
372 if (strict_format)
373 goto error;
376 if (ossdev->sample_rate>=0)
378 val = ossdev->sample_rate;
379 rc = ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
380 if (rc != 0 || !NEAR_MATCH(val, ossdev->sample_rate)) {
381 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
382 if (strict_format)
383 goto error;
386 ossdev->fd = fd;
388 ossdev->bOutputEnabled = TRUE; /* OSS enables by default */
389 ossdev->bInputEnabled = TRUE; /* OSS enables by default */
390 if (ossdev->open_access == O_RDONLY)
391 ossdev->bOutputEnabled = FALSE;
392 if (ossdev->open_access == O_WRONLY)
393 ossdev->bInputEnabled = FALSE;
395 if (ossdev->bTriggerSupport) {
396 int trigger;
397 trigger = getEnables(ossdev);
398 /* If we do not have full duplex, but they opened RDWR
399 ** (as you have to in order for an mmap to succeed)
400 ** then we start out with input off
402 if (ossdev->open_access == O_RDWR && !ossdev->full_duplex &&
403 ossdev->bInputEnabled && ossdev->bOutputEnabled) {
404 ossdev->bInputEnabled = FALSE;
405 trigger &= ~PCM_ENABLE_INPUT;
406 ioctl(fd, SNDCTL_DSP_SETTRIGGER, &trigger);
410 return MMSYSERR_NOERROR;
412 error:
413 close(fd);
414 return WAVERR_BADFORMAT;
415 error2:
416 close(fd);
417 return MMSYSERR_ERROR;
420 /******************************************************************
421 * OSS_OpenDevice
423 * since OSS has poor capabilities in full duplex, we try here to let a program
424 * open the device for both waveout and wavein streams...
425 * this is hackish, but it's the way OSS interface is done...
427 DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
428 int* frag, int strict_format,
429 int sample_rate, int channels, int fmt)
431 DWORD ret;
432 DWORD open_access;
433 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev,req_access,frag,strict_format,sample_rate,channels,fmt);
435 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
437 TRACE("Opening RDWR because full_duplex=%d and req_access=%d\n",
438 ossdev->full_duplex,req_access);
439 open_access = O_RDWR;
441 else
443 open_access=req_access;
446 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
447 if (ossdev->open_count == 0)
449 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
451 ossdev->audio_fragment = (frag) ? *frag : 0;
452 ossdev->sample_rate = sample_rate;
453 ossdev->channels = channels;
454 ossdev->format = fmt;
455 ossdev->open_access = open_access;
456 ossdev->owner_tid = GetCurrentThreadId();
458 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
459 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
460 (req_access == O_RDONLY || req_access == O_WRONLY))
462 int enable;
463 if (req_access == O_WRONLY)
464 ossdev->bInputEnabled=0;
465 else
466 ossdev->bOutputEnabled=0;
467 enable = getEnables(ossdev);
468 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
469 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
470 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
473 else
475 /* check we really open with the same parameters */
476 if (ossdev->open_access != open_access)
478 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
479 return WAVERR_BADFORMAT;
482 /* check if the audio parameters are the same */
483 if (ossdev->sample_rate != sample_rate ||
484 ossdev->channels != channels ||
485 ossdev->format != fmt)
487 /* This is not a fatal error because MSACM might do the remapping */
488 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
489 "OSS doesn't allow us different parameters\n"
490 "audio_frag(%x/%x) sample_rate(%d/%d) channels(%d/%d) fmt(%d/%d)\n",
491 ossdev->audio_fragment, frag ? *frag : 0,
492 ossdev->sample_rate, sample_rate,
493 ossdev->channels, channels,
494 ossdev->format, fmt);
495 return WAVERR_BADFORMAT;
497 /* check if the fragment sizes are the same */
498 if (ossdev->audio_fragment != (frag ? *frag : 0) )
500 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
501 "Please run winecfg, open \"Audio\" page and set\n"
502 "\"Hardware Acceleration\" to \"Emulation\".\n");
503 return WAVERR_BADFORMAT;
505 if (GetCurrentThreadId() != ossdev->owner_tid)
507 WARN("Another thread is trying to access audio...\n");
508 return MMSYSERR_ERROR;
510 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
511 (req_access == O_RDONLY || req_access == O_WRONLY))
513 int enable;
514 if (req_access == O_WRONLY)
515 ossdev->bOutputEnabled=1;
516 else
517 ossdev->bInputEnabled=1;
518 enable = getEnables(ossdev);
519 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
520 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
521 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
525 ossdev->open_count++;
527 return MMSYSERR_NOERROR;
530 /******************************************************************
531 * OSS_CloseDevice
535 void OSS_CloseDevice(OSS_DEVICE* ossdev)
537 TRACE("(%p)\n",ossdev);
538 if (ossdev->open_count>0) {
539 ossdev->open_count--;
540 } else {
541 WARN("OSS_CloseDevice called too many times\n");
543 if (ossdev->open_count == 0)
545 fcntl(ossdev->fd, F_SETFL, fcntl(ossdev->fd, F_GETFL) & ~O_NDELAY);
546 /* reset the device before we close it in case it is in a bad state */
547 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
548 if (close(ossdev->fd) != 0) FIXME("Cannot close %d: %s\n", ossdev->fd, strerror(errno));
552 /******************************************************************
553 * OSS_ResetDevice
555 * Resets the device. OSS Commercial requires the device to be closed
556 * after a SNDCTL_DSP_RESET ioctl call... this function implements
557 * this behavior...
558 * FIXME: This causes problems when doing full duplex so we really
559 * only reset when not doing full duplex. We need to do this better
560 * someday.
562 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
564 DWORD ret = MMSYSERR_NOERROR;
565 int old_fd = ossdev->fd;
566 TRACE("(%p)\n", ossdev);
568 if (ossdev->open_count == 1) {
569 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
571 perror("ioctl SNDCTL_DSP_RESET");
572 return -1;
574 close(ossdev->fd);
575 ret = OSS_RawOpenDevice(ossdev, 1);
576 TRACE("Changing fd from %d to %d\n", old_fd, ossdev->fd);
577 } else
578 WARN("Not resetting device because it is in full duplex mode!\n");
580 return ret;
583 static const int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
584 static const int win_std_rates[5]={96000,48000,44100,22050,11025};
585 static const int win_std_formats[2][2][5]=
586 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
587 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
588 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
589 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
590 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
591 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
592 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
593 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
596 static void OSS_Info(int fd)
598 /* Note that this only reports the formats supported by the hardware.
599 * The driver may support other formats and do the conversions in
600 * software which is why we don't use this value
602 int oss_mask, oss_caps;
603 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &oss_mask) >= 0) {
604 TRACE("Formats=%08x ( ", oss_mask);
605 if (oss_mask & AFMT_MU_LAW) TRACE("AFMT_MU_LAW ");
606 if (oss_mask & AFMT_A_LAW) TRACE("AFMT_A_LAW ");
607 if (oss_mask & AFMT_IMA_ADPCM) TRACE("AFMT_IMA_ADPCM ");
608 if (oss_mask & AFMT_U8) TRACE("AFMT_U8 ");
609 if (oss_mask & AFMT_S16_LE) TRACE("AFMT_S16_LE ");
610 if (oss_mask & AFMT_S16_BE) TRACE("AFMT_S16_BE ");
611 if (oss_mask & AFMT_S8) TRACE("AFMT_S8 ");
612 if (oss_mask & AFMT_U16_LE) TRACE("AFMT_U16_LE ");
613 if (oss_mask & AFMT_U16_BE) TRACE("AFMT_U16_BE ");
614 if (oss_mask & AFMT_MPEG) TRACE("AFMT_MPEG ");
615 #ifdef AFMT_AC3
616 if (oss_mask & AFMT_AC3) TRACE("AFMT_AC3 ");
617 #endif
618 #ifdef AFMT_VORBIS
619 if (oss_mask & AFMT_VORBIS) TRACE("AFMT_VORBIS ");
620 #endif
621 #ifdef AFMT_S32_LE
622 if (oss_mask & AFMT_S32_LE) TRACE("AFMT_S32_LE ");
623 #endif
624 #ifdef AFMT_S32_BE
625 if (oss_mask & AFMT_S32_BE) TRACE("AFMT_S32_BE ");
626 #endif
627 #ifdef AFMT_FLOAT
628 if (oss_mask & AFMT_FLOAT) TRACE("AFMT_FLOAT ");
629 #endif
630 #ifdef AFMT_S24_LE
631 if (oss_mask & AFMT_S24_LE) TRACE("AFMT_S24_LE ");
632 #endif
633 #ifdef AFMT_S24_BE
634 if (oss_mask & AFMT_S24_BE) TRACE("AFMT_S24_BE ");
635 #endif
636 #ifdef AFMT_SPDIF_RAW
637 if (oss_mask & AFMT_SPDIF_RAW) TRACE("AFMT_SPDIF_RAW ");
638 #endif
639 TRACE(")\n");
641 if (ioctl(fd, SNDCTL_DSP_GETCAPS, &oss_caps) >= 0) {
642 TRACE("Caps=%08x\n",oss_caps);
643 TRACE("\tRevision: %d\n", oss_caps&DSP_CAP_REVISION);
644 TRACE("\tDuplex: %s\n", oss_caps & DSP_CAP_DUPLEX ? "true" : "false");
645 TRACE("\tRealtime: %s\n", oss_caps & DSP_CAP_REALTIME ? "true" : "false");
646 TRACE("\tBatch: %s\n", oss_caps & DSP_CAP_BATCH ? "true" : "false");
647 TRACE("\tCoproc: %s\n", oss_caps & DSP_CAP_COPROC ? "true" : "false");
648 TRACE("\tTrigger: %s\n", oss_caps & DSP_CAP_TRIGGER ? "true" : "false");
649 TRACE("\tMmap: %s\n", oss_caps & DSP_CAP_MMAP ? "true" : "false");
650 #ifdef DSP_CAP_MULTI
651 TRACE("\tMulti: %s\n", oss_caps & DSP_CAP_MULTI ? "true" : "false");
652 #endif
653 #ifdef DSP_CAP_BIND
654 TRACE("\tBind: %s\n", oss_caps & DSP_CAP_BIND ? "true" : "false");
655 #endif
656 #ifdef DSP_CAP_INPUT
657 TRACE("\tInput: %s\n", oss_caps & DSP_CAP_INPUT ? "true" : "false");
658 #endif
659 #ifdef DSP_CAP_OUTPUT
660 TRACE("\tOutput: %s\n", oss_caps & DSP_CAP_OUTPUT ? "true" : "false");
661 #endif
662 #ifdef DSP_CAP_VIRTUAL
663 TRACE("\tVirtual: %s\n", oss_caps & DSP_CAP_VIRTUAL ? "true" : "false");
664 #endif
665 #ifdef DSP_CAP_ANALOGOUT
666 TRACE("\tAnalog Out: %s\n", oss_caps & DSP_CAP_ANALOGOUT ? "true" : "false");
667 #endif
668 #ifdef DSP_CAP_ANALOGIN
669 TRACE("\tAnalog In: %s\n", oss_caps & DSP_CAP_ANALOGIN ? "true" : "false");
670 #endif
671 #ifdef DSP_CAP_DIGITALOUT
672 TRACE("\tDigital Out: %s\n", oss_caps & DSP_CAP_DIGITALOUT ? "true" : "false");
673 #endif
674 #ifdef DSP_CAP_DIGITALIN
675 TRACE("\tDigital In: %s\n", oss_caps & DSP_CAP_DIGITALIN ? "true" : "false");
676 #endif
677 #ifdef DSP_CAP_ADMASK
678 TRACE("\tA/D Mask: %s\n", oss_caps & DSP_CAP_ADMASK ? "true" : "false");
679 #endif
680 #ifdef DSP_CAP_SHADOW
681 TRACE("\tShadow: %s\n", oss_caps & DSP_CAP_SHADOW ? "true" : "false");
682 #endif
683 #ifdef DSP_CH_MASK
684 TRACE("\tChannel Mask: %x\n", oss_caps & DSP_CH_MASK);
685 #endif
686 #ifdef DSP_CAP_SLAVE
687 TRACE("\tSlave: %s\n", oss_caps & DSP_CAP_SLAVE ? "true" : "false");
688 #endif
692 /******************************************************************
693 * OSS_WaveOutInit
697 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
699 int rc,arg;
700 int f,c;
701 unsigned int r;
702 BOOL has_mixer = FALSE;
703 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
705 if (OSS_OpenDevice(ossdev, O_WRONLY, NULL, 0,-1,-1,-1) != 0)
706 return FALSE;
708 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
710 #if defined(SNDCTL_MIXERINFO)
712 int mixer;
713 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
714 oss_mixerinfo info;
715 info.dev = 0;
716 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
717 lstrcpynA(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
718 strcpy(ossdev->ds_desc.szDrvname, "wineoss.drv");
719 MultiByteToWideChar(CP_UNIXCP, 0, info.name, sizeof(info.name),
720 ossdev->out_caps.szPname,
721 sizeof(ossdev->out_caps.szPname) / sizeof(WCHAR));
722 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
723 has_mixer = TRUE;
724 } else {
725 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
727 close(mixer);
728 } else {
729 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
732 #elif defined(SOUND_MIXER_INFO)
734 int mixer;
735 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
736 mixer_info info;
737 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
738 lstrcpynA(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
739 strcpy(ossdev->ds_desc.szDrvname, "wineoss.drv");
740 MultiByteToWideChar(CP_UNIXCP, 0, info.name, sizeof(info.name),
741 ossdev->out_caps.szPname,
742 sizeof(ossdev->out_caps.szPname) / sizeof(WCHAR));
743 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
744 has_mixer = TRUE;
745 } else {
746 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
747 * implement it properly, and there are probably similar issues
748 * on other platforms, so we warn but try to go ahead.
750 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
752 close(mixer);
753 } else {
754 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
757 #endif /* SOUND_MIXER_INFO */
759 if (WINE_TRACE_ON(wave))
760 OSS_Info(ossdev->fd);
762 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
763 ossdev->out_caps.wPid = 0x0001; /* Product ID */
765 ossdev->out_caps.vDriverVersion = 0x0100;
766 ossdev->out_caps.wChannels = 1;
767 ossdev->out_caps.dwFormats = 0x00000000;
768 ossdev->out_caps.wReserved1 = 0;
769 ossdev->out_caps.dwSupport = has_mixer ? WAVECAPS_VOLUME : 0;
771 /* direct sound caps */
772 ossdev->ds_caps.dwFlags = DSCAPS_CERTIFIED;
773 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY8BIT;
774 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY16BIT;
775 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYMONO;
776 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYSTEREO;
777 ossdev->ds_caps.dwFlags |= DSCAPS_CONTINUOUSRATE;
779 ossdev->ds_caps.dwPrimaryBuffers = 1;
780 ossdev->ds_caps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
781 ossdev->ds_caps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
783 /* We must first set the format and the stereo mode as some sound cards
784 * may support 44kHz mono but not 44kHz stereo. Also we must
785 * systematically check the return value of these ioctls as they will
786 * always succeed (see OSS Linux) but will modify the parameter to match
787 * whatever they support. The OSS specs also say we must first set the
788 * sample size, then the stereo and then the sample rate.
790 for (f=0;f<2;f++) {
791 arg=win_std_oss_fmts[f];
792 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
793 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
794 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
795 rc,arg,win_std_oss_fmts[f]);
796 continue;
798 if (f == 0)
799 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY8BIT;
800 else if (f == 1)
801 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY16BIT;
803 for (c = 1; c <= MAX_CHANNELS; c++) {
804 arg=c;
805 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
806 if( rc == -1) break;
807 if (rc!=0 || arg!=c) {
808 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
809 continue;
811 if (c == 1) {
812 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
813 } else if (c == 2) {
814 ossdev->out_caps.wChannels = 2;
815 if (has_mixer)
816 ossdev->out_caps.dwSupport|=WAVECAPS_LRVOLUME;
817 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
818 } else
819 ossdev->out_caps.wChannels = c;
821 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
822 arg=win_std_rates[r];
823 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
824 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
825 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
826 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
827 ossdev->out_caps.dwFormats|=win_std_formats[f][c-1][r];
832 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
833 if (arg & DSP_CAP_TRIGGER)
834 ossdev->bTriggerSupport = TRUE;
835 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
836 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
838 /* well, might as well use the DirectSound cap flag for something */
839 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
840 !(arg & DSP_CAP_BATCH)) {
841 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
842 } else {
843 ossdev->ds_caps.dwFlags |= DSCAPS_EMULDRIVER;
845 #ifdef DSP_CAP_MULTI /* not every oss has this */
846 /* check for hardware secondary buffer support (multi open) */
847 if ((arg & DSP_CAP_MULTI) &&
848 (ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
849 TRACE("hardware secondary buffer support available\n");
851 ossdev->ds_caps.dwMaxHwMixingAllBuffers = 16;
852 ossdev->ds_caps.dwMaxHwMixingStaticBuffers = 0;
853 ossdev->ds_caps.dwMaxHwMixingStreamingBuffers = 16;
855 ossdev->ds_caps.dwFreeHwMixingAllBuffers = 16;
856 ossdev->ds_caps.dwFreeHwMixingStaticBuffers = 0;
857 ossdev->ds_caps.dwFreeHwMixingStreamingBuffers = 16;
859 #endif
861 OSS_CloseDevice(ossdev);
862 TRACE("out wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
863 ossdev->out_caps.wChannels, ossdev->out_caps.dwFormats,
864 ossdev->out_caps.dwSupport);
865 return TRUE;
868 /******************************************************************
869 * OSS_WaveInInit
873 static BOOL OSS_WaveInInit(OSS_DEVICE* ossdev)
875 int rc,arg;
876 int f,c;
877 unsigned int r;
878 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
880 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0,-1,-1,-1) != 0)
881 return FALSE;
883 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
885 #if defined(SNDCTL_MIXERINFO)
887 int mixer;
888 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
889 oss_mixerinfo info;
890 info.dev = 0;
891 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
892 MultiByteToWideChar(CP_UNIXCP, 0, info.name, -1,
893 ossdev->in_caps.szPname,
894 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
895 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
896 } else {
897 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
899 close(mixer);
900 } else {
901 WARN("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
904 #elif defined(SOUND_MIXER_INFO)
906 int mixer;
907 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
908 mixer_info info;
909 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
910 MultiByteToWideChar(CP_UNIXCP, 0, info.name, -1,
911 ossdev->in_caps.szPname,
912 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
913 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
914 } else {
915 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
916 * implement it properly, and there are probably similar issues
917 * on other platforms, so we warn but try to go ahead.
919 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
921 close(mixer);
922 } else {
923 WARN("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
926 #endif /* SOUND_MIXER_INFO */
928 if (WINE_TRACE_ON(wave))
929 OSS_Info(ossdev->fd);
931 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
932 ossdev->in_caps.wPid = 0x0001; /* Product ID */
934 ossdev->in_caps.dwFormats = 0x00000000;
935 ossdev->in_caps.wChannels = 1;
936 ossdev->in_caps.wReserved1 = 0;
938 /* direct sound caps */
939 ossdev->dsc_caps.dwSize = sizeof(ossdev->dsc_caps);
940 ossdev->dsc_caps.dwFlags = 0;
941 ossdev->dsc_caps.dwFormats = 0x00000000;
942 ossdev->dsc_caps.dwChannels = 1;
944 /* See the comment in OSS_WaveOutInit for the loop order */
945 for (f=0;f<2;f++) {
946 arg=win_std_oss_fmts[f];
947 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
948 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
949 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
950 rc,arg,win_std_oss_fmts[f]);
951 continue;
954 for (c = 1; c <= MAX_CHANNELS; c++) {
955 arg=c;
956 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
957 if( rc == -1) break;
958 if (rc!=0 || arg!=c) {
959 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
960 continue;
962 if (c > 1) {
963 ossdev->in_caps.wChannels = c;
964 ossdev->dsc_caps.dwChannels = c;
967 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
968 arg=win_std_rates[r];
969 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
970 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
971 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
972 ossdev->in_caps.dwFormats|=win_std_formats[f][c-1][r];
973 ossdev->dsc_caps.dwFormats|=win_std_formats[f][c-1][r];
978 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
979 if (arg & DSP_CAP_TRIGGER)
980 ossdev->bTriggerSupport = TRUE;
981 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
982 !(arg & DSP_CAP_BATCH)) {
983 /* FIXME: enable the next statement if you want to work on the driver */
984 #if 0
985 ossdev->in_caps_support |= WAVECAPS_DIRECTSOUND;
986 #endif
988 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH))
989 ossdev->in_caps_support |= WAVECAPS_SAMPLEACCURATE;
991 OSS_CloseDevice(ossdev);
992 TRACE("in wChannels = %d, dwFormats = %08X, in_caps_support = %08X\n",
993 ossdev->in_caps.wChannels, ossdev->in_caps.dwFormats, ossdev->in_caps_support);
994 return TRUE;
997 /******************************************************************
998 * OSS_WaveFullDuplexInit
1002 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
1004 int rc,arg;
1005 int f,c;
1006 unsigned int r;
1007 int caps;
1008 BOOL has_mixer = FALSE;
1009 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
1011 /* The OSS documentation says we must call SNDCTL_SETDUPLEX
1012 * *before* checking for SNDCTL_DSP_GETCAPS otherwise we may
1013 * get the wrong result. This ioctl must even be done before
1014 * setting the fragment size so that only OSS_RawOpenDevice is
1015 * in a position to do it. So we set full_duplex speculatively
1016 * and adjust right after.
1018 ossdev->full_duplex=1;
1019 rc=OSS_OpenDevice(ossdev, O_RDWR, NULL, 0,-1,-1,-1);
1020 ossdev->full_duplex=0;
1021 if (rc != 0)
1022 return;
1024 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
1026 #if defined(SNDCTL_MIXERINFO)
1028 int mixer;
1029 if ((mixer = open(ossdev->mixer_name, O_RDWR|O_NDELAY)) >= 0) {
1030 oss_mixerinfo info;
1031 info.dev = 0;
1032 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
1033 has_mixer = TRUE;
1034 } else {
1035 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
1037 close(mixer);
1038 } else {
1039 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
1042 #elif defined(SOUND_MIXER_INFO)
1044 int mixer;
1045 if ((mixer = open(ossdev->mixer_name, O_RDWR|O_NDELAY)) >= 0) {
1046 mixer_info info;
1047 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
1048 has_mixer = TRUE;
1049 } else {
1050 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
1051 * implement it properly, and there are probably similar issues
1052 * on other platforms, so we warn but try to go ahead.
1054 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
1056 close(mixer);
1057 } else {
1058 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
1061 #endif /* SOUND_MIXER_INFO */
1063 TRACE("%s\n", ossdev->ds_desc.szDesc);
1065 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
1066 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
1068 ossdev->duplex_out_caps = ossdev->out_caps;
1070 ossdev->duplex_out_caps.wChannels = 1;
1071 ossdev->duplex_out_caps.dwFormats = 0x00000000;
1072 ossdev->duplex_out_caps.dwSupport = has_mixer ? WAVECAPS_VOLUME : 0;
1074 if (WINE_TRACE_ON(wave))
1075 OSS_Info(ossdev->fd);
1077 /* See the comment in OSS_WaveOutInit for the loop order */
1078 for (f=0;f<2;f++) {
1079 arg=win_std_oss_fmts[f];
1080 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
1081 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
1082 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1083 rc,arg,win_std_oss_fmts[f]);
1084 continue;
1087 for (c = 1; c <= MAX_CHANNELS; c++) {
1088 arg=c;
1089 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
1090 if( rc == -1) break;
1091 if (rc!=0 || arg!=c) {
1092 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
1093 continue;
1095 if (c == 1) {
1096 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
1097 } else if (c == 2) {
1098 ossdev->duplex_out_caps.wChannels = 2;
1099 if (has_mixer)
1100 ossdev->duplex_out_caps.dwSupport|=WAVECAPS_LRVOLUME;
1101 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
1102 } else
1103 ossdev->duplex_out_caps.wChannels = c;
1105 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
1106 arg=win_std_rates[r];
1107 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
1108 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
1109 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
1110 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
1111 ossdev->duplex_out_caps.dwFormats|=win_std_formats[f][c-1][r];
1116 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
1117 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
1118 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
1120 /* well, might as well use the DirectSound cap flag for something */
1121 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
1122 !(arg & DSP_CAP_BATCH)) {
1123 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
1126 OSS_CloseDevice(ossdev);
1127 TRACE("duplex wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
1128 ossdev->duplex_out_caps.wChannels,
1129 ossdev->duplex_out_caps.dwFormats,
1130 ossdev->duplex_out_caps.dwSupport);
1133 static char* StrDup(const char* str, const char* def)
1135 char* dst;
1136 if (str==NULL)
1137 str=def;
1138 dst=HeapAlloc(GetProcessHeap(),0,strlen(str)+1);
1139 strcpy(dst, str);
1140 return dst;
1143 /******************************************************************
1144 * OSS_WaveInit
1146 * Initialize internal structures from OSS information
1148 LRESULT OSS_WaveInit(void)
1150 char* str;
1151 unsigned int i;
1153 /* FIXME: Remove unneeded members of WOutDev and WInDev */
1154 TRACE("()\n");
1156 str=getenv("AUDIODEV");
1157 if (str!=NULL)
1159 WOutDev[0].ossdev.dev_name = WInDev[0].ossdev.dev_name = StrDup(str,"");
1160 WOutDev[0].ossdev.mixer_name = WInDev[0].ossdev.mixer_name = StrDup(getenv("MIXERDEV"),"/dev/mixer");
1161 for (i = 1; i < MAX_WAVEDRV; ++i)
1163 WOutDev[i].ossdev.dev_name = WInDev[i].ossdev.dev_name = StrDup("",NULL);
1164 WOutDev[i].ossdev.mixer_name = WInDev[i].ossdev.mixer_name = StrDup("",NULL);
1167 else
1169 WOutDev[0].ossdev.dev_name = WInDev[0].ossdev.dev_name = StrDup("/dev/dsp",NULL);
1170 WOutDev[0].ossdev.mixer_name = WInDev[0].ossdev.mixer_name = StrDup("/dev/mixer",NULL);
1171 for (i = 1; i < MAX_WAVEDRV; ++i)
1173 WOutDev[i].ossdev.dev_name = WInDev[i].ossdev.dev_name = HeapAlloc(GetProcessHeap(),0,11);
1174 sprintf(WOutDev[i].ossdev.dev_name, "/dev/dsp%u", i);
1175 WOutDev[i].ossdev.mixer_name = WInDev[i].ossdev.mixer_name = HeapAlloc(GetProcessHeap(),0,13);
1176 sprintf(WOutDev[i].ossdev.mixer_name, "/dev/mixer%u", i);
1180 for (i = 0; i < MAX_WAVEDRV; ++i)
1182 WOutDev[i].ossdev.interface_name = WInDev[i].ossdev.interface_name =
1183 HeapAlloc(GetProcessHeap(),0,9+strlen(WOutDev[i].ossdev.dev_name)+1);
1184 sprintf(WOutDev[i].ossdev.interface_name, "wineoss: %s", WOutDev[i].ossdev.dev_name);
1187 /* start with output devices */
1188 for (i = 0; i < MAX_WAVEDRV; ++i)
1190 if (*WOutDev[i].ossdev.dev_name == '\0' || OSS_WaveOutInit(&WOutDev[i].ossdev))
1192 WOutDev[numOutDev].state = WINE_WS_CLOSED;
1193 WOutDev[numOutDev].volume = 0xffffffff;
1194 numOutDev++;
1198 /* then do input devices */
1199 for (i = 0; i < MAX_WAVEDRV; ++i)
1201 if (*WInDev[i].ossdev.dev_name=='\0' || OSS_WaveInInit(&WInDev[i].ossdev))
1203 WInDev[numInDev].state = WINE_WS_CLOSED;
1204 numInDev++;
1208 /* finish with the full duplex bits */
1209 for (i = 0; i < MAX_WAVEDRV; i++)
1210 if (*WOutDev[i].ossdev.dev_name!='\0')
1211 OSS_WaveFullDuplexInit(&WOutDev[i].ossdev);
1213 TRACE("%d wave out devices\n", numOutDev);
1214 for (i = 0; i < numOutDev; i++) {
1215 TRACE("%u: %s, %s, %s\n", i, WOutDev[i].ossdev.dev_name,
1216 WOutDev[i].ossdev.mixer_name, WOutDev[i].ossdev.interface_name);
1219 TRACE("%d wave in devices\n", numInDev);
1220 for (i = 0; i < numInDev; i++) {
1221 TRACE("%u: %s, %s, %s\n", i, WInDev[i].ossdev.dev_name,
1222 WInDev[i].ossdev.mixer_name, WInDev[i].ossdev.interface_name);
1225 return 0;
1228 /******************************************************************
1229 * OSS_WaveExit
1231 * Delete/clear internal structures of OSS information
1233 LRESULT OSS_WaveExit(void)
1235 int i;
1236 TRACE("()\n");
1238 for (i = 0; i < MAX_WAVEDRV; ++i)
1240 HeapFree(GetProcessHeap(), 0, WOutDev[i].ossdev.dev_name);
1241 HeapFree(GetProcessHeap(), 0, WOutDev[i].ossdev.mixer_name);
1242 HeapFree(GetProcessHeap(), 0, WOutDev[i].ossdev.interface_name);
1245 ZeroMemory(WOutDev, sizeof(WOutDev));
1246 ZeroMemory(WInDev, sizeof(WInDev));
1248 numOutDev = 0;
1249 numInDev = 0;
1251 return 0;
1254 /******************************************************************
1255 * OSS_InitRingMessage
1257 * Initialize the ring of messages for passing between driver's caller and playback/record
1258 * thread
1260 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
1262 omr->msg_toget = 0;
1263 omr->msg_tosave = 0;
1264 #ifdef USE_PIPE_SYNC
1265 if (pipe(omr->msg_pipe) < 0) {
1266 omr->msg_pipe[0] = -1;
1267 omr->msg_pipe[1] = -1;
1268 ERR("could not create pipe, error=%s\n", strerror(errno));
1270 #else
1271 omr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1272 #endif
1273 omr->ring_buffer_size = OSS_RING_BUFFER_INCREMENT;
1274 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(OSS_MSG));
1275 InitializeCriticalSection(&omr->msg_crst);
1276 omr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": OSS_MSG_RING.msg_crst");
1277 return 0;
1280 /******************************************************************
1281 * OSS_DestroyRingMessage
1284 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
1286 #ifdef USE_PIPE_SYNC
1287 close(omr->msg_pipe[0]);
1288 close(omr->msg_pipe[1]);
1289 #else
1290 CloseHandle(omr->msg_event);
1291 #endif
1292 HeapFree(GetProcessHeap(),0,omr->messages);
1293 omr->msg_crst.DebugInfo->Spare[0] = 0;
1294 DeleteCriticalSection(&omr->msg_crst);
1295 return 0;
1298 /******************************************************************
1299 * OSS_AddRingMessage
1301 * Inserts a new message into the ring (should be called from DriverProc derived routines)
1303 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
1305 HANDLE hEvent = INVALID_HANDLE_VALUE;
1307 EnterCriticalSection(&omr->msg_crst);
1308 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
1310 int old_ring_buffer_size = omr->ring_buffer_size;
1311 omr->ring_buffer_size += OSS_RING_BUFFER_INCREMENT;
1312 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
1313 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(OSS_MSG));
1314 /* Now we need to rearrange the ring buffer so that the new
1315 buffers just allocated are in between omr->msg_tosave and
1316 omr->msg_toget.
1318 if (omr->msg_tosave < omr->msg_toget)
1320 memmove(&(omr->messages[omr->msg_toget + OSS_RING_BUFFER_INCREMENT]),
1321 &(omr->messages[omr->msg_toget]),
1322 sizeof(OSS_MSG)*(old_ring_buffer_size - omr->msg_toget)
1324 omr->msg_toget += OSS_RING_BUFFER_INCREMENT;
1327 if (wait)
1329 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1330 if (hEvent == INVALID_HANDLE_VALUE)
1332 ERR("can't create event !?\n");
1333 LeaveCriticalSection(&omr->msg_crst);
1334 return 0;
1336 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
1337 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1338 omr->msg_toget,getCmdString(omr->messages[omr->msg_toget].msg),
1339 omr->msg_tosave,getCmdString(omr->messages[omr->msg_tosave].msg));
1341 /* fast messages have to be added at the start of the queue */
1342 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
1343 omr->messages[omr->msg_toget].msg = msg;
1344 omr->messages[omr->msg_toget].param = param;
1345 omr->messages[omr->msg_toget].hEvent = hEvent;
1347 else
1349 omr->messages[omr->msg_tosave].msg = msg;
1350 omr->messages[omr->msg_tosave].param = param;
1351 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
1352 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
1354 LeaveCriticalSection(&omr->msg_crst);
1355 /* signal a new message */
1356 SIGNAL_OMR(omr);
1357 if (wait)
1359 /* wait for playback/record thread to have processed the message */
1360 WaitForSingleObject(hEvent, INFINITE);
1361 CloseHandle(hEvent);
1363 return 1;
1366 /******************************************************************
1367 * OSS_RetrieveRingMessage
1369 * Get a message from the ring. Should be called by the playback/record thread.
1371 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
1372 enum win_wm_message *msg, DWORD_PTR *param, HANDLE *hEvent)
1374 EnterCriticalSection(&omr->msg_crst);
1376 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1378 LeaveCriticalSection(&omr->msg_crst);
1379 return 0;
1382 *msg = omr->messages[omr->msg_toget].msg;
1383 omr->messages[omr->msg_toget].msg = 0;
1384 *param = omr->messages[omr->msg_toget].param;
1385 *hEvent = omr->messages[omr->msg_toget].hEvent;
1386 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
1387 CLEAR_OMR(omr);
1388 LeaveCriticalSection(&omr->msg_crst);
1389 return 1;
1392 /******************************************************************
1393 * OSS_PeekRingMessage
1395 * Peek at a message from the ring but do not remove it.
1396 * Should be called by the playback/record thread.
1398 static int OSS_PeekRingMessage(OSS_MSG_RING* omr,
1399 enum win_wm_message *msg,
1400 DWORD_PTR *param, HANDLE *hEvent)
1402 EnterCriticalSection(&omr->msg_crst);
1404 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1406 LeaveCriticalSection(&omr->msg_crst);
1407 return 0;
1410 *msg = omr->messages[omr->msg_toget].msg;
1411 *param = omr->messages[omr->msg_toget].param;
1412 *hEvent = omr->messages[omr->msg_toget].hEvent;
1413 LeaveCriticalSection(&omr->msg_crst);
1414 return 1;
1417 /*======================================================================*
1418 * Low level WAVE OUT implementation *
1419 *======================================================================*/
1421 /**************************************************************************
1422 * wodNotifyClient [internal]
1424 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1426 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lx dwParam2 = %04lx\n", wMsg,
1427 wMsg == WOM_OPEN ? "WOM_OPEN" : wMsg == WOM_CLOSE ? "WOM_CLOSE" :
1428 wMsg == WOM_DONE ? "WOM_DONE" : "Unknown", dwParam1, dwParam2);
1430 switch (wMsg) {
1431 case WOM_OPEN:
1432 case WOM_CLOSE:
1433 case WOM_DONE:
1434 if (wwo->wFlags != DCB_NULL &&
1435 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
1436 (HDRVR)wwo->waveDesc.hWave, wMsg,
1437 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
1438 WARN("can't notify client !\n");
1439 return MMSYSERR_ERROR;
1441 break;
1442 default:
1443 FIXME("Unknown callback message %u\n", wMsg);
1444 return MMSYSERR_INVALPARAM;
1446 return MMSYSERR_NOERROR;
1449 /**************************************************************************
1450 * wodUpdatePlayedTotal [internal]
1453 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
1455 audio_buf_info dspspace;
1456 DWORD notplayed;
1457 if (!info) info = &dspspace;
1459 if (ioctl(wwo->ossdev.fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
1460 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev.dev_name, strerror(errno));
1461 return FALSE;
1464 /* GETOSPACE is not always accurate when we're down to the last fragment or two;
1465 ** we try to accommodate that here by assuming that the dsp is empty by looking
1466 ** at the clock rather than the result of GETOSPACE */
1467 notplayed = wwo->dwBufferSize - info->bytes;
1468 if (notplayed > 0 && notplayed < (info->fragsize * 2))
1470 if (wwo->dwProjectedFinishTime && GetTickCount() >= wwo->dwProjectedFinishTime)
1472 TRACE("Adjusting for a presumed OSS bug and assuming all data has been played.\n");
1473 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1474 return TRUE;
1476 else
1477 /* Some OSS drivers will clean up nicely if given a POST, so give 'em the chance... */
1478 ioctl(wwo->ossdev.fd, SNDCTL_DSP_POST, 0);
1481 wwo->dwPlayedTotal = wwo->dwWrittenTotal - notplayed;
1482 return TRUE;
1485 /**************************************************************************
1486 * wodPlayer_BeginWaveHdr [internal]
1488 * Makes the specified lpWaveHdr the currently playing wave header.
1489 * If the specified wave header is a begin loop and we're not already in
1490 * a loop, setup the loop.
1492 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1494 wwo->lpPlayPtr = lpWaveHdr;
1496 if (!lpWaveHdr) return;
1498 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1499 if (wwo->lpLoopPtr) {
1500 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1501 } else {
1502 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1503 wwo->lpLoopPtr = lpWaveHdr;
1504 /* Windows does not touch WAVEHDR.dwLoops,
1505 * so we need to make an internal copy */
1506 wwo->dwLoops = lpWaveHdr->dwLoops;
1509 wwo->dwPartialOffset = 0;
1512 /**************************************************************************
1513 * wodPlayer_PlayPtrNext [internal]
1515 * Advance the play pointer to the next waveheader, looping if required.
1517 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
1519 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1521 wwo->dwPartialOffset = 0;
1522 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1523 /* We're at the end of a loop, loop if required */
1524 if (--wwo->dwLoops > 0) {
1525 wwo->lpPlayPtr = wwo->lpLoopPtr;
1526 } else {
1527 /* Handle overlapping loops correctly */
1528 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1529 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1530 /* shall we consider the END flag for the closing loop or for
1531 * the opening one or for both ???
1532 * code assumes for closing loop only
1534 } else {
1535 lpWaveHdr = lpWaveHdr->lpNext;
1537 wwo->lpLoopPtr = NULL;
1538 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1540 } else {
1541 /* We're not in a loop. Advance to the next wave header */
1542 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1545 return lpWaveHdr;
1548 /**************************************************************************
1549 * wodPlayer_TicksTillEmpty [internal]
1550 * Returns the number of ticks until we think the DSP should be empty
1552 static DWORD wodPlayer_TicksTillEmpty(const WINE_WAVEOUT *wwo)
1554 return ((wwo->dwWrittenTotal - wwo->dwPlayedTotal) * 1000)
1555 / wwo->waveFormat.Format.nAvgBytesPerSec;
1558 /**************************************************************************
1559 * wodPlayer_DSPWait [internal]
1560 * Returns the number of milliseconds to wait for the DSP buffer to write
1561 * one fragment.
1563 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1565 /* time for one fragment to be played */
1566 return wwo->dwFragmentSize * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1569 /**************************************************************************
1570 * wodPlayer_NotifyWait [internal]
1571 * Returns the number of milliseconds to wait before attempting to notify
1572 * completion of the specified wavehdr.
1573 * This is based on the number of bytes remaining to be written in the
1574 * wave.
1576 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1578 DWORD dwMillis;
1580 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1581 dwMillis = 1;
1582 } else {
1583 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1584 if (!dwMillis) dwMillis = 1;
1587 return dwMillis;
1591 /**************************************************************************
1592 * wodPlayer_WriteMaxFrags [internal]
1593 * Writes the maximum number of bytes possible to the DSP and returns
1594 * TRUE iff the current playPtr has been fully played
1596 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
1598 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1599 DWORD toWrite = min(dwLength, *bytes);
1600 int written;
1601 BOOL ret = FALSE;
1603 TRACE("Writing wavehdr %p.%u[%u]/%u\n",
1604 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
1606 if (toWrite > 0)
1608 written = write(wwo->ossdev.fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
1609 if (written <= 0) {
1610 TRACE("write(%s, %p, %d) failed (%s) returned %d\n", wwo->ossdev.dev_name,
1611 wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite, strerror(errno), written);
1612 return FALSE;
1615 else
1616 written = 0;
1618 if (written >= dwLength) {
1619 /* If we wrote all current wavehdr, skip to the next one */
1620 wodPlayer_PlayPtrNext(wwo);
1621 ret = TRUE;
1622 } else {
1623 /* Remove the amount written */
1624 wwo->dwPartialOffset += written;
1626 *bytes -= written;
1627 wwo->dwWrittenTotal += written;
1628 TRACE("dwWrittenTotal=%u\n", wwo->dwWrittenTotal);
1629 return ret;
1633 /**************************************************************************
1634 * wodPlayer_NotifyCompletions [internal]
1636 * Notifies and remove from queue all wavehdrs which have been played to
1637 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1638 * we notify all wavehdrs and remove them all from the queue even if they
1639 * are unplayed or part of a loop.
1641 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1643 LPWAVEHDR lpWaveHdr;
1645 /* Start from lpQueuePtr and keep notifying until:
1646 * - we hit an unwritten wavehdr
1647 * - we hit the beginning of a running loop
1648 * - we hit a wavehdr which hasn't finished playing
1650 #if 0
1651 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1652 (force ||
1653 (lpWaveHdr != wwo->lpPlayPtr &&
1654 lpWaveHdr != wwo->lpLoopPtr &&
1655 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1657 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1659 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1660 lpWaveHdr->dwFlags |= WHDR_DONE;
1662 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1664 #else
1665 for (;;)
1667 lpWaveHdr = wwo->lpQueuePtr;
1668 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
1669 if (!force)
1671 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
1672 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
1673 if (lpWaveHdr->reserved > wwo->dwPlayedTotal) {TRACE("still playing %p (%lu/%u)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
1675 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1677 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1678 lpWaveHdr->dwFlags |= WHDR_DONE;
1680 wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
1682 #endif
1683 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1684 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1687 /**************************************************************************
1688 * wodPlayer_Reset [internal]
1690 * wodPlayer helper. Resets current output stream.
1692 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
1694 wodUpdatePlayedTotal(wwo, NULL);
1695 /* updates current notify list */
1696 wodPlayer_NotifyCompletions(wwo, FALSE);
1698 /* flush all possible output */
1699 if (OSS_ResetDevice(&wwo->ossdev) != MMSYSERR_NOERROR)
1701 wwo->hThread = 0;
1702 wwo->state = WINE_WS_STOPPED;
1703 ExitThread(-1);
1706 if (reset) {
1707 enum win_wm_message msg;
1708 DWORD_PTR param;
1709 HANDLE ev;
1711 /* remove any buffer */
1712 wodPlayer_NotifyCompletions(wwo, TRUE);
1714 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1715 wwo->state = WINE_WS_STOPPED;
1716 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1717 /* Clear partial wavehdr */
1718 wwo->dwPartialOffset = 0;
1720 /* remove any existing message in the ring */
1721 EnterCriticalSection(&wwo->msgRing.msg_crst);
1722 /* return all pending headers in queue */
1723 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1725 if (msg != WINE_WM_HEADER)
1727 FIXME("shouldn't have headers left\n");
1728 SetEvent(ev);
1729 continue;
1731 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1732 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1734 wodNotifyClient(wwo, WOM_DONE, param, 0);
1736 RESET_OMR(&wwo->msgRing);
1737 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1738 } else {
1739 if (wwo->lpLoopPtr) {
1740 /* complicated case, not handled yet (could imply modifying the loop counter */
1741 FIXME("Pausing while in loop isn't correctly handled yet, expect strange results\n");
1742 wwo->lpPlayPtr = wwo->lpLoopPtr;
1743 wwo->dwPartialOffset = 0;
1744 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1745 } else {
1746 LPWAVEHDR ptr;
1747 DWORD sz = wwo->dwPartialOffset;
1749 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1750 /* compute the max size playable from lpQueuePtr */
1751 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1752 sz += ptr->dwBufferLength;
1754 /* because the reset lpPlayPtr will be lpQueuePtr */
1755 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1756 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1757 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1758 wwo->lpPlayPtr = wwo->lpQueuePtr;
1760 wwo->state = WINE_WS_PAUSED;
1764 /**************************************************************************
1765 * wodPlayer_ProcessMessages [internal]
1767 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1769 LPWAVEHDR lpWaveHdr;
1770 enum win_wm_message msg;
1771 DWORD_PTR param;
1772 HANDLE ev;
1774 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1775 TRACE("Received %s %lx\n", getCmdString(msg), param);
1776 switch (msg) {
1777 case WINE_WM_PAUSING:
1778 wodPlayer_Reset(wwo, FALSE);
1779 SetEvent(ev);
1780 break;
1781 case WINE_WM_RESTARTING:
1782 if (wwo->state == WINE_WS_PAUSED)
1784 wwo->state = WINE_WS_PLAYING;
1786 SetEvent(ev);
1787 break;
1788 case WINE_WM_HEADER:
1789 lpWaveHdr = (LPWAVEHDR)param;
1791 /* insert buffer at the end of queue */
1793 LPWAVEHDR* wh;
1794 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1795 *wh = lpWaveHdr;
1797 if (!wwo->lpPlayPtr)
1798 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1799 if (wwo->state == WINE_WS_STOPPED)
1800 wwo->state = WINE_WS_PLAYING;
1801 break;
1802 case WINE_WM_RESETTING:
1803 wodPlayer_Reset(wwo, TRUE);
1804 SetEvent(ev);
1805 break;
1806 case WINE_WM_UPDATE:
1807 wodUpdatePlayedTotal(wwo, NULL);
1808 SetEvent(ev);
1809 break;
1810 case WINE_WM_BREAKLOOP:
1811 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1812 /* ensure exit at end of current loop */
1813 wwo->dwLoops = 1;
1815 SetEvent(ev);
1816 break;
1817 case WINE_WM_CLOSING:
1818 /* sanity check: this should not happen since the device must have been reset before */
1819 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1820 wwo->hThread = 0;
1821 wwo->state = WINE_WS_CLOSED;
1822 SetEvent(ev);
1823 ExitThread(0);
1824 /* shouldn't go here */
1825 default:
1826 FIXME("unknown message %d\n", msg);
1827 break;
1832 /**************************************************************************
1833 * wodPlayer_FeedDSP [internal]
1834 * Feed as much sound data as we can into the DSP and return the number of
1835 * milliseconds before it will be necessary to feed the DSP again.
1837 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1839 audio_buf_info dspspace;
1840 DWORD availInQ;
1842 if (!wodUpdatePlayedTotal(wwo, &dspspace)) return INFINITE;
1843 availInQ = dspspace.bytes;
1844 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1845 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1847 /* no more room... no need to try to feed */
1848 if (dspspace.fragments != 0) {
1849 /* Feed from partial wavehdr */
1850 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1851 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1854 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1855 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1856 do {
1857 TRACE("Setting time to elapse for %p to %u\n",
1858 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1859 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1860 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1861 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1864 if (wwo->bNeedPost) {
1865 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1866 * if it didn't get one, we give it the other */
1867 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1868 ioctl(wwo->ossdev.fd, SNDCTL_DSP_POST, 0);
1869 wwo->bNeedPost = FALSE;
1873 return wodPlayer_DSPWait(wwo);
1877 /**************************************************************************
1878 * wodPlayer [internal]
1880 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1882 WORD uDevID = (DWORD_PTR)pmt;
1883 WINE_WAVEOUT* wwo = &WOutDev[uDevID];
1884 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1885 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1886 DWORD dwSleepTime;
1888 wwo->state = WINE_WS_STOPPED;
1889 SetEvent(wwo->hStartUpEvent);
1891 for (;;) {
1892 /** Wait for the shortest time before an action is required. If there
1893 * are no pending actions, wait forever for a command.
1895 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1896 TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1897 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1898 wodPlayer_ProcessMessages(wwo);
1899 if (wwo->state == WINE_WS_PLAYING) {
1900 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1901 if (dwNextFeedTime != INFINITE)
1902 wwo->dwProjectedFinishTime = GetTickCount() + wodPlayer_TicksTillEmpty(wwo);
1903 else
1904 wwo->dwProjectedFinishTime = 0;
1906 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1907 if (dwNextFeedTime == INFINITE) {
1908 /* FeedDSP ran out of data, but before flushing, */
1909 /* check that a notification didn't give us more */
1910 wodPlayer_ProcessMessages(wwo);
1911 if (!wwo->lpPlayPtr) {
1912 TRACE("flushing\n");
1913 ioctl(wwo->ossdev.fd, SNDCTL_DSP_SYNC, 0);
1914 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1915 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1916 } else {
1917 TRACE("recovering\n");
1918 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1921 } else {
1922 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1926 return 0;
1929 /**************************************************************************
1930 * wodGetDevCaps [internal]
1932 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1934 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1936 if (lpCaps == NULL) {
1937 WARN("not enabled\n");
1938 return MMSYSERR_NOTENABLED;
1941 if (wDevID >= numOutDev) {
1942 WARN("numOutDev reached !\n");
1943 return MMSYSERR_BADDEVICEID;
1946 if (WOutDev[wDevID].ossdev.open_access == O_RDWR)
1947 memcpy(lpCaps, &WOutDev[wDevID].ossdev.duplex_out_caps, min(dwSize, sizeof(*lpCaps)));
1948 else
1949 memcpy(lpCaps, &WOutDev[wDevID].ossdev.out_caps, min(dwSize, sizeof(*lpCaps)));
1951 return MMSYSERR_NOERROR;
1954 /**************************************************************************
1955 * wodOpen [internal]
1957 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1959 int audio_fragment;
1960 WINE_WAVEOUT* wwo;
1961 audio_buf_info info;
1962 DWORD ret;
1964 TRACE("(%u, %p[cb=%08lx], %08X);\n", wDevID, lpDesc, lpDesc->dwCallback, dwFlags);
1965 if (lpDesc == NULL) {
1966 WARN("Invalid Parameter !\n");
1967 return MMSYSERR_INVALPARAM;
1969 if (wDevID >= numOutDev) {
1970 TRACE("MAX_WAVOUTDRV reached !\n");
1971 return MMSYSERR_BADDEVICEID;
1974 /* only PCM format is supported so far... */
1975 if (!supportedFormat(lpDesc->lpFormat)) {
1976 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1977 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1978 lpDesc->lpFormat->nSamplesPerSec);
1979 return WAVERR_BADFORMAT;
1982 if (dwFlags & WAVE_FORMAT_QUERY) {
1983 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1984 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1985 lpDesc->lpFormat->nSamplesPerSec);
1986 return MMSYSERR_NOERROR;
1989 /* nBlockAlign and nAvgBytesPerSec are output variables for dsound */
1990 if (lpDesc->lpFormat->nBlockAlign != lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8) {
1991 lpDesc->lpFormat->nBlockAlign = lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
1992 WARN("Fixing nBlockAlign\n");
1994 if (lpDesc->lpFormat->nAvgBytesPerSec!= lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign) {
1995 lpDesc->lpFormat->nAvgBytesPerSec = lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
1996 WARN("Fixing nAvgBytesPerSec\n");
1999 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2000 lpDesc->lpFormat->nSamplesPerSec,
2001 lpDesc->lpFormat->wBitsPerSample,
2002 lpDesc->lpFormat->nChannels,
2003 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
2004 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
2005 "UNSUPPORTED");
2007 wwo = &WOutDev[wDevID];
2009 if ((dwFlags & WAVE_DIRECTSOUND) &&
2010 !(wwo->ossdev.duplex_out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
2011 /* not supported, ignore it */
2012 dwFlags &= ~WAVE_DIRECTSOUND;
2014 if (dwFlags & WAVE_DIRECTSOUND) {
2015 if (wwo->ossdev.duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2016 /* we have realtime DirectSound, fragments just waste our time,
2017 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2018 audio_fragment = 0x0020000B;
2019 else
2020 /* to approximate realtime, we must use small fragments,
2021 * let's try to fragment the above 64KB (256 * 2^8) */
2022 audio_fragment = 0x01000008;
2023 } else {
2024 /* A wave device must have a worst case latency of 10 ms so calculate
2025 * the largest fragment size less than 10 ms long.
2027 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2028 int shift = 0;
2029 while ((1 << shift) <= fsize)
2030 shift++;
2031 shift--;
2032 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2035 TRACE("requesting %d %d byte fragments (%d ms/fragment)\n",
2036 audio_fragment >> 16, 1 << (audio_fragment & 0xffff),
2037 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2039 if (wwo->state != WINE_WS_CLOSED) {
2040 WARN("already allocated\n");
2041 return MMSYSERR_ALLOCATED;
2044 /* we want to be able to mmap() the device, which means it must be opened readable,
2045 * otherwise mmap() will fail (at least under Linux) */
2046 ret = OSS_OpenDevice(&wwo->ossdev,
2047 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
2048 &audio_fragment,
2049 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
2050 lpDesc->lpFormat->nSamplesPerSec,
2051 lpDesc->lpFormat->nChannels,
2052 (lpDesc->lpFormat->wBitsPerSample == 16)
2053 ? AFMT_S16_LE : AFMT_U8);
2054 if ((ret==MMSYSERR_NOERROR) && (dwFlags & WAVE_DIRECTSOUND)) {
2055 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev.sample_rate;
2056 lpDesc->lpFormat->nChannels=wwo->ossdev.channels;
2057 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev.format == AFMT_U8 ? 8 : 16);
2058 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
2059 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
2060 TRACE("OSS_OpenDevice returned this format: %dx%dx%d\n",
2061 lpDesc->lpFormat->nSamplesPerSec,
2062 lpDesc->lpFormat->wBitsPerSample,
2063 lpDesc->lpFormat->nChannels);
2065 if (ret != 0) return ret;
2066 wwo->state = WINE_WS_STOPPED;
2068 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2070 wwo->waveDesc = *lpDesc;
2071 copy_format(lpDesc->lpFormat, &wwo->waveFormat);
2073 /* Read output space info for future reference */
2074 if (ioctl(wwo->ossdev.fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2075 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev.dev_name, strerror(errno));
2076 OSS_CloseDevice(&wwo->ossdev);
2077 wwo->state = WINE_WS_CLOSED;
2078 return MMSYSERR_NOTENABLED;
2081 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2082 info.fragsize, (info.fragsize * 1000) / (wwo->ossdev.sample_rate *
2083 wwo->ossdev.channels * (wwo->ossdev.format == AFMT_U8 ? 1 : 2)));
2085 /* Check that fragsize is correct per our settings above */
2086 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
2087 /* we've tried to set 1K fragments or less, but it didn't work */
2088 WARN("fragment size set failed, size is now %d\n", info.fragsize);
2091 /* Remember fragsize and total buffer size for future use */
2092 wwo->dwFragmentSize = info.fragsize;
2093 wwo->dwBufferSize = info.fragstotal * info.fragsize;
2094 wwo->dwPlayedTotal = 0;
2095 wwo->dwWrittenTotal = 0;
2096 wwo->bNeedPost = TRUE;
2098 TRACE("fd=%d fragstotal=%d fragsize=%d BufferSize=%d\n",
2099 wwo->ossdev.fd, info.fragstotal, info.fragsize, wwo->dwBufferSize);
2100 if (wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign) {
2101 ERR("Fragment doesn't contain an integral number of data blocks fragsize=%d BlockAlign=%d\n",wwo->dwFragmentSize,wwo->waveFormat.Format.nBlockAlign);
2102 /* Some SoundBlaster 16 cards return an incorrect (odd) fragment
2103 * size for 16 bit sound. This will cause a system crash when we try
2104 * to write just the specified odd number of bytes. So if we
2105 * detect something is wrong we'd better fix it.
2107 wwo->dwFragmentSize-=wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign;
2110 OSS_InitRingMessage(&wwo->msgRing);
2112 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2113 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwo->dwThreadID));
2114 if (wwo->hThread)
2115 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2116 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
2117 CloseHandle(wwo->hStartUpEvent);
2118 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
2120 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2121 wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
2122 wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
2123 wwo->waveFormat.Format.nBlockAlign);
2125 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
2128 /**************************************************************************
2129 * wodClose [internal]
2131 static DWORD wodClose(WORD wDevID)
2133 DWORD ret = MMSYSERR_NOERROR;
2134 WINE_WAVEOUT* wwo;
2136 TRACE("(%u);\n", wDevID);
2138 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2139 WARN("bad device ID !\n");
2140 return MMSYSERR_BADDEVICEID;
2143 wwo = &WOutDev[wDevID];
2144 if (wwo->lpQueuePtr) {
2145 WARN("buffers still playing !\n");
2146 ret = WAVERR_STILLPLAYING;
2147 } else {
2148 if (wwo->hThread != INVALID_HANDLE_VALUE) {
2149 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
2152 OSS_DestroyRingMessage(&wwo->msgRing);
2154 OSS_CloseDevice(&wwo->ossdev);
2155 wwo->state = WINE_WS_CLOSED;
2156 wwo->dwFragmentSize = 0;
2157 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
2159 return ret;
2162 /**************************************************************************
2163 * wodWrite [internal]
2166 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2168 WORD delta;
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 delta = lpWaveHdr->dwBufferLength % WOutDev[wDevID].waveFormat.Format.nBlockAlign;
2188 if (delta != 0)
2190 WARN("WaveHdr length isn't a multiple of the PCM block size: %d %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].waveFormat.Format.nBlockAlign);
2191 lpWaveHdr->dwBufferLength -= delta;
2194 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
2196 return MMSYSERR_NOERROR;
2199 /**************************************************************************
2200 * wodPause [internal]
2202 static DWORD wodPause(WORD wDevID)
2204 TRACE("(%u);!\n", wDevID);
2206 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2207 WARN("bad device ID !\n");
2208 return MMSYSERR_BADDEVICEID;
2211 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
2213 return MMSYSERR_NOERROR;
2216 /**************************************************************************
2217 * wodRestart [internal]
2219 static DWORD wodRestart(WORD wDevID)
2221 TRACE("(%u);\n", wDevID);
2223 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2224 WARN("bad device ID !\n");
2225 return MMSYSERR_BADDEVICEID;
2228 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2230 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2231 /* FIXME: Myst crashes with this ... hmm -MM
2232 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2235 return MMSYSERR_NOERROR;
2238 /**************************************************************************
2239 * wodReset [internal]
2241 static DWORD wodReset(WORD wDevID)
2243 TRACE("(%u);\n", wDevID);
2245 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2246 WARN("bad device ID !\n");
2247 return MMSYSERR_BADDEVICEID;
2250 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2252 return MMSYSERR_NOERROR;
2255 /**************************************************************************
2256 * wodGetPosition [internal]
2258 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2260 WINE_WAVEOUT* wwo;
2262 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
2264 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2265 WARN("bad device ID !\n");
2266 return MMSYSERR_BADDEVICEID;
2269 if (lpTime == NULL) {
2270 WARN("invalid parameter: lpTime == NULL\n");
2271 return MMSYSERR_INVALPARAM;
2274 wwo = &WOutDev[wDevID];
2275 #ifdef EXACT_WODPOSITION
2276 if (wwo->ossdev.open_access == O_RDWR) {
2277 if (wwo->ossdev.duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2278 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2279 } else {
2280 if (wwo->ossdev.out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2281 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2283 #endif
2285 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
2288 /**************************************************************************
2289 * wodBreakLoop [internal]
2291 static DWORD wodBreakLoop(WORD wDevID)
2293 TRACE("(%u);\n", wDevID);
2295 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2296 WARN("bad device ID !\n");
2297 return MMSYSERR_BADDEVICEID;
2299 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
2300 return MMSYSERR_NOERROR;
2303 /**************************************************************************
2304 * wodGetVolume [internal]
2306 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
2308 int mixer;
2309 int volume;
2310 DWORD left, right;
2311 DWORD last_left, last_right;
2313 TRACE("(%u, %p);\n", wDevID, lpdwVol);
2315 if (lpdwVol == NULL) {
2316 WARN("not enabled\n");
2317 return MMSYSERR_NOTENABLED;
2319 if (wDevID >= numOutDev) {
2320 WARN("invalid parameter\n");
2321 return MMSYSERR_INVALPARAM;
2323 if (WOutDev[wDevID].ossdev.open_access == O_RDWR) {
2324 if (!(WOutDev[wDevID].ossdev.duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2325 TRACE("Volume not supported\n");
2326 return MMSYSERR_NOTSUPPORTED;
2328 } else {
2329 if (!(WOutDev[wDevID].ossdev.out_caps.dwSupport & WAVECAPS_VOLUME)) {
2330 TRACE("Volume not supported\n");
2331 return MMSYSERR_NOTSUPPORTED;
2335 if ((mixer = open(WOutDev[wDevID].ossdev.mixer_name, O_RDONLY|O_NDELAY)) < 0) {
2336 WARN("mixer device not available !\n");
2337 return MMSYSERR_NOTENABLED;
2339 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
2340 close(mixer);
2341 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)\n",
2342 WOutDev[wDevID].ossdev.mixer_name, strerror(errno));
2343 return MMSYSERR_NOTENABLED;
2345 close(mixer);
2347 left = LOBYTE(volume);
2348 right = HIBYTE(volume);
2349 TRACE("left=%d right=%d !\n", left, right);
2350 last_left = (LOWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2351 last_right = (HIWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2352 TRACE("last_left=%d last_right=%d !\n", last_left, last_right);
2353 if (last_left == left && last_right == right)
2354 *lpdwVol = WOutDev[wDevID].volume;
2355 else
2356 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
2357 return MMSYSERR_NOERROR;
2360 /**************************************************************************
2361 * wodSetVolume [internal]
2363 DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
2365 int mixer;
2366 int volume;
2367 DWORD left, right;
2369 TRACE("(%u, %08X);\n", wDevID, dwParam);
2371 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
2372 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
2373 volume = left + (right << 8);
2375 if (wDevID >= numOutDev) {
2376 WARN("invalid parameter: wDevID > %d\n", numOutDev);
2377 return MMSYSERR_INVALPARAM;
2379 if (WOutDev[wDevID].ossdev.open_access == O_RDWR) {
2380 if (!(WOutDev[wDevID].ossdev.duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2381 TRACE("Volume not supported\n");
2382 return MMSYSERR_NOTSUPPORTED;
2384 } else {
2385 if (!(WOutDev[wDevID].ossdev.out_caps.dwSupport & WAVECAPS_VOLUME)) {
2386 TRACE("Volume not supported\n");
2387 return MMSYSERR_NOTSUPPORTED;
2390 if ((mixer = open(WOutDev[wDevID].ossdev.mixer_name, O_WRONLY|O_NDELAY)) < 0) {
2391 WARN("open(%s) failed (%s)\n", WOutDev[wDevID].ossdev.mixer_name, strerror(errno));
2392 return MMSYSERR_NOTENABLED;
2394 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
2395 close(mixer);
2396 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n",
2397 WOutDev[wDevID].ossdev.mixer_name, strerror(errno));
2398 return MMSYSERR_NOTENABLED;
2400 TRACE("volume=%04x\n", (unsigned)volume);
2401 close(mixer);
2403 /* save requested volume */
2404 WOutDev[wDevID].volume = dwParam;
2406 return MMSYSERR_NOERROR;
2409 /**************************************************************************
2410 * wodMessage (WINEOSS.7)
2412 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
2413 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2415 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
2416 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
2418 switch (wMsg) {
2419 case DRVM_INIT:
2420 case DRVM_EXIT:
2421 case DRVM_ENABLE:
2422 case DRVM_DISABLE:
2423 /* FIXME: Pretend this is supported */
2424 return 0;
2425 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2426 case WODM_CLOSE: return wodClose (wDevID);
2427 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2428 case WODM_PAUSE: return wodPause (wDevID);
2429 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
2430 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
2431 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2432 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2433 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
2434 case WODM_GETNUMDEVS: return numOutDev;
2435 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
2436 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
2437 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2438 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2439 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
2440 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
2441 case WODM_RESTART: return wodRestart (wDevID);
2442 case WODM_RESET: return wodReset (wDevID);
2444 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2445 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2446 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
2447 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2448 default:
2449 FIXME("unknown message %d!\n", wMsg);
2451 return MMSYSERR_NOTSUPPORTED;
2454 /*======================================================================*
2455 * Low level WAVE IN implementation *
2456 *======================================================================*/
2458 /**************************************************************************
2459 * widNotifyClient [internal]
2461 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2463 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lx dwParam2 = %04lx\n", wMsg,
2464 wMsg == WIM_OPEN ? "WIM_OPEN" : wMsg == WIM_CLOSE ? "WIM_CLOSE" :
2465 wMsg == WIM_DATA ? "WIM_DATA" : "Unknown", dwParam1, dwParam2);
2467 switch (wMsg) {
2468 case WIM_OPEN:
2469 case WIM_CLOSE:
2470 case WIM_DATA:
2471 if (wwi->wFlags != DCB_NULL &&
2472 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2473 (HDRVR)wwi->waveDesc.hWave, wMsg,
2474 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2475 WARN("can't notify client !\n");
2476 return MMSYSERR_ERROR;
2478 break;
2479 default:
2480 FIXME("Unknown callback message %u\n", wMsg);
2481 return MMSYSERR_INVALPARAM;
2483 return MMSYSERR_NOERROR;
2486 /**************************************************************************
2487 * widGetDevCaps [internal]
2489 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
2491 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
2493 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2495 if (wDevID >= numInDev) {
2496 TRACE("numOutDev reached !\n");
2497 return MMSYSERR_BADDEVICEID;
2500 memcpy(lpCaps, &WInDev[wDevID].ossdev.in_caps, min(dwSize, sizeof(*lpCaps)));
2501 return MMSYSERR_NOERROR;
2504 /**************************************************************************
2505 * widRecorder_ReadHeaders [internal]
2507 static void widRecorder_ReadHeaders(WINE_WAVEIN * wwi)
2509 enum win_wm_message tmp_msg;
2510 DWORD_PTR tmp_param;
2511 HANDLE tmp_ev;
2512 WAVEHDR* lpWaveHdr;
2514 while (OSS_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
2515 if (tmp_msg == WINE_WM_HEADER) {
2516 LPWAVEHDR* wh;
2517 lpWaveHdr = (LPWAVEHDR)tmp_param;
2518 lpWaveHdr->lpNext = 0;
2520 if (wwi->lpQueuePtr == 0)
2521 wwi->lpQueuePtr = lpWaveHdr;
2522 else {
2523 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2524 *wh = lpWaveHdr;
2526 } else {
2527 ERR("should only have headers left\n");
2532 /**************************************************************************
2533 * widRecorder [internal]
2535 static DWORD CALLBACK widRecorder(LPVOID pmt)
2537 WORD uDevID = (DWORD_PTR)pmt;
2538 WINE_WAVEIN* wwi = &WInDev[uDevID];
2539 WAVEHDR* lpWaveHdr;
2540 DWORD dwSleepTime;
2541 DWORD bytesRead;
2542 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2543 char *pOffset = buffer;
2544 audio_buf_info info;
2545 int xs;
2546 enum win_wm_message msg;
2547 DWORD_PTR param;
2548 HANDLE ev;
2549 int enable;
2551 wwi->state = WINE_WS_STOPPED;
2552 wwi->dwTotalRecorded = 0;
2553 wwi->dwTotalRead = 0;
2554 wwi->lpQueuePtr = NULL;
2556 SetEvent(wwi->hStartUpEvent);
2558 /* disable input so capture will begin when triggered */
2559 wwi->ossdev.bInputEnabled = FALSE;
2560 enable = getEnables(&wwi->ossdev);
2561 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2562 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2564 /* the soundblaster live needs a micro wake to get its recording started
2565 * (or GETISPACE will have 0 frags all the time)
2567 read(wwi->ossdev.fd, &xs, 4);
2569 /* make sleep time to be # of ms to output a fragment */
2570 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
2571 TRACE("sleeptime=%d ms\n", dwSleepTime);
2573 for (;;) {
2574 /* wait for dwSleepTime or an event in thread's queue */
2575 /* FIXME: could improve wait time depending on queue state,
2576 * ie, number of queued fragments
2579 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2581 lpWaveHdr = wwi->lpQueuePtr;
2583 ioctl(wwi->ossdev.fd, SNDCTL_DSP_GETISPACE, &info);
2584 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2586 /* read all the fragments accumulated so far */
2587 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2589 info.fragments --;
2591 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2593 /* directly read fragment in wavehdr */
2594 bytesRead = read(wwi->ossdev.fd,
2595 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2596 wwi->dwFragmentSize);
2598 TRACE("bytesRead=%d (direct)\n", bytesRead);
2599 if (bytesRead != (DWORD) -1)
2601 /* update number of bytes recorded in current buffer and by this device */
2602 lpWaveHdr->dwBytesRecorded += bytesRead;
2603 wwi->dwTotalRead += bytesRead;
2604 wwi->dwTotalRecorded = wwi->dwTotalRead;
2606 /* buffer is full. notify client */
2607 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2609 /* must copy the value of next waveHdr, because we have no idea of what
2610 * will be done with the content of lpWaveHdr in callback
2612 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2614 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2615 lpWaveHdr->dwFlags |= WHDR_DONE;
2617 wwi->lpQueuePtr = lpNext;
2618 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2619 lpWaveHdr = lpNext;
2621 } else {
2622 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev.dev_name,
2623 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2624 wwi->dwFragmentSize, strerror(errno));
2627 else
2629 /* read the fragment in a local buffer */
2630 bytesRead = read(wwi->ossdev.fd, buffer, wwi->dwFragmentSize);
2631 pOffset = buffer;
2633 TRACE("bytesRead=%d (local)\n", bytesRead);
2635 if (bytesRead == (DWORD) -1) {
2636 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev.dev_name,
2637 buffer, wwi->dwFragmentSize, strerror(errno));
2638 continue;
2641 /* copy data in client buffers */
2642 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2644 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2646 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2647 pOffset,
2648 dwToCopy);
2650 /* update number of bytes recorded in current buffer and by this device */
2651 lpWaveHdr->dwBytesRecorded += dwToCopy;
2652 wwi->dwTotalRead += dwToCopy;
2653 wwi->dwTotalRecorded = wwi->dwTotalRead;
2654 bytesRead -= dwToCopy;
2655 pOffset += dwToCopy;
2657 /* client buffer is full. notify client */
2658 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2660 /* must copy the value of next waveHdr, because we have no idea of what
2661 * will be done with the content of lpWaveHdr in callback
2663 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2664 TRACE("lpNext=%p\n", lpNext);
2666 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2667 lpWaveHdr->dwFlags |= WHDR_DONE;
2669 wwi->lpQueuePtr = lpNext;
2670 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2672 lpWaveHdr = lpNext;
2673 if (!lpNext && bytesRead) {
2674 /* before we give up, check for more header messages */
2675 while (OSS_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
2677 if (msg == WINE_WM_HEADER) {
2678 LPWAVEHDR hdr;
2679 OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
2680 hdr = ((LPWAVEHDR)param);
2681 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg), hdr, ev);
2682 hdr->lpNext = 0;
2683 if (lpWaveHdr == 0) {
2684 /* new head of queue */
2685 wwi->lpQueuePtr = lpWaveHdr = hdr;
2686 } else {
2687 /* insert buffer at the end of queue */
2688 LPWAVEHDR* wh;
2689 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2690 *wh = hdr;
2692 } else
2693 break;
2696 if (lpWaveHdr == 0) {
2697 /* no more buffer to copy data to, but we did read more.
2698 * what hasn't been copied will be dropped
2700 WARN("buffer under run! %u bytes dropped.\n", bytesRead);
2701 wwi->lpQueuePtr = NULL;
2702 break;
2711 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2713 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2715 TRACE("msg=%s param=0x%lx\n", getCmdString(msg), param);
2716 switch (msg) {
2717 case WINE_WM_PAUSING:
2718 wwi->state = WINE_WS_PAUSED;
2719 /*FIXME("Device should stop recording\n");*/
2720 SetEvent(ev);
2721 break;
2722 case WINE_WM_STARTING:
2723 wwi->state = WINE_WS_PLAYING;
2725 if (wwi->ossdev.bTriggerSupport)
2727 /* start the recording */
2728 wwi->ossdev.bInputEnabled = TRUE;
2729 enable = getEnables(&wwi->ossdev);
2730 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2731 wwi->ossdev.bInputEnabled = FALSE;
2732 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2735 else
2737 unsigned char data[4];
2738 /* read 4 bytes to start the recording */
2739 read(wwi->ossdev.fd, data, 4);
2742 SetEvent(ev);
2743 break;
2744 case WINE_WM_HEADER:
2745 lpWaveHdr = (LPWAVEHDR)param;
2746 lpWaveHdr->lpNext = 0;
2748 /* insert buffer at the end of queue */
2750 LPWAVEHDR* wh;
2751 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2752 *wh = lpWaveHdr;
2754 break;
2755 case WINE_WM_STOPPING:
2756 if (wwi->state != WINE_WS_STOPPED)
2758 if (wwi->ossdev.bTriggerSupport)
2760 /* stop the recording */
2761 wwi->ossdev.bInputEnabled = FALSE;
2762 enable = getEnables(&wwi->ossdev);
2763 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2764 wwi->ossdev.bInputEnabled = FALSE;
2765 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2769 /* read any headers in queue */
2770 widRecorder_ReadHeaders(wwi);
2772 /* return current buffer to app */
2773 lpWaveHdr = wwi->lpQueuePtr;
2774 if (lpWaveHdr)
2776 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2777 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2778 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2779 lpWaveHdr->dwFlags |= WHDR_DONE;
2780 wwi->lpQueuePtr = lpNext;
2781 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2784 wwi->state = WINE_WS_STOPPED;
2785 SetEvent(ev);
2786 break;
2787 case WINE_WM_RESETTING:
2788 if (wwi->state != WINE_WS_STOPPED)
2790 if (wwi->ossdev.bTriggerSupport)
2792 /* stop the recording */
2793 wwi->ossdev.bInputEnabled = FALSE;
2794 enable = getEnables(&wwi->ossdev);
2795 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2796 wwi->ossdev.bInputEnabled = FALSE;
2797 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2801 wwi->state = WINE_WS_STOPPED;
2802 wwi->dwTotalRecorded = 0;
2803 wwi->dwTotalRead = 0;
2805 /* read any headers in queue */
2806 widRecorder_ReadHeaders(wwi);
2808 /* return all buffers to the app */
2809 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2810 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2811 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2812 lpWaveHdr->dwFlags |= WHDR_DONE;
2813 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2814 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2817 wwi->lpQueuePtr = NULL;
2818 SetEvent(ev);
2819 break;
2820 case WINE_WM_UPDATE:
2821 if (wwi->state == WINE_WS_PLAYING) {
2822 audio_buf_info tmp_info;
2823 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_GETISPACE, &tmp_info) < 0)
2824 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2825 else
2826 wwi->dwTotalRecorded = wwi->dwTotalRead + tmp_info.bytes;
2828 SetEvent(ev);
2829 break;
2830 case WINE_WM_CLOSING:
2831 wwi->hThread = 0;
2832 wwi->state = WINE_WS_CLOSED;
2833 SetEvent(ev);
2834 HeapFree(GetProcessHeap(), 0, buffer);
2835 ExitThread(0);
2836 /* shouldn't go here */
2837 default:
2838 FIXME("unknown message %d\n", msg);
2839 break;
2843 ExitThread(0);
2844 /* just for not generating compilation warnings... should never be executed */
2845 return 0;
2849 /**************************************************************************
2850 * widOpen [internal]
2852 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2854 WINE_WAVEIN* wwi;
2855 audio_buf_info info;
2856 int audio_fragment;
2857 DWORD ret;
2859 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
2860 if (lpDesc == NULL) {
2861 WARN("Invalid Parameter !\n");
2862 return MMSYSERR_INVALPARAM;
2864 if (wDevID >= numInDev) {
2865 WARN("bad device id: %d >= %d\n", wDevID, numInDev);
2866 return MMSYSERR_BADDEVICEID;
2869 /* only PCM format is supported so far... */
2870 if (!supportedFormat(lpDesc->lpFormat)) {
2871 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2872 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2873 lpDesc->lpFormat->nSamplesPerSec);
2874 return WAVERR_BADFORMAT;
2877 if (dwFlags & WAVE_FORMAT_QUERY) {
2878 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2879 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2880 lpDesc->lpFormat->nSamplesPerSec);
2881 return MMSYSERR_NOERROR;
2884 /* nBlockAlign and nAvgBytesPerSec are output variables for dsound */
2885 if (lpDesc->lpFormat->nBlockAlign != lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8) {
2886 lpDesc->lpFormat->nBlockAlign = lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
2887 WARN("Fixing nBlockAlign\n");
2889 if (lpDesc->lpFormat->nAvgBytesPerSec!= lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign) {
2890 lpDesc->lpFormat->nAvgBytesPerSec = lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
2891 WARN("Fixing nAvgBytesPerSec\n");
2894 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2895 lpDesc->lpFormat->nSamplesPerSec,
2896 lpDesc->lpFormat->wBitsPerSample,
2897 lpDesc->lpFormat->nChannels,
2898 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
2899 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
2900 "UNSUPPORTED");
2902 wwi = &WInDev[wDevID];
2904 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2906 if ((dwFlags & WAVE_DIRECTSOUND) &&
2907 !(wwi->ossdev.in_caps_support & WAVECAPS_DIRECTSOUND))
2908 /* not supported, ignore it */
2909 dwFlags &= ~WAVE_DIRECTSOUND;
2911 if (dwFlags & WAVE_DIRECTSOUND) {
2912 TRACE("has DirectSoundCapture driver\n");
2913 if (wwi->ossdev.in_caps_support & WAVECAPS_SAMPLEACCURATE)
2914 /* we have realtime DirectSound, fragments just waste our time,
2915 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2916 audio_fragment = 0x0020000B;
2917 else
2918 /* to approximate realtime, we must use small fragments,
2919 * let's try to fragment the above 64KB (256 * 2^8) */
2920 audio_fragment = 0x01000008;
2921 } else {
2922 TRACE("doesn't have DirectSoundCapture driver\n");
2923 if (wwi->ossdev.open_count > 0) {
2924 TRACE("Using output device audio_fragment\n");
2925 /* FIXME: This may not be optimal for capture but it allows us
2926 * to do hardware playback without hardware capture. */
2927 audio_fragment = wwi->ossdev.audio_fragment;
2928 } else {
2929 /* A wave device must have a worst case latency of 10 ms so calculate
2930 * the largest fragment size less than 10 ms long.
2932 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2933 int shift = 0;
2934 while ((1 << shift) <= fsize)
2935 shift++;
2936 shift--;
2937 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2941 TRACE("requesting %d %d byte fragments (%d ms)\n", audio_fragment >> 16,
2942 1 << (audio_fragment & 0xffff),
2943 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2945 ret = OSS_OpenDevice(&wwi->ossdev, O_RDONLY, &audio_fragment,
2947 lpDesc->lpFormat->nSamplesPerSec,
2948 lpDesc->lpFormat->nChannels,
2949 (lpDesc->lpFormat->wBitsPerSample == 16)
2950 ? AFMT_S16_LE : AFMT_U8);
2951 if (ret != 0) return ret;
2952 wwi->state = WINE_WS_STOPPED;
2954 if (wwi->lpQueuePtr) {
2955 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2956 wwi->lpQueuePtr = NULL;
2958 wwi->dwTotalRecorded = 0;
2959 wwi->dwTotalRead = 0;
2960 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2962 wwi->waveDesc = *lpDesc;
2963 copy_format(lpDesc->lpFormat, &wwi->waveFormat);
2965 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
2966 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
2967 wwi->ossdev.dev_name, strerror(errno));
2968 OSS_CloseDevice(&wwi->ossdev);
2969 wwi->state = WINE_WS_CLOSED;
2970 return MMSYSERR_NOTENABLED;
2973 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2974 info.fragsize, (info.fragsize * 1000) / (wwi->ossdev.sample_rate *
2975 wwi->ossdev.channels * (wwi->ossdev.format == AFMT_U8 ? 1 : 2)));
2977 wwi->dwFragmentSize = info.fragsize;
2979 TRACE("dwFragmentSize=%u\n", wwi->dwFragmentSize);
2980 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2981 wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
2982 wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
2983 wwi->waveFormat.Format.nBlockAlign);
2985 OSS_InitRingMessage(&wwi->msgRing);
2987 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2988 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwi->dwThreadID));
2989 if (wwi->hThread)
2990 SetThreadPriority(wwi->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2991 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2992 CloseHandle(wwi->hStartUpEvent);
2993 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2995 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2998 /**************************************************************************
2999 * widClose [internal]
3001 static DWORD widClose(WORD wDevID)
3003 WINE_WAVEIN* wwi;
3005 TRACE("(%u);\n", wDevID);
3006 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3007 WARN("can't close !\n");
3008 return MMSYSERR_INVALHANDLE;
3011 wwi = &WInDev[wDevID];
3013 if (wwi->lpQueuePtr != NULL) {
3014 WARN("still buffers open !\n");
3015 return WAVERR_STILLPLAYING;
3018 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
3019 OSS_CloseDevice(&wwi->ossdev);
3020 wwi->state = WINE_WS_CLOSED;
3021 wwi->dwFragmentSize = 0;
3022 OSS_DestroyRingMessage(&wwi->msgRing);
3023 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
3026 /**************************************************************************
3027 * widAddBuffer [internal]
3029 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3031 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
3033 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3034 WARN("can't do it !\n");
3035 return MMSYSERR_INVALHANDLE;
3037 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
3038 TRACE("never been prepared !\n");
3039 return WAVERR_UNPREPARED;
3041 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
3042 TRACE("header already in use !\n");
3043 return WAVERR_STILLPLAYING;
3046 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
3047 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3048 lpWaveHdr->dwBytesRecorded = 0;
3049 lpWaveHdr->lpNext = NULL;
3051 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
3052 return MMSYSERR_NOERROR;
3055 /**************************************************************************
3056 * widStart [internal]
3058 static DWORD widStart(WORD wDevID)
3060 TRACE("(%u);\n", wDevID);
3061 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3062 WARN("can't start recording !\n");
3063 return MMSYSERR_INVALHANDLE;
3066 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
3067 return MMSYSERR_NOERROR;
3070 /**************************************************************************
3071 * widStop [internal]
3073 static DWORD widStop(WORD wDevID)
3075 TRACE("(%u);\n", wDevID);
3076 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3077 WARN("can't stop !\n");
3078 return MMSYSERR_INVALHANDLE;
3081 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3083 return MMSYSERR_NOERROR;
3086 /**************************************************************************
3087 * widReset [internal]
3089 static DWORD widReset(WORD wDevID)
3091 TRACE("(%u);\n", wDevID);
3092 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3093 WARN("can't reset !\n");
3094 return MMSYSERR_INVALHANDLE;
3096 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3097 return MMSYSERR_NOERROR;
3100 /**************************************************************************
3101 * widGetPosition [internal]
3103 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3105 WINE_WAVEIN* wwi;
3107 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
3109 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3110 WARN("can't get pos !\n");
3111 return MMSYSERR_INVALHANDLE;
3114 if (lpTime == NULL) {
3115 WARN("invalid parameter: lpTime == NULL\n");
3116 return MMSYSERR_INVALPARAM;
3119 wwi = &WInDev[wDevID];
3120 #ifdef EXACT_WIDPOSITION
3121 if (wwi->ossdev.in_caps_support & WAVECAPS_SAMPLEACCURATE)
3122 OSS_AddRingMessage(&(wwi->msgRing), WINE_WM_UPDATE, 0, TRUE);
3123 #endif
3125 return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->waveFormat);
3128 /**************************************************************************
3129 * widMessage (WINEOSS.6)
3131 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD_PTR dwUser,
3132 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3134 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
3135 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
3137 switch (wMsg) {
3138 case DRVM_INIT:
3139 case DRVM_EXIT:
3140 case DRVM_ENABLE:
3141 case DRVM_DISABLE:
3142 /* FIXME: Pretend this is supported */
3143 return 0;
3144 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3145 case WIDM_CLOSE: return widClose (wDevID);
3146 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3147 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
3148 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
3149 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
3150 case WIDM_GETNUMDEVS: return numInDev;
3151 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
3152 case WIDM_RESET: return widReset (wDevID);
3153 case WIDM_START: return widStart (wDevID);
3154 case WIDM_STOP: return widStop (wDevID);
3155 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
3156 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
3157 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3158 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3159 default:
3160 FIXME("unknown message %u!\n", wMsg);
3162 return MMSYSERR_NOTSUPPORTED;
3165 #else /* !HAVE_OSS */
3167 /**************************************************************************
3168 * wodMessage (WINEOSS.7)
3170 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD_PTR dwUser,
3171 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3173 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3174 return MMSYSERR_NOTENABLED;
3177 /**************************************************************************
3178 * widMessage (WINEOSS.6)
3180 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD_PTR dwUser,
3181 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3183 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3184 return MMSYSERR_NOTENABLED;
3187 #endif /* HAVE_OSS */