Remove the unused 1st parameter of ACTION_VerifyComponentForAction.
[wine/multimedia.git] / dlls / winmm / wineoss / audio.c
blob498e08f1dc214e2ec98d90e5ebc1d393407b1be0
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "dsdriver.h"
69 #include "ks.h"
70 #include "ksguid.h"
71 #include "ksmedia.h"
72 #include "oss.h"
73 #include "wine/debug.h"
75 #include "audio.h"
77 WINE_DEFAULT_DEBUG_CHANNEL(wave);
79 /* Allow 1% deviation for sample rates (some ES137x cards) */
80 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
82 #ifdef HAVE_OSS
84 OSS_DEVICE OSS_Devices[MAX_WAVEDRV];
85 WINE_WAVEOUT WOutDev[MAX_WAVEDRV];
86 WINE_WAVEIN WInDev[MAX_WAVEDRV];
87 unsigned numOutDev;
88 unsigned numInDev;
90 /* state diagram for waveOut writing:
92 * +---------+-------------+---------------+---------------------------------+
93 * | state | function | event | new state |
94 * +---------+-------------+---------------+---------------------------------+
95 * | | open() | | STOPPED |
96 * | PAUSED | write() | | PAUSED |
97 * | STOPPED | write() | <thrd create> | PLAYING |
98 * | PLAYING | write() | HEADER | PLAYING |
99 * | (other) | write() | <error> | |
100 * | (any) | pause() | PAUSING | PAUSED |
101 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
102 * | (any) | reset() | RESETTING | STOPPED |
103 * | (any) | close() | CLOSING | CLOSED |
104 * +---------+-------------+---------------+---------------------------------+
107 /* These strings used only for tracing */
108 static const char * getCmdString(enum win_wm_message msg)
110 static char unknown[32];
111 #define MSG_TO_STR(x) case x: return #x
112 switch(msg) {
113 MSG_TO_STR(WINE_WM_PAUSING);
114 MSG_TO_STR(WINE_WM_RESTARTING);
115 MSG_TO_STR(WINE_WM_RESETTING);
116 MSG_TO_STR(WINE_WM_HEADER);
117 MSG_TO_STR(WINE_WM_UPDATE);
118 MSG_TO_STR(WINE_WM_BREAKLOOP);
119 MSG_TO_STR(WINE_WM_CLOSING);
120 MSG_TO_STR(WINE_WM_STARTING);
121 MSG_TO_STR(WINE_WM_STOPPING);
123 #undef MSG_TO_STR
124 sprintf(unknown, "UNKNOWN(0x%08x)", msg);
125 return unknown;
128 int getEnables(OSS_DEVICE *ossdev)
130 return ( (ossdev->bOutputEnabled ? PCM_ENABLE_OUTPUT : 0) |
131 (ossdev->bInputEnabled ? PCM_ENABLE_INPUT : 0) );
134 static const char * getMessage(UINT msg)
136 static char unknown[32];
137 #define MSG_TO_STR(x) case x: return #x
138 switch(msg) {
139 MSG_TO_STR(DRVM_INIT);
140 MSG_TO_STR(DRVM_EXIT);
141 MSG_TO_STR(DRVM_ENABLE);
142 MSG_TO_STR(DRVM_DISABLE);
143 MSG_TO_STR(WIDM_OPEN);
144 MSG_TO_STR(WIDM_CLOSE);
145 MSG_TO_STR(WIDM_ADDBUFFER);
146 MSG_TO_STR(WIDM_PREPARE);
147 MSG_TO_STR(WIDM_UNPREPARE);
148 MSG_TO_STR(WIDM_GETDEVCAPS);
149 MSG_TO_STR(WIDM_GETNUMDEVS);
150 MSG_TO_STR(WIDM_GETPOS);
151 MSG_TO_STR(WIDM_RESET);
152 MSG_TO_STR(WIDM_START);
153 MSG_TO_STR(WIDM_STOP);
154 MSG_TO_STR(WODM_OPEN);
155 MSG_TO_STR(WODM_CLOSE);
156 MSG_TO_STR(WODM_WRITE);
157 MSG_TO_STR(WODM_PAUSE);
158 MSG_TO_STR(WODM_GETPOS);
159 MSG_TO_STR(WODM_BREAKLOOP);
160 MSG_TO_STR(WODM_PREPARE);
161 MSG_TO_STR(WODM_UNPREPARE);
162 MSG_TO_STR(WODM_GETDEVCAPS);
163 MSG_TO_STR(WODM_GETNUMDEVS);
164 MSG_TO_STR(WODM_GETPITCH);
165 MSG_TO_STR(WODM_SETPITCH);
166 MSG_TO_STR(WODM_GETPLAYBACKRATE);
167 MSG_TO_STR(WODM_SETPLAYBACKRATE);
168 MSG_TO_STR(WODM_GETVOLUME);
169 MSG_TO_STR(WODM_SETVOLUME);
170 MSG_TO_STR(WODM_RESTART);
171 MSG_TO_STR(WODM_RESET);
172 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
173 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
174 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
175 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
177 #undef MSG_TO_STR
178 sprintf(unknown, "UNKNOWN(0x%04x)", msg);
179 return unknown;
182 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
184 TRACE("(%u, %p)\n", wDevID, dwParam1);
186 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev->interface_name, -1,
187 NULL, 0 ) * sizeof(WCHAR);
188 return MMSYSERR_NOERROR;
191 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
193 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev->interface_name, -1,
194 NULL, 0 ) * sizeof(WCHAR))
196 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].ossdev->interface_name, -1,
197 dwParam1, dwParam2 / sizeof(WCHAR));
198 return MMSYSERR_NOERROR;
201 return MMSYSERR_INVALPARAM;
204 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
206 TRACE("(%u, %p)\n", wDevID, dwParam1);
208 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev->interface_name, -1,
209 NULL, 0 ) * sizeof(WCHAR);
210 return MMSYSERR_NOERROR;
213 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
215 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev->interface_name, -1,
216 NULL, 0 ) * sizeof(WCHAR))
218 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].ossdev->interface_name, -1,
219 dwParam1, dwParam2 / sizeof(WCHAR));
220 return MMSYSERR_NOERROR;
223 return MMSYSERR_INVALPARAM;
226 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
227 WAVEFORMATPCMEX* format)
229 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
230 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
231 format->Format.nChannels, format->Format.nAvgBytesPerSec);
232 TRACE("Position in bytes=%lu\n", position);
234 switch (lpTime->wType) {
235 case TIME_SAMPLES:
236 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
237 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
238 break;
239 case TIME_MS:
240 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
241 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
242 break;
243 case TIME_SMPTE:
244 lpTime->u.smpte.fps = 30;
245 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
246 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
247 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
248 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
249 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
250 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
251 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
252 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
253 lpTime->u.smpte.fps = 30;
254 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
255 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
256 lpTime->u.smpte.hour, lpTime->u.smpte.min,
257 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
258 break;
259 default:
260 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
261 lpTime->wType = TIME_BYTES;
262 /* fall through */
263 case TIME_BYTES:
264 lpTime->u.cb = position;
265 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
266 break;
268 return MMSYSERR_NOERROR;
271 static BOOL supportedFormat(LPWAVEFORMATEX wf)
273 TRACE("(%p)\n",wf);
275 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
276 return FALSE;
278 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
279 if (wf->nChannels >= 1 && wf->nChannels <= MAX_CHANNELS) {
280 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
281 return TRUE;
283 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
284 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
286 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
287 if (wf->nChannels >=1 && wf->nChannels <= MAX_CHANNELS) {
288 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
289 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
290 return TRUE;
291 } else
292 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
294 } else
295 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
296 } else
297 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
299 return FALSE;
302 void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
304 ZeroMemory(wf2, sizeof(wf2));
305 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
306 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
307 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
308 memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
309 else
310 memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
313 /*======================================================================*
314 * Low level WAVE implementation *
315 *======================================================================*/
317 /******************************************************************
318 * OSS_RawOpenDevice
320 * Low level device opening (from values stored in ossdev)
322 static DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
324 int fd, val, rc;
325 TRACE("(%p,%d)\n",ossdev,strict_format);
327 TRACE("open_access=%s\n",
328 ossdev->open_access == O_RDONLY ? "O_RDONLY" :
329 ossdev->open_access == O_WRONLY ? "O_WRONLY" :
330 ossdev->open_access == O_RDWR ? "O_RDWR" : "Unknown");
332 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
334 WARN("Couldn't open %s (%s)\n", ossdev->dev_name, strerror(errno));
335 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
337 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
338 /* turn full duplex on if it has been requested */
339 if (ossdev->open_access == O_RDWR && ossdev->full_duplex) {
340 rc = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
341 /* on *BSD, as full duplex is always enabled by default, this ioctl
342 * will fail with EINVAL
343 * so, we don't consider EINVAL an error here
345 if (rc != 0 && errno != EINVAL) {
346 WARN("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev->dev_name, strerror(errno));
347 goto error2;
351 if (ossdev->audio_fragment) {
352 rc = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
353 if (rc != 0) {
354 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev->dev_name, strerror(errno));
355 goto error2;
359 /* First size and channels then samplerate */
360 if (ossdev->format>=0)
362 val = ossdev->format;
363 rc = ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
364 if (rc != 0 || val != ossdev->format) {
365 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
366 if (strict_format)
367 goto error;
370 if (ossdev->channels>=0)
372 val = ossdev->channels;
373 rc = ioctl(fd, SNDCTL_DSP_CHANNELS, &ossdev->channels);
374 if (rc != 0 || val != ossdev->channels) {
375 TRACE("Can't set channels to %u (returned %d)\n", val, ossdev->channels);
376 if (strict_format)
377 goto error;
380 if (ossdev->sample_rate>=0)
382 val = ossdev->sample_rate;
383 rc = ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
384 if (rc != 0 || !NEAR_MATCH(val, ossdev->sample_rate)) {
385 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
386 if (strict_format)
387 goto error;
390 ossdev->fd = fd;
392 if (ossdev->bTriggerSupport) {
393 int trigger;
394 rc = ioctl(fd, SNDCTL_DSP_GETTRIGGER, &trigger);
395 if (rc != 0) {
396 ERR("ioctl(%s, SNDCTL_DSP_GETTRIGGER) failed (%s)\n",
397 ossdev->dev_name, strerror(errno));
398 goto error;
401 ossdev->bOutputEnabled = ((trigger & PCM_ENABLE_OUTPUT) == PCM_ENABLE_OUTPUT);
402 ossdev->bInputEnabled = ((trigger & PCM_ENABLE_INPUT) == PCM_ENABLE_INPUT);
404 /* If we do not have full duplex, but they opened RDWR
405 ** (as you have to in order for an mmap to succeed)
406 ** then we start out with input off
408 if (ossdev->open_access == O_RDWR && !ossdev->full_duplex &&
409 ossdev->bInputEnabled && ossdev->bOutputEnabled) {
410 ossdev->bInputEnabled = FALSE;
411 trigger &= ~PCM_ENABLE_INPUT;
412 ioctl(fd, SNDCTL_DSP_SETTRIGGER, &trigger);
414 } else {
415 ossdev->bOutputEnabled = TRUE; /* OSS enables by default */
416 ossdev->bInputEnabled = TRUE; /* OSS enables by default */
419 if (ossdev->open_access == O_RDONLY)
420 ossdev->bOutputEnabled = FALSE;
421 if (ossdev->open_access == O_WRONLY)
422 ossdev->bInputEnabled = FALSE;
424 return MMSYSERR_NOERROR;
426 error:
427 close(fd);
428 return WAVERR_BADFORMAT;
429 error2:
430 close(fd);
431 return MMSYSERR_ERROR;
434 /******************************************************************
435 * OSS_OpenDevice
437 * since OSS has poor capabilities in full duplex, we try here to let a program
438 * open the device for both waveout and wavein streams...
439 * this is hackish, but it's the way OSS interface is done...
441 DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
442 int* frag, int strict_format,
443 int sample_rate, int channels, int fmt)
445 DWORD ret;
446 DWORD open_access;
447 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev,req_access,frag,strict_format,sample_rate,channels,fmt);
449 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
451 TRACE("Opening RDWR because full_duplex=%d and req_access=%d\n",
452 ossdev->full_duplex,req_access);
453 open_access = O_RDWR;
455 else
457 open_access=req_access;
460 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
461 if (ossdev->open_count == 0)
463 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
465 ossdev->audio_fragment = (frag) ? *frag : 0;
466 ossdev->sample_rate = sample_rate;
467 ossdev->channels = channels;
468 ossdev->format = fmt;
469 ossdev->open_access = open_access;
470 ossdev->owner_tid = GetCurrentThreadId();
472 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
473 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
474 (req_access == O_RDONLY || req_access == O_WRONLY))
476 int enable;
477 if (req_access == O_WRONLY)
478 ossdev->bInputEnabled=0;
479 else
480 ossdev->bOutputEnabled=0;
481 enable = getEnables(ossdev);
482 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
483 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
484 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
487 else
489 /* check we really open with the same parameters */
490 if (ossdev->open_access != open_access)
492 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
493 return WAVERR_BADFORMAT;
496 /* check if the audio parameters are the same */
497 if (ossdev->sample_rate != sample_rate ||
498 ossdev->channels != channels ||
499 ossdev->format != fmt)
501 /* This is not a fatal error because MSACM might do the remapping */
502 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
503 "OSS doesn't allow us different parameters\n"
504 "audio_frag(%x/%x) sample_rate(%d/%d) channels(%d/%d) fmt(%d/%d)\n",
505 ossdev->audio_fragment, frag ? *frag : 0,
506 ossdev->sample_rate, sample_rate,
507 ossdev->channels, channels,
508 ossdev->format, fmt);
509 return WAVERR_BADFORMAT;
511 /* check if the fragment sizes are the same */
512 if (ossdev->audio_fragment != (frag ? *frag : 0) )
514 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
515 "Please run winecfg, open \"Audio\" page and set\n"
516 "\"Hardware Acceleration\" to \"Emulation\".\n");
517 return WAVERR_BADFORMAT;
519 if (GetCurrentThreadId() != ossdev->owner_tid)
521 WARN("Another thread is trying to access audio...\n");
522 return MMSYSERR_ERROR;
524 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
525 (req_access == O_RDONLY || req_access == O_WRONLY))
527 int enable;
528 if (req_access == O_WRONLY)
529 ossdev->bOutputEnabled=1;
530 else
531 ossdev->bInputEnabled=1;
532 enable = getEnables(ossdev);
533 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
534 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
535 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
539 ossdev->open_count++;
541 return MMSYSERR_NOERROR;
544 /******************************************************************
545 * OSS_CloseDevice
549 void OSS_CloseDevice(OSS_DEVICE* ossdev)
551 TRACE("(%p)\n",ossdev);
552 if (ossdev->open_count>0) {
553 ossdev->open_count--;
554 } else {
555 WARN("OSS_CloseDevice called too many times\n");
557 if (ossdev->open_count == 0)
559 fcntl(ossdev->fd, F_SETFL, fcntl(ossdev->fd, F_GETFL) & ~O_NDELAY);
560 /* reset the device before we close it in case it is in a bad state */
561 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
562 if (close(ossdev->fd) != 0) FIXME("Cannot close %d: %s\n", ossdev->fd, strerror(errno));
566 /******************************************************************
567 * OSS_ResetDevice
569 * Resets the device. OSS Commercial requires the device to be closed
570 * after a SNDCTL_DSP_RESET ioctl call... this function implements
571 * this behavior...
572 * FIXME: This causes problems when doing full duplex so we really
573 * only reset when not doing full duplex. We need to do this better
574 * someday.
576 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
578 DWORD ret = MMSYSERR_NOERROR;
579 int old_fd = ossdev->fd;
580 TRACE("(%p)\n", ossdev);
582 if (ossdev->open_count == 1) {
583 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
585 perror("ioctl SNDCTL_DSP_RESET");
586 return -1;
588 close(ossdev->fd);
589 ret = OSS_RawOpenDevice(ossdev, 1);
590 TRACE("Changing fd from %d to %d\n", old_fd, ossdev->fd);
591 } else
592 WARN("Not resetting device because it is in full duplex mode!\n");
594 return ret;
597 static const int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
598 static const int win_std_rates[5]={96000,48000,44100,22050,11025};
599 static const int win_std_formats[2][2][5]=
600 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
601 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
602 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
603 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
604 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
605 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
606 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
607 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
610 static void OSS_Info(int fd)
612 /* Note that this only reports the formats supported by the hardware.
613 * The driver may support other formats and do the conversions in
614 * software which is why we don't use this value
616 int oss_mask, oss_caps;
617 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &oss_mask) >= 0) {
618 TRACE("Formats=%08x ( ", oss_mask);
619 if (oss_mask & AFMT_MU_LAW) TRACE("AFMT_MU_LAW ");
620 if (oss_mask & AFMT_A_LAW) TRACE("AFMT_A_LAW ");
621 if (oss_mask & AFMT_IMA_ADPCM) TRACE("AFMT_IMA_ADPCM ");
622 if (oss_mask & AFMT_U8) TRACE("AFMT_U8 ");
623 if (oss_mask & AFMT_S16_LE) TRACE("AFMT_S16_LE ");
624 if (oss_mask & AFMT_S16_BE) TRACE("AFMT_S16_BE ");
625 if (oss_mask & AFMT_S8) TRACE("AFMT_S8 ");
626 if (oss_mask & AFMT_U16_LE) TRACE("AFMT_U16_LE ");
627 if (oss_mask & AFMT_U16_BE) TRACE("AFMT_U16_BE ");
628 if (oss_mask & AFMT_MPEG) TRACE("AFMT_MPEG ");
629 #ifdef AFMT_AC3
630 if (oss_mask & AFMT_AC3) TRACE("AFMT_AC3 ");
631 #endif
632 #ifdef AFMT_VORBIS
633 if (oss_mask & AFMT_VORBIS) TRACE("AFMT_VORBIS ");
634 #endif
635 #ifdef AFMT_S32_LE
636 if (oss_mask & AFMT_S32_LE) TRACE("AFMT_S32_LE ");
637 #endif
638 #ifdef AFMT_S32_BE
639 if (oss_mask & AFMT_S32_BE) TRACE("AFMT_S32_BE ");
640 #endif
641 #ifdef AFMT_FLOAT
642 if (oss_mask & AFMT_FLOAT) TRACE("AFMT_FLOAT ");
643 #endif
644 #ifdef AFMT_S24_LE
645 if (oss_mask & AFMT_S24_LE) TRACE("AFMT_S24_LE ");
646 #endif
647 #ifdef AFMT_S24_BE
648 if (oss_mask & AFMT_S24_BE) TRACE("AFMT_S24_BE ");
649 #endif
650 #ifdef AFMT_SPDIF_RAW
651 if (oss_mask & AFMT_SPDIF_RAW) TRACE("AFMT_SPDIF_RAW ");
652 #endif
653 TRACE(")\n");
655 if (ioctl(fd, SNDCTL_DSP_GETCAPS, &oss_caps) >= 0) {
656 TRACE("Caps=%08x\n",oss_caps);
657 TRACE("\tRevision: %d\n", oss_caps&DSP_CAP_REVISION);
658 TRACE("\tDuplex: %s\n", oss_caps & DSP_CAP_DUPLEX ? "true" : "false");
659 TRACE("\tRealtime: %s\n", oss_caps & DSP_CAP_REALTIME ? "true" : "false");
660 TRACE("\tBatch: %s\n", oss_caps & DSP_CAP_BATCH ? "true" : "false");
661 TRACE("\tCoproc: %s\n", oss_caps & DSP_CAP_COPROC ? "true" : "false");
662 TRACE("\tTrigger: %s\n", oss_caps & DSP_CAP_TRIGGER ? "true" : "false");
663 TRACE("\tMmap: %s\n", oss_caps & DSP_CAP_MMAP ? "true" : "false");
664 #ifdef DSP_CAP_MULTI
665 TRACE("\tMulti: %s\n", oss_caps & DSP_CAP_MULTI ? "true" : "false");
666 #endif
667 #ifdef DSP_CAP_BIND
668 TRACE("\tBind: %s\n", oss_caps & DSP_CAP_BIND ? "true" : "false");
669 #endif
670 #ifdef DSP_CAP_INPUT
671 TRACE("\tInput: %s\n", oss_caps & DSP_CAP_INPUT ? "true" : "false");
672 #endif
673 #ifdef DSP_CAP_OUTPUT
674 TRACE("\tOutput: %s\n", oss_caps & DSP_CAP_OUTPUT ? "true" : "false");
675 #endif
676 #ifdef DSP_CAP_VIRTUAL
677 TRACE("\tVirtual: %s\n", oss_caps & DSP_CAP_VIRTUAL ? "true" : "false");
678 #endif
679 #ifdef DSP_CAP_ANALOGOUT
680 TRACE("\tAnalog Out: %s\n", oss_caps & DSP_CAP_ANALOGOUT ? "true" : "false");
681 #endif
682 #ifdef DSP_CAP_ANALOGIN
683 TRACE("\tAnalog In: %s\n", oss_caps & DSP_CAP_ANALOGIN ? "true" : "false");
684 #endif
685 #ifdef DSP_CAP_DIGITALOUT
686 TRACE("\tDigital Out: %s\n", oss_caps & DSP_CAP_DIGITALOUT ? "true" : "false");
687 #endif
688 #ifdef DSP_CAP_DIGITALIN
689 TRACE("\tDigital In: %s\n", oss_caps & DSP_CAP_DIGITALIN ? "true" : "false");
690 #endif
691 #ifdef DSP_CAP_ADMASK
692 TRACE("\tA/D Mask: %s\n", oss_caps & DSP_CAP_ADMASK ? "true" : "false");
693 #endif
694 #ifdef DSP_CAP_SHADOW
695 TRACE("\tShadow: %s\n", oss_caps & DSP_CAP_SHADOW ? "true" : "false");
696 #endif
697 #ifdef DSP_CH_MASK
698 TRACE("\tChannel Mask: %x\n", oss_caps & DSP_CH_MASK);
699 #endif
700 #ifdef DSP_CAP_SLAVE
701 TRACE("\tSlave: %s\n", oss_caps & DSP_CAP_SLAVE ? "true" : "false");
702 #endif
706 /******************************************************************
707 * OSS_WaveOutInit
711 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
713 int rc,arg;
714 int f,c,r;
715 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
717 if (OSS_OpenDevice(ossdev, O_WRONLY, NULL, 0,-1,-1,-1) != 0)
718 return FALSE;
720 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
722 #ifdef SOUND_MIXER_INFO
724 int mixer;
725 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
726 mixer_info info;
727 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
728 lstrcpynA(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
729 strcpy(ossdev->ds_desc.szDrvname, "wineoss.drv");
730 MultiByteToWideChar(CP_ACP, 0, info.name, sizeof(info.name),
731 ossdev->out_caps.szPname,
732 sizeof(ossdev->out_caps.szPname) / sizeof(WCHAR));
733 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
734 } else {
735 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
736 * implement it properly, and there are probably similar issues
737 * on other platforms, so we warn but try to go ahead.
739 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
741 close(mixer);
742 } else {
743 ERR("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
744 OSS_CloseDevice(ossdev);
745 return FALSE;
748 #endif /* SOUND_MIXER_INFO */
750 if (WINE_TRACE_ON(wave))
751 OSS_Info(ossdev->fd);
753 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
754 ossdev->out_caps.wPid = 0x0001; /* Product ID */
756 ossdev->out_caps.vDriverVersion = 0x0100;
757 ossdev->out_caps.wChannels = 1;
758 ossdev->out_caps.dwFormats = 0x00000000;
759 ossdev->out_caps.wReserved1 = 0;
760 ossdev->out_caps.dwSupport = WAVECAPS_VOLUME;
762 /* direct sound caps */
763 ossdev->ds_caps.dwFlags = DSCAPS_CERTIFIED;
764 ossdev->ds_caps.dwPrimaryBuffers = 1;
765 ossdev->ds_caps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
766 ossdev->ds_caps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
768 /* We must first set the format and the stereo mode as some sound cards
769 * may support 44kHz mono but not 44kHz stereo. Also we must
770 * systematically check the return value of these ioctls as they will
771 * always succeed (see OSS Linux) but will modify the parameter to match
772 * whatever they support. The OSS specs also say we must first set the
773 * sample size, then the stereo and then the sample rate.
775 for (f=0;f<2;f++) {
776 arg=win_std_oss_fmts[f];
777 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
778 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
779 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
780 rc,arg,win_std_oss_fmts[f]);
781 continue;
783 if (f == 0)
784 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY8BIT;
785 else if (f == 1)
786 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY16BIT;
788 for (c = 1; c <= MAX_CHANNELS; c++) {
789 arg=c;
790 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
791 if (rc!=0 || arg!=c) {
792 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
793 continue;
795 if (c == 1) {
796 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
797 } else if (c == 2) {
798 ossdev->out_caps.wChannels = 2;
799 ossdev->out_caps.dwSupport|=WAVECAPS_LRVOLUME;
800 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
801 } else
802 ossdev->out_caps.wChannels = c;
804 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
805 arg=win_std_rates[r];
806 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
807 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
808 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
809 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
810 ossdev->out_caps.dwFormats|=win_std_formats[f][c-1][r];
815 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
816 if (arg & DSP_CAP_TRIGGER)
817 ossdev->bTriggerSupport = TRUE;
818 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
819 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
821 /* well, might as well use the DirectSound cap flag for something */
822 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
823 !(arg & DSP_CAP_BATCH)) {
824 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
825 } else {
826 ossdev->ds_caps.dwFlags |= DSCAPS_EMULDRIVER;
828 #ifdef DSP_CAP_MULTI /* not every oss has this */
829 /* check for hardware secondary buffer support (multi open) */
830 if ((arg & DSP_CAP_MULTI) &&
831 (ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
832 TRACE("hardware secondary buffer support available\n");
833 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARY8BIT)
834 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY8BIT;
835 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARY16BIT)
836 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY16BIT;
837 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARYMONO)
838 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYMONO;
839 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARYSTEREO)
840 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYSTEREO;
842 ossdev->ds_caps.dwMaxHwMixingAllBuffers = 16;
843 ossdev->ds_caps.dwMaxHwMixingStaticBuffers = 0;
844 ossdev->ds_caps.dwMaxHwMixingStreamingBuffers = 16;
846 ossdev->ds_caps.dwFreeHwMixingAllBuffers = 16;
847 ossdev->ds_caps.dwFreeHwMixingStaticBuffers = 0;
848 ossdev->ds_caps.dwFreeHwMixingStreamingBuffers = 16;
850 #endif
852 OSS_CloseDevice(ossdev);
853 TRACE("out wChannels = %d, dwFormats = %08lX, dwSupport = %08lX\n",
854 ossdev->out_caps.wChannels, ossdev->out_caps.dwFormats,
855 ossdev->out_caps.dwSupport);
856 return TRUE;
859 /******************************************************************
860 * OSS_WaveInInit
864 static BOOL OSS_WaveInInit(OSS_DEVICE* ossdev)
866 int rc,arg;
867 int f,c,r;
868 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
870 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0,-1,-1,-1) != 0)
871 return FALSE;
873 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
875 #ifdef SOUND_MIXER_INFO
877 int mixer;
878 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
879 mixer_info info;
880 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
881 MultiByteToWideChar(CP_ACP, 0, info.name, -1,
882 ossdev->in_caps.szPname,
883 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
884 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
885 } else {
886 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
887 * implement it properly, and there are probably similar issues
888 * on other platforms, so we warn but try to go ahead.
890 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
892 close(mixer);
893 } else {
894 ERR("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
895 OSS_CloseDevice(ossdev);
896 return FALSE;
899 #endif /* SOUND_MIXER_INFO */
901 if (WINE_TRACE_ON(wave))
902 OSS_Info(ossdev->fd);
904 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
905 ossdev->in_caps.wPid = 0x0001; /* Product ID */
907 ossdev->in_caps.dwFormats = 0x00000000;
908 ossdev->in_caps.wChannels = 1;
909 ossdev->in_caps.wReserved1 = 0;
911 /* direct sound caps */
912 ossdev->dsc_caps.dwSize = sizeof(ossdev->dsc_caps);
913 ossdev->dsc_caps.dwFlags = 0;
914 ossdev->dsc_caps.dwFormats = 0x00000000;
915 ossdev->dsc_caps.dwChannels = 1;
917 /* See the comment in OSS_WaveOutInit for the loop order */
918 for (f=0;f<2;f++) {
919 arg=win_std_oss_fmts[f];
920 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
921 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
922 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
923 rc,arg,win_std_oss_fmts[f]);
924 continue;
927 for (c = 1; c <= MAX_CHANNELS; c++) {
928 arg=c;
929 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
930 if (rc!=0 || arg!=c) {
931 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
932 continue;
934 if (c > 1) {
935 ossdev->in_caps.wChannels = c;
936 ossdev->dsc_caps.dwChannels = c;
939 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
940 arg=win_std_rates[r];
941 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
942 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
943 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
944 ossdev->in_caps.dwFormats|=win_std_formats[f][c-1][r];
945 ossdev->dsc_caps.dwFormats|=win_std_formats[f][c-1][r];
950 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
951 if (arg & DSP_CAP_TRIGGER)
952 ossdev->bTriggerSupport = TRUE;
953 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
954 !(arg & DSP_CAP_BATCH)) {
955 /* FIXME: enable the next statement if you want to work on the driver */
956 #if 0
957 ossdev->in_caps_support |= WAVECAPS_DIRECTSOUND;
958 #endif
960 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH))
961 ossdev->in_caps_support |= WAVECAPS_SAMPLEACCURATE;
963 OSS_CloseDevice(ossdev);
964 TRACE("in wChannels = %d, dwFormats = %08lX, in_caps_support = %08lX\n",
965 ossdev->in_caps.wChannels, ossdev->in_caps.dwFormats, ossdev->in_caps_support);
966 return TRUE;
969 /******************************************************************
970 * OSS_WaveFullDuplexInit
974 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
976 int rc,arg;
977 int f,c,r;
978 int caps;
979 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
981 /* The OSS documentation says we must call SNDCTL_SETDUPLEX
982 * *before* checking for SNDCTL_DSP_GETCAPS otherwise we may
983 * get the wrong result. This ioctl must even be done before
984 * setting the fragment size so that only OSS_RawOpenDevice is
985 * in a position to do it. So we set full_duplex speculatively
986 * and adjust right after.
988 ossdev->full_duplex=1;
989 rc=OSS_OpenDevice(ossdev, O_RDWR, NULL, 0,-1,-1,-1);
990 ossdev->full_duplex=0;
991 if (rc != 0)
992 return;
994 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
995 TRACE("%s\n", ossdev->ds_desc.szDesc);
997 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
998 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
1000 ossdev->duplex_out_caps = ossdev->out_caps;
1002 ossdev->duplex_out_caps.wChannels = 1;
1003 ossdev->duplex_out_caps.dwFormats = 0x00000000;
1004 ossdev->duplex_out_caps.dwSupport = WAVECAPS_VOLUME;
1006 if (WINE_TRACE_ON(wave))
1007 OSS_Info(ossdev->fd);
1009 /* See the comment in OSS_WaveOutInit for the loop order */
1010 for (f=0;f<2;f++) {
1011 arg=win_std_oss_fmts[f];
1012 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
1013 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
1014 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1015 rc,arg,win_std_oss_fmts[f]);
1016 continue;
1019 for (c = 1; c <= MAX_CHANNELS; c++) {
1020 arg=c;
1021 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
1022 if (rc!=0 || arg!=c) {
1023 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
1024 continue;
1026 if (c == 1) {
1027 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
1028 } else if (c == 2) {
1029 ossdev->duplex_out_caps.wChannels = 2;
1030 ossdev->duplex_out_caps.dwSupport|=WAVECAPS_LRVOLUME;
1031 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
1032 } else
1033 ossdev->duplex_out_caps.wChannels = c;
1035 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
1036 arg=win_std_rates[r];
1037 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
1038 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
1039 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
1040 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
1041 ossdev->duplex_out_caps.dwFormats|=win_std_formats[f][c-1][r];
1046 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
1047 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
1048 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
1050 /* well, might as well use the DirectSound cap flag for something */
1051 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
1052 !(arg & DSP_CAP_BATCH)) {
1053 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
1056 OSS_CloseDevice(ossdev);
1057 TRACE("duplex wChannels = %d, dwFormats = %08lX, dwSupport = %08lX\n",
1058 ossdev->duplex_out_caps.wChannels,
1059 ossdev->duplex_out_caps.dwFormats,
1060 ossdev->duplex_out_caps.dwSupport);
1063 static char* StrDup(const char* str, const char* def)
1065 char* dst;
1066 if (str==NULL)
1067 str=def;
1068 dst=HeapAlloc(GetProcessHeap(),0,strlen(str)+1);
1069 strcpy(dst, str);
1070 return dst;
1073 /******************************************************************
1074 * OSS_WaveInit
1076 * Initialize internal structures from OSS information
1078 LONG OSS_WaveInit(void)
1080 char* str;
1081 int i;
1083 TRACE("()\n");
1085 str=getenv("AUDIODEV");
1086 if (str!=NULL)
1088 OSS_Devices[0].dev_name=StrDup(str,"");
1089 OSS_Devices[0].mixer_name=StrDup(getenv("MIXERDEV"),"/dev/mixer");
1090 for (i = 1; i < MAX_WAVEDRV; ++i)
1092 OSS_Devices[i].dev_name=StrDup("",NULL);
1093 OSS_Devices[i].mixer_name=StrDup("",NULL);
1096 else
1098 OSS_Devices[0].dev_name=StrDup("/dev/dsp",NULL);
1099 OSS_Devices[0].mixer_name=StrDup("/dev/mixer",NULL);
1100 for (i = 1; i < MAX_WAVEDRV; ++i)
1102 OSS_Devices[i].dev_name=HeapAlloc(GetProcessHeap(),0,11);
1103 sprintf(OSS_Devices[i].dev_name, "/dev/dsp%d", i);
1104 OSS_Devices[i].mixer_name=HeapAlloc(GetProcessHeap(),0,13);
1105 sprintf(OSS_Devices[i].mixer_name, "/dev/mixer%d", i);
1109 for (i = 0; i < MAX_WAVEDRV; ++i)
1111 OSS_Devices[i].interface_name=HeapAlloc(GetProcessHeap(),0,9+strlen(OSS_Devices[i].dev_name)+1);
1112 sprintf(OSS_Devices[i].interface_name, "wineoss: %s", OSS_Devices[i].dev_name);
1115 /* start with output devices */
1116 for (i = 0; i < MAX_WAVEDRV; ++i)
1118 if (*OSS_Devices[i].dev_name=='\0' || OSS_WaveOutInit(&OSS_Devices[i]))
1120 WOutDev[numOutDev].state = WINE_WS_CLOSED;
1121 WOutDev[numOutDev].ossdev = &OSS_Devices[i];
1122 WOutDev[numOutDev].volume = 0xffffffff;
1123 numOutDev++;
1127 /* then do input devices */
1128 for (i = 0; i < MAX_WAVEDRV; ++i)
1130 if (*OSS_Devices[i].dev_name=='\0' || OSS_WaveInInit(&OSS_Devices[i]))
1132 WInDev[numInDev].state = WINE_WS_CLOSED;
1133 WInDev[numInDev].ossdev = &OSS_Devices[i];
1134 numInDev++;
1138 /* finish with the full duplex bits */
1139 for (i = 0; i < MAX_WAVEDRV; i++)
1140 if (*OSS_Devices[i].dev_name!='\0')
1141 OSS_WaveFullDuplexInit(&OSS_Devices[i]);
1143 TRACE("%d wave out devices\n", numOutDev);
1144 for (i = 0; i < numOutDev; i++) {
1145 TRACE("%d: %s, %s, %s\n", i, WOutDev[i].ossdev->dev_name,
1146 WOutDev[i].ossdev->mixer_name, WOutDev[i].ossdev->interface_name);
1149 TRACE("%d wave in devices\n", numInDev);
1150 for (i = 0; i < numInDev; i++) {
1151 TRACE("%d: %s, %s, %s\n", i, WInDev[i].ossdev->dev_name,
1152 WInDev[i].ossdev->mixer_name, WInDev[i].ossdev->interface_name);
1155 return 0;
1158 /******************************************************************
1159 * OSS_InitRingMessage
1161 * Initialize the ring of messages for passing between driver's caller and playback/record
1162 * thread
1164 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
1166 omr->msg_toget = 0;
1167 omr->msg_tosave = 0;
1168 #ifdef USE_PIPE_SYNC
1169 if (pipe(omr->msg_pipe) < 0) {
1170 omr->msg_pipe[0] = -1;
1171 omr->msg_pipe[1] = -1;
1172 ERR("could not create pipe, error=%s\n", strerror(errno));
1174 #else
1175 omr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1176 #endif
1177 omr->ring_buffer_size = OSS_RING_BUFFER_INCREMENT;
1178 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(OSS_MSG));
1179 InitializeCriticalSection(&omr->msg_crst);
1180 omr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)"WINEOSS_msg_crst";
1181 return 0;
1184 /******************************************************************
1185 * OSS_DestroyRingMessage
1188 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
1190 #ifdef USE_PIPE_SYNC
1191 close(omr->msg_pipe[0]);
1192 close(omr->msg_pipe[1]);
1193 #else
1194 CloseHandle(omr->msg_event);
1195 #endif
1196 HeapFree(GetProcessHeap(),0,omr->messages);
1197 DeleteCriticalSection(&omr->msg_crst);
1198 return 0;
1201 /******************************************************************
1202 * OSS_AddRingMessage
1204 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
1206 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
1208 HANDLE hEvent = INVALID_HANDLE_VALUE;
1210 EnterCriticalSection(&omr->msg_crst);
1211 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
1213 int old_ring_buffer_size = omr->ring_buffer_size;
1214 omr->ring_buffer_size += OSS_RING_BUFFER_INCREMENT;
1215 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
1216 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(OSS_MSG));
1217 /* Now we need to rearrange the ring buffer so that the new
1218 buffers just allocated are in between omr->msg_tosave and
1219 omr->msg_toget.
1221 if (omr->msg_tosave < omr->msg_toget)
1223 memmove(&(omr->messages[omr->msg_toget + OSS_RING_BUFFER_INCREMENT]),
1224 &(omr->messages[omr->msg_toget]),
1225 sizeof(OSS_MSG)*(old_ring_buffer_size - omr->msg_toget)
1227 omr->msg_toget += OSS_RING_BUFFER_INCREMENT;
1230 if (wait)
1232 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1233 if (hEvent == INVALID_HANDLE_VALUE)
1235 ERR("can't create event !?\n");
1236 LeaveCriticalSection(&omr->msg_crst);
1237 return 0;
1239 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
1240 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1241 omr->msg_toget,getCmdString(omr->messages[omr->msg_toget].msg),
1242 omr->msg_tosave,getCmdString(omr->messages[omr->msg_tosave].msg));
1244 /* fast messages have to be added at the start of the queue */
1245 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
1246 omr->messages[omr->msg_toget].msg = msg;
1247 omr->messages[omr->msg_toget].param = param;
1248 omr->messages[omr->msg_toget].hEvent = hEvent;
1250 else
1252 omr->messages[omr->msg_tosave].msg = msg;
1253 omr->messages[omr->msg_tosave].param = param;
1254 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
1255 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
1257 LeaveCriticalSection(&omr->msg_crst);
1258 /* signal a new message */
1259 SIGNAL_OMR(omr);
1260 if (wait)
1262 /* wait for playback/record thread to have processed the message */
1263 WaitForSingleObject(hEvent, INFINITE);
1264 CloseHandle(hEvent);
1266 return 1;
1269 /******************************************************************
1270 * OSS_RetrieveRingMessage
1272 * Get a message from the ring. Should be called by the playback/record thread.
1274 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
1275 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
1277 EnterCriticalSection(&omr->msg_crst);
1279 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1281 LeaveCriticalSection(&omr->msg_crst);
1282 return 0;
1285 *msg = omr->messages[omr->msg_toget].msg;
1286 omr->messages[omr->msg_toget].msg = 0;
1287 *param = omr->messages[omr->msg_toget].param;
1288 *hEvent = omr->messages[omr->msg_toget].hEvent;
1289 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
1290 CLEAR_OMR(omr);
1291 LeaveCriticalSection(&omr->msg_crst);
1292 return 1;
1295 /******************************************************************
1296 * OSS_PeekRingMessage
1298 * Peek at a message from the ring but do not remove it.
1299 * Should be called by the playback/record thread.
1301 static int OSS_PeekRingMessage(OSS_MSG_RING* omr,
1302 enum win_wm_message *msg,
1303 DWORD *param, HANDLE *hEvent)
1305 EnterCriticalSection(&omr->msg_crst);
1307 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1309 LeaveCriticalSection(&omr->msg_crst);
1310 return 0;
1313 *msg = omr->messages[omr->msg_toget].msg;
1314 *param = omr->messages[omr->msg_toget].param;
1315 *hEvent = omr->messages[omr->msg_toget].hEvent;
1316 LeaveCriticalSection(&omr->msg_crst);
1317 return 1;
1320 /*======================================================================*
1321 * Low level WAVE OUT implementation *
1322 *======================================================================*/
1324 /**************************************************************************
1325 * wodNotifyClient [internal]
1327 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1329 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lX dwParam2 = %04lX\n", wMsg,
1330 wMsg == WOM_OPEN ? "WOM_OPEN" : wMsg == WOM_CLOSE ? "WOM_CLOSE" :
1331 wMsg == WOM_DONE ? "WOM_DONE" : "Unknown", dwParam1, dwParam2);
1333 switch (wMsg) {
1334 case WOM_OPEN:
1335 case WOM_CLOSE:
1336 case WOM_DONE:
1337 if (wwo->wFlags != DCB_NULL &&
1338 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
1339 (HDRVR)wwo->waveDesc.hWave, wMsg,
1340 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
1341 WARN("can't notify client !\n");
1342 return MMSYSERR_ERROR;
1344 break;
1345 default:
1346 FIXME("Unknown callback message %u\n", wMsg);
1347 return MMSYSERR_INVALPARAM;
1349 return MMSYSERR_NOERROR;
1352 /**************************************************************************
1353 * wodUpdatePlayedTotal [internal]
1356 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
1358 audio_buf_info dspspace;
1359 DWORD notplayed;
1360 if (!info) info = &dspspace;
1362 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
1363 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
1364 return FALSE;
1367 /* GETOSPACE is not always accurate when we're down to the last fragment or two;
1368 ** we try to accommodate that here by assuming that the dsp is empty by looking
1369 ** at the clock rather than the result of GETOSPACE */
1370 notplayed = wwo->dwBufferSize - info->bytes;
1371 if (notplayed > 0 && notplayed < (info->fragsize * 2))
1373 if (wwo->dwProjectedFinishTime && GetTickCount() >= wwo->dwProjectedFinishTime)
1375 TRACE("Adjusting for a presumed OSS bug and assuming all data has been played.\n");
1376 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1377 return TRUE;
1379 else
1380 /* Some OSS drivers will clean up nicely if given a POST, so give 'em the chance... */
1381 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1384 wwo->dwPlayedTotal = wwo->dwWrittenTotal - notplayed;
1385 return TRUE;
1388 /**************************************************************************
1389 * wodPlayer_BeginWaveHdr [internal]
1391 * Makes the specified lpWaveHdr the currently playing wave header.
1392 * If the specified wave header is a begin loop and we're not already in
1393 * a loop, setup the loop.
1395 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1397 wwo->lpPlayPtr = lpWaveHdr;
1399 if (!lpWaveHdr) return;
1401 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1402 if (wwo->lpLoopPtr) {
1403 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1404 } else {
1405 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1406 wwo->lpLoopPtr = lpWaveHdr;
1407 /* Windows does not touch WAVEHDR.dwLoops,
1408 * so we need to make an internal copy */
1409 wwo->dwLoops = lpWaveHdr->dwLoops;
1412 wwo->dwPartialOffset = 0;
1415 /**************************************************************************
1416 * wodPlayer_PlayPtrNext [internal]
1418 * Advance the play pointer to the next waveheader, looping if required.
1420 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
1422 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1424 wwo->dwPartialOffset = 0;
1425 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1426 /* We're at the end of a loop, loop if required */
1427 if (--wwo->dwLoops > 0) {
1428 wwo->lpPlayPtr = wwo->lpLoopPtr;
1429 } else {
1430 /* Handle overlapping loops correctly */
1431 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1432 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1433 /* shall we consider the END flag for the closing loop or for
1434 * the opening one or for both ???
1435 * code assumes for closing loop only
1437 } else {
1438 lpWaveHdr = lpWaveHdr->lpNext;
1440 wwo->lpLoopPtr = NULL;
1441 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1443 } else {
1444 /* We're not in a loop. Advance to the next wave header */
1445 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1448 return lpWaveHdr;
1451 /**************************************************************************
1452 * wodPlayer_TicksTillEmpty [internal]
1453 * Returns the number of ticks until we think the DSP should be empty
1455 static DWORD wodPlayer_TicksTillEmpty(const WINE_WAVEOUT *wwo)
1457 return ((wwo->dwWrittenTotal - wwo->dwPlayedTotal) * 1000)
1458 / wwo->waveFormat.Format.nAvgBytesPerSec;
1461 /**************************************************************************
1462 * wodPlayer_DSPWait [internal]
1463 * Returns the number of milliseconds to wait for the DSP buffer to write
1464 * one fragment.
1466 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1468 /* time for one fragment to be played */
1469 return wwo->dwFragmentSize * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1472 /**************************************************************************
1473 * wodPlayer_NotifyWait [internal]
1474 * Returns the number of milliseconds to wait before attempting to notify
1475 * completion of the specified wavehdr.
1476 * This is based on the number of bytes remaining to be written in the
1477 * wave.
1479 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1481 DWORD dwMillis;
1483 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1484 dwMillis = 1;
1485 } else {
1486 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1487 if (!dwMillis) dwMillis = 1;
1490 return dwMillis;
1494 /**************************************************************************
1495 * wodPlayer_WriteMaxFrags [internal]
1496 * Writes the maximum number of bytes possible to the DSP and returns
1497 * TRUE iff the current playPtr has been fully played
1499 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
1501 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1502 DWORD toWrite = min(dwLength, *bytes);
1503 int written;
1504 BOOL ret = FALSE;
1506 TRACE("Writing wavehdr %p.%lu[%lu]/%lu\n",
1507 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
1509 if (toWrite > 0)
1511 written = write(wwo->ossdev->fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
1512 if (written <= 0) {
1513 TRACE("write(%s, %p, %ld) failed (%s) returned %d\n", wwo->ossdev->dev_name,
1514 wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite, strerror(errno), written);
1515 return FALSE;
1518 else
1519 written = 0;
1521 if (written >= dwLength) {
1522 /* If we wrote all current wavehdr, skip to the next one */
1523 wodPlayer_PlayPtrNext(wwo);
1524 ret = TRUE;
1525 } else {
1526 /* Remove the amount written */
1527 wwo->dwPartialOffset += written;
1529 *bytes -= written;
1530 wwo->dwWrittenTotal += written;
1531 TRACE("dwWrittenTotal=%lu\n", wwo->dwWrittenTotal);
1532 return ret;
1536 /**************************************************************************
1537 * wodPlayer_NotifyCompletions [internal]
1539 * Notifies and remove from queue all wavehdrs which have been played to
1540 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1541 * we notify all wavehdrs and remove them all from the queue even if they
1542 * are unplayed or part of a loop.
1544 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1546 LPWAVEHDR lpWaveHdr;
1548 /* Start from lpQueuePtr and keep notifying until:
1549 * - we hit an unwritten wavehdr
1550 * - we hit the beginning of a running loop
1551 * - we hit a wavehdr which hasn't finished playing
1553 #if 0
1554 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1555 (force ||
1556 (lpWaveHdr != wwo->lpPlayPtr &&
1557 lpWaveHdr != wwo->lpLoopPtr &&
1558 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1560 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1562 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1563 lpWaveHdr->dwFlags |= WHDR_DONE;
1565 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1567 #else
1568 for (;;)
1570 lpWaveHdr = wwo->lpQueuePtr;
1571 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
1572 if (!force)
1574 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
1575 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
1576 if (lpWaveHdr->reserved > wwo->dwPlayedTotal){TRACE("still playing %p (%lu/%lu)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
1578 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1580 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1581 lpWaveHdr->dwFlags |= WHDR_DONE;
1583 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1585 #endif
1586 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1587 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1590 /**************************************************************************
1591 * wodPlayer_Reset [internal]
1593 * wodPlayer helper. Resets current output stream.
1595 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
1597 wodUpdatePlayedTotal(wwo, NULL);
1598 /* updates current notify list */
1599 wodPlayer_NotifyCompletions(wwo, FALSE);
1601 /* flush all possible output */
1602 if (OSS_ResetDevice(wwo->ossdev) != MMSYSERR_NOERROR)
1604 wwo->hThread = 0;
1605 wwo->state = WINE_WS_STOPPED;
1606 ExitThread(-1);
1609 if (reset) {
1610 enum win_wm_message msg;
1611 DWORD param;
1612 HANDLE ev;
1614 /* remove any buffer */
1615 wodPlayer_NotifyCompletions(wwo, TRUE);
1617 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1618 wwo->state = WINE_WS_STOPPED;
1619 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1620 /* Clear partial wavehdr */
1621 wwo->dwPartialOffset = 0;
1623 /* remove any existing message in the ring */
1624 EnterCriticalSection(&wwo->msgRing.msg_crst);
1625 /* return all pending headers in queue */
1626 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1628 if (msg != WINE_WM_HEADER)
1630 FIXME("shouldn't have headers left\n");
1631 SetEvent(ev);
1632 continue;
1634 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1635 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1637 wodNotifyClient(wwo, WOM_DONE, param, 0);
1639 RESET_OMR(&wwo->msgRing);
1640 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1641 } else {
1642 if (wwo->lpLoopPtr) {
1643 /* complicated case, not handled yet (could imply modifying the loop counter */
1644 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1645 wwo->lpPlayPtr = wwo->lpLoopPtr;
1646 wwo->dwPartialOffset = 0;
1647 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1648 } else {
1649 LPWAVEHDR ptr;
1650 DWORD sz = wwo->dwPartialOffset;
1652 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1653 /* compute the max size playable from lpQueuePtr */
1654 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1655 sz += ptr->dwBufferLength;
1657 /* because the reset lpPlayPtr will be lpQueuePtr */
1658 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1659 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1660 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1661 wwo->lpPlayPtr = wwo->lpQueuePtr;
1663 wwo->state = WINE_WS_PAUSED;
1667 /**************************************************************************
1668 * wodPlayer_ProcessMessages [internal]
1670 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1672 LPWAVEHDR lpWaveHdr;
1673 enum win_wm_message msg;
1674 DWORD param;
1675 HANDLE ev;
1677 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1678 TRACE("Received %s %lx\n", getCmdString(msg), param);
1679 switch (msg) {
1680 case WINE_WM_PAUSING:
1681 wodPlayer_Reset(wwo, FALSE);
1682 SetEvent(ev);
1683 break;
1684 case WINE_WM_RESTARTING:
1685 if (wwo->state == WINE_WS_PAUSED)
1687 wwo->state = WINE_WS_PLAYING;
1689 SetEvent(ev);
1690 break;
1691 case WINE_WM_HEADER:
1692 lpWaveHdr = (LPWAVEHDR)param;
1694 /* insert buffer at the end of queue */
1696 LPWAVEHDR* wh;
1697 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1698 *wh = lpWaveHdr;
1700 if (!wwo->lpPlayPtr)
1701 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1702 if (wwo->state == WINE_WS_STOPPED)
1703 wwo->state = WINE_WS_PLAYING;
1704 break;
1705 case WINE_WM_RESETTING:
1706 wodPlayer_Reset(wwo, TRUE);
1707 SetEvent(ev);
1708 break;
1709 case WINE_WM_UPDATE:
1710 wodUpdatePlayedTotal(wwo, NULL);
1711 SetEvent(ev);
1712 break;
1713 case WINE_WM_BREAKLOOP:
1714 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1715 /* ensure exit at end of current loop */
1716 wwo->dwLoops = 1;
1718 SetEvent(ev);
1719 break;
1720 case WINE_WM_CLOSING:
1721 /* sanity check: this should not happen since the device must have been reset before */
1722 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1723 wwo->hThread = 0;
1724 wwo->state = WINE_WS_CLOSED;
1725 SetEvent(ev);
1726 ExitThread(0);
1727 /* shouldn't go here */
1728 default:
1729 FIXME("unknown message %d\n", msg);
1730 break;
1735 /**************************************************************************
1736 * wodPlayer_FeedDSP [internal]
1737 * Feed as much sound data as we can into the DSP and return the number of
1738 * milliseconds before it will be necessary to feed the DSP again.
1740 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1742 audio_buf_info dspspace;
1743 DWORD availInQ;
1745 if (!wodUpdatePlayedTotal(wwo, &dspspace)) return INFINITE;
1746 availInQ = dspspace.bytes;
1747 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1748 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1750 /* no more room... no need to try to feed */
1751 if (dspspace.fragments != 0) {
1752 /* Feed from partial wavehdr */
1753 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1754 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1757 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1758 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1759 do {
1760 TRACE("Setting time to elapse for %p to %lu\n",
1761 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1762 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1763 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1764 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1767 if (wwo->bNeedPost) {
1768 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1769 * if it didn't get one, we give it the other */
1770 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1771 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1772 wwo->bNeedPost = FALSE;
1776 return wodPlayer_DSPWait(wwo);
1780 /**************************************************************************
1781 * wodPlayer [internal]
1783 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1785 WORD uDevID = (DWORD)pmt;
1786 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1787 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1788 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1789 DWORD dwSleepTime;
1791 wwo->state = WINE_WS_STOPPED;
1792 SetEvent(wwo->hStartUpEvent);
1794 for (;;) {
1795 /** Wait for the shortest time before an action is required. If there
1796 * are no pending actions, wait forever for a command.
1798 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1799 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1800 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1801 wodPlayer_ProcessMessages(wwo);
1802 if (wwo->state == WINE_WS_PLAYING) {
1803 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1804 if (dwNextFeedTime != INFINITE)
1805 wwo->dwProjectedFinishTime = GetTickCount() + wodPlayer_TicksTillEmpty(wwo);
1806 else
1807 wwo->dwProjectedFinishTime = 0;
1809 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1810 if (dwNextFeedTime == INFINITE) {
1811 /* FeedDSP ran out of data, but before flushing, */
1812 /* check that a notification didn't give us more */
1813 wodPlayer_ProcessMessages(wwo);
1814 if (!wwo->lpPlayPtr) {
1815 TRACE("flushing\n");
1816 ioctl(wwo->ossdev->fd, SNDCTL_DSP_SYNC, 0);
1817 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1818 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1819 } else {
1820 TRACE("recovering\n");
1821 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1824 } else {
1825 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1830 /**************************************************************************
1831 * wodGetDevCaps [internal]
1833 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1835 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1837 if (lpCaps == NULL) {
1838 WARN("not enabled\n");
1839 return MMSYSERR_NOTENABLED;
1842 if (wDevID >= numOutDev) {
1843 WARN("numOutDev reached !\n");
1844 return MMSYSERR_BADDEVICEID;
1847 if (WOutDev[wDevID].ossdev->open_access == O_RDWR)
1848 memcpy(lpCaps, &WOutDev[wDevID].ossdev->duplex_out_caps, min(dwSize, sizeof(*lpCaps)));
1849 else
1850 memcpy(lpCaps, &WOutDev[wDevID].ossdev->out_caps, min(dwSize, sizeof(*lpCaps)));
1852 return MMSYSERR_NOERROR;
1855 /**************************************************************************
1856 * wodOpen [internal]
1858 DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1860 int audio_fragment;
1861 WINE_WAVEOUT* wwo;
1862 audio_buf_info info;
1863 DWORD ret;
1865 TRACE("(%u, %p[cb=%08lx], %08lX);\n", wDevID, lpDesc, lpDesc->dwCallback, dwFlags);
1866 if (lpDesc == NULL) {
1867 WARN("Invalid Parameter !\n");
1868 return MMSYSERR_INVALPARAM;
1870 if (wDevID >= numOutDev) {
1871 TRACE("MAX_WAVOUTDRV reached !\n");
1872 return MMSYSERR_BADDEVICEID;
1875 /* only PCM format is supported so far... */
1876 if (!supportedFormat(lpDesc->lpFormat)) {
1877 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1878 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1879 lpDesc->lpFormat->nSamplesPerSec);
1880 return WAVERR_BADFORMAT;
1883 if (dwFlags & WAVE_FORMAT_QUERY) {
1884 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1885 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1886 lpDesc->lpFormat->nSamplesPerSec);
1887 return MMSYSERR_NOERROR;
1890 TRACE("OSS_OpenDevice requested this format: %ldx%dx%d %s\n",
1891 lpDesc->lpFormat->nSamplesPerSec,
1892 lpDesc->lpFormat->wBitsPerSample,
1893 lpDesc->lpFormat->nChannels,
1894 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
1895 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
1896 "UNSUPPORTED");
1898 wwo = &WOutDev[wDevID];
1900 if ((dwFlags & WAVE_DIRECTSOUND) &&
1901 !(wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
1902 /* not supported, ignore it */
1903 dwFlags &= ~WAVE_DIRECTSOUND;
1905 if (dwFlags & WAVE_DIRECTSOUND) {
1906 if (wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1907 /* we have realtime DirectSound, fragments just waste our time,
1908 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1909 audio_fragment = 0x0020000B;
1910 else
1911 /* to approximate realtime, we must use small fragments,
1912 * let's try to fragment the above 64KB (256 * 2^8) */
1913 audio_fragment = 0x01000008;
1914 } else {
1915 /* A wave device must have a worst case latency of 10 ms so calculate
1916 * the largest fragment size less than 10 ms long.
1918 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
1919 int shift = 0;
1920 while ((1 << shift) <= fsize)
1921 shift++;
1922 shift--;
1923 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
1926 TRACE("requesting %d %d byte fragments (%ld ms/fragment)\n",
1927 audio_fragment >> 16, 1 << (audio_fragment & 0xffff),
1928 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
1930 if (wwo->state != WINE_WS_CLOSED) {
1931 WARN("already allocated\n");
1932 return MMSYSERR_ALLOCATED;
1935 /* we want to be able to mmap() the device, which means it must be opened readable,
1936 * otherwise mmap() will fail (at least under Linux) */
1937 ret = OSS_OpenDevice(wwo->ossdev,
1938 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
1939 &audio_fragment,
1940 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
1941 lpDesc->lpFormat->nSamplesPerSec,
1942 lpDesc->lpFormat->nChannels,
1943 (lpDesc->lpFormat->wBitsPerSample == 16)
1944 ? AFMT_S16_LE : AFMT_U8);
1945 if ((ret==MMSYSERR_NOERROR) && (dwFlags & WAVE_DIRECTSOUND)) {
1946 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev->sample_rate;
1947 lpDesc->lpFormat->nChannels=wwo->ossdev->channels;
1948 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev->format == AFMT_U8 ? 8 : 16);
1949 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
1950 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
1951 TRACE("OSS_OpenDevice returned this format: %ldx%dx%d\n",
1952 lpDesc->lpFormat->nSamplesPerSec,
1953 lpDesc->lpFormat->wBitsPerSample,
1954 lpDesc->lpFormat->nChannels);
1956 if (ret != 0) return ret;
1957 wwo->state = WINE_WS_STOPPED;
1959 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1961 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1962 copy_format(lpDesc->lpFormat, &wwo->waveFormat);
1964 if (wwo->waveFormat.Format.wBitsPerSample == 0) {
1965 WARN("Resetting zeroed wBitsPerSample\n");
1966 wwo->waveFormat.Format.wBitsPerSample = 8 *
1967 (wwo->waveFormat.Format.nAvgBytesPerSec /
1968 wwo->waveFormat.Format.nSamplesPerSec) /
1969 wwo->waveFormat.Format.nChannels;
1971 /* Read output space info for future reference */
1972 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
1973 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
1974 OSS_CloseDevice(wwo->ossdev);
1975 wwo->state = WINE_WS_CLOSED;
1976 return MMSYSERR_NOTENABLED;
1979 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
1980 info.fragsize, (info.fragsize * 1000) / (wwo->ossdev->sample_rate *
1981 wwo->ossdev->channels * (wwo->ossdev->format == AFMT_U8 ? 1 : 2)));
1983 /* Check that fragsize is correct per our settings above */
1984 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
1985 /* we've tried to set 1K fragments or less, but it didn't work */
1986 ERR("fragment size set failed, size is now %d\n", info.fragsize);
1987 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
1988 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
1991 /* Remember fragsize and total buffer size for future use */
1992 wwo->dwFragmentSize = info.fragsize;
1993 wwo->dwBufferSize = info.fragstotal * info.fragsize;
1994 wwo->dwPlayedTotal = 0;
1995 wwo->dwWrittenTotal = 0;
1996 wwo->bNeedPost = TRUE;
1998 TRACE("fd=%d fragstotal=%d fragsize=%d BufferSize=%ld\n",
1999 wwo->ossdev->fd, info.fragstotal, info.fragsize, wwo->dwBufferSize);
2000 if (wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign) {
2001 ERR("Fragment doesn't contain an integral number of data blocks fragsize=%ld BlockAlign=%d\n",wwo->dwFragmentSize,wwo->waveFormat.Format.nBlockAlign);
2002 /* Some SoundBlaster 16 cards return an incorrect (odd) fragment
2003 * size for 16 bit sound. This will cause a system crash when we try
2004 * to write just the specified odd number of bytes. So if we
2005 * detect something is wrong we'd better fix it.
2007 wwo->dwFragmentSize-=wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign;
2010 OSS_InitRingMessage(&wwo->msgRing);
2012 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2013 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
2014 if (wwo->hThread)
2015 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2016 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
2017 CloseHandle(wwo->hStartUpEvent);
2018 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
2020 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2021 wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
2022 wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
2023 wwo->waveFormat.Format.nBlockAlign);
2025 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
2028 /**************************************************************************
2029 * wodClose [internal]
2031 static DWORD wodClose(WORD wDevID)
2033 DWORD ret = MMSYSERR_NOERROR;
2034 WINE_WAVEOUT* wwo;
2036 TRACE("(%u);\n", wDevID);
2038 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2039 WARN("bad device ID !\n");
2040 return MMSYSERR_BADDEVICEID;
2043 wwo = &WOutDev[wDevID];
2044 if (wwo->lpQueuePtr) {
2045 WARN("buffers still playing !\n");
2046 ret = WAVERR_STILLPLAYING;
2047 } else {
2048 if (wwo->hThread != INVALID_HANDLE_VALUE) {
2049 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
2052 OSS_DestroyRingMessage(&wwo->msgRing);
2054 OSS_CloseDevice(wwo->ossdev);
2055 wwo->state = WINE_WS_CLOSED;
2056 wwo->dwFragmentSize = 0;
2057 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
2059 return ret;
2062 /**************************************************************************
2063 * wodWrite [internal]
2066 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2068 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2070 /* first, do the sanity checks... */
2071 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2072 WARN("bad dev ID !\n");
2073 return MMSYSERR_BADDEVICEID;
2076 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
2077 return WAVERR_UNPREPARED;
2079 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2080 return WAVERR_STILLPLAYING;
2082 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2083 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2084 lpWaveHdr->lpNext = 0;
2086 if ((lpWaveHdr->dwBufferLength & (WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1)) != 0)
2088 WARN("WaveHdr length isn't a multiple of the PCM block size: %ld %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].waveFormat.Format.nBlockAlign);
2089 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1);
2092 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2094 return MMSYSERR_NOERROR;
2097 /**************************************************************************
2098 * wodPause [internal]
2100 static DWORD wodPause(WORD wDevID)
2102 TRACE("(%u);!\n", wDevID);
2104 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2105 WARN("bad device ID !\n");
2106 return MMSYSERR_BADDEVICEID;
2109 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
2111 return MMSYSERR_NOERROR;
2114 /**************************************************************************
2115 * wodRestart [internal]
2117 static DWORD wodRestart(WORD wDevID)
2119 TRACE("(%u);\n", wDevID);
2121 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2122 WARN("bad device ID !\n");
2123 return MMSYSERR_BADDEVICEID;
2126 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2128 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2129 /* FIXME: Myst crashes with this ... hmm -MM
2130 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2133 return MMSYSERR_NOERROR;
2136 /**************************************************************************
2137 * wodReset [internal]
2139 static DWORD wodReset(WORD wDevID)
2141 TRACE("(%u);\n", wDevID);
2143 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2144 WARN("bad device ID !\n");
2145 return MMSYSERR_BADDEVICEID;
2148 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2150 return MMSYSERR_NOERROR;
2153 /**************************************************************************
2154 * wodGetPosition [internal]
2156 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2158 WINE_WAVEOUT* wwo;
2160 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
2162 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2163 WARN("bad device ID !\n");
2164 return MMSYSERR_BADDEVICEID;
2167 if (lpTime == NULL) {
2168 WARN("invalid parameter: lpTime == NULL\n");
2169 return MMSYSERR_INVALPARAM;
2172 wwo = &WOutDev[wDevID];
2173 #ifdef EXACT_WODPOSITION
2174 if (wwo->ossdev->open_access == O_RDWR) {
2175 if (wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2176 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2177 } else {
2178 if (wwo->ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2179 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2181 #endif
2183 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
2186 /**************************************************************************
2187 * wodBreakLoop [internal]
2189 static DWORD wodBreakLoop(WORD wDevID)
2191 TRACE("(%u);\n", wDevID);
2193 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2194 WARN("bad device ID !\n");
2195 return MMSYSERR_BADDEVICEID;
2197 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
2198 return MMSYSERR_NOERROR;
2201 /**************************************************************************
2202 * wodGetVolume [internal]
2204 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
2206 int mixer;
2207 int volume;
2208 DWORD left, right;
2209 DWORD last_left, last_right;
2211 TRACE("(%u, %p);\n", wDevID, lpdwVol);
2213 if (lpdwVol == NULL) {
2214 WARN("not enabled\n");
2215 return MMSYSERR_NOTENABLED;
2217 if (wDevID >= numOutDev) {
2218 WARN("invalid parameter\n");
2219 return MMSYSERR_INVALPARAM;
2222 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_RDONLY|O_NDELAY)) < 0) {
2223 WARN("mixer device not available !\n");
2224 return MMSYSERR_NOTENABLED;
2226 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
2227 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)\n",
2228 WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2229 return MMSYSERR_NOTENABLED;
2231 close(mixer);
2233 left = LOBYTE(volume);
2234 right = HIBYTE(volume);
2235 TRACE("left=%ld right=%ld !\n", left, right);
2236 last_left = (LOWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2237 last_right = (HIWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2238 TRACE("last_left=%ld last_right=%ld !\n", last_left, last_right);
2239 if (last_left == left && last_right == right)
2240 *lpdwVol = WOutDev[wDevID].volume;
2241 else
2242 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
2243 return MMSYSERR_NOERROR;
2246 /**************************************************************************
2247 * wodSetVolume [internal]
2249 DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
2251 int mixer;
2252 int volume;
2253 DWORD left, right;
2255 TRACE("(%u, %08lX);\n", wDevID, dwParam);
2257 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
2258 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
2259 volume = left + (right << 8);
2261 if (wDevID >= numOutDev) {
2262 WARN("invalid parameter: wDevID > %d\n", numOutDev);
2263 return MMSYSERR_INVALPARAM;
2265 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_WRONLY|O_NDELAY)) < 0) {
2266 WARN("open(%s) failed (%s)\n", WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2267 return MMSYSERR_NOTENABLED;
2269 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
2270 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n",
2271 WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2272 return MMSYSERR_NOTENABLED;
2273 } else {
2274 TRACE("volume=%04x\n", (unsigned)volume);
2276 close(mixer);
2278 /* save requested volume */
2279 WOutDev[wDevID].volume = dwParam;
2281 return MMSYSERR_NOERROR;
2284 /**************************************************************************
2285 * wodMessage (WINEOSS.7)
2287 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2288 DWORD dwParam1, DWORD dwParam2)
2290 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
2291 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
2293 switch (wMsg) {
2294 case DRVM_INIT:
2295 case DRVM_EXIT:
2296 case DRVM_ENABLE:
2297 case DRVM_DISABLE:
2298 /* FIXME: Pretend this is supported */
2299 return 0;
2300 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2301 case WODM_CLOSE: return wodClose (wDevID);
2302 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2303 case WODM_PAUSE: return wodPause (wDevID);
2304 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
2305 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
2306 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2307 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2308 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
2309 case WODM_GETNUMDEVS: return numOutDev;
2310 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
2311 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
2312 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2313 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2314 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
2315 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
2316 case WODM_RESTART: return wodRestart (wDevID);
2317 case WODM_RESET: return wodReset (wDevID);
2319 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2320 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2321 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
2322 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2323 default:
2324 FIXME("unknown message %d!\n", wMsg);
2326 return MMSYSERR_NOTSUPPORTED;
2329 /*======================================================================*
2330 * Low level WAVE IN implementation *
2331 *======================================================================*/
2333 /**************************************************************************
2334 * widNotifyClient [internal]
2336 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2338 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lX dwParam2 = %04lX\n", wMsg,
2339 wMsg == WIM_OPEN ? "WIM_OPEN" : wMsg == WIM_CLOSE ? "WIM_CLOSE" :
2340 wMsg == WIM_DATA ? "WIM_DATA" : "Unknown", dwParam1, dwParam2);
2342 switch (wMsg) {
2343 case WIM_OPEN:
2344 case WIM_CLOSE:
2345 case WIM_DATA:
2346 if (wwi->wFlags != DCB_NULL &&
2347 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2348 (HDRVR)wwi->waveDesc.hWave, wMsg,
2349 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2350 WARN("can't notify client !\n");
2351 return MMSYSERR_ERROR;
2353 break;
2354 default:
2355 FIXME("Unknown callback message %u\n", wMsg);
2356 return MMSYSERR_INVALPARAM;
2358 return MMSYSERR_NOERROR;
2361 /**************************************************************************
2362 * widGetDevCaps [internal]
2364 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
2366 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
2368 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2370 if (wDevID >= numInDev) {
2371 TRACE("numOutDev reached !\n");
2372 return MMSYSERR_BADDEVICEID;
2375 memcpy(lpCaps, &WInDev[wDevID].ossdev->in_caps, min(dwSize, sizeof(*lpCaps)));
2376 return MMSYSERR_NOERROR;
2379 /**************************************************************************
2380 * widRecorder_ReadHeaders [internal]
2382 static void widRecorder_ReadHeaders(WINE_WAVEIN * wwi)
2384 enum win_wm_message tmp_msg;
2385 DWORD tmp_param;
2386 HANDLE tmp_ev;
2387 WAVEHDR* lpWaveHdr;
2389 while (OSS_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
2390 if (tmp_msg == WINE_WM_HEADER) {
2391 LPWAVEHDR* wh;
2392 lpWaveHdr = (LPWAVEHDR)tmp_param;
2393 lpWaveHdr->lpNext = 0;
2395 if (wwi->lpQueuePtr == 0)
2396 wwi->lpQueuePtr = lpWaveHdr;
2397 else {
2398 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2399 *wh = lpWaveHdr;
2401 } else {
2402 ERR("should only have headers left\n");
2407 /**************************************************************************
2408 * widRecorder [internal]
2410 static DWORD CALLBACK widRecorder(LPVOID pmt)
2412 WORD uDevID = (DWORD)pmt;
2413 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2414 WAVEHDR* lpWaveHdr;
2415 DWORD dwSleepTime;
2416 DWORD bytesRead;
2417 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2418 char *pOffset = buffer;
2419 audio_buf_info info;
2420 int xs;
2421 enum win_wm_message msg;
2422 DWORD param;
2423 HANDLE ev;
2424 int enable;
2426 wwi->state = WINE_WS_STOPPED;
2427 wwi->dwTotalRecorded = 0;
2428 wwi->dwTotalRead = 0;
2429 wwi->lpQueuePtr = NULL;
2431 SetEvent(wwi->hStartUpEvent);
2433 /* disable input so capture will begin when triggered */
2434 wwi->ossdev->bInputEnabled = FALSE;
2435 enable = getEnables(wwi->ossdev);
2436 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2437 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2439 /* the soundblaster live needs a micro wake to get its recording started
2440 * (or GETISPACE will have 0 frags all the time)
2442 read(wwi->ossdev->fd, &xs, 4);
2444 /* make sleep time to be # of ms to output a fragment */
2445 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
2446 TRACE("sleeptime=%ld ms\n", dwSleepTime);
2448 for (;;) {
2449 /* wait for dwSleepTime or an event in thread's queue */
2450 /* FIXME: could improve wait time depending on queue state,
2451 * ie, number of queued fragments
2454 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2456 lpWaveHdr = wwi->lpQueuePtr;
2458 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info);
2459 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2461 /* read all the fragments accumulated so far */
2462 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2464 info.fragments --;
2466 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2468 /* directly read fragment in wavehdr */
2469 bytesRead = read(wwi->ossdev->fd,
2470 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2471 wwi->dwFragmentSize);
2473 TRACE("bytesRead=%ld (direct)\n", bytesRead);
2474 if (bytesRead != (DWORD) -1)
2476 /* update number of bytes recorded in current buffer and by this device */
2477 lpWaveHdr->dwBytesRecorded += bytesRead;
2478 wwi->dwTotalRead += bytesRead;
2479 wwi->dwTotalRecorded = wwi->dwTotalRead;
2481 /* buffer is full. notify client */
2482 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2484 /* must copy the value of next waveHdr, because we have no idea of what
2485 * will be done with the content of lpWaveHdr in callback
2487 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2489 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2490 lpWaveHdr->dwFlags |= WHDR_DONE;
2492 wwi->lpQueuePtr = lpNext;
2493 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2494 lpWaveHdr = lpNext;
2496 } else {
2497 TRACE("read(%s, %p, %ld) failed (%s)\n", wwi->ossdev->dev_name,
2498 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2499 wwi->dwFragmentSize, strerror(errno));
2502 else
2504 /* read the fragment in a local buffer */
2505 bytesRead = read(wwi->ossdev->fd, buffer, wwi->dwFragmentSize);
2506 pOffset = buffer;
2508 TRACE("bytesRead=%ld (local)\n", bytesRead);
2510 if (bytesRead == (DWORD) -1) {
2511 TRACE("read(%s, %p, %ld) failed (%s)\n", wwi->ossdev->dev_name,
2512 buffer, wwi->dwFragmentSize, strerror(errno));
2513 continue;
2516 /* copy data in client buffers */
2517 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2519 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2521 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2522 pOffset,
2523 dwToCopy);
2525 /* update number of bytes recorded in current buffer and by this device */
2526 lpWaveHdr->dwBytesRecorded += dwToCopy;
2527 wwi->dwTotalRead += dwToCopy;
2528 wwi->dwTotalRecorded = wwi->dwTotalRead;
2529 bytesRead -= dwToCopy;
2530 pOffset += dwToCopy;
2532 /* client buffer is full. notify client */
2533 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2535 /* must copy the value of next waveHdr, because we have no idea of what
2536 * will be done with the content of lpWaveHdr in callback
2538 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2539 TRACE("lpNext=%p\n", lpNext);
2541 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2542 lpWaveHdr->dwFlags |= WHDR_DONE;
2544 wwi->lpQueuePtr = lpNext;
2545 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2547 lpWaveHdr = lpNext;
2548 if (!lpNext && bytesRead) {
2549 /* before we give up, check for more header messages */
2550 while (OSS_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
2552 if (msg == WINE_WM_HEADER) {
2553 LPWAVEHDR hdr;
2554 OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
2555 hdr = ((LPWAVEHDR)param);
2556 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg), hdr, ev);
2557 hdr->lpNext = 0;
2558 if (lpWaveHdr == 0) {
2559 /* new head of queue */
2560 wwi->lpQueuePtr = lpWaveHdr = hdr;
2561 } else {
2562 /* insert buffer at the end of queue */
2563 LPWAVEHDR* wh;
2564 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2565 *wh = hdr;
2567 } else
2568 break;
2571 if (lpWaveHdr == 0) {
2572 /* no more buffer to copy data to, but we did read more.
2573 * what hasn't been copied will be dropped
2575 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
2576 wwi->lpQueuePtr = NULL;
2577 break;
2586 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2588 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2590 TRACE("msg=%s param=0x%lx\n", getCmdString(msg), param);
2591 switch (msg) {
2592 case WINE_WM_PAUSING:
2593 wwi->state = WINE_WS_PAUSED;
2594 /*FIXME("Device should stop recording\n");*/
2595 SetEvent(ev);
2596 break;
2597 case WINE_WM_STARTING:
2598 wwi->state = WINE_WS_PLAYING;
2600 if (wwi->ossdev->bTriggerSupport)
2602 /* start the recording */
2603 wwi->ossdev->bInputEnabled = TRUE;
2604 enable = getEnables(wwi->ossdev);
2605 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2606 wwi->ossdev->bInputEnabled = FALSE;
2607 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2610 else
2612 unsigned char data[4];
2613 /* read 4 bytes to start the recording */
2614 read(wwi->ossdev->fd, data, 4);
2617 SetEvent(ev);
2618 break;
2619 case WINE_WM_HEADER:
2620 lpWaveHdr = (LPWAVEHDR)param;
2621 lpWaveHdr->lpNext = 0;
2623 /* insert buffer at the end of queue */
2625 LPWAVEHDR* wh;
2626 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2627 *wh = lpWaveHdr;
2629 break;
2630 case WINE_WM_STOPPING:
2631 if (wwi->state != WINE_WS_STOPPED)
2633 if (wwi->ossdev->bTriggerSupport)
2635 /* stop the recording */
2636 wwi->ossdev->bInputEnabled = FALSE;
2637 enable = getEnables(wwi->ossdev);
2638 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2639 wwi->ossdev->bInputEnabled = FALSE;
2640 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2644 /* read any headers in queue */
2645 widRecorder_ReadHeaders(wwi);
2647 /* return current buffer to app */
2648 lpWaveHdr = wwi->lpQueuePtr;
2649 if (lpWaveHdr)
2651 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2652 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2653 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2654 lpWaveHdr->dwFlags |= WHDR_DONE;
2655 wwi->lpQueuePtr = lpNext;
2656 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2659 wwi->state = WINE_WS_STOPPED;
2660 SetEvent(ev);
2661 break;
2662 case WINE_WM_RESETTING:
2663 if (wwi->state != WINE_WS_STOPPED)
2665 if (wwi->ossdev->bTriggerSupport)
2667 /* stop the recording */
2668 wwi->ossdev->bInputEnabled = FALSE;
2669 enable = getEnables(wwi->ossdev);
2670 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2671 wwi->ossdev->bInputEnabled = FALSE;
2672 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2676 wwi->state = WINE_WS_STOPPED;
2677 wwi->dwTotalRecorded = 0;
2678 wwi->dwTotalRead = 0;
2680 /* read any headers in queue */
2681 widRecorder_ReadHeaders(wwi);
2683 /* return all buffers to the app */
2684 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2685 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2686 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2687 lpWaveHdr->dwFlags |= WHDR_DONE;
2688 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2689 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2692 wwi->lpQueuePtr = NULL;
2693 SetEvent(ev);
2694 break;
2695 case WINE_WM_UPDATE:
2696 if (wwi->state == WINE_WS_PLAYING) {
2697 audio_buf_info tmp_info;
2698 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &tmp_info) < 0)
2699 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2700 else
2701 wwi->dwTotalRecorded = wwi->dwTotalRead + tmp_info.bytes;
2703 SetEvent(ev);
2704 break;
2705 case WINE_WM_CLOSING:
2706 wwi->hThread = 0;
2707 wwi->state = WINE_WS_CLOSED;
2708 SetEvent(ev);
2709 HeapFree(GetProcessHeap(), 0, buffer);
2710 ExitThread(0);
2711 /* shouldn't go here */
2712 default:
2713 FIXME("unknown message %d\n", msg);
2714 break;
2718 ExitThread(0);
2719 /* just for not generating compilation warnings... should never be executed */
2720 return 0;
2724 /**************************************************************************
2725 * widOpen [internal]
2727 DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2729 WINE_WAVEIN* wwi;
2730 audio_buf_info info;
2731 int audio_fragment;
2732 DWORD ret;
2734 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
2735 if (lpDesc == NULL) {
2736 WARN("Invalid Parameter !\n");
2737 return MMSYSERR_INVALPARAM;
2739 if (wDevID >= numInDev) {
2740 WARN("bad device id: %d >= %d\n", wDevID, numInDev);
2741 return MMSYSERR_BADDEVICEID;
2744 /* only PCM format is supported so far... */
2745 if (!supportedFormat(lpDesc->lpFormat)) {
2746 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2747 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2748 lpDesc->lpFormat->nSamplesPerSec);
2749 return WAVERR_BADFORMAT;
2752 if (dwFlags & WAVE_FORMAT_QUERY) {
2753 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2754 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2755 lpDesc->lpFormat->nSamplesPerSec);
2756 return MMSYSERR_NOERROR;
2759 TRACE("OSS_OpenDevice requested this format: %ldx%dx%d %s\n",
2760 lpDesc->lpFormat->nSamplesPerSec,
2761 lpDesc->lpFormat->wBitsPerSample,
2762 lpDesc->lpFormat->nChannels,
2763 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
2764 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
2765 "UNSUPPORTED");
2767 wwi = &WInDev[wDevID];
2769 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2771 if ((dwFlags & WAVE_DIRECTSOUND) &&
2772 !(wwi->ossdev->in_caps_support & WAVECAPS_DIRECTSOUND))
2773 /* not supported, ignore it */
2774 dwFlags &= ~WAVE_DIRECTSOUND;
2776 if (dwFlags & WAVE_DIRECTSOUND) {
2777 TRACE("has DirectSoundCapture driver\n");
2778 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
2779 /* we have realtime DirectSound, fragments just waste our time,
2780 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2781 audio_fragment = 0x0020000B;
2782 else
2783 /* to approximate realtime, we must use small fragments,
2784 * let's try to fragment the above 64KB (256 * 2^8) */
2785 audio_fragment = 0x01000008;
2786 } else {
2787 TRACE("doesn't have DirectSoundCapture driver\n");
2788 if (wwi->ossdev->open_count > 0) {
2789 TRACE("Using output device audio_fragment\n");
2790 /* FIXME: This may not be optimal for capture but it allows us
2791 * to do hardware playback without hardware capture. */
2792 audio_fragment = wwi->ossdev->audio_fragment;
2793 } else {
2794 /* A wave device must have a worst case latency of 10 ms so calculate
2795 * the largest fragment size less than 10 ms long.
2797 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2798 int shift = 0;
2799 while ((1 << shift) <= fsize)
2800 shift++;
2801 shift--;
2802 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2806 TRACE("requesting %d %d byte fragments (%ld ms)\n", audio_fragment >> 16,
2807 1 << (audio_fragment & 0xffff),
2808 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2810 ret = OSS_OpenDevice(wwi->ossdev, O_RDONLY, &audio_fragment,
2812 lpDesc->lpFormat->nSamplesPerSec,
2813 lpDesc->lpFormat->nChannels,
2814 (lpDesc->lpFormat->wBitsPerSample == 16)
2815 ? AFMT_S16_LE : AFMT_U8);
2816 if (ret != 0) return ret;
2817 wwi->state = WINE_WS_STOPPED;
2819 if (wwi->lpQueuePtr) {
2820 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2821 wwi->lpQueuePtr = NULL;
2823 wwi->dwTotalRecorded = 0;
2824 wwi->dwTotalRead = 0;
2825 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2827 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2828 copy_format(lpDesc->lpFormat, &wwi->waveFormat);
2830 if (wwi->waveFormat.Format.wBitsPerSample == 0) {
2831 WARN("Resetting zeroed wBitsPerSample\n");
2832 wwi->waveFormat.Format.wBitsPerSample = 8 *
2833 (wwi->waveFormat.Format.nAvgBytesPerSec /
2834 wwi->waveFormat.Format.nSamplesPerSec) /
2835 wwi->waveFormat.Format.nChannels;
2838 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
2839 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
2840 wwi->ossdev->dev_name, strerror(errno));
2841 OSS_CloseDevice(wwi->ossdev);
2842 wwi->state = WINE_WS_CLOSED;
2843 return MMSYSERR_NOTENABLED;
2846 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2847 info.fragsize, (info.fragsize * 1000) / (wwi->ossdev->sample_rate *
2848 wwi->ossdev->channels * (wwi->ossdev->format == AFMT_U8 ? 1 : 2)));
2850 wwi->dwFragmentSize = info.fragsize;
2852 TRACE("dwFragmentSize=%lu\n", wwi->dwFragmentSize);
2853 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2854 wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
2855 wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
2856 wwi->waveFormat.Format.nBlockAlign);
2858 OSS_InitRingMessage(&wwi->msgRing);
2860 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2861 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
2862 if (wwi->hThread)
2863 SetThreadPriority(wwi->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2864 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2865 CloseHandle(wwi->hStartUpEvent);
2866 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2868 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2871 /**************************************************************************
2872 * widClose [internal]
2874 static DWORD widClose(WORD wDevID)
2876 WINE_WAVEIN* wwi;
2878 TRACE("(%u);\n", wDevID);
2879 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2880 WARN("can't close !\n");
2881 return MMSYSERR_INVALHANDLE;
2884 wwi = &WInDev[wDevID];
2886 if (wwi->lpQueuePtr != NULL) {
2887 WARN("still buffers open !\n");
2888 return WAVERR_STILLPLAYING;
2891 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
2892 OSS_CloseDevice(wwi->ossdev);
2893 wwi->state = WINE_WS_CLOSED;
2894 wwi->dwFragmentSize = 0;
2895 OSS_DestroyRingMessage(&wwi->msgRing);
2896 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
2899 /**************************************************************************
2900 * widAddBuffer [internal]
2902 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2904 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2906 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2907 WARN("can't do it !\n");
2908 return MMSYSERR_INVALHANDLE;
2910 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2911 TRACE("never been prepared !\n");
2912 return WAVERR_UNPREPARED;
2914 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2915 TRACE("header already in use !\n");
2916 return WAVERR_STILLPLAYING;
2919 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2920 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2921 lpWaveHdr->dwBytesRecorded = 0;
2922 lpWaveHdr->lpNext = NULL;
2924 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2925 return MMSYSERR_NOERROR;
2928 /**************************************************************************
2929 * widStart [internal]
2931 static DWORD widStart(WORD wDevID)
2933 TRACE("(%u);\n", wDevID);
2934 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2935 WARN("can't start recording !\n");
2936 return MMSYSERR_INVALHANDLE;
2939 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
2940 return MMSYSERR_NOERROR;
2943 /**************************************************************************
2944 * widStop [internal]
2946 static DWORD widStop(WORD wDevID)
2948 TRACE("(%u);\n", wDevID);
2949 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2950 WARN("can't stop !\n");
2951 return MMSYSERR_INVALHANDLE;
2954 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
2956 return MMSYSERR_NOERROR;
2959 /**************************************************************************
2960 * widReset [internal]
2962 static DWORD widReset(WORD wDevID)
2964 TRACE("(%u);\n", wDevID);
2965 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2966 WARN("can't reset !\n");
2967 return MMSYSERR_INVALHANDLE;
2969 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2970 return MMSYSERR_NOERROR;
2973 /**************************************************************************
2974 * widGetPosition [internal]
2976 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2978 WINE_WAVEIN* wwi;
2980 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
2982 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2983 WARN("can't get pos !\n");
2984 return MMSYSERR_INVALHANDLE;
2987 if (lpTime == NULL) {
2988 WARN("invalid parameter: lpTime == NULL\n");
2989 return MMSYSERR_INVALPARAM;
2992 wwi = &WInDev[wDevID];
2993 #ifdef EXACT_WIDPOSITION
2994 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
2995 OSS_AddRingMessage(&(wwi->msgRing), WINE_WM_UPDATE, 0, TRUE);
2996 #endif
2998 return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->waveFormat);
3001 /**************************************************************************
3002 * widMessage (WINEOSS.6)
3004 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3005 DWORD dwParam1, DWORD dwParam2)
3007 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
3008 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
3010 switch (wMsg) {
3011 case DRVM_INIT:
3012 case DRVM_EXIT:
3013 case DRVM_ENABLE:
3014 case DRVM_DISABLE:
3015 /* FIXME: Pretend this is supported */
3016 return 0;
3017 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3018 case WIDM_CLOSE: return widClose (wDevID);
3019 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3020 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
3021 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
3022 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
3023 case WIDM_GETNUMDEVS: return numInDev;
3024 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
3025 case WIDM_RESET: return widReset (wDevID);
3026 case WIDM_START: return widStart (wDevID);
3027 case WIDM_STOP: return widStop (wDevID);
3028 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
3029 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
3030 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3031 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3032 default:
3033 FIXME("unknown message %u!\n", wMsg);
3035 return MMSYSERR_NOTSUPPORTED;
3038 #else /* !HAVE_OSS */
3040 /**************************************************************************
3041 * wodMessage (WINEOSS.7)
3043 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3044 DWORD dwParam1, DWORD dwParam2)
3046 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3047 return MMSYSERR_NOTENABLED;
3050 /**************************************************************************
3051 * widMessage (WINEOSS.6)
3053 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3054 DWORD dwParam1, DWORD dwParam2)
3056 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3057 return MMSYSERR_NOTENABLED;
3060 #endif /* HAVE_OSS */