2 * Sample Wine Driver for Open Sound System (featured in Linux and FreeBSD)
4 * Copyright 1994 Martin Ayotte
5 * 1999 Eric Pouech (async playing in waveOut/waveIn)
6 * 2000 Eric Pouech (loops in waveOut)
7 * 2002 Eric Pouech (full duplex)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * pause in waveOut does not work correctly in loop mode
26 * Direct Sound Capture driver does not work (not complete yet)
29 /* an exact wodGetPosition is usually not worth the extra context switches,
30 * as we're going to have near fragment accuracy anyway */
31 #define EXACT_WODPOSITION
32 #define EXACT_WIDPOSITION
35 #include "wine/port.h"
46 #ifdef HAVE_SYS_IOCTL_H
47 # include <sys/ioctl.h>
49 #ifdef HAVE_SYS_MMAN_H
50 # include <sys/mman.h>
55 #ifdef HAVE_SYS_POLL_H
56 # include <sys/poll.h>
73 #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)
84 OSS_DEVICE OSS_Devices
[MAX_WAVEDRV
];
85 WINE_WAVEOUT WOutDev
[MAX_WAVEDRV
];
86 WINE_WAVEIN WInDev
[MAX_WAVEDRV
];
90 /* state diagram for waveOut writing:
92 * +---------+-------------+---------------+---------------------------------+
93 * | state | function | event | new state |
94 * +---------+-------------+---------------+---------------------------------+
95 * | | open() | | STOPPED |
96 * | PAUSED | write() | | PAUSED |
97 * | STOPPED | write() | <thrd create> | PLAYING |
98 * | PLAYING | write() | HEADER | PLAYING |
99 * | (other) | write() | <error> | |
100 * | (any) | pause() | PAUSING | PAUSED |
101 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
102 * | (any) | reset() | RESETTING | STOPPED |
103 * | (any) | close() | CLOSING | CLOSED |
104 * +---------+-------------+---------------+---------------------------------+
107 /* These strings used only for tracing */
108 static const char * getCmdString(enum win_wm_message msg
)
110 static char unknown
[32];
111 #define MSG_TO_STR(x) case x: return #x
113 MSG_TO_STR(WINE_WM_PAUSING
);
114 MSG_TO_STR(WINE_WM_RESTARTING
);
115 MSG_TO_STR(WINE_WM_RESETTING
);
116 MSG_TO_STR(WINE_WM_HEADER
);
117 MSG_TO_STR(WINE_WM_UPDATE
);
118 MSG_TO_STR(WINE_WM_BREAKLOOP
);
119 MSG_TO_STR(WINE_WM_CLOSING
);
120 MSG_TO_STR(WINE_WM_STARTING
);
121 MSG_TO_STR(WINE_WM_STOPPING
);
124 sprintf(unknown
, "UNKNOWN(0x%08x)", msg
);
128 int getEnables(OSS_DEVICE
*ossdev
)
130 return ( (ossdev
->bOutputEnabled
? PCM_ENABLE_OUTPUT
: 0) |
131 (ossdev
->bInputEnabled
? PCM_ENABLE_INPUT
: 0) );
134 static const char * getMessage(UINT msg
)
136 static char unknown
[32];
137 #define MSG_TO_STR(x) case x: return #x
139 MSG_TO_STR(DRVM_INIT
);
140 MSG_TO_STR(DRVM_EXIT
);
141 MSG_TO_STR(DRVM_ENABLE
);
142 MSG_TO_STR(DRVM_DISABLE
);
143 MSG_TO_STR(WIDM_OPEN
);
144 MSG_TO_STR(WIDM_CLOSE
);
145 MSG_TO_STR(WIDM_ADDBUFFER
);
146 MSG_TO_STR(WIDM_PREPARE
);
147 MSG_TO_STR(WIDM_UNPREPARE
);
148 MSG_TO_STR(WIDM_GETDEVCAPS
);
149 MSG_TO_STR(WIDM_GETNUMDEVS
);
150 MSG_TO_STR(WIDM_GETPOS
);
151 MSG_TO_STR(WIDM_RESET
);
152 MSG_TO_STR(WIDM_START
);
153 MSG_TO_STR(WIDM_STOP
);
154 MSG_TO_STR(WODM_OPEN
);
155 MSG_TO_STR(WODM_CLOSE
);
156 MSG_TO_STR(WODM_WRITE
);
157 MSG_TO_STR(WODM_PAUSE
);
158 MSG_TO_STR(WODM_GETPOS
);
159 MSG_TO_STR(WODM_BREAKLOOP
);
160 MSG_TO_STR(WODM_PREPARE
);
161 MSG_TO_STR(WODM_UNPREPARE
);
162 MSG_TO_STR(WODM_GETDEVCAPS
);
163 MSG_TO_STR(WODM_GETNUMDEVS
);
164 MSG_TO_STR(WODM_GETPITCH
);
165 MSG_TO_STR(WODM_SETPITCH
);
166 MSG_TO_STR(WODM_GETPLAYBACKRATE
);
167 MSG_TO_STR(WODM_SETPLAYBACKRATE
);
168 MSG_TO_STR(WODM_GETVOLUME
);
169 MSG_TO_STR(WODM_SETVOLUME
);
170 MSG_TO_STR(WODM_RESTART
);
171 MSG_TO_STR(WODM_RESET
);
172 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE
);
173 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE
);
174 MSG_TO_STR(DRV_QUERYDSOUNDIFACE
);
175 MSG_TO_STR(DRV_QUERYDSOUNDDESC
);
178 sprintf(unknown
, "UNKNOWN(0x%04x)", msg
);
182 static DWORD
wodDevInterfaceSize(UINT wDevID
, LPDWORD dwParam1
)
184 TRACE("(%u, %p)\n", wDevID
, dwParam1
);
186 *dwParam1
= MultiByteToWideChar(CP_ACP
, 0, WOutDev
[wDevID
].ossdev
->interface_name
, -1,
187 NULL
, 0 ) * sizeof(WCHAR
);
188 return MMSYSERR_NOERROR
;
191 static DWORD
wodDevInterface(UINT wDevID
, PWCHAR dwParam1
, DWORD dwParam2
)
193 if (dwParam2
>= MultiByteToWideChar(CP_ACP
, 0, WOutDev
[wDevID
].ossdev
->interface_name
, -1,
194 NULL
, 0 ) * sizeof(WCHAR
))
196 MultiByteToWideChar(CP_ACP
, 0, WOutDev
[wDevID
].ossdev
->interface_name
, -1,
197 dwParam1
, dwParam2
/ sizeof(WCHAR
));
198 return MMSYSERR_NOERROR
;
201 return MMSYSERR_INVALPARAM
;
204 static DWORD
widDevInterfaceSize(UINT wDevID
, LPDWORD dwParam1
)
206 TRACE("(%u, %p)\n", wDevID
, dwParam1
);
208 *dwParam1
= MultiByteToWideChar(CP_ACP
, 0, WInDev
[wDevID
].ossdev
->interface_name
, -1,
209 NULL
, 0 ) * sizeof(WCHAR
);
210 return MMSYSERR_NOERROR
;
213 static DWORD
widDevInterface(UINT wDevID
, PWCHAR dwParam1
, DWORD dwParam2
)
215 if (dwParam2
>= MultiByteToWideChar(CP_ACP
, 0, WInDev
[wDevID
].ossdev
->interface_name
, -1,
216 NULL
, 0 ) * sizeof(WCHAR
))
218 MultiByteToWideChar(CP_ACP
, 0, WInDev
[wDevID
].ossdev
->interface_name
, -1,
219 dwParam1
, dwParam2
/ sizeof(WCHAR
));
220 return MMSYSERR_NOERROR
;
223 return MMSYSERR_INVALPARAM
;
226 static DWORD
bytes_to_mmtime(LPMMTIME lpTime
, DWORD position
,
227 WAVEFORMATPCMEX
* format
)
229 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
230 lpTime
->wType
, format
->Format
.wBitsPerSample
, format
->Format
.nSamplesPerSec
,
231 format
->Format
.nChannels
, format
->Format
.nAvgBytesPerSec
);
232 TRACE("Position in bytes=%u\n", position
);
234 switch (lpTime
->wType
) {
236 lpTime
->u
.sample
= position
/ (format
->Format
.wBitsPerSample
/ 8 * format
->Format
.nChannels
);
237 TRACE("TIME_SAMPLES=%u\n", lpTime
->u
.sample
);
240 lpTime
->u
.ms
= 1000.0 * position
/ (format
->Format
.wBitsPerSample
/ 8 * format
->Format
.nChannels
* format
->Format
.nSamplesPerSec
);
241 TRACE("TIME_MS=%u\n", lpTime
->u
.ms
);
244 lpTime
->u
.smpte
.fps
= 30;
245 position
= position
/ (format
->Format
.wBitsPerSample
/ 8 * format
->Format
.nChannels
);
246 position
+= (format
->Format
.nSamplesPerSec
/ lpTime
->u
.smpte
.fps
) - 1; /* round up */
247 lpTime
->u
.smpte
.sec
= position
/ format
->Format
.nSamplesPerSec
;
248 position
-= lpTime
->u
.smpte
.sec
* format
->Format
.nSamplesPerSec
;
249 lpTime
->u
.smpte
.min
= lpTime
->u
.smpte
.sec
/ 60;
250 lpTime
->u
.smpte
.sec
-= 60 * lpTime
->u
.smpte
.min
;
251 lpTime
->u
.smpte
.hour
= lpTime
->u
.smpte
.min
/ 60;
252 lpTime
->u
.smpte
.min
-= 60 * lpTime
->u
.smpte
.hour
;
253 lpTime
->u
.smpte
.fps
= 30;
254 lpTime
->u
.smpte
.frame
= position
* lpTime
->u
.smpte
.fps
/ format
->Format
.nSamplesPerSec
;
255 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
256 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
257 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
260 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime
->wType
);
261 lpTime
->wType
= TIME_BYTES
;
264 lpTime
->u
.cb
= position
;
265 TRACE("TIME_BYTES=%u\n", lpTime
->u
.cb
);
268 return MMSYSERR_NOERROR
;
271 static BOOL
supportedFormat(LPWAVEFORMATEX wf
)
275 if (wf
->nSamplesPerSec
<DSBFREQUENCY_MIN
||wf
->nSamplesPerSec
>DSBFREQUENCY_MAX
)
278 if (wf
->wFormatTag
== WAVE_FORMAT_PCM
) {
279 if (wf
->nChannels
>= 1 && wf
->nChannels
<= MAX_CHANNELS
) {
280 if (wf
->wBitsPerSample
==8||wf
->wBitsPerSample
==16)
283 } else if (wf
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
) {
284 WAVEFORMATEXTENSIBLE
* wfex
= (WAVEFORMATEXTENSIBLE
*)wf
;
286 if (wf
->cbSize
== 22 && IsEqualGUID(&wfex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
)) {
287 if (wf
->nChannels
>=1 && wf
->nChannels
<= MAX_CHANNELS
) {
288 if (wf
->wBitsPerSample
==wfex
->Samples
.wValidBitsPerSample
) {
289 if (wf
->wBitsPerSample
==8||wf
->wBitsPerSample
==16)
292 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
295 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
297 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
302 void copy_format(LPWAVEFORMATEX wf1
, LPWAVEFORMATPCMEX wf2
)
304 ZeroMemory(wf2
, sizeof(wf2
));
305 if (wf1
->wFormatTag
== WAVE_FORMAT_PCM
)
306 memcpy(wf2
, wf1
, sizeof(PCMWAVEFORMAT
));
307 else if (wf1
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
308 memcpy(wf2
, wf1
, sizeof(WAVEFORMATPCMEX
));
310 memcpy(wf2
, wf1
, sizeof(WAVEFORMATEX
) + wf1
->cbSize
);
313 /*======================================================================*
314 * Low level WAVE implementation *
315 *======================================================================*/
317 /******************************************************************
320 * Low level device opening (from values stored in ossdev)
322 static DWORD
OSS_RawOpenDevice(OSS_DEVICE
* ossdev
, int strict_format
)
325 TRACE("(%p,%d)\n",ossdev
,strict_format
);
327 TRACE("open_access=%s\n",
328 ossdev
->open_access
== O_RDONLY
? "O_RDONLY" :
329 ossdev
->open_access
== O_WRONLY
? "O_WRONLY" :
330 ossdev
->open_access
== O_RDWR
? "O_RDWR" : "Unknown");
332 if ((fd
= open(ossdev
->dev_name
, ossdev
->open_access
|O_NDELAY
, 0)) == -1)
334 WARN("Couldn't open %s (%s)\n", ossdev
->dev_name
, strerror(errno
));
335 return (errno
== EBUSY
) ? MMSYSERR_ALLOCATED
: MMSYSERR_ERROR
;
337 fcntl(fd
, F_SETFD
, 1); /* set close on exec flag */
338 /* turn full duplex on if it has been requested */
339 if (ossdev
->open_access
== O_RDWR
&& ossdev
->full_duplex
) {
340 rc
= ioctl(fd
, SNDCTL_DSP_SETDUPLEX
, 0);
341 /* on *BSD, as full duplex is always enabled by default, this ioctl
342 * will fail with EINVAL
343 * so, we don't consider EINVAL an error here
345 if (rc
!= 0 && errno
!= EINVAL
) {
346 WARN("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev
->dev_name
, strerror(errno
));
351 if (ossdev
->audio_fragment
) {
352 rc
= ioctl(fd
, SNDCTL_DSP_SETFRAGMENT
, &ossdev
->audio_fragment
);
354 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev
->dev_name
, strerror(errno
));
359 /* First size and channels then samplerate */
360 if (ossdev
->format
>=0)
362 val
= ossdev
->format
;
363 rc
= ioctl(fd
, SNDCTL_DSP_SETFMT
, &ossdev
->format
);
364 if (rc
!= 0 || val
!= ossdev
->format
) {
365 TRACE("Can't set format to %d (returned %d)\n", val
, ossdev
->format
);
370 if (ossdev
->channels
>=0)
372 val
= ossdev
->channels
;
373 rc
= ioctl(fd
, SNDCTL_DSP_CHANNELS
, &ossdev
->channels
);
374 if (rc
!= 0 || val
!= ossdev
->channels
) {
375 TRACE("Can't set channels to %u (returned %d)\n", val
, ossdev
->channels
);
380 if (ossdev
->sample_rate
>=0)
382 val
= ossdev
->sample_rate
;
383 rc
= ioctl(fd
, SNDCTL_DSP_SPEED
, &ossdev
->sample_rate
);
384 if (rc
!= 0 || !NEAR_MATCH(val
, ossdev
->sample_rate
)) {
385 TRACE("Can't set sample_rate to %u (returned %d)\n", val
, ossdev
->sample_rate
);
392 if (ossdev
->bTriggerSupport
) {
394 rc
= ioctl(fd
, SNDCTL_DSP_GETTRIGGER
, &trigger
);
396 ERR("ioctl(%s, SNDCTL_DSP_GETTRIGGER) failed (%s)\n",
397 ossdev
->dev_name
, strerror(errno
));
401 ossdev
->bOutputEnabled
= ((trigger
& PCM_ENABLE_OUTPUT
) == PCM_ENABLE_OUTPUT
);
402 ossdev
->bInputEnabled
= ((trigger
& PCM_ENABLE_INPUT
) == PCM_ENABLE_INPUT
);
404 /* If we do not have full duplex, but they opened RDWR
405 ** (as you have to in order for an mmap to succeed)
406 ** then we start out with input off
408 if (ossdev
->open_access
== O_RDWR
&& !ossdev
->full_duplex
&&
409 ossdev
->bInputEnabled
&& ossdev
->bOutputEnabled
) {
410 ossdev
->bInputEnabled
= FALSE
;
411 trigger
&= ~PCM_ENABLE_INPUT
;
412 ioctl(fd
, SNDCTL_DSP_SETTRIGGER
, &trigger
);
415 ossdev
->bOutputEnabled
= TRUE
; /* OSS enables by default */
416 ossdev
->bInputEnabled
= TRUE
; /* OSS enables by default */
419 if (ossdev
->open_access
== O_RDONLY
)
420 ossdev
->bOutputEnabled
= FALSE
;
421 if (ossdev
->open_access
== O_WRONLY
)
422 ossdev
->bInputEnabled
= FALSE
;
424 return MMSYSERR_NOERROR
;
428 return WAVERR_BADFORMAT
;
431 return MMSYSERR_ERROR
;
434 /******************************************************************
437 * since OSS has poor capabilities in full duplex, we try here to let a program
438 * open the device for both waveout and wavein streams...
439 * this is hackish, but it's the way OSS interface is done...
441 DWORD
OSS_OpenDevice(OSS_DEVICE
* ossdev
, unsigned req_access
,
442 int* frag
, int strict_format
,
443 int sample_rate
, int channels
, int fmt
)
447 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev
,req_access
,frag
,strict_format
,sample_rate
,channels
,fmt
);
449 if (ossdev
->full_duplex
&& (req_access
== O_RDONLY
|| req_access
== O_WRONLY
))
451 TRACE("Opening RDWR because full_duplex=%d and req_access=%d\n",
452 ossdev
->full_duplex
,req_access
);
453 open_access
= O_RDWR
;
457 open_access
=req_access
;
460 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
461 if (ossdev
->open_count
== 0)
463 if (access(ossdev
->dev_name
, 0) != 0) return MMSYSERR_NODRIVER
;
465 ossdev
->audio_fragment
= (frag
) ? *frag
: 0;
466 ossdev
->sample_rate
= sample_rate
;
467 ossdev
->channels
= channels
;
468 ossdev
->format
= fmt
;
469 ossdev
->open_access
= open_access
;
470 ossdev
->owner_tid
= GetCurrentThreadId();
472 if ((ret
= OSS_RawOpenDevice(ossdev
,strict_format
)) != MMSYSERR_NOERROR
) return ret
;
473 if (ossdev
->full_duplex
&& ossdev
->bTriggerSupport
&&
474 (req_access
== O_RDONLY
|| req_access
== O_WRONLY
))
477 if (req_access
== O_WRONLY
)
478 ossdev
->bInputEnabled
=0;
480 ossdev
->bOutputEnabled
=0;
481 enable
= getEnables(ossdev
);
482 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable
);
483 if (ioctl(ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0)
484 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev
->dev_name
, enable
, strerror(errno
));
489 /* check we really open with the same parameters */
490 if (ossdev
->open_access
!= open_access
)
492 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
493 return WAVERR_BADFORMAT
;
496 /* check if the audio parameters are the same */
497 if (ossdev
->sample_rate
!= sample_rate
||
498 ossdev
->channels
!= channels
||
499 ossdev
->format
!= fmt
)
501 /* This is not a fatal error because MSACM might do the remapping */
502 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
503 "OSS doesn't allow us different parameters\n"
504 "audio_frag(%x/%x) sample_rate(%d/%d) channels(%d/%d) fmt(%d/%d)\n",
505 ossdev
->audio_fragment
, frag
? *frag
: 0,
506 ossdev
->sample_rate
, sample_rate
,
507 ossdev
->channels
, channels
,
508 ossdev
->format
, fmt
);
509 return WAVERR_BADFORMAT
;
511 /* check if the fragment sizes are the same */
512 if (ossdev
->audio_fragment
!= (frag
? *frag
: 0) )
514 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
515 "Please run winecfg, open \"Audio\" page and set\n"
516 "\"Hardware Acceleration\" to \"Emulation\".\n");
517 return WAVERR_BADFORMAT
;
519 if (GetCurrentThreadId() != ossdev
->owner_tid
)
521 WARN("Another thread is trying to access audio...\n");
522 return MMSYSERR_ERROR
;
524 if (ossdev
->full_duplex
&& ossdev
->bTriggerSupport
&&
525 (req_access
== O_RDONLY
|| req_access
== O_WRONLY
))
528 if (req_access
== O_WRONLY
)
529 ossdev
->bOutputEnabled
=1;
531 ossdev
->bInputEnabled
=1;
532 enable
= getEnables(ossdev
);
533 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable
);
534 if (ioctl(ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0)
535 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev
->dev_name
, enable
, strerror(errno
));
539 ossdev
->open_count
++;
541 return MMSYSERR_NOERROR
;
544 /******************************************************************
549 void OSS_CloseDevice(OSS_DEVICE
* ossdev
)
551 TRACE("(%p)\n",ossdev
);
552 if (ossdev
->open_count
>0) {
553 ossdev
->open_count
--;
555 WARN("OSS_CloseDevice called too many times\n");
557 if (ossdev
->open_count
== 0)
559 fcntl(ossdev
->fd
, F_SETFL
, fcntl(ossdev
->fd
, F_GETFL
) & ~O_NDELAY
);
560 /* reset the device before we close it in case it is in a bad state */
561 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
562 if (close(ossdev
->fd
) != 0) FIXME("Cannot close %d: %s\n", ossdev
->fd
, strerror(errno
));
566 /******************************************************************
569 * Resets the device. OSS Commercial requires the device to be closed
570 * after a SNDCTL_DSP_RESET ioctl call... this function implements
572 * FIXME: This causes problems when doing full duplex so we really
573 * only reset when not doing full duplex. We need to do this better
576 static DWORD
OSS_ResetDevice(OSS_DEVICE
* ossdev
)
578 DWORD ret
= MMSYSERR_NOERROR
;
579 int old_fd
= ossdev
->fd
;
580 TRACE("(%p)\n", ossdev
);
582 if (ossdev
->open_count
== 1) {
583 if (ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, NULL
) == -1)
585 perror("ioctl SNDCTL_DSP_RESET");
589 ret
= OSS_RawOpenDevice(ossdev
, 1);
590 TRACE("Changing fd from %d to %d\n", old_fd
, ossdev
->fd
);
592 WARN("Not resetting device because it is in full duplex mode!\n");
597 static const int win_std_oss_fmts
[2]={AFMT_U8
,AFMT_S16_LE
};
598 static const int win_std_rates
[5]={96000,48000,44100,22050,11025};
599 static const int win_std_formats
[2][2][5]=
600 {{{WAVE_FORMAT_96M08
, WAVE_FORMAT_48M08
, WAVE_FORMAT_4M08
,
601 WAVE_FORMAT_2M08
, WAVE_FORMAT_1M08
},
602 {WAVE_FORMAT_96S08
, WAVE_FORMAT_48S08
, WAVE_FORMAT_4S08
,
603 WAVE_FORMAT_2S08
, WAVE_FORMAT_1S08
}},
604 {{WAVE_FORMAT_96M16
, WAVE_FORMAT_48M16
, WAVE_FORMAT_4M16
,
605 WAVE_FORMAT_2M16
, WAVE_FORMAT_1M16
},
606 {WAVE_FORMAT_96S16
, WAVE_FORMAT_48S16
, WAVE_FORMAT_4S16
,
607 WAVE_FORMAT_2S16
, WAVE_FORMAT_1S16
}},
610 static void OSS_Info(int fd
)
612 /* Note that this only reports the formats supported by the hardware.
613 * The driver may support other formats and do the conversions in
614 * software which is why we don't use this value
616 int oss_mask
, oss_caps
;
617 if (ioctl(fd
, SNDCTL_DSP_GETFMTS
, &oss_mask
) >= 0) {
618 TRACE("Formats=%08x ( ", oss_mask
);
619 if (oss_mask
& AFMT_MU_LAW
) TRACE("AFMT_MU_LAW ");
620 if (oss_mask
& AFMT_A_LAW
) TRACE("AFMT_A_LAW ");
621 if (oss_mask
& AFMT_IMA_ADPCM
) TRACE("AFMT_IMA_ADPCM ");
622 if (oss_mask
& AFMT_U8
) TRACE("AFMT_U8 ");
623 if (oss_mask
& AFMT_S16_LE
) TRACE("AFMT_S16_LE ");
624 if (oss_mask
& AFMT_S16_BE
) TRACE("AFMT_S16_BE ");
625 if (oss_mask
& AFMT_S8
) TRACE("AFMT_S8 ");
626 if (oss_mask
& AFMT_U16_LE
) TRACE("AFMT_U16_LE ");
627 if (oss_mask
& AFMT_U16_BE
) TRACE("AFMT_U16_BE ");
628 if (oss_mask
& AFMT_MPEG
) TRACE("AFMT_MPEG ");
630 if (oss_mask
& AFMT_AC3
) TRACE("AFMT_AC3 ");
633 if (oss_mask
& AFMT_VORBIS
) TRACE("AFMT_VORBIS ");
636 if (oss_mask
& AFMT_S32_LE
) TRACE("AFMT_S32_LE ");
639 if (oss_mask
& AFMT_S32_BE
) TRACE("AFMT_S32_BE ");
642 if (oss_mask
& AFMT_FLOAT
) TRACE("AFMT_FLOAT ");
645 if (oss_mask
& AFMT_S24_LE
) TRACE("AFMT_S24_LE ");
648 if (oss_mask
& AFMT_S24_BE
) TRACE("AFMT_S24_BE ");
650 #ifdef AFMT_SPDIF_RAW
651 if (oss_mask
& AFMT_SPDIF_RAW
) TRACE("AFMT_SPDIF_RAW ");
655 if (ioctl(fd
, SNDCTL_DSP_GETCAPS
, &oss_caps
) >= 0) {
656 TRACE("Caps=%08x\n",oss_caps
);
657 TRACE("\tRevision: %d\n", oss_caps
&DSP_CAP_REVISION
);
658 TRACE("\tDuplex: %s\n", oss_caps
& DSP_CAP_DUPLEX
? "true" : "false");
659 TRACE("\tRealtime: %s\n", oss_caps
& DSP_CAP_REALTIME
? "true" : "false");
660 TRACE("\tBatch: %s\n", oss_caps
& DSP_CAP_BATCH
? "true" : "false");
661 TRACE("\tCoproc: %s\n", oss_caps
& DSP_CAP_COPROC
? "true" : "false");
662 TRACE("\tTrigger: %s\n", oss_caps
& DSP_CAP_TRIGGER
? "true" : "false");
663 TRACE("\tMmap: %s\n", oss_caps
& DSP_CAP_MMAP
? "true" : "false");
665 TRACE("\tMulti: %s\n", oss_caps
& DSP_CAP_MULTI
? "true" : "false");
668 TRACE("\tBind: %s\n", oss_caps
& DSP_CAP_BIND
? "true" : "false");
671 TRACE("\tInput: %s\n", oss_caps
& DSP_CAP_INPUT
? "true" : "false");
673 #ifdef DSP_CAP_OUTPUT
674 TRACE("\tOutput: %s\n", oss_caps
& DSP_CAP_OUTPUT
? "true" : "false");
676 #ifdef DSP_CAP_VIRTUAL
677 TRACE("\tVirtual: %s\n", oss_caps
& DSP_CAP_VIRTUAL
? "true" : "false");
679 #ifdef DSP_CAP_ANALOGOUT
680 TRACE("\tAnalog Out: %s\n", oss_caps
& DSP_CAP_ANALOGOUT
? "true" : "false");
682 #ifdef DSP_CAP_ANALOGIN
683 TRACE("\tAnalog In: %s\n", oss_caps
& DSP_CAP_ANALOGIN
? "true" : "false");
685 #ifdef DSP_CAP_DIGITALOUT
686 TRACE("\tDigital Out: %s\n", oss_caps
& DSP_CAP_DIGITALOUT
? "true" : "false");
688 #ifdef DSP_CAP_DIGITALIN
689 TRACE("\tDigital In: %s\n", oss_caps
& DSP_CAP_DIGITALIN
? "true" : "false");
691 #ifdef DSP_CAP_ADMASK
692 TRACE("\tA/D Mask: %s\n", oss_caps
& DSP_CAP_ADMASK
? "true" : "false");
694 #ifdef DSP_CAP_SHADOW
695 TRACE("\tShadow: %s\n", oss_caps
& DSP_CAP_SHADOW
? "true" : "false");
698 TRACE("\tChannel Mask: %x\n", oss_caps
& DSP_CH_MASK
);
701 TRACE("\tSlave: %s\n", oss_caps
& DSP_CAP_SLAVE
? "true" : "false");
706 /******************************************************************
711 static BOOL
OSS_WaveOutInit(OSS_DEVICE
* ossdev
)
715 BOOL has_mixer
= FALSE
;
716 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
718 if (OSS_OpenDevice(ossdev
, O_WRONLY
, NULL
, 0,-1,-1,-1) != 0)
721 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
723 #ifdef SOUND_MIXER_INFO
726 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
728 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
729 lstrcpynA(ossdev
->ds_desc
.szDesc
, info
.name
, sizeof(info
.name
));
730 strcpy(ossdev
->ds_desc
.szDrvname
, "wineoss.drv");
731 MultiByteToWideChar(CP_ACP
, 0, info
.name
, sizeof(info
.name
),
732 ossdev
->out_caps
.szPname
,
733 sizeof(ossdev
->out_caps
.szPname
) / sizeof(WCHAR
));
734 TRACE("%s: %s\n", ossdev
->mixer_name
, ossdev
->ds_desc
.szDesc
);
737 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
738 * implement it properly, and there are probably similar issues
739 * on other platforms, so we warn but try to go ahead.
741 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev
->mixer_name
);
745 WARN("open(%s) failed (%s)\n", ossdev
->mixer_name
, strerror(errno
));
748 #endif /* SOUND_MIXER_INFO */
750 if (WINE_TRACE_ON(wave
))
751 OSS_Info(ossdev
->fd
);
753 ossdev
->out_caps
.wMid
= 0x00FF; /* Manufac ID */
754 ossdev
->out_caps
.wPid
= 0x0001; /* Product ID */
756 ossdev
->out_caps
.vDriverVersion
= 0x0100;
757 ossdev
->out_caps
.wChannels
= 1;
758 ossdev
->out_caps
.dwFormats
= 0x00000000;
759 ossdev
->out_caps
.wReserved1
= 0;
760 ossdev
->out_caps
.dwSupport
= has_mixer
? WAVECAPS_VOLUME
: 0;
762 /* direct sound caps */
763 ossdev
->ds_caps
.dwFlags
= DSCAPS_CERTIFIED
;
764 ossdev
->ds_caps
.dwPrimaryBuffers
= 1;
765 ossdev
->ds_caps
.dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
766 ossdev
->ds_caps
.dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
768 /* We must first set the format and the stereo mode as some sound cards
769 * may support 44kHz mono but not 44kHz stereo. Also we must
770 * systematically check the return value of these ioctls as they will
771 * always succeed (see OSS Linux) but will modify the parameter to match
772 * whatever they support. The OSS specs also say we must first set the
773 * sample size, then the stereo and then the sample rate.
776 arg
=win_std_oss_fmts
[f
];
777 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
778 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
779 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
780 rc
,arg
,win_std_oss_fmts
[f
]);
784 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARY8BIT
;
786 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARY16BIT
;
788 for (c
= 1; c
<= MAX_CHANNELS
; c
++) {
790 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_CHANNELS
, &arg
);
792 if (rc
!=0 || arg
!=c
) {
793 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc
,arg
,c
);
797 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYMONO
;
799 ossdev
->out_caps
.wChannels
= 2;
801 ossdev
->out_caps
.dwSupport
|=WAVECAPS_LRVOLUME
;
802 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYSTEREO
;
804 ossdev
->out_caps
.wChannels
= c
;
806 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
807 arg
=win_std_rates
[r
];
808 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
809 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
810 rc
,arg
,win_std_rates
[r
],win_std_oss_fmts
[f
],c
);
811 if (rc
==0 && arg
!=0 && NEAR_MATCH(arg
,win_std_rates
[r
]) && c
< 3)
812 ossdev
->out_caps
.dwFormats
|=win_std_formats
[f
][c
-1][r
];
817 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
818 if (arg
& DSP_CAP_TRIGGER
)
819 ossdev
->bTriggerSupport
= TRUE
;
820 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
)) {
821 ossdev
->out_caps
.dwSupport
|= WAVECAPS_SAMPLEACCURATE
;
823 /* well, might as well use the DirectSound cap flag for something */
824 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
825 !(arg
& DSP_CAP_BATCH
)) {
826 ossdev
->out_caps
.dwSupport
|= WAVECAPS_DIRECTSOUND
;
828 ossdev
->ds_caps
.dwFlags
|= DSCAPS_EMULDRIVER
;
830 #ifdef DSP_CAP_MULTI /* not every oss has this */
831 /* check for hardware secondary buffer support (multi open) */
832 if ((arg
& DSP_CAP_MULTI
) &&
833 (ossdev
->out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
834 TRACE("hardware secondary buffer support available\n");
835 if (ossdev
->ds_caps
.dwFlags
& DSCAPS_PRIMARY8BIT
)
836 ossdev
->ds_caps
.dwFlags
|= DSCAPS_SECONDARY8BIT
;
837 if (ossdev
->ds_caps
.dwFlags
& DSCAPS_PRIMARY16BIT
)
838 ossdev
->ds_caps
.dwFlags
|= DSCAPS_SECONDARY16BIT
;
839 if (ossdev
->ds_caps
.dwFlags
& DSCAPS_PRIMARYMONO
)
840 ossdev
->ds_caps
.dwFlags
|= DSCAPS_SECONDARYMONO
;
841 if (ossdev
->ds_caps
.dwFlags
& DSCAPS_PRIMARYSTEREO
)
842 ossdev
->ds_caps
.dwFlags
|= DSCAPS_SECONDARYSTEREO
;
844 ossdev
->ds_caps
.dwMaxHwMixingAllBuffers
= 16;
845 ossdev
->ds_caps
.dwMaxHwMixingStaticBuffers
= 0;
846 ossdev
->ds_caps
.dwMaxHwMixingStreamingBuffers
= 16;
848 ossdev
->ds_caps
.dwFreeHwMixingAllBuffers
= 16;
849 ossdev
->ds_caps
.dwFreeHwMixingStaticBuffers
= 0;
850 ossdev
->ds_caps
.dwFreeHwMixingStreamingBuffers
= 16;
854 OSS_CloseDevice(ossdev
);
855 TRACE("out wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
856 ossdev
->out_caps
.wChannels
, ossdev
->out_caps
.dwFormats
,
857 ossdev
->out_caps
.dwSupport
);
861 /******************************************************************
866 static BOOL
OSS_WaveInInit(OSS_DEVICE
* ossdev
)
870 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
872 if (OSS_OpenDevice(ossdev
, O_RDONLY
, NULL
, 0,-1,-1,-1) != 0)
875 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
877 #ifdef SOUND_MIXER_INFO
880 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
882 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
883 MultiByteToWideChar(CP_ACP
, 0, info
.name
, -1,
884 ossdev
->in_caps
.szPname
,
885 sizeof(ossdev
->in_caps
.szPname
) / sizeof(WCHAR
));
886 TRACE("%s: %s\n", ossdev
->mixer_name
, ossdev
->ds_desc
.szDesc
);
888 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
889 * implement it properly, and there are probably similar issues
890 * on other platforms, so we warn but try to go ahead.
892 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev
->mixer_name
);
896 WARN("open(%s) failed (%s)\n", ossdev
->mixer_name
, strerror(errno
));
899 #endif /* SOUND_MIXER_INFO */
901 if (WINE_TRACE_ON(wave
))
902 OSS_Info(ossdev
->fd
);
904 ossdev
->in_caps
.wMid
= 0x00FF; /* Manufac ID */
905 ossdev
->in_caps
.wPid
= 0x0001; /* Product ID */
907 ossdev
->in_caps
.dwFormats
= 0x00000000;
908 ossdev
->in_caps
.wChannels
= 1;
909 ossdev
->in_caps
.wReserved1
= 0;
911 /* direct sound caps */
912 ossdev
->dsc_caps
.dwSize
= sizeof(ossdev
->dsc_caps
);
913 ossdev
->dsc_caps
.dwFlags
= 0;
914 ossdev
->dsc_caps
.dwFormats
= 0x00000000;
915 ossdev
->dsc_caps
.dwChannels
= 1;
917 /* See the comment in OSS_WaveOutInit for the loop order */
919 arg
=win_std_oss_fmts
[f
];
920 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
921 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
922 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
923 rc
,arg
,win_std_oss_fmts
[f
]);
927 for (c
= 1; c
<= MAX_CHANNELS
; c
++) {
929 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_CHANNELS
, &arg
);
931 if (rc
!=0 || arg
!=c
) {
932 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc
,arg
,c
);
936 ossdev
->in_caps
.wChannels
= c
;
937 ossdev
->dsc_caps
.dwChannels
= c
;
940 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
941 arg
=win_std_rates
[r
];
942 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
943 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc
,arg
,win_std_rates
[r
],win_std_oss_fmts
[f
],c
);
944 if (rc
==0 && NEAR_MATCH(arg
,win_std_rates
[r
]) && c
< 3)
945 ossdev
->in_caps
.dwFormats
|=win_std_formats
[f
][c
-1][r
];
946 ossdev
->dsc_caps
.dwFormats
|=win_std_formats
[f
][c
-1][r
];
951 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
952 if (arg
& DSP_CAP_TRIGGER
)
953 ossdev
->bTriggerSupport
= TRUE
;
954 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
955 !(arg
& DSP_CAP_BATCH
)) {
956 /* FIXME: enable the next statement if you want to work on the driver */
958 ossdev
->in_caps_support
|= WAVECAPS_DIRECTSOUND
;
961 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
))
962 ossdev
->in_caps_support
|= WAVECAPS_SAMPLEACCURATE
;
964 OSS_CloseDevice(ossdev
);
965 TRACE("in wChannels = %d, dwFormats = %08X, in_caps_support = %08X\n",
966 ossdev
->in_caps
.wChannels
, ossdev
->in_caps
.dwFormats
, ossdev
->in_caps_support
);
970 /******************************************************************
971 * OSS_WaveFullDuplexInit
975 static void OSS_WaveFullDuplexInit(OSS_DEVICE
* ossdev
)
980 BOOL has_mixer
= FALSE
;
981 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
983 /* The OSS documentation says we must call SNDCTL_SETDUPLEX
984 * *before* checking for SNDCTL_DSP_GETCAPS otherwise we may
985 * get the wrong result. This ioctl must even be done before
986 * setting the fragment size so that only OSS_RawOpenDevice is
987 * in a position to do it. So we set full_duplex speculatively
988 * and adjust right after.
990 ossdev
->full_duplex
=1;
991 rc
=OSS_OpenDevice(ossdev
, O_RDWR
, NULL
, 0,-1,-1,-1);
992 ossdev
->full_duplex
=0;
996 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
998 #ifdef SOUND_MIXER_INFO
1001 if ((mixer
= open(ossdev
->mixer_name
, O_RDWR
|O_NDELAY
)) >= 0) {
1003 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
1006 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
1007 * implement it properly, and there are probably similar issues
1008 * on other platforms, so we warn but try to go ahead.
1010 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev
->mixer_name
);
1014 WARN("open(%s) failed (%s)\n", ossdev
->mixer_name
, strerror(errno
));
1017 #endif /* SOUND_MIXER_INFO */
1019 TRACE("%s\n", ossdev
->ds_desc
.szDesc
);
1021 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &caps
) == 0)
1022 ossdev
->full_duplex
= (caps
& DSP_CAP_DUPLEX
);
1024 ossdev
->duplex_out_caps
= ossdev
->out_caps
;
1026 ossdev
->duplex_out_caps
.wChannels
= 1;
1027 ossdev
->duplex_out_caps
.dwFormats
= 0x00000000;
1028 ossdev
->duplex_out_caps
.dwSupport
= has_mixer
? WAVECAPS_VOLUME
: 0;
1030 if (WINE_TRACE_ON(wave
))
1031 OSS_Info(ossdev
->fd
);
1033 /* See the comment in OSS_WaveOutInit for the loop order */
1035 arg
=win_std_oss_fmts
[f
];
1036 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
1037 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
1038 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1039 rc
,arg
,win_std_oss_fmts
[f
]);
1043 for (c
= 1; c
<= MAX_CHANNELS
; c
++) {
1045 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_CHANNELS
, &arg
);
1046 if( rc
== -1) break;
1047 if (rc
!=0 || arg
!=c
) {
1048 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc
,arg
,c
);
1052 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYMONO
;
1053 } else if (c
== 2) {
1054 ossdev
->duplex_out_caps
.wChannels
= 2;
1056 ossdev
->duplex_out_caps
.dwSupport
|=WAVECAPS_LRVOLUME
;
1057 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYSTEREO
;
1059 ossdev
->duplex_out_caps
.wChannels
= c
;
1061 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
1062 arg
=win_std_rates
[r
];
1063 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
1064 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
1065 rc
,arg
,win_std_rates
[r
],win_std_oss_fmts
[f
],c
);
1066 if (rc
==0 && arg
!=0 && NEAR_MATCH(arg
,win_std_rates
[r
]) && c
< 3)
1067 ossdev
->duplex_out_caps
.dwFormats
|=win_std_formats
[f
][c
-1][r
];
1072 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
1073 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
)) {
1074 ossdev
->duplex_out_caps
.dwSupport
|= WAVECAPS_SAMPLEACCURATE
;
1076 /* well, might as well use the DirectSound cap flag for something */
1077 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
1078 !(arg
& DSP_CAP_BATCH
)) {
1079 ossdev
->duplex_out_caps
.dwSupport
|= WAVECAPS_DIRECTSOUND
;
1082 OSS_CloseDevice(ossdev
);
1083 TRACE("duplex wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
1084 ossdev
->duplex_out_caps
.wChannels
,
1085 ossdev
->duplex_out_caps
.dwFormats
,
1086 ossdev
->duplex_out_caps
.dwSupport
);
1089 static char* StrDup(const char* str
, const char* def
)
1094 dst
=HeapAlloc(GetProcessHeap(),0,strlen(str
)+1);
1099 /******************************************************************
1102 * Initialize internal structures from OSS information
1104 LRESULT
OSS_WaveInit(void)
1111 str
=getenv("AUDIODEV");
1114 OSS_Devices
[0].dev_name
=StrDup(str
,"");
1115 OSS_Devices
[0].mixer_name
=StrDup(getenv("MIXERDEV"),"/dev/mixer");
1116 for (i
= 1; i
< MAX_WAVEDRV
; ++i
)
1118 OSS_Devices
[i
].dev_name
=StrDup("",NULL
);
1119 OSS_Devices
[i
].mixer_name
=StrDup("",NULL
);
1124 OSS_Devices
[0].dev_name
=StrDup("/dev/dsp",NULL
);
1125 OSS_Devices
[0].mixer_name
=StrDup("/dev/mixer",NULL
);
1126 for (i
= 1; i
< MAX_WAVEDRV
; ++i
)
1128 OSS_Devices
[i
].dev_name
=HeapAlloc(GetProcessHeap(),0,11);
1129 sprintf(OSS_Devices
[i
].dev_name
, "/dev/dsp%d", i
);
1130 OSS_Devices
[i
].mixer_name
=HeapAlloc(GetProcessHeap(),0,13);
1131 sprintf(OSS_Devices
[i
].mixer_name
, "/dev/mixer%d", i
);
1135 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
1137 OSS_Devices
[i
].interface_name
=HeapAlloc(GetProcessHeap(),0,9+strlen(OSS_Devices
[i
].dev_name
)+1);
1138 sprintf(OSS_Devices
[i
].interface_name
, "wineoss: %s", OSS_Devices
[i
].dev_name
);
1141 /* start with output devices */
1142 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
1144 if (*OSS_Devices
[i
].dev_name
=='\0' || OSS_WaveOutInit(&OSS_Devices
[i
]))
1146 WOutDev
[numOutDev
].state
= WINE_WS_CLOSED
;
1147 WOutDev
[numOutDev
].ossdev
= &OSS_Devices
[i
];
1148 WOutDev
[numOutDev
].volume
= 0xffffffff;
1153 /* then do input devices */
1154 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
1156 if (*OSS_Devices
[i
].dev_name
=='\0' || OSS_WaveInInit(&OSS_Devices
[i
]))
1158 WInDev
[numInDev
].state
= WINE_WS_CLOSED
;
1159 WInDev
[numInDev
].ossdev
= &OSS_Devices
[i
];
1164 /* finish with the full duplex bits */
1165 for (i
= 0; i
< MAX_WAVEDRV
; i
++)
1166 if (*OSS_Devices
[i
].dev_name
!='\0')
1167 OSS_WaveFullDuplexInit(&OSS_Devices
[i
]);
1169 TRACE("%d wave out devices\n", numOutDev
);
1170 for (i
= 0; i
< numOutDev
; i
++) {
1171 TRACE("%d: %s, %s, %s\n", i
, WOutDev
[i
].ossdev
->dev_name
,
1172 WOutDev
[i
].ossdev
->mixer_name
, WOutDev
[i
].ossdev
->interface_name
);
1175 TRACE("%d wave in devices\n", numInDev
);
1176 for (i
= 0; i
< numInDev
; i
++) {
1177 TRACE("%d: %s, %s, %s\n", i
, WInDev
[i
].ossdev
->dev_name
,
1178 WInDev
[i
].ossdev
->mixer_name
, WInDev
[i
].ossdev
->interface_name
);
1184 /******************************************************************
1187 * Delete/clear internal structures of OSS information
1189 LRESULT
OSS_WaveExit(void)
1194 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
1196 HeapFree(GetProcessHeap(), 0, OSS_Devices
[i
].dev_name
);
1197 HeapFree(GetProcessHeap(), 0, OSS_Devices
[i
].mixer_name
);
1198 HeapFree(GetProcessHeap(), 0, OSS_Devices
[i
].interface_name
);
1201 ZeroMemory(OSS_Devices
, sizeof(OSS_Devices
));
1202 ZeroMemory(WOutDev
, sizeof(WOutDev
));
1203 ZeroMemory(WInDev
, sizeof(WInDev
));
1211 /******************************************************************
1212 * OSS_InitRingMessage
1214 * Initialize the ring of messages for passing between driver's caller and playback/record
1217 static int OSS_InitRingMessage(OSS_MSG_RING
* omr
)
1220 omr
->msg_tosave
= 0;
1221 #ifdef USE_PIPE_SYNC
1222 if (pipe(omr
->msg_pipe
) < 0) {
1223 omr
->msg_pipe
[0] = -1;
1224 omr
->msg_pipe
[1] = -1;
1225 ERR("could not create pipe, error=%s\n", strerror(errno
));
1228 omr
->msg_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1230 omr
->ring_buffer_size
= OSS_RING_BUFFER_INCREMENT
;
1231 omr
->messages
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,omr
->ring_buffer_size
* sizeof(OSS_MSG
));
1232 InitializeCriticalSection(&omr
->msg_crst
);
1233 omr
->msg_crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": OSS_MSG_RING.msg_crst");
1237 /******************************************************************
1238 * OSS_DestroyRingMessage
1241 static int OSS_DestroyRingMessage(OSS_MSG_RING
* omr
)
1243 #ifdef USE_PIPE_SYNC
1244 close(omr
->msg_pipe
[0]);
1245 close(omr
->msg_pipe
[1]);
1247 CloseHandle(omr
->msg_event
);
1249 HeapFree(GetProcessHeap(),0,omr
->messages
);
1250 omr
->msg_crst
.DebugInfo
->Spare
[0] = 0;
1251 DeleteCriticalSection(&omr
->msg_crst
);
1255 /******************************************************************
1256 * OSS_AddRingMessage
1258 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
1260 static int OSS_AddRingMessage(OSS_MSG_RING
* omr
, enum win_wm_message msg
, DWORD param
, BOOL wait
)
1262 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
1264 EnterCriticalSection(&omr
->msg_crst
);
1265 if ((omr
->msg_toget
== ((omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
)))
1267 int old_ring_buffer_size
= omr
->ring_buffer_size
;
1268 omr
->ring_buffer_size
+= OSS_RING_BUFFER_INCREMENT
;
1269 TRACE("omr->ring_buffer_size=%d\n",omr
->ring_buffer_size
);
1270 omr
->messages
= HeapReAlloc(GetProcessHeap(),0,omr
->messages
, omr
->ring_buffer_size
* sizeof(OSS_MSG
));
1271 /* Now we need to rearrange the ring buffer so that the new
1272 buffers just allocated are in between omr->msg_tosave and
1275 if (omr
->msg_tosave
< omr
->msg_toget
)
1277 memmove(&(omr
->messages
[omr
->msg_toget
+ OSS_RING_BUFFER_INCREMENT
]),
1278 &(omr
->messages
[omr
->msg_toget
]),
1279 sizeof(OSS_MSG
)*(old_ring_buffer_size
- omr
->msg_toget
)
1281 omr
->msg_toget
+= OSS_RING_BUFFER_INCREMENT
;
1286 hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1287 if (hEvent
== INVALID_HANDLE_VALUE
)
1289 ERR("can't create event !?\n");
1290 LeaveCriticalSection(&omr
->msg_crst
);
1293 if (omr
->msg_toget
!= omr
->msg_tosave
&& omr
->messages
[omr
->msg_toget
].msg
!= WINE_WM_HEADER
)
1294 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1295 omr
->msg_toget
,getCmdString(omr
->messages
[omr
->msg_toget
].msg
),
1296 omr
->msg_tosave
,getCmdString(omr
->messages
[omr
->msg_tosave
].msg
));
1298 /* fast messages have to be added at the start of the queue */
1299 omr
->msg_toget
= (omr
->msg_toget
+ omr
->ring_buffer_size
- 1) % omr
->ring_buffer_size
;
1300 omr
->messages
[omr
->msg_toget
].msg
= msg
;
1301 omr
->messages
[omr
->msg_toget
].param
= param
;
1302 omr
->messages
[omr
->msg_toget
].hEvent
= hEvent
;
1306 omr
->messages
[omr
->msg_tosave
].msg
= msg
;
1307 omr
->messages
[omr
->msg_tosave
].param
= param
;
1308 omr
->messages
[omr
->msg_tosave
].hEvent
= INVALID_HANDLE_VALUE
;
1309 omr
->msg_tosave
= (omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
;
1311 LeaveCriticalSection(&omr
->msg_crst
);
1312 /* signal a new message */
1316 /* wait for playback/record thread to have processed the message */
1317 WaitForSingleObject(hEvent
, INFINITE
);
1318 CloseHandle(hEvent
);
1323 /******************************************************************
1324 * OSS_RetrieveRingMessage
1326 * Get a message from the ring. Should be called by the playback/record thread.
1328 static int OSS_RetrieveRingMessage(OSS_MSG_RING
* omr
,
1329 enum win_wm_message
*msg
, DWORD
*param
, HANDLE
*hEvent
)
1331 EnterCriticalSection(&omr
->msg_crst
);
1333 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
1335 LeaveCriticalSection(&omr
->msg_crst
);
1339 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
1340 omr
->messages
[omr
->msg_toget
].msg
= 0;
1341 *param
= omr
->messages
[omr
->msg_toget
].param
;
1342 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
1343 omr
->msg_toget
= (omr
->msg_toget
+ 1) % omr
->ring_buffer_size
;
1345 LeaveCriticalSection(&omr
->msg_crst
);
1349 /******************************************************************
1350 * OSS_PeekRingMessage
1352 * Peek at a message from the ring but do not remove it.
1353 * Should be called by the playback/record thread.
1355 static int OSS_PeekRingMessage(OSS_MSG_RING
* omr
,
1356 enum win_wm_message
*msg
,
1357 DWORD
*param
, HANDLE
*hEvent
)
1359 EnterCriticalSection(&omr
->msg_crst
);
1361 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
1363 LeaveCriticalSection(&omr
->msg_crst
);
1367 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
1368 *param
= omr
->messages
[omr
->msg_toget
].param
;
1369 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
1370 LeaveCriticalSection(&omr
->msg_crst
);
1374 /*======================================================================*
1375 * Low level WAVE OUT implementation *
1376 *======================================================================*/
1378 /**************************************************************************
1379 * wodNotifyClient [internal]
1381 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
1383 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04X dwParam2 = %04X\n", wMsg
,
1384 wMsg
== WOM_OPEN
? "WOM_OPEN" : wMsg
== WOM_CLOSE
? "WOM_CLOSE" :
1385 wMsg
== WOM_DONE
? "WOM_DONE" : "Unknown", dwParam1
, dwParam2
);
1391 if (wwo
->wFlags
!= DCB_NULL
&&
1392 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
,
1393 (HDRVR
)wwo
->waveDesc
.hWave
, wMsg
,
1394 wwo
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
1395 WARN("can't notify client !\n");
1396 return MMSYSERR_ERROR
;
1400 FIXME("Unknown callback message %u\n", wMsg
);
1401 return MMSYSERR_INVALPARAM
;
1403 return MMSYSERR_NOERROR
;
1406 /**************************************************************************
1407 * wodUpdatePlayedTotal [internal]
1410 static BOOL
wodUpdatePlayedTotal(WINE_WAVEOUT
* wwo
, audio_buf_info
* info
)
1412 audio_buf_info dspspace
;
1414 if (!info
) info
= &dspspace
;
1416 if (ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_GETOSPACE
, info
) < 0) {
1417 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo
->ossdev
->dev_name
, strerror(errno
));
1421 /* GETOSPACE is not always accurate when we're down to the last fragment or two;
1422 ** we try to accommodate that here by assuming that the dsp is empty by looking
1423 ** at the clock rather than the result of GETOSPACE */
1424 notplayed
= wwo
->dwBufferSize
- info
->bytes
;
1425 if (notplayed
> 0 && notplayed
< (info
->fragsize
* 2))
1427 if (wwo
->dwProjectedFinishTime
&& GetTickCount() >= wwo
->dwProjectedFinishTime
)
1429 TRACE("Adjusting for a presumed OSS bug and assuming all data has been played.\n");
1430 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
;
1434 /* Some OSS drivers will clean up nicely if given a POST, so give 'em the chance... */
1435 ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_POST
, 0);
1438 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
- notplayed
;
1442 /**************************************************************************
1443 * wodPlayer_BeginWaveHdr [internal]
1445 * Makes the specified lpWaveHdr the currently playing wave header.
1446 * If the specified wave header is a begin loop and we're not already in
1447 * a loop, setup the loop.
1449 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
1451 wwo
->lpPlayPtr
= lpWaveHdr
;
1453 if (!lpWaveHdr
) return;
1455 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
) {
1456 if (wwo
->lpLoopPtr
) {
1457 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
1459 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
1460 wwo
->lpLoopPtr
= lpWaveHdr
;
1461 /* Windows does not touch WAVEHDR.dwLoops,
1462 * so we need to make an internal copy */
1463 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
1466 wwo
->dwPartialOffset
= 0;
1469 /**************************************************************************
1470 * wodPlayer_PlayPtrNext [internal]
1472 * Advance the play pointer to the next waveheader, looping if required.
1474 static LPWAVEHDR
wodPlayer_PlayPtrNext(WINE_WAVEOUT
* wwo
)
1476 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
1478 wwo
->dwPartialOffset
= 0;
1479 if ((lpWaveHdr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
) {
1480 /* We're at the end of a loop, loop if required */
1481 if (--wwo
->dwLoops
> 0) {
1482 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1484 /* Handle overlapping loops correctly */
1485 if (wwo
->lpLoopPtr
!= lpWaveHdr
&& (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)) {
1486 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1487 /* shall we consider the END flag for the closing loop or for
1488 * the opening one or for both ???
1489 * code assumes for closing loop only
1492 lpWaveHdr
= lpWaveHdr
->lpNext
;
1494 wwo
->lpLoopPtr
= NULL
;
1495 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
);
1498 /* We're not in a loop. Advance to the next wave header */
1499 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
= lpWaveHdr
->lpNext
);
1505 /**************************************************************************
1506 * wodPlayer_TicksTillEmpty [internal]
1507 * Returns the number of ticks until we think the DSP should be empty
1509 static DWORD
wodPlayer_TicksTillEmpty(const WINE_WAVEOUT
*wwo
)
1511 return ((wwo
->dwWrittenTotal
- wwo
->dwPlayedTotal
) * 1000)
1512 / wwo
->waveFormat
.Format
.nAvgBytesPerSec
;
1515 /**************************************************************************
1516 * wodPlayer_DSPWait [internal]
1517 * Returns the number of milliseconds to wait for the DSP buffer to write
1520 static DWORD
wodPlayer_DSPWait(const WINE_WAVEOUT
*wwo
)
1522 /* time for one fragment to be played */
1523 return wwo
->dwFragmentSize
* 1000 / wwo
->waveFormat
.Format
.nAvgBytesPerSec
;
1526 /**************************************************************************
1527 * wodPlayer_NotifyWait [internal]
1528 * Returns the number of milliseconds to wait before attempting to notify
1529 * completion of the specified wavehdr.
1530 * This is based on the number of bytes remaining to be written in the
1533 static DWORD
wodPlayer_NotifyWait(const WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
1537 if (lpWaveHdr
->reserved
< wwo
->dwPlayedTotal
) {
1540 dwMillis
= (lpWaveHdr
->reserved
- wwo
->dwPlayedTotal
) * 1000 / wwo
->waveFormat
.Format
.nAvgBytesPerSec
;
1541 if (!dwMillis
) dwMillis
= 1;
1548 /**************************************************************************
1549 * wodPlayer_WriteMaxFrags [internal]
1550 * Writes the maximum number of bytes possible to the DSP and returns
1551 * TRUE iff the current playPtr has been fully played
1553 static BOOL
wodPlayer_WriteMaxFrags(WINE_WAVEOUT
* wwo
, DWORD
* bytes
)
1555 DWORD dwLength
= wwo
->lpPlayPtr
->dwBufferLength
- wwo
->dwPartialOffset
;
1556 DWORD toWrite
= min(dwLength
, *bytes
);
1560 TRACE("Writing wavehdr %p.%u[%u]/%u\n",
1561 wwo
->lpPlayPtr
, wwo
->dwPartialOffset
, wwo
->lpPlayPtr
->dwBufferLength
, toWrite
);
1565 written
= write(wwo
->ossdev
->fd
, wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
, toWrite
);
1567 TRACE("write(%s, %p, %d) failed (%s) returned %d\n", wwo
->ossdev
->dev_name
,
1568 wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
, toWrite
, strerror(errno
), written
);
1575 if (written
>= dwLength
) {
1576 /* If we wrote all current wavehdr, skip to the next one */
1577 wodPlayer_PlayPtrNext(wwo
);
1580 /* Remove the amount written */
1581 wwo
->dwPartialOffset
+= written
;
1584 wwo
->dwWrittenTotal
+= written
;
1585 TRACE("dwWrittenTotal=%u\n", wwo
->dwWrittenTotal
);
1590 /**************************************************************************
1591 * wodPlayer_NotifyCompletions [internal]
1593 * Notifies and remove from queue all wavehdrs which have been played to
1594 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1595 * we notify all wavehdrs and remove them all from the queue even if they
1596 * are unplayed or part of a loop.
1598 static DWORD
wodPlayer_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
1600 LPWAVEHDR lpWaveHdr
;
1602 /* Start from lpQueuePtr and keep notifying until:
1603 * - we hit an unwritten wavehdr
1604 * - we hit the beginning of a running loop
1605 * - we hit a wavehdr which hasn't finished playing
1608 while ((lpWaveHdr
= wwo
->lpQueuePtr
) &&
1610 (lpWaveHdr
!= wwo
->lpPlayPtr
&&
1611 lpWaveHdr
!= wwo
->lpLoopPtr
&&
1612 lpWaveHdr
->reserved
<= wwo
->dwPlayedTotal
))) {
1614 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
1616 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1617 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1619 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
1624 lpWaveHdr
= wwo
->lpQueuePtr
;
1625 if (!lpWaveHdr
) {TRACE("Empty queue\n"); break;}
1628 if (lpWaveHdr
== wwo
->lpPlayPtr
) {TRACE("play %p\n", lpWaveHdr
); break;}
1629 if (lpWaveHdr
== wwo
->lpLoopPtr
) {TRACE("loop %p\n", lpWaveHdr
); break;}
1630 if (lpWaveHdr
->reserved
> wwo
->dwPlayedTotal
) {TRACE("still playing %p (%u/%u)\n", lpWaveHdr
, lpWaveHdr
->reserved
, wwo
->dwPlayedTotal
);break;}
1632 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
1634 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1635 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1637 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
1640 return (lpWaveHdr
&& lpWaveHdr
!= wwo
->lpPlayPtr
&& lpWaveHdr
!= wwo
->lpLoopPtr
) ?
1641 wodPlayer_NotifyWait(wwo
, lpWaveHdr
) : INFINITE
;
1644 /**************************************************************************
1645 * wodPlayer_Reset [internal]
1647 * wodPlayer helper. Resets current output stream.
1649 static void wodPlayer_Reset(WINE_WAVEOUT
* wwo
, BOOL reset
)
1651 wodUpdatePlayedTotal(wwo
, NULL
);
1652 /* updates current notify list */
1653 wodPlayer_NotifyCompletions(wwo
, FALSE
);
1655 /* flush all possible output */
1656 if (OSS_ResetDevice(wwo
->ossdev
) != MMSYSERR_NOERROR
)
1659 wwo
->state
= WINE_WS_STOPPED
;
1664 enum win_wm_message msg
;
1668 /* remove any buffer */
1669 wodPlayer_NotifyCompletions(wwo
, TRUE
);
1671 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
1672 wwo
->state
= WINE_WS_STOPPED
;
1673 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
= 0;
1674 /* Clear partial wavehdr */
1675 wwo
->dwPartialOffset
= 0;
1677 /* remove any existing message in the ring */
1678 EnterCriticalSection(&wwo
->msgRing
.msg_crst
);
1679 /* return all pending headers in queue */
1680 while (OSS_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
))
1682 if (msg
!= WINE_WM_HEADER
)
1684 FIXME("shouldn't have headers left\n");
1688 ((LPWAVEHDR
)param
)->dwFlags
&= ~WHDR_INQUEUE
;
1689 ((LPWAVEHDR
)param
)->dwFlags
|= WHDR_DONE
;
1691 wodNotifyClient(wwo
, WOM_DONE
, param
, 0);
1693 RESET_OMR(&wwo
->msgRing
);
1694 LeaveCriticalSection(&wwo
->msgRing
.msg_crst
);
1696 if (wwo
->lpLoopPtr
) {
1697 /* complicated case, not handled yet (could imply modifying the loop counter */
1698 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1699 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1700 wwo
->dwPartialOffset
= 0;
1701 wwo
->dwWrittenTotal
= wwo
->dwPlayedTotal
; /* this is wrong !!! */
1704 DWORD sz
= wwo
->dwPartialOffset
;
1706 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1707 /* compute the max size playable from lpQueuePtr */
1708 for (ptr
= wwo
->lpQueuePtr
; ptr
!= wwo
->lpPlayPtr
; ptr
= ptr
->lpNext
) {
1709 sz
+= ptr
->dwBufferLength
;
1711 /* because the reset lpPlayPtr will be lpQueuePtr */
1712 if (wwo
->dwWrittenTotal
> wwo
->dwPlayedTotal
+ sz
) ERR("grin\n");
1713 wwo
->dwPartialOffset
= sz
- (wwo
->dwWrittenTotal
- wwo
->dwPlayedTotal
);
1714 wwo
->dwWrittenTotal
= wwo
->dwPlayedTotal
;
1715 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
;
1717 wwo
->state
= WINE_WS_PAUSED
;
1721 /**************************************************************************
1722 * wodPlayer_ProcessMessages [internal]
1724 static void wodPlayer_ProcessMessages(WINE_WAVEOUT
* wwo
)
1726 LPWAVEHDR lpWaveHdr
;
1727 enum win_wm_message msg
;
1731 while (OSS_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
)) {
1732 TRACE("Received %s %x\n", getCmdString(msg
), param
);
1734 case WINE_WM_PAUSING
:
1735 wodPlayer_Reset(wwo
, FALSE
);
1738 case WINE_WM_RESTARTING
:
1739 if (wwo
->state
== WINE_WS_PAUSED
)
1741 wwo
->state
= WINE_WS_PLAYING
;
1745 case WINE_WM_HEADER
:
1746 lpWaveHdr
= (LPWAVEHDR
)param
;
1748 /* insert buffer at the end of queue */
1751 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
1754 if (!wwo
->lpPlayPtr
)
1755 wodPlayer_BeginWaveHdr(wwo
,lpWaveHdr
);
1756 if (wwo
->state
== WINE_WS_STOPPED
)
1757 wwo
->state
= WINE_WS_PLAYING
;
1759 case WINE_WM_RESETTING
:
1760 wodPlayer_Reset(wwo
, TRUE
);
1763 case WINE_WM_UPDATE
:
1764 wodUpdatePlayedTotal(wwo
, NULL
);
1767 case WINE_WM_BREAKLOOP
:
1768 if (wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpLoopPtr
!= NULL
) {
1769 /* ensure exit at end of current loop */
1774 case WINE_WM_CLOSING
:
1775 /* sanity check: this should not happen since the device must have been reset before */
1776 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
1778 wwo
->state
= WINE_WS_CLOSED
;
1781 /* shouldn't go here */
1783 FIXME("unknown message %d\n", msg
);
1789 /**************************************************************************
1790 * wodPlayer_FeedDSP [internal]
1791 * Feed as much sound data as we can into the DSP and return the number of
1792 * milliseconds before it will be necessary to feed the DSP again.
1794 static DWORD
wodPlayer_FeedDSP(WINE_WAVEOUT
* wwo
)
1796 audio_buf_info dspspace
;
1799 if (!wodUpdatePlayedTotal(wwo
, &dspspace
)) return INFINITE
;
1800 availInQ
= dspspace
.bytes
;
1801 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1802 dspspace
.fragments
, dspspace
.fragstotal
, dspspace
.fragsize
, dspspace
.bytes
);
1804 /* no more room... no need to try to feed */
1805 if (dspspace
.fragments
!= 0) {
1806 /* Feed from partial wavehdr */
1807 if (wwo
->lpPlayPtr
&& wwo
->dwPartialOffset
!= 0) {
1808 wodPlayer_WriteMaxFrags(wwo
, &availInQ
);
1811 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1812 if (wwo
->dwPartialOffset
== 0 && wwo
->lpPlayPtr
) {
1814 TRACE("Setting time to elapse for %p to %u\n",
1815 wwo
->lpPlayPtr
, wwo
->dwWrittenTotal
+ wwo
->lpPlayPtr
->dwBufferLength
);
1816 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1817 wwo
->lpPlayPtr
->reserved
= wwo
->dwWrittenTotal
+ wwo
->lpPlayPtr
->dwBufferLength
;
1818 } while (wodPlayer_WriteMaxFrags(wwo
, &availInQ
) && wwo
->lpPlayPtr
&& availInQ
> 0);
1821 if (wwo
->bNeedPost
) {
1822 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1823 * if it didn't get one, we give it the other */
1824 if (wwo
->dwBufferSize
< availInQ
+ 2 * wwo
->dwFragmentSize
)
1825 ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_POST
, 0);
1826 wwo
->bNeedPost
= FALSE
;
1830 return wodPlayer_DSPWait(wwo
);
1834 /**************************************************************************
1835 * wodPlayer [internal]
1837 static DWORD CALLBACK
wodPlayer(LPVOID pmt
)
1839 WORD uDevID
= (DWORD
)pmt
;
1840 WINE_WAVEOUT
* wwo
= (WINE_WAVEOUT
*)&WOutDev
[uDevID
];
1841 DWORD dwNextFeedTime
= INFINITE
; /* Time before DSP needs feeding */
1842 DWORD dwNextNotifyTime
= INFINITE
; /* Time before next wave completion */
1845 wwo
->state
= WINE_WS_STOPPED
;
1846 SetEvent(wwo
->hStartUpEvent
);
1849 /** Wait for the shortest time before an action is required. If there
1850 * are no pending actions, wait forever for a command.
1852 dwSleepTime
= min(dwNextFeedTime
, dwNextNotifyTime
);
1853 TRACE("waiting %ums (%u,%u)\n", dwSleepTime
, dwNextFeedTime
, dwNextNotifyTime
);
1854 WAIT_OMR(&wwo
->msgRing
, dwSleepTime
);
1855 wodPlayer_ProcessMessages(wwo
);
1856 if (wwo
->state
== WINE_WS_PLAYING
) {
1857 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1858 if (dwNextFeedTime
!= INFINITE
)
1859 wwo
->dwProjectedFinishTime
= GetTickCount() + wodPlayer_TicksTillEmpty(wwo
);
1861 wwo
->dwProjectedFinishTime
= 0;
1863 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1864 if (dwNextFeedTime
== INFINITE
) {
1865 /* FeedDSP ran out of data, but before flushing, */
1866 /* check that a notification didn't give us more */
1867 wodPlayer_ProcessMessages(wwo
);
1868 if (!wwo
->lpPlayPtr
) {
1869 TRACE("flushing\n");
1870 ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_SYNC
, 0);
1871 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
;
1872 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1874 TRACE("recovering\n");
1875 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1879 dwNextFeedTime
= dwNextNotifyTime
= INFINITE
;
1884 /**************************************************************************
1885 * wodGetDevCaps [internal]
1887 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSW lpCaps
, DWORD dwSize
)
1889 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
1891 if (lpCaps
== NULL
) {
1892 WARN("not enabled\n");
1893 return MMSYSERR_NOTENABLED
;
1896 if (wDevID
>= numOutDev
) {
1897 WARN("numOutDev reached !\n");
1898 return MMSYSERR_BADDEVICEID
;
1901 if (WOutDev
[wDevID
].ossdev
->open_access
== O_RDWR
)
1902 memcpy(lpCaps
, &WOutDev
[wDevID
].ossdev
->duplex_out_caps
, min(dwSize
, sizeof(*lpCaps
)));
1904 memcpy(lpCaps
, &WOutDev
[wDevID
].ossdev
->out_caps
, min(dwSize
, sizeof(*lpCaps
)));
1906 return MMSYSERR_NOERROR
;
1909 /**************************************************************************
1910 * wodOpen [internal]
1912 DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1916 audio_buf_info info
;
1919 TRACE("(%u, %p[cb=%08x], %08X);\n", wDevID
, lpDesc
, lpDesc
->dwCallback
, dwFlags
);
1920 if (lpDesc
== NULL
) {
1921 WARN("Invalid Parameter !\n");
1922 return MMSYSERR_INVALPARAM
;
1924 if (wDevID
>= numOutDev
) {
1925 TRACE("MAX_WAVOUTDRV reached !\n");
1926 return MMSYSERR_BADDEVICEID
;
1929 /* only PCM format is supported so far... */
1930 if (!supportedFormat(lpDesc
->lpFormat
)) {
1931 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1932 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1933 lpDesc
->lpFormat
->nSamplesPerSec
);
1934 return WAVERR_BADFORMAT
;
1937 if (dwFlags
& WAVE_FORMAT_QUERY
) {
1938 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1939 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1940 lpDesc
->lpFormat
->nSamplesPerSec
);
1941 return MMSYSERR_NOERROR
;
1944 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
1945 lpDesc
->lpFormat
->nSamplesPerSec
,
1946 lpDesc
->lpFormat
->wBitsPerSample
,
1947 lpDesc
->lpFormat
->nChannels
,
1948 lpDesc
->lpFormat
->wFormatTag
== WAVE_FORMAT_PCM
? "WAVE_FORMAT_PCM" :
1949 lpDesc
->lpFormat
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
? "WAVE_FORMAT_EXTENSIBLE" :
1952 wwo
= &WOutDev
[wDevID
];
1954 if ((dwFlags
& WAVE_DIRECTSOUND
) &&
1955 !(wwo
->ossdev
->duplex_out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
))
1956 /* not supported, ignore it */
1957 dwFlags
&= ~WAVE_DIRECTSOUND
;
1959 if (dwFlags
& WAVE_DIRECTSOUND
) {
1960 if (wwo
->ossdev
->duplex_out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
1961 /* we have realtime DirectSound, fragments just waste our time,
1962 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1963 audio_fragment
= 0x0020000B;
1965 /* to approximate realtime, we must use small fragments,
1966 * let's try to fragment the above 64KB (256 * 2^8) */
1967 audio_fragment
= 0x01000008;
1969 /* A wave device must have a worst case latency of 10 ms so calculate
1970 * the largest fragment size less than 10 ms long.
1972 int fsize
= lpDesc
->lpFormat
->nAvgBytesPerSec
/ 100; /* 10 ms chunk */
1974 while ((1 << shift
) <= fsize
)
1977 audio_fragment
= 0x00100000 + shift
; /* 16 fragments of 2^shift */
1980 TRACE("requesting %d %d byte fragments (%d ms/fragment)\n",
1981 audio_fragment
>> 16, 1 << (audio_fragment
& 0xffff),
1982 ((1 << (audio_fragment
& 0xffff)) * 1000) / lpDesc
->lpFormat
->nAvgBytesPerSec
);
1984 if (wwo
->state
!= WINE_WS_CLOSED
) {
1985 WARN("already allocated\n");
1986 return MMSYSERR_ALLOCATED
;
1989 /* we want to be able to mmap() the device, which means it must be opened readable,
1990 * otherwise mmap() will fail (at least under Linux) */
1991 ret
= OSS_OpenDevice(wwo
->ossdev
,
1992 (dwFlags
& WAVE_DIRECTSOUND
) ? O_RDWR
: O_WRONLY
,
1994 (dwFlags
& WAVE_DIRECTSOUND
) ? 0 : 1,
1995 lpDesc
->lpFormat
->nSamplesPerSec
,
1996 lpDesc
->lpFormat
->nChannels
,
1997 (lpDesc
->lpFormat
->wBitsPerSample
== 16)
1998 ? AFMT_S16_LE
: AFMT_U8
);
1999 if ((ret
==MMSYSERR_NOERROR
) && (dwFlags
& WAVE_DIRECTSOUND
)) {
2000 lpDesc
->lpFormat
->nSamplesPerSec
=wwo
->ossdev
->sample_rate
;
2001 lpDesc
->lpFormat
->nChannels
=wwo
->ossdev
->channels
;
2002 lpDesc
->lpFormat
->wBitsPerSample
=(wwo
->ossdev
->format
== AFMT_U8
? 8 : 16);
2003 lpDesc
->lpFormat
->nBlockAlign
=lpDesc
->lpFormat
->nChannels
*lpDesc
->lpFormat
->wBitsPerSample
/8;
2004 lpDesc
->lpFormat
->nAvgBytesPerSec
=lpDesc
->lpFormat
->nSamplesPerSec
*lpDesc
->lpFormat
->nBlockAlign
;
2005 TRACE("OSS_OpenDevice returned this format: %dx%dx%d\n",
2006 lpDesc
->lpFormat
->nSamplesPerSec
,
2007 lpDesc
->lpFormat
->wBitsPerSample
,
2008 lpDesc
->lpFormat
->nChannels
);
2010 if (ret
!= 0) return ret
;
2011 wwo
->state
= WINE_WS_STOPPED
;
2013 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2015 memcpy(&wwo
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
2016 copy_format(lpDesc
->lpFormat
, &wwo
->waveFormat
);
2018 if (wwo
->waveFormat
.Format
.wBitsPerSample
== 0) {
2019 WARN("Resetting zeroed wBitsPerSample\n");
2020 wwo
->waveFormat
.Format
.wBitsPerSample
= 8 *
2021 (wwo
->waveFormat
.Format
.nAvgBytesPerSec
/
2022 wwo
->waveFormat
.Format
.nSamplesPerSec
) /
2023 wwo
->waveFormat
.Format
.nChannels
;
2025 /* Read output space info for future reference */
2026 if (ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
2027 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo
->ossdev
->dev_name
, strerror(errno
));
2028 OSS_CloseDevice(wwo
->ossdev
);
2029 wwo
->state
= WINE_WS_CLOSED
;
2030 return MMSYSERR_NOTENABLED
;
2033 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info
.fragstotal
,
2034 info
.fragsize
, (info
.fragsize
* 1000) / (wwo
->ossdev
->sample_rate
*
2035 wwo
->ossdev
->channels
* (wwo
->ossdev
->format
== AFMT_U8
? 1 : 2)));
2037 /* Check that fragsize is correct per our settings above */
2038 if ((info
.fragsize
> 1024) && (LOWORD(audio_fragment
) <= 10)) {
2039 /* we've tried to set 1K fragments or less, but it didn't work */
2040 ERR("fragment size set failed, size is now %d\n", info
.fragsize
);
2041 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
2042 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
2045 /* Remember fragsize and total buffer size for future use */
2046 wwo
->dwFragmentSize
= info
.fragsize
;
2047 wwo
->dwBufferSize
= info
.fragstotal
* info
.fragsize
;
2048 wwo
->dwPlayedTotal
= 0;
2049 wwo
->dwWrittenTotal
= 0;
2050 wwo
->bNeedPost
= TRUE
;
2052 TRACE("fd=%d fragstotal=%d fragsize=%d BufferSize=%d\n",
2053 wwo
->ossdev
->fd
, info
.fragstotal
, info
.fragsize
, wwo
->dwBufferSize
);
2054 if (wwo
->dwFragmentSize
% wwo
->waveFormat
.Format
.nBlockAlign
) {
2055 ERR("Fragment doesn't contain an integral number of data blocks fragsize=%d BlockAlign=%d\n",wwo
->dwFragmentSize
,wwo
->waveFormat
.Format
.nBlockAlign
);
2056 /* Some SoundBlaster 16 cards return an incorrect (odd) fragment
2057 * size for 16 bit sound. This will cause a system crash when we try
2058 * to write just the specified odd number of bytes. So if we
2059 * detect something is wrong we'd better fix it.
2061 wwo
->dwFragmentSize
-=wwo
->dwFragmentSize
% wwo
->waveFormat
.Format
.nBlockAlign
;
2064 OSS_InitRingMessage(&wwo
->msgRing
);
2066 wwo
->hStartUpEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2067 wwo
->hThread
= CreateThread(NULL
, 0, wodPlayer
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwo
->dwThreadID
));
2069 SetThreadPriority(wwo
->hThread
, THREAD_PRIORITY_TIME_CRITICAL
);
2070 WaitForSingleObject(wwo
->hStartUpEvent
, INFINITE
);
2071 CloseHandle(wwo
->hStartUpEvent
);
2072 wwo
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
2074 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2075 wwo
->waveFormat
.Format
.wBitsPerSample
, wwo
->waveFormat
.Format
.nAvgBytesPerSec
,
2076 wwo
->waveFormat
.Format
.nSamplesPerSec
, wwo
->waveFormat
.Format
.nChannels
,
2077 wwo
->waveFormat
.Format
.nBlockAlign
);
2079 return wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
2082 /**************************************************************************
2083 * wodClose [internal]
2085 static DWORD
wodClose(WORD wDevID
)
2087 DWORD ret
= MMSYSERR_NOERROR
;
2090 TRACE("(%u);\n", wDevID
);
2092 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2093 WARN("bad device ID !\n");
2094 return MMSYSERR_BADDEVICEID
;
2097 wwo
= &WOutDev
[wDevID
];
2098 if (wwo
->lpQueuePtr
) {
2099 WARN("buffers still playing !\n");
2100 ret
= WAVERR_STILLPLAYING
;
2102 if (wwo
->hThread
!= INVALID_HANDLE_VALUE
) {
2103 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
2106 OSS_DestroyRingMessage(&wwo
->msgRing
);
2108 OSS_CloseDevice(wwo
->ossdev
);
2109 wwo
->state
= WINE_WS_CLOSED
;
2110 wwo
->dwFragmentSize
= 0;
2111 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
2116 /**************************************************************************
2117 * wodWrite [internal]
2120 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
2122 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
2124 /* first, do the sanity checks... */
2125 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2126 WARN("bad dev ID !\n");
2127 return MMSYSERR_BADDEVICEID
;
2130 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
2131 return WAVERR_UNPREPARED
;
2133 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
2134 return WAVERR_STILLPLAYING
;
2136 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
2137 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
2138 lpWaveHdr
->lpNext
= 0;
2140 if ((lpWaveHdr
->dwBufferLength
& (WOutDev
[wDevID
].waveFormat
.Format
.nBlockAlign
- 1)) != 0)
2142 WARN("WaveHdr length isn't a multiple of the PCM block size: %d %% %d\n",lpWaveHdr
->dwBufferLength
,WOutDev
[wDevID
].waveFormat
.Format
.nBlockAlign
);
2143 lpWaveHdr
->dwBufferLength
&= ~(WOutDev
[wDevID
].waveFormat
.Format
.nBlockAlign
- 1);
2146 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
2148 return MMSYSERR_NOERROR
;
2151 /**************************************************************************
2152 * wodPause [internal]
2154 static DWORD
wodPause(WORD wDevID
)
2156 TRACE("(%u);!\n", wDevID
);
2158 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2159 WARN("bad device ID !\n");
2160 return MMSYSERR_BADDEVICEID
;
2163 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_PAUSING
, 0, TRUE
);
2165 return MMSYSERR_NOERROR
;
2168 /**************************************************************************
2169 * wodRestart [internal]
2171 static DWORD
wodRestart(WORD wDevID
)
2173 TRACE("(%u);\n", wDevID
);
2175 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2176 WARN("bad device ID !\n");
2177 return MMSYSERR_BADDEVICEID
;
2180 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESTARTING
, 0, TRUE
);
2182 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2183 /* FIXME: Myst crashes with this ... hmm -MM
2184 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2187 return MMSYSERR_NOERROR
;
2190 /**************************************************************************
2191 * wodReset [internal]
2193 static DWORD
wodReset(WORD wDevID
)
2195 TRACE("(%u);\n", wDevID
);
2197 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2198 WARN("bad device ID !\n");
2199 return MMSYSERR_BADDEVICEID
;
2202 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
2204 return MMSYSERR_NOERROR
;
2207 /**************************************************************************
2208 * wodGetPosition [internal]
2210 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
2214 TRACE("(%u, %p, %u);\n", wDevID
, lpTime
, uSize
);
2216 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2217 WARN("bad device ID !\n");
2218 return MMSYSERR_BADDEVICEID
;
2221 if (lpTime
== NULL
) {
2222 WARN("invalid parameter: lpTime == NULL\n");
2223 return MMSYSERR_INVALPARAM
;
2226 wwo
= &WOutDev
[wDevID
];
2227 #ifdef EXACT_WODPOSITION
2228 if (wwo
->ossdev
->open_access
== O_RDWR
) {
2229 if (wwo
->ossdev
->duplex_out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
2230 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
2232 if (wwo
->ossdev
->out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
2233 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
2237 return bytes_to_mmtime(lpTime
, wwo
->dwPlayedTotal
, &wwo
->waveFormat
);
2240 /**************************************************************************
2241 * wodBreakLoop [internal]
2243 static DWORD
wodBreakLoop(WORD wDevID
)
2245 TRACE("(%u);\n", wDevID
);
2247 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2248 WARN("bad device ID !\n");
2249 return MMSYSERR_BADDEVICEID
;
2251 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_BREAKLOOP
, 0, TRUE
);
2252 return MMSYSERR_NOERROR
;
2255 /**************************************************************************
2256 * wodGetVolume [internal]
2258 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
2263 DWORD last_left
, last_right
;
2265 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
2267 if (lpdwVol
== NULL
) {
2268 WARN("not enabled\n");
2269 return MMSYSERR_NOTENABLED
;
2271 if (wDevID
>= numOutDev
) {
2272 WARN("invalid parameter\n");
2273 return MMSYSERR_INVALPARAM
;
2275 if (WOutDev
[wDevID
].ossdev
->open_access
== O_RDWR
) {
2276 if (!(WOutDev
[wDevID
].ossdev
->duplex_out_caps
.dwSupport
& WAVECAPS_VOLUME
)) {
2277 TRACE("Volume not supported\n");
2278 return MMSYSERR_NOTSUPPORTED
;
2281 if (!(WOutDev
[wDevID
].ossdev
->out_caps
.dwSupport
& WAVECAPS_VOLUME
)) {
2282 TRACE("Volume not supported\n");
2283 return MMSYSERR_NOTSUPPORTED
;
2287 if ((mixer
= open(WOutDev
[wDevID
].ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) < 0) {
2288 WARN("mixer device not available !\n");
2289 return MMSYSERR_NOTENABLED
;
2291 if (ioctl(mixer
, SOUND_MIXER_READ_PCM
, &volume
) == -1) {
2293 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)\n",
2294 WOutDev
[wDevID
].ossdev
->mixer_name
, strerror(errno
));
2295 return MMSYSERR_NOTENABLED
;
2299 left
= LOBYTE(volume
);
2300 right
= HIBYTE(volume
);
2301 TRACE("left=%d right=%d !\n", left
, right
);
2302 last_left
= (LOWORD(WOutDev
[wDevID
].volume
) * 100) / 0xFFFFl
;
2303 last_right
= (HIWORD(WOutDev
[wDevID
].volume
) * 100) / 0xFFFFl
;
2304 TRACE("last_left=%d last_right=%d !\n", last_left
, last_right
);
2305 if (last_left
== left
&& last_right
== right
)
2306 *lpdwVol
= WOutDev
[wDevID
].volume
;
2308 *lpdwVol
= ((left
* 0xFFFFl
) / 100) + (((right
* 0xFFFFl
) / 100) << 16);
2309 return MMSYSERR_NOERROR
;
2312 /**************************************************************************
2313 * wodSetVolume [internal]
2315 DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
2321 TRACE("(%u, %08X);\n", wDevID
, dwParam
);
2323 left
= (LOWORD(dwParam
) * 100) / 0xFFFFl
;
2324 right
= (HIWORD(dwParam
) * 100) / 0xFFFFl
;
2325 volume
= left
+ (right
<< 8);
2327 if (wDevID
>= numOutDev
) {
2328 WARN("invalid parameter: wDevID > %d\n", numOutDev
);
2329 return MMSYSERR_INVALPARAM
;
2331 if (WOutDev
[wDevID
].ossdev
->open_access
== O_RDWR
) {
2332 if (!(WOutDev
[wDevID
].ossdev
->duplex_out_caps
.dwSupport
& WAVECAPS_VOLUME
)) {
2333 TRACE("Volume not supported\n");
2334 return MMSYSERR_NOTSUPPORTED
;
2337 if (!(WOutDev
[wDevID
].ossdev
->out_caps
.dwSupport
& WAVECAPS_VOLUME
)) {
2338 TRACE("Volume not supported\n");
2339 return MMSYSERR_NOTSUPPORTED
;
2342 if ((mixer
= open(WOutDev
[wDevID
].ossdev
->mixer_name
, O_WRONLY
|O_NDELAY
)) < 0) {
2343 WARN("open(%s) failed (%s)\n", WOutDev
[wDevID
].ossdev
->mixer_name
, strerror(errno
));
2344 return MMSYSERR_NOTENABLED
;
2346 if (ioctl(mixer
, SOUND_MIXER_WRITE_PCM
, &volume
) == -1) {
2348 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n",
2349 WOutDev
[wDevID
].ossdev
->mixer_name
, strerror(errno
));
2350 return MMSYSERR_NOTENABLED
;
2352 TRACE("volume=%04x\n", (unsigned)volume
);
2355 /* save requested volume */
2356 WOutDev
[wDevID
].volume
= dwParam
;
2358 return MMSYSERR_NOERROR
;
2361 /**************************************************************************
2362 * wodMessage (WINEOSS.7)
2364 DWORD WINAPI
OSS_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
2365 DWORD dwParam1
, DWORD dwParam2
)
2367 TRACE("(%u, %s, %08X, %08X, %08X);\n",
2368 wDevID
, getMessage(wMsg
), dwUser
, dwParam1
, dwParam2
);
2375 /* FIXME: Pretend this is supported */
2377 case WODM_OPEN
: return wodOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
2378 case WODM_CLOSE
: return wodClose (wDevID
);
2379 case WODM_WRITE
: return wodWrite (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
2380 case WODM_PAUSE
: return wodPause (wDevID
);
2381 case WODM_GETPOS
: return wodGetPosition (wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
2382 case WODM_BREAKLOOP
: return wodBreakLoop (wDevID
);
2383 case WODM_PREPARE
: return MMSYSERR_NOTSUPPORTED
;
2384 case WODM_UNPREPARE
: return MMSYSERR_NOTSUPPORTED
;
2385 case WODM_GETDEVCAPS
: return wodGetDevCaps (wDevID
, (LPWAVEOUTCAPSW
)dwParam1
, dwParam2
);
2386 case WODM_GETNUMDEVS
: return numOutDev
;
2387 case WODM_GETPITCH
: return MMSYSERR_NOTSUPPORTED
;
2388 case WODM_SETPITCH
: return MMSYSERR_NOTSUPPORTED
;
2389 case WODM_GETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
2390 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
2391 case WODM_GETVOLUME
: return wodGetVolume (wDevID
, (LPDWORD
)dwParam1
);
2392 case WODM_SETVOLUME
: return wodSetVolume (wDevID
, dwParam1
);
2393 case WODM_RESTART
: return wodRestart (wDevID
);
2394 case WODM_RESET
: return wodReset (wDevID
);
2396 case DRV_QUERYDEVICEINTERFACESIZE
: return wodDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
2397 case DRV_QUERYDEVICEINTERFACE
: return wodDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
2398 case DRV_QUERYDSOUNDIFACE
: return wodDsCreate (wDevID
, (PIDSDRIVER
*)dwParam1
);
2399 case DRV_QUERYDSOUNDDESC
: return wodDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
2401 FIXME("unknown message %d!\n", wMsg
);
2403 return MMSYSERR_NOTSUPPORTED
;
2406 /*======================================================================*
2407 * Low level WAVE IN implementation *
2408 *======================================================================*/
2410 /**************************************************************************
2411 * widNotifyClient [internal]
2413 static DWORD
widNotifyClient(WINE_WAVEIN
* wwi
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
2415 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04X dwParam2 = %04X\n", wMsg
,
2416 wMsg
== WIM_OPEN
? "WIM_OPEN" : wMsg
== WIM_CLOSE
? "WIM_CLOSE" :
2417 wMsg
== WIM_DATA
? "WIM_DATA" : "Unknown", dwParam1
, dwParam2
);
2423 if (wwi
->wFlags
!= DCB_NULL
&&
2424 !DriverCallback(wwi
->waveDesc
.dwCallback
, wwi
->wFlags
,
2425 (HDRVR
)wwi
->waveDesc
.hWave
, wMsg
,
2426 wwi
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
2427 WARN("can't notify client !\n");
2428 return MMSYSERR_ERROR
;
2432 FIXME("Unknown callback message %u\n", wMsg
);
2433 return MMSYSERR_INVALPARAM
;
2435 return MMSYSERR_NOERROR
;
2438 /**************************************************************************
2439 * widGetDevCaps [internal]
2441 static DWORD
widGetDevCaps(WORD wDevID
, LPWAVEINCAPSW lpCaps
, DWORD dwSize
)
2443 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
2445 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
2447 if (wDevID
>= numInDev
) {
2448 TRACE("numOutDev reached !\n");
2449 return MMSYSERR_BADDEVICEID
;
2452 memcpy(lpCaps
, &WInDev
[wDevID
].ossdev
->in_caps
, min(dwSize
, sizeof(*lpCaps
)));
2453 return MMSYSERR_NOERROR
;
2456 /**************************************************************************
2457 * widRecorder_ReadHeaders [internal]
2459 static void widRecorder_ReadHeaders(WINE_WAVEIN
* wwi
)
2461 enum win_wm_message tmp_msg
;
2466 while (OSS_RetrieveRingMessage(&wwi
->msgRing
, &tmp_msg
, &tmp_param
, &tmp_ev
)) {
2467 if (tmp_msg
== WINE_WM_HEADER
) {
2469 lpWaveHdr
= (LPWAVEHDR
)tmp_param
;
2470 lpWaveHdr
->lpNext
= 0;
2472 if (wwi
->lpQueuePtr
== 0)
2473 wwi
->lpQueuePtr
= lpWaveHdr
;
2475 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2479 ERR("should only have headers left\n");
2484 /**************************************************************************
2485 * widRecorder [internal]
2487 static DWORD CALLBACK
widRecorder(LPVOID pmt
)
2489 WORD uDevID
= (DWORD
)pmt
;
2490 WINE_WAVEIN
* wwi
= (WINE_WAVEIN
*)&WInDev
[uDevID
];
2494 LPVOID buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, wwi
->dwFragmentSize
);
2495 char *pOffset
= buffer
;
2496 audio_buf_info info
;
2498 enum win_wm_message msg
;
2503 wwi
->state
= WINE_WS_STOPPED
;
2504 wwi
->dwTotalRecorded
= 0;
2505 wwi
->dwTotalRead
= 0;
2506 wwi
->lpQueuePtr
= NULL
;
2508 SetEvent(wwi
->hStartUpEvent
);
2510 /* disable input so capture will begin when triggered */
2511 wwi
->ossdev
->bInputEnabled
= FALSE
;
2512 enable
= getEnables(wwi
->ossdev
);
2513 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0)
2514 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
->dev_name
, strerror(errno
));
2516 /* the soundblaster live needs a micro wake to get its recording started
2517 * (or GETISPACE will have 0 frags all the time)
2519 read(wwi
->ossdev
->fd
, &xs
, 4);
2521 /* make sleep time to be # of ms to output a fragment */
2522 dwSleepTime
= (wwi
->dwFragmentSize
* 1000) / wwi
->waveFormat
.Format
.nAvgBytesPerSec
;
2523 TRACE("sleeptime=%d ms\n", dwSleepTime
);
2526 /* wait for dwSleepTime or an event in thread's queue */
2527 /* FIXME: could improve wait time depending on queue state,
2528 * ie, number of queued fragments
2531 if (wwi
->lpQueuePtr
!= NULL
&& wwi
->state
== WINE_WS_PLAYING
)
2533 lpWaveHdr
= wwi
->lpQueuePtr
;
2535 ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_GETISPACE
, &info
);
2536 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info
.fragments
, info
.fragsize
, info
.fragstotal
, info
.bytes
);
2538 /* read all the fragments accumulated so far */
2539 while ((info
.fragments
> 0) && (wwi
->lpQueuePtr
))
2543 if (lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
>= wwi
->dwFragmentSize
)
2545 /* directly read fragment in wavehdr */
2546 bytesRead
= read(wwi
->ossdev
->fd
,
2547 lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2548 wwi
->dwFragmentSize
);
2550 TRACE("bytesRead=%d (direct)\n", bytesRead
);
2551 if (bytesRead
!= (DWORD
) -1)
2553 /* update number of bytes recorded in current buffer and by this device */
2554 lpWaveHdr
->dwBytesRecorded
+= bytesRead
;
2555 wwi
->dwTotalRead
+= bytesRead
;
2556 wwi
->dwTotalRecorded
= wwi
->dwTotalRead
;
2558 /* buffer is full. notify client */
2559 if (lpWaveHdr
->dwBytesRecorded
== lpWaveHdr
->dwBufferLength
)
2561 /* must copy the value of next waveHdr, because we have no idea of what
2562 * will be done with the content of lpWaveHdr in callback
2564 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2566 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2567 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2569 wwi
->lpQueuePtr
= lpNext
;
2570 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2574 TRACE("read(%s, %p, %d) failed (%s)\n", wwi
->ossdev
->dev_name
,
2575 lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2576 wwi
->dwFragmentSize
, strerror(errno
));
2581 /* read the fragment in a local buffer */
2582 bytesRead
= read(wwi
->ossdev
->fd
, buffer
, wwi
->dwFragmentSize
);
2585 TRACE("bytesRead=%d (local)\n", bytesRead
);
2587 if (bytesRead
== (DWORD
) -1) {
2588 TRACE("read(%s, %p, %d) failed (%s)\n", wwi
->ossdev
->dev_name
,
2589 buffer
, wwi
->dwFragmentSize
, strerror(errno
));
2593 /* copy data in client buffers */
2594 while (bytesRead
!= (DWORD
) -1 && bytesRead
> 0)
2596 DWORD dwToCopy
= min (bytesRead
, lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
);
2598 memcpy(lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2602 /* update number of bytes recorded in current buffer and by this device */
2603 lpWaveHdr
->dwBytesRecorded
+= dwToCopy
;
2604 wwi
->dwTotalRead
+= dwToCopy
;
2605 wwi
->dwTotalRecorded
= wwi
->dwTotalRead
;
2606 bytesRead
-= dwToCopy
;
2607 pOffset
+= dwToCopy
;
2609 /* client buffer is full. notify client */
2610 if (lpWaveHdr
->dwBytesRecorded
== lpWaveHdr
->dwBufferLength
)
2612 /* must copy the value of next waveHdr, because we have no idea of what
2613 * will be done with the content of lpWaveHdr in callback
2615 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2616 TRACE("lpNext=%p\n", lpNext
);
2618 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2619 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2621 wwi
->lpQueuePtr
= lpNext
;
2622 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2625 if (!lpNext
&& bytesRead
) {
2626 /* before we give up, check for more header messages */
2627 while (OSS_PeekRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
))
2629 if (msg
== WINE_WM_HEADER
) {
2631 OSS_RetrieveRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
);
2632 hdr
= ((LPWAVEHDR
)param
);
2633 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg
), hdr
, ev
);
2635 if (lpWaveHdr
== 0) {
2636 /* new head of queue */
2637 wwi
->lpQueuePtr
= lpWaveHdr
= hdr
;
2639 /* insert buffer at the end of queue */
2641 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2648 if (lpWaveHdr
== 0) {
2649 /* no more buffer to copy data to, but we did read more.
2650 * what hasn't been copied will be dropped
2652 WARN("buffer under run! %u bytes dropped.\n", bytesRead
);
2653 wwi
->lpQueuePtr
= NULL
;
2663 WAIT_OMR(&wwi
->msgRing
, dwSleepTime
);
2665 while (OSS_RetrieveRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
))
2667 TRACE("msg=%s param=0x%x\n", getCmdString(msg
), param
);
2669 case WINE_WM_PAUSING
:
2670 wwi
->state
= WINE_WS_PAUSED
;
2671 /*FIXME("Device should stop recording\n");*/
2674 case WINE_WM_STARTING
:
2675 wwi
->state
= WINE_WS_PLAYING
;
2677 if (wwi
->ossdev
->bTriggerSupport
)
2679 /* start the recording */
2680 wwi
->ossdev
->bInputEnabled
= TRUE
;
2681 enable
= getEnables(wwi
->ossdev
);
2682 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2683 wwi
->ossdev
->bInputEnabled
= FALSE
;
2684 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
->dev_name
, strerror(errno
));
2689 unsigned char data
[4];
2690 /* read 4 bytes to start the recording */
2691 read(wwi
->ossdev
->fd
, data
, 4);
2696 case WINE_WM_HEADER
:
2697 lpWaveHdr
= (LPWAVEHDR
)param
;
2698 lpWaveHdr
->lpNext
= 0;
2700 /* insert buffer at the end of queue */
2703 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2707 case WINE_WM_STOPPING
:
2708 if (wwi
->state
!= WINE_WS_STOPPED
)
2710 if (wwi
->ossdev
->bTriggerSupport
)
2712 /* stop the recording */
2713 wwi
->ossdev
->bInputEnabled
= FALSE
;
2714 enable
= getEnables(wwi
->ossdev
);
2715 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2716 wwi
->ossdev
->bInputEnabled
= FALSE
;
2717 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
->dev_name
, strerror(errno
));
2721 /* read any headers in queue */
2722 widRecorder_ReadHeaders(wwi
);
2724 /* return current buffer to app */
2725 lpWaveHdr
= wwi
->lpQueuePtr
;
2728 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2729 TRACE("stop %p %p\n", lpWaveHdr
, lpWaveHdr
->lpNext
);
2730 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2731 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2732 wwi
->lpQueuePtr
= lpNext
;
2733 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2736 wwi
->state
= WINE_WS_STOPPED
;
2739 case WINE_WM_RESETTING
:
2740 if (wwi
->state
!= WINE_WS_STOPPED
)
2742 if (wwi
->ossdev
->bTriggerSupport
)
2744 /* stop the recording */
2745 wwi
->ossdev
->bInputEnabled
= FALSE
;
2746 enable
= getEnables(wwi
->ossdev
);
2747 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2748 wwi
->ossdev
->bInputEnabled
= FALSE
;
2749 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
->dev_name
, strerror(errno
));
2753 wwi
->state
= WINE_WS_STOPPED
;
2754 wwi
->dwTotalRecorded
= 0;
2755 wwi
->dwTotalRead
= 0;
2757 /* read any headers in queue */
2758 widRecorder_ReadHeaders(wwi
);
2760 /* return all buffers to the app */
2761 for (lpWaveHdr
= wwi
->lpQueuePtr
; lpWaveHdr
; lpWaveHdr
= lpWaveHdr
->lpNext
) {
2762 TRACE("reset %p %p\n", lpWaveHdr
, lpWaveHdr
->lpNext
);
2763 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2764 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2765 wwi
->lpQueuePtr
= lpWaveHdr
->lpNext
;
2766 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2769 wwi
->lpQueuePtr
= NULL
;
2772 case WINE_WM_UPDATE
:
2773 if (wwi
->state
== WINE_WS_PLAYING
) {
2774 audio_buf_info tmp_info
;
2775 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_GETISPACE
, &tmp_info
) < 0)
2776 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", wwi
->ossdev
->dev_name
, strerror(errno
));
2778 wwi
->dwTotalRecorded
= wwi
->dwTotalRead
+ tmp_info
.bytes
;
2782 case WINE_WM_CLOSING
:
2784 wwi
->state
= WINE_WS_CLOSED
;
2786 HeapFree(GetProcessHeap(), 0, buffer
);
2788 /* shouldn't go here */
2790 FIXME("unknown message %d\n", msg
);
2796 /* just for not generating compilation warnings... should never be executed */
2801 /**************************************************************************
2802 * widOpen [internal]
2804 DWORD
widOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
2807 audio_buf_info info
;
2811 TRACE("(%u, %p, %08X);\n", wDevID
, lpDesc
, dwFlags
);
2812 if (lpDesc
== NULL
) {
2813 WARN("Invalid Parameter !\n");
2814 return MMSYSERR_INVALPARAM
;
2816 if (wDevID
>= numInDev
) {
2817 WARN("bad device id: %d >= %d\n", wDevID
, numInDev
);
2818 return MMSYSERR_BADDEVICEID
;
2821 /* only PCM format is supported so far... */
2822 if (!supportedFormat(lpDesc
->lpFormat
)) {
2823 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2824 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
2825 lpDesc
->lpFormat
->nSamplesPerSec
);
2826 return WAVERR_BADFORMAT
;
2829 if (dwFlags
& WAVE_FORMAT_QUERY
) {
2830 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2831 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
2832 lpDesc
->lpFormat
->nSamplesPerSec
);
2833 return MMSYSERR_NOERROR
;
2836 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2837 lpDesc
->lpFormat
->nSamplesPerSec
,
2838 lpDesc
->lpFormat
->wBitsPerSample
,
2839 lpDesc
->lpFormat
->nChannels
,
2840 lpDesc
->lpFormat
->wFormatTag
== WAVE_FORMAT_PCM
? "WAVE_FORMAT_PCM" :
2841 lpDesc
->lpFormat
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
? "WAVE_FORMAT_EXTENSIBLE" :
2844 wwi
= &WInDev
[wDevID
];
2846 if (wwi
->state
!= WINE_WS_CLOSED
) return MMSYSERR_ALLOCATED
;
2848 if ((dwFlags
& WAVE_DIRECTSOUND
) &&
2849 !(wwi
->ossdev
->in_caps_support
& WAVECAPS_DIRECTSOUND
))
2850 /* not supported, ignore it */
2851 dwFlags
&= ~WAVE_DIRECTSOUND
;
2853 if (dwFlags
& WAVE_DIRECTSOUND
) {
2854 TRACE("has DirectSoundCapture driver\n");
2855 if (wwi
->ossdev
->in_caps_support
& WAVECAPS_SAMPLEACCURATE
)
2856 /* we have realtime DirectSound, fragments just waste our time,
2857 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2858 audio_fragment
= 0x0020000B;
2860 /* to approximate realtime, we must use small fragments,
2861 * let's try to fragment the above 64KB (256 * 2^8) */
2862 audio_fragment
= 0x01000008;
2864 TRACE("doesn't have DirectSoundCapture driver\n");
2865 if (wwi
->ossdev
->open_count
> 0) {
2866 TRACE("Using output device audio_fragment\n");
2867 /* FIXME: This may not be optimal for capture but it allows us
2868 * to do hardware playback without hardware capture. */
2869 audio_fragment
= wwi
->ossdev
->audio_fragment
;
2871 /* A wave device must have a worst case latency of 10 ms so calculate
2872 * the largest fragment size less than 10 ms long.
2874 int fsize
= lpDesc
->lpFormat
->nAvgBytesPerSec
/ 100; /* 10 ms chunk */
2876 while ((1 << shift
) <= fsize
)
2879 audio_fragment
= 0x00100000 + shift
; /* 16 fragments of 2^shift */
2883 TRACE("requesting %d %d byte fragments (%d ms)\n", audio_fragment
>> 16,
2884 1 << (audio_fragment
& 0xffff),
2885 ((1 << (audio_fragment
& 0xffff)) * 1000) / lpDesc
->lpFormat
->nAvgBytesPerSec
);
2887 ret
= OSS_OpenDevice(wwi
->ossdev
, O_RDONLY
, &audio_fragment
,
2889 lpDesc
->lpFormat
->nSamplesPerSec
,
2890 lpDesc
->lpFormat
->nChannels
,
2891 (lpDesc
->lpFormat
->wBitsPerSample
== 16)
2892 ? AFMT_S16_LE
: AFMT_U8
);
2893 if (ret
!= 0) return ret
;
2894 wwi
->state
= WINE_WS_STOPPED
;
2896 if (wwi
->lpQueuePtr
) {
2897 WARN("Should have an empty queue (%p)\n", wwi
->lpQueuePtr
);
2898 wwi
->lpQueuePtr
= NULL
;
2900 wwi
->dwTotalRecorded
= 0;
2901 wwi
->dwTotalRead
= 0;
2902 wwi
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2904 memcpy(&wwi
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
2905 copy_format(lpDesc
->lpFormat
, &wwi
->waveFormat
);
2907 if (wwi
->waveFormat
.Format
.wBitsPerSample
== 0) {
2908 WARN("Resetting zeroed wBitsPerSample\n");
2909 wwi
->waveFormat
.Format
.wBitsPerSample
= 8 *
2910 (wwi
->waveFormat
.Format
.nAvgBytesPerSec
/
2911 wwi
->waveFormat
.Format
.nSamplesPerSec
) /
2912 wwi
->waveFormat
.Format
.nChannels
;
2915 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
2916 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
2917 wwi
->ossdev
->dev_name
, strerror(errno
));
2918 OSS_CloseDevice(wwi
->ossdev
);
2919 wwi
->state
= WINE_WS_CLOSED
;
2920 return MMSYSERR_NOTENABLED
;
2923 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info
.fragstotal
,
2924 info
.fragsize
, (info
.fragsize
* 1000) / (wwi
->ossdev
->sample_rate
*
2925 wwi
->ossdev
->channels
* (wwi
->ossdev
->format
== AFMT_U8
? 1 : 2)));
2927 wwi
->dwFragmentSize
= info
.fragsize
;
2929 TRACE("dwFragmentSize=%u\n", wwi
->dwFragmentSize
);
2930 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2931 wwi
->waveFormat
.Format
.wBitsPerSample
, wwi
->waveFormat
.Format
.nAvgBytesPerSec
,
2932 wwi
->waveFormat
.Format
.nSamplesPerSec
, wwi
->waveFormat
.Format
.nChannels
,
2933 wwi
->waveFormat
.Format
.nBlockAlign
);
2935 OSS_InitRingMessage(&wwi
->msgRing
);
2937 wwi
->hStartUpEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2938 wwi
->hThread
= CreateThread(NULL
, 0, widRecorder
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwi
->dwThreadID
));
2940 SetThreadPriority(wwi
->hThread
, THREAD_PRIORITY_TIME_CRITICAL
);
2941 WaitForSingleObject(wwi
->hStartUpEvent
, INFINITE
);
2942 CloseHandle(wwi
->hStartUpEvent
);
2943 wwi
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
2945 return widNotifyClient(wwi
, WIM_OPEN
, 0L, 0L);
2948 /**************************************************************************
2949 * widClose [internal]
2951 static DWORD
widClose(WORD wDevID
)
2955 TRACE("(%u);\n", wDevID
);
2956 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2957 WARN("can't close !\n");
2958 return MMSYSERR_INVALHANDLE
;
2961 wwi
= &WInDev
[wDevID
];
2963 if (wwi
->lpQueuePtr
!= NULL
) {
2964 WARN("still buffers open !\n");
2965 return WAVERR_STILLPLAYING
;
2968 OSS_AddRingMessage(&wwi
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
2969 OSS_CloseDevice(wwi
->ossdev
);
2970 wwi
->state
= WINE_WS_CLOSED
;
2971 wwi
->dwFragmentSize
= 0;
2972 OSS_DestroyRingMessage(&wwi
->msgRing
);
2973 return widNotifyClient(wwi
, WIM_CLOSE
, 0L, 0L);
2976 /**************************************************************************
2977 * widAddBuffer [internal]
2979 static DWORD
widAddBuffer(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
2981 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
2983 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2984 WARN("can't do it !\n");
2985 return MMSYSERR_INVALHANDLE
;
2987 if (!(lpWaveHdr
->dwFlags
& WHDR_PREPARED
)) {
2988 TRACE("never been prepared !\n");
2989 return WAVERR_UNPREPARED
;
2991 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
) {
2992 TRACE("header already in use !\n");
2993 return WAVERR_STILLPLAYING
;
2996 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
2997 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
2998 lpWaveHdr
->dwBytesRecorded
= 0;
2999 lpWaveHdr
->lpNext
= NULL
;
3001 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
3002 return MMSYSERR_NOERROR
;
3005 /**************************************************************************
3006 * widStart [internal]
3008 static DWORD
widStart(WORD wDevID
)
3010 TRACE("(%u);\n", wDevID
);
3011 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3012 WARN("can't start recording !\n");
3013 return MMSYSERR_INVALHANDLE
;
3016 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STARTING
, 0, TRUE
);
3017 return MMSYSERR_NOERROR
;
3020 /**************************************************************************
3021 * widStop [internal]
3023 static DWORD
widStop(WORD wDevID
)
3025 TRACE("(%u);\n", wDevID
);
3026 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3027 WARN("can't stop !\n");
3028 return MMSYSERR_INVALHANDLE
;
3031 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STOPPING
, 0, TRUE
);
3033 return MMSYSERR_NOERROR
;
3036 /**************************************************************************
3037 * widReset [internal]
3039 static DWORD
widReset(WORD wDevID
)
3041 TRACE("(%u);\n", wDevID
);
3042 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3043 WARN("can't reset !\n");
3044 return MMSYSERR_INVALHANDLE
;
3046 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
3047 return MMSYSERR_NOERROR
;
3050 /**************************************************************************
3051 * widGetPosition [internal]
3053 static DWORD
widGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
3057 TRACE("(%u, %p, %u);\n", wDevID
, lpTime
, uSize
);
3059 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3060 WARN("can't get pos !\n");
3061 return MMSYSERR_INVALHANDLE
;
3064 if (lpTime
== NULL
) {
3065 WARN("invalid parameter: lpTime == NULL\n");
3066 return MMSYSERR_INVALPARAM
;
3069 wwi
= &WInDev
[wDevID
];
3070 #ifdef EXACT_WIDPOSITION
3071 if (wwi
->ossdev
->in_caps_support
& WAVECAPS_SAMPLEACCURATE
)
3072 OSS_AddRingMessage(&(wwi
->msgRing
), WINE_WM_UPDATE
, 0, TRUE
);
3075 return bytes_to_mmtime(lpTime
, wwi
->dwTotalRecorded
, &wwi
->waveFormat
);
3078 /**************************************************************************
3079 * widMessage (WINEOSS.6)
3081 DWORD WINAPI
OSS_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3082 DWORD dwParam1
, DWORD dwParam2
)
3084 TRACE("(%u, %s, %08X, %08X, %08X);\n",
3085 wDevID
, getMessage(wMsg
), dwUser
, dwParam1
, dwParam2
);
3092 /* FIXME: Pretend this is supported */
3094 case WIDM_OPEN
: return widOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
3095 case WIDM_CLOSE
: return widClose (wDevID
);
3096 case WIDM_ADDBUFFER
: return widAddBuffer (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
3097 case WIDM_PREPARE
: return MMSYSERR_NOTSUPPORTED
;
3098 case WIDM_UNPREPARE
: return MMSYSERR_NOTSUPPORTED
;
3099 case WIDM_GETDEVCAPS
: return widGetDevCaps (wDevID
, (LPWAVEINCAPSW
)dwParam1
, dwParam2
);
3100 case WIDM_GETNUMDEVS
: return numInDev
;
3101 case WIDM_GETPOS
: return widGetPosition(wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
3102 case WIDM_RESET
: return widReset (wDevID
);
3103 case WIDM_START
: return widStart (wDevID
);
3104 case WIDM_STOP
: return widStop (wDevID
);
3105 case DRV_QUERYDEVICEINTERFACESIZE
: return widDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
3106 case DRV_QUERYDEVICEINTERFACE
: return widDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
3107 case DRV_QUERYDSOUNDIFACE
: return widDsCreate (wDevID
, (PIDSCDRIVER
*)dwParam1
);
3108 case DRV_QUERYDSOUNDDESC
: return widDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
3110 FIXME("unknown message %u!\n", wMsg
);
3112 return MMSYSERR_NOTSUPPORTED
;
3115 #else /* !HAVE_OSS */
3117 /**************************************************************************
3118 * wodMessage (WINEOSS.7)
3120 DWORD WINAPI
OSS_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3121 DWORD dwParam1
, DWORD dwParam2
)
3123 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
3124 return MMSYSERR_NOTENABLED
;
3127 /**************************************************************************
3128 * widMessage (WINEOSS.6)
3130 DWORD WINAPI
OSS_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3131 DWORD dwParam1
, DWORD dwParam2
)
3133 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
3134 return MMSYSERR_NOTENABLED
;
3137 #endif /* HAVE_OSS */