Use same GUIDs as win2k and xp for dsound drivers.
[wine/multimedia.git] / dlls / winmm / wineoss / audio.c
blob67169b06de59131f246c7d407601a167bc6b73e0
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"
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47 #include <errno.h>
48 #include <fcntl.h>
49 #ifdef HAVE_SYS_IOCTL_H
50 # include <sys/ioctl.h>
51 #endif
52 #ifdef HAVE_SYS_MMAN_H
53 # include <sys/mman.h>
54 #endif
55 #ifdef HAVE_SYS_POLL_H
56 # include <sys/poll.h>
57 #endif
59 #include "windef.h"
60 #include "wingdi.h"
61 #include "winerror.h"
62 #include "wine/winuser16.h"
63 #include "mmddk.h"
64 #include "dsound.h"
65 #include "dsdriver.h"
66 #include "oss.h"
67 #include "wine/debug.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(wave);
71 /* Allow 1% deviation for sample rates (some ES137x cards) */
72 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
74 #ifdef HAVE_OSS
76 #define MAX_WAVEDRV (6)
78 /* state diagram for waveOut writing:
80 * +---------+-------------+---------------+---------------------------------+
81 * | state | function | event | new state |
82 * +---------+-------------+---------------+---------------------------------+
83 * | | open() | | STOPPED |
84 * | PAUSED | write() | | PAUSED |
85 * | STOPPED | write() | <thrd create> | PLAYING |
86 * | PLAYING | write() | HEADER | PLAYING |
87 * | (other) | write() | <error> | |
88 * | (any) | pause() | PAUSING | PAUSED |
89 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
90 * | (any) | reset() | RESETTING | STOPPED |
91 * | (any) | close() | CLOSING | CLOSED |
92 * +---------+-------------+---------------+---------------------------------+
95 /* states of the playing device */
96 #define WINE_WS_PLAYING 0
97 #define WINE_WS_PAUSED 1
98 #define WINE_WS_STOPPED 2
99 #define WINE_WS_CLOSED 3
101 /* events to be send to device */
102 enum win_wm_message {
103 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
104 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
107 #ifdef USE_PIPE_SYNC
108 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
109 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
110 #define RESET_OMR(omr) do { } while (0)
111 #define WAIT_OMR(omr, sleep) \
112 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
113 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
114 #else
115 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
116 #define CLEAR_OMR(omr) do { } while (0)
117 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
118 #define WAIT_OMR(omr, sleep) \
119 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
120 #endif
122 typedef struct {
123 enum win_wm_message msg; /* message identifier */
124 DWORD param; /* parameter for this message */
125 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
126 } OSS_MSG;
128 /* implement an in-process message ring for better performance
129 * (compared to passing thru the server)
130 * this ring will be used by the input (resp output) record (resp playback) routine
132 #define OSS_RING_BUFFER_INCREMENT 64
133 typedef struct {
134 int ring_buffer_size;
135 OSS_MSG * messages;
136 int msg_tosave;
137 int msg_toget;
138 #ifdef USE_PIPE_SYNC
139 int msg_pipe[2];
140 #else
141 HANDLE msg_event;
142 #endif
143 CRITICAL_SECTION msg_crst;
144 } OSS_MSG_RING;
146 typedef struct tagOSS_DEVICE {
147 char dev_name[32];
148 char mixer_name[32];
149 unsigned open_count;
150 WAVEOUTCAPSA out_caps;
151 WAVEINCAPSA in_caps;
152 DWORD in_caps_support;
153 unsigned open_access;
154 int fd;
155 DWORD owner_tid;
156 int sample_rate;
157 int stereo;
158 int format;
159 unsigned audio_fragment;
160 BOOL full_duplex;
161 BOOL bTriggerSupport;
162 BOOL bOutputEnabled;
163 BOOL bInputEnabled;
164 DSDRIVERDESC ds_desc;
165 DSDRIVERCAPS ds_caps;
166 DSCDRIVERCAPS dsc_caps;
167 GUID ds_guid;
168 GUID dsc_guid;
169 } OSS_DEVICE;
171 static OSS_DEVICE OSS_Devices[MAX_WAVEDRV];
173 typedef struct {
174 OSS_DEVICE* ossdev;
175 volatile int state; /* one of the WINE_WS_ manifest constants */
176 WAVEOPENDESC waveDesc;
177 WORD wFlags;
178 PCMWAVEFORMAT format;
180 /* OSS information */
181 DWORD dwFragmentSize; /* size of OSS buffer fragment */
182 DWORD dwBufferSize; /* size of whole OSS buffer in bytes */
183 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
184 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
185 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
187 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
188 DWORD dwLoops; /* private copy of loop counter */
190 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
191 DWORD dwWrittenTotal; /* number of bytes written to OSS buffer since opening */
192 BOOL bNeedPost; /* whether audio still needs to be physically started */
194 /* synchronization stuff */
195 HANDLE hStartUpEvent;
196 HANDLE hThread;
197 DWORD dwThreadID;
198 OSS_MSG_RING msgRing;
200 /* DirectSound stuff */
201 LPBYTE mapping;
202 DWORD maplen;
203 } WINE_WAVEOUT;
205 typedef struct {
206 OSS_DEVICE* ossdev;
207 volatile int state;
208 DWORD dwFragmentSize; /* OpenSound '/dev/dsp' give us that size */
209 WAVEOPENDESC waveDesc;
210 WORD wFlags;
211 PCMWAVEFORMAT format;
212 LPWAVEHDR lpQueuePtr;
213 DWORD dwTotalRecorded;
215 /* synchronization stuff */
216 HANDLE hThread;
217 DWORD dwThreadID;
218 HANDLE hStartUpEvent;
219 OSS_MSG_RING msgRing;
221 /* DirectSound stuff */
222 LPBYTE mapping;
223 DWORD maplen;
224 } WINE_WAVEIN;
226 static WINE_WAVEOUT WOutDev [MAX_WAVEDRV];
227 static WINE_WAVEIN WInDev [MAX_WAVEDRV];
228 static unsigned numOutDev;
229 static unsigned numInDev;
231 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
232 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv);
233 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
234 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc);
235 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid);
236 static DWORD widDsGuid(UINT wDevID, LPGUID pGuid);
238 /* These strings used only for tracing */
239 static const char *wodPlayerCmdString[] = {
240 "WINE_WM_PAUSING",
241 "WINE_WM_RESTARTING",
242 "WINE_WM_RESETTING",
243 "WINE_WM_HEADER",
244 "WINE_WM_UPDATE",
245 "WINE_WM_BREAKLOOP",
246 "WINE_WM_CLOSING",
247 "WINE_WM_STARTING",
248 "WINE_WM_STOPPING",
251 static int getEnables(OSS_DEVICE *ossdev)
253 return ( (ossdev->bOutputEnabled ? PCM_ENABLE_OUTPUT : 0) |
254 (ossdev->bInputEnabled ? PCM_ENABLE_INPUT : 0) );
257 /*======================================================================*
258 * Low level WAVE implementation *
259 *======================================================================*/
261 /******************************************************************
262 * OSS_RawOpenDevice
264 * Low level device opening (from values stored in ossdev)
266 static DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
268 int fd, val, rc;
269 TRACE("(%p,%d)\n",ossdev,strict_format);
271 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
273 WARN("Couldn't open %s (%s)\n", ossdev->dev_name, strerror(errno));
274 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
276 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
277 /* turn full duplex on if it has been requested */
278 if (ossdev->open_access == O_RDWR && ossdev->full_duplex) {
279 rc = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
280 /* on *BSD, as full duplex is always enabled by default, this ioctl
281 * will fail with EINVAL
282 * so, we don't consider EINVAL an error here
284 if (rc != 0 && errno != EINVAL) {
285 ERR("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev->dev_name, strerror(errno));
286 goto error2;
290 if (ossdev->audio_fragment) {
291 rc = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
292 if (rc != 0) {
293 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev->dev_name, strerror(errno));
294 goto error2;
298 /* First size and stereo then samplerate */
299 if (ossdev->format>=0)
301 val = ossdev->format;
302 rc = ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
303 if (rc != 0 || val != ossdev->format) {
304 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
305 if (strict_format)
306 goto error;
309 if (ossdev->stereo>=0)
311 val = ossdev->stereo;
312 rc = ioctl(fd, SNDCTL_DSP_STEREO, &ossdev->stereo);
313 if (rc != 0 || val != ossdev->stereo) {
314 TRACE("Can't set stereo to %u (returned %d)\n", val, ossdev->stereo);
315 if (strict_format)
316 goto error;
319 if (ossdev->sample_rate>=0)
321 val = ossdev->sample_rate;
322 rc = ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
323 if (rc != 0 || !NEAR_MATCH(val, ossdev->sample_rate)) {
324 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
325 if (strict_format)
326 goto error;
329 ossdev->fd = fd;
331 if (ossdev->bTriggerSupport) {
332 int trigger;
333 rc = ioctl(fd, SNDCTL_DSP_GETTRIGGER, &trigger);
334 if (rc != 0) {
335 ERR("ioctl(%s, SNDCTL_DSP_GETTRIGGER) failed (%s)\n",
336 ossdev->dev_name, strerror(errno));
337 goto error;
340 ossdev->bOutputEnabled = ((trigger & PCM_ENABLE_OUTPUT) == PCM_ENABLE_OUTPUT);
341 ossdev->bInputEnabled = ((trigger & PCM_ENABLE_INPUT) == PCM_ENABLE_INPUT);
342 } else {
343 ossdev->bOutputEnabled = TRUE; /* OSS enables by default */
344 ossdev->bInputEnabled = TRUE; /* OSS enables by default */
347 return MMSYSERR_NOERROR;
349 error:
350 close(fd);
351 return WAVERR_BADFORMAT;
352 error2:
353 close(fd);
354 return MMSYSERR_ERROR;
357 /******************************************************************
358 * OSS_OpenDevice
360 * since OSS has poor capabilities in full duplex, we try here to let a program
361 * open the device for both waveout and wavein streams...
362 * this is hackish, but it's the way OSS interface is done...
364 static DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
365 int* frag, int strict_format,
366 int sample_rate, int stereo, int fmt)
368 DWORD ret;
369 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev,req_access,frag,strict_format,sample_rate,stereo,fmt);
371 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
372 req_access = O_RDWR;
374 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
375 if (ossdev->open_count == 0)
377 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
379 ossdev->audio_fragment = (frag) ? *frag : 0;
380 ossdev->sample_rate = sample_rate;
381 ossdev->stereo = stereo;
382 ossdev->format = fmt;
383 ossdev->open_access = req_access;
384 ossdev->owner_tid = GetCurrentThreadId();
386 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
388 else
390 /* check we really open with the same parameters */
391 if (ossdev->open_access != req_access)
393 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
394 return WAVERR_BADFORMAT;
397 /* check if the audio parameters are the same */
398 if (ossdev->sample_rate != sample_rate ||
399 ossdev->stereo != stereo ||
400 ossdev->format != fmt)
402 /* This is not a fatal error because MSACM might do the remapping */
403 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
404 "OSS doesn't allow us different parameters\n"
405 "audio_frag(%x/%x) sample_rate(%d/%d) stereo(%d/%d) fmt(%d/%d)\n",
406 ossdev->audio_fragment, frag ? *frag : 0,
407 ossdev->sample_rate, sample_rate,
408 ossdev->stereo, stereo,
409 ossdev->format, fmt);
410 return WAVERR_BADFORMAT;
412 /* check if the fragment sizes are the same */
413 if (ossdev->audio_fragment != (frag ? *frag : 0) ) {
414 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
415 "Use: \"HardwareAcceleration\" = \"Emulation\" in the [dsound] section of your config file.\n");
416 return WAVERR_BADFORMAT;
418 if (GetCurrentThreadId() != ossdev->owner_tid)
420 WARN("Another thread is trying to access audio...\n");
421 return MMSYSERR_ERROR;
425 ossdev->open_count++;
427 return MMSYSERR_NOERROR;
430 /******************************************************************
431 * OSS_CloseDevice
435 static void OSS_CloseDevice(OSS_DEVICE* ossdev)
437 TRACE("(%p)\n",ossdev);
438 if (ossdev->open_count>0) {
439 ossdev->open_count--;
440 } else {
441 WARN("OSS_CloseDevice called too many times\n");
443 if (ossdev->open_count == 0)
445 /* reset the device before we close it in case it is in a bad state */
446 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
447 close(ossdev->fd);
451 /******************************************************************
452 * OSS_ResetDevice
454 * Resets the device. OSS Commercial requires the device to be closed
455 * after a SNDCTL_DSP_RESET ioctl call... this function implements
456 * this behavior...
457 * FIXME: This causes problems when doing full duplex so we really
458 * only reset when not doing full duplex. We need to do this better
459 * someday.
461 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
463 DWORD ret = MMSYSERR_NOERROR;
464 int old_fd = ossdev->fd;
465 TRACE("(%p)\n", ossdev);
467 if (ossdev->open_count == 1) {
468 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
470 perror("ioctl SNDCTL_DSP_RESET");
471 return -1;
473 close(ossdev->fd);
474 ret = OSS_RawOpenDevice(ossdev, 1);
475 TRACE("Changing fd from %d to %d\n", old_fd, ossdev->fd);
476 } else
477 WARN("Not resetting device because it is in full duplex mode!\n");
479 return ret;
482 const static int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
483 const static int win_std_rates[5]={96000,48000,44100,22050,11025};
484 const static int win_std_formats[2][2][5]=
485 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
486 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
487 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
488 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
489 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
490 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
491 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
492 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
495 /******************************************************************
496 * OSS_WaveOutInit
500 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
502 int rc,arg;
503 int f,c,r;
504 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
506 if (OSS_OpenDevice(ossdev, O_WRONLY, NULL, 0,-1,-1,-1) != 0) return FALSE;
507 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
509 #ifdef SOUND_MIXER_INFO
511 int mixer;
512 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
513 mixer_info info;
514 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
515 close(mixer);
516 strncpy(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
517 strcpy(ossdev->ds_desc.szDrvName, "wineoss.drv");
518 strncpy(ossdev->out_caps.szPname, info.name, sizeof(info.name));
519 TRACE("%s\n", ossdev->ds_desc.szDesc);
520 } else {
521 ERR("%s: can't read info!\n", ossdev->mixer_name);
522 OSS_CloseDevice(ossdev);
523 close(mixer);
524 return FALSE;
526 } else {
527 ERR("%s: not found!\n", ossdev->mixer_name);
528 OSS_CloseDevice(ossdev);
529 return FALSE;
532 #endif /* SOUND_MIXER_INFO */
534 /* FIXME: some programs compare this string against the content of the
535 * registry for MM drivers. The names have to match in order for the
536 * program to work (e.g. MS win9x mplayer.exe)
538 #ifdef EMULATE_SB16
539 ossdev->out_caps.wMid = 0x0002;
540 ossdev->out_caps.wPid = 0x0104;
541 strcpy(ossdev->out_caps.szPname, "SB16 Wave Out");
542 #else
543 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
544 ossdev->out_caps.wPid = 0x0001; /* Product ID */
545 #endif
546 ossdev->out_caps.vDriverVersion = 0x0100;
547 ossdev->out_caps.wChannels = 1;
548 ossdev->out_caps.dwFormats = 0x00000000;
549 ossdev->out_caps.wReserved1 = 0;
550 ossdev->out_caps.dwSupport = WAVECAPS_VOLUME;
552 /* direct sound caps */
553 ossdev->ds_caps.dwFlags = 0;
554 ossdev->ds_caps.dwPrimaryBuffers = 1;
556 if (WINE_TRACE_ON(wave)) {
557 /* Note that this only reports the formats supported by the hardware.
558 * The driver may support other formats and do the conversions in
559 * software which is why we don't use this value
561 int oss_mask;
562 ioctl(ossdev->fd, SNDCTL_DSP_GETFMTS, &oss_mask);
563 TRACE("OSS dsp out mask=%08x\n", oss_mask);
566 /* We must first set the format and the stereo mode as some sound cards
567 * may support 44kHz mono but not 44kHz stereo. Also we must
568 * systematically check the return value of these ioctls as they will
569 * always succeed (see OSS Linux) but will modify the parameter to match
570 * whatever they support. The OSS specs also say we must first set the
571 * sample size, then the stereo and then the sample rate.
573 for (f=0;f<2;f++) {
574 arg=win_std_oss_fmts[f];
575 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
576 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
577 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
578 rc,arg,win_std_oss_fmts[f]);
579 continue;
581 if (f == 0)
582 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY8BIT;
583 else if (f == 1)
584 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY16BIT;
586 for (c=0;c<2;c++) {
587 arg=c;
588 rc=ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &arg);
589 if (rc!=0 || arg!=c) {
590 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc,arg,c);
591 continue;
593 if (c == 0) {
594 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
595 } else if (c==1) {
596 ossdev->out_caps.wChannels=2;
597 ossdev->out_caps.dwSupport|=WAVECAPS_LRVOLUME;
598 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
601 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
602 arg=win_std_rates[r];
603 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
604 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
605 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c+1);
606 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]))
607 ossdev->out_caps.dwFormats|=win_std_formats[f][c][r];
612 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
613 TRACE("OSS dsp out caps=%08X\n", arg);
614 if (arg & DSP_CAP_TRIGGER)
615 ossdev->bTriggerSupport = TRUE;
616 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
617 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
619 /* well, might as well use the DirectSound cap flag for something */
620 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
621 !(arg & DSP_CAP_BATCH)) {
622 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
623 } else {
624 ossdev->ds_caps.dwFlags |= DSCAPS_EMULDRIVER;
627 OSS_CloseDevice(ossdev);
628 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
629 ossdev->out_caps.dwFormats, ossdev->out_caps.dwSupport);
630 return TRUE;
633 /******************************************************************
634 * OSS_WaveInInit
638 static BOOL OSS_WaveInInit(OSS_DEVICE* ossdev)
640 int rc,arg;
641 int f,c,r;
642 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
644 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0,-1,-1,-1) != 0) return FALSE;
645 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
647 #ifdef SOUND_MIXER_INFO
649 int mixer;
650 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
651 mixer_info info;
652 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
653 close(mixer);
654 strncpy(ossdev->in_caps.szPname, info.name, sizeof(info.name));
655 TRACE("%s\n", ossdev->ds_desc.szDesc);
656 } else {
657 ERR("%s: can't read info!\n", ossdev->mixer_name);
658 OSS_CloseDevice(ossdev);
659 close(mixer);
660 return FALSE;
662 } else {
663 ERR("%s: not found!\n", ossdev->mixer_name);
664 OSS_CloseDevice(ossdev);
665 return FALSE;
668 #endif /* SOUND_MIXER_INFO */
670 /* See comment in OSS_WaveOutInit */
671 #ifdef EMULATE_SB16
672 ossdev->in_caps.wMid = 0x0002;
673 ossdev->in_caps.wPid = 0x0004;
674 strcpy(ossdev->in_caps.szPname, "SB16 Wave In");
675 #else
676 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
677 ossdev->in_caps.wPid = 0x0001; /* Product ID */
678 #endif
679 ossdev->in_caps.dwFormats = 0x00000000;
680 ossdev->in_caps.wChannels = 1;
681 ossdev->in_caps.wReserved1 = 0;
683 /* direct sound caps */
684 ossdev->dsc_caps.dwSize = sizeof(ossdev->dsc_caps);
685 ossdev->dsc_caps.dwFlags = 0;
686 ossdev->dsc_caps.dwFormats = 0x00000000;
687 ossdev->dsc_caps.dwChannels = 1;
689 if (WINE_TRACE_ON(wave)) {
690 /* Note that this only reports the formats supported by the hardware.
691 * The driver may support other formats and do the conversions in
692 * software which is why we don't use this value
694 int oss_mask;
695 ioctl(ossdev->fd, SNDCTL_DSP_GETFMTS, &oss_mask);
696 TRACE("OSS dsp out mask=%08x\n", oss_mask);
699 /* See the comment in OSS_WaveOutInit */
700 for (f=0;f<2;f++) {
701 arg=win_std_oss_fmts[f];
702 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
703 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
704 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
705 rc,arg,win_std_oss_fmts[f]);
706 continue;
709 for (c=0;c<2;c++) {
710 arg=c;
711 rc=ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &arg);
712 if (rc!=0 || arg!=c) {
713 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc,arg,c);
714 continue;
716 if (c==1) {
717 ossdev->in_caps.wChannels=2;
718 ossdev->dsc_caps.dwChannels=2;
721 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
722 arg=win_std_rates[r];
723 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
724 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);
725 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]))
726 ossdev->in_caps.dwFormats|=win_std_formats[f][c][r];
727 ossdev->dsc_caps.dwFormats|=win_std_formats[f][c][r];
732 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
733 TRACE("OSS dsp in caps=%08X\n", arg);
734 if (arg & DSP_CAP_TRIGGER)
735 ossdev->bTriggerSupport = TRUE;
736 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
737 !(arg & DSP_CAP_BATCH)) {
738 /* FIXME: enable the next statement if you want to work on the driver */
739 #if 0
740 ossdev->in_caps_support |= WAVECAPS_DIRECTSOUND;
741 #endif
743 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH))
744 ossdev->in_caps_support |= WAVECAPS_SAMPLEACCURATE;
746 OSS_CloseDevice(ossdev);
747 TRACE("in dwFormats = %08lX\n", ossdev->in_caps.dwFormats);
748 return TRUE;
751 /******************************************************************
752 * OSS_WaveFullDuplexInit
756 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
758 int caps;
759 TRACE("(%p)\n",ossdev);
761 if (OSS_OpenDevice(ossdev, O_RDWR, NULL, 0,-1,-1,-1) != 0) return;
762 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
764 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
766 OSS_CloseDevice(ossdev);
769 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
770 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
771 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
772 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
773 guid.Data4[6] = b7; guid.Data4[7] = b8;
774 /******************************************************************
775 * OSS_WaveInit
777 * Initialize internal structures from OSS information
779 LONG OSS_WaveInit(void)
781 int i;
782 TRACE("()\n");
784 for (i = 0; i < MAX_WAVEDRV; ++i)
786 if (i == 0) {
787 sprintf((char *)OSS_Devices[i].dev_name, "/dev/dsp");
788 sprintf((char *)OSS_Devices[i].mixer_name, "/dev/mixer");
789 } else {
790 sprintf((char *)OSS_Devices[i].dev_name, "/dev/dsp%d", i);
791 sprintf((char *)OSS_Devices[i].mixer_name, "/dev/mixer%d", i);
794 INIT_GUID(OSS_Devices[i].ds_guid, 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
795 INIT_GUID(OSS_Devices[i].dsc_guid, 0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
798 /* start with output devices */
799 for (i = 0; i < MAX_WAVEDRV; ++i)
801 if (OSS_WaveOutInit(&OSS_Devices[i]))
803 WOutDev[numOutDev].state = WINE_WS_CLOSED;
804 WOutDev[numOutDev].ossdev = &OSS_Devices[i];
805 numOutDev++;
809 /* then do input devices */
810 for (i = 0; i < MAX_WAVEDRV; ++i)
812 if (OSS_WaveInInit(&OSS_Devices[i]))
814 WInDev[numInDev].state = WINE_WS_CLOSED;
815 WInDev[numInDev].ossdev = &OSS_Devices[i];
816 numInDev++;
820 /* finish with the full duplex bits */
821 for (i = 0; i < MAX_WAVEDRV; i++)
822 OSS_WaveFullDuplexInit(&OSS_Devices[i]);
824 return 0;
827 /******************************************************************
828 * OSS_InitRingMessage
830 * Initialize the ring of messages for passing between driver's caller and playback/record
831 * thread
833 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
835 omr->msg_toget = 0;
836 omr->msg_tosave = 0;
837 #ifdef USE_PIPE_SYNC
838 if (pipe(omr->msg_pipe) < 0) {
839 omr->msg_pipe[0] = -1;
840 omr->msg_pipe[1] = -1;
841 ERR("could not create pipe, error=%s\n", strerror(errno));
843 #else
844 omr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
845 #endif
846 omr->ring_buffer_size = OSS_RING_BUFFER_INCREMENT;
847 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(OSS_MSG));
848 InitializeCriticalSection(&omr->msg_crst);
849 return 0;
852 /******************************************************************
853 * OSS_DestroyRingMessage
856 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
858 #ifdef USE_PIPE_SYNC
859 close(omr->msg_pipe[0]);
860 close(omr->msg_pipe[1]);
861 #else
862 CloseHandle(omr->msg_event);
863 #endif
864 HeapFree(GetProcessHeap(),0,omr->messages);
865 DeleteCriticalSection(&omr->msg_crst);
866 return 0;
869 /******************************************************************
870 * OSS_AddRingMessage
872 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
874 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
876 HANDLE hEvent = INVALID_HANDLE_VALUE;
878 EnterCriticalSection(&omr->msg_crst);
879 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
881 omr->ring_buffer_size += OSS_RING_BUFFER_INCREMENT;
882 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
883 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(OSS_MSG));
885 if (wait)
887 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
888 if (hEvent == INVALID_HANDLE_VALUE)
890 ERR("can't create event !?\n");
891 LeaveCriticalSection(&omr->msg_crst);
892 return 0;
894 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
895 FIXME("two fast messages in the queue!!!!\n");
897 /* fast messages have to be added at the start of the queue */
898 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
900 omr->messages[omr->msg_toget].msg = msg;
901 omr->messages[omr->msg_toget].param = param;
902 omr->messages[omr->msg_toget].hEvent = hEvent;
904 else
906 omr->messages[omr->msg_tosave].msg = msg;
907 omr->messages[omr->msg_tosave].param = param;
908 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
909 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
911 LeaveCriticalSection(&omr->msg_crst);
912 /* signal a new message */
913 SIGNAL_OMR(omr);
914 if (wait)
916 /* wait for playback/record thread to have processed the message */
917 WaitForSingleObject(hEvent, INFINITE);
918 CloseHandle(hEvent);
920 return 1;
923 /******************************************************************
924 * OSS_RetrieveRingMessage
926 * Get a message from the ring. Should be called by the playback/record thread.
928 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
929 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
931 EnterCriticalSection(&omr->msg_crst);
933 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
935 LeaveCriticalSection(&omr->msg_crst);
936 return 0;
939 *msg = omr->messages[omr->msg_toget].msg;
940 omr->messages[omr->msg_toget].msg = 0;
941 *param = omr->messages[omr->msg_toget].param;
942 *hEvent = omr->messages[omr->msg_toget].hEvent;
943 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
944 CLEAR_OMR(omr);
945 LeaveCriticalSection(&omr->msg_crst);
946 return 1;
949 /******************************************************************
950 * OSS_PeekRingMessage
952 * Peek at a message from the ring but do not remove it.
953 * Should be called by the playback/record thread.
955 static int OSS_PeekRingMessage(OSS_MSG_RING* omr,
956 enum win_wm_message *msg,
957 DWORD *param, HANDLE *hEvent)
959 EnterCriticalSection(&omr->msg_crst);
961 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
963 LeaveCriticalSection(&omr->msg_crst);
964 return 0;
967 *msg = omr->messages[omr->msg_toget].msg;
968 *param = omr->messages[omr->msg_toget].param;
969 *hEvent = omr->messages[omr->msg_toget].hEvent;
970 LeaveCriticalSection(&omr->msg_crst);
971 return 1;
974 /*======================================================================*
975 * Low level WAVE OUT implementation *
976 *======================================================================*/
978 /**************************************************************************
979 * wodNotifyClient [internal]
981 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
983 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
985 switch (wMsg) {
986 case WOM_OPEN:
987 case WOM_CLOSE:
988 case WOM_DONE:
989 if (wwo->wFlags != DCB_NULL &&
990 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
991 (HDRVR)wwo->waveDesc.hWave, wMsg,
992 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
993 WARN("can't notify client !\n");
994 return MMSYSERR_ERROR;
996 break;
997 default:
998 FIXME("Unknown callback message %u\n", wMsg);
999 return MMSYSERR_INVALPARAM;
1001 return MMSYSERR_NOERROR;
1004 /**************************************************************************
1005 * wodUpdatePlayedTotal [internal]
1008 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
1010 audio_buf_info dspspace;
1011 if (!info) info = &dspspace;
1013 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
1014 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
1015 return FALSE;
1017 wwo->dwPlayedTotal = wwo->dwWrittenTotal - (wwo->dwBufferSize - info->bytes);
1018 return TRUE;
1021 /**************************************************************************
1022 * wodPlayer_BeginWaveHdr [internal]
1024 * Makes the specified lpWaveHdr the currently playing wave header.
1025 * If the specified wave header is a begin loop and we're not already in
1026 * a loop, setup the loop.
1028 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1030 wwo->lpPlayPtr = lpWaveHdr;
1032 if (!lpWaveHdr) return;
1034 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1035 if (wwo->lpLoopPtr) {
1036 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1037 } else {
1038 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1039 wwo->lpLoopPtr = lpWaveHdr;
1040 /* Windows does not touch WAVEHDR.dwLoops,
1041 * so we need to make an internal copy */
1042 wwo->dwLoops = lpWaveHdr->dwLoops;
1045 wwo->dwPartialOffset = 0;
1048 /**************************************************************************
1049 * wodPlayer_PlayPtrNext [internal]
1051 * Advance the play pointer to the next waveheader, looping if required.
1053 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
1055 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1057 wwo->dwPartialOffset = 0;
1058 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1059 /* We're at the end of a loop, loop if required */
1060 if (--wwo->dwLoops > 0) {
1061 wwo->lpPlayPtr = wwo->lpLoopPtr;
1062 } else {
1063 /* Handle overlapping loops correctly */
1064 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1065 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1066 /* shall we consider the END flag for the closing loop or for
1067 * the opening one or for both ???
1068 * code assumes for closing loop only
1070 } else {
1071 lpWaveHdr = lpWaveHdr->lpNext;
1073 wwo->lpLoopPtr = NULL;
1074 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1076 } else {
1077 /* We're not in a loop. Advance to the next wave header */
1078 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1081 return lpWaveHdr;
1084 /**************************************************************************
1085 * wodPlayer_DSPWait [internal]
1086 * Returns the number of milliseconds to wait for the DSP buffer to write
1087 * one fragment.
1089 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1091 /* time for one fragment to be played */
1092 return wwo->dwFragmentSize * 1000 / wwo->format.wf.nAvgBytesPerSec;
1095 /**************************************************************************
1096 * wodPlayer_NotifyWait [internal]
1097 * Returns the number of milliseconds to wait before attempting to notify
1098 * completion of the specified wavehdr.
1099 * This is based on the number of bytes remaining to be written in the
1100 * wave.
1102 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1104 DWORD dwMillis;
1106 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1107 dwMillis = 1;
1108 } else {
1109 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
1110 if (!dwMillis) dwMillis = 1;
1113 return dwMillis;
1117 /**************************************************************************
1118 * wodPlayer_WriteMaxFrags [internal]
1119 * Writes the maximum number of bytes possible to the DSP and returns
1120 * TRUE iff the current playPtr has been fully played
1122 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
1124 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1125 DWORD toWrite = min(dwLength, *bytes);
1126 int written;
1127 BOOL ret = FALSE;
1129 TRACE("Writing wavehdr %p.%lu[%lu]/%lu\n",
1130 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
1132 if (toWrite > 0)
1134 written = write(wwo->ossdev->fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
1135 if (written <= 0) return FALSE;
1137 else
1138 written = 0;
1140 if (written >= dwLength) {
1141 /* If we wrote all current wavehdr, skip to the next one */
1142 wodPlayer_PlayPtrNext(wwo);
1143 ret = TRUE;
1144 } else {
1145 /* Remove the amount written */
1146 wwo->dwPartialOffset += written;
1148 *bytes -= written;
1149 wwo->dwWrittenTotal += written;
1151 return ret;
1155 /**************************************************************************
1156 * wodPlayer_NotifyCompletions [internal]
1158 * Notifies and remove from queue all wavehdrs which have been played to
1159 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1160 * we notify all wavehdrs and remove them all from the queue even if they
1161 * are unplayed or part of a loop.
1163 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1165 LPWAVEHDR lpWaveHdr;
1167 /* Start from lpQueuePtr and keep notifying until:
1168 * - we hit an unwritten wavehdr
1169 * - we hit the beginning of a running loop
1170 * - we hit a wavehdr which hasn't finished playing
1172 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1173 (force ||
1174 (lpWaveHdr != wwo->lpPlayPtr &&
1175 lpWaveHdr != wwo->lpLoopPtr &&
1176 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1178 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1180 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1181 lpWaveHdr->dwFlags |= WHDR_DONE;
1183 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1185 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1186 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1189 /**************************************************************************
1190 * wodPlayer_Reset [internal]
1192 * wodPlayer helper. Resets current output stream.
1194 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
1196 wodUpdatePlayedTotal(wwo, NULL);
1197 /* updates current notify list */
1198 wodPlayer_NotifyCompletions(wwo, FALSE);
1200 /* flush all possible output */
1201 if (OSS_ResetDevice(wwo->ossdev) != MMSYSERR_NOERROR)
1203 wwo->hThread = 0;
1204 wwo->state = WINE_WS_STOPPED;
1205 ExitThread(-1);
1208 if (reset) {
1209 enum win_wm_message msg;
1210 DWORD param;
1211 HANDLE ev;
1213 /* remove any buffer */
1214 wodPlayer_NotifyCompletions(wwo, TRUE);
1216 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1217 wwo->state = WINE_WS_STOPPED;
1218 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1219 /* Clear partial wavehdr */
1220 wwo->dwPartialOffset = 0;
1222 /* remove any existing message in the ring */
1223 EnterCriticalSection(&wwo->msgRing.msg_crst);
1224 /* return all pending headers in queue */
1225 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1227 if (msg != WINE_WM_HEADER)
1229 FIXME("shouldn't have headers left\n");
1230 SetEvent(ev);
1231 continue;
1233 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1234 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1236 wodNotifyClient(wwo, WOM_DONE, param, 0);
1238 RESET_OMR(&wwo->msgRing);
1239 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1240 } else {
1241 if (wwo->lpLoopPtr) {
1242 /* complicated case, not handled yet (could imply modifying the loop counter */
1243 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1244 wwo->lpPlayPtr = wwo->lpLoopPtr;
1245 wwo->dwPartialOffset = 0;
1246 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1247 } else {
1248 LPWAVEHDR ptr;
1249 DWORD sz = wwo->dwPartialOffset;
1251 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1252 /* compute the max size playable from lpQueuePtr */
1253 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1254 sz += ptr->dwBufferLength;
1256 /* because the reset lpPlayPtr will be lpQueuePtr */
1257 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1258 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1259 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1260 wwo->lpPlayPtr = wwo->lpQueuePtr;
1262 wwo->state = WINE_WS_PAUSED;
1266 /**************************************************************************
1267 * wodPlayer_ProcessMessages [internal]
1269 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1271 LPWAVEHDR lpWaveHdr;
1272 enum win_wm_message msg;
1273 DWORD param;
1274 HANDLE ev;
1276 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1277 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
1278 switch (msg) {
1279 case WINE_WM_PAUSING:
1280 wodPlayer_Reset(wwo, FALSE);
1281 SetEvent(ev);
1282 break;
1283 case WINE_WM_RESTARTING:
1284 if (wwo->state == WINE_WS_PAUSED)
1286 wwo->state = WINE_WS_PLAYING;
1288 SetEvent(ev);
1289 break;
1290 case WINE_WM_HEADER:
1291 lpWaveHdr = (LPWAVEHDR)param;
1293 /* insert buffer at the end of queue */
1295 LPWAVEHDR* wh;
1296 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1297 *wh = lpWaveHdr;
1299 if (!wwo->lpPlayPtr)
1300 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1301 if (wwo->state == WINE_WS_STOPPED)
1302 wwo->state = WINE_WS_PLAYING;
1303 break;
1304 case WINE_WM_RESETTING:
1305 wodPlayer_Reset(wwo, TRUE);
1306 SetEvent(ev);
1307 break;
1308 case WINE_WM_UPDATE:
1309 wodUpdatePlayedTotal(wwo, NULL);
1310 SetEvent(ev);
1311 break;
1312 case WINE_WM_BREAKLOOP:
1313 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1314 /* ensure exit at end of current loop */
1315 wwo->dwLoops = 1;
1317 SetEvent(ev);
1318 break;
1319 case WINE_WM_CLOSING:
1320 /* sanity check: this should not happen since the device must have been reset before */
1321 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1322 wwo->hThread = 0;
1323 wwo->state = WINE_WS_CLOSED;
1324 SetEvent(ev);
1325 ExitThread(0);
1326 /* shouldn't go here */
1327 default:
1328 FIXME("unknown message %d\n", msg);
1329 break;
1334 /**************************************************************************
1335 * wodPlayer_FeedDSP [internal]
1336 * Feed as much sound data as we can into the DSP and return the number of
1337 * milliseconds before it will be necessary to feed the DSP again.
1339 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1341 audio_buf_info dspspace;
1342 DWORD availInQ;
1344 wodUpdatePlayedTotal(wwo, &dspspace);
1345 availInQ = dspspace.bytes;
1346 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1347 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1349 /* input queue empty and output buffer with less than one fragment to play
1350 * actually some cards do not play the fragment before the last if this one is partially feed
1351 * so we need to test for full the availability of 2 fragments
1353 if (!wwo->lpPlayPtr && wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize &&
1354 !wwo->bNeedPost) {
1355 TRACE("Run out of wavehdr:s...\n");
1356 return INFINITE;
1359 /* no more room... no need to try to feed */
1360 if (dspspace.fragments != 0) {
1361 /* Feed from partial wavehdr */
1362 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1363 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1366 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1367 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1368 do {
1369 TRACE("Setting time to elapse for %p to %lu\n",
1370 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1371 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1372 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1373 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1376 if (wwo->bNeedPost) {
1377 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1378 * if it didn't get one, we give it the other */
1379 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1380 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1381 wwo->bNeedPost = FALSE;
1385 return wodPlayer_DSPWait(wwo);
1389 /**************************************************************************
1390 * wodPlayer [internal]
1392 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1394 WORD uDevID = (DWORD)pmt;
1395 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1396 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1397 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1398 DWORD dwSleepTime;
1400 wwo->state = WINE_WS_STOPPED;
1401 SetEvent(wwo->hStartUpEvent);
1403 for (;;) {
1404 /** Wait for the shortest time before an action is required. If there
1405 * are no pending actions, wait forever for a command.
1407 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1408 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1409 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1410 wodPlayer_ProcessMessages(wwo);
1411 if (wwo->state == WINE_WS_PLAYING) {
1412 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1413 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1414 if (dwNextFeedTime == INFINITE) {
1415 /* FeedDSP ran out of data, but before flushing, */
1416 /* check that a notification didn't give us more */
1417 wodPlayer_ProcessMessages(wwo);
1418 if (!wwo->lpPlayPtr) {
1419 TRACE("flushing\n");
1420 ioctl(wwo->ossdev->fd, SNDCTL_DSP_SYNC, 0);
1421 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1422 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1423 } else {
1424 TRACE("recovering\n");
1425 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1428 } else {
1429 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1434 /**************************************************************************
1435 * wodGetDevCaps [internal]
1437 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1439 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1441 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1443 if (wDevID >= numOutDev) {
1444 TRACE("numOutDev reached !\n");
1445 return MMSYSERR_BADDEVICEID;
1448 memcpy(lpCaps, &WOutDev[wDevID].ossdev->out_caps, min(dwSize, sizeof(*lpCaps)));
1449 return MMSYSERR_NOERROR;
1452 /**************************************************************************
1453 * wodOpen [internal]
1455 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1457 int audio_fragment;
1458 WINE_WAVEOUT* wwo;
1459 audio_buf_info info;
1460 DWORD ret;
1462 TRACE("(%u, %p[cb=%08lx], %08lX);\n", wDevID, lpDesc, lpDesc->dwCallback, dwFlags);
1463 if (lpDesc == NULL) {
1464 WARN("Invalid Parameter !\n");
1465 return MMSYSERR_INVALPARAM;
1467 if (wDevID >= numOutDev) {
1468 TRACE("MAX_WAVOUTDRV reached !\n");
1469 return MMSYSERR_BADDEVICEID;
1472 /* only PCM format is supported so far... */
1473 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1474 lpDesc->lpFormat->nChannels == 0 ||
1475 lpDesc->lpFormat->nSamplesPerSec == 0) {
1476 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1477 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1478 lpDesc->lpFormat->nSamplesPerSec);
1479 return WAVERR_BADFORMAT;
1482 if (dwFlags & WAVE_FORMAT_QUERY) {
1483 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1484 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1485 lpDesc->lpFormat->nSamplesPerSec);
1486 return MMSYSERR_NOERROR;
1489 wwo = &WOutDev[wDevID];
1491 if ((dwFlags & WAVE_DIRECTSOUND) &&
1492 !(wwo->ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
1493 /* not supported, ignore it */
1494 dwFlags &= ~WAVE_DIRECTSOUND;
1496 if (dwFlags & WAVE_DIRECTSOUND) {
1497 if (wwo->ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1498 /* we have realtime DirectSound, fragments just waste our time,
1499 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1500 audio_fragment = 0x0020000B;
1501 else
1502 /* to approximate realtime, we must use small fragments,
1503 * let's try to fragment the above 64KB (256 * 2^8) */
1504 audio_fragment = 0x01000008;
1505 } else {
1506 /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
1507 * thus leading to 46ms per fragment, and a turnaround time of 185ms
1509 /* 16 fragments max, 2^10=1024 bytes per fragment */
1510 audio_fragment = 0x000F000A;
1512 if (wwo->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
1513 /* we want to be able to mmap() the device, which means it must be opened readable,
1514 * otherwise mmap() will fail (at least under Linux) */
1515 ret = OSS_OpenDevice(wwo->ossdev,
1516 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
1517 &audio_fragment,
1518 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
1519 lpDesc->lpFormat->nSamplesPerSec,
1520 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
1521 (lpDesc->lpFormat->wBitsPerSample == 16)
1522 ? AFMT_S16_LE : AFMT_U8);
1523 if ((ret==MMSYSERR_NOERROR) && (dwFlags & WAVE_DIRECTSOUND)) {
1524 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev->sample_rate;
1525 lpDesc->lpFormat->nChannels=(wwo->ossdev->stereo ? 2 : 1);
1526 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev->format == AFMT_U8 ? 8 : 16);
1527 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
1528 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
1529 TRACE("OSS_OpenDevice returned this format: %ldx%dx%d\n",
1530 lpDesc->lpFormat->nSamplesPerSec,
1531 lpDesc->lpFormat->wBitsPerSample,
1532 lpDesc->lpFormat->nChannels);
1534 if (ret != 0) return ret;
1535 wwo->state = WINE_WS_STOPPED;
1537 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1539 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1540 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1542 if (wwo->format.wBitsPerSample == 0) {
1543 WARN("Resetting zeroed wBitsPerSample\n");
1544 wwo->format.wBitsPerSample = 8 *
1545 (wwo->format.wf.nAvgBytesPerSec /
1546 wwo->format.wf.nSamplesPerSec) /
1547 wwo->format.wf.nChannels;
1549 /* Read output space info for future reference */
1550 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
1551 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
1552 OSS_CloseDevice(wwo->ossdev);
1553 wwo->state = WINE_WS_CLOSED;
1554 return MMSYSERR_NOTENABLED;
1557 /* Check that fragsize is correct per our settings above */
1558 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
1559 /* we've tried to set 1K fragments or less, but it didn't work */
1560 ERR("fragment size set failed, size is now %d\n", info.fragsize);
1561 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
1562 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
1565 /* Remember fragsize and total buffer size for future use */
1566 wwo->dwFragmentSize = info.fragsize;
1567 wwo->dwBufferSize = info.fragstotal * info.fragsize;
1568 wwo->dwPlayedTotal = 0;
1569 wwo->dwWrittenTotal = 0;
1570 wwo->bNeedPost = TRUE;
1572 OSS_InitRingMessage(&wwo->msgRing);
1574 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1575 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1576 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1577 CloseHandle(wwo->hStartUpEvent);
1578 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1580 TRACE("fd=%d fragmentSize=%ld\n",
1581 wwo->ossdev->fd, wwo->dwFragmentSize);
1582 if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1583 ERR("Fragment doesn't contain an integral number of data blocks\n");
1585 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1586 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1587 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1588 wwo->format.wf.nBlockAlign);
1590 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1593 /**************************************************************************
1594 * wodClose [internal]
1596 static DWORD wodClose(WORD wDevID)
1598 DWORD ret = MMSYSERR_NOERROR;
1599 WINE_WAVEOUT* wwo;
1601 TRACE("(%u);\n", wDevID);
1603 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1604 WARN("bad device ID !\n");
1605 return MMSYSERR_BADDEVICEID;
1608 wwo = &WOutDev[wDevID];
1609 if (wwo->lpQueuePtr) {
1610 WARN("buffers still playing !\n");
1611 ret = WAVERR_STILLPLAYING;
1612 } else {
1613 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1614 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1616 if (wwo->mapping) {
1617 munmap(wwo->mapping, wwo->maplen);
1618 wwo->mapping = NULL;
1621 OSS_DestroyRingMessage(&wwo->msgRing);
1623 OSS_CloseDevice(wwo->ossdev);
1624 wwo->state = WINE_WS_CLOSED;
1625 wwo->dwFragmentSize = 0;
1626 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1628 return ret;
1631 /**************************************************************************
1632 * wodWrite [internal]
1635 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1637 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1639 /* first, do the sanity checks... */
1640 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1641 WARN("bad dev ID !\n");
1642 return MMSYSERR_BADDEVICEID;
1645 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1646 return WAVERR_UNPREPARED;
1648 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1649 return WAVERR_STILLPLAYING;
1651 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1652 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1653 lpWaveHdr->lpNext = 0;
1655 if ((lpWaveHdr->dwBufferLength & (WOutDev[wDevID].format.wf.nBlockAlign - 1)) != 0)
1657 WARN("WaveHdr length isn't a multiple of the PCM block size: %ld %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].format.wf.nBlockAlign);
1658 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].format.wf.nBlockAlign - 1);
1661 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1663 return MMSYSERR_NOERROR;
1666 /**************************************************************************
1667 * wodPrepare [internal]
1669 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1671 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1673 if (wDevID >= numOutDev) {
1674 WARN("bad device ID !\n");
1675 return MMSYSERR_BADDEVICEID;
1678 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1679 return WAVERR_STILLPLAYING;
1681 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1682 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1683 return MMSYSERR_NOERROR;
1686 /**************************************************************************
1687 * wodUnprepare [internal]
1689 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1691 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1693 if (wDevID >= numOutDev) {
1694 WARN("bad device ID !\n");
1695 return MMSYSERR_BADDEVICEID;
1698 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1699 return WAVERR_STILLPLAYING;
1701 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1702 lpWaveHdr->dwFlags |= WHDR_DONE;
1704 return MMSYSERR_NOERROR;
1707 /**************************************************************************
1708 * wodPause [internal]
1710 static DWORD wodPause(WORD wDevID)
1712 TRACE("(%u);!\n", wDevID);
1714 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1715 WARN("bad device ID !\n");
1716 return MMSYSERR_BADDEVICEID;
1719 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1721 return MMSYSERR_NOERROR;
1724 /**************************************************************************
1725 * wodRestart [internal]
1727 static DWORD wodRestart(WORD wDevID)
1729 TRACE("(%u);\n", wDevID);
1731 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1732 WARN("bad device ID !\n");
1733 return MMSYSERR_BADDEVICEID;
1736 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1738 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1739 /* FIXME: Myst crashes with this ... hmm -MM
1740 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1743 return MMSYSERR_NOERROR;
1746 /**************************************************************************
1747 * wodReset [internal]
1749 static DWORD wodReset(WORD wDevID)
1751 TRACE("(%u);\n", wDevID);
1753 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1754 WARN("bad device ID !\n");
1755 return MMSYSERR_BADDEVICEID;
1758 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1760 return MMSYSERR_NOERROR;
1763 /**************************************************************************
1764 * wodGetPosition [internal]
1766 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1768 int time;
1769 DWORD val;
1770 WINE_WAVEOUT* wwo;
1772 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1774 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1775 WARN("bad device ID !\n");
1776 return MMSYSERR_BADDEVICEID;
1779 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1781 wwo = &WOutDev[wDevID];
1782 #ifdef EXACT_WODPOSITION
1783 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1784 #endif
1785 val = wwo->dwPlayedTotal;
1787 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1788 lpTime->wType, wwo->format.wBitsPerSample,
1789 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1790 wwo->format.wf.nAvgBytesPerSec);
1791 TRACE("dwPlayedTotal=%lu\n", val);
1793 switch (lpTime->wType) {
1794 case TIME_BYTES:
1795 lpTime->u.cb = val;
1796 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1797 break;
1798 case TIME_SAMPLES:
1799 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1800 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1801 break;
1802 case TIME_SMPTE:
1803 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1804 lpTime->u.smpte.hour = time / 108000;
1805 time -= lpTime->u.smpte.hour * 108000;
1806 lpTime->u.smpte.min = time / 1800;
1807 time -= lpTime->u.smpte.min * 1800;
1808 lpTime->u.smpte.sec = time / 30;
1809 time -= lpTime->u.smpte.sec * 30;
1810 lpTime->u.smpte.frame = time;
1811 lpTime->u.smpte.fps = 30;
1812 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1813 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1814 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1815 break;
1816 default:
1817 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1818 lpTime->wType = TIME_MS;
1819 case TIME_MS:
1820 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1821 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1822 break;
1824 return MMSYSERR_NOERROR;
1827 /**************************************************************************
1828 * wodBreakLoop [internal]
1830 static DWORD wodBreakLoop(WORD wDevID)
1832 TRACE("(%u);\n", wDevID);
1834 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
1835 WARN("bad device ID !\n");
1836 return MMSYSERR_BADDEVICEID;
1838 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1839 return MMSYSERR_NOERROR;
1842 /**************************************************************************
1843 * wodGetVolume [internal]
1845 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1847 int mixer;
1848 int volume;
1849 DWORD left, right;
1851 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1853 if (lpdwVol == NULL)
1854 return MMSYSERR_NOTENABLED;
1855 if (wDevID >= numOutDev)
1856 return MMSYSERR_INVALPARAM;
1858 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_RDONLY|O_NDELAY)) < 0) {
1859 WARN("mixer device not available !\n");
1860 return MMSYSERR_NOTENABLED;
1862 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
1863 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)n", WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
1864 return MMSYSERR_NOTENABLED;
1866 close(mixer);
1867 left = LOBYTE(volume);
1868 right = HIBYTE(volume);
1869 TRACE("left=%ld right=%ld !\n", left, right);
1870 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1871 return MMSYSERR_NOERROR;
1874 /**************************************************************************
1875 * wodSetVolume [internal]
1877 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1879 int mixer;
1880 int volume;
1881 DWORD left, right;
1883 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1885 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1886 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1887 volume = left + (right << 8);
1889 if (wDevID >= numOutDev) return MMSYSERR_INVALPARAM;
1891 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_WRONLY|O_NDELAY)) < 0) {
1892 WARN("mixer device not available !\n");
1893 return MMSYSERR_NOTENABLED;
1895 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
1896 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n", WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
1897 return MMSYSERR_NOTENABLED;
1898 } else {
1899 TRACE("volume=%04x\n", (unsigned)volume);
1901 close(mixer);
1902 return MMSYSERR_NOERROR;
1905 /**************************************************************************
1906 * wodMessage (WINEOSS.7)
1908 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1909 DWORD dwParam1, DWORD dwParam2)
1911 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1912 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1914 switch (wMsg) {
1915 case DRVM_INIT:
1916 case DRVM_EXIT:
1917 case DRVM_ENABLE:
1918 case DRVM_DISABLE:
1919 /* FIXME: Pretend this is supported */
1920 return 0;
1921 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1922 case WODM_CLOSE: return wodClose (wDevID);
1923 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1924 case WODM_PAUSE: return wodPause (wDevID);
1925 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1926 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1927 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1928 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1929 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1930 case WODM_GETNUMDEVS: return numOutDev;
1931 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1932 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1933 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1934 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1935 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1936 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1937 case WODM_RESTART: return wodRestart (wDevID);
1938 case WODM_RESET: return wodReset (wDevID);
1940 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1941 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1942 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
1943 default:
1944 FIXME("unknown message %d!\n", wMsg);
1946 return MMSYSERR_NOTSUPPORTED;
1949 /*======================================================================*
1950 * Low level DSOUND implementation *
1951 *======================================================================*/
1953 typedef struct IDsDriverImpl IDsDriverImpl;
1954 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1956 struct IDsDriverImpl
1958 /* IUnknown fields */
1959 ICOM_VFIELD(IDsDriver);
1960 DWORD ref;
1961 /* IDsDriverImpl fields */
1962 UINT wDevID;
1963 IDsDriverBufferImpl*primary;
1966 struct IDsDriverBufferImpl
1968 /* IUnknown fields */
1969 ICOM_VFIELD(IDsDriverBuffer);
1970 DWORD ref;
1971 /* IDsDriverBufferImpl fields */
1972 IDsDriverImpl* drv;
1973 DWORD buflen;
1976 static HRESULT DSDB_MapPrimary(IDsDriverBufferImpl *dsdb)
1978 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1979 TRACE("(%p)\n",dsdb);
1980 if (!wwo->mapping) {
1981 wwo->mapping = mmap(NULL, wwo->maplen, PROT_WRITE, MAP_SHARED,
1982 wwo->ossdev->fd, 0);
1983 if (wwo->mapping == (LPBYTE)-1) {
1984 TRACE("(%p): Could not map sound device for direct access (%s)\n", dsdb, strerror(errno));
1985 return DSERR_GENERIC;
1987 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb, wwo->mapping, wwo->maplen);
1989 /* for some reason, es1371 and sblive! sometimes have junk in here.
1990 * clear it, or we get junk noise */
1991 /* some libc implementations are buggy: their memset reads from the buffer...
1992 * to work around it, we have to zero the block by hand. We don't do the expected:
1993 * memset(wwo->mapping,0, wwo->maplen);
1996 char* p1 = wwo->mapping;
1997 unsigned len = wwo->maplen;
1999 if (len >= 16) /* so we can have at least a 4 long area to store... */
2001 /* the mmap:ed value is (at least) dword aligned
2002 * so, start filling the complete unsigned long:s
2004 int b = len >> 2;
2005 unsigned long* p4 = (unsigned long*)p1;
2007 while (b--) *p4++ = 0;
2008 /* prepare for filling the rest */
2009 len &= 3;
2010 p1 = (unsigned char*)p4;
2012 /* in all cases, fill the remaining bytes */
2013 while (len-- != 0) *p1++ = 0;
2016 return DS_OK;
2019 static HRESULT DSDB_UnmapPrimary(IDsDriverBufferImpl *dsdb)
2021 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
2022 TRACE("(%p)\n",dsdb);
2023 if (wwo->mapping) {
2024 if (munmap(wwo->mapping, wwo->maplen) < 0) {
2025 ERR("(%p): Could not unmap sound device (%s)\n", dsdb, strerror(errno));
2026 return DSERR_GENERIC;
2028 wwo->mapping = NULL;
2029 TRACE("(%p): sound device unmapped\n", dsdb);
2031 return DS_OK;
2034 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
2036 ICOM_THIS(IDsDriverBufferImpl,iface);
2037 TRACE("(%p,%s,%p)\n",iface,debugstr_guid(riid),*ppobj);
2039 if ( IsEqualGUID(riid, &IID_IUnknown) ||
2040 IsEqualGUID(riid, &IID_IDsDriverBuffer) ) {
2041 IDsDriverBuffer_AddRef(iface);
2042 *ppobj = (LPVOID)This;
2043 return DS_OK;
2046 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
2047 FIXME("IDsDriverNotify not implemented\n");
2048 *ppobj = (LPVOID)0;
2049 return E_NOINTERFACE;
2052 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
2053 FIXME("IDsDriverPropertySet not implemented\n");
2054 *ppobj = (LPVOID)0;
2055 return E_NOINTERFACE;
2058 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
2060 *ppobj = 0;
2062 return E_NOINTERFACE;
2065 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
2067 ICOM_THIS(IDsDriverBufferImpl,iface);
2068 TRACE("(%p)\n",This);
2069 This->ref++;
2070 TRACE("ref=%ld\n",This->ref);
2071 return This->ref;
2074 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
2076 ICOM_THIS(IDsDriverBufferImpl,iface);
2077 TRACE("(%p)\n",This);
2078 if (--This->ref) {
2079 TRACE("ref=%ld\n",This->ref);
2080 return This->ref;
2082 if (This == This->drv->primary)
2083 This->drv->primary = NULL;
2084 DSDB_UnmapPrimary(This);
2085 HeapFree(GetProcessHeap(),0,This);
2086 TRACE("ref=0\n");
2087 return 0;
2090 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
2091 LPVOID*ppvAudio1,LPDWORD pdwLen1,
2092 LPVOID*ppvAudio2,LPDWORD pdwLen2,
2093 DWORD dwWritePosition,DWORD dwWriteLen,
2094 DWORD dwFlags)
2096 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2097 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
2098 * and that we don't support secondary buffers, this method will never be called */
2099 TRACE("(%p): stub\n",iface);
2100 return DSERR_UNSUPPORTED;
2103 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
2104 LPVOID pvAudio1,DWORD dwLen1,
2105 LPVOID pvAudio2,DWORD dwLen2)
2107 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2108 TRACE("(%p): stub\n",iface);
2109 return DSERR_UNSUPPORTED;
2112 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
2113 LPWAVEFORMATEX pwfx)
2115 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2117 TRACE("(%p,%p)\n",iface,pwfx);
2118 /* On our request (GetDriverDesc flags), DirectSound has by now used
2119 * waveOutClose/waveOutOpen to set the format...
2120 * unfortunately, this means our mmap() is now gone...
2121 * so we need to somehow signal to our DirectSound implementation
2122 * that it should completely recreate this HW buffer...
2123 * this unexpected error code should do the trick... */
2124 return DSERR_BUFFERLOST;
2127 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
2129 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2130 TRACE("(%p,%ld): stub\n",iface,dwFreq);
2131 return DSERR_UNSUPPORTED;
2134 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
2136 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2137 FIXME("(%p,%p): stub!\n",iface,pVolPan);
2138 return DSERR_UNSUPPORTED;
2141 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
2143 /* ICOM_THIS(IDsDriverImpl,iface); */
2144 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
2145 return DSERR_UNSUPPORTED;
2148 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
2149 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
2151 ICOM_THIS(IDsDriverBufferImpl,iface);
2152 count_info info;
2153 DWORD ptr;
2155 TRACE("(%p)\n",iface);
2156 if (WOutDev[This->drv->wDevID].state == WINE_WS_CLOSED) {
2157 ERR("device not open, but accessing?\n");
2158 return DSERR_UNINITIALIZED;
2160 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
2161 ERR("ioctl(%s, SNDCTL_DSP_GETOPTR) failed (%s)\n", WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2162 return DSERR_GENERIC;
2164 ptr = info.ptr & ~3; /* align the pointer, just in case */
2165 if (lpdwPlay) *lpdwPlay = ptr;
2166 if (lpdwWrite) {
2167 /* add some safety margin (not strictly necessary, but...) */
2168 if (WOutDev[This->drv->wDevID].ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2169 *lpdwWrite = ptr + 32;
2170 else
2171 *lpdwWrite = ptr + WOutDev[This->drv->wDevID].dwFragmentSize;
2172 while (*lpdwWrite > This->buflen)
2173 *lpdwWrite -= This->buflen;
2175 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay?*lpdwPlay:0, lpdwWrite?*lpdwWrite:0);
2176 return DS_OK;
2179 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
2181 ICOM_THIS(IDsDriverBufferImpl,iface);
2182 int enable;
2183 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
2184 WOutDev[This->drv->wDevID].ossdev->bOutputEnabled = TRUE;
2185 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
2186 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2187 if (errno == EINVAL) {
2188 /* Don't give up yet. OSS trigger support is inconsistent. */
2189 if (WOutDev[This->drv->wDevID].ossdev->open_count == 1) {
2190 /* try the opposite input enable */
2191 if (WOutDev[This->drv->wDevID].ossdev->bInputEnabled == FALSE)
2192 WOutDev[This->drv->wDevID].ossdev->bInputEnabled = TRUE;
2193 else
2194 WOutDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
2195 /* try it again */
2196 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
2197 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) >= 0)
2198 return DS_OK;
2201 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2202 WOutDev[This->drv->wDevID].ossdev->bOutputEnabled = FALSE;
2203 return DSERR_GENERIC;
2205 return DS_OK;
2208 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
2210 ICOM_THIS(IDsDriverBufferImpl,iface);
2211 int enable;
2212 TRACE("(%p)\n",iface);
2213 /* no more playing */
2214 WOutDev[This->drv->wDevID].ossdev->bOutputEnabled = FALSE;
2215 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
2216 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2217 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2218 return DSERR_GENERIC;
2220 #if 0
2221 /* the play position must be reset to the beginning of the buffer */
2222 if (ioctl(WOutDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_RESET, 0) < 0) {
2223 ERR("ioctl(%s, SNDCTL_DSP_RESET) failed (%s)\n", WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2224 return DSERR_GENERIC;
2226 #endif
2227 /* Most OSS drivers just can't stop the playback without closing the device...
2228 * so we need to somehow signal to our DirectSound implementation
2229 * that it should completely recreate this HW buffer...
2230 * this unexpected error code should do the trick... */
2231 return DSERR_BUFFERLOST;
2234 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
2236 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2237 IDsDriverBufferImpl_QueryInterface,
2238 IDsDriverBufferImpl_AddRef,
2239 IDsDriverBufferImpl_Release,
2240 IDsDriverBufferImpl_Lock,
2241 IDsDriverBufferImpl_Unlock,
2242 IDsDriverBufferImpl_SetFormat,
2243 IDsDriverBufferImpl_SetFrequency,
2244 IDsDriverBufferImpl_SetVolumePan,
2245 IDsDriverBufferImpl_SetPosition,
2246 IDsDriverBufferImpl_GetPosition,
2247 IDsDriverBufferImpl_Play,
2248 IDsDriverBufferImpl_Stop
2251 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
2253 ICOM_THIS(IDsDriverImpl,iface);
2254 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
2256 if ( IsEqualGUID(riid, &IID_IUnknown) ||
2257 IsEqualGUID(riid, &IID_IDsDriver) ) {
2258 IDsDriver_AddRef(iface);
2259 *ppobj = (LPVOID)This;
2260 return DS_OK;
2263 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
2265 *ppobj = 0;
2267 return E_NOINTERFACE;
2270 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
2272 ICOM_THIS(IDsDriverImpl,iface);
2273 TRACE("(%p)\n",This);
2274 This->ref++;
2275 TRACE("ref=%ld\n",This->ref);
2276 return This->ref;
2279 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
2281 ICOM_THIS(IDsDriverImpl,iface);
2282 TRACE("(%p)\n",This);
2283 if (--This->ref) {
2284 TRACE("ref=%ld\n",This->ref);
2285 return This->ref;
2287 HeapFree(GetProcessHeap(),0,This);
2288 TRACE("ref=0\n");
2289 return 0;
2292 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
2294 ICOM_THIS(IDsDriverImpl,iface);
2295 TRACE("(%p,%p)\n",iface,pDesc);
2297 /* copy version from driver */
2298 memcpy(pDesc, &(WOutDev[This->wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
2300 pDesc->dwFlags |= DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
2301 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
2302 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
2303 pDesc->wVxdId = 0;
2304 pDesc->wReserved = 0;
2305 pDesc->ulDeviceNum = This->wDevID;
2306 pDesc->dwHeapType = DSDHEAP_NOHEAP;
2307 pDesc->pvDirectDrawHeap = NULL;
2308 pDesc->dwMemStartAddress = 0;
2309 pDesc->dwMemEndAddress = 0;
2310 pDesc->dwMemAllocExtra = 0;
2311 pDesc->pvReserved1 = NULL;
2312 pDesc->pvReserved2 = NULL;
2313 return DS_OK;
2316 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
2318 ICOM_THIS(IDsDriverImpl,iface);
2319 int enable;
2320 TRACE("(%p)\n",iface);
2322 /* make sure the card doesn't start playing before we want it to */
2323 WOutDev[This->wDevID].ossdev->bOutputEnabled = FALSE;
2324 enable = getEnables(WOutDev[This->wDevID].ossdev);
2325 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2326 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev[This->wDevID].ossdev->dev_name, strerror(errno));
2327 return DSERR_GENERIC;
2329 return DS_OK;
2332 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
2334 ICOM_THIS(IDsDriverImpl,iface);
2335 TRACE("(%p)\n",iface);
2336 if (This->primary) {
2337 ERR("problem with DirectSound: primary not released\n");
2338 return DSERR_GENERIC;
2340 return DS_OK;
2343 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
2345 ICOM_THIS(IDsDriverImpl,iface);
2346 TRACE("(%p,%p)\n",iface,pCaps);
2347 memcpy(pCaps, &(WOutDev[This->wDevID].ossdev->ds_caps), sizeof(DSDRIVERCAPS));
2348 return DS_OK;
2351 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
2352 LPWAVEFORMATEX pwfx,
2353 DWORD dwFlags, DWORD dwCardAddress,
2354 LPDWORD pdwcbBufferSize,
2355 LPBYTE *ppbBuffer,
2356 LPVOID *ppvObj)
2358 ICOM_THIS(IDsDriverImpl,iface);
2359 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
2360 HRESULT err;
2361 audio_buf_info info;
2362 int enable = 0;
2363 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
2365 /* we only support primary buffers */
2366 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
2367 return DSERR_UNSUPPORTED;
2368 if (This->primary)
2369 return DSERR_ALLOCATED;
2370 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
2371 return DSERR_CONTROLUNAVAIL;
2373 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsDriverBufferImpl));
2374 if (*ippdsdb == NULL)
2375 return DSERR_OUTOFMEMORY;
2376 (*ippdsdb)->lpVtbl = &dsdbvt;
2377 (*ippdsdb)->ref = 1;
2378 (*ippdsdb)->drv = This;
2380 /* check how big the DMA buffer is now */
2381 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2382 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", WOutDev[This->wDevID].ossdev->dev_name, strerror(errno));
2383 HeapFree(GetProcessHeap(),0,*ippdsdb);
2384 *ippdsdb = NULL;
2385 return DSERR_GENERIC;
2387 WOutDev[This->wDevID].maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
2389 /* map the DMA buffer */
2390 err = DSDB_MapPrimary(*ippdsdb);
2391 if (err != DS_OK) {
2392 HeapFree(GetProcessHeap(),0,*ippdsdb);
2393 *ippdsdb = NULL;
2394 return err;
2397 /* primary buffer is ready to go */
2398 *pdwcbBufferSize = WOutDev[This->wDevID].maplen;
2399 *ppbBuffer = WOutDev[This->wDevID].mapping;
2401 /* some drivers need some extra nudging after mapping */
2402 WOutDev[This->wDevID].ossdev->bOutputEnabled = FALSE;
2403 enable = getEnables(WOutDev[This->wDevID].ossdev);
2404 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2405 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev[This->wDevID].ossdev->dev_name, strerror(errno));
2406 return DSERR_GENERIC;
2409 This->primary = *ippdsdb;
2411 return DS_OK;
2414 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
2415 PIDSDRIVERBUFFER pBuffer,
2416 LPVOID *ppvObj)
2418 /* ICOM_THIS(IDsDriverImpl,iface); */
2419 TRACE("(%p,%p): stub\n",iface,pBuffer);
2420 return DSERR_INVALIDCALL;
2423 static ICOM_VTABLE(IDsDriver) dsdvt =
2425 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2426 IDsDriverImpl_QueryInterface,
2427 IDsDriverImpl_AddRef,
2428 IDsDriverImpl_Release,
2429 IDsDriverImpl_GetDriverDesc,
2430 IDsDriverImpl_Open,
2431 IDsDriverImpl_Close,
2432 IDsDriverImpl_GetCaps,
2433 IDsDriverImpl_CreateSoundBuffer,
2434 IDsDriverImpl_DuplicateSoundBuffer
2437 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2439 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
2440 TRACE("(%d,%p)\n",wDevID,drv);
2442 /* the HAL isn't much better than the HEL if we can't do mmap() */
2443 if (!(WOutDev[wDevID].ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
2444 ERR("DirectSound flag not set\n");
2445 MESSAGE("This sound card's driver does not support direct access\n");
2446 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2447 return MMSYSERR_NOTSUPPORTED;
2450 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsDriverImpl));
2451 if (!*idrv)
2452 return MMSYSERR_NOMEM;
2453 (*idrv)->lpVtbl = &dsdvt;
2454 (*idrv)->ref = 1;
2456 (*idrv)->wDevID = wDevID;
2457 (*idrv)->primary = NULL;
2458 return MMSYSERR_NOERROR;
2461 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2463 TRACE("(%d,%p)\n",wDevID,desc);
2464 memcpy(desc, &(WOutDev[wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
2465 return MMSYSERR_NOERROR;
2468 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
2470 TRACE("(%d,%p)\n",wDevID,pGuid);
2471 memcpy(pGuid, &(WOutDev[wDevID].ossdev->ds_guid), sizeof(GUID));
2472 return MMSYSERR_NOERROR;
2475 /*======================================================================*
2476 * Low level WAVE IN implementation *
2477 *======================================================================*/
2479 /**************************************************************************
2480 * widNotifyClient [internal]
2482 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2484 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
2486 switch (wMsg) {
2487 case WIM_OPEN:
2488 case WIM_CLOSE:
2489 case WIM_DATA:
2490 if (wwi->wFlags != DCB_NULL &&
2491 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
2492 (HDRVR)wwi->waveDesc.hWave, wMsg,
2493 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2494 WARN("can't notify client !\n");
2495 return MMSYSERR_ERROR;
2497 break;
2498 default:
2499 FIXME("Unknown callback message %u\n", wMsg);
2500 return MMSYSERR_INVALPARAM;
2502 return MMSYSERR_NOERROR;
2505 /**************************************************************************
2506 * widGetDevCaps [internal]
2508 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
2510 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
2512 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2514 if (wDevID >= numInDev) {
2515 TRACE("numOutDev reached !\n");
2516 return MMSYSERR_BADDEVICEID;
2519 memcpy(lpCaps, &WInDev[wDevID].ossdev->in_caps, min(dwSize, sizeof(*lpCaps)));
2520 return MMSYSERR_NOERROR;
2523 /**************************************************************************
2524 * widRecorder [internal]
2526 static DWORD CALLBACK widRecorder(LPVOID pmt)
2528 WORD uDevID = (DWORD)pmt;
2529 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2530 WAVEHDR* lpWaveHdr;
2531 DWORD dwSleepTime;
2532 DWORD bytesRead;
2533 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
2534 char *pOffset = buffer;
2535 audio_buf_info info;
2536 int xs;
2537 enum win_wm_message msg;
2538 DWORD param;
2539 HANDLE ev;
2540 int enable;
2542 wwi->state = WINE_WS_STOPPED;
2543 wwi->dwTotalRecorded = 0;
2544 wwi->lpQueuePtr = NULL;
2546 SetEvent(wwi->hStartUpEvent);
2548 /* disable input so capture will begin when triggered */
2549 wwi->ossdev->bInputEnabled = FALSE;
2550 enable = getEnables(wwi->ossdev);
2551 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
2552 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2554 /* the soundblaster live needs a micro wake to get its recording started
2555 * (or GETISPACE will have 0 frags all the time)
2557 read(wwi->ossdev->fd, &xs, 4);
2559 /* make sleep time to be # of ms to output a fragment */
2560 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->format.wf.nAvgBytesPerSec;
2561 TRACE("sleeptime=%ld ms\n", dwSleepTime);
2563 for (;;) {
2564 /* wait for dwSleepTime or an event in thread's queue */
2565 /* FIXME: could improve wait time depending on queue state,
2566 * ie, number of queued fragments
2569 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2571 lpWaveHdr = wwi->lpQueuePtr;
2573 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info);
2574 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
2576 /* read all the fragments accumulated so far */
2577 while ((info.fragments > 0) && (wwi->lpQueuePtr))
2579 info.fragments --;
2581 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
2583 /* directly read fragment in wavehdr */
2584 bytesRead = read(wwi->ossdev->fd,
2585 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2586 wwi->dwFragmentSize);
2588 TRACE("bytesRead=%ld (direct)\n", bytesRead);
2589 if (bytesRead != (DWORD) -1)
2591 /* update number of bytes recorded in current buffer and by this device */
2592 lpWaveHdr->dwBytesRecorded += bytesRead;
2593 wwi->dwTotalRecorded += bytesRead;
2595 /* buffer is full. notify client */
2596 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2598 /* must copy the value of next waveHdr, because we have no idea of what
2599 * will be done with the content of lpWaveHdr in callback
2601 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2603 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2604 lpWaveHdr->dwFlags |= WHDR_DONE;
2606 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2607 lpWaveHdr = wwi->lpQueuePtr = lpNext;
2611 else
2613 /* read the fragment in a local buffer */
2614 bytesRead = read(wwi->ossdev->fd, buffer, wwi->dwFragmentSize);
2615 pOffset = buffer;
2617 TRACE("bytesRead=%ld (local)\n", bytesRead);
2619 /* copy data in client buffers */
2620 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2622 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2624 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2625 pOffset,
2626 dwToCopy);
2628 /* update number of bytes recorded in current buffer and by this device */
2629 lpWaveHdr->dwBytesRecorded += dwToCopy;
2630 wwi->dwTotalRecorded += dwToCopy;
2631 bytesRead -= dwToCopy;
2632 pOffset += dwToCopy;
2634 /* client buffer is full. notify client */
2635 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2637 /* must copy the value of next waveHdr, because we have no idea of what
2638 * will be done with the content of lpWaveHdr in callback
2640 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2641 TRACE("lpNext=%p\n", lpNext);
2643 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2644 lpWaveHdr->dwFlags |= WHDR_DONE;
2646 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2648 wwi->lpQueuePtr = lpWaveHdr = lpNext;
2649 if (!lpNext && bytesRead) {
2650 /* before we give up, check for more header messages */
2651 while (OSS_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
2653 if (msg == WINE_WM_HEADER) {
2654 LPWAVEHDR hdr;
2655 OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
2656 hdr = ((LPWAVEHDR)param);
2657 TRACE("msg = %s, hdr = %p, ev = %p\n", wodPlayerCmdString[msg - WM_USER - 1], hdr, ev);
2658 hdr->lpNext = 0;
2659 if (lpWaveHdr == 0) {
2660 /* new head of queue */
2661 wwi->lpQueuePtr = lpWaveHdr = hdr;
2662 } else {
2663 /* insert buffer at the end of queue */
2664 LPWAVEHDR* wh;
2665 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2666 *wh = hdr;
2668 } else
2669 break;
2672 if (lpWaveHdr == 0) {
2673 /* no more buffer to copy data to, but we did read more.
2674 * what hasn't been copied will be dropped
2676 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
2677 wwi->lpQueuePtr = NULL;
2678 break;
2687 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2689 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2691 TRACE("msg=%s param=0x%lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
2692 switch (msg) {
2693 case WINE_WM_PAUSING:
2694 wwi->state = WINE_WS_PAUSED;
2695 /*FIXME("Device should stop recording\n");*/
2696 SetEvent(ev);
2697 break;
2698 case WINE_WM_STARTING:
2699 wwi->state = WINE_WS_PLAYING;
2701 if (wwi->ossdev->bTriggerSupport)
2703 /* start the recording */
2704 wwi->ossdev->bInputEnabled = TRUE;
2705 enable = getEnables(wwi->ossdev);
2706 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2707 wwi->ossdev->bInputEnabled = FALSE;
2708 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2711 else
2713 unsigned char data[4];
2714 /* read 4 bytes to start the recording */
2715 read(wwi->ossdev->fd, data, 4);
2718 SetEvent(ev);
2719 break;
2720 case WINE_WM_HEADER:
2721 lpWaveHdr = (LPWAVEHDR)param;
2722 lpWaveHdr->lpNext = 0;
2724 /* insert buffer at the end of queue */
2726 LPWAVEHDR* wh;
2727 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2728 *wh = lpWaveHdr;
2730 break;
2731 case WINE_WM_STOPPING:
2732 case WINE_WM_RESETTING:
2733 wwi->state = WINE_WS_STOPPED;
2734 /* return all buffers to the app */
2735 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2736 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2737 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2738 lpWaveHdr->dwFlags |= WHDR_DONE;
2740 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2742 wwi->lpQueuePtr = NULL;
2743 SetEvent(ev);
2744 break;
2745 case WINE_WM_CLOSING:
2746 wwi->hThread = 0;
2747 wwi->state = WINE_WS_CLOSED;
2748 SetEvent(ev);
2749 HeapFree(GetProcessHeap(), 0, buffer);
2750 ExitThread(0);
2751 /* shouldn't go here */
2752 default:
2753 FIXME("unknown message %d\n", msg);
2754 break;
2758 ExitThread(0);
2759 /* just for not generating compilation warnings... should never be executed */
2760 return 0;
2764 /**************************************************************************
2765 * widOpen [internal]
2767 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2769 WINE_WAVEIN* wwi;
2770 int fragment_size;
2771 int audio_fragment;
2772 DWORD ret;
2774 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
2775 if (lpDesc == NULL) {
2776 WARN("Invalid Parameter !\n");
2777 return MMSYSERR_INVALPARAM;
2779 if (wDevID >= numInDev) return MMSYSERR_BADDEVICEID;
2781 /* only PCM format is supported so far... */
2782 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
2783 lpDesc->lpFormat->nChannels == 0 ||
2784 lpDesc->lpFormat->nSamplesPerSec == 0) {
2785 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2786 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2787 lpDesc->lpFormat->nSamplesPerSec);
2788 return WAVERR_BADFORMAT;
2791 if (dwFlags & WAVE_FORMAT_QUERY) {
2792 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2793 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2794 lpDesc->lpFormat->nSamplesPerSec);
2795 return MMSYSERR_NOERROR;
2798 wwi = &WInDev[wDevID];
2800 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
2802 if ((dwFlags & WAVE_DIRECTSOUND) &&
2803 !(wwi->ossdev->in_caps_support & WAVECAPS_DIRECTSOUND))
2804 /* not supported, ignore it */
2805 dwFlags &= ~WAVE_DIRECTSOUND;
2807 if (dwFlags & WAVE_DIRECTSOUND) {
2808 TRACE("has DirectSoundCapture driver\n");
2809 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
2810 /* we have realtime DirectSound, fragments just waste our time,
2811 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2812 audio_fragment = 0x0020000B;
2813 else
2814 /* to approximate realtime, we must use small fragments,
2815 * let's try to fragment the above 64KB (256 * 2^8) */
2816 audio_fragment = 0x01000008;
2817 } else {
2818 TRACE("doesn't have DirectSoundCapture driver\n");
2819 /* This is actually hand tuned to work so that my SB Live:
2820 * - does not skip
2821 * - does not buffer too much
2822 * when sending with the Shoutcast winamp plugin
2824 /* 15 fragments max, 2^10 = 1024 bytes per fragment */
2825 audio_fragment = 0x000F000A;
2828 TRACE("using %d %d byte fragments\n", audio_fragment >> 16, 1 << (audio_fragment & 0xffff));
2830 ret = OSS_OpenDevice(wwi->ossdev, O_RDONLY, &audio_fragment,
2832 lpDesc->lpFormat->nSamplesPerSec,
2833 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
2834 (lpDesc->lpFormat->wBitsPerSample == 16)
2835 ? AFMT_S16_LE : AFMT_U8);
2836 if (ret != 0) return ret;
2837 wwi->state = WINE_WS_STOPPED;
2839 if (wwi->lpQueuePtr) {
2840 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2841 wwi->lpQueuePtr = NULL;
2843 wwi->dwTotalRecorded = 0;
2844 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2846 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2847 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
2849 if (wwi->format.wBitsPerSample == 0) {
2850 WARN("Resetting zeroed wBitsPerSample\n");
2851 wwi->format.wBitsPerSample = 8 *
2852 (wwi->format.wf.nAvgBytesPerSec /
2853 wwi->format.wf.nSamplesPerSec) /
2854 wwi->format.wf.nChannels;
2857 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETBLKSIZE, &fragment_size);
2858 if (fragment_size == -1) {
2859 WARN("ioctl(%s, SNDCTL_DSP_GETBLKSIZE) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
2860 OSS_CloseDevice(wwi->ossdev);
2861 wwi->state = WINE_WS_CLOSED;
2862 return MMSYSERR_NOTENABLED;
2864 wwi->dwFragmentSize = fragment_size;
2866 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2867 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
2868 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
2869 wwi->format.wf.nBlockAlign);
2871 OSS_InitRingMessage(&wwi->msgRing);
2873 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
2874 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
2875 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
2876 CloseHandle(wwi->hStartUpEvent);
2877 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
2879 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2882 /**************************************************************************
2883 * widClose [internal]
2885 static DWORD widClose(WORD wDevID)
2887 WINE_WAVEIN* wwi;
2889 TRACE("(%u);\n", wDevID);
2890 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2891 WARN("can't close !\n");
2892 return MMSYSERR_INVALHANDLE;
2895 wwi = &WInDev[wDevID];
2897 if (wwi->lpQueuePtr != NULL) {
2898 WARN("still buffers open !\n");
2899 return WAVERR_STILLPLAYING;
2902 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
2903 OSS_CloseDevice(wwi->ossdev);
2904 wwi->state = WINE_WS_CLOSED;
2905 wwi->dwFragmentSize = 0;
2906 OSS_DestroyRingMessage(&wwi->msgRing);
2907 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
2910 /**************************************************************************
2911 * widAddBuffer [internal]
2913 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2915 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2917 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2918 WARN("can't do it !\n");
2919 return MMSYSERR_INVALHANDLE;
2921 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2922 TRACE("never been prepared !\n");
2923 return WAVERR_UNPREPARED;
2925 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2926 TRACE("header already in use !\n");
2927 return WAVERR_STILLPLAYING;
2930 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2931 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2932 lpWaveHdr->dwBytesRecorded = 0;
2933 lpWaveHdr->lpNext = NULL;
2935 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2936 return MMSYSERR_NOERROR;
2939 /**************************************************************************
2940 * widPrepare [internal]
2942 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2944 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2946 if (wDevID >= numInDev) return MMSYSERR_INVALHANDLE;
2948 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2949 return WAVERR_STILLPLAYING;
2951 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2952 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2953 lpWaveHdr->dwBytesRecorded = 0;
2954 TRACE("header prepared !\n");
2955 return MMSYSERR_NOERROR;
2958 /**************************************************************************
2959 * widUnprepare [internal]
2961 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2963 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2964 if (wDevID >= numInDev) return MMSYSERR_INVALHANDLE;
2966 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2967 return WAVERR_STILLPLAYING;
2969 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
2970 lpWaveHdr->dwFlags |= WHDR_DONE;
2972 return MMSYSERR_NOERROR;
2975 /**************************************************************************
2976 * widStart [internal]
2978 static DWORD widStart(WORD wDevID)
2980 TRACE("(%u);\n", wDevID);
2981 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2982 WARN("can't start recording !\n");
2983 return MMSYSERR_INVALHANDLE;
2986 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
2987 return MMSYSERR_NOERROR;
2990 /**************************************************************************
2991 * widStop [internal]
2993 static DWORD widStop(WORD wDevID)
2995 TRACE("(%u);\n", wDevID);
2996 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
2997 WARN("can't stop !\n");
2998 return MMSYSERR_INVALHANDLE;
3001 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3003 return MMSYSERR_NOERROR;
3006 /**************************************************************************
3007 * widReset [internal]
3009 static DWORD widReset(WORD wDevID)
3011 TRACE("(%u);\n", wDevID);
3012 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3013 WARN("can't reset !\n");
3014 return MMSYSERR_INVALHANDLE;
3016 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3017 return MMSYSERR_NOERROR;
3020 /**************************************************************************
3021 * widGetPosition [internal]
3023 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3025 int time;
3026 WINE_WAVEIN* wwi;
3028 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
3030 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3031 WARN("can't get pos !\n");
3032 return MMSYSERR_INVALHANDLE;
3034 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
3036 wwi = &WInDev[wDevID];
3038 TRACE("wType=%04X !\n", lpTime->wType);
3039 TRACE("wBitsPerSample=%u\n", wwi->format.wBitsPerSample);
3040 TRACE("nSamplesPerSec=%lu\n", wwi->format.wf.nSamplesPerSec);
3041 TRACE("nChannels=%u\n", wwi->format.wf.nChannels);
3042 TRACE("nAvgBytesPerSec=%lu\n", wwi->format.wf.nAvgBytesPerSec);
3043 switch (lpTime->wType) {
3044 case TIME_BYTES:
3045 lpTime->u.cb = wwi->dwTotalRecorded;
3046 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
3047 break;
3048 case TIME_SAMPLES:
3049 lpTime->u.sample = wwi->dwTotalRecorded * 8 /
3050 wwi->format.wBitsPerSample / wwi->format.wf.nChannels;
3051 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
3052 break;
3053 case TIME_SMPTE:
3054 time = wwi->dwTotalRecorded /
3055 (wwi->format.wf.nAvgBytesPerSec / 1000);
3056 lpTime->u.smpte.hour = time / 108000;
3057 time -= lpTime->u.smpte.hour * 108000;
3058 lpTime->u.smpte.min = time / 1800;
3059 time -= lpTime->u.smpte.min * 1800;
3060 lpTime->u.smpte.sec = time / 30;
3061 time -= lpTime->u.smpte.sec * 30;
3062 lpTime->u.smpte.frame = time;
3063 lpTime->u.smpte.fps = 30;
3064 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
3065 lpTime->u.smpte.hour, lpTime->u.smpte.min,
3066 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
3067 break;
3068 case TIME_MS:
3069 lpTime->u.ms = wwi->dwTotalRecorded /
3070 (wwi->format.wf.nAvgBytesPerSec / 1000);
3071 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
3072 break;
3073 default:
3074 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime->wType);
3075 lpTime->wType = TIME_MS;
3077 return MMSYSERR_NOERROR;
3080 /**************************************************************************
3081 * widMessage (WINEOSS.6)
3083 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3084 DWORD dwParam1, DWORD dwParam2)
3086 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
3087 wDevID, wMsg, dwUser, dwParam1, dwParam2);
3089 switch (wMsg) {
3090 case DRVM_INIT:
3091 case DRVM_EXIT:
3092 case DRVM_ENABLE:
3093 case DRVM_DISABLE:
3094 /* FIXME: Pretend this is supported */
3095 return 0;
3096 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3097 case WIDM_CLOSE: return widClose (wDevID);
3098 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3099 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3100 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3101 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
3102 case WIDM_GETNUMDEVS: return numInDev;
3103 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
3104 case WIDM_RESET: return widReset (wDevID);
3105 case WIDM_START: return widStart (wDevID);
3106 case WIDM_STOP: return widStop (wDevID);
3107 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3108 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3109 case DRV_QUERYDSOUNDGUID: return widDsGuid (wDevID, (LPGUID)dwParam1);
3110 default:
3111 FIXME("unknown message %u!\n", wMsg);
3113 return MMSYSERR_NOTSUPPORTED;
3116 /*======================================================================*
3117 * Low level DSOUND property set implementation *
3118 *======================================================================*/
3120 typedef struct IDsDriverPropertySetImpl IDsDriverPropertySetImpl;
3122 struct IDsDriverPropertySetImpl
3124 /* IUnknown fields */
3125 ICOM_VFIELD(IDsDriverPropertySet);
3126 DWORD ref;
3129 static HRESULT WINAPI IDsDriverPropertySetImpl_QueryInterface(
3130 PIDSDRIVERPROPERTYSET iface,
3131 REFIID riid,
3132 LPVOID *ppobj)
3134 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3135 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
3137 if ( IsEqualGUID(riid, &IID_IUnknown) ||
3138 IsEqualGUID(riid, &IID_IDsDriverPropertySet) ) {
3139 IDsDriverPropertySet_AddRef(iface);
3140 *ppobj = (LPVOID)This;
3141 return DS_OK;
3144 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
3146 *ppobj = 0;
3148 return E_NOINTERFACE;
3151 static ULONG WINAPI IDsDriverPropertySetImpl_AddRef(PIDSDRIVERPROPERTYSET iface)
3153 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3154 DWORD ref;
3155 TRACE("(%p) ref was %ld\n", This, This->ref);
3157 ref = InterlockedIncrement(&(This->ref));
3158 return ref;
3161 static ULONG WINAPI IDsDriverPropertySetImpl_Release(PIDSDRIVERPROPERTYSET iface)
3163 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3164 DWORD ref;
3165 TRACE("(%p) ref was %ld\n", This, This->ref);
3167 ref = InterlockedDecrement(&(This->ref));
3168 return ref;
3171 static HRESULT WINAPI IDsDriverPropertySetImpl_Get(
3172 PIDSDRIVERPROPERTYSET iface,
3173 PDSPROPERTY pDsProperty,
3174 LPVOID pPropertyParams,
3175 ULONG cbPropertyParams,
3176 LPVOID pPropertyData,
3177 ULONG cbPropertyData,
3178 PULONG pcbReturnedData )
3180 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3181 FIXME("(%p,%p,%p,%lx,%p,%lx,%p)\n",This,pDsProperty,pPropertyParams,cbPropertyParams,pPropertyData,cbPropertyData,pcbReturnedData);
3182 return DSERR_UNSUPPORTED;
3185 static HRESULT WINAPI IDsDriverPropertySetImpl_Set(
3186 PIDSDRIVERPROPERTYSET iface,
3187 PDSPROPERTY pDsProperty,
3188 LPVOID pPropertyParams,
3189 ULONG cbPropertyParams,
3190 LPVOID pPropertyData,
3191 ULONG cbPropertyData )
3193 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3194 FIXME("(%p,%p,%p,%lx,%p,%lx)\n",This,pDsProperty,pPropertyParams,cbPropertyParams,pPropertyData,cbPropertyData);
3195 return DSERR_UNSUPPORTED;
3198 static HRESULT WINAPI IDsDriverPropertySetImpl_QuerySupport(
3199 PIDSDRIVERPROPERTYSET iface,
3200 REFGUID PropertySetId,
3201 ULONG PropertyId,
3202 PULONG pSupport )
3204 ICOM_THIS(IDsDriverPropertySetImpl,iface);
3205 FIXME("(%p,%s,%lx,%p)\n",This,debugstr_guid(PropertySetId),PropertyId,pSupport);
3206 return DSERR_UNSUPPORTED;
3209 ICOM_VTABLE(IDsDriverPropertySet) dsdpsvt =
3211 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3212 IDsDriverPropertySetImpl_QueryInterface,
3213 IDsDriverPropertySetImpl_AddRef,
3214 IDsDriverPropertySetImpl_Release,
3215 IDsDriverPropertySetImpl_Get,
3216 IDsDriverPropertySetImpl_Set,
3217 IDsDriverPropertySetImpl_QuerySupport,
3220 /*======================================================================*
3221 * Low level DSOUND notify implementation *
3222 *======================================================================*/
3224 typedef struct IDsDriverNotifyImpl IDsDriverNotifyImpl;
3226 struct IDsDriverNotifyImpl
3228 /* IUnknown fields */
3229 ICOM_VFIELD(IDsDriverNotify);
3230 DWORD ref;
3231 /* IDsDriverNotifyImpl fields */
3232 LPDSBPOSITIONNOTIFY notifies;
3233 int nrofnotifies;
3236 static HRESULT WINAPI IDsDriverNotifyImpl_QueryInterface(
3237 PIDSDRIVERNOTIFY iface,
3238 REFIID riid,
3239 LPVOID *ppobj)
3241 ICOM_THIS(IDsDriverNotifyImpl,iface);
3242 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
3244 if ( IsEqualGUID(riid, &IID_IUnknown) ||
3245 IsEqualGUID(riid, &IID_IDsDriverNotify) ) {
3246 IDsDriverNotify_AddRef(iface);
3247 *ppobj = This;
3248 return DS_OK;
3251 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
3253 *ppobj = 0;
3255 return E_NOINTERFACE;
3258 static ULONG WINAPI IDsDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface)
3260 ICOM_THIS(IDsDriverNotifyImpl,iface);
3261 DWORD ref;
3262 TRACE("(%p) ref was %ld\n", This, This->ref);
3264 ref = InterlockedIncrement(&(This->ref));
3265 return ref;
3268 static ULONG WINAPI IDsDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface)
3270 ICOM_THIS(IDsDriverNotifyImpl,iface);
3271 DWORD ref;
3272 TRACE("(%p) ref was %ld\n", This, This->ref);
3274 ref = InterlockedDecrement(&(This->ref));
3275 /* FIXME: A notification should be a part of a buffer rather than pointed
3276 * to from a buffer. Hence the -1 ref count */
3277 if (ref == -1) {
3278 if (This->notifies != NULL)
3279 HeapFree(GetProcessHeap(), 0, This->notifies);
3281 HeapFree(GetProcessHeap(),0,This);
3282 return 0;
3285 return ref;
3288 static HRESULT WINAPI IDsDriverNotifyImpl_SetNotificationPositions(
3289 PIDSDRIVERNOTIFY iface,
3290 DWORD howmuch,
3291 LPCDSBPOSITIONNOTIFY notify)
3293 ICOM_THIS(IDsDriverNotifyImpl,iface);
3294 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
3296 if (!notify) {
3297 WARN("invalid parameter\n");
3298 return DSERR_INVALIDPARAM;
3301 if (TRACE_ON(wave)) {
3302 int i;
3303 for (i=0;i<howmuch;i++)
3304 TRACE("notify at %ld to 0x%08lx\n",
3305 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
3308 /* Make an internal copy of the caller-supplied array.
3309 * Replace the existing copy if one is already present. */
3310 This->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3311 This->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
3312 memcpy(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
3313 This->nrofnotifies = howmuch;
3315 return S_OK;
3318 ICOM_VTABLE(IDsDriverNotify) dsdnvt =
3320 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3321 IDsDriverNotifyImpl_QueryInterface,
3322 IDsDriverNotifyImpl_AddRef,
3323 IDsDriverNotifyImpl_Release,
3324 IDsDriverNotifyImpl_SetNotificationPositions,
3327 /*======================================================================*
3328 * Low level DSOUND capture implementation *
3329 *======================================================================*/
3331 typedef struct IDsCaptureDriverImpl IDsCaptureDriverImpl;
3332 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl;
3334 struct IDsCaptureDriverImpl
3336 /* IUnknown fields */
3337 ICOM_VFIELD(IDsCaptureDriver);
3338 DWORD ref;
3339 /* IDsCaptureDriverImpl fields */
3340 UINT wDevID;
3341 IDsCaptureDriverBufferImpl* capture_buffer;
3344 struct IDsCaptureDriverBufferImpl
3346 /* IUnknown fields */
3347 ICOM_VFIELD(IDsCaptureDriverBuffer);
3348 DWORD ref;
3349 /* IDsCaptureDriverBufferImpl fields */
3350 IDsCaptureDriverImpl* drv;
3351 DWORD buflen;
3353 /* IDsDriverNotifyImpl fields */
3354 IDsDriverNotifyImpl* notify;
3355 int notify_index;
3357 /* IDsDriverPropertySetImpl fields */
3358 IDsDriverPropertySetImpl* property_set;
3361 static HRESULT DSDB_MapCapture(IDsCaptureDriverBufferImpl *dscdb)
3363 WINE_WAVEIN *wwi = &(WInDev[dscdb->drv->wDevID]);
3364 if (!wwi->mapping) {
3365 wwi->mapping = mmap(NULL, wwi->maplen, PROT_WRITE, MAP_SHARED,
3366 wwi->ossdev->fd, 0);
3367 if (wwi->mapping == (LPBYTE)-1) {
3368 TRACE("(%p): Could not map sound device for direct access (%s)\n", dscdb, strerror(errno));
3369 return DSERR_GENERIC;
3371 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dscdb, wwi->mapping, wwi->maplen);
3373 /* for some reason, es1371 and sblive! sometimes have junk in here.
3374 * clear it, or we get junk noise */
3375 /* some libc implementations are buggy: their memset reads from the buffer...
3376 * to work around it, we have to zero the block by hand. We don't do the expected:
3377 * memset(wwo->mapping,0, wwo->maplen);
3380 char* p1 = wwi->mapping;
3381 unsigned len = wwi->maplen;
3383 if (len >= 16) /* so we can have at least a 4 long area to store... */
3385 /* the mmap:ed value is (at least) dword aligned
3386 * so, start filling the complete unsigned long:s
3388 int b = len >> 2;
3389 unsigned long* p4 = (unsigned long*)p1;
3391 while (b--) *p4++ = 0;
3392 /* prepare for filling the rest */
3393 len &= 3;
3394 p1 = (unsigned char*)p4;
3396 /* in all cases, fill the remaining bytes */
3397 while (len-- != 0) *p1++ = 0;
3400 return DS_OK;
3403 static HRESULT DSDB_UnmapCapture(IDsCaptureDriverBufferImpl *dscdb)
3405 WINE_WAVEIN *wwi = &(WInDev[dscdb->drv->wDevID]);
3406 if (wwi->mapping) {
3407 if (munmap(wwi->mapping, wwi->maplen) < 0) {
3408 ERR("(%p): Could not unmap sound device (%s)\n", dscdb, strerror(errno));
3409 return DSERR_GENERIC;
3411 wwi->mapping = NULL;
3412 TRACE("(%p): sound device unmapped\n", dscdb);
3414 return DS_OK;
3417 static HRESULT WINAPI IDsCaptureDriverBufferImpl_QueryInterface(PIDSCDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
3419 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3420 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
3422 if ( IsEqualGUID(riid, &IID_IUnknown) ||
3423 IsEqualGUID(riid, &IID_IDsCaptureDriverBuffer) ) {
3424 IDsCaptureDriverBuffer_AddRef(iface);
3425 *ppobj = (LPVOID)This;
3426 return DS_OK;
3429 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
3430 if (!This->notify) {
3431 This->notify = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->notify));
3432 if (This->notify) {
3433 This->notify->ref = 0; /* release when ref = -1 */
3434 This->notify->lpVtbl = &dsdnvt;
3437 if (This->notify) {
3438 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
3439 *ppobj = (LPVOID)This->notify;
3440 return DS_OK;
3442 *ppobj = 0;
3443 return E_FAIL;
3446 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
3447 if (!This->property_set) {
3448 This->property_set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This->property_set));
3449 if (This->property_set) {
3450 This->property_set->ref = 0; /* release when ref = -1 */
3451 This->property_set->lpVtbl = &dsdpsvt;
3454 if (This->property_set) {
3455 IDsDriverPropertySet_AddRef((PIDSDRIVERPROPERTYSET)This->property_set);
3456 *ppobj = (LPVOID)This->property_set;
3457 return DS_OK;
3459 *ppobj = 0;
3460 return E_FAIL;
3463 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
3465 *ppobj = 0;
3467 return DSERR_UNSUPPORTED;
3470 static ULONG WINAPI IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface)
3472 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3473 This->ref++;
3474 return This->ref;
3477 static ULONG WINAPI IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface)
3479 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3480 if (--This->ref)
3481 return This->ref;
3482 DSDB_UnmapCapture(This);
3483 if (This->notify)
3484 IDsDriverNotify_Release((PIDSDRIVERNOTIFY)This->notify);
3485 if (This->property_set)
3486 IDsDriverPropertySet_Release((PIDSDRIVERPROPERTYSET)This->property_set);
3487 HeapFree(GetProcessHeap(),0,This);
3488 return 0;
3491 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Lock(PIDSCDRIVERBUFFER iface,
3492 LPVOID*ppvAudio1,LPDWORD pdwLen1,
3493 LPVOID*ppvAudio2,LPDWORD pdwLen2,
3494 DWORD dwWritePosition,DWORD dwWriteLen,
3495 DWORD dwFlags)
3497 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3498 FIXME("(%p,%p,%p,%p,%p,%ld,%ld,0x%08lx): stub!\n",This,ppvAudio1,pdwLen1,ppvAudio2,pdwLen2,
3499 dwWritePosition,dwWriteLen,dwFlags);
3500 return DS_OK;
3503 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Unlock(PIDSCDRIVERBUFFER iface,
3504 LPVOID pvAudio1,DWORD dwLen1,
3505 LPVOID pvAudio2,DWORD dwLen2)
3507 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3508 FIXME("(%p,%p,%ld,%p,%ld): stub!\n",This,pvAudio1,dwLen1,pvAudio2,dwLen2);
3509 return DS_OK;
3512 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetPosition(PIDSCDRIVERBUFFER iface,
3513 LPDWORD lpdwCapture,
3514 LPDWORD lpdwRead)
3516 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3517 count_info info;
3518 DWORD ptr;
3519 TRACE("(%p,%p,%p)\n",This,lpdwCapture,lpdwRead);
3521 if (WInDev[This->drv->wDevID].state == WINE_WS_CLOSED) {
3522 ERR("device not open, but accessing?\n");
3523 return DSERR_UNINITIALIZED;
3525 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
3526 ERR("ioctl(%s, SNDCTL_DSP_GETIPTR) failed (%s)\n", WInDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
3527 return DSERR_GENERIC;
3529 ptr = info.ptr & ~3; /* align the pointer, just in case */
3530 if (lpdwCapture) *lpdwCapture = ptr;
3531 if (lpdwRead) {
3532 /* add some safety margin (not strictly necessary, but...) */
3533 if (WInDev[This->drv->wDevID].ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
3534 *lpdwRead = ptr + 32;
3535 else
3536 *lpdwRead = ptr + WInDev[This->drv->wDevID].dwFragmentSize;
3537 while (*lpdwRead > This->buflen)
3538 *lpdwRead -= This->buflen;
3540 TRACE("capturepos=%ld, readpos=%ld\n", lpdwCapture?*lpdwCapture:0, lpdwRead?*lpdwRead:0);
3541 return DS_OK;
3544 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetStatus(PIDSCDRIVERBUFFER iface, LPDWORD lpdwStatus)
3546 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3547 FIXME("(%p,%p): stub!\n",This,lpdwStatus);
3548 return DSERR_UNSUPPORTED;
3551 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface, DWORD dwFlags)
3553 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3554 int enable;
3555 TRACE("(%p,%lx)\n",This,dwFlags);
3556 WInDev[This->drv->wDevID].ossdev->bInputEnabled = TRUE;
3557 enable = getEnables(WInDev[This->drv->wDevID].ossdev);
3558 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3559 if (errno == EINVAL) {
3560 /* Don't give up yet. OSS trigger support is inconsistent. */
3561 if (WInDev[This->drv->wDevID].ossdev->open_count == 1) {
3562 /* try the opposite output enable */
3563 if (WInDev[This->drv->wDevID].ossdev->bOutputEnabled == FALSE)
3564 WInDev[This->drv->wDevID].ossdev->bOutputEnabled = TRUE;
3565 else
3566 WInDev[This->drv->wDevID].ossdev->bOutputEnabled = FALSE;
3567 /* try it again */
3568 enable = getEnables(WInDev[This->drv->wDevID].ossdev);
3569 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) >= 0)
3570 return DS_OK;
3573 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
3574 WInDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
3575 return DSERR_GENERIC;
3577 return DS_OK;
3580 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface)
3582 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3583 int enable;
3584 TRACE("(%p)\n",This);
3585 /* no more captureing */
3586 WInDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
3587 enable = getEnables(WInDev[This->drv->wDevID].ossdev);
3588 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3589 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
3590 return DSERR_GENERIC;
3593 /* Most OSS drivers just can't stop capturing without closing the device...
3594 * so we need to somehow signal to our DirectSound implementation
3595 * that it should completely recreate this HW buffer...
3596 * this unexpected error code should do the trick... */
3597 return DSERR_BUFFERLOST;
3600 static HRESULT WINAPI IDsCaptureDriverBufferImpl_SetFormat(PIDSCDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
3602 ICOM_THIS(IDsCaptureDriverBufferImpl,iface);
3603 FIXME("(%p): stub!\n",This);
3604 return DSERR_UNSUPPORTED;
3607 static ICOM_VTABLE(IDsCaptureDriverBuffer) dscdbvt =
3609 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3610 IDsCaptureDriverBufferImpl_QueryInterface,
3611 IDsCaptureDriverBufferImpl_AddRef,
3612 IDsCaptureDriverBufferImpl_Release,
3613 IDsCaptureDriverBufferImpl_Lock,
3614 IDsCaptureDriverBufferImpl_Unlock,
3615 IDsCaptureDriverBufferImpl_SetFormat,
3616 IDsCaptureDriverBufferImpl_GetPosition,
3617 IDsCaptureDriverBufferImpl_GetStatus,
3618 IDsCaptureDriverBufferImpl_Start,
3619 IDsCaptureDriverBufferImpl_Stop
3622 static HRESULT WINAPI IDsCaptureDriverImpl_QueryInterface(PIDSCDRIVER iface, REFIID riid, LPVOID *ppobj)
3624 ICOM_THIS(IDsCaptureDriverImpl,iface);
3625 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
3627 if ( IsEqualGUID(riid, &IID_IUnknown) ||
3628 IsEqualGUID(riid, &IID_IDsCaptureDriver) ) {
3629 IDsCaptureDriver_AddRef(iface);
3630 *ppobj = (LPVOID)This;
3631 return DS_OK;
3634 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
3636 *ppobj = 0;
3638 return E_NOINTERFACE;
3641 static ULONG WINAPI IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface)
3643 ICOM_THIS(IDsCaptureDriverImpl,iface);
3644 TRACE("(%p)\n",This);
3645 This->ref++;
3646 TRACE("ref=%ld\n",This->ref);
3647 return This->ref;
3650 static ULONG WINAPI IDsCaptureDriverImpl_Release(PIDSCDRIVER iface)
3652 ICOM_THIS(IDsCaptureDriverImpl,iface);
3653 TRACE("(%p)\n",This);
3654 if (--This->ref) {
3655 TRACE("ref=%ld\n",This->ref);
3656 return This->ref;
3658 HeapFree(GetProcessHeap(),0,This);
3659 TRACE("ref=0\n");
3660 return 0;
3663 static HRESULT WINAPI IDsCaptureDriverImpl_GetDriverDesc(PIDSCDRIVER iface, PDSDRIVERDESC pDesc)
3665 ICOM_THIS(IDsCaptureDriverImpl,iface);
3666 TRACE("(%p,%p)\n",This,pDesc);
3668 if (!pDesc) {
3669 TRACE("invalid parameter\n");
3670 return DSERR_INVALIDPARAM;
3673 /* copy version from driver */
3674 memcpy(pDesc, &(WInDev[This->wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
3676 pDesc->dwFlags |= DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
3677 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK |
3678 DSDDESC_DONTNEEDSECONDARYLOCK;
3679 pDesc->dnDevNode = WInDev[This->wDevID].waveDesc.dnDevNode;
3680 pDesc->wVxdId = 0;
3681 pDesc->wReserved = 0;
3682 pDesc->ulDeviceNum = This->wDevID;
3683 pDesc->dwHeapType = DSDHEAP_NOHEAP;
3684 pDesc->pvDirectDrawHeap = NULL;
3685 pDesc->dwMemStartAddress = 0;
3686 pDesc->dwMemEndAddress = 0;
3687 pDesc->dwMemAllocExtra = 0;
3688 pDesc->pvReserved1 = NULL;
3689 pDesc->pvReserved2 = NULL;
3690 return DS_OK;
3693 static HRESULT WINAPI IDsCaptureDriverImpl_Open(PIDSCDRIVER iface)
3695 ICOM_THIS(IDsCaptureDriverImpl,iface);
3696 TRACE("(%p)\n",This);
3697 return DS_OK;
3700 static HRESULT WINAPI IDsCaptureDriverImpl_Close(PIDSCDRIVER iface)
3702 ICOM_THIS(IDsCaptureDriverImpl,iface);
3703 TRACE("(%p)\n",This);
3704 if (This->capture_buffer) {
3705 ERR("problem with DirectSound: capture buffer not released\n");
3706 return DSERR_GENERIC;
3708 return DS_OK;
3711 static HRESULT WINAPI IDsCaptureDriverImpl_GetCaps(PIDSCDRIVER iface, PDSCDRIVERCAPS pCaps)
3713 ICOM_THIS(IDsCaptureDriverImpl,iface);
3714 TRACE("(%p,%p)\n",This,pCaps);
3715 memcpy(pCaps, &(WInDev[This->wDevID].ossdev->dsc_caps), sizeof(DSCDRIVERCAPS));
3716 return DS_OK;
3719 static HRESULT WINAPI IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface,
3720 LPWAVEFORMATEX pwfx,
3721 DWORD dwFlags,
3722 DWORD dwCardAddress,
3723 LPDWORD pdwcbBufferSize,
3724 LPBYTE *ppbBuffer,
3725 LPVOID *ppvObj)
3727 ICOM_THIS(IDsCaptureDriverImpl,iface);
3728 IDsCaptureDriverBufferImpl** ippdscdb = (IDsCaptureDriverBufferImpl**)ppvObj;
3729 HRESULT err;
3730 audio_buf_info info;
3731 int enable;
3732 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",This,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
3734 if (This->capture_buffer) {
3735 TRACE("already allocated\n");
3736 return DSERR_ALLOCATED;
3739 *ippdscdb = (IDsCaptureDriverBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverBufferImpl));
3740 if (*ippdscdb == NULL) {
3741 TRACE("out of memory\n");
3742 return DSERR_OUTOFMEMORY;
3745 (*ippdscdb)->lpVtbl = &dscdbvt;
3746 (*ippdscdb)->ref = 1;
3747 (*ippdscdb)->drv = This;
3748 (*ippdscdb)->notify = 0;
3749 (*ippdscdb)->notify_index = 0;
3750 (*ippdscdb)->property_set = 0;
3752 if (WInDev[This->wDevID].state == WINE_WS_CLOSED) {
3753 WAVEOPENDESC desc;
3754 desc.hWave = 0;
3755 desc.lpFormat = pwfx;
3756 desc.dwCallback = 0;
3757 desc.dwInstance = 0;
3758 desc.uMappedDeviceID = 0;
3759 desc.dnDevNode = 0;
3760 err = widOpen(This->wDevID, &desc, dwFlags | WAVE_DIRECTSOUND);
3761 if (err != MMSYSERR_NOERROR) {
3762 TRACE("widOpen failed\n");
3763 return err;
3767 /* check how big the DMA buffer is now */
3768 if (ioctl(WInDev[This->wDevID].ossdev->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
3769 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", WInDev[This->wDevID].ossdev->dev_name, strerror(errno));
3770 HeapFree(GetProcessHeap(),0,*ippdscdb);
3771 *ippdscdb = NULL;
3772 return DSERR_GENERIC;
3774 WInDev[This->wDevID].maplen = (*ippdscdb)->buflen = info.fragstotal * info.fragsize;
3776 /* map the DMA buffer */
3777 err = DSDB_MapCapture(*ippdscdb);
3778 if (err != DS_OK) {
3779 HeapFree(GetProcessHeap(),0,*ippdscdb);
3780 *ippdscdb = NULL;
3781 return err;
3784 /* capture buffer is ready to go */
3785 *pdwcbBufferSize = WInDev[This->wDevID].maplen;
3786 *ppbBuffer = WInDev[This->wDevID].mapping;
3788 /* some drivers need some extra nudging after mapping */
3789 WInDev[This->wDevID].ossdev->bInputEnabled = FALSE;
3790 enable = getEnables(WInDev[This->wDevID].ossdev);
3791 if (ioctl(WInDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3792 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev[This->wDevID].ossdev->dev_name, strerror(errno));
3793 return DSERR_GENERIC;
3796 This->capture_buffer = *ippdscdb;
3798 return DS_OK;
3801 static ICOM_VTABLE(IDsCaptureDriver) dscdvt =
3803 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3804 IDsCaptureDriverImpl_QueryInterface,
3805 IDsCaptureDriverImpl_AddRef,
3806 IDsCaptureDriverImpl_Release,
3807 IDsCaptureDriverImpl_GetDriverDesc,
3808 IDsCaptureDriverImpl_Open,
3809 IDsCaptureDriverImpl_Close,
3810 IDsCaptureDriverImpl_GetCaps,
3811 IDsCaptureDriverImpl_CreateCaptureBuffer
3814 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
3816 IDsCaptureDriverImpl** idrv = (IDsCaptureDriverImpl**)drv;
3817 TRACE("(%d,%p)\n",wDevID,drv);
3819 /* the HAL isn't much better than the HEL if we can't do mmap() */
3820 if (!(WInDev[wDevID].ossdev->in_caps_support & WAVECAPS_DIRECTSOUND)) {
3821 ERR("DirectSoundCapture flag not set\n");
3822 MESSAGE("This sound card's driver does not support direct access\n");
3823 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
3824 return MMSYSERR_NOTSUPPORTED;
3827 *idrv = (IDsCaptureDriverImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverImpl));
3828 if (!*idrv)
3829 return MMSYSERR_NOMEM;
3830 (*idrv)->lpVtbl = &dscdvt;
3831 (*idrv)->ref = 1;
3833 (*idrv)->wDevID = wDevID;
3834 (*idrv)->capture_buffer = NULL;
3835 return MMSYSERR_NOERROR;
3838 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
3840 memcpy(desc, &(WInDev[wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
3841 return MMSYSERR_NOERROR;
3844 static DWORD widDsGuid(UINT wDevID, LPGUID pGuid)
3846 TRACE("(%d,%p)\n",wDevID,pGuid);
3848 memcpy(pGuid, &(WInDev[wDevID].ossdev->dsc_guid), sizeof(GUID));
3850 return MMSYSERR_NOERROR;
3853 #else /* !HAVE_OSS */
3855 /**************************************************************************
3856 * wodMessage (WINEOSS.7)
3858 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3859 DWORD dwParam1, DWORD dwParam2)
3861 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3862 return MMSYSERR_NOTENABLED;
3865 /**************************************************************************
3866 * widMessage (WINEOSS.6)
3868 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3869 DWORD dwParam1, DWORD dwParam2)
3871 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3872 return MMSYSERR_NOTENABLED;
3875 #endif /* HAVE_OSS */