Removed some more uses of the non-standard ICOM_THIS macro.
[wine/dcerpc.git] / dlls / winmm / wineoss / audio.c
blob35d05813faa3023f17546645e72ec08c057994c5
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
38 #define EXACT_WIDPOSITION
40 #include "config.h"
41 #include "wine/port.h"
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <string.h>
47 #ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50 #include <errno.h>
51 #include <fcntl.h>
52 #ifdef HAVE_SYS_IOCTL_H
53 # include <sys/ioctl.h>
54 #endif
55 #ifdef HAVE_SYS_MMAN_H
56 # include <sys/mman.h>
57 #endif
58 #ifdef HAVE_SYS_POLL_H
59 # include <sys/poll.h>
60 #endif
62 #include "windef.h"
63 #include "winbase.h"
64 #include "wingdi.h"
65 #include "winerror.h"
66 #include "wine/winuser16.h"
67 #include "mmddk.h"
68 #include "mmreg.h"
69 #include "dsound.h"
70 #include "dsdriver.h"
71 #include "ks.h"
72 #include "ksguid.h"
73 #include "ksmedia.h"
74 #include "oss.h"
75 #include "wine/debug.h"
77 WINE_DEFAULT_DEBUG_CHANNEL(wave);
79 /* Allow 1% deviation for sample rates (some ES137x cards) */
80 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
82 #ifdef HAVE_OSS
84 #define MAX_WAVEDRV (6)
86 /* state diagram for waveOut writing:
88 * +---------+-------------+---------------+---------------------------------+
89 * | state | function | event | new state |
90 * +---------+-------------+---------------+---------------------------------+
91 * | | open() | | STOPPED |
92 * | PAUSED | write() | | PAUSED |
93 * | STOPPED | write() | <thrd create> | PLAYING |
94 * | PLAYING | write() | HEADER | PLAYING |
95 * | (other) | write() | <error> | |
96 * | (any) | pause() | PAUSING | PAUSED |
97 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
98 * | (any) | reset() | RESETTING | STOPPED |
99 * | (any) | close() | CLOSING | CLOSED |
100 * +---------+-------------+---------------+---------------------------------+
103 /* states of the playing device */
104 #define WINE_WS_PLAYING 0
105 #define WINE_WS_PAUSED 1
106 #define WINE_WS_STOPPED 2
107 #define WINE_WS_CLOSED 3
109 /* events to be send to device */
110 enum win_wm_message {
111 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
112 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
115 #ifdef USE_PIPE_SYNC
116 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
117 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
118 #define RESET_OMR(omr) do { } while (0)
119 #define WAIT_OMR(omr, sleep) \
120 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
121 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
122 #else
123 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
124 #define CLEAR_OMR(omr) do { } while (0)
125 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
126 #define WAIT_OMR(omr, sleep) \
127 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
128 #endif
130 typedef struct {
131 enum win_wm_message msg; /* message identifier */
132 DWORD param; /* parameter for this message */
133 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
134 } OSS_MSG;
136 /* implement an in-process message ring for better performance
137 * (compared to passing thru the server)
138 * this ring will be used by the input (resp output) record (resp playback) routine
140 #define OSS_RING_BUFFER_INCREMENT 64
141 typedef struct {
142 int ring_buffer_size;
143 OSS_MSG * messages;
144 int msg_tosave;
145 int msg_toget;
146 #ifdef USE_PIPE_SYNC
147 int msg_pipe[2];
148 #else
149 HANDLE msg_event;
150 #endif
151 CRITICAL_SECTION msg_crst;
152 } OSS_MSG_RING;
154 typedef struct tagOSS_DEVICE {
155 char* dev_name;
156 char* mixer_name;
157 char* interface_name;
158 unsigned open_count;
159 WAVEOUTCAPSA out_caps;
160 WAVEOUTCAPSA duplex_out_caps;
161 WAVEINCAPSA in_caps;
162 DWORD in_caps_support;
163 unsigned open_access;
164 int fd;
165 DWORD owner_tid;
166 int sample_rate;
167 int stereo;
168 int format;
169 unsigned audio_fragment;
170 BOOL full_duplex;
171 BOOL bTriggerSupport;
172 BOOL bOutputEnabled;
173 BOOL bInputEnabled;
174 DSDRIVERDESC ds_desc;
175 DSDRIVERCAPS ds_caps;
176 DSCDRIVERCAPS dsc_caps;
177 } OSS_DEVICE;
179 static OSS_DEVICE OSS_Devices[MAX_WAVEDRV];
181 typedef struct {
182 OSS_DEVICE* ossdev;
183 volatile int state; /* one of the WINE_WS_ manifest constants */
184 WAVEOPENDESC waveDesc;
185 WORD wFlags;
186 WAVEFORMATPCMEX waveFormat;
187 DWORD volume;
189 /* OSS information */
190 DWORD dwFragmentSize; /* size of OSS buffer fragment */
191 DWORD dwBufferSize; /* size of whole OSS buffer in bytes */
192 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
193 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
194 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
196 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
197 DWORD dwLoops; /* private copy of loop counter */
199 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
200 DWORD dwWrittenTotal; /* number of bytes written to OSS buffer since opening */
201 BOOL bNeedPost; /* whether audio still needs to be physically started */
203 /* synchronization stuff */
204 HANDLE hStartUpEvent;
205 HANDLE hThread;
206 DWORD dwThreadID;
207 OSS_MSG_RING msgRing;
208 } WINE_WAVEOUT;
210 typedef struct {
211 OSS_DEVICE* ossdev;
212 volatile int state;
213 DWORD dwFragmentSize; /* OpenSound '/dev/dsp' give us that size */
214 WAVEOPENDESC waveDesc;
215 WORD wFlags;
216 WAVEFORMATPCMEX waveFormat;
217 LPWAVEHDR lpQueuePtr;
218 DWORD dwTotalRecorded;
219 DWORD dwTotalRead;
221 /* synchronization stuff */
222 HANDLE hThread;
223 DWORD dwThreadID;
224 HANDLE hStartUpEvent;
225 OSS_MSG_RING msgRing;
226 } WINE_WAVEIN;
228 static WINE_WAVEOUT WOutDev [MAX_WAVEDRV];
229 static WINE_WAVEIN WInDev [MAX_WAVEDRV];
230 static unsigned numOutDev;
231 static unsigned numInDev;
233 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
234 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv);
235 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
236 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc);
238 /* These strings used only for tracing */
239 static const char * getCmdString(enum win_wm_message msg)
241 static char unknown[32];
242 #define MSG_TO_STR(x) case x: return #x
243 switch(msg) {
244 MSG_TO_STR(WINE_WM_PAUSING);
245 MSG_TO_STR(WINE_WM_RESTARTING);
246 MSG_TO_STR(WINE_WM_RESETTING);
247 MSG_TO_STR(WINE_WM_HEADER);
248 MSG_TO_STR(WINE_WM_UPDATE);
249 MSG_TO_STR(WINE_WM_BREAKLOOP);
250 MSG_TO_STR(WINE_WM_CLOSING);
251 MSG_TO_STR(WINE_WM_STARTING);
252 MSG_TO_STR(WINE_WM_STOPPING);
254 #undef MSG_TO_STR
255 sprintf(unknown, "UNKNOWN(0x%08x)", msg);
256 return unknown;
259 static int getEnables(OSS_DEVICE *ossdev)
261 return ( (ossdev->bOutputEnabled ? PCM_ENABLE_OUTPUT : 0) |
262 (ossdev->bInputEnabled ? PCM_ENABLE_INPUT : 0) );
265 static const char * getMessage(UINT msg)
267 static char unknown[32];
268 #define MSG_TO_STR(x) case x: return #x
269 switch(msg) {
270 MSG_TO_STR(DRVM_INIT);
271 MSG_TO_STR(DRVM_EXIT);
272 MSG_TO_STR(DRVM_ENABLE);
273 MSG_TO_STR(DRVM_DISABLE);
274 MSG_TO_STR(WIDM_OPEN);
275 MSG_TO_STR(WIDM_CLOSE);
276 MSG_TO_STR(WIDM_ADDBUFFER);
277 MSG_TO_STR(WIDM_PREPARE);
278 MSG_TO_STR(WIDM_UNPREPARE);
279 MSG_TO_STR(WIDM_GETDEVCAPS);
280 MSG_TO_STR(WIDM_GETNUMDEVS);
281 MSG_TO_STR(WIDM_GETPOS);
282 MSG_TO_STR(WIDM_RESET);
283 MSG_TO_STR(WIDM_START);
284 MSG_TO_STR(WIDM_STOP);
285 MSG_TO_STR(WODM_OPEN);
286 MSG_TO_STR(WODM_CLOSE);
287 MSG_TO_STR(WODM_WRITE);
288 MSG_TO_STR(WODM_PAUSE);
289 MSG_TO_STR(WODM_GETPOS);
290 MSG_TO_STR(WODM_BREAKLOOP);
291 MSG_TO_STR(WODM_PREPARE);
292 MSG_TO_STR(WODM_UNPREPARE);
293 MSG_TO_STR(WODM_GETDEVCAPS);
294 MSG_TO_STR(WODM_GETNUMDEVS);
295 MSG_TO_STR(WODM_GETPITCH);
296 MSG_TO_STR(WODM_SETPITCH);
297 MSG_TO_STR(WODM_GETPLAYBACKRATE);
298 MSG_TO_STR(WODM_SETPLAYBACKRATE);
299 MSG_TO_STR(WODM_GETVOLUME);
300 MSG_TO_STR(WODM_SETVOLUME);
301 MSG_TO_STR(WODM_RESTART);
302 MSG_TO_STR(WODM_RESET);
303 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
304 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
305 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
306 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
308 #undef MSG_TO_STR
309 sprintf(unknown, "UNKNOWN(0x%04x)", msg);
310 return unknown;
313 static DWORD wdDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
315 TRACE("(%u, %p)\n", wDevID, dwParam1);
317 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, OSS_Devices[wDevID].interface_name, -1,
318 NULL, 0 ) * sizeof(WCHAR);
319 return MMSYSERR_NOERROR;
322 static DWORD wdDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
324 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, OSS_Devices[wDevID].interface_name, -1,
325 NULL, 0 ) * sizeof(WCHAR))
327 MultiByteToWideChar(CP_ACP, 0, OSS_Devices[wDevID].interface_name, -1,
328 dwParam1, dwParam2 / sizeof(WCHAR));
329 return MMSYSERR_NOERROR;
332 return MMSYSERR_INVALPARAM;
335 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
336 WAVEFORMATPCMEX* format)
338 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
339 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
340 format->Format.nChannels, format->Format.nAvgBytesPerSec);
341 TRACE("Position in bytes=%lu\n", position);
343 switch (lpTime->wType) {
344 case TIME_SAMPLES:
345 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
346 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
347 break;
348 case TIME_MS:
349 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
350 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
351 break;
352 case TIME_SMPTE:
353 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
354 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
355 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
356 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
357 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
358 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
359 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
360 lpTime->u.smpte.fps = 30;
361 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
362 position -= lpTime->u.smpte.frame * format->Format.nSamplesPerSec / lpTime->u.smpte.fps;
363 if (position != 0)
365 /* Round up */
366 lpTime->u.smpte.frame++;
368 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
369 lpTime->u.smpte.hour, lpTime->u.smpte.min,
370 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
371 break;
372 default:
373 FIXME("Format %d not supported ! use TIME_BYTES !\n", lpTime->wType);
374 lpTime->wType = TIME_BYTES;
375 /* fall through */
376 case TIME_BYTES:
377 lpTime->u.cb = position;
378 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
379 break;
381 return MMSYSERR_NOERROR;
384 static BOOL supportedFormat(LPWAVEFORMATEX wf)
386 TRACE("(%p)\n",wf);
388 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
389 return FALSE;
391 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
392 if (wf->nChannels==1||wf->nChannels==2) {
393 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
394 return TRUE;
396 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
397 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
399 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
400 if (wf->nChannels==1||wf->nChannels==2) {
401 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
402 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
403 return TRUE;
404 } else
405 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
407 } else
408 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
409 } else
410 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
412 return FALSE;
415 static void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
417 ZeroMemory(wf2, sizeof(wf2));
418 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
419 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
420 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
421 memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
422 else
423 memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
426 /*======================================================================*
427 * Low level WAVE implementation *
428 *======================================================================*/
430 /******************************************************************
431 * OSS_RawOpenDevice
433 * Low level device opening (from values stored in ossdev)
435 static DWORD OSS_RawOpenDevice(OSS_DEVICE* ossdev, int strict_format)
437 int fd, val, rc;
438 TRACE("(%p,%d)\n",ossdev,strict_format);
440 TRACE("open_access=%s\n",
441 ossdev->open_access == O_RDONLY ? "O_RDONLY" :
442 ossdev->open_access == O_WRONLY ? "O_WRONLY" :
443 ossdev->open_access == O_RDWR ? "O_RDWR" : "Unknown");
445 if ((fd = open(ossdev->dev_name, ossdev->open_access|O_NDELAY, 0)) == -1)
447 WARN("Couldn't open %s (%s)\n", ossdev->dev_name, strerror(errno));
448 return (errno == EBUSY) ? MMSYSERR_ALLOCATED : MMSYSERR_ERROR;
450 fcntl(fd, F_SETFD, 1); /* set close on exec flag */
451 /* turn full duplex on if it has been requested */
452 if (ossdev->open_access == O_RDWR && ossdev->full_duplex) {
453 rc = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
454 /* on *BSD, as full duplex is always enabled by default, this ioctl
455 * will fail with EINVAL
456 * so, we don't consider EINVAL an error here
458 if (rc != 0 && errno != EINVAL) {
459 WARN("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev->dev_name, strerror(errno));
460 goto error2;
464 if (ossdev->audio_fragment) {
465 rc = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &ossdev->audio_fragment);
466 if (rc != 0) {
467 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev->dev_name, strerror(errno));
468 goto error2;
472 /* First size and stereo then samplerate */
473 if (ossdev->format>=0)
475 val = ossdev->format;
476 rc = ioctl(fd, SNDCTL_DSP_SETFMT, &ossdev->format);
477 if (rc != 0 || val != ossdev->format) {
478 TRACE("Can't set format to %d (returned %d)\n", val, ossdev->format);
479 if (strict_format)
480 goto error;
483 if (ossdev->stereo>=0)
485 val = ossdev->stereo;
486 rc = ioctl(fd, SNDCTL_DSP_STEREO, &ossdev->stereo);
487 if (rc != 0 || val != ossdev->stereo) {
488 TRACE("Can't set stereo to %u (returned %d)\n", val, ossdev->stereo);
489 if (strict_format)
490 goto error;
493 if (ossdev->sample_rate>=0)
495 val = ossdev->sample_rate;
496 rc = ioctl(fd, SNDCTL_DSP_SPEED, &ossdev->sample_rate);
497 if (rc != 0 || !NEAR_MATCH(val, ossdev->sample_rate)) {
498 TRACE("Can't set sample_rate to %u (returned %d)\n", val, ossdev->sample_rate);
499 if (strict_format)
500 goto error;
503 ossdev->fd = fd;
505 if (ossdev->bTriggerSupport) {
506 int trigger;
507 rc = ioctl(fd, SNDCTL_DSP_GETTRIGGER, &trigger);
508 if (rc != 0) {
509 ERR("ioctl(%s, SNDCTL_DSP_GETTRIGGER) failed (%s)\n",
510 ossdev->dev_name, strerror(errno));
511 goto error;
514 ossdev->bOutputEnabled = ((trigger & PCM_ENABLE_OUTPUT) == PCM_ENABLE_OUTPUT);
515 ossdev->bInputEnabled = ((trigger & PCM_ENABLE_INPUT) == PCM_ENABLE_INPUT);
516 } else {
517 ossdev->bOutputEnabled = TRUE; /* OSS enables by default */
518 ossdev->bInputEnabled = TRUE; /* OSS enables by default */
521 return MMSYSERR_NOERROR;
523 error:
524 close(fd);
525 return WAVERR_BADFORMAT;
526 error2:
527 close(fd);
528 return MMSYSERR_ERROR;
531 /******************************************************************
532 * OSS_OpenDevice
534 * since OSS has poor capabilities in full duplex, we try here to let a program
535 * open the device for both waveout and wavein streams...
536 * this is hackish, but it's the way OSS interface is done...
538 static DWORD OSS_OpenDevice(OSS_DEVICE* ossdev, unsigned req_access,
539 int* frag, int strict_format,
540 int sample_rate, int stereo, int fmt)
542 DWORD ret;
543 DWORD open_access;
544 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev,req_access,frag,strict_format,sample_rate,stereo,fmt);
546 if (ossdev->full_duplex && (req_access == O_RDONLY || req_access == O_WRONLY))
548 TRACE("Opening RDWR because full_duplex=%d and req_access=%d\n",
549 ossdev->full_duplex,req_access);
550 open_access = O_RDWR;
552 else
554 open_access=req_access;
557 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
558 if (ossdev->open_count == 0)
560 if (access(ossdev->dev_name, 0) != 0) return MMSYSERR_NODRIVER;
562 ossdev->audio_fragment = (frag) ? *frag : 0;
563 ossdev->sample_rate = sample_rate;
564 ossdev->stereo = stereo;
565 ossdev->format = fmt;
566 ossdev->open_access = open_access;
567 ossdev->owner_tid = GetCurrentThreadId();
569 if ((ret = OSS_RawOpenDevice(ossdev,strict_format)) != MMSYSERR_NOERROR) return ret;
570 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
571 (req_access == O_RDONLY || req_access == O_WRONLY))
573 int enable;
574 if (req_access == O_WRONLY)
575 ossdev->bInputEnabled=0;
576 else
577 ossdev->bOutputEnabled=0;
578 enable = getEnables(ossdev);
579 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
580 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
581 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
584 else
586 /* check we really open with the same parameters */
587 if (ossdev->open_access != open_access)
589 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
590 return WAVERR_BADFORMAT;
593 /* check if the audio parameters are the same */
594 if (ossdev->sample_rate != sample_rate ||
595 ossdev->stereo != stereo ||
596 ossdev->format != fmt)
598 /* This is not a fatal error because MSACM might do the remapping */
599 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
600 "OSS doesn't allow us different parameters\n"
601 "audio_frag(%x/%x) sample_rate(%d/%d) stereo(%d/%d) fmt(%d/%d)\n",
602 ossdev->audio_fragment, frag ? *frag : 0,
603 ossdev->sample_rate, sample_rate,
604 ossdev->stereo, stereo,
605 ossdev->format, fmt);
606 return WAVERR_BADFORMAT;
608 /* check if the fragment sizes are the same */
609 if (ossdev->audio_fragment != (frag ? *frag : 0) ) {
610 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
611 "Use: \"HardwareAcceleration\" = \"Emulation\" in the [dsound] section of your config file.\n");
612 return WAVERR_BADFORMAT;
614 if (GetCurrentThreadId() != ossdev->owner_tid)
616 WARN("Another thread is trying to access audio...\n");
617 return MMSYSERR_ERROR;
619 if (ossdev->full_duplex && ossdev->bTriggerSupport &&
620 (req_access == O_RDONLY || req_access == O_WRONLY))
622 int enable;
623 if (req_access == O_WRONLY)
624 ossdev->bOutputEnabled=1;
625 else
626 ossdev->bInputEnabled=1;
627 enable = getEnables(ossdev);
628 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable);
629 if (ioctl(ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
630 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev->dev_name, enable, strerror(errno));
634 ossdev->open_count++;
636 return MMSYSERR_NOERROR;
639 /******************************************************************
640 * OSS_CloseDevice
644 static void OSS_CloseDevice(OSS_DEVICE* ossdev)
646 TRACE("(%p)\n",ossdev);
647 if (ossdev->open_count>0) {
648 ossdev->open_count--;
649 } else {
650 WARN("OSS_CloseDevice called too many times\n");
652 if (ossdev->open_count == 0)
654 /* reset the device before we close it in case it is in a bad state */
655 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
656 close(ossdev->fd);
660 /******************************************************************
661 * OSS_ResetDevice
663 * Resets the device. OSS Commercial requires the device to be closed
664 * after a SNDCTL_DSP_RESET ioctl call... this function implements
665 * this behavior...
666 * FIXME: This causes problems when doing full duplex so we really
667 * only reset when not doing full duplex. We need to do this better
668 * someday.
670 static DWORD OSS_ResetDevice(OSS_DEVICE* ossdev)
672 DWORD ret = MMSYSERR_NOERROR;
673 int old_fd = ossdev->fd;
674 TRACE("(%p)\n", ossdev);
676 if (ossdev->open_count == 1) {
677 if (ioctl(ossdev->fd, SNDCTL_DSP_RESET, NULL) == -1)
679 perror("ioctl SNDCTL_DSP_RESET");
680 return -1;
682 close(ossdev->fd);
683 ret = OSS_RawOpenDevice(ossdev, 1);
684 TRACE("Changing fd from %d to %d\n", old_fd, ossdev->fd);
685 } else
686 WARN("Not resetting device because it is in full duplex mode!\n");
688 return ret;
691 const static int win_std_oss_fmts[2]={AFMT_U8,AFMT_S16_LE};
692 const static int win_std_rates[5]={96000,48000,44100,22050,11025};
693 const static int win_std_formats[2][2][5]=
694 {{{WAVE_FORMAT_96M08, WAVE_FORMAT_48M08, WAVE_FORMAT_4M08,
695 WAVE_FORMAT_2M08, WAVE_FORMAT_1M08},
696 {WAVE_FORMAT_96S08, WAVE_FORMAT_48S08, WAVE_FORMAT_4S08,
697 WAVE_FORMAT_2S08, WAVE_FORMAT_1S08}},
698 {{WAVE_FORMAT_96M16, WAVE_FORMAT_48M16, WAVE_FORMAT_4M16,
699 WAVE_FORMAT_2M16, WAVE_FORMAT_1M16},
700 {WAVE_FORMAT_96S16, WAVE_FORMAT_48S16, WAVE_FORMAT_4S16,
701 WAVE_FORMAT_2S16, WAVE_FORMAT_1S16}},
704 static void OSS_Info(int fd)
706 /* Note that this only reports the formats supported by the hardware.
707 * The driver may support other formats and do the conversions in
708 * software which is why we don't use this value
710 int oss_mask, oss_caps;
711 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &oss_mask) >= 0) {
712 TRACE("Formats=%08x ( ", oss_mask);
713 if (oss_mask & AFMT_MU_LAW) TRACE("AFMT_MU_LAW ");
714 if (oss_mask & AFMT_A_LAW) TRACE("AFMT_A_LAW ");
715 if (oss_mask & AFMT_IMA_ADPCM) TRACE("AFMT_IMA_ADPCM ");
716 if (oss_mask & AFMT_U8) TRACE("AFMT_U8 ");
717 if (oss_mask & AFMT_S16_LE) TRACE("AFMT_S16_LE ");
718 if (oss_mask & AFMT_S16_BE) TRACE("AFMT_S16_BE ");
719 if (oss_mask & AFMT_S8) TRACE("AFMT_S8 ");
720 if (oss_mask & AFMT_U16_LE) TRACE("AFMT_U16_LE ");
721 if (oss_mask & AFMT_U16_BE) TRACE("AFMT_U16_BE ");
722 if (oss_mask & AFMT_MPEG) TRACE("AFMT_MPEG ");
723 #ifdef AFMT_AC3
724 if (oss_mask & AFMT_AC3) TRACE("AFMT_AC3 ");
725 #endif
726 #ifdef AFMT_VORBIS
727 if (oss_mask & AFMT_VORBIS) TRACE("AFMT_VORBIS ");
728 #endif
729 #ifdef AFMT_S32_LE
730 if (oss_mask & AFMT_S32_LE) TRACE("AFMT_S32_LE ");
731 #endif
732 #ifdef AFMT_S32_BE
733 if (oss_mask & AFMT_S32_BE) TRACE("AFMT_S32_BE ");
734 #endif
735 #ifdef AFMT_FLOAT
736 if (oss_mask & AFMT_FLOAT) TRACE("AFMT_FLOAT ");
737 #endif
738 #ifdef AFMT_S24_LE
739 if (oss_mask & AFMT_S24_LE) TRACE("AFMT_S24_LE ");
740 #endif
741 #ifdef AFMT_S24_BE
742 if (oss_mask & AFMT_S24_BE) TRACE("AFMT_S24_BE ");
743 #endif
744 #ifdef AFMT_SPDIF_RAW
745 if (oss_mask & AFMT_SPDIF_RAW) TRACE("AFMT_SPDIF_RAW ");
746 #endif
747 TRACE(")\n");
749 if (ioctl(fd, SNDCTL_DSP_GETCAPS, &oss_caps) >= 0) {
750 TRACE("Caps=%08x\n",oss_caps);
751 TRACE("\tRevision: %d\n", oss_caps&DSP_CAP_REVISION);
752 TRACE("\tDuplex: %s\n", oss_caps & DSP_CAP_DUPLEX ? "true" : "false");
753 TRACE("\tRealtime: %s\n", oss_caps & DSP_CAP_REALTIME ? "true" : "false");
754 TRACE("\tBatch: %s\n", oss_caps & DSP_CAP_BATCH ? "true" : "false");
755 TRACE("\tCoproc: %s\n", oss_caps & DSP_CAP_COPROC ? "true" : "false");
756 TRACE("\tTrigger: %s\n", oss_caps & DSP_CAP_TRIGGER ? "true" : "false");
757 TRACE("\tMmap: %s\n", oss_caps & DSP_CAP_MMAP ? "true" : "false");
758 #ifdef DSP_CAP_MULTI
759 TRACE("\tMulti: %s\n", oss_caps & DSP_CAP_MULTI ? "true" : "false");
760 #endif
761 #ifdef DSP_CAP_BIND
762 TRACE("\tBind: %s\n", oss_caps & DSP_CAP_BIND ? "true" : "false");
763 #endif
764 #ifdef DSP_CAP_INPUT
765 TRACE("\tInput: %s\n", oss_caps & DSP_CAP_INPUT ? "true" : "false");
766 #endif
767 #ifdef DSP_CAP_OUTPUT
768 TRACE("\tOutput: %s\n", oss_caps & DSP_CAP_OUTPUT ? "true" : "false");
769 #endif
770 #ifdef DSP_CAP_VIRTUAL
771 TRACE("\tVirtual: %s\n", oss_caps & DSP_CAP_VIRTUAL ? "true" : "false");
772 #endif
773 #ifdef DSP_CAP_ANALOGOUT
774 TRACE("\tAnalog Out: %s\n", oss_caps & DSP_CAP_ANALOGOUT ? "true" : "false");
775 #endif
776 #ifdef DSP_CAP_ANALOGIN
777 TRACE("\tAnalog In: %s\n", oss_caps & DSP_CAP_ANALOGIN ? "true" : "false");
778 #endif
779 #ifdef DSP_CAP_DIGITALOUT
780 TRACE("\tDigital Out: %s\n", oss_caps & DSP_CAP_DIGITALOUT ? "true" : "false");
781 #endif
782 #ifdef DSP_CAP_DIGITALIN
783 TRACE("\tDigital In: %s\n", oss_caps & DSP_CAP_DIGITALIN ? "true" : "false");
784 #endif
785 #ifdef DSP_CAP_ADMASK
786 TRACE("\tA/D Mask: %s\n", oss_caps & DSP_CAP_ADMASK ? "true" : "false");
787 #endif
788 #ifdef DSP_CAP_SHADOW
789 TRACE("\tShadow: %s\n", oss_caps & DSP_CAP_SHADOW ? "true" : "false");
790 #endif
791 #ifdef DSP_CH_MASK
792 TRACE("\tChannel Mask: %x\n", oss_caps & DSP_CH_MASK);
793 #endif
794 #ifdef DSP_CAP_SLAVE
795 TRACE("\tSlave: %s\n", oss_caps & DSP_CAP_SLAVE ? "true" : "false");
796 #endif
800 /******************************************************************
801 * OSS_WaveOutInit
805 static BOOL OSS_WaveOutInit(OSS_DEVICE* ossdev)
807 int rc,arg;
808 int f,c,r;
809 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
811 if (OSS_OpenDevice(ossdev, O_WRONLY, NULL, 0,-1,-1,-1) != 0)
812 return FALSE;
814 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
816 #ifdef SOUND_MIXER_INFO
818 int mixer;
819 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
820 mixer_info info;
821 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
822 strncpy(ossdev->ds_desc.szDesc, info.name, sizeof(info.name));
823 strcpy(ossdev->ds_desc.szDrvName, "wineoss.drv");
824 strncpy(ossdev->out_caps.szPname, info.name, sizeof(info.name));
825 TRACE("%s\n", ossdev->ds_desc.szDesc);
826 } else {
827 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
828 * implement it properly, and there are probably similar issues
829 * on other platforms, so we warn but try to go ahead.
831 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
833 close(mixer);
834 } else {
835 ERR("%s: %s\n", ossdev->mixer_name , strerror( errno ));
836 OSS_CloseDevice(ossdev);
837 return FALSE;
840 #endif /* SOUND_MIXER_INFO */
842 if (WINE_TRACE_ON(wave))
843 OSS_Info(ossdev->fd);
845 /* FIXME: some programs compare this string against the content of the
846 * registry for MM drivers. The names have to match in order for the
847 * program to work (e.g. MS win9x mplayer.exe)
849 #ifdef EMULATE_SB16
850 ossdev->out_caps.wMid = 0x0002;
851 ossdev->out_caps.wPid = 0x0104;
852 strcpy(ossdev->out_caps.szPname, "SB16 Wave Out");
853 #else
854 ossdev->out_caps.wMid = 0x00FF; /* Manufac ID */
855 ossdev->out_caps.wPid = 0x0001; /* Product ID */
856 #endif
857 ossdev->out_caps.vDriverVersion = 0x0100;
858 ossdev->out_caps.wChannels = 1;
859 ossdev->out_caps.dwFormats = 0x00000000;
860 ossdev->out_caps.wReserved1 = 0;
861 ossdev->out_caps.dwSupport = WAVECAPS_VOLUME;
863 /* direct sound caps */
864 ossdev->ds_caps.dwFlags = DSCAPS_CERTIFIED;
865 ossdev->ds_caps.dwPrimaryBuffers = 1;
866 ossdev->ds_caps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
867 ossdev->ds_caps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
869 /* We must first set the format and the stereo mode as some sound cards
870 * may support 44kHz mono but not 44kHz stereo. Also we must
871 * systematically check the return value of these ioctls as they will
872 * always succeed (see OSS Linux) but will modify the parameter to match
873 * whatever they support. The OSS specs also say we must first set the
874 * sample size, then the stereo and then the sample rate.
876 for (f=0;f<2;f++) {
877 arg=win_std_oss_fmts[f];
878 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
879 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
880 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
881 rc,arg,win_std_oss_fmts[f]);
882 continue;
884 if (f == 0)
885 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY8BIT;
886 else if (f == 1)
887 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARY16BIT;
889 for (c=0;c<2;c++) {
890 arg=c;
891 rc=ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &arg);
892 if (rc!=0 || arg!=c) {
893 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc,arg,c);
894 continue;
896 if (c == 0) {
897 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
898 } else if (c==1) {
899 ossdev->out_caps.wChannels=2;
900 ossdev->out_caps.dwSupport|=WAVECAPS_LRVOLUME;
901 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
904 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
905 arg=win_std_rates[r];
906 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
907 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
908 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c+1);
909 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]))
910 ossdev->out_caps.dwFormats|=win_std_formats[f][c][r];
915 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
916 if (arg & DSP_CAP_TRIGGER)
917 ossdev->bTriggerSupport = TRUE;
918 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
919 ossdev->out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
921 /* well, might as well use the DirectSound cap flag for something */
922 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
923 !(arg & DSP_CAP_BATCH)) {
924 ossdev->out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
925 } else {
926 ossdev->ds_caps.dwFlags |= DSCAPS_EMULDRIVER;
928 #ifdef DSP_CAP_MULTI /* not every oss has this */
929 /* check for hardware secondary buffer support (multi open) */
930 if ((arg & DSP_CAP_MULTI) &&
931 (ossdev->out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
932 TRACE("hardware secondary buffer support available\n");
933 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARY8BIT)
934 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY8BIT;
935 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARY16BIT)
936 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARY16BIT;
937 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARYMONO)
938 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYMONO;
939 if (ossdev->ds_caps.dwFlags & DSCAPS_PRIMARYSTEREO)
940 ossdev->ds_caps.dwFlags |= DSCAPS_SECONDARYSTEREO;
942 ossdev->ds_caps.dwMaxHwMixingAllBuffers = 16;
943 ossdev->ds_caps.dwMaxHwMixingStaticBuffers = 0;
944 ossdev->ds_caps.dwMaxHwMixingStreamingBuffers = 16;
946 ossdev->ds_caps.dwFreeHwMixingAllBuffers = 16;
947 ossdev->ds_caps.dwFreeHwMixingStaticBuffers = 0;
948 ossdev->ds_caps.dwFreeHwMixingStreamingBuffers = 16;
950 #endif
952 OSS_CloseDevice(ossdev);
953 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
954 ossdev->out_caps.dwFormats, ossdev->out_caps.dwSupport);
955 return TRUE;
958 /******************************************************************
959 * OSS_WaveInInit
963 static BOOL OSS_WaveInInit(OSS_DEVICE* ossdev)
965 int rc,arg;
966 int f,c,r;
967 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
969 if (OSS_OpenDevice(ossdev, O_RDONLY, NULL, 0,-1,-1,-1) != 0)
970 return FALSE;
972 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
974 #ifdef SOUND_MIXER_INFO
976 int mixer;
977 if ((mixer = open(ossdev->mixer_name, O_RDONLY|O_NDELAY)) >= 0) {
978 mixer_info info;
979 if (ioctl(mixer, SOUND_MIXER_INFO, &info) >= 0) {
980 strncpy(ossdev->in_caps.szPname, info.name, sizeof(info.name));
981 TRACE("%s\n", ossdev->ds_desc.szDesc);
982 } else {
983 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
984 * implement it properly, and there are probably similar issues
985 * on other platforms, so we warn but try to go ahead.
987 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev->mixer_name);
989 close(mixer);
990 } else {
991 ERR("%s: %s\n", ossdev->mixer_name, strerror(errno));
992 OSS_CloseDevice(ossdev);
993 return FALSE;
996 #endif /* SOUND_MIXER_INFO */
998 if (WINE_TRACE_ON(wave))
999 OSS_Info(ossdev->fd);
1001 /* See comment in OSS_WaveOutInit */
1002 #ifdef EMULATE_SB16
1003 ossdev->in_caps.wMid = 0x0002;
1004 ossdev->in_caps.wPid = 0x0004;
1005 strcpy(ossdev->in_caps.szPname, "SB16 Wave In");
1006 #else
1007 ossdev->in_caps.wMid = 0x00FF; /* Manufac ID */
1008 ossdev->in_caps.wPid = 0x0001; /* Product ID */
1009 #endif
1010 ossdev->in_caps.dwFormats = 0x00000000;
1011 ossdev->in_caps.wChannels = 1;
1012 ossdev->in_caps.wReserved1 = 0;
1014 /* direct sound caps */
1015 ossdev->dsc_caps.dwSize = sizeof(ossdev->dsc_caps);
1016 ossdev->dsc_caps.dwFlags = 0;
1017 ossdev->dsc_caps.dwFormats = 0x00000000;
1018 ossdev->dsc_caps.dwChannels = 1;
1020 /* See the comment in OSS_WaveOutInit for the loop order */
1021 for (f=0;f<2;f++) {
1022 arg=win_std_oss_fmts[f];
1023 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
1024 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
1025 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1026 rc,arg,win_std_oss_fmts[f]);
1027 continue;
1030 for (c=0;c<2;c++) {
1031 arg=c;
1032 rc=ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &arg);
1033 if (rc!=0 || arg!=c) {
1034 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc,arg,c);
1035 continue;
1037 if (c==1) {
1038 ossdev->in_caps.wChannels=2;
1039 ossdev->dsc_caps.dwChannels=2;
1042 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
1043 arg=win_std_rates[r];
1044 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
1045 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);
1046 if (rc==0 && NEAR_MATCH(arg,win_std_rates[r]))
1047 ossdev->in_caps.dwFormats|=win_std_formats[f][c][r];
1048 ossdev->dsc_caps.dwFormats|=win_std_formats[f][c][r];
1053 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
1054 if (arg & DSP_CAP_TRIGGER)
1055 ossdev->bTriggerSupport = TRUE;
1056 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
1057 !(arg & DSP_CAP_BATCH)) {
1058 /* FIXME: enable the next statement if you want to work on the driver */
1059 #if 0
1060 ossdev->in_caps_support |= WAVECAPS_DIRECTSOUND;
1061 #endif
1063 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH))
1064 ossdev->in_caps_support |= WAVECAPS_SAMPLEACCURATE;
1066 OSS_CloseDevice(ossdev);
1067 TRACE("in dwFormats = %08lX, in_caps_support = %08lX\n",
1068 ossdev->in_caps.dwFormats, ossdev->in_caps_support);
1069 return TRUE;
1072 /******************************************************************
1073 * OSS_WaveFullDuplexInit
1077 static void OSS_WaveFullDuplexInit(OSS_DEVICE* ossdev)
1079 int rc,arg;
1080 int f,c,r;
1081 int caps;
1082 TRACE("(%p) %s\n", ossdev, ossdev->dev_name);
1084 /* The OSS documentation says we must call SNDCTL_SETDUPLEX
1085 * *before* checking for SNDCTL_DSP_GETCAPS otherwise we may
1086 * get the wrong result. This ioctl must even be done before
1087 * setting the fragment size so that only OSS_RawOpenDevice is
1088 * in a position to do it. So we set full_duplex speculatively
1089 * and adjust right after.
1091 ossdev->full_duplex=1;
1092 rc=OSS_OpenDevice(ossdev, O_RDWR, NULL, 0,-1,-1,-1);
1093 ossdev->full_duplex=0;
1094 if (rc != 0)
1095 return;
1097 ioctl(ossdev->fd, SNDCTL_DSP_RESET, 0);
1098 TRACE("%s\n", ossdev->ds_desc.szDesc);
1101 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &caps) == 0)
1102 ossdev->full_duplex = (caps & DSP_CAP_DUPLEX);
1104 ossdev->duplex_out_caps = ossdev->out_caps;
1106 ossdev->duplex_out_caps.wChannels = 1;
1107 ossdev->duplex_out_caps.dwFormats = 0x00000000;
1108 ossdev->duplex_out_caps.dwSupport = WAVECAPS_VOLUME;
1110 if (WINE_TRACE_ON(wave))
1111 OSS_Info(ossdev->fd);
1113 /* See the comment in OSS_WaveOutInit for the loop order */
1114 for (f=0;f<2;f++) {
1115 arg=win_std_oss_fmts[f];
1116 rc=ioctl(ossdev->fd, SNDCTL_DSP_SAMPLESIZE, &arg);
1117 if (rc!=0 || arg!=win_std_oss_fmts[f]) {
1118 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1119 rc,arg,win_std_oss_fmts[f]);
1120 continue;
1123 for (c=0;c<2;c++) {
1124 arg=c;
1125 rc=ioctl(ossdev->fd, SNDCTL_DSP_STEREO, &arg);
1126 if (rc!=0 || arg!=c) {
1127 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc,arg,c);
1128 continue;
1130 if (c == 0) {
1131 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYMONO;
1132 } else if (c==1) {
1133 ossdev->duplex_out_caps.wChannels=2;
1134 ossdev->duplex_out_caps.dwSupport|=WAVECAPS_LRVOLUME;
1135 ossdev->ds_caps.dwFlags |= DSCAPS_PRIMARYSTEREO;
1138 for (r=0;r<sizeof(win_std_rates)/sizeof(*win_std_rates);r++) {
1139 arg=win_std_rates[r];
1140 rc=ioctl(ossdev->fd, SNDCTL_DSP_SPEED, &arg);
1141 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
1142 rc,arg,win_std_rates[r],win_std_oss_fmts[f],c+1);
1143 if (rc==0 && arg!=0 && NEAR_MATCH(arg,win_std_rates[r]))
1144 ossdev->duplex_out_caps.dwFormats|=win_std_formats[f][c][r];
1149 if (ioctl(ossdev->fd, SNDCTL_DSP_GETCAPS, &arg) == 0) {
1150 if ((arg & DSP_CAP_REALTIME) && !(arg & DSP_CAP_BATCH)) {
1151 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
1153 /* well, might as well use the DirectSound cap flag for something */
1154 if ((arg & DSP_CAP_TRIGGER) && (arg & DSP_CAP_MMAP) &&
1155 !(arg & DSP_CAP_BATCH)) {
1156 ossdev->duplex_out_caps.dwSupport |= WAVECAPS_DIRECTSOUND;
1159 OSS_CloseDevice(ossdev);
1160 TRACE("duplex dwFormats = %08lX, dwSupport = %08lX\n",
1161 ossdev->duplex_out_caps.dwFormats, ossdev->duplex_out_caps.dwSupport);
1164 static char* StrDup(const char* str, const char* def)
1166 char* dst;
1167 if (str==NULL)
1168 str=def;
1169 dst=HeapAlloc(GetProcessHeap(),0,strlen(str)+1);
1170 strcpy(dst, str);
1171 return dst;
1174 /******************************************************************
1175 * OSS_WaveInit
1177 * Initialize internal structures from OSS information
1179 LONG OSS_WaveInit(void)
1181 char* str;
1182 int i;
1184 TRACE("()\n");
1186 str=getenv("AUDIODEV");
1187 if (str!=NULL)
1189 OSS_Devices[0].dev_name=StrDup(str,"");
1190 OSS_Devices[0].mixer_name=StrDup(getenv("MIXERDEV"),"/dev/mixer");
1191 for (i = 1; i < MAX_WAVEDRV; ++i)
1193 OSS_Devices[i].dev_name=StrDup("",NULL);
1194 OSS_Devices[i].mixer_name=StrDup("",NULL);
1197 else
1199 OSS_Devices[0].dev_name=StrDup("/dev/dsp",NULL);
1200 OSS_Devices[0].mixer_name=StrDup("/dev/mixer",NULL);
1201 for (i = 1; i < MAX_WAVEDRV; ++i)
1203 OSS_Devices[i].dev_name=HeapAlloc(GetProcessHeap(),0,11);
1204 sprintf(OSS_Devices[i].dev_name, "/dev/dsp%d", i);
1205 OSS_Devices[i].mixer_name=HeapAlloc(GetProcessHeap(),0,13);
1206 sprintf(OSS_Devices[i].mixer_name, "/dev/mixer%d", i);
1210 for (i = 0; i < MAX_WAVEDRV; ++i)
1212 OSS_Devices[i].interface_name=HeapAlloc(GetProcessHeap(),0,9+strlen(OSS_Devices[i].dev_name)+1);
1213 sprintf(OSS_Devices[i].interface_name, "wineoss: %s", OSS_Devices[i].dev_name);
1216 /* start with output devices */
1217 for (i = 0; i < MAX_WAVEDRV; ++i)
1219 if (*OSS_Devices[i].dev_name=='\0' || OSS_WaveOutInit(&OSS_Devices[i]))
1221 WOutDev[numOutDev].state = WINE_WS_CLOSED;
1222 WOutDev[numOutDev].ossdev = &OSS_Devices[i];
1223 WOutDev[numOutDev].volume = 0xffffffff;
1224 numOutDev++;
1228 /* then do input devices */
1229 for (i = 0; i < MAX_WAVEDRV; ++i)
1231 if (*OSS_Devices[i].dev_name=='\0' || OSS_WaveInInit(&OSS_Devices[i]))
1233 WInDev[numInDev].state = WINE_WS_CLOSED;
1234 WInDev[numInDev].ossdev = &OSS_Devices[i];
1235 numInDev++;
1239 /* finish with the full duplex bits */
1240 for (i = 0; i < MAX_WAVEDRV; i++)
1241 if (*OSS_Devices[i].dev_name!='\0')
1242 OSS_WaveFullDuplexInit(&OSS_Devices[i]);
1244 return 0;
1247 /******************************************************************
1248 * OSS_InitRingMessage
1250 * Initialize the ring of messages for passing between driver's caller and playback/record
1251 * thread
1253 static int OSS_InitRingMessage(OSS_MSG_RING* omr)
1255 omr->msg_toget = 0;
1256 omr->msg_tosave = 0;
1257 #ifdef USE_PIPE_SYNC
1258 if (pipe(omr->msg_pipe) < 0) {
1259 omr->msg_pipe[0] = -1;
1260 omr->msg_pipe[1] = -1;
1261 ERR("could not create pipe, error=%s\n", strerror(errno));
1263 #else
1264 omr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
1265 #endif
1266 omr->ring_buffer_size = OSS_RING_BUFFER_INCREMENT;
1267 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(OSS_MSG));
1268 InitializeCriticalSection(&omr->msg_crst);
1269 omr->msg_crst.DebugInfo->Spare[1] = (DWORD)"WINEOSS_msg_crst";
1270 return 0;
1273 /******************************************************************
1274 * OSS_DestroyRingMessage
1277 static int OSS_DestroyRingMessage(OSS_MSG_RING* omr)
1279 #ifdef USE_PIPE_SYNC
1280 close(omr->msg_pipe[0]);
1281 close(omr->msg_pipe[1]);
1282 #else
1283 CloseHandle(omr->msg_event);
1284 #endif
1285 HeapFree(GetProcessHeap(),0,omr->messages);
1286 DeleteCriticalSection(&omr->msg_crst);
1287 return 0;
1290 /******************************************************************
1291 * OSS_AddRingMessage
1293 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
1295 static int OSS_AddRingMessage(OSS_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
1297 HANDLE hEvent = INVALID_HANDLE_VALUE;
1299 EnterCriticalSection(&omr->msg_crst);
1300 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
1302 int old_ring_buffer_size = omr->ring_buffer_size;
1303 omr->ring_buffer_size += OSS_RING_BUFFER_INCREMENT;
1304 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
1305 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(OSS_MSG));
1306 /* Now we need to rearrange the ring buffer so that the new
1307 buffers just allocated are in between omr->msg_tosave and
1308 omr->msg_toget.
1310 if (omr->msg_tosave < omr->msg_toget)
1312 memmove(&(omr->messages[omr->msg_toget + OSS_RING_BUFFER_INCREMENT]),
1313 &(omr->messages[omr->msg_toget]),
1314 sizeof(OSS_MSG)*(old_ring_buffer_size - omr->msg_toget)
1316 omr->msg_toget += OSS_RING_BUFFER_INCREMENT;
1319 if (wait)
1321 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1322 if (hEvent == INVALID_HANDLE_VALUE)
1324 ERR("can't create event !?\n");
1325 LeaveCriticalSection(&omr->msg_crst);
1326 return 0;
1328 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
1329 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1330 omr->msg_toget,getCmdString(omr->messages[omr->msg_toget].msg),
1331 omr->msg_tosave,getCmdString(omr->messages[omr->msg_tosave].msg));
1333 /* fast messages have to be added at the start of the queue */
1334 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
1335 omr->messages[omr->msg_toget].msg = msg;
1336 omr->messages[omr->msg_toget].param = param;
1337 omr->messages[omr->msg_toget].hEvent = hEvent;
1339 else
1341 omr->messages[omr->msg_tosave].msg = msg;
1342 omr->messages[omr->msg_tosave].param = param;
1343 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
1344 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
1346 LeaveCriticalSection(&omr->msg_crst);
1347 /* signal a new message */
1348 SIGNAL_OMR(omr);
1349 if (wait)
1351 /* wait for playback/record thread to have processed the message */
1352 WaitForSingleObject(hEvent, INFINITE);
1353 CloseHandle(hEvent);
1355 return 1;
1358 /******************************************************************
1359 * OSS_RetrieveRingMessage
1361 * Get a message from the ring. Should be called by the playback/record thread.
1363 static int OSS_RetrieveRingMessage(OSS_MSG_RING* omr,
1364 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
1366 EnterCriticalSection(&omr->msg_crst);
1368 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1370 LeaveCriticalSection(&omr->msg_crst);
1371 return 0;
1374 *msg = omr->messages[omr->msg_toget].msg;
1375 omr->messages[omr->msg_toget].msg = 0;
1376 *param = omr->messages[omr->msg_toget].param;
1377 *hEvent = omr->messages[omr->msg_toget].hEvent;
1378 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
1379 CLEAR_OMR(omr);
1380 LeaveCriticalSection(&omr->msg_crst);
1381 return 1;
1384 /******************************************************************
1385 * OSS_PeekRingMessage
1387 * Peek at a message from the ring but do not remove it.
1388 * Should be called by the playback/record thread.
1390 static int OSS_PeekRingMessage(OSS_MSG_RING* omr,
1391 enum win_wm_message *msg,
1392 DWORD *param, HANDLE *hEvent)
1394 EnterCriticalSection(&omr->msg_crst);
1396 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1398 LeaveCriticalSection(&omr->msg_crst);
1399 return 0;
1402 *msg = omr->messages[omr->msg_toget].msg;
1403 *param = omr->messages[omr->msg_toget].param;
1404 *hEvent = omr->messages[omr->msg_toget].hEvent;
1405 LeaveCriticalSection(&omr->msg_crst);
1406 return 1;
1409 /*======================================================================*
1410 * Low level WAVE OUT implementation *
1411 *======================================================================*/
1413 /**************************************************************************
1414 * wodNotifyClient [internal]
1416 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1418 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lX dwParam2 = %04lX\n", wMsg,
1419 wMsg == WOM_OPEN ? "WOM_OPEN" : wMsg == WOM_CLOSE ? "WOM_CLOSE" :
1420 wMsg == WOM_DONE ? "WOM_DONE" : "Unknown", dwParam1, dwParam2);
1422 switch (wMsg) {
1423 case WOM_OPEN:
1424 case WOM_CLOSE:
1425 case WOM_DONE:
1426 if (wwo->wFlags != DCB_NULL &&
1427 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
1428 (HDRVR)wwo->waveDesc.hWave, wMsg,
1429 wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
1430 WARN("can't notify client !\n");
1431 return MMSYSERR_ERROR;
1433 break;
1434 default:
1435 FIXME("Unknown callback message %u\n", wMsg);
1436 return MMSYSERR_INVALPARAM;
1438 return MMSYSERR_NOERROR;
1441 /**************************************************************************
1442 * wodUpdatePlayedTotal [internal]
1445 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, audio_buf_info* info)
1447 audio_buf_info dspspace;
1448 if (!info) info = &dspspace;
1450 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, info) < 0) {
1451 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
1452 return FALSE;
1454 wwo->dwPlayedTotal = wwo->dwWrittenTotal - (wwo->dwBufferSize - info->bytes);
1455 return TRUE;
1458 /**************************************************************************
1459 * wodPlayer_BeginWaveHdr [internal]
1461 * Makes the specified lpWaveHdr the currently playing wave header.
1462 * If the specified wave header is a begin loop and we're not already in
1463 * a loop, setup the loop.
1465 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1467 wwo->lpPlayPtr = lpWaveHdr;
1469 if (!lpWaveHdr) return;
1471 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1472 if (wwo->lpLoopPtr) {
1473 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1474 } else {
1475 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1476 wwo->lpLoopPtr = lpWaveHdr;
1477 /* Windows does not touch WAVEHDR.dwLoops,
1478 * so we need to make an internal copy */
1479 wwo->dwLoops = lpWaveHdr->dwLoops;
1482 wwo->dwPartialOffset = 0;
1485 /**************************************************************************
1486 * wodPlayer_PlayPtrNext [internal]
1488 * Advance the play pointer to the next waveheader, looping if required.
1490 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
1492 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1494 wwo->dwPartialOffset = 0;
1495 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1496 /* We're at the end of a loop, loop if required */
1497 if (--wwo->dwLoops > 0) {
1498 wwo->lpPlayPtr = wwo->lpLoopPtr;
1499 } else {
1500 /* Handle overlapping loops correctly */
1501 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1502 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1503 /* shall we consider the END flag for the closing loop or for
1504 * the opening one or for both ???
1505 * code assumes for closing loop only
1507 } else {
1508 lpWaveHdr = lpWaveHdr->lpNext;
1510 wwo->lpLoopPtr = NULL;
1511 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1513 } else {
1514 /* We're not in a loop. Advance to the next wave header */
1515 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1518 return lpWaveHdr;
1521 /**************************************************************************
1522 * wodPlayer_DSPWait [internal]
1523 * Returns the number of milliseconds to wait for the DSP buffer to write
1524 * one fragment.
1526 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1528 /* time for one fragment to be played */
1529 return wwo->dwFragmentSize * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1532 /**************************************************************************
1533 * wodPlayer_NotifyWait [internal]
1534 * Returns the number of milliseconds to wait before attempting to notify
1535 * completion of the specified wavehdr.
1536 * This is based on the number of bytes remaining to be written in the
1537 * wave.
1539 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1541 DWORD dwMillis;
1543 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1544 dwMillis = 1;
1545 } else {
1546 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
1547 if (!dwMillis) dwMillis = 1;
1550 return dwMillis;
1554 /**************************************************************************
1555 * wodPlayer_WriteMaxFrags [internal]
1556 * Writes the maximum number of bytes possible to the DSP and returns
1557 * TRUE iff the current playPtr has been fully played
1559 static BOOL wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
1561 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1562 DWORD toWrite = min(dwLength, *bytes);
1563 int written;
1564 BOOL ret = FALSE;
1566 TRACE("Writing wavehdr %p.%lu[%lu]/%lu\n",
1567 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength, toWrite);
1569 if (toWrite > 0)
1571 written = write(wwo->ossdev->fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite);
1572 if (written <= 0) {
1573 TRACE("write(%s, %p, %ld) failed (%s) returned %d\n", wwo->ossdev->dev_name,
1574 wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toWrite, strerror(errno), written);
1575 return FALSE;
1578 else
1579 written = 0;
1581 if (written >= dwLength) {
1582 /* If we wrote all current wavehdr, skip to the next one */
1583 wodPlayer_PlayPtrNext(wwo);
1584 ret = TRUE;
1585 } else {
1586 /* Remove the amount written */
1587 wwo->dwPartialOffset += written;
1589 *bytes -= written;
1590 wwo->dwWrittenTotal += written;
1591 TRACE("dwWrittenTotal=%lu\n", wwo->dwWrittenTotal);
1592 return ret;
1596 /**************************************************************************
1597 * wodPlayer_NotifyCompletions [internal]
1599 * Notifies and remove from queue all wavehdrs which have been played to
1600 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1601 * we notify all wavehdrs and remove them all from the queue even if they
1602 * are unplayed or part of a loop.
1604 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1606 LPWAVEHDR lpWaveHdr;
1608 /* Start from lpQueuePtr and keep notifying until:
1609 * - we hit an unwritten wavehdr
1610 * - we hit the beginning of a running loop
1611 * - we hit a wavehdr which hasn't finished playing
1613 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1614 (force ||
1615 (lpWaveHdr != wwo->lpPlayPtr &&
1616 lpWaveHdr != wwo->lpLoopPtr &&
1617 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1619 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1621 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1622 lpWaveHdr->dwFlags |= WHDR_DONE;
1624 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1626 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1627 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1630 /**************************************************************************
1631 * wodPlayer_Reset [internal]
1633 * wodPlayer helper. Resets current output stream.
1635 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
1637 wodUpdatePlayedTotal(wwo, NULL);
1638 /* updates current notify list */
1639 wodPlayer_NotifyCompletions(wwo, FALSE);
1641 /* flush all possible output */
1642 if (OSS_ResetDevice(wwo->ossdev) != MMSYSERR_NOERROR)
1644 wwo->hThread = 0;
1645 wwo->state = WINE_WS_STOPPED;
1646 ExitThread(-1);
1649 if (reset) {
1650 enum win_wm_message msg;
1651 DWORD param;
1652 HANDLE ev;
1654 /* remove any buffer */
1655 wodPlayer_NotifyCompletions(wwo, TRUE);
1657 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1658 wwo->state = WINE_WS_STOPPED;
1659 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1660 /* Clear partial wavehdr */
1661 wwo->dwPartialOffset = 0;
1663 /* remove any existing message in the ring */
1664 EnterCriticalSection(&wwo->msgRing.msg_crst);
1665 /* return all pending headers in queue */
1666 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1668 if (msg != WINE_WM_HEADER)
1670 FIXME("shouldn't have headers left\n");
1671 SetEvent(ev);
1672 continue;
1674 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1675 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1677 wodNotifyClient(wwo, WOM_DONE, param, 0);
1679 RESET_OMR(&wwo->msgRing);
1680 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1681 } else {
1682 if (wwo->lpLoopPtr) {
1683 /* complicated case, not handled yet (could imply modifying the loop counter */
1684 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1685 wwo->lpPlayPtr = wwo->lpLoopPtr;
1686 wwo->dwPartialOffset = 0;
1687 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
1688 } else {
1689 LPWAVEHDR ptr;
1690 DWORD sz = wwo->dwPartialOffset;
1692 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1693 /* compute the max size playable from lpQueuePtr */
1694 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
1695 sz += ptr->dwBufferLength;
1697 /* because the reset lpPlayPtr will be lpQueuePtr */
1698 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
1699 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
1700 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
1701 wwo->lpPlayPtr = wwo->lpQueuePtr;
1703 wwo->state = WINE_WS_PAUSED;
1707 /**************************************************************************
1708 * wodPlayer_ProcessMessages [internal]
1710 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1712 LPWAVEHDR lpWaveHdr;
1713 enum win_wm_message msg;
1714 DWORD param;
1715 HANDLE ev;
1717 while (OSS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1718 TRACE("Received %s %lx\n", getCmdString(msg), param);
1719 switch (msg) {
1720 case WINE_WM_PAUSING:
1721 wodPlayer_Reset(wwo, FALSE);
1722 SetEvent(ev);
1723 break;
1724 case WINE_WM_RESTARTING:
1725 if (wwo->state == WINE_WS_PAUSED)
1727 wwo->state = WINE_WS_PLAYING;
1729 SetEvent(ev);
1730 break;
1731 case WINE_WM_HEADER:
1732 lpWaveHdr = (LPWAVEHDR)param;
1734 /* insert buffer at the end of queue */
1736 LPWAVEHDR* wh;
1737 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1738 *wh = lpWaveHdr;
1740 if (!wwo->lpPlayPtr)
1741 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1742 if (wwo->state == WINE_WS_STOPPED)
1743 wwo->state = WINE_WS_PLAYING;
1744 break;
1745 case WINE_WM_RESETTING:
1746 wodPlayer_Reset(wwo, TRUE);
1747 SetEvent(ev);
1748 break;
1749 case WINE_WM_UPDATE:
1750 wodUpdatePlayedTotal(wwo, NULL);
1751 SetEvent(ev);
1752 break;
1753 case WINE_WM_BREAKLOOP:
1754 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1755 /* ensure exit at end of current loop */
1756 wwo->dwLoops = 1;
1758 SetEvent(ev);
1759 break;
1760 case WINE_WM_CLOSING:
1761 /* sanity check: this should not happen since the device must have been reset before */
1762 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1763 wwo->hThread = 0;
1764 wwo->state = WINE_WS_CLOSED;
1765 SetEvent(ev);
1766 ExitThread(0);
1767 /* shouldn't go here */
1768 default:
1769 FIXME("unknown message %d\n", msg);
1770 break;
1775 /**************************************************************************
1776 * wodPlayer_FeedDSP [internal]
1777 * Feed as much sound data as we can into the DSP and return the number of
1778 * milliseconds before it will be necessary to feed the DSP again.
1780 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1782 audio_buf_info dspspace;
1783 DWORD availInQ;
1785 wodUpdatePlayedTotal(wwo, &dspspace);
1786 availInQ = dspspace.bytes;
1787 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1788 dspspace.fragments, dspspace.fragstotal, dspspace.fragsize, dspspace.bytes);
1790 /* input queue empty and output buffer with less than one fragment to play
1791 * actually some cards do not play the fragment before the last if this one is partially feed
1792 * so we need to test for full the availability of 2 fragments
1794 if (!wwo->lpPlayPtr && wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize &&
1795 !wwo->bNeedPost) {
1796 TRACE("Run out of wavehdr:s...\n");
1797 return INFINITE;
1800 /* no more room... no need to try to feed */
1801 if (dspspace.fragments != 0) {
1802 /* Feed from partial wavehdr */
1803 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1804 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1807 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1808 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1809 do {
1810 TRACE("Setting time to elapse for %p to %lu\n",
1811 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1812 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1813 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1814 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1817 if (wwo->bNeedPost) {
1818 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1819 * if it didn't get one, we give it the other */
1820 if (wwo->dwBufferSize < availInQ + 2 * wwo->dwFragmentSize)
1821 ioctl(wwo->ossdev->fd, SNDCTL_DSP_POST, 0);
1822 wwo->bNeedPost = FALSE;
1826 return wodPlayer_DSPWait(wwo);
1830 /**************************************************************************
1831 * wodPlayer [internal]
1833 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1835 WORD uDevID = (DWORD)pmt;
1836 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1837 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1838 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1839 DWORD dwSleepTime;
1841 wwo->state = WINE_WS_STOPPED;
1842 SetEvent(wwo->hStartUpEvent);
1844 for (;;) {
1845 /** Wait for the shortest time before an action is required. If there
1846 * are no pending actions, wait forever for a command.
1848 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1849 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1850 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1851 wodPlayer_ProcessMessages(wwo);
1852 if (wwo->state == WINE_WS_PLAYING) {
1853 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1854 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1855 if (dwNextFeedTime == INFINITE) {
1856 /* FeedDSP ran out of data, but before flushing, */
1857 /* check that a notification didn't give us more */
1858 wodPlayer_ProcessMessages(wwo);
1859 if (!wwo->lpPlayPtr) {
1860 TRACE("flushing\n");
1861 ioctl(wwo->ossdev->fd, SNDCTL_DSP_SYNC, 0);
1862 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
1863 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1864 } else {
1865 TRACE("recovering\n");
1866 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1869 } else {
1870 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1875 /**************************************************************************
1876 * wodGetDevCaps [internal]
1878 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1880 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1882 if (lpCaps == NULL) {
1883 WARN("not enabled\n");
1884 return MMSYSERR_NOTENABLED;
1887 if (wDevID >= numOutDev) {
1888 WARN("numOutDev reached !\n");
1889 return MMSYSERR_BADDEVICEID;
1892 if (WOutDev[wDevID].ossdev->open_access == O_RDWR)
1893 memcpy(lpCaps, &WOutDev[wDevID].ossdev->duplex_out_caps, min(dwSize, sizeof(*lpCaps)));
1894 else
1895 memcpy(lpCaps, &WOutDev[wDevID].ossdev->out_caps, min(dwSize, sizeof(*lpCaps)));
1897 return MMSYSERR_NOERROR;
1900 /**************************************************************************
1901 * wodOpen [internal]
1903 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1905 int audio_fragment;
1906 WINE_WAVEOUT* wwo;
1907 audio_buf_info info;
1908 DWORD ret;
1910 TRACE("(%u, %p[cb=%08lx], %08lX);\n", wDevID, lpDesc, lpDesc->dwCallback, dwFlags);
1911 if (lpDesc == NULL) {
1912 WARN("Invalid Parameter !\n");
1913 return MMSYSERR_INVALPARAM;
1915 if (wDevID >= numOutDev) {
1916 TRACE("MAX_WAVOUTDRV reached !\n");
1917 return MMSYSERR_BADDEVICEID;
1920 /* only PCM format is supported so far... */
1921 if (!supportedFormat(lpDesc->lpFormat)) {
1922 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1923 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1924 lpDesc->lpFormat->nSamplesPerSec);
1925 return WAVERR_BADFORMAT;
1928 if (dwFlags & WAVE_FORMAT_QUERY) {
1929 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1930 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1931 lpDesc->lpFormat->nSamplesPerSec);
1932 return MMSYSERR_NOERROR;
1935 TRACE("OSS_OpenDevice requested this format: %ldx%dx%d %s\n",
1936 lpDesc->lpFormat->nSamplesPerSec,
1937 lpDesc->lpFormat->wBitsPerSample,
1938 lpDesc->lpFormat->nChannels,
1939 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
1940 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
1941 "UNSUPPORTED");
1943 wwo = &WOutDev[wDevID];
1945 if ((dwFlags & WAVE_DIRECTSOUND) &&
1946 !(wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_DIRECTSOUND))
1947 /* not supported, ignore it */
1948 dwFlags &= ~WAVE_DIRECTSOUND;
1950 if (dwFlags & WAVE_DIRECTSOUND) {
1951 if (wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1952 /* we have realtime DirectSound, fragments just waste our time,
1953 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1954 audio_fragment = 0x0020000B;
1955 else
1956 /* to approximate realtime, we must use small fragments,
1957 * let's try to fragment the above 64KB (256 * 2^8) */
1958 audio_fragment = 0x01000008;
1959 } else {
1960 /* A wave device must have a worst case latency of 10 ms so calculate
1961 * the largest fragment size less than 10 ms long.
1963 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
1964 int shift = 0;
1965 while ((1 << shift) <= fsize)
1966 shift++;
1967 shift--;
1968 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
1971 TRACE("requesting %d %d byte fragments (%ld ms/fragment)\n",
1972 audio_fragment >> 16, 1 << (audio_fragment & 0xffff),
1973 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
1975 if (wwo->state != WINE_WS_CLOSED) {
1976 WARN("already allocated\n");
1977 return MMSYSERR_ALLOCATED;
1980 /* we want to be able to mmap() the device, which means it must be opened readable,
1981 * otherwise mmap() will fail (at least under Linux) */
1982 ret = OSS_OpenDevice(wwo->ossdev,
1983 (dwFlags & WAVE_DIRECTSOUND) ? O_RDWR : O_WRONLY,
1984 &audio_fragment,
1985 (dwFlags & WAVE_DIRECTSOUND) ? 0 : 1,
1986 lpDesc->lpFormat->nSamplesPerSec,
1987 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
1988 (lpDesc->lpFormat->wBitsPerSample == 16)
1989 ? AFMT_S16_LE : AFMT_U8);
1990 if ((ret==MMSYSERR_NOERROR) && (dwFlags & WAVE_DIRECTSOUND)) {
1991 lpDesc->lpFormat->nSamplesPerSec=wwo->ossdev->sample_rate;
1992 lpDesc->lpFormat->nChannels=(wwo->ossdev->stereo ? 2 : 1);
1993 lpDesc->lpFormat->wBitsPerSample=(wwo->ossdev->format == AFMT_U8 ? 8 : 16);
1994 lpDesc->lpFormat->nBlockAlign=lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
1995 lpDesc->lpFormat->nAvgBytesPerSec=lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
1996 TRACE("OSS_OpenDevice returned this format: %ldx%dx%d\n",
1997 lpDesc->lpFormat->nSamplesPerSec,
1998 lpDesc->lpFormat->wBitsPerSample,
1999 lpDesc->lpFormat->nChannels);
2001 if (ret != 0) return ret;
2002 wwo->state = WINE_WS_STOPPED;
2004 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2006 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2007 copy_format(lpDesc->lpFormat, &wwo->waveFormat);
2009 if (wwo->waveFormat.Format.wBitsPerSample == 0) {
2010 WARN("Resetting zeroed wBitsPerSample\n");
2011 wwo->waveFormat.Format.wBitsPerSample = 8 *
2012 (wwo->waveFormat.Format.nAvgBytesPerSec /
2013 wwo->waveFormat.Format.nSamplesPerSec) /
2014 wwo->waveFormat.Format.nChannels;
2016 /* Read output space info for future reference */
2017 if (ioctl(wwo->ossdev->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
2018 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo->ossdev->dev_name, strerror(errno));
2019 OSS_CloseDevice(wwo->ossdev);
2020 wwo->state = WINE_WS_CLOSED;
2021 return MMSYSERR_NOTENABLED;
2024 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
2025 info.fragsize, (info.fragsize * 1000) / (wwo->ossdev->sample_rate *
2026 (wwo->ossdev->stereo ? 2 : 1) *
2027 (wwo->ossdev->format == AFMT_U8 ? 1 : 2)));
2029 /* Check that fragsize is correct per our settings above */
2030 if ((info.fragsize > 1024) && (LOWORD(audio_fragment) <= 10)) {
2031 /* we've tried to set 1K fragments or less, but it didn't work */
2032 ERR("fragment size set failed, size is now %d\n", info.fragsize);
2033 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
2034 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
2037 /* Remember fragsize and total buffer size for future use */
2038 wwo->dwFragmentSize = info.fragsize;
2039 wwo->dwBufferSize = info.fragstotal * info.fragsize;
2040 wwo->dwPlayedTotal = 0;
2041 wwo->dwWrittenTotal = 0;
2042 wwo->bNeedPost = TRUE;
2044 TRACE("fd=%d fragstotal=%d fragsize=%d BufferSize=%ld\n",
2045 wwo->ossdev->fd, info.fragstotal, info.fragsize, wwo->dwBufferSize);
2046 if (wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign) {
2047 ERR("Fragment doesn't contain an integral number of data blocks fragsize=%ld BlockAlign=%d\n",wwo->dwFragmentSize,wwo->waveFormat.Format.nBlockAlign);
2048 /* Some SoundBlaster 16 cards return an incorrect (odd) fragment
2049 * size for 16 bit sound. This will cause a system crash when we try
2050 * to write just the specified odd number of bytes. So if we
2051 * detect something is wrong we'd better fix it.
2053 wwo->dwFragmentSize-=wwo->dwFragmentSize % wwo->waveFormat.Format.nBlockAlign;
2056 OSS_InitRingMessage(&wwo->msgRing);
2058 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
2059 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
2060 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
2061 CloseHandle(wwo->hStartUpEvent);
2062 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
2064 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2065 wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
2066 wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
2067 wwo->waveFormat.Format.nBlockAlign);
2069 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
2072 /**************************************************************************
2073 * wodClose [internal]
2075 static DWORD wodClose(WORD wDevID)
2077 DWORD ret = MMSYSERR_NOERROR;
2078 WINE_WAVEOUT* wwo;
2080 TRACE("(%u);\n", wDevID);
2082 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2083 WARN("bad device ID !\n");
2084 return MMSYSERR_BADDEVICEID;
2087 wwo = &WOutDev[wDevID];
2088 if (wwo->lpQueuePtr) {
2089 WARN("buffers still playing !\n");
2090 ret = WAVERR_STILLPLAYING;
2091 } else {
2092 if (wwo->hThread != INVALID_HANDLE_VALUE) {
2093 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
2096 OSS_DestroyRingMessage(&wwo->msgRing);
2098 OSS_CloseDevice(wwo->ossdev);
2099 wwo->state = WINE_WS_CLOSED;
2100 wwo->dwFragmentSize = 0;
2101 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
2103 return ret;
2106 /**************************************************************************
2107 * wodWrite [internal]
2110 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2112 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2114 /* first, do the sanity checks... */
2115 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2116 WARN("bad dev ID !\n");
2117 return MMSYSERR_BADDEVICEID;
2120 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
2121 return WAVERR_UNPREPARED;
2123 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2124 return WAVERR_STILLPLAYING;
2126 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2127 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2128 lpWaveHdr->lpNext = 0;
2130 if ((lpWaveHdr->dwBufferLength & (WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1)) != 0)
2132 WARN("WaveHdr length isn't a multiple of the PCM block size: %ld %% %d\n",lpWaveHdr->dwBufferLength,WOutDev[wDevID].waveFormat.Format.nBlockAlign);
2133 lpWaveHdr->dwBufferLength &= ~(WOutDev[wDevID].waveFormat.Format.nBlockAlign - 1);
2136 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2138 return MMSYSERR_NOERROR;
2141 /**************************************************************************
2142 * wodPrepare [internal]
2144 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2146 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2148 if (wDevID >= numOutDev) {
2149 WARN("bad device ID !\n");
2150 return MMSYSERR_BADDEVICEID;
2153 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2154 return WAVERR_STILLPLAYING;
2156 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2157 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2158 return MMSYSERR_NOERROR;
2161 /**************************************************************************
2162 * wodUnprepare [internal]
2164 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2166 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2168 if (wDevID >= numOutDev) {
2169 WARN("bad device ID !\n");
2170 return MMSYSERR_BADDEVICEID;
2173 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2174 return WAVERR_STILLPLAYING;
2176 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
2177 lpWaveHdr->dwFlags |= WHDR_DONE;
2179 return MMSYSERR_NOERROR;
2182 /**************************************************************************
2183 * wodPause [internal]
2185 static DWORD wodPause(WORD wDevID)
2187 TRACE("(%u);!\n", wDevID);
2189 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2190 WARN("bad device ID !\n");
2191 return MMSYSERR_BADDEVICEID;
2194 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
2196 return MMSYSERR_NOERROR;
2199 /**************************************************************************
2200 * wodRestart [internal]
2202 static DWORD wodRestart(WORD wDevID)
2204 TRACE("(%u);\n", wDevID);
2206 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2207 WARN("bad device ID !\n");
2208 return MMSYSERR_BADDEVICEID;
2211 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2213 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2214 /* FIXME: Myst crashes with this ... hmm -MM
2215 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2218 return MMSYSERR_NOERROR;
2221 /**************************************************************************
2222 * wodReset [internal]
2224 static DWORD wodReset(WORD wDevID)
2226 TRACE("(%u);\n", wDevID);
2228 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2229 WARN("bad device ID !\n");
2230 return MMSYSERR_BADDEVICEID;
2233 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2235 return MMSYSERR_NOERROR;
2238 /**************************************************************************
2239 * wodGetPosition [internal]
2241 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2243 WINE_WAVEOUT* wwo;
2245 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
2247 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2248 WARN("bad device ID !\n");
2249 return MMSYSERR_BADDEVICEID;
2252 if (lpTime == NULL) {
2253 WARN("invalid parameter: lpTime == NULL\n");
2254 return MMSYSERR_INVALPARAM;
2257 wwo = &WOutDev[wDevID];
2258 #ifdef EXACT_WODPOSITION
2259 if (wwo->ossdev->open_access == O_RDWR) {
2260 if (wwo->ossdev->duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2261 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2262 } else {
2263 if (wwo->ossdev->out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2264 OSS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2266 #endif
2268 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
2271 /**************************************************************************
2272 * wodBreakLoop [internal]
2274 static DWORD wodBreakLoop(WORD wDevID)
2276 TRACE("(%u);\n", wDevID);
2278 if (wDevID >= numOutDev || WOutDev[wDevID].state == WINE_WS_CLOSED) {
2279 WARN("bad device ID !\n");
2280 return MMSYSERR_BADDEVICEID;
2282 OSS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
2283 return MMSYSERR_NOERROR;
2286 /**************************************************************************
2287 * wodGetVolume [internal]
2289 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
2291 int mixer;
2292 int volume;
2293 DWORD left, right;
2294 DWORD last_left, last_right;
2296 TRACE("(%u, %p);\n", wDevID, lpdwVol);
2298 if (lpdwVol == NULL) {
2299 WARN("not enabled\n");
2300 return MMSYSERR_NOTENABLED;
2302 if (wDevID >= numOutDev) {
2303 WARN("invalid parameter\n");
2304 return MMSYSERR_INVALPARAM;
2307 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_RDONLY|O_NDELAY)) < 0) {
2308 WARN("mixer device not available !\n");
2309 return MMSYSERR_NOTENABLED;
2311 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
2312 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)\n",
2313 WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2314 return MMSYSERR_NOTENABLED;
2316 close(mixer);
2318 left = LOBYTE(volume);
2319 right = HIBYTE(volume);
2320 TRACE("left=%ld right=%ld !\n", left, right);
2321 last_left = (LOWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2322 last_right = (HIWORD(WOutDev[wDevID].volume) * 100) / 0xFFFFl;
2323 TRACE("last_left=%ld last_right=%ld !\n", last_left, last_right);
2324 if (last_left == left && last_right == right)
2325 *lpdwVol = WOutDev[wDevID].volume;
2326 else
2327 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
2328 return MMSYSERR_NOERROR;
2331 /**************************************************************************
2332 * wodSetVolume [internal]
2334 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
2336 int mixer;
2337 int volume;
2338 DWORD left, right;
2340 TRACE("(%u, %08lX);\n", wDevID, dwParam);
2342 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
2343 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
2344 volume = left + (right << 8);
2346 if (wDevID >= numOutDev) {
2347 WARN("invalid parameter: wDevID > %d\n", numOutDev);
2348 return MMSYSERR_INVALPARAM;
2350 if ((mixer = open(WOutDev[wDevID].ossdev->mixer_name, O_WRONLY|O_NDELAY)) < 0) {
2351 WARN("mixer device not available !\n");
2352 return MMSYSERR_NOTENABLED;
2354 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
2355 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n",
2356 WOutDev[wDevID].ossdev->mixer_name, strerror(errno));
2357 return MMSYSERR_NOTENABLED;
2358 } else {
2359 TRACE("volume=%04x\n", (unsigned)volume);
2361 close(mixer);
2363 /* save requested volume */
2364 WOutDev[wDevID].volume = dwParam;
2366 return MMSYSERR_NOERROR;
2369 /**************************************************************************
2370 * wodMessage (WINEOSS.7)
2372 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2373 DWORD dwParam1, DWORD dwParam2)
2375 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
2376 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
2378 switch (wMsg) {
2379 case DRVM_INIT:
2380 case DRVM_EXIT:
2381 case DRVM_ENABLE:
2382 case DRVM_DISABLE:
2383 /* FIXME: Pretend this is supported */
2384 return 0;
2385 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2386 case WODM_CLOSE: return wodClose (wDevID);
2387 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2388 case WODM_PAUSE: return wodPause (wDevID);
2389 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
2390 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
2391 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2392 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2393 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
2394 case WODM_GETNUMDEVS: return numOutDev;
2395 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
2396 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
2397 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2398 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2399 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
2400 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
2401 case WODM_RESTART: return wodRestart (wDevID);
2402 case WODM_RESET: return wodReset (wDevID);
2404 case DRV_QUERYDEVICEINTERFACESIZE: return wdDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2405 case DRV_QUERYDEVICEINTERFACE: return wdDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2406 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
2407 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2408 default:
2409 FIXME("unknown message %d!\n", wMsg);
2411 return MMSYSERR_NOTSUPPORTED;
2414 /*======================================================================*
2415 * Low level DSOUND definitions *
2416 *======================================================================*/
2418 typedef struct IDsDriverPropertySetImpl IDsDriverPropertySetImpl;
2419 typedef struct IDsDriverNotifyImpl IDsDriverNotifyImpl;
2420 typedef struct IDsDriverImpl IDsDriverImpl;
2421 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
2423 struct IDsDriverPropertySetImpl
2425 /* IUnknown fields */
2426 IDsDriverPropertySetVtbl *lpVtbl;
2427 DWORD ref;
2429 IDsDriverBufferImpl* buffer;
2432 struct IDsDriverNotifyImpl
2434 /* IUnknown fields */
2435 IDsDriverNotifyVtbl *lpVtbl;
2436 DWORD ref;
2438 /* IDsDriverNotifyImpl fields */
2439 LPDSBPOSITIONNOTIFY notifies;
2440 int nrofnotifies;
2442 IDsDriverBufferImpl* buffer;
2445 struct IDsDriverImpl
2447 /* IUnknown fields */
2448 IDsDriverVtbl *lpVtbl;
2449 DWORD ref;
2451 /* IDsDriverImpl fields */
2452 UINT wDevID;
2453 IDsDriverBufferImpl* primary;
2455 int nrofsecondaries;
2456 IDsDriverBufferImpl** secondaries;
2459 struct IDsDriverBufferImpl
2461 /* IUnknown fields */
2462 IDsDriverBufferVtbl *lpVtbl;
2463 DWORD ref;
2465 /* IDsDriverBufferImpl fields */
2466 IDsDriverImpl* drv;
2467 DWORD buflen;
2468 WAVEFORMATPCMEX wfex;
2469 LPBYTE mapping;
2470 DWORD maplen;
2471 int fd;
2472 DWORD dwFlags;
2474 /* IDsDriverNotifyImpl fields */
2475 IDsDriverNotifyImpl* notify;
2476 int notify_index;
2478 /* IDsDriverPropertySetImpl fields */
2479 IDsDriverPropertySetImpl* property_set;
2482 static HRESULT WINAPI IDsDriverPropertySetImpl_Create(
2483 IDsDriverBufferImpl * dsdb,
2484 IDsDriverPropertySetImpl **pdsdps);
2486 static HRESULT WINAPI IDsDriverNotifyImpl_Create(
2487 IDsDriverBufferImpl * dsdb,
2488 IDsDriverNotifyImpl **pdsdn);
2490 /*======================================================================*
2491 * Low level DSOUND property set implementation *
2492 *======================================================================*/
2494 static HRESULT WINAPI IDsDriverPropertySetImpl_QueryInterface(
2495 PIDSDRIVERPROPERTYSET iface,
2496 REFIID riid,
2497 LPVOID *ppobj)
2499 IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
2500 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
2502 if ( IsEqualGUID(riid, &IID_IUnknown) ||
2503 IsEqualGUID(riid, &IID_IDsDriverPropertySet) ) {
2504 IDsDriverPropertySet_AddRef(iface);
2505 *ppobj = (LPVOID)This;
2506 return DS_OK;
2509 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
2511 *ppobj = 0;
2512 return E_NOINTERFACE;
2515 static ULONG WINAPI IDsDriverPropertySetImpl_AddRef(PIDSDRIVERPROPERTYSET iface)
2517 IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
2518 TRACE("(%p) ref was %ld\n", This, This->ref);
2520 return InterlockedIncrement(&(This->ref));
2523 static ULONG WINAPI IDsDriverPropertySetImpl_Release(PIDSDRIVERPROPERTYSET iface)
2525 IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
2526 DWORD ref;
2527 TRACE("(%p) ref was %ld\n", This, This->ref);
2529 ref = InterlockedDecrement(&(This->ref));
2530 if (ref == 0) {
2531 IDsDriverBuffer_Release((PIDSDRIVERBUFFER)This->buffer);
2532 HeapFree(GetProcessHeap(),0,This);
2533 TRACE("(%p) released\n",This);
2535 return ref;
2538 static HRESULT WINAPI IDsDriverPropertySetImpl_Get(
2539 PIDSDRIVERPROPERTYSET iface,
2540 PDSPROPERTY pDsProperty,
2541 LPVOID pPropertyParams,
2542 ULONG cbPropertyParams,
2543 LPVOID pPropertyData,
2544 ULONG cbPropertyData,
2545 PULONG pcbReturnedData )
2547 IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
2548 FIXME("(%p,%p,%p,%lx,%p,%lx,%p)\n",This,pDsProperty,pPropertyParams,cbPropertyParams,pPropertyData,cbPropertyData,pcbReturnedData);
2549 return DSERR_UNSUPPORTED;
2552 static HRESULT WINAPI IDsDriverPropertySetImpl_Set(
2553 PIDSDRIVERPROPERTYSET iface,
2554 PDSPROPERTY pDsProperty,
2555 LPVOID pPropertyParams,
2556 ULONG cbPropertyParams,
2557 LPVOID pPropertyData,
2558 ULONG cbPropertyData )
2560 IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
2561 FIXME("(%p,%p,%p,%lx,%p,%lx)\n",This,pDsProperty,pPropertyParams,cbPropertyParams,pPropertyData,cbPropertyData);
2562 return DSERR_UNSUPPORTED;
2565 static HRESULT WINAPI IDsDriverPropertySetImpl_QuerySupport(
2566 PIDSDRIVERPROPERTYSET iface,
2567 REFGUID PropertySetId,
2568 ULONG PropertyId,
2569 PULONG pSupport )
2571 IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
2572 FIXME("(%p,%s,%lx,%p)\n",This,debugstr_guid(PropertySetId),PropertyId,pSupport);
2573 return DSERR_UNSUPPORTED;
2576 IDsDriverPropertySetVtbl dsdpsvt =
2578 IDsDriverPropertySetImpl_QueryInterface,
2579 IDsDriverPropertySetImpl_AddRef,
2580 IDsDriverPropertySetImpl_Release,
2581 IDsDriverPropertySetImpl_Get,
2582 IDsDriverPropertySetImpl_Set,
2583 IDsDriverPropertySetImpl_QuerySupport,
2586 /*======================================================================*
2587 * Low level DSOUND notify implementation *
2588 *======================================================================*/
2590 static HRESULT WINAPI IDsDriverNotifyImpl_QueryInterface(
2591 PIDSDRIVERNOTIFY iface,
2592 REFIID riid,
2593 LPVOID *ppobj)
2595 IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
2596 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
2598 if ( IsEqualGUID(riid, &IID_IUnknown) ||
2599 IsEqualGUID(riid, &IID_IDsDriverNotify) ) {
2600 IDsDriverNotify_AddRef(iface);
2601 *ppobj = This;
2602 return DS_OK;
2605 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
2607 *ppobj = 0;
2608 return E_NOINTERFACE;
2611 static ULONG WINAPI IDsDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface)
2613 IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
2614 TRACE("(%p) ref was %ld\n", This, This->ref);
2616 return InterlockedIncrement(&(This->ref));
2619 static ULONG WINAPI IDsDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface)
2621 IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
2622 DWORD ref;
2623 TRACE("(%p) ref was %ld\n", This, This->ref);
2625 ref = InterlockedDecrement(&(This->ref));
2626 if (ref == 0) {
2627 IDsDriverBuffer_Release((PIDSDRIVERBUFFER)This->buffer);
2628 if (This->notifies != NULL)
2629 HeapFree(GetProcessHeap(), 0, This->notifies);
2630 HeapFree(GetProcessHeap(),0,This);
2631 TRACE("(%p) released\n",This);
2633 return ref;
2636 static HRESULT WINAPI IDsDriverNotifyImpl_SetNotificationPositions(
2637 PIDSDRIVERNOTIFY iface,
2638 DWORD howmuch,
2639 LPCDSBPOSITIONNOTIFY notify)
2641 IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
2642 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
2644 if (!notify) {
2645 WARN("invalid parameter\n");
2646 return DSERR_INVALIDPARAM;
2649 if (TRACE_ON(wave)) {
2650 int i;
2651 for (i=0;i<howmuch;i++)
2652 TRACE("notify at %ld to 0x%08lx\n",
2653 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
2656 /* Make an internal copy of the caller-supplied array.
2657 * Replace the existing copy if one is already present. */
2658 if (This->notifies)
2659 This->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2660 This->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
2661 else
2662 This->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2663 howmuch * sizeof(DSBPOSITIONNOTIFY));
2665 memcpy(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
2666 This->nrofnotifies = howmuch;
2668 return S_OK;
2671 IDsDriverNotifyVtbl dsdnvt =
2673 IDsDriverNotifyImpl_QueryInterface,
2674 IDsDriverNotifyImpl_AddRef,
2675 IDsDriverNotifyImpl_Release,
2676 IDsDriverNotifyImpl_SetNotificationPositions,
2679 /*======================================================================*
2680 * Low level DSOUND implementation *
2681 *======================================================================*/
2683 static HRESULT DSDB_MapBuffer(IDsDriverBufferImpl *dsdb)
2685 TRACE("(%p), format=%ldx%dx%d\n", dsdb, dsdb->wfex.Format.nSamplesPerSec,
2686 dsdb->wfex.Format.wBitsPerSample, dsdb->wfex.Format.nChannels);
2687 if (!dsdb->mapping) {
2688 dsdb->mapping = mmap(NULL, dsdb->maplen, PROT_WRITE, MAP_SHARED,
2689 dsdb->fd, 0);
2690 if (dsdb->mapping == (LPBYTE)-1) {
2691 ERR("Could not map sound device for direct access (%s)\n", strerror(errno));
2692 ERR("Use: \"HardwareAcceleration\" = \"Emulation\" in the [dsound] section of your config file.\n");
2693 return DSERR_GENERIC;
2695 TRACE("The sound device has been mapped for direct access at %p, size=%ld\n", dsdb->mapping, dsdb->maplen);
2697 /* for some reason, es1371 and sblive! sometimes have junk in here.
2698 * clear it, or we get junk noise */
2699 /* some libc implementations are buggy: their memset reads from the buffer...
2700 * to work around it, we have to zero the block by hand. We don't do the expected:
2701 * memset(dsdb->mapping,0, dsdb->maplen);
2704 unsigned char* p1 = dsdb->mapping;
2705 unsigned len = dsdb->maplen;
2706 unsigned char silence = (dsdb->wfex.Format.wBitsPerSample == 8) ? 128 : 0;
2707 unsigned long ulsilence = (dsdb->wfex.Format.wBitsPerSample == 8) ? 0x80808080 : 0;
2709 if (len >= 16) /* so we can have at least a 4 long area to store... */
2711 /* the mmap:ed value is (at least) dword aligned
2712 * so, start filling the complete unsigned long:s
2714 int b = len >> 2;
2715 unsigned long* p4 = (unsigned long*)p1;
2717 while (b--) *p4++ = ulsilence;
2718 /* prepare for filling the rest */
2719 len &= 3;
2720 p1 = (unsigned char*)p4;
2722 /* in all cases, fill the remaining bytes */
2723 while (len-- != 0) *p1++ = silence;
2726 return DS_OK;
2729 static HRESULT DSDB_UnmapBuffer(IDsDriverBufferImpl *dsdb)
2731 TRACE("(%p)\n",dsdb);
2732 if (dsdb->mapping) {
2733 if (munmap(dsdb->mapping, dsdb->maplen) < 0) {
2734 ERR("(%p): Could not unmap sound device (%s)\n", dsdb, strerror(errno));
2735 return DSERR_GENERIC;
2737 dsdb->mapping = NULL;
2738 TRACE("(%p): sound device unmapped\n", dsdb);
2740 return DS_OK;
2743 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
2745 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
2746 TRACE("(%p,%s,%p)\n",iface,debugstr_guid(riid),*ppobj);
2748 if ( IsEqualGUID(riid, &IID_IUnknown) ||
2749 IsEqualGUID(riid, &IID_IDsDriverBuffer) ) {
2750 IDsDriverBuffer_AddRef(iface);
2751 *ppobj = (LPVOID)This;
2752 return DS_OK;
2755 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
2756 if (!This->notify)
2757 IDsDriverNotifyImpl_Create(This, &(This->notify));
2758 if (This->notify) {
2759 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
2760 *ppobj = (LPVOID)This->notify;
2761 return DS_OK;
2763 *ppobj = 0;
2764 return E_FAIL;
2767 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
2768 if (!This->property_set)
2769 IDsDriverPropertySetImpl_Create(This, &(This->property_set));
2770 if (This->property_set) {
2771 IDsDriverPropertySet_AddRef((PIDSDRIVERPROPERTYSET)This->property_set);
2772 *ppobj = (LPVOID)This->property_set;
2773 return DS_OK;
2775 *ppobj = 0;
2776 return E_FAIL;
2779 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
2781 *ppobj = 0;
2783 return E_NOINTERFACE;
2786 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
2788 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
2789 TRACE("(%p) ref was %ld\n", This, This->ref);
2791 return InterlockedIncrement(&(This->ref));
2794 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
2796 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
2797 DWORD ref;
2798 TRACE("(%p) ref was %ld\n", This, This->ref);
2800 ref = InterlockedDecrement(&(This->ref));
2801 if (ref)
2802 return ref;
2804 if (This == This->drv->primary)
2805 This->drv->primary = NULL;
2806 else {
2807 int i;
2808 for (i = 0; i < This->drv->nrofsecondaries; i++)
2809 if (This->drv->secondaries[i] == This)
2810 break;
2811 if (i < This->drv->nrofsecondaries) {
2812 /* Put the last buffer of the list in the (now empty) position */
2813 This->drv->secondaries[i] = This->drv->secondaries[This->drv->nrofsecondaries - 1];
2814 This->drv->nrofsecondaries--;
2815 This->drv->secondaries = HeapReAlloc(GetProcessHeap(),0,
2816 This->drv->secondaries,
2817 sizeof(PIDSDRIVERBUFFER)*This->drv->nrofsecondaries);
2818 TRACE("(%p) buffer count is now %d\n", This, This->drv->nrofsecondaries);
2821 WOutDev[This->drv->wDevID].ossdev->ds_caps.dwFreeHwMixingAllBuffers++;
2822 WOutDev[This->drv->wDevID].ossdev->ds_caps.dwFreeHwMixingStreamingBuffers++;
2825 DSDB_UnmapBuffer(This);
2826 HeapFree(GetProcessHeap(),0,This);
2827 TRACE("(%p) released\n",This);
2828 return 0;
2831 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
2832 LPVOID*ppvAudio1,LPDWORD pdwLen1,
2833 LPVOID*ppvAudio2,LPDWORD pdwLen2,
2834 DWORD dwWritePosition,DWORD dwWriteLen,
2835 DWORD dwFlags)
2837 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
2838 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
2839 * and that we don't support secondary buffers, this method will never be called */
2840 TRACE("(%p): stub\n",iface);
2841 return DSERR_UNSUPPORTED;
2844 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
2845 LPVOID pvAudio1,DWORD dwLen1,
2846 LPVOID pvAudio2,DWORD dwLen2)
2848 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
2849 TRACE("(%p): stub\n",iface);
2850 return DSERR_UNSUPPORTED;
2853 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
2854 LPWAVEFORMATEX pwfx)
2856 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
2858 TRACE("(%p,%p)\n",iface,pwfx);
2859 /* On our request (GetDriverDesc flags), DirectSound has by now used
2860 * waveOutClose/waveOutOpen to set the format...
2861 * unfortunately, this means our mmap() is now gone...
2862 * so we need to somehow signal to our DirectSound implementation
2863 * that it should completely recreate this HW buffer...
2864 * this unexpected error code should do the trick... */
2865 return DSERR_BUFFERLOST;
2868 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
2870 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
2871 TRACE("(%p,%ld): stub\n",iface,dwFreq);
2872 return DSERR_UNSUPPORTED;
2875 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
2877 DWORD vol;
2878 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
2879 TRACE("(%p,%p)\n",This,pVolPan);
2881 vol = pVolPan->dwTotalLeftAmpFactor | (pVolPan->dwTotalRightAmpFactor << 16);
2883 if (wodSetVolume(This->drv->wDevID, vol) != MMSYSERR_NOERROR) {
2884 WARN("wodSetVolume failed\n");
2885 return DSERR_INVALIDPARAM;
2888 return DS_OK;
2891 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
2893 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
2894 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
2895 return DSERR_UNSUPPORTED;
2898 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
2899 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
2901 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
2902 count_info info;
2903 DWORD ptr;
2905 TRACE("(%p)\n",iface);
2906 if (WOutDev[This->drv->wDevID].state == WINE_WS_CLOSED) {
2907 ERR("device not open, but accessing?\n");
2908 return DSERR_UNINITIALIZED;
2910 if (ioctl(This->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
2911 ERR("ioctl(%s, SNDCTL_DSP_GETOPTR) failed (%s)\n",
2912 WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2913 return DSERR_GENERIC;
2915 ptr = info.ptr & ~3; /* align the pointer, just in case */
2916 if (lpdwPlay) *lpdwPlay = ptr;
2917 if (lpdwWrite) {
2918 /* add some safety margin (not strictly necessary, but...) */
2919 if (WOutDev[This->drv->wDevID].ossdev->duplex_out_caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
2920 *lpdwWrite = ptr + 32;
2921 else
2922 *lpdwWrite = ptr + WOutDev[This->drv->wDevID].dwFragmentSize;
2923 while (*lpdwWrite > This->buflen)
2924 *lpdwWrite -= This->buflen;
2926 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay?*lpdwPlay:0, lpdwWrite?*lpdwWrite:0);
2927 return DS_OK;
2930 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
2932 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
2933 int enable;
2934 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
2935 WOutDev[This->drv->wDevID].ossdev->bOutputEnabled = TRUE;
2936 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
2937 if (ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2938 if (errno == EINVAL) {
2939 /* Don't give up yet. OSS trigger support is inconsistent. */
2940 if (WOutDev[This->drv->wDevID].ossdev->open_count == 1) {
2941 /* try the opposite input enable */
2942 if (WOutDev[This->drv->wDevID].ossdev->bInputEnabled == FALSE)
2943 WOutDev[This->drv->wDevID].ossdev->bInputEnabled = TRUE;
2944 else
2945 WOutDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
2946 /* try it again */
2947 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
2948 if (ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &enable) >= 0)
2949 return DS_OK;
2952 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",
2953 WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2954 WOutDev[This->drv->wDevID].ossdev->bOutputEnabled = FALSE;
2955 return DSERR_GENERIC;
2957 return DS_OK;
2960 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
2962 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
2963 int enable;
2964 TRACE("(%p)\n",iface);
2965 /* no more playing */
2966 WOutDev[This->drv->wDevID].ossdev->bOutputEnabled = FALSE;
2967 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
2968 if (ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
2969 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2970 return DSERR_GENERIC;
2972 #if 0
2973 /* the play position must be reset to the beginning of the buffer */
2974 if (ioctl(This->fd, SNDCTL_DSP_RESET, 0) < 0) {
2975 ERR("ioctl(%s, SNDCTL_DSP_RESET) failed (%s)\n", WOutDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
2976 return DSERR_GENERIC;
2978 #endif
2979 /* Most OSS drivers just can't stop the playback without closing the device...
2980 * so we need to somehow signal to our DirectSound implementation
2981 * that it should completely recreate this HW buffer...
2982 * this unexpected error code should do the trick... */
2983 /* FIXME: ...unless we are doing full duplex, then it's not nice to close the device */
2984 if (WOutDev[This->drv->wDevID].ossdev->open_count == 1)
2985 return DSERR_BUFFERLOST;
2987 return DS_OK;
2990 static IDsDriverBufferVtbl dsdbvt =
2992 IDsDriverBufferImpl_QueryInterface,
2993 IDsDriverBufferImpl_AddRef,
2994 IDsDriverBufferImpl_Release,
2995 IDsDriverBufferImpl_Lock,
2996 IDsDriverBufferImpl_Unlock,
2997 IDsDriverBufferImpl_SetFormat,
2998 IDsDriverBufferImpl_SetFrequency,
2999 IDsDriverBufferImpl_SetVolumePan,
3000 IDsDriverBufferImpl_SetPosition,
3001 IDsDriverBufferImpl_GetPosition,
3002 IDsDriverBufferImpl_Play,
3003 IDsDriverBufferImpl_Stop
3006 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
3008 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3009 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
3011 if ( IsEqualGUID(riid, &IID_IUnknown) ||
3012 IsEqualGUID(riid, &IID_IDsDriver) ) {
3013 IDsDriver_AddRef(iface);
3014 *ppobj = (LPVOID)This;
3015 return DS_OK;
3018 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
3020 *ppobj = 0;
3022 return E_NOINTERFACE;
3025 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
3027 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3028 TRACE("(%p) ref was %ld\n", This, This->ref);
3030 return InterlockedIncrement(&(This->ref));
3033 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
3035 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3036 DWORD ref;
3037 TRACE("(%p) ref was %ld\n", This, This->ref);
3039 ref = InterlockedDecrement(&(This->ref));
3040 if (ref == 0) {
3041 HeapFree(GetProcessHeap(),0,This);
3042 TRACE("(%p) released\n",This);
3044 return ref;
3047 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface,
3048 PDSDRIVERDESC pDesc)
3050 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3051 TRACE("(%p,%p)\n",iface,pDesc);
3053 /* copy version from driver */
3054 memcpy(pDesc, &(WOutDev[This->wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
3056 pDesc->dwFlags |= DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
3057 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK |
3058 DSDDESC_DONTNEEDSECONDARYLOCK;
3059 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
3060 pDesc->wVxdId = 0;
3061 pDesc->wReserved = 0;
3062 pDesc->ulDeviceNum = This->wDevID;
3063 pDesc->dwHeapType = DSDHEAP_NOHEAP;
3064 pDesc->pvDirectDrawHeap = NULL;
3065 pDesc->dwMemStartAddress = 0;
3066 pDesc->dwMemEndAddress = 0;
3067 pDesc->dwMemAllocExtra = 0;
3068 pDesc->pvReserved1 = NULL;
3069 pDesc->pvReserved2 = NULL;
3070 return DS_OK;
3073 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
3075 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3076 int enable;
3077 TRACE("(%p)\n",iface);
3079 /* make sure the card doesn't start playing before we want it to */
3080 WOutDev[This->wDevID].ossdev->bOutputEnabled = FALSE;
3081 enable = getEnables(WOutDev[This->wDevID].ossdev);
3082 if (ioctl(WOutDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3083 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev[This->wDevID].ossdev->dev_name, strerror(errno));
3084 return DSERR_GENERIC;
3086 return DS_OK;
3089 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
3091 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3092 TRACE("(%p)\n",iface);
3093 if (This->primary) {
3094 ERR("problem with DirectSound: primary not released\n");
3095 return DSERR_GENERIC;
3097 return DS_OK;
3100 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
3102 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3103 TRACE("(%p,%p)\n",iface,pCaps);
3104 memcpy(pCaps, &(WOutDev[This->wDevID].ossdev->ds_caps), sizeof(DSDRIVERCAPS));
3105 return DS_OK;
3108 static HRESULT WINAPI DSD_CreatePrimaryBuffer(PIDSDRIVER iface,
3109 LPWAVEFORMATEX pwfx,
3110 DWORD dwFlags,
3111 DWORD dwCardAddress,
3112 LPDWORD pdwcbBufferSize,
3113 LPBYTE *ppbBuffer,
3114 LPVOID *ppvObj)
3116 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3117 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
3118 HRESULT err;
3119 audio_buf_info info;
3120 int enable = 0;
3121 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",iface,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
3123 if (This->primary)
3124 return DSERR_ALLOCATED;
3125 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
3126 return DSERR_CONTROLUNAVAIL;
3128 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsDriverBufferImpl));
3129 if (*ippdsdb == NULL)
3130 return DSERR_OUTOFMEMORY;
3131 (*ippdsdb)->lpVtbl = &dsdbvt;
3132 (*ippdsdb)->ref = 1;
3133 (*ippdsdb)->drv = This;
3134 copy_format(pwfx, &(*ippdsdb)->wfex);
3135 (*ippdsdb)->fd = WOutDev[This->wDevID].ossdev->fd;
3136 (*ippdsdb)->dwFlags = dwFlags;
3138 /* check how big the DMA buffer is now */
3139 if (ioctl((*ippdsdb)->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
3140 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n",
3141 WOutDev[This->wDevID].ossdev->dev_name, strerror(errno));
3142 HeapFree(GetProcessHeap(),0,*ippdsdb);
3143 *ippdsdb = NULL;
3144 return DSERR_GENERIC;
3146 (*ippdsdb)->maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
3148 /* map the DMA buffer */
3149 err = DSDB_MapBuffer(*ippdsdb);
3150 if (err != DS_OK) {
3151 HeapFree(GetProcessHeap(),0,*ippdsdb);
3152 *ippdsdb = NULL;
3153 return err;
3156 /* primary buffer is ready to go */
3157 *pdwcbBufferSize = (*ippdsdb)->maplen;
3158 *ppbBuffer = (*ippdsdb)->mapping;
3160 /* some drivers need some extra nudging after mapping */
3161 WOutDev[This->wDevID].ossdev->bOutputEnabled = FALSE;
3162 enable = getEnables(WOutDev[This->wDevID].ossdev);
3163 if (ioctl((*ippdsdb)->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3164 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",
3165 WOutDev[This->wDevID].ossdev->dev_name, strerror(errno));
3166 return DSERR_GENERIC;
3169 This->primary = *ippdsdb;
3171 return DS_OK;
3174 static HRESULT WINAPI DSD_CreateSecondaryBuffer(PIDSDRIVER iface,
3175 LPWAVEFORMATEX pwfx,
3176 DWORD dwFlags,
3177 DWORD dwCardAddress,
3178 LPDWORD pdwcbBufferSize,
3179 LPBYTE *ppbBuffer,
3180 LPVOID *ppvObj)
3182 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3183 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
3184 FIXME("(%p,%p,%lx,%lx,%p,%p,%p): stub\n",This,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
3186 *ippdsdb = 0;
3187 return DSERR_UNSUPPORTED;
3190 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
3191 LPWAVEFORMATEX pwfx,
3192 DWORD dwFlags,
3193 DWORD dwCardAddress,
3194 LPDWORD pdwcbBufferSize,
3195 LPBYTE *ppbBuffer,
3196 LPVOID *ppvObj)
3198 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",iface,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
3200 if (dwFlags & DSBCAPS_PRIMARYBUFFER)
3201 return DSD_CreatePrimaryBuffer(iface,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
3203 return DSD_CreateSecondaryBuffer(iface,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
3206 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
3207 PIDSDRIVERBUFFER pBuffer,
3208 LPVOID *ppvObj)
3210 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
3211 TRACE("(%p,%p): stub\n",iface,pBuffer);
3212 return DSERR_INVALIDCALL;
3215 static IDsDriverVtbl dsdvt =
3217 IDsDriverImpl_QueryInterface,
3218 IDsDriverImpl_AddRef,
3219 IDsDriverImpl_Release,
3220 IDsDriverImpl_GetDriverDesc,
3221 IDsDriverImpl_Open,
3222 IDsDriverImpl_Close,
3223 IDsDriverImpl_GetCaps,
3224 IDsDriverImpl_CreateSoundBuffer,
3225 IDsDriverImpl_DuplicateSoundBuffer
3228 static HRESULT WINAPI IDsDriverPropertySetImpl_Create(
3229 IDsDriverBufferImpl * dsdb,
3230 IDsDriverPropertySetImpl **pdsdps)
3232 IDsDriverPropertySetImpl * dsdps;
3233 TRACE("(%p,%p)\n",dsdb,pdsdps);
3235 dsdps = (IDsDriverPropertySetImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsdps));
3236 if (dsdps == NULL) {
3237 WARN("out of memory\n");
3238 return DSERR_OUTOFMEMORY;
3241 dsdps->ref = 0;
3242 dsdps->lpVtbl = &dsdpsvt;
3243 dsdps->buffer = dsdb;
3244 dsdb->property_set = dsdps;
3245 IDsDriverBuffer_AddRef((PIDSDRIVER)dsdb);
3247 *pdsdps = dsdps;
3248 return DS_OK;
3251 static HRESULT WINAPI IDsDriverNotifyImpl_Create(
3252 IDsDriverBufferImpl * dsdb,
3253 IDsDriverNotifyImpl **pdsdn)
3255 IDsDriverNotifyImpl * dsdn;
3256 TRACE("(%p,%p)\n",dsdb,pdsdn);
3258 dsdn = (IDsDriverNotifyImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsdn));
3260 if (dsdn == NULL) {
3261 WARN("out of memory\n");
3262 return DSERR_OUTOFMEMORY;
3265 dsdn->ref = 0;
3266 dsdn->lpVtbl = &dsdnvt;
3267 dsdn->buffer = dsdb;
3268 dsdb->notify = dsdn;
3269 IDsDriverBuffer_AddRef((PIDSDRIVER)dsdb);
3271 *pdsdn = dsdn;
3272 return DS_OK;
3275 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
3277 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
3278 TRACE("(%d,%p)\n",wDevID,drv);
3280 /* the HAL isn't much better than the HEL if we can't do mmap() */
3281 if (!(WOutDev[wDevID].ossdev->duplex_out_caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
3282 ERR("DirectSound flag not set\n");
3283 MESSAGE("This sound card's driver does not support direct access\n");
3284 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
3285 return MMSYSERR_NOTSUPPORTED;
3288 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsDriverImpl));
3289 if (!*idrv)
3290 return MMSYSERR_NOMEM;
3291 (*idrv)->lpVtbl = &dsdvt;
3292 (*idrv)->ref = 1;
3293 (*idrv)->wDevID = wDevID;
3294 (*idrv)->primary = NULL;
3295 (*idrv)->nrofsecondaries = 0;
3296 (*idrv)->secondaries = NULL;
3298 return MMSYSERR_NOERROR;
3301 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
3303 TRACE("(%d,%p)\n",wDevID,desc);
3304 memcpy(desc, &(WOutDev[wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
3305 return MMSYSERR_NOERROR;
3308 /*======================================================================*
3309 * Low level WAVE IN implementation *
3310 *======================================================================*/
3312 /**************************************************************************
3313 * widNotifyClient [internal]
3315 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
3317 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04lX dwParam2 = %04lX\n", wMsg,
3318 wMsg == WIM_OPEN ? "WIM_OPEN" : wMsg == WIM_CLOSE ? "WIM_CLOSE" :
3319 wMsg == WIM_DATA ? "WIM_DATA" : "Unknown", dwParam1, dwParam2);
3321 switch (wMsg) {
3322 case WIM_OPEN:
3323 case WIM_CLOSE:
3324 case WIM_DATA:
3325 if (wwi->wFlags != DCB_NULL &&
3326 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
3327 (HDRVR)wwi->waveDesc.hWave, wMsg,
3328 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
3329 WARN("can't notify client !\n");
3330 return MMSYSERR_ERROR;
3332 break;
3333 default:
3334 FIXME("Unknown callback message %u\n", wMsg);
3335 return MMSYSERR_INVALPARAM;
3337 return MMSYSERR_NOERROR;
3340 /**************************************************************************
3341 * widGetDevCaps [internal]
3343 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
3345 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
3347 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
3349 if (wDevID >= numInDev) {
3350 TRACE("numOutDev reached !\n");
3351 return MMSYSERR_BADDEVICEID;
3354 memcpy(lpCaps, &WInDev[wDevID].ossdev->in_caps, min(dwSize, sizeof(*lpCaps)));
3355 return MMSYSERR_NOERROR;
3358 /**************************************************************************
3359 * widRecorder_ReadHeaders [internal]
3361 static void widRecorder_ReadHeaders(WINE_WAVEIN * wwi)
3363 enum win_wm_message tmp_msg;
3364 DWORD tmp_param;
3365 HANDLE tmp_ev;
3366 WAVEHDR* lpWaveHdr;
3368 while (OSS_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
3369 if (tmp_msg == WINE_WM_HEADER) {
3370 LPWAVEHDR* wh;
3371 lpWaveHdr = (LPWAVEHDR)tmp_param;
3372 lpWaveHdr->lpNext = 0;
3374 if (wwi->lpQueuePtr == 0)
3375 wwi->lpQueuePtr = lpWaveHdr;
3376 else {
3377 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
3378 *wh = lpWaveHdr;
3380 } else {
3381 ERR("should only have headers left\n");
3386 /**************************************************************************
3387 * widRecorder [internal]
3389 static DWORD CALLBACK widRecorder(LPVOID pmt)
3391 WORD uDevID = (DWORD)pmt;
3392 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
3393 WAVEHDR* lpWaveHdr;
3394 DWORD dwSleepTime;
3395 DWORD bytesRead;
3396 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwFragmentSize);
3397 char *pOffset = buffer;
3398 audio_buf_info info;
3399 int xs;
3400 enum win_wm_message msg;
3401 DWORD param;
3402 HANDLE ev;
3403 int enable;
3405 wwi->state = WINE_WS_STOPPED;
3406 wwi->dwTotalRecorded = 0;
3407 wwi->dwTotalRead = 0;
3408 wwi->lpQueuePtr = NULL;
3410 SetEvent(wwi->hStartUpEvent);
3412 /* disable input so capture will begin when triggered */
3413 wwi->ossdev->bInputEnabled = FALSE;
3414 enable = getEnables(wwi->ossdev);
3415 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
3416 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
3418 /* the soundblaster live needs a micro wake to get its recording started
3419 * (or GETISPACE will have 0 frags all the time)
3421 read(wwi->ossdev->fd, &xs, 4);
3423 /* make sleep time to be # of ms to output a fragment */
3424 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
3425 TRACE("sleeptime=%ld ms\n", dwSleepTime);
3427 for (;;) {
3428 /* wait for dwSleepTime or an event in thread's queue */
3429 /* FIXME: could improve wait time depending on queue state,
3430 * ie, number of queued fragments
3433 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
3435 lpWaveHdr = wwi->lpQueuePtr;
3437 ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info);
3438 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
3440 /* read all the fragments accumulated so far */
3441 while ((info.fragments > 0) && (wwi->lpQueuePtr))
3443 info.fragments --;
3445 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
3447 /* directly read fragment in wavehdr */
3448 bytesRead = read(wwi->ossdev->fd,
3449 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
3450 wwi->dwFragmentSize);
3452 TRACE("bytesRead=%ld (direct)\n", bytesRead);
3453 if (bytesRead != (DWORD) -1)
3455 /* update number of bytes recorded in current buffer and by this device */
3456 lpWaveHdr->dwBytesRecorded += bytesRead;
3457 wwi->dwTotalRead += bytesRead;
3458 wwi->dwTotalRecorded = wwi->dwTotalRead;
3460 /* buffer is full. notify client */
3461 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
3463 /* must copy the value of next waveHdr, because we have no idea of what
3464 * will be done with the content of lpWaveHdr in callback
3466 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
3468 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
3469 lpWaveHdr->dwFlags |= WHDR_DONE;
3471 wwi->lpQueuePtr = lpNext;
3472 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
3473 lpWaveHdr = lpNext;
3475 } else {
3476 TRACE("read(%s, %p, %ld) failed (%s)\n", wwi->ossdev->dev_name,
3477 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
3478 wwi->dwFragmentSize, strerror(errno));
3481 else
3483 /* read the fragment in a local buffer */
3484 bytesRead = read(wwi->ossdev->fd, buffer, wwi->dwFragmentSize);
3485 pOffset = buffer;
3487 TRACE("bytesRead=%ld (local)\n", bytesRead);
3489 if (bytesRead == (DWORD) -1) {
3490 TRACE("read(%s, %p, %ld) failed (%s)\n", wwi->ossdev->dev_name,
3491 buffer, wwi->dwFragmentSize, strerror(errno));
3492 continue;
3495 /* copy data in client buffers */
3496 while (bytesRead != (DWORD) -1 && bytesRead > 0)
3498 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
3500 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
3501 pOffset,
3502 dwToCopy);
3504 /* update number of bytes recorded in current buffer and by this device */
3505 lpWaveHdr->dwBytesRecorded += dwToCopy;
3506 wwi->dwTotalRead += dwToCopy;
3507 wwi->dwTotalRecorded = wwi->dwTotalRead;
3508 bytesRead -= dwToCopy;
3509 pOffset += dwToCopy;
3511 /* client buffer is full. notify client */
3512 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
3514 /* must copy the value of next waveHdr, because we have no idea of what
3515 * will be done with the content of lpWaveHdr in callback
3517 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
3518 TRACE("lpNext=%p\n", lpNext);
3520 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
3521 lpWaveHdr->dwFlags |= WHDR_DONE;
3523 wwi->lpQueuePtr = lpNext;
3524 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
3526 lpWaveHdr = lpNext;
3527 if (!lpNext && bytesRead) {
3528 /* before we give up, check for more header messages */
3529 while (OSS_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
3531 if (msg == WINE_WM_HEADER) {
3532 LPWAVEHDR hdr;
3533 OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
3534 hdr = ((LPWAVEHDR)param);
3535 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg), hdr, ev);
3536 hdr->lpNext = 0;
3537 if (lpWaveHdr == 0) {
3538 /* new head of queue */
3539 wwi->lpQueuePtr = lpWaveHdr = hdr;
3540 } else {
3541 /* insert buffer at the end of queue */
3542 LPWAVEHDR* wh;
3543 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
3544 *wh = hdr;
3546 } else
3547 break;
3550 if (lpWaveHdr == 0) {
3551 /* no more buffer to copy data to, but we did read more.
3552 * what hasn't been copied will be dropped
3554 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
3555 wwi->lpQueuePtr = NULL;
3556 break;
3565 WAIT_OMR(&wwi->msgRing, dwSleepTime);
3567 while (OSS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
3569 TRACE("msg=%s param=0x%lx\n", getCmdString(msg), param);
3570 switch (msg) {
3571 case WINE_WM_PAUSING:
3572 wwi->state = WINE_WS_PAUSED;
3573 /*FIXME("Device should stop recording\n");*/
3574 SetEvent(ev);
3575 break;
3576 case WINE_WM_STARTING:
3577 wwi->state = WINE_WS_PLAYING;
3579 if (wwi->ossdev->bTriggerSupport)
3581 /* start the recording */
3582 wwi->ossdev->bInputEnabled = TRUE;
3583 enable = getEnables(wwi->ossdev);
3584 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3585 wwi->ossdev->bInputEnabled = FALSE;
3586 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
3589 else
3591 unsigned char data[4];
3592 /* read 4 bytes to start the recording */
3593 read(wwi->ossdev->fd, data, 4);
3596 SetEvent(ev);
3597 break;
3598 case WINE_WM_HEADER:
3599 lpWaveHdr = (LPWAVEHDR)param;
3600 lpWaveHdr->lpNext = 0;
3602 /* insert buffer at the end of queue */
3604 LPWAVEHDR* wh;
3605 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
3606 *wh = lpWaveHdr;
3608 break;
3609 case WINE_WM_STOPPING:
3610 if (wwi->state != WINE_WS_STOPPED)
3612 if (wwi->ossdev->bTriggerSupport)
3614 /* stop the recording */
3615 wwi->ossdev->bInputEnabled = FALSE;
3616 enable = getEnables(wwi->ossdev);
3617 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3618 wwi->ossdev->bInputEnabled = FALSE;
3619 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
3623 /* read any headers in queue */
3624 widRecorder_ReadHeaders(wwi);
3626 /* return current buffer to app */
3627 lpWaveHdr = wwi->lpQueuePtr;
3628 if (lpWaveHdr)
3630 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
3631 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
3632 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
3633 lpWaveHdr->dwFlags |= WHDR_DONE;
3634 wwi->lpQueuePtr = lpNext;
3635 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
3638 wwi->state = WINE_WS_STOPPED;
3639 SetEvent(ev);
3640 break;
3641 case WINE_WM_RESETTING:
3642 if (wwi->state != WINE_WS_STOPPED)
3644 if (wwi->ossdev->bTriggerSupport)
3646 /* stop the recording */
3647 wwi->ossdev->bInputEnabled = FALSE;
3648 enable = getEnables(wwi->ossdev);
3649 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
3650 wwi->ossdev->bInputEnabled = FALSE;
3651 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
3655 wwi->state = WINE_WS_STOPPED;
3656 wwi->dwTotalRecorded = 0;
3657 wwi->dwTotalRead = 0;
3659 /* read any headers in queue */
3660 widRecorder_ReadHeaders(wwi);
3662 /* return all buffers to the app */
3663 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
3664 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
3665 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
3666 lpWaveHdr->dwFlags |= WHDR_DONE;
3667 wwi->lpQueuePtr = lpWaveHdr->lpNext;
3668 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
3671 wwi->lpQueuePtr = NULL;
3672 SetEvent(ev);
3673 break;
3674 case WINE_WM_UPDATE:
3675 if (wwi->state == WINE_WS_PLAYING) {
3676 audio_buf_info tmp_info;
3677 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &tmp_info) < 0)
3678 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", wwi->ossdev->dev_name, strerror(errno));
3679 else
3680 wwi->dwTotalRecorded = wwi->dwTotalRead + tmp_info.bytes;
3682 SetEvent(ev);
3683 break;
3684 case WINE_WM_CLOSING:
3685 wwi->hThread = 0;
3686 wwi->state = WINE_WS_CLOSED;
3687 SetEvent(ev);
3688 HeapFree(GetProcessHeap(), 0, buffer);
3689 ExitThread(0);
3690 /* shouldn't go here */
3691 default:
3692 FIXME("unknown message %d\n", msg);
3693 break;
3697 ExitThread(0);
3698 /* just for not generating compilation warnings... should never be executed */
3699 return 0;
3703 /**************************************************************************
3704 * widOpen [internal]
3706 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
3708 WINE_WAVEIN* wwi;
3709 audio_buf_info info;
3710 int audio_fragment;
3711 DWORD ret;
3713 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
3714 if (lpDesc == NULL) {
3715 WARN("Invalid Parameter !\n");
3716 return MMSYSERR_INVALPARAM;
3718 if (wDevID >= numInDev) {
3719 WARN("bad device id: %d >= %d\n", wDevID, numInDev);
3720 return MMSYSERR_BADDEVICEID;
3723 /* only PCM format is supported so far... */
3724 if (!supportedFormat(lpDesc->lpFormat)) {
3725 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
3726 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
3727 lpDesc->lpFormat->nSamplesPerSec);
3728 return WAVERR_BADFORMAT;
3731 if (dwFlags & WAVE_FORMAT_QUERY) {
3732 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
3733 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
3734 lpDesc->lpFormat->nSamplesPerSec);
3735 return MMSYSERR_NOERROR;
3738 TRACE("OSS_OpenDevice requested this format: %ldx%dx%d %s\n",
3739 lpDesc->lpFormat->nSamplesPerSec,
3740 lpDesc->lpFormat->wBitsPerSample,
3741 lpDesc->lpFormat->nChannels,
3742 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_PCM ? "WAVE_FORMAT_PCM" :
3743 lpDesc->lpFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE ? "WAVE_FORMAT_EXTENSIBLE" :
3744 "UNSUPPORTED");
3746 wwi = &WInDev[wDevID];
3748 if (wwi->state != WINE_WS_CLOSED) return MMSYSERR_ALLOCATED;
3750 if ((dwFlags & WAVE_DIRECTSOUND) &&
3751 !(wwi->ossdev->in_caps_support & WAVECAPS_DIRECTSOUND))
3752 /* not supported, ignore it */
3753 dwFlags &= ~WAVE_DIRECTSOUND;
3755 if (dwFlags & WAVE_DIRECTSOUND) {
3756 TRACE("has DirectSoundCapture driver\n");
3757 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
3758 /* we have realtime DirectSound, fragments just waste our time,
3759 * but a large buffer is good, so choose 64KB (32 * 2^11) */
3760 audio_fragment = 0x0020000B;
3761 else
3762 /* to approximate realtime, we must use small fragments,
3763 * let's try to fragment the above 64KB (256 * 2^8) */
3764 audio_fragment = 0x01000008;
3765 } else {
3766 TRACE("doesn't have DirectSoundCapture driver\n");
3767 if (wwi->ossdev->open_count > 0) {
3768 TRACE("Using output device audio_fragment\n");
3769 /* FIXME: This may not be optimal for capture but it allows us
3770 * to do hardware playback without hardware capture. */
3771 audio_fragment = wwi->ossdev->audio_fragment;
3772 } else {
3773 /* A wave device must have a worst case latency of 10 ms so calculate
3774 * the largest fragment size less than 10 ms long.
3776 int fsize = lpDesc->lpFormat->nAvgBytesPerSec / 100; /* 10 ms chunk */
3777 int shift = 0;
3778 while ((1 << shift) <= fsize)
3779 shift++;
3780 shift--;
3781 audio_fragment = 0x00100000 + shift; /* 16 fragments of 2^shift */
3785 TRACE("requesting %d %d byte fragments (%ld ms)\n", audio_fragment >> 16,
3786 1 << (audio_fragment & 0xffff),
3787 ((1 << (audio_fragment & 0xffff)) * 1000) / lpDesc->lpFormat->nAvgBytesPerSec);
3789 ret = OSS_OpenDevice(wwi->ossdev, O_RDONLY, &audio_fragment,
3791 lpDesc->lpFormat->nSamplesPerSec,
3792 (lpDesc->lpFormat->nChannels > 1) ? 1 : 0,
3793 (lpDesc->lpFormat->wBitsPerSample == 16)
3794 ? AFMT_S16_LE : AFMT_U8);
3795 if (ret != 0) return ret;
3796 wwi->state = WINE_WS_STOPPED;
3798 if (wwi->lpQueuePtr) {
3799 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
3800 wwi->lpQueuePtr = NULL;
3802 wwi->dwTotalRecorded = 0;
3803 wwi->dwTotalRead = 0;
3804 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
3806 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
3807 copy_format(lpDesc->lpFormat, &wwi->waveFormat);
3809 if (wwi->waveFormat.Format.wBitsPerSample == 0) {
3810 WARN("Resetting zeroed wBitsPerSample\n");
3811 wwi->waveFormat.Format.wBitsPerSample = 8 *
3812 (wwi->waveFormat.Format.nAvgBytesPerSec /
3813 wwi->waveFormat.Format.nSamplesPerSec) /
3814 wwi->waveFormat.Format.nChannels;
3817 if (ioctl(wwi->ossdev->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
3818 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
3819 wwi->ossdev->dev_name, strerror(errno));
3820 OSS_CloseDevice(wwi->ossdev);
3821 wwi->state = WINE_WS_CLOSED;
3822 return MMSYSERR_NOTENABLED;
3825 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info.fragstotal,
3826 info.fragsize, (info.fragsize * 1000) / (wwi->ossdev->sample_rate *
3827 (wwi->ossdev->stereo ? 2 : 1) *
3828 (wwi->ossdev->format == AFMT_U8 ? 1 : 2)));
3830 wwi->dwFragmentSize = info.fragsize;
3832 TRACE("dwFragmentSize=%lu\n", wwi->dwFragmentSize);
3833 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
3834 wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
3835 wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
3836 wwi->waveFormat.Format.nBlockAlign);
3838 OSS_InitRingMessage(&wwi->msgRing);
3840 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
3841 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
3842 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
3843 CloseHandle(wwi->hStartUpEvent);
3844 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
3846 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
3849 /**************************************************************************
3850 * widClose [internal]
3852 static DWORD widClose(WORD wDevID)
3854 WINE_WAVEIN* wwi;
3856 TRACE("(%u);\n", wDevID);
3857 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3858 WARN("can't close !\n");
3859 return MMSYSERR_INVALHANDLE;
3862 wwi = &WInDev[wDevID];
3864 if (wwi->lpQueuePtr != NULL) {
3865 WARN("still buffers open !\n");
3866 return WAVERR_STILLPLAYING;
3869 OSS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
3870 OSS_CloseDevice(wwi->ossdev);
3871 wwi->state = WINE_WS_CLOSED;
3872 wwi->dwFragmentSize = 0;
3873 OSS_DestroyRingMessage(&wwi->msgRing);
3874 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
3877 /**************************************************************************
3878 * widAddBuffer [internal]
3880 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3882 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3884 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3885 WARN("can't do it !\n");
3886 return MMSYSERR_INVALHANDLE;
3888 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
3889 TRACE("never been prepared !\n");
3890 return WAVERR_UNPREPARED;
3892 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
3893 TRACE("header already in use !\n");
3894 return WAVERR_STILLPLAYING;
3897 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
3898 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3899 lpWaveHdr->dwBytesRecorded = 0;
3900 lpWaveHdr->lpNext = NULL;
3902 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
3903 return MMSYSERR_NOERROR;
3906 /**************************************************************************
3907 * widPrepare [internal]
3909 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3911 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3913 if (wDevID >= numInDev) return MMSYSERR_INVALHANDLE;
3915 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
3916 return WAVERR_STILLPLAYING;
3918 lpWaveHdr->dwFlags |= WHDR_PREPARED;
3919 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3920 lpWaveHdr->dwBytesRecorded = 0;
3921 TRACE("header prepared !\n");
3922 return MMSYSERR_NOERROR;
3925 /**************************************************************************
3926 * widUnprepare [internal]
3928 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3930 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3931 if (wDevID >= numInDev) return MMSYSERR_INVALHANDLE;
3933 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
3934 return WAVERR_STILLPLAYING;
3936 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
3937 lpWaveHdr->dwFlags |= WHDR_DONE;
3939 return MMSYSERR_NOERROR;
3942 /**************************************************************************
3943 * widStart [internal]
3945 static DWORD widStart(WORD wDevID)
3947 TRACE("(%u);\n", wDevID);
3948 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3949 WARN("can't start recording !\n");
3950 return MMSYSERR_INVALHANDLE;
3953 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
3954 return MMSYSERR_NOERROR;
3957 /**************************************************************************
3958 * widStop [internal]
3960 static DWORD widStop(WORD wDevID)
3962 TRACE("(%u);\n", wDevID);
3963 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3964 WARN("can't stop !\n");
3965 return MMSYSERR_INVALHANDLE;
3968 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3970 return MMSYSERR_NOERROR;
3973 /**************************************************************************
3974 * widReset [internal]
3976 static DWORD widReset(WORD wDevID)
3978 TRACE("(%u);\n", wDevID);
3979 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3980 WARN("can't reset !\n");
3981 return MMSYSERR_INVALHANDLE;
3983 OSS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3984 return MMSYSERR_NOERROR;
3987 /**************************************************************************
3988 * widGetPosition [internal]
3990 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3992 WINE_WAVEIN* wwi;
3994 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
3996 if (wDevID >= numInDev || WInDev[wDevID].state == WINE_WS_CLOSED) {
3997 WARN("can't get pos !\n");
3998 return MMSYSERR_INVALHANDLE;
4001 if (lpTime == NULL) {
4002 WARN("invalid parameter: lpTime == NULL\n");
4003 return MMSYSERR_INVALPARAM;
4006 wwi = &WInDev[wDevID];
4007 #ifdef EXACT_WIDPOSITION
4008 if (wwi->ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
4009 OSS_AddRingMessage(&(wwi->msgRing), WINE_WM_UPDATE, 0, TRUE);
4010 #endif
4012 return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->waveFormat);
4015 /**************************************************************************
4016 * widMessage (WINEOSS.6)
4018 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
4019 DWORD dwParam1, DWORD dwParam2)
4021 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
4022 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
4024 switch (wMsg) {
4025 case DRVM_INIT:
4026 case DRVM_EXIT:
4027 case DRVM_ENABLE:
4028 case DRVM_DISABLE:
4029 /* FIXME: Pretend this is supported */
4030 return 0;
4031 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
4032 case WIDM_CLOSE: return widClose (wDevID);
4033 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
4034 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
4035 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
4036 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
4037 case WIDM_GETNUMDEVS: return numInDev;
4038 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
4039 case WIDM_RESET: return widReset (wDevID);
4040 case WIDM_START: return widStart (wDevID);
4041 case WIDM_STOP: return widStop (wDevID);
4042 case DRV_QUERYDEVICEINTERFACESIZE: return wdDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
4043 case DRV_QUERYDEVICEINTERFACE: return wdDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
4044 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
4045 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
4046 default:
4047 FIXME("unknown message %u!\n", wMsg);
4049 return MMSYSERR_NOTSUPPORTED;
4052 /*======================================================================*
4053 * Low level DSOUND capture definitions *
4054 *======================================================================*/
4056 typedef struct IDsCaptureDriverPropertySetImpl IDsCaptureDriverPropertySetImpl;
4057 typedef struct IDsCaptureDriverNotifyImpl IDsCaptureDriverNotifyImpl;
4058 typedef struct IDsCaptureDriverImpl IDsCaptureDriverImpl;
4059 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl;
4061 struct IDsCaptureDriverPropertySetImpl
4063 /* IUnknown fields */
4064 IDsDriverPropertySetVtbl *lpVtbl;
4065 DWORD ref;
4067 IDsCaptureDriverBufferImpl* capture_buffer;
4070 struct IDsCaptureDriverNotifyImpl
4072 /* IUnknown fields */
4073 IDsDriverNotifyVtbl *lpVtbl;
4074 DWORD ref;
4076 IDsCaptureDriverBufferImpl* capture_buffer;
4079 struct IDsCaptureDriverImpl
4081 /* IUnknown fields */
4082 IDsCaptureDriverVtbl *lpVtbl;
4083 DWORD ref;
4085 /* IDsCaptureDriverImpl fields */
4086 UINT wDevID;
4087 IDsCaptureDriverBufferImpl* capture_buffer;
4090 struct IDsCaptureDriverBufferImpl
4092 /* IUnknown fields */
4093 IDsCaptureDriverBufferVtbl *lpVtbl;
4094 DWORD ref;
4096 /* IDsCaptureDriverBufferImpl fields */
4097 IDsCaptureDriverImpl* drv;
4098 DWORD buflen;
4099 LPBYTE buffer;
4100 DWORD writeptr;
4101 LPBYTE mapping;
4102 DWORD maplen;
4104 /* IDsDriverNotifyImpl fields */
4105 IDsCaptureDriverNotifyImpl* notify;
4106 int notify_index;
4107 LPDSBPOSITIONNOTIFY notifies;
4108 int nrofnotifies;
4110 /* IDsDriverPropertySetImpl fields */
4111 IDsCaptureDriverPropertySetImpl* property_set;
4113 BOOL is_capturing;
4114 BOOL is_looping;
4117 static HRESULT WINAPI IDsCaptureDriverPropertySetImpl_Create(
4118 IDsCaptureDriverBufferImpl * dscdb,
4119 IDsCaptureDriverPropertySetImpl **pdscdps);
4121 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_Create(
4122 IDsCaptureDriverBufferImpl * dsdcb,
4123 IDsCaptureDriverNotifyImpl **pdscdn);
4125 /*======================================================================*
4126 * Low level DSOUND capture property set implementation *
4127 *======================================================================*/
4129 static HRESULT WINAPI IDsCaptureDriverPropertySetImpl_QueryInterface(
4130 PIDSDRIVERPROPERTYSET iface,
4131 REFIID riid,
4132 LPVOID *ppobj)
4134 IDsCaptureDriverPropertySetImpl *This = (IDsCaptureDriverPropertySetImpl *)iface;
4135 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
4137 if ( IsEqualGUID(riid, &IID_IUnknown) ||
4138 IsEqualGUID(riid, &IID_IDsDriverPropertySet) ) {
4139 IDsDriverPropertySet_AddRef(iface);
4140 *ppobj = (LPVOID)This;
4141 return DS_OK;
4144 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
4146 *ppobj = 0;
4147 return E_NOINTERFACE;
4150 static ULONG WINAPI IDsCaptureDriverPropertySetImpl_AddRef(
4151 PIDSDRIVERPROPERTYSET iface)
4153 IDsCaptureDriverPropertySetImpl *This = (IDsCaptureDriverPropertySetImpl *)iface;
4154 TRACE("(%p) ref was %ld\n", This, This->ref);
4156 return InterlockedIncrement(&(This->ref));
4159 static ULONG WINAPI IDsCaptureDriverPropertySetImpl_Release(
4160 PIDSDRIVERPROPERTYSET iface)
4162 IDsCaptureDriverPropertySetImpl *This = (IDsCaptureDriverPropertySetImpl *)iface;
4163 DWORD ref;
4164 TRACE("(%p) ref was %ld\n", This, This->ref);
4166 ref = InterlockedDecrement(&(This->ref));
4167 if (ref == 0) {
4168 IDsCaptureDriverBuffer_Release((PIDSCDRIVERBUFFER)This->capture_buffer);
4169 This->capture_buffer->property_set = NULL;
4170 HeapFree(GetProcessHeap(),0,This);
4171 TRACE("(%p) released\n",This);
4173 return ref;
4176 static HRESULT WINAPI IDsCaptureDriverPropertySetImpl_Get(
4177 PIDSDRIVERPROPERTYSET iface,
4178 PDSPROPERTY pDsProperty,
4179 LPVOID pPropertyParams,
4180 ULONG cbPropertyParams,
4181 LPVOID pPropertyData,
4182 ULONG cbPropertyData,
4183 PULONG pcbReturnedData )
4185 IDsCaptureDriverPropertySetImpl *This = (IDsCaptureDriverPropertySetImpl *)iface;
4186 FIXME("(%p,%p,%p,%lx,%p,%lx,%p)\n",This,pDsProperty,pPropertyParams,
4187 cbPropertyParams,pPropertyData,cbPropertyData,pcbReturnedData);
4188 return DSERR_UNSUPPORTED;
4191 static HRESULT WINAPI IDsCaptureDriverPropertySetImpl_Set(
4192 PIDSDRIVERPROPERTYSET iface,
4193 PDSPROPERTY pDsProperty,
4194 LPVOID pPropertyParams,
4195 ULONG cbPropertyParams,
4196 LPVOID pPropertyData,
4197 ULONG cbPropertyData )
4199 IDsCaptureDriverPropertySetImpl *This = (IDsCaptureDriverPropertySetImpl *)iface;
4200 FIXME("(%p,%p,%p,%lx,%p,%lx)\n",This,pDsProperty,pPropertyParams,
4201 cbPropertyParams,pPropertyData,cbPropertyData);
4202 return DSERR_UNSUPPORTED;
4205 static HRESULT WINAPI IDsCaptureDriverPropertySetImpl_QuerySupport(
4206 PIDSDRIVERPROPERTYSET iface,
4207 REFGUID PropertySetId,
4208 ULONG PropertyId,
4209 PULONG pSupport )
4211 IDsCaptureDriverPropertySetImpl *This = (IDsCaptureDriverPropertySetImpl *)iface;
4212 FIXME("(%p,%s,%lx,%p)\n",This,debugstr_guid(PropertySetId),PropertyId,
4213 pSupport);
4214 return DSERR_UNSUPPORTED;
4217 IDsDriverPropertySetVtbl dscdpsvt =
4219 IDsCaptureDriverPropertySetImpl_QueryInterface,
4220 IDsCaptureDriverPropertySetImpl_AddRef,
4221 IDsCaptureDriverPropertySetImpl_Release,
4222 IDsCaptureDriverPropertySetImpl_Get,
4223 IDsCaptureDriverPropertySetImpl_Set,
4224 IDsCaptureDriverPropertySetImpl_QuerySupport,
4227 /*======================================================================*
4228 * Low level DSOUND capture notify implementation *
4229 *======================================================================*/
4231 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_QueryInterface(
4232 PIDSDRIVERNOTIFY iface,
4233 REFIID riid,
4234 LPVOID *ppobj)
4236 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
4237 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
4239 if ( IsEqualGUID(riid, &IID_IUnknown) ||
4240 IsEqualGUID(riid, &IID_IDsDriverNotify) ) {
4241 IDsDriverNotify_AddRef(iface);
4242 *ppobj = This;
4243 return DS_OK;
4246 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
4248 *ppobj = 0;
4249 return E_NOINTERFACE;
4252 static ULONG WINAPI IDsCaptureDriverNotifyImpl_AddRef(
4253 PIDSDRIVERNOTIFY iface)
4255 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
4256 TRACE("(%p) ref was %ld\n", This, This->ref);
4258 return InterlockedIncrement(&(This->ref));
4261 static ULONG WINAPI IDsCaptureDriverNotifyImpl_Release(
4262 PIDSDRIVERNOTIFY iface)
4264 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
4265 DWORD ref;
4266 TRACE("(%p) ref was %ld\n", This, This->ref);
4268 ref = InterlockedDecrement(&(This->ref));
4269 if (ref == 0) {
4270 IDsCaptureDriverBuffer_Release((PIDSCDRIVERBUFFER)This->capture_buffer);
4271 This->capture_buffer->notify = NULL;
4272 HeapFree(GetProcessHeap(),0,This);
4273 TRACE("(%p) released\n",This);
4275 return ref;
4278 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_SetNotificationPositions(
4279 PIDSDRIVERNOTIFY iface,
4280 DWORD howmuch,
4281 LPCDSBPOSITIONNOTIFY notify)
4283 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
4284 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
4286 if (!notify) {
4287 WARN("invalid parameter\n");
4288 return DSERR_INVALIDPARAM;
4291 if (TRACE_ON(wave)) {
4292 int i;
4293 for (i=0;i<howmuch;i++)
4294 TRACE("notify at %ld to 0x%08lx\n",
4295 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
4298 /* Make an internal copy of the caller-supplied array.
4299 * Replace the existing copy if one is already present. */
4300 if (This->capture_buffer->notifies)
4301 This->capture_buffer->notifies = HeapReAlloc(GetProcessHeap(),
4302 HEAP_ZERO_MEMORY, This->capture_buffer->notifies,
4303 howmuch * sizeof(DSBPOSITIONNOTIFY));
4304 else
4305 This->capture_buffer->notifies = HeapAlloc(GetProcessHeap(),
4306 HEAP_ZERO_MEMORY, howmuch * sizeof(DSBPOSITIONNOTIFY));
4308 memcpy(This->capture_buffer->notifies, notify,
4309 howmuch * sizeof(DSBPOSITIONNOTIFY));
4310 This->capture_buffer->nrofnotifies = howmuch;
4312 return S_OK;
4315 IDsDriverNotifyVtbl dscdnvt =
4317 IDsCaptureDriverNotifyImpl_QueryInterface,
4318 IDsCaptureDriverNotifyImpl_AddRef,
4319 IDsCaptureDriverNotifyImpl_Release,
4320 IDsCaptureDriverNotifyImpl_SetNotificationPositions,
4323 /*======================================================================*
4324 * Low level DSOUND capture implementation *
4325 *======================================================================*/
4327 static HRESULT DSCDB_MapBuffer(IDsCaptureDriverBufferImpl *dscdb)
4329 if (!dscdb->mapping) {
4330 dscdb->mapping = mmap(NULL, dscdb->maplen, PROT_READ, MAP_SHARED,
4331 WInDev[dscdb->drv->wDevID].ossdev->fd, 0);
4332 if (dscdb->mapping == (LPBYTE)-1) {
4333 TRACE("(%p): Could not map sound device for direct access (%s)\n",
4334 dscdb, strerror(errno));
4335 return DSERR_GENERIC;
4337 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dscdb, dscdb->mapping, dscdb->maplen);
4339 return DS_OK;
4342 static HRESULT DSCDB_UnmapBuffer(IDsCaptureDriverBufferImpl *dscdb)
4344 if (dscdb->mapping) {
4345 if (munmap(dscdb->mapping, dscdb->maplen) < 0) {
4346 ERR("(%p): Could not unmap sound device (%s)\n",
4347 dscdb, strerror(errno));
4348 return DSERR_GENERIC;
4350 dscdb->mapping = NULL;
4351 TRACE("(%p): sound device unmapped\n", dscdb);
4353 return DS_OK;
4356 static HRESULT WINAPI IDsCaptureDriverBufferImpl_QueryInterface(
4357 PIDSCDRIVERBUFFER iface,
4358 REFIID riid,
4359 LPVOID *ppobj)
4361 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4362 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
4364 *ppobj = 0;
4366 if ( IsEqualGUID(riid, &IID_IUnknown) ||
4367 IsEqualGUID(riid, &IID_IDsCaptureDriverBuffer) ) {
4368 IDsCaptureDriverBuffer_AddRef(iface);
4369 *ppobj = (LPVOID)This;
4370 return DS_OK;
4373 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
4374 if (!This->notify)
4375 IDsCaptureDriverNotifyImpl_Create(This, &(This->notify));
4376 if (This->notify) {
4377 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
4378 *ppobj = (LPVOID)This->notify;
4379 return DS_OK;
4381 return E_FAIL;
4384 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
4385 if (!This->property_set)
4386 IDsCaptureDriverPropertySetImpl_Create(This, &(This->property_set));
4387 if (This->property_set) {
4388 IDsDriverPropertySet_AddRef((PIDSDRIVERPROPERTYSET)This->property_set);
4389 *ppobj = (LPVOID)This->property_set;
4390 return DS_OK;
4392 return E_FAIL;
4395 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
4396 return DSERR_UNSUPPORTED;
4399 static ULONG WINAPI IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface)
4401 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4402 TRACE("(%p) ref was %ld\n", This, This->ref);
4404 return InterlockedIncrement(&(This->ref));
4407 static ULONG WINAPI IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface)
4409 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4410 DWORD ref;
4411 TRACE("(%p) ref was %ld\n", This, This->ref);
4413 ref = InterlockedDecrement(&(This->ref));
4414 if (ref == 0) {
4415 DSCDB_UnmapBuffer(This);
4416 if (This->notifies != NULL)
4417 HeapFree(GetProcessHeap(), 0, This->notifies);
4418 HeapFree(GetProcessHeap(),0,This);
4419 TRACE("(%p) released\n",This);
4421 return ref;
4424 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Lock(
4425 PIDSCDRIVERBUFFER iface,
4426 LPVOID* ppvAudio1,
4427 LPDWORD pdwLen1,
4428 LPVOID* ppvAudio2,
4429 LPDWORD pdwLen2,
4430 DWORD dwWritePosition,
4431 DWORD dwWriteLen,
4432 DWORD dwFlags)
4434 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4435 FIXME("(%p,%p,%p,%p,%p,%ld,%ld,0x%08lx): stub!\n",This,ppvAudio1,pdwLen1,
4436 ppvAudio2,pdwLen2,dwWritePosition,dwWriteLen,dwFlags);
4437 return DS_OK;
4440 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Unlock(
4441 PIDSCDRIVERBUFFER iface,
4442 LPVOID pvAudio1,
4443 DWORD dwLen1,
4444 LPVOID pvAudio2,
4445 DWORD dwLen2)
4447 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4448 FIXME("(%p,%p,%ld,%p,%ld): stub!\n",This,pvAudio1,dwLen1,pvAudio2,dwLen2);
4449 return DS_OK;
4452 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetPosition(
4453 PIDSCDRIVERBUFFER iface,
4454 LPDWORD lpdwCapture,
4455 LPDWORD lpdwRead)
4457 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4458 count_info info;
4459 DWORD ptr;
4460 TRACE("(%p,%p,%p)\n",This,lpdwCapture,lpdwRead);
4462 if (WInDev[This->drv->wDevID].state == WINE_WS_CLOSED) {
4463 ERR("device not open, but accessing?\n");
4464 return DSERR_UNINITIALIZED;
4467 if (!This->is_capturing) {
4468 if (lpdwCapture)
4469 *lpdwCapture = 0;
4470 if (lpdwRead)
4471 *lpdwRead = 0;
4474 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
4475 ERR("ioctl(%s, SNDCTL_DSP_GETIPTR) failed (%s)\n",
4476 WInDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
4477 return DSERR_GENERIC;
4479 ptr = info.ptr & ~3; /* align the pointer, just in case */
4480 if (lpdwCapture) *lpdwCapture = ptr;
4481 if (lpdwRead) {
4482 /* add some safety margin (not strictly necessary, but...) */
4483 if (WInDev[This->drv->wDevID].ossdev->in_caps_support & WAVECAPS_SAMPLEACCURATE)
4484 *lpdwRead = ptr + 32;
4485 else
4486 *lpdwRead = ptr + WInDev[This->drv->wDevID].dwFragmentSize;
4487 while (*lpdwRead > This->buflen)
4488 *lpdwRead -= This->buflen;
4490 TRACE("capturepos=%ld, readpos=%ld\n", lpdwCapture?*lpdwCapture:0, lpdwRead?*lpdwRead:0);
4491 return DS_OK;
4494 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetStatus(
4495 PIDSCDRIVERBUFFER iface,
4496 LPDWORD lpdwStatus)
4498 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4499 TRACE("(%p,%p)\n",This,lpdwStatus);
4501 if (This->is_capturing) {
4502 if (This->is_looping)
4503 *lpdwStatus = DSCBSTATUS_CAPTURING | DSCBSTATUS_LOOPING;
4504 else
4505 *lpdwStatus = DSCBSTATUS_CAPTURING;
4506 } else
4507 *lpdwStatus = 0;
4509 return DS_OK;
4512 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Start(
4513 PIDSCDRIVERBUFFER iface,
4514 DWORD dwFlags)
4516 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4517 int enable;
4518 TRACE("(%p,%lx)\n",This,dwFlags);
4520 if (This->is_capturing)
4521 return DS_OK;
4523 if (dwFlags & DSCBSTART_LOOPING)
4524 This->is_looping = TRUE;
4526 WInDev[This->drv->wDevID].ossdev->bInputEnabled = TRUE;
4527 enable = getEnables(WInDev[This->drv->wDevID].ossdev);
4528 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
4529 if (errno == EINVAL) {
4530 /* Don't give up yet. OSS trigger support is inconsistent. */
4531 if (WInDev[This->drv->wDevID].ossdev->open_count == 1) {
4532 /* try the opposite output enable */
4533 if (WInDev[This->drv->wDevID].ossdev->bOutputEnabled == FALSE)
4534 WInDev[This->drv->wDevID].ossdev->bOutputEnabled = TRUE;
4535 else
4536 WInDev[This->drv->wDevID].ossdev->bOutputEnabled = FALSE;
4537 /* try it again */
4538 enable = getEnables(WInDev[This->drv->wDevID].ossdev);
4539 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) >= 0) {
4540 This->is_capturing = TRUE;
4541 return DS_OK;
4545 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",
4546 WInDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
4547 WInDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
4548 return DSERR_GENERIC;
4551 This->is_capturing = TRUE;
4552 return DS_OK;
4555 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface)
4557 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4558 int enable;
4559 TRACE("(%p)\n",This);
4561 if (!This->is_capturing)
4562 return DS_OK;
4564 /* no more captureing */
4565 WInDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
4566 enable = getEnables(WInDev[This->drv->wDevID].ossdev);
4567 if (ioctl(WInDev[This->drv->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
4568 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",
4569 WInDev[This->drv->wDevID].ossdev->dev_name, strerror(errno));
4570 return DSERR_GENERIC;
4573 /* send a final event if necessary */
4574 if (This->nrofnotifies > 0) {
4575 if (This->notifies[This->nrofnotifies - 1].dwOffset == DSBPN_OFFSETSTOP)
4576 SetEvent(This->notifies[This->nrofnotifies - 1].hEventNotify);
4579 This->is_capturing = FALSE;
4580 This->is_looping = FALSE;
4582 /* Most OSS drivers just can't stop capturing without closing the device...
4583 * so we need to somehow signal to our DirectSound implementation
4584 * that it should completely recreate this HW buffer...
4585 * this unexpected error code should do the trick... */
4586 return DSERR_BUFFERLOST;
4589 static HRESULT WINAPI IDsCaptureDriverBufferImpl_SetFormat(
4590 PIDSCDRIVERBUFFER iface,
4591 LPWAVEFORMATEX pwfx)
4593 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
4594 FIXME("(%p): stub!\n",This);
4595 return DSERR_UNSUPPORTED;
4598 static IDsCaptureDriverBufferVtbl dscdbvt =
4600 IDsCaptureDriverBufferImpl_QueryInterface,
4601 IDsCaptureDriverBufferImpl_AddRef,
4602 IDsCaptureDriverBufferImpl_Release,
4603 IDsCaptureDriverBufferImpl_Lock,
4604 IDsCaptureDriverBufferImpl_Unlock,
4605 IDsCaptureDriverBufferImpl_SetFormat,
4606 IDsCaptureDriverBufferImpl_GetPosition,
4607 IDsCaptureDriverBufferImpl_GetStatus,
4608 IDsCaptureDriverBufferImpl_Start,
4609 IDsCaptureDriverBufferImpl_Stop
4612 static HRESULT WINAPI IDsCaptureDriverImpl_QueryInterface(
4613 PIDSCDRIVER iface,
4614 REFIID riid,
4615 LPVOID *ppobj)
4617 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
4618 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
4620 if ( IsEqualGUID(riid, &IID_IUnknown) ||
4621 IsEqualGUID(riid, &IID_IDsCaptureDriver) ) {
4622 IDsCaptureDriver_AddRef(iface);
4623 *ppobj = (LPVOID)This;
4624 return DS_OK;
4627 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
4629 *ppobj = 0;
4631 return E_NOINTERFACE;
4634 static ULONG WINAPI IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface)
4636 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
4637 TRACE("(%p) ref was %ld\n", This, This->ref);
4639 return InterlockedIncrement(&(This->ref));
4642 static ULONG WINAPI IDsCaptureDriverImpl_Release(PIDSCDRIVER iface)
4644 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
4645 DWORD ref;
4646 TRACE("(%p) ref was %ld\n", This, This->ref);
4648 ref = InterlockedDecrement(&(This->ref));
4649 if (ref == 0) {
4650 HeapFree(GetProcessHeap(),0,This);
4651 TRACE("(%p) released\n",This);
4653 return ref;
4656 static HRESULT WINAPI IDsCaptureDriverImpl_GetDriverDesc(
4657 PIDSCDRIVER iface,
4658 PDSDRIVERDESC pDesc)
4660 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
4661 TRACE("(%p,%p)\n",This,pDesc);
4663 if (!pDesc) {
4664 TRACE("invalid parameter\n");
4665 return DSERR_INVALIDPARAM;
4668 /* copy version from driver */
4669 memcpy(pDesc, &(WInDev[This->wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
4671 pDesc->dwFlags |= DSDDESC_USESYSTEMMEMORY;
4672 pDesc->dnDevNode = WInDev[This->wDevID].waveDesc.dnDevNode;
4673 pDesc->wVxdId = 0;
4674 pDesc->wReserved = 0;
4675 pDesc->ulDeviceNum = This->wDevID;
4676 pDesc->dwHeapType = DSDHEAP_NOHEAP;
4677 pDesc->pvDirectDrawHeap = NULL;
4678 pDesc->dwMemStartAddress = 0;
4679 pDesc->dwMemEndAddress = 0;
4680 pDesc->dwMemAllocExtra = 0;
4681 pDesc->pvReserved1 = NULL;
4682 pDesc->pvReserved2 = NULL;
4683 return DS_OK;
4686 static HRESULT WINAPI IDsCaptureDriverImpl_Open(PIDSCDRIVER iface)
4688 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
4689 TRACE("(%p)\n",This);
4690 return DS_OK;
4693 static HRESULT WINAPI IDsCaptureDriverImpl_Close(PIDSCDRIVER iface)
4695 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
4696 TRACE("(%p)\n",This);
4697 if (This->capture_buffer) {
4698 ERR("problem with DirectSound: capture buffer not released\n");
4699 return DSERR_GENERIC;
4701 return DS_OK;
4704 static HRESULT WINAPI IDsCaptureDriverImpl_GetCaps(
4705 PIDSCDRIVER iface,
4706 PDSCDRIVERCAPS pCaps)
4708 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
4709 TRACE("(%p,%p)\n",This,pCaps);
4710 memcpy(pCaps, &(WInDev[This->wDevID].ossdev->dsc_caps), sizeof(DSCDRIVERCAPS));
4711 return DS_OK;
4714 static HRESULT WINAPI IDsCaptureDriverImpl_CreateCaptureBuffer(
4715 PIDSCDRIVER iface,
4716 LPWAVEFORMATEX pwfx,
4717 DWORD dwFlags,
4718 DWORD dwCardAddress,
4719 LPDWORD pdwcbBufferSize,
4720 LPBYTE *ppbBuffer,
4721 LPVOID *ppvObj)
4723 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
4724 IDsCaptureDriverBufferImpl** ippdscdb = (IDsCaptureDriverBufferImpl**)ppvObj;
4725 HRESULT err;
4726 audio_buf_info info;
4727 int enable;
4728 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",This,pwfx,dwFlags,dwCardAddress,
4729 pdwcbBufferSize,ppbBuffer,ppvObj);
4731 if (This->capture_buffer) {
4732 TRACE("already allocated\n");
4733 return DSERR_ALLOCATED;
4736 *ippdscdb = (IDsCaptureDriverBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverBufferImpl));
4737 if (*ippdscdb == NULL) {
4738 TRACE("out of memory\n");
4739 return DSERR_OUTOFMEMORY;
4742 (*ippdscdb)->lpVtbl = &dscdbvt;
4743 (*ippdscdb)->ref = 1;
4744 (*ippdscdb)->drv = This;
4745 (*ippdscdb)->notify = NULL;
4746 (*ippdscdb)->notify_index = 0;
4747 (*ippdscdb)->notifies = NULL;
4748 (*ippdscdb)->nrofnotifies = 0;
4749 (*ippdscdb)->property_set = NULL;
4750 (*ippdscdb)->is_capturing = FALSE;
4751 (*ippdscdb)->is_looping = FALSE;
4753 if (WInDev[This->wDevID].state == WINE_WS_CLOSED) {
4754 WAVEOPENDESC desc;
4755 desc.hWave = 0;
4756 desc.lpFormat = pwfx;
4757 desc.dwCallback = 0;
4758 desc.dwInstance = 0;
4759 desc.uMappedDeviceID = 0;
4760 desc.dnDevNode = 0;
4761 err = widOpen(This->wDevID, &desc, dwFlags | WAVE_DIRECTSOUND);
4762 if (err != MMSYSERR_NOERROR) {
4763 TRACE("widOpen failed\n");
4764 return err;
4768 /* check how big the DMA buffer is now */
4769 if (ioctl(WInDev[This->wDevID].ossdev->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
4770 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
4771 WInDev[This->wDevID].ossdev->dev_name, strerror(errno));
4772 HeapFree(GetProcessHeap(),0,*ippdscdb);
4773 *ippdscdb = NULL;
4774 return DSERR_GENERIC;
4776 (*ippdscdb)->maplen = (*ippdscdb)->buflen = info.fragstotal * info.fragsize;
4778 /* map the DMA buffer */
4779 err = DSCDB_MapBuffer(*ippdscdb);
4780 if (err != DS_OK) {
4781 HeapFree(GetProcessHeap(),0,*ippdscdb);
4782 *ippdscdb = NULL;
4783 return err;
4786 /* capture buffer is ready to go */
4787 *pdwcbBufferSize = (*ippdscdb)->maplen;
4788 *ppbBuffer = (*ippdscdb)->mapping;
4790 /* some drivers need some extra nudging after mapping */
4791 WInDev[This->wDevID].ossdev->bInputEnabled = FALSE;
4792 enable = getEnables(WInDev[This->wDevID].ossdev);
4793 if (ioctl(WInDev[This->wDevID].ossdev->fd, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
4794 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",
4795 WInDev[This->wDevID].ossdev->dev_name, strerror(errno));
4796 return DSERR_GENERIC;
4799 This->capture_buffer = *ippdscdb;
4801 return DS_OK;
4804 static IDsCaptureDriverVtbl dscdvt =
4806 IDsCaptureDriverImpl_QueryInterface,
4807 IDsCaptureDriverImpl_AddRef,
4808 IDsCaptureDriverImpl_Release,
4809 IDsCaptureDriverImpl_GetDriverDesc,
4810 IDsCaptureDriverImpl_Open,
4811 IDsCaptureDriverImpl_Close,
4812 IDsCaptureDriverImpl_GetCaps,
4813 IDsCaptureDriverImpl_CreateCaptureBuffer
4816 static HRESULT WINAPI IDsCaptureDriverPropertySetImpl_Create(
4817 IDsCaptureDriverBufferImpl * dscdb,
4818 IDsCaptureDriverPropertySetImpl **pdscdps)
4820 IDsCaptureDriverPropertySetImpl * dscdps;
4821 TRACE("(%p,%p)\n",dscdb,pdscdps);
4823 dscdps = (IDsCaptureDriverPropertySetImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dscdps));
4824 if (dscdps == NULL) {
4825 WARN("out of memory\n");
4826 return DSERR_OUTOFMEMORY;
4829 dscdps->ref = 0;
4830 dscdps->lpVtbl = &dscdpsvt;
4831 dscdps->capture_buffer = dscdb;
4832 dscdb->property_set = dscdps;
4833 IDsCaptureDriverBuffer_AddRef((PIDSCDRIVER)dscdb);
4835 *pdscdps = dscdps;
4836 return DS_OK;
4839 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_Create(
4840 IDsCaptureDriverBufferImpl * dscdb,
4841 IDsCaptureDriverNotifyImpl **pdscdn)
4843 IDsCaptureDriverNotifyImpl * dscdn;
4844 TRACE("(%p,%p)\n",dscdb,pdscdn);
4846 dscdn = (IDsCaptureDriverNotifyImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dscdn));
4847 if (dscdn == NULL) {
4848 WARN("out of memory\n");
4849 return DSERR_OUTOFMEMORY;
4852 dscdn->ref = 0;
4853 dscdn->lpVtbl = &dscdnvt;
4854 dscdn->capture_buffer = dscdb;
4855 dscdb->notify = dscdn;
4856 IDsCaptureDriverBuffer_AddRef((PIDSCDRIVER)dscdb);
4858 *pdscdn = dscdn;
4859 return DS_OK;
4862 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
4864 IDsCaptureDriverImpl** idrv = (IDsCaptureDriverImpl**)drv;
4865 TRACE("(%d,%p)\n",wDevID,drv);
4867 /* the HAL isn't much better than the HEL if we can't do mmap() */
4868 if (!(WInDev[wDevID].ossdev->in_caps_support & WAVECAPS_DIRECTSOUND)) {
4869 ERR("DirectSoundCapture flag not set\n");
4870 MESSAGE("This sound card's driver does not support direct access\n");
4871 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
4872 return MMSYSERR_NOTSUPPORTED;
4875 *idrv = (IDsCaptureDriverImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverImpl));
4876 if (!*idrv)
4877 return MMSYSERR_NOMEM;
4878 (*idrv)->lpVtbl = &dscdvt;
4879 (*idrv)->ref = 1;
4881 (*idrv)->wDevID = wDevID;
4882 (*idrv)->capture_buffer = NULL;
4883 return MMSYSERR_NOERROR;
4886 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
4888 memcpy(desc, &(WInDev[wDevID].ossdev->ds_desc), sizeof(DSDRIVERDESC));
4889 return MMSYSERR_NOERROR;
4892 #else /* !HAVE_OSS */
4894 /**************************************************************************
4895 * wodMessage (WINEOSS.7)
4897 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
4898 DWORD dwParam1, DWORD dwParam2)
4900 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
4901 return MMSYSERR_NOTENABLED;
4904 /**************************************************************************
4905 * widMessage (WINEOSS.6)
4907 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
4908 DWORD dwParam1, DWORD dwParam2)
4910 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
4911 return MMSYSERR_NOTENABLED;
4914 #endif /* HAVE_OSS */