Initialize the pwave array whenever we don't have a hardware buffer.
[wine/multimedia.git] / dlls / winmm / wineoss / audio.c
blobdcde0a746eeb6ed1b1f2b543cdea8fc1ccd84b49
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 BOOL bTriggerSupport;
161 } OSS_DEVICE;
163 static OSS_DEVICE OSS_Devices[MAX_WAVEDRV];
165 typedef struct {
166 OSS_DEVICE* ossdev;
167 volatile int state; /* one of the WINE_WS_ manifest constants */
168 WAVEOPENDESC waveDesc;
169 WORD wFlags;
170 PCMWAVEFORMAT format;
172 /* OSS information */
173 DWORD dwFragmentSize; /* size of OSS buffer fragment */
174 DWORD dwBufferSize; /* size of whole OSS buffer in bytes */
175 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
176 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
177 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
179 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
180 DWORD dwLoops; /* private copy of loop counter */
182 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
183 DWORD dwWrittenTotal; /* number of bytes written to OSS buffer since opening */
184 BOOL bNeedPost; /* whether audio still needs to be physically started */
186 /* synchronization stuff */
187 HANDLE hStartUpEvent;
188 HANDLE hThread;
189 DWORD dwThreadID;
190 OSS_MSG_RING msgRing;
192 /* DirectSound stuff */
193 LPBYTE mapping;
194 DWORD maplen;
195 } WINE_WAVEOUT;
197 typedef struct {
198 OSS_DEVICE* ossdev;
199 volatile int state;
200 DWORD dwFragmentSize; /* OpenSound '/dev/dsp' give us that size */
201 WAVEOPENDESC waveDesc;
202 WORD wFlags;
203 PCMWAVEFORMAT format;
204 LPWAVEHDR lpQueuePtr;
205 DWORD dwTotalRecorded;
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 DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
243 int fd, val, err;
245 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
247 WARN("Couldn't open %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 err=MMSYSERR_NOERROR;
262 if (ossdev->format)
264 val = ossdev->format;
265 ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
266 if (val != ossdev->format) {
267 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
268 err=WAVERR_BADFORMAT;
269 if (strict_format)
270 goto error;
273 if (ossdev->stereo)
275 val = ossdev->stereo;
276 ioctl(fd, SNDCTL_DSP_STEREO, &ossdev->stereo);
277 if (val != ossdev->stereo) {
278 TRACE("Can't set stereo to %u (returned %d)\n", val, ossdev->stereo);
279 err=WAVERR_BADFORMAT;
280 if (strict_format)
281 goto error;
284 if (ossdev->sample_rate)
286 val = ossdev->sample_rate;
287 ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
288 if (!NEAR_MATCH(val, ossdev->sample_rate)) {
289 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
290 err=WAVERR_BADFORMAT;
291 if (strict_format)
292 goto error;
295 ossdev->fd = fd;
296 return err;
298 error:
299 close(fd);
300 return err;
303 /******************************************************************
304 * OSS_OpenDevice
306 * since OSS has poor capabilities in full duplex, we try here to let a program
307 * open the device for both waveout and wavein streams...
308 * this is hackish, but it's the way OSS interface is done...
310 static DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
311 int* frag, int strict_format,
312 int sample_rate, int stereo, int fmt)
314 DWORD ret;
316 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
317 req_access = O_RDWR;
319 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
320 if (ossdev->open_count == 0)
322 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
324 ossdev->audio_fragment = (frag) ? *frag : 0;
325 ossdev->sample_rate = sample_rate;
326 ossdev->stereo = stereo;
327 ossdev->format = fmt;
328 ossdev->open_access = req_access;
329 ossdev->owner_tid = GetCurrentThreadId();
331 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
333 else
335 /* check we really open with the same parameters */
336 if (ossdev->open_access != req_access)
338 WARN("Mismatch in access...\n");
339 return WAVERR_BADFORMAT;
341 /* FIXME: if really needed, we could do, in this case, on the fly
342 * PCM conversion (using the MSACM ad hoc driver)
344 if (ossdev->audio_fragment != (frag ? *frag : 0) ||
345 ossdev->sample_rate != sample_rate ||
346 ossdev->stereo != stereo ||
347 ossdev->format != fmt)
349 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
350 "OSS doesn't allow us different parameters\n"
351 "audio_frag(%x/%x) sample_rate(%d/%d) stereo(%d/%d) fmt(%d/%d)\n",
352 ossdev->audio_fragment, frag ? *frag : 0,
353 ossdev->sample_rate, sample_rate,
354 ossdev->stereo, stereo,
355 ossdev->format, fmt);
356 return WAVERR_BADFORMAT;
358 if (GetCurrentThreadId() != ossdev->owner_tid)
360 WARN("Another thread is trying to access audio...\n");
361 return MMSYSERR_ERROR;
365 ossdev->open_count++;
367 return MMSYSERR_NOERROR;
370 /******************************************************************
371 * OSS_CloseDevice
375 static void OSS_CloseDevice(OSS_DEVICE* ossdev)
377 if (--ossdev->open_count == 0)
379 /* reset the device before we close it in case it is in a bad state */
380 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
381 close(ossdev->fd);
385 /******************************************************************
386 * OSS_ResetDevice
388 * Resets the device. OSS Commercial requires the device to be closed
389 * after a SNDCTL_DSP_RESET ioctl call... this function implements
390 * this behavior...
392 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
394 DWORD ret;
396 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
398 perror("ioctl SNDCTL_DSP_RESET");
399 return -1;
401 TRACE("Changing fd from %d to ", ossdev->fd);
402 close(ossdev->fd);
403 ret = OSS_RawOpenDevice(ossdev, 1);
404 TRACE("%d\n", ossdev->fd);
405 return ret;
408 const static int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
409 const static int win_std_rates[5]={96000,48000,44100,22050,11025};
410 const static int win_std_formats[2][2][5]=
411 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
412 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
413 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
414 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
415 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
416 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
417 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
418 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
421 /******************************************************************
422 * OSS_WaveOutInit
426 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
428 int rc,arg;
429 int f,c,r;
431 if (OSS_OpenDevice(ossdev, O_WRONLY, NULL, 0, 0, 0, 0) != 0) return FALSE;
432 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
434 /* FIXME: some programs compare this string against the content of the
435 * registry for MM drivers. The names have to match in order for the
436 * program to work (e.g. MS win9x mplayer.exe)
438 #ifdef EMULATE_SB16
439 ossdev->out_caps.wMid = 0x0002;
440 ossdev->out_caps.wPid = 0x0104;
441 strcpy(ossdev->out_caps.szPname, "SB16 Wave Out");
442 #else
443 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
444 ossdev->out_caps.wPid = 0x0001; /* Product ID */
445 /* strcpy(ossdev->out_caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
446 strcpy(ossdev->out_caps.szPname, "CS4236/37/38");
447 #endif
448 ossdev->out_caps.vDriverVersion = 0x0100;
449 ossdev->out_caps.wChannels = 1;
450 ossdev->out_caps.dwFormats = 0x00000000;
451 ossdev->out_caps.wReserved1 = 0;
452 ossdev->out_caps.dwSupport = WAVECAPS_VOLUME;
454 if (WINE_TRACE_ON(wave)) {
455 /* Note that this only reports the formats supported by the hardware.
456 * The driver may support other formats and do the conversions in
457 * software which is why we don't use this value
459 int oss_mask;
460 ioctl(ossdev->fd, SNDCTL_DSP_GETFMTS, &oss_mask);
461 TRACE("OSS dsp out mask=%08x\n", oss_mask);
464 /* We must first set the format and the stereo mode as some sound cards
465 * may support 44kHz mono but not 44kHz stereo. Also we must
466 * systematically check the return value of these ioctls as they will
467 * always succeed (see OSS Linux) but will modify the parameter to match
468 * whatever they support. The OSS specs also say we must first set the
469 * sample size, then the stereo and then the sample rate.
471 for (f=0;f<2;f++) {
472 arg=win_std_oss_fmts[f];
473 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
474 if (rc!=0 || arg!=win_std_oss_fmts[f])
475 continue;
477 for (c=0;c<2;c++) {
478 arg=c;
479 rc=ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &arg);
480 if (rc!=0 || arg!=c)
481 continue;
482 if (c==1) {
483 ossdev->out_caps.wChannels=2;
484 ossdev->out_caps.dwSupport|=WAVECAPS_LRVOLUME;
487 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
488 arg=win_std_rates[r];
489 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
490 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
491 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c+1);
492 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]))
493 ossdev->out_caps.dwFormats|=win_std_formats[f][c][r];
498 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
499 TRACE("OSS dsp out caps=%08X\n", arg);
500 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
501 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
503 /* well, might as well use the DirectSound cap flag for something */
504 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
505 !(arg & DSP_CAP_BATCH))
506 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
508 OSS_CloseDevice(ossdev);
509 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
510 ossdev->out_caps.dwFormats, ossdev->out_caps.dwSupport);
511 return TRUE;
514 /******************************************************************
515 * OSS_WaveInInit
519 static BOOL OSS_WaveInInit(OSS_DEVICE* ossdev)
521 int rc,arg;
522 int f,c,r;
524 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0, 0, 0, 0) != 0) return FALSE;
525 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
527 /* See comment in OSS_WaveOutInit */
528 #ifdef EMULATE_SB16
529 ossdev->in_caps.wMid = 0x0002;
530 ossdev->in_caps.wPid = 0x0004;
531 strcpy(ossdev->in_caps.szPname, "SB16 Wave In");
532 #else
533 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
534 ossdev->in_caps.wPid = 0x0001; /* Product ID */
535 strcpy(ossdev->in_caps.szPname, "OpenSoundSystem WAVIN Driver");
536 #endif
537 ossdev->in_caps.dwFormats = 0x00000000;
538 ossdev->in_caps.wChannels = 1;
539 ossdev->in_caps.wReserved1 = 0;
540 ossdev->bTriggerSupport = FALSE;
542 if (WINE_TRACE_ON(wave)) {
543 /* Note that this only reports the formats supported by the hardware.
544 * The driver may support other formats and do the conversions in
545 * software which is why we don't use this value
547 int oss_mask;
548 ioctl(ossdev->fd, SNDCTL_DSP_GETFMTS, &oss_mask);
549 TRACE("OSS dsp out mask=%08x\n", oss_mask);
552 /* See the comment in OSS_WaveOutInit */
553 for (f=0;f<2;f++) {
554 arg=win_std_oss_fmts[f];
555 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
556 if (rc!=0 || arg!=win_std_oss_fmts[f])
557 continue;
559 for (c=0;c<2;c++) {
560 arg=c;
561 rc=ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &arg);
562 if (rc!=0 || arg!=c)
563 continue;
564 if (c==1) {
565 ossdev->in_caps.wChannels=2;
568 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
569 arg=win_std_rates[r];
570 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
571 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc,arg,win_std_rates[r],win_std_oss_fmts[f],c+1);
572 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]))
573 ossdev->in_caps.dwFormats|=win_std_formats[f][c][r];
578 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
579 TRACE("OSS dsp in caps=%08X\n", arg);
580 if (arg & DSP_CAP_TRIGGER)
581 ossdev->bTriggerSupport = TRUE;
583 OSS_CloseDevice(ossdev);
584 TRACE("in dwFormats = %08lX\n", ossdev->in_caps.dwFormats);
585 return TRUE;
588 /******************************************************************
589 * OSS_WaveFullDuplexInit
593 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
595 int caps;
597 if (OSS_OpenDevice(ossdev, O_RDWR, NULL, 0, 0, 0, 0) != 0) return;
598 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
600 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
602 OSS_CloseDevice(ossdev);
605 /******************************************************************
606 * OSS_WaveInit
608 * Initialize internal structures from OSS information
610 LONG OSS_WaveInit(void)
612 int i;
614 /* FIXME: only one device is supported */
615 memset(&OSS_Devices, 0, sizeof(OSS_Devices));
616 /* FIXME: should check that dsp actually points to dsp0, or that dsp0 exists
617 * we should also be able to configure (bitmap) which devices we want to use...
618 * - or even store the name of all drivers in our configuration
620 OSS_Devices[0].dev_name = "/dev/dsp";
621 OSS_Devices[0].mixer_name = "/dev/mixer";
622 OSS_Devices[1].dev_name = "/dev/dsp1";
623 OSS_Devices[1].mixer_name = "/dev/mixer1";
624 OSS_Devices[2].dev_name = "/dev/dsp2";
625 OSS_Devices[2].mixer_name = "/dev/mixer2";
627 /* start with output device */
628 for (i = 0; i < MAX_WAVEDRV; ++i)
630 if (OSS_WaveOutInit(&OSS_Devices[i]))
632 WOutDev[numOutDev].state = WINE_WS_CLOSED;
633 WOutDev[numOutDev].ossdev = &OSS_Devices[i];
634 numOutDev++;
638 /* then do input device */
639 for (i = 0; i < MAX_WAVEDRV; ++i)
641 if (OSS_WaveInInit(&OSS_Devices[i]))
643 WInDev[numInDev].state = WINE_WS_CLOSED;
644 WInDev[numInDev].ossdev = &OSS_Devices[i];
645 numInDev++;
649 /* finish with the full duplex bits */
650 for (i = 0; i < MAX_WAVEDRV; i++)
651 OSS_WaveFullDuplexInit(&OSS_Devices[i]);
653 return 0;
656 /******************************************************************
657 * OSS_InitRingMessage
659 * Initialize the ring of messages for passing between driver's caller and playback/record
660 * thread
662 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
664 omr->msg_toget = 0;
665 omr->msg_tosave = 0;
666 #ifdef USE_PIPE_SYNC
667 if (pipe(omr->msg_pipe) < 0) {
668 omr->msg_pipe[0] = -1;
669 omr->msg_pipe[1] = -1;
670 ERR("could not create pipe, error=%s\n", strerror(errno));
672 #else
673 omr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
674 #endif
675 memset(omr->messages, 0, sizeof(OSS_MSG) * OSS_RING_BUFFER_SIZE);
676 InitializeCriticalSection(&omr->msg_crst);
677 return 0;
680 /******************************************************************
681 * OSS_DestroyRingMessage
684 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
686 #ifdef USE_PIPE_SYNC
687 close(omr->msg_pipe[0]);
688 close(omr->msg_pipe[1]);
689 #else
690 CloseHandle(omr->msg_event);
691 #endif
692 DeleteCriticalSection(&omr->msg_crst);
693 return 0;
696 /******************************************************************
697 * OSS_AddRingMessage
699 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
701 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
703 HANDLE hEvent = INVALID_HANDLE_VALUE;
705 EnterCriticalSection(&omr->msg_crst);
706 if ((omr->msg_toget == ((omr->msg_tosave + 1) % OSS_RING_BUFFER_SIZE))) /* buffer overflow ? */
708 ERR("buffer overflow !?\n");
709 LeaveCriticalSection(&omr->msg_crst);
710 return 0;
712 if (wait)
714 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
715 if (hEvent == INVALID_HANDLE_VALUE)
717 ERR("can't create event !?\n");
718 LeaveCriticalSection(&omr->msg_crst);
719 return 0;
721 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
722 FIXME("two fast messages in the queue!!!!\n");
724 /* fast messages have to be added at the start of the queue */
725 omr->msg_toget = (omr->msg_toget + OSS_RING_BUFFER_SIZE - 1) % OSS_RING_BUFFER_SIZE;
727 omr->messages[omr->msg_toget].msg = msg;
728 omr->messages[omr->msg_toget].param = param;
729 omr->messages[omr->msg_toget].hEvent = hEvent;
731 else
733 omr->messages[omr->msg_tosave].msg = msg;
734 omr->messages[omr->msg_tosave].param = param;
735 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
736 omr->msg_tosave = (omr->msg_tosave + 1) % OSS_RING_BUFFER_SIZE;
738 LeaveCriticalSection(&omr->msg_crst);
739 /* signal a new message */
740 SIGNAL_OMR(omr);
741 if (wait)
743 /* wait for playback/record thread to have processed the message */
744 WaitForSingleObject(hEvent, INFINITE);
745 CloseHandle(hEvent);
747 return 1;
750 /******************************************************************
751 * OSS_RetrieveRingMessage
753 * Get a message from the ring. Should be called by the playback/record thread.
755 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
756 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
758 EnterCriticalSection(&omr->msg_crst);
760 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
762 LeaveCriticalSection(&omr->msg_crst);
763 return 0;
766 *msg = omr->messages[omr->msg_toget].msg;
767 omr->messages[omr->msg_toget].msg = 0;
768 *param = omr->messages[omr->msg_toget].param;
769 *hEvent = omr->messages[omr->msg_toget].hEvent;
770 omr->msg_toget = (omr->msg_toget + 1) % OSS_RING_BUFFER_SIZE;
771 CLEAR_OMR(omr);
772 LeaveCriticalSection(&omr->msg_crst);
773 return 1;
776 /*======================================================================*
777 * Low level WAVE OUT implementation *
778 *======================================================================*/
780 /**************************************************************************
781 * wodNotifyClient [internal]
783 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
785 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
787 switch (wMsg) {
788 case WOM_OPEN:
789 case WOM_CLOSE:
790 case WOM_DONE:
791 if (wwo->wFlags != DCB_NULL &&
792 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
793 (HDRVR)wwo->waveDesc.hWave, wMsg,
794 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
795 WARN("can't notify client !\n");
796 return MMSYSERR_ERROR;
798 break;
799 default:
800 FIXME("Unknown callback message %u\n", wMsg);
801 return MMSYSERR_INVALPARAM;
803 return MMSYSERR_NOERROR;
806 /**************************************************************************
807 * wodUpdatePlayedTotal [internal]
810 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
812 audio_buf_info dspspace;
813 if (!info) info = &dspspace;
815 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
816 ERR("ioctl can't 'SNDCTL_DSP_GETOSPACE' !\n");
817 return FALSE;
819 wwo->dwPlayedTotal = wwo->dwWrittenTotal - (wwo->dwBufferSize - info->bytes);
820 return TRUE;
823 /**************************************************************************
824 * wodPlayer_BeginWaveHdr [internal]
826 * Makes the specified lpWaveHdr the currently playing wave header.
827 * If the specified wave header is a begin loop and we're not already in
828 * a loop, setup the loop.
830 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
832 wwo->lpPlayPtr = lpWaveHdr;
834 if (!lpWaveHdr) return;
836 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
837 if (wwo->lpLoopPtr) {
838 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
839 } else {
840 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
841 wwo->lpLoopPtr = lpWaveHdr;
842 /* Windows does not touch WAVEHDR.dwLoops,
843 * so we need to make an internal copy */
844 wwo->dwLoops = lpWaveHdr->dwLoops;
847 wwo->dwPartialOffset = 0;
850 /**************************************************************************
851 * wodPlayer_PlayPtrNext [internal]
853 * Advance the play pointer to the next waveheader, looping if required.
855 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
857 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
859 wwo->dwPartialOffset = 0;
860 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
861 /* We're at the end of a loop, loop if required */
862 if (--wwo->dwLoops > 0) {
863 wwo->lpPlayPtr = wwo->lpLoopPtr;
864 } else {
865 /* Handle overlapping loops correctly */
866 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
867 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
868 /* shall we consider the END flag for the closing loop or for
869 * the opening one or for both ???
870 * code assumes for closing loop only
872 } else {
873 lpWaveHdr = lpWaveHdr->lpNext;
875 wwo->lpLoopPtr = NULL;
876 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
878 } else {
879 /* We're not in a loop. Advance to the next wave header */
880 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
883 return lpWaveHdr;
886 /**************************************************************************
887 * wodPlayer_DSPWait [internal]
888 * Returns the number of milliseconds to wait for the DSP buffer to write
889 * one fragment.
891 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
893 /* time for one fragment to be played */
894 return wwo->dwFragmentSize * 1000 / wwo->format.wf.nAvgBytesPerSec;
897 /**************************************************************************
898 * wodPlayer_NotifyWait [internal]
899 * Returns the number of milliseconds to wait before attempting to notify
900 * completion of the specified wavehdr.
901 * This is based on the number of bytes remaining to be written in the
902 * wave.
904 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
906 DWORD dwMillis;
908 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
909 dwMillis = 1;
910 } else {
911 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
912 if (!dwMillis) dwMillis = 1;
915 return dwMillis;
919 /**************************************************************************
920 * wodPlayer_WriteMaxFrags [internal]
921 * Writes the maximum number of bytes possible to the DSP and returns
922 * TRUE iff the current playPtr has been fully played
924 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
926 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
927 DWORD toWrite = min(dwLength, *bytes);
928 int written;
929 BOOL ret = FALSE;
931 TRACE("Writing wavehdr %p.%lu[%lu]/%lu\n",
932 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
934 if (toWrite > 0)
936 written = write(wwo->ossdev->fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
937 if (written <= 0) return FALSE;
939 else
940 written = 0;
942 if (written >= dwLength) {
943 /* If we wrote all current wavehdr, skip to the next one */
944 wodPlayer_PlayPtrNext(wwo);
945 ret = TRUE;
946 } else {
947 /* Remove the amount written */
948 wwo->dwPartialOffset += written;
950 *bytes -= written;
951 wwo->dwWrittenTotal += written;
953 return ret;
957 /**************************************************************************
958 * wodPlayer_NotifyCompletions [internal]
960 * Notifies and remove from queue all wavehdrs which have been played to
961 * the speaker (ie. they have cleared the OSS buffer). If force is true,
962 * we notify all wavehdrs and remove them all from the queue even if they
963 * are unplayed or part of a loop.
965 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
967 LPWAVEHDR lpWaveHdr;
969 /* Start from lpQueuePtr and keep notifying until:
970 * - we hit an unwritten wavehdr
971 * - we hit the beginning of a running loop
972 * - we hit a wavehdr which hasn't finished playing
974 while ((lpWaveHdr = wwo->lpQueuePtr) &&
975 (force ||
976 (lpWaveHdr != wwo->lpPlayPtr &&
977 lpWaveHdr != wwo->lpLoopPtr &&
978 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
980 wwo->lpQueuePtr = lpWaveHdr->lpNext;
982 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
983 lpWaveHdr->dwFlags |= WHDR_DONE;
985 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
987 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
988 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
991 /**************************************************************************
992 * wodPlayer_Reset [internal]
994 * wodPlayer helper. Resets current output stream.
996 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
998 wodUpdatePlayedTotal(wwo, NULL);
999 /* updates current notify list */
1000 wodPlayer_NotifyCompletions(wwo, FALSE);
1002 /* flush all possible output */
1003 if (OSS_ResetDevice(wwo->ossdev) != MMSYSERR_NOERROR)
1005 wwo->hThread = 0;
1006 wwo->state = WINE_WS_STOPPED;
1007 ExitThread(-1);
1010 if (reset) {
1011 enum win_wm_message msg;
1012 DWORD param;
1013 HANDLE ev;
1015 /* remove any buffer */
1016 wodPlayer_NotifyCompletions(wwo, TRUE);
1018 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1019 wwo->state = WINE_WS_STOPPED;
1020 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1021 /* Clear partial wavehdr */
1022 wwo->dwPartialOffset = 0;
1024 /* remove any existing message in the ring */
1025 EnterCriticalSection(&wwo->msgRing.msg_crst);
1026 /* return all pending headers in queue */
1027 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1029 if (msg != WINE_WM_HEADER)
1031 FIXME("shouldn't have headers left\n");
1032 SetEvent(ev);
1033 continue;
1035 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1036 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1038 wodNotifyClient(wwo, WOM_DONE, param, 0);
1040 RESET_OMR(&wwo->msgRing);
1041 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1042 } else {
1043 if (wwo->lpLoopPtr) {
1044 /* complicated case, not handled yet (could imply modifying the loop counter */
1045 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1046 wwo->lpPlayPtr = wwo->lpLoopPtr;
1047 wwo->dwPartialOffset = 0;
1048 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1049 } else {
1050 LPWAVEHDR ptr;
1051 DWORD sz = wwo->dwPartialOffset;
1053 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1054 /* compute the max size playable from lpQueuePtr */
1055 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1056 sz += ptr->dwBufferLength;
1058 /* because the reset lpPlayPtr will be lpQueuePtr */
1059 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1060 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1061 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1062 wwo->lpPlayPtr = wwo->lpQueuePtr;
1064 wwo->state = WINE_WS_PAUSED;
1068 /**************************************************************************
1069 * wodPlayer_ProcessMessages [internal]
1071 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1073 LPWAVEHDR lpWaveHdr;
1074 enum win_wm_message msg;
1075 DWORD param;
1076 HANDLE ev;
1078 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1079 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
1080 switch (msg) {
1081 case WINE_WM_PAUSING:
1082 wodPlayer_Reset(wwo, FALSE);
1083 SetEvent(ev);
1084 break;
1085 case WINE_WM_RESTARTING:
1086 if (wwo->state == WINE_WS_PAUSED)
1088 wwo->state = WINE_WS_PLAYING;
1090 SetEvent(ev);
1091 break;
1092 case WINE_WM_HEADER:
1093 lpWaveHdr = (LPWAVEHDR)param;
1095 /* insert buffer at the end of queue */
1097 LPWAVEHDR* wh;
1098 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1099 *wh = lpWaveHdr;
1101 if (!wwo->lpPlayPtr)
1102 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1103 if (wwo->state == WINE_WS_STOPPED)
1104 wwo->state = WINE_WS_PLAYING;
1105 break;
1106 case WINE_WM_RESETTING:
1107 wodPlayer_Reset(wwo, TRUE);
1108 SetEvent(ev);
1109 break;
1110 case WINE_WM_UPDATE:
1111 wodUpdatePlayedTotal(wwo, NULL);
1112 SetEvent(ev);
1113 break;
1114 case WINE_WM_BREAKLOOP:
1115 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1116 /* ensure exit at end of current loop */
1117 wwo->dwLoops = 1;
1119 SetEvent(ev);
1120 break;
1121 case WINE_WM_CLOSING:
1122 /* sanity check: this should not happen since the device must have been reset before */
1123 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1124 wwo->hThread = 0;
1125 wwo->state = WINE_WS_CLOSED;
1126 SetEvent(ev);
1127 ExitThread(0);
1128 /* shouldn't go here */
1129 default:
1130 FIXME("unknown message %d\n", msg);
1131 break;
1136 /**************************************************************************
1137 * wodPlayer_FeedDSP [internal]
1138 * Feed as much sound data as we can into the DSP and return the number of
1139 * milliseconds before it will be necessary to feed the DSP again.
1141 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1143 audio_buf_info dspspace;
1144 DWORD availInQ;
1146 wodUpdatePlayedTotal(wwo, &dspspace);
1147 availInQ = dspspace.bytes;
1148 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1149 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1151 /* input queue empty and output buffer with less than one fragment to play
1152 * actually some cards do not play the fragment before the last if this one is partially feed
1153 * so we need to test for full the availability of 2 fragments
1155 if (!wwo->lpPlayPtr && wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize &&
1156 !wwo->bNeedPost) {
1157 TRACE("Run out of wavehdr:s...\n");
1158 return INFINITE;
1161 /* no more room... no need to try to feed */
1162 if (dspspace.fragments != 0) {
1163 /* Feed from partial wavehdr */
1164 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1165 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1168 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1169 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1170 do {
1171 TRACE("Setting time to elapse for %p to %lu\n",
1172 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1173 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1174 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1175 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1178 if (wwo->bNeedPost) {
1179 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1180 * if it didn't get one, we give it the other */
1181 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1182 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1183 wwo->bNeedPost = FALSE;
1187 return wodPlayer_DSPWait(wwo);
1191 /**************************************************************************
1192 * wodPlayer [internal]
1194 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1196 WORD uDevID = (DWORD)pmt;
1197 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1198 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1199 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1200 DWORD dwSleepTime;
1202 wwo->state = WINE_WS_STOPPED;
1203 SetEvent(wwo->hStartUpEvent);
1205 for (;;) {
1206 /** Wait for the shortest time before an action is required. If there
1207 * are no pending actions, wait forever for a command.
1209 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1210 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1211 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1212 wodPlayer_ProcessMessages(wwo);
1213 if (wwo->state == WINE_WS_PLAYING) {
1214 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1215 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1216 if (dwNextFeedTime == INFINITE) {
1217 /* FeedDSP ran out of data, but before flushing, */
1218 /* check that a notification didn't give us more */
1219 wodPlayer_ProcessMessages(wwo);
1220 if (!wwo->lpPlayPtr) {
1221 TRACE("flushing\n");
1222 ioctl(wwo->ossdev->fd, SNDCTL_DSP_SYNC, 0);
1223 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1224 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1225 } else {
1226 TRACE("recovering\n");
1227 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1230 } else {
1231 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1236 /**************************************************************************
1237 * wodGetDevCaps [internal]
1239 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1241 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1243 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1245 if (wDevID >= numOutDev) {
1246 TRACE("numOutDev reached !\n");
1247 return MMSYSERR_BADDEVICEID;
1250 memcpy(lpCaps, &WOutDev[wDevID].ossdev->out_caps, min(dwSize, sizeof(*lpCaps)));
1251 return MMSYSERR_NOERROR;
1254 /**************************************************************************
1255 * wodOpen [internal]
1257 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1259 int audio_fragment;
1260 WINE_WAVEOUT* wwo;
1261 audio_buf_info info;
1262 DWORD ret;
1264 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1265 if (lpDesc == NULL) {
1266 WARN("Invalid Parameter !\n");
1267 return MMSYSERR_INVALPARAM;
1269 if (wDevID >= numOutDev) {
1270 TRACE("MAX_WAVOUTDRV reached !\n");
1271 return MMSYSERR_BADDEVICEID;
1274 /* only PCM format is supported so far... */
1275 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1276 lpDesc->lpFormat->nChannels == 0 ||
1277 lpDesc->lpFormat->nSamplesPerSec == 0) {
1278 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1279 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1280 lpDesc->lpFormat->nSamplesPerSec);
1281 return WAVERR_BADFORMAT;
1284 if (dwFlags & WAVE_FORMAT_QUERY) {
1285 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1286 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1287 lpDesc->lpFormat->nSamplesPerSec);
1288 return MMSYSERR_NOERROR;
1291 wwo = &WOutDev[wDevID];
1293 if ((dwFlags & WAVE_DIRECTSOUND) &&
1294 !(wwo->ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
1295 /* not supported, ignore it */
1296 dwFlags &= ~WAVE_DIRECTSOUND;
1298 if (dwFlags & WAVE_DIRECTSOUND) {
1299 if (wwo->ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1300 /* we have realtime DirectSound, fragments just waste our time,
1301 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1302 audio_fragment = 0x0020000B;
1303 else
1304 /* to approximate realtime, we must use small fragments,
1305 * let's try to fragment the above 64KB (256 * 2^8) */
1306 audio_fragment = 0x01000008;
1307 } else {
1308 /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
1309 * thus leading to 46ms per fragment, and a turnaround time of 185ms
1311 /* 16 fragments max, 2^10=1024 bytes per fragment */
1312 audio_fragment = 0x000F000A;
1314 if (wwo->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
1315 /* we want to be able to mmap() the device, which means it must be opened readable,
1316 * otherwise mmap() will fail (at least under Linux) */
1317 ret = OSS_OpenDevice(wwo->ossdev,
1318 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
1319 &audio_fragment,
1320 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
1321 lpDesc->lpFormat->nSamplesPerSec,
1322 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
1323 (lpDesc->lpFormat->wBitsPerSample == 16)
1324 ? AFMT_S16_LE : AFMT_U8);
1325 if ((ret==WAVERR_BADFORMAT) && (dwFlags & WAVE_DIRECTSOUND)) {
1326 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev->sample_rate;
1327 lpDesc->lpFormat->nChannels=(wwo->ossdev->stereo ? 2 : 1);
1328 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev->format == AFMT_U8 ? 8 : 16);
1329 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
1330 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
1331 ret=MMSYSERR_NOERROR;
1332 TRACE("OSS_OpenDevice returned this format: %ldx%dx%d\n",
1333 lpDesc->lpFormat->nSamplesPerSec,
1334 lpDesc->lpFormat->wBitsPerSample,
1335 lpDesc->lpFormat->nChannels);
1337 if (ret != 0) return ret;
1338 wwo->state = WINE_WS_STOPPED;
1340 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1342 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1343 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1345 if (wwo->format.wBitsPerSample == 0) {
1346 WARN("Resetting zeroed wBitsPerSample\n");
1347 wwo->format.wBitsPerSample = 8 *
1348 (wwo->format.wf.nAvgBytesPerSec /
1349 wwo->format.wf.nSamplesPerSec) /
1350 wwo->format.wf.nChannels;
1352 /* Read output space info for future reference */
1353 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
1354 ERR("ioctl can't 'SNDCTL_DSP_GETOSPACE' !\n");
1355 OSS_CloseDevice(wwo->ossdev);
1356 wwo->state = WINE_WS_CLOSED;
1357 return MMSYSERR_NOTENABLED;
1360 /* Check that fragsize is correct per our settings above */
1361 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
1362 /* we've tried to set 1K fragments or less, but it didn't work */
1363 ERR("fragment size set failed, size is now %d\n", info.fragsize);
1364 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
1365 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
1368 /* Remember fragsize and total buffer size for future use */
1369 wwo->dwFragmentSize = info.fragsize;
1370 wwo->dwBufferSize = info.fragstotal * info.fragsize;
1371 wwo->dwPlayedTotal = 0;
1372 wwo->dwWrittenTotal = 0;
1373 wwo->bNeedPost = TRUE;
1375 OSS_InitRingMessage(&wwo->msgRing);
1377 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1378 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1379 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1380 CloseHandle(wwo->hStartUpEvent);
1381 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1383 TRACE("fd=%d fragmentSize=%ld\n",
1384 wwo->ossdev->fd, wwo->dwFragmentSize);
1385 if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1386 ERR("Fragment doesn't contain an integral number of data blocks\n");
1388 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1389 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1390 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1391 wwo->format.wf.nBlockAlign);
1393 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1396 /**************************************************************************
1397 * wodClose [internal]
1399 static DWORD wodClose(WORD wDevID)
1401 DWORD ret = MMSYSERR_NOERROR;
1402 WINE_WAVEOUT* wwo;
1404 TRACE("(%u);\n", wDevID);
1406 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1407 WARN("bad device ID !\n");
1408 return MMSYSERR_BADDEVICEID;
1411 wwo = &WOutDev[wDevID];
1412 if (wwo->lpQueuePtr) {
1413 WARN("buffers still playing !\n");
1414 ret = WAVERR_STILLPLAYING;
1415 } else {
1416 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1417 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1419 if (wwo->mapping) {
1420 munmap(wwo->mapping, wwo->maplen);
1421 wwo->mapping = NULL;
1424 OSS_DestroyRingMessage(&wwo->msgRing);
1426 OSS_CloseDevice(wwo->ossdev);
1427 wwo->state = WINE_WS_CLOSED;
1428 wwo->dwFragmentSize = 0;
1429 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1431 return ret;
1434 /**************************************************************************
1435 * wodWrite [internal]
1438 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1440 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1442 /* first, do the sanity checks... */
1443 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1444 WARN("bad dev ID !\n");
1445 return MMSYSERR_BADDEVICEID;
1448 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1449 return WAVERR_UNPREPARED;
1451 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1452 return WAVERR_STILLPLAYING;
1454 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1455 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1456 lpWaveHdr->lpNext = 0;
1458 if ((lpWaveHdr->dwBufferLength & (WOutDev[wDevID].format.wf.nBlockAlign - 1)) != 0)
1460 WARN("WaveHdr length isn't a multiple of the PCM block size: %ld %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].format.wf.nBlockAlign);
1461 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].format.wf.nBlockAlign - 1);
1464 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1466 return MMSYSERR_NOERROR;
1469 /**************************************************************************
1470 * wodPrepare [internal]
1472 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1474 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1476 if (wDevID >= numOutDev) {
1477 WARN("bad device ID !\n");
1478 return MMSYSERR_BADDEVICEID;
1481 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1482 return WAVERR_STILLPLAYING;
1484 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1485 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1486 return MMSYSERR_NOERROR;
1489 /**************************************************************************
1490 * wodUnprepare [internal]
1492 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1494 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1496 if (wDevID >= numOutDev) {
1497 WARN("bad device ID !\n");
1498 return MMSYSERR_BADDEVICEID;
1501 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1502 return WAVERR_STILLPLAYING;
1504 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1505 lpWaveHdr->dwFlags |= WHDR_DONE;
1507 return MMSYSERR_NOERROR;
1510 /**************************************************************************
1511 * wodPause [internal]
1513 static DWORD wodPause(WORD wDevID)
1515 TRACE("(%u);!\n", wDevID);
1517 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1518 WARN("bad device ID !\n");
1519 return MMSYSERR_BADDEVICEID;
1522 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1524 return MMSYSERR_NOERROR;
1527 /**************************************************************************
1528 * wodRestart [internal]
1530 static DWORD wodRestart(WORD wDevID)
1532 TRACE("(%u);\n", wDevID);
1534 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1535 WARN("bad device ID !\n");
1536 return MMSYSERR_BADDEVICEID;
1539 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1541 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1542 /* FIXME: Myst crashes with this ... hmm -MM
1543 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1546 return MMSYSERR_NOERROR;
1549 /**************************************************************************
1550 * wodReset [internal]
1552 static DWORD wodReset(WORD wDevID)
1554 TRACE("(%u);\n", wDevID);
1556 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1557 WARN("bad device ID !\n");
1558 return MMSYSERR_BADDEVICEID;
1561 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1563 return MMSYSERR_NOERROR;
1566 /**************************************************************************
1567 * wodGetPosition [internal]
1569 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1571 int time;
1572 DWORD val;
1573 WINE_WAVEOUT* wwo;
1575 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1577 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1578 WARN("bad device ID !\n");
1579 return MMSYSERR_BADDEVICEID;
1582 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1584 wwo = &WOutDev[wDevID];
1585 #ifdef EXACT_WODPOSITION
1586 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1587 #endif
1588 val = wwo->dwPlayedTotal;
1590 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1591 lpTime->wType, wwo->format.wBitsPerSample,
1592 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1593 wwo->format.wf.nAvgBytesPerSec);
1594 TRACE("dwPlayedTotal=%lu\n", val);
1596 switch (lpTime->wType) {
1597 case TIME_BYTES:
1598 lpTime->u.cb = val;
1599 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1600 break;
1601 case TIME_SAMPLES:
1602 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1603 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1604 break;
1605 case TIME_SMPTE:
1606 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1607 lpTime->u.smpte.hour = time / 108000;
1608 time -= lpTime->u.smpte.hour * 108000;
1609 lpTime->u.smpte.min = time / 1800;
1610 time -= lpTime->u.smpte.min * 1800;
1611 lpTime->u.smpte.sec = time / 30;
1612 time -= lpTime->u.smpte.sec * 30;
1613 lpTime->u.smpte.frame = time;
1614 lpTime->u.smpte.fps = 30;
1615 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1616 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1617 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1618 break;
1619 default:
1620 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1621 lpTime->wType = TIME_MS;
1622 case TIME_MS:
1623 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1624 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1625 break;
1627 return MMSYSERR_NOERROR;
1630 /**************************************************************************
1631 * wodBreakLoop [internal]
1633 static DWORD wodBreakLoop(WORD wDevID)
1635 TRACE("(%u);\n", wDevID);
1637 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1638 WARN("bad device ID !\n");
1639 return MMSYSERR_BADDEVICEID;
1641 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1642 return MMSYSERR_NOERROR;
1645 /**************************************************************************
1646 * wodGetVolume [internal]
1648 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1650 int mixer;
1651 int volume;
1652 DWORD left, right;
1654 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1656 if (lpdwVol == NULL)
1657 return MMSYSERR_NOTENABLED;
1658 if (wDevID >= numOutDev) return MMSYSERR_INVALPARAM;
1660 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_RDONLY|O_NDELAY)) < 0) {
1661 WARN("mixer device not available !\n");
1662 return MMSYSERR_NOTENABLED;
1664 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
1665 WARN("unable to read mixer !\n");
1666 return MMSYSERR_NOTENABLED;
1668 close(mixer);
1669 left = LOBYTE(volume);
1670 right = HIBYTE(volume);
1671 TRACE("left=%ld right=%ld !\n", left, right);
1672 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1673 return MMSYSERR_NOERROR;
1676 /**************************************************************************
1677 * wodSetVolume [internal]
1679 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1681 int mixer;
1682 int volume;
1683 DWORD left, right;
1685 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1687 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1688 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1689 volume = left + (right << 8);
1691 if (wDevID >= numOutDev) return MMSYSERR_INVALPARAM;
1693 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_WRONLY|O_NDELAY)) < 0) {
1694 WARN("mixer device not available !\n");
1695 return MMSYSERR_NOTENABLED;
1697 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
1698 WARN("unable to set mixer !\n");
1699 return MMSYSERR_NOTENABLED;
1700 } else {
1701 TRACE("volume=%04x\n", (unsigned)volume);
1703 close(mixer);
1704 return MMSYSERR_NOERROR;
1707 /**************************************************************************
1708 * wodMessage (WINEOSS.7)
1710 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1711 DWORD dwParam1, DWORD dwParam2)
1713 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1714 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1716 switch (wMsg) {
1717 case DRVM_INIT:
1718 case DRVM_EXIT:
1719 case DRVM_ENABLE:
1720 case DRVM_DISABLE:
1721 /* FIXME: Pretend this is supported */
1722 return 0;
1723 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1724 case WODM_CLOSE: return wodClose (wDevID);
1725 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1726 case WODM_PAUSE: return wodPause (wDevID);
1727 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1728 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1729 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1730 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1731 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1732 case WODM_GETNUMDEVS: return numOutDev;
1733 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1734 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1735 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1736 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1737 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1738 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1739 case WODM_RESTART: return wodRestart (wDevID);
1740 case WODM_RESET: return wodReset (wDevID);
1742 case DRV_QUERYDSOUNDIFACE: return wodDsCreate(wDevID, (PIDSDRIVER*)dwParam1);
1743 default:
1744 FIXME("unknown message %d!\n", wMsg);
1746 return MMSYSERR_NOTSUPPORTED;
1749 /*======================================================================*
1750 * Low level DSOUND implementation *
1751 *======================================================================*/
1753 typedef struct IDsDriverImpl IDsDriverImpl;
1754 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1756 struct IDsDriverImpl
1758 /* IUnknown fields */
1759 ICOM_VFIELD(IDsDriver);
1760 DWORD ref;
1761 /* IDsDriverImpl fields */
1762 UINT wDevID;
1763 IDsDriverBufferImpl*primary;
1766 struct IDsDriverBufferImpl
1768 /* IUnknown fields */
1769 ICOM_VFIELD(IDsDriverBuffer);
1770 DWORD ref;
1771 /* IDsDriverBufferImpl fields */
1772 IDsDriverImpl* drv;
1773 DWORD buflen;
1776 static HRESULT DSDB_MapPrimary(IDsDriverBufferImpl *dsdb)
1778 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1779 if (!wwo->mapping) {
1780 wwo->mapping = mmap(NULL, wwo->maplen, PROT_WRITE, MAP_SHARED,
1781 wwo->ossdev->fd, 0);
1782 if (wwo->mapping == (LPBYTE)-1) {
1783 TRACE("(%p): Could not map sound device for direct access (%s)\n", dsdb, strerror(errno));
1784 return DSERR_GENERIC;
1786 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb, wwo->mapping, wwo->maplen);
1788 /* for some reason, es1371 and sblive! sometimes have junk in here.
1789 * clear it, or we get junk noise */
1790 /* some libc implementations are buggy: their memset reads from the buffer...
1791 * to work around it, we have to zero the block by hand. We don't do the expected:
1792 * memset(wwo->mapping,0, wwo->maplen);
1795 char* p1 = wwo->mapping;
1796 unsigned len = wwo->maplen;
1798 if (len >= 16) /* so we can have at least a 4 long area to store... */
1800 /* the mmap:ed value is (at least) dword aligned
1801 * so, start filling the complete unsigned long:s
1803 int b = len >> 2;
1804 unsigned long* p4 = (unsigned long*)p1;
1806 while (b--) *p4++ = 0;
1807 /* prepare for filling the rest */
1808 len &= 3;
1809 p1 = (unsigned char*)p4;
1811 /* in all cases, fill the remaining bytes */
1812 while (len-- != 0) *p1++ = 0;
1815 return DS_OK;
1818 static HRESULT DSDB_UnmapPrimary(IDsDriverBufferImpl *dsdb)
1820 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1821 if (wwo->mapping) {
1822 if (munmap(wwo->mapping, wwo->maplen) < 0) {
1823 ERR("(%p): Could not unmap sound device (errno=%d)\n", dsdb, errno);
1824 return DSERR_GENERIC;
1826 wwo->mapping = NULL;
1827 TRACE("(%p): sound device unmapped\n", dsdb);
1829 return DS_OK;
1832 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
1834 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1835 FIXME("(): stub!\n");
1836 return DSERR_UNSUPPORTED;
1839 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
1841 ICOM_THIS(IDsDriverBufferImpl,iface);
1842 This->ref++;
1843 return This->ref;
1846 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
1848 ICOM_THIS(IDsDriverBufferImpl,iface);
1849 if (--This->ref)
1850 return This->ref;
1851 if (This == This->drv->primary)
1852 This->drv->primary = NULL;
1853 DSDB_UnmapPrimary(This);
1854 HeapFree(GetProcessHeap(),0,This);
1855 return 0;
1858 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
1859 LPVOID*ppvAudio1,LPDWORD pdwLen1,
1860 LPVOID*ppvAudio2,LPDWORD pdwLen2,
1861 DWORD dwWritePosition,DWORD dwWriteLen,
1862 DWORD dwFlags)
1864 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1865 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
1866 * and that we don't support secondary buffers, this method will never be called */
1867 TRACE("(%p): stub\n",iface);
1868 return DSERR_UNSUPPORTED;
1871 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
1872 LPVOID pvAudio1,DWORD dwLen1,
1873 LPVOID pvAudio2,DWORD dwLen2)
1875 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1876 TRACE("(%p): stub\n",iface);
1877 return DSERR_UNSUPPORTED;
1880 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
1881 LPWAVEFORMATEX pwfx)
1883 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1885 TRACE("(%p,%p)\n",iface,pwfx);
1886 /* On our request (GetDriverDesc flags), DirectSound has by now used
1887 * waveOutClose/waveOutOpen to set the format...
1888 * unfortunately, this means our mmap() is now gone...
1889 * so we need to somehow signal to our DirectSound implementation
1890 * that it should completely recreate this HW buffer...
1891 * this unexpected error code should do the trick... */
1892 return DSERR_BUFFERLOST;
1895 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
1897 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1898 TRACE("(%p,%ld): stub\n",iface,dwFreq);
1899 return DSERR_UNSUPPORTED;
1902 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
1904 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1905 FIXME("(%p,%p): stub!\n",iface,pVolPan);
1906 return DSERR_UNSUPPORTED;
1909 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
1911 /* ICOM_THIS(IDsDriverImpl,iface); */
1912 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
1913 return DSERR_UNSUPPORTED;
1916 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
1917 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
1919 ICOM_THIS(IDsDriverBufferImpl,iface);
1920 count_info info;
1921 DWORD ptr;
1923 TRACE("(%p)\n",iface);
1924 if (WOutDev[This->drv->wDevID].state == WINE_WS_CLOSED) {
1925 ERR("device not open, but accessing?\n");
1926 return DSERR_UNINITIALIZED;
1928 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
1929 ERR("ioctl failed (%d)\n", errno);
1930 return DSERR_GENERIC;
1932 ptr = info.ptr & ~3; /* align the pointer, just in case */
1933 if (lpdwPlay) *lpdwPlay = ptr;
1934 if (lpdwWrite) {
1935 /* add some safety margin (not strictly necessary, but...) */
1936 if (WOutDev[This->drv->wDevID].ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1937 *lpdwWrite = ptr + 32;
1938 else
1939 *lpdwWrite = ptr + WOutDev[This->drv->wDevID].dwFragmentSize;
1940 while (*lpdwWrite > This->buflen)
1941 *lpdwWrite -= This->buflen;
1943 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay?*lpdwPlay:0, lpdwWrite?*lpdwWrite:0);
1944 return DS_OK;
1947 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
1949 ICOM_THIS(IDsDriverBufferImpl,iface);
1950 int enable = PCM_ENABLE_OUTPUT;
1951 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
1952 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1953 ERR("ioctl failed (%d)\n", errno);
1954 return DSERR_GENERIC;
1956 return DS_OK;
1959 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
1961 ICOM_THIS(IDsDriverBufferImpl,iface);
1962 int enable = 0;
1963 TRACE("(%p)\n",iface);
1964 /* no more playing */
1965 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1966 ERR("ioctl failed (%d)\n", errno);
1967 return DSERR_GENERIC;
1969 #if 0
1970 /* the play position must be reset to the beginning of the buffer */
1971 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_RESET, 0) < 0) {
1972 ERR("ioctl failed (%d)\n", errno);
1973 return DSERR_GENERIC;
1975 #endif
1976 /* Most OSS drivers just can't stop the playback without closing the device...
1977 * so we need to somehow signal to our DirectSound implementation
1978 * that it should completely recreate this HW buffer...
1979 * this unexpected error code should do the trick... */
1980 return DSERR_BUFFERLOST;
1983 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
1985 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1986 IDsDriverBufferImpl_QueryInterface,
1987 IDsDriverBufferImpl_AddRef,
1988 IDsDriverBufferImpl_Release,
1989 IDsDriverBufferImpl_Lock,
1990 IDsDriverBufferImpl_Unlock,
1991 IDsDriverBufferImpl_SetFormat,
1992 IDsDriverBufferImpl_SetFrequency,
1993 IDsDriverBufferImpl_SetVolumePan,
1994 IDsDriverBufferImpl_SetPosition,
1995 IDsDriverBufferImpl_GetPosition,
1996 IDsDriverBufferImpl_Play,
1997 IDsDriverBufferImpl_Stop
2000 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
2002 /* ICOM_THIS(IDsDriverImpl,iface); */
2003 FIXME("(%p): stub!\n",iface);
2004 return DSERR_UNSUPPORTED;
2007 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
2009 ICOM_THIS(IDsDriverImpl,iface);
2010 This->ref++;
2011 return This->ref;
2014 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
2016 ICOM_THIS(IDsDriverImpl,iface);
2017 if (--This->ref)
2018 return This->ref;
2019 HeapFree(GetProcessHeap(),0,This);
2020 return 0;
2023 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
2025 ICOM_THIS(IDsDriverImpl,iface);
2026 TRACE("(%p,%p)\n",iface,pDesc);
2027 pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
2028 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
2029 strcpy(pDesc->szDesc,"WineOSS DirectSound Driver");
2030 strcpy(pDesc->szDrvName,"wineoss.drv");
2031 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
2032 pDesc->wVxdId = 0;
2033 pDesc->wReserved = 0;
2034 pDesc->ulDeviceNum = This->wDevID;
2035 pDesc->dwHeapType = DSDHEAP_NOHEAP;
2036 pDesc->pvDirectDrawHeap = NULL;
2037 pDesc->dwMemStartAddress = 0;
2038 pDesc->dwMemEndAddress = 0;
2039 pDesc->dwMemAllocExtra = 0;
2040 pDesc->pvReserved1 = NULL;
2041 pDesc->pvReserved2 = NULL;
2042 return DS_OK;
2045 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
2047 ICOM_THIS(IDsDriverImpl,iface);
2048 int enable = 0;
2050 TRACE("(%p)\n",iface);
2051 /* make sure the card doesn't start playing before we want it to */
2052 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2053 ERR("ioctl failed (%d)\n", errno);
2054 return DSERR_GENERIC;
2056 return DS_OK;
2059 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
2061 ICOM_THIS(IDsDriverImpl,iface);
2062 TRACE("(%p)\n",iface);
2063 if (This->primary) {
2064 ERR("problem with DirectSound: primary not released\n");
2065 return DSERR_GENERIC;
2067 return DS_OK;
2070 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
2072 /* ICOM_THIS(IDsDriverImpl,iface); */
2073 TRACE("(%p,%p)\n",iface,pCaps);
2074 memset(pCaps, 0, sizeof(*pCaps));
2075 /* FIXME: need to check actual capabilities */
2076 pCaps->dwFlags = DSCAPS_PRIMARYMONO | DSCAPS_PRIMARYSTEREO |
2077 DSCAPS_PRIMARY8BIT | DSCAPS_PRIMARY16BIT;
2078 pCaps->dwPrimaryBuffers = 1;
2079 /* the other fields only apply to secondary buffers, which we don't support
2080 * (unless we want to mess with wavetable synthesizers and MIDI) */
2081 return DS_OK;
2084 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
2085 LPWAVEFORMATEX pwfx,
2086 DWORD dwFlags, DWORD dwCardAddress,
2087 LPDWORD pdwcbBufferSize,
2088 LPBYTE *ppbBuffer,
2089 LPVOID *ppvObj)
2091 ICOM_THIS(IDsDriverImpl,iface);
2092 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
2093 HRESULT err;
2094 audio_buf_info info;
2095 int enable = 0;
2097 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
2098 /* we only support primary buffers */
2099 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
2100 return DSERR_UNSUPPORTED;
2101 if (This->primary)
2102 return DSERR_ALLOCATED;
2103 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
2104 return DSERR_CONTROLUNAVAIL;
2106 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
2107 if (*ippdsdb == NULL)
2108 return DSERR_OUTOFMEMORY;
2109 ICOM_VTBL(*ippdsdb) = &dsdbvt;
2110 (*ippdsdb)->ref = 1;
2111 (*ippdsdb)->drv = This;
2113 /* check how big the DMA buffer is now */
2114 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2115 ERR("ioctl failed (%d)\n", errno);
2116 HeapFree(GetProcessHeap(),0,*ippdsdb);
2117 *ippdsdb = NULL;
2118 return DSERR_GENERIC;
2120 WOutDev[This->wDevID].maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
2122 /* map the DMA buffer */
2123 err = DSDB_MapPrimary(*ippdsdb);
2124 if (err != DS_OK) {
2125 HeapFree(GetProcessHeap(),0,*ippdsdb);
2126 *ippdsdb = NULL;
2127 return err;
2130 /* primary buffer is ready to go */
2131 *pdwcbBufferSize = WOutDev[This->wDevID].maplen;
2132 *ppbBuffer = WOutDev[This->wDevID].mapping;
2134 /* some drivers need some extra nudging after mapping */
2135 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2136 ERR("ioctl failed (%d)\n", errno);
2137 return DSERR_GENERIC;
2140 This->primary = *ippdsdb;
2142 return DS_OK;
2145 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
2146 PIDSDRIVERBUFFER pBuffer,
2147 LPVOID *ppvObj)
2149 /* ICOM_THIS(IDsDriverImpl,iface); */
2150 TRACE("(%p,%p): stub\n",iface,pBuffer);
2151 return DSERR_INVALIDCALL;
2154 static ICOM_VTABLE(IDsDriver) dsdvt =
2156 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2157 IDsDriverImpl_QueryInterface,
2158 IDsDriverImpl_AddRef,
2159 IDsDriverImpl_Release,
2160 IDsDriverImpl_GetDriverDesc,
2161 IDsDriverImpl_Open,
2162 IDsDriverImpl_Close,
2163 IDsDriverImpl_GetCaps,
2164 IDsDriverImpl_CreateSoundBuffer,
2165 IDsDriverImpl_DuplicateSoundBuffer
2168 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2170 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
2172 /* the HAL isn't much better than the HEL if we can't do mmap() */
2173 if (!(WOutDev[wDevID].ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
2174 ERR("DirectSound flag not set\n");
2175 MESSAGE("This sound card's driver does not support direct access\n");
2176 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2177 return MMSYSERR_NOTSUPPORTED;
2180 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
2181 if (!*idrv)
2182 return MMSYSERR_NOMEM;
2183 ICOM_VTBL(*idrv) = &dsdvt;
2184 (*idrv)->ref = 1;
2186 (*idrv)->wDevID = wDevID;
2187 (*idrv)->primary = NULL;
2188 return MMSYSERR_NOERROR;
2191 /*======================================================================*
2192 * Low level WAVE IN implementation *
2193 *======================================================================*/
2195 /**************************************************************************
2196 * widNotifyClient [internal]
2198 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2200 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
2202 switch (wMsg) {
2203 case WIM_OPEN:
2204 case WIM_CLOSE:
2205 case WIM_DATA:
2206 if (wwi->wFlags != DCB_NULL &&
2207 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2208 (HDRVR)wwi->waveDesc.hWave, wMsg,
2209 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2210 WARN("can't notify client !\n");
2211 return MMSYSERR_ERROR;
2213 break;
2214 default:
2215 FIXME("Unknown callback message %u\n", wMsg);
2216 return MMSYSERR_INVALPARAM;
2218 return MMSYSERR_NOERROR;
2221 /**************************************************************************
2222 * widGetDevCaps [internal]
2224 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
2226 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
2228 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2230 if (wDevID >= numInDev) {
2231 TRACE("numOutDev reached !\n");
2232 return MMSYSERR_BADDEVICEID;
2235 memcpy(lpCaps, &WInDev[wDevID].ossdev->in_caps, min(dwSize, sizeof(*lpCaps)));
2236 return MMSYSERR_NOERROR;
2239 /**************************************************************************
2240 * widRecorder [internal]
2242 static DWORD CALLBACK widRecorder(LPVOID pmt)
2244 WORD uDevID = (DWORD)pmt;
2245 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2246 WAVEHDR* lpWaveHdr;
2247 DWORD dwSleepTime;
2248 DWORD bytesRead;
2249 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2250 LPVOID pOffset = buffer;
2251 audio_buf_info info;
2252 int xs;
2253 enum win_wm_message msg;
2254 DWORD param;
2255 HANDLE ev;
2257 wwi->state = WINE_WS_STOPPED;
2258 wwi->dwTotalRecorded = 0;
2260 SetEvent(wwi->hStartUpEvent);
2262 /* the soundblaster live needs a micro wake to get its recording started
2263 * (or GETISPACE will have 0 frags all the time)
2265 read(wwi->ossdev->fd, &xs, 4);
2267 /* make sleep time to be # of ms to output a fragment */
2268 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->format.wf.nAvgBytesPerSec;
2269 TRACE("sleeptime=%ld ms\n", dwSleepTime);
2271 for (;;) {
2272 /* wait for dwSleepTime or an event in thread's queue */
2273 /* FIXME: could improve wait time depending on queue state,
2274 * ie, number of queued fragments
2277 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2279 lpWaveHdr = wwi->lpQueuePtr;
2281 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info);
2282 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2284 /* read all the fragments accumulated so far */
2285 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2287 info.fragments --;
2289 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2291 /* directly read fragment in wavehdr */
2292 bytesRead = read(wwi->ossdev->fd,
2293 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2294 wwi->dwFragmentSize);
2296 TRACE("bytesRead=%ld (direct)\n", bytesRead);
2297 if (bytesRead != (DWORD) -1)
2299 /* update number of bytes recorded in current buffer and by this device */
2300 lpWaveHdr->dwBytesRecorded += bytesRead;
2301 wwi->dwTotalRecorded += bytesRead;
2303 /* buffer is full. notify client */
2304 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2306 /* must copy the value of next waveHdr, because we have no idea of what
2307 * will be done with the content of lpWaveHdr in callback
2309 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2311 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2312 lpWaveHdr->dwFlags |= WHDR_DONE;
2314 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2315 lpWaveHdr = wwi->lpQueuePtr = lpNext;
2319 else
2321 /* read the fragment in a local buffer */
2322 bytesRead = read(wwi->ossdev->fd, buffer, wwi->dwFragmentSize);
2323 pOffset = buffer;
2325 TRACE("bytesRead=%ld (local)\n", bytesRead);
2327 /* copy data in client buffers */
2328 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2330 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2332 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2333 pOffset,
2334 dwToCopy);
2336 /* update number of bytes recorded in current buffer and by this device */
2337 lpWaveHdr->dwBytesRecorded += dwToCopy;
2338 wwi->dwTotalRecorded += dwToCopy;
2339 bytesRead -= dwToCopy;
2340 pOffset += dwToCopy;
2342 /* client buffer is full. notify client */
2343 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2345 /* must copy the value of next waveHdr, because we have no idea of what
2346 * will be done with the content of lpWaveHdr in callback
2348 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2349 TRACE("lpNext=%p\n", lpNext);
2351 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2352 lpWaveHdr->dwFlags |= WHDR_DONE;
2354 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2356 wwi->lpQueuePtr = lpWaveHdr = lpNext;
2357 if (!lpNext && bytesRead) {
2358 /* no more buffer to copy data to, but we did read more.
2359 * what hasn't been copied will be dropped
2361 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
2362 wwi->lpQueuePtr = NULL;
2363 break;
2371 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2373 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2376 TRACE("msg=0x%x param=0x%lx\n", msg, param);
2377 switch (msg) {
2378 case WINE_WM_PAUSING:
2379 wwi->state = WINE_WS_PAUSED;
2380 /*FIXME("Device should stop recording\n");*/
2381 SetEvent(ev);
2382 break;
2383 case WINE_WM_RESTARTING:
2385 int enable = PCM_ENABLE_INPUT;
2386 wwi->state = WINE_WS_PLAYING;
2388 if (wwi->ossdev->bTriggerSupport)
2390 /* start the recording */
2391 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2393 ERR("ioctl(SNDCTL_DSP_SETTRIGGER) failed (%d)\n", errno);
2396 else
2398 unsigned char data[4];
2399 /* read 4 bytes to start the recording */
2400 read(wwi->ossdev->fd, data, 4);
2403 SetEvent(ev);
2404 break;
2406 case WINE_WM_HEADER:
2407 lpWaveHdr = (LPWAVEHDR)param;
2408 lpWaveHdr->lpNext = 0;
2410 /* insert buffer at the end of queue */
2412 LPWAVEHDR* wh;
2413 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2414 *wh = lpWaveHdr;
2416 break;
2417 case WINE_WM_RESETTING:
2418 wwi->state = WINE_WS_STOPPED;
2419 /* return all buffers to the app */
2420 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2421 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2422 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2423 lpWaveHdr->dwFlags |= WHDR_DONE;
2425 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2427 wwi->lpQueuePtr = NULL;
2428 SetEvent(ev);
2429 break;
2430 case WINE_WM_CLOSING:
2431 wwi->hThread = 0;
2432 wwi->state = WINE_WS_CLOSED;
2433 SetEvent(ev);
2434 HeapFree(GetProcessHeap(), 0, buffer);
2435 ExitThread(0);
2436 /* shouldn't go here */
2437 default:
2438 FIXME("unknown message %d\n", msg);
2439 break;
2443 ExitThread(0);
2444 /* just for not generating compilation warnings... should never be executed */
2445 return 0;
2449 /**************************************************************************
2450 * widOpen [internal]
2452 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2454 WINE_WAVEIN* wwi;
2455 int fragment_size;
2456 int audio_fragment;
2457 DWORD ret;
2459 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
2460 if (lpDesc == NULL) {
2461 WARN("Invalid Parameter !\n");
2462 return MMSYSERR_INVALPARAM;
2464 if (wDevID >= numInDev) return MMSYSERR_BADDEVICEID;
2466 /* only PCM format is supported so far... */
2467 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
2468 lpDesc->lpFormat->nChannels == 0 ||
2469 lpDesc->lpFormat->nSamplesPerSec == 0) {
2470 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2471 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2472 lpDesc->lpFormat->nSamplesPerSec);
2473 return WAVERR_BADFORMAT;
2476 if (dwFlags & WAVE_FORMAT_QUERY) {
2477 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2478 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2479 lpDesc->lpFormat->nSamplesPerSec);
2480 return MMSYSERR_NOERROR;
2483 wwi = &WInDev[wDevID];
2485 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2486 /* This is actually hand tuned to work so that my SB Live:
2487 * - does not skip
2488 * - does not buffer too much
2489 * when sending with the Shoutcast winamp plugin
2491 /* 15 fragments max, 2^10 = 1024 bytes per fragment */
2492 audio_fragment = 0x000F000A;
2493 ret = OSS_OpenDevice(wwi->ossdev, O_RDONLY, &audio_fragment,
2495 lpDesc->lpFormat->nSamplesPerSec,
2496 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
2497 (lpDesc->lpFormat->wBitsPerSample == 16)
2498 ? AFMT_S16_LE : AFMT_U8);
2499 if (ret != 0) return ret;
2500 wwi->state = WINE_WS_STOPPED;
2502 if (wwi->lpQueuePtr) {
2503 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2504 wwi->lpQueuePtr = NULL;
2506 wwi->dwTotalRecorded = 0;
2507 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2509 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2510 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
2512 if (wwi->format.wBitsPerSample == 0) {
2513 WARN("Resetting zeroed wBitsPerSample\n");
2514 wwi->format.wBitsPerSample = 8 *
2515 (wwi->format.wf.nAvgBytesPerSec /
2516 wwi->format.wf.nSamplesPerSec) /
2517 wwi->format.wf.nChannels;
2520 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETBLKSIZE, &fragment_size);
2521 if (fragment_size == -1) {
2522 WARN("IOCTL can't 'SNDCTL_DSP_GETBLKSIZE' !\n");
2523 OSS_CloseDevice(wwi->ossdev);
2524 wwi->state = WINE_WS_CLOSED;
2525 return MMSYSERR_NOTENABLED;
2527 wwi->dwFragmentSize = fragment_size;
2529 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2530 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
2531 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
2532 wwi->format.wf.nBlockAlign);
2534 OSS_InitRingMessage(&wwi->msgRing);
2536 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
2537 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
2538 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2539 CloseHandle(wwi->hStartUpEvent);
2540 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2542 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2545 /**************************************************************************
2546 * widClose [internal]
2548 static DWORD widClose(WORD wDevID)
2550 WINE_WAVEIN* wwi;
2552 TRACE("(%u);\n", wDevID);
2553 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2554 WARN("can't close !\n");
2555 return MMSYSERR_INVALHANDLE;
2558 wwi = &WInDev[wDevID];
2560 if (wwi->lpQueuePtr != NULL) {
2561 WARN("still buffers open !\n");
2562 return WAVERR_STILLPLAYING;
2565 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
2566 OSS_CloseDevice(wwi->ossdev);
2567 wwi->state = WINE_WS_CLOSED;
2568 wwi->dwFragmentSize = 0;
2569 OSS_DestroyRingMessage(&wwi->msgRing);
2570 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
2573 /**************************************************************************
2574 * widAddBuffer [internal]
2576 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2578 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2580 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2581 WARN("can't do it !\n");
2582 return MMSYSERR_INVALHANDLE;
2584 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2585 TRACE("never been prepared !\n");
2586 return WAVERR_UNPREPARED;
2588 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2589 TRACE("header already in use !\n");
2590 return WAVERR_STILLPLAYING;
2593 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2594 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2595 lpWaveHdr->dwBytesRecorded = 0;
2596 lpWaveHdr->lpNext = NULL;
2598 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2599 return MMSYSERR_NOERROR;
2602 /**************************************************************************
2603 * widPrepare [internal]
2605 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2607 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2609 if (wDevID >= numInDev) return MMSYSERR_INVALHANDLE;
2611 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2612 return WAVERR_STILLPLAYING;
2614 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2615 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2616 lpWaveHdr->dwBytesRecorded = 0;
2617 TRACE("header prepared !\n");
2618 return MMSYSERR_NOERROR;
2621 /**************************************************************************
2622 * widUnprepare [internal]
2624 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2626 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2627 if (wDevID >= numInDev) return MMSYSERR_INVALHANDLE;
2629 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2630 return WAVERR_STILLPLAYING;
2632 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
2633 lpWaveHdr->dwFlags |= WHDR_DONE;
2635 return MMSYSERR_NOERROR;
2638 /**************************************************************************
2639 * widStart [internal]
2641 static DWORD widStart(WORD wDevID)
2643 TRACE("(%u);\n", wDevID);
2644 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2645 WARN("can't start recording !\n");
2646 return MMSYSERR_INVALHANDLE;
2649 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2650 return MMSYSERR_NOERROR;
2653 /**************************************************************************
2654 * widStop [internal]
2656 static DWORD widStop(WORD wDevID)
2658 TRACE("(%u);\n", wDevID);
2659 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2660 WARN("can't stop !\n");
2661 return MMSYSERR_INVALHANDLE;
2663 /* FIXME: reset aint stop */
2664 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2666 return MMSYSERR_NOERROR;
2669 /**************************************************************************
2670 * widReset [internal]
2672 static DWORD widReset(WORD wDevID)
2674 TRACE("(%u);\n", wDevID);
2675 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2676 WARN("can't reset !\n");
2677 return MMSYSERR_INVALHANDLE;
2679 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2680 return MMSYSERR_NOERROR;
2683 /**************************************************************************
2684 * widGetPosition [internal]
2686 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2688 int time;
2689 WINE_WAVEIN* wwi;
2691 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
2693 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2694 WARN("can't get pos !\n");
2695 return MMSYSERR_INVALHANDLE;
2697 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
2699 wwi = &WInDev[wDevID];
2701 TRACE("wType=%04X !\n", lpTime->wType);
2702 TRACE("wBitsPerSample=%u\n", wwi->format.wBitsPerSample);
2703 TRACE("nSamplesPerSec=%lu\n", wwi->format.wf.nSamplesPerSec);
2704 TRACE("nChannels=%u\n", wwi->format.wf.nChannels);
2705 TRACE("nAvgBytesPerSec=%lu\n", wwi->format.wf.nAvgBytesPerSec);
2706 switch (lpTime->wType) {
2707 case TIME_BYTES:
2708 lpTime->u.cb = wwi->dwTotalRecorded;
2709 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
2710 break;
2711 case TIME_SAMPLES:
2712 lpTime->u.sample = wwi->dwTotalRecorded * 8 /
2713 wwi->format.wBitsPerSample;
2714 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
2715 break;
2716 case TIME_SMPTE:
2717 time = wwi->dwTotalRecorded /
2718 (wwi->format.wf.nAvgBytesPerSec / 1000);
2719 lpTime->u.smpte.hour = time / 108000;
2720 time -= lpTime->u.smpte.hour * 108000;
2721 lpTime->u.smpte.min = time / 1800;
2722 time -= lpTime->u.smpte.min * 1800;
2723 lpTime->u.smpte.sec = time / 30;
2724 time -= lpTime->u.smpte.sec * 30;
2725 lpTime->u.smpte.frame = time;
2726 lpTime->u.smpte.fps = 30;
2727 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
2728 lpTime->u.smpte.hour, lpTime->u.smpte.min,
2729 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
2730 break;
2731 case TIME_MS:
2732 lpTime->u.ms = wwi->dwTotalRecorded /
2733 (wwi->format.wf.nAvgBytesPerSec / 1000);
2734 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
2735 break;
2736 default:
2737 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime->wType);
2738 lpTime->wType = TIME_MS;
2740 return MMSYSERR_NOERROR;
2743 /**************************************************************************
2744 * widMessage (WINEOSS.6)
2746 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2747 DWORD dwParam1, DWORD dwParam2)
2749 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2750 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2752 switch (wMsg) {
2753 case DRVM_INIT:
2754 case DRVM_EXIT:
2755 case DRVM_ENABLE:
2756 case DRVM_DISABLE:
2757 /* FIXME: Pretend this is supported */
2758 return 0;
2759 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2760 case WIDM_CLOSE: return widClose (wDevID);
2761 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2762 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2763 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2764 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
2765 case WIDM_GETNUMDEVS: return numInDev;
2766 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
2767 case WIDM_RESET: return widReset (wDevID);
2768 case WIDM_START: return widStart (wDevID);
2769 case WIDM_STOP: return widStop (wDevID);
2770 default:
2771 FIXME("unknown message %u!\n", wMsg);
2773 return MMSYSERR_NOTSUPPORTED;
2776 #else /* !HAVE_OSS */
2778 /**************************************************************************
2779 * wodMessage (WINEOSS.7)
2781 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2782 DWORD dwParam1, DWORD dwParam2)
2784 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2785 return MMSYSERR_NOTENABLED;
2788 /**************************************************************************
2789 * widMessage (WINEOSS.6)
2791 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2792 DWORD dwParam1, DWORD dwParam2)
2794 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2795 return MMSYSERR_NOTENABLED;
2798 #endif /* HAVE_OSS */