1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 * Sample Wine Driver for Open Sound System (featured in Linux and FreeBSD)
5 * Copyright 1994 Martin Ayotte
6 * 1999 Eric Pouech (async playing in waveOut/waveIn)
7 * 2000 Eric Pouech (loops in waveOut)
8 * 2002 Eric Pouech (full duplex)
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * pause in waveOut does not work correctly in loop mode
27 * Direct Sound Capture driver does not work (not complete yet)
30 /*#define EMULATE_SB16*/
32 /* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
35 /* an exact wodGetPosition is usually not worth the extra context switches,
36 * as we're going to have near fragment accuracy anyway */
37 /* #define EXACT_WODPOSITION */
40 #include "wine/port.h"
51 #ifdef HAVE_SYS_IOCTL_H
52 # include <sys/ioctl.h>
54 #ifdef HAVE_SYS_MMAN_H
55 # include <sys/mman.h>
57 #ifdef HAVE_SYS_POLL_H
58 # include <sys/poll.h>
65 #include "wine/winuser16.h"
70 #include "wine/debug.h"
72 WINE_DEFAULT_DEBUG_CHANNEL(wave
);
74 /* Allow 1% deviation for sample rates (some ES137x cards) */
75 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
79 #define MAX_WAVEDRV (6)
81 /* state diagram for waveOut writing:
83 * +---------+-------------+---------------+---------------------------------+
84 * | state | function | event | new state |
85 * +---------+-------------+---------------+---------------------------------+
86 * | | open() | | STOPPED |
87 * | PAUSED | write() | | PAUSED |
88 * | STOPPED | write() | <thrd create> | PLAYING |
89 * | PLAYING | write() | HEADER | PLAYING |
90 * | (other) | write() | <error> | |
91 * | (any) | pause() | PAUSING | PAUSED |
92 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
93 * | (any) | reset() | RESETTING | STOPPED |
94 * | (any) | close() | CLOSING | CLOSED |
95 * +---------+-------------+---------------+---------------------------------+
98 /* states of the playing device */
99 #define WINE_WS_PLAYING 0
100 #define WINE_WS_PAUSED 1
101 #define WINE_WS_STOPPED 2
102 #define WINE_WS_CLOSED 3
104 /* events to be send to device */
105 enum win_wm_message
{
106 WINE_WM_PAUSING
= WM_USER
+ 1, WINE_WM_RESTARTING
, WINE_WM_RESETTING
, WINE_WM_HEADER
,
107 WINE_WM_UPDATE
, WINE_WM_BREAKLOOP
, WINE_WM_CLOSING
, WINE_WM_STARTING
, WINE_WM_STOPPING
111 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
112 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
113 #define RESET_OMR(omr) do { } while (0)
114 #define WAIT_OMR(omr, sleep) \
115 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
116 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
118 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
119 #define CLEAR_OMR(omr) do { } while (0)
120 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
121 #define WAIT_OMR(omr, sleep) \
122 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
126 enum win_wm_message msg
; /* message identifier */
127 DWORD param
; /* parameter for this message */
128 HANDLE hEvent
; /* if message is synchronous, handle of event for synchro */
131 /* implement an in-process message ring for better performance
132 * (compared to passing thru the server)
133 * this ring will be used by the input (resp output) record (resp playback) routine
135 #define OSS_RING_BUFFER_INCREMENT 64
137 int ring_buffer_size
;
146 CRITICAL_SECTION msg_crst
;
149 typedef struct tagOSS_DEVICE
{
153 WAVEOUTCAPSA out_caps
;
155 DWORD in_caps_support
;
156 unsigned open_access
;
162 unsigned audio_fragment
;
164 BOOL bTriggerSupport
;
167 DSDRIVERDESC ds_desc
;
168 DSDRIVERCAPS ds_caps
;
169 DSCDRIVERCAPS dsc_caps
;
174 static OSS_DEVICE OSS_Devices
[MAX_WAVEDRV
];
178 volatile int state
; /* one of the WINE_WS_ manifest constants */
179 WAVEOPENDESC waveDesc
;
181 PCMWAVEFORMAT format
;
183 /* OSS information */
184 DWORD dwFragmentSize
; /* size of OSS buffer fragment */
185 DWORD dwBufferSize
; /* size of whole OSS buffer in bytes */
186 LPWAVEHDR lpQueuePtr
; /* start of queued WAVEHDRs (waiting to be notified) */
187 LPWAVEHDR lpPlayPtr
; /* start of not yet fully played buffers */
188 DWORD dwPartialOffset
; /* Offset of not yet written bytes in lpPlayPtr */
190 LPWAVEHDR lpLoopPtr
; /* pointer of first buffer in loop, if any */
191 DWORD dwLoops
; /* private copy of loop counter */
193 DWORD dwPlayedTotal
; /* number of bytes actually played since opening */
194 DWORD dwWrittenTotal
; /* number of bytes written to OSS buffer since opening */
195 BOOL bNeedPost
; /* whether audio still needs to be physically started */
197 /* synchronization stuff */
198 HANDLE hStartUpEvent
;
201 OSS_MSG_RING msgRing
;
203 /* DirectSound stuff */
211 DWORD dwFragmentSize
; /* OpenSound '/dev/dsp' give us that size */
212 WAVEOPENDESC waveDesc
;
214 PCMWAVEFORMAT format
;
215 LPWAVEHDR lpQueuePtr
;
216 DWORD dwTotalRecorded
;
218 /* synchronization stuff */
221 HANDLE hStartUpEvent
;
222 OSS_MSG_RING msgRing
;
224 /* DirectSound stuff */
229 static WINE_WAVEOUT WOutDev
[MAX_WAVEDRV
];
230 static WINE_WAVEIN WInDev
[MAX_WAVEDRV
];
231 static unsigned numOutDev
;
232 static unsigned numInDev
;
234 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
);
235 static DWORD
widDsCreate(UINT wDevID
, PIDSCDRIVER
* drv
);
236 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
);
237 static DWORD
widDsDesc(UINT wDevID
, PDSDRIVERDESC desc
);
238 static DWORD
wodDsGuid(UINT wDevID
, LPGUID pGuid
);
239 static DWORD
widDsGuid(UINT wDevID
, LPGUID pGuid
);
241 /* These strings used only for tracing */
242 static const char *wodPlayerCmdString
[] = {
244 "WINE_WM_RESTARTING",
254 static int getEnables(OSS_DEVICE
*ossdev
)
256 return ( (ossdev
->bOutputEnabled
? PCM_ENABLE_OUTPUT
: 0) |
257 (ossdev
->bInputEnabled
? PCM_ENABLE_INPUT
: 0) );
260 /*======================================================================*
261 * Low level WAVE implementation *
262 *======================================================================*/
264 /******************************************************************
267 * Low level device opening (from values stored in ossdev)
269 static DWORD
OSS_RawOpenDevice(OSS_DEVICE
* ossdev
, int strict_format
)
272 TRACE("(%p,%d)\n",ossdev
,strict_format
);
274 if ((fd
= open(ossdev
->dev_name
, ossdev
->open_access
|O_NDELAY
, 0)) == -1)
276 WARN("Couldn't open %s (%s)\n", ossdev
->dev_name
, strerror(errno
));
277 return (errno
== EBUSY
) ? MMSYSERR_ALLOCATED
: MMSYSERR_ERROR
;
279 fcntl(fd
, F_SETFD
, 1); /* set close on exec flag */
280 /* turn full duplex on if it has been requested */
281 if (ossdev
->open_access
== O_RDWR
&& ossdev
->full_duplex
) {
282 rc
= ioctl(fd
, SNDCTL_DSP_SETDUPLEX
, 0);
283 /* on *BSD, as full duplex is always enabled by default, this ioctl
284 * will fail with EINVAL
285 * so, we don't consider EINVAL an error here
287 if (rc
!= 0 && errno
!= EINVAL
) {
288 ERR("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev
->dev_name
, strerror(errno
));
293 if (ossdev
->audio_fragment
) {
294 rc
= ioctl(fd
, SNDCTL_DSP_SETFRAGMENT
, &ossdev
->audio_fragment
);
296 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev
->dev_name
, strerror(errno
));
301 /* First size and stereo then samplerate */
302 if (ossdev
->format
>=0)
304 val
= ossdev
->format
;
305 rc
= ioctl(fd
, SNDCTL_DSP_SETFMT
, &ossdev
->format
);
306 if (rc
!= 0 || val
!= ossdev
->format
) {
307 TRACE("Can't set format to %d (returned %d)\n", val
, ossdev
->format
);
312 if (ossdev
->stereo
>=0)
314 val
= ossdev
->stereo
;
315 rc
= ioctl(fd
, SNDCTL_DSP_STEREO
, &ossdev
->stereo
);
316 if (rc
!= 0 || val
!= ossdev
->stereo
) {
317 TRACE("Can't set stereo to %u (returned %d)\n", val
, ossdev
->stereo
);
322 if (ossdev
->sample_rate
>=0)
324 val
= ossdev
->sample_rate
;
325 rc
= ioctl(fd
, SNDCTL_DSP_SPEED
, &ossdev
->sample_rate
);
326 if (rc
!= 0 || !NEAR_MATCH(val
, ossdev
->sample_rate
)) {
327 TRACE("Can't set sample_rate to %u (returned %d)\n", val
, ossdev
->sample_rate
);
334 if (ossdev
->bTriggerSupport
) {
336 rc
= ioctl(fd
, SNDCTL_DSP_GETTRIGGER
, &trigger
);
338 ERR("ioctl(%s, SNDCTL_DSP_GETTRIGGER) failed (%s)\n",
339 ossdev
->dev_name
, strerror(errno
));
343 ossdev
->bOutputEnabled
= ((trigger
& PCM_ENABLE_OUTPUT
) == PCM_ENABLE_OUTPUT
);
344 ossdev
->bInputEnabled
= ((trigger
& PCM_ENABLE_INPUT
) == PCM_ENABLE_INPUT
);
346 ossdev
->bOutputEnabled
= TRUE
; /* OSS enables by default */
347 ossdev
->bInputEnabled
= TRUE
; /* OSS enables by default */
350 return MMSYSERR_NOERROR
;
354 return WAVERR_BADFORMAT
;
357 return MMSYSERR_ERROR
;
360 /******************************************************************
363 * since OSS has poor capabilities in full duplex, we try here to let a program
364 * open the device for both waveout and wavein streams...
365 * this is hackish, but it's the way OSS interface is done...
367 static DWORD
OSS_OpenDevice(OSS_DEVICE
* ossdev
, unsigned req_access
,
368 int* frag
, int strict_format
,
369 int sample_rate
, int stereo
, int fmt
)
372 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev
,req_access
,frag
,strict_format
,sample_rate
,stereo
,fmt
);
374 if (ossdev
->full_duplex
&& (req_access
== O_RDONLY
|| req_access
== O_WRONLY
))
377 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
378 if (ossdev
->open_count
== 0)
380 if (access(ossdev
->dev_name
, 0) != 0) return MMSYSERR_NODRIVER
;
382 ossdev
->audio_fragment
= (frag
) ? *frag
: 0;
383 ossdev
->sample_rate
= sample_rate
;
384 ossdev
->stereo
= stereo
;
385 ossdev
->format
= fmt
;
386 ossdev
->open_access
= req_access
;
387 ossdev
->owner_tid
= GetCurrentThreadId();
389 if ((ret
= OSS_RawOpenDevice(ossdev
,strict_format
)) != MMSYSERR_NOERROR
) return ret
;
393 /* check we really open with the same parameters */
394 if (ossdev
->open_access
!= req_access
)
396 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
397 return WAVERR_BADFORMAT
;
400 /* check if the audio parameters are the same */
401 if (ossdev
->sample_rate
!= sample_rate
||
402 ossdev
->stereo
!= stereo
||
403 ossdev
->format
!= fmt
)
405 /* This is not a fatal error because MSACM might do the remapping */
406 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
407 "OSS doesn't allow us different parameters\n"
408 "audio_frag(%x/%x) sample_rate(%d/%d) stereo(%d/%d) fmt(%d/%d)\n",
409 ossdev
->audio_fragment
, frag
? *frag
: 0,
410 ossdev
->sample_rate
, sample_rate
,
411 ossdev
->stereo
, stereo
,
412 ossdev
->format
, fmt
);
413 return WAVERR_BADFORMAT
;
415 /* check if the fragment sizes are the same */
416 if (ossdev
->audio_fragment
!= (frag
? *frag
: 0) ) {
417 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
418 "Use: \"HardwareAcceleration\" = \"Emulation\" in the [dsound] section of your config file.\n");
419 return WAVERR_BADFORMAT
;
421 if (GetCurrentThreadId() != ossdev
->owner_tid
)
423 WARN("Another thread is trying to access audio...\n");
424 return MMSYSERR_ERROR
;
428 ossdev
->open_count
++;
430 return MMSYSERR_NOERROR
;
433 /******************************************************************
438 static void OSS_CloseDevice(OSS_DEVICE
* ossdev
)
440 TRACE("(%p)\n",ossdev
);
441 if (ossdev
->open_count
>0) {
442 ossdev
->open_count
--;
444 WARN("OSS_CloseDevice called too many times\n");
446 if (ossdev
->open_count
== 0)
448 /* reset the device before we close it in case it is in a bad state */
449 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
454 /******************************************************************
457 * Resets the device. OSS Commercial requires the device to be closed
458 * after a SNDCTL_DSP_RESET ioctl call... this function implements
460 * FIXME: This causes problems when doing full duplex so we really
461 * only reset when not doing full duplex. We need to do this better
464 static DWORD
OSS_ResetDevice(OSS_DEVICE
* ossdev
)
466 DWORD ret
= MMSYSERR_NOERROR
;
467 int old_fd
= ossdev
->fd
;
468 TRACE("(%p)\n", ossdev
);
470 if (ossdev
->open_count
== 1) {
471 if (ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, NULL
) == -1)
473 perror("ioctl SNDCTL_DSP_RESET");
477 ret
= OSS_RawOpenDevice(ossdev
, 1);
478 TRACE("Changing fd from %d to %d\n", old_fd
, ossdev
->fd
);
480 WARN("Not resetting device because it is in full duplex mode!\n");
485 const static int win_std_oss_fmts
[2]={AFMT_U8
,AFMT_S16_LE
};
486 const static int win_std_rates
[5]={96000,48000,44100,22050,11025};
487 const static int win_std_formats
[2][2][5]=
488 {{{WAVE_FORMAT_96M08
, WAVE_FORMAT_48M08
, WAVE_FORMAT_4M08
,
489 WAVE_FORMAT_2M08
, WAVE_FORMAT_1M08
},
490 {WAVE_FORMAT_96S08
, WAVE_FORMAT_48S08
, WAVE_FORMAT_4S08
,
491 WAVE_FORMAT_2S08
, WAVE_FORMAT_1S08
}},
492 {{WAVE_FORMAT_96M16
, WAVE_FORMAT_48M16
, WAVE_FORMAT_4M16
,
493 WAVE_FORMAT_2M16
, WAVE_FORMAT_1M16
},
494 {WAVE_FORMAT_96S16
, WAVE_FORMAT_48S16
, WAVE_FORMAT_4S16
,
495 WAVE_FORMAT_2S16
, WAVE_FORMAT_1S16
}},
498 /******************************************************************
503 static BOOL
OSS_WaveOutInit(OSS_DEVICE
* ossdev
)
507 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
509 if (OSS_OpenDevice(ossdev
, O_WRONLY
, NULL
, 0,-1,-1,-1) != 0) return FALSE
;
510 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
512 #ifdef SOUND_MIXER_INFO
515 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
517 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
519 strncpy(ossdev
->ds_desc
.szDesc
, info
.name
, sizeof(info
.name
));
520 strcpy(ossdev
->ds_desc
.szDrvName
, "wineoss.drv");
521 strncpy(ossdev
->out_caps
.szPname
, info
.name
, sizeof(info
.name
));
522 TRACE("%s\n", ossdev
->ds_desc
.szDesc
);
524 ERR("%s: can't read info!\n", ossdev
->mixer_name
);
525 OSS_CloseDevice(ossdev
);
530 ERR("%s: %s\n", ossdev
->mixer_name
, strerror( errno
));
531 OSS_CloseDevice(ossdev
);
535 #endif /* SOUND_MIXER_INFO */
537 /* FIXME: some programs compare this string against the content of the
538 * registry for MM drivers. The names have to match in order for the
539 * program to work (e.g. MS win9x mplayer.exe)
542 ossdev
->out_caps
.wMid
= 0x0002;
543 ossdev
->out_caps
.wPid
= 0x0104;
544 strcpy(ossdev
->out_caps
.szPname
, "SB16 Wave Out");
546 ossdev
->out_caps
.wMid
= 0x00FF; /* Manufac ID */
547 ossdev
->out_caps
.wPid
= 0x0001; /* Product ID */
549 ossdev
->out_caps
.vDriverVersion
= 0x0100;
550 ossdev
->out_caps
.wChannels
= 1;
551 ossdev
->out_caps
.dwFormats
= 0x00000000;
552 ossdev
->out_caps
.wReserved1
= 0;
553 ossdev
->out_caps
.dwSupport
= WAVECAPS_VOLUME
;
555 /* direct sound caps */
556 ossdev
->ds_caps
.dwFlags
= 0;
557 ossdev
->ds_caps
.dwPrimaryBuffers
= 1;
558 ossdev
->ds_caps
.dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
559 ossdev
->ds_caps
.dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
561 if (WINE_TRACE_ON(wave
)) {
562 /* Note that this only reports the formats supported by the hardware.
563 * The driver may support other formats and do the conversions in
564 * software which is why we don't use this value
567 ioctl(ossdev
->fd
, SNDCTL_DSP_GETFMTS
, &oss_mask
);
568 TRACE("OSS dsp out mask=%08x\n", oss_mask
);
571 /* We must first set the format and the stereo mode as some sound cards
572 * may support 44kHz mono but not 44kHz stereo. Also we must
573 * systematically check the return value of these ioctls as they will
574 * always succeed (see OSS Linux) but will modify the parameter to match
575 * whatever they support. The OSS specs also say we must first set the
576 * sample size, then the stereo and then the sample rate.
579 arg
=win_std_oss_fmts
[f
];
580 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
581 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
582 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
583 rc
,arg
,win_std_oss_fmts
[f
]);
587 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARY8BIT
;
589 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARY16BIT
;
593 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_STEREO
, &arg
);
594 if (rc
!=0 || arg
!=c
) {
595 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc
,arg
,c
);
599 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYMONO
;
601 ossdev
->out_caps
.wChannels
=2;
602 ossdev
->out_caps
.dwSupport
|=WAVECAPS_LRVOLUME
;
603 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYSTEREO
;
606 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
607 arg
=win_std_rates
[r
];
608 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
609 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
610 rc
,arg
,win_std_rates
[r
],win_std_oss_fmts
[f
],c
+1);
611 if (rc
==0 && arg
!=0 && NEAR_MATCH(arg
,win_std_rates
[r
]))
612 ossdev
->out_caps
.dwFormats
|=win_std_formats
[f
][c
][r
];
617 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
618 TRACE("OSS dsp out caps=%08X\n", arg
);
619 if (arg
& DSP_CAP_TRIGGER
)
620 ossdev
->bTriggerSupport
= TRUE
;
621 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
)) {
622 ossdev
->out_caps
.dwSupport
|= WAVECAPS_SAMPLEACCURATE
;
624 /* well, might as well use the DirectSound cap flag for something */
625 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
626 !(arg
& DSP_CAP_BATCH
)) {
627 ossdev
->out_caps
.dwSupport
|= WAVECAPS_DIRECTSOUND
;
629 ossdev
->ds_caps
.dwFlags
|= DSCAPS_EMULDRIVER
;
632 OSS_CloseDevice(ossdev
);
633 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
634 ossdev
->out_caps
.dwFormats
, ossdev
->out_caps
.dwSupport
);
638 /******************************************************************
643 static BOOL
OSS_WaveInInit(OSS_DEVICE
* ossdev
)
647 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
649 if (OSS_OpenDevice(ossdev
, O_RDONLY
, NULL
, 0,-1,-1,-1) != 0)
652 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
654 #ifdef SOUND_MIXER_INFO
657 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
659 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
661 strncpy(ossdev
->in_caps
.szPname
, info
.name
, sizeof(info
.name
));
662 TRACE("%s\n", ossdev
->ds_desc
.szDesc
);
664 ERR("%s: can't read info!\n", ossdev
->mixer_name
);
665 OSS_CloseDevice(ossdev
);
670 ERR("%s: %s\n", ossdev
->mixer_name
, strerror(errno
));
671 OSS_CloseDevice(ossdev
);
675 #endif /* SOUND_MIXER_INFO */
677 /* See comment in OSS_WaveOutInit */
679 ossdev
->in_caps
.wMid
= 0x0002;
680 ossdev
->in_caps
.wPid
= 0x0004;
681 strcpy(ossdev
->in_caps
.szPname
, "SB16 Wave In");
683 ossdev
->in_caps
.wMid
= 0x00FF; /* Manufac ID */
684 ossdev
->in_caps
.wPid
= 0x0001; /* Product ID */
686 ossdev
->in_caps
.dwFormats
= 0x00000000;
687 ossdev
->in_caps
.wChannels
= 1;
688 ossdev
->in_caps
.wReserved1
= 0;
690 /* direct sound caps */
691 ossdev
->dsc_caps
.dwSize
= sizeof(ossdev
->dsc_caps
);
692 ossdev
->dsc_caps
.dwFlags
= 0;
693 ossdev
->dsc_caps
.dwFormats
= 0x00000000;
694 ossdev
->dsc_caps
.dwChannels
= 1;
696 if (WINE_TRACE_ON(wave
)) {
697 /* Note that this only reports the formats supported by the hardware.
698 * The driver may support other formats and do the conversions in
699 * software which is why we don't use this value
702 ioctl(ossdev
->fd
, SNDCTL_DSP_GETFMTS
, &oss_mask
);
703 TRACE("OSS dsp out mask=%08x\n", oss_mask
);
706 /* See the comment in OSS_WaveOutInit */
708 arg
=win_std_oss_fmts
[f
];
709 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
710 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
711 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
712 rc
,arg
,win_std_oss_fmts
[f
]);
718 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_STEREO
, &arg
);
719 if (rc
!=0 || arg
!=c
) {
720 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc
,arg
,c
);
724 ossdev
->in_caps
.wChannels
=2;
725 ossdev
->dsc_caps
.dwChannels
=2;
728 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
729 arg
=win_std_rates
[r
];
730 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
731 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",rc
,arg
,win_std_rates
[r
],win_std_oss_fmts
[f
],c
+1);
732 if (rc
==0 && NEAR_MATCH(arg
,win_std_rates
[r
]))
733 ossdev
->in_caps
.dwFormats
|=win_std_formats
[f
][c
][r
];
734 ossdev
->dsc_caps
.dwFormats
|=win_std_formats
[f
][c
][r
];
739 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
740 TRACE("OSS dsp in caps=%08X\n", arg
);
741 if (arg
& DSP_CAP_TRIGGER
)
742 ossdev
->bTriggerSupport
= TRUE
;
743 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
744 !(arg
& DSP_CAP_BATCH
)) {
745 /* FIXME: enable the next statement if you want to work on the driver */
747 ossdev
->in_caps_support
|= WAVECAPS_DIRECTSOUND
;
750 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
))
751 ossdev
->in_caps_support
|= WAVECAPS_SAMPLEACCURATE
;
753 OSS_CloseDevice(ossdev
);
754 TRACE("in dwFormats = %08lX\n", ossdev
->in_caps
.dwFormats
);
758 /******************************************************************
759 * OSS_WaveFullDuplexInit
763 static void OSS_WaveFullDuplexInit(OSS_DEVICE
* ossdev
)
766 TRACE("(%p)\n",ossdev
);
768 if (OSS_OpenDevice(ossdev
, O_RDWR
, NULL
, 0,-1,-1,-1) != 0) return;
769 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &caps
) == 0)
771 ossdev
->full_duplex
= (caps
& DSP_CAP_DUPLEX
);
773 OSS_CloseDevice(ossdev
);
776 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
777 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
778 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
779 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
780 guid.Data4[6] = b7; guid.Data4[7] = b8;
781 /******************************************************************
784 * Initialize internal structures from OSS information
786 LONG
OSS_WaveInit(void)
791 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
794 sprintf((char *)OSS_Devices
[i
].dev_name
, "/dev/dsp");
795 sprintf((char *)OSS_Devices
[i
].mixer_name
, "/dev/mixer");
797 sprintf((char *)OSS_Devices
[i
].dev_name
, "/dev/dsp%d", i
);
798 sprintf((char *)OSS_Devices
[i
].mixer_name
, "/dev/mixer%d", i
);
801 INIT_GUID(OSS_Devices
[i
].ds_guid
, 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i
);
802 INIT_GUID(OSS_Devices
[i
].dsc_guid
, 0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i
);
805 /* start with output devices */
806 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
808 if (OSS_WaveOutInit(&OSS_Devices
[i
]))
810 WOutDev
[numOutDev
].state
= WINE_WS_CLOSED
;
811 WOutDev
[numOutDev
].ossdev
= &OSS_Devices
[i
];
816 /* then do input devices */
817 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
819 if (OSS_WaveInInit(&OSS_Devices
[i
]))
821 WInDev
[numInDev
].state
= WINE_WS_CLOSED
;
822 WInDev
[numInDev
].ossdev
= &OSS_Devices
[i
];
827 /* finish with the full duplex bits */
828 for (i
= 0; i
< MAX_WAVEDRV
; i
++)
829 OSS_WaveFullDuplexInit(&OSS_Devices
[i
]);
834 /******************************************************************
835 * OSS_InitRingMessage
837 * Initialize the ring of messages for passing between driver's caller and playback/record
840 static int OSS_InitRingMessage(OSS_MSG_RING
* omr
)
845 if (pipe(omr
->msg_pipe
) < 0) {
846 omr
->msg_pipe
[0] = -1;
847 omr
->msg_pipe
[1] = -1;
848 ERR("could not create pipe, error=%s\n", strerror(errno
));
851 omr
->msg_event
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
853 omr
->ring_buffer_size
= OSS_RING_BUFFER_INCREMENT
;
854 omr
->messages
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,omr
->ring_buffer_size
* sizeof(OSS_MSG
));
855 InitializeCriticalSection(&omr
->msg_crst
);
859 /******************************************************************
860 * OSS_DestroyRingMessage
863 static int OSS_DestroyRingMessage(OSS_MSG_RING
* omr
)
866 close(omr
->msg_pipe
[0]);
867 close(omr
->msg_pipe
[1]);
869 CloseHandle(omr
->msg_event
);
871 HeapFree(GetProcessHeap(),0,omr
->messages
);
872 DeleteCriticalSection(&omr
->msg_crst
);
876 /******************************************************************
879 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
881 static int OSS_AddRingMessage(OSS_MSG_RING
* omr
, enum win_wm_message msg
, DWORD param
, BOOL wait
)
883 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
885 EnterCriticalSection(&omr
->msg_crst
);
886 if ((omr
->msg_toget
== ((omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
)))
888 omr
->ring_buffer_size
+= OSS_RING_BUFFER_INCREMENT
;
889 TRACE("omr->ring_buffer_size=%d\n",omr
->ring_buffer_size
);
890 omr
->messages
= HeapReAlloc(GetProcessHeap(),0,omr
->messages
, omr
->ring_buffer_size
* sizeof(OSS_MSG
));
894 hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
895 if (hEvent
== INVALID_HANDLE_VALUE
)
897 ERR("can't create event !?\n");
898 LeaveCriticalSection(&omr
->msg_crst
);
901 if (omr
->msg_toget
!= omr
->msg_tosave
&& omr
->messages
[omr
->msg_toget
].msg
!= WINE_WM_HEADER
)
902 FIXME("two fast messages in the queue!!!!\n");
904 /* fast messages have to be added at the start of the queue */
905 omr
->msg_toget
= (omr
->msg_toget
+ omr
->ring_buffer_size
- 1) % omr
->ring_buffer_size
;
907 omr
->messages
[omr
->msg_toget
].msg
= msg
;
908 omr
->messages
[omr
->msg_toget
].param
= param
;
909 omr
->messages
[omr
->msg_toget
].hEvent
= hEvent
;
913 omr
->messages
[omr
->msg_tosave
].msg
= msg
;
914 omr
->messages
[omr
->msg_tosave
].param
= param
;
915 omr
->messages
[omr
->msg_tosave
].hEvent
= INVALID_HANDLE_VALUE
;
916 omr
->msg_tosave
= (omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
;
918 LeaveCriticalSection(&omr
->msg_crst
);
919 /* signal a new message */
923 /* wait for playback/record thread to have processed the message */
924 WaitForSingleObject(hEvent
, INFINITE
);
930 /******************************************************************
931 * OSS_RetrieveRingMessage
933 * Get a message from the ring. Should be called by the playback/record thread.
935 static int OSS_RetrieveRingMessage(OSS_MSG_RING
* omr
,
936 enum win_wm_message
*msg
, DWORD
*param
, HANDLE
*hEvent
)
938 EnterCriticalSection(&omr
->msg_crst
);
940 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
942 LeaveCriticalSection(&omr
->msg_crst
);
946 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
947 omr
->messages
[omr
->msg_toget
].msg
= 0;
948 *param
= omr
->messages
[omr
->msg_toget
].param
;
949 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
950 omr
->msg_toget
= (omr
->msg_toget
+ 1) % omr
->ring_buffer_size
;
952 LeaveCriticalSection(&omr
->msg_crst
);
956 /******************************************************************
957 * OSS_PeekRingMessage
959 * Peek at a message from the ring but do not remove it.
960 * Should be called by the playback/record thread.
962 static int OSS_PeekRingMessage(OSS_MSG_RING
* omr
,
963 enum win_wm_message
*msg
,
964 DWORD
*param
, HANDLE
*hEvent
)
966 EnterCriticalSection(&omr
->msg_crst
);
968 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
970 LeaveCriticalSection(&omr
->msg_crst
);
974 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
975 *param
= omr
->messages
[omr
->msg_toget
].param
;
976 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
977 LeaveCriticalSection(&omr
->msg_crst
);
981 /*======================================================================*
982 * Low level WAVE OUT implementation *
983 *======================================================================*/
985 /**************************************************************************
986 * wodNotifyClient [internal]
988 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
990 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg
, dwParam1
, dwParam2
);
996 if (wwo
->wFlags
!= DCB_NULL
&&
997 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
,
998 (HDRVR
)wwo
->waveDesc
.hWave
, wMsg
,
999 wwo
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
1000 WARN("can't notify client !\n");
1001 return MMSYSERR_ERROR
;
1005 FIXME("Unknown callback message %u\n", wMsg
);
1006 return MMSYSERR_INVALPARAM
;
1008 return MMSYSERR_NOERROR
;
1011 /**************************************************************************
1012 * wodUpdatePlayedTotal [internal]
1015 static BOOL
wodUpdatePlayedTotal(WINE_WAVEOUT
* wwo
, audio_buf_info
* info
)
1017 audio_buf_info dspspace
;
1018 if (!info
) info
= &dspspace
;
1020 if (ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_GETOSPACE
, info
) < 0) {
1021 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo
->ossdev
->dev_name
, strerror(errno
));
1024 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
- (wwo
->dwBufferSize
- info
->bytes
);
1028 /**************************************************************************
1029 * wodPlayer_BeginWaveHdr [internal]
1031 * Makes the specified lpWaveHdr the currently playing wave header.
1032 * If the specified wave header is a begin loop and we're not already in
1033 * a loop, setup the loop.
1035 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
1037 wwo
->lpPlayPtr
= lpWaveHdr
;
1039 if (!lpWaveHdr
) return;
1041 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
) {
1042 if (wwo
->lpLoopPtr
) {
1043 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
1045 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
1046 wwo
->lpLoopPtr
= lpWaveHdr
;
1047 /* Windows does not touch WAVEHDR.dwLoops,
1048 * so we need to make an internal copy */
1049 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
1052 wwo
->dwPartialOffset
= 0;
1055 /**************************************************************************
1056 * wodPlayer_PlayPtrNext [internal]
1058 * Advance the play pointer to the next waveheader, looping if required.
1060 static LPWAVEHDR
wodPlayer_PlayPtrNext(WINE_WAVEOUT
* wwo
)
1062 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
1064 wwo
->dwPartialOffset
= 0;
1065 if ((lpWaveHdr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
) {
1066 /* We're at the end of a loop, loop if required */
1067 if (--wwo
->dwLoops
> 0) {
1068 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1070 /* Handle overlapping loops correctly */
1071 if (wwo
->lpLoopPtr
!= lpWaveHdr
&& (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)) {
1072 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1073 /* shall we consider the END flag for the closing loop or for
1074 * the opening one or for both ???
1075 * code assumes for closing loop only
1078 lpWaveHdr
= lpWaveHdr
->lpNext
;
1080 wwo
->lpLoopPtr
= NULL
;
1081 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
);
1084 /* We're not in a loop. Advance to the next wave header */
1085 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
= lpWaveHdr
->lpNext
);
1091 /**************************************************************************
1092 * wodPlayer_DSPWait [internal]
1093 * Returns the number of milliseconds to wait for the DSP buffer to write
1096 static DWORD
wodPlayer_DSPWait(const WINE_WAVEOUT
*wwo
)
1098 /* time for one fragment to be played */
1099 return wwo
->dwFragmentSize
* 1000 / wwo
->format
.wf
.nAvgBytesPerSec
;
1102 /**************************************************************************
1103 * wodPlayer_NotifyWait [internal]
1104 * Returns the number of milliseconds to wait before attempting to notify
1105 * completion of the specified wavehdr.
1106 * This is based on the number of bytes remaining to be written in the
1109 static DWORD
wodPlayer_NotifyWait(const WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
1113 if (lpWaveHdr
->reserved
< wwo
->dwPlayedTotal
) {
1116 dwMillis
= (lpWaveHdr
->reserved
- wwo
->dwPlayedTotal
) * 1000 / wwo
->format
.wf
.nAvgBytesPerSec
;
1117 if (!dwMillis
) dwMillis
= 1;
1124 /**************************************************************************
1125 * wodPlayer_WriteMaxFrags [internal]
1126 * Writes the maximum number of bytes possible to the DSP and returns
1127 * TRUE iff the current playPtr has been fully played
1129 static BOOL
wodPlayer_WriteMaxFrags(WINE_WAVEOUT
* wwo
, DWORD
* bytes
)
1131 DWORD dwLength
= wwo
->lpPlayPtr
->dwBufferLength
- wwo
->dwPartialOffset
;
1132 DWORD toWrite
= min(dwLength
, *bytes
);
1136 TRACE("Writing wavehdr %p.%lu[%lu]/%lu\n",
1137 wwo
->lpPlayPtr
, wwo
->dwPartialOffset
, wwo
->lpPlayPtr
->dwBufferLength
, toWrite
);
1141 written
= write(wwo
->ossdev
->fd
, wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
, toWrite
);
1142 if (written
<= 0) return FALSE
;
1147 if (written
>= dwLength
) {
1148 /* If we wrote all current wavehdr, skip to the next one */
1149 wodPlayer_PlayPtrNext(wwo
);
1152 /* Remove the amount written */
1153 wwo
->dwPartialOffset
+= written
;
1156 wwo
->dwWrittenTotal
+= written
;
1162 /**************************************************************************
1163 * wodPlayer_NotifyCompletions [internal]
1165 * Notifies and remove from queue all wavehdrs which have been played to
1166 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1167 * we notify all wavehdrs and remove them all from the queue even if they
1168 * are unplayed or part of a loop.
1170 static DWORD
wodPlayer_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
1172 LPWAVEHDR lpWaveHdr
;
1174 /* Start from lpQueuePtr and keep notifying until:
1175 * - we hit an unwritten wavehdr
1176 * - we hit the beginning of a running loop
1177 * - we hit a wavehdr which hasn't finished playing
1179 while ((lpWaveHdr
= wwo
->lpQueuePtr
) &&
1181 (lpWaveHdr
!= wwo
->lpPlayPtr
&&
1182 lpWaveHdr
!= wwo
->lpLoopPtr
&&
1183 lpWaveHdr
->reserved
<= wwo
->dwPlayedTotal
))) {
1185 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
1187 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1188 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1190 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
1192 return (lpWaveHdr
&& lpWaveHdr
!= wwo
->lpPlayPtr
&& lpWaveHdr
!= wwo
->lpLoopPtr
) ?
1193 wodPlayer_NotifyWait(wwo
, lpWaveHdr
) : INFINITE
;
1196 /**************************************************************************
1197 * wodPlayer_Reset [internal]
1199 * wodPlayer helper. Resets current output stream.
1201 static void wodPlayer_Reset(WINE_WAVEOUT
* wwo
, BOOL reset
)
1203 wodUpdatePlayedTotal(wwo
, NULL
);
1204 /* updates current notify list */
1205 wodPlayer_NotifyCompletions(wwo
, FALSE
);
1207 /* flush all possible output */
1208 if (OSS_ResetDevice(wwo
->ossdev
) != MMSYSERR_NOERROR
)
1211 wwo
->state
= WINE_WS_STOPPED
;
1216 enum win_wm_message msg
;
1220 /* remove any buffer */
1221 wodPlayer_NotifyCompletions(wwo
, TRUE
);
1223 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
1224 wwo
->state
= WINE_WS_STOPPED
;
1225 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
= 0;
1226 /* Clear partial wavehdr */
1227 wwo
->dwPartialOffset
= 0;
1229 /* remove any existing message in the ring */
1230 EnterCriticalSection(&wwo
->msgRing
.msg_crst
);
1231 /* return all pending headers in queue */
1232 while (OSS_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
))
1234 if (msg
!= WINE_WM_HEADER
)
1236 FIXME("shouldn't have headers left\n");
1240 ((LPWAVEHDR
)param
)->dwFlags
&= ~WHDR_INQUEUE
;
1241 ((LPWAVEHDR
)param
)->dwFlags
|= WHDR_DONE
;
1243 wodNotifyClient(wwo
, WOM_DONE
, param
, 0);
1245 RESET_OMR(&wwo
->msgRing
);
1246 LeaveCriticalSection(&wwo
->msgRing
.msg_crst
);
1248 if (wwo
->lpLoopPtr
) {
1249 /* complicated case, not handled yet (could imply modifying the loop counter */
1250 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1251 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1252 wwo
->dwPartialOffset
= 0;
1253 wwo
->dwWrittenTotal
= wwo
->dwPlayedTotal
; /* this is wrong !!! */
1256 DWORD sz
= wwo
->dwPartialOffset
;
1258 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1259 /* compute the max size playable from lpQueuePtr */
1260 for (ptr
= wwo
->lpQueuePtr
; ptr
!= wwo
->lpPlayPtr
; ptr
= ptr
->lpNext
) {
1261 sz
+= ptr
->dwBufferLength
;
1263 /* because the reset lpPlayPtr will be lpQueuePtr */
1264 if (wwo
->dwWrittenTotal
> wwo
->dwPlayedTotal
+ sz
) ERR("grin\n");
1265 wwo
->dwPartialOffset
= sz
- (wwo
->dwWrittenTotal
- wwo
->dwPlayedTotal
);
1266 wwo
->dwWrittenTotal
= wwo
->dwPlayedTotal
;
1267 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
;
1269 wwo
->state
= WINE_WS_PAUSED
;
1273 /**************************************************************************
1274 * wodPlayer_ProcessMessages [internal]
1276 static void wodPlayer_ProcessMessages(WINE_WAVEOUT
* wwo
)
1278 LPWAVEHDR lpWaveHdr
;
1279 enum win_wm_message msg
;
1283 while (OSS_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
)) {
1284 TRACE("Received %s %lx\n", wodPlayerCmdString
[msg
- WM_USER
- 1], param
);
1286 case WINE_WM_PAUSING
:
1287 wodPlayer_Reset(wwo
, FALSE
);
1290 case WINE_WM_RESTARTING
:
1291 if (wwo
->state
== WINE_WS_PAUSED
)
1293 wwo
->state
= WINE_WS_PLAYING
;
1297 case WINE_WM_HEADER
:
1298 lpWaveHdr
= (LPWAVEHDR
)param
;
1300 /* insert buffer at the end of queue */
1303 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
1306 if (!wwo
->lpPlayPtr
)
1307 wodPlayer_BeginWaveHdr(wwo
,lpWaveHdr
);
1308 if (wwo
->state
== WINE_WS_STOPPED
)
1309 wwo
->state
= WINE_WS_PLAYING
;
1311 case WINE_WM_RESETTING
:
1312 wodPlayer_Reset(wwo
, TRUE
);
1315 case WINE_WM_UPDATE
:
1316 wodUpdatePlayedTotal(wwo
, NULL
);
1319 case WINE_WM_BREAKLOOP
:
1320 if (wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpLoopPtr
!= NULL
) {
1321 /* ensure exit at end of current loop */
1326 case WINE_WM_CLOSING
:
1327 /* sanity check: this should not happen since the device must have been reset before */
1328 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
1330 wwo
->state
= WINE_WS_CLOSED
;
1333 /* shouldn't go here */
1335 FIXME("unknown message %d\n", msg
);
1341 /**************************************************************************
1342 * wodPlayer_FeedDSP [internal]
1343 * Feed as much sound data as we can into the DSP and return the number of
1344 * milliseconds before it will be necessary to feed the DSP again.
1346 static DWORD
wodPlayer_FeedDSP(WINE_WAVEOUT
* wwo
)
1348 audio_buf_info dspspace
;
1351 wodUpdatePlayedTotal(wwo
, &dspspace
);
1352 availInQ
= dspspace
.bytes
;
1353 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1354 dspspace
.fragments
, dspspace
.fragstotal
, dspspace
.fragsize
, dspspace
.bytes
);
1356 /* input queue empty and output buffer with less than one fragment to play
1357 * actually some cards do not play the fragment before the last if this one is partially feed
1358 * so we need to test for full the availability of 2 fragments
1360 if (!wwo
->lpPlayPtr
&& wwo
->dwBufferSize
< availInQ
+ 2 * wwo
->dwFragmentSize
&&
1362 TRACE("Run out of wavehdr:s...\n");
1366 /* no more room... no need to try to feed */
1367 if (dspspace
.fragments
!= 0) {
1368 /* Feed from partial wavehdr */
1369 if (wwo
->lpPlayPtr
&& wwo
->dwPartialOffset
!= 0) {
1370 wodPlayer_WriteMaxFrags(wwo
, &availInQ
);
1373 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1374 if (wwo
->dwPartialOffset
== 0 && wwo
->lpPlayPtr
) {
1376 TRACE("Setting time to elapse for %p to %lu\n",
1377 wwo
->lpPlayPtr
, wwo
->dwWrittenTotal
+ wwo
->lpPlayPtr
->dwBufferLength
);
1378 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1379 wwo
->lpPlayPtr
->reserved
= wwo
->dwWrittenTotal
+ wwo
->lpPlayPtr
->dwBufferLength
;
1380 } while (wodPlayer_WriteMaxFrags(wwo
, &availInQ
) && wwo
->lpPlayPtr
&& availInQ
> 0);
1383 if (wwo
->bNeedPost
) {
1384 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1385 * if it didn't get one, we give it the other */
1386 if (wwo
->dwBufferSize
< availInQ
+ 2 * wwo
->dwFragmentSize
)
1387 ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_POST
, 0);
1388 wwo
->bNeedPost
= FALSE
;
1392 return wodPlayer_DSPWait(wwo
);
1396 /**************************************************************************
1397 * wodPlayer [internal]
1399 static DWORD CALLBACK
wodPlayer(LPVOID pmt
)
1401 WORD uDevID
= (DWORD
)pmt
;
1402 WINE_WAVEOUT
* wwo
= (WINE_WAVEOUT
*)&WOutDev
[uDevID
];
1403 DWORD dwNextFeedTime
= INFINITE
; /* Time before DSP needs feeding */
1404 DWORD dwNextNotifyTime
= INFINITE
; /* Time before next wave completion */
1407 wwo
->state
= WINE_WS_STOPPED
;
1408 SetEvent(wwo
->hStartUpEvent
);
1411 /** Wait for the shortest time before an action is required. If there
1412 * are no pending actions, wait forever for a command.
1414 dwSleepTime
= min(dwNextFeedTime
, dwNextNotifyTime
);
1415 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime
, dwNextFeedTime
, dwNextNotifyTime
);
1416 WAIT_OMR(&wwo
->msgRing
, dwSleepTime
);
1417 wodPlayer_ProcessMessages(wwo
);
1418 if (wwo
->state
== WINE_WS_PLAYING
) {
1419 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1420 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1421 if (dwNextFeedTime
== INFINITE
) {
1422 /* FeedDSP ran out of data, but before flushing, */
1423 /* check that a notification didn't give us more */
1424 wodPlayer_ProcessMessages(wwo
);
1425 if (!wwo
->lpPlayPtr
) {
1426 TRACE("flushing\n");
1427 ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_SYNC
, 0);
1428 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
;
1429 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1431 TRACE("recovering\n");
1432 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1436 dwNextFeedTime
= dwNextNotifyTime
= INFINITE
;
1441 /**************************************************************************
1442 * wodGetDevCaps [internal]
1444 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSA lpCaps
, DWORD dwSize
)
1446 TRACE("(%u, %p, %lu);\n", wDevID
, lpCaps
, dwSize
);
1448 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
1450 if (wDevID
>= numOutDev
) {
1451 TRACE("numOutDev reached !\n");
1452 return MMSYSERR_BADDEVICEID
;
1455 memcpy(lpCaps
, &WOutDev
[wDevID
].ossdev
->out_caps
, min(dwSize
, sizeof(*lpCaps
)));
1456 return MMSYSERR_NOERROR
;
1459 /**************************************************************************
1460 * wodOpen [internal]
1462 static DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1466 audio_buf_info info
;
1469 TRACE("(%u, %p[cb=%08lx], %08lX);\n", wDevID
, lpDesc
, lpDesc
->dwCallback
, dwFlags
);
1470 if (lpDesc
== NULL
) {
1471 WARN("Invalid Parameter !\n");
1472 return MMSYSERR_INVALPARAM
;
1474 if (wDevID
>= numOutDev
) {
1475 TRACE("MAX_WAVOUTDRV reached !\n");
1476 return MMSYSERR_BADDEVICEID
;
1479 /* only PCM format is supported so far... */
1480 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
1481 lpDesc
->lpFormat
->nChannels
== 0 ||
1482 lpDesc
->lpFormat
->nSamplesPerSec
== 0) {
1483 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1484 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1485 lpDesc
->lpFormat
->nSamplesPerSec
);
1486 return WAVERR_BADFORMAT
;
1489 if (dwFlags
& WAVE_FORMAT_QUERY
) {
1490 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1491 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1492 lpDesc
->lpFormat
->nSamplesPerSec
);
1493 return MMSYSERR_NOERROR
;
1496 wwo
= &WOutDev
[wDevID
];
1498 if ((dwFlags
& WAVE_DIRECTSOUND
) &&
1499 !(wwo
->ossdev
->out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
))
1500 /* not supported, ignore it */
1501 dwFlags
&= ~WAVE_DIRECTSOUND
;
1503 if (dwFlags
& WAVE_DIRECTSOUND
) {
1504 if (wwo
->ossdev
->out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
1505 /* we have realtime DirectSound, fragments just waste our time,
1506 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1507 audio_fragment
= 0x0020000B;
1509 /* to approximate realtime, we must use small fragments,
1510 * let's try to fragment the above 64KB (256 * 2^8) */
1511 audio_fragment
= 0x01000008;
1513 /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
1514 * thus leading to 46ms per fragment, and a turnaround time of 185ms
1516 /* 16 fragments max, 2^10=1024 bytes per fragment */
1517 audio_fragment
= 0x000F000A;
1519 if (wwo
->state
!= WINE_WS_CLOSED
) return MMSYSERR_ALLOCATED
;
1520 /* we want to be able to mmap() the device, which means it must be opened readable,
1521 * otherwise mmap() will fail (at least under Linux) */
1522 ret
= OSS_OpenDevice(wwo
->ossdev
,
1523 (dwFlags
& WAVE_DIRECTSOUND
) ? O_RDWR
: O_WRONLY
,
1525 (dwFlags
& WAVE_DIRECTSOUND
) ? 0 : 1,
1526 lpDesc
->lpFormat
->nSamplesPerSec
,
1527 (lpDesc
->lpFormat
->nChannels
> 1) ? 1 : 0,
1528 (lpDesc
->lpFormat
->wBitsPerSample
== 16)
1529 ? AFMT_S16_LE
: AFMT_U8
);
1530 if ((ret
==MMSYSERR_NOERROR
) && (dwFlags
& WAVE_DIRECTSOUND
)) {
1531 lpDesc
->lpFormat
->nSamplesPerSec
=wwo
->ossdev
->sample_rate
;
1532 lpDesc
->lpFormat
->nChannels
=(wwo
->ossdev
->stereo
? 2 : 1);
1533 lpDesc
->lpFormat
->wBitsPerSample
=(wwo
->ossdev
->format
== AFMT_U8
? 8 : 16);
1534 lpDesc
->lpFormat
->nBlockAlign
=lpDesc
->lpFormat
->nChannels
*lpDesc
->lpFormat
->wBitsPerSample
/8;
1535 lpDesc
->lpFormat
->nAvgBytesPerSec
=lpDesc
->lpFormat
->nSamplesPerSec
*lpDesc
->lpFormat
->nBlockAlign
;
1536 TRACE("OSS_OpenDevice returned this format: %ldx%dx%d\n",
1537 lpDesc
->lpFormat
->nSamplesPerSec
,
1538 lpDesc
->lpFormat
->wBitsPerSample
,
1539 lpDesc
->lpFormat
->nChannels
);
1541 if (ret
!= 0) return ret
;
1542 wwo
->state
= WINE_WS_STOPPED
;
1544 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1546 memcpy(&wwo
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
1547 memcpy(&wwo
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
1549 if (wwo
->format
.wBitsPerSample
== 0) {
1550 WARN("Resetting zeroed wBitsPerSample\n");
1551 wwo
->format
.wBitsPerSample
= 8 *
1552 (wwo
->format
.wf
.nAvgBytesPerSec
/
1553 wwo
->format
.wf
.nSamplesPerSec
) /
1554 wwo
->format
.wf
.nChannels
;
1556 /* Read output space info for future reference */
1557 if (ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
1558 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo
->ossdev
->dev_name
, strerror(errno
));
1559 OSS_CloseDevice(wwo
->ossdev
);
1560 wwo
->state
= WINE_WS_CLOSED
;
1561 return MMSYSERR_NOTENABLED
;
1564 /* Check that fragsize is correct per our settings above */
1565 if ((info
.fragsize
> 1024) && (LOWORD(audio_fragment
) <= 10)) {
1566 /* we've tried to set 1K fragments or less, but it didn't work */
1567 ERR("fragment size set failed, size is now %d\n", info
.fragsize
);
1568 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
1569 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
1572 /* Remember fragsize and total buffer size for future use */
1573 wwo
->dwFragmentSize
= info
.fragsize
;
1574 wwo
->dwBufferSize
= info
.fragstotal
* info
.fragsize
;
1575 wwo
->dwPlayedTotal
= 0;
1576 wwo
->dwWrittenTotal
= 0;
1577 wwo
->bNeedPost
= TRUE
;
1579 OSS_InitRingMessage(&wwo
->msgRing
);
1581 wwo
->hStartUpEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
1582 wwo
->hThread
= CreateThread(NULL
, 0, wodPlayer
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwo
->dwThreadID
));
1583 WaitForSingleObject(wwo
->hStartUpEvent
, INFINITE
);
1584 CloseHandle(wwo
->hStartUpEvent
);
1585 wwo
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
1587 TRACE("fd=%d fragmentSize=%ld\n",
1588 wwo
->ossdev
->fd
, wwo
->dwFragmentSize
);
1589 if (wwo
->dwFragmentSize
% wwo
->format
.wf
.nBlockAlign
)
1590 ERR("Fragment doesn't contain an integral number of data blocks\n");
1592 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1593 wwo
->format
.wBitsPerSample
, wwo
->format
.wf
.nAvgBytesPerSec
,
1594 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1595 wwo
->format
.wf
.nBlockAlign
);
1597 return wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
1600 /**************************************************************************
1601 * wodClose [internal]
1603 static DWORD
wodClose(WORD wDevID
)
1605 DWORD ret
= MMSYSERR_NOERROR
;
1608 TRACE("(%u);\n", wDevID
);
1610 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1611 WARN("bad device ID !\n");
1612 return MMSYSERR_BADDEVICEID
;
1615 wwo
= &WOutDev
[wDevID
];
1616 if (wwo
->lpQueuePtr
) {
1617 WARN("buffers still playing !\n");
1618 ret
= WAVERR_STILLPLAYING
;
1620 if (wwo
->hThread
!= INVALID_HANDLE_VALUE
) {
1621 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
1624 munmap(wwo
->mapping
, wwo
->maplen
);
1625 wwo
->mapping
= NULL
;
1628 OSS_DestroyRingMessage(&wwo
->msgRing
);
1630 OSS_CloseDevice(wwo
->ossdev
);
1631 wwo
->state
= WINE_WS_CLOSED
;
1632 wwo
->dwFragmentSize
= 0;
1633 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
1638 /**************************************************************************
1639 * wodWrite [internal]
1642 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1644 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1646 /* first, do the sanity checks... */
1647 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1648 WARN("bad dev ID !\n");
1649 return MMSYSERR_BADDEVICEID
;
1652 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
1653 return WAVERR_UNPREPARED
;
1655 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1656 return WAVERR_STILLPLAYING
;
1658 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1659 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
1660 lpWaveHdr
->lpNext
= 0;
1662 if ((lpWaveHdr
->dwBufferLength
& (WOutDev
[wDevID
].format
.wf
.nBlockAlign
- 1)) != 0)
1664 WARN("WaveHdr length isn't a multiple of the PCM block size: %ld %% %d\n",lpWaveHdr
->dwBufferLength
,WOutDev
[wDevID
].format
.wf
.nBlockAlign
);
1665 lpWaveHdr
->dwBufferLength
&= ~(WOutDev
[wDevID
].format
.wf
.nBlockAlign
- 1);
1668 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
1670 return MMSYSERR_NOERROR
;
1673 /**************************************************************************
1674 * wodPrepare [internal]
1676 static DWORD
wodPrepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1678 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1680 if (wDevID
>= numOutDev
) {
1681 WARN("bad device ID !\n");
1682 return MMSYSERR_BADDEVICEID
;
1685 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1686 return WAVERR_STILLPLAYING
;
1688 lpWaveHdr
->dwFlags
|= WHDR_PREPARED
;
1689 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1690 return MMSYSERR_NOERROR
;
1693 /**************************************************************************
1694 * wodUnprepare [internal]
1696 static DWORD
wodUnprepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1698 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1700 if (wDevID
>= numOutDev
) {
1701 WARN("bad device ID !\n");
1702 return MMSYSERR_BADDEVICEID
;
1705 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1706 return WAVERR_STILLPLAYING
;
1708 lpWaveHdr
->dwFlags
&= ~WHDR_PREPARED
;
1709 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1711 return MMSYSERR_NOERROR
;
1714 /**************************************************************************
1715 * wodPause [internal]
1717 static DWORD
wodPause(WORD wDevID
)
1719 TRACE("(%u);!\n", wDevID
);
1721 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1722 WARN("bad device ID !\n");
1723 return MMSYSERR_BADDEVICEID
;
1726 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_PAUSING
, 0, TRUE
);
1728 return MMSYSERR_NOERROR
;
1731 /**************************************************************************
1732 * wodRestart [internal]
1734 static DWORD
wodRestart(WORD wDevID
)
1736 TRACE("(%u);\n", wDevID
);
1738 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1739 WARN("bad device ID !\n");
1740 return MMSYSERR_BADDEVICEID
;
1743 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESTARTING
, 0, TRUE
);
1745 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1746 /* FIXME: Myst crashes with this ... hmm -MM
1747 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1750 return MMSYSERR_NOERROR
;
1753 /**************************************************************************
1754 * wodReset [internal]
1756 static DWORD
wodReset(WORD wDevID
)
1758 TRACE("(%u);\n", wDevID
);
1760 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1761 WARN("bad device ID !\n");
1762 return MMSYSERR_BADDEVICEID
;
1765 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
1767 return MMSYSERR_NOERROR
;
1770 /**************************************************************************
1771 * wodGetPosition [internal]
1773 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
1779 TRACE("(%u, %p, %lu);\n", wDevID
, lpTime
, uSize
);
1781 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1782 WARN("bad device ID !\n");
1783 return MMSYSERR_BADDEVICEID
;
1786 if (lpTime
== NULL
) return MMSYSERR_INVALPARAM
;
1788 wwo
= &WOutDev
[wDevID
];
1789 #ifdef EXACT_WODPOSITION
1790 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
1792 val
= wwo
->dwPlayedTotal
;
1794 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1795 lpTime
->wType
, wwo
->format
.wBitsPerSample
,
1796 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1797 wwo
->format
.wf
.nAvgBytesPerSec
);
1798 TRACE("dwPlayedTotal=%lu\n", val
);
1800 switch (lpTime
->wType
) {
1803 TRACE("TIME_BYTES=%lu\n", lpTime
->u
.cb
);
1806 lpTime
->u
.sample
= val
* 8 / wwo
->format
.wBitsPerSample
/wwo
->format
.wf
.nChannels
;
1807 TRACE("TIME_SAMPLES=%lu\n", lpTime
->u
.sample
);
1810 time
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1811 lpTime
->u
.smpte
.hour
= time
/ (60 * 60 * 1000);
1812 time
-= lpTime
->u
.smpte
.hour
* (60 * 60 * 1000);
1813 lpTime
->u
.smpte
.min
= time
/ (60 * 1000);
1814 time
-= lpTime
->u
.smpte
.min
* (60 * 1000);
1815 lpTime
->u
.smpte
.sec
= time
/ 1000;
1816 time
-= lpTime
->u
.smpte
.sec
* 1000;
1817 lpTime
->u
.smpte
.frame
= time
* 30 / 1000;
1818 lpTime
->u
.smpte
.fps
= 30;
1819 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1820 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
1821 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
1824 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime
->wType
);
1825 lpTime
->wType
= TIME_MS
;
1827 lpTime
->u
.ms
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1828 TRACE("TIME_MS=%lu\n", lpTime
->u
.ms
);
1831 return MMSYSERR_NOERROR
;
1834 /**************************************************************************
1835 * wodBreakLoop [internal]
1837 static DWORD
wodBreakLoop(WORD wDevID
)
1839 TRACE("(%u);\n", wDevID
);
1841 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1842 WARN("bad device ID !\n");
1843 return MMSYSERR_BADDEVICEID
;
1845 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_BREAKLOOP
, 0, TRUE
);
1846 return MMSYSERR_NOERROR
;
1849 /**************************************************************************
1850 * wodGetVolume [internal]
1852 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
1858 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
1860 if (lpdwVol
== NULL
)
1861 return MMSYSERR_NOTENABLED
;
1862 if (wDevID
>= numOutDev
)
1863 return MMSYSERR_INVALPARAM
;
1865 if ((mixer
= open(WOutDev
[wDevID
].ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) < 0) {
1866 WARN("mixer device not available !\n");
1867 return MMSYSERR_NOTENABLED
;
1869 if (ioctl(mixer
, SOUND_MIXER_READ_PCM
, &volume
) == -1) {
1870 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)n", WOutDev
[wDevID
].ossdev
->mixer_name
, strerror(errno
));
1871 return MMSYSERR_NOTENABLED
;
1874 left
= LOBYTE(volume
);
1875 right
= HIBYTE(volume
);
1876 TRACE("left=%ld right=%ld !\n", left
, right
);
1877 *lpdwVol
= ((left
* 0xFFFFl
) / 100) + (((right
* 0xFFFFl
) / 100) << 16);
1878 return MMSYSERR_NOERROR
;
1881 /**************************************************************************
1882 * wodSetVolume [internal]
1884 static DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
1890 TRACE("(%u, %08lX);\n", wDevID
, dwParam
);
1892 left
= (LOWORD(dwParam
) * 100) / 0xFFFFl
;
1893 right
= (HIWORD(dwParam
) * 100) / 0xFFFFl
;
1894 volume
= left
+ (right
<< 8);
1896 if (wDevID
>= numOutDev
) {
1897 WARN("invalid parameter: wDevID > %d\n", numOutDev
);
1898 return MMSYSERR_INVALPARAM
;
1901 if ((mixer
= open(WOutDev
[wDevID
].ossdev
->mixer_name
, O_WRONLY
|O_NDELAY
)) < 0) {
1902 WARN("mixer device not available !\n");
1903 return MMSYSERR_NOTENABLED
;
1905 if (ioctl(mixer
, SOUND_MIXER_WRITE_PCM
, &volume
) == -1) {
1906 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n", WOutDev
[wDevID
].ossdev
->mixer_name
, strerror(errno
));
1907 return MMSYSERR_NOTENABLED
;
1909 TRACE("volume=%04x\n", (unsigned)volume
);
1912 return MMSYSERR_NOERROR
;
1915 /**************************************************************************
1916 * wodMessage (WINEOSS.7)
1918 DWORD WINAPI
OSS_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1919 DWORD dwParam1
, DWORD dwParam2
)
1921 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1922 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1929 /* FIXME: Pretend this is supported */
1931 case WODM_OPEN
: return wodOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
1932 case WODM_CLOSE
: return wodClose (wDevID
);
1933 case WODM_WRITE
: return wodWrite (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1934 case WODM_PAUSE
: return wodPause (wDevID
);
1935 case WODM_GETPOS
: return wodGetPosition (wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
1936 case WODM_BREAKLOOP
: return wodBreakLoop (wDevID
);
1937 case WODM_PREPARE
: return wodPrepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1938 case WODM_UNPREPARE
: return wodUnprepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1939 case WODM_GETDEVCAPS
: return wodGetDevCaps (wDevID
, (LPWAVEOUTCAPSA
)dwParam1
, dwParam2
);
1940 case WODM_GETNUMDEVS
: return numOutDev
;
1941 case WODM_GETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1942 case WODM_SETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1943 case WODM_GETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1944 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1945 case WODM_GETVOLUME
: return wodGetVolume (wDevID
, (LPDWORD
)dwParam1
);
1946 case WODM_SETVOLUME
: return wodSetVolume (wDevID
, dwParam1
);
1947 case WODM_RESTART
: return wodRestart (wDevID
);
1948 case WODM_RESET
: return wodReset (wDevID
);
1950 case DRV_QUERYDSOUNDIFACE
: return wodDsCreate (wDevID
, (PIDSDRIVER
*)dwParam1
);
1951 case DRV_QUERYDSOUNDDESC
: return wodDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
1952 case DRV_QUERYDSOUNDGUID
: return wodDsGuid (wDevID
, (LPGUID
)dwParam1
);
1954 FIXME("unknown message %d!\n", wMsg
);
1956 return MMSYSERR_NOTSUPPORTED
;
1959 /*======================================================================*
1960 * Low level DSOUND implementation *
1961 *======================================================================*/
1963 typedef struct IDsDriverImpl IDsDriverImpl
;
1964 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
1966 struct IDsDriverImpl
1968 /* IUnknown fields */
1969 ICOM_VFIELD(IDsDriver
);
1971 /* IDsDriverImpl fields */
1973 IDsDriverBufferImpl
*primary
;
1976 struct IDsDriverBufferImpl
1978 /* IUnknown fields */
1979 ICOM_VFIELD(IDsDriverBuffer
);
1981 /* IDsDriverBufferImpl fields */
1987 static HRESULT
DSDB_MapPrimary(IDsDriverBufferImpl
*dsdb
)
1989 WINE_WAVEOUT
*wwo
= &(WOutDev
[dsdb
->drv
->wDevID
]);
1990 TRACE("(%p)\n",dsdb
);
1991 if (!wwo
->mapping
) {
1992 wwo
->mapping
= mmap(NULL
, wwo
->maplen
, PROT_WRITE
, MAP_SHARED
,
1993 wwo
->ossdev
->fd
, 0);
1994 if (wwo
->mapping
== (LPBYTE
)-1) {
1995 TRACE("(%p): Could not map sound device for direct access (%s)\n", dsdb
, strerror(errno
));
1996 return DSERR_GENERIC
;
1998 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb
, wwo
->mapping
, wwo
->maplen
);
2000 /* for some reason, es1371 and sblive! sometimes have junk in here.
2001 * clear it, or we get junk noise */
2002 /* some libc implementations are buggy: their memset reads from the buffer...
2003 * to work around it, we have to zero the block by hand. We don't do the expected:
2004 * memset(wwo->mapping,0, wwo->maplen);
2007 unsigned char* p1
= wwo
->mapping
;
2008 unsigned len
= wwo
->maplen
;
2009 unsigned char silence
= (dsdb
->wfx
.wBitsPerSample
== 8) ? 128 : 0;
2010 unsigned long ulsilence
= (dsdb
->wfx
.wBitsPerSample
== 8) ? 0x80808080 : 0;
2012 if (len
>= 16) /* so we can have at least a 4 long area to store... */
2014 /* the mmap:ed value is (at least) dword aligned
2015 * so, start filling the complete unsigned long:s
2018 unsigned long* p4
= (unsigned long*)p1
;
2020 while (b
--) *p4
++ = ulsilence
;
2021 /* prepare for filling the rest */
2023 p1
= (unsigned char*)p4
;
2025 /* in all cases, fill the remaining bytes */
2026 while (len
-- != 0) *p1
++ = silence
;
2032 static HRESULT
DSDB_UnmapPrimary(IDsDriverBufferImpl
*dsdb
)
2034 WINE_WAVEOUT
*wwo
= &(WOutDev
[dsdb
->drv
->wDevID
]);
2035 TRACE("(%p)\n",dsdb
);
2037 if (munmap(wwo
->mapping
, wwo
->maplen
) < 0) {
2038 ERR("(%p): Could not unmap sound device (%s)\n", dsdb
, strerror(errno
));
2039 return DSERR_GENERIC
;
2041 wwo
->mapping
= NULL
;
2042 TRACE("(%p): sound device unmapped\n", dsdb
);
2047 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
2049 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2050 TRACE("(%p,%s,%p)\n",iface
,debugstr_guid(riid
),*ppobj
);
2052 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
2053 IsEqualGUID(riid
, &IID_IDsDriverBuffer
) ) {
2054 IDsDriverBuffer_AddRef(iface
);
2055 *ppobj
= (LPVOID
)This
;
2059 if ( IsEqualGUID( &IID_IDsDriverNotify
, riid
) ) {
2060 FIXME("IDsDriverNotify not implemented\n");
2062 return E_NOINTERFACE
;
2065 if ( IsEqualGUID( &IID_IDsDriverPropertySet
, riid
) ) {
2066 FIXME("IDsDriverPropertySet not implemented\n");
2068 return E_NOINTERFACE
;
2071 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
2075 return E_NOINTERFACE
;
2078 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
2080 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2081 TRACE("(%p)\n",This
);
2083 TRACE("ref=%ld\n",This
->ref
);
2087 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
2089 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2090 TRACE("(%p)\n",This
);
2092 TRACE("ref=%ld\n",This
->ref
);
2095 if (This
== This
->drv
->primary
)
2096 This
->drv
->primary
= NULL
;
2097 DSDB_UnmapPrimary(This
);
2098 HeapFree(GetProcessHeap(),0,This
);
2103 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
2104 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
2105 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
2106 DWORD dwWritePosition
,DWORD dwWriteLen
,
2109 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2110 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
2111 * and that we don't support secondary buffers, this method will never be called */
2112 TRACE("(%p): stub\n",iface
);
2113 return DSERR_UNSUPPORTED
;
2116 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
2117 LPVOID pvAudio1
,DWORD dwLen1
,
2118 LPVOID pvAudio2
,DWORD dwLen2
)
2120 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2121 TRACE("(%p): stub\n",iface
);
2122 return DSERR_UNSUPPORTED
;
2125 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
,
2126 LPWAVEFORMATEX pwfx
)
2128 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2130 TRACE("(%p,%p)\n",iface
,pwfx
);
2131 /* On our request (GetDriverDesc flags), DirectSound has by now used
2132 * waveOutClose/waveOutOpen to set the format...
2133 * unfortunately, this means our mmap() is now gone...
2134 * so we need to somehow signal to our DirectSound implementation
2135 * that it should completely recreate this HW buffer...
2136 * this unexpected error code should do the trick... */
2137 return DSERR_BUFFERLOST
;
2140 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
2142 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2143 TRACE("(%p,%ld): stub\n",iface
,dwFreq
);
2144 return DSERR_UNSUPPORTED
;
2147 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
2150 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2151 TRACE("(%p,%p)\n",This
,pVolPan
);
2153 vol
= pVolPan
->dwTotalLeftAmpFactor
| (pVolPan
->dwTotalRightAmpFactor
<< 16);
2155 if (wodSetVolume(This
->drv
->wDevID
, vol
) != MMSYSERR_NOERROR
) {
2156 WARN("wodSetVolume failed\n");
2157 return DSERR_INVALIDPARAM
;
2163 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
2165 /* ICOM_THIS(IDsDriverImpl,iface); */
2166 TRACE("(%p,%ld): stub\n",iface
,dwNewPos
);
2167 return DSERR_UNSUPPORTED
;
2170 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
2171 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
2173 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2177 TRACE("(%p)\n",iface
);
2178 if (WOutDev
[This
->drv
->wDevID
].state
== WINE_WS_CLOSED
) {
2179 ERR("device not open, but accessing?\n");
2180 return DSERR_UNINITIALIZED
;
2182 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
2183 ERR("ioctl(%s, SNDCTL_DSP_GETOPTR) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2184 return DSERR_GENERIC
;
2186 ptr
= info
.ptr
& ~3; /* align the pointer, just in case */
2187 if (lpdwPlay
) *lpdwPlay
= ptr
;
2189 /* add some safety margin (not strictly necessary, but...) */
2190 if (WOutDev
[This
->drv
->wDevID
].ossdev
->out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
2191 *lpdwWrite
= ptr
+ 32;
2193 *lpdwWrite
= ptr
+ WOutDev
[This
->drv
->wDevID
].dwFragmentSize
;
2194 while (*lpdwWrite
> This
->buflen
)
2195 *lpdwWrite
-= This
->buflen
;
2197 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay
?*lpdwPlay
:0, lpdwWrite
?*lpdwWrite
:0);
2201 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
2203 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2205 TRACE("(%p,%lx,%lx,%lx)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
2206 WOutDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= TRUE
;
2207 enable
= getEnables(WOutDev
[This
->drv
->wDevID
].ossdev
);
2208 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2209 if (errno
== EINVAL
) {
2210 /* Don't give up yet. OSS trigger support is inconsistent. */
2211 if (WOutDev
[This
->drv
->wDevID
].ossdev
->open_count
== 1) {
2212 /* try the opposite input enable */
2213 if (WOutDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
== FALSE
)
2214 WOutDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= TRUE
;
2216 WOutDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= FALSE
;
2218 enable
= getEnables(WOutDev
[This
->drv
->wDevID
].ossdev
);
2219 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) >= 0)
2223 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2224 WOutDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
2225 return DSERR_GENERIC
;
2230 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
2232 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2234 TRACE("(%p)\n",iface
);
2235 /* no more playing */
2236 WOutDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
2237 enable
= getEnables(WOutDev
[This
->drv
->wDevID
].ossdev
);
2238 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2239 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2240 return DSERR_GENERIC
;
2243 /* the play position must be reset to the beginning of the buffer */
2244 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_RESET
, 0) < 0) {
2245 ERR("ioctl(%s, SNDCTL_DSP_RESET) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2246 return DSERR_GENERIC
;
2249 /* Most OSS drivers just can't stop the playback without closing the device...
2250 * so we need to somehow signal to our DirectSound implementation
2251 * that it should completely recreate this HW buffer...
2252 * this unexpected error code should do the trick... */
2253 /* FIXME: ...unless we are doing full duplex, then its not nice to close the device */
2254 if (WOutDev
[This
->drv
->wDevID
].ossdev
->open_count
== 1)
2255 return DSERR_BUFFERLOST
;
2260 static ICOM_VTABLE(IDsDriverBuffer
) dsdbvt
=
2262 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2263 IDsDriverBufferImpl_QueryInterface
,
2264 IDsDriverBufferImpl_AddRef
,
2265 IDsDriverBufferImpl_Release
,
2266 IDsDriverBufferImpl_Lock
,
2267 IDsDriverBufferImpl_Unlock
,
2268 IDsDriverBufferImpl_SetFormat
,
2269 IDsDriverBufferImpl_SetFrequency
,
2270 IDsDriverBufferImpl_SetVolumePan
,
2271 IDsDriverBufferImpl_SetPosition
,
2272 IDsDriverBufferImpl_GetPosition
,
2273 IDsDriverBufferImpl_Play
,
2274 IDsDriverBufferImpl_Stop
2277 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
2279 ICOM_THIS(IDsDriverImpl
,iface
);
2280 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
2282 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
2283 IsEqualGUID(riid
, &IID_IDsDriver
) ) {
2284 IDsDriver_AddRef(iface
);
2285 *ppobj
= (LPVOID
)This
;
2289 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
2293 return E_NOINTERFACE
;
2296 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
2298 ICOM_THIS(IDsDriverImpl
,iface
);
2299 TRACE("(%p)\n",This
);
2301 TRACE("ref=%ld\n",This
->ref
);
2305 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
2307 ICOM_THIS(IDsDriverImpl
,iface
);
2308 TRACE("(%p)\n",This
);
2310 TRACE("ref=%ld\n",This
->ref
);
2313 HeapFree(GetProcessHeap(),0,This
);
2318 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
, PDSDRIVERDESC pDesc
)
2320 ICOM_THIS(IDsDriverImpl
,iface
);
2321 TRACE("(%p,%p)\n",iface
,pDesc
);
2323 /* copy version from driver */
2324 memcpy(pDesc
, &(WOutDev
[This
->wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
2326 pDesc
->dwFlags
|= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
|
2327 DSDDESC_USESYSTEMMEMORY
| DSDDESC_DONTNEEDPRIMARYLOCK
;
2328 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
2330 pDesc
->wReserved
= 0;
2331 pDesc
->ulDeviceNum
= This
->wDevID
;
2332 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
2333 pDesc
->pvDirectDrawHeap
= NULL
;
2334 pDesc
->dwMemStartAddress
= 0;
2335 pDesc
->dwMemEndAddress
= 0;
2336 pDesc
->dwMemAllocExtra
= 0;
2337 pDesc
->pvReserved1
= NULL
;
2338 pDesc
->pvReserved2
= NULL
;
2342 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
2344 ICOM_THIS(IDsDriverImpl
,iface
);
2346 TRACE("(%p)\n",iface
);
2348 /* make sure the card doesn't start playing before we want it to */
2349 WOutDev
[This
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
2350 enable
= getEnables(WOutDev
[This
->wDevID
].ossdev
);
2351 if (ioctl(WOutDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2352 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2353 return DSERR_GENERIC
;
2358 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
2360 ICOM_THIS(IDsDriverImpl
,iface
);
2361 TRACE("(%p)\n",iface
);
2362 if (This
->primary
) {
2363 ERR("problem with DirectSound: primary not released\n");
2364 return DSERR_GENERIC
;
2369 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
2371 ICOM_THIS(IDsDriverImpl
,iface
);
2372 TRACE("(%p,%p)\n",iface
,pCaps
);
2373 memcpy(pCaps
, &(WOutDev
[This
->wDevID
].ossdev
->ds_caps
), sizeof(DSDRIVERCAPS
));
2377 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
2378 LPWAVEFORMATEX pwfx
,
2380 DWORD dwCardAddress
,
2381 LPDWORD pdwcbBufferSize
,
2385 ICOM_THIS(IDsDriverImpl
,iface
);
2386 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
2388 audio_buf_info info
;
2390 TRACE("(%p,%p,%lx,%lx)\n",iface
,pwfx
,dwFlags
,dwCardAddress
);
2392 /* we only support primary buffers */
2393 if (!(dwFlags
& DSBCAPS_PRIMARYBUFFER
))
2394 return DSERR_UNSUPPORTED
;
2396 return DSERR_ALLOCATED
;
2397 if (dwFlags
& (DSBCAPS_CTRLFREQUENCY
| DSBCAPS_CTRLPAN
))
2398 return DSERR_CONTROLUNAVAIL
;
2400 *ippdsdb
= (IDsDriverBufferImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsDriverBufferImpl
));
2401 if (*ippdsdb
== NULL
)
2402 return DSERR_OUTOFMEMORY
;
2403 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
2404 (*ippdsdb
)->ref
= 1;
2405 (*ippdsdb
)->drv
= This
;
2406 (*ippdsdb
)->wfx
= *pwfx
;
2408 /* check how big the DMA buffer is now */
2409 if (ioctl(WOutDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
2410 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", WOutDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2411 HeapFree(GetProcessHeap(),0,*ippdsdb
);
2413 return DSERR_GENERIC
;
2415 WOutDev
[This
->wDevID
].maplen
= (*ippdsdb
)->buflen
= info
.fragstotal
* info
.fragsize
;
2417 /* map the DMA buffer */
2418 err
= DSDB_MapPrimary(*ippdsdb
);
2420 HeapFree(GetProcessHeap(),0,*ippdsdb
);
2425 /* primary buffer is ready to go */
2426 *pdwcbBufferSize
= WOutDev
[This
->wDevID
].maplen
;
2427 *ppbBuffer
= WOutDev
[This
->wDevID
].mapping
;
2429 /* some drivers need some extra nudging after mapping */
2430 WOutDev
[This
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
2431 enable
= getEnables(WOutDev
[This
->wDevID
].ossdev
);
2432 if (ioctl(WOutDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2433 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2434 return DSERR_GENERIC
;
2437 This
->primary
= *ippdsdb
;
2442 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
2443 PIDSDRIVERBUFFER pBuffer
,
2446 /* ICOM_THIS(IDsDriverImpl,iface); */
2447 TRACE("(%p,%p): stub\n",iface
,pBuffer
);
2448 return DSERR_INVALIDCALL
;
2451 static ICOM_VTABLE(IDsDriver
) dsdvt
=
2453 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2454 IDsDriverImpl_QueryInterface
,
2455 IDsDriverImpl_AddRef
,
2456 IDsDriverImpl_Release
,
2457 IDsDriverImpl_GetDriverDesc
,
2459 IDsDriverImpl_Close
,
2460 IDsDriverImpl_GetCaps
,
2461 IDsDriverImpl_CreateSoundBuffer
,
2462 IDsDriverImpl_DuplicateSoundBuffer
2465 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
2467 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
2468 TRACE("(%d,%p)\n",wDevID
,drv
);
2470 /* the HAL isn't much better than the HEL if we can't do mmap() */
2471 if (!(WOutDev
[wDevID
].ossdev
->out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
2472 ERR("DirectSound flag not set\n");
2473 MESSAGE("This sound card's driver does not support direct access\n");
2474 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2475 return MMSYSERR_NOTSUPPORTED
;
2478 *idrv
= (IDsDriverImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsDriverImpl
));
2480 return MMSYSERR_NOMEM
;
2481 (*idrv
)->lpVtbl
= &dsdvt
;
2484 (*idrv
)->wDevID
= wDevID
;
2485 (*idrv
)->primary
= NULL
;
2486 return MMSYSERR_NOERROR
;
2489 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
2491 TRACE("(%d,%p)\n",wDevID
,desc
);
2492 memcpy(desc
, &(WOutDev
[wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
2493 return MMSYSERR_NOERROR
;
2496 static DWORD
wodDsGuid(UINT wDevID
, LPGUID pGuid
)
2498 TRACE("(%d,%p)\n",wDevID
,pGuid
);
2499 memcpy(pGuid
, &(WOutDev
[wDevID
].ossdev
->ds_guid
), sizeof(GUID
));
2500 return MMSYSERR_NOERROR
;
2503 /*======================================================================*
2504 * Low level WAVE IN implementation *
2505 *======================================================================*/
2507 /**************************************************************************
2508 * widNotifyClient [internal]
2510 static DWORD
widNotifyClient(WINE_WAVEIN
* wwi
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
2512 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg
, dwParam1
, dwParam2
);
2518 if (wwi
->wFlags
!= DCB_NULL
&&
2519 !DriverCallback(wwi
->waveDesc
.dwCallback
, wwi
->wFlags
,
2520 (HDRVR
)wwi
->waveDesc
.hWave
, wMsg
,
2521 wwi
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
2522 WARN("can't notify client !\n");
2523 return MMSYSERR_ERROR
;
2527 FIXME("Unknown callback message %u\n", wMsg
);
2528 return MMSYSERR_INVALPARAM
;
2530 return MMSYSERR_NOERROR
;
2533 /**************************************************************************
2534 * widGetDevCaps [internal]
2536 static DWORD
widGetDevCaps(WORD wDevID
, LPWAVEINCAPSA lpCaps
, DWORD dwSize
)
2538 TRACE("(%u, %p, %lu);\n", wDevID
, lpCaps
, dwSize
);
2540 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
2542 if (wDevID
>= numInDev
) {
2543 TRACE("numOutDev reached !\n");
2544 return MMSYSERR_BADDEVICEID
;
2547 memcpy(lpCaps
, &WInDev
[wDevID
].ossdev
->in_caps
, min(dwSize
, sizeof(*lpCaps
)));
2548 return MMSYSERR_NOERROR
;
2551 /**************************************************************************
2552 * widRecorder [internal]
2554 static DWORD CALLBACK
widRecorder(LPVOID pmt
)
2556 WORD uDevID
= (DWORD
)pmt
;
2557 WINE_WAVEIN
* wwi
= (WINE_WAVEIN
*)&WInDev
[uDevID
];
2561 LPVOID buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, wwi
->dwFragmentSize
);
2562 char *pOffset
= buffer
;
2563 audio_buf_info info
;
2565 enum win_wm_message msg
;
2570 wwi
->state
= WINE_WS_STOPPED
;
2571 wwi
->dwTotalRecorded
= 0;
2572 wwi
->lpQueuePtr
= NULL
;
2574 SetEvent(wwi
->hStartUpEvent
);
2576 /* disable input so capture will begin when triggered */
2577 wwi
->ossdev
->bInputEnabled
= FALSE
;
2578 enable
= getEnables(wwi
->ossdev
);
2579 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0)
2580 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
->dev_name
, strerror(errno
));
2582 /* the soundblaster live needs a micro wake to get its recording started
2583 * (or GETISPACE will have 0 frags all the time)
2585 read(wwi
->ossdev
->fd
, &xs
, 4);
2587 /* make sleep time to be # of ms to output a fragment */
2588 dwSleepTime
= (wwi
->dwFragmentSize
* 1000) / wwi
->format
.wf
.nAvgBytesPerSec
;
2589 TRACE("sleeptime=%ld ms\n", dwSleepTime
);
2592 /* wait for dwSleepTime or an event in thread's queue */
2593 /* FIXME: could improve wait time depending on queue state,
2594 * ie, number of queued fragments
2597 if (wwi
->lpQueuePtr
!= NULL
&& wwi
->state
== WINE_WS_PLAYING
)
2599 lpWaveHdr
= wwi
->lpQueuePtr
;
2601 ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_GETISPACE
, &info
);
2602 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info
.fragments
, info
.fragsize
, info
.fragstotal
, info
.bytes
);
2604 /* read all the fragments accumulated so far */
2605 while ((info
.fragments
> 0) && (wwi
->lpQueuePtr
))
2609 if (lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
>= wwi
->dwFragmentSize
)
2611 /* directly read fragment in wavehdr */
2612 bytesRead
= read(wwi
->ossdev
->fd
,
2613 lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2614 wwi
->dwFragmentSize
);
2616 TRACE("bytesRead=%ld (direct)\n", bytesRead
);
2617 if (bytesRead
!= (DWORD
) -1)
2619 /* update number of bytes recorded in current buffer and by this device */
2620 lpWaveHdr
->dwBytesRecorded
+= bytesRead
;
2621 wwi
->dwTotalRecorded
+= bytesRead
;
2623 /* buffer is full. notify client */
2624 if (lpWaveHdr
->dwBytesRecorded
== lpWaveHdr
->dwBufferLength
)
2626 /* must copy the value of next waveHdr, because we have no idea of what
2627 * will be done with the content of lpWaveHdr in callback
2629 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2631 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2632 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2634 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2635 lpWaveHdr
= wwi
->lpQueuePtr
= lpNext
;
2641 /* read the fragment in a local buffer */
2642 bytesRead
= read(wwi
->ossdev
->fd
, buffer
, wwi
->dwFragmentSize
);
2645 TRACE("bytesRead=%ld (local)\n", bytesRead
);
2647 /* copy data in client buffers */
2648 while (bytesRead
!= (DWORD
) -1 && bytesRead
> 0)
2650 DWORD dwToCopy
= min (bytesRead
, lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
);
2652 memcpy(lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2656 /* update number of bytes recorded in current buffer and by this device */
2657 lpWaveHdr
->dwBytesRecorded
+= dwToCopy
;
2658 wwi
->dwTotalRecorded
+= dwToCopy
;
2659 bytesRead
-= dwToCopy
;
2660 pOffset
+= dwToCopy
;
2662 /* client buffer is full. notify client */
2663 if (lpWaveHdr
->dwBytesRecorded
== lpWaveHdr
->dwBufferLength
)
2665 /* must copy the value of next waveHdr, because we have no idea of what
2666 * will be done with the content of lpWaveHdr in callback
2668 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2669 TRACE("lpNext=%p\n", lpNext
);
2671 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2672 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2674 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2676 wwi
->lpQueuePtr
= lpWaveHdr
= lpNext
;
2677 if (!lpNext
&& bytesRead
) {
2678 /* before we give up, check for more header messages */
2679 while (OSS_PeekRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
))
2681 if (msg
== WINE_WM_HEADER
) {
2683 OSS_RetrieveRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
);
2684 hdr
= ((LPWAVEHDR
)param
);
2685 TRACE("msg = %s, hdr = %p, ev = %p\n", wodPlayerCmdString
[msg
- WM_USER
- 1], hdr
, ev
);
2687 if (lpWaveHdr
== 0) {
2688 /* new head of queue */
2689 wwi
->lpQueuePtr
= lpWaveHdr
= hdr
;
2691 /* insert buffer at the end of queue */
2693 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2700 if (lpWaveHdr
== 0) {
2701 /* no more buffer to copy data to, but we did read more.
2702 * what hasn't been copied will be dropped
2704 WARN("buffer under run! %lu bytes dropped.\n", bytesRead
);
2705 wwi
->lpQueuePtr
= NULL
;
2715 WAIT_OMR(&wwi
->msgRing
, dwSleepTime
);
2717 while (OSS_RetrieveRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
))
2719 TRACE("msg=%s param=0x%lx\n", wodPlayerCmdString
[msg
- WM_USER
- 1], param
);
2721 case WINE_WM_PAUSING
:
2722 wwi
->state
= WINE_WS_PAUSED
;
2723 /*FIXME("Device should stop recording\n");*/
2726 case WINE_WM_STARTING
:
2727 wwi
->state
= WINE_WS_PLAYING
;
2729 if (wwi
->ossdev
->bTriggerSupport
)
2731 /* start the recording */
2732 wwi
->ossdev
->bInputEnabled
= TRUE
;
2733 enable
= getEnables(wwi
->ossdev
);
2734 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2735 wwi
->ossdev
->bInputEnabled
= FALSE
;
2736 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
->dev_name
, strerror(errno
));
2741 unsigned char data
[4];
2742 /* read 4 bytes to start the recording */
2743 read(wwi
->ossdev
->fd
, data
, 4);
2748 case WINE_WM_HEADER
:
2749 lpWaveHdr
= (LPWAVEHDR
)param
;
2750 lpWaveHdr
->lpNext
= 0;
2752 /* insert buffer at the end of queue */
2755 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2759 case WINE_WM_STOPPING
:
2760 case WINE_WM_RESETTING
:
2761 wwi
->state
= WINE_WS_STOPPED
;
2762 wwi
->dwTotalRecorded
= 0;
2763 /* return all buffers to the app */
2764 for (lpWaveHdr
= wwi
->lpQueuePtr
; lpWaveHdr
; lpWaveHdr
= lpWaveHdr
->lpNext
) {
2765 TRACE("reset %p %p\n", lpWaveHdr
, lpWaveHdr
->lpNext
);
2766 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2767 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2769 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2771 wwi
->lpQueuePtr
= NULL
;
2774 case WINE_WM_CLOSING
:
2776 wwi
->state
= WINE_WS_CLOSED
;
2778 HeapFree(GetProcessHeap(), 0, buffer
);
2780 /* shouldn't go here */
2782 FIXME("unknown message %d\n", msg
);
2788 /* just for not generating compilation warnings... should never be executed */
2793 /**************************************************************************
2794 * widOpen [internal]
2796 static DWORD
widOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
2803 TRACE("(%u, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
2804 if (lpDesc
== NULL
) {
2805 WARN("Invalid Parameter !\n");
2806 return MMSYSERR_INVALPARAM
;
2808 if (wDevID
>= numInDev
) return MMSYSERR_BADDEVICEID
;
2810 /* only PCM format is supported so far... */
2811 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
2812 lpDesc
->lpFormat
->nChannels
== 0 ||
2813 lpDesc
->lpFormat
->nSamplesPerSec
== 0) {
2814 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2815 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
2816 lpDesc
->lpFormat
->nSamplesPerSec
);
2817 return WAVERR_BADFORMAT
;
2820 if (dwFlags
& WAVE_FORMAT_QUERY
) {
2821 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2822 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
2823 lpDesc
->lpFormat
->nSamplesPerSec
);
2824 return MMSYSERR_NOERROR
;
2827 wwi
= &WInDev
[wDevID
];
2829 if (wwi
->state
!= WINE_WS_CLOSED
) return MMSYSERR_ALLOCATED
;
2831 if ((dwFlags
& WAVE_DIRECTSOUND
) &&
2832 !(wwi
->ossdev
->in_caps_support
& WAVECAPS_DIRECTSOUND
))
2833 /* not supported, ignore it */
2834 dwFlags
&= ~WAVE_DIRECTSOUND
;
2836 if (dwFlags
& WAVE_DIRECTSOUND
) {
2837 TRACE("has DirectSoundCapture driver\n");
2838 if (wwi
->ossdev
->in_caps_support
& WAVECAPS_SAMPLEACCURATE
)
2839 /* we have realtime DirectSound, fragments just waste our time,
2840 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2841 audio_fragment
= 0x0020000B;
2843 /* to approximate realtime, we must use small fragments,
2844 * let's try to fragment the above 64KB (256 * 2^8) */
2845 audio_fragment
= 0x01000008;
2847 TRACE("doesn't have DirectSoundCapture driver\n");
2848 if (wwi
->ossdev
->open_count
> 0) {
2849 TRACE("Using output device audio_fragment\n");
2850 /* FIXME: This may not be optimal for capture but it allows us
2851 * to do hardware playback without hardware capture. */
2852 audio_fragment
= wwi
->ossdev
->audio_fragment
;
2854 /* This is actually hand tuned to work so that my SB Live:
2856 * - does not buffer too much
2857 * when sending with the Shoutcast winamp plugin
2859 /* 15 fragments max, 2^10 = 1024 bytes per fragment */
2860 audio_fragment
= 0x000F000A;
2864 TRACE("using %d %d byte fragments\n", audio_fragment
>> 16,
2865 1 << (audio_fragment
& 0xffff));
2867 ret
= OSS_OpenDevice(wwi
->ossdev
, O_RDONLY
, &audio_fragment
,
2869 lpDesc
->lpFormat
->nSamplesPerSec
,
2870 (lpDesc
->lpFormat
->nChannels
> 1) ? 1 : 0,
2871 (lpDesc
->lpFormat
->wBitsPerSample
== 16)
2872 ? AFMT_S16_LE
: AFMT_U8
);
2873 if (ret
!= 0) return ret
;
2874 wwi
->state
= WINE_WS_STOPPED
;
2876 if (wwi
->lpQueuePtr
) {
2877 WARN("Should have an empty queue (%p)\n", wwi
->lpQueuePtr
);
2878 wwi
->lpQueuePtr
= NULL
;
2880 wwi
->dwTotalRecorded
= 0;
2881 wwi
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2883 memcpy(&wwi
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
2884 memcpy(&wwi
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
2886 if (wwi
->format
.wBitsPerSample
== 0) {
2887 WARN("Resetting zeroed wBitsPerSample\n");
2888 wwi
->format
.wBitsPerSample
= 8 *
2889 (wwi
->format
.wf
.nAvgBytesPerSec
/
2890 wwi
->format
.wf
.nSamplesPerSec
) /
2891 wwi
->format
.wf
.nChannels
;
2894 ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_GETBLKSIZE
, &fragment_size
);
2895 if (fragment_size
== -1) {
2896 WARN("ioctl(%s, SNDCTL_DSP_GETBLKSIZE) failed (%s)\n",
2897 wwi
->ossdev
->dev_name
, strerror(errno
));
2898 OSS_CloseDevice(wwi
->ossdev
);
2899 wwi
->state
= WINE_WS_CLOSED
;
2900 return MMSYSERR_NOTENABLED
;
2902 wwi
->dwFragmentSize
= fragment_size
;
2904 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2905 wwi
->format
.wBitsPerSample
, wwi
->format
.wf
.nAvgBytesPerSec
,
2906 wwi
->format
.wf
.nSamplesPerSec
, wwi
->format
.wf
.nChannels
,
2907 wwi
->format
.wf
.nBlockAlign
);
2909 OSS_InitRingMessage(&wwi
->msgRing
);
2911 wwi
->hStartUpEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
2912 wwi
->hThread
= CreateThread(NULL
, 0, widRecorder
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwi
->dwThreadID
));
2913 WaitForSingleObject(wwi
->hStartUpEvent
, INFINITE
);
2914 CloseHandle(wwi
->hStartUpEvent
);
2915 wwi
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
2917 return widNotifyClient(wwi
, WIM_OPEN
, 0L, 0L);
2920 /**************************************************************************
2921 * widClose [internal]
2923 static DWORD
widClose(WORD wDevID
)
2927 TRACE("(%u);\n", wDevID
);
2928 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2929 WARN("can't close !\n");
2930 return MMSYSERR_INVALHANDLE
;
2933 wwi
= &WInDev
[wDevID
];
2935 if (wwi
->lpQueuePtr
!= NULL
) {
2936 WARN("still buffers open !\n");
2937 return WAVERR_STILLPLAYING
;
2940 OSS_AddRingMessage(&wwi
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
2941 OSS_CloseDevice(wwi
->ossdev
);
2942 wwi
->state
= WINE_WS_CLOSED
;
2943 wwi
->dwFragmentSize
= 0;
2944 OSS_DestroyRingMessage(&wwi
->msgRing
);
2945 return widNotifyClient(wwi
, WIM_CLOSE
, 0L, 0L);
2948 /**************************************************************************
2949 * widAddBuffer [internal]
2951 static DWORD
widAddBuffer(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
2953 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
2955 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2956 WARN("can't do it !\n");
2957 return MMSYSERR_INVALHANDLE
;
2959 if (!(lpWaveHdr
->dwFlags
& WHDR_PREPARED
)) {
2960 TRACE("never been prepared !\n");
2961 return WAVERR_UNPREPARED
;
2963 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
) {
2964 TRACE("header already in use !\n");
2965 return WAVERR_STILLPLAYING
;
2968 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
2969 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
2970 lpWaveHdr
->dwBytesRecorded
= 0;
2971 lpWaveHdr
->lpNext
= NULL
;
2973 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
2974 return MMSYSERR_NOERROR
;
2977 /**************************************************************************
2978 * widPrepare [internal]
2980 static DWORD
widPrepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
2982 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
2984 if (wDevID
>= numInDev
) return MMSYSERR_INVALHANDLE
;
2986 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
2987 return WAVERR_STILLPLAYING
;
2989 lpWaveHdr
->dwFlags
|= WHDR_PREPARED
;
2990 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
2991 lpWaveHdr
->dwBytesRecorded
= 0;
2992 TRACE("header prepared !\n");
2993 return MMSYSERR_NOERROR
;
2996 /**************************************************************************
2997 * widUnprepare [internal]
2999 static DWORD
widUnprepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
3001 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
3002 if (wDevID
>= numInDev
) return MMSYSERR_INVALHANDLE
;
3004 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
3005 return WAVERR_STILLPLAYING
;
3007 lpWaveHdr
->dwFlags
&= ~WHDR_PREPARED
;
3008 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
3010 return MMSYSERR_NOERROR
;
3013 /**************************************************************************
3014 * widStart [internal]
3016 static DWORD
widStart(WORD wDevID
)
3018 TRACE("(%u);\n", wDevID
);
3019 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3020 WARN("can't start recording !\n");
3021 return MMSYSERR_INVALHANDLE
;
3024 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STARTING
, 0, TRUE
);
3025 return MMSYSERR_NOERROR
;
3028 /**************************************************************************
3029 * widStop [internal]
3031 static DWORD
widStop(WORD wDevID
)
3033 TRACE("(%u);\n", wDevID
);
3034 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3035 WARN("can't stop !\n");
3036 return MMSYSERR_INVALHANDLE
;
3039 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STOPPING
, 0, TRUE
);
3041 return MMSYSERR_NOERROR
;
3044 /**************************************************************************
3045 * widReset [internal]
3047 static DWORD
widReset(WORD wDevID
)
3049 TRACE("(%u);\n", wDevID
);
3050 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3051 WARN("can't reset !\n");
3052 return MMSYSERR_INVALHANDLE
;
3054 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
3055 return MMSYSERR_NOERROR
;
3058 /**************************************************************************
3059 * widGetPosition [internal]
3061 static DWORD
widGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
3066 TRACE("(%u, %p, %lu);\n", wDevID
, lpTime
, uSize
);
3068 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3069 WARN("can't get pos !\n");
3070 return MMSYSERR_INVALHANDLE
;
3072 if (lpTime
== NULL
) return MMSYSERR_INVALPARAM
;
3074 wwi
= &WInDev
[wDevID
];
3076 TRACE("wType=%04X !\n", lpTime
->wType
);
3077 TRACE("wBitsPerSample=%u\n", wwi
->format
.wBitsPerSample
);
3078 TRACE("nSamplesPerSec=%lu\n", wwi
->format
.wf
.nSamplesPerSec
);
3079 TRACE("nChannels=%u\n", wwi
->format
.wf
.nChannels
);
3080 TRACE("nAvgBytesPerSec=%lu\n", wwi
->format
.wf
.nAvgBytesPerSec
);
3081 TRACE("dwTotalRecorded=%lu\n",wwi
->dwTotalRecorded
);
3082 switch (lpTime
->wType
) {
3084 lpTime
->u
.cb
= wwi
->dwTotalRecorded
;
3085 TRACE("TIME_BYTES=%lu\n", lpTime
->u
.cb
);
3088 lpTime
->u
.sample
= wwi
->dwTotalRecorded
* 8 /
3089 wwi
->format
.wBitsPerSample
/ wwi
->format
.wf
.nChannels
;
3090 TRACE("TIME_SAMPLES=%lu\n", lpTime
->u
.sample
);
3093 time
= wwi
->dwTotalRecorded
/
3094 (wwi
->format
.wf
.nAvgBytesPerSec
/ 1000);
3095 lpTime
->u
.smpte
.hour
= time
/ (60 * 60 * 1000);
3096 time
-= lpTime
->u
.smpte
.hour
* (60 * 60 * 1000);
3097 lpTime
->u
.smpte
.min
= time
/ (60 * 1000);
3098 time
-= lpTime
->u
.smpte
.min
* (60 * 1000);
3099 lpTime
->u
.smpte
.sec
= time
/ 1000;
3100 time
-= lpTime
->u
.smpte
.sec
* 1000;
3101 lpTime
->u
.smpte
.frame
= time
* 30 / 1000;
3102 lpTime
->u
.smpte
.fps
= 30;
3103 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
3104 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
3105 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
3108 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime
->wType
);
3109 lpTime
->wType
= TIME_MS
;
3111 lpTime
->u
.ms
= wwi
->dwTotalRecorded
/
3112 (wwi
->format
.wf
.nAvgBytesPerSec
/ 1000);
3113 TRACE("TIME_MS=%lu\n", lpTime
->u
.ms
);
3116 return MMSYSERR_NOERROR
;
3119 /**************************************************************************
3120 * widMessage (WINEOSS.6)
3122 DWORD WINAPI
OSS_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3123 DWORD dwParam1
, DWORD dwParam2
)
3125 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
3126 wDevID
, 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 widPrepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
3139 case WIDM_UNPREPARE
: return widUnprepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
3140 case WIDM_GETDEVCAPS
: return widGetDevCaps (wDevID
, (LPWAVEINCAPSA
)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_QUERYDSOUNDIFACE
: return widDsCreate (wDevID
, (PIDSCDRIVER
*)dwParam1
);
3147 case DRV_QUERYDSOUNDDESC
: return widDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
3148 case DRV_QUERYDSOUNDGUID
: return widDsGuid (wDevID
, (LPGUID
)dwParam1
);
3150 FIXME("unknown message %u!\n", wMsg
);
3152 return MMSYSERR_NOTSUPPORTED
;
3155 /*======================================================================*
3156 * Low level DSOUND property set implementation *
3157 *======================================================================*/
3159 typedef struct IDsDriverPropertySetImpl IDsDriverPropertySetImpl
;
3161 struct IDsDriverPropertySetImpl
3163 /* IUnknown fields */
3164 ICOM_VFIELD(IDsDriverPropertySet
);
3168 static HRESULT WINAPI
IDsDriverPropertySetImpl_QueryInterface(
3169 PIDSDRIVERPROPERTYSET iface
,
3173 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3174 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
3176 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
3177 IsEqualGUID(riid
, &IID_IDsDriverPropertySet
) ) {
3178 IDsDriverPropertySet_AddRef(iface
);
3179 *ppobj
= (LPVOID
)This
;
3183 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
3187 return E_NOINTERFACE
;
3190 static ULONG WINAPI
IDsDriverPropertySetImpl_AddRef(PIDSDRIVERPROPERTYSET iface
)
3192 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3194 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
3196 ref
= InterlockedIncrement(&(This
->ref
));
3200 static ULONG WINAPI
IDsDriverPropertySetImpl_Release(PIDSDRIVERPROPERTYSET iface
)
3202 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3204 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
3206 ref
= InterlockedDecrement(&(This
->ref
));
3210 static HRESULT WINAPI
IDsDriverPropertySetImpl_Get(
3211 PIDSDRIVERPROPERTYSET iface
,
3212 PDSPROPERTY pDsProperty
,
3213 LPVOID pPropertyParams
,
3214 ULONG cbPropertyParams
,
3215 LPVOID pPropertyData
,
3216 ULONG cbPropertyData
,
3217 PULONG pcbReturnedData
)
3219 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3220 FIXME("(%p,%p,%p,%lx,%p,%lx,%p)\n",This
,pDsProperty
,pPropertyParams
,cbPropertyParams
,pPropertyData
,cbPropertyData
,pcbReturnedData
);
3221 return DSERR_UNSUPPORTED
;
3224 static HRESULT WINAPI
IDsDriverPropertySetImpl_Set(
3225 PIDSDRIVERPROPERTYSET iface
,
3226 PDSPROPERTY pDsProperty
,
3227 LPVOID pPropertyParams
,
3228 ULONG cbPropertyParams
,
3229 LPVOID pPropertyData
,
3230 ULONG cbPropertyData
)
3232 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3233 FIXME("(%p,%p,%p,%lx,%p,%lx)\n",This
,pDsProperty
,pPropertyParams
,cbPropertyParams
,pPropertyData
,cbPropertyData
);
3234 return DSERR_UNSUPPORTED
;
3237 static HRESULT WINAPI
IDsDriverPropertySetImpl_QuerySupport(
3238 PIDSDRIVERPROPERTYSET iface
,
3239 REFGUID PropertySetId
,
3243 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3244 FIXME("(%p,%s,%lx,%p)\n",This
,debugstr_guid(PropertySetId
),PropertyId
,pSupport
);
3245 return DSERR_UNSUPPORTED
;
3248 ICOM_VTABLE(IDsDriverPropertySet
) dsdpsvt
=
3250 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3251 IDsDriverPropertySetImpl_QueryInterface
,
3252 IDsDriverPropertySetImpl_AddRef
,
3253 IDsDriverPropertySetImpl_Release
,
3254 IDsDriverPropertySetImpl_Get
,
3255 IDsDriverPropertySetImpl_Set
,
3256 IDsDriverPropertySetImpl_QuerySupport
,
3259 /*======================================================================*
3260 * Low level DSOUND notify implementation *
3261 *======================================================================*/
3263 typedef struct IDsDriverNotifyImpl IDsDriverNotifyImpl
;
3265 struct IDsDriverNotifyImpl
3267 /* IUnknown fields */
3268 ICOM_VFIELD(IDsDriverNotify
);
3270 /* IDsDriverNotifyImpl fields */
3271 LPDSBPOSITIONNOTIFY notifies
;
3275 static HRESULT WINAPI
IDsDriverNotifyImpl_QueryInterface(
3276 PIDSDRIVERNOTIFY iface
,
3280 ICOM_THIS(IDsDriverNotifyImpl
,iface
);
3281 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
3283 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
3284 IsEqualGUID(riid
, &IID_IDsDriverNotify
) ) {
3285 IDsDriverNotify_AddRef(iface
);
3290 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
3294 return E_NOINTERFACE
;
3297 static ULONG WINAPI
IDsDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface
)
3299 ICOM_THIS(IDsDriverNotifyImpl
,iface
);
3301 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
3303 ref
= InterlockedIncrement(&(This
->ref
));
3307 static ULONG WINAPI
IDsDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface
)
3309 ICOM_THIS(IDsDriverNotifyImpl
,iface
);
3311 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
3313 ref
= InterlockedDecrement(&(This
->ref
));
3314 /* FIXME: A notification should be a part of a buffer rather than pointed
3315 * to from a buffer. Hence the -1 ref count */
3317 if (This
->notifies
!= NULL
)
3318 HeapFree(GetProcessHeap(), 0, This
->notifies
);
3320 HeapFree(GetProcessHeap(),0,This
);
3327 static HRESULT WINAPI
IDsDriverNotifyImpl_SetNotificationPositions(
3328 PIDSDRIVERNOTIFY iface
,
3330 LPCDSBPOSITIONNOTIFY notify
)
3332 ICOM_THIS(IDsDriverNotifyImpl
,iface
);
3333 TRACE("(%p,0x%08lx,%p)\n",This
,howmuch
,notify
);
3336 WARN("invalid parameter\n");
3337 return DSERR_INVALIDPARAM
;
3340 if (TRACE_ON(wave
)) {
3342 for (i
=0;i
<howmuch
;i
++)
3343 TRACE("notify at %ld to 0x%08lx\n",
3344 notify
[i
].dwOffset
,(DWORD
)notify
[i
].hEventNotify
);
3347 /* Make an internal copy of the caller-supplied array.
3348 * Replace the existing copy if one is already present. */
3350 This
->notifies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
3351 This
->notifies
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
3353 This
->notifies
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
3354 howmuch
* sizeof(DSBPOSITIONNOTIFY
));
3356 memcpy(This
->notifies
, notify
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
3357 This
->nrofnotifies
= howmuch
;
3362 ICOM_VTABLE(IDsDriverNotify
) dsdnvt
=
3364 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3365 IDsDriverNotifyImpl_QueryInterface
,
3366 IDsDriverNotifyImpl_AddRef
,
3367 IDsDriverNotifyImpl_Release
,
3368 IDsDriverNotifyImpl_SetNotificationPositions
,
3371 /*======================================================================*
3372 * Low level DSOUND capture implementation *
3373 *======================================================================*/
3375 typedef struct IDsCaptureDriverImpl IDsCaptureDriverImpl
;
3376 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl
;
3378 struct IDsCaptureDriverImpl
3380 /* IUnknown fields */
3381 ICOM_VFIELD(IDsCaptureDriver
);
3383 /* IDsCaptureDriverImpl fields */
3385 IDsCaptureDriverBufferImpl
* capture_buffer
;
3388 struct IDsCaptureDriverBufferImpl
3390 /* IUnknown fields */
3391 ICOM_VFIELD(IDsCaptureDriverBuffer
);
3393 /* IDsCaptureDriverBufferImpl fields */
3394 IDsCaptureDriverImpl
* drv
;
3397 /* IDsDriverNotifyImpl fields */
3398 IDsDriverNotifyImpl
* notify
;
3401 /* IDsDriverPropertySetImpl fields */
3402 IDsDriverPropertySetImpl
* property_set
;
3405 static HRESULT
DSDB_MapCapture(IDsCaptureDriverBufferImpl
*dscdb
)
3407 WINE_WAVEIN
*wwi
= &(WInDev
[dscdb
->drv
->wDevID
]);
3408 if (!wwi
->mapping
) {
3409 wwi
->mapping
= mmap(NULL
, wwi
->maplen
, PROT_WRITE
, MAP_SHARED
,
3410 wwi
->ossdev
->fd
, 0);
3411 if (wwi
->mapping
== (LPBYTE
)-1) {
3412 TRACE("(%p): Could not map sound device for direct access (%s)\n", dscdb
, strerror(errno
));
3413 return DSERR_GENERIC
;
3415 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dscdb
, wwi
->mapping
, wwi
->maplen
);
3417 /* for some reason, es1371 and sblive! sometimes have junk in here.
3418 * clear it, or we get junk noise */
3419 /* some libc implementations are buggy: their memset reads from the buffer...
3420 * to work around it, we have to zero the block by hand. We don't do the expected:
3421 * memset(wwo->mapping,0, wwo->maplen);
3424 char* p1
= wwi
->mapping
;
3425 unsigned len
= wwi
->maplen
;
3427 if (len
>= 16) /* so we can have at least a 4 long area to store... */
3429 /* the mmap:ed value is (at least) dword aligned
3430 * so, start filling the complete unsigned long:s
3433 unsigned long* p4
= (unsigned long*)p1
;
3435 while (b
--) *p4
++ = 0;
3436 /* prepare for filling the rest */
3438 p1
= (unsigned char*)p4
;
3440 /* in all cases, fill the remaining bytes */
3441 while (len
-- != 0) *p1
++ = 0;
3447 static HRESULT
DSDB_UnmapCapture(IDsCaptureDriverBufferImpl
*dscdb
)
3449 WINE_WAVEIN
*wwi
= &(WInDev
[dscdb
->drv
->wDevID
]);
3451 if (munmap(wwi
->mapping
, wwi
->maplen
) < 0) {
3452 ERR("(%p): Could not unmap sound device (%s)\n", dscdb
, strerror(errno
));
3453 return DSERR_GENERIC
;
3455 wwi
->mapping
= NULL
;
3456 TRACE("(%p): sound device unmapped\n", dscdb
);
3461 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_QueryInterface(PIDSCDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
3463 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3464 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
3466 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
3467 IsEqualGUID(riid
, &IID_IDsCaptureDriverBuffer
) ) {
3468 IDsCaptureDriverBuffer_AddRef(iface
);
3469 *ppobj
= (LPVOID
)This
;
3473 if ( IsEqualGUID( &IID_IDsDriverNotify
, riid
) ) {
3474 if (!This
->notify
) {
3475 This
->notify
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
->notify
));
3477 This
->notify
->ref
= 0; /* release when ref = -1 */
3478 This
->notify
->lpVtbl
= &dsdnvt
;
3482 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY
)This
->notify
);
3483 *ppobj
= (LPVOID
)This
->notify
;
3490 if ( IsEqualGUID( &IID_IDsDriverPropertySet
, riid
) ) {
3491 if (!This
->property_set
) {
3492 This
->property_set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
->property_set
));
3493 if (This
->property_set
) {
3494 This
->property_set
->ref
= 0; /* release when ref = -1 */
3495 This
->property_set
->lpVtbl
= &dsdpsvt
;
3498 if (This
->property_set
) {
3499 IDsDriverPropertySet_AddRef((PIDSDRIVERPROPERTYSET
)This
->property_set
);
3500 *ppobj
= (LPVOID
)This
->property_set
;
3507 FIXME("(%p,%s,%p) unsupported GUID\n", This
, debugstr_guid(riid
), ppobj
);
3511 return DSERR_UNSUPPORTED
;
3514 static ULONG WINAPI
IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface
)
3516 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3521 static ULONG WINAPI
IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface
)
3523 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3526 DSDB_UnmapCapture(This
);
3528 IDsDriverNotify_Release((PIDSDRIVERNOTIFY
)This
->notify
);
3529 if (This
->property_set
)
3530 IDsDriverPropertySet_Release((PIDSDRIVERPROPERTYSET
)This
->property_set
);
3531 HeapFree(GetProcessHeap(),0,This
);
3535 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Lock(PIDSCDRIVERBUFFER iface
,
3536 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
3537 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
3538 DWORD dwWritePosition
,DWORD dwWriteLen
,
3541 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3542 FIXME("(%p,%p,%p,%p,%p,%ld,%ld,0x%08lx): stub!\n",This
,ppvAudio1
,pdwLen1
,ppvAudio2
,pdwLen2
,
3543 dwWritePosition
,dwWriteLen
,dwFlags
);
3547 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Unlock(PIDSCDRIVERBUFFER iface
,
3548 LPVOID pvAudio1
,DWORD dwLen1
,
3549 LPVOID pvAudio2
,DWORD dwLen2
)
3551 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3552 FIXME("(%p,%p,%ld,%p,%ld): stub!\n",This
,pvAudio1
,dwLen1
,pvAudio2
,dwLen2
);
3556 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_GetPosition(PIDSCDRIVERBUFFER iface
,
3557 LPDWORD lpdwCapture
,
3560 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3563 TRACE("(%p,%p,%p)\n",This
,lpdwCapture
,lpdwRead
);
3565 if (WInDev
[This
->drv
->wDevID
].state
== WINE_WS_CLOSED
) {
3566 ERR("device not open, but accessing?\n");
3567 return DSERR_UNINITIALIZED
;
3569 if (ioctl(WInDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
3570 ERR("ioctl(%s, SNDCTL_DSP_GETIPTR) failed (%s)\n", WInDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3571 return DSERR_GENERIC
;
3573 ptr
= info
.ptr
& ~3; /* align the pointer, just in case */
3574 if (lpdwCapture
) *lpdwCapture
= ptr
;
3576 /* add some safety margin (not strictly necessary, but...) */
3577 if (WInDev
[This
->drv
->wDevID
].ossdev
->in_caps_support
& WAVECAPS_SAMPLEACCURATE
)
3578 *lpdwRead
= ptr
+ 32;
3580 *lpdwRead
= ptr
+ WInDev
[This
->drv
->wDevID
].dwFragmentSize
;
3581 while (*lpdwRead
> This
->buflen
)
3582 *lpdwRead
-= This
->buflen
;
3584 TRACE("capturepos=%ld, readpos=%ld\n", lpdwCapture
?*lpdwCapture
:0, lpdwRead
?*lpdwRead
:0);
3588 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_GetStatus(PIDSCDRIVERBUFFER iface
, LPDWORD lpdwStatus
)
3590 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3591 FIXME("(%p,%p): stub!\n",This
,lpdwStatus
);
3592 return DSERR_UNSUPPORTED
;
3595 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface
, DWORD dwFlags
)
3597 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3599 TRACE("(%p,%lx)\n",This
,dwFlags
);
3600 WInDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= TRUE
;
3601 enable
= getEnables(WInDev
[This
->drv
->wDevID
].ossdev
);
3602 if (ioctl(WInDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
3603 if (errno
== EINVAL
) {
3604 /* Don't give up yet. OSS trigger support is inconsistent. */
3605 if (WInDev
[This
->drv
->wDevID
].ossdev
->open_count
== 1) {
3606 /* try the opposite output enable */
3607 if (WInDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
== FALSE
)
3608 WInDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= TRUE
;
3610 WInDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
3612 enable
= getEnables(WInDev
[This
->drv
->wDevID
].ossdev
);
3613 if (ioctl(WInDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) >= 0)
3617 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3618 WInDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= FALSE
;
3619 return DSERR_GENERIC
;
3624 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface
)
3626 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3628 TRACE("(%p)\n",This
);
3629 /* no more captureing */
3630 WInDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= FALSE
;
3631 enable
= getEnables(WInDev
[This
->drv
->wDevID
].ossdev
);
3632 if (ioctl(WInDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
3633 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3634 return DSERR_GENERIC
;
3637 /* Most OSS drivers just can't stop capturing without closing the device...
3638 * so we need to somehow signal to our DirectSound implementation
3639 * that it should completely recreate this HW buffer...
3640 * this unexpected error code should do the trick... */
3641 return DSERR_BUFFERLOST
;
3644 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_SetFormat(PIDSCDRIVERBUFFER iface
, LPWAVEFORMATEX pwfx
)
3646 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3647 FIXME("(%p): stub!\n",This
);
3648 return DSERR_UNSUPPORTED
;
3651 static ICOM_VTABLE(IDsCaptureDriverBuffer
) dscdbvt
=
3653 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3654 IDsCaptureDriverBufferImpl_QueryInterface
,
3655 IDsCaptureDriverBufferImpl_AddRef
,
3656 IDsCaptureDriverBufferImpl_Release
,
3657 IDsCaptureDriverBufferImpl_Lock
,
3658 IDsCaptureDriverBufferImpl_Unlock
,
3659 IDsCaptureDriverBufferImpl_SetFormat
,
3660 IDsCaptureDriverBufferImpl_GetPosition
,
3661 IDsCaptureDriverBufferImpl_GetStatus
,
3662 IDsCaptureDriverBufferImpl_Start
,
3663 IDsCaptureDriverBufferImpl_Stop
3666 static HRESULT WINAPI
IDsCaptureDriverImpl_QueryInterface(PIDSCDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
3668 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3669 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
3671 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
3672 IsEqualGUID(riid
, &IID_IDsCaptureDriver
) ) {
3673 IDsCaptureDriver_AddRef(iface
);
3674 *ppobj
= (LPVOID
)This
;
3678 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
3682 return E_NOINTERFACE
;
3685 static ULONG WINAPI
IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface
)
3687 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3688 TRACE("(%p)\n",This
);
3690 TRACE("ref=%ld\n",This
->ref
);
3694 static ULONG WINAPI
IDsCaptureDriverImpl_Release(PIDSCDRIVER iface
)
3696 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3697 TRACE("(%p)\n",This
);
3699 TRACE("ref=%ld\n",This
->ref
);
3702 HeapFree(GetProcessHeap(),0,This
);
3707 static HRESULT WINAPI
IDsCaptureDriverImpl_GetDriverDesc(PIDSCDRIVER iface
, PDSDRIVERDESC pDesc
)
3709 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3710 TRACE("(%p,%p)\n",This
,pDesc
);
3713 TRACE("invalid parameter\n");
3714 return DSERR_INVALIDPARAM
;
3717 /* copy version from driver */
3718 memcpy(pDesc
, &(WInDev
[This
->wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
3720 pDesc
->dwFlags
|= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
|
3721 DSDDESC_USESYSTEMMEMORY
| DSDDESC_DONTNEEDPRIMARYLOCK
|
3722 DSDDESC_DONTNEEDSECONDARYLOCK
;
3723 pDesc
->dnDevNode
= WInDev
[This
->wDevID
].waveDesc
.dnDevNode
;
3725 pDesc
->wReserved
= 0;
3726 pDesc
->ulDeviceNum
= This
->wDevID
;
3727 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
3728 pDesc
->pvDirectDrawHeap
= NULL
;
3729 pDesc
->dwMemStartAddress
= 0;
3730 pDesc
->dwMemEndAddress
= 0;
3731 pDesc
->dwMemAllocExtra
= 0;
3732 pDesc
->pvReserved1
= NULL
;
3733 pDesc
->pvReserved2
= NULL
;
3737 static HRESULT WINAPI
IDsCaptureDriverImpl_Open(PIDSCDRIVER iface
)
3739 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3740 TRACE("(%p)\n",This
);
3744 static HRESULT WINAPI
IDsCaptureDriverImpl_Close(PIDSCDRIVER iface
)
3746 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3747 TRACE("(%p)\n",This
);
3748 if (This
->capture_buffer
) {
3749 ERR("problem with DirectSound: capture buffer not released\n");
3750 return DSERR_GENERIC
;
3755 static HRESULT WINAPI
IDsCaptureDriverImpl_GetCaps(PIDSCDRIVER iface
, PDSCDRIVERCAPS pCaps
)
3757 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3758 TRACE("(%p,%p)\n",This
,pCaps
);
3759 memcpy(pCaps
, &(WInDev
[This
->wDevID
].ossdev
->dsc_caps
), sizeof(DSCDRIVERCAPS
));
3763 static HRESULT WINAPI
IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface
,
3764 LPWAVEFORMATEX pwfx
,
3766 DWORD dwCardAddress
,
3767 LPDWORD pdwcbBufferSize
,
3771 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3772 IDsCaptureDriverBufferImpl
** ippdscdb
= (IDsCaptureDriverBufferImpl
**)ppvObj
;
3774 audio_buf_info info
;
3776 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",This
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
3778 if (This
->capture_buffer
) {
3779 TRACE("already allocated\n");
3780 return DSERR_ALLOCATED
;
3783 *ippdscdb
= (IDsCaptureDriverBufferImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsCaptureDriverBufferImpl
));
3784 if (*ippdscdb
== NULL
) {
3785 TRACE("out of memory\n");
3786 return DSERR_OUTOFMEMORY
;
3789 (*ippdscdb
)->lpVtbl
= &dscdbvt
;
3790 (*ippdscdb
)->ref
= 1;
3791 (*ippdscdb
)->drv
= This
;
3792 (*ippdscdb
)->notify
= 0;
3793 (*ippdscdb
)->notify_index
= 0;
3794 (*ippdscdb
)->property_set
= 0;
3796 if (WInDev
[This
->wDevID
].state
== WINE_WS_CLOSED
) {
3799 desc
.lpFormat
= pwfx
;
3800 desc
.dwCallback
= 0;
3801 desc
.dwInstance
= 0;
3802 desc
.uMappedDeviceID
= 0;
3804 err
= widOpen(This
->wDevID
, &desc
, dwFlags
| WAVE_DIRECTSOUND
);
3805 if (err
!= MMSYSERR_NOERROR
) {
3806 TRACE("widOpen failed\n");
3811 /* check how big the DMA buffer is now */
3812 if (ioctl(WInDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
3813 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", WInDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3814 HeapFree(GetProcessHeap(),0,*ippdscdb
);
3816 return DSERR_GENERIC
;
3818 WInDev
[This
->wDevID
].maplen
= (*ippdscdb
)->buflen
= info
.fragstotal
* info
.fragsize
;
3820 /* map the DMA buffer */
3821 err
= DSDB_MapCapture(*ippdscdb
);
3823 HeapFree(GetProcessHeap(),0,*ippdscdb
);
3828 /* capture buffer is ready to go */
3829 *pdwcbBufferSize
= WInDev
[This
->wDevID
].maplen
;
3830 *ppbBuffer
= WInDev
[This
->wDevID
].mapping
;
3832 /* some drivers need some extra nudging after mapping */
3833 WInDev
[This
->wDevID
].ossdev
->bInputEnabled
= FALSE
;
3834 enable
= getEnables(WInDev
[This
->wDevID
].ossdev
);
3835 if (ioctl(WInDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
3836 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3837 return DSERR_GENERIC
;
3840 This
->capture_buffer
= *ippdscdb
;
3845 static ICOM_VTABLE(IDsCaptureDriver
) dscdvt
=
3847 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3848 IDsCaptureDriverImpl_QueryInterface
,
3849 IDsCaptureDriverImpl_AddRef
,
3850 IDsCaptureDriverImpl_Release
,
3851 IDsCaptureDriverImpl_GetDriverDesc
,
3852 IDsCaptureDriverImpl_Open
,
3853 IDsCaptureDriverImpl_Close
,
3854 IDsCaptureDriverImpl_GetCaps
,
3855 IDsCaptureDriverImpl_CreateCaptureBuffer
3858 static DWORD
widDsCreate(UINT wDevID
, PIDSCDRIVER
* drv
)
3860 IDsCaptureDriverImpl
** idrv
= (IDsCaptureDriverImpl
**)drv
;
3861 TRACE("(%d,%p)\n",wDevID
,drv
);
3863 /* the HAL isn't much better than the HEL if we can't do mmap() */
3864 if (!(WInDev
[wDevID
].ossdev
->in_caps_support
& WAVECAPS_DIRECTSOUND
)) {
3865 ERR("DirectSoundCapture flag not set\n");
3866 MESSAGE("This sound card's driver does not support direct access\n");
3867 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
3868 return MMSYSERR_NOTSUPPORTED
;
3871 *idrv
= (IDsCaptureDriverImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsCaptureDriverImpl
));
3873 return MMSYSERR_NOMEM
;
3874 (*idrv
)->lpVtbl
= &dscdvt
;
3877 (*idrv
)->wDevID
= wDevID
;
3878 (*idrv
)->capture_buffer
= NULL
;
3879 return MMSYSERR_NOERROR
;
3882 static DWORD
widDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
3884 memcpy(desc
, &(WInDev
[wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
3885 return MMSYSERR_NOERROR
;
3888 static DWORD
widDsGuid(UINT wDevID
, LPGUID pGuid
)
3890 TRACE("(%d,%p)\n",wDevID
,pGuid
);
3892 memcpy(pGuid
, &(WInDev
[wDevID
].ossdev
->dsc_guid
), sizeof(GUID
));
3894 return MMSYSERR_NOERROR
;
3897 #else /* !HAVE_OSS */
3899 /**************************************************************************
3900 * wodMessage (WINEOSS.7)
3902 DWORD WINAPI
OSS_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3903 DWORD dwParam1
, DWORD dwParam2
)
3905 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
3906 return MMSYSERR_NOTENABLED
;
3909 /**************************************************************************
3910 * widMessage (WINEOSS.6)
3912 DWORD WINAPI
OSS_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3913 DWORD dwParam1
, DWORD dwParam2
)
3915 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
3916 return MMSYSERR_NOTENABLED
;
3919 #endif /* HAVE_OSS */