Fixed SMPTE time.
[wine/multimedia.git] / dlls / winmm / wineoss / audio.c
blobe109a5b6ec2c060e7e9a33539920cd26e7ddf7b7
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
27 * Direct Sound Capture driver does not work (not complete yet)
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"
40 #include "wine/port.h"
42 #include <stdlib.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <string.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49 #include <errno.h>
50 #include <fcntl.h>
51 #ifdef HAVE_SYS_IOCTL_H
52 # include <sys/ioctl.h>
53 #endif
54 #ifdef HAVE_SYS_MMAN_H
55 # include <sys/mman.h>
56 #endif
57 #ifdef HAVE_SYS_POLL_H
58 # include <sys/poll.h>
59 #endif
61 #include "windef.h"
62 #include "winbase.h"
63 #include "wingdi.h"
64 #include "winerror.h"
65 #include "wine/winuser16.h"
66 #include "mmddk.h"
67 #include "dsound.h"
68 #include "dsdriver.h"
69 #include "oss.h"
70 #include "wine/debug.h"
72 WINE_DEFAULT_DEBUG_CHANNEL(wave);
74 /* Allow 1% deviation for sample rates (some ES137x cards) */
75 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
77 #ifdef HAVE_OSS
79 #define MAX_WAVEDRV (6)
81 /* state diagram for waveOut writing:
83 * +---------+-------------+---------------+---------------------------------+
84 * | state | function | event | new state |
85 * +---------+-------------+---------------+---------------------------------+
86 * | | open() | | STOPPED |
87 * | PAUSED | write() | | PAUSED |
88 * | STOPPED | write() | <thrd create> | PLAYING |
89 * | PLAYING | write() | HEADER | PLAYING |
90 * | (other) | write() | <error> | |
91 * | (any) | pause() | PAUSING | PAUSED |
92 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
93 * | (any) | reset() | RESETTING | STOPPED |
94 * | (any) | close() | CLOSING | CLOSED |
95 * +---------+-------------+---------------+---------------------------------+
98 /* states of the playing device */
99 #define WINE_WS_PLAYING 0
100 #define WINE_WS_PAUSED 1
101 #define WINE_WS_STOPPED 2
102 #define WINE_WS_CLOSED 3
104 /* events to be send to device */
105 enum win_wm_message {
106 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
107 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
110 #ifdef USE_PIPE_SYNC
111 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
112 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
113 #define RESET_OMR(omr) do { } while (0)
114 #define WAIT_OMR(omr, sleep) \
115 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
116 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
117 #else
118 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
119 #define CLEAR_OMR(omr) do { } while (0)
120 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
121 #define WAIT_OMR(omr, sleep) \
122 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
123 #endif
125 typedef struct {
126 enum win_wm_message msg; /* message identifier */
127 DWORD param; /* parameter for this message */
128 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
129 } OSS_MSG;
131 /* implement an in-process message ring for better performance
132 * (compared to passing thru the server)
133 * this ring will be used by the input (resp output) record (resp playback) routine
135 #define OSS_RING_BUFFER_INCREMENT 64
136 typedef struct {
137 int ring_buffer_size;
138 OSS_MSG * messages;
139 int msg_tosave;
140 int msg_toget;
141 #ifdef USE_PIPE_SYNC
142 int msg_pipe[2];
143 #else
144 HANDLE msg_event;
145 #endif
146 CRITICAL_SECTION msg_crst;
147 } OSS_MSG_RING;
149 typedef struct tagOSS_DEVICE {
150 char dev_name[32];
151 char mixer_name[32];
152 unsigned open_count;
153 WAVEOUTCAPSA out_caps;
154 WAVEINCAPSA in_caps;
155 DWORD in_caps_support;
156 unsigned open_access;
157 int fd;
158 DWORD owner_tid;
159 int sample_rate;
160 int stereo;
161 int format;
162 unsigned audio_fragment;
163 BOOL full_duplex;
164 BOOL bTriggerSupport;
165 BOOL bOutputEnabled;
166 BOOL bInputEnabled;
167 DSDRIVERDESC ds_desc;
168 DSDRIVERCAPS ds_caps;
169 DSCDRIVERCAPS dsc_caps;
170 GUID ds_guid;
171 GUID dsc_guid;
172 } OSS_DEVICE;
174 static OSS_DEVICE OSS_Devices[MAX_WAVEDRV];
176 typedef struct {
177 OSS_DEVICE* ossdev;
178 volatile int state; /* one of the WINE_WS_ manifest constants */
179 WAVEOPENDESC waveDesc;
180 WORD wFlags;
181 PCMWAVEFORMAT format;
183 /* OSS information */
184 DWORD dwFragmentSize; /* size of OSS buffer fragment */
185 DWORD dwBufferSize; /* size of whole OSS buffer in bytes */
186 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
187 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
188 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
190 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
191 DWORD dwLoops; /* private copy of loop counter */
193 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
194 DWORD dwWrittenTotal; /* number of bytes written to OSS buffer since opening */
195 BOOL bNeedPost; /* whether audio still needs to be physically started */
197 /* synchronization stuff */
198 HANDLE hStartUpEvent;
199 HANDLE hThread;
200 DWORD dwThreadID;
201 OSS_MSG_RING msgRing;
203 /* DirectSound stuff */
204 LPBYTE mapping;
205 DWORD maplen;
206 } WINE_WAVEOUT;
208 typedef struct {
209 OSS_DEVICE* ossdev;
210 volatile int state;
211 DWORD dwFragmentSize; /* OpenSound '/dev/dsp' give us that size */
212 WAVEOPENDESC waveDesc;
213 WORD wFlags;
214 PCMWAVEFORMAT format;
215 LPWAVEHDR lpQueuePtr;
216 DWORD dwTotalRecorded;
218 /* synchronization stuff */
219 HANDLE hThread;
220 DWORD dwThreadID;
221 HANDLE hStartUpEvent;
222 OSS_MSG_RING msgRing;
224 /* DirectSound stuff */
225 LPBYTE mapping;
226 DWORD maplen;
227 } WINE_WAVEIN;
229 static WINE_WAVEOUT WOutDev [MAX_WAVEDRV];
230 static WINE_WAVEIN WInDev [MAX_WAVEDRV];
231 static unsigned numOutDev;
232 static unsigned numInDev;
234 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
235 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv);
236 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
237 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc);
238 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid);
239 static DWORD widDsGuid(UINT wDevID, LPGUID pGuid);
241 /* These strings used only for tracing */
242 static const char *wodPlayerCmdString[] = {
243 "WINE_WM_PAUSING",
244 "WINE_WM_RESTARTING",
245 "WINE_WM_RESETTING",
246 "WINE_WM_HEADER",
247 "WINE_WM_UPDATE",
248 "WINE_WM_BREAKLOOP",
249 "WINE_WM_CLOSING",
250 "WINE_WM_STARTING",
251 "WINE_WM_STOPPING",
254 static int getEnables(OSS_DEVICE *ossdev)
256 return ( (ossdev->bOutputEnabled ? PCM_ENABLE_OUTPUT : 0) |
257 (ossdev->bInputEnabled ? PCM_ENABLE_INPUT : 0) );
260 /*======================================================================*
261 * Low level WAVE implementation *
262 *======================================================================*/
264 /******************************************************************
265 * OSS_RawOpenDevice
267 * Low level device opening (from values stored in ossdev)
269 static DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
271 int fd, val, rc;
272 TRACE("(%p,%d)\n",ossdev,strict_format);
274 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
276 WARN("Couldn't open %s (%s)\n", ossdev->dev_name, strerror(errno));
277 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
279 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
280 /* turn full duplex on if it has been requested */
281 if (ossdev->open_access == O_RDWR && ossdev->full_duplex) {
282 rc = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
283 /* on *BSD, as full duplex is always enabled by default, this ioctl
284 * will fail with EINVAL
285 * so, we don't consider EINVAL an error here
287 if (rc != 0 && errno != EINVAL) {
288 ERR("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev->dev_name, strerror(errno));
289 goto error2;
293 if (ossdev->audio_fragment) {
294 rc = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
295 if (rc != 0) {
296 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev->dev_name, strerror(errno));
297 goto error2;
301 /* First size and stereo then samplerate */
302 if (ossdev->format>=0)
304 val = ossdev->format;
305 rc = ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
306 if (rc != 0 || val != ossdev->format) {
307 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
308 if (strict_format)
309 goto error;
312 if (ossdev->stereo>=0)
314 val = ossdev->stereo;
315 rc = ioctl(fd, SNDCTL_DSP_STEREO, &ossdev->stereo);
316 if (rc != 0 || val != ossdev->stereo) {
317 TRACE("Can't set stereo to %u (returned %d)\n", val, ossdev->stereo);
318 if (strict_format)
319 goto error;
322 if (ossdev->sample_rate>=0)
324 val = ossdev->sample_rate;
325 rc = ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
326 if (rc != 0 || !NEAR_MATCH(val, ossdev->sample_rate)) {
327 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
328 if (strict_format)
329 goto error;
332 ossdev->fd = fd;
334 if (ossdev->bTriggerSupport) {
335 int trigger;
336 rc = ioctl(fd, SNDCTL_DSP_GETTRIGGER, &trigger);
337 if (rc != 0) {
338 ERR("ioctl(%s, SNDCTL_DSP_GETTRIGGER) failed (%s)\n",
339 ossdev->dev_name, strerror(errno));
340 goto error;
343 ossdev->bOutputEnabled = ((trigger & PCM_ENABLE_OUTPUT) == PCM_ENABLE_OUTPUT);
344 ossdev->bInputEnabled = ((trigger & PCM_ENABLE_INPUT) == PCM_ENABLE_INPUT);
345 } else {
346 ossdev->bOutputEnabled = TRUE; /* OSS enables by default */
347 ossdev->bInputEnabled = TRUE; /* OSS enables by default */
350 return MMSYSERR_NOERROR;
352 error:
353 close(fd);
354 return WAVERR_BADFORMAT;
355 error2:
356 close(fd);
357 return MMSYSERR_ERROR;
360 /******************************************************************
361 * OSS_OpenDevice
363 * since OSS has poor capabilities in full duplex, we try here to let a program
364 * open the device for both waveout and wavein streams...
365 * this is hackish, but it's the way OSS interface is done...
367 static DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
368 int* frag, int strict_format,
369 int sample_rate, int stereo, int fmt)
371 DWORD ret;
372 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev,req_access,frag,strict_format,sample_rate,stereo,fmt);
374 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
375 req_access = O_RDWR;
377 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
378 if (ossdev->open_count == 0)
380 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
382 ossdev->audio_fragment = (frag) ? *frag : 0;
383 ossdev->sample_rate = sample_rate;
384 ossdev->stereo = stereo;
385 ossdev->format = fmt;
386 ossdev->open_access = req_access;
387 ossdev->owner_tid = GetCurrentThreadId();
389 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
391 else
393 /* check we really open with the same parameters */
394 if (ossdev->open_access != req_access)
396 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
397 return WAVERR_BADFORMAT;
400 /* check if the audio parameters are the same */
401 if (ossdev->sample_rate != sample_rate ||
402 ossdev->stereo != stereo ||
403 ossdev->format != fmt)
405 /* This is not a fatal error because MSACM might do the remapping */
406 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
407 "OSS doesn't allow us different parameters\n"
408 "audio_frag(%x/%x) sample_rate(%d/%d) stereo(%d/%d) fmt(%d/%d)\n",
409 ossdev->audio_fragment, frag ? *frag : 0,
410 ossdev->sample_rate, sample_rate,
411 ossdev->stereo, stereo,
412 ossdev->format, fmt);
413 return WAVERR_BADFORMAT;
415 /* check if the fragment sizes are the same */
416 if (ossdev->audio_fragment != (frag ? *frag : 0) ) {
417 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
418 "Use: \"HardwareAcceleration\" = \"Emulation\" in the [dsound] section of your config file.\n");
419 return WAVERR_BADFORMAT;
421 if (GetCurrentThreadId() != ossdev->owner_tid)
423 WARN("Another thread is trying to access audio...\n");
424 return MMSYSERR_ERROR;
428 ossdev->open_count++;
430 return MMSYSERR_NOERROR;
433 /******************************************************************
434 * OSS_CloseDevice
438 static void OSS_CloseDevice(OSS_DEVICE* ossdev)
440 TRACE("(%p)\n",ossdev);
441 if (ossdev->open_count>0) {
442 ossdev->open_count--;
443 } else {
444 WARN("OSS_CloseDevice called too many times\n");
446 if (ossdev->open_count == 0)
448 /* reset the device before we close it in case it is in a bad state */
449 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
450 close(ossdev->fd);
454 /******************************************************************
455 * OSS_ResetDevice
457 * Resets the device. OSS Commercial requires the device to be closed
458 * after a SNDCTL_DSP_RESET ioctl call... this function implements
459 * this behavior...
460 * FIXME: This causes problems when doing full duplex so we really
461 * only reset when not doing full duplex. We need to do this better
462 * someday.
464 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
466 DWORD ret = MMSYSERR_NOERROR;
467 int old_fd = ossdev->fd;
468 TRACE("(%p)\n", ossdev);
470 if (ossdev->open_count == 1) {
471 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
473 perror("ioctl SNDCTL_DSP_RESET");
474 return -1;
476 close(ossdev->fd);
477 ret = OSS_RawOpenDevice(ossdev, 1);
478 TRACE("Changing fd from %d to %d\n", old_fd, ossdev->fd);
479 } else
480 WARN("Not resetting device because it is in full duplex mode!\n");
482 return ret;
485 const static int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
486 const static int win_std_rates[5]={96000,48000,44100,22050,11025};
487 const static int win_std_formats[2][2][5]=
488 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
489 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
490 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
491 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
492 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
493 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
494 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
495 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
498 /******************************************************************
499 * OSS_WaveOutInit
503 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
505 int rc,arg;
506 int f,c,r;
507 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
509 if (OSS_OpenDevice(ossdev, O_WRONLY, NULL, 0,-1,-1,-1) != 0) return FALSE;
510 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
512 #ifdef SOUND_MIXER_INFO
514 int mixer;
515 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
516 mixer_info info;
517 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
518 close(mixer);
519 strncpy(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
520 strcpy(ossdev->ds_desc.szDrvName, "wineoss.drv");
521 strncpy(ossdev->out_caps.szPname, info.name, sizeof(info.name));
522 TRACE("%s\n", ossdev->ds_desc.szDesc);
523 } else {
524 ERR("%s: can't read info!\n", ossdev->mixer_name);
525 OSS_CloseDevice(ossdev);
526 close(mixer);
527 return FALSE;
529 } else {
530 ERR("%s: %s\n", ossdev->mixer_name , strerror( errno ));
531 OSS_CloseDevice(ossdev);
532 return FALSE;
535 #endif /* SOUND_MIXER_INFO */
537 /* FIXME: some programs compare this string against the content of the
538 * registry for MM drivers. The names have to match in order for the
539 * program to work (e.g. MS win9x mplayer.exe)
541 #ifdef EMULATE_SB16
542 ossdev->out_caps.wMid = 0x0002;
543 ossdev->out_caps.wPid = 0x0104;
544 strcpy(ossdev->out_caps.szPname, "SB16 Wave Out");
545 #else
546 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
547 ossdev->out_caps.wPid = 0x0001; /* Product ID */
548 #endif
549 ossdev->out_caps.vDriverVersion = 0x0100;
550 ossdev->out_caps.wChannels = 1;
551 ossdev->out_caps.dwFormats = 0x00000000;
552 ossdev->out_caps.wReserved1 = 0;
553 ossdev->out_caps.dwSupport = WAVECAPS_VOLUME;
555 /* direct sound caps */
556 ossdev->ds_caps.dwFlags = 0;
557 ossdev->ds_caps.dwPrimaryBuffers = 1;
558 ossdev->ds_caps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
559 ossdev->ds_caps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
561 if (WINE_TRACE_ON(wave)) {
562 /* Note that this only reports the formats supported by the hardware.
563 * The driver may support other formats and do the conversions in
564 * software which is why we don't use this value
566 int oss_mask;
567 ioctl(ossdev->fd, SNDCTL_DSP_GETFMTS, &oss_mask);
568 TRACE("OSS dsp out mask=%08x\n", oss_mask);
571 /* We must first set the format and the stereo mode as some sound cards
572 * may support 44kHz mono but not 44kHz stereo. Also we must
573 * systematically check the return value of these ioctls as they will
574 * always succeed (see OSS Linux) but will modify the parameter to match
575 * whatever they support. The OSS specs also say we must first set the
576 * sample size, then the stereo and then the sample rate.
578 for (f=0;f<2;f++) {
579 arg=win_std_oss_fmts[f];
580 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
581 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
582 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
583 rc,arg,win_std_oss_fmts[f]);
584 continue;
586 if (f == 0)
587 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY8BIT;
588 else if (f == 1)
589 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY16BIT;
591 for (c=0;c<2;c++) {
592 arg=c;
593 rc=ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &arg);
594 if (rc!=0 || arg!=c) {
595 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc,arg,c);
596 continue;
598 if (c == 0) {
599 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
600 } else if (c==1) {
601 ossdev->out_caps.wChannels=2;
602 ossdev->out_caps.dwSupport|=WAVECAPS_LRVOLUME;
603 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
606 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
607 arg=win_std_rates[r];
608 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
609 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
610 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c+1);
611 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]))
612 ossdev->out_caps.dwFormats|=win_std_formats[f][c][r];
617 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
618 TRACE("OSS dsp out caps=%08X\n", arg);
619 if (arg & DSP_CAP_TRIGGER)
620 ossdev->bTriggerSupport = TRUE;
621 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
622 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
624 /* well, might as well use the DirectSound cap flag for something */
625 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
626 !(arg & DSP_CAP_BATCH)) {
627 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
628 } else {
629 ossdev->ds_caps.dwFlags |= DSCAPS_EMULDRIVER;
632 OSS_CloseDevice(ossdev);
633 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
634 ossdev->out_caps.dwFormats, ossdev->out_caps.dwSupport);
635 return TRUE;
638 /******************************************************************
639 * OSS_WaveInInit
643 static BOOL OSS_WaveInInit(OSS_DEVICE* ossdev)
645 int rc,arg;
646 int f,c,r;
647 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
649 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0,-1,-1,-1) != 0)
650 return FALSE;
652 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
654 #ifdef SOUND_MIXER_INFO
656 int mixer;
657 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
658 mixer_info info;
659 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
660 close(mixer);
661 strncpy(ossdev->in_caps.szPname, info.name, sizeof(info.name));
662 TRACE("%s\n", ossdev->ds_desc.szDesc);
663 } else {
664 ERR("%s: can't read info!\n", ossdev->mixer_name);
665 OSS_CloseDevice(ossdev);
666 close(mixer);
667 return FALSE;
669 } else {
670 ERR("%s: %s\n", ossdev->mixer_name, strerror(errno));
671 OSS_CloseDevice(ossdev);
672 return FALSE;
675 #endif /* SOUND_MIXER_INFO */
677 /* See comment in OSS_WaveOutInit */
678 #ifdef EMULATE_SB16
679 ossdev->in_caps.wMid = 0x0002;
680 ossdev->in_caps.wPid = 0x0004;
681 strcpy(ossdev->in_caps.szPname, "SB16 Wave In");
682 #else
683 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
684 ossdev->in_caps.wPid = 0x0001; /* Product ID */
685 #endif
686 ossdev->in_caps.dwFormats = 0x00000000;
687 ossdev->in_caps.wChannels = 1;
688 ossdev->in_caps.wReserved1 = 0;
690 /* direct sound caps */
691 ossdev->dsc_caps.dwSize = sizeof(ossdev->dsc_caps);
692 ossdev->dsc_caps.dwFlags = 0;
693 ossdev->dsc_caps.dwFormats = 0x00000000;
694 ossdev->dsc_caps.dwChannels = 1;
696 if (WINE_TRACE_ON(wave)) {
697 /* Note that this only reports the formats supported by the hardware.
698 * The driver may support other formats and do the conversions in
699 * software which is why we don't use this value
701 int oss_mask;
702 ioctl(ossdev->fd, SNDCTL_DSP_GETFMTS, &oss_mask);
703 TRACE("OSS dsp out mask=%08x\n", oss_mask);
706 /* See the comment in OSS_WaveOutInit */
707 for (f=0;f<2;f++) {
708 arg=win_std_oss_fmts[f];
709 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
710 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
711 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
712 rc,arg,win_std_oss_fmts[f]);
713 continue;
716 for (c=0;c<2;c++) {
717 arg=c;
718 rc=ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &arg);
719 if (rc!=0 || arg!=c) {
720 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc,arg,c);
721 continue;
723 if (c==1) {
724 ossdev->in_caps.wChannels=2;
725 ossdev->dsc_caps.dwChannels=2;
728 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
729 arg=win_std_rates[r];
730 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
731 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);
732 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]))
733 ossdev->in_caps.dwFormats|=win_std_formats[f][c][r];
734 ossdev->dsc_caps.dwFormats|=win_std_formats[f][c][r];
739 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
740 TRACE("OSS dsp in caps=%08X\n", arg);
741 if (arg & DSP_CAP_TRIGGER)
742 ossdev->bTriggerSupport = TRUE;
743 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
744 !(arg & DSP_CAP_BATCH)) {
745 /* FIXME: enable the next statement if you want to work on the driver */
746 #if 0
747 ossdev->in_caps_support |= WAVECAPS_DIRECTSOUND;
748 #endif
750 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH))
751 ossdev->in_caps_support |= WAVECAPS_SAMPLEACCURATE;
753 OSS_CloseDevice(ossdev);
754 TRACE("in dwFormats = %08lX\n", ossdev->in_caps.dwFormats);
755 return TRUE;
758 /******************************************************************
759 * OSS_WaveFullDuplexInit
763 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
765 int caps;
766 TRACE("(%p)\n",ossdev);
768 if (OSS_OpenDevice(ossdev, O_RDWR, NULL, 0,-1,-1,-1) != 0) return;
769 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
771 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
773 OSS_CloseDevice(ossdev);
776 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
777 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
778 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
779 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
780 guid.Data4[6] = b7; guid.Data4[7] = b8;
781 /******************************************************************
782 * OSS_WaveInit
784 * Initialize internal structures from OSS information
786 LONG OSS_WaveInit(void)
788 int i;
789 TRACE("()\n");
791 for (i = 0; i < MAX_WAVEDRV; ++i)
793 if (i == 0) {
794 sprintf((char *)OSS_Devices[i].dev_name, "/dev/dsp");
795 sprintf((char *)OSS_Devices[i].mixer_name, "/dev/mixer");
796 } else {
797 sprintf((char *)OSS_Devices[i].dev_name, "/dev/dsp%d", i);
798 sprintf((char *)OSS_Devices[i].mixer_name, "/dev/mixer%d", i);
801 INIT_GUID(OSS_Devices[i].ds_guid, 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
802 INIT_GUID(OSS_Devices[i].dsc_guid, 0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
805 /* start with output devices */
806 for (i = 0; i < MAX_WAVEDRV; ++i)
808 if (OSS_WaveOutInit(&OSS_Devices[i]))
810 WOutDev[numOutDev].state = WINE_WS_CLOSED;
811 WOutDev[numOutDev].ossdev = &OSS_Devices[i];
812 numOutDev++;
816 /* then do input devices */
817 for (i = 0; i < MAX_WAVEDRV; ++i)
819 if (OSS_WaveInInit(&OSS_Devices[i]))
821 WInDev[numInDev].state = WINE_WS_CLOSED;
822 WInDev[numInDev].ossdev = &OSS_Devices[i];
823 numInDev++;
827 /* finish with the full duplex bits */
828 for (i = 0; i < MAX_WAVEDRV; i++)
829 OSS_WaveFullDuplexInit(&OSS_Devices[i]);
831 return 0;
834 /******************************************************************
835 * OSS_InitRingMessage
837 * Initialize the ring of messages for passing between driver's caller and playback/record
838 * thread
840 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
842 omr->msg_toget = 0;
843 omr->msg_tosave = 0;
844 #ifdef USE_PIPE_SYNC
845 if (pipe(omr->msg_pipe) < 0) {
846 omr->msg_pipe[0] = -1;
847 omr->msg_pipe[1] = -1;
848 ERR("could not create pipe, error=%s\n", strerror(errno));
850 #else
851 omr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
852 #endif
853 omr->ring_buffer_size = OSS_RING_BUFFER_INCREMENT;
854 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(OSS_MSG));
855 InitializeCriticalSection(&omr->msg_crst);
856 return 0;
859 /******************************************************************
860 * OSS_DestroyRingMessage
863 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
865 #ifdef USE_PIPE_SYNC
866 close(omr->msg_pipe[0]);
867 close(omr->msg_pipe[1]);
868 #else
869 CloseHandle(omr->msg_event);
870 #endif
871 HeapFree(GetProcessHeap(),0,omr->messages);
872 DeleteCriticalSection(&omr->msg_crst);
873 return 0;
876 /******************************************************************
877 * OSS_AddRingMessage
879 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
881 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
883 HANDLE hEvent = INVALID_HANDLE_VALUE;
885 EnterCriticalSection(&omr->msg_crst);
886 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
888 omr->ring_buffer_size += OSS_RING_BUFFER_INCREMENT;
889 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
890 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(OSS_MSG));
892 if (wait)
894 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
895 if (hEvent == INVALID_HANDLE_VALUE)
897 ERR("can't create event !?\n");
898 LeaveCriticalSection(&omr->msg_crst);
899 return 0;
901 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
902 FIXME("two fast messages in the queue!!!!\n");
904 /* fast messages have to be added at the start of the queue */
905 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
907 omr->messages[omr->msg_toget].msg = msg;
908 omr->messages[omr->msg_toget].param = param;
909 omr->messages[omr->msg_toget].hEvent = hEvent;
911 else
913 omr->messages[omr->msg_tosave].msg = msg;
914 omr->messages[omr->msg_tosave].param = param;
915 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
916 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
918 LeaveCriticalSection(&omr->msg_crst);
919 /* signal a new message */
920 SIGNAL_OMR(omr);
921 if (wait)
923 /* wait for playback/record thread to have processed the message */
924 WaitForSingleObject(hEvent, INFINITE);
925 CloseHandle(hEvent);
927 return 1;
930 /******************************************************************
931 * OSS_RetrieveRingMessage
933 * Get a message from the ring. Should be called by the playback/record thread.
935 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
936 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
938 EnterCriticalSection(&omr->msg_crst);
940 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
942 LeaveCriticalSection(&omr->msg_crst);
943 return 0;
946 *msg = omr->messages[omr->msg_toget].msg;
947 omr->messages[omr->msg_toget].msg = 0;
948 *param = omr->messages[omr->msg_toget].param;
949 *hEvent = omr->messages[omr->msg_toget].hEvent;
950 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
951 CLEAR_OMR(omr);
952 LeaveCriticalSection(&omr->msg_crst);
953 return 1;
956 /******************************************************************
957 * OSS_PeekRingMessage
959 * Peek at a message from the ring but do not remove it.
960 * Should be called by the playback/record thread.
962 static int OSS_PeekRingMessage(OSS_MSG_RING* omr,
963 enum win_wm_message *msg,
964 DWORD *param, HANDLE *hEvent)
966 EnterCriticalSection(&omr->msg_crst);
968 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
970 LeaveCriticalSection(&omr->msg_crst);
971 return 0;
974 *msg = omr->messages[omr->msg_toget].msg;
975 *param = omr->messages[omr->msg_toget].param;
976 *hEvent = omr->messages[omr->msg_toget].hEvent;
977 LeaveCriticalSection(&omr->msg_crst);
978 return 1;
981 /*======================================================================*
982 * Low level WAVE OUT implementation *
983 *======================================================================*/
985 /**************************************************************************
986 * wodNotifyClient [internal]
988 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
990 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
992 switch (wMsg) {
993 case WOM_OPEN:
994 case WOM_CLOSE:
995 case WOM_DONE:
996 if (wwo->wFlags != DCB_NULL &&
997 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
998 (HDRVR)wwo->waveDesc.hWave, wMsg,
999 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
1000 WARN("can't notify client !\n");
1001 return MMSYSERR_ERROR;
1003 break;
1004 default:
1005 FIXME("Unknown callback message %u\n", wMsg);
1006 return MMSYSERR_INVALPARAM;
1008 return MMSYSERR_NOERROR;
1011 /**************************************************************************
1012 * wodUpdatePlayedTotal [internal]
1015 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
1017 audio_buf_info dspspace;
1018 if (!info) info = &dspspace;
1020 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
1021 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
1022 return FALSE;
1024 wwo->dwPlayedTotal = wwo->dwWrittenTotal - (wwo->dwBufferSize - info->bytes);
1025 return TRUE;
1028 /**************************************************************************
1029 * wodPlayer_BeginWaveHdr [internal]
1031 * Makes the specified lpWaveHdr the currently playing wave header.
1032 * If the specified wave header is a begin loop and we're not already in
1033 * a loop, setup the loop.
1035 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1037 wwo->lpPlayPtr = lpWaveHdr;
1039 if (!lpWaveHdr) return;
1041 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1042 if (wwo->lpLoopPtr) {
1043 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1044 } else {
1045 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1046 wwo->lpLoopPtr = lpWaveHdr;
1047 /* Windows does not touch WAVEHDR.dwLoops,
1048 * so we need to make an internal copy */
1049 wwo->dwLoops = lpWaveHdr->dwLoops;
1052 wwo->dwPartialOffset = 0;
1055 /**************************************************************************
1056 * wodPlayer_PlayPtrNext [internal]
1058 * Advance the play pointer to the next waveheader, looping if required.
1060 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
1062 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1064 wwo->dwPartialOffset = 0;
1065 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1066 /* We're at the end of a loop, loop if required */
1067 if (--wwo->dwLoops > 0) {
1068 wwo->lpPlayPtr = wwo->lpLoopPtr;
1069 } else {
1070 /* Handle overlapping loops correctly */
1071 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1072 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1073 /* shall we consider the END flag for the closing loop or for
1074 * the opening one or for both ???
1075 * code assumes for closing loop only
1077 } else {
1078 lpWaveHdr = lpWaveHdr->lpNext;
1080 wwo->lpLoopPtr = NULL;
1081 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1083 } else {
1084 /* We're not in a loop. Advance to the next wave header */
1085 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1088 return lpWaveHdr;
1091 /**************************************************************************
1092 * wodPlayer_DSPWait [internal]
1093 * Returns the number of milliseconds to wait for the DSP buffer to write
1094 * one fragment.
1096 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1098 /* time for one fragment to be played */
1099 return wwo->dwFragmentSize * 1000 / wwo->format.wf.nAvgBytesPerSec;
1102 /**************************************************************************
1103 * wodPlayer_NotifyWait [internal]
1104 * Returns the number of milliseconds to wait before attempting to notify
1105 * completion of the specified wavehdr.
1106 * This is based on the number of bytes remaining to be written in the
1107 * wave.
1109 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1111 DWORD dwMillis;
1113 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1114 dwMillis = 1;
1115 } else {
1116 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
1117 if (!dwMillis) dwMillis = 1;
1120 return dwMillis;
1124 /**************************************************************************
1125 * wodPlayer_WriteMaxFrags [internal]
1126 * Writes the maximum number of bytes possible to the DSP and returns
1127 * TRUE iff the current playPtr has been fully played
1129 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
1131 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1132 DWORD toWrite = min(dwLength, *bytes);
1133 int written;
1134 BOOL ret = FALSE;
1136 TRACE("Writing wavehdr %p.%lu[%lu]/%lu\n",
1137 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
1139 if (toWrite > 0)
1141 written = write(wwo->ossdev->fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
1142 if (written <= 0) return FALSE;
1144 else
1145 written = 0;
1147 if (written >= dwLength) {
1148 /* If we wrote all current wavehdr, skip to the next one */
1149 wodPlayer_PlayPtrNext(wwo);
1150 ret = TRUE;
1151 } else {
1152 /* Remove the amount written */
1153 wwo->dwPartialOffset += written;
1155 *bytes -= written;
1156 wwo->dwWrittenTotal += written;
1158 return ret;
1162 /**************************************************************************
1163 * wodPlayer_NotifyCompletions [internal]
1165 * Notifies and remove from queue all wavehdrs which have been played to
1166 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1167 * we notify all wavehdrs and remove them all from the queue even if they
1168 * are unplayed or part of a loop.
1170 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1172 LPWAVEHDR lpWaveHdr;
1174 /* Start from lpQueuePtr and keep notifying until:
1175 * - we hit an unwritten wavehdr
1176 * - we hit the beginning of a running loop
1177 * - we hit a wavehdr which hasn't finished playing
1179 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1180 (force ||
1181 (lpWaveHdr != wwo->lpPlayPtr &&
1182 lpWaveHdr != wwo->lpLoopPtr &&
1183 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1185 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1187 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1188 lpWaveHdr->dwFlags |= WHDR_DONE;
1190 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1192 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1193 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1196 /**************************************************************************
1197 * wodPlayer_Reset [internal]
1199 * wodPlayer helper. Resets current output stream.
1201 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
1203 wodUpdatePlayedTotal(wwo, NULL);
1204 /* updates current notify list */
1205 wodPlayer_NotifyCompletions(wwo, FALSE);
1207 /* flush all possible output */
1208 if (OSS_ResetDevice(wwo->ossdev) != MMSYSERR_NOERROR)
1210 wwo->hThread = 0;
1211 wwo->state = WINE_WS_STOPPED;
1212 ExitThread(-1);
1215 if (reset) {
1216 enum win_wm_message msg;
1217 DWORD param;
1218 HANDLE ev;
1220 /* remove any buffer */
1221 wodPlayer_NotifyCompletions(wwo, TRUE);
1223 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1224 wwo->state = WINE_WS_STOPPED;
1225 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1226 /* Clear partial wavehdr */
1227 wwo->dwPartialOffset = 0;
1229 /* remove any existing message in the ring */
1230 EnterCriticalSection(&wwo->msgRing.msg_crst);
1231 /* return all pending headers in queue */
1232 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1234 if (msg != WINE_WM_HEADER)
1236 FIXME("shouldn't have headers left\n");
1237 SetEvent(ev);
1238 continue;
1240 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1241 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1243 wodNotifyClient(wwo, WOM_DONE, param, 0);
1245 RESET_OMR(&wwo->msgRing);
1246 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1247 } else {
1248 if (wwo->lpLoopPtr) {
1249 /* complicated case, not handled yet (could imply modifying the loop counter */
1250 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1251 wwo->lpPlayPtr = wwo->lpLoopPtr;
1252 wwo->dwPartialOffset = 0;
1253 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1254 } else {
1255 LPWAVEHDR ptr;
1256 DWORD sz = wwo->dwPartialOffset;
1258 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1259 /* compute the max size playable from lpQueuePtr */
1260 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1261 sz += ptr->dwBufferLength;
1263 /* because the reset lpPlayPtr will be lpQueuePtr */
1264 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1265 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1266 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1267 wwo->lpPlayPtr = wwo->lpQueuePtr;
1269 wwo->state = WINE_WS_PAUSED;
1273 /**************************************************************************
1274 * wodPlayer_ProcessMessages [internal]
1276 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1278 LPWAVEHDR lpWaveHdr;
1279 enum win_wm_message msg;
1280 DWORD param;
1281 HANDLE ev;
1283 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1284 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
1285 switch (msg) {
1286 case WINE_WM_PAUSING:
1287 wodPlayer_Reset(wwo, FALSE);
1288 SetEvent(ev);
1289 break;
1290 case WINE_WM_RESTARTING:
1291 if (wwo->state == WINE_WS_PAUSED)
1293 wwo->state = WINE_WS_PLAYING;
1295 SetEvent(ev);
1296 break;
1297 case WINE_WM_HEADER:
1298 lpWaveHdr = (LPWAVEHDR)param;
1300 /* insert buffer at the end of queue */
1302 LPWAVEHDR* wh;
1303 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1304 *wh = lpWaveHdr;
1306 if (!wwo->lpPlayPtr)
1307 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1308 if (wwo->state == WINE_WS_STOPPED)
1309 wwo->state = WINE_WS_PLAYING;
1310 break;
1311 case WINE_WM_RESETTING:
1312 wodPlayer_Reset(wwo, TRUE);
1313 SetEvent(ev);
1314 break;
1315 case WINE_WM_UPDATE:
1316 wodUpdatePlayedTotal(wwo, NULL);
1317 SetEvent(ev);
1318 break;
1319 case WINE_WM_BREAKLOOP:
1320 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1321 /* ensure exit at end of current loop */
1322 wwo->dwLoops = 1;
1324 SetEvent(ev);
1325 break;
1326 case WINE_WM_CLOSING:
1327 /* sanity check: this should not happen since the device must have been reset before */
1328 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1329 wwo->hThread = 0;
1330 wwo->state = WINE_WS_CLOSED;
1331 SetEvent(ev);
1332 ExitThread(0);
1333 /* shouldn't go here */
1334 default:
1335 FIXME("unknown message %d\n", msg);
1336 break;
1341 /**************************************************************************
1342 * wodPlayer_FeedDSP [internal]
1343 * Feed as much sound data as we can into the DSP and return the number of
1344 * milliseconds before it will be necessary to feed the DSP again.
1346 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1348 audio_buf_info dspspace;
1349 DWORD availInQ;
1351 wodUpdatePlayedTotal(wwo, &dspspace);
1352 availInQ = dspspace.bytes;
1353 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1354 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1356 /* input queue empty and output buffer with less than one fragment to play
1357 * actually some cards do not play the fragment before the last if this one is partially feed
1358 * so we need to test for full the availability of 2 fragments
1360 if (!wwo->lpPlayPtr && wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize &&
1361 !wwo->bNeedPost) {
1362 TRACE("Run out of wavehdr:s...\n");
1363 return INFINITE;
1366 /* no more room... no need to try to feed */
1367 if (dspspace.fragments != 0) {
1368 /* Feed from partial wavehdr */
1369 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1370 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1373 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1374 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1375 do {
1376 TRACE("Setting time to elapse for %p to %lu\n",
1377 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1378 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1379 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1380 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1383 if (wwo->bNeedPost) {
1384 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1385 * if it didn't get one, we give it the other */
1386 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1387 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1388 wwo->bNeedPost = FALSE;
1392 return wodPlayer_DSPWait(wwo);
1396 /**************************************************************************
1397 * wodPlayer [internal]
1399 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1401 WORD uDevID = (DWORD)pmt;
1402 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1403 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1404 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1405 DWORD dwSleepTime;
1407 wwo->state = WINE_WS_STOPPED;
1408 SetEvent(wwo->hStartUpEvent);
1410 for (;;) {
1411 /** Wait for the shortest time before an action is required. If there
1412 * are no pending actions, wait forever for a command.
1414 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1415 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1416 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1417 wodPlayer_ProcessMessages(wwo);
1418 if (wwo->state == WINE_WS_PLAYING) {
1419 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1420 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1421 if (dwNextFeedTime == INFINITE) {
1422 /* FeedDSP ran out of data, but before flushing, */
1423 /* check that a notification didn't give us more */
1424 wodPlayer_ProcessMessages(wwo);
1425 if (!wwo->lpPlayPtr) {
1426 TRACE("flushing\n");
1427 ioctl(wwo->ossdev->fd, SNDCTL_DSP_SYNC, 0);
1428 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1429 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1430 } else {
1431 TRACE("recovering\n");
1432 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1435 } else {
1436 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1441 /**************************************************************************
1442 * wodGetDevCaps [internal]
1444 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1446 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1448 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1450 if (wDevID >= numOutDev) {
1451 TRACE("numOutDev reached !\n");
1452 return MMSYSERR_BADDEVICEID;
1455 memcpy(lpCaps, &WOutDev[wDevID].ossdev->out_caps, min(dwSize, sizeof(*lpCaps)));
1456 return MMSYSERR_NOERROR;
1459 /**************************************************************************
1460 * wodOpen [internal]
1462 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1464 int audio_fragment;
1465 WINE_WAVEOUT* wwo;
1466 audio_buf_info info;
1467 DWORD ret;
1469 TRACE("(%u, %p[cb=%08lx], %08lX);\n", wDevID, lpDesc, lpDesc->dwCallback, dwFlags);
1470 if (lpDesc == NULL) {
1471 WARN("Invalid Parameter !\n");
1472 return MMSYSERR_INVALPARAM;
1474 if (wDevID >= numOutDev) {
1475 TRACE("MAX_WAVOUTDRV reached !\n");
1476 return MMSYSERR_BADDEVICEID;
1479 /* only PCM format is supported so far... */
1480 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1481 lpDesc->lpFormat->nChannels == 0 ||
1482 lpDesc->lpFormat->nSamplesPerSec == 0) {
1483 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1484 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1485 lpDesc->lpFormat->nSamplesPerSec);
1486 return WAVERR_BADFORMAT;
1489 if (dwFlags & WAVE_FORMAT_QUERY) {
1490 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1491 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1492 lpDesc->lpFormat->nSamplesPerSec);
1493 return MMSYSERR_NOERROR;
1496 wwo = &WOutDev[wDevID];
1498 if ((dwFlags & WAVE_DIRECTSOUND) &&
1499 !(wwo->ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
1500 /* not supported, ignore it */
1501 dwFlags &= ~WAVE_DIRECTSOUND;
1503 if (dwFlags & WAVE_DIRECTSOUND) {
1504 if (wwo->ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1505 /* we have realtime DirectSound, fragments just waste our time,
1506 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1507 audio_fragment = 0x0020000B;
1508 else
1509 /* to approximate realtime, we must use small fragments,
1510 * let's try to fragment the above 64KB (256 * 2^8) */
1511 audio_fragment = 0x01000008;
1512 } else {
1513 /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
1514 * thus leading to 46ms per fragment, and a turnaround time of 185ms
1516 /* 16 fragments max, 2^10=1024 bytes per fragment */
1517 audio_fragment = 0x000F000A;
1519 if (wwo->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
1520 /* we want to be able to mmap() the device, which means it must be opened readable,
1521 * otherwise mmap() will fail (at least under Linux) */
1522 ret = OSS_OpenDevice(wwo->ossdev,
1523 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
1524 &audio_fragment,
1525 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
1526 lpDesc->lpFormat->nSamplesPerSec,
1527 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
1528 (lpDesc->lpFormat->wBitsPerSample == 16)
1529 ? AFMT_S16_LE : AFMT_U8);
1530 if ((ret==MMSYSERR_NOERROR) && (dwFlags & WAVE_DIRECTSOUND)) {
1531 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev->sample_rate;
1532 lpDesc->lpFormat->nChannels=(wwo->ossdev->stereo ? 2 : 1);
1533 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev->format == AFMT_U8 ? 8 : 16);
1534 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
1535 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
1536 TRACE("OSS_OpenDevice returned this format: %ldx%dx%d\n",
1537 lpDesc->lpFormat->nSamplesPerSec,
1538 lpDesc->lpFormat->wBitsPerSample,
1539 lpDesc->lpFormat->nChannels);
1541 if (ret != 0) return ret;
1542 wwo->state = WINE_WS_STOPPED;
1544 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1546 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1547 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1549 if (wwo->format.wBitsPerSample == 0) {
1550 WARN("Resetting zeroed wBitsPerSample\n");
1551 wwo->format.wBitsPerSample = 8 *
1552 (wwo->format.wf.nAvgBytesPerSec /
1553 wwo->format.wf.nSamplesPerSec) /
1554 wwo->format.wf.nChannels;
1556 /* Read output space info for future reference */
1557 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
1558 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
1559 OSS_CloseDevice(wwo->ossdev);
1560 wwo->state = WINE_WS_CLOSED;
1561 return MMSYSERR_NOTENABLED;
1564 /* Check that fragsize is correct per our settings above */
1565 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
1566 /* we've tried to set 1K fragments or less, but it didn't work */
1567 ERR("fragment size set failed, size is now %d\n", info.fragsize);
1568 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
1569 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
1572 /* Remember fragsize and total buffer size for future use */
1573 wwo->dwFragmentSize = info.fragsize;
1574 wwo->dwBufferSize = info.fragstotal * info.fragsize;
1575 wwo->dwPlayedTotal = 0;
1576 wwo->dwWrittenTotal = 0;
1577 wwo->bNeedPost = TRUE;
1579 OSS_InitRingMessage(&wwo->msgRing);
1581 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1582 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1583 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1584 CloseHandle(wwo->hStartUpEvent);
1585 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1587 TRACE("fd=%d fragmentSize=%ld\n",
1588 wwo->ossdev->fd, wwo->dwFragmentSize);
1589 if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1590 ERR("Fragment doesn't contain an integral number of data blocks\n");
1592 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1593 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1594 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1595 wwo->format.wf.nBlockAlign);
1597 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1600 /**************************************************************************
1601 * wodClose [internal]
1603 static DWORD wodClose(WORD wDevID)
1605 DWORD ret = MMSYSERR_NOERROR;
1606 WINE_WAVEOUT* wwo;
1608 TRACE("(%u);\n", wDevID);
1610 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1611 WARN("bad device ID !\n");
1612 return MMSYSERR_BADDEVICEID;
1615 wwo = &WOutDev[wDevID];
1616 if (wwo->lpQueuePtr) {
1617 WARN("buffers still playing !\n");
1618 ret = WAVERR_STILLPLAYING;
1619 } else {
1620 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1621 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1623 if (wwo->mapping) {
1624 munmap(wwo->mapping, wwo->maplen);
1625 wwo->mapping = NULL;
1628 OSS_DestroyRingMessage(&wwo->msgRing);
1630 OSS_CloseDevice(wwo->ossdev);
1631 wwo->state = WINE_WS_CLOSED;
1632 wwo->dwFragmentSize = 0;
1633 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1635 return ret;
1638 /**************************************************************************
1639 * wodWrite [internal]
1642 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1644 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1646 /* first, do the sanity checks... */
1647 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1648 WARN("bad dev ID !\n");
1649 return MMSYSERR_BADDEVICEID;
1652 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1653 return WAVERR_UNPREPARED;
1655 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1656 return WAVERR_STILLPLAYING;
1658 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1659 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1660 lpWaveHdr->lpNext = 0;
1662 if ((lpWaveHdr->dwBufferLength & (WOutDev[wDevID].format.wf.nBlockAlign - 1)) != 0)
1664 WARN("WaveHdr length isn't a multiple of the PCM block size: %ld %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].format.wf.nBlockAlign);
1665 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].format.wf.nBlockAlign - 1);
1668 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1670 return MMSYSERR_NOERROR;
1673 /**************************************************************************
1674 * wodPrepare [internal]
1676 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1678 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1680 if (wDevID >= numOutDev) {
1681 WARN("bad device ID !\n");
1682 return MMSYSERR_BADDEVICEID;
1685 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1686 return WAVERR_STILLPLAYING;
1688 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1689 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1690 return MMSYSERR_NOERROR;
1693 /**************************************************************************
1694 * wodUnprepare [internal]
1696 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1698 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1700 if (wDevID >= numOutDev) {
1701 WARN("bad device ID !\n");
1702 return MMSYSERR_BADDEVICEID;
1705 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1706 return WAVERR_STILLPLAYING;
1708 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1709 lpWaveHdr->dwFlags |= WHDR_DONE;
1711 return MMSYSERR_NOERROR;
1714 /**************************************************************************
1715 * wodPause [internal]
1717 static DWORD wodPause(WORD wDevID)
1719 TRACE("(%u);!\n", wDevID);
1721 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1722 WARN("bad device ID !\n");
1723 return MMSYSERR_BADDEVICEID;
1726 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1728 return MMSYSERR_NOERROR;
1731 /**************************************************************************
1732 * wodRestart [internal]
1734 static DWORD wodRestart(WORD wDevID)
1736 TRACE("(%u);\n", wDevID);
1738 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1739 WARN("bad device ID !\n");
1740 return MMSYSERR_BADDEVICEID;
1743 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1745 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1746 /* FIXME: Myst crashes with this ... hmm -MM
1747 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1750 return MMSYSERR_NOERROR;
1753 /**************************************************************************
1754 * wodReset [internal]
1756 static DWORD wodReset(WORD wDevID)
1758 TRACE("(%u);\n", wDevID);
1760 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1761 WARN("bad device ID !\n");
1762 return MMSYSERR_BADDEVICEID;
1765 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1767 return MMSYSERR_NOERROR;
1770 /**************************************************************************
1771 * wodGetPosition [internal]
1773 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1775 int time;
1776 DWORD val;
1777 WINE_WAVEOUT* wwo;
1779 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1781 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1782 WARN("bad device ID !\n");
1783 return MMSYSERR_BADDEVICEID;
1786 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1788 wwo = &WOutDev[wDevID];
1789 #ifdef EXACT_WODPOSITION
1790 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1791 #endif
1792 val = wwo->dwPlayedTotal;
1794 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1795 lpTime->wType, wwo->format.wBitsPerSample,
1796 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1797 wwo->format.wf.nAvgBytesPerSec);
1798 TRACE("dwPlayedTotal=%lu\n", val);
1800 switch (lpTime->wType) {
1801 case TIME_BYTES:
1802 lpTime->u.cb = val;
1803 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1804 break;
1805 case TIME_SAMPLES:
1806 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1807 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1808 break;
1809 case TIME_SMPTE:
1810 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1811 lpTime->u.smpte.hour = time / (60 * 60 * 1000);
1812 time -= lpTime->u.smpte.hour * (60 * 60 * 1000);
1813 lpTime->u.smpte.min = time / (60 * 1000);
1814 time -= lpTime->u.smpte.min * (60 * 1000);
1815 lpTime->u.smpte.sec = time / 1000;
1816 time -= lpTime->u.smpte.sec * 1000;
1817 lpTime->u.smpte.frame = time * 30 / 1000;
1818 lpTime->u.smpte.fps = 30;
1819 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1820 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1821 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1822 break;
1823 default:
1824 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1825 lpTime->wType = TIME_MS;
1826 case TIME_MS:
1827 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1828 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1829 break;
1831 return MMSYSERR_NOERROR;
1834 /**************************************************************************
1835 * wodBreakLoop [internal]
1837 static DWORD wodBreakLoop(WORD wDevID)
1839 TRACE("(%u);\n", wDevID);
1841 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1842 WARN("bad device ID !\n");
1843 return MMSYSERR_BADDEVICEID;
1845 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1846 return MMSYSERR_NOERROR;
1849 /**************************************************************************
1850 * wodGetVolume [internal]
1852 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1854 int mixer;
1855 int volume;
1856 DWORD left, right;
1858 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1860 if (lpdwVol == NULL)
1861 return MMSYSERR_NOTENABLED;
1862 if (wDevID >= numOutDev)
1863 return MMSYSERR_INVALPARAM;
1865 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_RDONLY|O_NDELAY)) < 0) {
1866 WARN("mixer device not available !\n");
1867 return MMSYSERR_NOTENABLED;
1869 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
1870 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)n", WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
1871 return MMSYSERR_NOTENABLED;
1873 close(mixer);
1874 left = LOBYTE(volume);
1875 right = HIBYTE(volume);
1876 TRACE("left=%ld right=%ld !\n", left, right);
1877 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1878 return MMSYSERR_NOERROR;
1881 /**************************************************************************
1882 * wodSetVolume [internal]
1884 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1886 int mixer;
1887 int volume;
1888 DWORD left, right;
1890 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1892 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1893 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1894 volume = left + (right << 8);
1896 if (wDevID >= numOutDev) {
1897 WARN("invalid parameter: wDevID > %d\n", numOutDev);
1898 return MMSYSERR_INVALPARAM;
1901 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_WRONLY|O_NDELAY)) < 0) {
1902 WARN("mixer device not available !\n");
1903 return MMSYSERR_NOTENABLED;
1905 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
1906 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n", WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
1907 return MMSYSERR_NOTENABLED;
1908 } else {
1909 TRACE("volume=%04x\n", (unsigned)volume);
1911 close(mixer);
1912 return MMSYSERR_NOERROR;
1915 /**************************************************************************
1916 * wodMessage (WINEOSS.7)
1918 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1919 DWORD dwParam1, DWORD dwParam2)
1921 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1922 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1924 switch (wMsg) {
1925 case DRVM_INIT:
1926 case DRVM_EXIT:
1927 case DRVM_ENABLE:
1928 case DRVM_DISABLE:
1929 /* FIXME: Pretend this is supported */
1930 return 0;
1931 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1932 case WODM_CLOSE: return wodClose (wDevID);
1933 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1934 case WODM_PAUSE: return wodPause (wDevID);
1935 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1936 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1937 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1938 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1939 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1940 case WODM_GETNUMDEVS: return numOutDev;
1941 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1942 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1943 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1944 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1945 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1946 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1947 case WODM_RESTART: return wodRestart (wDevID);
1948 case WODM_RESET: return wodReset (wDevID);
1950 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1951 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1952 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
1953 default:
1954 FIXME("unknown message %d!\n", wMsg);
1956 return MMSYSERR_NOTSUPPORTED;
1959 /*======================================================================*
1960 * Low level DSOUND implementation *
1961 *======================================================================*/
1963 typedef struct IDsDriverImpl IDsDriverImpl;
1964 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1966 struct IDsDriverImpl
1968 /* IUnknown fields */
1969 ICOM_VFIELD(IDsDriver);
1970 DWORD ref;
1971 /* IDsDriverImpl fields */
1972 UINT wDevID;
1973 IDsDriverBufferImpl*primary;
1976 struct IDsDriverBufferImpl
1978 /* IUnknown fields */
1979 ICOM_VFIELD(IDsDriverBuffer);
1980 DWORD ref;
1981 /* IDsDriverBufferImpl fields */
1982 IDsDriverImpl* drv;
1983 DWORD buflen;
1986 static HRESULT DSDB_MapPrimary(IDsDriverBufferImpl *dsdb)
1988 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1989 TRACE("(%p)\n",dsdb);
1990 if (!wwo->mapping) {
1991 wwo->mapping = mmap(NULL, wwo->maplen, PROT_WRITE, MAP_SHARED,
1992 wwo->ossdev->fd, 0);
1993 if (wwo->mapping == (LPBYTE)-1) {
1994 TRACE("(%p): Could not map sound device for direct access (%s)\n", dsdb, strerror(errno));
1995 return DSERR_GENERIC;
1997 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb, wwo->mapping, wwo->maplen);
1999 /* for some reason, es1371 and sblive! sometimes have junk in here.
2000 * clear it, or we get junk noise */
2001 /* some libc implementations are buggy: their memset reads from the buffer...
2002 * to work around it, we have to zero the block by hand. We don't do the expected:
2003 * memset(wwo->mapping,0, wwo->maplen);
2006 char* p1 = wwo->mapping;
2007 unsigned len = wwo->maplen;
2009 if (len >= 16) /* so we can have at least a 4 long area to store... */
2011 /* the mmap:ed value is (at least) dword aligned
2012 * so, start filling the complete unsigned long:s
2014 int b = len >> 2;
2015 unsigned long* p4 = (unsigned long*)p1;
2017 while (b--) *p4++ = 0;
2018 /* prepare for filling the rest */
2019 len &= 3;
2020 p1 = (unsigned char*)p4;
2022 /* in all cases, fill the remaining bytes */
2023 while (len-- != 0) *p1++ = 0;
2026 return DS_OK;
2029 static HRESULT DSDB_UnmapPrimary(IDsDriverBufferImpl *dsdb)
2031 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
2032 TRACE("(%p)\n",dsdb);
2033 if (wwo->mapping) {
2034 if (munmap(wwo->mapping, wwo->maplen) < 0) {
2035 ERR("(%p): Could not unmap sound device (%s)\n", dsdb, strerror(errno));
2036 return DSERR_GENERIC;
2038 wwo->mapping = NULL;
2039 TRACE("(%p): sound device unmapped\n", dsdb);
2041 return DS_OK;
2044 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
2046 ICOM_THIS(IDsDriverBufferImpl,iface);
2047 TRACE("(%p,%s,%p)\n",iface,debugstr_guid(riid),*ppobj);
2049 if ( IsEqualGUID(riid, &IID_IUnknown) ||
2050 IsEqualGUID(riid, &IID_IDsDriverBuffer) ) {
2051 IDsDriverBuffer_AddRef(iface);
2052 *ppobj = (LPVOID)This;
2053 return DS_OK;
2056 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
2057 FIXME("IDsDriverNotify not implemented\n");
2058 *ppobj = (LPVOID)0;
2059 return E_NOINTERFACE;
2062 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
2063 FIXME("IDsDriverPropertySet not implemented\n");
2064 *ppobj = (LPVOID)0;
2065 return E_NOINTERFACE;
2068 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
2070 *ppobj = 0;
2072 return E_NOINTERFACE;
2075 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
2077 ICOM_THIS(IDsDriverBufferImpl,iface);
2078 TRACE("(%p)\n",This);
2079 This->ref++;
2080 TRACE("ref=%ld\n",This->ref);
2081 return This->ref;
2084 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
2086 ICOM_THIS(IDsDriverBufferImpl,iface);
2087 TRACE("(%p)\n",This);
2088 if (--This->ref) {
2089 TRACE("ref=%ld\n",This->ref);
2090 return This->ref;
2092 if (This == This->drv->primary)
2093 This->drv->primary = NULL;
2094 DSDB_UnmapPrimary(This);
2095 HeapFree(GetProcessHeap(),0,This);
2096 TRACE("ref=0\n");
2097 return 0;
2100 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
2101 LPVOID*ppvAudio1,LPDWORD pdwLen1,
2102 LPVOID*ppvAudio2,LPDWORD pdwLen2,
2103 DWORD dwWritePosition,DWORD dwWriteLen,
2104 DWORD dwFlags)
2106 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2107 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
2108 * and that we don't support secondary buffers, this method will never be called */
2109 TRACE("(%p): stub\n",iface);
2110 return DSERR_UNSUPPORTED;
2113 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
2114 LPVOID pvAudio1,DWORD dwLen1,
2115 LPVOID pvAudio2,DWORD dwLen2)
2117 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2118 TRACE("(%p): stub\n",iface);
2119 return DSERR_UNSUPPORTED;
2122 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
2123 LPWAVEFORMATEX pwfx)
2125 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2127 TRACE("(%p,%p)\n",iface,pwfx);
2128 /* On our request (GetDriverDesc flags), DirectSound has by now used
2129 * waveOutClose/waveOutOpen to set the format...
2130 * unfortunately, this means our mmap() is now gone...
2131 * so we need to somehow signal to our DirectSound implementation
2132 * that it should completely recreate this HW buffer...
2133 * this unexpected error code should do the trick... */
2134 return DSERR_BUFFERLOST;
2137 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
2139 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2140 TRACE("(%p,%ld): stub\n",iface,dwFreq);
2141 return DSERR_UNSUPPORTED;
2144 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
2146 DWORD vol;
2147 ICOM_THIS(IDsDriverBufferImpl,iface);
2148 TRACE("(%p,%p)\n",This,pVolPan);
2150 vol = pVolPan->dwTotalLeftAmpFactor | (pVolPan->dwTotalRightAmpFactor << 16);
2152 if (wodSetVolume(This->drv->wDevID, vol) != MMSYSERR_NOERROR) {
2153 WARN("wodSetVolume failed\n");
2154 return DSERR_INVALIDPARAM;
2157 return DS_OK;
2160 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
2162 /* ICOM_THIS(IDsDriverImpl,iface); */
2163 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
2164 return DSERR_UNSUPPORTED;
2167 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
2168 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
2170 ICOM_THIS(IDsDriverBufferImpl,iface);
2171 count_info info;
2172 DWORD ptr;
2174 TRACE("(%p)\n",iface);
2175 if (WOutDev[This->drv->wDevID].state == WINE_WS_CLOSED) {
2176 ERR("device not open, but accessing?\n");
2177 return DSERR_UNINITIALIZED;
2179 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
2180 ERR("ioctl(%s, SNDCTL_DSP_GETOPTR) failed (%s)\n", WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2181 return DSERR_GENERIC;
2183 ptr = info.ptr & ~3; /* align the pointer, just in case */
2184 if (lpdwPlay) *lpdwPlay = ptr;
2185 if (lpdwWrite) {
2186 /* add some safety margin (not strictly necessary, but...) */
2187 if (WOutDev[This->drv->wDevID].ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2188 *lpdwWrite = ptr + 32;
2189 else
2190 *lpdwWrite = ptr + WOutDev[This->drv->wDevID].dwFragmentSize;
2191 while (*lpdwWrite > This->buflen)
2192 *lpdwWrite -= This->buflen;
2194 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay?*lpdwPlay:0, lpdwWrite?*lpdwWrite:0);
2195 return DS_OK;
2198 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
2200 ICOM_THIS(IDsDriverBufferImpl,iface);
2201 int enable;
2202 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
2203 WOutDev[This->drv->wDevID].ossdev->bOutputEnabled = TRUE;
2204 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
2205 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2206 if (errno == EINVAL) {
2207 /* Don't give up yet. OSS trigger support is inconsistent. */
2208 if (WOutDev[This->drv->wDevID].ossdev->open_count == 1) {
2209 /* try the opposite input enable */
2210 if (WOutDev[This->drv->wDevID].ossdev->bInputEnabled == FALSE)
2211 WOutDev[This->drv->wDevID].ossdev->bInputEnabled = TRUE;
2212 else
2213 WOutDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
2214 /* try it again */
2215 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
2216 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) >= 0)
2217 return DS_OK;
2220 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2221 WOutDev[This->drv->wDevID].ossdev->bOutputEnabled = FALSE;
2222 return DSERR_GENERIC;
2224 return DS_OK;
2227 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
2229 ICOM_THIS(IDsDriverBufferImpl,iface);
2230 int enable;
2231 TRACE("(%p)\n",iface);
2232 /* no more playing */
2233 WOutDev[This->drv->wDevID].ossdev->bOutputEnabled = FALSE;
2234 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
2235 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2236 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2237 return DSERR_GENERIC;
2239 #if 0
2240 /* the play position must be reset to the beginning of the buffer */
2241 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_RESET, 0) < 0) {
2242 ERR("ioctl(%s, SNDCTL_DSP_RESET) failed (%s)\n", WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2243 return DSERR_GENERIC;
2245 #endif
2246 /* Most OSS drivers just can't stop the playback without closing the device...
2247 * so we need to somehow signal to our DirectSound implementation
2248 * that it should completely recreate this HW buffer...
2249 * this unexpected error code should do the trick... */
2250 /* FIXME: ...unless we are doing full duplex, then its not nice to close the device */
2251 if (WOutDev[This->drv->wDevID].ossdev->open_count == 1)
2252 return DSERR_BUFFERLOST;
2254 return DS_OK;
2257 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
2259 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2260 IDsDriverBufferImpl_QueryInterface,
2261 IDsDriverBufferImpl_AddRef,
2262 IDsDriverBufferImpl_Release,
2263 IDsDriverBufferImpl_Lock,
2264 IDsDriverBufferImpl_Unlock,
2265 IDsDriverBufferImpl_SetFormat,
2266 IDsDriverBufferImpl_SetFrequency,
2267 IDsDriverBufferImpl_SetVolumePan,
2268 IDsDriverBufferImpl_SetPosition,
2269 IDsDriverBufferImpl_GetPosition,
2270 IDsDriverBufferImpl_Play,
2271 IDsDriverBufferImpl_Stop
2274 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
2276 ICOM_THIS(IDsDriverImpl,iface);
2277 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
2279 if ( IsEqualGUID(riid, &IID_IUnknown) ||
2280 IsEqualGUID(riid, &IID_IDsDriver) ) {
2281 IDsDriver_AddRef(iface);
2282 *ppobj = (LPVOID)This;
2283 return DS_OK;
2286 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
2288 *ppobj = 0;
2290 return E_NOINTERFACE;
2293 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
2295 ICOM_THIS(IDsDriverImpl,iface);
2296 TRACE("(%p)\n",This);
2297 This->ref++;
2298 TRACE("ref=%ld\n",This->ref);
2299 return This->ref;
2302 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
2304 ICOM_THIS(IDsDriverImpl,iface);
2305 TRACE("(%p)\n",This);
2306 if (--This->ref) {
2307 TRACE("ref=%ld\n",This->ref);
2308 return This->ref;
2310 HeapFree(GetProcessHeap(),0,This);
2311 TRACE("ref=0\n");
2312 return 0;
2315 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
2317 ICOM_THIS(IDsDriverImpl,iface);
2318 TRACE("(%p,%p)\n",iface,pDesc);
2320 /* copy version from driver */
2321 memcpy(pDesc, &(WOutDev[This->wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
2323 pDesc->dwFlags |= DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
2324 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
2325 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
2326 pDesc->wVxdId = 0;
2327 pDesc->wReserved = 0;
2328 pDesc->ulDeviceNum = This->wDevID;
2329 pDesc->dwHeapType = DSDHEAP_NOHEAP;
2330 pDesc->pvDirectDrawHeap = NULL;
2331 pDesc->dwMemStartAddress = 0;
2332 pDesc->dwMemEndAddress = 0;
2333 pDesc->dwMemAllocExtra = 0;
2334 pDesc->pvReserved1 = NULL;
2335 pDesc->pvReserved2 = NULL;
2336 return DS_OK;
2339 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
2341 ICOM_THIS(IDsDriverImpl,iface);
2342 int enable;
2343 TRACE("(%p)\n",iface);
2345 /* make sure the card doesn't start playing before we want it to */
2346 WOutDev[This->wDevID].ossdev->bOutputEnabled = FALSE;
2347 enable = getEnables(WOutDev[This->wDevID].ossdev);
2348 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2349 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev[This->wDevID].ossdev->dev_name, strerror(errno));
2350 return DSERR_GENERIC;
2352 return DS_OK;
2355 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
2357 ICOM_THIS(IDsDriverImpl,iface);
2358 TRACE("(%p)\n",iface);
2359 if (This->primary) {
2360 ERR("problem with DirectSound: primary not released\n");
2361 return DSERR_GENERIC;
2363 return DS_OK;
2366 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
2368 ICOM_THIS(IDsDriverImpl,iface);
2369 TRACE("(%p,%p)\n",iface,pCaps);
2370 memcpy(pCaps, &(WOutDev[This->wDevID].ossdev->ds_caps), sizeof(DSDRIVERCAPS));
2371 return DS_OK;
2374 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
2375 LPWAVEFORMATEX pwfx,
2376 DWORD dwFlags, DWORD dwCardAddress,
2377 LPDWORD pdwcbBufferSize,
2378 LPBYTE *ppbBuffer,
2379 LPVOID *ppvObj)
2381 ICOM_THIS(IDsDriverImpl,iface);
2382 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
2383 HRESULT err;
2384 audio_buf_info info;
2385 int enable = 0;
2386 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
2388 /* we only support primary buffers */
2389 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
2390 return DSERR_UNSUPPORTED;
2391 if (This->primary)
2392 return DSERR_ALLOCATED;
2393 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
2394 return DSERR_CONTROLUNAVAIL;
2396 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsDriverBufferImpl));
2397 if (*ippdsdb == NULL)
2398 return DSERR_OUTOFMEMORY;
2399 (*ippdsdb)->lpVtbl = &dsdbvt;
2400 (*ippdsdb)->ref = 1;
2401 (*ippdsdb)->drv = This;
2403 /* check how big the DMA buffer is now */
2404 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2405 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", WOutDev[This->wDevID].ossdev->dev_name, strerror(errno));
2406 HeapFree(GetProcessHeap(),0,*ippdsdb);
2407 *ippdsdb = NULL;
2408 return DSERR_GENERIC;
2410 WOutDev[This->wDevID].maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
2412 /* map the DMA buffer */
2413 err = DSDB_MapPrimary(*ippdsdb);
2414 if (err != DS_OK) {
2415 HeapFree(GetProcessHeap(),0,*ippdsdb);
2416 *ippdsdb = NULL;
2417 return err;
2420 /* primary buffer is ready to go */
2421 *pdwcbBufferSize = WOutDev[This->wDevID].maplen;
2422 *ppbBuffer = WOutDev[This->wDevID].mapping;
2424 /* some drivers need some extra nudging after mapping */
2425 WOutDev[This->wDevID].ossdev->bOutputEnabled = FALSE;
2426 enable = getEnables(WOutDev[This->wDevID].ossdev);
2427 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2428 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev[This->wDevID].ossdev->dev_name, strerror(errno));
2429 return DSERR_GENERIC;
2432 This->primary = *ippdsdb;
2434 return DS_OK;
2437 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
2438 PIDSDRIVERBUFFER pBuffer,
2439 LPVOID *ppvObj)
2441 /* ICOM_THIS(IDsDriverImpl,iface); */
2442 TRACE("(%p,%p): stub\n",iface,pBuffer);
2443 return DSERR_INVALIDCALL;
2446 static ICOM_VTABLE(IDsDriver) dsdvt =
2448 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2449 IDsDriverImpl_QueryInterface,
2450 IDsDriverImpl_AddRef,
2451 IDsDriverImpl_Release,
2452 IDsDriverImpl_GetDriverDesc,
2453 IDsDriverImpl_Open,
2454 IDsDriverImpl_Close,
2455 IDsDriverImpl_GetCaps,
2456 IDsDriverImpl_CreateSoundBuffer,
2457 IDsDriverImpl_DuplicateSoundBuffer
2460 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2462 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
2463 TRACE("(%d,%p)\n",wDevID,drv);
2465 /* the HAL isn't much better than the HEL if we can't do mmap() */
2466 if (!(WOutDev[wDevID].ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
2467 ERR("DirectSound flag not set\n");
2468 MESSAGE("This sound card's driver does not support direct access\n");
2469 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2470 return MMSYSERR_NOTSUPPORTED;
2473 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsDriverImpl));
2474 if (!*idrv)
2475 return MMSYSERR_NOMEM;
2476 (*idrv)->lpVtbl = &dsdvt;
2477 (*idrv)->ref = 1;
2479 (*idrv)->wDevID = wDevID;
2480 (*idrv)->primary = NULL;
2481 return MMSYSERR_NOERROR;
2484 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2486 TRACE("(%d,%p)\n",wDevID,desc);
2487 memcpy(desc, &(WOutDev[wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
2488 return MMSYSERR_NOERROR;
2491 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
2493 TRACE("(%d,%p)\n",wDevID,pGuid);
2494 memcpy(pGuid, &(WOutDev[wDevID].ossdev->ds_guid), sizeof(GUID));
2495 return MMSYSERR_NOERROR;
2498 /*======================================================================*
2499 * Low level WAVE IN implementation *
2500 *======================================================================*/
2502 /**************************************************************************
2503 * widNotifyClient [internal]
2505 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2507 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
2509 switch (wMsg) {
2510 case WIM_OPEN:
2511 case WIM_CLOSE:
2512 case WIM_DATA:
2513 if (wwi->wFlags != DCB_NULL &&
2514 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2515 (HDRVR)wwi->waveDesc.hWave, wMsg,
2516 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2517 WARN("can't notify client !\n");
2518 return MMSYSERR_ERROR;
2520 break;
2521 default:
2522 FIXME("Unknown callback message %u\n", wMsg);
2523 return MMSYSERR_INVALPARAM;
2525 return MMSYSERR_NOERROR;
2528 /**************************************************************************
2529 * widGetDevCaps [internal]
2531 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
2533 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
2535 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2537 if (wDevID >= numInDev) {
2538 TRACE("numOutDev reached !\n");
2539 return MMSYSERR_BADDEVICEID;
2542 memcpy(lpCaps, &WInDev[wDevID].ossdev->in_caps, min(dwSize, sizeof(*lpCaps)));
2543 return MMSYSERR_NOERROR;
2546 /**************************************************************************
2547 * widRecorder [internal]
2549 static DWORD CALLBACK widRecorder(LPVOID pmt)
2551 WORD uDevID = (DWORD)pmt;
2552 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2553 WAVEHDR* lpWaveHdr;
2554 DWORD dwSleepTime;
2555 DWORD bytesRead;
2556 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2557 char *pOffset = buffer;
2558 audio_buf_info info;
2559 int xs;
2560 enum win_wm_message msg;
2561 DWORD param;
2562 HANDLE ev;
2563 int enable;
2565 wwi->state = WINE_WS_STOPPED;
2566 wwi->dwTotalRecorded = 0;
2567 wwi->lpQueuePtr = NULL;
2569 SetEvent(wwi->hStartUpEvent);
2571 /* disable input so capture will begin when triggered */
2572 wwi->ossdev->bInputEnabled = FALSE;
2573 enable = getEnables(wwi->ossdev);
2574 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2575 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2577 /* the soundblaster live needs a micro wake to get its recording started
2578 * (or GETISPACE will have 0 frags all the time)
2580 read(wwi->ossdev->fd, &xs, 4);
2582 /* make sleep time to be # of ms to output a fragment */
2583 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->format.wf.nAvgBytesPerSec;
2584 TRACE("sleeptime=%ld ms\n", dwSleepTime);
2586 for (;;) {
2587 /* wait for dwSleepTime or an event in thread's queue */
2588 /* FIXME: could improve wait time depending on queue state,
2589 * ie, number of queued fragments
2592 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2594 lpWaveHdr = wwi->lpQueuePtr;
2596 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info);
2597 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2599 /* read all the fragments accumulated so far */
2600 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2602 info.fragments --;
2604 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2606 /* directly read fragment in wavehdr */
2607 bytesRead = read(wwi->ossdev->fd,
2608 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2609 wwi->dwFragmentSize);
2611 TRACE("bytesRead=%ld (direct)\n", bytesRead);
2612 if (bytesRead != (DWORD) -1)
2614 /* update number of bytes recorded in current buffer and by this device */
2615 lpWaveHdr->dwBytesRecorded += bytesRead;
2616 wwi->dwTotalRecorded += bytesRead;
2618 /* buffer is full. notify client */
2619 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2621 /* must copy the value of next waveHdr, because we have no idea of what
2622 * will be done with the content of lpWaveHdr in callback
2624 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2626 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2627 lpWaveHdr->dwFlags |= WHDR_DONE;
2629 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2630 lpWaveHdr = wwi->lpQueuePtr = lpNext;
2634 else
2636 /* read the fragment in a local buffer */
2637 bytesRead = read(wwi->ossdev->fd, buffer, wwi->dwFragmentSize);
2638 pOffset = buffer;
2640 TRACE("bytesRead=%ld (local)\n", bytesRead);
2642 /* copy data in client buffers */
2643 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2645 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2647 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2648 pOffset,
2649 dwToCopy);
2651 /* update number of bytes recorded in current buffer and by this device */
2652 lpWaveHdr->dwBytesRecorded += dwToCopy;
2653 wwi->dwTotalRecorded += dwToCopy;
2654 bytesRead -= dwToCopy;
2655 pOffset += dwToCopy;
2657 /* client buffer is full. notify client */
2658 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2660 /* must copy the value of next waveHdr, because we have no idea of what
2661 * will be done with the content of lpWaveHdr in callback
2663 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2664 TRACE("lpNext=%p\n", lpNext);
2666 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2667 lpWaveHdr->dwFlags |= WHDR_DONE;
2669 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2671 wwi->lpQueuePtr = lpWaveHdr = lpNext;
2672 if (!lpNext && bytesRead) {
2673 /* before we give up, check for more header messages */
2674 while (OSS_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
2676 if (msg == WINE_WM_HEADER) {
2677 LPWAVEHDR hdr;
2678 OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
2679 hdr = ((LPWAVEHDR)param);
2680 TRACE("msg = %s, hdr = %p, ev = %p\n", wodPlayerCmdString[msg - WM_USER - 1], hdr, ev);
2681 hdr->lpNext = 0;
2682 if (lpWaveHdr == 0) {
2683 /* new head of queue */
2684 wwi->lpQueuePtr = lpWaveHdr = hdr;
2685 } else {
2686 /* insert buffer at the end of queue */
2687 LPWAVEHDR* wh;
2688 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2689 *wh = hdr;
2691 } else
2692 break;
2695 if (lpWaveHdr == 0) {
2696 /* no more buffer to copy data to, but we did read more.
2697 * what hasn't been copied will be dropped
2699 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
2700 wwi->lpQueuePtr = NULL;
2701 break;
2710 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2712 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2714 TRACE("msg=%s param=0x%lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
2715 switch (msg) {
2716 case WINE_WM_PAUSING:
2717 wwi->state = WINE_WS_PAUSED;
2718 /*FIXME("Device should stop recording\n");*/
2719 SetEvent(ev);
2720 break;
2721 case WINE_WM_STARTING:
2722 wwi->state = WINE_WS_PLAYING;
2724 if (wwi->ossdev->bTriggerSupport)
2726 /* start the recording */
2727 wwi->ossdev->bInputEnabled = TRUE;
2728 enable = getEnables(wwi->ossdev);
2729 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2730 wwi->ossdev->bInputEnabled = FALSE;
2731 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2734 else
2736 unsigned char data[4];
2737 /* read 4 bytes to start the recording */
2738 read(wwi->ossdev->fd, data, 4);
2741 SetEvent(ev);
2742 break;
2743 case WINE_WM_HEADER:
2744 lpWaveHdr = (LPWAVEHDR)param;
2745 lpWaveHdr->lpNext = 0;
2747 /* insert buffer at the end of queue */
2749 LPWAVEHDR* wh;
2750 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2751 *wh = lpWaveHdr;
2753 break;
2754 case WINE_WM_STOPPING:
2755 case WINE_WM_RESETTING:
2756 wwi->state = WINE_WS_STOPPED;
2757 wwi->dwTotalRecorded = 0;
2758 /* return all buffers to the app */
2759 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2760 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2761 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2762 lpWaveHdr->dwFlags |= WHDR_DONE;
2764 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2766 wwi->lpQueuePtr = NULL;
2767 SetEvent(ev);
2768 break;
2769 case WINE_WM_CLOSING:
2770 wwi->hThread = 0;
2771 wwi->state = WINE_WS_CLOSED;
2772 SetEvent(ev);
2773 HeapFree(GetProcessHeap(), 0, buffer);
2774 ExitThread(0);
2775 /* shouldn't go here */
2776 default:
2777 FIXME("unknown message %d\n", msg);
2778 break;
2782 ExitThread(0);
2783 /* just for not generating compilation warnings... should never be executed */
2784 return 0;
2788 /**************************************************************************
2789 * widOpen [internal]
2791 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2793 WINE_WAVEIN* wwi;
2794 int fragment_size;
2795 int audio_fragment;
2796 DWORD ret;
2798 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
2799 if (lpDesc == NULL) {
2800 WARN("Invalid Parameter !\n");
2801 return MMSYSERR_INVALPARAM;
2803 if (wDevID >= numInDev) return MMSYSERR_BADDEVICEID;
2805 /* only PCM format is supported so far... */
2806 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
2807 lpDesc->lpFormat->nChannels == 0 ||
2808 lpDesc->lpFormat->nSamplesPerSec == 0) {
2809 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2810 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2811 lpDesc->lpFormat->nSamplesPerSec);
2812 return WAVERR_BADFORMAT;
2815 if (dwFlags & WAVE_FORMAT_QUERY) {
2816 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2817 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2818 lpDesc->lpFormat->nSamplesPerSec);
2819 return MMSYSERR_NOERROR;
2822 wwi = &WInDev[wDevID];
2824 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2826 if ((dwFlags & WAVE_DIRECTSOUND) &&
2827 !(wwi->ossdev->in_caps_support & WAVECAPS_DIRECTSOUND))
2828 /* not supported, ignore it */
2829 dwFlags &= ~WAVE_DIRECTSOUND;
2831 if (dwFlags & WAVE_DIRECTSOUND) {
2832 TRACE("has DirectSoundCapture driver\n");
2833 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
2834 /* we have realtime DirectSound, fragments just waste our time,
2835 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2836 audio_fragment = 0x0020000B;
2837 else
2838 /* to approximate realtime, we must use small fragments,
2839 * let's try to fragment the above 64KB (256 * 2^8) */
2840 audio_fragment = 0x01000008;
2841 } else {
2842 TRACE("doesn't have DirectSoundCapture driver\n");
2843 if (wwi->ossdev->open_count > 0) {
2844 TRACE("Using output device audio_fragment\n");
2845 /* FIXME: This may not be optimal for capture but it allows us
2846 * to do hardware playback without hardware capture. */
2847 audio_fragment = wwi->ossdev->audio_fragment;
2848 } else {
2849 /* This is actually hand tuned to work so that my SB Live:
2850 * - does not skip
2851 * - does not buffer too much
2852 * when sending with the Shoutcast winamp plugin
2854 /* 15 fragments max, 2^10 = 1024 bytes per fragment */
2855 audio_fragment = 0x000F000A;
2859 TRACE("using %d %d byte fragments\n", audio_fragment >> 16,
2860 1 << (audio_fragment & 0xffff));
2862 ret = OSS_OpenDevice(wwi->ossdev, O_RDONLY, &audio_fragment,
2864 lpDesc->lpFormat->nSamplesPerSec,
2865 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
2866 (lpDesc->lpFormat->wBitsPerSample == 16)
2867 ? AFMT_S16_LE : AFMT_U8);
2868 if (ret != 0) return ret;
2869 wwi->state = WINE_WS_STOPPED;
2871 if (wwi->lpQueuePtr) {
2872 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2873 wwi->lpQueuePtr = NULL;
2875 wwi->dwTotalRecorded = 0;
2876 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2878 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2879 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
2881 if (wwi->format.wBitsPerSample == 0) {
2882 WARN("Resetting zeroed wBitsPerSample\n");
2883 wwi->format.wBitsPerSample = 8 *
2884 (wwi->format.wf.nAvgBytesPerSec /
2885 wwi->format.wf.nSamplesPerSec) /
2886 wwi->format.wf.nChannels;
2889 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETBLKSIZE, &fragment_size);
2890 if (fragment_size == -1) {
2891 WARN("ioctl(%s, SNDCTL_DSP_GETBLKSIZE) failed (%s)\n",
2892 wwi->ossdev->dev_name, strerror(errno));
2893 OSS_CloseDevice(wwi->ossdev);
2894 wwi->state = WINE_WS_CLOSED;
2895 return MMSYSERR_NOTENABLED;
2897 wwi->dwFragmentSize = fragment_size;
2899 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2900 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
2901 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
2902 wwi->format.wf.nBlockAlign);
2904 OSS_InitRingMessage(&wwi->msgRing);
2906 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
2907 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
2908 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2909 CloseHandle(wwi->hStartUpEvent);
2910 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2912 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2915 /**************************************************************************
2916 * widClose [internal]
2918 static DWORD widClose(WORD wDevID)
2920 WINE_WAVEIN* wwi;
2922 TRACE("(%u);\n", wDevID);
2923 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2924 WARN("can't close !\n");
2925 return MMSYSERR_INVALHANDLE;
2928 wwi = &WInDev[wDevID];
2930 if (wwi->lpQueuePtr != NULL) {
2931 WARN("still buffers open !\n");
2932 return WAVERR_STILLPLAYING;
2935 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
2936 OSS_CloseDevice(wwi->ossdev);
2937 wwi->state = WINE_WS_CLOSED;
2938 wwi->dwFragmentSize = 0;
2939 OSS_DestroyRingMessage(&wwi->msgRing);
2940 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
2943 /**************************************************************************
2944 * widAddBuffer [internal]
2946 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2948 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2950 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2951 WARN("can't do it !\n");
2952 return MMSYSERR_INVALHANDLE;
2954 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2955 TRACE("never been prepared !\n");
2956 return WAVERR_UNPREPARED;
2958 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2959 TRACE("header already in use !\n");
2960 return WAVERR_STILLPLAYING;
2963 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2964 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2965 lpWaveHdr->dwBytesRecorded = 0;
2966 lpWaveHdr->lpNext = NULL;
2968 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2969 return MMSYSERR_NOERROR;
2972 /**************************************************************************
2973 * widPrepare [internal]
2975 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2977 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2979 if (wDevID >= numInDev) return MMSYSERR_INVALHANDLE;
2981 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2982 return WAVERR_STILLPLAYING;
2984 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2985 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2986 lpWaveHdr->dwBytesRecorded = 0;
2987 TRACE("header prepared !\n");
2988 return MMSYSERR_NOERROR;
2991 /**************************************************************************
2992 * widUnprepare [internal]
2994 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2996 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2997 if (wDevID >= numInDev) return MMSYSERR_INVALHANDLE;
2999 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
3000 return WAVERR_STILLPLAYING;
3002 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
3003 lpWaveHdr->dwFlags |= WHDR_DONE;
3005 return MMSYSERR_NOERROR;
3008 /**************************************************************************
3009 * widStart [internal]
3011 static DWORD widStart(WORD wDevID)
3013 TRACE("(%u);\n", wDevID);
3014 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3015 WARN("can't start recording !\n");
3016 return MMSYSERR_INVALHANDLE;
3019 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
3020 return MMSYSERR_NOERROR;
3023 /**************************************************************************
3024 * widStop [internal]
3026 static DWORD widStop(WORD wDevID)
3028 TRACE("(%u);\n", wDevID);
3029 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3030 WARN("can't stop !\n");
3031 return MMSYSERR_INVALHANDLE;
3034 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3036 return MMSYSERR_NOERROR;
3039 /**************************************************************************
3040 * widReset [internal]
3042 static DWORD widReset(WORD wDevID)
3044 TRACE("(%u);\n", wDevID);
3045 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3046 WARN("can't reset !\n");
3047 return MMSYSERR_INVALHANDLE;
3049 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3050 return MMSYSERR_NOERROR;
3053 /**************************************************************************
3054 * widGetPosition [internal]
3056 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3058 int time;
3059 WINE_WAVEIN* wwi;
3061 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
3063 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3064 WARN("can't get pos !\n");
3065 return MMSYSERR_INVALHANDLE;
3067 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
3069 wwi = &WInDev[wDevID];
3071 TRACE("wType=%04X !\n", lpTime->wType);
3072 TRACE("wBitsPerSample=%u\n", wwi->format.wBitsPerSample);
3073 TRACE("nSamplesPerSec=%lu\n", wwi->format.wf.nSamplesPerSec);
3074 TRACE("nChannels=%u\n", wwi->format.wf.nChannels);
3075 TRACE("nAvgBytesPerSec=%lu\n", wwi->format.wf.nAvgBytesPerSec);
3076 TRACE("dwTotalRecorded=%lu\n",wwi->dwTotalRecorded);
3077 switch (lpTime->wType) {
3078 case TIME_BYTES:
3079 lpTime->u.cb = wwi->dwTotalRecorded;
3080 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
3081 break;
3082 case TIME_SAMPLES:
3083 lpTime->u.sample = wwi->dwTotalRecorded * 8 /
3084 wwi->format.wBitsPerSample / wwi->format.wf.nChannels;
3085 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
3086 break;
3087 case TIME_SMPTE:
3088 time = wwi->dwTotalRecorded /
3089 (wwi->format.wf.nAvgBytesPerSec / 1000);
3090 lpTime->u.smpte.hour = time / (60 * 60 * 1000);
3091 time -= lpTime->u.smpte.hour * (60 * 60 * 1000);
3092 lpTime->u.smpte.min = time / (60 * 1000);
3093 time -= lpTime->u.smpte.min * (60 * 1000);
3094 lpTime->u.smpte.sec = time / 1000;
3095 time -= lpTime->u.smpte.sec * 1000;
3096 lpTime->u.smpte.frame = time * 30 / 1000;
3097 lpTime->u.smpte.fps = 30;
3098 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
3099 lpTime->u.smpte.hour, lpTime->u.smpte.min,
3100 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
3101 break;
3102 default:
3103 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime->wType);
3104 lpTime->wType = TIME_MS;
3105 case TIME_MS:
3106 lpTime->u.ms = wwi->dwTotalRecorded /
3107 (wwi->format.wf.nAvgBytesPerSec / 1000);
3108 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
3109 break;
3111 return MMSYSERR_NOERROR;
3114 /**************************************************************************
3115 * widMessage (WINEOSS.6)
3117 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3118 DWORD dwParam1, DWORD dwParam2)
3120 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
3121 wDevID, wMsg, dwUser, dwParam1, dwParam2);
3123 switch (wMsg) {
3124 case DRVM_INIT:
3125 case DRVM_EXIT:
3126 case DRVM_ENABLE:
3127 case DRVM_DISABLE:
3128 /* FIXME: Pretend this is supported */
3129 return 0;
3130 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3131 case WIDM_CLOSE: return widClose (wDevID);
3132 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3133 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3134 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3135 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
3136 case WIDM_GETNUMDEVS: return numInDev;
3137 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
3138 case WIDM_RESET: return widReset (wDevID);
3139 case WIDM_START: return widStart (wDevID);
3140 case WIDM_STOP: return widStop (wDevID);
3141 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3142 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3143 case DRV_QUERYDSOUNDGUID: return widDsGuid (wDevID, (LPGUID)dwParam1);
3144 default:
3145 FIXME("unknown message %u!\n", wMsg);
3147 return MMSYSERR_NOTSUPPORTED;
3150 /*======================================================================*
3151 * Low level DSOUND property set implementation *
3152 *======================================================================*/
3154 typedef struct IDsDriverPropertySetImpl IDsDriverPropertySetImpl;
3156 struct IDsDriverPropertySetImpl
3158 /* IUnknown fields */
3159 ICOM_VFIELD(IDsDriverPropertySet);
3160 DWORD ref;
3163 static HRESULT WINAPI IDsDriverPropertySetImpl_QueryInterface(
3164 PIDSDRIVERPROPERTYSET iface,
3165 REFIID riid,
3166 LPVOID *ppobj)
3168 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3169 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
3171 if ( IsEqualGUID(riid, &IID_IUnknown) ||
3172 IsEqualGUID(riid, &IID_IDsDriverPropertySet) ) {
3173 IDsDriverPropertySet_AddRef(iface);
3174 *ppobj = (LPVOID)This;
3175 return DS_OK;
3178 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
3180 *ppobj = 0;
3182 return E_NOINTERFACE;
3185 static ULONG WINAPI IDsDriverPropertySetImpl_AddRef(PIDSDRIVERPROPERTYSET iface)
3187 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3188 DWORD ref;
3189 TRACE("(%p) ref was %ld\n", This, This->ref);
3191 ref = InterlockedIncrement(&(This->ref));
3192 return ref;
3195 static ULONG WINAPI IDsDriverPropertySetImpl_Release(PIDSDRIVERPROPERTYSET iface)
3197 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3198 DWORD ref;
3199 TRACE("(%p) ref was %ld\n", This, This->ref);
3201 ref = InterlockedDecrement(&(This->ref));
3202 return ref;
3205 static HRESULT WINAPI IDsDriverPropertySetImpl_Get(
3206 PIDSDRIVERPROPERTYSET iface,
3207 PDSPROPERTY pDsProperty,
3208 LPVOID pPropertyParams,
3209 ULONG cbPropertyParams,
3210 LPVOID pPropertyData,
3211 ULONG cbPropertyData,
3212 PULONG pcbReturnedData )
3214 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3215 FIXME("(%p,%p,%p,%lx,%p,%lx,%p)\n",This,pDsProperty,pPropertyParams,cbPropertyParams,pPropertyData,cbPropertyData,pcbReturnedData);
3216 return DSERR_UNSUPPORTED;
3219 static HRESULT WINAPI IDsDriverPropertySetImpl_Set(
3220 PIDSDRIVERPROPERTYSET iface,
3221 PDSPROPERTY pDsProperty,
3222 LPVOID pPropertyParams,
3223 ULONG cbPropertyParams,
3224 LPVOID pPropertyData,
3225 ULONG cbPropertyData )
3227 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3228 FIXME("(%p,%p,%p,%lx,%p,%lx)\n",This,pDsProperty,pPropertyParams,cbPropertyParams,pPropertyData,cbPropertyData);
3229 return DSERR_UNSUPPORTED;
3232 static HRESULT WINAPI IDsDriverPropertySetImpl_QuerySupport(
3233 PIDSDRIVERPROPERTYSET iface,
3234 REFGUID PropertySetId,
3235 ULONG PropertyId,
3236 PULONG pSupport )
3238 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3239 FIXME("(%p,%s,%lx,%p)\n",This,debugstr_guid(PropertySetId),PropertyId,pSupport);
3240 return DSERR_UNSUPPORTED;
3243 ICOM_VTABLE(IDsDriverPropertySet) dsdpsvt =
3245 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3246 IDsDriverPropertySetImpl_QueryInterface,
3247 IDsDriverPropertySetImpl_AddRef,
3248 IDsDriverPropertySetImpl_Release,
3249 IDsDriverPropertySetImpl_Get,
3250 IDsDriverPropertySetImpl_Set,
3251 IDsDriverPropertySetImpl_QuerySupport,
3254 /*======================================================================*
3255 * Low level DSOUND notify implementation *
3256 *======================================================================*/
3258 typedef struct IDsDriverNotifyImpl IDsDriverNotifyImpl;
3260 struct IDsDriverNotifyImpl
3262 /* IUnknown fields */
3263 ICOM_VFIELD(IDsDriverNotify);
3264 DWORD ref;
3265 /* IDsDriverNotifyImpl fields */
3266 LPDSBPOSITIONNOTIFY notifies;
3267 int nrofnotifies;
3270 static HRESULT WINAPI IDsDriverNotifyImpl_QueryInterface(
3271 PIDSDRIVERNOTIFY iface,
3272 REFIID riid,
3273 LPVOID *ppobj)
3275 ICOM_THIS(IDsDriverNotifyImpl,iface);
3276 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
3278 if ( IsEqualGUID(riid, &IID_IUnknown) ||
3279 IsEqualGUID(riid, &IID_IDsDriverNotify) ) {
3280 IDsDriverNotify_AddRef(iface);
3281 *ppobj = This;
3282 return DS_OK;
3285 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
3287 *ppobj = 0;
3289 return E_NOINTERFACE;
3292 static ULONG WINAPI IDsDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface)
3294 ICOM_THIS(IDsDriverNotifyImpl,iface);
3295 DWORD ref;
3296 TRACE("(%p) ref was %ld\n", This, This->ref);
3298 ref = InterlockedIncrement(&(This->ref));
3299 return ref;
3302 static ULONG WINAPI IDsDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface)
3304 ICOM_THIS(IDsDriverNotifyImpl,iface);
3305 DWORD ref;
3306 TRACE("(%p) ref was %ld\n", This, This->ref);
3308 ref = InterlockedDecrement(&(This->ref));
3309 /* FIXME: A notification should be a part of a buffer rather than pointed
3310 * to from a buffer. Hence the -1 ref count */
3311 if (ref == -1) {
3312 if (This->notifies != NULL)
3313 HeapFree(GetProcessHeap(), 0, This->notifies);
3315 HeapFree(GetProcessHeap(),0,This);
3316 return 0;
3319 return ref;
3322 static HRESULT WINAPI IDsDriverNotifyImpl_SetNotificationPositions(
3323 PIDSDRIVERNOTIFY iface,
3324 DWORD howmuch,
3325 LPCDSBPOSITIONNOTIFY notify)
3327 ICOM_THIS(IDsDriverNotifyImpl,iface);
3328 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
3330 if (!notify) {
3331 WARN("invalid parameter\n");
3332 return DSERR_INVALIDPARAM;
3335 if (TRACE_ON(wave)) {
3336 int i;
3337 for (i=0;i<howmuch;i++)
3338 TRACE("notify at %ld to 0x%08lx\n",
3339 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
3342 /* Make an internal copy of the caller-supplied array.
3343 * Replace the existing copy if one is already present. */
3344 This->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3345 This->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
3346 memcpy(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
3347 This->nrofnotifies = howmuch;
3349 return S_OK;
3352 ICOM_VTABLE(IDsDriverNotify) dsdnvt =
3354 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3355 IDsDriverNotifyImpl_QueryInterface,
3356 IDsDriverNotifyImpl_AddRef,
3357 IDsDriverNotifyImpl_Release,
3358 IDsDriverNotifyImpl_SetNotificationPositions,
3361 /*======================================================================*
3362 * Low level DSOUND capture implementation *
3363 *======================================================================*/
3365 typedef struct IDsCaptureDriverImpl IDsCaptureDriverImpl;
3366 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl;
3368 struct IDsCaptureDriverImpl
3370 /* IUnknown fields */
3371 ICOM_VFIELD(IDsCaptureDriver);
3372 DWORD ref;
3373 /* IDsCaptureDriverImpl fields */
3374 UINT wDevID;
3375 IDsCaptureDriverBufferImpl* capture_buffer;
3378 struct IDsCaptureDriverBufferImpl
3380 /* IUnknown fields */
3381 ICOM_VFIELD(IDsCaptureDriverBuffer);
3382 DWORD ref;
3383 /* IDsCaptureDriverBufferImpl fields */
3384 IDsCaptureDriverImpl* drv;
3385 DWORD buflen;
3387 /* IDsDriverNotifyImpl fields */
3388 IDsDriverNotifyImpl* notify;
3389 int notify_index;
3391 /* IDsDriverPropertySetImpl fields */
3392 IDsDriverPropertySetImpl* property_set;
3395 static HRESULT DSDB_MapCapture(IDsCaptureDriverBufferImpl *dscdb)
3397 WINE_WAVEIN *wwi = &(WInDev[dscdb->drv->wDevID]);
3398 if (!wwi->mapping) {
3399 wwi->mapping = mmap(NULL, wwi->maplen, PROT_WRITE, MAP_SHARED,
3400 wwi->ossdev->fd, 0);
3401 if (wwi->mapping == (LPBYTE)-1) {
3402 TRACE("(%p): Could not map sound device for direct access (%s)\n", dscdb, strerror(errno));
3403 return DSERR_GENERIC;
3405 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dscdb, wwi->mapping, wwi->maplen);
3407 /* for some reason, es1371 and sblive! sometimes have junk in here.
3408 * clear it, or we get junk noise */
3409 /* some libc implementations are buggy: their memset reads from the buffer...
3410 * to work around it, we have to zero the block by hand. We don't do the expected:
3411 * memset(wwo->mapping,0, wwo->maplen);
3414 char* p1 = wwi->mapping;
3415 unsigned len = wwi->maplen;
3417 if (len >= 16) /* so we can have at least a 4 long area to store... */
3419 /* the mmap:ed value is (at least) dword aligned
3420 * so, start filling the complete unsigned long:s
3422 int b = len >> 2;
3423 unsigned long* p4 = (unsigned long*)p1;
3425 while (b--) *p4++ = 0;
3426 /* prepare for filling the rest */
3427 len &= 3;
3428 p1 = (unsigned char*)p4;
3430 /* in all cases, fill the remaining bytes */
3431 while (len-- != 0) *p1++ = 0;
3434 return DS_OK;
3437 static HRESULT DSDB_UnmapCapture(IDsCaptureDriverBufferImpl *dscdb)
3439 WINE_WAVEIN *wwi = &(WInDev[dscdb->drv->wDevID]);
3440 if (wwi->mapping) {
3441 if (munmap(wwi->mapping, wwi->maplen) < 0) {
3442 ERR("(%p): Could not unmap sound device (%s)\n", dscdb, strerror(errno));
3443 return DSERR_GENERIC;
3445 wwi->mapping = NULL;
3446 TRACE("(%p): sound device unmapped\n", dscdb);
3448 return DS_OK;
3451 static HRESULT WINAPI IDsCaptureDriverBufferImpl_QueryInterface(PIDSCDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
3453 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3454 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
3456 if ( IsEqualGUID(riid, &IID_IUnknown) ||
3457 IsEqualGUID(riid, &IID_IDsCaptureDriverBuffer) ) {
3458 IDsCaptureDriverBuffer_AddRef(iface);
3459 *ppobj = (LPVOID)This;
3460 return DS_OK;
3463 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
3464 if (!This->notify) {
3465 This->notify = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->notify));
3466 if (This->notify) {
3467 This->notify->ref = 0; /* release when ref = -1 */
3468 This->notify->lpVtbl = &dsdnvt;
3471 if (This->notify) {
3472 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
3473 *ppobj = (LPVOID)This->notify;
3474 return DS_OK;
3476 *ppobj = 0;
3477 return E_FAIL;
3480 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
3481 if (!This->property_set) {
3482 This->property_set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->property_set));
3483 if (This->property_set) {
3484 This->property_set->ref = 0; /* release when ref = -1 */
3485 This->property_set->lpVtbl = &dsdpsvt;
3488 if (This->property_set) {
3489 IDsDriverPropertySet_AddRef((PIDSDRIVERPROPERTYSET)This->property_set);
3490 *ppobj = (LPVOID)This->property_set;
3491 return DS_OK;
3493 *ppobj = 0;
3494 return E_FAIL;
3497 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
3499 *ppobj = 0;
3501 return DSERR_UNSUPPORTED;
3504 static ULONG WINAPI IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface)
3506 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3507 This->ref++;
3508 return This->ref;
3511 static ULONG WINAPI IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface)
3513 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3514 if (--This->ref)
3515 return This->ref;
3516 DSDB_UnmapCapture(This);
3517 if (This->notify)
3518 IDsDriverNotify_Release((PIDSDRIVERNOTIFY)This->notify);
3519 if (This->property_set)
3520 IDsDriverPropertySet_Release((PIDSDRIVERPROPERTYSET)This->property_set);
3521 HeapFree(GetProcessHeap(),0,This);
3522 return 0;
3525 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Lock(PIDSCDRIVERBUFFER iface,
3526 LPVOID*ppvAudio1,LPDWORD pdwLen1,
3527 LPVOID*ppvAudio2,LPDWORD pdwLen2,
3528 DWORD dwWritePosition,DWORD dwWriteLen,
3529 DWORD dwFlags)
3531 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3532 FIXME("(%p,%p,%p,%p,%p,%ld,%ld,0x%08lx): stub!\n",This,ppvAudio1,pdwLen1,ppvAudio2,pdwLen2,
3533 dwWritePosition,dwWriteLen,dwFlags);
3534 return DS_OK;
3537 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Unlock(PIDSCDRIVERBUFFER iface,
3538 LPVOID pvAudio1,DWORD dwLen1,
3539 LPVOID pvAudio2,DWORD dwLen2)
3541 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3542 FIXME("(%p,%p,%ld,%p,%ld): stub!\n",This,pvAudio1,dwLen1,pvAudio2,dwLen2);
3543 return DS_OK;
3546 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetPosition(PIDSCDRIVERBUFFER iface,
3547 LPDWORD lpdwCapture,
3548 LPDWORD lpdwRead)
3550 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3551 count_info info;
3552 DWORD ptr;
3553 TRACE("(%p,%p,%p)\n",This,lpdwCapture,lpdwRead);
3555 if (WInDev[This->drv->wDevID].state == WINE_WS_CLOSED) {
3556 ERR("device not open, but accessing?\n");
3557 return DSERR_UNINITIALIZED;
3559 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
3560 ERR("ioctl(%s, SNDCTL_DSP_GETIPTR) failed (%s)\n", WInDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
3561 return DSERR_GENERIC;
3563 ptr = info.ptr & ~3; /* align the pointer, just in case */
3564 if (lpdwCapture) *lpdwCapture = ptr;
3565 if (lpdwRead) {
3566 /* add some safety margin (not strictly necessary, but...) */
3567 if (WInDev[This->drv->wDevID].ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
3568 *lpdwRead = ptr + 32;
3569 else
3570 *lpdwRead = ptr + WInDev[This->drv->wDevID].dwFragmentSize;
3571 while (*lpdwRead > This->buflen)
3572 *lpdwRead -= This->buflen;
3574 TRACE("capturepos=%ld, readpos=%ld\n", lpdwCapture?*lpdwCapture:0, lpdwRead?*lpdwRead:0);
3575 return DS_OK;
3578 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetStatus(PIDSCDRIVERBUFFER iface, LPDWORD lpdwStatus)
3580 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3581 FIXME("(%p,%p): stub!\n",This,lpdwStatus);
3582 return DSERR_UNSUPPORTED;
3585 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface, DWORD dwFlags)
3587 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3588 int enable;
3589 TRACE("(%p,%lx)\n",This,dwFlags);
3590 WInDev[This->drv->wDevID].ossdev->bInputEnabled = TRUE;
3591 enable = getEnables(WInDev[This->drv->wDevID].ossdev);
3592 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3593 if (errno == EINVAL) {
3594 /* Don't give up yet. OSS trigger support is inconsistent. */
3595 if (WInDev[This->drv->wDevID].ossdev->open_count == 1) {
3596 /* try the opposite output enable */
3597 if (WInDev[This->drv->wDevID].ossdev->bOutputEnabled == FALSE)
3598 WInDev[This->drv->wDevID].ossdev->bOutputEnabled = TRUE;
3599 else
3600 WInDev[This->drv->wDevID].ossdev->bOutputEnabled = FALSE;
3601 /* try it again */
3602 enable = getEnables(WInDev[This->drv->wDevID].ossdev);
3603 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) >= 0)
3604 return DS_OK;
3607 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
3608 WInDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
3609 return DSERR_GENERIC;
3611 return DS_OK;
3614 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface)
3616 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3617 int enable;
3618 TRACE("(%p)\n",This);
3619 /* no more captureing */
3620 WInDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
3621 enable = getEnables(WInDev[This->drv->wDevID].ossdev);
3622 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3623 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
3624 return DSERR_GENERIC;
3627 /* Most OSS drivers just can't stop capturing without closing the device...
3628 * so we need to somehow signal to our DirectSound implementation
3629 * that it should completely recreate this HW buffer...
3630 * this unexpected error code should do the trick... */
3631 return DSERR_BUFFERLOST;
3634 static HRESULT WINAPI IDsCaptureDriverBufferImpl_SetFormat(PIDSCDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
3636 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3637 FIXME("(%p): stub!\n",This);
3638 return DSERR_UNSUPPORTED;
3641 static ICOM_VTABLE(IDsCaptureDriverBuffer) dscdbvt =
3643 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3644 IDsCaptureDriverBufferImpl_QueryInterface,
3645 IDsCaptureDriverBufferImpl_AddRef,
3646 IDsCaptureDriverBufferImpl_Release,
3647 IDsCaptureDriverBufferImpl_Lock,
3648 IDsCaptureDriverBufferImpl_Unlock,
3649 IDsCaptureDriverBufferImpl_SetFormat,
3650 IDsCaptureDriverBufferImpl_GetPosition,
3651 IDsCaptureDriverBufferImpl_GetStatus,
3652 IDsCaptureDriverBufferImpl_Start,
3653 IDsCaptureDriverBufferImpl_Stop
3656 static HRESULT WINAPI IDsCaptureDriverImpl_QueryInterface(PIDSCDRIVER iface, REFIID riid, LPVOID *ppobj)
3658 ICOM_THIS(IDsCaptureDriverImpl,iface);
3659 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
3661 if ( IsEqualGUID(riid, &IID_IUnknown) ||
3662 IsEqualGUID(riid, &IID_IDsCaptureDriver) ) {
3663 IDsCaptureDriver_AddRef(iface);
3664 *ppobj = (LPVOID)This;
3665 return DS_OK;
3668 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
3670 *ppobj = 0;
3672 return E_NOINTERFACE;
3675 static ULONG WINAPI IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface)
3677 ICOM_THIS(IDsCaptureDriverImpl,iface);
3678 TRACE("(%p)\n",This);
3679 This->ref++;
3680 TRACE("ref=%ld\n",This->ref);
3681 return This->ref;
3684 static ULONG WINAPI IDsCaptureDriverImpl_Release(PIDSCDRIVER iface)
3686 ICOM_THIS(IDsCaptureDriverImpl,iface);
3687 TRACE("(%p)\n",This);
3688 if (--This->ref) {
3689 TRACE("ref=%ld\n",This->ref);
3690 return This->ref;
3692 HeapFree(GetProcessHeap(),0,This);
3693 TRACE("ref=0\n");
3694 return 0;
3697 static HRESULT WINAPI IDsCaptureDriverImpl_GetDriverDesc(PIDSCDRIVER iface, PDSDRIVERDESC pDesc)
3699 ICOM_THIS(IDsCaptureDriverImpl,iface);
3700 TRACE("(%p,%p)\n",This,pDesc);
3702 if (!pDesc) {
3703 TRACE("invalid parameter\n");
3704 return DSERR_INVALIDPARAM;
3707 /* copy version from driver */
3708 memcpy(pDesc, &(WInDev[This->wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
3710 pDesc->dwFlags |= DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
3711 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK |
3712 DSDDESC_DONTNEEDSECONDARYLOCK;
3713 pDesc->dnDevNode = WInDev[This->wDevID].waveDesc.dnDevNode;
3714 pDesc->wVxdId = 0;
3715 pDesc->wReserved = 0;
3716 pDesc->ulDeviceNum = This->wDevID;
3717 pDesc->dwHeapType = DSDHEAP_NOHEAP;
3718 pDesc->pvDirectDrawHeap = NULL;
3719 pDesc->dwMemStartAddress = 0;
3720 pDesc->dwMemEndAddress = 0;
3721 pDesc->dwMemAllocExtra = 0;
3722 pDesc->pvReserved1 = NULL;
3723 pDesc->pvReserved2 = NULL;
3724 return DS_OK;
3727 static HRESULT WINAPI IDsCaptureDriverImpl_Open(PIDSCDRIVER iface)
3729 ICOM_THIS(IDsCaptureDriverImpl,iface);
3730 TRACE("(%p)\n",This);
3731 return DS_OK;
3734 static HRESULT WINAPI IDsCaptureDriverImpl_Close(PIDSCDRIVER iface)
3736 ICOM_THIS(IDsCaptureDriverImpl,iface);
3737 TRACE("(%p)\n",This);
3738 if (This->capture_buffer) {
3739 ERR("problem with DirectSound: capture buffer not released\n");
3740 return DSERR_GENERIC;
3742 return DS_OK;
3745 static HRESULT WINAPI IDsCaptureDriverImpl_GetCaps(PIDSCDRIVER iface, PDSCDRIVERCAPS pCaps)
3747 ICOM_THIS(IDsCaptureDriverImpl,iface);
3748 TRACE("(%p,%p)\n",This,pCaps);
3749 memcpy(pCaps, &(WInDev[This->wDevID].ossdev->dsc_caps), sizeof(DSCDRIVERCAPS));
3750 return DS_OK;
3753 static HRESULT WINAPI IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface,
3754 LPWAVEFORMATEX pwfx,
3755 DWORD dwFlags,
3756 DWORD dwCardAddress,
3757 LPDWORD pdwcbBufferSize,
3758 LPBYTE *ppbBuffer,
3759 LPVOID *ppvObj)
3761 ICOM_THIS(IDsCaptureDriverImpl,iface);
3762 IDsCaptureDriverBufferImpl** ippdscdb = (IDsCaptureDriverBufferImpl**)ppvObj;
3763 HRESULT err;
3764 audio_buf_info info;
3765 int enable;
3766 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",This,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
3768 if (This->capture_buffer) {
3769 TRACE("already allocated\n");
3770 return DSERR_ALLOCATED;
3773 *ippdscdb = (IDsCaptureDriverBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverBufferImpl));
3774 if (*ippdscdb == NULL) {
3775 TRACE("out of memory\n");
3776 return DSERR_OUTOFMEMORY;
3779 (*ippdscdb)->lpVtbl = &dscdbvt;
3780 (*ippdscdb)->ref = 1;
3781 (*ippdscdb)->drv = This;
3782 (*ippdscdb)->notify = 0;
3783 (*ippdscdb)->notify_index = 0;
3784 (*ippdscdb)->property_set = 0;
3786 if (WInDev[This->wDevID].state == WINE_WS_CLOSED) {
3787 WAVEOPENDESC desc;
3788 desc.hWave = 0;
3789 desc.lpFormat = pwfx;
3790 desc.dwCallback = 0;
3791 desc.dwInstance = 0;
3792 desc.uMappedDeviceID = 0;
3793 desc.dnDevNode = 0;
3794 err = widOpen(This->wDevID, &desc, dwFlags | WAVE_DIRECTSOUND);
3795 if (err != MMSYSERR_NOERROR) {
3796 TRACE("widOpen failed\n");
3797 return err;
3801 /* check how big the DMA buffer is now */
3802 if (ioctl(WInDev[This->wDevID].ossdev->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
3803 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", WInDev[This->wDevID].ossdev->dev_name, strerror(errno));
3804 HeapFree(GetProcessHeap(),0,*ippdscdb);
3805 *ippdscdb = NULL;
3806 return DSERR_GENERIC;
3808 WInDev[This->wDevID].maplen = (*ippdscdb)->buflen = info.fragstotal * info.fragsize;
3810 /* map the DMA buffer */
3811 err = DSDB_MapCapture(*ippdscdb);
3812 if (err != DS_OK) {
3813 HeapFree(GetProcessHeap(),0,*ippdscdb);
3814 *ippdscdb = NULL;
3815 return err;
3818 /* capture buffer is ready to go */
3819 *pdwcbBufferSize = WInDev[This->wDevID].maplen;
3820 *ppbBuffer = WInDev[This->wDevID].mapping;
3822 /* some drivers need some extra nudging after mapping */
3823 WInDev[This->wDevID].ossdev->bInputEnabled = FALSE;
3824 enable = getEnables(WInDev[This->wDevID].ossdev);
3825 if (ioctl(WInDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3826 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev[This->wDevID].ossdev->dev_name, strerror(errno));
3827 return DSERR_GENERIC;
3830 This->capture_buffer = *ippdscdb;
3832 return DS_OK;
3835 static ICOM_VTABLE(IDsCaptureDriver) dscdvt =
3837 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3838 IDsCaptureDriverImpl_QueryInterface,
3839 IDsCaptureDriverImpl_AddRef,
3840 IDsCaptureDriverImpl_Release,
3841 IDsCaptureDriverImpl_GetDriverDesc,
3842 IDsCaptureDriverImpl_Open,
3843 IDsCaptureDriverImpl_Close,
3844 IDsCaptureDriverImpl_GetCaps,
3845 IDsCaptureDriverImpl_CreateCaptureBuffer
3848 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
3850 IDsCaptureDriverImpl** idrv = (IDsCaptureDriverImpl**)drv;
3851 TRACE("(%d,%p)\n",wDevID,drv);
3853 /* the HAL isn't much better than the HEL if we can't do mmap() */
3854 if (!(WInDev[wDevID].ossdev->in_caps_support & WAVECAPS_DIRECTSOUND)) {
3855 ERR("DirectSoundCapture flag not set\n");
3856 MESSAGE("This sound card's driver does not support direct access\n");
3857 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
3858 return MMSYSERR_NOTSUPPORTED;
3861 *idrv = (IDsCaptureDriverImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverImpl));
3862 if (!*idrv)
3863 return MMSYSERR_NOMEM;
3864 (*idrv)->lpVtbl = &dscdvt;
3865 (*idrv)->ref = 1;
3867 (*idrv)->wDevID = wDevID;
3868 (*idrv)->capture_buffer = NULL;
3869 return MMSYSERR_NOERROR;
3872 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
3874 memcpy(desc, &(WInDev[wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
3875 return MMSYSERR_NOERROR;
3878 static DWORD widDsGuid(UINT wDevID, LPGUID pGuid)
3880 TRACE("(%d,%p)\n",wDevID,pGuid);
3882 memcpy(pGuid, &(WInDev[wDevID].ossdev->dsc_guid), sizeof(GUID));
3884 return MMSYSERR_NOERROR;
3887 #else /* !HAVE_OSS */
3889 /**************************************************************************
3890 * wodMessage (WINEOSS.7)
3892 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3893 DWORD dwParam1, DWORD dwParam2)
3895 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3896 return MMSYSERR_NOTENABLED;
3899 /**************************************************************************
3900 * widMessage (WINEOSS.6)
3902 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3903 DWORD dwParam1, DWORD dwParam2)
3905 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3906 return MMSYSERR_NOTENABLED;
3909 #endif /* HAVE_OSS */