Moved mode setting out of .spec file into Makefile.
[wine/gsoc_dplay.git] / dlls / winmm / wineoss / audio.c
blob10e95cbfc336a8961e4a6905ffd9d9b69188c2e8
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)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * FIXME:
25 * pause in waveOut does not work correctly in loop mode
26 * experimental full duplex mode
27 * only one sound card is currently supported
30 /*#define EMULATE_SB16*/
32 /* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
33 #define USE_PIPE_SYNC
35 /* an exact wodGetPosition is usually not worth the extra context switches,
36 * as we're going to have near fragment accuracy anyway */
37 /* #define EXACT_WODPOSITION */
39 #include "config.h"
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47 #include <errno.h>
48 #include <fcntl.h>
49 #ifdef HAVE_SYS_IOCTL_H
50 # include <sys/ioctl.h>
51 #endif
52 #ifdef HAVE_SYS_MMAN_H
53 # include <sys/mman.h>
54 #endif
55 #ifdef HAVE_SYS_POLL_H
56 # include <sys/poll.h>
57 #endif
59 #include "windef.h"
60 #include "wingdi.h"
61 #include "winerror.h"
62 #include "wine/winuser16.h"
63 #include "mmddk.h"
64 #include "dsound.h"
65 #include "dsdriver.h"
66 #include "oss.h"
67 #include "wine/debug.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(wave);
71 /* Allow 1% deviation for sample rates (some ES137x cards) */
72 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
74 #ifdef HAVE_OSS
76 #define MAX_WAVEDRV (1)
78 /* state diagram for waveOut writing:
80 * +---------+-------------+---------------+---------------------------------+
81 * | state | function | event | new state |
82 * +---------+-------------+---------------+---------------------------------+
83 * | | open() | | STOPPED |
84 * | PAUSED | write() | | PAUSED |
85 * | STOPPED | write() | <thrd create> | PLAYING |
86 * | PLAYING | write() | HEADER | PLAYING |
87 * | (other) | write() | <error> | |
88 * | (any) | pause() | PAUSING | PAUSED |
89 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
90 * | (any) | reset() | RESETTING | STOPPED |
91 * | (any) | close() | CLOSING | CLOSED |
92 * +---------+-------------+---------------+---------------------------------+
95 /* states of the playing device */
96 #define WINE_WS_PLAYING 0
97 #define WINE_WS_PAUSED 1
98 #define WINE_WS_STOPPED 2
99 #define WINE_WS_CLOSED 3
101 /* events to be send to device */
102 enum win_wm_message {
103 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
104 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING
107 #ifdef USE_PIPE_SYNC
108 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
109 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
110 #define RESET_OMR(omr) do { } while (0)
111 #define WAIT_OMR(omr, sleep) \
112 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
113 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
114 #else
115 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
116 #define CLEAR_OMR(omr) do { } while (0)
117 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
118 #define WAIT_OMR(omr, sleep) \
119 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
120 #endif
122 typedef struct {
123 enum win_wm_message msg; /* message identifier */
124 DWORD param; /* parameter for this message */
125 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
126 } OSS_MSG;
128 /* implement an in-process message ring for better performance
129 * (compared to passing thru the server)
130 * this ring will be used by the input (resp output) record (resp playback) routine
132 typedef struct {
133 /* FIXME: this could be made a dynamically growing array (if needed) */
134 /* maybe it's needed, a Humongous game manages to transmit 128 messages at once at startup */
135 #define OSS_RING_BUFFER_SIZE 192
136 OSS_MSG messages[OSS_RING_BUFFER_SIZE];
137 int msg_tosave;
138 int msg_toget;
139 #ifdef USE_PIPE_SYNC
140 int msg_pipe[2];
141 #else
142 HANDLE msg_event;
143 #endif
144 CRITICAL_SECTION msg_crst;
145 } OSS_MSG_RING;
147 typedef struct {
148 int unixdev;
149 volatile int state; /* one of the WINE_WS_ manifest constants */
150 WAVEOPENDESC waveDesc;
151 WORD wFlags;
152 PCMWAVEFORMAT format;
154 /* OSS information */
155 DWORD dwFragmentSize; /* size of OSS buffer fragment */
156 DWORD dwBufferSize; /* size of whole OSS buffer in bytes */
157 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
158 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
159 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
161 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
162 DWORD dwLoops; /* private copy of loop counter */
164 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
165 DWORD dwWrittenTotal; /* number of bytes written to OSS buffer since opening */
166 BOOL bNeedPost; /* whether audio still needs to be physically started */
168 /* synchronization stuff */
169 HANDLE hStartUpEvent;
170 HANDLE hThread;
171 DWORD dwThreadID;
172 OSS_MSG_RING msgRing;
174 /* DirectSound stuff */
175 LPBYTE mapping;
176 DWORD maplen;
177 } WINE_WAVEOUT;
179 typedef struct {
180 int unixdev;
181 volatile int state;
182 DWORD dwFragmentSize; /* OpenSound '/dev/dsp' give us that size */
183 WAVEOPENDESC waveDesc;
184 WORD wFlags;
185 PCMWAVEFORMAT format;
186 LPWAVEHDR lpQueuePtr;
187 DWORD dwTotalRecorded;
188 BOOL bTriggerSupport;
190 /* synchronization stuff */
191 HANDLE hThread;
192 DWORD dwThreadID;
193 HANDLE hStartUpEvent;
194 OSS_MSG_RING msgRing;
195 } WINE_WAVEIN;
197 static WINE_WAVEOUT WOutDev [MAX_WAVEDRV];
198 static WINE_WAVEIN WInDev [MAX_WAVEDRV];
199 static unsigned numOutDev;
200 static unsigned numInDev;
202 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
204 /* These strings used only for tracing */
205 static const char *wodPlayerCmdString[] = {
206 "WINE_WM_PAUSING",
207 "WINE_WM_RESTARTING",
208 "WINE_WM_RESETTING",
209 "WINE_WM_HEADER",
210 "WINE_WM_UPDATE",
211 "WINE_WM_BREAKLOOP",
212 "WINE_WM_CLOSING",
215 /*======================================================================*
216 * Low level WAVE implementation *
217 *======================================================================*/
218 #define USE_FULLDUPLEX
220 typedef struct tagOSS_DEVICE {
221 const char* dev_name;
222 const char* mixer_name;
223 unsigned open_count;
224 WAVEOUTCAPSA out_caps;
225 WAVEINCAPSA in_caps;
226 #ifdef USE_FULLDUPLEX
227 unsigned open_access;
228 int fd;
229 DWORD owner_tid;
230 unsigned sample_rate;
231 unsigned stereo;
232 unsigned format;
233 unsigned audio_fragment;
234 #endif
235 BOOL full_duplex;
236 } OSS_DEVICE;
238 OSS_DEVICE OSS_Devices[MAX_WAVEDRV];
240 /******************************************************************
241 * OSS_OpenDevice
243 * since OSS has poor capabilities in full duplex, we try here to let a program
244 * open the device for both waveout and wavein streams...
245 * this is hackish, but it's the way OSS interface is done...
247 static DWORD OSS_OpenDevice(unsigned wDevID, int* pfd, unsigned req_access,
248 int* frag, int sample_rate, int stereo, int fmt)
250 OSS_DEVICE* ossdev;
251 int val;
252 int fd = -1;
254 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_BADDEVICEID;
255 ossdev = &OSS_Devices[wDevID];
257 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
258 req_access = O_RDWR;
260 /* FIXME: race with close */
261 if (ossdev->open_count == 0)
263 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
265 if ((fd = open(ossdev->dev_name, req_access|O_NDELAY, 0)) == -1)
267 WARN("Couldn't open out %s (%s)\n", ossdev->dev_name, strerror(errno));
268 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
270 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
271 /* turn full duplex on if it has been requested */
272 if (req_access == O_RDWR && ossdev->full_duplex)
273 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
275 if (frag) ioctl(fd, SNDCTL_DSP_SETFRAGMENT, frag);
277 /* First size and stereo then samplerate */
278 if (fmt)
280 val = fmt;
281 ioctl(fd, SNDCTL_DSP_SETFMT, &val);
282 if (val != fmt)
283 ERR("Can't set format to %d (%d)\n", fmt, val);
285 if (stereo)
287 val = stereo;
288 ioctl(fd, SNDCTL_DSP_STEREO, &val);
289 if (val != stereo)
290 ERR("Can't set stereo to %u (%d)\n", stereo, val);
292 if (sample_rate)
294 val = sample_rate;
295 ioctl(fd, SNDCTL_DSP_SPEED, &sample_rate);
296 if (!NEAR_MATCH(val, sample_rate))
297 ERR("Can't set sample_rate to %u (%d)\n", sample_rate, val);
299 #ifdef USE_FULLDUPLEX
300 ossdev->audio_fragment = (frag) ? *frag : 0;
301 ossdev->sample_rate = sample_rate;
302 ossdev->stereo = stereo;
303 ossdev->format = fmt;
304 ossdev->open_access = req_access;
305 ossdev->owner_tid = GetCurrentThreadId();
306 ossdev->fd = fd;
307 #endif
309 else
311 #ifdef USE_FULLDUPLEX
312 /* check we really open with the same parameters */
313 if (ossdev->open_access != req_access)
315 WARN("Mismatch in access...\n");
316 return WAVERR_BADFORMAT;
318 /* FIXME: if really needed, we could do, in this case, on the fly
319 * PCM conversion (using the MSACM ad hoc driver)
321 if (ossdev->audio_fragment != (frag ? *frag : 0) ||
322 ossdev->sample_rate != sample_rate ||
323 ossdev->stereo != stereo ||
324 ossdev->format != fmt)
326 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
327 "OSS doesn't allow us different parameters\n"
328 "audio_frag(%x/%x) sample_rate(%d/%d) stereo(%d/%d) fmt(%d/%d)\n",
329 ossdev->audio_fragment, frag ? *frag : 0,
330 ossdev->sample_rate, sample_rate,
331 ossdev->stereo, stereo,
332 ossdev->format, fmt);
333 return WAVERR_BADFORMAT;
335 if (GetCurrentThreadId() != ossdev->owner_tid)
337 WARN("Another thread is trying to access audio...\n");
338 return MMSYSERR_ERROR;
340 #endif
343 #ifdef USE_FULLDUPLEX
344 ossdev->open_count++;
345 #endif
346 *pfd = fd;
347 return MMSYSERR_NOERROR;
350 /******************************************************************
351 * OSS_CloseDevice
355 static void OSS_CloseDevice(unsigned wDevID, int fd)
357 OSS_DEVICE* ossdev;
359 if (wDevID >= MAX_WAVEDRV) return;
360 #ifdef USE_FULLDUPLEX
361 ossdev = &OSS_Devices[wDevID];
362 if (fd != ossdev->fd) FIXME("What the heck????\n");
363 if (--ossdev->open_count == 0) close(ossdev->fd);
364 #else
365 /* wDevID: is not used yet, we handle only one global device /dev/dsp */
366 close(fd);
367 #endif
370 /******************************************************************
371 * OSS_WaveOutInit
375 static void OSS_WaveOutInit(unsigned devID, OSS_DEVICE* ossdev)
377 int audio;
378 int smplrate;
379 int samplesize = 16;
380 int dsp_stereo = 1;
381 int bytespersmpl;
382 int caps;
383 int mask;
385 WOutDev[devID].unixdev = -1;
386 memset(&ossdev->out_caps, 0, sizeof(ossdev->out_caps));
388 if (OSS_OpenDevice(devID, &audio, O_WRONLY, NULL, 0, 0, 0) != 0) return;
389 numOutDev++;
391 ioctl(audio, SNDCTL_DSP_RESET, 0);
393 /* FIXME: some programs compare this string against the content of the registry
394 * for MM drivers. The names have to match in order for the program to work
395 * (e.g. MS win9x mplayer.exe)
397 #ifdef EMULATE_SB16
398 ossdev->out_caps.wMid = 0x0002;
399 ossdev->out_caps.wPid = 0x0104;
400 strcpy(ossdev->out_caps.szPname, "SB16 Wave Out");
401 #else
402 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
403 ossdev->out_caps.wPid = 0x0001; /* Product ID */
404 /* strcpy(ossdev->out_caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
405 strcpy(ossdev->out_caps.szPname, "CS4236/37/38");
406 #endif
407 ossdev->out_caps.vDriverVersion = 0x0100;
408 ossdev->out_caps.dwFormats = 0x00000000;
409 ossdev->out_caps.dwSupport = WAVECAPS_VOLUME;
411 ioctl(audio, SNDCTL_DSP_GETFMTS, &mask);
412 TRACE("OSS dsp out mask=%08x\n", mask);
414 /* First bytespersampl, then stereo */
415 bytespersmpl = (ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &samplesize) != 0) ? 1 : 2;
417 ossdev->out_caps.wChannels = (ioctl(audio, SNDCTL_DSP_STEREO, &dsp_stereo) != 0) ? 1 : 2;
418 if (ossdev->out_caps.wChannels > 1) ossdev->out_caps.dwSupport |= WAVECAPS_LRVOLUME;
420 smplrate = 44100;
421 if (ioctl(audio, SNDCTL_DSP_SPEED, &smplrate) == 0) {
422 if (mask & AFMT_U8) {
423 ossdev->out_caps.dwFormats |= WAVE_FORMAT_4M08;
424 if (ossdev->out_caps.wChannels > 1)
425 ossdev->out_caps.dwFormats |= WAVE_FORMAT_4S08;
427 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
428 ossdev->out_caps.dwFormats |= WAVE_FORMAT_4M16;
429 if (ossdev->out_caps.wChannels > 1)
430 ossdev->out_caps.dwFormats |= WAVE_FORMAT_4S16;
433 smplrate = 22050;
434 if (ioctl(audio, SNDCTL_DSP_SPEED, &smplrate) == 0) {
435 if (mask & AFMT_U8) {
436 ossdev->out_caps.dwFormats |= WAVE_FORMAT_2M08;
437 if (ossdev->out_caps.wChannels > 1)
438 ossdev->out_caps.dwFormats |= WAVE_FORMAT_2S08;
440 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
441 ossdev->out_caps.dwFormats |= WAVE_FORMAT_2M16;
442 if (ossdev->out_caps.wChannels > 1)
443 ossdev->out_caps.dwFormats |= WAVE_FORMAT_2S16;
446 smplrate = 11025;
447 if (ioctl(audio, SNDCTL_DSP_SPEED, &smplrate) == 0) {
448 if (mask & AFMT_U8) {
449 ossdev->out_caps.dwFormats |= WAVE_FORMAT_1M08;
450 if (ossdev->out_caps.wChannels > 1)
451 ossdev->out_caps.dwFormats |= WAVE_FORMAT_1S08;
453 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
454 ossdev->out_caps.dwFormats |= WAVE_FORMAT_1M16;
455 if (ossdev->out_caps.wChannels > 1)
456 ossdev->out_caps.dwFormats |= WAVE_FORMAT_1S16;
459 if (ioctl(audio, SNDCTL_DSP_GETCAPS, &caps) == 0) {
460 TRACE("OSS dsp out caps=%08X\n", caps);
461 if ((caps & DSP_CAP_REALTIME) && !(caps & DSP_CAP_BATCH)) {
462 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
464 /* well, might as well use the DirectSound cap flag for something */
465 if ((caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) &&
466 !(caps & DSP_CAP_BATCH))
467 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
469 OSS_CloseDevice(devID, audio);
470 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
471 ossdev->out_caps.dwFormats, ossdev->out_caps.dwSupport);
475 /******************************************************************
476 * OSS_WaveInInit
480 static void OSS_WaveInInit(unsigned devID, OSS_DEVICE* ossdev)
482 int audio;
483 int smplrate;
484 int samplesize = 16;
485 int dsp_stereo = 1;
486 int bytespersmpl;
487 int caps;
488 int mask;
490 samplesize = 16;
491 dsp_stereo = 1;
493 WInDev[devID].unixdev = -1;
495 memset(&ossdev->in_caps, 0, sizeof(ossdev->in_caps));
497 if (OSS_OpenDevice(0, &audio, O_RDONLY, NULL, 0, 0, 0) != 0) return;
498 numInDev++;
500 ioctl(audio, SNDCTL_DSP_RESET, 0);
502 #ifdef EMULATE_SB16
503 ossdev->in_caps.wMid = 0x0002;
504 ossdev->in_caps.wPid = 0x0004;
505 strcpy(ossdev->in_caps.szPname, "SB16 Wave In");
506 #else
507 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
508 ossdev->in_caps.wPid = 0x0001; /* Product ID */
509 strcpy(ossdev->in_caps.szPname, "OpenSoundSystem WAVIN Driver");
510 #endif
511 ossdev->in_caps.dwFormats = 0x00000000;
512 ossdev->in_caps.wChannels = (ioctl(audio, SNDCTL_DSP_STEREO, &dsp_stereo) != 0) ? 1 : 2;
514 WInDev[devID].bTriggerSupport = FALSE;
515 if (ioctl(audio, SNDCTL_DSP_GETCAPS, &caps) == 0) {
516 TRACE("OSS dsp in caps=%08X\n", caps);
517 if (caps & DSP_CAP_TRIGGER)
518 WInDev[devID].bTriggerSupport = TRUE;
521 ioctl(audio, SNDCTL_DSP_GETFMTS, &mask);
522 TRACE("OSS in dsp mask=%08x\n", mask);
524 bytespersmpl = (ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &samplesize) != 0) ? 1 : 2;
525 smplrate = 44100;
526 if (ioctl(audio, SNDCTL_DSP_SPEED, &smplrate) == 0) {
527 if (mask & AFMT_U8) {
528 ossdev->in_caps.dwFormats |= WAVE_FORMAT_4M08;
529 if (ossdev->in_caps.wChannels > 1)
530 ossdev->in_caps.dwFormats |= WAVE_FORMAT_4S08;
532 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
533 ossdev->in_caps.dwFormats |= WAVE_FORMAT_4M16;
534 if (ossdev->in_caps.wChannels > 1)
535 ossdev->in_caps.dwFormats |= WAVE_FORMAT_4S16;
538 smplrate = 22050;
539 if (ioctl(audio, SNDCTL_DSP_SPEED, &smplrate) == 0) {
540 if (mask & AFMT_U8) {
541 ossdev->in_caps.dwFormats |= WAVE_FORMAT_2M08;
542 if (ossdev->in_caps.wChannels > 1)
543 ossdev->in_caps.dwFormats |= WAVE_FORMAT_2S08;
545 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
546 ossdev->in_caps.dwFormats |= WAVE_FORMAT_2M16;
547 if (ossdev->in_caps.wChannels > 1)
548 ossdev->in_caps.dwFormats |= WAVE_FORMAT_2S16;
551 smplrate = 11025;
552 if (ioctl(audio, SNDCTL_DSP_SPEED, &smplrate) == 0) {
553 if (mask & AFMT_U8) {
554 ossdev->in_caps.dwFormats |= WAVE_FORMAT_1M08;
555 if (ossdev->in_caps.wChannels > 1)
556 ossdev->in_caps.dwFormats |= WAVE_FORMAT_1S08;
558 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
559 ossdev->in_caps.dwFormats |= WAVE_FORMAT_1M16;
560 if (ossdev->in_caps.wChannels > 1)
561 ossdev->in_caps.dwFormats |= WAVE_FORMAT_1S16;
564 OSS_CloseDevice(devID, audio);
565 TRACE("in dwFormats = %08lX\n", ossdev->in_caps.dwFormats);
568 /******************************************************************
569 * OSS_WaveFullDuplexInit
573 static void OSS_WaveFullDuplexInit(unsigned devID, OSS_DEVICE* ossdev)
575 #ifdef USE_FULLDUPLEX
576 int audio;
577 int caps;
579 if (OSS_OpenDevice(devID, &audio, O_RDWR, NULL, 0, 0, 0) != 0) return;
580 if (ioctl(audio, SNDCTL_DSP_GETCAPS, &caps) == 0)
582 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
584 OSS_CloseDevice(devID, audio);
585 #endif
588 /******************************************************************
589 * OSS_WaveInit
591 * Initialize internal structures from OSS information
593 LONG OSS_WaveInit(void)
595 int i;
597 /* FIXME: only one device is supported */
598 memset(&OSS_Devices, 0, sizeof(OSS_Devices));
599 OSS_Devices[0].dev_name = "/dev/dsp";
600 OSS_Devices[0].mixer_name = "/dev/mixer";
602 /* start with output device */
603 for (i = 0; i < MAX_WAVEDRV; ++i)
604 OSS_WaveOutInit(i, &OSS_Devices[i]);
606 /* then do input device */
607 for (i = 0; i < MAX_WAVEDRV; ++i)
608 OSS_WaveInInit(i, &OSS_Devices[i]);
610 /* finish with the full duplex bits */
611 for (i = 0; i < MAX_WAVEDRV; i++)
612 OSS_WaveFullDuplexInit(i, &OSS_Devices[i]);
614 return 0;
617 /******************************************************************
618 * OSS_InitRingMessage
620 * Initialize the ring of messages for passing between driver's caller and playback/record
621 * thread
623 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
625 omr->msg_toget = 0;
626 omr->msg_tosave = 0;
627 #ifdef USE_PIPE_SYNC
628 if (pipe(omr->msg_pipe) < 0) {
629 omr->msg_pipe[0] = -1;
630 omr->msg_pipe[1] = -1;
631 ERR("could not create pipe, error=%s\n", strerror(errno));
633 #else
634 omr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
635 #endif
636 memset(omr->messages, 0, sizeof(OSS_MSG) * OSS_RING_BUFFER_SIZE);
637 InitializeCriticalSection(&omr->msg_crst);
638 return 0;
641 /******************************************************************
642 * OSS_DestroyRingMessage
645 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
647 #ifdef USE_PIPE_SYNC
648 close(omr->msg_pipe[0]);
649 close(omr->msg_pipe[1]);
650 #else
651 CloseHandle(omr->msg_event);
652 #endif
653 DeleteCriticalSection(&omr->msg_crst);
654 return 0;
657 /******************************************************************
658 * OSS_AddRingMessage
660 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
662 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
664 HANDLE hEvent = INVALID_HANDLE_VALUE;
666 EnterCriticalSection(&omr->msg_crst);
667 if ((omr->msg_toget == ((omr->msg_tosave + 1) % OSS_RING_BUFFER_SIZE))) /* buffer overflow ? */
669 ERR("buffer overflow !?\n");
670 LeaveCriticalSection(&omr->msg_crst);
671 return 0;
673 if (wait)
675 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
676 if (hEvent == INVALID_HANDLE_VALUE)
678 ERR("can't create event !?\n");
679 LeaveCriticalSection(&omr->msg_crst);
680 return 0;
682 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
683 FIXME("two fast messages in the queue!!!!\n");
685 /* fast messages have to be added at the start of the queue */
686 omr->msg_toget = (omr->msg_toget + OSS_RING_BUFFER_SIZE - 1) % OSS_RING_BUFFER_SIZE;
688 omr->messages[omr->msg_toget].msg = msg;
689 omr->messages[omr->msg_toget].param = param;
690 omr->messages[omr->msg_toget].hEvent = hEvent;
692 else
694 omr->messages[omr->msg_tosave].msg = msg;
695 omr->messages[omr->msg_tosave].param = param;
696 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
697 omr->msg_tosave = (omr->msg_tosave + 1) % OSS_RING_BUFFER_SIZE;
699 LeaveCriticalSection(&omr->msg_crst);
700 /* signal a new message */
701 SIGNAL_OMR(omr);
702 if (wait)
704 /* wait for playback/record thread to have processed the message */
705 WaitForSingleObject(hEvent, INFINITE);
706 CloseHandle(hEvent);
708 return 1;
711 /******************************************************************
712 * OSS_RetrieveRingMessage
714 * Get a message from the ring. Should be called by the playback/record thread.
716 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
717 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
719 EnterCriticalSection(&omr->msg_crst);
721 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
723 LeaveCriticalSection(&omr->msg_crst);
724 return 0;
727 *msg = omr->messages[omr->msg_toget].msg;
728 omr->messages[omr->msg_toget].msg = 0;
729 *param = omr->messages[omr->msg_toget].param;
730 *hEvent = omr->messages[omr->msg_toget].hEvent;
731 omr->msg_toget = (omr->msg_toget + 1) % OSS_RING_BUFFER_SIZE;
732 CLEAR_OMR(omr);
733 LeaveCriticalSection(&omr->msg_crst);
734 return 1;
737 /*======================================================================*
738 * Low level WAVE OUT implementation *
739 *======================================================================*/
741 /**************************************************************************
742 * wodNotifyClient [internal]
744 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
746 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
748 switch (wMsg) {
749 case WOM_OPEN:
750 case WOM_CLOSE:
751 case WOM_DONE:
752 if (wwo->wFlags != DCB_NULL &&
753 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
754 (HDRVR)wwo->waveDesc.hWave, wMsg,
755 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
756 WARN("can't notify client !\n");
757 return MMSYSERR_ERROR;
759 break;
760 default:
761 FIXME("Unknown callback message %u\n", wMsg);
762 return MMSYSERR_INVALPARAM;
764 return MMSYSERR_NOERROR;
767 /**************************************************************************
768 * wodUpdatePlayedTotal [internal]
771 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
773 audio_buf_info dspspace;
774 if (!info) info = &dspspace;
776 if (ioctl(wwo->unixdev, SNDCTL_DSP_GETOSPACE, info) < 0) {
777 ERR("ioctl can't 'SNDCTL_DSP_GETOSPACE' !\n");
778 return FALSE;
780 wwo->dwPlayedTotal = wwo->dwWrittenTotal - (wwo->dwBufferSize - info->bytes);
781 return TRUE;
784 /**************************************************************************
785 * wodPlayer_BeginWaveHdr [internal]
787 * Makes the specified lpWaveHdr the currently playing wave header.
788 * If the specified wave header is a begin loop and we're not already in
789 * a loop, setup the loop.
791 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
793 wwo->lpPlayPtr = lpWaveHdr;
795 if (!lpWaveHdr) return;
797 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
798 if (wwo->lpLoopPtr) {
799 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
800 } else {
801 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
802 wwo->lpLoopPtr = lpWaveHdr;
803 /* Windows does not touch WAVEHDR.dwLoops,
804 * so we need to make an internal copy */
805 wwo->dwLoops = lpWaveHdr->dwLoops;
808 wwo->dwPartialOffset = 0;
811 /**************************************************************************
812 * wodPlayer_PlayPtrNext [internal]
814 * Advance the play pointer to the next waveheader, looping if required.
816 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
818 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
820 wwo->dwPartialOffset = 0;
821 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
822 /* We're at the end of a loop, loop if required */
823 if (--wwo->dwLoops > 0) {
824 wwo->lpPlayPtr = wwo->lpLoopPtr;
825 } else {
826 /* Handle overlapping loops correctly */
827 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
828 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
829 /* shall we consider the END flag for the closing loop or for
830 * the opening one or for both ???
831 * code assumes for closing loop only
833 } else {
834 lpWaveHdr = lpWaveHdr->lpNext;
836 wwo->lpLoopPtr = NULL;
837 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
839 } else {
840 /* We're not in a loop. Advance to the next wave header */
841 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
844 return lpWaveHdr;
847 /**************************************************************************
848 * wodPlayer_DSPWait [internal]
849 * Returns the number of milliseconds to wait for the DSP buffer to write
850 * one fragment.
852 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
854 /* time for one fragment to be played */
855 return wwo->dwFragmentSize * 1000 / wwo->format.wf.nAvgBytesPerSec;
858 /**************************************************************************
859 * wodPlayer_NotifyWait [internal]
860 * Returns the number of milliseconds to wait before attempting to notify
861 * completion of the specified wavehdr.
862 * This is based on the number of bytes remaining to be written in the
863 * wave.
865 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
867 DWORD dwMillis;
869 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
870 dwMillis = 1;
871 } else {
872 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
873 if (!dwMillis) dwMillis = 1;
876 return dwMillis;
880 /**************************************************************************
881 * wodPlayer_WriteMaxFrags [internal]
882 * Writes the maximum number of bytes possible to the DSP and returns
883 * TRUE iff the current playPtr has been fully played
885 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
887 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
888 DWORD toWrite = min(dwLength, *bytes);
889 int written;
890 BOOL ret = FALSE;
892 TRACE("Writing wavehdr %p.%lu[%lu]/%lu\n",
893 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
895 if (toWrite > 0)
897 written = write(wwo->unixdev, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
898 if (written <= 0) return FALSE;
900 else
901 written = 0;
903 if (written >= dwLength) {
904 /* If we wrote all current wavehdr, skip to the next one */
905 wodPlayer_PlayPtrNext(wwo);
906 ret = TRUE;
907 } else {
908 /* Remove the amount written */
909 wwo->dwPartialOffset += written;
911 *bytes -= written;
912 wwo->dwWrittenTotal += written;
914 return ret;
918 /**************************************************************************
919 * wodPlayer_NotifyCompletions [internal]
921 * Notifies and remove from queue all wavehdrs which have been played to
922 * the speaker (ie. they have cleared the OSS buffer). If force is true,
923 * we notify all wavehdrs and remove them all from the queue even if they
924 * are unplayed or part of a loop.
926 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
928 LPWAVEHDR lpWaveHdr;
930 /* Start from lpQueuePtr and keep notifying until:
931 * - we hit an unwritten wavehdr
932 * - we hit the beginning of a running loop
933 * - we hit a wavehdr which hasn't finished playing
935 while ((lpWaveHdr = wwo->lpQueuePtr) &&
936 (force ||
937 (lpWaveHdr != wwo->lpPlayPtr &&
938 lpWaveHdr != wwo->lpLoopPtr &&
939 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
941 wwo->lpQueuePtr = lpWaveHdr->lpNext;
943 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
944 lpWaveHdr->dwFlags |= WHDR_DONE;
946 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
948 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
949 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
952 /**************************************************************************
953 * wodPlayer_Reset [internal]
955 * wodPlayer helper. Resets current output stream.
957 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
959 wodUpdatePlayedTotal(wwo, NULL);
960 /* updates current notify list */
961 wodPlayer_NotifyCompletions(wwo, FALSE);
963 /* flush all possible output */
964 if (ioctl(wwo->unixdev, SNDCTL_DSP_RESET, 0) == -1) {
965 perror("ioctl SNDCTL_DSP_RESET");
966 wwo->hThread = 0;
967 wwo->state = WINE_WS_STOPPED;
968 ExitThread(-1);
971 if (reset) {
972 enum win_wm_message msg;
973 DWORD param;
974 HANDLE ev;
976 /* remove any buffer */
977 wodPlayer_NotifyCompletions(wwo, TRUE);
979 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
980 wwo->state = WINE_WS_STOPPED;
981 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
982 /* Clear partial wavehdr */
983 wwo->dwPartialOffset = 0;
985 /* remove any existing message in the ring */
986 EnterCriticalSection(&wwo->msgRing.msg_crst);
987 /* return all pending headers in queue */
988 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
990 if (msg != WINE_WM_HEADER)
992 FIXME("shouldn't have headers left\n");
993 SetEvent(ev);
994 continue;
996 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
997 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
999 wodNotifyClient(wwo, WOM_DONE, param, 0);
1001 RESET_OMR(&wwo->msgRing);
1002 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1003 } else {
1004 if (wwo->lpLoopPtr) {
1005 /* complicated case, not handled yet (could imply modifying the loop counter */
1006 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1007 wwo->lpPlayPtr = wwo->lpLoopPtr;
1008 wwo->dwPartialOffset = 0;
1009 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1010 } else {
1011 LPWAVEHDR ptr;
1012 DWORD sz = wwo->dwPartialOffset;
1014 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1015 /* compute the max size playable from lpQueuePtr */
1016 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1017 sz += ptr->dwBufferLength;
1019 /* because the reset lpPlayPtr will be lpQueuePtr */
1020 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1021 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1022 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1023 wwo->lpPlayPtr = wwo->lpQueuePtr;
1025 wwo->state = WINE_WS_PAUSED;
1029 /**************************************************************************
1030 * wodPlayer_ProcessMessages [internal]
1032 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1034 LPWAVEHDR lpWaveHdr;
1035 enum win_wm_message msg;
1036 DWORD param;
1037 HANDLE ev;
1039 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1040 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
1041 switch (msg) {
1042 case WINE_WM_PAUSING:
1043 wodPlayer_Reset(wwo, FALSE);
1044 SetEvent(ev);
1045 break;
1046 case WINE_WM_RESTARTING:
1047 if (wwo->state == WINE_WS_PAUSED)
1049 wwo->state = WINE_WS_PLAYING;
1051 SetEvent(ev);
1052 break;
1053 case WINE_WM_HEADER:
1054 lpWaveHdr = (LPWAVEHDR)param;
1056 /* insert buffer at the end of queue */
1058 LPWAVEHDR* wh;
1059 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1060 *wh = lpWaveHdr;
1062 if (!wwo->lpPlayPtr)
1063 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1064 if (wwo->state == WINE_WS_STOPPED)
1065 wwo->state = WINE_WS_PLAYING;
1066 break;
1067 case WINE_WM_RESETTING:
1068 wodPlayer_Reset(wwo, TRUE);
1069 SetEvent(ev);
1070 break;
1071 case WINE_WM_UPDATE:
1072 wodUpdatePlayedTotal(wwo, NULL);
1073 SetEvent(ev);
1074 break;
1075 case WINE_WM_BREAKLOOP:
1076 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1077 /* ensure exit at end of current loop */
1078 wwo->dwLoops = 1;
1080 SetEvent(ev);
1081 break;
1082 case WINE_WM_CLOSING:
1083 /* sanity check: this should not happen since the device must have been reset before */
1084 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1085 wwo->hThread = 0;
1086 wwo->state = WINE_WS_CLOSED;
1087 SetEvent(ev);
1088 ExitThread(0);
1089 /* shouldn't go here */
1090 default:
1091 FIXME("unknown message %d\n", msg);
1092 break;
1097 /**************************************************************************
1098 * wodPlayer_FeedDSP [internal]
1099 * Feed as much sound data as we can into the DSP and return the number of
1100 * milliseconds before it will be necessary to feed the DSP again.
1102 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1104 audio_buf_info dspspace;
1105 DWORD availInQ;
1107 wodUpdatePlayedTotal(wwo, &dspspace);
1108 availInQ = dspspace.bytes;
1109 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1110 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1112 /* input queue empty and output buffer with less than one fragment to play */
1113 if (!wwo->lpPlayPtr && wwo->dwBufferSize < availInQ + wwo->dwFragmentSize) {
1114 TRACE("Run out of wavehdr:s...\n");
1115 return INFINITE;
1118 /* no more room... no need to try to feed */
1119 if (dspspace.fragments != 0) {
1120 /* Feed from partial wavehdr */
1121 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1122 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1125 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1126 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1127 do {
1128 TRACE("Setting time to elapse for %p to %lu\n",
1129 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1130 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1131 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1132 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1135 if (wwo->bNeedPost) {
1136 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1137 * if it didn't get one, we give it the other */
1138 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1139 ioctl(wwo->unixdev, SNDCTL_DSP_POST, 0);
1140 wwo->bNeedPost = FALSE;
1144 return wodPlayer_DSPWait(wwo);
1148 /**************************************************************************
1149 * wodPlayer [internal]
1151 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1153 WORD uDevID = (DWORD)pmt;
1154 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1155 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1156 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1157 DWORD dwSleepTime;
1159 wwo->state = WINE_WS_STOPPED;
1160 SetEvent(wwo->hStartUpEvent);
1162 for (;;) {
1163 /** Wait for the shortest time before an action is required. If there
1164 * are no pending actions, wait forever for a command.
1166 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1167 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1168 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1169 wodPlayer_ProcessMessages(wwo);
1170 if (wwo->state == WINE_WS_PLAYING) {
1171 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1172 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1173 if (dwNextFeedTime == INFINITE) {
1174 /* FeedDSP ran out of data, but before flushing, */
1175 /* check that a notification didn't give us more */
1176 wodPlayer_ProcessMessages(wwo);
1177 if (!wwo->lpPlayPtr) {
1178 TRACE("flushing\n");
1179 ioctl(wwo->unixdev, SNDCTL_DSP_SYNC, 0);
1180 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1182 else {
1183 TRACE("recovering\n");
1184 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1187 } else {
1188 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1193 /**************************************************************************
1194 * wodGetDevCaps [internal]
1196 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1198 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1200 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1202 if (wDevID >= MAX_WAVEDRV) {
1203 TRACE("MAX_WAVDRV reached !\n");
1204 return MMSYSERR_BADDEVICEID;
1207 memcpy(lpCaps, &OSS_Devices[wDevID].out_caps, min(dwSize, sizeof(*lpCaps)));
1208 return MMSYSERR_NOERROR;
1211 /**************************************************************************
1212 * wodOpen [internal]
1214 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1216 int audio_fragment;
1217 WINE_WAVEOUT* wwo;
1218 audio_buf_info info;
1219 DWORD ret;
1221 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1222 if (lpDesc == NULL) {
1223 WARN("Invalid Parameter !\n");
1224 return MMSYSERR_INVALPARAM;
1226 if (wDevID >= MAX_WAVEDRV) {
1227 TRACE("MAX_WAVOUTDRV reached !\n");
1228 return MMSYSERR_BADDEVICEID;
1231 /* only PCM format is supported so far... */
1232 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1233 lpDesc->lpFormat->nChannels == 0 ||
1234 lpDesc->lpFormat->nSamplesPerSec == 0) {
1235 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1236 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1237 lpDesc->lpFormat->nSamplesPerSec);
1238 return WAVERR_BADFORMAT;
1241 if (dwFlags & WAVE_FORMAT_QUERY) {
1242 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1243 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1244 lpDesc->lpFormat->nSamplesPerSec);
1245 return MMSYSERR_NOERROR;
1248 wwo = &WOutDev[wDevID];
1250 if ((dwFlags & WAVE_DIRECTSOUND) && !(OSS_Devices[wDevID].out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
1251 /* not supported, ignore it */
1252 dwFlags &= ~WAVE_DIRECTSOUND;
1254 if (dwFlags & WAVE_DIRECTSOUND) {
1255 if (OSS_Devices[wDevID].out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1256 /* we have realtime DirectSound, fragments just waste our time,
1257 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1258 audio_fragment = 0x0020000B;
1259 else
1260 /* to approximate realtime, we must use small fragments,
1261 * let's try to fragment the above 64KB (256 * 2^8) */
1262 audio_fragment = 0x01000008;
1263 } else {
1264 /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
1265 * thus leading to 46ms per fragment, and a turnaround time of 185ms
1267 /* 16 fragments max, 2^10=1024 bytes per fragment */
1268 audio_fragment = 0x000F000A;
1270 if (wwo->unixdev != -1) return MMSYSERR_ALLOCATED;
1271 /* we want to be able to mmap() the device, which means it must be opened readable,
1272 * otherwise mmap() will fail (at least under Linux) */
1273 ret = OSS_OpenDevice(wDevID, &wwo->unixdev,
1274 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
1275 &audio_fragment, lpDesc->lpFormat->nSamplesPerSec,
1276 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
1277 (lpDesc->lpFormat->wBitsPerSample == 16)
1278 ? AFMT_S16_LE : AFMT_U8);
1279 if (ret != 0) return ret;
1281 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1283 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1284 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1286 if (wwo->format.wBitsPerSample == 0) {
1287 WARN("Resetting zeroed wBitsPerSample\n");
1288 wwo->format.wBitsPerSample = 8 *
1289 (wwo->format.wf.nAvgBytesPerSec /
1290 wwo->format.wf.nSamplesPerSec) /
1291 wwo->format.wf.nChannels;
1293 /* Read output space info for future reference */
1294 if (ioctl(wwo->unixdev, SNDCTL_DSP_GETOSPACE, &info) < 0) {
1295 ERR("ioctl can't 'SNDCTL_DSP_GETOSPACE' !\n");
1296 OSS_CloseDevice(wDevID, wwo->unixdev);
1297 wwo->unixdev = -1;
1298 return MMSYSERR_NOTENABLED;
1301 /* Check that fragsize is correct per our settings above */
1302 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
1303 /* we've tried to set 1K fragments or less, but it didn't work */
1304 ERR("fragment size set failed, size is now %d\n", info.fragsize);
1305 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
1306 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
1309 /* Remember fragsize and total buffer size for future use */
1310 wwo->dwFragmentSize = info.fragsize;
1311 wwo->dwBufferSize = info.fragstotal * info.fragsize;
1312 wwo->dwPlayedTotal = 0;
1313 wwo->dwWrittenTotal = 0;
1314 wwo->bNeedPost = TRUE;
1316 OSS_InitRingMessage(&wwo->msgRing);
1318 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1319 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1320 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1321 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1322 CloseHandle(wwo->hStartUpEvent);
1323 } else {
1324 wwo->hThread = INVALID_HANDLE_VALUE;
1325 wwo->dwThreadID = 0;
1327 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1329 TRACE("fd=%d fragmentSize=%ld\n",
1330 wwo->unixdev, wwo->dwFragmentSize);
1331 if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1332 ERR("Fragment doesn't contain an integral number of data blocks\n");
1334 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1335 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1336 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1337 wwo->format.wf.nBlockAlign);
1339 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1342 /**************************************************************************
1343 * wodClose [internal]
1345 static DWORD wodClose(WORD wDevID)
1347 DWORD ret = MMSYSERR_NOERROR;
1348 WINE_WAVEOUT* wwo;
1350 TRACE("(%u);\n", wDevID);
1352 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].unixdev == -1) {
1353 WARN("bad device ID !\n");
1354 return MMSYSERR_BADDEVICEID;
1357 wwo = &WOutDev[wDevID];
1358 if (wwo->lpQueuePtr) {
1359 WARN("buffers still playing !\n");
1360 ret = WAVERR_STILLPLAYING;
1361 } else {
1362 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1363 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1365 if (wwo->mapping) {
1366 munmap(wwo->mapping, wwo->maplen);
1367 wwo->mapping = NULL;
1370 OSS_DestroyRingMessage(&wwo->msgRing);
1372 OSS_CloseDevice(wDevID, wwo->unixdev);
1373 wwo->unixdev = -1;
1374 wwo->dwFragmentSize = 0;
1375 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1377 return ret;
1380 /**************************************************************************
1381 * wodWrite [internal]
1384 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1386 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1388 /* first, do the sanity checks... */
1389 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].unixdev == -1) {
1390 WARN("bad dev ID !\n");
1391 return MMSYSERR_BADDEVICEID;
1394 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1395 return WAVERR_UNPREPARED;
1397 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1398 return WAVERR_STILLPLAYING;
1400 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1401 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1402 lpWaveHdr->lpNext = 0;
1404 if ((lpWaveHdr->dwBufferLength & ~(WOutDev[wDevID].format.wf.nBlockAlign - 1)) != 0)
1406 WARN("WaveHdr length isn't a multiple of the PCM block size\n");
1407 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].format.wf.nBlockAlign - 1);
1410 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1412 return MMSYSERR_NOERROR;
1415 /**************************************************************************
1416 * wodPrepare [internal]
1418 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1420 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1422 if (wDevID >= MAX_WAVEDRV) {
1423 WARN("bad device ID !\n");
1424 return MMSYSERR_BADDEVICEID;
1427 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1428 return WAVERR_STILLPLAYING;
1430 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1431 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1432 return MMSYSERR_NOERROR;
1435 /**************************************************************************
1436 * wodUnprepare [internal]
1438 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1440 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1442 if (wDevID >= MAX_WAVEDRV) {
1443 WARN("bad device ID !\n");
1444 return MMSYSERR_BADDEVICEID;
1447 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1448 return WAVERR_STILLPLAYING;
1450 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1451 lpWaveHdr->dwFlags |= WHDR_DONE;
1453 return MMSYSERR_NOERROR;
1456 /**************************************************************************
1457 * wodPause [internal]
1459 static DWORD wodPause(WORD wDevID)
1461 TRACE("(%u);!\n", wDevID);
1463 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].unixdev == -1) {
1464 WARN("bad device ID !\n");
1465 return MMSYSERR_BADDEVICEID;
1468 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1470 return MMSYSERR_NOERROR;
1473 /**************************************************************************
1474 * wodRestart [internal]
1476 static DWORD wodRestart(WORD wDevID)
1478 TRACE("(%u);\n", wDevID);
1480 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].unixdev == -1) {
1481 WARN("bad device ID !\n");
1482 return MMSYSERR_BADDEVICEID;
1485 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1487 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1488 /* FIXME: Myst crashes with this ... hmm -MM
1489 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1492 return MMSYSERR_NOERROR;
1495 /**************************************************************************
1496 * wodReset [internal]
1498 static DWORD wodReset(WORD wDevID)
1500 TRACE("(%u);\n", wDevID);
1502 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].unixdev == -1) {
1503 WARN("bad device ID !\n");
1504 return MMSYSERR_BADDEVICEID;
1507 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1509 return MMSYSERR_NOERROR;
1512 /**************************************************************************
1513 * wodGetPosition [internal]
1515 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1517 int time;
1518 DWORD val;
1519 WINE_WAVEOUT* wwo;
1521 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1523 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].unixdev == -1) {
1524 WARN("bad device ID !\n");
1525 return MMSYSERR_BADDEVICEID;
1528 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1530 wwo = &WOutDev[wDevID];
1531 #ifdef EXACT_WODPOSITION
1532 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1533 #endif
1534 val = wwo->dwPlayedTotal;
1536 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1537 lpTime->wType, wwo->format.wBitsPerSample,
1538 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1539 wwo->format.wf.nAvgBytesPerSec);
1540 TRACE("dwPlayedTotal=%lu\n", val);
1542 switch (lpTime->wType) {
1543 case TIME_BYTES:
1544 lpTime->u.cb = val;
1545 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1546 break;
1547 case TIME_SAMPLES:
1548 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1549 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1550 break;
1551 case TIME_SMPTE:
1552 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1553 lpTime->u.smpte.hour = time / 108000;
1554 time -= lpTime->u.smpte.hour * 108000;
1555 lpTime->u.smpte.min = time / 1800;
1556 time -= lpTime->u.smpte.min * 1800;
1557 lpTime->u.smpte.sec = time / 30;
1558 time -= lpTime->u.smpte.sec * 30;
1559 lpTime->u.smpte.frame = time;
1560 lpTime->u.smpte.fps = 30;
1561 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1562 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1563 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1564 break;
1565 default:
1566 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1567 lpTime->wType = TIME_MS;
1568 case TIME_MS:
1569 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1570 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1571 break;
1573 return MMSYSERR_NOERROR;
1576 /**************************************************************************
1577 * wodBreakLoop [internal]
1579 static DWORD wodBreakLoop(WORD wDevID)
1581 TRACE("(%u);\n", wDevID);
1583 if (wDevID >= MAX_WAVEDRV || WOutDev[wDevID].unixdev == -1) {
1584 WARN("bad device ID !\n");
1585 return MMSYSERR_BADDEVICEID;
1587 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1588 return MMSYSERR_NOERROR;
1591 /**************************************************************************
1592 * wodGetVolume [internal]
1594 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1596 int mixer;
1597 int volume;
1598 DWORD left, right;
1600 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1602 if (lpdwVol == NULL)
1603 return MMSYSERR_NOTENABLED;
1604 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_INVALPARAM;
1606 if ((mixer = open(OSS_Devices[wDevID].mixer_name, O_RDONLY|O_NDELAY)) < 0) {
1607 WARN("mixer device not available !\n");
1608 return MMSYSERR_NOTENABLED;
1610 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
1611 WARN("unable to read mixer !\n");
1612 return MMSYSERR_NOTENABLED;
1614 close(mixer);
1615 left = LOBYTE(volume);
1616 right = HIBYTE(volume);
1617 TRACE("left=%ld right=%ld !\n", left, right);
1618 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1619 return MMSYSERR_NOERROR;
1622 /**************************************************************************
1623 * wodSetVolume [internal]
1625 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1627 int mixer;
1628 int volume;
1629 DWORD left, right;
1631 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1633 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1634 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1635 volume = left + (right << 8);
1637 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_INVALPARAM;
1639 if ((mixer = open(OSS_Devices[wDevID].mixer_name, O_WRONLY|O_NDELAY)) < 0) {
1640 WARN("mixer device not available !\n");
1641 return MMSYSERR_NOTENABLED;
1643 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
1644 WARN("unable to set mixer !\n");
1645 return MMSYSERR_NOTENABLED;
1646 } else {
1647 TRACE("volume=%04x\n", (unsigned)volume);
1649 close(mixer);
1650 return MMSYSERR_NOERROR;
1653 /**************************************************************************
1654 * wodMessage (WINEOSS.7)
1656 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1657 DWORD dwParam1, DWORD dwParam2)
1659 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1660 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1662 switch (wMsg) {
1663 case DRVM_INIT:
1664 case DRVM_EXIT:
1665 case DRVM_ENABLE:
1666 case DRVM_DISABLE:
1667 /* FIXME: Pretend this is supported */
1668 return 0;
1669 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1670 case WODM_CLOSE: return wodClose (wDevID);
1671 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1672 case WODM_PAUSE: return wodPause (wDevID);
1673 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1674 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1675 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1676 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1677 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1678 case WODM_GETNUMDEVS: return numOutDev;
1679 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1680 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1681 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1682 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1683 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1684 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1685 case WODM_RESTART: return wodRestart (wDevID);
1686 case WODM_RESET: return wodReset (wDevID);
1688 case DRV_QUERYDSOUNDIFACE: return wodDsCreate(wDevID, (PIDSDRIVER*)dwParam1);
1689 default:
1690 FIXME("unknown message %d!\n", wMsg);
1692 return MMSYSERR_NOTSUPPORTED;
1695 /*======================================================================*
1696 * Low level DSOUND implementation *
1697 *======================================================================*/
1699 typedef struct IDsDriverImpl IDsDriverImpl;
1700 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1702 struct IDsDriverImpl
1704 /* IUnknown fields */
1705 ICOM_VFIELD(IDsDriver);
1706 DWORD ref;
1707 /* IDsDriverImpl fields */
1708 UINT wDevID;
1709 IDsDriverBufferImpl*primary;
1712 struct IDsDriverBufferImpl
1714 /* IUnknown fields */
1715 ICOM_VFIELD(IDsDriverBuffer);
1716 DWORD ref;
1717 /* IDsDriverBufferImpl fields */
1718 IDsDriverImpl* drv;
1719 DWORD buflen;
1722 static HRESULT DSDB_MapPrimary(IDsDriverBufferImpl *dsdb)
1724 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1725 if (!wwo->mapping) {
1726 wwo->mapping = mmap(NULL, wwo->maplen, PROT_WRITE, MAP_SHARED,
1727 wwo->unixdev, 0);
1728 if (wwo->mapping == (LPBYTE)-1) {
1729 ERR("(%p): Could not map sound device for direct access (%s)\n", dsdb, strerror(errno));
1730 return DSERR_GENERIC;
1732 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb, wwo->mapping, wwo->maplen);
1734 /* for some reason, es1371 and sblive! sometimes have junk in here.
1735 * clear it, or we get junk noise */
1736 /* some libc implementations are buggy: their memset reads from the buffer...
1737 * to work around it, we have to zero the block by hand. We don't do the expected:
1738 * memset(wwo->mapping,0, wwo->maplen);
1741 char* p1 = wwo->mapping;
1742 unsigned len = wwo->maplen;
1744 if (len >= 16) /* so we can have at least a 4 long area to store... */
1746 /* the mmap:ed value is (at least) dword aligned
1747 * so, start filling the complete unsigned long:s
1749 int b = len >> 2;
1750 unsigned long* p4 = (unsigned long*)p1;
1752 while (b--) *p4++ = 0;
1753 /* prepare for filling the rest */
1754 len &= 3;
1755 p1 = (unsigned char*)p4;
1757 /* in all cases, fill the remaining bytes */
1758 while (len-- != 0) *p1++ = 0;
1761 return DS_OK;
1764 static HRESULT DSDB_UnmapPrimary(IDsDriverBufferImpl *dsdb)
1766 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1767 if (wwo->mapping) {
1768 if (munmap(wwo->mapping, wwo->maplen) < 0) {
1769 ERR("(%p): Could not unmap sound device (errno=%d)\n", dsdb, errno);
1770 return DSERR_GENERIC;
1772 wwo->mapping = NULL;
1773 TRACE("(%p): sound device unmapped\n", dsdb);
1775 return DS_OK;
1778 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
1780 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1781 FIXME("(): stub!\n");
1782 return DSERR_UNSUPPORTED;
1785 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
1787 ICOM_THIS(IDsDriverBufferImpl,iface);
1788 This->ref++;
1789 return This->ref;
1792 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
1794 ICOM_THIS(IDsDriverBufferImpl,iface);
1795 if (--This->ref)
1796 return This->ref;
1797 if (This == This->drv->primary)
1798 This->drv->primary = NULL;
1799 DSDB_UnmapPrimary(This);
1800 HeapFree(GetProcessHeap(),0,This);
1801 return 0;
1804 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
1805 LPVOID*ppvAudio1,LPDWORD pdwLen1,
1806 LPVOID*ppvAudio2,LPDWORD pdwLen2,
1807 DWORD dwWritePosition,DWORD dwWriteLen,
1808 DWORD dwFlags)
1810 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1811 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
1812 * and that we don't support secondary buffers, this method will never be called */
1813 TRACE("(%p): stub\n",iface);
1814 return DSERR_UNSUPPORTED;
1817 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
1818 LPVOID pvAudio1,DWORD dwLen1,
1819 LPVOID pvAudio2,DWORD dwLen2)
1821 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1822 TRACE("(%p): stub\n",iface);
1823 return DSERR_UNSUPPORTED;
1826 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
1827 LPWAVEFORMATEX pwfx)
1829 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1831 TRACE("(%p,%p)\n",iface,pwfx);
1832 /* On our request (GetDriverDesc flags), DirectSound has by now used
1833 * waveOutClose/waveOutOpen to set the format...
1834 * unfortunately, this means our mmap() is now gone...
1835 * so we need to somehow signal to our DirectSound implementation
1836 * that it should completely recreate this HW buffer...
1837 * this unexpected error code should do the trick... */
1838 return DSERR_BUFFERLOST;
1841 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
1843 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1844 TRACE("(%p,%ld): stub\n",iface,dwFreq);
1845 return DSERR_UNSUPPORTED;
1848 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
1850 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1851 FIXME("(%p,%p): stub!\n",iface,pVolPan);
1852 return DSERR_UNSUPPORTED;
1855 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
1857 /* ICOM_THIS(IDsDriverImpl,iface); */
1858 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
1859 return DSERR_UNSUPPORTED;
1862 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
1863 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
1865 ICOM_THIS(IDsDriverBufferImpl,iface);
1866 count_info info;
1867 DWORD ptr;
1869 TRACE("(%p)\n",iface);
1870 if (WOutDev[This->drv->wDevID].unixdev == -1) {
1871 ERR("device not open, but accessing?\n");
1872 return DSERR_UNINITIALIZED;
1874 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_GETOPTR, &info) < 0) {
1875 ERR("ioctl failed (%d)\n", errno);
1876 return DSERR_GENERIC;
1878 ptr = info.ptr & ~3; /* align the pointer, just in case */
1879 if (lpdwPlay) *lpdwPlay = ptr;
1880 if (lpdwWrite) {
1881 /* add some safety margin (not strictly necessary, but...) */
1882 if (OSS_Devices[This->drv->wDevID].out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1883 *lpdwWrite = ptr + 32;
1884 else
1885 *lpdwWrite = ptr + WOutDev[This->drv->wDevID].dwFragmentSize;
1886 while (*lpdwWrite > This->buflen)
1887 *lpdwWrite -= This->buflen;
1889 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay?*lpdwPlay:0, lpdwWrite?*lpdwWrite:0);
1890 return DS_OK;
1893 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
1895 ICOM_THIS(IDsDriverBufferImpl,iface);
1896 int enable = PCM_ENABLE_OUTPUT;
1897 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
1898 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1899 ERR("ioctl failed (%d)\n", errno);
1900 return DSERR_GENERIC;
1902 return DS_OK;
1905 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
1907 ICOM_THIS(IDsDriverBufferImpl,iface);
1908 int enable = 0;
1909 TRACE("(%p)\n",iface);
1910 /* no more playing */
1911 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1912 ERR("ioctl failed (%d)\n", errno);
1913 return DSERR_GENERIC;
1915 #if 0
1916 /* the play position must be reset to the beginning of the buffer */
1917 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_RESET, 0) < 0) {
1918 ERR("ioctl failed (%d)\n", errno);
1919 return DSERR_GENERIC;
1921 #endif
1922 /* Most OSS drivers just can't stop the playback without closing the device...
1923 * so we need to somehow signal to our DirectSound implementation
1924 * that it should completely recreate this HW buffer...
1925 * this unexpected error code should do the trick... */
1926 return DSERR_BUFFERLOST;
1929 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
1931 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1932 IDsDriverBufferImpl_QueryInterface,
1933 IDsDriverBufferImpl_AddRef,
1934 IDsDriverBufferImpl_Release,
1935 IDsDriverBufferImpl_Lock,
1936 IDsDriverBufferImpl_Unlock,
1937 IDsDriverBufferImpl_SetFormat,
1938 IDsDriverBufferImpl_SetFrequency,
1939 IDsDriverBufferImpl_SetVolumePan,
1940 IDsDriverBufferImpl_SetPosition,
1941 IDsDriverBufferImpl_GetPosition,
1942 IDsDriverBufferImpl_Play,
1943 IDsDriverBufferImpl_Stop
1946 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
1948 /* ICOM_THIS(IDsDriverImpl,iface); */
1949 FIXME("(%p): stub!\n",iface);
1950 return DSERR_UNSUPPORTED;
1953 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
1955 ICOM_THIS(IDsDriverImpl,iface);
1956 This->ref++;
1957 return This->ref;
1960 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
1962 ICOM_THIS(IDsDriverImpl,iface);
1963 if (--This->ref)
1964 return This->ref;
1965 HeapFree(GetProcessHeap(),0,This);
1966 return 0;
1969 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
1971 ICOM_THIS(IDsDriverImpl,iface);
1972 TRACE("(%p,%p)\n",iface,pDesc);
1973 pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
1974 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
1975 strcpy(pDesc->szDesc,"WineOSS DirectSound Driver");
1976 strcpy(pDesc->szDrvName,"wineoss.drv");
1977 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
1978 pDesc->wVxdId = 0;
1979 pDesc->wReserved = 0;
1980 pDesc->ulDeviceNum = This->wDevID;
1981 pDesc->dwHeapType = DSDHEAP_NOHEAP;
1982 pDesc->pvDirectDrawHeap = NULL;
1983 pDesc->dwMemStartAddress = 0;
1984 pDesc->dwMemEndAddress = 0;
1985 pDesc->dwMemAllocExtra = 0;
1986 pDesc->pvReserved1 = NULL;
1987 pDesc->pvReserved2 = NULL;
1988 return DS_OK;
1991 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
1993 ICOM_THIS(IDsDriverImpl,iface);
1994 int enable = 0;
1996 TRACE("(%p)\n",iface);
1997 /* make sure the card doesn't start playing before we want it to */
1998 if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1999 ERR("ioctl failed (%d)\n", errno);
2000 return DSERR_GENERIC;
2002 return DS_OK;
2005 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
2007 ICOM_THIS(IDsDriverImpl,iface);
2008 TRACE("(%p)\n",iface);
2009 if (This->primary) {
2010 ERR("problem with DirectSound: primary not released\n");
2011 return DSERR_GENERIC;
2013 return DS_OK;
2016 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
2018 /* ICOM_THIS(IDsDriverImpl,iface); */
2019 TRACE("(%p,%p)\n",iface,pCaps);
2020 memset(pCaps, 0, sizeof(*pCaps));
2021 /* FIXME: need to check actual capabilities */
2022 pCaps->dwFlags = DSCAPS_PRIMARYMONO | DSCAPS_PRIMARYSTEREO |
2023 DSCAPS_PRIMARY8BIT | DSCAPS_PRIMARY16BIT;
2024 pCaps->dwPrimaryBuffers = 1;
2025 /* the other fields only apply to secondary buffers, which we don't support
2026 * (unless we want to mess with wavetable synthesizers and MIDI) */
2027 return DS_OK;
2030 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
2031 LPWAVEFORMATEX pwfx,
2032 DWORD dwFlags, DWORD dwCardAddress,
2033 LPDWORD pdwcbBufferSize,
2034 LPBYTE *ppbBuffer,
2035 LPVOID *ppvObj)
2037 ICOM_THIS(IDsDriverImpl,iface);
2038 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
2039 HRESULT err;
2040 audio_buf_info info;
2041 int enable = 0;
2043 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
2044 /* we only support primary buffers */
2045 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
2046 return DSERR_UNSUPPORTED;
2047 if (This->primary)
2048 return DSERR_ALLOCATED;
2049 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
2050 return DSERR_CONTROLUNAVAIL;
2052 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
2053 if (*ippdsdb == NULL)
2054 return DSERR_OUTOFMEMORY;
2055 ICOM_VTBL(*ippdsdb) = &dsdbvt;
2056 (*ippdsdb)->ref = 1;
2057 (*ippdsdb)->drv = This;
2059 /* check how big the DMA buffer is now */
2060 if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2061 ERR("ioctl failed (%d)\n", errno);
2062 HeapFree(GetProcessHeap(),0,*ippdsdb);
2063 *ippdsdb = NULL;
2064 return DSERR_GENERIC;
2066 WOutDev[This->wDevID].maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
2068 /* map the DMA buffer */
2069 err = DSDB_MapPrimary(*ippdsdb);
2070 if (err != DS_OK) {
2071 HeapFree(GetProcessHeap(),0,*ippdsdb);
2072 *ippdsdb = NULL;
2073 return err;
2076 /* primary buffer is ready to go */
2077 *pdwcbBufferSize = WOutDev[This->wDevID].maplen;
2078 *ppbBuffer = WOutDev[This->wDevID].mapping;
2080 /* some drivers need some extra nudging after mapping */
2081 if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2082 ERR("ioctl failed (%d)\n", errno);
2083 return DSERR_GENERIC;
2086 This->primary = *ippdsdb;
2088 return DS_OK;
2091 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
2092 PIDSDRIVERBUFFER pBuffer,
2093 LPVOID *ppvObj)
2095 /* ICOM_THIS(IDsDriverImpl,iface); */
2096 TRACE("(%p,%p): stub\n",iface,pBuffer);
2097 return DSERR_INVALIDCALL;
2100 static ICOM_VTABLE(IDsDriver) dsdvt =
2102 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2103 IDsDriverImpl_QueryInterface,
2104 IDsDriverImpl_AddRef,
2105 IDsDriverImpl_Release,
2106 IDsDriverImpl_GetDriverDesc,
2107 IDsDriverImpl_Open,
2108 IDsDriverImpl_Close,
2109 IDsDriverImpl_GetCaps,
2110 IDsDriverImpl_CreateSoundBuffer,
2111 IDsDriverImpl_DuplicateSoundBuffer
2114 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2116 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
2118 /* the HAL isn't much better than the HEL if we can't do mmap() */
2119 if (!(OSS_Devices[wDevID].out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
2120 ERR("DirectSound flag not set\n");
2121 MESSAGE("This sound card's driver does not support direct access\n");
2122 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2123 return MMSYSERR_NOTSUPPORTED;
2126 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
2127 if (!*idrv)
2128 return MMSYSERR_NOMEM;
2129 ICOM_VTBL(*idrv) = &dsdvt;
2130 (*idrv)->ref = 1;
2132 (*idrv)->wDevID = wDevID;
2133 (*idrv)->primary = NULL;
2134 return MMSYSERR_NOERROR;
2137 /*======================================================================*
2138 * Low level WAVE IN implementation *
2139 *======================================================================*/
2141 /**************************************************************************
2142 * widNotifyClient [internal]
2144 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2146 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
2148 switch (wMsg) {
2149 case WIM_OPEN:
2150 case WIM_CLOSE:
2151 case WIM_DATA:
2152 if (wwi->wFlags != DCB_NULL &&
2153 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2154 (HDRVR)wwi->waveDesc.hWave, wMsg,
2155 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2156 WARN("can't notify client !\n");
2157 return MMSYSERR_ERROR;
2159 break;
2160 default:
2161 FIXME("Unknown callback message %u\n", wMsg);
2162 return MMSYSERR_INVALPARAM;
2164 return MMSYSERR_NOERROR;
2167 /**************************************************************************
2168 * widGetDevCaps [internal]
2170 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
2172 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
2174 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2176 if (wDevID >= MAX_WAVEDRV) {
2177 TRACE("MAX_WAVDRV reached !\n");
2178 return MMSYSERR_BADDEVICEID;
2181 memcpy(lpCaps, &OSS_Devices[wDevID].in_caps, min(dwSize, sizeof(*lpCaps)));
2182 return MMSYSERR_NOERROR;
2185 /**************************************************************************
2186 * widRecorder [internal]
2188 static DWORD CALLBACK widRecorder(LPVOID pmt)
2190 WORD uDevID = (DWORD)pmt;
2191 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2192 WAVEHDR* lpWaveHdr;
2193 DWORD dwSleepTime;
2194 DWORD bytesRead;
2195 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2196 LPVOID pOffset = buffer;
2197 audio_buf_info info;
2198 int xs;
2199 enum win_wm_message msg;
2200 DWORD param;
2201 HANDLE ev;
2203 wwi->state = WINE_WS_STOPPED;
2204 wwi->dwTotalRecorded = 0;
2206 SetEvent(wwi->hStartUpEvent);
2208 /* the soundblaster live needs a micro wake to get its recording started
2209 * (or GETISPACE will have 0 frags all the time)
2211 read(wwi->unixdev,&xs,4);
2213 /* make sleep time to be # of ms to output a fragment */
2214 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->format.wf.nAvgBytesPerSec;
2215 TRACE("sleeptime=%ld ms\n", dwSleepTime);
2217 for (;;) {
2218 /* wait for dwSleepTime or an event in thread's queue */
2219 /* FIXME: could improve wait time depending on queue state,
2220 * ie, number of queued fragments
2223 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2225 lpWaveHdr = wwi->lpQueuePtr;
2227 ioctl(wwi->unixdev, SNDCTL_DSP_GETISPACE, &info);
2228 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2230 /* read all the fragments accumulated so far */
2231 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2233 info.fragments --;
2235 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2237 /* directly read fragment in wavehdr */
2238 bytesRead = read(wwi->unixdev,
2239 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2240 wwi->dwFragmentSize);
2242 TRACE("bytesRead=%ld (direct)\n", bytesRead);
2243 if (bytesRead != (DWORD) -1)
2245 /* update number of bytes recorded in current buffer and by this device */
2246 lpWaveHdr->dwBytesRecorded += bytesRead;
2247 wwi->dwTotalRecorded += bytesRead;
2249 /* buffer is full. notify client */
2250 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2252 /* must copy the value of next waveHdr, because we have no idea of what
2253 * will be done with the content of lpWaveHdr in callback
2255 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2257 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2258 lpWaveHdr->dwFlags |= WHDR_DONE;
2260 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2261 lpWaveHdr = wwi->lpQueuePtr = lpNext;
2265 else
2267 /* read the fragment in a local buffer */
2268 bytesRead = read(wwi->unixdev, buffer, wwi->dwFragmentSize);
2269 pOffset = buffer;
2271 TRACE("bytesRead=%ld (local)\n", bytesRead);
2273 /* copy data in client buffers */
2274 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2276 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2278 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2279 pOffset,
2280 dwToCopy);
2282 /* update number of bytes recorded in current buffer and by this device */
2283 lpWaveHdr->dwBytesRecorded += dwToCopy;
2284 wwi->dwTotalRecorded += dwToCopy;
2285 bytesRead -= dwToCopy;
2286 pOffset += dwToCopy;
2288 /* client buffer is full. notify client */
2289 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2291 /* must copy the value of next waveHdr, because we have no idea of what
2292 * will be done with the content of lpWaveHdr in callback
2294 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2295 TRACE("lpNext=%p\n", lpNext);
2297 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2298 lpWaveHdr->dwFlags |= WHDR_DONE;
2300 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2302 wwi->lpQueuePtr = lpWaveHdr = lpNext;
2303 if (!lpNext && bytesRead) {
2304 /* no more buffer to copy data to, but we did read more.
2305 * what hasn't been copied will be dropped
2307 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
2308 wwi->lpQueuePtr = NULL;
2309 break;
2317 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2319 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2322 TRACE("msg=0x%x param=0x%lx\n", msg, param);
2323 switch (msg) {
2324 case WINE_WM_PAUSING:
2325 wwi->state = WINE_WS_PAUSED;
2326 /*FIXME("Device should stop recording\n");*/
2327 SetEvent(ev);
2328 break;
2329 case WINE_WM_RESTARTING:
2331 int enable = PCM_ENABLE_INPUT;
2332 wwi->state = WINE_WS_PLAYING;
2334 if (wwi->bTriggerSupport)
2336 /* start the recording */
2337 if (ioctl(wwi->unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2339 ERR("ioctl(SNDCTL_DSP_SETTRIGGER) failed (%d)\n", errno);
2342 else
2344 unsigned char data[4];
2345 /* read 4 bytes to start the recording */
2346 read(wwi->unixdev, data, 4);
2349 SetEvent(ev);
2350 break;
2352 case WINE_WM_HEADER:
2353 lpWaveHdr = (LPWAVEHDR)param;
2354 lpWaveHdr->lpNext = 0;
2356 /* insert buffer at the end of queue */
2358 LPWAVEHDR* wh;
2359 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2360 *wh = lpWaveHdr;
2362 break;
2363 case WINE_WM_RESETTING:
2364 wwi->state = WINE_WS_STOPPED;
2365 /* return all buffers to the app */
2366 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2367 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2368 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2369 lpWaveHdr->dwFlags |= WHDR_DONE;
2371 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2373 wwi->lpQueuePtr = NULL;
2374 SetEvent(ev);
2375 break;
2376 case WINE_WM_CLOSING:
2377 wwi->hThread = 0;
2378 wwi->state = WINE_WS_CLOSED;
2379 SetEvent(ev);
2380 HeapFree(GetProcessHeap(), 0, buffer);
2381 ExitThread(0);
2382 /* shouldn't go here */
2383 default:
2384 FIXME("unknown message %d\n", msg);
2385 break;
2389 ExitThread(0);
2390 /* just for not generating compilation warnings... should never be executed */
2391 return 0;
2395 /**************************************************************************
2396 * widOpen [internal]
2398 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2400 WINE_WAVEIN* wwi;
2401 int fragment_size;
2402 int audio_fragment;
2403 DWORD ret;
2405 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
2406 if (lpDesc == NULL) {
2407 WARN("Invalid Parameter !\n");
2408 return MMSYSERR_INVALPARAM;
2410 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_BADDEVICEID;
2412 /* only PCM format is supported so far... */
2413 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
2414 lpDesc->lpFormat->nChannels == 0 ||
2415 lpDesc->lpFormat->nSamplesPerSec == 0) {
2416 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2417 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2418 lpDesc->lpFormat->nSamplesPerSec);
2419 return WAVERR_BADFORMAT;
2422 if (dwFlags & WAVE_FORMAT_QUERY) {
2423 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2424 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2425 lpDesc->lpFormat->nSamplesPerSec);
2426 return MMSYSERR_NOERROR;
2429 wwi = &WInDev[wDevID];
2431 if (wwi->unixdev != -1) return MMSYSERR_ALLOCATED;
2432 /* This is actually hand tuned to work so that my SB Live:
2433 * - does not skip
2434 * - does not buffer too much
2435 * when sending with the Shoutcast winamp plugin
2437 /* 15 fragments max, 2^10 = 1024 bytes per fragment */
2438 audio_fragment = 0x000F000A;
2439 ret = OSS_OpenDevice(wDevID, &wwi->unixdev,
2440 O_RDONLY, &audio_fragment,
2441 lpDesc->lpFormat->nSamplesPerSec,
2442 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
2443 (lpDesc->lpFormat->wBitsPerSample == 16)
2444 ? AFMT_S16_LE : AFMT_U8);
2445 if (ret != 0) return ret;
2446 if (wwi->lpQueuePtr) {
2447 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2448 wwi->lpQueuePtr = NULL;
2450 wwi->dwTotalRecorded = 0;
2451 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2453 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2454 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
2456 if (wwi->format.wBitsPerSample == 0) {
2457 WARN("Resetting zeroed wBitsPerSample\n");
2458 wwi->format.wBitsPerSample = 8 *
2459 (wwi->format.wf.nAvgBytesPerSec /
2460 wwi->format.wf.nSamplesPerSec) /
2461 wwi->format.wf.nChannels;
2464 ioctl(wwi->unixdev, SNDCTL_DSP_GETBLKSIZE, &fragment_size);
2465 if (fragment_size == -1) {
2466 WARN("IOCTL can't 'SNDCTL_DSP_GETBLKSIZE' !\n");
2467 OSS_CloseDevice(wDevID, wwi->unixdev);
2468 wwi->unixdev = -1;
2469 return MMSYSERR_NOTENABLED;
2471 wwi->dwFragmentSize = fragment_size;
2473 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2474 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
2475 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
2476 wwi->format.wf.nBlockAlign);
2478 OSS_InitRingMessage(&wwi->msgRing);
2480 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
2481 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
2482 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2483 CloseHandle(wwi->hStartUpEvent);
2484 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2486 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2489 /**************************************************************************
2490 * widClose [internal]
2492 static DWORD widClose(WORD wDevID)
2494 WINE_WAVEIN* wwi;
2496 TRACE("(%u);\n", wDevID);
2497 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].unixdev == -1) {
2498 WARN("can't close !\n");
2499 return MMSYSERR_INVALHANDLE;
2502 wwi = &WInDev[wDevID];
2504 if (wwi->lpQueuePtr != NULL) {
2505 WARN("still buffers open !\n");
2506 return WAVERR_STILLPLAYING;
2509 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
2510 OSS_CloseDevice(wDevID, wwi->unixdev);
2511 wwi->unixdev = -1;
2512 wwi->dwFragmentSize = 0;
2513 OSS_DestroyRingMessage(&wwi->msgRing);
2514 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
2517 /**************************************************************************
2518 * widAddBuffer [internal]
2520 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2522 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2524 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].unixdev == -1) {
2525 WARN("can't do it !\n");
2526 return MMSYSERR_INVALHANDLE;
2528 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2529 TRACE("never been prepared !\n");
2530 return WAVERR_UNPREPARED;
2532 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2533 TRACE("header already in use !\n");
2534 return WAVERR_STILLPLAYING;
2537 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2538 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2539 lpWaveHdr->dwBytesRecorded = 0;
2540 lpWaveHdr->lpNext = NULL;
2542 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2543 return MMSYSERR_NOERROR;
2546 /**************************************************************************
2547 * widPrepare [internal]
2549 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2551 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2553 if (wDevID >= MAX_WAVEDRV) return MMSYSERR_INVALHANDLE;
2555 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2556 return WAVERR_STILLPLAYING;
2558 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2559 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2560 lpWaveHdr->dwBytesRecorded = 0;
2561 TRACE("header prepared !\n");
2562 return MMSYSERR_NOERROR;
2565 /**************************************************************************
2566 * widUnprepare [internal]
2568 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2570 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;
2579 return MMSYSERR_NOERROR;
2582 /**************************************************************************
2583 * widStart [internal]
2585 static DWORD widStart(WORD wDevID)
2587 TRACE("(%u);\n", wDevID);
2588 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].unixdev == -1) {
2589 WARN("can't start recording !\n");
2590 return MMSYSERR_INVALHANDLE;
2593 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2594 return MMSYSERR_NOERROR;
2597 /**************************************************************************
2598 * widStop [internal]
2600 static DWORD widStop(WORD wDevID)
2602 TRACE("(%u);\n", wDevID);
2603 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].unixdev == -1) {
2604 WARN("can't stop !\n");
2605 return MMSYSERR_INVALHANDLE;
2607 /* FIXME: reset aint stop */
2608 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2610 return MMSYSERR_NOERROR;
2613 /**************************************************************************
2614 * widReset [internal]
2616 static DWORD widReset(WORD wDevID)
2618 TRACE("(%u);\n", wDevID);
2619 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].unixdev == -1) {
2620 WARN("can't reset !\n");
2621 return MMSYSERR_INVALHANDLE;
2623 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2624 return MMSYSERR_NOERROR;
2627 /**************************************************************************
2628 * widGetPosition [internal]
2630 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2632 int time;
2633 WINE_WAVEIN* wwi;
2635 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
2637 if (wDevID >= MAX_WAVEDRV || WInDev[wDevID].unixdev == -1) {
2638 WARN("can't get pos !\n");
2639 return MMSYSERR_INVALHANDLE;
2641 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
2643 wwi = &WInDev[wDevID];
2645 TRACE("wType=%04X !\n", lpTime->wType);
2646 TRACE("wBitsPerSample=%u\n", wwi->format.wBitsPerSample);
2647 TRACE("nSamplesPerSec=%lu\n", wwi->format.wf.nSamplesPerSec);
2648 TRACE("nChannels=%u\n", wwi->format.wf.nChannels);
2649 TRACE("nAvgBytesPerSec=%lu\n", wwi->format.wf.nAvgBytesPerSec);
2650 switch (lpTime->wType) {
2651 case TIME_BYTES:
2652 lpTime->u.cb = wwi->dwTotalRecorded;
2653 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
2654 break;
2655 case TIME_SAMPLES:
2656 lpTime->u.sample = wwi->dwTotalRecorded * 8 /
2657 wwi->format.wBitsPerSample;
2658 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
2659 break;
2660 case TIME_SMPTE:
2661 time = wwi->dwTotalRecorded /
2662 (wwi->format.wf.nAvgBytesPerSec / 1000);
2663 lpTime->u.smpte.hour = time / 108000;
2664 time -= lpTime->u.smpte.hour * 108000;
2665 lpTime->u.smpte.min = time / 1800;
2666 time -= lpTime->u.smpte.min * 1800;
2667 lpTime->u.smpte.sec = time / 30;
2668 time -= lpTime->u.smpte.sec * 30;
2669 lpTime->u.smpte.frame = time;
2670 lpTime->u.smpte.fps = 30;
2671 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
2672 lpTime->u.smpte.hour, lpTime->u.smpte.min,
2673 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
2674 break;
2675 case TIME_MS:
2676 lpTime->u.ms = wwi->dwTotalRecorded /
2677 (wwi->format.wf.nAvgBytesPerSec / 1000);
2678 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
2679 break;
2680 default:
2681 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime->wType);
2682 lpTime->wType = TIME_MS;
2684 return MMSYSERR_NOERROR;
2687 /**************************************************************************
2688 * widMessage (WINEOSS.6)
2690 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2691 DWORD dwParam1, DWORD dwParam2)
2693 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2694 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2696 switch (wMsg) {
2697 case DRVM_INIT:
2698 case DRVM_EXIT:
2699 case DRVM_ENABLE:
2700 case DRVM_DISABLE:
2701 /* FIXME: Pretend this is supported */
2702 return 0;
2703 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2704 case WIDM_CLOSE: return widClose (wDevID);
2705 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2706 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2707 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2708 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
2709 case WIDM_GETNUMDEVS: return numInDev;
2710 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
2711 case WIDM_RESET: return widReset (wDevID);
2712 case WIDM_START: return widStart (wDevID);
2713 case WIDM_STOP: return widStop (wDevID);
2714 default:
2715 FIXME("unknown message %u!\n", wMsg);
2717 return MMSYSERR_NOTSUPPORTED;
2720 #else /* !HAVE_OSS */
2722 /**************************************************************************
2723 * wodMessage (WINEOSS.7)
2725 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2726 DWORD dwParam1, DWORD dwParam2)
2728 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2729 return MMSYSERR_NOTENABLED;
2732 /**************************************************************************
2733 * widMessage (WINEOSS.6)
2735 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2736 DWORD dwParam1, DWORD dwParam2)
2738 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2739 return MMSYSERR_NOTENABLED;
2742 #endif /* HAVE_OSS */