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>
74 #include "wine/debug.h"
78 WINE_DEFAULT_DEBUG_CHANNEL(wave
);
80 /* Allow 1% deviation for sample rates (some ES137x cards) */
81 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
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 ossdev
->bOutputEnabled
= TRUE
; /* OSS enables by default */
393 ossdev
->bInputEnabled
= TRUE
; /* OSS enables by default */
394 if (ossdev
->open_access
== O_RDONLY
)
395 ossdev
->bOutputEnabled
= FALSE
;
396 if (ossdev
->open_access
== O_WRONLY
)
397 ossdev
->bInputEnabled
= FALSE
;
399 if (ossdev
->bTriggerSupport
) {
401 trigger
= getEnables(ossdev
);
402 /* If we do not have full duplex, but they opened RDWR
403 ** (as you have to in order for an mmap to succeed)
404 ** then we start out with input off
406 if (ossdev
->open_access
== O_RDWR
&& !ossdev
->full_duplex
&&
407 ossdev
->bInputEnabled
&& ossdev
->bOutputEnabled
) {
408 ossdev
->bInputEnabled
= FALSE
;
409 trigger
&= ~PCM_ENABLE_INPUT
;
410 ioctl(fd
, SNDCTL_DSP_SETTRIGGER
, &trigger
);
414 return MMSYSERR_NOERROR
;
418 return WAVERR_BADFORMAT
;
421 return MMSYSERR_ERROR
;
424 /******************************************************************
427 * since OSS has poor capabilities in full duplex, we try here to let a program
428 * open the device for both waveout and wavein streams...
429 * this is hackish, but it's the way OSS interface is done...
431 DWORD
OSS_OpenDevice(OSS_DEVICE
* ossdev
, unsigned req_access
,
432 int* frag
, int strict_format
,
433 int sample_rate
, int channels
, int fmt
)
437 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev
,req_access
,frag
,strict_format
,sample_rate
,channels
,fmt
);
439 if (ossdev
->full_duplex
&& (req_access
== O_RDONLY
|| req_access
== O_WRONLY
))
441 TRACE("Opening RDWR because full_duplex=%d and req_access=%d\n",
442 ossdev
->full_duplex
,req_access
);
443 open_access
= O_RDWR
;
447 open_access
=req_access
;
450 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
451 if (ossdev
->open_count
== 0)
453 if (access(ossdev
->dev_name
, 0) != 0) return MMSYSERR_NODRIVER
;
455 ossdev
->audio_fragment
= (frag
) ? *frag
: 0;
456 ossdev
->sample_rate
= sample_rate
;
457 ossdev
->channels
= channels
;
458 ossdev
->format
= fmt
;
459 ossdev
->open_access
= open_access
;
460 ossdev
->owner_tid
= GetCurrentThreadId();
462 if ((ret
= OSS_RawOpenDevice(ossdev
,strict_format
)) != MMSYSERR_NOERROR
) return ret
;
463 if (ossdev
->full_duplex
&& ossdev
->bTriggerSupport
&&
464 (req_access
== O_RDONLY
|| req_access
== O_WRONLY
))
467 if (req_access
== O_WRONLY
)
468 ossdev
->bInputEnabled
=0;
470 ossdev
->bOutputEnabled
=0;
471 enable
= getEnables(ossdev
);
472 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable
);
473 if (ioctl(ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0)
474 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev
->dev_name
, enable
, strerror(errno
));
479 /* check we really open with the same parameters */
480 if (ossdev
->open_access
!= open_access
)
482 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
483 return WAVERR_BADFORMAT
;
486 /* check if the audio parameters are the same */
487 if (ossdev
->sample_rate
!= sample_rate
||
488 ossdev
->channels
!= channels
||
489 ossdev
->format
!= fmt
)
491 /* This is not a fatal error because MSACM might do the remapping */
492 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
493 "OSS doesn't allow us different parameters\n"
494 "audio_frag(%x/%x) sample_rate(%d/%d) channels(%d/%d) fmt(%d/%d)\n",
495 ossdev
->audio_fragment
, frag
? *frag
: 0,
496 ossdev
->sample_rate
, sample_rate
,
497 ossdev
->channels
, channels
,
498 ossdev
->format
, fmt
);
499 return WAVERR_BADFORMAT
;
501 /* check if the fragment sizes are the same */
502 if (ossdev
->audio_fragment
!= (frag
? *frag
: 0) )
504 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
505 "Please run winecfg, open \"Audio\" page and set\n"
506 "\"Hardware Acceleration\" to \"Emulation\".\n");
507 return WAVERR_BADFORMAT
;
509 if (GetCurrentThreadId() != ossdev
->owner_tid
)
511 WARN("Another thread is trying to access audio...\n");
512 return MMSYSERR_ERROR
;
514 if (ossdev
->full_duplex
&& ossdev
->bTriggerSupport
&&
515 (req_access
== O_RDONLY
|| req_access
== O_WRONLY
))
518 if (req_access
== O_WRONLY
)
519 ossdev
->bOutputEnabled
=1;
521 ossdev
->bInputEnabled
=1;
522 enable
= getEnables(ossdev
);
523 TRACE("Calling SNDCTL_DSP_SETTRIGGER with %x\n",enable
);
524 if (ioctl(ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0)
525 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER, %d) failed (%s)\n",ossdev
->dev_name
, enable
, strerror(errno
));
529 ossdev
->open_count
++;
531 return MMSYSERR_NOERROR
;
534 /******************************************************************
539 void OSS_CloseDevice(OSS_DEVICE
* ossdev
)
541 TRACE("(%p)\n",ossdev
);
542 if (ossdev
->open_count
>0) {
543 ossdev
->open_count
--;
545 WARN("OSS_CloseDevice called too many times\n");
547 if (ossdev
->open_count
== 0)
549 fcntl(ossdev
->fd
, F_SETFL
, fcntl(ossdev
->fd
, F_GETFL
) & ~O_NDELAY
);
550 /* reset the device before we close it in case it is in a bad state */
551 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
552 if (close(ossdev
->fd
) != 0) FIXME("Cannot close %d: %s\n", ossdev
->fd
, strerror(errno
));
556 /******************************************************************
559 * Resets the device. OSS Commercial requires the device to be closed
560 * after a SNDCTL_DSP_RESET ioctl call... this function implements
562 * FIXME: This causes problems when doing full duplex so we really
563 * only reset when not doing full duplex. We need to do this better
566 static DWORD
OSS_ResetDevice(OSS_DEVICE
* ossdev
)
568 DWORD ret
= MMSYSERR_NOERROR
;
569 int old_fd
= ossdev
->fd
;
570 TRACE("(%p)\n", ossdev
);
572 if (ossdev
->open_count
== 1) {
573 if (ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, NULL
) == -1)
575 perror("ioctl SNDCTL_DSP_RESET");
579 ret
= OSS_RawOpenDevice(ossdev
, 1);
580 TRACE("Changing fd from %d to %d\n", old_fd
, ossdev
->fd
);
582 WARN("Not resetting device because it is in full duplex mode!\n");
587 static const int win_std_oss_fmts
[2]={AFMT_U8
,AFMT_S16_LE
};
588 static const int win_std_rates
[5]={96000,48000,44100,22050,11025};
589 static const int win_std_formats
[2][2][5]=
590 {{{WAVE_FORMAT_96M08
, WAVE_FORMAT_48M08
, WAVE_FORMAT_4M08
,
591 WAVE_FORMAT_2M08
, WAVE_FORMAT_1M08
},
592 {WAVE_FORMAT_96S08
, WAVE_FORMAT_48S08
, WAVE_FORMAT_4S08
,
593 WAVE_FORMAT_2S08
, WAVE_FORMAT_1S08
}},
594 {{WAVE_FORMAT_96M16
, WAVE_FORMAT_48M16
, WAVE_FORMAT_4M16
,
595 WAVE_FORMAT_2M16
, WAVE_FORMAT_1M16
},
596 {WAVE_FORMAT_96S16
, WAVE_FORMAT_48S16
, WAVE_FORMAT_4S16
,
597 WAVE_FORMAT_2S16
, WAVE_FORMAT_1S16
}},
600 static void OSS_Info(int fd
)
602 /* Note that this only reports the formats supported by the hardware.
603 * The driver may support other formats and do the conversions in
604 * software which is why we don't use this value
606 int oss_mask
, oss_caps
;
607 if (ioctl(fd
, SNDCTL_DSP_GETFMTS
, &oss_mask
) >= 0) {
608 TRACE("Formats=%08x ( ", oss_mask
);
609 if (oss_mask
& AFMT_MU_LAW
) TRACE("AFMT_MU_LAW ");
610 if (oss_mask
& AFMT_A_LAW
) TRACE("AFMT_A_LAW ");
611 if (oss_mask
& AFMT_IMA_ADPCM
) TRACE("AFMT_IMA_ADPCM ");
612 if (oss_mask
& AFMT_U8
) TRACE("AFMT_U8 ");
613 if (oss_mask
& AFMT_S16_LE
) TRACE("AFMT_S16_LE ");
614 if (oss_mask
& AFMT_S16_BE
) TRACE("AFMT_S16_BE ");
615 if (oss_mask
& AFMT_S8
) TRACE("AFMT_S8 ");
616 if (oss_mask
& AFMT_U16_LE
) TRACE("AFMT_U16_LE ");
617 if (oss_mask
& AFMT_U16_BE
) TRACE("AFMT_U16_BE ");
618 if (oss_mask
& AFMT_MPEG
) TRACE("AFMT_MPEG ");
620 if (oss_mask
& AFMT_AC3
) TRACE("AFMT_AC3 ");
623 if (oss_mask
& AFMT_VORBIS
) TRACE("AFMT_VORBIS ");
626 if (oss_mask
& AFMT_S32_LE
) TRACE("AFMT_S32_LE ");
629 if (oss_mask
& AFMT_S32_BE
) TRACE("AFMT_S32_BE ");
632 if (oss_mask
& AFMT_FLOAT
) TRACE("AFMT_FLOAT ");
635 if (oss_mask
& AFMT_S24_LE
) TRACE("AFMT_S24_LE ");
638 if (oss_mask
& AFMT_S24_BE
) TRACE("AFMT_S24_BE ");
640 #ifdef AFMT_SPDIF_RAW
641 if (oss_mask
& AFMT_SPDIF_RAW
) TRACE("AFMT_SPDIF_RAW ");
645 if (ioctl(fd
, SNDCTL_DSP_GETCAPS
, &oss_caps
) >= 0) {
646 TRACE("Caps=%08x\n",oss_caps
);
647 TRACE("\tRevision: %d\n", oss_caps
&DSP_CAP_REVISION
);
648 TRACE("\tDuplex: %s\n", oss_caps
& DSP_CAP_DUPLEX
? "true" : "false");
649 TRACE("\tRealtime: %s\n", oss_caps
& DSP_CAP_REALTIME
? "true" : "false");
650 TRACE("\tBatch: %s\n", oss_caps
& DSP_CAP_BATCH
? "true" : "false");
651 TRACE("\tCoproc: %s\n", oss_caps
& DSP_CAP_COPROC
? "true" : "false");
652 TRACE("\tTrigger: %s\n", oss_caps
& DSP_CAP_TRIGGER
? "true" : "false");
653 TRACE("\tMmap: %s\n", oss_caps
& DSP_CAP_MMAP
? "true" : "false");
655 TRACE("\tMulti: %s\n", oss_caps
& DSP_CAP_MULTI
? "true" : "false");
658 TRACE("\tBind: %s\n", oss_caps
& DSP_CAP_BIND
? "true" : "false");
661 TRACE("\tInput: %s\n", oss_caps
& DSP_CAP_INPUT
? "true" : "false");
663 #ifdef DSP_CAP_OUTPUT
664 TRACE("\tOutput: %s\n", oss_caps
& DSP_CAP_OUTPUT
? "true" : "false");
666 #ifdef DSP_CAP_VIRTUAL
667 TRACE("\tVirtual: %s\n", oss_caps
& DSP_CAP_VIRTUAL
? "true" : "false");
669 #ifdef DSP_CAP_ANALOGOUT
670 TRACE("\tAnalog Out: %s\n", oss_caps
& DSP_CAP_ANALOGOUT
? "true" : "false");
672 #ifdef DSP_CAP_ANALOGIN
673 TRACE("\tAnalog In: %s\n", oss_caps
& DSP_CAP_ANALOGIN
? "true" : "false");
675 #ifdef DSP_CAP_DIGITALOUT
676 TRACE("\tDigital Out: %s\n", oss_caps
& DSP_CAP_DIGITALOUT
? "true" : "false");
678 #ifdef DSP_CAP_DIGITALIN
679 TRACE("\tDigital In: %s\n", oss_caps
& DSP_CAP_DIGITALIN
? "true" : "false");
681 #ifdef DSP_CAP_ADMASK
682 TRACE("\tA/D Mask: %s\n", oss_caps
& DSP_CAP_ADMASK
? "true" : "false");
684 #ifdef DSP_CAP_SHADOW
685 TRACE("\tShadow: %s\n", oss_caps
& DSP_CAP_SHADOW
? "true" : "false");
688 TRACE("\tChannel Mask: %x\n", oss_caps
& DSP_CH_MASK
);
691 TRACE("\tSlave: %s\n", oss_caps
& DSP_CAP_SLAVE
? "true" : "false");
696 /******************************************************************
701 static BOOL
OSS_WaveOutInit(OSS_DEVICE
* ossdev
)
705 BOOL has_mixer
= FALSE
;
706 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
708 if (OSS_OpenDevice(ossdev
, O_WRONLY
, NULL
, 0,-1,-1,-1) != 0)
711 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
713 #if defined(SNDCTL_MIXERINFO)
716 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
719 if (ioctl(mixer
, SNDCTL_MIXERINFO
, &info
) >= 0) {
720 lstrcpynA(ossdev
->ds_desc
.szDesc
, info
.name
, sizeof(info
.name
));
721 strcpy(ossdev
->ds_desc
.szDrvname
, "wineoss.drv");
722 MultiByteToWideChar(CP_ACP
, 0, info
.name
, sizeof(info
.name
),
723 ossdev
->out_caps
.szPname
,
724 sizeof(ossdev
->out_caps
.szPname
) / sizeof(WCHAR
));
725 TRACE("%s: %s\n", ossdev
->mixer_name
, ossdev
->ds_desc
.szDesc
);
728 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev
->mixer_name
);
732 WARN("open(%s) failed (%s)\n", ossdev
->mixer_name
, strerror(errno
));
735 #elif defined(SOUND_MIXER_INFO)
738 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
740 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
741 lstrcpynA(ossdev
->ds_desc
.szDesc
, info
.name
, sizeof(info
.name
));
742 strcpy(ossdev
->ds_desc
.szDrvname
, "wineoss.drv");
743 MultiByteToWideChar(CP_ACP
, 0, info
.name
, sizeof(info
.name
),
744 ossdev
->out_caps
.szPname
,
745 sizeof(ossdev
->out_caps
.szPname
) / sizeof(WCHAR
));
746 TRACE("%s: %s\n", ossdev
->mixer_name
, ossdev
->ds_desc
.szDesc
);
749 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
750 * implement it properly, and there are probably similar issues
751 * on other platforms, so we warn but try to go ahead.
753 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev
->mixer_name
);
757 WARN("open(%s) failed (%s)\n", ossdev
->mixer_name
, strerror(errno
));
760 #endif /* SOUND_MIXER_INFO */
762 if (WINE_TRACE_ON(wave
))
763 OSS_Info(ossdev
->fd
);
765 ossdev
->out_caps
.wMid
= 0x00FF; /* Manufac ID */
766 ossdev
->out_caps
.wPid
= 0x0001; /* Product ID */
768 ossdev
->out_caps
.vDriverVersion
= 0x0100;
769 ossdev
->out_caps
.wChannels
= 1;
770 ossdev
->out_caps
.dwFormats
= 0x00000000;
771 ossdev
->out_caps
.wReserved1
= 0;
772 ossdev
->out_caps
.dwSupport
= has_mixer
? WAVECAPS_VOLUME
: 0;
774 /* direct sound caps */
775 ossdev
->ds_caps
.dwFlags
= DSCAPS_CERTIFIED
;
776 ossdev
->ds_caps
.dwFlags
|= DSCAPS_SECONDARY8BIT
;
777 ossdev
->ds_caps
.dwFlags
|= DSCAPS_SECONDARY16BIT
;
778 ossdev
->ds_caps
.dwFlags
|= DSCAPS_SECONDARYMONO
;
779 ossdev
->ds_caps
.dwFlags
|= DSCAPS_SECONDARYSTEREO
;
780 ossdev
->ds_caps
.dwFlags
|= DSCAPS_CONTINUOUSRATE
;
782 ossdev
->ds_caps
.dwPrimaryBuffers
= 1;
783 ossdev
->ds_caps
.dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
784 ossdev
->ds_caps
.dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
786 /* We must first set the format and the stereo mode as some sound cards
787 * may support 44kHz mono but not 44kHz stereo. Also we must
788 * systematically check the return value of these ioctls as they will
789 * always succeed (see OSS Linux) but will modify the parameter to match
790 * whatever they support. The OSS specs also say we must first set the
791 * sample size, then the stereo and then the sample rate.
794 arg
=win_std_oss_fmts
[f
];
795 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
796 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
797 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
798 rc
,arg
,win_std_oss_fmts
[f
]);
802 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARY8BIT
;
804 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARY16BIT
;
806 for (c
= 1; c
<= MAX_CHANNELS
; c
++) {
808 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_CHANNELS
, &arg
);
810 if (rc
!=0 || arg
!=c
) {
811 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc
,arg
,c
);
815 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYMONO
;
817 ossdev
->out_caps
.wChannels
= 2;
819 ossdev
->out_caps
.dwSupport
|=WAVECAPS_LRVOLUME
;
820 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYSTEREO
;
822 ossdev
->out_caps
.wChannels
= c
;
824 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
825 arg
=win_std_rates
[r
];
826 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
827 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
828 rc
,arg
,win_std_rates
[r
],win_std_oss_fmts
[f
],c
);
829 if (rc
==0 && arg
!=0 && NEAR_MATCH(arg
,win_std_rates
[r
]) && c
< 3)
830 ossdev
->out_caps
.dwFormats
|=win_std_formats
[f
][c
-1][r
];
835 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
836 if (arg
& DSP_CAP_TRIGGER
)
837 ossdev
->bTriggerSupport
= TRUE
;
838 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
)) {
839 ossdev
->out_caps
.dwSupport
|= WAVECAPS_SAMPLEACCURATE
;
841 /* well, might as well use the DirectSound cap flag for something */
842 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
843 !(arg
& DSP_CAP_BATCH
)) {
844 ossdev
->out_caps
.dwSupport
|= WAVECAPS_DIRECTSOUND
;
846 ossdev
->ds_caps
.dwFlags
|= DSCAPS_EMULDRIVER
;
848 #ifdef DSP_CAP_MULTI /* not every oss has this */
849 /* check for hardware secondary buffer support (multi open) */
850 if ((arg
& DSP_CAP_MULTI
) &&
851 (ossdev
->out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
852 TRACE("hardware secondary buffer support available\n");
854 ossdev
->ds_caps
.dwMaxHwMixingAllBuffers
= 16;
855 ossdev
->ds_caps
.dwMaxHwMixingStaticBuffers
= 0;
856 ossdev
->ds_caps
.dwMaxHwMixingStreamingBuffers
= 16;
858 ossdev
->ds_caps
.dwFreeHwMixingAllBuffers
= 16;
859 ossdev
->ds_caps
.dwFreeHwMixingStaticBuffers
= 0;
860 ossdev
->ds_caps
.dwFreeHwMixingStreamingBuffers
= 16;
864 OSS_CloseDevice(ossdev
);
865 TRACE("out wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
866 ossdev
->out_caps
.wChannels
, ossdev
->out_caps
.dwFormats
,
867 ossdev
->out_caps
.dwSupport
);
871 /******************************************************************
876 static BOOL
OSS_WaveInInit(OSS_DEVICE
* ossdev
)
880 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
882 if (OSS_OpenDevice(ossdev
, O_RDONLY
, NULL
, 0,-1,-1,-1) != 0)
885 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
887 #if defined(SNDCTL_MIXERINFO)
890 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
893 if (ioctl(mixer
, SNDCTL_MIXERINFO
, &info
) >= 0) {
894 MultiByteToWideChar(CP_ACP
, 0, info
.name
, -1,
895 ossdev
->in_caps
.szPname
,
896 sizeof(ossdev
->in_caps
.szPname
) / sizeof(WCHAR
));
897 TRACE("%s: %s\n", ossdev
->mixer_name
, ossdev
->ds_desc
.szDesc
);
899 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev
->mixer_name
);
903 WARN("open(%s) failed (%s)\n", ossdev
->mixer_name
, strerror(errno
));
906 #elif defined(SOUND_MIXER_INFO)
909 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
911 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
912 MultiByteToWideChar(CP_ACP
, 0, info
.name
, -1,
913 ossdev
->in_caps
.szPname
,
914 sizeof(ossdev
->in_caps
.szPname
) / sizeof(WCHAR
));
915 TRACE("%s: %s\n", ossdev
->mixer_name
, ossdev
->ds_desc
.szDesc
);
917 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
918 * implement it properly, and there are probably similar issues
919 * on other platforms, so we warn but try to go ahead.
921 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev
->mixer_name
);
925 WARN("open(%s) failed (%s)\n", ossdev
->mixer_name
, strerror(errno
));
928 #endif /* SOUND_MIXER_INFO */
930 if (WINE_TRACE_ON(wave
))
931 OSS_Info(ossdev
->fd
);
933 ossdev
->in_caps
.wMid
= 0x00FF; /* Manufac ID */
934 ossdev
->in_caps
.wPid
= 0x0001; /* Product ID */
936 ossdev
->in_caps
.dwFormats
= 0x00000000;
937 ossdev
->in_caps
.wChannels
= 1;
938 ossdev
->in_caps
.wReserved1
= 0;
940 /* direct sound caps */
941 ossdev
->dsc_caps
.dwSize
= sizeof(ossdev
->dsc_caps
);
942 ossdev
->dsc_caps
.dwFlags
= 0;
943 ossdev
->dsc_caps
.dwFormats
= 0x00000000;
944 ossdev
->dsc_caps
.dwChannels
= 1;
946 /* See the comment in OSS_WaveOutInit for the loop order */
948 arg
=win_std_oss_fmts
[f
];
949 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
950 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
951 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
952 rc
,arg
,win_std_oss_fmts
[f
]);
956 for (c
= 1; c
<= MAX_CHANNELS
; c
++) {
958 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_CHANNELS
, &arg
);
960 if (rc
!=0 || arg
!=c
) {
961 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc
,arg
,c
);
965 ossdev
->in_caps
.wChannels
= c
;
966 ossdev
->dsc_caps
.dwChannels
= c
;
969 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
970 arg
=win_std_rates
[r
];
971 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
972 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc
,arg
,win_std_rates
[r
],win_std_oss_fmts
[f
],c
);
973 if (rc
==0 && NEAR_MATCH(arg
,win_std_rates
[r
]) && c
< 3)
974 ossdev
->in_caps
.dwFormats
|=win_std_formats
[f
][c
-1][r
];
975 ossdev
->dsc_caps
.dwFormats
|=win_std_formats
[f
][c
-1][r
];
980 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
981 if (arg
& DSP_CAP_TRIGGER
)
982 ossdev
->bTriggerSupport
= TRUE
;
983 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
984 !(arg
& DSP_CAP_BATCH
)) {
985 /* FIXME: enable the next statement if you want to work on the driver */
987 ossdev
->in_caps_support
|= WAVECAPS_DIRECTSOUND
;
990 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
))
991 ossdev
->in_caps_support
|= WAVECAPS_SAMPLEACCURATE
;
993 OSS_CloseDevice(ossdev
);
994 TRACE("in wChannels = %d, dwFormats = %08X, in_caps_support = %08X\n",
995 ossdev
->in_caps
.wChannels
, ossdev
->in_caps
.dwFormats
, ossdev
->in_caps_support
);
999 /******************************************************************
1000 * OSS_WaveFullDuplexInit
1004 static void OSS_WaveFullDuplexInit(OSS_DEVICE
* ossdev
)
1009 BOOL has_mixer
= FALSE
;
1010 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
1012 /* The OSS documentation says we must call SNDCTL_SETDUPLEX
1013 * *before* checking for SNDCTL_DSP_GETCAPS otherwise we may
1014 * get the wrong result. This ioctl must even be done before
1015 * setting the fragment size so that only OSS_RawOpenDevice is
1016 * in a position to do it. So we set full_duplex speculatively
1017 * and adjust right after.
1019 ossdev
->full_duplex
=1;
1020 rc
=OSS_OpenDevice(ossdev
, O_RDWR
, NULL
, 0,-1,-1,-1);
1021 ossdev
->full_duplex
=0;
1025 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
1027 #if defined(SNDCTL_MIXERINFO)
1030 if ((mixer
= open(ossdev
->mixer_name
, O_RDWR
|O_NDELAY
)) >= 0) {
1033 if (ioctl(mixer
, SNDCTL_MIXERINFO
, &info
) >= 0) {
1036 WARN("%s: cannot read SNDCTL_MIXERINFO!\n", ossdev
->mixer_name
);
1040 WARN("open(%s) failed (%s)\n", ossdev
->mixer_name
, strerror(errno
));
1043 #elif defined(SOUND_MIXER_INFO)
1046 if ((mixer
= open(ossdev
->mixer_name
, O_RDWR
|O_NDELAY
)) >= 0) {
1048 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
1051 /* FreeBSD up to at least 5.2 provides this ioctl, but does not
1052 * implement it properly, and there are probably similar issues
1053 * on other platforms, so we warn but try to go ahead.
1055 WARN("%s: cannot read SOUND_MIXER_INFO!\n", ossdev
->mixer_name
);
1059 WARN("open(%s) failed (%s)\n", ossdev
->mixer_name
, strerror(errno
));
1062 #endif /* SOUND_MIXER_INFO */
1064 TRACE("%s\n", ossdev
->ds_desc
.szDesc
);
1066 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &caps
) == 0)
1067 ossdev
->full_duplex
= (caps
& DSP_CAP_DUPLEX
);
1069 ossdev
->duplex_out_caps
= ossdev
->out_caps
;
1071 ossdev
->duplex_out_caps
.wChannels
= 1;
1072 ossdev
->duplex_out_caps
.dwFormats
= 0x00000000;
1073 ossdev
->duplex_out_caps
.dwSupport
= has_mixer
? WAVECAPS_VOLUME
: 0;
1075 if (WINE_TRACE_ON(wave
))
1076 OSS_Info(ossdev
->fd
);
1078 /* See the comment in OSS_WaveOutInit for the loop order */
1080 arg
=win_std_oss_fmts
[f
];
1081 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
1082 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
1083 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
1084 rc
,arg
,win_std_oss_fmts
[f
]);
1088 for (c
= 1; c
<= MAX_CHANNELS
; c
++) {
1090 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_CHANNELS
, &arg
);
1091 if( rc
== -1) break;
1092 if (rc
!=0 || arg
!=c
) {
1093 TRACE("DSP_CHANNELS: rc=%d returned %d for %d\n",rc
,arg
,c
);
1097 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYMONO
;
1098 } else if (c
== 2) {
1099 ossdev
->duplex_out_caps
.wChannels
= 2;
1101 ossdev
->duplex_out_caps
.dwSupport
|=WAVECAPS_LRVOLUME
;
1102 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYSTEREO
;
1104 ossdev
->duplex_out_caps
.wChannels
= c
;
1106 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
1107 arg
=win_std_rates
[r
];
1108 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
1109 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
1110 rc
,arg
,win_std_rates
[r
],win_std_oss_fmts
[f
],c
);
1111 if (rc
==0 && arg
!=0 && NEAR_MATCH(arg
,win_std_rates
[r
]) && c
< 3)
1112 ossdev
->duplex_out_caps
.dwFormats
|=win_std_formats
[f
][c
-1][r
];
1117 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
1118 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
)) {
1119 ossdev
->duplex_out_caps
.dwSupport
|= WAVECAPS_SAMPLEACCURATE
;
1121 /* well, might as well use the DirectSound cap flag for something */
1122 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
1123 !(arg
& DSP_CAP_BATCH
)) {
1124 ossdev
->duplex_out_caps
.dwSupport
|= WAVECAPS_DIRECTSOUND
;
1127 OSS_CloseDevice(ossdev
);
1128 TRACE("duplex wChannels = %d, dwFormats = %08X, dwSupport = %08X\n",
1129 ossdev
->duplex_out_caps
.wChannels
,
1130 ossdev
->duplex_out_caps
.dwFormats
,
1131 ossdev
->duplex_out_caps
.dwSupport
);
1134 static char* StrDup(const char* str
, const char* def
)
1139 dst
=HeapAlloc(GetProcessHeap(),0,strlen(str
)+1);
1144 /******************************************************************
1147 * Initialize internal structures from OSS information
1149 LRESULT
OSS_WaveInit(void)
1156 str
=getenv("AUDIODEV");
1159 WOutDev
[0].ossdev
.dev_name
= WInDev
[0].ossdev
.dev_name
= StrDup(str
,"");
1160 WOutDev
[0].ossdev
.mixer_name
= WInDev
[0].ossdev
.mixer_name
= StrDup(getenv("MIXERDEV"),"/dev/mixer");
1161 for (i
= 1; i
< MAX_WAVEDRV
; ++i
)
1163 WOutDev
[i
].ossdev
.dev_name
= WInDev
[i
].ossdev
.dev_name
= StrDup("",NULL
);
1164 WOutDev
[i
].ossdev
.mixer_name
= WInDev
[i
].ossdev
.mixer_name
= StrDup("",NULL
);
1169 WOutDev
[0].ossdev
.dev_name
= WInDev
[0].ossdev
.dev_name
= StrDup("/dev/dsp",NULL
);
1170 WOutDev
[0].ossdev
.mixer_name
= WInDev
[0].ossdev
.mixer_name
= StrDup("/dev/mixer",NULL
);
1171 for (i
= 1; i
< MAX_WAVEDRV
; ++i
)
1173 WOutDev
[i
].ossdev
.dev_name
= WInDev
[i
].ossdev
.dev_name
= HeapAlloc(GetProcessHeap(),0,11);
1174 sprintf(WOutDev
[i
].ossdev
.dev_name
, "/dev/dsp%d", i
);
1175 WOutDev
[i
].ossdev
.mixer_name
= WInDev
[i
].ossdev
.mixer_name
= HeapAlloc(GetProcessHeap(),0,13);
1176 sprintf(WOutDev
[i
].ossdev
.mixer_name
, "/dev/mixer%d", i
);
1180 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
1182 WOutDev
[i
].ossdev
.interface_name
= WInDev
[i
].ossdev
.interface_name
=
1183 HeapAlloc(GetProcessHeap(),0,9+strlen(WOutDev
[i
].ossdev
.dev_name
)+1);
1184 sprintf(WOutDev
[i
].ossdev
.interface_name
, "wineoss: %s", WOutDev
[i
].ossdev
.dev_name
);
1187 /* start with output devices */
1188 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
1190 if (*WOutDev
[i
].ossdev
.dev_name
== '\0' || OSS_WaveOutInit(&WOutDev
[i
].ossdev
))
1192 WOutDev
[numOutDev
].state
= WINE_WS_CLOSED
;
1193 WOutDev
[numOutDev
].volume
= 0xffffffff;
1198 /* then do input devices */
1199 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
1201 if (*WInDev
[i
].ossdev
.dev_name
=='\0' || OSS_WaveInInit(&WInDev
[i
].ossdev
))
1203 WInDev
[numInDev
].state
= WINE_WS_CLOSED
;
1208 /* finish with the full duplex bits */
1209 for (i
= 0; i
< MAX_WAVEDRV
; i
++)
1210 if (*WOutDev
[i
].ossdev
.dev_name
!='\0')
1211 OSS_WaveFullDuplexInit(&WOutDev
[i
].ossdev
);
1213 TRACE("%d wave out devices\n", numOutDev
);
1214 for (i
= 0; i
< numOutDev
; i
++) {
1215 TRACE("%d: %s, %s, %s\n", i
, WOutDev
[i
].ossdev
.dev_name
,
1216 WOutDev
[i
].ossdev
.mixer_name
, WOutDev
[i
].ossdev
.interface_name
);
1219 TRACE("%d wave in devices\n", numInDev
);
1220 for (i
= 0; i
< numInDev
; i
++) {
1221 TRACE("%d: %s, %s, %s\n", i
, WInDev
[i
].ossdev
.dev_name
,
1222 WInDev
[i
].ossdev
.mixer_name
, WInDev
[i
].ossdev
.interface_name
);
1228 /******************************************************************
1231 * Delete/clear internal structures of OSS information
1233 LRESULT
OSS_WaveExit(void)
1238 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
1240 HeapFree(GetProcessHeap(), 0, WOutDev
[i
].ossdev
.dev_name
);
1241 HeapFree(GetProcessHeap(), 0, WOutDev
[i
].ossdev
.mixer_name
);
1242 HeapFree(GetProcessHeap(), 0, WOutDev
[i
].ossdev
.interface_name
);
1245 ZeroMemory(WOutDev
, sizeof(WOutDev
));
1246 ZeroMemory(WInDev
, sizeof(WInDev
));
1254 /******************************************************************
1255 * OSS_InitRingMessage
1257 * Initialize the ring of messages for passing between driver's caller and playback/record
1260 static int OSS_InitRingMessage(OSS_MSG_RING
* omr
)
1263 omr
->msg_tosave
= 0;
1264 #ifdef USE_PIPE_SYNC
1265 if (pipe(omr
->msg_pipe
) < 0) {
1266 omr
->msg_pipe
[0] = -1;
1267 omr
->msg_pipe
[1] = -1;
1268 ERR("could not create pipe, error=%s\n", strerror(errno
));
1271 omr
->msg_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1273 omr
->ring_buffer_size
= OSS_RING_BUFFER_INCREMENT
;
1274 omr
->messages
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,omr
->ring_buffer_size
* sizeof(OSS_MSG
));
1275 InitializeCriticalSection(&omr
->msg_crst
);
1276 omr
->msg_crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": OSS_MSG_RING.msg_crst");
1280 /******************************************************************
1281 * OSS_DestroyRingMessage
1284 static int OSS_DestroyRingMessage(OSS_MSG_RING
* omr
)
1286 #ifdef USE_PIPE_SYNC
1287 close(omr
->msg_pipe
[0]);
1288 close(omr
->msg_pipe
[1]);
1290 CloseHandle(omr
->msg_event
);
1292 HeapFree(GetProcessHeap(),0,omr
->messages
);
1293 omr
->msg_crst
.DebugInfo
->Spare
[0] = 0;
1294 DeleteCriticalSection(&omr
->msg_crst
);
1298 /******************************************************************
1299 * OSS_AddRingMessage
1301 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
1303 static int OSS_AddRingMessage(OSS_MSG_RING
* omr
, enum win_wm_message msg
, DWORD param
, BOOL wait
)
1305 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
1307 EnterCriticalSection(&omr
->msg_crst
);
1308 if ((omr
->msg_toget
== ((omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
)))
1310 int old_ring_buffer_size
= omr
->ring_buffer_size
;
1311 omr
->ring_buffer_size
+= OSS_RING_BUFFER_INCREMENT
;
1312 TRACE("omr->ring_buffer_size=%d\n",omr
->ring_buffer_size
);
1313 omr
->messages
= HeapReAlloc(GetProcessHeap(),0,omr
->messages
, omr
->ring_buffer_size
* sizeof(OSS_MSG
));
1314 /* Now we need to rearrange the ring buffer so that the new
1315 buffers just allocated are in between omr->msg_tosave and
1318 if (omr
->msg_tosave
< omr
->msg_toget
)
1320 memmove(&(omr
->messages
[omr
->msg_toget
+ OSS_RING_BUFFER_INCREMENT
]),
1321 &(omr
->messages
[omr
->msg_toget
]),
1322 sizeof(OSS_MSG
)*(old_ring_buffer_size
- omr
->msg_toget
)
1324 omr
->msg_toget
+= OSS_RING_BUFFER_INCREMENT
;
1329 hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1330 if (hEvent
== INVALID_HANDLE_VALUE
)
1332 ERR("can't create event !?\n");
1333 LeaveCriticalSection(&omr
->msg_crst
);
1336 if (omr
->msg_toget
!= omr
->msg_tosave
&& omr
->messages
[omr
->msg_toget
].msg
!= WINE_WM_HEADER
)
1337 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1338 omr
->msg_toget
,getCmdString(omr
->messages
[omr
->msg_toget
].msg
),
1339 omr
->msg_tosave
,getCmdString(omr
->messages
[omr
->msg_tosave
].msg
));
1341 /* fast messages have to be added at the start of the queue */
1342 omr
->msg_toget
= (omr
->msg_toget
+ omr
->ring_buffer_size
- 1) % omr
->ring_buffer_size
;
1343 omr
->messages
[omr
->msg_toget
].msg
= msg
;
1344 omr
->messages
[omr
->msg_toget
].param
= param
;
1345 omr
->messages
[omr
->msg_toget
].hEvent
= hEvent
;
1349 omr
->messages
[omr
->msg_tosave
].msg
= msg
;
1350 omr
->messages
[omr
->msg_tosave
].param
= param
;
1351 omr
->messages
[omr
->msg_tosave
].hEvent
= INVALID_HANDLE_VALUE
;
1352 omr
->msg_tosave
= (omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
;
1354 LeaveCriticalSection(&omr
->msg_crst
);
1355 /* signal a new message */
1359 /* wait for playback/record thread to have processed the message */
1360 WaitForSingleObject(hEvent
, INFINITE
);
1361 CloseHandle(hEvent
);
1366 /******************************************************************
1367 * OSS_RetrieveRingMessage
1369 * Get a message from the ring. Should be called by the playback/record thread.
1371 static int OSS_RetrieveRingMessage(OSS_MSG_RING
* omr
,
1372 enum win_wm_message
*msg
, DWORD
*param
, HANDLE
*hEvent
)
1374 EnterCriticalSection(&omr
->msg_crst
);
1376 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
1378 LeaveCriticalSection(&omr
->msg_crst
);
1382 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
1383 omr
->messages
[omr
->msg_toget
].msg
= 0;
1384 *param
= omr
->messages
[omr
->msg_toget
].param
;
1385 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
1386 omr
->msg_toget
= (omr
->msg_toget
+ 1) % omr
->ring_buffer_size
;
1388 LeaveCriticalSection(&omr
->msg_crst
);
1392 /******************************************************************
1393 * OSS_PeekRingMessage
1395 * Peek at a message from the ring but do not remove it.
1396 * Should be called by the playback/record thread.
1398 static int OSS_PeekRingMessage(OSS_MSG_RING
* omr
,
1399 enum win_wm_message
*msg
,
1400 DWORD
*param
, HANDLE
*hEvent
)
1402 EnterCriticalSection(&omr
->msg_crst
);
1404 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
1406 LeaveCriticalSection(&omr
->msg_crst
);
1410 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
1411 *param
= omr
->messages
[omr
->msg_toget
].param
;
1412 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
1413 LeaveCriticalSection(&omr
->msg_crst
);
1417 /*======================================================================*
1418 * Low level WAVE OUT implementation *
1419 *======================================================================*/
1421 /**************************************************************************
1422 * wodNotifyClient [internal]
1424 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
1426 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04X dwParam2 = %04X\n", wMsg
,
1427 wMsg
== WOM_OPEN
? "WOM_OPEN" : wMsg
== WOM_CLOSE
? "WOM_CLOSE" :
1428 wMsg
== WOM_DONE
? "WOM_DONE" : "Unknown", dwParam1
, dwParam2
);
1434 if (wwo
->wFlags
!= DCB_NULL
&&
1435 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
,
1436 (HDRVR
)wwo
->waveDesc
.hWave
, wMsg
,
1437 wwo
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
1438 WARN("can't notify client !\n");
1439 return MMSYSERR_ERROR
;
1443 FIXME("Unknown callback message %u\n", wMsg
);
1444 return MMSYSERR_INVALPARAM
;
1446 return MMSYSERR_NOERROR
;
1449 /**************************************************************************
1450 * wodUpdatePlayedTotal [internal]
1453 static BOOL
wodUpdatePlayedTotal(WINE_WAVEOUT
* wwo
, audio_buf_info
* info
)
1455 audio_buf_info dspspace
;
1457 if (!info
) info
= &dspspace
;
1459 if (ioctl(wwo
->ossdev
.fd
, SNDCTL_DSP_GETOSPACE
, info
) < 0) {
1460 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo
->ossdev
.dev_name
, strerror(errno
));
1464 /* GETOSPACE is not always accurate when we're down to the last fragment or two;
1465 ** we try to accommodate that here by assuming that the dsp is empty by looking
1466 ** at the clock rather than the result of GETOSPACE */
1467 notplayed
= wwo
->dwBufferSize
- info
->bytes
;
1468 if (notplayed
> 0 && notplayed
< (info
->fragsize
* 2))
1470 if (wwo
->dwProjectedFinishTime
&& GetTickCount() >= wwo
->dwProjectedFinishTime
)
1472 TRACE("Adjusting for a presumed OSS bug and assuming all data has been played.\n");
1473 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
;
1477 /* Some OSS drivers will clean up nicely if given a POST, so give 'em the chance... */
1478 ioctl(wwo
->ossdev
.fd
, SNDCTL_DSP_POST
, 0);
1481 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
- notplayed
;
1485 /**************************************************************************
1486 * wodPlayer_BeginWaveHdr [internal]
1488 * Makes the specified lpWaveHdr the currently playing wave header.
1489 * If the specified wave header is a begin loop and we're not already in
1490 * a loop, setup the loop.
1492 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
1494 wwo
->lpPlayPtr
= lpWaveHdr
;
1496 if (!lpWaveHdr
) return;
1498 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
) {
1499 if (wwo
->lpLoopPtr
) {
1500 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
1502 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
1503 wwo
->lpLoopPtr
= lpWaveHdr
;
1504 /* Windows does not touch WAVEHDR.dwLoops,
1505 * so we need to make an internal copy */
1506 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
1509 wwo
->dwPartialOffset
= 0;
1512 /**************************************************************************
1513 * wodPlayer_PlayPtrNext [internal]
1515 * Advance the play pointer to the next waveheader, looping if required.
1517 static LPWAVEHDR
wodPlayer_PlayPtrNext(WINE_WAVEOUT
* wwo
)
1519 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
1521 wwo
->dwPartialOffset
= 0;
1522 if ((lpWaveHdr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
) {
1523 /* We're at the end of a loop, loop if required */
1524 if (--wwo
->dwLoops
> 0) {
1525 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1527 /* Handle overlapping loops correctly */
1528 if (wwo
->lpLoopPtr
!= lpWaveHdr
&& (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)) {
1529 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1530 /* shall we consider the END flag for the closing loop or for
1531 * the opening one or for both ???
1532 * code assumes for closing loop only
1535 lpWaveHdr
= lpWaveHdr
->lpNext
;
1537 wwo
->lpLoopPtr
= NULL
;
1538 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
);
1541 /* We're not in a loop. Advance to the next wave header */
1542 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
= lpWaveHdr
->lpNext
);
1548 /**************************************************************************
1549 * wodPlayer_TicksTillEmpty [internal]
1550 * Returns the number of ticks until we think the DSP should be empty
1552 static DWORD
wodPlayer_TicksTillEmpty(const WINE_WAVEOUT
*wwo
)
1554 return ((wwo
->dwWrittenTotal
- wwo
->dwPlayedTotal
) * 1000)
1555 / wwo
->waveFormat
.Format
.nAvgBytesPerSec
;
1558 /**************************************************************************
1559 * wodPlayer_DSPWait [internal]
1560 * Returns the number of milliseconds to wait for the DSP buffer to write
1563 static DWORD
wodPlayer_DSPWait(const WINE_WAVEOUT
*wwo
)
1565 /* time for one fragment to be played */
1566 return wwo
->dwFragmentSize
* 1000 / wwo
->waveFormat
.Format
.nAvgBytesPerSec
;
1569 /**************************************************************************
1570 * wodPlayer_NotifyWait [internal]
1571 * Returns the number of milliseconds to wait before attempting to notify
1572 * completion of the specified wavehdr.
1573 * This is based on the number of bytes remaining to be written in the
1576 static DWORD
wodPlayer_NotifyWait(const WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
1580 if (lpWaveHdr
->reserved
< wwo
->dwPlayedTotal
) {
1583 dwMillis
= (lpWaveHdr
->reserved
- wwo
->dwPlayedTotal
) * 1000 / wwo
->waveFormat
.Format
.nAvgBytesPerSec
;
1584 if (!dwMillis
) dwMillis
= 1;
1591 /**************************************************************************
1592 * wodPlayer_WriteMaxFrags [internal]
1593 * Writes the maximum number of bytes possible to the DSP and returns
1594 * TRUE iff the current playPtr has been fully played
1596 static BOOL
wodPlayer_WriteMaxFrags(WINE_WAVEOUT
* wwo
, DWORD
* bytes
)
1598 DWORD dwLength
= wwo
->lpPlayPtr
->dwBufferLength
- wwo
->dwPartialOffset
;
1599 DWORD toWrite
= min(dwLength
, *bytes
);
1603 TRACE("Writing wavehdr %p.%u[%u]/%u\n",
1604 wwo
->lpPlayPtr
, wwo
->dwPartialOffset
, wwo
->lpPlayPtr
->dwBufferLength
, toWrite
);
1608 written
= write(wwo
->ossdev
.fd
, wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
, toWrite
);
1610 TRACE("write(%s, %p, %d) failed (%s) returned %d\n", wwo
->ossdev
.dev_name
,
1611 wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
, toWrite
, strerror(errno
), written
);
1618 if (written
>= dwLength
) {
1619 /* If we wrote all current wavehdr, skip to the next one */
1620 wodPlayer_PlayPtrNext(wwo
);
1623 /* Remove the amount written */
1624 wwo
->dwPartialOffset
+= written
;
1627 wwo
->dwWrittenTotal
+= written
;
1628 TRACE("dwWrittenTotal=%u\n", wwo
->dwWrittenTotal
);
1633 /**************************************************************************
1634 * wodPlayer_NotifyCompletions [internal]
1636 * Notifies and remove from queue all wavehdrs which have been played to
1637 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1638 * we notify all wavehdrs and remove them all from the queue even if they
1639 * are unplayed or part of a loop.
1641 static DWORD
wodPlayer_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
1643 LPWAVEHDR lpWaveHdr
;
1645 /* Start from lpQueuePtr and keep notifying until:
1646 * - we hit an unwritten wavehdr
1647 * - we hit the beginning of a running loop
1648 * - we hit a wavehdr which hasn't finished playing
1651 while ((lpWaveHdr
= wwo
->lpQueuePtr
) &&
1653 (lpWaveHdr
!= wwo
->lpPlayPtr
&&
1654 lpWaveHdr
!= wwo
->lpLoopPtr
&&
1655 lpWaveHdr
->reserved
<= wwo
->dwPlayedTotal
))) {
1657 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
1659 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1660 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1662 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
1667 lpWaveHdr
= wwo
->lpQueuePtr
;
1668 if (!lpWaveHdr
) {TRACE("Empty queue\n"); break;}
1671 if (lpWaveHdr
== wwo
->lpPlayPtr
) {TRACE("play %p\n", lpWaveHdr
); break;}
1672 if (lpWaveHdr
== wwo
->lpLoopPtr
) {TRACE("loop %p\n", lpWaveHdr
); break;}
1673 if (lpWaveHdr
->reserved
> wwo
->dwPlayedTotal
) {TRACE("still playing %p (%u/%u)\n", lpWaveHdr
, lpWaveHdr
->reserved
, wwo
->dwPlayedTotal
);break;}
1675 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
1677 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1678 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1680 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
1683 return (lpWaveHdr
&& lpWaveHdr
!= wwo
->lpPlayPtr
&& lpWaveHdr
!= wwo
->lpLoopPtr
) ?
1684 wodPlayer_NotifyWait(wwo
, lpWaveHdr
) : INFINITE
;
1687 /**************************************************************************
1688 * wodPlayer_Reset [internal]
1690 * wodPlayer helper. Resets current output stream.
1692 static void wodPlayer_Reset(WINE_WAVEOUT
* wwo
, BOOL reset
)
1694 wodUpdatePlayedTotal(wwo
, NULL
);
1695 /* updates current notify list */
1696 wodPlayer_NotifyCompletions(wwo
, FALSE
);
1698 /* flush all possible output */
1699 if (OSS_ResetDevice(&wwo
->ossdev
) != MMSYSERR_NOERROR
)
1702 wwo
->state
= WINE_WS_STOPPED
;
1707 enum win_wm_message msg
;
1711 /* remove any buffer */
1712 wodPlayer_NotifyCompletions(wwo
, TRUE
);
1714 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
1715 wwo
->state
= WINE_WS_STOPPED
;
1716 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
= 0;
1717 /* Clear partial wavehdr */
1718 wwo
->dwPartialOffset
= 0;
1720 /* remove any existing message in the ring */
1721 EnterCriticalSection(&wwo
->msgRing
.msg_crst
);
1722 /* return all pending headers in queue */
1723 while (OSS_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
))
1725 if (msg
!= WINE_WM_HEADER
)
1727 FIXME("shouldn't have headers left\n");
1731 ((LPWAVEHDR
)param
)->dwFlags
&= ~WHDR_INQUEUE
;
1732 ((LPWAVEHDR
)param
)->dwFlags
|= WHDR_DONE
;
1734 wodNotifyClient(wwo
, WOM_DONE
, param
, 0);
1736 RESET_OMR(&wwo
->msgRing
);
1737 LeaveCriticalSection(&wwo
->msgRing
.msg_crst
);
1739 if (wwo
->lpLoopPtr
) {
1740 /* complicated case, not handled yet (could imply modifying the loop counter */
1741 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1742 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1743 wwo
->dwPartialOffset
= 0;
1744 wwo
->dwWrittenTotal
= wwo
->dwPlayedTotal
; /* this is wrong !!! */
1747 DWORD sz
= wwo
->dwPartialOffset
;
1749 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1750 /* compute the max size playable from lpQueuePtr */
1751 for (ptr
= wwo
->lpQueuePtr
; ptr
!= wwo
->lpPlayPtr
; ptr
= ptr
->lpNext
) {
1752 sz
+= ptr
->dwBufferLength
;
1754 /* because the reset lpPlayPtr will be lpQueuePtr */
1755 if (wwo
->dwWrittenTotal
> wwo
->dwPlayedTotal
+ sz
) ERR("grin\n");
1756 wwo
->dwPartialOffset
= sz
- (wwo
->dwWrittenTotal
- wwo
->dwPlayedTotal
);
1757 wwo
->dwWrittenTotal
= wwo
->dwPlayedTotal
;
1758 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
;
1760 wwo
->state
= WINE_WS_PAUSED
;
1764 /**************************************************************************
1765 * wodPlayer_ProcessMessages [internal]
1767 static void wodPlayer_ProcessMessages(WINE_WAVEOUT
* wwo
)
1769 LPWAVEHDR lpWaveHdr
;
1770 enum win_wm_message msg
;
1774 while (OSS_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
)) {
1775 TRACE("Received %s %x\n", getCmdString(msg
), param
);
1777 case WINE_WM_PAUSING
:
1778 wodPlayer_Reset(wwo
, FALSE
);
1781 case WINE_WM_RESTARTING
:
1782 if (wwo
->state
== WINE_WS_PAUSED
)
1784 wwo
->state
= WINE_WS_PLAYING
;
1788 case WINE_WM_HEADER
:
1789 lpWaveHdr
= (LPWAVEHDR
)param
;
1791 /* insert buffer at the end of queue */
1794 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
1797 if (!wwo
->lpPlayPtr
)
1798 wodPlayer_BeginWaveHdr(wwo
,lpWaveHdr
);
1799 if (wwo
->state
== WINE_WS_STOPPED
)
1800 wwo
->state
= WINE_WS_PLAYING
;
1802 case WINE_WM_RESETTING
:
1803 wodPlayer_Reset(wwo
, TRUE
);
1806 case WINE_WM_UPDATE
:
1807 wodUpdatePlayedTotal(wwo
, NULL
);
1810 case WINE_WM_BREAKLOOP
:
1811 if (wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpLoopPtr
!= NULL
) {
1812 /* ensure exit at end of current loop */
1817 case WINE_WM_CLOSING
:
1818 /* sanity check: this should not happen since the device must have been reset before */
1819 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
1821 wwo
->state
= WINE_WS_CLOSED
;
1824 /* shouldn't go here */
1826 FIXME("unknown message %d\n", msg
);
1832 /**************************************************************************
1833 * wodPlayer_FeedDSP [internal]
1834 * Feed as much sound data as we can into the DSP and return the number of
1835 * milliseconds before it will be necessary to feed the DSP again.
1837 static DWORD
wodPlayer_FeedDSP(WINE_WAVEOUT
* wwo
)
1839 audio_buf_info dspspace
;
1842 if (!wodUpdatePlayedTotal(wwo
, &dspspace
)) return INFINITE
;
1843 availInQ
= dspspace
.bytes
;
1844 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1845 dspspace
.fragments
, dspspace
.fragstotal
, dspspace
.fragsize
, dspspace
.bytes
);
1847 /* no more room... no need to try to feed */
1848 if (dspspace
.fragments
!= 0) {
1849 /* Feed from partial wavehdr */
1850 if (wwo
->lpPlayPtr
&& wwo
->dwPartialOffset
!= 0) {
1851 wodPlayer_WriteMaxFrags(wwo
, &availInQ
);
1854 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1855 if (wwo
->dwPartialOffset
== 0 && wwo
->lpPlayPtr
) {
1857 TRACE("Setting time to elapse for %p to %u\n",
1858 wwo
->lpPlayPtr
, wwo
->dwWrittenTotal
+ wwo
->lpPlayPtr
->dwBufferLength
);
1859 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1860 wwo
->lpPlayPtr
->reserved
= wwo
->dwWrittenTotal
+ wwo
->lpPlayPtr
->dwBufferLength
;
1861 } while (wodPlayer_WriteMaxFrags(wwo
, &availInQ
) && wwo
->lpPlayPtr
&& availInQ
> 0);
1864 if (wwo
->bNeedPost
) {
1865 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1866 * if it didn't get one, we give it the other */
1867 if (wwo
->dwBufferSize
< availInQ
+ 2 * wwo
->dwFragmentSize
)
1868 ioctl(wwo
->ossdev
.fd
, SNDCTL_DSP_POST
, 0);
1869 wwo
->bNeedPost
= FALSE
;
1873 return wodPlayer_DSPWait(wwo
);
1877 /**************************************************************************
1878 * wodPlayer [internal]
1880 static DWORD CALLBACK
wodPlayer(LPVOID pmt
)
1882 WORD uDevID
= (DWORD
)pmt
;
1883 WINE_WAVEOUT
* wwo
= (WINE_WAVEOUT
*)&WOutDev
[uDevID
];
1884 DWORD dwNextFeedTime
= INFINITE
; /* Time before DSP needs feeding */
1885 DWORD dwNextNotifyTime
= INFINITE
; /* Time before next wave completion */
1888 wwo
->state
= WINE_WS_STOPPED
;
1889 SetEvent(wwo
->hStartUpEvent
);
1892 /** Wait for the shortest time before an action is required. If there
1893 * are no pending actions, wait forever for a command.
1895 dwSleepTime
= min(dwNextFeedTime
, dwNextNotifyTime
);
1896 TRACE("waiting %ums (%u,%u)\n", dwSleepTime
, dwNextFeedTime
, dwNextNotifyTime
);
1897 WAIT_OMR(&wwo
->msgRing
, dwSleepTime
);
1898 wodPlayer_ProcessMessages(wwo
);
1899 if (wwo
->state
== WINE_WS_PLAYING
) {
1900 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1901 if (dwNextFeedTime
!= INFINITE
)
1902 wwo
->dwProjectedFinishTime
= GetTickCount() + wodPlayer_TicksTillEmpty(wwo
);
1904 wwo
->dwProjectedFinishTime
= 0;
1906 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1907 if (dwNextFeedTime
== INFINITE
) {
1908 /* FeedDSP ran out of data, but before flushing, */
1909 /* check that a notification didn't give us more */
1910 wodPlayer_ProcessMessages(wwo
);
1911 if (!wwo
->lpPlayPtr
) {
1912 TRACE("flushing\n");
1913 ioctl(wwo
->ossdev
.fd
, SNDCTL_DSP_SYNC
, 0);
1914 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
;
1915 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1917 TRACE("recovering\n");
1918 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1922 dwNextFeedTime
= dwNextNotifyTime
= INFINITE
;
1927 /**************************************************************************
1928 * wodGetDevCaps [internal]
1930 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSW lpCaps
, DWORD dwSize
)
1932 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
1934 if (lpCaps
== NULL
) {
1935 WARN("not enabled\n");
1936 return MMSYSERR_NOTENABLED
;
1939 if (wDevID
>= numOutDev
) {
1940 WARN("numOutDev reached !\n");
1941 return MMSYSERR_BADDEVICEID
;
1944 if (WOutDev
[wDevID
].ossdev
.open_access
== O_RDWR
)
1945 memcpy(lpCaps
, &WOutDev
[wDevID
].ossdev
.duplex_out_caps
, min(dwSize
, sizeof(*lpCaps
)));
1947 memcpy(lpCaps
, &WOutDev
[wDevID
].ossdev
.out_caps
, min(dwSize
, sizeof(*lpCaps
)));
1949 return MMSYSERR_NOERROR
;
1952 /**************************************************************************
1953 * wodOpen [internal]
1955 DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1959 audio_buf_info info
;
1962 TRACE("(%u, %p[cb=%08x], %08X);\n", wDevID
, lpDesc
, lpDesc
->dwCallback
, dwFlags
);
1963 if (lpDesc
== NULL
) {
1964 WARN("Invalid Parameter !\n");
1965 return MMSYSERR_INVALPARAM
;
1967 if (wDevID
>= numOutDev
) {
1968 TRACE("MAX_WAVOUTDRV reached !\n");
1969 return MMSYSERR_BADDEVICEID
;
1972 /* only PCM format is supported so far... */
1973 if (!supportedFormat(lpDesc
->lpFormat
)) {
1974 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1975 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1976 lpDesc
->lpFormat
->nSamplesPerSec
);
1977 return WAVERR_BADFORMAT
;
1980 if (dwFlags
& WAVE_FORMAT_QUERY
) {
1981 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1982 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1983 lpDesc
->lpFormat
->nSamplesPerSec
);
1984 return MMSYSERR_NOERROR
;
1987 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
1988 lpDesc
->lpFormat
->nSamplesPerSec
,
1989 lpDesc
->lpFormat
->wBitsPerSample
,
1990 lpDesc
->lpFormat
->nChannels
,
1991 lpDesc
->lpFormat
->wFormatTag
== WAVE_FORMAT_PCM
? "WAVE_FORMAT_PCM" :
1992 lpDesc
->lpFormat
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
? "WAVE_FORMAT_EXTENSIBLE" :
1995 wwo
= &WOutDev
[wDevID
];
1997 if ((dwFlags
& WAVE_DIRECTSOUND
) &&
1998 !(wwo
->ossdev
.duplex_out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
))
1999 /* not supported, ignore it */
2000 dwFlags
&= ~WAVE_DIRECTSOUND
;
2002 if (dwFlags
& WAVE_DIRECTSOUND
) {
2003 if (wwo
->ossdev
.duplex_out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
2004 /* we have realtime DirectSound, fragments just waste our time,
2005 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2006 audio_fragment
= 0x0020000B;
2008 /* to approximate realtime, we must use small fragments,
2009 * let's try to fragment the above 64KB (256 * 2^8) */
2010 audio_fragment
= 0x01000008;
2012 /* A wave device must have a worst case latency of 10 ms so calculate
2013 * the largest fragment size less than 10 ms long.
2015 int fsize
= lpDesc
->lpFormat
->nAvgBytesPerSec
/ 100; /* 10 ms chunk */
2017 while ((1 << shift
) <= fsize
)
2020 audio_fragment
= 0x00100000 + shift
; /* 16 fragments of 2^shift */
2023 TRACE("requesting %d %d byte fragments (%d ms/fragment)\n",
2024 audio_fragment
>> 16, 1 << (audio_fragment
& 0xffff),
2025 ((1 << (audio_fragment
& 0xffff)) * 1000) / lpDesc
->lpFormat
->nAvgBytesPerSec
);
2027 if (wwo
->state
!= WINE_WS_CLOSED
) {
2028 WARN("already allocated\n");
2029 return MMSYSERR_ALLOCATED
;
2032 /* we want to be able to mmap() the device, which means it must be opened readable,
2033 * otherwise mmap() will fail (at least under Linux) */
2034 ret
= OSS_OpenDevice(&wwo
->ossdev
,
2035 (dwFlags
& WAVE_DIRECTSOUND
) ? O_RDWR
: O_WRONLY
,
2037 (dwFlags
& WAVE_DIRECTSOUND
) ? 0 : 1,
2038 lpDesc
->lpFormat
->nSamplesPerSec
,
2039 lpDesc
->lpFormat
->nChannels
,
2040 (lpDesc
->lpFormat
->wBitsPerSample
== 16)
2041 ? AFMT_S16_LE
: AFMT_U8
);
2042 if ((ret
==MMSYSERR_NOERROR
) && (dwFlags
& WAVE_DIRECTSOUND
)) {
2043 lpDesc
->lpFormat
->nSamplesPerSec
=wwo
->ossdev
.sample_rate
;
2044 lpDesc
->lpFormat
->nChannels
=wwo
->ossdev
.channels
;
2045 lpDesc
->lpFormat
->wBitsPerSample
=(wwo
->ossdev
.format
== AFMT_U8
? 8 : 16);
2046 lpDesc
->lpFormat
->nBlockAlign
=lpDesc
->lpFormat
->nChannels
*lpDesc
->lpFormat
->wBitsPerSample
/8;
2047 lpDesc
->lpFormat
->nAvgBytesPerSec
=lpDesc
->lpFormat
->nSamplesPerSec
*lpDesc
->lpFormat
->nBlockAlign
;
2048 TRACE("OSS_OpenDevice returned this format: %dx%dx%d\n",
2049 lpDesc
->lpFormat
->nSamplesPerSec
,
2050 lpDesc
->lpFormat
->wBitsPerSample
,
2051 lpDesc
->lpFormat
->nChannels
);
2053 if (ret
!= 0) return ret
;
2054 wwo
->state
= WINE_WS_STOPPED
;
2056 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2058 memcpy(&wwo
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
2059 copy_format(lpDesc
->lpFormat
, &wwo
->waveFormat
);
2061 if (wwo
->waveFormat
.Format
.wBitsPerSample
== 0) {
2062 WARN("Resetting zeroed wBitsPerSample\n");
2063 wwo
->waveFormat
.Format
.wBitsPerSample
= 8 *
2064 (wwo
->waveFormat
.Format
.nAvgBytesPerSec
/
2065 wwo
->waveFormat
.Format
.nSamplesPerSec
) /
2066 wwo
->waveFormat
.Format
.nChannels
;
2068 /* Read output space info for future reference */
2069 if (ioctl(wwo
->ossdev
.fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
2070 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo
->ossdev
.dev_name
, strerror(errno
));
2071 OSS_CloseDevice(&wwo
->ossdev
);
2072 wwo
->state
= WINE_WS_CLOSED
;
2073 return MMSYSERR_NOTENABLED
;
2076 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info
.fragstotal
,
2077 info
.fragsize
, (info
.fragsize
* 1000) / (wwo
->ossdev
.sample_rate
*
2078 wwo
->ossdev
.channels
* (wwo
->ossdev
.format
== AFMT_U8
? 1 : 2)));
2080 /* Check that fragsize is correct per our settings above */
2081 if ((info
.fragsize
> 1024) && (LOWORD(audio_fragment
) <= 10)) {
2082 /* we've tried to set 1K fragments or less, but it didn't work */
2083 WARN("fragment size set failed, size is now %d\n", info
.fragsize
);
2086 /* Remember fragsize and total buffer size for future use */
2087 wwo
->dwFragmentSize
= info
.fragsize
;
2088 wwo
->dwBufferSize
= info
.fragstotal
* info
.fragsize
;
2089 wwo
->dwPlayedTotal
= 0;
2090 wwo
->dwWrittenTotal
= 0;
2091 wwo
->bNeedPost
= TRUE
;
2093 TRACE("fd=%d fragstotal=%d fragsize=%d BufferSize=%d\n",
2094 wwo
->ossdev
.fd
, info
.fragstotal
, info
.fragsize
, wwo
->dwBufferSize
);
2095 if (wwo
->dwFragmentSize
% wwo
->waveFormat
.Format
.nBlockAlign
) {
2096 ERR("Fragment doesn't contain an integral number of data blocks fragsize=%d BlockAlign=%d\n",wwo
->dwFragmentSize
,wwo
->waveFormat
.Format
.nBlockAlign
);
2097 /* Some SoundBlaster 16 cards return an incorrect (odd) fragment
2098 * size for 16 bit sound. This will cause a system crash when we try
2099 * to write just the specified odd number of bytes. So if we
2100 * detect something is wrong we'd better fix it.
2102 wwo
->dwFragmentSize
-=wwo
->dwFragmentSize
% wwo
->waveFormat
.Format
.nBlockAlign
;
2105 OSS_InitRingMessage(&wwo
->msgRing
);
2107 wwo
->hStartUpEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2108 wwo
->hThread
= CreateThread(NULL
, 0, wodPlayer
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwo
->dwThreadID
));
2110 SetThreadPriority(wwo
->hThread
, THREAD_PRIORITY_TIME_CRITICAL
);
2111 WaitForSingleObject(wwo
->hStartUpEvent
, INFINITE
);
2112 CloseHandle(wwo
->hStartUpEvent
);
2113 wwo
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
2115 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2116 wwo
->waveFormat
.Format
.wBitsPerSample
, wwo
->waveFormat
.Format
.nAvgBytesPerSec
,
2117 wwo
->waveFormat
.Format
.nSamplesPerSec
, wwo
->waveFormat
.Format
.nChannels
,
2118 wwo
->waveFormat
.Format
.nBlockAlign
);
2120 return wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
2123 /**************************************************************************
2124 * wodClose [internal]
2126 static DWORD
wodClose(WORD wDevID
)
2128 DWORD ret
= MMSYSERR_NOERROR
;
2131 TRACE("(%u);\n", wDevID
);
2133 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2134 WARN("bad device ID !\n");
2135 return MMSYSERR_BADDEVICEID
;
2138 wwo
= &WOutDev
[wDevID
];
2139 if (wwo
->lpQueuePtr
) {
2140 WARN("buffers still playing !\n");
2141 ret
= WAVERR_STILLPLAYING
;
2143 if (wwo
->hThread
!= INVALID_HANDLE_VALUE
) {
2144 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
2147 OSS_DestroyRingMessage(&wwo
->msgRing
);
2149 OSS_CloseDevice(&wwo
->ossdev
);
2150 wwo
->state
= WINE_WS_CLOSED
;
2151 wwo
->dwFragmentSize
= 0;
2152 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
2157 /**************************************************************************
2158 * wodWrite [internal]
2161 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
2163 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
2165 /* first, do the sanity checks... */
2166 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2167 WARN("bad dev ID !\n");
2168 return MMSYSERR_BADDEVICEID
;
2171 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
2172 return WAVERR_UNPREPARED
;
2174 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
2175 return WAVERR_STILLPLAYING
;
2177 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
2178 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
2179 lpWaveHdr
->lpNext
= 0;
2181 if ((lpWaveHdr
->dwBufferLength
& (WOutDev
[wDevID
].waveFormat
.Format
.nBlockAlign
- 1)) != 0)
2183 WARN("WaveHdr length isn't a multiple of the PCM block size: %d %% %d\n",lpWaveHdr
->dwBufferLength
,WOutDev
[wDevID
].waveFormat
.Format
.nBlockAlign
);
2184 lpWaveHdr
->dwBufferLength
&= ~(WOutDev
[wDevID
].waveFormat
.Format
.nBlockAlign
- 1);
2187 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
2189 return MMSYSERR_NOERROR
;
2192 /**************************************************************************
2193 * wodPause [internal]
2195 static DWORD
wodPause(WORD wDevID
)
2197 TRACE("(%u);!\n", wDevID
);
2199 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2200 WARN("bad device ID !\n");
2201 return MMSYSERR_BADDEVICEID
;
2204 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_PAUSING
, 0, TRUE
);
2206 return MMSYSERR_NOERROR
;
2209 /**************************************************************************
2210 * wodRestart [internal]
2212 static DWORD
wodRestart(WORD wDevID
)
2214 TRACE("(%u);\n", wDevID
);
2216 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2217 WARN("bad device ID !\n");
2218 return MMSYSERR_BADDEVICEID
;
2221 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESTARTING
, 0, TRUE
);
2223 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2224 /* FIXME: Myst crashes with this ... hmm -MM
2225 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2228 return MMSYSERR_NOERROR
;
2231 /**************************************************************************
2232 * wodReset [internal]
2234 static DWORD
wodReset(WORD wDevID
)
2236 TRACE("(%u);\n", wDevID
);
2238 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2239 WARN("bad device ID !\n");
2240 return MMSYSERR_BADDEVICEID
;
2243 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
2245 return MMSYSERR_NOERROR
;
2248 /**************************************************************************
2249 * wodGetPosition [internal]
2251 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
2255 TRACE("(%u, %p, %u);\n", wDevID
, lpTime
, uSize
);
2257 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2258 WARN("bad device ID !\n");
2259 return MMSYSERR_BADDEVICEID
;
2262 if (lpTime
== NULL
) {
2263 WARN("invalid parameter: lpTime == NULL\n");
2264 return MMSYSERR_INVALPARAM
;
2267 wwo
= &WOutDev
[wDevID
];
2268 #ifdef EXACT_WODPOSITION
2269 if (wwo
->ossdev
.open_access
== O_RDWR
) {
2270 if (wwo
->ossdev
.duplex_out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
2271 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
2273 if (wwo
->ossdev
.out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
2274 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
2278 return bytes_to_mmtime(lpTime
, wwo
->dwPlayedTotal
, &wwo
->waveFormat
);
2281 /**************************************************************************
2282 * wodBreakLoop [internal]
2284 static DWORD
wodBreakLoop(WORD wDevID
)
2286 TRACE("(%u);\n", wDevID
);
2288 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2289 WARN("bad device ID !\n");
2290 return MMSYSERR_BADDEVICEID
;
2292 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_BREAKLOOP
, 0, TRUE
);
2293 return MMSYSERR_NOERROR
;
2296 /**************************************************************************
2297 * wodGetVolume [internal]
2299 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
2304 DWORD last_left
, last_right
;
2306 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
2308 if (lpdwVol
== NULL
) {
2309 WARN("not enabled\n");
2310 return MMSYSERR_NOTENABLED
;
2312 if (wDevID
>= numOutDev
) {
2313 WARN("invalid parameter\n");
2314 return MMSYSERR_INVALPARAM
;
2316 if (WOutDev
[wDevID
].ossdev
.open_access
== O_RDWR
) {
2317 if (!(WOutDev
[wDevID
].ossdev
.duplex_out_caps
.dwSupport
& WAVECAPS_VOLUME
)) {
2318 TRACE("Volume not supported\n");
2319 return MMSYSERR_NOTSUPPORTED
;
2322 if (!(WOutDev
[wDevID
].ossdev
.out_caps
.dwSupport
& WAVECAPS_VOLUME
)) {
2323 TRACE("Volume not supported\n");
2324 return MMSYSERR_NOTSUPPORTED
;
2328 if ((mixer
= open(WOutDev
[wDevID
].ossdev
.mixer_name
, O_RDONLY
|O_NDELAY
)) < 0) {
2329 WARN("mixer device not available !\n");
2330 return MMSYSERR_NOTENABLED
;
2332 if (ioctl(mixer
, SOUND_MIXER_READ_PCM
, &volume
) == -1) {
2334 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)\n",
2335 WOutDev
[wDevID
].ossdev
.mixer_name
, strerror(errno
));
2336 return MMSYSERR_NOTENABLED
;
2340 left
= LOBYTE(volume
);
2341 right
= HIBYTE(volume
);
2342 TRACE("left=%d right=%d !\n", left
, right
);
2343 last_left
= (LOWORD(WOutDev
[wDevID
].volume
) * 100) / 0xFFFFl
;
2344 last_right
= (HIWORD(WOutDev
[wDevID
].volume
) * 100) / 0xFFFFl
;
2345 TRACE("last_left=%d last_right=%d !\n", last_left
, last_right
);
2346 if (last_left
== left
&& last_right
== right
)
2347 *lpdwVol
= WOutDev
[wDevID
].volume
;
2349 *lpdwVol
= ((left
* 0xFFFFl
) / 100) + (((right
* 0xFFFFl
) / 100) << 16);
2350 return MMSYSERR_NOERROR
;
2353 /**************************************************************************
2354 * wodSetVolume [internal]
2356 DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
2362 TRACE("(%u, %08X);\n", wDevID
, dwParam
);
2364 left
= (LOWORD(dwParam
) * 100) / 0xFFFFl
;
2365 right
= (HIWORD(dwParam
) * 100) / 0xFFFFl
;
2366 volume
= left
+ (right
<< 8);
2368 if (wDevID
>= numOutDev
) {
2369 WARN("invalid parameter: wDevID > %d\n", numOutDev
);
2370 return MMSYSERR_INVALPARAM
;
2372 if (WOutDev
[wDevID
].ossdev
.open_access
== O_RDWR
) {
2373 if (!(WOutDev
[wDevID
].ossdev
.duplex_out_caps
.dwSupport
& WAVECAPS_VOLUME
)) {
2374 TRACE("Volume not supported\n");
2375 return MMSYSERR_NOTSUPPORTED
;
2378 if (!(WOutDev
[wDevID
].ossdev
.out_caps
.dwSupport
& WAVECAPS_VOLUME
)) {
2379 TRACE("Volume not supported\n");
2380 return MMSYSERR_NOTSUPPORTED
;
2383 if ((mixer
= open(WOutDev
[wDevID
].ossdev
.mixer_name
, O_WRONLY
|O_NDELAY
)) < 0) {
2384 WARN("open(%s) failed (%s)\n", WOutDev
[wDevID
].ossdev
.mixer_name
, strerror(errno
));
2385 return MMSYSERR_NOTENABLED
;
2387 if (ioctl(mixer
, SOUND_MIXER_WRITE_PCM
, &volume
) == -1) {
2389 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n",
2390 WOutDev
[wDevID
].ossdev
.mixer_name
, strerror(errno
));
2391 return MMSYSERR_NOTENABLED
;
2393 TRACE("volume=%04x\n", (unsigned)volume
);
2396 /* save requested volume */
2397 WOutDev
[wDevID
].volume
= dwParam
;
2399 return MMSYSERR_NOERROR
;
2402 /**************************************************************************
2403 * wodMessage (WINEOSS.7)
2405 DWORD WINAPI
OSS_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
2406 DWORD dwParam1
, DWORD dwParam2
)
2408 TRACE("(%u, %s, %08X, %08X, %08X);\n",
2409 wDevID
, getMessage(wMsg
), dwUser
, dwParam1
, dwParam2
);
2416 /* FIXME: Pretend this is supported */
2418 case WODM_OPEN
: return wodOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
2419 case WODM_CLOSE
: return wodClose (wDevID
);
2420 case WODM_WRITE
: return wodWrite (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
2421 case WODM_PAUSE
: return wodPause (wDevID
);
2422 case WODM_GETPOS
: return wodGetPosition (wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
2423 case WODM_BREAKLOOP
: return wodBreakLoop (wDevID
);
2424 case WODM_PREPARE
: return MMSYSERR_NOTSUPPORTED
;
2425 case WODM_UNPREPARE
: return MMSYSERR_NOTSUPPORTED
;
2426 case WODM_GETDEVCAPS
: return wodGetDevCaps (wDevID
, (LPWAVEOUTCAPSW
)dwParam1
, dwParam2
);
2427 case WODM_GETNUMDEVS
: return numOutDev
;
2428 case WODM_GETPITCH
: return MMSYSERR_NOTSUPPORTED
;
2429 case WODM_SETPITCH
: return MMSYSERR_NOTSUPPORTED
;
2430 case WODM_GETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
2431 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
2432 case WODM_GETVOLUME
: return wodGetVolume (wDevID
, (LPDWORD
)dwParam1
);
2433 case WODM_SETVOLUME
: return wodSetVolume (wDevID
, dwParam1
);
2434 case WODM_RESTART
: return wodRestart (wDevID
);
2435 case WODM_RESET
: return wodReset (wDevID
);
2437 case DRV_QUERYDEVICEINTERFACESIZE
: return wodDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
2438 case DRV_QUERYDEVICEINTERFACE
: return wodDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
2439 case DRV_QUERYDSOUNDIFACE
: return wodDsCreate (wDevID
, (PIDSDRIVER
*)dwParam1
);
2440 case DRV_QUERYDSOUNDDESC
: return wodDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
2442 FIXME("unknown message %d!\n", wMsg
);
2444 return MMSYSERR_NOTSUPPORTED
;
2447 /*======================================================================*
2448 * Low level WAVE IN implementation *
2449 *======================================================================*/
2451 /**************************************************************************
2452 * widNotifyClient [internal]
2454 static DWORD
widNotifyClient(WINE_WAVEIN
* wwi
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
2456 TRACE("wMsg = 0x%04x (%s) dwParm1 = %04X dwParam2 = %04X\n", wMsg
,
2457 wMsg
== WIM_OPEN
? "WIM_OPEN" : wMsg
== WIM_CLOSE
? "WIM_CLOSE" :
2458 wMsg
== WIM_DATA
? "WIM_DATA" : "Unknown", dwParam1
, dwParam2
);
2464 if (wwi
->wFlags
!= DCB_NULL
&&
2465 !DriverCallback(wwi
->waveDesc
.dwCallback
, wwi
->wFlags
,
2466 (HDRVR
)wwi
->waveDesc
.hWave
, wMsg
,
2467 wwi
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
2468 WARN("can't notify client !\n");
2469 return MMSYSERR_ERROR
;
2473 FIXME("Unknown callback message %u\n", wMsg
);
2474 return MMSYSERR_INVALPARAM
;
2476 return MMSYSERR_NOERROR
;
2479 /**************************************************************************
2480 * widGetDevCaps [internal]
2482 static DWORD
widGetDevCaps(WORD wDevID
, LPWAVEINCAPSW lpCaps
, DWORD dwSize
)
2484 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
2486 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
2488 if (wDevID
>= numInDev
) {
2489 TRACE("numOutDev reached !\n");
2490 return MMSYSERR_BADDEVICEID
;
2493 memcpy(lpCaps
, &WInDev
[wDevID
].ossdev
.in_caps
, min(dwSize
, sizeof(*lpCaps
)));
2494 return MMSYSERR_NOERROR
;
2497 /**************************************************************************
2498 * widRecorder_ReadHeaders [internal]
2500 static void widRecorder_ReadHeaders(WINE_WAVEIN
* wwi
)
2502 enum win_wm_message tmp_msg
;
2507 while (OSS_RetrieveRingMessage(&wwi
->msgRing
, &tmp_msg
, &tmp_param
, &tmp_ev
)) {
2508 if (tmp_msg
== WINE_WM_HEADER
) {
2510 lpWaveHdr
= (LPWAVEHDR
)tmp_param
;
2511 lpWaveHdr
->lpNext
= 0;
2513 if (wwi
->lpQueuePtr
== 0)
2514 wwi
->lpQueuePtr
= lpWaveHdr
;
2516 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2520 ERR("should only have headers left\n");
2525 /**************************************************************************
2526 * widRecorder [internal]
2528 static DWORD CALLBACK
widRecorder(LPVOID pmt
)
2530 WORD uDevID
= (DWORD
)pmt
;
2531 WINE_WAVEIN
* wwi
= (WINE_WAVEIN
*)&WInDev
[uDevID
];
2535 LPVOID buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, wwi
->dwFragmentSize
);
2536 char *pOffset
= buffer
;
2537 audio_buf_info info
;
2539 enum win_wm_message msg
;
2544 wwi
->state
= WINE_WS_STOPPED
;
2545 wwi
->dwTotalRecorded
= 0;
2546 wwi
->dwTotalRead
= 0;
2547 wwi
->lpQueuePtr
= NULL
;
2549 SetEvent(wwi
->hStartUpEvent
);
2551 /* disable input so capture will begin when triggered */
2552 wwi
->ossdev
.bInputEnabled
= FALSE
;
2553 enable
= getEnables(&wwi
->ossdev
);
2554 if (ioctl(wwi
->ossdev
.fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0)
2555 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
.dev_name
, strerror(errno
));
2557 /* the soundblaster live needs a micro wake to get its recording started
2558 * (or GETISPACE will have 0 frags all the time)
2560 read(wwi
->ossdev
.fd
, &xs
, 4);
2562 /* make sleep time to be # of ms to output a fragment */
2563 dwSleepTime
= (wwi
->dwFragmentSize
* 1000) / wwi
->waveFormat
.Format
.nAvgBytesPerSec
;
2564 TRACE("sleeptime=%d ms\n", dwSleepTime
);
2567 /* wait for dwSleepTime or an event in thread's queue */
2568 /* FIXME: could improve wait time depending on queue state,
2569 * ie, number of queued fragments
2572 if (wwi
->lpQueuePtr
!= NULL
&& wwi
->state
== WINE_WS_PLAYING
)
2574 lpWaveHdr
= wwi
->lpQueuePtr
;
2576 ioctl(wwi
->ossdev
.fd
, SNDCTL_DSP_GETISPACE
, &info
);
2577 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info
.fragments
, info
.fragsize
, info
.fragstotal
, info
.bytes
);
2579 /* read all the fragments accumulated so far */
2580 while ((info
.fragments
> 0) && (wwi
->lpQueuePtr
))
2584 if (lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
>= wwi
->dwFragmentSize
)
2586 /* directly read fragment in wavehdr */
2587 bytesRead
= read(wwi
->ossdev
.fd
,
2588 lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2589 wwi
->dwFragmentSize
);
2591 TRACE("bytesRead=%d (direct)\n", bytesRead
);
2592 if (bytesRead
!= (DWORD
) -1)
2594 /* update number of bytes recorded in current buffer and by this device */
2595 lpWaveHdr
->dwBytesRecorded
+= bytesRead
;
2596 wwi
->dwTotalRead
+= bytesRead
;
2597 wwi
->dwTotalRecorded
= wwi
->dwTotalRead
;
2599 /* buffer is full. notify client */
2600 if (lpWaveHdr
->dwBytesRecorded
== lpWaveHdr
->dwBufferLength
)
2602 /* must copy the value of next waveHdr, because we have no idea of what
2603 * will be done with the content of lpWaveHdr in callback
2605 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2607 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2608 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2610 wwi
->lpQueuePtr
= lpNext
;
2611 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2615 TRACE("read(%s, %p, %d) failed (%s)\n", wwi
->ossdev
.dev_name
,
2616 lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2617 wwi
->dwFragmentSize
, strerror(errno
));
2622 /* read the fragment in a local buffer */
2623 bytesRead
= read(wwi
->ossdev
.fd
, buffer
, wwi
->dwFragmentSize
);
2626 TRACE("bytesRead=%d (local)\n", bytesRead
);
2628 if (bytesRead
== (DWORD
) -1) {
2629 TRACE("read(%s, %p, %d) failed (%s)\n", wwi
->ossdev
.dev_name
,
2630 buffer
, wwi
->dwFragmentSize
, strerror(errno
));
2634 /* copy data in client buffers */
2635 while (bytesRead
!= (DWORD
) -1 && bytesRead
> 0)
2637 DWORD dwToCopy
= min (bytesRead
, lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
);
2639 memcpy(lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2643 /* update number of bytes recorded in current buffer and by this device */
2644 lpWaveHdr
->dwBytesRecorded
+= dwToCopy
;
2645 wwi
->dwTotalRead
+= dwToCopy
;
2646 wwi
->dwTotalRecorded
= wwi
->dwTotalRead
;
2647 bytesRead
-= dwToCopy
;
2648 pOffset
+= dwToCopy
;
2650 /* client buffer is full. notify client */
2651 if (lpWaveHdr
->dwBytesRecorded
== lpWaveHdr
->dwBufferLength
)
2653 /* must copy the value of next waveHdr, because we have no idea of what
2654 * will be done with the content of lpWaveHdr in callback
2656 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2657 TRACE("lpNext=%p\n", lpNext
);
2659 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2660 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2662 wwi
->lpQueuePtr
= lpNext
;
2663 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2666 if (!lpNext
&& bytesRead
) {
2667 /* before we give up, check for more header messages */
2668 while (OSS_PeekRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
))
2670 if (msg
== WINE_WM_HEADER
) {
2672 OSS_RetrieveRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
);
2673 hdr
= ((LPWAVEHDR
)param
);
2674 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg
), hdr
, ev
);
2676 if (lpWaveHdr
== 0) {
2677 /* new head of queue */
2678 wwi
->lpQueuePtr
= lpWaveHdr
= hdr
;
2680 /* insert buffer at the end of queue */
2682 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2689 if (lpWaveHdr
== 0) {
2690 /* no more buffer to copy data to, but we did read more.
2691 * what hasn't been copied will be dropped
2693 WARN("buffer under run! %u bytes dropped.\n", bytesRead
);
2694 wwi
->lpQueuePtr
= NULL
;
2704 WAIT_OMR(&wwi
->msgRing
, dwSleepTime
);
2706 while (OSS_RetrieveRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
))
2708 TRACE("msg=%s param=0x%x\n", getCmdString(msg
), param
);
2710 case WINE_WM_PAUSING
:
2711 wwi
->state
= WINE_WS_PAUSED
;
2712 /*FIXME("Device should stop recording\n");*/
2715 case WINE_WM_STARTING
:
2716 wwi
->state
= WINE_WS_PLAYING
;
2718 if (wwi
->ossdev
.bTriggerSupport
)
2720 /* start the recording */
2721 wwi
->ossdev
.bInputEnabled
= TRUE
;
2722 enable
= getEnables(&wwi
->ossdev
);
2723 if (ioctl(wwi
->ossdev
.fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2724 wwi
->ossdev
.bInputEnabled
= FALSE
;
2725 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
.dev_name
, strerror(errno
));
2730 unsigned char data
[4];
2731 /* read 4 bytes to start the recording */
2732 read(wwi
->ossdev
.fd
, data
, 4);
2737 case WINE_WM_HEADER
:
2738 lpWaveHdr
= (LPWAVEHDR
)param
;
2739 lpWaveHdr
->lpNext
= 0;
2741 /* insert buffer at the end of queue */
2744 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2748 case WINE_WM_STOPPING
:
2749 if (wwi
->state
!= WINE_WS_STOPPED
)
2751 if (wwi
->ossdev
.bTriggerSupport
)
2753 /* stop the recording */
2754 wwi
->ossdev
.bInputEnabled
= FALSE
;
2755 enable
= getEnables(&wwi
->ossdev
);
2756 if (ioctl(wwi
->ossdev
.fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2757 wwi
->ossdev
.bInputEnabled
= FALSE
;
2758 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
.dev_name
, strerror(errno
));
2762 /* read any headers in queue */
2763 widRecorder_ReadHeaders(wwi
);
2765 /* return current buffer to app */
2766 lpWaveHdr
= wwi
->lpQueuePtr
;
2769 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2770 TRACE("stop %p %p\n", lpWaveHdr
, lpWaveHdr
->lpNext
);
2771 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2772 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2773 wwi
->lpQueuePtr
= lpNext
;
2774 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2777 wwi
->state
= WINE_WS_STOPPED
;
2780 case WINE_WM_RESETTING
:
2781 if (wwi
->state
!= WINE_WS_STOPPED
)
2783 if (wwi
->ossdev
.bTriggerSupport
)
2785 /* stop the recording */
2786 wwi
->ossdev
.bInputEnabled
= FALSE
;
2787 enable
= getEnables(&wwi
->ossdev
);
2788 if (ioctl(wwi
->ossdev
.fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2789 wwi
->ossdev
.bInputEnabled
= FALSE
;
2790 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
.dev_name
, strerror(errno
));
2794 wwi
->state
= WINE_WS_STOPPED
;
2795 wwi
->dwTotalRecorded
= 0;
2796 wwi
->dwTotalRead
= 0;
2798 /* read any headers in queue */
2799 widRecorder_ReadHeaders(wwi
);
2801 /* return all buffers to the app */
2802 for (lpWaveHdr
= wwi
->lpQueuePtr
; lpWaveHdr
; lpWaveHdr
= lpWaveHdr
->lpNext
) {
2803 TRACE("reset %p %p\n", lpWaveHdr
, lpWaveHdr
->lpNext
);
2804 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2805 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2806 wwi
->lpQueuePtr
= lpWaveHdr
->lpNext
;
2807 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2810 wwi
->lpQueuePtr
= NULL
;
2813 case WINE_WM_UPDATE
:
2814 if (wwi
->state
== WINE_WS_PLAYING
) {
2815 audio_buf_info tmp_info
;
2816 if (ioctl(wwi
->ossdev
.fd
, SNDCTL_DSP_GETISPACE
, &tmp_info
) < 0)
2817 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", wwi
->ossdev
.dev_name
, strerror(errno
));
2819 wwi
->dwTotalRecorded
= wwi
->dwTotalRead
+ tmp_info
.bytes
;
2823 case WINE_WM_CLOSING
:
2825 wwi
->state
= WINE_WS_CLOSED
;
2827 HeapFree(GetProcessHeap(), 0, buffer
);
2829 /* shouldn't go here */
2831 FIXME("unknown message %d\n", msg
);
2837 /* just for not generating compilation warnings... should never be executed */
2842 /**************************************************************************
2843 * widOpen [internal]
2845 DWORD
widOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
2848 audio_buf_info info
;
2852 TRACE("(%u, %p, %08X);\n", wDevID
, lpDesc
, dwFlags
);
2853 if (lpDesc
== NULL
) {
2854 WARN("Invalid Parameter !\n");
2855 return MMSYSERR_INVALPARAM
;
2857 if (wDevID
>= numInDev
) {
2858 WARN("bad device id: %d >= %d\n", wDevID
, numInDev
);
2859 return MMSYSERR_BADDEVICEID
;
2862 /* only PCM format is supported so far... */
2863 if (!supportedFormat(lpDesc
->lpFormat
)) {
2864 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2865 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
2866 lpDesc
->lpFormat
->nSamplesPerSec
);
2867 return WAVERR_BADFORMAT
;
2870 if (dwFlags
& WAVE_FORMAT_QUERY
) {
2871 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2872 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
2873 lpDesc
->lpFormat
->nSamplesPerSec
);
2874 return MMSYSERR_NOERROR
;
2877 TRACE("OSS_OpenDevice requested this format: %dx%dx%d %s\n",
2878 lpDesc
->lpFormat
->nSamplesPerSec
,
2879 lpDesc
->lpFormat
->wBitsPerSample
,
2880 lpDesc
->lpFormat
->nChannels
,
2881 lpDesc
->lpFormat
->wFormatTag
== WAVE_FORMAT_PCM
? "WAVE_FORMAT_PCM" :
2882 lpDesc
->lpFormat
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
? "WAVE_FORMAT_EXTENSIBLE" :
2885 wwi
= &WInDev
[wDevID
];
2887 if (wwi
->state
!= WINE_WS_CLOSED
) return MMSYSERR_ALLOCATED
;
2889 if ((dwFlags
& WAVE_DIRECTSOUND
) &&
2890 !(wwi
->ossdev
.in_caps_support
& WAVECAPS_DIRECTSOUND
))
2891 /* not supported, ignore it */
2892 dwFlags
&= ~WAVE_DIRECTSOUND
;
2894 if (dwFlags
& WAVE_DIRECTSOUND
) {
2895 TRACE("has DirectSoundCapture driver\n");
2896 if (wwi
->ossdev
.in_caps_support
& WAVECAPS_SAMPLEACCURATE
)
2897 /* we have realtime DirectSound, fragments just waste our time,
2898 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2899 audio_fragment
= 0x0020000B;
2901 /* to approximate realtime, we must use small fragments,
2902 * let's try to fragment the above 64KB (256 * 2^8) */
2903 audio_fragment
= 0x01000008;
2905 TRACE("doesn't have DirectSoundCapture driver\n");
2906 if (wwi
->ossdev
.open_count
> 0) {
2907 TRACE("Using output device audio_fragment\n");
2908 /* FIXME: This may not be optimal for capture but it allows us
2909 * to do hardware playback without hardware capture. */
2910 audio_fragment
= wwi
->ossdev
.audio_fragment
;
2912 /* A wave device must have a worst case latency of 10 ms so calculate
2913 * the largest fragment size less than 10 ms long.
2915 int fsize
= lpDesc
->lpFormat
->nAvgBytesPerSec
/ 100; /* 10 ms chunk */
2917 while ((1 << shift
) <= fsize
)
2920 audio_fragment
= 0x00100000 + shift
; /* 16 fragments of 2^shift */
2924 TRACE("requesting %d %d byte fragments (%d ms)\n", audio_fragment
>> 16,
2925 1 << (audio_fragment
& 0xffff),
2926 ((1 << (audio_fragment
& 0xffff)) * 1000) / lpDesc
->lpFormat
->nAvgBytesPerSec
);
2928 ret
= OSS_OpenDevice(&wwi
->ossdev
, O_RDONLY
, &audio_fragment
,
2930 lpDesc
->lpFormat
->nSamplesPerSec
,
2931 lpDesc
->lpFormat
->nChannels
,
2932 (lpDesc
->lpFormat
->wBitsPerSample
== 16)
2933 ? AFMT_S16_LE
: AFMT_U8
);
2934 if (ret
!= 0) return ret
;
2935 wwi
->state
= WINE_WS_STOPPED
;
2937 if (wwi
->lpQueuePtr
) {
2938 WARN("Should have an empty queue (%p)\n", wwi
->lpQueuePtr
);
2939 wwi
->lpQueuePtr
= NULL
;
2941 wwi
->dwTotalRecorded
= 0;
2942 wwi
->dwTotalRead
= 0;
2943 wwi
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2945 memcpy(&wwi
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
2946 copy_format(lpDesc
->lpFormat
, &wwi
->waveFormat
);
2948 if (wwi
->waveFormat
.Format
.wBitsPerSample
== 0) {
2949 WARN("Resetting zeroed wBitsPerSample\n");
2950 wwi
->waveFormat
.Format
.wBitsPerSample
= 8 *
2951 (wwi
->waveFormat
.Format
.nAvgBytesPerSec
/
2952 wwi
->waveFormat
.Format
.nSamplesPerSec
) /
2953 wwi
->waveFormat
.Format
.nChannels
;
2956 if (ioctl(wwi
->ossdev
.fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
2957 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n",
2958 wwi
->ossdev
.dev_name
, strerror(errno
));
2959 OSS_CloseDevice(&wwi
->ossdev
);
2960 wwi
->state
= WINE_WS_CLOSED
;
2961 return MMSYSERR_NOTENABLED
;
2964 TRACE("got %d %d byte fragments (%d ms/fragment)\n", info
.fragstotal
,
2965 info
.fragsize
, (info
.fragsize
* 1000) / (wwi
->ossdev
.sample_rate
*
2966 wwi
->ossdev
.channels
* (wwi
->ossdev
.format
== AFMT_U8
? 1 : 2)));
2968 wwi
->dwFragmentSize
= info
.fragsize
;
2970 TRACE("dwFragmentSize=%u\n", wwi
->dwFragmentSize
);
2971 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2972 wwi
->waveFormat
.Format
.wBitsPerSample
, wwi
->waveFormat
.Format
.nAvgBytesPerSec
,
2973 wwi
->waveFormat
.Format
.nSamplesPerSec
, wwi
->waveFormat
.Format
.nChannels
,
2974 wwi
->waveFormat
.Format
.nBlockAlign
);
2976 OSS_InitRingMessage(&wwi
->msgRing
);
2978 wwi
->hStartUpEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2979 wwi
->hThread
= CreateThread(NULL
, 0, widRecorder
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwi
->dwThreadID
));
2981 SetThreadPriority(wwi
->hThread
, THREAD_PRIORITY_TIME_CRITICAL
);
2982 WaitForSingleObject(wwi
->hStartUpEvent
, INFINITE
);
2983 CloseHandle(wwi
->hStartUpEvent
);
2984 wwi
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
2986 return widNotifyClient(wwi
, WIM_OPEN
, 0L, 0L);
2989 /**************************************************************************
2990 * widClose [internal]
2992 static DWORD
widClose(WORD wDevID
)
2996 TRACE("(%u);\n", wDevID
);
2997 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2998 WARN("can't close !\n");
2999 return MMSYSERR_INVALHANDLE
;
3002 wwi
= &WInDev
[wDevID
];
3004 if (wwi
->lpQueuePtr
!= NULL
) {
3005 WARN("still buffers open !\n");
3006 return WAVERR_STILLPLAYING
;
3009 OSS_AddRingMessage(&wwi
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
3010 OSS_CloseDevice(&wwi
->ossdev
);
3011 wwi
->state
= WINE_WS_CLOSED
;
3012 wwi
->dwFragmentSize
= 0;
3013 OSS_DestroyRingMessage(&wwi
->msgRing
);
3014 return widNotifyClient(wwi
, WIM_CLOSE
, 0L, 0L);
3017 /**************************************************************************
3018 * widAddBuffer [internal]
3020 static DWORD
widAddBuffer(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
3022 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
3024 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3025 WARN("can't do it !\n");
3026 return MMSYSERR_INVALHANDLE
;
3028 if (!(lpWaveHdr
->dwFlags
& WHDR_PREPARED
)) {
3029 TRACE("never been prepared !\n");
3030 return WAVERR_UNPREPARED
;
3032 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
) {
3033 TRACE("header already in use !\n");
3034 return WAVERR_STILLPLAYING
;
3037 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
3038 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
3039 lpWaveHdr
->dwBytesRecorded
= 0;
3040 lpWaveHdr
->lpNext
= NULL
;
3042 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
3043 return MMSYSERR_NOERROR
;
3046 /**************************************************************************
3047 * widStart [internal]
3049 static DWORD
widStart(WORD wDevID
)
3051 TRACE("(%u);\n", wDevID
);
3052 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3053 WARN("can't start recording !\n");
3054 return MMSYSERR_INVALHANDLE
;
3057 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STARTING
, 0, TRUE
);
3058 return MMSYSERR_NOERROR
;
3061 /**************************************************************************
3062 * widStop [internal]
3064 static DWORD
widStop(WORD wDevID
)
3066 TRACE("(%u);\n", wDevID
);
3067 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3068 WARN("can't stop !\n");
3069 return MMSYSERR_INVALHANDLE
;
3072 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STOPPING
, 0, TRUE
);
3074 return MMSYSERR_NOERROR
;
3077 /**************************************************************************
3078 * widReset [internal]
3080 static DWORD
widReset(WORD wDevID
)
3082 TRACE("(%u);\n", wDevID
);
3083 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3084 WARN("can't reset !\n");
3085 return MMSYSERR_INVALHANDLE
;
3087 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
3088 return MMSYSERR_NOERROR
;
3091 /**************************************************************************
3092 * widGetPosition [internal]
3094 static DWORD
widGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
3098 TRACE("(%u, %p, %u);\n", wDevID
, lpTime
, uSize
);
3100 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3101 WARN("can't get pos !\n");
3102 return MMSYSERR_INVALHANDLE
;
3105 if (lpTime
== NULL
) {
3106 WARN("invalid parameter: lpTime == NULL\n");
3107 return MMSYSERR_INVALPARAM
;
3110 wwi
= &WInDev
[wDevID
];
3111 #ifdef EXACT_WIDPOSITION
3112 if (wwi
->ossdev
.in_caps_support
& WAVECAPS_SAMPLEACCURATE
)
3113 OSS_AddRingMessage(&(wwi
->msgRing
), WINE_WM_UPDATE
, 0, TRUE
);
3116 return bytes_to_mmtime(lpTime
, wwi
->dwTotalRecorded
, &wwi
->waveFormat
);
3119 /**************************************************************************
3120 * widMessage (WINEOSS.6)
3122 DWORD WINAPI
OSS_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3123 DWORD dwParam1
, DWORD dwParam2
)
3125 TRACE("(%u, %s, %08X, %08X, %08X);\n",
3126 wDevID
, getMessage(wMsg
), dwUser
, dwParam1
, dwParam2
);
3133 /* FIXME: Pretend this is supported */
3135 case WIDM_OPEN
: return widOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
3136 case WIDM_CLOSE
: return widClose (wDevID
);
3137 case WIDM_ADDBUFFER
: return widAddBuffer (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
3138 case WIDM_PREPARE
: return MMSYSERR_NOTSUPPORTED
;
3139 case WIDM_UNPREPARE
: return MMSYSERR_NOTSUPPORTED
;
3140 case WIDM_GETDEVCAPS
: return widGetDevCaps (wDevID
, (LPWAVEINCAPSW
)dwParam1
, dwParam2
);
3141 case WIDM_GETNUMDEVS
: return numInDev
;
3142 case WIDM_GETPOS
: return widGetPosition(wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
3143 case WIDM_RESET
: return widReset (wDevID
);
3144 case WIDM_START
: return widStart (wDevID
);
3145 case WIDM_STOP
: return widStop (wDevID
);
3146 case DRV_QUERYDEVICEINTERFACESIZE
: return widDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
3147 case DRV_QUERYDEVICEINTERFACE
: return widDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
3148 case DRV_QUERYDSOUNDIFACE
: return widDsCreate (wDevID
, (PIDSCDRIVER
*)dwParam1
);
3149 case DRV_QUERYDSOUNDDESC
: return widDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
3151 FIXME("unknown message %u!\n", wMsg
);
3153 return MMSYSERR_NOTSUPPORTED
;
3156 #else /* !HAVE_OSS */
3158 /**************************************************************************
3159 * wodMessage (WINEOSS.7)
3161 DWORD WINAPI
OSS_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3162 DWORD dwParam1
, DWORD dwParam2
)
3164 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
3165 return MMSYSERR_NOTENABLED
;
3168 /**************************************************************************
3169 * widMessage (WINEOSS.6)
3171 DWORD WINAPI
OSS_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3172 DWORD dwParam1
, DWORD dwParam2
)
3174 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
3175 return MMSYSERR_NOTENABLED
;
3178 #endif /* HAVE_OSS */