Added support for several soundcard.
[wine.git] / dlls / winmm / wineoss / audio.c
blob82e0661f3306e7f2eab01e6eb59bb2a2aa801928
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Sample Wine Driver for Open Sound System (featured in Linux and FreeBSD)
5 * Copyright 1994 Martin Ayotte
6 * 1999 Eric Pouech (async playing in waveOut/waveIn)
7 * 2000 Eric Pouech (loops in waveOut)
8 * 2002 Eric Pouech (full duplex)
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * FIXME:
26 * pause in waveOut does not work correctly in loop mode
29 /*#define EMULATE_SB16*/
31 /* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
32 #define USE_PIPE_SYNC
34 /* an exact wodGetPosition is usually not worth the extra context switches,
35 * as we're going to have near fragment accuracy anyway */
36 /* #define EXACT_WODPOSITION */
38 #include "config.h"
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #include <errno.h>
47 #include <fcntl.h>
48 #ifdef HAVE_SYS_IOCTL_H
49 # include <sys/ioctl.h>
50 #endif
51 #ifdef HAVE_SYS_MMAN_H
52 # include <sys/mman.h>
53 #endif
54 #ifdef HAVE_SYS_POLL_H
55 # include <sys/poll.h>
56 #endif
58 #include "windef.h"
59 #include "wingdi.h"
60 #include "winerror.h"
61 #include "wine/winuser16.h"
62 #include "mmddk.h"
63 #include "dsound.h"
64 #include "dsdriver.h"
65 #include "oss.h"
66 #include "wine/debug.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(wave);
70 /* Allow 1% deviation for sample rates (some ES137x cards) */
71 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
73 #ifdef HAVE_OSS
75 #define MAX_WAVEDRV (3)
77 /* state diagram for waveOut writing:
79 * +---------+-------------+---------------+---------------------------------+
80 * | state | function | event | new state |
81 * +---------+-------------+---------------+---------------------------------+
82 * | | open() | | STOPPED |
83 * | PAUSED | write() | | PAUSED |
84 * | STOPPED | write() | <thrd create> | PLAYING |
85 * | PLAYING | write() | HEADER | PLAYING |
86 * | (other) | write() | <error> | |
87 * | (any) | pause() | PAUSING | PAUSED |
88 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
89 * | (any) | reset() | RESETTING | STOPPED |
90 * | (any) | close() | CLOSING | CLOSED |
91 * +---------+-------------+---------------+---------------------------------+
94 /* states of the playing device */
95 #define WINE_WS_PLAYING 0
96 #define WINE_WS_PAUSED 1
97 #define WINE_WS_STOPPED 2
98 #define WINE_WS_CLOSED 3
100 /* events to be send to device */
101 enum win_wm_message {
102 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
103 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING
106 #ifdef USE_PIPE_SYNC
107 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
108 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
109 #define RESET_OMR(omr) do { } while (0)
110 #define WAIT_OMR(omr, sleep) \
111 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
112 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
113 #else
114 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
115 #define CLEAR_OMR(omr) do { } while (0)
116 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
117 #define WAIT_OMR(omr, sleep) \
118 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
119 #endif
121 typedef struct {
122 enum win_wm_message msg; /* message identifier */
123 DWORD param; /* parameter for this message */
124 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
125 } OSS_MSG;
127 /* implement an in-process message ring for better performance
128 * (compared to passing thru the server)
129 * this ring will be used by the input (resp output) record (resp playback) routine
131 typedef struct {
132 /* FIXME: this could be made a dynamically growing array (if needed) */
133 /* maybe it's needed, a Humongous game manages to transmit 128 messages at once at startup */
134 #define OSS_RING_BUFFER_SIZE 192
135 OSS_MSG messages[OSS_RING_BUFFER_SIZE];
136 int msg_tosave;
137 int msg_toget;
138 #ifdef USE_PIPE_SYNC
139 int msg_pipe[2];
140 #else
141 HANDLE msg_event;
142 #endif
143 CRITICAL_SECTION msg_crst;
144 } OSS_MSG_RING;
146 typedef struct tagOSS_DEVICE {
147 const char* dev_name;
148 const char* mixer_name;
149 unsigned open_count;
150 WAVEOUTCAPSA out_caps;
151 WAVEINCAPSA in_caps;
152 unsigned open_access;
153 int fd;
154 DWORD owner_tid;
155 unsigned sample_rate;
156 unsigned stereo;
157 unsigned format;
158 unsigned audio_fragment;
159 BOOL full_duplex;
160 } OSS_DEVICE;
162 static OSS_DEVICE OSS_Devices[MAX_WAVEDRV];
164 typedef struct {
165 OSS_DEVICE* ossdev;
166 volatile int state; /* one of the WINE_WS_ manifest constants */
167 WAVEOPENDESC waveDesc;
168 WORD wFlags;
169 PCMWAVEFORMAT format;
171 /* OSS information */
172 DWORD dwFragmentSize; /* size of OSS buffer fragment */
173 DWORD dwBufferSize; /* size of whole OSS buffer in bytes */
174 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
175 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
176 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
178 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
179 DWORD dwLoops; /* private copy of loop counter */
181 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
182 DWORD dwWrittenTotal; /* number of bytes written to OSS buffer since opening */
183 BOOL bNeedPost; /* whether audio still needs to be physically started */
185 /* synchronization stuff */
186 HANDLE hStartUpEvent;
187 HANDLE hThread;
188 DWORD dwThreadID;
189 OSS_MSG_RING msgRing;
191 /* DirectSound stuff */
192 LPBYTE mapping;
193 DWORD maplen;
194 } WINE_WAVEOUT;
196 typedef struct {
197 OSS_DEVICE* ossdev;
198 volatile int state;
199 DWORD dwFragmentSize; /* OpenSound '/dev/dsp' give us that size */
200 WAVEOPENDESC waveDesc;
201 WORD wFlags;
202 PCMWAVEFORMAT format;
203 LPWAVEHDR lpQueuePtr;
204 DWORD dwTotalRecorded;
205 BOOL bTriggerSupport;
207 /* synchronization stuff */
208 HANDLE hThread;
209 DWORD dwThreadID;
210 HANDLE hStartUpEvent;
211 OSS_MSG_RING msgRing;
212 } WINE_WAVEIN;
214 static WINE_WAVEOUT WOutDev [MAX_WAVEDRV];
215 static WINE_WAVEIN WInDev [MAX_WAVEDRV];
216 static unsigned numOutDev;
217 static unsigned numInDev;
219 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
221 /* These strings used only for tracing */
222 static const char *wodPlayerCmdString[] = {
223 "WINE_WM_PAUSING",
224 "WINE_WM_RESTARTING",
225 "WINE_WM_RESETTING",
226 "WINE_WM_HEADER",
227 "WINE_WM_UPDATE",
228 "WINE_WM_BREAKLOOP",
229 "WINE_WM_CLOSING",
232 /*======================================================================*
233 * Low level WAVE implementation *
234 *======================================================================*/
236 /******************************************************************
237 * OSS_RawOpenDevice
239 * Low level device opening (from values stored in ossdev)
241 static int OSS_RawOpenDevice(OSS_DEVICE* ossdev, int* frag)
243 int fd, val;
245 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
247 WARN("Couldn't open out %s (%s)\n", ossdev->dev_name, strerror(errno));
248 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
250 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
251 /* turn full duplex on if it has been requested */
252 if (ossdev->open_access == O_RDWR && ossdev->full_duplex)
253 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
255 if (ossdev->audio_fragment)
257 ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
260 /* First size and stereo then samplerate */
261 if (ossdev->format)
263 val = ossdev->format;
264 ioctl(fd, SNDCTL_DSP_SETFMT, &val);
265 if (val != ossdev->format)
266 ERR("Can't set format to %d (%d)\n", ossdev->format, val);
268 if (ossdev->stereo)
270 val = ossdev->stereo;
271 ioctl(fd, SNDCTL_DSP_STEREO, &val);
272 if (val != ossdev->stereo)
273 ERR("Can't set stereo to %u (%d)\n", ossdev->stereo, val);
275 if (ossdev->sample_rate)
277 val = ossdev->sample_rate;
278 ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
279 if (!NEAR_MATCH(val, ossdev->sample_rate))
280 ERR("Can't set sample_rate to %u (%d)\n", ossdev->sample_rate, val);
282 return fd;
285 /******************************************************************
286 * OSS_OpenDevice
288 * since OSS has poor capabilities in full duplex, we try here to let a program
289 * open the device for both waveout and wavein streams...
290 * this is hackish, but it's the way OSS interface is done...
292 static DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
293 int* frag, int sample_rate, int stereo, int fmt)
295 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
296 req_access = O_RDWR;
298 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
299 if (ossdev->open_count == 0)
301 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
303 ossdev->audio_fragment = (frag) ? *frag : 0;
304 ossdev->sample_rate = sample_rate;
305 ossdev->stereo = stereo;
306 ossdev->format = fmt;
307 ossdev->open_access = req_access;
308 ossdev->owner_tid = GetCurrentThreadId();
310 if ((ossdev->fd = OSS_RawOpenDevice(ossdev, frag)) == -1)
311 return MMSYSERR_ERROR;
313 else
315 /* check we really open with the same parameters */
316 if (ossdev->open_access != req_access)
318 WARN("Mismatch in access...\n");
319 return WAVERR_BADFORMAT;
321 /* FIXME: if really needed, we could do, in this case, on the fly
322 * PCM conversion (using the MSACM ad hoc driver)
324 if (ossdev->audio_fragment != (frag ? *frag : 0) ||
325 ossdev->sample_rate != sample_rate ||
326 ossdev->stereo != stereo ||
327 ossdev->format != fmt)
329 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
330 "OSS doesn't allow us different parameters\n"
331 "audio_frag(%x/%x) sample_rate(%d/%d) stereo(%d/%d) fmt(%d/%d)\n",
332 ossdev->audio_fragment, frag ? *frag : 0,
333 ossdev->sample_rate, sample_rate,
334 ossdev->stereo, stereo,
335 ossdev->format, fmt);
336 return WAVERR_BADFORMAT;
338 if (GetCurrentThreadId() != ossdev->owner_tid)
340 WARN("Another thread is trying to access audio...\n");
341 return MMSYSERR_ERROR;
345 ossdev->open_count++;
347 return MMSYSERR_NOERROR;
350 /******************************************************************
351 * OSS_CloseDevice
355 static void OSS_CloseDevice(OSS_DEVICE* ossdev)
357 if (--ossdev->open_count == 0) close(ossdev->fd);
360 /******************************************************************
361 * OSS_ResetDevice
363 * Resets the device. OSS Commercial requires the device to be closed
364 * after a SNDCTL_DSP_RESET ioctl call... this function implements
365 * this behavior...
367 static int OSS_ResetDevice(OSS_DEVICE* ossdev)
369 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
371 perror("ioctl SNDCTL_DSP_RESET");
372 return -1;
374 TRACE("Changing fd from %d to ", ossdev->fd);
375 close(ossdev->fd);
376 OSS_RawOpenDevice(ossdev, &ossdev->audio_fragment);
377 TRACE("%d\n", ossdev->fd);
378 return ossdev->fd;
381 /******************************************************************
382 * OSS_WaveOutInit
386 static void OSS_WaveOutInit(unsigned devID, OSS_DEVICE* ossdev)
388 int smplrate;
389 int samplesize = 16;
390 int dsp_stereo = 1;
391 int bytespersmpl;
392 int caps;
393 int mask;
395 WOutDev[devID].state = WINE_WS_CLOSED;
396 WOutDev[devID].ossdev = ossdev;
397 memset(&ossdev->out_caps, 0, sizeof(ossdev->out_caps));
399 if (OSS_OpenDevice(WOutDev[devID].ossdev, O_WRONLY, NULL, 0, 0, 0) != 0) return;
400 numOutDev++;
402 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
404 /* FIXME: some programs compare this string against the content of the registry
405 * for MM drivers. The names have to match in order for the program to work
406 * (e.g. MS win9x mplayer.exe)
408 #ifdef EMULATE_SB16
409 ossdev->out_caps.wMid = 0x0002;
410 ossdev->out_caps.wPid = 0x0104;
411 strcpy(ossdev->out_caps.szPname, "SB16 Wave Out");
412 #else
413 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
414 ossdev->out_caps.wPid = 0x0001; /* Product ID */
415 /* strcpy(ossdev->out_caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
416 strcpy(ossdev->out_caps.szPname, "CS4236/37/38");
417 #endif
418 ossdev->out_caps.vDriverVersion = 0x0100;
419 ossdev->out_caps.dwFormats = 0x00000000;
420 ossdev->out_caps.dwSupport = WAVECAPS_VOLUME;
422 ioctl(ossdev->fd, SNDCTL_DSP_GETFMTS, &mask);
423 TRACE("OSS dsp out mask=%08x\n", mask);
425 /* First bytespersampl, then stereo */
426 bytespersmpl = (ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &samplesize) != 0) ? 1 : 2;
428 ossdev->out_caps.wChannels = (ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &dsp_stereo) != 0) ? 1 : 2;
429 if (ossdev->out_caps.wChannels > 1) ossdev->out_caps.dwSupport |= WAVECAPS_LRVOLUME;
431 smplrate = 44100;
432 if (ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &smplrate) == 0) {
433 if (mask & AFMT_U8) {
434 ossdev->out_caps.dwFormats |= WAVE_FORMAT_4M08;
435 if (ossdev->out_caps.wChannels > 1)
436 ossdev->out_caps.dwFormats |= WAVE_FORMAT_4S08;
438 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
439 ossdev->out_caps.dwFormats |= WAVE_FORMAT_4M16;
440 if (ossdev->out_caps.wChannels > 1)
441 ossdev->out_caps.dwFormats |= WAVE_FORMAT_4S16;
444 smplrate = 22050;
445 if (ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &smplrate) == 0) {
446 if (mask & AFMT_U8) {
447 ossdev->out_caps.dwFormats |= WAVE_FORMAT_2M08;
448 if (ossdev->out_caps.wChannels > 1)
449 ossdev->out_caps.dwFormats |= WAVE_FORMAT_2S08;
451 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
452 ossdev->out_caps.dwFormats |= WAVE_FORMAT_2M16;
453 if (ossdev->out_caps.wChannels > 1)
454 ossdev->out_caps.dwFormats |= WAVE_FORMAT_2S16;
457 smplrate = 11025;
458 if (ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &smplrate) == 0) {
459 if (mask & AFMT_U8) {
460 ossdev->out_caps.dwFormats |= WAVE_FORMAT_1M08;
461 if (ossdev->out_caps.wChannels > 1)
462 ossdev->out_caps.dwFormats |= WAVE_FORMAT_1S08;
464 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
465 ossdev->out_caps.dwFormats |= WAVE_FORMAT_1M16;
466 if (ossdev->out_caps.wChannels > 1)
467 ossdev->out_caps.dwFormats |= WAVE_FORMAT_1S16;
470 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0) {
471 TRACE("OSS dsp out caps=%08X\n", caps);
472 if ((caps & DSP_CAP_REALTIME) && !(caps & DSP_CAP_BATCH)) {
473 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
475 /* well, might as well use the DirectSound cap flag for something */
476 if ((caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) &&
477 !(caps & DSP_CAP_BATCH))
478 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
480 OSS_CloseDevice(ossdev);
481 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
482 ossdev->out_caps.dwFormats, ossdev->out_caps.dwSupport);
486 /******************************************************************
487 * OSS_WaveInInit
491 static void OSS_WaveInInit(unsigned devID, OSS_DEVICE* ossdev)
493 int smplrate;
494 int samplesize = 16;
495 int dsp_stereo = 1;
496 int bytespersmpl;
497 int caps;
498 int mask;
500 samplesize = 16;
501 dsp_stereo = 1;
503 WInDev[devID].state = WINE_WS_CLOSED;
504 WInDev[devID].ossdev = ossdev;
506 memset(&ossdev->in_caps, 0, sizeof(ossdev->in_caps));
508 if (OSS_OpenDevice(WInDev[devID].ossdev, O_RDONLY, NULL, 0, 0, 0) != 0) return;
509 numInDev++;
511 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
513 #ifdef EMULATE_SB16
514 ossdev->in_caps.wMid = 0x0002;
515 ossdev->in_caps.wPid = 0x0004;
516 strcpy(ossdev->in_caps.szPname, "SB16 Wave In");
517 #else
518 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
519 ossdev->in_caps.wPid = 0x0001; /* Product ID */
520 strcpy(ossdev->in_caps.szPname, "OpenSoundSystem WAVIN Driver");
521 #endif
522 ossdev->in_caps.dwFormats = 0x00000000;
523 ossdev->in_caps.wChannels = (ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &dsp_stereo) != 0) ? 1 : 2;
525 WInDev[devID].bTriggerSupport = FALSE;
526 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0) {
527 TRACE("OSS dsp in caps=%08X\n", caps);
528 if (caps & DSP_CAP_TRIGGER)
529 WInDev[devID].bTriggerSupport = TRUE;
532 ioctl(ossdev->fd, SNDCTL_DSP_GETFMTS, &mask);
533 TRACE("OSS in dsp mask=%08x\n", mask);
535 bytespersmpl = (ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &samplesize) != 0) ? 1 : 2;
536 smplrate = 44100;
537 if (ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &smplrate) == 0) {
538 if (mask & AFMT_U8) {
539 ossdev->in_caps.dwFormats |= WAVE_FORMAT_4M08;
540 if (ossdev->in_caps.wChannels > 1)
541 ossdev->in_caps.dwFormats |= WAVE_FORMAT_4S08;
543 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
544 ossdev->in_caps.dwFormats |= WAVE_FORMAT_4M16;
545 if (ossdev->in_caps.wChannels > 1)
546 ossdev->in_caps.dwFormats |= WAVE_FORMAT_4S16;
549 smplrate = 22050;
550 if (ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &smplrate) == 0) {
551 if (mask & AFMT_U8) {
552 ossdev->in_caps.dwFormats |= WAVE_FORMAT_2M08;
553 if (ossdev->in_caps.wChannels > 1)
554 ossdev->in_caps.dwFormats |= WAVE_FORMAT_2S08;
556 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
557 ossdev->in_caps.dwFormats |= WAVE_FORMAT_2M16;
558 if (ossdev->in_caps.wChannels > 1)
559 ossdev->in_caps.dwFormats |= WAVE_FORMAT_2S16;
562 smplrate = 11025;
563 if (ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &smplrate) == 0) {
564 if (mask & AFMT_U8) {
565 ossdev->in_caps.dwFormats |= WAVE_FORMAT_1M08;
566 if (ossdev->in_caps.wChannels > 1)
567 ossdev->in_caps.dwFormats |= WAVE_FORMAT_1S08;
569 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
570 ossdev->in_caps.dwFormats |= WAVE_FORMAT_1M16;
571 if (ossdev->in_caps.wChannels > 1)
572 ossdev->in_caps.dwFormats |= WAVE_FORMAT_1S16;
575 OSS_CloseDevice(ossdev);
576 TRACE("in dwFormats = %08lX\n", ossdev->in_caps.dwFormats);
579 /******************************************************************
580 * OSS_WaveFullDuplexInit
584 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
586 int caps;
588 if (OSS_OpenDevice(ossdev, O_RDWR, NULL, 0, 0, 0) != 0) return;
589 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
591 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
593 OSS_CloseDevice(ossdev);
596 /******************************************************************
597 * OSS_WaveInit
599 * Initialize internal structures from OSS information
601 LONG OSS_WaveInit(void)
603 int i;
605 /* FIXME: only one device is supported */
606 memset(&OSS_Devices, 0, sizeof(OSS_Devices));
607 /* FIXME: should check that dsp actually points to dsp0, or that dsp0 exists
608 * we should also be able to configure (bitmap) which devices we want to use...
609 * - or even store the name of all drivers in our configuration
611 OSS_Devices[0].dev_name = "/dev/dsp";
612 OSS_Devices[0].mixer_name = "/dev/mixer";
613 OSS_Devices[1].dev_name = "/dev/dsp1";
614 OSS_Devices[1].mixer_name = "/dev/mixer1";
615 OSS_Devices[2].dev_name = "/dev/dsp2";
616 OSS_Devices[2].mixer_name = "/dev/mixer2";
618 /* start with output device */
619 for (i = 0; i < MAX_WAVEDRV; ++i)
620 OSS_WaveOutInit(i, &OSS_Devices[i]);
622 /* then do input device */
623 for (i = 0; i < MAX_WAVEDRV; ++i)
624 OSS_WaveInInit(i, &OSS_Devices[i]);
626 /* finish with the full duplex bits */
627 for (i = 0; i < MAX_WAVEDRV; i++)
628 OSS_WaveFullDuplexInit(&OSS_Devices[i]);
630 return 0;
633 /******************************************************************
634 * OSS_InitRingMessage
636 * Initialize the ring of messages for passing between driver's caller and playback/record
637 * thread
639 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
641 omr->msg_toget = 0;
642 omr->msg_tosave = 0;
643 #ifdef USE_PIPE_SYNC
644 if (pipe(omr->msg_pipe) < 0) {
645 omr->msg_pipe[0] = -1;
646 omr->msg_pipe[1] = -1;
647 ERR("could not create pipe, error=%s\n", strerror(errno));
649 #else
650 omr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
651 #endif
652 memset(omr->messages, 0, sizeof(OSS_MSG) * OSS_RING_BUFFER_SIZE);
653 InitializeCriticalSection(&omr->msg_crst);
654 return 0;
657 /******************************************************************
658 * OSS_DestroyRingMessage
661 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
663 #ifdef USE_PIPE_SYNC
664 close(omr->msg_pipe[0]);
665 close(omr->msg_pipe[1]);
666 #else
667 CloseHandle(omr->msg_event);
668 #endif
669 DeleteCriticalSection(&omr->msg_crst);
670 return 0;
673 /******************************************************************
674 * OSS_AddRingMessage
676 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
678 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
680 HANDLE hEvent = INVALID_HANDLE_VALUE;
682 EnterCriticalSection(&omr->msg_crst);
683 if ((omr->msg_toget == ((omr->msg_tosave + 1) % OSS_RING_BUFFER_SIZE))) /* buffer overflow ? */
685 ERR("buffer overflow !?\n");
686 LeaveCriticalSection(&omr->msg_crst);
687 return 0;
689 if (wait)
691 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
692 if (hEvent == INVALID_HANDLE_VALUE)
694 ERR("can't create event !?\n");
695 LeaveCriticalSection(&omr->msg_crst);
696 return 0;
698 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
699 FIXME("two fast messages in the queue!!!!\n");
701 /* fast messages have to be added at the start of the queue */
702 omr->msg_toget = (omr->msg_toget + OSS_RING_BUFFER_SIZE - 1) % OSS_RING_BUFFER_SIZE;
704 omr->messages[omr->msg_toget].msg = msg;
705 omr->messages[omr->msg_toget].param = param;
706 omr->messages[omr->msg_toget].hEvent = hEvent;
708 else
710 omr->messages[omr->msg_tosave].msg = msg;
711 omr->messages[omr->msg_tosave].param = param;
712 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
713 omr->msg_tosave = (omr->msg_tosave + 1) % OSS_RING_BUFFER_SIZE;
715 LeaveCriticalSection(&omr->msg_crst);
716 /* signal a new message */
717 SIGNAL_OMR(omr);
718 if (wait)
720 /* wait for playback/record thread to have processed the message */
721 WaitForSingleObject(hEvent, INFINITE);
722 CloseHandle(hEvent);
724 return 1;
727 /******************************************************************
728 * OSS_RetrieveRingMessage
730 * Get a message from the ring. Should be called by the playback/record thread.
732 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
733 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
735 EnterCriticalSection(&omr->msg_crst);
737 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
739 LeaveCriticalSection(&omr->msg_crst);
740 return 0;
743 *msg = omr->messages[omr->msg_toget].msg;
744 omr->messages[omr->msg_toget].msg = 0;
745 *param = omr->messages[omr->msg_toget].param;
746 *hEvent = omr->messages[omr->msg_toget].hEvent;
747 omr->msg_toget = (omr->msg_toget + 1) % OSS_RING_BUFFER_SIZE;
748 CLEAR_OMR(omr);
749 LeaveCriticalSection(&omr->msg_crst);
750 return 1;
753 /*======================================================================*
754 * Low level WAVE OUT implementation *
755 *======================================================================*/
757 /**************************************************************************
758 * wodNotifyClient [internal]
760 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
762 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
764 switch (wMsg) {
765 case WOM_OPEN:
766 case WOM_CLOSE:
767 case WOM_DONE:
768 if (wwo->wFlags != DCB_NULL &&
769 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
770 (HDRVR)wwo->waveDesc.hWave, wMsg,
771 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
772 WARN("can't notify client !\n");
773 return MMSYSERR_ERROR;
775 break;
776 default:
777 FIXME("Unknown callback message %u\n", wMsg);
778 return MMSYSERR_INVALPARAM;
780 return MMSYSERR_NOERROR;
783 /**************************************************************************
784 * wodUpdatePlayedTotal [internal]
787 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
789 audio_buf_info dspspace;
790 if (!info) info = &dspspace;
792 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
793 ERR("ioctl can't 'SNDCTL_DSP_GETOSPACE' !\n");
794 return FALSE;
796 wwo->dwPlayedTotal = wwo->dwWrittenTotal - (wwo->dwBufferSize - info->bytes);
797 return TRUE;
800 /**************************************************************************
801 * wodPlayer_BeginWaveHdr [internal]
803 * Makes the specified lpWaveHdr the currently playing wave header.
804 * If the specified wave header is a begin loop and we're not already in
805 * a loop, setup the loop.
807 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
809 wwo->lpPlayPtr = lpWaveHdr;
811 if (!lpWaveHdr) return;
813 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
814 if (wwo->lpLoopPtr) {
815 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
816 } else {
817 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
818 wwo->lpLoopPtr = lpWaveHdr;
819 /* Windows does not touch WAVEHDR.dwLoops,
820 * so we need to make an internal copy */
821 wwo->dwLoops = lpWaveHdr->dwLoops;
824 wwo->dwPartialOffset = 0;
827 /**************************************************************************
828 * wodPlayer_PlayPtrNext [internal]
830 * Advance the play pointer to the next waveheader, looping if required.
832 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
834 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
836 wwo->dwPartialOffset = 0;
837 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
838 /* We're at the end of a loop, loop if required */
839 if (--wwo->dwLoops > 0) {
840 wwo->lpPlayPtr = wwo->lpLoopPtr;
841 } else {
842 /* Handle overlapping loops correctly */
843 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
844 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
845 /* shall we consider the END flag for the closing loop or for
846 * the opening one or for both ???
847 * code assumes for closing loop only
849 } else {
850 lpWaveHdr = lpWaveHdr->lpNext;
852 wwo->lpLoopPtr = NULL;
853 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
855 } else {
856 /* We're not in a loop. Advance to the next wave header */
857 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
860 return lpWaveHdr;
863 /**************************************************************************
864 * wodPlayer_DSPWait [internal]
865 * Returns the number of milliseconds to wait for the DSP buffer to write
866 * one fragment.
868 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
870 /* time for one fragment to be played */
871 return wwo->dwFragmentSize * 1000 / wwo->format.wf.nAvgBytesPerSec;
874 /**************************************************************************
875 * wodPlayer_NotifyWait [internal]
876 * Returns the number of milliseconds to wait before attempting to notify
877 * completion of the specified wavehdr.
878 * This is based on the number of bytes remaining to be written in the
879 * wave.
881 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
883 DWORD dwMillis;
885 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
886 dwMillis = 1;
887 } else {
888 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
889 if (!dwMillis) dwMillis = 1;
892 return dwMillis;
896 /**************************************************************************
897 * wodPlayer_WriteMaxFrags [internal]
898 * Writes the maximum number of bytes possible to the DSP and returns
899 * TRUE iff the current playPtr has been fully played
901 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
903 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
904 DWORD toWrite = min(dwLength, *bytes);
905 int written;
906 BOOL ret = FALSE;
908 TRACE("Writing wavehdr %p.%lu[%lu]/%lu\n",
909 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
911 if (toWrite > 0)
913 written = write(wwo->ossdev->fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
914 if (written <= 0) return FALSE;
916 else
917 written = 0;
919 if (written >= dwLength) {
920 /* If we wrote all current wavehdr, skip to the next one */
921 wodPlayer_PlayPtrNext(wwo);
922 ret = TRUE;
923 } else {
924 /* Remove the amount written */
925 wwo->dwPartialOffset += written;
927 *bytes -= written;
928 wwo->dwWrittenTotal += written;
930 return ret;
934 /**************************************************************************
935 * wodPlayer_NotifyCompletions [internal]
937 * Notifies and remove from queue all wavehdrs which have been played to
938 * the speaker (ie. they have cleared the OSS buffer). If force is true,
939 * we notify all wavehdrs and remove them all from the queue even if they
940 * are unplayed or part of a loop.
942 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
944 LPWAVEHDR lpWaveHdr;
946 /* Start from lpQueuePtr and keep notifying until:
947 * - we hit an unwritten wavehdr
948 * - we hit the beginning of a running loop
949 * - we hit a wavehdr which hasn't finished playing
951 while ((lpWaveHdr = wwo->lpQueuePtr) &&
952 (force ||
953 (lpWaveHdr != wwo->lpPlayPtr &&
954 lpWaveHdr != wwo->lpLoopPtr &&
955 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
957 wwo->lpQueuePtr = lpWaveHdr->lpNext;
959 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
960 lpWaveHdr->dwFlags |= WHDR_DONE;
962 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
964 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
965 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
968 /**************************************************************************
969 * wodPlayer_Reset [internal]
971 * wodPlayer helper. Resets current output stream.
973 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
975 wodUpdatePlayedTotal(wwo, NULL);
976 /* updates current notify list */
977 wodPlayer_NotifyCompletions(wwo, FALSE);
979 /* flush all possible output */
980 if (OSS_ResetDevice(wwo->ossdev) == -1)
982 wwo->hThread = 0;
983 wwo->state = WINE_WS_STOPPED;
984 ExitThread(-1);
987 if (reset) {
988 enum win_wm_message msg;
989 DWORD param;
990 HANDLE ev;
992 /* remove any buffer */
993 wodPlayer_NotifyCompletions(wwo, TRUE);
995 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
996 wwo->state = WINE_WS_STOPPED;
997 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
998 /* Clear partial wavehdr */
999 wwo->dwPartialOffset = 0;
1001 /* remove any existing message in the ring */
1002 EnterCriticalSection(&wwo->msgRing.msg_crst);
1003 /* return all pending headers in queue */
1004 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1006 if (msg != WINE_WM_HEADER)
1008 FIXME("shouldn't have headers left\n");
1009 SetEvent(ev);
1010 continue;
1012 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1013 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1015 wodNotifyClient(wwo, WOM_DONE, param, 0);
1017 RESET_OMR(&wwo->msgRing);
1018 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1019 } else {
1020 if (wwo->lpLoopPtr) {
1021 /* complicated case, not handled yet (could imply modifying the loop counter */
1022 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1023 wwo->lpPlayPtr = wwo->lpLoopPtr;
1024 wwo->dwPartialOffset = 0;
1025 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1026 } else {
1027 LPWAVEHDR ptr;
1028 DWORD sz = wwo->dwPartialOffset;
1030 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1031 /* compute the max size playable from lpQueuePtr */
1032 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1033 sz += ptr->dwBufferLength;
1035 /* because the reset lpPlayPtr will be lpQueuePtr */
1036 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1037 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1038 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1039 wwo->lpPlayPtr = wwo->lpQueuePtr;
1041 wwo->state = WINE_WS_PAUSED;
1045 /**************************************************************************
1046 * wodPlayer_ProcessMessages [internal]
1048 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1050 LPWAVEHDR lpWaveHdr;
1051 enum win_wm_message msg;
1052 DWORD param;
1053 HANDLE ev;
1055 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1056 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
1057 switch (msg) {
1058 case WINE_WM_PAUSING:
1059 wodPlayer_Reset(wwo, FALSE);
1060 SetEvent(ev);
1061 break;
1062 case WINE_WM_RESTARTING:
1063 if (wwo->state == WINE_WS_PAUSED)
1065 wwo->state = WINE_WS_PLAYING;
1067 SetEvent(ev);
1068 break;
1069 case WINE_WM_HEADER:
1070 lpWaveHdr = (LPWAVEHDR)param;
1072 /* insert buffer at the end of queue */
1074 LPWAVEHDR* wh;
1075 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1076 *wh = lpWaveHdr;
1078 if (!wwo->lpPlayPtr)
1079 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1080 if (wwo->state == WINE_WS_STOPPED)
1081 wwo->state = WINE_WS_PLAYING;
1082 break;
1083 case WINE_WM_RESETTING:
1084 wodPlayer_Reset(wwo, TRUE);
1085 SetEvent(ev);
1086 break;
1087 case WINE_WM_UPDATE:
1088 wodUpdatePlayedTotal(wwo, NULL);
1089 SetEvent(ev);
1090 break;
1091 case WINE_WM_BREAKLOOP:
1092 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1093 /* ensure exit at end of current loop */
1094 wwo->dwLoops = 1;
1096 SetEvent(ev);
1097 break;
1098 case WINE_WM_CLOSING:
1099 /* sanity check: this should not happen since the device must have been reset before */
1100 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1101 wwo->hThread = 0;
1102 wwo->state = WINE_WS_CLOSED;
1103 SetEvent(ev);
1104 ExitThread(0);
1105 /* shouldn't go here */
1106 default:
1107 FIXME("unknown message %d\n", msg);
1108 break;
1113 /**************************************************************************
1114 * wodPlayer_FeedDSP [internal]
1115 * Feed as much sound data as we can into the DSP and return the number of
1116 * milliseconds before it will be necessary to feed the DSP again.
1118 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1120 audio_buf_info dspspace;
1121 DWORD availInQ;
1123 wodUpdatePlayedTotal(wwo, &dspspace);
1124 availInQ = dspspace.bytes;
1125 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1126 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1128 /* input queue empty and output buffer with less than one fragment to play */
1129 if (!wwo->lpPlayPtr && wwo->dwBufferSize < availInQ + wwo->dwFragmentSize) {
1130 TRACE("Run out of wavehdr:s...\n");
1131 return INFINITE;
1134 /* no more room... no need to try to feed */
1135 if (dspspace.fragments != 0) {
1136 /* Feed from partial wavehdr */
1137 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1138 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1141 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1142 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1143 do {
1144 TRACE("Setting time to elapse for %p to %lu\n",
1145 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1146 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1147 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1148 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1151 if (wwo->bNeedPost) {
1152 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1153 * if it didn't get one, we give it the other */
1154 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1155 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1156 wwo->bNeedPost = FALSE;
1160 return wodPlayer_DSPWait(wwo);
1164 /**************************************************************************
1165 * wodPlayer [internal]
1167 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1169 WORD uDevID = (DWORD)pmt;
1170 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1171 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1172 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1173 DWORD dwSleepTime;
1175 wwo->state = WINE_WS_STOPPED;
1176 SetEvent(wwo->hStartUpEvent);
1178 for (;;) {
1179 /** Wait for the shortest time before an action is required. If there
1180 * are no pending actions, wait forever for a command.
1182 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1183 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1184 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1185 wodPlayer_ProcessMessages(wwo);
1186 if (wwo->state == WINE_WS_PLAYING) {
1187 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1188 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1189 if (dwNextFeedTime == INFINITE) {
1190 /* FeedDSP ran out of data, but before flushing, */
1191 /* check that a notification didn't give us more */
1192 wodPlayer_ProcessMessages(wwo);
1193 if (!wwo->lpPlayPtr) {
1194 TRACE("flushing\n");
1195 ioctl(wwo->ossdev->fd, SNDCTL_DSP_SYNC, 0);
1196 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1198 else {
1199 TRACE("recovering\n");
1200 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1203 } else {
1204 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1209 /**************************************************************************
1210 * wodGetDevCaps [internal]
1212 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1214 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1216 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1218 if (wDevID >= MAX_WAVEDRV) {
1219 TRACE("MAX_WAVDRV reached !\n");
1220 return MMSYSERR_BADDEVICEID;
1223 memcpy(lpCaps, &OSS_Devices[wDevID].out_caps, min(dwSize, sizeof(*lpCaps)));
1224 return MMSYSERR_NOERROR;
1227 /**************************************************************************
1228 * wodOpen [internal]
1230 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1232 int audio_fragment;
1233 WINE_WAVEOUT* wwo;
1234 audio_buf_info info;
1235 DWORD ret;
1237 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1238 if (lpDesc == NULL) {
1239 WARN("Invalid Parameter !\n");
1240 return MMSYSERR_INVALPARAM;
1242 if (wDevID >= MAX_WAVEDRV) {
1243 TRACE("MAX_WAVOUTDRV reached !\n");
1244 return MMSYSERR_BADDEVICEID;
1247 /* only PCM format is supported so far... */
1248 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1249 lpDesc->lpFormat->nChannels == 0 ||
1250 lpDesc->lpFormat->nSamplesPerSec == 0) {
1251 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1252 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1253 lpDesc->lpFormat->nSamplesPerSec);
1254 return WAVERR_BADFORMAT;
1257 if (dwFlags & WAVE_FORMAT_QUERY) {
1258 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1259 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1260 lpDesc->lpFormat->nSamplesPerSec);
1261 return MMSYSERR_NOERROR;
1264 wwo = &WOutDev[wDevID];
1266 if ((dwFlags & WAVE_DIRECTSOUND) && !(OSS_Devices[wDevID].out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
1267 /* not supported, ignore it */
1268 dwFlags &= ~WAVE_DIRECTSOUND;
1270 if (dwFlags & WAVE_DIRECTSOUND) {
1271 if (OSS_Devices[wDevID].out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1272 /* we have realtime DirectSound, fragments just waste our time,
1273 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1274 audio_fragment = 0x0020000B;
1275 else
1276 /* to approximate realtime, we must use small fragments,
1277 * let's try to fragment the above 64KB (256 * 2^8) */
1278 audio_fragment = 0x01000008;
1279 } else {
1280 /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
1281 * thus leading to 46ms per fragment, and a turnaround time of 185ms
1283 /* 16 fragments max, 2^10=1024 bytes per fragment */
1284 audio_fragment = 0x000F000A;
1286 if (wwo->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
1287 /* we want to be able to mmap() the device, which means it must be opened readable,
1288 * otherwise mmap() will fail (at least under Linux) */
1289 ret = OSS_OpenDevice(wwo->ossdev,
1290 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
1291 &audio_fragment, lpDesc->lpFormat->nSamplesPerSec,
1292 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
1293 (lpDesc->lpFormat->wBitsPerSample == 16)
1294 ? AFMT_S16_LE : AFMT_U8);
1295 if (ret != 0) return ret;
1296 wwo->state = WINE_WS_STOPPED;
1298 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1300 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1301 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1303 if (wwo->format.wBitsPerSample == 0) {
1304 WARN("Resetting zeroed wBitsPerSample\n");
1305 wwo->format.wBitsPerSample = 8 *
1306 (wwo->format.wf.nAvgBytesPerSec /
1307 wwo->format.wf.nSamplesPerSec) /
1308 wwo->format.wf.nChannels;
1310 /* Read output space info for future reference */
1311 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
1312 ERR("ioctl can't 'SNDCTL_DSP_GETOSPACE' !\n");
1313 OSS_CloseDevice(wwo->ossdev);
1314 wwo->state = WINE_WS_CLOSED;
1315 return MMSYSERR_NOTENABLED;
1318 /* Check that fragsize is correct per our settings above */
1319 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
1320 /* we've tried to set 1K fragments or less, but it didn't work */
1321 ERR("fragment size set failed, size is now %d\n", info.fragsize);
1322 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
1323 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
1326 /* Remember fragsize and total buffer size for future use */
1327 wwo->dwFragmentSize = info.fragsize;
1328 wwo->dwBufferSize = info.fragstotal * info.fragsize;
1329 wwo->dwPlayedTotal = 0;
1330 wwo->dwWrittenTotal = 0;
1331 wwo->bNeedPost = TRUE;
1333 OSS_InitRingMessage(&wwo->msgRing);
1335 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1336 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1337 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1338 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1339 CloseHandle(wwo->hStartUpEvent);
1340 } else {
1341 wwo->hThread = INVALID_HANDLE_VALUE;
1342 wwo->dwThreadID = 0;
1344 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1346 TRACE("fd=%d fragmentSize=%ld\n",
1347 wwo->ossdev->fd, wwo->dwFragmentSize);
1348 if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1349 ERR("Fragment doesn't contain an integral number of data blocks\n");
1351 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1352 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1353 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1354 wwo->format.wf.nBlockAlign);
1356 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1359 /**************************************************************************
1360 * wodClose [internal]
1362 static DWORD wodClose(WORD wDevID)
1364 DWORD ret = MMSYSERR_NOERROR;
1365 WINE_WAVEOUT* wwo;
1367 TRACE("(%u);\n", wDevID);
1369 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1370 WARN("bad device ID !\n");
1371 return MMSYSERR_BADDEVICEID;
1374 wwo = &WOutDev[wDevID];
1375 if (wwo->lpQueuePtr) {
1376 WARN("buffers still playing !\n");
1377 ret = WAVERR_STILLPLAYING;
1378 } else {
1379 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1380 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1382 if (wwo->mapping) {
1383 munmap(wwo->mapping, wwo->maplen);
1384 wwo->mapping = NULL;
1387 OSS_DestroyRingMessage(&wwo->msgRing);
1389 OSS_CloseDevice(wwo->ossdev);
1390 wwo->state = WINE_WS_CLOSED;
1391 wwo->dwFragmentSize = 0;
1392 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1394 return ret;
1397 /**************************************************************************
1398 * wodWrite [internal]
1401 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1403 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1405 /* first, do the sanity checks... */
1406 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1407 WARN("bad dev ID !\n");
1408 return MMSYSERR_BADDEVICEID;
1411 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1412 return WAVERR_UNPREPARED;
1414 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1415 return WAVERR_STILLPLAYING;
1417 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1418 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1419 lpWaveHdr->lpNext = 0;
1421 if ((lpWaveHdr->dwBufferLength & ~(WOutDev[wDevID].format.wf.nBlockAlign - 1)) != 0)
1423 WARN("WaveHdr length isn't a multiple of the PCM block size\n");
1424 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].format.wf.nBlockAlign - 1);
1427 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1429 return MMSYSERR_NOERROR;
1432 /**************************************************************************
1433 * wodPrepare [internal]
1435 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1437 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1439 if (wDevID >= MAX_WAVEDRV) {
1440 WARN("bad device ID !\n");
1441 return MMSYSERR_BADDEVICEID;
1444 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1445 return WAVERR_STILLPLAYING;
1447 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1448 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1449 return MMSYSERR_NOERROR;
1452 /**************************************************************************
1453 * wodUnprepare [internal]
1455 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1457 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1459 if (wDevID >= MAX_WAVEDRV) {
1460 WARN("bad device ID !\n");
1461 return MMSYSERR_BADDEVICEID;
1464 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1465 return WAVERR_STILLPLAYING;
1467 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1468 lpWaveHdr->dwFlags |= WHDR_DONE;
1470 return MMSYSERR_NOERROR;
1473 /**************************************************************************
1474 * wodPause [internal]
1476 static DWORD wodPause(WORD wDevID)
1478 TRACE("(%u);!\n", wDevID);
1480 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1481 WARN("bad device ID !\n");
1482 return MMSYSERR_BADDEVICEID;
1485 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1487 return MMSYSERR_NOERROR;
1490 /**************************************************************************
1491 * wodRestart [internal]
1493 static DWORD wodRestart(WORD wDevID)
1495 TRACE("(%u);\n", wDevID);
1497 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1498 WARN("bad device ID !\n");
1499 return MMSYSERR_BADDEVICEID;
1502 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1504 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1505 /* FIXME: Myst crashes with this ... hmm -MM
1506 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1509 return MMSYSERR_NOERROR;
1512 /**************************************************************************
1513 * wodReset [internal]
1515 static DWORD wodReset(WORD wDevID)
1517 TRACE("(%u);\n", wDevID);
1519 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1520 WARN("bad device ID !\n");
1521 return MMSYSERR_BADDEVICEID;
1524 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1526 return MMSYSERR_NOERROR;
1529 /**************************************************************************
1530 * wodGetPosition [internal]
1532 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1534 int time;
1535 DWORD val;
1536 WINE_WAVEOUT* wwo;
1538 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1540 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1541 WARN("bad device ID !\n");
1542 return MMSYSERR_BADDEVICEID;
1545 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1547 wwo = &WOutDev[wDevID];
1548 #ifdef EXACT_WODPOSITION
1549 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1550 #endif
1551 val = wwo->dwPlayedTotal;
1553 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1554 lpTime->wType, wwo->format.wBitsPerSample,
1555 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1556 wwo->format.wf.nAvgBytesPerSec);
1557 TRACE("dwPlayedTotal=%lu\n", val);
1559 switch (lpTime->wType) {
1560 case TIME_BYTES:
1561 lpTime->u.cb = val;
1562 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1563 break;
1564 case TIME_SAMPLES:
1565 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1566 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1567 break;
1568 case TIME_SMPTE:
1569 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1570 lpTime->u.smpte.hour = time / 108000;
1571 time -= lpTime->u.smpte.hour * 108000;
1572 lpTime->u.smpte.min = time / 1800;
1573 time -= lpTime->u.smpte.min * 1800;
1574 lpTime->u.smpte.sec = time / 30;
1575 time -= lpTime->u.smpte.sec * 30;
1576 lpTime->u.smpte.frame = time;
1577 lpTime->u.smpte.fps = 30;
1578 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1579 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1580 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1581 break;
1582 default:
1583 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1584 lpTime->wType = TIME_MS;
1585 case TIME_MS:
1586 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1587 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1588 break;
1590 return MMSYSERR_NOERROR;
1593 /**************************************************************************
1594 * wodBreakLoop [internal]
1596 static DWORD wodBreakLoop(WORD wDevID)
1598 TRACE("(%u);\n", wDevID);
1600 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1601 WARN("bad device ID !\n");
1602 return MMSYSERR_BADDEVICEID;
1604 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1605 return MMSYSERR_NOERROR;
1608 /**************************************************************************
1609 * wodGetVolume [internal]
1611 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1613 int mixer;
1614 int volume;
1615 DWORD left, right;
1617 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1619 if (lpdwVol == NULL)
1620 return MMSYSERR_NOTENABLED;
1621 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_INVALPARAM;
1623 if ((mixer = open(OSS_Devices[wDevID].mixer_name, O_RDONLY|O_NDELAY)) < 0) {
1624 WARN("mixer device not available !\n");
1625 return MMSYSERR_NOTENABLED;
1627 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
1628 WARN("unable to read mixer !\n");
1629 return MMSYSERR_NOTENABLED;
1631 close(mixer);
1632 left = LOBYTE(volume);
1633 right = HIBYTE(volume);
1634 TRACE("left=%ld right=%ld !\n", left, right);
1635 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1636 return MMSYSERR_NOERROR;
1639 /**************************************************************************
1640 * wodSetVolume [internal]
1642 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1644 int mixer;
1645 int volume;
1646 DWORD left, right;
1648 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1650 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1651 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1652 volume = left + (right << 8);
1654 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_INVALPARAM;
1656 if ((mixer = open(OSS_Devices[wDevID].mixer_name, O_WRONLY|O_NDELAY)) < 0) {
1657 WARN("mixer device not available !\n");
1658 return MMSYSERR_NOTENABLED;
1660 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
1661 WARN("unable to set mixer !\n");
1662 return MMSYSERR_NOTENABLED;
1663 } else {
1664 TRACE("volume=%04x\n", (unsigned)volume);
1666 close(mixer);
1667 return MMSYSERR_NOERROR;
1670 /**************************************************************************
1671 * wodMessage (WINEOSS.7)
1673 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1674 DWORD dwParam1, DWORD dwParam2)
1676 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1677 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1679 switch (wMsg) {
1680 case DRVM_INIT:
1681 case DRVM_EXIT:
1682 case DRVM_ENABLE:
1683 case DRVM_DISABLE:
1684 /* FIXME: Pretend this is supported */
1685 return 0;
1686 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1687 case WODM_CLOSE: return wodClose (wDevID);
1688 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1689 case WODM_PAUSE: return wodPause (wDevID);
1690 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1691 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1692 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1693 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1694 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1695 case WODM_GETNUMDEVS: return numOutDev;
1696 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1697 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1698 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1699 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1700 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1701 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1702 case WODM_RESTART: return wodRestart (wDevID);
1703 case WODM_RESET: return wodReset (wDevID);
1705 case DRV_QUERYDSOUNDIFACE: return wodDsCreate(wDevID, (PIDSDRIVER*)dwParam1);
1706 default:
1707 FIXME("unknown message %d!\n", wMsg);
1709 return MMSYSERR_NOTSUPPORTED;
1712 /*======================================================================*
1713 * Low level DSOUND implementation *
1714 *======================================================================*/
1716 typedef struct IDsDriverImpl IDsDriverImpl;
1717 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1719 struct IDsDriverImpl
1721 /* IUnknown fields */
1722 ICOM_VFIELD(IDsDriver);
1723 DWORD ref;
1724 /* IDsDriverImpl fields */
1725 UINT wDevID;
1726 IDsDriverBufferImpl*primary;
1729 struct IDsDriverBufferImpl
1731 /* IUnknown fields */
1732 ICOM_VFIELD(IDsDriverBuffer);
1733 DWORD ref;
1734 /* IDsDriverBufferImpl fields */
1735 IDsDriverImpl* drv;
1736 DWORD buflen;
1739 static HRESULT DSDB_MapPrimary(IDsDriverBufferImpl *dsdb)
1741 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1742 if (!wwo->mapping) {
1743 wwo->mapping = mmap(NULL, wwo->maplen, PROT_WRITE, MAP_SHARED,
1744 wwo->ossdev->fd, 0);
1745 if (wwo->mapping == (LPBYTE)-1) {
1746 ERR("(%p): Could not map sound device for direct access (%s)\n", dsdb, strerror(errno));
1747 return DSERR_GENERIC;
1749 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb, wwo->mapping, wwo->maplen);
1751 /* for some reason, es1371 and sblive! sometimes have junk in here.
1752 * clear it, or we get junk noise */
1753 /* some libc implementations are buggy: their memset reads from the buffer...
1754 * to work around it, we have to zero the block by hand. We don't do the expected:
1755 * memset(wwo->mapping,0, wwo->maplen);
1758 char* p1 = wwo->mapping;
1759 unsigned len = wwo->maplen;
1761 if (len >= 16) /* so we can have at least a 4 long area to store... */
1763 /* the mmap:ed value is (at least) dword aligned
1764 * so, start filling the complete unsigned long:s
1766 int b = len >> 2;
1767 unsigned long* p4 = (unsigned long*)p1;
1769 while (b--) *p4++ = 0;
1770 /* prepare for filling the rest */
1771 len &= 3;
1772 p1 = (unsigned char*)p4;
1774 /* in all cases, fill the remaining bytes */
1775 while (len-- != 0) *p1++ = 0;
1778 return DS_OK;
1781 static HRESULT DSDB_UnmapPrimary(IDsDriverBufferImpl *dsdb)
1783 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1784 if (wwo->mapping) {
1785 if (munmap(wwo->mapping, wwo->maplen) < 0) {
1786 ERR("(%p): Could not unmap sound device (errno=%d)\n", dsdb, errno);
1787 return DSERR_GENERIC;
1789 wwo->mapping = NULL;
1790 TRACE("(%p): sound device unmapped\n", dsdb);
1792 return DS_OK;
1795 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
1797 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1798 FIXME("(): stub!\n");
1799 return DSERR_UNSUPPORTED;
1802 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
1804 ICOM_THIS(IDsDriverBufferImpl,iface);
1805 This->ref++;
1806 return This->ref;
1809 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
1811 ICOM_THIS(IDsDriverBufferImpl,iface);
1812 if (--This->ref)
1813 return This->ref;
1814 if (This == This->drv->primary)
1815 This->drv->primary = NULL;
1816 DSDB_UnmapPrimary(This);
1817 HeapFree(GetProcessHeap(),0,This);
1818 return 0;
1821 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
1822 LPVOID*ppvAudio1,LPDWORD pdwLen1,
1823 LPVOID*ppvAudio2,LPDWORD pdwLen2,
1824 DWORD dwWritePosition,DWORD dwWriteLen,
1825 DWORD dwFlags)
1827 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1828 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
1829 * and that we don't support secondary buffers, this method will never be called */
1830 TRACE("(%p): stub\n",iface);
1831 return DSERR_UNSUPPORTED;
1834 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
1835 LPVOID pvAudio1,DWORD dwLen1,
1836 LPVOID pvAudio2,DWORD dwLen2)
1838 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1839 TRACE("(%p): stub\n",iface);
1840 return DSERR_UNSUPPORTED;
1843 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
1844 LPWAVEFORMATEX pwfx)
1846 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1848 TRACE("(%p,%p)\n",iface,pwfx);
1849 /* On our request (GetDriverDesc flags), DirectSound has by now used
1850 * waveOutClose/waveOutOpen to set the format...
1851 * unfortunately, this means our mmap() is now gone...
1852 * so we need to somehow signal to our DirectSound implementation
1853 * that it should completely recreate this HW buffer...
1854 * this unexpected error code should do the trick... */
1855 return DSERR_BUFFERLOST;
1858 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
1860 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1861 TRACE("(%p,%ld): stub\n",iface,dwFreq);
1862 return DSERR_UNSUPPORTED;
1865 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
1867 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1868 FIXME("(%p,%p): stub!\n",iface,pVolPan);
1869 return DSERR_UNSUPPORTED;
1872 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
1874 /* ICOM_THIS(IDsDriverImpl,iface); */
1875 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
1876 return DSERR_UNSUPPORTED;
1879 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
1880 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
1882 ICOM_THIS(IDsDriverBufferImpl,iface);
1883 count_info info;
1884 DWORD ptr;
1886 TRACE("(%p)\n",iface);
1887 if (WOutDev[This->drv->wDevID].state == WINE_WS_CLOSED) {
1888 ERR("device not open, but accessing?\n");
1889 return DSERR_UNINITIALIZED;
1891 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
1892 ERR("ioctl failed (%d)\n", errno);
1893 return DSERR_GENERIC;
1895 ptr = info.ptr & ~3; /* align the pointer, just in case */
1896 if (lpdwPlay) *lpdwPlay = ptr;
1897 if (lpdwWrite) {
1898 /* add some safety margin (not strictly necessary, but...) */
1899 if (OSS_Devices[This->drv->wDevID].out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1900 *lpdwWrite = ptr + 32;
1901 else
1902 *lpdwWrite = ptr + WOutDev[This->drv->wDevID].dwFragmentSize;
1903 while (*lpdwWrite > This->buflen)
1904 *lpdwWrite -= This->buflen;
1906 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay?*lpdwPlay:0, lpdwWrite?*lpdwWrite:0);
1907 return DS_OK;
1910 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
1912 ICOM_THIS(IDsDriverBufferImpl,iface);
1913 int enable = PCM_ENABLE_OUTPUT;
1914 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
1915 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1916 ERR("ioctl failed (%d)\n", errno);
1917 return DSERR_GENERIC;
1919 return DS_OK;
1922 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
1924 ICOM_THIS(IDsDriverBufferImpl,iface);
1925 int enable = 0;
1926 TRACE("(%p)\n",iface);
1927 /* no more playing */
1928 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1929 ERR("ioctl failed (%d)\n", errno);
1930 return DSERR_GENERIC;
1932 #if 0
1933 /* the play position must be reset to the beginning of the buffer */
1934 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_RESET, 0) < 0) {
1935 ERR("ioctl failed (%d)\n", errno);
1936 return DSERR_GENERIC;
1938 #endif
1939 /* Most OSS drivers just can't stop the playback without closing the device...
1940 * so we need to somehow signal to our DirectSound implementation
1941 * that it should completely recreate this HW buffer...
1942 * this unexpected error code should do the trick... */
1943 return DSERR_BUFFERLOST;
1946 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
1948 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1949 IDsDriverBufferImpl_QueryInterface,
1950 IDsDriverBufferImpl_AddRef,
1951 IDsDriverBufferImpl_Release,
1952 IDsDriverBufferImpl_Lock,
1953 IDsDriverBufferImpl_Unlock,
1954 IDsDriverBufferImpl_SetFormat,
1955 IDsDriverBufferImpl_SetFrequency,
1956 IDsDriverBufferImpl_SetVolumePan,
1957 IDsDriverBufferImpl_SetPosition,
1958 IDsDriverBufferImpl_GetPosition,
1959 IDsDriverBufferImpl_Play,
1960 IDsDriverBufferImpl_Stop
1963 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
1965 /* ICOM_THIS(IDsDriverImpl,iface); */
1966 FIXME("(%p): stub!\n",iface);
1967 return DSERR_UNSUPPORTED;
1970 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
1972 ICOM_THIS(IDsDriverImpl,iface);
1973 This->ref++;
1974 return This->ref;
1977 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
1979 ICOM_THIS(IDsDriverImpl,iface);
1980 if (--This->ref)
1981 return This->ref;
1982 HeapFree(GetProcessHeap(),0,This);
1983 return 0;
1986 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
1988 ICOM_THIS(IDsDriverImpl,iface);
1989 TRACE("(%p,%p)\n",iface,pDesc);
1990 pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
1991 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
1992 strcpy(pDesc->szDesc,"WineOSS DirectSound Driver");
1993 strcpy(pDesc->szDrvName,"wineoss.drv");
1994 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
1995 pDesc->wVxdId = 0;
1996 pDesc->wReserved = 0;
1997 pDesc->ulDeviceNum = This->wDevID;
1998 pDesc->dwHeapType = DSDHEAP_NOHEAP;
1999 pDesc->pvDirectDrawHeap = NULL;
2000 pDesc->dwMemStartAddress = 0;
2001 pDesc->dwMemEndAddress = 0;
2002 pDesc->dwMemAllocExtra = 0;
2003 pDesc->pvReserved1 = NULL;
2004 pDesc->pvReserved2 = NULL;
2005 return DS_OK;
2008 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
2010 ICOM_THIS(IDsDriverImpl,iface);
2011 int enable = 0;
2013 TRACE("(%p)\n",iface);
2014 /* make sure the card doesn't start playing before we want it to */
2015 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2016 ERR("ioctl failed (%d)\n", errno);
2017 return DSERR_GENERIC;
2019 return DS_OK;
2022 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
2024 ICOM_THIS(IDsDriverImpl,iface);
2025 TRACE("(%p)\n",iface);
2026 if (This->primary) {
2027 ERR("problem with DirectSound: primary not released\n");
2028 return DSERR_GENERIC;
2030 return DS_OK;
2033 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
2035 /* ICOM_THIS(IDsDriverImpl,iface); */
2036 TRACE("(%p,%p)\n",iface,pCaps);
2037 memset(pCaps, 0, sizeof(*pCaps));
2038 /* FIXME: need to check actual capabilities */
2039 pCaps->dwFlags = DSCAPS_PRIMARYMONO | DSCAPS_PRIMARYSTEREO |
2040 DSCAPS_PRIMARY8BIT | DSCAPS_PRIMARY16BIT;
2041 pCaps->dwPrimaryBuffers = 1;
2042 /* the other fields only apply to secondary buffers, which we don't support
2043 * (unless we want to mess with wavetable synthesizers and MIDI) */
2044 return DS_OK;
2047 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
2048 LPWAVEFORMATEX pwfx,
2049 DWORD dwFlags, DWORD dwCardAddress,
2050 LPDWORD pdwcbBufferSize,
2051 LPBYTE *ppbBuffer,
2052 LPVOID *ppvObj)
2054 ICOM_THIS(IDsDriverImpl,iface);
2055 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
2056 HRESULT err;
2057 audio_buf_info info;
2058 int enable = 0;
2060 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
2061 /* we only support primary buffers */
2062 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
2063 return DSERR_UNSUPPORTED;
2064 if (This->primary)
2065 return DSERR_ALLOCATED;
2066 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
2067 return DSERR_CONTROLUNAVAIL;
2069 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
2070 if (*ippdsdb == NULL)
2071 return DSERR_OUTOFMEMORY;
2072 ICOM_VTBL(*ippdsdb) = &dsdbvt;
2073 (*ippdsdb)->ref = 1;
2074 (*ippdsdb)->drv = This;
2076 /* check how big the DMA buffer is now */
2077 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2078 ERR("ioctl failed (%d)\n", errno);
2079 HeapFree(GetProcessHeap(),0,*ippdsdb);
2080 *ippdsdb = NULL;
2081 return DSERR_GENERIC;
2083 WOutDev[This->wDevID].maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
2085 /* map the DMA buffer */
2086 err = DSDB_MapPrimary(*ippdsdb);
2087 if (err != DS_OK) {
2088 HeapFree(GetProcessHeap(),0,*ippdsdb);
2089 *ippdsdb = NULL;
2090 return err;
2093 /* primary buffer is ready to go */
2094 *pdwcbBufferSize = WOutDev[This->wDevID].maplen;
2095 *ppbBuffer = WOutDev[This->wDevID].mapping;
2097 /* some drivers need some extra nudging after mapping */
2098 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2099 ERR("ioctl failed (%d)\n", errno);
2100 return DSERR_GENERIC;
2103 This->primary = *ippdsdb;
2105 return DS_OK;
2108 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
2109 PIDSDRIVERBUFFER pBuffer,
2110 LPVOID *ppvObj)
2112 /* ICOM_THIS(IDsDriverImpl,iface); */
2113 TRACE("(%p,%p): stub\n",iface,pBuffer);
2114 return DSERR_INVALIDCALL;
2117 static ICOM_VTABLE(IDsDriver) dsdvt =
2119 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2120 IDsDriverImpl_QueryInterface,
2121 IDsDriverImpl_AddRef,
2122 IDsDriverImpl_Release,
2123 IDsDriverImpl_GetDriverDesc,
2124 IDsDriverImpl_Open,
2125 IDsDriverImpl_Close,
2126 IDsDriverImpl_GetCaps,
2127 IDsDriverImpl_CreateSoundBuffer,
2128 IDsDriverImpl_DuplicateSoundBuffer
2131 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2133 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
2135 /* the HAL isn't much better than the HEL if we can't do mmap() */
2136 if (!(OSS_Devices[wDevID].out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
2137 ERR("DirectSound flag not set\n");
2138 MESSAGE("This sound card's driver does not support direct access\n");
2139 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2140 return MMSYSERR_NOTSUPPORTED;
2143 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
2144 if (!*idrv)
2145 return MMSYSERR_NOMEM;
2146 ICOM_VTBL(*idrv) = &dsdvt;
2147 (*idrv)->ref = 1;
2149 (*idrv)->wDevID = wDevID;
2150 (*idrv)->primary = NULL;
2151 return MMSYSERR_NOERROR;
2154 /*======================================================================*
2155 * Low level WAVE IN implementation *
2156 *======================================================================*/
2158 /**************************************************************************
2159 * widNotifyClient [internal]
2161 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2163 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
2165 switch (wMsg) {
2166 case WIM_OPEN:
2167 case WIM_CLOSE:
2168 case WIM_DATA:
2169 if (wwi->wFlags != DCB_NULL &&
2170 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2171 (HDRVR)wwi->waveDesc.hWave, wMsg,
2172 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2173 WARN("can't notify client !\n");
2174 return MMSYSERR_ERROR;
2176 break;
2177 default:
2178 FIXME("Unknown callback message %u\n", wMsg);
2179 return MMSYSERR_INVALPARAM;
2181 return MMSYSERR_NOERROR;
2184 /**************************************************************************
2185 * widGetDevCaps [internal]
2187 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
2189 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
2191 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2193 if (wDevID >= MAX_WAVEDRV) {
2194 TRACE("MAX_WAVDRV reached !\n");
2195 return MMSYSERR_BADDEVICEID;
2198 memcpy(lpCaps, &OSS_Devices[wDevID].in_caps, min(dwSize, sizeof(*lpCaps)));
2199 return MMSYSERR_NOERROR;
2202 /**************************************************************************
2203 * widRecorder [internal]
2205 static DWORD CALLBACK widRecorder(LPVOID pmt)
2207 WORD uDevID = (DWORD)pmt;
2208 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2209 WAVEHDR* lpWaveHdr;
2210 DWORD dwSleepTime;
2211 DWORD bytesRead;
2212 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2213 LPVOID pOffset = buffer;
2214 audio_buf_info info;
2215 int xs;
2216 enum win_wm_message msg;
2217 DWORD param;
2218 HANDLE ev;
2220 wwi->state = WINE_WS_STOPPED;
2221 wwi->dwTotalRecorded = 0;
2223 SetEvent(wwi->hStartUpEvent);
2225 /* the soundblaster live needs a micro wake to get its recording started
2226 * (or GETISPACE will have 0 frags all the time)
2228 read(wwi->ossdev->fd, &xs, 4);
2230 /* make sleep time to be # of ms to output a fragment */
2231 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->format.wf.nAvgBytesPerSec;
2232 TRACE("sleeptime=%ld ms\n", dwSleepTime);
2234 for (;;) {
2235 /* wait for dwSleepTime or an event in thread's queue */
2236 /* FIXME: could improve wait time depending on queue state,
2237 * ie, number of queued fragments
2240 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2242 lpWaveHdr = wwi->lpQueuePtr;
2244 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info);
2245 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2247 /* read all the fragments accumulated so far */
2248 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2250 info.fragments --;
2252 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2254 /* directly read fragment in wavehdr */
2255 bytesRead = read(wwi->ossdev->fd,
2256 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2257 wwi->dwFragmentSize);
2259 TRACE("bytesRead=%ld (direct)\n", bytesRead);
2260 if (bytesRead != (DWORD) -1)
2262 /* update number of bytes recorded in current buffer and by this device */
2263 lpWaveHdr->dwBytesRecorded += bytesRead;
2264 wwi->dwTotalRecorded += bytesRead;
2266 /* buffer is full. notify client */
2267 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2269 /* must copy the value of next waveHdr, because we have no idea of what
2270 * will be done with the content of lpWaveHdr in callback
2272 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2274 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2275 lpWaveHdr->dwFlags |= WHDR_DONE;
2277 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2278 lpWaveHdr = wwi->lpQueuePtr = lpNext;
2282 else
2284 /* read the fragment in a local buffer */
2285 bytesRead = read(wwi->ossdev->fd, buffer, wwi->dwFragmentSize);
2286 pOffset = buffer;
2288 TRACE("bytesRead=%ld (local)\n", bytesRead);
2290 /* copy data in client buffers */
2291 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2293 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2295 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2296 pOffset,
2297 dwToCopy);
2299 /* update number of bytes recorded in current buffer and by this device */
2300 lpWaveHdr->dwBytesRecorded += dwToCopy;
2301 wwi->dwTotalRecorded += dwToCopy;
2302 bytesRead -= dwToCopy;
2303 pOffset += dwToCopy;
2305 /* client buffer is full. notify client */
2306 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2308 /* must copy the value of next waveHdr, because we have no idea of what
2309 * will be done with the content of lpWaveHdr in callback
2311 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2312 TRACE("lpNext=%p\n", lpNext);
2314 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2315 lpWaveHdr->dwFlags |= WHDR_DONE;
2317 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2319 wwi->lpQueuePtr = lpWaveHdr = lpNext;
2320 if (!lpNext && bytesRead) {
2321 /* no more buffer to copy data to, but we did read more.
2322 * what hasn't been copied will be dropped
2324 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
2325 wwi->lpQueuePtr = NULL;
2326 break;
2334 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2336 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2339 TRACE("msg=0x%x param=0x%lx\n", msg, param);
2340 switch (msg) {
2341 case WINE_WM_PAUSING:
2342 wwi->state = WINE_WS_PAUSED;
2343 /*FIXME("Device should stop recording\n");*/
2344 SetEvent(ev);
2345 break;
2346 case WINE_WM_RESTARTING:
2348 int enable = PCM_ENABLE_INPUT;
2349 wwi->state = WINE_WS_PLAYING;
2351 if (wwi->bTriggerSupport)
2353 /* start the recording */
2354 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2356 ERR("ioctl(SNDCTL_DSP_SETTRIGGER) failed (%d)\n", errno);
2359 else
2361 unsigned char data[4];
2362 /* read 4 bytes to start the recording */
2363 read(wwi->ossdev->fd, data, 4);
2366 SetEvent(ev);
2367 break;
2369 case WINE_WM_HEADER:
2370 lpWaveHdr = (LPWAVEHDR)param;
2371 lpWaveHdr->lpNext = 0;
2373 /* insert buffer at the end of queue */
2375 LPWAVEHDR* wh;
2376 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2377 *wh = lpWaveHdr;
2379 break;
2380 case WINE_WM_RESETTING:
2381 wwi->state = WINE_WS_STOPPED;
2382 /* return all buffers to the app */
2383 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2384 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2385 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2386 lpWaveHdr->dwFlags |= WHDR_DONE;
2388 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2390 wwi->lpQueuePtr = NULL;
2391 SetEvent(ev);
2392 break;
2393 case WINE_WM_CLOSING:
2394 wwi->hThread = 0;
2395 wwi->state = WINE_WS_CLOSED;
2396 SetEvent(ev);
2397 HeapFree(GetProcessHeap(), 0, buffer);
2398 ExitThread(0);
2399 /* shouldn't go here */
2400 default:
2401 FIXME("unknown message %d\n", msg);
2402 break;
2406 ExitThread(0);
2407 /* just for not generating compilation warnings... should never be executed */
2408 return 0;
2412 /**************************************************************************
2413 * widOpen [internal]
2415 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2417 WINE_WAVEIN* wwi;
2418 int fragment_size;
2419 int audio_fragment;
2420 DWORD ret;
2422 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
2423 if (lpDesc == NULL) {
2424 WARN("Invalid Parameter !\n");
2425 return MMSYSERR_INVALPARAM;
2427 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_BADDEVICEID;
2429 /* only PCM format is supported so far... */
2430 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
2431 lpDesc->lpFormat->nChannels == 0 ||
2432 lpDesc->lpFormat->nSamplesPerSec == 0) {
2433 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2434 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2435 lpDesc->lpFormat->nSamplesPerSec);
2436 return WAVERR_BADFORMAT;
2439 if (dwFlags & WAVE_FORMAT_QUERY) {
2440 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2441 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2442 lpDesc->lpFormat->nSamplesPerSec);
2443 return MMSYSERR_NOERROR;
2446 wwi = &WInDev[wDevID];
2448 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2449 /* This is actually hand tuned to work so that my SB Live:
2450 * - does not skip
2451 * - does not buffer too much
2452 * when sending with the Shoutcast winamp plugin
2454 /* 15 fragments max, 2^10 = 1024 bytes per fragment */
2455 audio_fragment = 0x000F000A;
2456 ret = OSS_OpenDevice(wwi->ossdev, O_RDONLY, &audio_fragment,
2457 lpDesc->lpFormat->nSamplesPerSec,
2458 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
2459 (lpDesc->lpFormat->wBitsPerSample == 16)
2460 ? AFMT_S16_LE : AFMT_U8);
2461 if (ret != 0) return ret;
2462 wwi->state = WINE_WS_STOPPED;
2464 if (wwi->lpQueuePtr) {
2465 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2466 wwi->lpQueuePtr = NULL;
2468 wwi->dwTotalRecorded = 0;
2469 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2471 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2472 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
2474 if (wwi->format.wBitsPerSample == 0) {
2475 WARN("Resetting zeroed wBitsPerSample\n");
2476 wwi->format.wBitsPerSample = 8 *
2477 (wwi->format.wf.nAvgBytesPerSec /
2478 wwi->format.wf.nSamplesPerSec) /
2479 wwi->format.wf.nChannels;
2482 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETBLKSIZE, &fragment_size);
2483 if (fragment_size == -1) {
2484 WARN("IOCTL can't 'SNDCTL_DSP_GETBLKSIZE' !\n");
2485 OSS_CloseDevice(wwi->ossdev);
2486 wwi->state = WINE_WS_CLOSED;
2487 return MMSYSERR_NOTENABLED;
2489 wwi->dwFragmentSize = fragment_size;
2491 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2492 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
2493 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
2494 wwi->format.wf.nBlockAlign);
2496 OSS_InitRingMessage(&wwi->msgRing);
2498 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
2499 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
2500 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2501 CloseHandle(wwi->hStartUpEvent);
2502 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2504 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2507 /**************************************************************************
2508 * widClose [internal]
2510 static DWORD widClose(WORD wDevID)
2512 WINE_WAVEIN* wwi;
2514 TRACE("(%u);\n", wDevID);
2515 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2516 WARN("can't close !\n");
2517 return MMSYSERR_INVALHANDLE;
2520 wwi = &WInDev[wDevID];
2522 if (wwi->lpQueuePtr != NULL) {
2523 WARN("still buffers open !\n");
2524 return WAVERR_STILLPLAYING;
2527 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
2528 OSS_CloseDevice(wwi->ossdev);
2529 wwi->state = WINE_WS_CLOSED;
2530 wwi->dwFragmentSize = 0;
2531 OSS_DestroyRingMessage(&wwi->msgRing);
2532 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
2535 /**************************************************************************
2536 * widAddBuffer [internal]
2538 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2540 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2542 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2543 WARN("can't do it !\n");
2544 return MMSYSERR_INVALHANDLE;
2546 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2547 TRACE("never been prepared !\n");
2548 return WAVERR_UNPREPARED;
2550 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2551 TRACE("header already in use !\n");
2552 return WAVERR_STILLPLAYING;
2555 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2556 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2557 lpWaveHdr->dwBytesRecorded = 0;
2558 lpWaveHdr->lpNext = NULL;
2560 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2561 return MMSYSERR_NOERROR;
2564 /**************************************************************************
2565 * widPrepare [internal]
2567 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2569 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2571 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_INVALHANDLE;
2573 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2574 return WAVERR_STILLPLAYING;
2576 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2577 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2578 lpWaveHdr->dwBytesRecorded = 0;
2579 TRACE("header prepared !\n");
2580 return MMSYSERR_NOERROR;
2583 /**************************************************************************
2584 * widUnprepare [internal]
2586 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2588 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2589 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_INVALHANDLE;
2591 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2592 return WAVERR_STILLPLAYING;
2594 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
2595 lpWaveHdr->dwFlags |= WHDR_DONE;
2597 return MMSYSERR_NOERROR;
2600 /**************************************************************************
2601 * widStart [internal]
2603 static DWORD widStart(WORD wDevID)
2605 TRACE("(%u);\n", wDevID);
2606 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2607 WARN("can't start recording !\n");
2608 return MMSYSERR_INVALHANDLE;
2611 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2612 return MMSYSERR_NOERROR;
2615 /**************************************************************************
2616 * widStop [internal]
2618 static DWORD widStop(WORD wDevID)
2620 TRACE("(%u);\n", wDevID);
2621 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2622 WARN("can't stop !\n");
2623 return MMSYSERR_INVALHANDLE;
2625 /* FIXME: reset aint stop */
2626 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2628 return MMSYSERR_NOERROR;
2631 /**************************************************************************
2632 * widReset [internal]
2634 static DWORD widReset(WORD wDevID)
2636 TRACE("(%u);\n", wDevID);
2637 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2638 WARN("can't reset !\n");
2639 return MMSYSERR_INVALHANDLE;
2641 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2642 return MMSYSERR_NOERROR;
2645 /**************************************************************************
2646 * widGetPosition [internal]
2648 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2650 int time;
2651 WINE_WAVEIN* wwi;
2653 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
2655 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2656 WARN("can't get pos !\n");
2657 return MMSYSERR_INVALHANDLE;
2659 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
2661 wwi = &WInDev[wDevID];
2663 TRACE("wType=%04X !\n", lpTime->wType);
2664 TRACE("wBitsPerSample=%u\n", wwi->format.wBitsPerSample);
2665 TRACE("nSamplesPerSec=%lu\n", wwi->format.wf.nSamplesPerSec);
2666 TRACE("nChannels=%u\n", wwi->format.wf.nChannels);
2667 TRACE("nAvgBytesPerSec=%lu\n", wwi->format.wf.nAvgBytesPerSec);
2668 switch (lpTime->wType) {
2669 case TIME_BYTES:
2670 lpTime->u.cb = wwi->dwTotalRecorded;
2671 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
2672 break;
2673 case TIME_SAMPLES:
2674 lpTime->u.sample = wwi->dwTotalRecorded * 8 /
2675 wwi->format.wBitsPerSample;
2676 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
2677 break;
2678 case TIME_SMPTE:
2679 time = wwi->dwTotalRecorded /
2680 (wwi->format.wf.nAvgBytesPerSec / 1000);
2681 lpTime->u.smpte.hour = time / 108000;
2682 time -= lpTime->u.smpte.hour * 108000;
2683 lpTime->u.smpte.min = time / 1800;
2684 time -= lpTime->u.smpte.min * 1800;
2685 lpTime->u.smpte.sec = time / 30;
2686 time -= lpTime->u.smpte.sec * 30;
2687 lpTime->u.smpte.frame = time;
2688 lpTime->u.smpte.fps = 30;
2689 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
2690 lpTime->u.smpte.hour, lpTime->u.smpte.min,
2691 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
2692 break;
2693 case TIME_MS:
2694 lpTime->u.ms = wwi->dwTotalRecorded /
2695 (wwi->format.wf.nAvgBytesPerSec / 1000);
2696 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
2697 break;
2698 default:
2699 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime->wType);
2700 lpTime->wType = TIME_MS;
2702 return MMSYSERR_NOERROR;
2705 /**************************************************************************
2706 * widMessage (WINEOSS.6)
2708 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2709 DWORD dwParam1, DWORD dwParam2)
2711 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2712 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2714 switch (wMsg) {
2715 case DRVM_INIT:
2716 case DRVM_EXIT:
2717 case DRVM_ENABLE:
2718 case DRVM_DISABLE:
2719 /* FIXME: Pretend this is supported */
2720 return 0;
2721 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2722 case WIDM_CLOSE: return widClose (wDevID);
2723 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2724 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2725 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2726 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
2727 case WIDM_GETNUMDEVS: return numInDev;
2728 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
2729 case WIDM_RESET: return widReset (wDevID);
2730 case WIDM_START: return widStart (wDevID);
2731 case WIDM_STOP: return widStop (wDevID);
2732 default:
2733 FIXME("unknown message %u!\n", wMsg);
2735 return MMSYSERR_NOTSUPPORTED;
2738 #else /* !HAVE_OSS */
2740 /**************************************************************************
2741 * wodMessage (WINEOSS.7)
2743 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2744 DWORD dwParam1, DWORD dwParam2)
2746 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2747 return MMSYSERR_NOTENABLED;
2750 /**************************************************************************
2751 * widMessage (WINEOSS.6)
2753 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2754 DWORD dwParam1, DWORD dwParam2)
2756 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2757 return MMSYSERR_NOTENABLED;
2760 #endif /* HAVE_OSS */