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 static DWORD
wdDevInterfaceSize(UINT wDevID
, LPDWORD dwParam1
)
262 TRACE("(%u, %p)\n", wDevID
, dwParam1
);
264 *dwParam1
= MultiByteToWideChar(CP_ACP
, 0, OSS_Devices
[wDevID
].dev_name
, -1,
265 NULL
, 0 ) * sizeof(WCHAR
);
266 return MMSYSERR_NOERROR
;
269 static DWORD
wdDevInterface(UINT wDevID
, PWCHAR dwParam1
, DWORD dwParam2
)
271 if (dwParam2
>= MultiByteToWideChar(CP_ACP
, 0, OSS_Devices
[wDevID
].dev_name
, -1,
272 NULL
, 0 ) * sizeof(WCHAR
))
274 MultiByteToWideChar(CP_ACP
, 0, OSS_Devices
[wDevID
].dev_name
, -1,
275 dwParam1
, dwParam2
/ sizeof(WCHAR
));
276 return MMSYSERR_NOERROR
;
279 return MMSYSERR_INVALPARAM
;
282 /*======================================================================*
283 * Low level WAVE implementation *
284 *======================================================================*/
286 /******************************************************************
289 * Low level device opening (from values stored in ossdev)
291 static DWORD
OSS_RawOpenDevice(OSS_DEVICE
* ossdev
, int strict_format
)
294 TRACE("(%p,%d)\n",ossdev
,strict_format
);
296 if ((fd
= open(ossdev
->dev_name
, ossdev
->open_access
|O_NDELAY
, 0)) == -1)
298 WARN("Couldn't open %s (%s)\n", ossdev
->dev_name
, strerror(errno
));
299 return (errno
== EBUSY
) ? MMSYSERR_ALLOCATED
: MMSYSERR_ERROR
;
301 fcntl(fd
, F_SETFD
, 1); /* set close on exec flag */
302 /* turn full duplex on if it has been requested */
303 if (ossdev
->open_access
== O_RDWR
&& ossdev
->full_duplex
) {
304 rc
= ioctl(fd
, SNDCTL_DSP_SETDUPLEX
, 0);
305 /* on *BSD, as full duplex is always enabled by default, this ioctl
306 * will fail with EINVAL
307 * so, we don't consider EINVAL an error here
309 if (rc
!= 0 && errno
!= EINVAL
) {
310 ERR("ioctl(%s, SNDCTL_DSP_SETDUPLEX) failed (%s)\n", ossdev
->dev_name
, strerror(errno
));
315 if (ossdev
->audio_fragment
) {
316 rc
= ioctl(fd
, SNDCTL_DSP_SETFRAGMENT
, &ossdev
->audio_fragment
);
318 ERR("ioctl(%s, SNDCTL_DSP_SETFRAGMENT) failed (%s)\n", ossdev
->dev_name
, strerror(errno
));
323 /* First size and stereo then samplerate */
324 if (ossdev
->format
>=0)
326 val
= ossdev
->format
;
327 rc
= ioctl(fd
, SNDCTL_DSP_SETFMT
, &ossdev
->format
);
328 if (rc
!= 0 || val
!= ossdev
->format
) {
329 TRACE("Can't set format to %d (returned %d)\n", val
, ossdev
->format
);
334 if (ossdev
->stereo
>=0)
336 val
= ossdev
->stereo
;
337 rc
= ioctl(fd
, SNDCTL_DSP_STEREO
, &ossdev
->stereo
);
338 if (rc
!= 0 || val
!= ossdev
->stereo
) {
339 TRACE("Can't set stereo to %u (returned %d)\n", val
, ossdev
->stereo
);
344 if (ossdev
->sample_rate
>=0)
346 val
= ossdev
->sample_rate
;
347 rc
= ioctl(fd
, SNDCTL_DSP_SPEED
, &ossdev
->sample_rate
);
348 if (rc
!= 0 || !NEAR_MATCH(val
, ossdev
->sample_rate
)) {
349 TRACE("Can't set sample_rate to %u (returned %d)\n", val
, ossdev
->sample_rate
);
356 if (ossdev
->bTriggerSupport
) {
358 rc
= ioctl(fd
, SNDCTL_DSP_GETTRIGGER
, &trigger
);
360 ERR("ioctl(%s, SNDCTL_DSP_GETTRIGGER) failed (%s)\n",
361 ossdev
->dev_name
, strerror(errno
));
365 ossdev
->bOutputEnabled
= ((trigger
& PCM_ENABLE_OUTPUT
) == PCM_ENABLE_OUTPUT
);
366 ossdev
->bInputEnabled
= ((trigger
& PCM_ENABLE_INPUT
) == PCM_ENABLE_INPUT
);
368 ossdev
->bOutputEnabled
= TRUE
; /* OSS enables by default */
369 ossdev
->bInputEnabled
= TRUE
; /* OSS enables by default */
372 return MMSYSERR_NOERROR
;
376 return WAVERR_BADFORMAT
;
379 return MMSYSERR_ERROR
;
382 /******************************************************************
385 * since OSS has poor capabilities in full duplex, we try here to let a program
386 * open the device for both waveout and wavein streams...
387 * this is hackish, but it's the way OSS interface is done...
389 static DWORD
OSS_OpenDevice(OSS_DEVICE
* ossdev
, unsigned req_access
,
390 int* frag
, int strict_format
,
391 int sample_rate
, int stereo
, int fmt
)
394 TRACE("(%p,%u,%p,%d,%d,%d,%x)\n",ossdev
,req_access
,frag
,strict_format
,sample_rate
,stereo
,fmt
);
396 if (ossdev
->full_duplex
&& (req_access
== O_RDONLY
|| req_access
== O_WRONLY
))
399 /* FIXME: this should be protected, and it also contains a race with OSS_CloseDevice */
400 if (ossdev
->open_count
== 0)
402 if (access(ossdev
->dev_name
, 0) != 0) return MMSYSERR_NODRIVER
;
404 ossdev
->audio_fragment
= (frag
) ? *frag
: 0;
405 ossdev
->sample_rate
= sample_rate
;
406 ossdev
->stereo
= stereo
;
407 ossdev
->format
= fmt
;
408 ossdev
->open_access
= req_access
;
409 ossdev
->owner_tid
= GetCurrentThreadId();
411 if ((ret
= OSS_RawOpenDevice(ossdev
,strict_format
)) != MMSYSERR_NOERROR
) return ret
;
415 /* check we really open with the same parameters */
416 if (ossdev
->open_access
!= req_access
)
418 ERR("FullDuplex: Mismatch in access. Your sound device is not full duplex capable.\n");
419 return WAVERR_BADFORMAT
;
422 /* check if the audio parameters are the same */
423 if (ossdev
->sample_rate
!= sample_rate
||
424 ossdev
->stereo
!= stereo
||
425 ossdev
->format
!= fmt
)
427 /* This is not a fatal error because MSACM might do the remapping */
428 WARN("FullDuplex: mismatch in PCM parameters for input and output\n"
429 "OSS doesn't allow us different parameters\n"
430 "audio_frag(%x/%x) sample_rate(%d/%d) stereo(%d/%d) fmt(%d/%d)\n",
431 ossdev
->audio_fragment
, frag
? *frag
: 0,
432 ossdev
->sample_rate
, sample_rate
,
433 ossdev
->stereo
, stereo
,
434 ossdev
->format
, fmt
);
435 return WAVERR_BADFORMAT
;
437 /* check if the fragment sizes are the same */
438 if (ossdev
->audio_fragment
!= (frag
? *frag
: 0) ) {
439 ERR("FullDuplex: Playback and Capture hardware acceleration levels are different.\n"
440 "Use: \"HardwareAcceleration\" = \"Emulation\" in the [dsound] section of your config file.\n");
441 return WAVERR_BADFORMAT
;
443 if (GetCurrentThreadId() != ossdev
->owner_tid
)
445 WARN("Another thread is trying to access audio...\n");
446 return MMSYSERR_ERROR
;
450 ossdev
->open_count
++;
452 return MMSYSERR_NOERROR
;
455 /******************************************************************
460 static void OSS_CloseDevice(OSS_DEVICE
* ossdev
)
462 TRACE("(%p)\n",ossdev
);
463 if (ossdev
->open_count
>0) {
464 ossdev
->open_count
--;
466 WARN("OSS_CloseDevice called too many times\n");
468 if (ossdev
->open_count
== 0)
470 /* reset the device before we close it in case it is in a bad state */
471 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
476 /******************************************************************
479 * Resets the device. OSS Commercial requires the device to be closed
480 * after a SNDCTL_DSP_RESET ioctl call... this function implements
482 * FIXME: This causes problems when doing full duplex so we really
483 * only reset when not doing full duplex. We need to do this better
486 static DWORD
OSS_ResetDevice(OSS_DEVICE
* ossdev
)
488 DWORD ret
= MMSYSERR_NOERROR
;
489 int old_fd
= ossdev
->fd
;
490 TRACE("(%p)\n", ossdev
);
492 if (ossdev
->open_count
== 1) {
493 if (ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, NULL
) == -1)
495 perror("ioctl SNDCTL_DSP_RESET");
499 ret
= OSS_RawOpenDevice(ossdev
, 1);
500 TRACE("Changing fd from %d to %d\n", old_fd
, ossdev
->fd
);
502 WARN("Not resetting device because it is in full duplex mode!\n");
507 const static int win_std_oss_fmts
[2]={AFMT_U8
,AFMT_S16_LE
};
508 const static int win_std_rates
[5]={96000,48000,44100,22050,11025};
509 const static int win_std_formats
[2][2][5]=
510 {{{WAVE_FORMAT_96M08
, WAVE_FORMAT_48M08
, WAVE_FORMAT_4M08
,
511 WAVE_FORMAT_2M08
, WAVE_FORMAT_1M08
},
512 {WAVE_FORMAT_96S08
, WAVE_FORMAT_48S08
, WAVE_FORMAT_4S08
,
513 WAVE_FORMAT_2S08
, WAVE_FORMAT_1S08
}},
514 {{WAVE_FORMAT_96M16
, WAVE_FORMAT_48M16
, WAVE_FORMAT_4M16
,
515 WAVE_FORMAT_2M16
, WAVE_FORMAT_1M16
},
516 {WAVE_FORMAT_96S16
, WAVE_FORMAT_48S16
, WAVE_FORMAT_4S16
,
517 WAVE_FORMAT_2S16
, WAVE_FORMAT_1S16
}},
520 /******************************************************************
525 static BOOL
OSS_WaveOutInit(OSS_DEVICE
* ossdev
)
529 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
531 if (OSS_OpenDevice(ossdev
, O_WRONLY
, NULL
, 0,-1,-1,-1) != 0) return FALSE
;
532 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
534 #ifdef SOUND_MIXER_INFO
537 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
539 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
541 strncpy(ossdev
->ds_desc
.szDesc
, info
.name
, sizeof(info
.name
));
542 strcpy(ossdev
->ds_desc
.szDrvName
, "wineoss.drv");
543 strncpy(ossdev
->out_caps
.szPname
, info
.name
, sizeof(info
.name
));
544 TRACE("%s\n", ossdev
->ds_desc
.szDesc
);
546 ERR("%s: can't read info!\n", ossdev
->mixer_name
);
547 OSS_CloseDevice(ossdev
);
552 ERR("%s: %s\n", ossdev
->mixer_name
, strerror( errno
));
553 OSS_CloseDevice(ossdev
);
557 #endif /* SOUND_MIXER_INFO */
559 /* FIXME: some programs compare this string against the content of the
560 * registry for MM drivers. The names have to match in order for the
561 * program to work (e.g. MS win9x mplayer.exe)
564 ossdev
->out_caps
.wMid
= 0x0002;
565 ossdev
->out_caps
.wPid
= 0x0104;
566 strcpy(ossdev
->out_caps
.szPname
, "SB16 Wave Out");
568 ossdev
->out_caps
.wMid
= 0x00FF; /* Manufac ID */
569 ossdev
->out_caps
.wPid
= 0x0001; /* Product ID */
571 ossdev
->out_caps
.vDriverVersion
= 0x0100;
572 ossdev
->out_caps
.wChannels
= 1;
573 ossdev
->out_caps
.dwFormats
= 0x00000000;
574 ossdev
->out_caps
.wReserved1
= 0;
575 ossdev
->out_caps
.dwSupport
= WAVECAPS_VOLUME
;
577 /* direct sound caps */
578 ossdev
->ds_caps
.dwFlags
= 0;
579 ossdev
->ds_caps
.dwPrimaryBuffers
= 1;
580 ossdev
->ds_caps
.dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
581 ossdev
->ds_caps
.dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
583 if (WINE_TRACE_ON(wave
)) {
584 /* Note that this only reports the formats supported by the hardware.
585 * The driver may support other formats and do the conversions in
586 * software which is why we don't use this value
589 ioctl(ossdev
->fd
, SNDCTL_DSP_GETFMTS
, &oss_mask
);
590 TRACE("OSS dsp out mask=%08x\n", oss_mask
);
593 /* We must first set the format and the stereo mode as some sound cards
594 * may support 44kHz mono but not 44kHz stereo. Also we must
595 * systematically check the return value of these ioctls as they will
596 * always succeed (see OSS Linux) but will modify the parameter to match
597 * whatever they support. The OSS specs also say we must first set the
598 * sample size, then the stereo and then the sample rate.
601 arg
=win_std_oss_fmts
[f
];
602 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
603 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
604 TRACE("DSP_SAMPLESIZE: rc=%d returned %d for %d\n",
605 rc
,arg
,win_std_oss_fmts
[f
]);
609 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARY8BIT
;
611 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARY16BIT
;
615 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_STEREO
, &arg
);
616 if (rc
!=0 || arg
!=c
) {
617 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc
,arg
,c
);
621 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYMONO
;
623 ossdev
->out_caps
.wChannels
=2;
624 ossdev
->out_caps
.dwSupport
|=WAVECAPS_LRVOLUME
;
625 ossdev
->ds_caps
.dwFlags
|= DSCAPS_PRIMARYSTEREO
;
628 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
629 arg
=win_std_rates
[r
];
630 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
631 TRACE("DSP_SPEED: rc=%d returned %d for %dx%dx%d\n",
632 rc
,arg
,win_std_rates
[r
],win_std_oss_fmts
[f
],c
+1);
633 if (rc
==0 && arg
!=0 && NEAR_MATCH(arg
,win_std_rates
[r
]))
634 ossdev
->out_caps
.dwFormats
|=win_std_formats
[f
][c
][r
];
639 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
640 TRACE("OSS dsp out caps=%08X\n", arg
);
641 if (arg
& DSP_CAP_TRIGGER
)
642 ossdev
->bTriggerSupport
= TRUE
;
643 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
)) {
644 ossdev
->out_caps
.dwSupport
|= WAVECAPS_SAMPLEACCURATE
;
646 /* well, might as well use the DirectSound cap flag for something */
647 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
648 !(arg
& DSP_CAP_BATCH
)) {
649 ossdev
->out_caps
.dwSupport
|= WAVECAPS_DIRECTSOUND
;
651 ossdev
->ds_caps
.dwFlags
|= DSCAPS_EMULDRIVER
;
654 OSS_CloseDevice(ossdev
);
655 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
656 ossdev
->out_caps
.dwFormats
, ossdev
->out_caps
.dwSupport
);
660 /******************************************************************
665 static BOOL
OSS_WaveInInit(OSS_DEVICE
* ossdev
)
669 TRACE("(%p) %s\n", ossdev
, ossdev
->dev_name
);
671 if (OSS_OpenDevice(ossdev
, O_RDONLY
, NULL
, 0,-1,-1,-1) != 0)
674 ioctl(ossdev
->fd
, SNDCTL_DSP_RESET
, 0);
676 #ifdef SOUND_MIXER_INFO
679 if ((mixer
= open(ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) >= 0) {
681 if (ioctl(mixer
, SOUND_MIXER_INFO
, &info
) >= 0) {
683 strncpy(ossdev
->in_caps
.szPname
, info
.name
, sizeof(info
.name
));
684 TRACE("%s\n", ossdev
->ds_desc
.szDesc
);
686 ERR("%s: can't read info!\n", ossdev
->mixer_name
);
687 OSS_CloseDevice(ossdev
);
692 ERR("%s: %s\n", ossdev
->mixer_name
, strerror(errno
));
693 OSS_CloseDevice(ossdev
);
697 #endif /* SOUND_MIXER_INFO */
699 /* See comment in OSS_WaveOutInit */
701 ossdev
->in_caps
.wMid
= 0x0002;
702 ossdev
->in_caps
.wPid
= 0x0004;
703 strcpy(ossdev
->in_caps
.szPname
, "SB16 Wave In");
705 ossdev
->in_caps
.wMid
= 0x00FF; /* Manufac ID */
706 ossdev
->in_caps
.wPid
= 0x0001; /* Product ID */
708 ossdev
->in_caps
.dwFormats
= 0x00000000;
709 ossdev
->in_caps
.wChannels
= 1;
710 ossdev
->in_caps
.wReserved1
= 0;
712 /* direct sound caps */
713 ossdev
->dsc_caps
.dwSize
= sizeof(ossdev
->dsc_caps
);
714 ossdev
->dsc_caps
.dwFlags
= 0;
715 ossdev
->dsc_caps
.dwFormats
= 0x00000000;
716 ossdev
->dsc_caps
.dwChannels
= 1;
718 if (WINE_TRACE_ON(wave
)) {
719 /* Note that this only reports the formats supported by the hardware.
720 * The driver may support other formats and do the conversions in
721 * software which is why we don't use this value
724 ioctl(ossdev
->fd
, SNDCTL_DSP_GETFMTS
, &oss_mask
);
725 TRACE("OSS dsp out mask=%08x\n", oss_mask
);
728 /* See the comment in OSS_WaveOutInit */
730 arg
=win_std_oss_fmts
[f
];
731 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SAMPLESIZE
, &arg
);
732 if (rc
!=0 || arg
!=win_std_oss_fmts
[f
]) {
733 TRACE("DSP_SAMPLESIZE: rc=%d returned 0x%x for 0x%x\n",
734 rc
,arg
,win_std_oss_fmts
[f
]);
740 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_STEREO
, &arg
);
741 if (rc
!=0 || arg
!=c
) {
742 TRACE("DSP_STEREO: rc=%d returned %d for %d\n",rc
,arg
,c
);
746 ossdev
->in_caps
.wChannels
=2;
747 ossdev
->dsc_caps
.dwChannels
=2;
750 for (r
=0;r
<sizeof(win_std_rates
)/sizeof(*win_std_rates
);r
++) {
751 arg
=win_std_rates
[r
];
752 rc
=ioctl(ossdev
->fd
, SNDCTL_DSP_SPEED
, &arg
);
753 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);
754 if (rc
==0 && NEAR_MATCH(arg
,win_std_rates
[r
]))
755 ossdev
->in_caps
.dwFormats
|=win_std_formats
[f
][c
][r
];
756 ossdev
->dsc_caps
.dwFormats
|=win_std_formats
[f
][c
][r
];
761 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &arg
) == 0) {
762 TRACE("OSS dsp in caps=%08X\n", arg
);
763 if (arg
& DSP_CAP_TRIGGER
)
764 ossdev
->bTriggerSupport
= TRUE
;
765 if ((arg
& DSP_CAP_TRIGGER
) && (arg
& DSP_CAP_MMAP
) &&
766 !(arg
& DSP_CAP_BATCH
)) {
767 /* FIXME: enable the next statement if you want to work on the driver */
769 ossdev
->in_caps_support
|= WAVECAPS_DIRECTSOUND
;
772 if ((arg
& DSP_CAP_REALTIME
) && !(arg
& DSP_CAP_BATCH
))
773 ossdev
->in_caps_support
|= WAVECAPS_SAMPLEACCURATE
;
775 OSS_CloseDevice(ossdev
);
776 TRACE("in dwFormats = %08lX\n", ossdev
->in_caps
.dwFormats
);
780 /******************************************************************
781 * OSS_WaveFullDuplexInit
785 static void OSS_WaveFullDuplexInit(OSS_DEVICE
* ossdev
)
788 TRACE("(%p)\n",ossdev
);
790 if (OSS_OpenDevice(ossdev
, O_RDWR
, NULL
, 0,-1,-1,-1) != 0) return;
791 if (ioctl(ossdev
->fd
, SNDCTL_DSP_GETCAPS
, &caps
) == 0)
793 ossdev
->full_duplex
= (caps
& DSP_CAP_DUPLEX
);
795 OSS_CloseDevice(ossdev
);
798 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
799 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
800 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
801 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
802 guid.Data4[6] = b7; guid.Data4[7] = b8;
803 /******************************************************************
806 * Initialize internal structures from OSS information
808 LONG
OSS_WaveInit(void)
813 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
816 sprintf((char *)OSS_Devices
[i
].dev_name
, "/dev/dsp");
817 sprintf((char *)OSS_Devices
[i
].mixer_name
, "/dev/mixer");
819 sprintf((char *)OSS_Devices
[i
].dev_name
, "/dev/dsp%d", i
);
820 sprintf((char *)OSS_Devices
[i
].mixer_name
, "/dev/mixer%d", i
);
823 INIT_GUID(OSS_Devices
[i
].ds_guid
, 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i
);
824 INIT_GUID(OSS_Devices
[i
].dsc_guid
, 0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i
);
827 /* start with output devices */
828 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
830 if (OSS_WaveOutInit(&OSS_Devices
[i
]))
832 WOutDev
[numOutDev
].state
= WINE_WS_CLOSED
;
833 WOutDev
[numOutDev
].ossdev
= &OSS_Devices
[i
];
838 /* then do input devices */
839 for (i
= 0; i
< MAX_WAVEDRV
; ++i
)
841 if (OSS_WaveInInit(&OSS_Devices
[i
]))
843 WInDev
[numInDev
].state
= WINE_WS_CLOSED
;
844 WInDev
[numInDev
].ossdev
= &OSS_Devices
[i
];
849 /* finish with the full duplex bits */
850 for (i
= 0; i
< MAX_WAVEDRV
; i
++)
851 OSS_WaveFullDuplexInit(&OSS_Devices
[i
]);
856 /******************************************************************
857 * OSS_InitRingMessage
859 * Initialize the ring of messages for passing between driver's caller and playback/record
862 static int OSS_InitRingMessage(OSS_MSG_RING
* omr
)
867 if (pipe(omr
->msg_pipe
) < 0) {
868 omr
->msg_pipe
[0] = -1;
869 omr
->msg_pipe
[1] = -1;
870 ERR("could not create pipe, error=%s\n", strerror(errno
));
873 omr
->msg_event
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
875 omr
->ring_buffer_size
= OSS_RING_BUFFER_INCREMENT
;
876 omr
->messages
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,omr
->ring_buffer_size
* sizeof(OSS_MSG
));
877 InitializeCriticalSection(&omr
->msg_crst
);
881 /******************************************************************
882 * OSS_DestroyRingMessage
885 static int OSS_DestroyRingMessage(OSS_MSG_RING
* omr
)
888 close(omr
->msg_pipe
[0]);
889 close(omr
->msg_pipe
[1]);
891 CloseHandle(omr
->msg_event
);
893 HeapFree(GetProcessHeap(),0,omr
->messages
);
894 DeleteCriticalSection(&omr
->msg_crst
);
898 /******************************************************************
901 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
903 static int OSS_AddRingMessage(OSS_MSG_RING
* omr
, enum win_wm_message msg
, DWORD param
, BOOL wait
)
905 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
907 EnterCriticalSection(&omr
->msg_crst
);
908 if ((omr
->msg_toget
== ((omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
)))
910 omr
->ring_buffer_size
+= OSS_RING_BUFFER_INCREMENT
;
911 TRACE("omr->ring_buffer_size=%d\n",omr
->ring_buffer_size
);
912 omr
->messages
= HeapReAlloc(GetProcessHeap(),0,omr
->messages
, omr
->ring_buffer_size
* sizeof(OSS_MSG
));
916 hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
917 if (hEvent
== INVALID_HANDLE_VALUE
)
919 ERR("can't create event !?\n");
920 LeaveCriticalSection(&omr
->msg_crst
);
923 if (omr
->msg_toget
!= omr
->msg_tosave
&& omr
->messages
[omr
->msg_toget
].msg
!= WINE_WM_HEADER
)
924 FIXME("two fast messages in the queue!!!!\n");
926 /* fast messages have to be added at the start of the queue */
927 omr
->msg_toget
= (omr
->msg_toget
+ omr
->ring_buffer_size
- 1) % omr
->ring_buffer_size
;
929 omr
->messages
[omr
->msg_toget
].msg
= msg
;
930 omr
->messages
[omr
->msg_toget
].param
= param
;
931 omr
->messages
[omr
->msg_toget
].hEvent
= hEvent
;
935 omr
->messages
[omr
->msg_tosave
].msg
= msg
;
936 omr
->messages
[omr
->msg_tosave
].param
= param
;
937 omr
->messages
[omr
->msg_tosave
].hEvent
= INVALID_HANDLE_VALUE
;
938 omr
->msg_tosave
= (omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
;
940 LeaveCriticalSection(&omr
->msg_crst
);
941 /* signal a new message */
945 /* wait for playback/record thread to have processed the message */
946 WaitForSingleObject(hEvent
, INFINITE
);
952 /******************************************************************
953 * OSS_RetrieveRingMessage
955 * Get a message from the ring. Should be called by the playback/record thread.
957 static int OSS_RetrieveRingMessage(OSS_MSG_RING
* omr
,
958 enum win_wm_message
*msg
, DWORD
*param
, HANDLE
*hEvent
)
960 EnterCriticalSection(&omr
->msg_crst
);
962 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
964 LeaveCriticalSection(&omr
->msg_crst
);
968 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
969 omr
->messages
[omr
->msg_toget
].msg
= 0;
970 *param
= omr
->messages
[omr
->msg_toget
].param
;
971 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
972 omr
->msg_toget
= (omr
->msg_toget
+ 1) % omr
->ring_buffer_size
;
974 LeaveCriticalSection(&omr
->msg_crst
);
978 /******************************************************************
979 * OSS_PeekRingMessage
981 * Peek at a message from the ring but do not remove it.
982 * Should be called by the playback/record thread.
984 static int OSS_PeekRingMessage(OSS_MSG_RING
* omr
,
985 enum win_wm_message
*msg
,
986 DWORD
*param
, HANDLE
*hEvent
)
988 EnterCriticalSection(&omr
->msg_crst
);
990 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
992 LeaveCriticalSection(&omr
->msg_crst
);
996 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
997 *param
= omr
->messages
[omr
->msg_toget
].param
;
998 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
999 LeaveCriticalSection(&omr
->msg_crst
);
1003 /*======================================================================*
1004 * Low level WAVE OUT implementation *
1005 *======================================================================*/
1007 /**************************************************************************
1008 * wodNotifyClient [internal]
1010 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
1012 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg
, dwParam1
, dwParam2
);
1018 if (wwo
->wFlags
!= DCB_NULL
&&
1019 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
,
1020 (HDRVR
)wwo
->waveDesc
.hWave
, wMsg
,
1021 wwo
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
1022 WARN("can't notify client !\n");
1023 return MMSYSERR_ERROR
;
1027 FIXME("Unknown callback message %u\n", wMsg
);
1028 return MMSYSERR_INVALPARAM
;
1030 return MMSYSERR_NOERROR
;
1033 /**************************************************************************
1034 * wodUpdatePlayedTotal [internal]
1037 static BOOL
wodUpdatePlayedTotal(WINE_WAVEOUT
* wwo
, audio_buf_info
* info
)
1039 audio_buf_info dspspace
;
1040 if (!info
) info
= &dspspace
;
1042 if (ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_GETOSPACE
, info
) < 0) {
1043 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo
->ossdev
->dev_name
, strerror(errno
));
1046 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
- (wwo
->dwBufferSize
- info
->bytes
);
1050 /**************************************************************************
1051 * wodPlayer_BeginWaveHdr [internal]
1053 * Makes the specified lpWaveHdr the currently playing wave header.
1054 * If the specified wave header is a begin loop and we're not already in
1055 * a loop, setup the loop.
1057 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
1059 wwo
->lpPlayPtr
= lpWaveHdr
;
1061 if (!lpWaveHdr
) return;
1063 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
) {
1064 if (wwo
->lpLoopPtr
) {
1065 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
1067 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
1068 wwo
->lpLoopPtr
= lpWaveHdr
;
1069 /* Windows does not touch WAVEHDR.dwLoops,
1070 * so we need to make an internal copy */
1071 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
1074 wwo
->dwPartialOffset
= 0;
1077 /**************************************************************************
1078 * wodPlayer_PlayPtrNext [internal]
1080 * Advance the play pointer to the next waveheader, looping if required.
1082 static LPWAVEHDR
wodPlayer_PlayPtrNext(WINE_WAVEOUT
* wwo
)
1084 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
1086 wwo
->dwPartialOffset
= 0;
1087 if ((lpWaveHdr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
) {
1088 /* We're at the end of a loop, loop if required */
1089 if (--wwo
->dwLoops
> 0) {
1090 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1092 /* Handle overlapping loops correctly */
1093 if (wwo
->lpLoopPtr
!= lpWaveHdr
&& (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)) {
1094 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1095 /* shall we consider the END flag for the closing loop or for
1096 * the opening one or for both ???
1097 * code assumes for closing loop only
1100 lpWaveHdr
= lpWaveHdr
->lpNext
;
1102 wwo
->lpLoopPtr
= NULL
;
1103 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
);
1106 /* We're not in a loop. Advance to the next wave header */
1107 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
= lpWaveHdr
->lpNext
);
1113 /**************************************************************************
1114 * wodPlayer_DSPWait [internal]
1115 * Returns the number of milliseconds to wait for the DSP buffer to write
1118 static DWORD
wodPlayer_DSPWait(const WINE_WAVEOUT
*wwo
)
1120 /* time for one fragment to be played */
1121 return wwo
->dwFragmentSize
* 1000 / wwo
->format
.wf
.nAvgBytesPerSec
;
1124 /**************************************************************************
1125 * wodPlayer_NotifyWait [internal]
1126 * Returns the number of milliseconds to wait before attempting to notify
1127 * completion of the specified wavehdr.
1128 * This is based on the number of bytes remaining to be written in the
1131 static DWORD
wodPlayer_NotifyWait(const WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
1135 if (lpWaveHdr
->reserved
< wwo
->dwPlayedTotal
) {
1138 dwMillis
= (lpWaveHdr
->reserved
- wwo
->dwPlayedTotal
) * 1000 / wwo
->format
.wf
.nAvgBytesPerSec
;
1139 if (!dwMillis
) dwMillis
= 1;
1146 /**************************************************************************
1147 * wodPlayer_WriteMaxFrags [internal]
1148 * Writes the maximum number of bytes possible to the DSP and returns
1149 * TRUE iff the current playPtr has been fully played
1151 static BOOL
wodPlayer_WriteMaxFrags(WINE_WAVEOUT
* wwo
, DWORD
* bytes
)
1153 DWORD dwLength
= wwo
->lpPlayPtr
->dwBufferLength
- wwo
->dwPartialOffset
;
1154 DWORD toWrite
= min(dwLength
, *bytes
);
1158 TRACE("Writing wavehdr %p.%lu[%lu]/%lu\n",
1159 wwo
->lpPlayPtr
, wwo
->dwPartialOffset
, wwo
->lpPlayPtr
->dwBufferLength
, toWrite
);
1163 written
= write(wwo
->ossdev
->fd
, wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
, toWrite
);
1164 if (written
<= 0) return FALSE
;
1169 if (written
>= dwLength
) {
1170 /* If we wrote all current wavehdr, skip to the next one */
1171 wodPlayer_PlayPtrNext(wwo
);
1174 /* Remove the amount written */
1175 wwo
->dwPartialOffset
+= written
;
1178 wwo
->dwWrittenTotal
+= written
;
1184 /**************************************************************************
1185 * wodPlayer_NotifyCompletions [internal]
1187 * Notifies and remove from queue all wavehdrs which have been played to
1188 * the speaker (ie. they have cleared the OSS buffer). If force is true,
1189 * we notify all wavehdrs and remove them all from the queue even if they
1190 * are unplayed or part of a loop.
1192 static DWORD
wodPlayer_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
1194 LPWAVEHDR lpWaveHdr
;
1196 /* Start from lpQueuePtr and keep notifying until:
1197 * - we hit an unwritten wavehdr
1198 * - we hit the beginning of a running loop
1199 * - we hit a wavehdr which hasn't finished playing
1201 while ((lpWaveHdr
= wwo
->lpQueuePtr
) &&
1203 (lpWaveHdr
!= wwo
->lpPlayPtr
&&
1204 lpWaveHdr
!= wwo
->lpLoopPtr
&&
1205 lpWaveHdr
->reserved
<= wwo
->dwPlayedTotal
))) {
1207 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
1209 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1210 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1212 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
1214 return (lpWaveHdr
&& lpWaveHdr
!= wwo
->lpPlayPtr
&& lpWaveHdr
!= wwo
->lpLoopPtr
) ?
1215 wodPlayer_NotifyWait(wwo
, lpWaveHdr
) : INFINITE
;
1218 /**************************************************************************
1219 * wodPlayer_Reset [internal]
1221 * wodPlayer helper. Resets current output stream.
1223 static void wodPlayer_Reset(WINE_WAVEOUT
* wwo
, BOOL reset
)
1225 wodUpdatePlayedTotal(wwo
, NULL
);
1226 /* updates current notify list */
1227 wodPlayer_NotifyCompletions(wwo
, FALSE
);
1229 /* flush all possible output */
1230 if (OSS_ResetDevice(wwo
->ossdev
) != MMSYSERR_NOERROR
)
1233 wwo
->state
= WINE_WS_STOPPED
;
1238 enum win_wm_message msg
;
1242 /* remove any buffer */
1243 wodPlayer_NotifyCompletions(wwo
, TRUE
);
1245 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
1246 wwo
->state
= WINE_WS_STOPPED
;
1247 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
= 0;
1248 /* Clear partial wavehdr */
1249 wwo
->dwPartialOffset
= 0;
1251 /* remove any existing message in the ring */
1252 EnterCriticalSection(&wwo
->msgRing
.msg_crst
);
1253 /* return all pending headers in queue */
1254 while (OSS_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
))
1256 if (msg
!= WINE_WM_HEADER
)
1258 FIXME("shouldn't have headers left\n");
1262 ((LPWAVEHDR
)param
)->dwFlags
&= ~WHDR_INQUEUE
;
1263 ((LPWAVEHDR
)param
)->dwFlags
|= WHDR_DONE
;
1265 wodNotifyClient(wwo
, WOM_DONE
, param
, 0);
1267 RESET_OMR(&wwo
->msgRing
);
1268 LeaveCriticalSection(&wwo
->msgRing
.msg_crst
);
1270 if (wwo
->lpLoopPtr
) {
1271 /* complicated case, not handled yet (could imply modifying the loop counter */
1272 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1273 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1274 wwo
->dwPartialOffset
= 0;
1275 wwo
->dwWrittenTotal
= wwo
->dwPlayedTotal
; /* this is wrong !!! */
1278 DWORD sz
= wwo
->dwPartialOffset
;
1280 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
1281 /* compute the max size playable from lpQueuePtr */
1282 for (ptr
= wwo
->lpQueuePtr
; ptr
!= wwo
->lpPlayPtr
; ptr
= ptr
->lpNext
) {
1283 sz
+= ptr
->dwBufferLength
;
1285 /* because the reset lpPlayPtr will be lpQueuePtr */
1286 if (wwo
->dwWrittenTotal
> wwo
->dwPlayedTotal
+ sz
) ERR("grin\n");
1287 wwo
->dwPartialOffset
= sz
- (wwo
->dwWrittenTotal
- wwo
->dwPlayedTotal
);
1288 wwo
->dwWrittenTotal
= wwo
->dwPlayedTotal
;
1289 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
;
1291 wwo
->state
= WINE_WS_PAUSED
;
1295 /**************************************************************************
1296 * wodPlayer_ProcessMessages [internal]
1298 static void wodPlayer_ProcessMessages(WINE_WAVEOUT
* wwo
)
1300 LPWAVEHDR lpWaveHdr
;
1301 enum win_wm_message msg
;
1305 while (OSS_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
)) {
1306 TRACE("Received %s %lx\n", wodPlayerCmdString
[msg
- WM_USER
- 1], param
);
1308 case WINE_WM_PAUSING
:
1309 wodPlayer_Reset(wwo
, FALSE
);
1312 case WINE_WM_RESTARTING
:
1313 if (wwo
->state
== WINE_WS_PAUSED
)
1315 wwo
->state
= WINE_WS_PLAYING
;
1319 case WINE_WM_HEADER
:
1320 lpWaveHdr
= (LPWAVEHDR
)param
;
1322 /* insert buffer at the end of queue */
1325 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
1328 if (!wwo
->lpPlayPtr
)
1329 wodPlayer_BeginWaveHdr(wwo
,lpWaveHdr
);
1330 if (wwo
->state
== WINE_WS_STOPPED
)
1331 wwo
->state
= WINE_WS_PLAYING
;
1333 case WINE_WM_RESETTING
:
1334 wodPlayer_Reset(wwo
, TRUE
);
1337 case WINE_WM_UPDATE
:
1338 wodUpdatePlayedTotal(wwo
, NULL
);
1341 case WINE_WM_BREAKLOOP
:
1342 if (wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpLoopPtr
!= NULL
) {
1343 /* ensure exit at end of current loop */
1348 case WINE_WM_CLOSING
:
1349 /* sanity check: this should not happen since the device must have been reset before */
1350 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
1352 wwo
->state
= WINE_WS_CLOSED
;
1355 /* shouldn't go here */
1357 FIXME("unknown message %d\n", msg
);
1363 /**************************************************************************
1364 * wodPlayer_FeedDSP [internal]
1365 * Feed as much sound data as we can into the DSP and return the number of
1366 * milliseconds before it will be necessary to feed the DSP again.
1368 static DWORD
wodPlayer_FeedDSP(WINE_WAVEOUT
* wwo
)
1370 audio_buf_info dspspace
;
1373 wodUpdatePlayedTotal(wwo
, &dspspace
);
1374 availInQ
= dspspace
.bytes
;
1375 TRACE("fragments=%d/%d, fragsize=%d, bytes=%d\n",
1376 dspspace
.fragments
, dspspace
.fragstotal
, dspspace
.fragsize
, dspspace
.bytes
);
1378 /* input queue empty and output buffer with less than one fragment to play
1379 * actually some cards do not play the fragment before the last if this one is partially feed
1380 * so we need to test for full the availability of 2 fragments
1382 if (!wwo
->lpPlayPtr
&& wwo
->dwBufferSize
< availInQ
+ 2 * wwo
->dwFragmentSize
&&
1384 TRACE("Run out of wavehdr:s...\n");
1388 /* no more room... no need to try to feed */
1389 if (dspspace
.fragments
!= 0) {
1390 /* Feed from partial wavehdr */
1391 if (wwo
->lpPlayPtr
&& wwo
->dwPartialOffset
!= 0) {
1392 wodPlayer_WriteMaxFrags(wwo
, &availInQ
);
1395 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1396 if (wwo
->dwPartialOffset
== 0 && wwo
->lpPlayPtr
) {
1398 TRACE("Setting time to elapse for %p to %lu\n",
1399 wwo
->lpPlayPtr
, wwo
->dwWrittenTotal
+ wwo
->lpPlayPtr
->dwBufferLength
);
1400 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1401 wwo
->lpPlayPtr
->reserved
= wwo
->dwWrittenTotal
+ wwo
->lpPlayPtr
->dwBufferLength
;
1402 } while (wodPlayer_WriteMaxFrags(wwo
, &availInQ
) && wwo
->lpPlayPtr
&& availInQ
> 0);
1405 if (wwo
->bNeedPost
) {
1406 /* OSS doesn't start before it gets either 2 fragments or a SNDCTL_DSP_POST;
1407 * if it didn't get one, we give it the other */
1408 if (wwo
->dwBufferSize
< availInQ
+ 2 * wwo
->dwFragmentSize
)
1409 ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_POST
, 0);
1410 wwo
->bNeedPost
= FALSE
;
1414 return wodPlayer_DSPWait(wwo
);
1418 /**************************************************************************
1419 * wodPlayer [internal]
1421 static DWORD CALLBACK
wodPlayer(LPVOID pmt
)
1423 WORD uDevID
= (DWORD
)pmt
;
1424 WINE_WAVEOUT
* wwo
= (WINE_WAVEOUT
*)&WOutDev
[uDevID
];
1425 DWORD dwNextFeedTime
= INFINITE
; /* Time before DSP needs feeding */
1426 DWORD dwNextNotifyTime
= INFINITE
; /* Time before next wave completion */
1429 wwo
->state
= WINE_WS_STOPPED
;
1430 SetEvent(wwo
->hStartUpEvent
);
1433 /** Wait for the shortest time before an action is required. If there
1434 * are no pending actions, wait forever for a command.
1436 dwSleepTime
= min(dwNextFeedTime
, dwNextNotifyTime
);
1437 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime
, dwNextFeedTime
, dwNextNotifyTime
);
1438 WAIT_OMR(&wwo
->msgRing
, dwSleepTime
);
1439 wodPlayer_ProcessMessages(wwo
);
1440 if (wwo
->state
== WINE_WS_PLAYING
) {
1441 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1442 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1443 if (dwNextFeedTime
== INFINITE
) {
1444 /* FeedDSP ran out of data, but before flushing, */
1445 /* check that a notification didn't give us more */
1446 wodPlayer_ProcessMessages(wwo
);
1447 if (!wwo
->lpPlayPtr
) {
1448 TRACE("flushing\n");
1449 ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_SYNC
, 0);
1450 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
;
1451 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1453 TRACE("recovering\n");
1454 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1458 dwNextFeedTime
= dwNextNotifyTime
= INFINITE
;
1463 /**************************************************************************
1464 * wodGetDevCaps [internal]
1466 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSA lpCaps
, DWORD dwSize
)
1468 TRACE("(%u, %p, %lu);\n", wDevID
, lpCaps
, dwSize
);
1470 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
1472 if (wDevID
>= numOutDev
) {
1473 TRACE("numOutDev reached !\n");
1474 return MMSYSERR_BADDEVICEID
;
1477 memcpy(lpCaps
, &WOutDev
[wDevID
].ossdev
->out_caps
, min(dwSize
, sizeof(*lpCaps
)));
1478 return MMSYSERR_NOERROR
;
1481 /**************************************************************************
1482 * wodOpen [internal]
1484 static DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1488 audio_buf_info info
;
1491 TRACE("(%u, %p[cb=%08lx], %08lX);\n", wDevID
, lpDesc
, lpDesc
->dwCallback
, dwFlags
);
1492 if (lpDesc
== NULL
) {
1493 WARN("Invalid Parameter !\n");
1494 return MMSYSERR_INVALPARAM
;
1496 if (wDevID
>= numOutDev
) {
1497 TRACE("MAX_WAVOUTDRV reached !\n");
1498 return MMSYSERR_BADDEVICEID
;
1501 /* only PCM format is supported so far... */
1502 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
1503 lpDesc
->lpFormat
->nChannels
== 0 ||
1504 lpDesc
->lpFormat
->nSamplesPerSec
== 0) {
1505 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1506 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1507 lpDesc
->lpFormat
->nSamplesPerSec
);
1508 return WAVERR_BADFORMAT
;
1511 if (dwFlags
& WAVE_FORMAT_QUERY
) {
1512 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1513 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1514 lpDesc
->lpFormat
->nSamplesPerSec
);
1515 return MMSYSERR_NOERROR
;
1518 wwo
= &WOutDev
[wDevID
];
1520 if ((dwFlags
& WAVE_DIRECTSOUND
) &&
1521 !(wwo
->ossdev
->out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
))
1522 /* not supported, ignore it */
1523 dwFlags
&= ~WAVE_DIRECTSOUND
;
1525 if (dwFlags
& WAVE_DIRECTSOUND
) {
1526 if (wwo
->ossdev
->out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
1527 /* we have realtime DirectSound, fragments just waste our time,
1528 * but a large buffer is good, so choose 64KB (32 * 2^11) */
1529 audio_fragment
= 0x0020000B;
1531 /* to approximate realtime, we must use small fragments,
1532 * let's try to fragment the above 64KB (256 * 2^8) */
1533 audio_fragment
= 0x01000008;
1535 /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
1536 * thus leading to 46ms per fragment, and a turnaround time of 185ms
1538 /* 16 fragments max, 2^10=1024 bytes per fragment */
1539 audio_fragment
= 0x000F000A;
1541 if (wwo
->state
!= WINE_WS_CLOSED
) return MMSYSERR_ALLOCATED
;
1542 /* we want to be able to mmap() the device, which means it must be opened readable,
1543 * otherwise mmap() will fail (at least under Linux) */
1544 ret
= OSS_OpenDevice(wwo
->ossdev
,
1545 (dwFlags
& WAVE_DIRECTSOUND
) ? O_RDWR
: O_WRONLY
,
1547 (dwFlags
& WAVE_DIRECTSOUND
) ? 0 : 1,
1548 lpDesc
->lpFormat
->nSamplesPerSec
,
1549 (lpDesc
->lpFormat
->nChannels
> 1) ? 1 : 0,
1550 (lpDesc
->lpFormat
->wBitsPerSample
== 16)
1551 ? AFMT_S16_LE
: AFMT_U8
);
1552 if ((ret
==MMSYSERR_NOERROR
) && (dwFlags
& WAVE_DIRECTSOUND
)) {
1553 lpDesc
->lpFormat
->nSamplesPerSec
=wwo
->ossdev
->sample_rate
;
1554 lpDesc
->lpFormat
->nChannels
=(wwo
->ossdev
->stereo
? 2 : 1);
1555 lpDesc
->lpFormat
->wBitsPerSample
=(wwo
->ossdev
->format
== AFMT_U8
? 8 : 16);
1556 lpDesc
->lpFormat
->nBlockAlign
=lpDesc
->lpFormat
->nChannels
*lpDesc
->lpFormat
->wBitsPerSample
/8;
1557 lpDesc
->lpFormat
->nAvgBytesPerSec
=lpDesc
->lpFormat
->nSamplesPerSec
*lpDesc
->lpFormat
->nBlockAlign
;
1558 TRACE("OSS_OpenDevice returned this format: %ldx%dx%d\n",
1559 lpDesc
->lpFormat
->nSamplesPerSec
,
1560 lpDesc
->lpFormat
->wBitsPerSample
,
1561 lpDesc
->lpFormat
->nChannels
);
1563 if (ret
!= 0) return ret
;
1564 wwo
->state
= WINE_WS_STOPPED
;
1566 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1568 memcpy(&wwo
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
1569 memcpy(&wwo
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
1571 if (wwo
->format
.wBitsPerSample
== 0) {
1572 WARN("Resetting zeroed wBitsPerSample\n");
1573 wwo
->format
.wBitsPerSample
= 8 *
1574 (wwo
->format
.wf
.nAvgBytesPerSec
/
1575 wwo
->format
.wf
.nSamplesPerSec
) /
1576 wwo
->format
.wf
.nChannels
;
1578 /* Read output space info for future reference */
1579 if (ioctl(wwo
->ossdev
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
1580 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", wwo
->ossdev
->dev_name
, strerror(errno
));
1581 OSS_CloseDevice(wwo
->ossdev
);
1582 wwo
->state
= WINE_WS_CLOSED
;
1583 return MMSYSERR_NOTENABLED
;
1586 /* Check that fragsize is correct per our settings above */
1587 if ((info
.fragsize
> 1024) && (LOWORD(audio_fragment
) <= 10)) {
1588 /* we've tried to set 1K fragments or less, but it didn't work */
1589 ERR("fragment size set failed, size is now %d\n", info
.fragsize
);
1590 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
1591 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
1594 /* Remember fragsize and total buffer size for future use */
1595 wwo
->dwFragmentSize
= info
.fragsize
;
1596 wwo
->dwBufferSize
= info
.fragstotal
* info
.fragsize
;
1597 wwo
->dwPlayedTotal
= 0;
1598 wwo
->dwWrittenTotal
= 0;
1599 wwo
->bNeedPost
= TRUE
;
1601 OSS_InitRingMessage(&wwo
->msgRing
);
1603 wwo
->hStartUpEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
1604 wwo
->hThread
= CreateThread(NULL
, 0, wodPlayer
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwo
->dwThreadID
));
1605 WaitForSingleObject(wwo
->hStartUpEvent
, INFINITE
);
1606 CloseHandle(wwo
->hStartUpEvent
);
1607 wwo
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
1609 TRACE("fd=%d fragmentSize=%ld\n",
1610 wwo
->ossdev
->fd
, wwo
->dwFragmentSize
);
1611 if (wwo
->dwFragmentSize
% wwo
->format
.wf
.nBlockAlign
)
1612 ERR("Fragment doesn't contain an integral number of data blocks\n");
1614 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1615 wwo
->format
.wBitsPerSample
, wwo
->format
.wf
.nAvgBytesPerSec
,
1616 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1617 wwo
->format
.wf
.nBlockAlign
);
1619 return wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
1622 /**************************************************************************
1623 * wodClose [internal]
1625 static DWORD
wodClose(WORD wDevID
)
1627 DWORD ret
= MMSYSERR_NOERROR
;
1630 TRACE("(%u);\n", wDevID
);
1632 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1633 WARN("bad device ID !\n");
1634 return MMSYSERR_BADDEVICEID
;
1637 wwo
= &WOutDev
[wDevID
];
1638 if (wwo
->lpQueuePtr
) {
1639 WARN("buffers still playing !\n");
1640 ret
= WAVERR_STILLPLAYING
;
1642 if (wwo
->hThread
!= INVALID_HANDLE_VALUE
) {
1643 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
1646 munmap(wwo
->mapping
, wwo
->maplen
);
1647 wwo
->mapping
= NULL
;
1650 OSS_DestroyRingMessage(&wwo
->msgRing
);
1652 OSS_CloseDevice(wwo
->ossdev
);
1653 wwo
->state
= WINE_WS_CLOSED
;
1654 wwo
->dwFragmentSize
= 0;
1655 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
1660 /**************************************************************************
1661 * wodWrite [internal]
1664 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1666 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1668 /* first, do the sanity checks... */
1669 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1670 WARN("bad dev ID !\n");
1671 return MMSYSERR_BADDEVICEID
;
1674 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
1675 return WAVERR_UNPREPARED
;
1677 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1678 return WAVERR_STILLPLAYING
;
1680 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1681 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
1682 lpWaveHdr
->lpNext
= 0;
1684 if ((lpWaveHdr
->dwBufferLength
& (WOutDev
[wDevID
].format
.wf
.nBlockAlign
- 1)) != 0)
1686 WARN("WaveHdr length isn't a multiple of the PCM block size: %ld %% %d\n",lpWaveHdr
->dwBufferLength
,WOutDev
[wDevID
].format
.wf
.nBlockAlign
);
1687 lpWaveHdr
->dwBufferLength
&= ~(WOutDev
[wDevID
].format
.wf
.nBlockAlign
- 1);
1690 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
1692 return MMSYSERR_NOERROR
;
1695 /**************************************************************************
1696 * wodPrepare [internal]
1698 static DWORD
wodPrepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1700 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1702 if (wDevID
>= numOutDev
) {
1703 WARN("bad device ID !\n");
1704 return MMSYSERR_BADDEVICEID
;
1707 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1708 return WAVERR_STILLPLAYING
;
1710 lpWaveHdr
->dwFlags
|= WHDR_PREPARED
;
1711 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1712 return MMSYSERR_NOERROR
;
1715 /**************************************************************************
1716 * wodUnprepare [internal]
1718 static DWORD
wodUnprepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1720 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1722 if (wDevID
>= numOutDev
) {
1723 WARN("bad device ID !\n");
1724 return MMSYSERR_BADDEVICEID
;
1727 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1728 return WAVERR_STILLPLAYING
;
1730 lpWaveHdr
->dwFlags
&= ~WHDR_PREPARED
;
1731 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1733 return MMSYSERR_NOERROR
;
1736 /**************************************************************************
1737 * wodPause [internal]
1739 static DWORD
wodPause(WORD wDevID
)
1741 TRACE("(%u);!\n", wDevID
);
1743 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1744 WARN("bad device ID !\n");
1745 return MMSYSERR_BADDEVICEID
;
1748 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_PAUSING
, 0, TRUE
);
1750 return MMSYSERR_NOERROR
;
1753 /**************************************************************************
1754 * wodRestart [internal]
1756 static DWORD
wodRestart(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_RESTARTING
, 0, TRUE
);
1767 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1768 /* FIXME: Myst crashes with this ... hmm -MM
1769 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1772 return MMSYSERR_NOERROR
;
1775 /**************************************************************************
1776 * wodReset [internal]
1778 static DWORD
wodReset(WORD wDevID
)
1780 TRACE("(%u);\n", wDevID
);
1782 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1783 WARN("bad device ID !\n");
1784 return MMSYSERR_BADDEVICEID
;
1787 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
1789 return MMSYSERR_NOERROR
;
1792 /**************************************************************************
1793 * wodGetPosition [internal]
1795 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
1801 TRACE("(%u, %p, %lu);\n", wDevID
, lpTime
, uSize
);
1803 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1804 WARN("bad device ID !\n");
1805 return MMSYSERR_BADDEVICEID
;
1808 if (lpTime
== NULL
) return MMSYSERR_INVALPARAM
;
1810 wwo
= &WOutDev
[wDevID
];
1811 #ifdef EXACT_WODPOSITION
1812 OSS_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
1814 val
= wwo
->dwPlayedTotal
;
1816 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1817 lpTime
->wType
, wwo
->format
.wBitsPerSample
,
1818 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1819 wwo
->format
.wf
.nAvgBytesPerSec
);
1820 TRACE("dwPlayedTotal=%lu\n", val
);
1822 switch (lpTime
->wType
) {
1825 TRACE("TIME_BYTES=%lu\n", lpTime
->u
.cb
);
1828 lpTime
->u
.sample
= val
* 8 / wwo
->format
.wBitsPerSample
/wwo
->format
.wf
.nChannels
;
1829 TRACE("TIME_SAMPLES=%lu\n", lpTime
->u
.sample
);
1832 time
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1833 lpTime
->u
.smpte
.hour
= time
/ (60 * 60 * 1000);
1834 time
-= lpTime
->u
.smpte
.hour
* (60 * 60 * 1000);
1835 lpTime
->u
.smpte
.min
= time
/ (60 * 1000);
1836 time
-= lpTime
->u
.smpte
.min
* (60 * 1000);
1837 lpTime
->u
.smpte
.sec
= time
/ 1000;
1838 time
-= lpTime
->u
.smpte
.sec
* 1000;
1839 lpTime
->u
.smpte
.frame
= time
* 30 / 1000;
1840 lpTime
->u
.smpte
.fps
= 30;
1841 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1842 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
1843 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
1846 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime
->wType
);
1847 lpTime
->wType
= TIME_MS
;
1849 lpTime
->u
.ms
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1850 TRACE("TIME_MS=%lu\n", lpTime
->u
.ms
);
1853 return MMSYSERR_NOERROR
;
1856 /**************************************************************************
1857 * wodBreakLoop [internal]
1859 static DWORD
wodBreakLoop(WORD wDevID
)
1861 TRACE("(%u);\n", wDevID
);
1863 if (wDevID
>= numOutDev
|| WOutDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1864 WARN("bad device ID !\n");
1865 return MMSYSERR_BADDEVICEID
;
1867 OSS_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_BREAKLOOP
, 0, TRUE
);
1868 return MMSYSERR_NOERROR
;
1871 /**************************************************************************
1872 * wodGetVolume [internal]
1874 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
1880 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
1882 if (lpdwVol
== NULL
)
1883 return MMSYSERR_NOTENABLED
;
1884 if (wDevID
>= numOutDev
)
1885 return MMSYSERR_INVALPARAM
;
1887 if ((mixer
= open(WOutDev
[wDevID
].ossdev
->mixer_name
, O_RDONLY
|O_NDELAY
)) < 0) {
1888 WARN("mixer device not available !\n");
1889 return MMSYSERR_NOTENABLED
;
1891 if (ioctl(mixer
, SOUND_MIXER_READ_PCM
, &volume
) == -1) {
1892 WARN("ioctl(%s, SOUND_MIXER_READ_PCM) failed (%s)n", WOutDev
[wDevID
].ossdev
->mixer_name
, strerror(errno
));
1893 return MMSYSERR_NOTENABLED
;
1896 left
= LOBYTE(volume
);
1897 right
= HIBYTE(volume
);
1898 TRACE("left=%ld right=%ld !\n", left
, right
);
1899 *lpdwVol
= ((left
* 0xFFFFl
) / 100) + (((right
* 0xFFFFl
) / 100) << 16);
1900 return MMSYSERR_NOERROR
;
1903 /**************************************************************************
1904 * wodSetVolume [internal]
1906 static DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
1912 TRACE("(%u, %08lX);\n", wDevID
, dwParam
);
1914 left
= (LOWORD(dwParam
) * 100) / 0xFFFFl
;
1915 right
= (HIWORD(dwParam
) * 100) / 0xFFFFl
;
1916 volume
= left
+ (right
<< 8);
1918 if (wDevID
>= numOutDev
) {
1919 WARN("invalid parameter: wDevID > %d\n", numOutDev
);
1920 return MMSYSERR_INVALPARAM
;
1923 if ((mixer
= open(WOutDev
[wDevID
].ossdev
->mixer_name
, O_WRONLY
|O_NDELAY
)) < 0) {
1924 WARN("mixer device not available !\n");
1925 return MMSYSERR_NOTENABLED
;
1927 if (ioctl(mixer
, SOUND_MIXER_WRITE_PCM
, &volume
) == -1) {
1928 WARN("ioctl(%s, SOUND_MIXER_WRITE_PCM) failed (%s)\n", WOutDev
[wDevID
].ossdev
->mixer_name
, strerror(errno
));
1929 return MMSYSERR_NOTENABLED
;
1931 TRACE("volume=%04x\n", (unsigned)volume
);
1934 return MMSYSERR_NOERROR
;
1937 /**************************************************************************
1938 * wodMessage (WINEOSS.7)
1940 DWORD WINAPI
OSS_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1941 DWORD dwParam1
, DWORD dwParam2
)
1943 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1944 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1951 /* FIXME: Pretend this is supported */
1953 case WODM_OPEN
: return wodOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
1954 case WODM_CLOSE
: return wodClose (wDevID
);
1955 case WODM_WRITE
: return wodWrite (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1956 case WODM_PAUSE
: return wodPause (wDevID
);
1957 case WODM_GETPOS
: return wodGetPosition (wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
1958 case WODM_BREAKLOOP
: return wodBreakLoop (wDevID
);
1959 case WODM_PREPARE
: return wodPrepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1960 case WODM_UNPREPARE
: return wodUnprepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1961 case WODM_GETDEVCAPS
: return wodGetDevCaps (wDevID
, (LPWAVEOUTCAPSA
)dwParam1
, dwParam2
);
1962 case WODM_GETNUMDEVS
: return numOutDev
;
1963 case WODM_GETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1964 case WODM_SETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1965 case WODM_GETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1966 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1967 case WODM_GETVOLUME
: return wodGetVolume (wDevID
, (LPDWORD
)dwParam1
);
1968 case WODM_SETVOLUME
: return wodSetVolume (wDevID
, dwParam1
);
1969 case WODM_RESTART
: return wodRestart (wDevID
);
1970 case WODM_RESET
: return wodReset (wDevID
);
1972 case DRV_QUERYDEVICEINTERFACESIZE
: return wdDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
1973 case DRV_QUERYDEVICEINTERFACE
: return wdDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
1974 case DRV_QUERYDSOUNDIFACE
: return wodDsCreate (wDevID
, (PIDSDRIVER
*)dwParam1
);
1975 case DRV_QUERYDSOUNDDESC
: return wodDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
1976 case DRV_QUERYDSOUNDGUID
: return wodDsGuid (wDevID
, (LPGUID
)dwParam1
);
1978 FIXME("unknown message %d!\n", wMsg
);
1980 return MMSYSERR_NOTSUPPORTED
;
1983 /*======================================================================*
1984 * Low level DSOUND implementation *
1985 *======================================================================*/
1987 typedef struct IDsDriverImpl IDsDriverImpl
;
1988 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
1990 struct IDsDriverImpl
1992 /* IUnknown fields */
1993 ICOM_VFIELD(IDsDriver
);
1995 /* IDsDriverImpl fields */
1997 IDsDriverBufferImpl
*primary
;
2000 struct IDsDriverBufferImpl
2002 /* IUnknown fields */
2003 ICOM_VFIELD(IDsDriverBuffer
);
2005 /* IDsDriverBufferImpl fields */
2011 static HRESULT
DSDB_MapPrimary(IDsDriverBufferImpl
*dsdb
)
2013 WINE_WAVEOUT
*wwo
= &(WOutDev
[dsdb
->drv
->wDevID
]);
2014 TRACE("(%p)\n",dsdb
);
2015 if (!wwo
->mapping
) {
2016 wwo
->mapping
= mmap(NULL
, wwo
->maplen
, PROT_WRITE
, MAP_SHARED
,
2017 wwo
->ossdev
->fd
, 0);
2018 if (wwo
->mapping
== (LPBYTE
)-1) {
2019 TRACE("(%p): Could not map sound device for direct access (%s)\n", dsdb
, strerror(errno
));
2020 return DSERR_GENERIC
;
2022 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb
, wwo
->mapping
, wwo
->maplen
);
2024 /* for some reason, es1371 and sblive! sometimes have junk in here.
2025 * clear it, or we get junk noise */
2026 /* some libc implementations are buggy: their memset reads from the buffer...
2027 * to work around it, we have to zero the block by hand. We don't do the expected:
2028 * memset(wwo->mapping,0, wwo->maplen);
2031 unsigned char* p1
= wwo
->mapping
;
2032 unsigned len
= wwo
->maplen
;
2033 unsigned char silence
= (dsdb
->wfx
.wBitsPerSample
== 8) ? 128 : 0;
2034 unsigned long ulsilence
= (dsdb
->wfx
.wBitsPerSample
== 8) ? 0x80808080 : 0;
2036 if (len
>= 16) /* so we can have at least a 4 long area to store... */
2038 /* the mmap:ed value is (at least) dword aligned
2039 * so, start filling the complete unsigned long:s
2042 unsigned long* p4
= (unsigned long*)p1
;
2044 while (b
--) *p4
++ = ulsilence
;
2045 /* prepare for filling the rest */
2047 p1
= (unsigned char*)p4
;
2049 /* in all cases, fill the remaining bytes */
2050 while (len
-- != 0) *p1
++ = silence
;
2056 static HRESULT
DSDB_UnmapPrimary(IDsDriverBufferImpl
*dsdb
)
2058 WINE_WAVEOUT
*wwo
= &(WOutDev
[dsdb
->drv
->wDevID
]);
2059 TRACE("(%p)\n",dsdb
);
2061 if (munmap(wwo
->mapping
, wwo
->maplen
) < 0) {
2062 ERR("(%p): Could not unmap sound device (%s)\n", dsdb
, strerror(errno
));
2063 return DSERR_GENERIC
;
2065 wwo
->mapping
= NULL
;
2066 TRACE("(%p): sound device unmapped\n", dsdb
);
2071 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
2073 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2074 TRACE("(%p,%s,%p)\n",iface
,debugstr_guid(riid
),*ppobj
);
2076 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
2077 IsEqualGUID(riid
, &IID_IDsDriverBuffer
) ) {
2078 IDsDriverBuffer_AddRef(iface
);
2079 *ppobj
= (LPVOID
)This
;
2083 if ( IsEqualGUID( &IID_IDsDriverNotify
, riid
) ) {
2084 FIXME("IDsDriverNotify not implemented\n");
2086 return E_NOINTERFACE
;
2089 if ( IsEqualGUID( &IID_IDsDriverPropertySet
, riid
) ) {
2090 FIXME("IDsDriverPropertySet not implemented\n");
2092 return E_NOINTERFACE
;
2095 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
2099 return E_NOINTERFACE
;
2102 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
2104 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2105 TRACE("(%p)\n",This
);
2107 TRACE("ref=%ld\n",This
->ref
);
2111 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
2113 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2114 TRACE("(%p)\n",This
);
2116 TRACE("ref=%ld\n",This
->ref
);
2119 if (This
== This
->drv
->primary
)
2120 This
->drv
->primary
= NULL
;
2121 DSDB_UnmapPrimary(This
);
2122 HeapFree(GetProcessHeap(),0,This
);
2127 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
2128 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
2129 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
2130 DWORD dwWritePosition
,DWORD dwWriteLen
,
2133 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2134 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
2135 * and that we don't support secondary buffers, this method will never be called */
2136 TRACE("(%p): stub\n",iface
);
2137 return DSERR_UNSUPPORTED
;
2140 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
2141 LPVOID pvAudio1
,DWORD dwLen1
,
2142 LPVOID pvAudio2
,DWORD dwLen2
)
2144 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2145 TRACE("(%p): stub\n",iface
);
2146 return DSERR_UNSUPPORTED
;
2149 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
,
2150 LPWAVEFORMATEX pwfx
)
2152 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2154 TRACE("(%p,%p)\n",iface
,pwfx
);
2155 /* On our request (GetDriverDesc flags), DirectSound has by now used
2156 * waveOutClose/waveOutOpen to set the format...
2157 * unfortunately, this means our mmap() is now gone...
2158 * so we need to somehow signal to our DirectSound implementation
2159 * that it should completely recreate this HW buffer...
2160 * this unexpected error code should do the trick... */
2161 return DSERR_BUFFERLOST
;
2164 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
2166 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2167 TRACE("(%p,%ld): stub\n",iface
,dwFreq
);
2168 return DSERR_UNSUPPORTED
;
2171 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
2174 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2175 TRACE("(%p,%p)\n",This
,pVolPan
);
2177 vol
= pVolPan
->dwTotalLeftAmpFactor
| (pVolPan
->dwTotalRightAmpFactor
<< 16);
2179 if (wodSetVolume(This
->drv
->wDevID
, vol
) != MMSYSERR_NOERROR
) {
2180 WARN("wodSetVolume failed\n");
2181 return DSERR_INVALIDPARAM
;
2187 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
2189 /* ICOM_THIS(IDsDriverImpl,iface); */
2190 TRACE("(%p,%ld): stub\n",iface
,dwNewPos
);
2191 return DSERR_UNSUPPORTED
;
2194 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
2195 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
2197 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2201 TRACE("(%p)\n",iface
);
2202 if (WOutDev
[This
->drv
->wDevID
].state
== WINE_WS_CLOSED
) {
2203 ERR("device not open, but accessing?\n");
2204 return DSERR_UNINITIALIZED
;
2206 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
2207 ERR("ioctl(%s, SNDCTL_DSP_GETOPTR) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2208 return DSERR_GENERIC
;
2210 ptr
= info
.ptr
& ~3; /* align the pointer, just in case */
2211 if (lpdwPlay
) *lpdwPlay
= ptr
;
2213 /* add some safety margin (not strictly necessary, but...) */
2214 if (WOutDev
[This
->drv
->wDevID
].ossdev
->out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
2215 *lpdwWrite
= ptr
+ 32;
2217 *lpdwWrite
= ptr
+ WOutDev
[This
->drv
->wDevID
].dwFragmentSize
;
2218 while (*lpdwWrite
> This
->buflen
)
2219 *lpdwWrite
-= This
->buflen
;
2221 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay
?*lpdwPlay
:0, lpdwWrite
?*lpdwWrite
:0);
2225 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
2227 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2229 TRACE("(%p,%lx,%lx,%lx)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
2230 WOutDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= TRUE
;
2231 enable
= getEnables(WOutDev
[This
->drv
->wDevID
].ossdev
);
2232 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2233 if (errno
== EINVAL
) {
2234 /* Don't give up yet. OSS trigger support is inconsistent. */
2235 if (WOutDev
[This
->drv
->wDevID
].ossdev
->open_count
== 1) {
2236 /* try the opposite input enable */
2237 if (WOutDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
== FALSE
)
2238 WOutDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= TRUE
;
2240 WOutDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= FALSE
;
2242 enable
= getEnables(WOutDev
[This
->drv
->wDevID
].ossdev
);
2243 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) >= 0)
2247 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2248 WOutDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
2249 return DSERR_GENERIC
;
2254 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
2256 ICOM_THIS(IDsDriverBufferImpl
,iface
);
2258 TRACE("(%p)\n",iface
);
2259 /* no more playing */
2260 WOutDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
2261 enable
= getEnables(WOutDev
[This
->drv
->wDevID
].ossdev
);
2262 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2263 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2264 return DSERR_GENERIC
;
2267 /* the play position must be reset to the beginning of the buffer */
2268 if (ioctl(WOutDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_RESET
, 0) < 0) {
2269 ERR("ioctl(%s, SNDCTL_DSP_RESET) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2270 return DSERR_GENERIC
;
2273 /* Most OSS drivers just can't stop the playback without closing the device...
2274 * so we need to somehow signal to our DirectSound implementation
2275 * that it should completely recreate this HW buffer...
2276 * this unexpected error code should do the trick... */
2277 /* FIXME: ...unless we are doing full duplex, then its not nice to close the device */
2278 if (WOutDev
[This
->drv
->wDevID
].ossdev
->open_count
== 1)
2279 return DSERR_BUFFERLOST
;
2284 static ICOM_VTABLE(IDsDriverBuffer
) dsdbvt
=
2286 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2287 IDsDriverBufferImpl_QueryInterface
,
2288 IDsDriverBufferImpl_AddRef
,
2289 IDsDriverBufferImpl_Release
,
2290 IDsDriverBufferImpl_Lock
,
2291 IDsDriverBufferImpl_Unlock
,
2292 IDsDriverBufferImpl_SetFormat
,
2293 IDsDriverBufferImpl_SetFrequency
,
2294 IDsDriverBufferImpl_SetVolumePan
,
2295 IDsDriverBufferImpl_SetPosition
,
2296 IDsDriverBufferImpl_GetPosition
,
2297 IDsDriverBufferImpl_Play
,
2298 IDsDriverBufferImpl_Stop
2301 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
2303 ICOM_THIS(IDsDriverImpl
,iface
);
2304 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
2306 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
2307 IsEqualGUID(riid
, &IID_IDsDriver
) ) {
2308 IDsDriver_AddRef(iface
);
2309 *ppobj
= (LPVOID
)This
;
2313 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
2317 return E_NOINTERFACE
;
2320 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
2322 ICOM_THIS(IDsDriverImpl
,iface
);
2323 TRACE("(%p)\n",This
);
2325 TRACE("ref=%ld\n",This
->ref
);
2329 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
2331 ICOM_THIS(IDsDriverImpl
,iface
);
2332 TRACE("(%p)\n",This
);
2334 TRACE("ref=%ld\n",This
->ref
);
2337 HeapFree(GetProcessHeap(),0,This
);
2342 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
, PDSDRIVERDESC pDesc
)
2344 ICOM_THIS(IDsDriverImpl
,iface
);
2345 TRACE("(%p,%p)\n",iface
,pDesc
);
2347 /* copy version from driver */
2348 memcpy(pDesc
, &(WOutDev
[This
->wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
2350 pDesc
->dwFlags
|= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
|
2351 DSDDESC_USESYSTEMMEMORY
| DSDDESC_DONTNEEDPRIMARYLOCK
;
2352 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
2354 pDesc
->wReserved
= 0;
2355 pDesc
->ulDeviceNum
= This
->wDevID
;
2356 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
2357 pDesc
->pvDirectDrawHeap
= NULL
;
2358 pDesc
->dwMemStartAddress
= 0;
2359 pDesc
->dwMemEndAddress
= 0;
2360 pDesc
->dwMemAllocExtra
= 0;
2361 pDesc
->pvReserved1
= NULL
;
2362 pDesc
->pvReserved2
= NULL
;
2366 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
2368 ICOM_THIS(IDsDriverImpl
,iface
);
2370 TRACE("(%p)\n",iface
);
2372 /* make sure the card doesn't start playing before we want it to */
2373 WOutDev
[This
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
2374 enable
= getEnables(WOutDev
[This
->wDevID
].ossdev
);
2375 if (ioctl(WOutDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2376 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2377 return DSERR_GENERIC
;
2382 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
2384 ICOM_THIS(IDsDriverImpl
,iface
);
2385 TRACE("(%p)\n",iface
);
2386 if (This
->primary
) {
2387 ERR("problem with DirectSound: primary not released\n");
2388 return DSERR_GENERIC
;
2393 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
2395 ICOM_THIS(IDsDriverImpl
,iface
);
2396 TRACE("(%p,%p)\n",iface
,pCaps
);
2397 memcpy(pCaps
, &(WOutDev
[This
->wDevID
].ossdev
->ds_caps
), sizeof(DSDRIVERCAPS
));
2401 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
2402 LPWAVEFORMATEX pwfx
,
2404 DWORD dwCardAddress
,
2405 LPDWORD pdwcbBufferSize
,
2409 ICOM_THIS(IDsDriverImpl
,iface
);
2410 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
2412 audio_buf_info info
;
2414 TRACE("(%p,%p,%lx,%lx)\n",iface
,pwfx
,dwFlags
,dwCardAddress
);
2416 /* we only support primary buffers */
2417 if (!(dwFlags
& DSBCAPS_PRIMARYBUFFER
))
2418 return DSERR_UNSUPPORTED
;
2420 return DSERR_ALLOCATED
;
2421 if (dwFlags
& (DSBCAPS_CTRLFREQUENCY
| DSBCAPS_CTRLPAN
))
2422 return DSERR_CONTROLUNAVAIL
;
2424 *ippdsdb
= (IDsDriverBufferImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsDriverBufferImpl
));
2425 if (*ippdsdb
== NULL
)
2426 return DSERR_OUTOFMEMORY
;
2427 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
2428 (*ippdsdb
)->ref
= 1;
2429 (*ippdsdb
)->drv
= This
;
2430 (*ippdsdb
)->wfx
= *pwfx
;
2432 /* check how big the DMA buffer is now */
2433 if (ioctl(WOutDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
2434 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n", WOutDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2435 HeapFree(GetProcessHeap(),0,*ippdsdb
);
2437 return DSERR_GENERIC
;
2439 WOutDev
[This
->wDevID
].maplen
= (*ippdsdb
)->buflen
= info
.fragstotal
* info
.fragsize
;
2441 /* map the DMA buffer */
2442 err
= DSDB_MapPrimary(*ippdsdb
);
2444 HeapFree(GetProcessHeap(),0,*ippdsdb
);
2449 /* primary buffer is ready to go */
2450 *pdwcbBufferSize
= WOutDev
[This
->wDevID
].maplen
;
2451 *ppbBuffer
= WOutDev
[This
->wDevID
].mapping
;
2453 /* some drivers need some extra nudging after mapping */
2454 WOutDev
[This
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
2455 enable
= getEnables(WOutDev
[This
->wDevID
].ossdev
);
2456 if (ioctl(WOutDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2457 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
2458 return DSERR_GENERIC
;
2461 This
->primary
= *ippdsdb
;
2466 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
2467 PIDSDRIVERBUFFER pBuffer
,
2470 /* ICOM_THIS(IDsDriverImpl,iface); */
2471 TRACE("(%p,%p): stub\n",iface
,pBuffer
);
2472 return DSERR_INVALIDCALL
;
2475 static ICOM_VTABLE(IDsDriver
) dsdvt
=
2477 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2478 IDsDriverImpl_QueryInterface
,
2479 IDsDriverImpl_AddRef
,
2480 IDsDriverImpl_Release
,
2481 IDsDriverImpl_GetDriverDesc
,
2483 IDsDriverImpl_Close
,
2484 IDsDriverImpl_GetCaps
,
2485 IDsDriverImpl_CreateSoundBuffer
,
2486 IDsDriverImpl_DuplicateSoundBuffer
2489 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
2491 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
2492 TRACE("(%d,%p)\n",wDevID
,drv
);
2494 /* the HAL isn't much better than the HEL if we can't do mmap() */
2495 if (!(WOutDev
[wDevID
].ossdev
->out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
2496 ERR("DirectSound flag not set\n");
2497 MESSAGE("This sound card's driver does not support direct access\n");
2498 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2499 return MMSYSERR_NOTSUPPORTED
;
2502 *idrv
= (IDsDriverImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsDriverImpl
));
2504 return MMSYSERR_NOMEM
;
2505 (*idrv
)->lpVtbl
= &dsdvt
;
2508 (*idrv
)->wDevID
= wDevID
;
2509 (*idrv
)->primary
= NULL
;
2510 return MMSYSERR_NOERROR
;
2513 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
2515 TRACE("(%d,%p)\n",wDevID
,desc
);
2516 memcpy(desc
, &(WOutDev
[wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
2517 return MMSYSERR_NOERROR
;
2520 static DWORD
wodDsGuid(UINT wDevID
, LPGUID pGuid
)
2522 TRACE("(%d,%p)\n",wDevID
,pGuid
);
2523 memcpy(pGuid
, &(WOutDev
[wDevID
].ossdev
->ds_guid
), sizeof(GUID
));
2524 return MMSYSERR_NOERROR
;
2527 /*======================================================================*
2528 * Low level WAVE IN implementation *
2529 *======================================================================*/
2531 /**************************************************************************
2532 * widNotifyClient [internal]
2534 static DWORD
widNotifyClient(WINE_WAVEIN
* wwi
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
2536 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg
, dwParam1
, dwParam2
);
2542 if (wwi
->wFlags
!= DCB_NULL
&&
2543 !DriverCallback(wwi
->waveDesc
.dwCallback
, wwi
->wFlags
,
2544 (HDRVR
)wwi
->waveDesc
.hWave
, wMsg
,
2545 wwi
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
2546 WARN("can't notify client !\n");
2547 return MMSYSERR_ERROR
;
2551 FIXME("Unknown callback message %u\n", wMsg
);
2552 return MMSYSERR_INVALPARAM
;
2554 return MMSYSERR_NOERROR
;
2557 /**************************************************************************
2558 * widGetDevCaps [internal]
2560 static DWORD
widGetDevCaps(WORD wDevID
, LPWAVEINCAPSA lpCaps
, DWORD dwSize
)
2562 TRACE("(%u, %p, %lu);\n", wDevID
, lpCaps
, dwSize
);
2564 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
2566 if (wDevID
>= numInDev
) {
2567 TRACE("numOutDev reached !\n");
2568 return MMSYSERR_BADDEVICEID
;
2571 memcpy(lpCaps
, &WInDev
[wDevID
].ossdev
->in_caps
, min(dwSize
, sizeof(*lpCaps
)));
2572 return MMSYSERR_NOERROR
;
2575 /**************************************************************************
2576 * widRecorder [internal]
2578 static DWORD CALLBACK
widRecorder(LPVOID pmt
)
2580 WORD uDevID
= (DWORD
)pmt
;
2581 WINE_WAVEIN
* wwi
= (WINE_WAVEIN
*)&WInDev
[uDevID
];
2585 LPVOID buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, wwi
->dwFragmentSize
);
2586 char *pOffset
= buffer
;
2587 audio_buf_info info
;
2589 enum win_wm_message msg
;
2594 wwi
->state
= WINE_WS_STOPPED
;
2595 wwi
->dwTotalRecorded
= 0;
2596 wwi
->lpQueuePtr
= NULL
;
2598 SetEvent(wwi
->hStartUpEvent
);
2600 /* disable input so capture will begin when triggered */
2601 wwi
->ossdev
->bInputEnabled
= FALSE
;
2602 enable
= getEnables(wwi
->ossdev
);
2603 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0)
2604 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
->dev_name
, strerror(errno
));
2606 /* the soundblaster live needs a micro wake to get its recording started
2607 * (or GETISPACE will have 0 frags all the time)
2609 read(wwi
->ossdev
->fd
, &xs
, 4);
2611 /* make sleep time to be # of ms to output a fragment */
2612 dwSleepTime
= (wwi
->dwFragmentSize
* 1000) / wwi
->format
.wf
.nAvgBytesPerSec
;
2613 TRACE("sleeptime=%ld ms\n", dwSleepTime
);
2616 /* wait for dwSleepTime or an event in thread's queue */
2617 /* FIXME: could improve wait time depending on queue state,
2618 * ie, number of queued fragments
2621 if (wwi
->lpQueuePtr
!= NULL
&& wwi
->state
== WINE_WS_PLAYING
)
2623 lpWaveHdr
= wwi
->lpQueuePtr
;
2625 ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_GETISPACE
, &info
);
2626 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info
.fragments
, info
.fragsize
, info
.fragstotal
, info
.bytes
);
2628 /* read all the fragments accumulated so far */
2629 while ((info
.fragments
> 0) && (wwi
->lpQueuePtr
))
2633 if (lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
>= wwi
->dwFragmentSize
)
2635 /* directly read fragment in wavehdr */
2636 bytesRead
= read(wwi
->ossdev
->fd
,
2637 lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2638 wwi
->dwFragmentSize
);
2640 TRACE("bytesRead=%ld (direct)\n", bytesRead
);
2641 if (bytesRead
!= (DWORD
) -1)
2643 /* update number of bytes recorded in current buffer and by this device */
2644 lpWaveHdr
->dwBytesRecorded
+= bytesRead
;
2645 wwi
->dwTotalRecorded
+= bytesRead
;
2647 /* buffer is full. notify client */
2648 if (lpWaveHdr
->dwBytesRecorded
== lpWaveHdr
->dwBufferLength
)
2650 /* must copy the value of next waveHdr, because we have no idea of what
2651 * will be done with the content of lpWaveHdr in callback
2653 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2655 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2656 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2658 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2659 lpWaveHdr
= wwi
->lpQueuePtr
= lpNext
;
2665 /* read the fragment in a local buffer */
2666 bytesRead
= read(wwi
->ossdev
->fd
, buffer
, wwi
->dwFragmentSize
);
2669 TRACE("bytesRead=%ld (local)\n", bytesRead
);
2671 /* copy data in client buffers */
2672 while (bytesRead
!= (DWORD
) -1 && bytesRead
> 0)
2674 DWORD dwToCopy
= min (bytesRead
, lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
);
2676 memcpy(lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
2680 /* update number of bytes recorded in current buffer and by this device */
2681 lpWaveHdr
->dwBytesRecorded
+= dwToCopy
;
2682 wwi
->dwTotalRecorded
+= dwToCopy
;
2683 bytesRead
-= dwToCopy
;
2684 pOffset
+= dwToCopy
;
2686 /* client buffer is full. notify client */
2687 if (lpWaveHdr
->dwBytesRecorded
== lpWaveHdr
->dwBufferLength
)
2689 /* must copy the value of next waveHdr, because we have no idea of what
2690 * will be done with the content of lpWaveHdr in callback
2692 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
2693 TRACE("lpNext=%p\n", lpNext
);
2695 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2696 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2698 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2700 wwi
->lpQueuePtr
= lpWaveHdr
= lpNext
;
2701 if (!lpNext
&& bytesRead
) {
2702 /* before we give up, check for more header messages */
2703 while (OSS_PeekRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
))
2705 if (msg
== WINE_WM_HEADER
) {
2707 OSS_RetrieveRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
);
2708 hdr
= ((LPWAVEHDR
)param
);
2709 TRACE("msg = %s, hdr = %p, ev = %p\n", wodPlayerCmdString
[msg
- WM_USER
- 1], hdr
, ev
);
2711 if (lpWaveHdr
== 0) {
2712 /* new head of queue */
2713 wwi
->lpQueuePtr
= lpWaveHdr
= hdr
;
2715 /* insert buffer at the end of queue */
2717 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2724 if (lpWaveHdr
== 0) {
2725 /* no more buffer to copy data to, but we did read more.
2726 * what hasn't been copied will be dropped
2728 WARN("buffer under run! %lu bytes dropped.\n", bytesRead
);
2729 wwi
->lpQueuePtr
= NULL
;
2739 WAIT_OMR(&wwi
->msgRing
, dwSleepTime
);
2741 while (OSS_RetrieveRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
))
2743 TRACE("msg=%s param=0x%lx\n", wodPlayerCmdString
[msg
- WM_USER
- 1], param
);
2745 case WINE_WM_PAUSING
:
2746 wwi
->state
= WINE_WS_PAUSED
;
2747 /*FIXME("Device should stop recording\n");*/
2750 case WINE_WM_STARTING
:
2751 wwi
->state
= WINE_WS_PLAYING
;
2753 if (wwi
->ossdev
->bTriggerSupport
)
2755 /* start the recording */
2756 wwi
->ossdev
->bInputEnabled
= TRUE
;
2757 enable
= getEnables(wwi
->ossdev
);
2758 if (ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
2759 wwi
->ossdev
->bInputEnabled
= FALSE
;
2760 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", wwi
->ossdev
->dev_name
, strerror(errno
));
2765 unsigned char data
[4];
2766 /* read 4 bytes to start the recording */
2767 read(wwi
->ossdev
->fd
, data
, 4);
2772 case WINE_WM_HEADER
:
2773 lpWaveHdr
= (LPWAVEHDR
)param
;
2774 lpWaveHdr
->lpNext
= 0;
2776 /* insert buffer at the end of queue */
2779 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
2783 case WINE_WM_STOPPING
:
2784 case WINE_WM_RESETTING
:
2785 wwi
->state
= WINE_WS_STOPPED
;
2786 wwi
->dwTotalRecorded
= 0;
2787 /* return all buffers to the app */
2788 for (lpWaveHdr
= wwi
->lpQueuePtr
; lpWaveHdr
; lpWaveHdr
= lpWaveHdr
->lpNext
) {
2789 TRACE("reset %p %p\n", lpWaveHdr
, lpWaveHdr
->lpNext
);
2790 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2791 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2793 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2795 wwi
->lpQueuePtr
= NULL
;
2798 case WINE_WM_CLOSING
:
2800 wwi
->state
= WINE_WS_CLOSED
;
2802 HeapFree(GetProcessHeap(), 0, buffer
);
2804 /* shouldn't go here */
2806 FIXME("unknown message %d\n", msg
);
2812 /* just for not generating compilation warnings... should never be executed */
2817 /**************************************************************************
2818 * widOpen [internal]
2820 static DWORD
widOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
2827 TRACE("(%u, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
2828 if (lpDesc
== NULL
) {
2829 WARN("Invalid Parameter !\n");
2830 return MMSYSERR_INVALPARAM
;
2832 if (wDevID
>= numInDev
) return MMSYSERR_BADDEVICEID
;
2834 /* only PCM format is supported so far... */
2835 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
2836 lpDesc
->lpFormat
->nChannels
== 0 ||
2837 lpDesc
->lpFormat
->nSamplesPerSec
== 0) {
2838 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2839 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
2840 lpDesc
->lpFormat
->nSamplesPerSec
);
2841 return WAVERR_BADFORMAT
;
2844 if (dwFlags
& WAVE_FORMAT_QUERY
) {
2845 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2846 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
2847 lpDesc
->lpFormat
->nSamplesPerSec
);
2848 return MMSYSERR_NOERROR
;
2851 wwi
= &WInDev
[wDevID
];
2853 if (wwi
->state
!= WINE_WS_CLOSED
) return MMSYSERR_ALLOCATED
;
2855 if ((dwFlags
& WAVE_DIRECTSOUND
) &&
2856 !(wwi
->ossdev
->in_caps_support
& WAVECAPS_DIRECTSOUND
))
2857 /* not supported, ignore it */
2858 dwFlags
&= ~WAVE_DIRECTSOUND
;
2860 if (dwFlags
& WAVE_DIRECTSOUND
) {
2861 TRACE("has DirectSoundCapture driver\n");
2862 if (wwi
->ossdev
->in_caps_support
& WAVECAPS_SAMPLEACCURATE
)
2863 /* we have realtime DirectSound, fragments just waste our time,
2864 * but a large buffer is good, so choose 64KB (32 * 2^11) */
2865 audio_fragment
= 0x0020000B;
2867 /* to approximate realtime, we must use small fragments,
2868 * let's try to fragment the above 64KB (256 * 2^8) */
2869 audio_fragment
= 0x01000008;
2871 TRACE("doesn't have DirectSoundCapture driver\n");
2872 if (wwi
->ossdev
->open_count
> 0) {
2873 TRACE("Using output device audio_fragment\n");
2874 /* FIXME: This may not be optimal for capture but it allows us
2875 * to do hardware playback without hardware capture. */
2876 audio_fragment
= wwi
->ossdev
->audio_fragment
;
2878 /* This is actually hand tuned to work so that my SB Live:
2880 * - does not buffer too much
2881 * when sending with the Shoutcast winamp plugin
2883 /* 15 fragments max, 2^10 = 1024 bytes per fragment */
2884 audio_fragment
= 0x000F000A;
2888 TRACE("using %d %d byte fragments\n", audio_fragment
>> 16,
2889 1 << (audio_fragment
& 0xffff));
2891 ret
= OSS_OpenDevice(wwi
->ossdev
, O_RDONLY
, &audio_fragment
,
2893 lpDesc
->lpFormat
->nSamplesPerSec
,
2894 (lpDesc
->lpFormat
->nChannels
> 1) ? 1 : 0,
2895 (lpDesc
->lpFormat
->wBitsPerSample
== 16)
2896 ? AFMT_S16_LE
: AFMT_U8
);
2897 if (ret
!= 0) return ret
;
2898 wwi
->state
= WINE_WS_STOPPED
;
2900 if (wwi
->lpQueuePtr
) {
2901 WARN("Should have an empty queue (%p)\n", wwi
->lpQueuePtr
);
2902 wwi
->lpQueuePtr
= NULL
;
2904 wwi
->dwTotalRecorded
= 0;
2905 wwi
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2907 memcpy(&wwi
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
2908 memcpy(&wwi
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
2910 if (wwi
->format
.wBitsPerSample
== 0) {
2911 WARN("Resetting zeroed wBitsPerSample\n");
2912 wwi
->format
.wBitsPerSample
= 8 *
2913 (wwi
->format
.wf
.nAvgBytesPerSec
/
2914 wwi
->format
.wf
.nSamplesPerSec
) /
2915 wwi
->format
.wf
.nChannels
;
2918 ioctl(wwi
->ossdev
->fd
, SNDCTL_DSP_GETBLKSIZE
, &fragment_size
);
2919 if (fragment_size
== -1) {
2920 WARN("ioctl(%s, SNDCTL_DSP_GETBLKSIZE) failed (%s)\n",
2921 wwi
->ossdev
->dev_name
, strerror(errno
));
2922 OSS_CloseDevice(wwi
->ossdev
);
2923 wwi
->state
= WINE_WS_CLOSED
;
2924 return MMSYSERR_NOTENABLED
;
2926 wwi
->dwFragmentSize
= fragment_size
;
2928 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2929 wwi
->format
.wBitsPerSample
, wwi
->format
.wf
.nAvgBytesPerSec
,
2930 wwi
->format
.wf
.nSamplesPerSec
, wwi
->format
.wf
.nChannels
,
2931 wwi
->format
.wf
.nBlockAlign
);
2933 OSS_InitRingMessage(&wwi
->msgRing
);
2935 wwi
->hStartUpEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
2936 wwi
->hThread
= CreateThread(NULL
, 0, widRecorder
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwi
->dwThreadID
));
2937 WaitForSingleObject(wwi
->hStartUpEvent
, INFINITE
);
2938 CloseHandle(wwi
->hStartUpEvent
);
2939 wwi
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
2941 return widNotifyClient(wwi
, WIM_OPEN
, 0L, 0L);
2944 /**************************************************************************
2945 * widClose [internal]
2947 static DWORD
widClose(WORD wDevID
)
2951 TRACE("(%u);\n", wDevID
);
2952 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2953 WARN("can't close !\n");
2954 return MMSYSERR_INVALHANDLE
;
2957 wwi
= &WInDev
[wDevID
];
2959 if (wwi
->lpQueuePtr
!= NULL
) {
2960 WARN("still buffers open !\n");
2961 return WAVERR_STILLPLAYING
;
2964 OSS_AddRingMessage(&wwi
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
2965 OSS_CloseDevice(wwi
->ossdev
);
2966 wwi
->state
= WINE_WS_CLOSED
;
2967 wwi
->dwFragmentSize
= 0;
2968 OSS_DestroyRingMessage(&wwi
->msgRing
);
2969 return widNotifyClient(wwi
, WIM_CLOSE
, 0L, 0L);
2972 /**************************************************************************
2973 * widAddBuffer [internal]
2975 static DWORD
widAddBuffer(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
2977 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
2979 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2980 WARN("can't do it !\n");
2981 return MMSYSERR_INVALHANDLE
;
2983 if (!(lpWaveHdr
->dwFlags
& WHDR_PREPARED
)) {
2984 TRACE("never been prepared !\n");
2985 return WAVERR_UNPREPARED
;
2987 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
) {
2988 TRACE("header already in use !\n");
2989 return WAVERR_STILLPLAYING
;
2992 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
2993 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
2994 lpWaveHdr
->dwBytesRecorded
= 0;
2995 lpWaveHdr
->lpNext
= NULL
;
2997 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
2998 return MMSYSERR_NOERROR
;
3001 /**************************************************************************
3002 * widPrepare [internal]
3004 static DWORD
widPrepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
3006 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
3008 if (wDevID
>= numInDev
) return MMSYSERR_INVALHANDLE
;
3010 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
3011 return WAVERR_STILLPLAYING
;
3013 lpWaveHdr
->dwFlags
|= WHDR_PREPARED
;
3014 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
3015 lpWaveHdr
->dwBytesRecorded
= 0;
3016 TRACE("header prepared !\n");
3017 return MMSYSERR_NOERROR
;
3020 /**************************************************************************
3021 * widUnprepare [internal]
3023 static DWORD
widUnprepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
3025 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
3026 if (wDevID
>= numInDev
) return MMSYSERR_INVALHANDLE
;
3028 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
3029 return WAVERR_STILLPLAYING
;
3031 lpWaveHdr
->dwFlags
&= ~WHDR_PREPARED
;
3032 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
3034 return MMSYSERR_NOERROR
;
3037 /**************************************************************************
3038 * widStart [internal]
3040 static DWORD
widStart(WORD wDevID
)
3042 TRACE("(%u);\n", wDevID
);
3043 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3044 WARN("can't start recording !\n");
3045 return MMSYSERR_INVALHANDLE
;
3048 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STARTING
, 0, TRUE
);
3049 return MMSYSERR_NOERROR
;
3052 /**************************************************************************
3053 * widStop [internal]
3055 static DWORD
widStop(WORD wDevID
)
3057 TRACE("(%u);\n", wDevID
);
3058 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3059 WARN("can't stop !\n");
3060 return MMSYSERR_INVALHANDLE
;
3063 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STOPPING
, 0, TRUE
);
3065 return MMSYSERR_NOERROR
;
3068 /**************************************************************************
3069 * widReset [internal]
3071 static DWORD
widReset(WORD wDevID
)
3073 TRACE("(%u);\n", wDevID
);
3074 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3075 WARN("can't reset !\n");
3076 return MMSYSERR_INVALHANDLE
;
3078 OSS_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
3079 return MMSYSERR_NOERROR
;
3082 /**************************************************************************
3083 * widGetPosition [internal]
3085 static DWORD
widGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
3090 TRACE("(%u, %p, %lu);\n", wDevID
, lpTime
, uSize
);
3092 if (wDevID
>= numInDev
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
3093 WARN("can't get pos !\n");
3094 return MMSYSERR_INVALHANDLE
;
3096 if (lpTime
== NULL
) return MMSYSERR_INVALPARAM
;
3098 wwi
= &WInDev
[wDevID
];
3100 TRACE("wType=%04X !\n", lpTime
->wType
);
3101 TRACE("wBitsPerSample=%u\n", wwi
->format
.wBitsPerSample
);
3102 TRACE("nSamplesPerSec=%lu\n", wwi
->format
.wf
.nSamplesPerSec
);
3103 TRACE("nChannels=%u\n", wwi
->format
.wf
.nChannels
);
3104 TRACE("nAvgBytesPerSec=%lu\n", wwi
->format
.wf
.nAvgBytesPerSec
);
3105 TRACE("dwTotalRecorded=%lu\n",wwi
->dwTotalRecorded
);
3106 switch (lpTime
->wType
) {
3108 lpTime
->u
.cb
= wwi
->dwTotalRecorded
;
3109 TRACE("TIME_BYTES=%lu\n", lpTime
->u
.cb
);
3112 lpTime
->u
.sample
= wwi
->dwTotalRecorded
* 8 /
3113 wwi
->format
.wBitsPerSample
/ wwi
->format
.wf
.nChannels
;
3114 TRACE("TIME_SAMPLES=%lu\n", lpTime
->u
.sample
);
3117 time
= wwi
->dwTotalRecorded
/
3118 (wwi
->format
.wf
.nAvgBytesPerSec
/ 1000);
3119 lpTime
->u
.smpte
.hour
= time
/ (60 * 60 * 1000);
3120 time
-= lpTime
->u
.smpte
.hour
* (60 * 60 * 1000);
3121 lpTime
->u
.smpte
.min
= time
/ (60 * 1000);
3122 time
-= lpTime
->u
.smpte
.min
* (60 * 1000);
3123 lpTime
->u
.smpte
.sec
= time
/ 1000;
3124 time
-= lpTime
->u
.smpte
.sec
* 1000;
3125 lpTime
->u
.smpte
.frame
= time
* 30 / 1000;
3126 lpTime
->u
.smpte
.fps
= 30;
3127 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
3128 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
3129 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
3132 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime
->wType
);
3133 lpTime
->wType
= TIME_MS
;
3135 lpTime
->u
.ms
= wwi
->dwTotalRecorded
/
3136 (wwi
->format
.wf
.nAvgBytesPerSec
/ 1000);
3137 TRACE("TIME_MS=%lu\n", lpTime
->u
.ms
);
3140 return MMSYSERR_NOERROR
;
3143 /**************************************************************************
3144 * widMessage (WINEOSS.6)
3146 DWORD WINAPI
OSS_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3147 DWORD dwParam1
, DWORD dwParam2
)
3149 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
3150 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
3157 /* FIXME: Pretend this is supported */
3159 case WIDM_OPEN
: return widOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
3160 case WIDM_CLOSE
: return widClose (wDevID
);
3161 case WIDM_ADDBUFFER
: return widAddBuffer (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
3162 case WIDM_PREPARE
: return widPrepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
3163 case WIDM_UNPREPARE
: return widUnprepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
3164 case WIDM_GETDEVCAPS
: return widGetDevCaps (wDevID
, (LPWAVEINCAPSA
)dwParam1
, dwParam2
);
3165 case WIDM_GETNUMDEVS
: return numInDev
;
3166 case WIDM_GETPOS
: return widGetPosition(wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
3167 case WIDM_RESET
: return widReset (wDevID
);
3168 case WIDM_START
: return widStart (wDevID
);
3169 case WIDM_STOP
: return widStop (wDevID
);
3170 case DRV_QUERYDEVICEINTERFACESIZE
: return wdDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
3171 case DRV_QUERYDEVICEINTERFACE
: return wdDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
3172 case DRV_QUERYDSOUNDIFACE
: return widDsCreate (wDevID
, (PIDSCDRIVER
*)dwParam1
);
3173 case DRV_QUERYDSOUNDDESC
: return widDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
3174 case DRV_QUERYDSOUNDGUID
: return widDsGuid (wDevID
, (LPGUID
)dwParam1
);
3176 FIXME("unknown message %u!\n", wMsg
);
3178 return MMSYSERR_NOTSUPPORTED
;
3181 /*======================================================================*
3182 * Low level DSOUND property set implementation *
3183 *======================================================================*/
3185 typedef struct IDsDriverPropertySetImpl IDsDriverPropertySetImpl
;
3187 struct IDsDriverPropertySetImpl
3189 /* IUnknown fields */
3190 ICOM_VFIELD(IDsDriverPropertySet
);
3194 static HRESULT WINAPI
IDsDriverPropertySetImpl_QueryInterface(
3195 PIDSDRIVERPROPERTYSET iface
,
3199 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3200 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
3202 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
3203 IsEqualGUID(riid
, &IID_IDsDriverPropertySet
) ) {
3204 IDsDriverPropertySet_AddRef(iface
);
3205 *ppobj
= (LPVOID
)This
;
3209 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
3213 return E_NOINTERFACE
;
3216 static ULONG WINAPI
IDsDriverPropertySetImpl_AddRef(PIDSDRIVERPROPERTYSET iface
)
3218 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3220 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
3222 ref
= InterlockedIncrement(&(This
->ref
));
3226 static ULONG WINAPI
IDsDriverPropertySetImpl_Release(PIDSDRIVERPROPERTYSET iface
)
3228 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3230 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
3232 ref
= InterlockedDecrement(&(This
->ref
));
3236 static HRESULT WINAPI
IDsDriverPropertySetImpl_Get(
3237 PIDSDRIVERPROPERTYSET iface
,
3238 PDSPROPERTY pDsProperty
,
3239 LPVOID pPropertyParams
,
3240 ULONG cbPropertyParams
,
3241 LPVOID pPropertyData
,
3242 ULONG cbPropertyData
,
3243 PULONG pcbReturnedData
)
3245 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3246 FIXME("(%p,%p,%p,%lx,%p,%lx,%p)\n",This
,pDsProperty
,pPropertyParams
,cbPropertyParams
,pPropertyData
,cbPropertyData
,pcbReturnedData
);
3247 return DSERR_UNSUPPORTED
;
3250 static HRESULT WINAPI
IDsDriverPropertySetImpl_Set(
3251 PIDSDRIVERPROPERTYSET iface
,
3252 PDSPROPERTY pDsProperty
,
3253 LPVOID pPropertyParams
,
3254 ULONG cbPropertyParams
,
3255 LPVOID pPropertyData
,
3256 ULONG cbPropertyData
)
3258 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3259 FIXME("(%p,%p,%p,%lx,%p,%lx)\n",This
,pDsProperty
,pPropertyParams
,cbPropertyParams
,pPropertyData
,cbPropertyData
);
3260 return DSERR_UNSUPPORTED
;
3263 static HRESULT WINAPI
IDsDriverPropertySetImpl_QuerySupport(
3264 PIDSDRIVERPROPERTYSET iface
,
3265 REFGUID PropertySetId
,
3269 ICOM_THIS(IDsDriverPropertySetImpl
,iface
);
3270 FIXME("(%p,%s,%lx,%p)\n",This
,debugstr_guid(PropertySetId
),PropertyId
,pSupport
);
3271 return DSERR_UNSUPPORTED
;
3274 ICOM_VTABLE(IDsDriverPropertySet
) dsdpsvt
=
3276 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3277 IDsDriverPropertySetImpl_QueryInterface
,
3278 IDsDriverPropertySetImpl_AddRef
,
3279 IDsDriverPropertySetImpl_Release
,
3280 IDsDriverPropertySetImpl_Get
,
3281 IDsDriverPropertySetImpl_Set
,
3282 IDsDriverPropertySetImpl_QuerySupport
,
3285 /*======================================================================*
3286 * Low level DSOUND notify implementation *
3287 *======================================================================*/
3289 typedef struct IDsDriverNotifyImpl IDsDriverNotifyImpl
;
3291 struct IDsDriverNotifyImpl
3293 /* IUnknown fields */
3294 ICOM_VFIELD(IDsDriverNotify
);
3296 /* IDsDriverNotifyImpl fields */
3297 LPDSBPOSITIONNOTIFY notifies
;
3301 static HRESULT WINAPI
IDsDriverNotifyImpl_QueryInterface(
3302 PIDSDRIVERNOTIFY iface
,
3306 ICOM_THIS(IDsDriverNotifyImpl
,iface
);
3307 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
3309 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
3310 IsEqualGUID(riid
, &IID_IDsDriverNotify
) ) {
3311 IDsDriverNotify_AddRef(iface
);
3316 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
3320 return E_NOINTERFACE
;
3323 static ULONG WINAPI
IDsDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface
)
3325 ICOM_THIS(IDsDriverNotifyImpl
,iface
);
3327 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
3329 ref
= InterlockedIncrement(&(This
->ref
));
3333 static ULONG WINAPI
IDsDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface
)
3335 ICOM_THIS(IDsDriverNotifyImpl
,iface
);
3337 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
3339 ref
= InterlockedDecrement(&(This
->ref
));
3340 /* FIXME: A notification should be a part of a buffer rather than pointed
3341 * to from a buffer. Hence the -1 ref count */
3343 if (This
->notifies
!= NULL
)
3344 HeapFree(GetProcessHeap(), 0, This
->notifies
);
3346 HeapFree(GetProcessHeap(),0,This
);
3353 static HRESULT WINAPI
IDsDriverNotifyImpl_SetNotificationPositions(
3354 PIDSDRIVERNOTIFY iface
,
3356 LPCDSBPOSITIONNOTIFY notify
)
3358 ICOM_THIS(IDsDriverNotifyImpl
,iface
);
3359 TRACE("(%p,0x%08lx,%p)\n",This
,howmuch
,notify
);
3362 WARN("invalid parameter\n");
3363 return DSERR_INVALIDPARAM
;
3366 if (TRACE_ON(wave
)) {
3368 for (i
=0;i
<howmuch
;i
++)
3369 TRACE("notify at %ld to 0x%08lx\n",
3370 notify
[i
].dwOffset
,(DWORD
)notify
[i
].hEventNotify
);
3373 /* Make an internal copy of the caller-supplied array.
3374 * Replace the existing copy if one is already present. */
3376 This
->notifies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
3377 This
->notifies
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
3379 This
->notifies
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
3380 howmuch
* sizeof(DSBPOSITIONNOTIFY
));
3382 memcpy(This
->notifies
, notify
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
3383 This
->nrofnotifies
= howmuch
;
3388 ICOM_VTABLE(IDsDriverNotify
) dsdnvt
=
3390 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3391 IDsDriverNotifyImpl_QueryInterface
,
3392 IDsDriverNotifyImpl_AddRef
,
3393 IDsDriverNotifyImpl_Release
,
3394 IDsDriverNotifyImpl_SetNotificationPositions
,
3397 /*======================================================================*
3398 * Low level DSOUND capture implementation *
3399 *======================================================================*/
3401 typedef struct IDsCaptureDriverImpl IDsCaptureDriverImpl
;
3402 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl
;
3404 struct IDsCaptureDriverImpl
3406 /* IUnknown fields */
3407 ICOM_VFIELD(IDsCaptureDriver
);
3409 /* IDsCaptureDriverImpl fields */
3411 IDsCaptureDriverBufferImpl
* capture_buffer
;
3414 struct IDsCaptureDriverBufferImpl
3416 /* IUnknown fields */
3417 ICOM_VFIELD(IDsCaptureDriverBuffer
);
3419 /* IDsCaptureDriverBufferImpl fields */
3420 IDsCaptureDriverImpl
* drv
;
3423 /* IDsDriverNotifyImpl fields */
3424 IDsDriverNotifyImpl
* notify
;
3427 /* IDsDriverPropertySetImpl fields */
3428 IDsDriverPropertySetImpl
* property_set
;
3431 static HRESULT
DSDB_MapCapture(IDsCaptureDriverBufferImpl
*dscdb
)
3433 WINE_WAVEIN
*wwi
= &(WInDev
[dscdb
->drv
->wDevID
]);
3434 if (!wwi
->mapping
) {
3435 wwi
->mapping
= mmap(NULL
, wwi
->maplen
, PROT_WRITE
, MAP_SHARED
,
3436 wwi
->ossdev
->fd
, 0);
3437 if (wwi
->mapping
== (LPBYTE
)-1) {
3438 TRACE("(%p): Could not map sound device for direct access (%s)\n", dscdb
, strerror(errno
));
3439 return DSERR_GENERIC
;
3441 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dscdb
, wwi
->mapping
, wwi
->maplen
);
3443 /* for some reason, es1371 and sblive! sometimes have junk in here.
3444 * clear it, or we get junk noise */
3445 /* some libc implementations are buggy: their memset reads from the buffer...
3446 * to work around it, we have to zero the block by hand. We don't do the expected:
3447 * memset(wwo->mapping,0, wwo->maplen);
3450 char* p1
= wwi
->mapping
;
3451 unsigned len
= wwi
->maplen
;
3453 if (len
>= 16) /* so we can have at least a 4 long area to store... */
3455 /* the mmap:ed value is (at least) dword aligned
3456 * so, start filling the complete unsigned long:s
3459 unsigned long* p4
= (unsigned long*)p1
;
3461 while (b
--) *p4
++ = 0;
3462 /* prepare for filling the rest */
3464 p1
= (unsigned char*)p4
;
3466 /* in all cases, fill the remaining bytes */
3467 while (len
-- != 0) *p1
++ = 0;
3473 static HRESULT
DSDB_UnmapCapture(IDsCaptureDriverBufferImpl
*dscdb
)
3475 WINE_WAVEIN
*wwi
= &(WInDev
[dscdb
->drv
->wDevID
]);
3477 if (munmap(wwi
->mapping
, wwi
->maplen
) < 0) {
3478 ERR("(%p): Could not unmap sound device (%s)\n", dscdb
, strerror(errno
));
3479 return DSERR_GENERIC
;
3481 wwi
->mapping
= NULL
;
3482 TRACE("(%p): sound device unmapped\n", dscdb
);
3487 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_QueryInterface(PIDSCDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
3489 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3490 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
3492 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
3493 IsEqualGUID(riid
, &IID_IDsCaptureDriverBuffer
) ) {
3494 IDsCaptureDriverBuffer_AddRef(iface
);
3495 *ppobj
= (LPVOID
)This
;
3499 if ( IsEqualGUID( &IID_IDsDriverNotify
, riid
) ) {
3500 if (!This
->notify
) {
3501 This
->notify
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
->notify
));
3503 This
->notify
->ref
= 0; /* release when ref = -1 */
3504 This
->notify
->lpVtbl
= &dsdnvt
;
3508 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY
)This
->notify
);
3509 *ppobj
= (LPVOID
)This
->notify
;
3516 if ( IsEqualGUID( &IID_IDsDriverPropertySet
, riid
) ) {
3517 if (!This
->property_set
) {
3518 This
->property_set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
->property_set
));
3519 if (This
->property_set
) {
3520 This
->property_set
->ref
= 0; /* release when ref = -1 */
3521 This
->property_set
->lpVtbl
= &dsdpsvt
;
3524 if (This
->property_set
) {
3525 IDsDriverPropertySet_AddRef((PIDSDRIVERPROPERTYSET
)This
->property_set
);
3526 *ppobj
= (LPVOID
)This
->property_set
;
3533 FIXME("(%p,%s,%p) unsupported GUID\n", This
, debugstr_guid(riid
), ppobj
);
3537 return DSERR_UNSUPPORTED
;
3540 static ULONG WINAPI
IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface
)
3542 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3547 static ULONG WINAPI
IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface
)
3549 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3552 DSDB_UnmapCapture(This
);
3554 IDsDriverNotify_Release((PIDSDRIVERNOTIFY
)This
->notify
);
3555 if (This
->property_set
)
3556 IDsDriverPropertySet_Release((PIDSDRIVERPROPERTYSET
)This
->property_set
);
3557 HeapFree(GetProcessHeap(),0,This
);
3561 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Lock(PIDSCDRIVERBUFFER iface
,
3562 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
3563 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
3564 DWORD dwWritePosition
,DWORD dwWriteLen
,
3567 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3568 FIXME("(%p,%p,%p,%p,%p,%ld,%ld,0x%08lx): stub!\n",This
,ppvAudio1
,pdwLen1
,ppvAudio2
,pdwLen2
,
3569 dwWritePosition
,dwWriteLen
,dwFlags
);
3573 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Unlock(PIDSCDRIVERBUFFER iface
,
3574 LPVOID pvAudio1
,DWORD dwLen1
,
3575 LPVOID pvAudio2
,DWORD dwLen2
)
3577 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3578 FIXME("(%p,%p,%ld,%p,%ld): stub!\n",This
,pvAudio1
,dwLen1
,pvAudio2
,dwLen2
);
3582 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_GetPosition(PIDSCDRIVERBUFFER iface
,
3583 LPDWORD lpdwCapture
,
3586 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3589 TRACE("(%p,%p,%p)\n",This
,lpdwCapture
,lpdwRead
);
3591 if (WInDev
[This
->drv
->wDevID
].state
== WINE_WS_CLOSED
) {
3592 ERR("device not open, but accessing?\n");
3593 return DSERR_UNINITIALIZED
;
3595 if (ioctl(WInDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
3596 ERR("ioctl(%s, SNDCTL_DSP_GETIPTR) failed (%s)\n", WInDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3597 return DSERR_GENERIC
;
3599 ptr
= info
.ptr
& ~3; /* align the pointer, just in case */
3600 if (lpdwCapture
) *lpdwCapture
= ptr
;
3602 /* add some safety margin (not strictly necessary, but...) */
3603 if (WInDev
[This
->drv
->wDevID
].ossdev
->in_caps_support
& WAVECAPS_SAMPLEACCURATE
)
3604 *lpdwRead
= ptr
+ 32;
3606 *lpdwRead
= ptr
+ WInDev
[This
->drv
->wDevID
].dwFragmentSize
;
3607 while (*lpdwRead
> This
->buflen
)
3608 *lpdwRead
-= This
->buflen
;
3610 TRACE("capturepos=%ld, readpos=%ld\n", lpdwCapture
?*lpdwCapture
:0, lpdwRead
?*lpdwRead
:0);
3614 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_GetStatus(PIDSCDRIVERBUFFER iface
, LPDWORD lpdwStatus
)
3616 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3617 FIXME("(%p,%p): stub!\n",This
,lpdwStatus
);
3618 return DSERR_UNSUPPORTED
;
3621 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface
, DWORD dwFlags
)
3623 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3625 TRACE("(%p,%lx)\n",This
,dwFlags
);
3626 WInDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= TRUE
;
3627 enable
= getEnables(WInDev
[This
->drv
->wDevID
].ossdev
);
3628 if (ioctl(WInDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
3629 if (errno
== EINVAL
) {
3630 /* Don't give up yet. OSS trigger support is inconsistent. */
3631 if (WInDev
[This
->drv
->wDevID
].ossdev
->open_count
== 1) {
3632 /* try the opposite output enable */
3633 if (WInDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
== FALSE
)
3634 WInDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= TRUE
;
3636 WInDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
3638 enable
= getEnables(WInDev
[This
->drv
->wDevID
].ossdev
);
3639 if (ioctl(WInDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) >= 0)
3643 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3644 WInDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= FALSE
;
3645 return DSERR_GENERIC
;
3650 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface
)
3652 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3654 TRACE("(%p)\n",This
);
3655 /* no more captureing */
3656 WInDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= FALSE
;
3657 enable
= getEnables(WInDev
[This
->drv
->wDevID
].ossdev
);
3658 if (ioctl(WInDev
[This
->drv
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
3659 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3660 return DSERR_GENERIC
;
3663 /* Most OSS drivers just can't stop capturing without closing the device...
3664 * so we need to somehow signal to our DirectSound implementation
3665 * that it should completely recreate this HW buffer...
3666 * this unexpected error code should do the trick... */
3667 return DSERR_BUFFERLOST
;
3670 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_SetFormat(PIDSCDRIVERBUFFER iface
, LPWAVEFORMATEX pwfx
)
3672 ICOM_THIS(IDsCaptureDriverBufferImpl
,iface
);
3673 FIXME("(%p): stub!\n",This
);
3674 return DSERR_UNSUPPORTED
;
3677 static ICOM_VTABLE(IDsCaptureDriverBuffer
) dscdbvt
=
3679 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3680 IDsCaptureDriverBufferImpl_QueryInterface
,
3681 IDsCaptureDriverBufferImpl_AddRef
,
3682 IDsCaptureDriverBufferImpl_Release
,
3683 IDsCaptureDriverBufferImpl_Lock
,
3684 IDsCaptureDriverBufferImpl_Unlock
,
3685 IDsCaptureDriverBufferImpl_SetFormat
,
3686 IDsCaptureDriverBufferImpl_GetPosition
,
3687 IDsCaptureDriverBufferImpl_GetStatus
,
3688 IDsCaptureDriverBufferImpl_Start
,
3689 IDsCaptureDriverBufferImpl_Stop
3692 static HRESULT WINAPI
IDsCaptureDriverImpl_QueryInterface(PIDSCDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
3694 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3695 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
3697 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
3698 IsEqualGUID(riid
, &IID_IDsCaptureDriver
) ) {
3699 IDsCaptureDriver_AddRef(iface
);
3700 *ppobj
= (LPVOID
)This
;
3704 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
3708 return E_NOINTERFACE
;
3711 static ULONG WINAPI
IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface
)
3713 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3714 TRACE("(%p)\n",This
);
3716 TRACE("ref=%ld\n",This
->ref
);
3720 static ULONG WINAPI
IDsCaptureDriverImpl_Release(PIDSCDRIVER iface
)
3722 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3723 TRACE("(%p)\n",This
);
3725 TRACE("ref=%ld\n",This
->ref
);
3728 HeapFree(GetProcessHeap(),0,This
);
3733 static HRESULT WINAPI
IDsCaptureDriverImpl_GetDriverDesc(PIDSCDRIVER iface
, PDSDRIVERDESC pDesc
)
3735 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3736 TRACE("(%p,%p)\n",This
,pDesc
);
3739 TRACE("invalid parameter\n");
3740 return DSERR_INVALIDPARAM
;
3743 /* copy version from driver */
3744 memcpy(pDesc
, &(WInDev
[This
->wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
3746 pDesc
->dwFlags
|= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
|
3747 DSDDESC_USESYSTEMMEMORY
| DSDDESC_DONTNEEDPRIMARYLOCK
|
3748 DSDDESC_DONTNEEDSECONDARYLOCK
;
3749 pDesc
->dnDevNode
= WInDev
[This
->wDevID
].waveDesc
.dnDevNode
;
3751 pDesc
->wReserved
= 0;
3752 pDesc
->ulDeviceNum
= This
->wDevID
;
3753 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
3754 pDesc
->pvDirectDrawHeap
= NULL
;
3755 pDesc
->dwMemStartAddress
= 0;
3756 pDesc
->dwMemEndAddress
= 0;
3757 pDesc
->dwMemAllocExtra
= 0;
3758 pDesc
->pvReserved1
= NULL
;
3759 pDesc
->pvReserved2
= NULL
;
3763 static HRESULT WINAPI
IDsCaptureDriverImpl_Open(PIDSCDRIVER iface
)
3765 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3766 TRACE("(%p)\n",This
);
3770 static HRESULT WINAPI
IDsCaptureDriverImpl_Close(PIDSCDRIVER iface
)
3772 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3773 TRACE("(%p)\n",This
);
3774 if (This
->capture_buffer
) {
3775 ERR("problem with DirectSound: capture buffer not released\n");
3776 return DSERR_GENERIC
;
3781 static HRESULT WINAPI
IDsCaptureDriverImpl_GetCaps(PIDSCDRIVER iface
, PDSCDRIVERCAPS pCaps
)
3783 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3784 TRACE("(%p,%p)\n",This
,pCaps
);
3785 memcpy(pCaps
, &(WInDev
[This
->wDevID
].ossdev
->dsc_caps
), sizeof(DSCDRIVERCAPS
));
3789 static HRESULT WINAPI
IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface
,
3790 LPWAVEFORMATEX pwfx
,
3792 DWORD dwCardAddress
,
3793 LPDWORD pdwcbBufferSize
,
3797 ICOM_THIS(IDsCaptureDriverImpl
,iface
);
3798 IDsCaptureDriverBufferImpl
** ippdscdb
= (IDsCaptureDriverBufferImpl
**)ppvObj
;
3800 audio_buf_info info
;
3802 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",This
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
3804 if (This
->capture_buffer
) {
3805 TRACE("already allocated\n");
3806 return DSERR_ALLOCATED
;
3809 *ippdscdb
= (IDsCaptureDriverBufferImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsCaptureDriverBufferImpl
));
3810 if (*ippdscdb
== NULL
) {
3811 TRACE("out of memory\n");
3812 return DSERR_OUTOFMEMORY
;
3815 (*ippdscdb
)->lpVtbl
= &dscdbvt
;
3816 (*ippdscdb
)->ref
= 1;
3817 (*ippdscdb
)->drv
= This
;
3818 (*ippdscdb
)->notify
= 0;
3819 (*ippdscdb
)->notify_index
= 0;
3820 (*ippdscdb
)->property_set
= 0;
3822 if (WInDev
[This
->wDevID
].state
== WINE_WS_CLOSED
) {
3825 desc
.lpFormat
= pwfx
;
3826 desc
.dwCallback
= 0;
3827 desc
.dwInstance
= 0;
3828 desc
.uMappedDeviceID
= 0;
3830 err
= widOpen(This
->wDevID
, &desc
, dwFlags
| WAVE_DIRECTSOUND
);
3831 if (err
!= MMSYSERR_NOERROR
) {
3832 TRACE("widOpen failed\n");
3837 /* check how big the DMA buffer is now */
3838 if (ioctl(WInDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
3839 ERR("ioctl(%s, SNDCTL_DSP_GETISPACE) failed (%s)\n", WInDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3840 HeapFree(GetProcessHeap(),0,*ippdscdb
);
3842 return DSERR_GENERIC
;
3844 WInDev
[This
->wDevID
].maplen
= (*ippdscdb
)->buflen
= info
.fragstotal
* info
.fragsize
;
3846 /* map the DMA buffer */
3847 err
= DSDB_MapCapture(*ippdscdb
);
3849 HeapFree(GetProcessHeap(),0,*ippdscdb
);
3854 /* capture buffer is ready to go */
3855 *pdwcbBufferSize
= WInDev
[This
->wDevID
].maplen
;
3856 *ppbBuffer
= WInDev
[This
->wDevID
].mapping
;
3858 /* some drivers need some extra nudging after mapping */
3859 WInDev
[This
->wDevID
].ossdev
->bInputEnabled
= FALSE
;
3860 enable
= getEnables(WInDev
[This
->wDevID
].ossdev
);
3861 if (ioctl(WInDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
3862 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WInDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
3863 return DSERR_GENERIC
;
3866 This
->capture_buffer
= *ippdscdb
;
3871 static ICOM_VTABLE(IDsCaptureDriver
) dscdvt
=
3873 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
3874 IDsCaptureDriverImpl_QueryInterface
,
3875 IDsCaptureDriverImpl_AddRef
,
3876 IDsCaptureDriverImpl_Release
,
3877 IDsCaptureDriverImpl_GetDriverDesc
,
3878 IDsCaptureDriverImpl_Open
,
3879 IDsCaptureDriverImpl_Close
,
3880 IDsCaptureDriverImpl_GetCaps
,
3881 IDsCaptureDriverImpl_CreateCaptureBuffer
3884 static DWORD
widDsCreate(UINT wDevID
, PIDSCDRIVER
* drv
)
3886 IDsCaptureDriverImpl
** idrv
= (IDsCaptureDriverImpl
**)drv
;
3887 TRACE("(%d,%p)\n",wDevID
,drv
);
3889 /* the HAL isn't much better than the HEL if we can't do mmap() */
3890 if (!(WInDev
[wDevID
].ossdev
->in_caps_support
& WAVECAPS_DIRECTSOUND
)) {
3891 ERR("DirectSoundCapture flag not set\n");
3892 MESSAGE("This sound card's driver does not support direct access\n");
3893 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
3894 return MMSYSERR_NOTSUPPORTED
;
3897 *idrv
= (IDsCaptureDriverImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsCaptureDriverImpl
));
3899 return MMSYSERR_NOMEM
;
3900 (*idrv
)->lpVtbl
= &dscdvt
;
3903 (*idrv
)->wDevID
= wDevID
;
3904 (*idrv
)->capture_buffer
= NULL
;
3905 return MMSYSERR_NOERROR
;
3908 static DWORD
widDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
3910 memcpy(desc
, &(WInDev
[wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
3911 return MMSYSERR_NOERROR
;
3914 static DWORD
widDsGuid(UINT wDevID
, LPGUID pGuid
)
3916 TRACE("(%d,%p)\n",wDevID
,pGuid
);
3918 memcpy(pGuid
, &(WInDev
[wDevID
].ossdev
->dsc_guid
), sizeof(GUID
));
3920 return MMSYSERR_NOERROR
;
3923 #else /* !HAVE_OSS */
3925 /**************************************************************************
3926 * wodMessage (WINEOSS.7)
3928 DWORD WINAPI
OSS_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3929 DWORD dwParam1
, DWORD dwParam2
)
3931 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
3932 return MMSYSERR_NOTENABLED
;
3935 /**************************************************************************
3936 * widMessage (WINEOSS.6)
3938 DWORD WINAPI
OSS_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
3939 DWORD dwParam1
, DWORD dwParam2
)
3941 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
3942 return MMSYSERR_NOTENABLED
;
3945 #endif /* HAVE_OSS */