2 * Sample Wine Driver for Open Sound System (featured in Linux and FreeBSD)
4 * Copyright 1994 Martin Ayotte
5 * 1999 Eric Pouech (async playing in waveOut/waveIn)
6 * 2000 Eric Pouech (loops in waveOut)
7 * 2002 Eric Pouech (full duplex)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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
35 #include "wine/port.h"
46 #ifdef HAVE_SYS_IOCTL_H
47 # include <sys/ioctl.h>
49 #ifdef HAVE_SYS_MMAN_H
50 # include <sys/mman.h>
55 #ifdef HAVE_SYS_POLL_H
56 # include <sys/poll.h>
58 #ifdef HAVE_SYS_ERRNO_H
59 #include <sys/errno.h>
61 #include <sys/soundcard.h>
73 #include "wine/debug.h"
81 WINE_DEFAULT_DEBUG_CHANNEL(wave);
83 /* Allow 1% deviation for sample rates (some ES137x cards) */
84 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
86 WINE_WAVEOUT WOutDev[MAX_WAVEDRV];
87 WINE_WAVEIN WInDev[MAX_WAVEDRV];
91 /* state diagram for waveOut writing:
93 * +---------+-------------+---------------+---------------------------------+
94 * | state | function | event | new state |
95 * +---------+-------------+---------------+---------------------------------+
96 * | | open() | | STOPPED |
97 * | PAUSED | write() | | PAUSED |
98 * | STOPPED | write() | <thrd create> | PLAYING |
99 * | PLAYING | write() | HEADER | PLAYING |
100 * | (other) | write() | <error> | |
101 * | (any) | pause() | PAUSING | PAUSED |
102 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
103 * | (any) | reset() | RESETTING | STOPPED |
104 * | (any) | close() | CLOSING | CLOSED |
105 * +---------+-------------+---------------+---------------------------------+
108 /* These strings used only for tracing */
109 static const char * getCmdString(enum win_wm_message msg)
111 #define MSG_TO_STR(x) case x: return #x
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);
124 return wine_dbg_sprintf("UNKNOWN(0x%08x)", msg);
127 int getEnables(OSS_DEVICE *ossdev)
129 return ( (ossdev->bOutputEnabled ? PCM_ENABLE_OUTPUT : 0) |
130 (ossdev->bInputEnabled ? PCM_ENABLE_INPUT : 0) );
133 static const char * getMessage(UINT msg)
135 #define MSG_TO_STR(x) case x: return #x
137 MSG_TO_STR(DRVM_INIT);
138 MSG_TO_STR(DRVM_EXIT);
139 MSG_TO_STR(DRVM_ENABLE);
140 MSG_TO_STR(DRVM_DISABLE);
141 MSG_TO_STR(WIDM_OPEN);
142 MSG_TO_STR(WIDM_CLOSE);
143 MSG_TO_STR(WIDM_ADDBUFFER);
144 MSG_TO_STR(WIDM_PREPARE);
145 MSG_TO_STR(WIDM_UNPREPARE);
146 MSG_TO_STR(WIDM_GETDEVCAPS);
147 MSG_TO_STR(WIDM_GETNUMDEVS);
148 MSG_TO_STR(WIDM_GETPOS);
149 MSG_TO_STR(WIDM_RESET);
150 MSG_TO_STR(WIDM_START);
151 MSG_TO_STR(WIDM_STOP);
152 MSG_TO_STR(WODM_OPEN);
153 MSG_TO_STR(WODM_CLOSE);
154 MSG_TO_STR(WODM_WRITE);
155 MSG_TO_STR(WODM_PAUSE);
156 MSG_TO_STR(WODM_GETPOS);
157 MSG_TO_STR(WODM_BREAKLOOP);
158 MSG_TO_STR(WODM_PREPARE);
159 MSG_TO_STR(WODM_UNPREPARE);
160 MSG_TO_STR(WODM_GETDEVCAPS);
161 MSG_TO_STR(WODM_GETNUMDEVS);
162 MSG_TO_STR(WODM_GETPITCH);
163 MSG_TO_STR(WODM_SETPITCH);
164 MSG_TO_STR(WODM_GETPLAYBACKRATE);
165 MSG_TO_STR(WODM_SETPLAYBACKRATE);
166 MSG_TO_STR(WODM_GETVOLUME);
167 MSG_TO_STR(WODM_SETVOLUME);
168 MSG_TO_STR(WODM_RESTART);
169 MSG_TO_STR(WODM_RESET);
170 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
171 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
172 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
173 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
176 return wine_dbg_sprintf("UNKNOWN(0x%04x)", msg);
179 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
181 TRACE("(%u, %p)\n", wDevID, dwParam1);
183 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].ossdev.interface_name, -1,
184 NULL, 0 ) * sizeof(WCHAR);
185 return MMSYSERR_NOERROR;
188 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
190 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].ossdev.interface_name, -1,
191 NULL, 0 ) * sizeof(WCHAR))
193 MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].ossdev.interface_name, -1,
194 dwParam1, dwParam2 / sizeof(WCHAR));
195 return MMSYSERR_NOERROR;
198 return MMSYSERR_INVALPARAM;
201 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
203 TRACE("(%u, %p)\n", wDevID, dwParam1);
205 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].ossdev.interface_name, -1,
206 NULL, 0 ) * sizeof(WCHAR);
207 return MMSYSERR_NOERROR;
210 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
212 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].ossdev.interface_name, -1,
213 NULL, 0 ) * sizeof(WCHAR))
215 MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].ossdev.interface_name, -1,
216 dwParam1, dwParam2 / sizeof(WCHAR));
217 return MMSYSERR_NOERROR;
220 return MMSYSERR_INVALPARAM;
223 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
224 WAVEFORMATPCMEX* format)
226 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
227 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
228 format->Format.nChannels, format->Format.nAvgBytesPerSec);
229 TRACE("Position in bytes=%u\n", position);
231 switch (lpTime->wType) {
233 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
234 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
237 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
238 TRACE("TIME_MS=%u\n", lpTime->u.ms);
241 lpTime->u.smpte.fps = 30;
242 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
243 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
244 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
245 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
246 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
247 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
248 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
249 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
250 lpTime->u.smpte.fps = 30;
251 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
252 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
253 lpTime->u.smpte.hour, lpTime->u.smpte.min,
254 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
257 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
258 lpTime->wType = TIME_BYTES;
261 lpTime->u.cb = position;
262 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
265 return MMSYSERR_NOERROR;
268 static BOOL supportedFormat(LPWAVEFORMATEX wf)
272 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
275 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
276 if (wf->nChannels >= 1 && wf->nChannels <= MAX_CHANNELS) {
277 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
280 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
281 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
283 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
284 if (wf->nChannels >=1 && wf->nChannels <= MAX_CHANNELS) {
285 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
286 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
289 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
292 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
294 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
299 void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
301 ZeroMemory(wf2, sizeof(*wf2));
302 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
303 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
304 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
305 memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
307 memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
310 /*======================================================================*
311 * Low level WAVE implementation *
312 *======================================================================*/
314 /******************************************************************
317 * Low level device opening (from values stored in ossdev)
319 static DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
322 TRACE("(%p,%d)\n",ossdev,strict_format);
324 TRACE("open_access=%s\n",
325 ossdev->open_access == O_RDONLY ? "O_RDONLY" :
326 ossdev->open_access == O_WRONLY ? "O_WRONLY" :
327 ossdev->open_access == O_RDWR ? "O_RDWR" : "Unknown");
329 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
331 WARN("Couldn't open %s (%s)\n", ossdev->dev_name, strerror(errno));
332 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
334 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
335 /* turn full duplex on if it has been requested */
336 if (ossdev->open_access == O_RDWR && ossdev->full_duplex) {
337 rc = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
338 /* on *BSD, as full duplex is always enabled by default, this ioctl
339 * will fail with EINVAL
340 * so, we don't consider EINVAL an error here
342 if (rc != 0 && errno != EINVAL) {
343 WARN("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev->dev_name, strerror(errno));
348 if (ossdev->audio_fragment) {
349 rc = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
351 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev->dev_name, strerror(errno));
356 /* First size and channels then samplerate */
357 if (ossdev->format>=0)
359 val = ossdev->format;
360 rc = ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
361 if (rc != 0 || val != ossdev->format) {
362 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
367 if (ossdev->channels>=0)
369 val = ossdev->channels;
370 rc = ioctl(fd, SNDCTL_DSP_CHANNELS, &ossdev->channels);
371 if (rc != 0 || val != ossdev->channels) {
372 TRACE("Can't set channels to %u (returned %d)\n", val, ossdev->channels);
377 if (ossdev->sample_rate>=0)
379 val = ossdev->sample_rate;
380 rc = ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
381 if (rc != 0 || !NEAR_MATCH(val, ossdev->sample_rate)) {
382 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
389 ossdev->bOutputEnabled = TRUE; /* OSS enables by default */
390 ossdev->bInputEnabled = TRUE; /* OSS enables by default */
391 if (ossdev->open_access == O_RDONLY)
392 ossdev->bOutputEnabled = FALSE;
393 if (ossdev->open_access == O_WRONLY)
394 ossdev->bInputEnabled = FALSE;
396 if (ossdev->bTriggerSupport) {
398 trigger = getEnables(ossdev);
399 /* If we do not have full duplex, but they opened RDWR
400 ** (as you have to in order for an mmap to succeed)
401 ** then we start out with input off
403 if (ossdev->open_access == O_RDWR && !ossdev->full_duplex &&
404 ossdev->bInputEnabled && ossdev->bOutputEnabled) {
405 ossdev->bInputEnabled = FALSE;
406 trigger &= ~PCM_ENABLE_INPUT;
407 ioctl(fd, SNDCTL_DSP_SETTRIGGER, &trigger);
411 return MMSYSERR_NOERROR;
415 return WAVERR_BADFORMAT;
418 return MMSYSERR_ERROR;
421 /******************************************************************
424 * since OSS has poor capabilities in full duplex, we try here to let a program
425 * open the device for both waveout and wavein streams...
426 * this is hackish, but it's the way OSS interface is done...
428 DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
429 int* frag, int strict_format,
430 int sample_rate, int channels, int fmt)
434 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev,req_access,frag,strict_format,sample_rate,channels,fmt);
436 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
438 TRACE("Opening RDWR because full_duplex=%d and req_access=%d\n",
439 ossdev->full_duplex,req_access);
440 open_access = O_RDWR;
444 open_access=req_access;
447 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
448 if (ossdev->open_count == 0)
450 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
452 ossdev->audio_fragment = (frag) ? *frag : 0;
453 ossdev->sample_rate = sample_rate;
454 ossdev->channels = channels;
455 ossdev->format = fmt;
456 ossdev->open_access = open_access;
457 ossdev->owner_tid = GetCurrentThreadId();
459 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
460 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
461 (req_access == O_RDONLY || req_access == O_WRONLY))
464 if (req_access == O_WRONLY)
465 ossdev->bInputEnabled=0;
467 ossdev->bOutputEnabled=0;
468 enable = getEnables(ossdev);
469 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
470 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
471 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
476 /* check we really open with the same parameters */
477 if (ossdev->open_access != open_access)
479 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
480 return WAVERR_BADFORMAT;
483 /* check if the audio parameters are the same */
484 if (ossdev->sample_rate != sample_rate ||
485 ossdev->channels != channels ||
486 ossdev->format != fmt)
488 /* This is not a fatal error because MSACM might do the remapping */
489 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
490 "OSS doesn't allow us different parameters\n"
491 "audio_frag(%x/%x) sample_rate(%d/%d) channels(%d/%d) fmt(%d/%d)\n",
492 ossdev->audio_fragment, frag ? *frag : 0,
493 ossdev->sample_rate, sample_rate,
494 ossdev->channels, channels,
495 ossdev->format, fmt);
496 return WAVERR_BADFORMAT;
498 /* check if the fragment sizes are the same */
499 if (ossdev->audio_fragment != (frag ? *frag : 0) )
501 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
502 "Please run winecfg, open \"Audio\" page and set\n"
503 "\"Hardware Acceleration\" to \"Emulation\".\n");
504 return WAVERR_BADFORMAT;
506 if (GetCurrentThreadId() != ossdev->owner_tid)
508 WARN("Another thread is trying to access audio...\n");
509 return MMSYSERR_ERROR;
511 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
512 (req_access == O_RDONLY || req_access == O_WRONLY))
515 if (req_access == O_WRONLY)
516 ossdev->bOutputEnabled=1;
518 ossdev->bInputEnabled=1;
519 enable = getEnables(ossdev);
520 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
521 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
522 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
526 ossdev->open_count++;
528 return MMSYSERR_NOERROR;
531 /******************************************************************
536 void OSS_CloseDevice(OSS_DEVICE* ossdev)
538 TRACE("(%p)\n",ossdev);
539 if (ossdev->open_count>0) {
540 ossdev->open_count--;
542 WARN("OSS_CloseDevice called too many times\n");
544 if (ossdev->open_count == 0)
546 fcntl(ossdev->fd, F_SETFL, fcntl(ossdev->fd, F_GETFL) & ~O_NDELAY);
547 /* reset the device before we close it in case it is in a bad state */
548 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
549 if (close(ossdev->fd) != 0) FIXME("Cannot close %d: %s\n", ossdev->fd, strerror(errno));
553 /******************************************************************
556 * Resets the device. OSS Commercial requires the device to be closed
557 * after a SNDCTL_DSP_RESET ioctl call... this function implements
559 * FIXME: This causes problems when doing full duplex so we really
560 * only reset when not doing full duplex. We need to do this better
563 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
565 DWORD ret = MMSYSERR_NOERROR;
566 int old_fd = ossdev->fd;
567 TRACE("(%p)\n", ossdev);
569 if (ossdev->open_count == 1) {
570 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
572 perror("ioctl SNDCTL_DSP_RESET");
576 ret = OSS_RawOpenDevice(ossdev, 1);
577 TRACE("Changing fd from %d to %d\n", old_fd, ossdev->fd);
579 WARN("Not resetting device because it is in full duplex mode!\n");
584 static const int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
585 static const int win_std_rates[5]={96000,48000,44100,22050,11025};
586 static const int win_std_formats[2][2][5]=
587 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
588 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
589 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
590 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
591 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
592 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
593 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
594 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
597 static void OSS_Info(int fd)
599 /* Note that this only reports the formats supported by the hardware.
600 * The driver may support other formats and do the conversions in
601 * software which is why we don't use this value
603 int oss_mask, oss_caps;
604 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &oss_mask) >= 0) {
605 TRACE("Formats=%08x ( ", oss_mask);
606 if (oss_mask & AFMT_MU_LAW) TRACE("AFMT_MU_LAW ");
607 if (oss_mask & AFMT_A_LAW) TRACE("AFMT_A_LAW ");
608 if (oss_mask & AFMT_IMA_ADPCM) TRACE("AFMT_IMA_ADPCM ");
609 if (oss_mask & AFMT_U8) TRACE("AFMT_U8 ");
610 if (oss_mask & AFMT_S16_LE) TRACE("AFMT_S16_LE ");
611 if (oss_mask & AFMT_S16_BE) TRACE("AFMT_S16_BE ");
612 if (oss_mask & AFMT_S8) TRACE("AFMT_S8 ");
613 if (oss_mask & AFMT_U16_LE) TRACE("AFMT_U16_LE ");
614 if (oss_mask & AFMT_U16_BE) TRACE("AFMT_U16_BE ");
615 if (oss_mask & AFMT_MPEG) TRACE("AFMT_MPEG ");
617 if (oss_mask & AFMT_AC3) TRACE("AFMT_AC3 ");
620 if (oss_mask & AFMT_VORBIS) TRACE("AFMT_VORBIS ");
623 if (oss_mask & AFMT_S32_LE) TRACE("AFMT_S32_LE ");
626 if (oss_mask & AFMT_S32_BE) TRACE("AFMT_S32_BE ");
629 if (oss_mask & AFMT_FLOAT) TRACE("AFMT_FLOAT ");
632 if (oss_mask & AFMT_S24_LE) TRACE("AFMT_S24_LE ");
635 if (oss_mask & AFMT_S24_BE) TRACE("AFMT_S24_BE ");
637 #ifdef AFMT_SPDIF_RAW
638 if (oss_mask & AFMT_SPDIF_RAW) TRACE("AFMT_SPDIF_RAW ");
642 if (ioctl(fd, SNDCTL_DSP_GETCAPS, &oss_caps) >= 0) {
643 TRACE("Caps=%08x\n",oss_caps);
644 TRACE("\tRevision: %d\n", oss_caps&DSP_CAP_REVISION);
645 TRACE("\tDuplex: %s\n", oss_caps & DSP_CAP_DUPLEX ? "true" : "false");
646 TRACE("\tRealtime: %s\n", oss_caps & DSP_CAP_REALTIME ? "true" : "false");
647 TRACE("\tBatch: %s\n", oss_caps & DSP_CAP_BATCH ? "true" : "false");
648 TRACE("\tCoproc: %s\n", oss_caps & DSP_CAP_COPROC ? "true" : "false");
649 TRACE("\tTrigger: %s\n", oss_caps & DSP_CAP_TRIGGER ? "true" : "false");
650 TRACE("\tMmap: %s\n", oss_caps & DSP_CAP_MMAP ? "true" : "false");
652 TRACE("\tMulti: %s\n", oss_caps & DSP_CAP_MULTI ? "true" : "false");
655 TRACE("\tBind: %s\n", oss_caps & DSP_CAP_BIND ? "true" : "false");
658 TRACE("\tInput: %s\n", oss_caps & DSP_CAP_INPUT ? "true" : "false");
660 #ifdef DSP_CAP_OUTPUT
661 TRACE("\tOutput: %s\n", oss_caps & DSP_CAP_OUTPUT ? "true" : "false");
663 #ifdef DSP_CAP_VIRTUAL
664 TRACE("\tVirtual: %s\n", oss_caps & DSP_CAP_VIRTUAL ? "true" : "false");
666 #ifdef DSP_CAP_ANALOGOUT
667 TRACE("\tAnalog Out: %s\n", oss_caps & DSP_CAP_ANALOGOUT ? "true" : "false");
669 #ifdef DSP_CAP_ANALOGIN
670 TRACE("\tAnalog In: %s\n", oss_caps & DSP_CAP_ANALOGIN ? "true" : "false");
672 #ifdef DSP_CAP_DIGITALOUT
673 TRACE("\tDigital Out: %s\n", oss_caps & DSP_CAP_DIGITALOUT ? "true" : "false");
675 #ifdef DSP_CAP_DIGITALIN
676 TRACE("\tDigital In: %s\n", oss_caps & DSP_CAP_DIGITALIN ? "true" : "false");
678 #ifdef DSP_CAP_ADMASK
679 TRACE("\tA/D Mask: %s\n", oss_caps & DSP_CAP_ADMASK ? "true" : "false");
681 #ifdef DSP_CAP_SHADOW
682 TRACE("\tShadow: %s\n", oss_caps & DSP_CAP_SHADOW ? "true" : "false");
685 TRACE("\tChannel Mask: %x\n", oss_caps & DSP_CH_MASK);
688 TRACE("\tSlave: %s\n", oss_caps & DSP_CAP_SLAVE ? "true" : "false");
693 /******************************************************************
698 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
703 BOOL has_mixer = FALSE;
704 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
706 if (OSS_OpenDevice(ossdev, O_WRONLY, NULL, 0,-1,-1,-1) != 0)
709 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
711 #if defined(SNDCTL_MIXERINFO)
714 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
717 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
718 lstrcpynA(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
719 strcpy(ossdev->ds_desc.szDrvname, "wineoss.drv");
720 MultiByteToWideChar(CP_UNIXCP, 0, info.name, sizeof(info.name),
721 ossdev->out_caps.szPname,
722 sizeof(ossdev->out_caps.szPname) / sizeof(WCHAR));
723 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
726 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
730 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
733 #elif defined(SOUND_MIXER_INFO)
736 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
738 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
739 lstrcpynA(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
740 strcpy(ossdev->ds_desc.szDrvname, "wineoss.drv");
741 MultiByteToWideChar(CP_UNIXCP, 0, info.name, sizeof(info.name),
742 ossdev->out_caps.szPname,
743 sizeof(ossdev->out_caps.szPname) / sizeof(WCHAR));
744 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
747 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
748 * implement it properly, and there are probably similar issues
749 * on other platforms, so we warn but try to go ahead.
751 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
755 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
758 #endif /* SOUND_MIXER_INFO */
760 if (WINE_TRACE_ON(wave))
761 OSS_Info(ossdev->fd);
763 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
764 ossdev->out_caps.wPid = 0x0001; /* Product ID */
766 ossdev->out_caps.vDriverVersion = 0x0100;
767 ossdev->out_caps.wChannels = 1;
768 ossdev->out_caps.dwFormats = 0x00000000;
769 ossdev->out_caps.wReserved1 = 0;
770 ossdev->out_caps.dwSupport = has_mixer ? WAVECAPS_VOLUME : 0;
772 /* direct sound caps */
773 ossdev->ds_caps.dwFlags = DSCAPS_CERTIFIED;
774 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY8BIT;
775 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY16BIT;
776 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYMONO;
777 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYSTEREO;
778 ossdev->ds_caps.dwFlags |= DSCAPS_CONTINUOUSRATE;
780 ossdev->ds_caps.dwPrimaryBuffers = 1;
781 ossdev->ds_caps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
782 ossdev->ds_caps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
784 /* We must first set the format and the stereo mode as some sound cards
785 * may support 44kHz mono but not 44kHz stereo. Also we must
786 * systematically check the return value of these ioctls as they will
787 * always succeed (see OSS Linux) but will modify the parameter to match
788 * whatever they support. The OSS specs also say we must first set the
789 * sample size, then the stereo and then the sample rate.
792 arg=win_std_oss_fmts[f];
793 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
794 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
795 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
796 rc,arg,win_std_oss_fmts[f]);
800 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY8BIT;
802 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY16BIT;
804 for (c = 1; c <= MAX_CHANNELS; c++) {
806 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
808 if (rc!=0 || arg!=c) {
809 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
813 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
815 ossdev->out_caps.wChannels = 2;
817 ossdev->out_caps.dwSupport|=WAVECAPS_LRVOLUME;
818 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
820 ossdev->out_caps.wChannels = c;
822 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
823 arg=win_std_rates[r];
824 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
825 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
826 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
827 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
828 ossdev->out_caps.dwFormats|=win_std_formats[f][c-1][r];
833 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
834 if (arg & DSP_CAP_TRIGGER)
835 ossdev->bTriggerSupport = TRUE;
836 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
837 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
839 /* well, might as well use the DirectSound cap flag for something */
840 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
841 !(arg & DSP_CAP_BATCH)) {
842 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
844 ossdev->ds_caps.dwFlags |= DSCAPS_EMULDRIVER;
846 #ifdef DSP_CAP_MULTI /* not every oss has this */
847 /* check for hardware secondary buffer support (multi open) */
848 if ((arg & DSP_CAP_MULTI) &&
849 (ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
850 TRACE("hardware secondary buffer support available\n");
852 ossdev->ds_caps.dwMaxHwMixingAllBuffers = 16;
853 ossdev->ds_caps.dwMaxHwMixingStaticBuffers = 0;
854 ossdev->ds_caps.dwMaxHwMixingStreamingBuffers = 16;
856 ossdev->ds_caps.dwFreeHwMixingAllBuffers = 16;
857 ossdev->ds_caps.dwFreeHwMixingStaticBuffers = 0;
858 ossdev->ds_caps.dwFreeHwMixingStreamingBuffers = 16;
862 OSS_CloseDevice(ossdev);
863 TRACE("out wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
864 ossdev->out_caps.wChannels, ossdev->out_caps.dwFormats,
865 ossdev->out_caps.dwSupport);
869 /******************************************************************
874 static BOOL OSS_WaveInInit(OSS_DEVICE* ossdev)
879 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
881 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0,-1,-1,-1) != 0)
884 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
886 #if defined(SNDCTL_MIXERINFO)
889 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
892 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
893 MultiByteToWideChar(CP_UNIXCP, 0, info.name, -1,
894 ossdev->in_caps.szPname,
895 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
896 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
898 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
902 WARN("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
905 #elif defined(SOUND_MIXER_INFO)
908 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
910 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
911 MultiByteToWideChar(CP_UNIXCP, 0, info.name, -1,
912 ossdev->in_caps.szPname,
913 sizeof(ossdev->in_caps.szPname) / sizeof(WCHAR));
914 TRACE("%s: %s\n", ossdev->mixer_name, ossdev->ds_desc.szDesc);
916 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
917 * implement it properly, and there are probably similar issues
918 * on other platforms, so we warn but try to go ahead.
920 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
924 WARN("open(%s) failed (%s)\n", ossdev->mixer_name, strerror(errno));
927 #endif /* SOUND_MIXER_INFO */
929 if (WINE_TRACE_ON(wave))
930 OSS_Info(ossdev->fd);
932 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
933 ossdev->in_caps.wPid = 0x0001; /* Product ID */
935 ossdev->in_caps.dwFormats = 0x00000000;
936 ossdev->in_caps.wChannels = 1;
937 ossdev->in_caps.wReserved1 = 0;
939 /* direct sound caps */
940 ossdev->dsc_caps.dwSize = sizeof(ossdev->dsc_caps);
941 ossdev->dsc_caps.dwFlags = 0;
942 ossdev->dsc_caps.dwFormats = 0x00000000;
943 ossdev->dsc_caps.dwChannels = 1;
945 /* See the comment in OSS_WaveOutInit for the loop order */
947 arg=win_std_oss_fmts[f];
948 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
949 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
950 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
951 rc,arg,win_std_oss_fmts[f]);
955 for (c = 1; c <= MAX_CHANNELS; c++) {
957 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
959 if (rc!=0 || arg!=c) {
960 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
964 ossdev->in_caps.wChannels = c;
965 ossdev->dsc_caps.dwChannels = c;
968 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
969 arg=win_std_rates[r];
970 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
971 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
972 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
973 ossdev->in_caps.dwFormats|=win_std_formats[f][c-1][r];
974 ossdev->dsc_caps.dwFormats|=win_std_formats[f][c-1][r];
979 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
980 if (arg & DSP_CAP_TRIGGER)
981 ossdev->bTriggerSupport = TRUE;
982 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
983 !(arg & DSP_CAP_BATCH)) {
984 /* FIXME: enable the next statement if you want to work on the driver */
986 ossdev->in_caps_support |= WAVECAPS_DIRECTSOUND;
989 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH))
990 ossdev->in_caps_support |= WAVECAPS_SAMPLEACCURATE;
992 OSS_CloseDevice(ossdev);
993 TRACE("in wChannels = %d, dwFormats = %08X, in_caps_support = %08X\n",
994 ossdev->in_caps.wChannels, ossdev->in_caps.dwFormats, ossdev->in_caps_support);
998 /******************************************************************
999 * OSS_WaveFullDuplexInit
1003 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
1009 BOOL has_mixer = FALSE;
1010 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
1012 /* The OSS documentation says we must call SNDCTL_SETDUPLEX
1013 * *before* checking for SNDCTL_DSP_GETCAPS otherwise we may
1014 * get the wrong result. This ioctl must even be done before
1015 * setting the fragment size so that only OSS_RawOpenDevice is
1016 * in a position to do it. So we set full_duplex speculatively
1017 * and adjust right after.
1019 ossdev->full_duplex=1;
1020 rc=OSS_OpenDevice(ossdev, O_RDWR, NULL, 0,-1,-1,-1);
1021 ossdev->full_duplex=0;
1025 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
1027 #if defined(SNDCTL_MIXERINFO)
1030 if ((mixer = open(ossdev->mixer_name, O_RDWR|O_NDELAY)) >= 0) {
1033 if (ioctl(mixer, SNDCTL_MIXERINFO, &info) >= 0) {
1036 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev->mixer_name);
1040 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
1043 #elif defined(SOUND_MIXER_INFO)
1046 if ((mixer = open(ossdev->mixer_name, O_RDWR|O_NDELAY)) >= 0) {
1048 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
1051 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
1052 * implement it properly, and there are probably similar issues
1053 * on other platforms, so we warn but try to go ahead.
1055 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
1059 WARN("open(%s) failed (%s)\n", ossdev->mixer_name , strerror(errno));
1062 #endif /* SOUND_MIXER_INFO */
1064 TRACE("%s\n", ossdev->ds_desc.szDesc);
1066 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
1067 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
1069 ossdev->duplex_out_caps = ossdev->out_caps;
1071 ossdev->duplex_out_caps.wChannels = 1;
1072 ossdev->duplex_out_caps.dwFormats = 0x00000000;
1073 ossdev->duplex_out_caps.dwSupport = has_mixer ? WAVECAPS_VOLUME : 0;
1075 if (WINE_TRACE_ON(wave))
1076 OSS_Info(ossdev->fd);
1078 /* See the comment in OSS_WaveOutInit for the loop order */
1080 arg=win_std_oss_fmts[f];
1081 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
1082 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
1083 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1084 rc,arg,win_std_oss_fmts[f]);
1088 for (c = 1; c <= MAX_CHANNELS; c++) {
1090 rc=ioctl(ossdev->fd, SNDCTL_DSP_CHANNELS, &arg);
1091 if( rc == -1) break;
1092 if (rc!=0 || arg!=c) {
1093 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc,arg,c);
1097 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
1098 } else if (c == 2) {
1099 ossdev->duplex_out_caps.wChannels = 2;
1101 ossdev->duplex_out_caps.dwSupport|=WAVECAPS_LRVOLUME;
1102 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
1104 ossdev->duplex_out_caps.wChannels = c;
1106 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
1107 arg=win_std_rates[r];
1108 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
1109 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
1110 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c);
1111 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]) && c < 3)
1112 ossdev->duplex_out_caps.dwFormats|=win_std_formats[f][c-1][r];
1117 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
1118 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
1119 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
1121 /* well, might as well use the DirectSound cap flag for something */
1122 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
1123 !(arg & DSP_CAP_BATCH)) {
1124 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
1127 OSS_CloseDevice(ossdev);
1128 TRACE("duplex wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
1129 ossdev->duplex_out_caps.wChannels,
1130 ossdev->duplex_out_caps.dwFormats,
1131 ossdev->duplex_out_caps.dwSupport);
1134 static char* StrDup(const char* str, const char* def)
1139 dst=HeapAlloc(GetProcessHeap(),0,strlen(str)+1);
1144 static int WAVE_loadcount;
1146 /******************************************************************
1149 * Initialize internal structures from OSS information
1151 static LRESULT OSS_WaveInit(void)
1156 /* FIXME: Remove unneeded members of WOutDev and WInDev */
1157 TRACE("(%i)\n", WAVE_loadcount);
1158 if (WAVE_loadcount++)
1161 str=getenv("AUDIODEV");
1164 WOutDev[0].ossdev.dev_name = WInDev[0].ossdev.dev_name = StrDup(str,"");
1165 WOutDev[0].ossdev.mixer_name = WInDev[0].ossdev.mixer_name = StrDup(getenv("MIXERDEV"),"/dev/mixer");
1166 for (i = 1; i < MAX_WAVEDRV; ++i)
1168 WOutDev[i].ossdev.dev_name = WInDev[i].ossdev.dev_name = StrDup("",NULL);
1169 WOutDev[i].ossdev.mixer_name = WInDev[i].ossdev.mixer_name = StrDup("",NULL);
1174 WOutDev[0].ossdev.dev_name = WInDev[0].ossdev.dev_name = StrDup("/dev/dsp",NULL);
1175 WOutDev[0].ossdev.mixer_name = WInDev[0].ossdev.mixer_name = StrDup("/dev/mixer",NULL);
1176 for (i = 1; i < MAX_WAVEDRV; ++i)
1178 WOutDev[i].ossdev.dev_name = WInDev[i].ossdev.dev_name = HeapAlloc(GetProcessHeap(),0,11);
1179 sprintf(WOutDev[i].ossdev.dev_name, "/dev/dsp%u", i);
1180 WOutDev[i].ossdev.mixer_name = WInDev[i].ossdev.mixer_name = HeapAlloc(GetProcessHeap(),0,13);
1181 sprintf(WOutDev[i].ossdev.mixer_name, "/dev/mixer%u", i);
1185 for (i = 0; i < MAX_WAVEDRV; ++i)
1187 WOutDev[i].ossdev.interface_name = WInDev[i].ossdev.interface_name =
1188 HeapAlloc(GetProcessHeap(),0,9+strlen(WOutDev[i].ossdev.dev_name)+1);
1189 sprintf(WOutDev[i].ossdev.interface_name, "wineoss: %s", WOutDev[i].ossdev.dev_name);
1192 /* start with output devices */
1193 for (i = 0; i < MAX_WAVEDRV; ++i)
1195 if (*WOutDev[i].ossdev.dev_name == '\0' || OSS_WaveOutInit(&WOutDev[i].ossdev))
1197 WOutDev[numOutDev].state = WINE_WS_CLOSED;
1198 WOutDev[numOutDev].volume = 0xffffffff;
1203 /* then do input devices */
1204 for (i = 0; i < MAX_WAVEDRV; ++i)
1206 if (*WInDev[i].ossdev.dev_name=='\0' || OSS_WaveInInit(&WInDev[i].ossdev))
1208 WInDev[numInDev].state = WINE_WS_CLOSED;
1213 /* finish with the full duplex bits */
1214 for (i = 0; i < MAX_WAVEDRV; i++)
1215 if (*WOutDev[i].ossdev.dev_name!='\0')
1216 OSS_WaveFullDuplexInit(&WOutDev[i].ossdev);
1218 TRACE("%d wave out devices\n", numOutDev);
1219 for (i = 0; i < numOutDev; i++) {
1220 TRACE("%u: %s, %s, %s\n", i, WOutDev[i].ossdev.dev_name,
1221 WOutDev[i].ossdev.mixer_name, WOutDev[i].ossdev.interface_name);
1224 TRACE("%d wave in devices\n", numInDev);
1225 for (i = 0; i < numInDev; i++) {
1226 TRACE("%u: %s, %s, %s\n", i, WInDev[i].ossdev.dev_name,
1227 WInDev[i].ossdev.mixer_name, WInDev[i].ossdev.interface_name);
1233 /******************************************************************
1236 * Delete/clear internal structures of OSS information
1238 static LRESULT OSS_WaveExit(void)
1241 TRACE("(%i)\n", WAVE_loadcount);
1242 if (--WAVE_loadcount)
1245 for (i = 0; i < MAX_WAVEDRV; ++i)
1247 HeapFree(GetProcessHeap(), 0, WOutDev[i].ossdev.dev_name);
1248 HeapFree(GetProcessHeap(), 0, WOutDev[i].ossdev.mixer_name);
1249 HeapFree(GetProcessHeap(), 0, WOutDev[i].ossdev.interface_name);
1252 ZeroMemory(WOutDev, sizeof(WOutDev));
1253 ZeroMemory(WInDev, sizeof(WInDev));
1261 /******************************************************************
1262 * OSS_InitRingMessage
1264 * Initialize the ring of messages for passing between driver's caller and playback/record
1267 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
1270 omr->msg_tosave = 0;
1271 #ifdef USE_PIPE_SYNC
1272 if (pipe(omr->msg_pipe) < 0) {
1273 omr->msg_pipe[0] = -1;
1274 omr->msg_pipe[1] = -1;
1275 ERR("could not create pipe, error=%s\n", strerror(errno));
1278 omr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1280 omr->ring_buffer_size = OSS_RING_BUFFER_INCREMENT;
1281 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(OSS_MSG));
1282 InitializeCriticalSection(&omr->msg_crst);
1283 omr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": OSS_MSG_RING.msg_crst");
1287 /******************************************************************
1288 * OSS_DestroyRingMessage
1291 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
1293 #ifdef USE_PIPE_SYNC
1294 close(omr->msg_pipe[0]);
1295 close(omr->msg_pipe[1]);
1297 CloseHandle(omr->msg_event);
1299 HeapFree(GetProcessHeap(),0,omr->messages);
1300 omr->msg_crst.DebugInfo->Spare[0] = 0;
1301 DeleteCriticalSection(&omr->msg_crst);
1305 /******************************************************************
1306 * OSS_AddRingMessage
1308 * Inserts a new message into the ring (should be called from DriverProc derived routines)
1310 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
1312 HANDLE hEvent = INVALID_HANDLE_VALUE;
1314 EnterCriticalSection(&omr->msg_crst);
1315 if (omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size))
1317 int old_ring_buffer_size = omr->ring_buffer_size;
1318 omr->ring_buffer_size += OSS_RING_BUFFER_INCREMENT;
1319 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
1320 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(OSS_MSG));
1321 /* Now we need to rearrange the ring buffer so that the new
1322 buffers just allocated are in between omr->msg_tosave and
1325 if (omr->msg_tosave < omr->msg_toget)
1327 memmove(&(omr->messages[omr->msg_toget + OSS_RING_BUFFER_INCREMENT]),
1328 &(omr->messages[omr->msg_toget]),
1329 sizeof(OSS_MSG)*(old_ring_buffer_size - omr->msg_toget)
1331 omr->msg_toget += OSS_RING_BUFFER_INCREMENT;
1336 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1337 if (hEvent == INVALID_HANDLE_VALUE)
1339 ERR("can't create event !?\n");
1340 LeaveCriticalSection(&omr->msg_crst);
1343 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
1344 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1345 omr->msg_toget,getCmdString(omr->messages[omr->msg_toget].msg),
1346 omr->msg_tosave,getCmdString(omr->messages[omr->msg_tosave].msg));
1348 /* fast messages have to be added at the start of the queue */
1349 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
1350 omr->messages[omr->msg_toget].msg = msg;
1351 omr->messages[omr->msg_toget].param = param;
1352 omr->messages[omr->msg_toget].hEvent = hEvent;
1356 omr->messages[omr->msg_tosave].msg = msg;
1357 omr->messages[omr->msg_tosave].param = param;
1358 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
1359 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
1361 LeaveCriticalSection(&omr->msg_crst);
1362 /* signal a new message */
1366 /* wait for playback/record thread to have processed the message */
1367 WaitForSingleObject(hEvent, INFINITE);
1368 CloseHandle(hEvent);
1373 /******************************************************************
1374 * OSS_RetrieveRingMessage
1376 * Get a message from the ring. Should be called by the playback/record thread.
1378 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
1379 enum win_wm_message *msg, DWORD_PTR *param, HANDLE *hEvent)
1381 EnterCriticalSection(&omr->msg_crst);
1383 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1385 LeaveCriticalSection(&omr->msg_crst);
1389 *msg = omr->messages[omr->msg_toget].msg;
1390 omr->messages[omr->msg_toget].msg = 0;
1391 *param = omr->messages[omr->msg_toget].param;
1392 *hEvent = omr->messages[omr->msg_toget].hEvent;
1393 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
1395 LeaveCriticalSection(&omr->msg_crst);
1399 /******************************************************************
1400 * OSS_PeekRingMessage
1402 * Peek at a message from the ring but do not remove it.
1403 * Should be called by the playback/record thread.
1405 static int OSS_PeekRingMessage(OSS_MSG_RING* omr,
1406 enum win_wm_message *msg,
1407 DWORD_PTR *param, HANDLE *hEvent)
1409 EnterCriticalSection(&omr->msg_crst);
1411 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1413 LeaveCriticalSection(&omr->msg_crst);
1417 *msg = omr->messages[omr->msg_toget].msg;
1418 *param = omr->messages[omr->msg_toget].param;
1419 *hEvent = omr->messages[omr->msg_toget].hEvent;
1420 LeaveCriticalSection(&omr->msg_crst);
1424 /*======================================================================*
1425 * Low level WAVE OUT implementation *
1426 *======================================================================*/
1428 /**************************************************************************
1429 * wodNotifyClient [internal]
1431 static void wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1433 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lx dwParam2 = %04lx\n", wMsg,
1434 wMsg == WOM_OPEN ? "WOM_OPEN" : wMsg == WOM_CLOSE ? "WOM_CLOSE" :
1435 wMsg == WOM_DONE ? "WOM_DONE" : "Unknown", dwParam1, dwParam2);
1441 DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
1442 (HDRVR)wwo->waveDesc.hWave, wMsg,
1443 wwo->waveDesc.dwInstance, dwParam1, dwParam2);
1446 FIXME("Unknown callback message %u\n", wMsg);
1450 /**************************************************************************
1451 * wodUpdatePlayedTotal [internal]
1454 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
1456 audio_buf_info dspspace;
1458 if (!info) info = &dspspace;
1460 if (ioctl(wwo->ossdev.fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
1461 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev.dev_name, strerror(errno));
1465 /* GETOSPACE is not always accurate when we're down to the last fragment or two;
1466 ** we try to accommodate that here by assuming that the dsp is empty by looking
1467 ** at the clock rather than the result of GETOSPACE */
1468 notplayed = wwo->dwBufferSize - info->bytes;
1469 if (notplayed > 0 && notplayed < (info->fragsize * 2))
1471 if (wwo->dwProjectedFinishTime && GetTickCount() >= wwo->dwProjectedFinishTime)
1473 TRACE("Adjusting for a presumed OSS bug and assuming all data has been played.\n");
1474 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1478 /* Some OSS drivers will clean up nicely if given a POST, so give 'em the chance... */
1479 ioctl(wwo->ossdev.fd, SNDCTL_DSP_POST, 0);
1482 wwo->dwPlayedTotal = wwo->dwWrittenTotal - notplayed;
1486 /**************************************************************************
1487 * wodPlayer_BeginWaveHdr [internal]
1489 * Makes the specified lpWaveHdr the currently playing wave header.
1490 * If the specified wave header is a begin loop and we're not already in
1491 * a loop, setup the loop.
1493 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1495 wwo->lpPlayPtr = lpWaveHdr;
1497 if (!lpWaveHdr) return;
1499 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1500 if (wwo->lpLoopPtr) {
1501 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1503 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1504 wwo->lpLoopPtr = lpWaveHdr;
1505 /* Windows does not touch WAVEHDR.dwLoops,
1506 * so we need to make an internal copy */
1507 wwo->dwLoops = lpWaveHdr->dwLoops;
1510 wwo->dwPartialOffset = 0;
1513 /**************************************************************************
1514 * wodPlayer_PlayPtrNext [internal]
1516 * Advance the play pointer to the next waveheader, looping if required.
1518 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
1520 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1522 wwo->dwPartialOffset = 0;
1523 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1524 /* We're at the end of a loop, loop if required */
1525 if (--wwo->dwLoops > 0) {
1526 wwo->lpPlayPtr = wwo->lpLoopPtr;
1528 /* Handle overlapping loops correctly */
1529 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1530 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1531 /* shall we consider the END flag for the closing loop or for
1532 * the opening one or for both ???
1533 * code assumes for closing loop only
1536 lpWaveHdr = lpWaveHdr->lpNext;
1538 wwo->lpLoopPtr = NULL;
1539 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1542 /* We're not in a loop. Advance to the next wave header */
1543 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1549 /**************************************************************************
1550 * wodPlayer_TicksTillEmpty [internal]
1551 * Returns the number of ticks until we think the DSP should be empty
1553 static DWORD wodPlayer_TicksTillEmpty(const WINE_WAVEOUT *wwo)
1555 return ((wwo->dwWrittenTotal - wwo->dwPlayedTotal) * 1000)
1556 / wwo->waveFormat.Format.nAvgBytesPerSec;
1559 /**************************************************************************
1560 * wodPlayer_DSPWait [internal]
1561 * Returns the number of milliseconds to wait for the DSP buffer to write
1564 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1566 /* time for one fragment to be played */
1567 return wwo->dwFragmentSize * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1570 /**************************************************************************
1571 * wodPlayer_NotifyWait [internal]
1572 * Returns the number of milliseconds to wait before attempting to notify
1573 * completion of the specified wavehdr.
1574 * This is based on the number of bytes remaining to be written in the
1577 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1581 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1584 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1585 if (!dwMillis) dwMillis = 1;
1592 /**************************************************************************
1593 * wodPlayer_WriteMaxFrags [internal]
1594 * Writes the maximum number of bytes possible to the DSP and returns
1595 * TRUE iff the current playPtr has been fully played
1597 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
1599 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1600 DWORD toWrite = min(dwLength, *bytes);
1604 TRACE("Writing wavehdr %p.%u[%u]/%u\n",
1605 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
1609 written = write(wwo->ossdev.fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
1611 TRACE("write(%s, %p, %d) failed (%s) returned %d\n", wwo->ossdev.dev_name,
1612 wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite, strerror(errno), written);
1619 if (written >= dwLength) {
1620 /* If we wrote all current wavehdr, skip to the next one */
1621 wodPlayer_PlayPtrNext(wwo);
1624 /* Remove the amount written */
1625 wwo->dwPartialOffset += written;
1628 wwo->dwWrittenTotal += written;
1629 TRACE("dwWrittenTotal=%u\n", wwo->dwWrittenTotal);
1634 /**************************************************************************
1635 * wodPlayer_NotifyCompletions [internal]
1637 * Notifies and remove from queue all wavehdrs which have been played to
1638 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1639 * we notify all wavehdrs and remove them all from the queue even if they
1640 * are unplayed or part of a loop.
1642 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1644 LPWAVEHDR lpWaveHdr;
1646 /* Start from lpQueuePtr and keep notifying until:
1647 * - we hit an unwritten wavehdr
1648 * - we hit the beginning of a running loop
1649 * - we hit a wavehdr which hasn't finished playing
1652 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1654 (lpWaveHdr != wwo->lpPlayPtr &&
1655 lpWaveHdr != wwo->lpLoopPtr &&
1656 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1658 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1660 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1661 lpWaveHdr->dwFlags |= WHDR_DONE;
1663 wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
1668 lpWaveHdr = wwo->lpQueuePtr;
1669 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
1672 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
1673 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
1674 if (lpWaveHdr->reserved > wwo->dwPlayedTotal) {TRACE("still playing %p (%lu/%u)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
1676 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1678 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1679 lpWaveHdr->dwFlags |= WHDR_DONE;
1681 wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
1684 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1685 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1688 /**************************************************************************
1689 * wodPlayer_Reset [internal]
1691 * wodPlayer helper. Resets current output stream.
1693 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
1695 wodUpdatePlayedTotal(wwo, NULL);
1696 /* updates current notify list */
1697 wodPlayer_NotifyCompletions(wwo, FALSE);
1699 /* flush all possible output */
1700 if (OSS_ResetDevice(&wwo->ossdev) != MMSYSERR_NOERROR)
1703 wwo->state = WINE_WS_STOPPED;
1708 enum win_wm_message msg;
1712 /* remove any buffer */
1713 wodPlayer_NotifyCompletions(wwo, TRUE);
1715 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1716 wwo->state = WINE_WS_STOPPED;
1717 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1718 /* Clear partial wavehdr */
1719 wwo->dwPartialOffset = 0;
1721 /* remove any existing message in the ring */
1722 EnterCriticalSection(&wwo->msgRing.msg_crst);
1723 /* return all pending headers in queue */
1724 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev))
1726 if (msg != WINE_WM_HEADER)
1728 FIXME("shouldn't have headers left\n");
1732 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1733 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1735 wodNotifyClient(wwo, WOM_DONE, param, 0);
1737 RESET_OMR(&wwo->msgRing);
1738 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1740 if (wwo->lpLoopPtr) {
1741 /* complicated case, not handled yet (could imply modifying the loop counter */
1742 FIXME("Pausing while in loop isn't correctly handled yet, expect strange results\n");
1743 wwo->lpPlayPtr = wwo->lpLoopPtr;
1744 wwo->dwPartialOffset = 0;
1745 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1748 DWORD sz = wwo->dwPartialOffset;
1750 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1751 /* compute the max size playable from lpQueuePtr */
1752 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1753 sz += ptr->dwBufferLength;
1755 /* because the reset lpPlayPtr will be lpQueuePtr */
1756 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1757 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1758 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1759 wwo->lpPlayPtr = wwo->lpQueuePtr;
1761 wwo->state = WINE_WS_PAUSED;
1765 /**************************************************************************
1766 * wodPlayer_ProcessMessages [internal]
1768 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1770 LPWAVEHDR lpWaveHdr;
1771 enum win_wm_message msg;
1775 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev)) {
1776 TRACE("Received %s %lx\n", getCmdString(msg), param);
1778 case WINE_WM_PAUSING:
1779 wodPlayer_Reset(wwo, FALSE);
1782 case WINE_WM_RESTARTING:
1783 if (wwo->state == WINE_WS_PAUSED)
1785 wwo->state = WINE_WS_PLAYING;
1789 case WINE_WM_HEADER:
1790 lpWaveHdr = (LPWAVEHDR)param;
1792 /* insert buffer at the end of queue */
1795 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1798 if (!wwo->lpPlayPtr)
1799 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1800 if (wwo->state == WINE_WS_STOPPED)
1801 wwo->state = WINE_WS_PLAYING;
1803 case WINE_WM_RESETTING:
1804 wodPlayer_Reset(wwo, TRUE);
1807 case WINE_WM_UPDATE:
1808 wodUpdatePlayedTotal(wwo, NULL);
1811 case WINE_WM_BREAKLOOP:
1812 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1813 /* ensure exit at end of current loop */
1818 case WINE_WM_CLOSING:
1819 /* sanity check: this should not happen since the device must have been reset before */
1820 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1822 wwo->state = WINE_WS_CLOSED;
1825 /* shouldn't go here */
1827 FIXME("unknown message %d\n", msg);
1833 /**************************************************************************
1834 * wodPlayer_FeedDSP [internal]
1835 * Feed as much sound data as we can into the DSP and return the number of
1836 * milliseconds before it will be necessary to feed the DSP again.
1838 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1840 audio_buf_info dspspace;
1843 if (!wodUpdatePlayedTotal(wwo, &dspspace)) return INFINITE;
1844 availInQ = dspspace.bytes;
1845 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1846 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1848 /* no more room... no need to try to feed */
1849 if (dspspace.fragments != 0) {
1850 /* Feed from partial wavehdr */
1851 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1852 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1855 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1856 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1858 TRACE("Setting time to elapse for %p to %u\n",
1859 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1860 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1861 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1862 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1865 if (wwo->bNeedPost) {
1866 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1867 * if it didn't get one, we give it the other */
1868 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1869 ioctl(wwo->ossdev.fd, SNDCTL_DSP_POST, 0);
1870 wwo->bNeedPost = FALSE;
1874 return wodPlayer_DSPWait(wwo);
1878 /**************************************************************************
1879 * wodPlayer [internal]
1881 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1883 WORD uDevID = (DWORD_PTR)pmt;
1884 WINE_WAVEOUT* wwo = &WOutDev[uDevID];
1885 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1886 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1889 wwo->state = WINE_WS_STOPPED;
1890 SetEvent(wwo->hStartUpEvent);
1893 /** Wait for the shortest time before an action is required. If there
1894 * are no pending actions, wait forever for a command.
1896 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1897 TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1898 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1899 wodPlayer_ProcessMessages(wwo);
1900 if (wwo->state == WINE_WS_PLAYING) {
1901 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1902 if (dwNextFeedTime != INFINITE)
1903 wwo->dwProjectedFinishTime = GetTickCount() + wodPlayer_TicksTillEmpty(wwo);
1905 wwo->dwProjectedFinishTime = 0;
1907 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1908 if (dwNextFeedTime == INFINITE) {
1909 /* FeedDSP ran out of data, but before flushing, */
1910 /* check that a notification didn't give us more */
1911 wodPlayer_ProcessMessages(wwo);
1912 if (!wwo->lpPlayPtr) {
1913 TRACE("flushing\n");
1914 ioctl(wwo->ossdev.fd, SNDCTL_DSP_SYNC, 0);
1915 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1916 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1918 TRACE("recovering\n");
1919 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1923 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1930 /**************************************************************************
1931 * wodGetDevCaps [internal]
1933 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1935 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1937 if (lpCaps == NULL) {
1938 WARN("not enabled\n");
1939 return MMSYSERR_NOTENABLED;
1942 if (wDevID >= numOutDev) {
1943 WARN("numOutDev reached !\n");
1944 return MMSYSERR_BADDEVICEID;
1947 if (WOutDev[wDevID].ossdev.open_access == O_RDWR)
1948 memcpy(lpCaps, &WOutDev[wDevID].ossdev.duplex_out_caps, min(dwSize, sizeof(*lpCaps)));
1950 memcpy(lpCaps, &WOutDev[wDevID].ossdev.out_caps, min(dwSize, sizeof(*lpCaps)));
1952 return MMSYSERR_NOERROR;
1955 /**************************************************************************
1956 * wodOpen [internal]
1958 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1962 audio_buf_info info;
1965 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1966 if (lpDesc == NULL) {
1967 WARN("Invalid Parameter !\n");
1968 return MMSYSERR_INVALPARAM;
1970 if (wDevID >= numOutDev) {
1971 TRACE("MAX_WAVOUTDRV reached !\n");
1972 return MMSYSERR_BADDEVICEID;
1975 /* only PCM format is supported so far... */
1976 if (!supportedFormat(lpDesc->lpFormat)) {
1977 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1978 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1979 lpDesc->lpFormat->nSamplesPerSec);
1980 return WAVERR_BADFORMAT;
1983 if (dwFlags & WAVE_FORMAT_QUERY) {
1984 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1985 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1986 lpDesc->lpFormat->nSamplesPerSec);
1987 return MMSYSERR_NOERROR;
1990 /* nBlockAlign and nAvgBytesPerSec are output variables for dsound */
1991 if (lpDesc->lpFormat->nBlockAlign != lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8) {
1992 lpDesc->lpFormat->nBlockAlign = lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
1993 WARN("Fixing nBlockAlign\n");
1995 if (lpDesc->lpFormat->nAvgBytesPerSec!= lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign) {
1996 lpDesc->lpFormat->nAvgBytesPerSec = lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
1997 WARN("Fixing nAvgBytesPerSec\n");
2000 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2001 lpDesc->lpFormat->nSamplesPerSec,
2002 lpDesc->lpFormat->wBitsPerSample,
2003 lpDesc->lpFormat->nChannels,
2004 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
2005 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
2008 wwo = &WOutDev[wDevID];
2010 if ((dwFlags & WAVE_DIRECTSOUND) &&
2011 !(wwo->ossdev.duplex_out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
2012 /* not supported, ignore it */
2013 dwFlags &= ~WAVE_DIRECTSOUND;
2015 if (dwFlags & WAVE_DIRECTSOUND) {
2016 if (wwo->ossdev.duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2017 /* we have realtime DirectSound, fragments just waste our time,
2018 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2019 audio_fragment = 0x0020000B;
2021 /* to approximate realtime, we must use small fragments,
2022 * let's try to fragment the above 64KB (256 * 2^8) */
2023 audio_fragment = 0x01000008;
2025 /* A wave device must have a worst case latency of 10 ms so calculate
2026 * the largest fragment size less than 10 ms long.
2028 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2030 while ((1 << shift) <= fsize)
2033 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2036 TRACE("requesting %d %d byte fragments (%d ms/fragment)\n",
2037 audio_fragment >> 16, 1 << (audio_fragment & 0xffff),
2038 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2040 if (wwo->state != WINE_WS_CLOSED) {
2041 WARN("already allocated\n");
2042 return MMSYSERR_ALLOCATED;
2045 /* we want to be able to mmap() the device, which means it must be opened readable,
2046 * otherwise mmap() will fail (at least under Linux) */
2047 ret = OSS_OpenDevice(&wwo->ossdev,
2048 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
2050 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
2051 lpDesc->lpFormat->nSamplesPerSec,
2052 lpDesc->lpFormat->nChannels,
2053 (lpDesc->lpFormat->wBitsPerSample == 16)
2054 ? AFMT_S16_LE : AFMT_U8);
2055 if ((ret==MMSYSERR_NOERROR) && (dwFlags & WAVE_DIRECTSOUND)) {
2056 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev.sample_rate;
2057 lpDesc->lpFormat->nChannels=wwo->ossdev.channels;
2058 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev.format == AFMT_U8 ? 8 : 16);
2059 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
2060 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
2061 TRACE("OSS_OpenDevice returned this format: %dx%dx%d\n",
2062 lpDesc->lpFormat->nSamplesPerSec,
2063 lpDesc->lpFormat->wBitsPerSample,
2064 lpDesc->lpFormat->nChannels);
2066 if (ret != 0) return ret;
2067 wwo->state = WINE_WS_STOPPED;
2069 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2071 wwo->waveDesc = *lpDesc;
2072 copy_format(lpDesc->lpFormat, &wwo->waveFormat);
2074 /* Read output space info for future reference */
2075 if (ioctl(wwo->ossdev.fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2076 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev.dev_name, strerror(errno));
2077 OSS_CloseDevice(&wwo->ossdev);
2078 wwo->state = WINE_WS_CLOSED;
2079 return MMSYSERR_NOTENABLED;
2082 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2083 info.fragsize, (info.fragsize * 1000) / (wwo->ossdev.sample_rate *
2084 wwo->ossdev.channels * (wwo->ossdev.format == AFMT_U8 ? 1 : 2)));
2086 /* Check that fragsize is correct per our settings above */
2087 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
2088 /* we've tried to set 1K fragments or less, but it didn't work */
2089 WARN("fragment size set failed, size is now %d\n", info.fragsize);
2092 /* Remember fragsize and total buffer size for future use */
2093 wwo->dwFragmentSize = info.fragsize;
2094 wwo->dwBufferSize = info.fragstotal * info.fragsize;
2095 wwo->dwPlayedTotal = 0;
2096 wwo->dwWrittenTotal = 0;
2097 wwo->bNeedPost = TRUE;
2099 TRACE("fd=%d fragstotal=%d fragsize=%d BufferSize=%d\n",
2100 wwo->ossdev.fd, info.fragstotal, info.fragsize, wwo->dwBufferSize);
2101 if (wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign) {
2102 ERR("Fragment doesn't contain an integral number of data blocks fragsize=%d BlockAlign=%d\n",wwo->dwFragmentSize,wwo->waveFormat.Format.nBlockAlign);
2103 /* Some SoundBlaster 16 cards return an incorrect (odd) fragment
2104 * size for 16 bit sound. This will cause a system crash when we try
2105 * to write just the specified odd number of bytes. So if we
2106 * detect something is wrong we'd better fix it.
2108 wwo->dwFragmentSize-=wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign;
2111 OSS_InitRingMessage(&wwo->msgRing);
2113 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2114 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwo->dwThreadID));
2116 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2117 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
2118 CloseHandle(wwo->hStartUpEvent);
2119 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
2121 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2122 wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
2123 wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
2124 wwo->waveFormat.Format.nBlockAlign);
2126 wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
2127 return MMSYSERR_NOERROR;
2130 /**************************************************************************
2131 * wodClose [internal]
2133 static DWORD wodClose(WORD wDevID)
2137 TRACE("(%u);\n", wDevID);
2139 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2140 WARN("bad device ID !\n");
2141 return MMSYSERR_BADDEVICEID;
2144 wwo = &WOutDev[wDevID];
2145 if (wwo->lpQueuePtr) {
2146 WARN("buffers still playing !\n");
2147 return WAVERR_STILLPLAYING;
2149 if (wwo->hThread != INVALID_HANDLE_VALUE) {
2150 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
2153 OSS_DestroyRingMessage(&wwo->msgRing);
2155 OSS_CloseDevice(&wwo->ossdev);
2156 wwo->state = WINE_WS_CLOSED;
2157 wwo->dwFragmentSize = 0;
2158 wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
2160 return MMSYSERR_NOERROR;
2163 /**************************************************************************
2164 * wodWrite [internal]
2167 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2170 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
2172 /* first, do the sanity checks... */
2173 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2174 WARN("bad dev ID !\n");
2175 return MMSYSERR_BADDEVICEID;
2178 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
2179 return WAVERR_UNPREPARED;
2181 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2182 return WAVERR_STILLPLAYING;
2184 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2185 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2186 lpWaveHdr->lpNext = 0;
2188 delta = lpWaveHdr->dwBufferLength % WOutDev[wDevID].waveFormat.Format.nBlockAlign;
2191 WARN("WaveHdr length isn't a multiple of the PCM block size: %d %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].waveFormat.Format.nBlockAlign);
2192 lpWaveHdr->dwBufferLength -= delta;
2195 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
2197 return MMSYSERR_NOERROR;
2200 /**************************************************************************
2201 * wodPause [internal]
2203 static DWORD wodPause(WORD wDevID)
2205 TRACE("(%u);!\n", wDevID);
2207 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2208 WARN("bad device ID !\n");
2209 return MMSYSERR_BADDEVICEID;
2212 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
2214 return MMSYSERR_NOERROR;
2217 /**************************************************************************
2218 * wodRestart [internal]
2220 static DWORD wodRestart(WORD wDevID)
2222 TRACE("(%u);\n", wDevID);
2224 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2225 WARN("bad device ID !\n");
2226 return MMSYSERR_BADDEVICEID;
2229 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2231 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2232 /* FIXME: Myst crashes with this ... hmm -MM
2233 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2236 return MMSYSERR_NOERROR;
2239 /**************************************************************************
2240 * wodReset [internal]
2242 static DWORD wodReset(WORD wDevID)
2244 TRACE("(%u);\n", wDevID);
2246 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2247 WARN("bad device ID !\n");
2248 return MMSYSERR_BADDEVICEID;
2251 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2253 return MMSYSERR_NOERROR;
2256 /**************************************************************************
2257 * wodGetPosition [internal]
2259 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2263 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
2265 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2266 WARN("bad device ID !\n");
2267 return MMSYSERR_BADDEVICEID;
2270 if (lpTime == NULL) {
2271 WARN("invalid parameter: lpTime == NULL\n");
2272 return MMSYSERR_INVALPARAM;
2275 wwo = &WOutDev[wDevID];
2276 #ifdef EXACT_WODPOSITION
2277 if (wwo->ossdev.open_access == O_RDWR) {
2278 if (wwo->ossdev.duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2279 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2281 if (wwo->ossdev.out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2282 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2286 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
2289 /**************************************************************************
2290 * wodBreakLoop [internal]
2292 static DWORD wodBreakLoop(WORD wDevID)
2294 TRACE("(%u);\n", wDevID);
2296 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2297 WARN("bad device ID !\n");
2298 return MMSYSERR_BADDEVICEID;
2300 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
2301 return MMSYSERR_NOERROR;
2304 /**************************************************************************
2305 * wodGetVolume [internal]
2307 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
2312 DWORD last_left, last_right;
2314 TRACE("(%u, %p);\n", wDevID, lpdwVol);
2316 if (lpdwVol == NULL) {
2317 WARN("not enabled\n");
2318 return MMSYSERR_NOTENABLED;
2320 if (wDevID >= numOutDev) {
2321 WARN("invalid parameter\n");
2322 return MMSYSERR_INVALPARAM;
2324 if (WOutDev[wDevID].ossdev.open_access == O_RDWR) {
2325 if (!(WOutDev[wDevID].ossdev.duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2326 TRACE("Volume not supported\n");
2327 return MMSYSERR_NOTSUPPORTED;
2330 if (!(WOutDev[wDevID].ossdev.out_caps.dwSupport & WAVECAPS_VOLUME)) {
2331 TRACE("Volume not supported\n");
2332 return MMSYSERR_NOTSUPPORTED;
2336 if ((mixer = open(WOutDev[wDevID].ossdev.mixer_name, O_RDONLY|O_NDELAY)) < 0) {
2337 WARN("mixer device not available !\n");
2338 return MMSYSERR_NOTENABLED;
2340 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
2342 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)\n",
2343 WOutDev[wDevID].ossdev.mixer_name, strerror(errno));
2344 return MMSYSERR_NOTENABLED;
2348 left = LOBYTE(volume);
2349 right = HIBYTE(volume);
2350 TRACE("left=%d right=%d !\n", left, right);
2351 last_left = (LOWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2352 last_right = (HIWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2353 TRACE("last_left=%d last_right=%d !\n", last_left, last_right);
2354 if (last_left == left && last_right == right)
2355 *lpdwVol = WOutDev[wDevID].volume;
2357 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
2358 return MMSYSERR_NOERROR;
2361 /**************************************************************************
2362 * wodSetVolume [internal]
2364 DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
2370 TRACE("(%u, %08X);\n", wDevID, dwParam);
2372 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
2373 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
2374 volume = left + (right << 8);
2376 if (wDevID >= numOutDev) {
2377 WARN("invalid parameter: wDevID > %d\n", numOutDev);
2378 return MMSYSERR_INVALPARAM;
2380 if (WOutDev[wDevID].ossdev.open_access == O_RDWR) {
2381 if (!(WOutDev[wDevID].ossdev.duplex_out_caps.dwSupport & WAVECAPS_VOLUME)) {
2382 TRACE("Volume not supported\n");
2383 return MMSYSERR_NOTSUPPORTED;
2386 if (!(WOutDev[wDevID].ossdev.out_caps.dwSupport & WAVECAPS_VOLUME)) {
2387 TRACE("Volume not supported\n");
2388 return MMSYSERR_NOTSUPPORTED;
2391 if ((mixer = open(WOutDev[wDevID].ossdev.mixer_name, O_WRONLY|O_NDELAY)) < 0) {
2392 WARN("open(%s) failed (%s)\n", WOutDev[wDevID].ossdev.mixer_name, strerror(errno));
2393 return MMSYSERR_NOTENABLED;
2395 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
2397 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n",
2398 WOutDev[wDevID].ossdev.mixer_name, strerror(errno));
2399 return MMSYSERR_NOTENABLED;
2401 TRACE("volume=%04x\n", (unsigned)volume);
2404 /* save requested volume */
2405 WOutDev[wDevID].volume = dwParam;
2407 return MMSYSERR_NOERROR;
2410 /**************************************************************************
2411 * wodMessage (WINEOSS.7)
2413 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
2414 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2416 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
2417 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
2424 /* FIXME: Pretend this is supported */
2426 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2427 case WODM_CLOSE: return wodClose (wDevID);
2428 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2429 case WODM_PAUSE: return wodPause (wDevID);
2430 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
2431 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
2432 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2433 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2434 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
2435 case WODM_GETNUMDEVS: return numOutDev;
2436 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
2437 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
2438 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2439 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2440 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
2441 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
2442 case WODM_RESTART: return wodRestart (wDevID);
2443 case WODM_RESET: return wodReset (wDevID);
2445 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2446 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2447 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
2448 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2450 FIXME("unknown message %d!\n", wMsg);
2452 return MMSYSERR_NOTSUPPORTED;
2455 /*======================================================================*
2456 * Low level WAVE IN implementation *
2457 *======================================================================*/
2459 /**************************************************************************
2460 * widNotifyClient [internal]
2462 static void widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2464 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lx dwParam2 = %04lx\n", wMsg,
2465 wMsg == WIM_OPEN ? "WIM_OPEN" : wMsg == WIM_CLOSE ? "WIM_CLOSE" :
2466 wMsg == WIM_DATA ? "WIM_DATA" : "Unknown", dwParam1, dwParam2);
2472 DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2473 (HDRVR)wwi->waveDesc.hWave, wMsg,
2474 wwi->waveDesc.dwInstance, dwParam1, dwParam2);
2477 FIXME("Unknown callback message %u\n", wMsg);
2481 /**************************************************************************
2482 * widGetDevCaps [internal]
2484 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
2486 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
2488 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2490 if (wDevID >= numInDev) {
2491 TRACE("numOutDev reached !\n");
2492 return MMSYSERR_BADDEVICEID;
2495 memcpy(lpCaps, &WInDev[wDevID].ossdev.in_caps, min(dwSize, sizeof(*lpCaps)));
2496 return MMSYSERR_NOERROR;
2499 /**************************************************************************
2500 * widRecorder_ReadHeaders [internal]
2502 static void widRecorder_ReadHeaders(WINE_WAVEIN * wwi)
2504 enum win_wm_message tmp_msg;
2505 DWORD_PTR tmp_param;
2509 while (OSS_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
2510 if (tmp_msg == WINE_WM_HEADER) {
2512 lpWaveHdr = (LPWAVEHDR)tmp_param;
2513 lpWaveHdr->lpNext = 0;
2515 if (wwi->lpQueuePtr == 0)
2516 wwi->lpQueuePtr = lpWaveHdr;
2518 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2522 ERR("should only have headers left\n");
2527 /**************************************************************************
2528 * widRecorder [internal]
2530 static DWORD CALLBACK widRecorder(LPVOID pmt)
2532 WORD uDevID = (DWORD_PTR)pmt;
2533 WINE_WAVEIN* wwi = &WInDev[uDevID];
2537 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2538 char *pOffset = buffer;
2539 audio_buf_info info;
2541 enum win_wm_message msg;
2546 wwi->state = WINE_WS_STOPPED;
2547 wwi->dwTotalRecorded = 0;
2548 wwi->dwTotalRead = 0;
2549 wwi->lpQueuePtr = NULL;
2551 SetEvent(wwi->hStartUpEvent);
2553 /* disable input so capture will begin when triggered */
2554 wwi->ossdev.bInputEnabled = FALSE;
2555 enable = getEnables(&wwi->ossdev);
2556 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2557 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2559 /* the soundblaster live needs a micro wake to get its recording started
2560 * (or GETISPACE will have 0 frags all the time)
2562 read(wwi->ossdev.fd, &xs, 4);
2564 /* make sleep time to be # of ms to output a fragment */
2565 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
2566 TRACE("sleeptime=%d ms\n", dwSleepTime);
2569 /* wait for dwSleepTime or an event in thread's queue */
2570 /* FIXME: could improve wait time depending on queue state,
2571 * ie, number of queued fragments
2574 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2576 lpWaveHdr = wwi->lpQueuePtr;
2578 ioctl(wwi->ossdev.fd, SNDCTL_DSP_GETISPACE, &info);
2579 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2581 /* read all the fragments accumulated so far */
2582 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2586 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2588 /* directly read fragment in wavehdr */
2589 bytesRead = read(wwi->ossdev.fd,
2590 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2591 wwi->dwFragmentSize);
2593 TRACE("bytesRead=%d (direct)\n", bytesRead);
2594 if (bytesRead != (DWORD) -1)
2596 /* update number of bytes recorded in current buffer and by this device */
2597 lpWaveHdr->dwBytesRecorded += bytesRead;
2598 wwi->dwTotalRead += bytesRead;
2599 wwi->dwTotalRecorded = wwi->dwTotalRead;
2601 /* buffer is full. notify client */
2602 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2604 /* must copy the value of next waveHdr, because we have no idea of what
2605 * will be done with the content of lpWaveHdr in callback
2607 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2609 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2610 lpWaveHdr->dwFlags |= WHDR_DONE;
2612 wwi->lpQueuePtr = lpNext;
2613 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2617 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev.dev_name,
2618 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2619 wwi->dwFragmentSize, strerror(errno));
2624 /* read the fragment in a local buffer */
2625 bytesRead = read(wwi->ossdev.fd, buffer, wwi->dwFragmentSize);
2628 TRACE("bytesRead=%d (local)\n", bytesRead);
2630 if (bytesRead == (DWORD) -1) {
2631 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->ossdev.dev_name,
2632 buffer, wwi->dwFragmentSize, strerror(errno));
2636 /* copy data in client buffers */
2637 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2639 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2641 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2645 /* update number of bytes recorded in current buffer and by this device */
2646 lpWaveHdr->dwBytesRecorded += dwToCopy;
2647 wwi->dwTotalRead += dwToCopy;
2648 wwi->dwTotalRecorded = wwi->dwTotalRead;
2649 bytesRead -= dwToCopy;
2650 pOffset += dwToCopy;
2652 /* client buffer is full. notify client */
2653 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2655 /* must copy the value of next waveHdr, because we have no idea of what
2656 * will be done with the content of lpWaveHdr in callback
2658 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2659 TRACE("lpNext=%p\n", lpNext);
2661 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2662 lpWaveHdr->dwFlags |= WHDR_DONE;
2664 wwi->lpQueuePtr = lpNext;
2665 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2668 if (!lpNext && bytesRead) {
2669 /* before we give up, check for more header messages */
2670 while (OSS_PeekRingMessage(&wwi->msgRing, &msg, ¶m, &ev))
2672 if (msg == WINE_WM_HEADER) {
2674 OSS_RetrieveRingMessage(&wwi->msgRing, &msg, ¶m, &ev);
2675 hdr = ((LPWAVEHDR)param);
2676 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg), hdr, ev);
2678 if (lpWaveHdr == 0) {
2679 /* new head of queue */
2680 wwi->lpQueuePtr = lpWaveHdr = hdr;
2682 /* insert buffer at the end of queue */
2684 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2691 if (lpWaveHdr == 0) {
2692 /* no more buffer to copy data to, but we did read more.
2693 * what hasn't been copied will be dropped
2695 WARN("buffer under run! %u bytes dropped.\n", bytesRead);
2696 wwi->lpQueuePtr = NULL;
2706 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2708 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, ¶m, &ev))
2710 TRACE("msg=%s param=0x%lx\n", getCmdString(msg), param);
2712 case WINE_WM_PAUSING:
2713 wwi->state = WINE_WS_PAUSED;
2714 /*FIXME("Device should stop recording\n");*/
2717 case WINE_WM_STARTING:
2718 wwi->state = WINE_WS_PLAYING;
2720 if (wwi->ossdev.bTriggerSupport)
2722 /* start the recording */
2723 wwi->ossdev.bInputEnabled = TRUE;
2724 enable = getEnables(&wwi->ossdev);
2725 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2726 wwi->ossdev.bInputEnabled = FALSE;
2727 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2732 unsigned char data[4];
2733 /* read 4 bytes to start the recording */
2734 read(wwi->ossdev.fd, data, 4);
2739 case WINE_WM_HEADER:
2740 lpWaveHdr = (LPWAVEHDR)param;
2741 lpWaveHdr->lpNext = 0;
2743 /* insert buffer at the end of queue */
2746 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2750 case WINE_WM_STOPPING:
2751 if (wwi->state != WINE_WS_STOPPED)
2753 if (wwi->ossdev.bTriggerSupport)
2755 /* stop the recording */
2756 wwi->ossdev.bInputEnabled = FALSE;
2757 enable = getEnables(&wwi->ossdev);
2758 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2759 wwi->ossdev.bInputEnabled = FALSE;
2760 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2764 /* read any headers in queue */
2765 widRecorder_ReadHeaders(wwi);
2767 /* return current buffer to app */
2768 lpWaveHdr = wwi->lpQueuePtr;
2771 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2772 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2773 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2774 lpWaveHdr->dwFlags |= WHDR_DONE;
2775 wwi->lpQueuePtr = lpNext;
2776 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2779 wwi->state = WINE_WS_STOPPED;
2782 case WINE_WM_RESETTING:
2783 if (wwi->state != WINE_WS_STOPPED)
2785 if (wwi->ossdev.bTriggerSupport)
2787 /* stop the recording */
2788 wwi->ossdev.bInputEnabled = FALSE;
2789 enable = getEnables(&wwi->ossdev);
2790 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2791 wwi->ossdev.bInputEnabled = FALSE;
2792 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2796 wwi->state = WINE_WS_STOPPED;
2797 wwi->dwTotalRecorded = 0;
2798 wwi->dwTotalRead = 0;
2800 /* read any headers in queue */
2801 widRecorder_ReadHeaders(wwi);
2803 /* return all buffers to the app */
2804 while (wwi->lpQueuePtr) {
2805 lpWaveHdr = wwi->lpQueuePtr;
2806 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2807 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2808 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2809 lpWaveHdr->dwFlags |= WHDR_DONE;
2810 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2815 case WINE_WM_UPDATE:
2816 if (wwi->state == WINE_WS_PLAYING) {
2817 audio_buf_info tmp_info;
2818 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_GETISPACE, &tmp_info) < 0)
2819 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", wwi->ossdev.dev_name, strerror(errno));
2821 wwi->dwTotalRecorded = wwi->dwTotalRead + tmp_info.bytes;
2825 case WINE_WM_CLOSING:
2827 wwi->state = WINE_WS_CLOSED;
2829 HeapFree(GetProcessHeap(), 0, buffer);
2831 /* shouldn't go here */
2833 FIXME("unknown message %d\n", msg);
2839 /* just for not generating compilation warnings... should never be executed */
2844 /**************************************************************************
2845 * widOpen [internal]
2847 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2850 audio_buf_info info;
2854 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
2855 if (lpDesc == NULL) {
2856 WARN("Invalid Parameter !\n");
2857 return MMSYSERR_INVALPARAM;
2859 if (wDevID >= numInDev) {
2860 WARN("bad device id: %d >= %d\n", wDevID, numInDev);
2861 return MMSYSERR_BADDEVICEID;
2864 /* only PCM format is supported so far... */
2865 if (!supportedFormat(lpDesc->lpFormat)) {
2866 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2867 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2868 lpDesc->lpFormat->nSamplesPerSec);
2869 return WAVERR_BADFORMAT;
2872 if (dwFlags & WAVE_FORMAT_QUERY) {
2873 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2874 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2875 lpDesc->lpFormat->nSamplesPerSec);
2876 return MMSYSERR_NOERROR;
2879 /* nBlockAlign and nAvgBytesPerSec are output variables for dsound */
2880 if (lpDesc->lpFormat->nBlockAlign != lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8) {
2881 lpDesc->lpFormat->nBlockAlign = lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
2882 WARN("Fixing nBlockAlign\n");
2884 if (lpDesc->lpFormat->nAvgBytesPerSec!= lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign) {
2885 lpDesc->lpFormat->nAvgBytesPerSec = lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
2886 WARN("Fixing nAvgBytesPerSec\n");
2889 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2890 lpDesc->lpFormat->nSamplesPerSec,
2891 lpDesc->lpFormat->wBitsPerSample,
2892 lpDesc->lpFormat->nChannels,
2893 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
2894 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
2897 wwi = &WInDev[wDevID];
2899 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2901 if ((dwFlags & WAVE_DIRECTSOUND) &&
2902 !(wwi->ossdev.in_caps_support & WAVECAPS_DIRECTSOUND))
2903 /* not supported, ignore it */
2904 dwFlags &= ~WAVE_DIRECTSOUND;
2906 if (dwFlags & WAVE_DIRECTSOUND) {
2907 TRACE("has DirectSoundCapture driver\n");
2908 if (wwi->ossdev.in_caps_support & WAVECAPS_SAMPLEACCURATE)
2909 /* we have realtime DirectSound, fragments just waste our time,
2910 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2911 audio_fragment = 0x0020000B;
2913 /* to approximate realtime, we must use small fragments,
2914 * let's try to fragment the above 64KB (256 * 2^8) */
2915 audio_fragment = 0x01000008;
2917 TRACE("doesn't have DirectSoundCapture driver\n");
2918 if (wwi->ossdev.open_count > 0) {
2919 TRACE("Using output device audio_fragment\n");
2920 /* FIXME: This may not be optimal for capture but it allows us
2921 * to do hardware playback without hardware capture. */
2922 audio_fragment = wwi->ossdev.audio_fragment;
2924 /* A wave device must have a worst case latency of 10 ms so calculate
2925 * the largest fragment size less than 10 ms long.
2927 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
2929 while ((1 << shift) <= fsize)
2932 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
2936 TRACE("requesting %d %d byte fragments (%d ms)\n", audio_fragment >> 16,
2937 1 << (audio_fragment & 0xffff),
2938 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
2940 ret = OSS_OpenDevice(&wwi->ossdev, O_RDONLY, &audio_fragment,
2942 lpDesc->lpFormat->nSamplesPerSec,
2943 lpDesc->lpFormat->nChannels,
2944 (lpDesc->lpFormat->wBitsPerSample == 16)
2945 ? AFMT_S16_LE : AFMT_U8);
2946 if (ret != 0) return ret;
2947 wwi->state = WINE_WS_STOPPED;
2949 if (wwi->lpQueuePtr) {
2950 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2951 wwi->lpQueuePtr = NULL;
2953 wwi->dwTotalRecorded = 0;
2954 wwi->dwTotalRead = 0;
2955 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2957 wwi->waveDesc = *lpDesc;
2958 copy_format(lpDesc->lpFormat, &wwi->waveFormat);
2960 if (ioctl(wwi->ossdev.fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
2961 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
2962 wwi->ossdev.dev_name, strerror(errno));
2963 OSS_CloseDevice(&wwi->ossdev);
2964 wwi->state = WINE_WS_CLOSED;
2965 return MMSYSERR_NOTENABLED;
2968 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2969 info.fragsize, (info.fragsize * 1000) / (wwi->ossdev.sample_rate *
2970 wwi->ossdev.channels * (wwi->ossdev.format == AFMT_U8 ? 1 : 2)));
2972 wwi->dwFragmentSize = info.fragsize;
2974 TRACE("dwFragmentSize=%u\n", wwi->dwFragmentSize);
2975 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2976 wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
2977 wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
2978 wwi->waveFormat.Format.nBlockAlign);
2980 OSS_InitRingMessage(&wwi->msgRing);
2982 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2983 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwi->dwThreadID));
2985 SetThreadPriority(wwi->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2986 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2987 CloseHandle(wwi->hStartUpEvent);
2988 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2990 widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2991 return MMSYSERR_NOERROR;
2994 /**************************************************************************
2995 * widClose [internal]
2997 static DWORD widClose(WORD wDevID)
3001 TRACE("(%u);\n", wDevID);
3002 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3003 WARN("can't close !\n");
3004 return MMSYSERR_INVALHANDLE;
3007 wwi = &WInDev[wDevID];
3009 if (wwi->lpQueuePtr != NULL) {
3010 WARN("still buffers open !\n");
3011 return WAVERR_STILLPLAYING;
3014 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
3015 OSS_CloseDevice(&wwi->ossdev);
3016 wwi->state = WINE_WS_CLOSED;
3017 wwi->dwFragmentSize = 0;
3018 OSS_DestroyRingMessage(&wwi->msgRing);
3019 widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
3020 return MMSYSERR_NOERROR;
3023 /**************************************************************************
3024 * widAddBuffer [internal]
3026 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3028 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
3030 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3031 WARN("can't do it !\n");
3032 return MMSYSERR_INVALHANDLE;
3034 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
3035 TRACE("never been prepared !\n");
3036 return WAVERR_UNPREPARED;
3038 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
3039 TRACE("header already in use !\n");
3040 return WAVERR_STILLPLAYING;
3043 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
3044 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3045 lpWaveHdr->dwBytesRecorded = 0;
3046 lpWaveHdr->lpNext = NULL;
3048 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
3049 return MMSYSERR_NOERROR;
3052 /**************************************************************************
3053 * widStart [internal]
3055 static DWORD widStart(WORD wDevID)
3057 TRACE("(%u);\n", wDevID);
3058 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3059 WARN("can't start recording !\n");
3060 return MMSYSERR_INVALHANDLE;
3063 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
3064 return MMSYSERR_NOERROR;
3067 /**************************************************************************
3068 * widStop [internal]
3070 static DWORD widStop(WORD wDevID)
3072 TRACE("(%u);\n", wDevID);
3073 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3074 WARN("can't stop !\n");
3075 return MMSYSERR_INVALHANDLE;
3078 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3080 return MMSYSERR_NOERROR;
3083 /**************************************************************************
3084 * widReset [internal]
3086 static DWORD widReset(WORD wDevID)
3088 TRACE("(%u);\n", wDevID);
3089 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3090 WARN("can't reset !\n");
3091 return MMSYSERR_INVALHANDLE;
3093 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3094 return MMSYSERR_NOERROR;
3097 /**************************************************************************
3098 * widGetPosition [internal]
3100 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3104 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
3106 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3107 WARN("can't get pos !\n");
3108 return MMSYSERR_INVALHANDLE;
3111 if (lpTime == NULL) {
3112 WARN("invalid parameter: lpTime == NULL\n");
3113 return MMSYSERR_INVALPARAM;
3116 wwi = &WInDev[wDevID];
3117 #ifdef EXACT_WIDPOSITION
3118 if (wwi->ossdev.in_caps_support & WAVECAPS_SAMPLEACCURATE)
3119 OSS_AddRingMessage(&(wwi->msgRing), WINE_WM_UPDATE, 0, TRUE);
3122 return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->waveFormat);
3125 /**************************************************************************
3126 * widMessage (WINEOSS.6)
3128 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD_PTR dwUser,
3129 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
3131 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
3132 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
3136 return OSS_WaveInit();
3138 return OSS_WaveExit();
3141 /* FIXME: Pretend this is supported */
3143 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3144 case WIDM_CLOSE: return widClose (wDevID);
3145 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3146 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
3147 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
3148 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
3149 case WIDM_GETNUMDEVS: return numInDev;
3150 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
3151 case WIDM_RESET: return widReset (wDevID);
3152 case WIDM_START: return widStart (wDevID);
3153 case WIDM_STOP: return widStop (wDevID);
3154 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
3155 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
3156 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3157 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3159 FIXME("unknown message %u!\n", wMsg);
3161 return MMSYSERR_NOTSUPPORTED;
3164 /**************************************************************************
3165 * DriverProc (WINEOSS.1)
3167 LRESULT CALLBACK OSS_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
3168 LPARAM dwParam1, LPARAM dwParam2)
3170 TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
3171 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
3180 case DRV_QUERYCONFIGURE:
3182 case DRV_CONFIGURE: MessageBoxA(0, "OSS MultiMedia Driver !", "OSS Driver", MB_OK); return 1;