2 * Wine Driver for EsounD Sound Server
3 * http://www.tux.org/~ricdude/EsounD.html
5 * Copyright 1994 Martin Ayotte
6 * 1999 Eric Pouech (async playing in waveOut/waveIn)
7 * 2000 Eric Pouech (loops in waveOut)
8 * 2004 Zhangrong Huang (EsounD version of this file)
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * with esd we cannot stop the audio that is already in
26 * the server's buffer.
29 * pause in waveOut does not work correctly in loop mode
31 * does something need to be done in for WaveIn DirectSound?
50 #ifdef HAVE_SYS_POLL_H
51 # include <sys/poll.h>
58 #include "wine/winuser16.h"
67 #include "wine/debug.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(wave
);
75 /* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
78 /* define if you want to use esd_monitor_stream instead of
79 * esd_record_stream for waveIn stream
81 /*#define WID_USE_ESDMON*/
83 #define BUFFER_REFILL_THRESHOLD 4
85 #define MAX_WAVEOUTDRV (10)
86 #define MAX_WAVEINDRV (10)
87 #define MAX_CHANNELS 2
89 /* state diagram for waveOut writing:
91 * +---------+-------------+---------------+---------------------------------+
92 * | state | function | event | new state |
93 * +---------+-------------+---------------+---------------------------------+
94 * | | open() | | STOPPED |
95 * | PAUSED | write() | | PAUSED |
96 * | STOPPED | write() | <thrd create> | PLAYING |
97 * | PLAYING | write() | HEADER | PLAYING |
98 * | (other) | write() | <error> | |
99 * | (any) | pause() | PAUSING | PAUSED |
100 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
101 * | (any) | reset() | RESETTING | STOPPED |
102 * | (any) | close() | CLOSING | CLOSED |
103 * +---------+-------------+---------------+---------------------------------+
106 /* states of the playing device */
107 #define WINE_WS_PLAYING 0
108 #define WINE_WS_PAUSED 1
109 #define WINE_WS_STOPPED 2
110 #define WINE_WS_CLOSED 3
112 /* events to be send to device */
113 enum win_wm_message
{
114 WINE_WM_PAUSING
= WM_USER
+ 1, WINE_WM_RESTARTING
, WINE_WM_RESETTING
, WINE_WM_HEADER
,
115 WINE_WM_UPDATE
, WINE_WM_BREAKLOOP
, WINE_WM_CLOSING
, WINE_WM_STARTING
, WINE_WM_STOPPING
119 #define SIGNAL_OMR(mr) do { int x = 0; write((mr)->msg_pipe[1], &x, sizeof(x)); } while (0)
120 #define CLEAR_OMR(mr) do { int x = 0; read((mr)->msg_pipe[0], &x, sizeof(x)); } while (0)
121 #define RESET_OMR(mr) do { } while (0)
122 #define WAIT_OMR(mr, sleep) \
123 do { struct pollfd pfd; pfd.fd = (mr)->msg_pipe[0]; \
124 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
126 #define SIGNAL_OMR(mr) do { SetEvent((mr)->msg_event); } while (0)
127 #define CLEAR_OMR(mr) do { } while (0)
128 #define RESET_OMR(mr) do { ResetEvent((mr)->msg_event); } while (0)
129 #define WAIT_OMR(mr, sleep) \
130 do { WaitForSingleObject((mr)->msg_event, sleep); } while (0)
134 enum win_wm_message msg
; /* message identifier */
135 DWORD_PTR param
; /* parameter for this message */
136 HANDLE hEvent
; /* if message is synchronous, handle of event for synchro */
139 /* implement an in-process message ring for better performance
140 * (compared to passing thru the server)
141 * this ring will be used by the input (resp output) record (resp playback) routine
143 #define ESD_RING_BUFFER_INCREMENT 64
146 int ring_buffer_size
;
154 CRITICAL_SECTION msg_crst
;
158 volatile int state
; /* one of the WINE_WS_ manifest constants */
159 WAVEOPENDESC waveDesc
;
161 WAVEFORMATPCMEX waveFormat
;
163 char interface_name
[32];
165 DWORD dwSleepTime
; /* Num of milliseconds to sleep between filling the dsp buffers */
167 /* esd information */
168 int esd_fd
; /* the socket fd we get from esd when opening a stream for playing */
173 DWORD volume_left
; /* volume control information */
176 LPWAVEHDR lpQueuePtr
; /* start of queued WAVEHDRs (waiting to be notified) */
177 LPWAVEHDR lpPlayPtr
; /* start of not yet fully played buffers */
178 DWORD dwPartialOffset
; /* Offset of not yet written bytes in lpPlayPtr */
180 LPWAVEHDR lpLoopPtr
; /* pointer of first buffer in loop, if any */
181 DWORD dwLoops
; /* private copy of loop counter */
183 DWORD dwPlayedTotal
; /* number of bytes actually played since opening */
184 DWORD dwWrittenTotal
; /* number of bytes written to the audio device since opening */
186 /* synchronization stuff */
187 HANDLE hStartUpEvent
;
189 ESD_MSG_RING msgRing
;
193 volatile int state
; /* one of the WINE_WS_ manifest constants */
194 WAVEOPENDESC waveDesc
;
196 WAVEFORMATPCMEX waveFormat
;
198 char interface_name
[32];
200 /* esd information */
201 int esd_fd
; /* the socket fd we get from esd when opening a stream for recording */
203 LPWAVEHDR lpQueuePtr
;
204 DWORD dwRecordedTotal
;
206 /* synchronization stuff */
207 HANDLE hStartUpEvent
;
209 ESD_MSG_RING msgRing
;
212 static WINE_WAVEOUT WOutDev
[MAX_WAVEOUTDRV
];
213 static WINE_WAVEIN WInDev
[MAX_WAVEINDRV
];
215 /* These strings used only for tracing */
216 static const char *wodPlayerCmdString
[] = {
218 "WINE_WM_RESTARTING",
229 /*======================================================================*
230 * Low level DSOUND implementation *
231 *======================================================================*/
233 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
235 /* we can't perform memory mapping as we don't have a file stream
236 interface with esd like we do with oss */
237 MESSAGE("This sound card's driver does not support direct access\n");
238 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
239 return MMSYSERR_NOTSUPPORTED
;
242 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
244 memset(desc
, 0, sizeof(*desc
));
245 strcpy(desc
->szDesc
, "Wine EsounD DirectSound Driver");
246 strcpy(desc
->szDrvname
, "wineesd.drv");
247 return MMSYSERR_NOERROR
;
250 /*======================================================================*
251 * Low level WAVE implementation *
252 *======================================================================*/
254 /* Volume functions derived from Alsaplayer source */
255 /* length is the number of 16 bit samples */
256 static void volume_effect16(void *bufin
, void* bufout
, int length
, int left
,
257 int right
, int nChannels
)
259 short *d_out
= bufout
;
264 TRACE("length == %d, nChannels == %d\n", length, nChannels);
267 if (right
== -1) right
= left
;
269 for(i
= 0; i
< length
; i
+=(nChannels
))
271 v
= (int) ((*(d_in
++) * left
) / 100);
272 *(d_out
++) = (v
>32767) ? 32767 : ((v
<-32768) ? -32768 : v
);
275 v
= (int) ((*(d_in
++) * right
) / 100);
276 *(d_out
++) = (v
>32767) ? 32767 : ((v
<-32768) ? -32768 : v
);
281 /* length is the number of 8 bit samples */
282 static void volume_effect8(void *bufin
, void* bufout
, int length
, int left
,
283 int right
, int nChannels
)
285 BYTE
*d_out
= bufout
;
290 TRACE("length == %d, nChannels == %d\n", length, nChannels);
293 if (right
== -1) right
= left
;
295 for(i
= 0; i
< length
; i
+=(nChannels
))
297 v
= (BYTE
) ((*(d_in
++) * left
) / 100);
298 *(d_out
++) = (v
>255) ? 255 : ((v
<0) ? 0 : v
);
301 v
= (BYTE
) ((*(d_in
++) * right
) / 100);
302 *(d_out
++) = (v
>255) ? 255 : ((v
<0) ? 0 : v
);
307 static DWORD
bytes_to_mmtime(LPMMTIME lpTime
, DWORD position
,
308 WAVEFORMATPCMEX
* format
)
310 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
311 lpTime
->wType
, format
->Format
.wBitsPerSample
, format
->Format
.nSamplesPerSec
,
312 format
->Format
.nChannels
, format
->Format
.nAvgBytesPerSec
);
313 TRACE("Position in bytes=%u\n", position
);
315 switch (lpTime
->wType
) {
317 lpTime
->u
.sample
= position
/ (format
->Format
.wBitsPerSample
/ 8 * format
->Format
.nChannels
);
318 TRACE("TIME_SAMPLES=%u\n", lpTime
->u
.sample
);
321 lpTime
->u
.ms
= 1000.0 * position
/ (format
->Format
.wBitsPerSample
/ 8 * format
->Format
.nChannels
* format
->Format
.nSamplesPerSec
);
322 TRACE("TIME_MS=%u\n", lpTime
->u
.ms
);
325 lpTime
->u
.smpte
.fps
= 30;
326 position
= position
/ (format
->Format
.wBitsPerSample
/ 8 * format
->Format
.nChannels
);
327 position
+= (format
->Format
.nSamplesPerSec
/ lpTime
->u
.smpte
.fps
) - 1; /* round up */
328 lpTime
->u
.smpte
.sec
= position
/ format
->Format
.nSamplesPerSec
;
329 position
-= lpTime
->u
.smpte
.sec
* format
->Format
.nSamplesPerSec
;
330 lpTime
->u
.smpte
.min
= lpTime
->u
.smpte
.sec
/ 60;
331 lpTime
->u
.smpte
.sec
-= 60 * lpTime
->u
.smpte
.min
;
332 lpTime
->u
.smpte
.hour
= lpTime
->u
.smpte
.min
/ 60;
333 lpTime
->u
.smpte
.min
-= 60 * lpTime
->u
.smpte
.hour
;
334 lpTime
->u
.smpte
.fps
= 30;
335 lpTime
->u
.smpte
.frame
= position
* lpTime
->u
.smpte
.fps
/ format
->Format
.nSamplesPerSec
;
336 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
337 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
338 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
341 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime
->wType
);
342 lpTime
->wType
= TIME_BYTES
;
345 lpTime
->u
.cb
= position
;
346 TRACE("TIME_BYTES=%u\n", lpTime
->u
.cb
);
349 return MMSYSERR_NOERROR
;
352 static BOOL
supportedFormat(LPWAVEFORMATEX wf
)
356 if (wf
->nSamplesPerSec
<DSBFREQUENCY_MIN
||wf
->nSamplesPerSec
>DSBFREQUENCY_MAX
)
359 if (wf
->wFormatTag
== WAVE_FORMAT_PCM
) {
360 if (wf
->nChannels
>= 1 && wf
->nChannels
<= MAX_CHANNELS
) {
361 if (wf
->wBitsPerSample
==8||wf
->wBitsPerSample
==16)
364 } else if (wf
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
) {
365 WAVEFORMATEXTENSIBLE
* wfex
= (WAVEFORMATEXTENSIBLE
*)wf
;
367 if (wf
->cbSize
== 22 && IsEqualGUID(&wfex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
)) {
368 if (wf
->nChannels
>=1 && wf
->nChannels
<= MAX_CHANNELS
) {
369 if (wf
->wBitsPerSample
==wfex
->Samples
.wValidBitsPerSample
) {
370 if (wf
->wBitsPerSample
==8||wf
->wBitsPerSample
==16)
373 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
376 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
378 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
383 static void copy_format(LPWAVEFORMATEX wf1
, LPWAVEFORMATPCMEX wf2
)
385 ZeroMemory(wf2
, sizeof(wf2
));
386 if (wf1
->wFormatTag
== WAVE_FORMAT_PCM
)
387 memcpy(wf2
, wf1
, sizeof(PCMWAVEFORMAT
));
388 else if (wf1
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
389 memcpy(wf2
, wf1
, sizeof(WAVEFORMATPCMEX
));
391 memcpy(wf2
, wf1
, sizeof(WAVEFORMATEX
) + wf1
->cbSize
);
394 /******************************************************************
395 * ESD_CloseWaveOutDevice
398 static void ESD_CloseWaveOutDevice(WINE_WAVEOUT
* wwo
)
400 esd_close(wwo
->esd_fd
); /* close the esd socket fd */
403 /* free up the buffer we use for volume and reset the size */
404 HeapFree(GetProcessHeap(), 0, wwo
->sound_buffer
);
405 wwo
->sound_buffer
= NULL
;
406 wwo
->buffer_size
= 0;
409 /******************************************************************
410 * ESD_CloseWaveInDevice
413 static void ESD_CloseWaveInDevice(WINE_WAVEIN
* wwi
)
415 esd_close(wwi
->esd_fd
); /* close the esd socket fd */
419 /******************************************************************
422 LONG
ESD_WaveClose(void)
426 /* close all open devices */
427 for(iDevice
= 0; iDevice
< MAX_WAVEOUTDRV
; iDevice
++)
429 if(WOutDev
[iDevice
].esd_fd
!= -1)
431 ESD_CloseWaveOutDevice(&WOutDev
[iDevice
]);
435 for(iDevice
= 0; iDevice
< MAX_WAVEINDRV
; iDevice
++)
437 if(WInDev
[iDevice
].esd_fd
!= -1)
439 ESD_CloseWaveInDevice(&WInDev
[iDevice
]);
446 /******************************************************************
449 * Initialize internal structures from ESD server info
451 LONG
ESD_WaveInit(void)
458 /* Testing whether the esd host is alive. */
459 if ((fd
= esd_open_sound(NULL
)) < 0)
461 WARN("esd_open_sound() failed (%d)\n", errno
);
466 /* initialize all device handles to -1 */
467 for (i
= 0; i
< MAX_WAVEOUTDRV
; ++i
)
469 static const WCHAR ini
[] = {'E','s','o','u','n','D',' ','W','a','v','e','O','u','t','D','r','i','v','e','r',0};
471 WOutDev
[i
].esd_fd
= -1;
472 memset(&WOutDev
[i
].caps
, 0, sizeof(WOutDev
[i
].caps
)); /* zero out
474 WOutDev
[i
].caps
.wMid
= 0x00FF; /* Manufacturer ID */
475 WOutDev
[i
].caps
.wPid
= 0x0001; /* Product ID */
476 lstrcpyW(WOutDev
[i
].caps
.szPname
, ini
);
477 snprintf(WOutDev
[i
].interface_name
, sizeof(WOutDev
[i
].interface_name
), "wineesd: %d", i
);
479 WOutDev
[i
].caps
.vDriverVersion
= 0x0100;
480 WOutDev
[i
].caps
.dwFormats
= 0x00000000;
481 WOutDev
[i
].caps
.dwSupport
= WAVECAPS_VOLUME
;
483 WOutDev
[i
].caps
.wChannels
= 2;
484 WOutDev
[i
].caps
.dwSupport
|= WAVECAPS_LRVOLUME
;
486 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M08
;
487 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S08
;
488 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S16
;
489 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M16
;
490 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M08
;
491 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S08
;
492 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M16
;
493 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S16
;
494 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M08
;
495 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S08
;
496 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M16
;
497 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S16
;
500 for (i
= 0; i
< MAX_WAVEINDRV
; ++i
)
502 static const WCHAR ini
[] = {'E','s','o','u','n','D',' ','W','a','v','e','I','n','D','r','i','v','e','r',0};
504 WInDev
[i
].esd_fd
= -1;
505 memset(&WInDev
[i
].caps
, 0, sizeof(WInDev
[i
].caps
)); /* zero out
507 WInDev
[i
].caps
.wMid
= 0x00FF;
508 WInDev
[i
].caps
.wPid
= 0x0001;
509 lstrcpyW(WInDev
[i
].caps
.szPname
, ini
);
510 snprintf(WInDev
[i
].interface_name
, sizeof(WInDev
[i
].interface_name
), "wineesd: %d", i
);
512 WInDev
[i
].caps
.vDriverVersion
= 0x0100;
513 WInDev
[i
].caps
.dwFormats
= 0x00000000;
515 WInDev
[i
].caps
.wChannels
= 2;
517 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M08
;
518 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S08
;
519 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S16
;
520 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M16
;
521 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M08
;
522 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S08
;
523 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M16
;
524 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S16
;
525 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M08
;
526 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S08
;
527 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M16
;
528 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S16
;
530 WInDev
[i
].caps
.wReserved1
= 0;
535 /******************************************************************
536 * ESD_InitRingMessage
538 * Initialize the ring of messages for passing between driver's caller and playback/record
541 static int ESD_InitRingMessage(ESD_MSG_RING
* mr
)
546 if (pipe(mr
->msg_pipe
) < 0) {
547 mr
->msg_pipe
[0] = -1;
548 mr
->msg_pipe
[1] = -1;
549 ERR("could not create pipe, error=%s\n", strerror(errno
));
552 mr
->msg_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
554 mr
->ring_buffer_size
= ESD_RING_BUFFER_INCREMENT
;
555 mr
->messages
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,mr
->ring_buffer_size
* sizeof(RING_MSG
));
556 InitializeCriticalSection(&mr
->msg_crst
);
557 mr
->msg_crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": ESD_MSG_RING.msg_crst");
561 /******************************************************************
562 * ESD_DestroyRingMessage
565 static int ESD_DestroyRingMessage(ESD_MSG_RING
* mr
)
568 close(mr
->msg_pipe
[0]);
569 close(mr
->msg_pipe
[1]);
571 CloseHandle(mr
->msg_event
);
573 HeapFree(GetProcessHeap(),0,mr
->messages
);
575 mr
->msg_crst
.DebugInfo
->Spare
[0] = 0;
576 DeleteCriticalSection(&mr
->msg_crst
);
580 /******************************************************************
583 * Inserts a new message into the ring (should be called from DriverProc derived routines)
585 static int ESD_AddRingMessage(ESD_MSG_RING
* mr
, enum win_wm_message msg
, DWORD param
, BOOL wait
)
587 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
589 EnterCriticalSection(&mr
->msg_crst
);
590 if ((mr
->msg_toget
== ((mr
->msg_tosave
+ 1) % mr
->ring_buffer_size
)))
592 int old_ring_buffer_size
= mr
->ring_buffer_size
;
593 mr
->ring_buffer_size
+= ESD_RING_BUFFER_INCREMENT
;
594 TRACE("mr->ring_buffer_size=%d\n",mr
->ring_buffer_size
);
595 mr
->messages
= HeapReAlloc(GetProcessHeap(),0,mr
->messages
, mr
->ring_buffer_size
* sizeof(RING_MSG
));
596 /* Now we need to rearrange the ring buffer so that the new
597 buffers just allocated are in between mr->msg_tosave and
600 if (mr
->msg_tosave
< mr
->msg_toget
)
602 memmove(&(mr
->messages
[mr
->msg_toget
+ ESD_RING_BUFFER_INCREMENT
]),
603 &(mr
->messages
[mr
->msg_toget
]),
604 sizeof(RING_MSG
)*(old_ring_buffer_size
- mr
->msg_toget
)
606 mr
->msg_toget
+= ESD_RING_BUFFER_INCREMENT
;
611 hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
612 if (hEvent
== INVALID_HANDLE_VALUE
)
614 ERR("can't create event !?\n");
615 LeaveCriticalSection(&mr
->msg_crst
);
618 if (mr
->msg_toget
!= mr
->msg_tosave
&& mr
->messages
[mr
->msg_toget
].msg
!= WINE_WM_HEADER
)
619 FIXME("two fast messages in the queue!!!!\n");
621 /* fast messages have to be added at the start of the queue */
622 mr
->msg_toget
= (mr
->msg_toget
+ mr
->ring_buffer_size
- 1) % mr
->ring_buffer_size
;
624 mr
->messages
[mr
->msg_toget
].msg
= msg
;
625 mr
->messages
[mr
->msg_toget
].param
= param
;
626 mr
->messages
[mr
->msg_toget
].hEvent
= hEvent
;
630 mr
->messages
[mr
->msg_tosave
].msg
= msg
;
631 mr
->messages
[mr
->msg_tosave
].param
= param
;
632 mr
->messages
[mr
->msg_tosave
].hEvent
= INVALID_HANDLE_VALUE
;
633 mr
->msg_tosave
= (mr
->msg_tosave
+ 1) % mr
->ring_buffer_size
;
636 LeaveCriticalSection(&mr
->msg_crst
);
638 /* signal a new message */
642 /* wait for playback/record thread to have processed the message */
643 WaitForSingleObject(hEvent
, INFINITE
);
650 /******************************************************************
651 * ESD_RetrieveRingMessage
653 * Get a message from the ring. Should be called by the playback/record thread.
655 static int ESD_RetrieveRingMessage(ESD_MSG_RING
* mr
, enum win_wm_message
*msg
,
656 DWORD_PTR
*param
, HANDLE
*hEvent
)
658 EnterCriticalSection(&mr
->msg_crst
);
660 if (mr
->msg_toget
== mr
->msg_tosave
) /* buffer empty ? */
662 LeaveCriticalSection(&mr
->msg_crst
);
666 *msg
= mr
->messages
[mr
->msg_toget
].msg
;
667 mr
->messages
[mr
->msg_toget
].msg
= 0;
668 *param
= mr
->messages
[mr
->msg_toget
].param
;
669 *hEvent
= mr
->messages
[mr
->msg_toget
].hEvent
;
670 mr
->msg_toget
= (mr
->msg_toget
+ 1) % mr
->ring_buffer_size
;
672 LeaveCriticalSection(&mr
->msg_crst
);
676 /*======================================================================*
677 * Low level WAVE OUT implementation *
678 *======================================================================*/
680 /**************************************************************************
681 * wodNotifyClient [internal]
683 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD_PTR dwParam1
,
686 TRACE("wMsg = 0x%04x dwParm1 = %08lX dwParam2 = %08lX\n", wMsg
, dwParam1
, dwParam2
);
692 if (wwo
->wFlags
!= DCB_NULL
&&
693 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
, (HDRVR
)wwo
->waveDesc
.hWave
,
694 wMsg
, wwo
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
695 WARN("can't notify client !\n");
696 return MMSYSERR_ERROR
;
700 FIXME("Unknown callback message %u\n", wMsg
);
701 return MMSYSERR_INVALPARAM
;
703 return MMSYSERR_NOERROR
;
706 /**************************************************************************
707 * wodUpdatePlayedTotal [internal]
710 static BOOL
wodUpdatePlayedTotal(WINE_WAVEOUT
* wwo
)
712 /* total played is the bytes written less the bytes to write ;-) */
713 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
;
718 /**************************************************************************
719 * wodPlayer_BeginWaveHdr [internal]
721 * Makes the specified lpWaveHdr the currently playing wave header.
722 * If the specified wave header is a begin loop and we're not already in
723 * a loop, setup the loop.
725 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
727 wwo
->lpPlayPtr
= lpWaveHdr
;
729 if (!lpWaveHdr
) return;
731 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
) {
732 if (wwo
->lpLoopPtr
) {
733 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
734 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
736 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
737 wwo
->lpLoopPtr
= lpWaveHdr
;
738 /* Windows does not touch WAVEHDR.dwLoops,
739 * so we need to make an internal copy */
740 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
743 wwo
->dwPartialOffset
= 0;
746 /**************************************************************************
747 * wodPlayer_PlayPtrNext [internal]
749 * Advance the play pointer to the next waveheader, looping if required.
751 static LPWAVEHDR
wodPlayer_PlayPtrNext(WINE_WAVEOUT
* wwo
)
753 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
755 wwo
->dwPartialOffset
= 0;
756 if ((lpWaveHdr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
) {
757 /* We're at the end of a loop, loop if required */
758 if (--wwo
->dwLoops
> 0) {
759 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
761 /* Handle overlapping loops correctly */
762 if (wwo
->lpLoopPtr
!= lpWaveHdr
&& (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)) {
763 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
764 /* shall we consider the END flag for the closing loop or for
765 * the opening one or for both ???
766 * code assumes for closing loop only
769 lpWaveHdr
= lpWaveHdr
->lpNext
;
771 wwo
->lpLoopPtr
= NULL
;
772 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
);
775 /* We're not in a loop. Advance to the next wave header */
776 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
= lpWaveHdr
->lpNext
);
782 /**************************************************************************
783 * wodPlayer_NotifyWait [internal]
784 * Returns the number of milliseconds to wait before attempting to notify
785 * completion of the specified wavehdr.
786 * This is based on the number of bytes remaining to be written in the
789 static DWORD
wodPlayer_NotifyWait(const WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
793 if(lpWaveHdr
->reserved
< wwo
->dwPlayedTotal
)
799 dwMillis
= (lpWaveHdr
->reserved
- wwo
->dwPlayedTotal
) * 1000 / wwo
->waveFormat
.Format
.nAvgBytesPerSec
;
800 if(!dwMillis
) dwMillis
= 1;
803 TRACE("dwMillis = %d\n", dwMillis
);
809 /**************************************************************************
810 * wodPlayer_WriteMaxFrags [internal]
811 * Writes the maximum number of bytes possible to the DSP and returns
812 * the number of bytes written.
814 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT
* wwo
, DWORD
* bytes
)
816 /* Only attempt to write to free bytes */
817 DWORD dwLength
= wwo
->lpPlayPtr
->dwBufferLength
- wwo
->dwPartialOffset
;
818 int toWrite
= min(dwLength
, *bytes
);
821 TRACE("Writing wavehdr %p.%u[%u]\n",
822 wwo
->lpPlayPtr
, wwo
->dwPartialOffset
, wwo
->lpPlayPtr
->dwBufferLength
);
824 /* see if our buffer isn't large enough for the data we are writing */
825 if(wwo
->buffer_size
< toWrite
)
827 if(wwo
->sound_buffer
)
829 wwo
->sound_buffer
= HeapReAlloc(GetProcessHeap(), 0, wwo
->sound_buffer
, toWrite
);
830 wwo
->buffer_size
= toWrite
;
834 /* if we don't have a buffer then get one */
835 if(!wwo
->sound_buffer
)
837 /* allocate some memory for the buffer */
838 wwo
->sound_buffer
= HeapAlloc(GetProcessHeap(), 0, toWrite
);
839 wwo
->buffer_size
= toWrite
;
842 /* if we don't have a buffer then error out */
843 if(!wwo
->sound_buffer
)
845 ERR("error allocating sound_buffer memory\n");
849 TRACE("toWrite == %d\n", toWrite
);
851 /* apply volume to the bits */
852 /* for single channel audio streams we only use the LEFT volume */
853 if(wwo
->waveFormat
.Format
.wBitsPerSample
== 16)
855 /* apply volume to the buffer we are about to send */
856 /* divide toWrite(bytes) by 2 as volume processes by 16 bits */
857 volume_effect16(wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
,
858 wwo
->sound_buffer
, toWrite
>>1, wwo
->volume_left
,
859 wwo
->volume_right
, wwo
->waveFormat
.Format
.nChannels
);
860 } else if(wwo
->waveFormat
.Format
.wBitsPerSample
== 8)
862 /* apply volume to the buffer we are about to send */
863 volume_effect8(wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
,
864 wwo
->sound_buffer
, toWrite
, wwo
->volume_left
,
865 wwo
->volume_right
, wwo
->waveFormat
.Format
.nChannels
);
868 FIXME("unsupported wwo->format.wBitsPerSample of %d\n",
869 wwo
->waveFormat
.Format
.wBitsPerSample
);
872 /* send the audio data to esd for playing */
873 written
= write(wwo
->esd_fd
, wwo
->sound_buffer
, toWrite
);
875 TRACE("written = %d\n", written
);
879 *bytes
= 0; /* apparently esd is actually full */
880 return written
; /* if we wrote nothing just return */
883 if (written
>= dwLength
)
884 wodPlayer_PlayPtrNext(wwo
); /* If we wrote all current wavehdr, skip to the next one */
886 wwo
->dwPartialOffset
+= written
; /* Remove the amount written */
888 if (written
< toWrite
)
893 wwo
->dwWrittenTotal
+= written
; /* update stats on this wave device */
895 return written
; /* return the number of bytes written */
899 /**************************************************************************
900 * wodPlayer_NotifyCompletions [internal]
902 * Notifies and remove from queue all wavehdrs which have been played to
903 * the speaker (ie. they have cleared the audio device). If force is true,
904 * we notify all wavehdrs and remove them all from the queue even if they
905 * are unplayed or part of a loop.
907 static DWORD
wodPlayer_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
911 if (wwo
->lpQueuePtr
) {
912 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), reserved=(%ld), dwWrittenTotal=(%d), force=(%d)\n",
916 wwo
->lpQueuePtr
->reserved
,
920 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), dwWrittenTotal=(%d), force=(%d)\n",
928 /* Start from lpQueuePtr and keep notifying until:
929 * - we hit an unwritten wavehdr
930 * - we hit the beginning of a running loop
931 * - we hit a wavehdr which hasn't finished playing
933 while ((lpWaveHdr
= wwo
->lpQueuePtr
) &&
935 (lpWaveHdr
!= wwo
->lpPlayPtr
&&
936 lpWaveHdr
!= wwo
->lpLoopPtr
&&
937 lpWaveHdr
->reserved
<= wwo
->dwWrittenTotal
))) {
939 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
941 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
942 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
944 wodNotifyClient(wwo
, WOM_DONE
, (DWORD_PTR
)lpWaveHdr
, 0);
946 return (lpWaveHdr
&& lpWaveHdr
!= wwo
->lpPlayPtr
&& lpWaveHdr
!= wwo
->lpLoopPtr
) ?
947 wodPlayer_NotifyWait(wwo
, lpWaveHdr
) : INFINITE
;
950 /**************************************************************************
951 * wodPlayer_Reset [internal]
953 * wodPlayer helper. Resets current output stream.
955 static void wodPlayer_Reset(WINE_WAVEOUT
* wwo
, BOOL reset
)
957 wodUpdatePlayedTotal(wwo
);
959 wodPlayer_NotifyCompletions(wwo
, FALSE
); /* updates current notify list */
961 /* we aren't able to flush any data that has already been written */
962 /* to esd, otherwise we would do the flushing here */
965 enum win_wm_message msg
;
969 /* remove any buffer */
970 wodPlayer_NotifyCompletions(wwo
, TRUE
);
972 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
973 wwo
->state
= WINE_WS_STOPPED
;
974 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
= 0;
976 wwo
->dwPartialOffset
= 0; /* Clear partial wavehdr */
978 /* remove any existing message in the ring */
979 EnterCriticalSection(&wwo
->msgRing
.msg_crst
);
981 /* return all pending headers in queue */
982 while (ESD_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
))
984 TRACE("flushing msg\n");
985 if (msg
!= WINE_WM_HEADER
)
987 FIXME("shouldn't have headers left\n");
991 ((LPWAVEHDR
)param
)->dwFlags
&= ~WHDR_INQUEUE
;
992 ((LPWAVEHDR
)param
)->dwFlags
|= WHDR_DONE
;
994 wodNotifyClient(wwo
, WOM_DONE
, param
, 0);
996 RESET_OMR(&wwo
->msgRing
);
997 LeaveCriticalSection(&wwo
->msgRing
.msg_crst
);
999 if (wwo
->lpLoopPtr
) {
1000 /* complicated case, not handled yet (could imply modifying the loop counter */
1001 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
1002 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1003 wwo
->dwPartialOffset
= 0;
1004 wwo
->dwWrittenTotal
= wwo
->dwPlayedTotal
; /* this is wrong !!! */
1006 /* the data already written is going to be played, so take */
1007 /* this fact into account here */
1008 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
;
1010 wwo
->state
= WINE_WS_PAUSED
;
1014 /**************************************************************************
1015 * wodPlayer_ProcessMessages [internal]
1017 static void wodPlayer_ProcessMessages(WINE_WAVEOUT
* wwo
)
1019 LPWAVEHDR lpWaveHdr
;
1020 enum win_wm_message msg
;
1024 while (ESD_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
)) {
1025 TRACE("Received %s %lx\n", wodPlayerCmdString
[msg
- WM_USER
- 1], param
);
1027 case WINE_WM_PAUSING
:
1028 wodPlayer_Reset(wwo
, FALSE
);
1031 case WINE_WM_RESTARTING
:
1032 wwo
->state
= WINE_WS_PLAYING
;
1035 case WINE_WM_HEADER
:
1036 lpWaveHdr
= (LPWAVEHDR
)param
;
1038 /* insert buffer at the end of queue */
1041 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
1044 if (!wwo
->lpPlayPtr
)
1045 wodPlayer_BeginWaveHdr(wwo
,lpWaveHdr
);
1046 if (wwo
->state
== WINE_WS_STOPPED
)
1047 wwo
->state
= WINE_WS_PLAYING
;
1049 case WINE_WM_RESETTING
:
1050 wodPlayer_Reset(wwo
, TRUE
);
1053 case WINE_WM_UPDATE
:
1054 wodUpdatePlayedTotal(wwo
);
1057 case WINE_WM_BREAKLOOP
:
1058 if (wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpLoopPtr
!= NULL
) {
1059 /* ensure exit at end of current loop */
1064 case WINE_WM_CLOSING
:
1065 /* sanity check: this should not happen since the device must have been reset before */
1066 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
1068 wwo
->state
= WINE_WS_CLOSED
;
1071 /* shouldn't go here */
1073 FIXME("unknown message %d\n", msg
);
1079 /**************************************************************************
1080 * wodPlayer_FeedDSP [internal]
1081 * Feed as much sound data as we can into the DSP and return the number of
1082 * milliseconds before it will be necessary to feed the DSP again.
1084 static DWORD
wodPlayer_FeedDSP(WINE_WAVEOUT
* wwo
)
1088 wodUpdatePlayedTotal(wwo
);
1089 /* better way to set availInQ? */
1090 availInQ
= ESD_BUF_SIZE
;
1091 TRACE("availInQ = %d\n", availInQ
);
1093 /* input queue empty */
1094 if (!wwo
->lpPlayPtr
) {
1095 TRACE("Run out of wavehdr:s... flushing\n");
1100 /* no more room... no need to try to feed */
1103 TRACE("no more room, no need to try to feed\n");
1104 return wwo
->dwSleepTime
;
1108 /* Feed from partial wavehdr */
1109 if (wwo
->lpPlayPtr
&& wwo
->dwPartialOffset
!= 0)
1111 TRACE("feeding from partial wavehdr\n");
1112 wodPlayer_WriteMaxFrags(wwo
, &availInQ
);
1115 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1116 if (!wwo
->dwPartialOffset
)
1118 while(wwo
->lpPlayPtr
&& availInQ
)
1120 TRACE("feeding waveheaders until we run out of space\n");
1121 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1122 wwo
->lpPlayPtr
->reserved
= wwo
->dwWrittenTotal
+ wwo
->lpPlayPtr
->dwBufferLength
;
1123 TRACE("reserved=(%ld) dwWrittenTotal=(%d) dwBufferLength=(%d)\n",
1124 wwo
->lpPlayPtr
->reserved
,
1125 wwo
->dwWrittenTotal
,
1126 wwo
->lpPlayPtr
->dwBufferLength
1128 wodPlayer_WriteMaxFrags(wwo
, &availInQ
);
1132 if (!wwo
->lpPlayPtr
) {
1133 TRACE("Ran out of wavehdrs\n");
1137 return wwo
->dwSleepTime
;
1141 /**************************************************************************
1142 * wodPlayer [internal]
1144 static DWORD CALLBACK
wodPlayer(LPVOID pmt
)
1146 WORD uDevID
= (DWORD_PTR
)pmt
;
1147 WINE_WAVEOUT
* wwo
= &WOutDev
[uDevID
];
1148 DWORD dwNextFeedTime
= INFINITE
; /* Time before DSP needs feeding */
1149 DWORD dwNextNotifyTime
= INFINITE
; /* Time before next wave completion */
1152 wwo
->state
= WINE_WS_STOPPED
;
1153 SetEvent(wwo
->hStartUpEvent
);
1156 /** Wait for the shortest time before an action is required. If there
1157 * are no pending actions, wait forever for a command.
1159 dwSleepTime
= min(dwNextFeedTime
, dwNextNotifyTime
);
1160 TRACE("waiting %ums (%u,%u)\n", dwSleepTime
, dwNextFeedTime
, dwNextNotifyTime
);
1161 WAIT_OMR(&wwo
->msgRing
, dwSleepTime
);
1162 wodPlayer_ProcessMessages(wwo
);
1163 if (wwo
->state
== WINE_WS_PLAYING
) {
1164 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1165 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1167 dwNextFeedTime
= dwNextNotifyTime
= INFINITE
;
1174 /**************************************************************************
1175 * wodGetDevCaps [internal]
1177 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSW lpCaps
, DWORD dwSize
)
1179 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
1181 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
1183 if (wDevID
>= MAX_WAVEOUTDRV
) {
1184 TRACE("MAX_WAVOUTDRV reached !\n");
1185 return MMSYSERR_BADDEVICEID
;
1188 memcpy(lpCaps
, &WOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1189 return MMSYSERR_NOERROR
;
1192 /**************************************************************************
1193 * wodOpen [internal]
1195 static DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1198 /* output to esound... */
1199 int out_bits
= ESD_BITS8
, out_channels
= ESD_MONO
, out_rate
;
1200 int out_mode
= ESD_STREAM
, out_func
= ESD_PLAY
;
1201 esd_format_t out_format
;
1203 TRACE("(%u, %p, %08X);\n", wDevID
, lpDesc
, dwFlags
);
1204 if (lpDesc
== NULL
) {
1205 WARN("Invalid Parameter !\n");
1206 return MMSYSERR_INVALPARAM
;
1208 if (wDevID
>= MAX_WAVEOUTDRV
) {
1209 TRACE("MAX_WAVOUTDRV reached !\n");
1210 return MMSYSERR_BADDEVICEID
;
1213 /* if this device is already open tell the app that it is allocated */
1214 if(WOutDev
[wDevID
].esd_fd
!= -1)
1216 TRACE("device already allocated\n");
1217 return MMSYSERR_ALLOCATED
;
1220 /* only PCM format is supported so far... */
1221 if (!supportedFormat(lpDesc
->lpFormat
)) {
1222 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1223 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1224 lpDesc
->lpFormat
->nSamplesPerSec
);
1225 return WAVERR_BADFORMAT
;
1228 if (dwFlags
& WAVE_FORMAT_QUERY
) {
1229 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1230 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1231 lpDesc
->lpFormat
->nSamplesPerSec
);
1232 return MMSYSERR_NOERROR
;
1235 wwo
= &WOutDev
[wDevID
];
1237 /* direct sound not supported, ignore the flag */
1238 dwFlags
&= ~WAVE_DIRECTSOUND
;
1240 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1242 wwo
->waveDesc
= *lpDesc
;
1243 copy_format(lpDesc
->lpFormat
, &wwo
->waveFormat
);
1245 if (wwo
->waveFormat
.Format
.wBitsPerSample
== 0) {
1246 WARN("Resetting zeroed wBitsPerSample\n");
1247 wwo
->waveFormat
.Format
.wBitsPerSample
= 8 *
1248 (wwo
->waveFormat
.Format
.nAvgBytesPerSec
/
1249 wwo
->waveFormat
.Format
.nSamplesPerSec
) /
1250 wwo
->waveFormat
.Format
.nChannels
;
1253 if (wwo
->waveFormat
.Format
.wBitsPerSample
== 8)
1254 out_bits
= ESD_BITS8
;
1255 else if (wwo
->waveFormat
.Format
.wBitsPerSample
== 16)
1256 out_bits
= ESD_BITS16
;
1258 if (wwo
->waveFormat
.Format
.nChannels
== 1)
1259 out_channels
= ESD_MONO
;
1260 else if (wwo
->waveFormat
.Format
.nChannels
== 2)
1261 out_channels
= ESD_STEREO
;
1263 out_format
= out_bits
| out_channels
| out_mode
| out_func
;
1264 out_rate
= (int) wwo
->waveFormat
.Format
.nSamplesPerSec
;
1265 TRACE("esd output format = 0x%08x, rate = %d\n", out_format
, out_rate
);
1267 wwo
->esd_fd
= esd_play_stream(out_format
, out_rate
, NULL
, "wineesd");
1269 /* clear these so we don't have any confusion ;-) */
1270 wwo
->sound_buffer
= 0;
1271 wwo
->buffer_size
= 0;
1273 if(wwo
->esd_fd
< 0) return MMSYSERR_ALLOCATED
;
1275 wwo
->dwPlayedTotal
= 0;
1276 wwo
->dwWrittenTotal
= 0;
1278 wwo
->dwSleepTime
= (1024 * 1000 * BUFFER_REFILL_THRESHOLD
) / wwo
->waveFormat
.Format
.nAvgBytesPerSec
;
1280 /* Initialize volume to full level */
1281 wwo
->volume_left
= 100;
1282 wwo
->volume_right
= 100;
1284 ESD_InitRingMessage(&wwo
->msgRing
);
1286 /* create player thread */
1287 if (!(dwFlags
& WAVE_DIRECTSOUND
)) {
1288 wwo
->hStartUpEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1289 wwo
->hThread
= CreateThread(NULL
, 0, wodPlayer
, (LPVOID
)(DWORD_PTR
)wDevID
,
1291 WaitForSingleObject(wwo
->hStartUpEvent
, INFINITE
);
1292 CloseHandle(wwo
->hStartUpEvent
);
1294 wwo
->hThread
= INVALID_HANDLE_VALUE
;
1296 wwo
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
1298 TRACE("esd=%d\n", wwo
->esd_fd
);
1300 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
1301 wwo
->waveFormat
.Format
.wBitsPerSample
, wwo
->waveFormat
.Format
.nAvgBytesPerSec
,
1302 wwo
->waveFormat
.Format
.nSamplesPerSec
, wwo
->waveFormat
.Format
.nChannels
,
1303 wwo
->waveFormat
.Format
.nBlockAlign
);
1305 return wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
1308 /**************************************************************************
1309 * wodClose [internal]
1311 static DWORD
wodClose(WORD wDevID
)
1313 DWORD ret
= MMSYSERR_NOERROR
;
1316 TRACE("(%u);\n", wDevID
);
1318 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].esd_fd
== -1) {
1319 WARN("bad device ID !\n");
1320 return MMSYSERR_BADDEVICEID
;
1323 wwo
= &WOutDev
[wDevID
];
1324 if (wwo
->lpQueuePtr
) {
1325 WARN("buffers still playing !\n");
1326 ret
= WAVERR_STILLPLAYING
;
1328 TRACE("imhere[3-close]\n");
1329 if (wwo
->hThread
!= INVALID_HANDLE_VALUE
) {
1330 ESD_AddRingMessage(&wwo
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
1333 ESD_DestroyRingMessage(&wwo
->msgRing
);
1335 ESD_CloseWaveOutDevice(wwo
); /* close the stream and clean things up */
1337 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
1342 /**************************************************************************
1343 * wodWrite [internal]
1346 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1348 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
1350 /* first, do the sanity checks... */
1351 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].esd_fd
== -1) {
1352 WARN("bad dev ID !\n");
1353 return MMSYSERR_BADDEVICEID
;
1356 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
1358 TRACE("unprepared\n");
1359 return WAVERR_UNPREPARED
;
1362 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1364 TRACE("still playing\n");
1365 return WAVERR_STILLPLAYING
;
1368 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1369 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
1370 lpWaveHdr
->lpNext
= 0;
1372 TRACE("adding ring message\n");
1373 ESD_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_HEADER
,
1374 (DWORD_PTR
)lpWaveHdr
, FALSE
);
1376 return MMSYSERR_NOERROR
;
1379 /**************************************************************************
1380 * wodPause [internal]
1382 static DWORD
wodPause(WORD wDevID
)
1384 TRACE("(%u);!\n", wDevID
);
1386 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].esd_fd
== -1) {
1387 WARN("bad device ID !\n");
1388 return MMSYSERR_BADDEVICEID
;
1391 TRACE("imhere[3-PAUSING]\n");
1392 ESD_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_PAUSING
, 0, TRUE
);
1394 return MMSYSERR_NOERROR
;
1397 /**************************************************************************
1398 * wodRestart [internal]
1400 static DWORD
wodRestart(WORD wDevID
)
1402 TRACE("(%u);\n", wDevID
);
1404 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].esd_fd
== -1) {
1405 WARN("bad device ID !\n");
1406 return MMSYSERR_BADDEVICEID
;
1409 if (WOutDev
[wDevID
].state
== WINE_WS_PAUSED
) {
1410 TRACE("imhere[3-RESTARTING]\n");
1411 ESD_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESTARTING
, 0, TRUE
);
1414 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1415 /* FIXME: Myst crashes with this ... hmm -MM
1416 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1419 return MMSYSERR_NOERROR
;
1422 /**************************************************************************
1423 * wodReset [internal]
1425 static DWORD
wodReset(WORD wDevID
)
1427 TRACE("(%u);\n", wDevID
);
1429 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].esd_fd
== -1) {
1430 WARN("bad device ID !\n");
1431 return MMSYSERR_BADDEVICEID
;
1434 TRACE("imhere[3-RESET]\n");
1435 ESD_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
1437 return MMSYSERR_NOERROR
;
1440 /**************************************************************************
1441 * wodGetPosition [internal]
1443 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
1447 TRACE("(%u, %p, %u);\n", wDevID
, lpTime
, uSize
);
1449 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].esd_fd
== -1) {
1450 WARN("bad device ID !\n");
1451 return MMSYSERR_BADDEVICEID
;
1454 if (lpTime
== NULL
) {
1455 WARN("invalid parameter: lpTime == NULL\n");
1456 return MMSYSERR_INVALPARAM
;
1459 wwo
= &WOutDev
[wDevID
];
1460 ESD_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
1462 return bytes_to_mmtime(lpTime
, wwo
->dwPlayedTotal
, &wwo
->waveFormat
);
1465 /**************************************************************************
1466 * wodBreakLoop [internal]
1468 static DWORD
wodBreakLoop(WORD wDevID
)
1470 TRACE("(%u);\n", wDevID
);
1472 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].esd_fd
== -1) {
1473 WARN("bad device ID !\n");
1474 return MMSYSERR_BADDEVICEID
;
1476 ESD_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_BREAKLOOP
, 0, TRUE
);
1477 return MMSYSERR_NOERROR
;
1480 /**************************************************************************
1481 * wodGetVolume [internal]
1483 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
1487 left
= WOutDev
[wDevID
].volume_left
;
1488 right
= WOutDev
[wDevID
].volume_right
;
1490 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
1492 *lpdwVol
= ((left
* 0xFFFFl
) / 100) + (((right
* 0xFFFFl
) / 100) <<
1495 return MMSYSERR_NOERROR
;
1498 /**************************************************************************
1499 * wodSetVolume [internal]
1501 static DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
1505 left
= (LOWORD(dwParam
) * 100) / 0xFFFFl
;
1506 right
= (HIWORD(dwParam
) * 100) / 0xFFFFl
;
1508 TRACE("(%u, %08X);\n", wDevID
, dwParam
);
1510 WOutDev
[wDevID
].volume_left
= left
;
1511 WOutDev
[wDevID
].volume_right
= right
;
1513 return MMSYSERR_NOERROR
;
1516 /**************************************************************************
1517 * wodGetNumDevs [internal]
1519 static DWORD
wodGetNumDevs(void)
1521 return MAX_WAVEOUTDRV
;
1524 /**************************************************************************
1525 * wodDevInterfaceSize [internal]
1527 static DWORD
wodDevInterfaceSize(UINT wDevID
, LPDWORD dwParam1
)
1529 TRACE("(%u, %p)\n", wDevID
, dwParam1
);
1531 *dwParam1
= MultiByteToWideChar(CP_ACP
, 0, WOutDev
[wDevID
].interface_name
, -1,
1532 NULL
, 0 ) * sizeof(WCHAR
);
1533 return MMSYSERR_NOERROR
;
1536 /**************************************************************************
1537 * wodDevInterface [internal]
1539 static DWORD
wodDevInterface(UINT wDevID
, PWCHAR dwParam1
, DWORD dwParam2
)
1541 if (dwParam2
>= MultiByteToWideChar(CP_ACP
, 0, WOutDev
[wDevID
].interface_name
, -1,
1542 NULL
, 0 ) * sizeof(WCHAR
))
1544 MultiByteToWideChar(CP_ACP
, 0, WOutDev
[wDevID
].interface_name
, -1,
1545 dwParam1
, dwParam2
/ sizeof(WCHAR
));
1546 return MMSYSERR_NOERROR
;
1548 return MMSYSERR_INVALPARAM
;
1551 /**************************************************************************
1552 * wodMessage (WINEESD.@)
1554 DWORD WINAPI
ESD_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1555 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
1557 TRACE("(%u, %04X, %08X, %08lX, %08lX);\n",
1558 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1565 /* FIXME: Pretend this is supported */
1567 case WODM_OPEN
: return wodOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
1568 case WODM_CLOSE
: return wodClose (wDevID
);
1569 case WODM_WRITE
: return wodWrite (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1570 case WODM_PAUSE
: return wodPause (wDevID
);
1571 case WODM_GETPOS
: return wodGetPosition (wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
1572 case WODM_BREAKLOOP
: return wodBreakLoop (wDevID
);
1573 case WODM_PREPARE
: return MMSYSERR_NOTSUPPORTED
;
1574 case WODM_UNPREPARE
: return MMSYSERR_NOTSUPPORTED
;
1575 case WODM_GETDEVCAPS
: return wodGetDevCaps (wDevID
, (LPWAVEOUTCAPSW
)dwParam1
, dwParam2
);
1576 case WODM_GETNUMDEVS
: return wodGetNumDevs ();
1577 case WODM_GETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1578 case WODM_SETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1579 case WODM_GETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1580 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1581 case WODM_GETVOLUME
: return wodGetVolume (wDevID
, (LPDWORD
)dwParam1
);
1582 case WODM_SETVOLUME
: return wodSetVolume (wDevID
, dwParam1
);
1583 case WODM_RESTART
: return wodRestart (wDevID
);
1584 case WODM_RESET
: return wodReset (wDevID
);
1586 case DRV_QUERYDEVICEINTERFACESIZE
: return wodDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
1587 case DRV_QUERYDEVICEINTERFACE
: return wodDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
1588 case DRV_QUERYDSOUNDIFACE
: return wodDsCreate (wDevID
, (PIDSDRIVER
*)dwParam1
);
1589 case DRV_QUERYDSOUNDDESC
: return wodDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
1591 FIXME("unknown message %d!\n", wMsg
);
1593 return MMSYSERR_NOTSUPPORTED
;
1596 /*======================================================================*
1597 * Low level WAVE IN implementation *
1598 *======================================================================*/
1600 /**************************************************************************
1601 * widGetNumDevs [internal]
1603 static DWORD
widGetNumDevs(void)
1605 TRACE("%d\n", MAX_WAVEINDRV
);
1606 return MAX_WAVEINDRV
;
1609 /**************************************************************************
1610 * widDevInterfaceSize [internal]
1612 static DWORD
widDevInterfaceSize(UINT wDevID
, LPDWORD dwParam1
)
1614 TRACE("(%u, %p)\n", wDevID
, dwParam1
);
1617 *dwParam1
= MultiByteToWideChar(CP_ACP
, 0, WInDev
[wDevID
].interface_name
, -1,
1618 NULL
, 0 ) * sizeof(WCHAR
);
1619 return MMSYSERR_NOERROR
;
1622 /**************************************************************************
1623 * widDevInterface [internal]
1625 static DWORD
widDevInterface(UINT wDevID
, PWCHAR dwParam1
, DWORD dwParam2
)
1627 if (dwParam2
>= MultiByteToWideChar(CP_ACP
, 0, WInDev
[wDevID
].interface_name
, -1,
1628 NULL
, 0 ) * sizeof(WCHAR
))
1630 MultiByteToWideChar(CP_ACP
, 0, WInDev
[wDevID
].interface_name
, -1,
1631 dwParam1
, dwParam2
/ sizeof(WCHAR
));
1632 return MMSYSERR_NOERROR
;
1634 return MMSYSERR_INVALPARAM
;
1637 /**************************************************************************
1638 * widNotifyClient [internal]
1640 static DWORD
widNotifyClient(WINE_WAVEIN
* wwi
, WORD wMsg
, DWORD_PTR dwParam1
,
1643 TRACE("wMsg = 0x%04x dwParm1 = %08lX dwParam2 = %08lX\n", wMsg
, dwParam1
, dwParam2
);
1649 if (wwi
->wFlags
!= DCB_NULL
&&
1650 !DriverCallback(wwi
->waveDesc
.dwCallback
, wwi
->wFlags
,
1651 (HDRVR
)wwi
->waveDesc
.hWave
, wMsg
,
1652 wwi
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
1653 WARN("can't notify client !\n");
1654 return MMSYSERR_ERROR
;
1658 FIXME("Unknown callback message %u\n", wMsg
);
1659 return MMSYSERR_INVALPARAM
;
1661 return MMSYSERR_NOERROR
;
1664 /**************************************************************************
1665 * widGetDevCaps [internal]
1667 static DWORD
widGetDevCaps(WORD wDevID
, LPWAVEINCAPSW lpCaps
, DWORD dwSize
)
1669 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
1671 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
1673 if (wDevID
>= MAX_WAVEINDRV
) {
1674 TRACE("MAX_WAVINDRV reached !\n");
1675 return MMSYSERR_BADDEVICEID
;
1678 memcpy(lpCaps
, &WInDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1679 return MMSYSERR_NOERROR
;
1682 /**************************************************************************
1683 * widRecorder [internal]
1685 static DWORD CALLBACK
widRecorder(LPVOID pmt
)
1687 WORD uDevID
= (DWORD_PTR
)pmt
;
1688 WINE_WAVEIN
* wwi
= &WInDev
[uDevID
];
1692 enum win_wm_message msg
;
1696 SetEvent(wwi
->hStartUpEvent
);
1698 /* make sleep time to be # of ms to record one packet */
1699 dwSleepTime
= (1024 * 1000) / wwi
->waveFormat
.Format
.nAvgBytesPerSec
;
1700 TRACE("sleeptime=%d ms\n", dwSleepTime
);
1703 TRACE("wwi->lpQueuePtr=(%p), wwi->state=(%d)\n",wwi
->lpQueuePtr
,wwi
->state
);
1705 /* read all data is esd input buffer. */
1706 if ((wwi
->lpQueuePtr
!= NULL
) && (wwi
->state
== WINE_WS_PLAYING
))
1708 lpWaveHdr
= wwi
->lpQueuePtr
;
1710 TRACE("read as much as we can\n");
1711 while(wwi
->lpQueuePtr
)
1713 TRACE("attempt to read %d bytes\n",lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
);
1714 bytesRead
= read(wwi
->esd_fd
,
1715 lpWaveHdr
->lpData
+ lpWaveHdr
->dwBytesRecorded
,
1716 lpWaveHdr
->dwBufferLength
- lpWaveHdr
->dwBytesRecorded
);
1717 TRACE("bytesRead=%d\n",bytesRead
);
1718 if (bytesRead
<= 0) break; /* So we can stop recording smoothly */
1720 lpWaveHdr
->dwBytesRecorded
+= bytesRead
;
1721 wwi
->dwRecordedTotal
+= bytesRead
;
1723 /* buffer full. notify client */
1724 if (lpWaveHdr
->dwBytesRecorded
>= lpWaveHdr
->dwBufferLength
)
1726 /* must copy the value of next waveHdr, because we have no idea of what
1727 * will be done with the content of lpWaveHdr in callback
1729 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
1731 TRACE("waveHdr full.\n");
1733 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1734 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1736 widNotifyClient(wwi
, WIM_DATA
, (DWORD_PTR
)lpWaveHdr
, 0);
1737 lpWaveHdr
= wwi
->lpQueuePtr
= lpNext
;
1742 /* wait for dwSleepTime or an event in thread's queue */
1743 WAIT_OMR(&wwi
->msgRing
, dwSleepTime
);
1745 while (ESD_RetrieveRingMessage(&wwi
->msgRing
, &msg
, ¶m
, &ev
))
1747 TRACE("msg=%s param=0x%lx\n",wodPlayerCmdString
[msg
- WM_USER
- 1], param
);
1749 case WINE_WM_PAUSING
:
1750 wwi
->state
= WINE_WS_PAUSED
;
1752 /* Put code here to "pause" esd recording
1757 case WINE_WM_STARTING
:
1758 wwi
->state
= WINE_WS_PLAYING
;
1760 /* Put code here to "start" esd recording
1765 case WINE_WM_HEADER
:
1766 lpWaveHdr
= (LPWAVEHDR
)param
;
1767 /* insert buffer at end of queue */
1770 int num_headers
= 0;
1771 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
))
1779 case WINE_WM_STOPPING
:
1780 if (wwi
->state
!= WINE_WS_STOPPED
)
1783 /* Put code here to "stop" esd recording
1786 /* return current buffer to app */
1787 lpWaveHdr
= wwi
->lpQueuePtr
;
1790 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
1791 TRACE("stop %p %p\n", lpWaveHdr
, lpWaveHdr
->lpNext
);
1792 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1793 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1794 widNotifyClient(wwi
, WIM_DATA
, (DWORD_PTR
)lpWaveHdr
, 0);
1795 wwi
->lpQueuePtr
= lpNext
;
1798 wwi
->state
= WINE_WS_STOPPED
;
1801 case WINE_WM_RESETTING
:
1802 wwi
->state
= WINE_WS_STOPPED
;
1803 wwi
->dwRecordedTotal
= 0;
1805 /* return all buffers to the app */
1806 for (lpWaveHdr
= wwi
->lpQueuePtr
; lpWaveHdr
; lpWaveHdr
= lpWaveHdr
->lpNext
) {
1807 TRACE("reset %p %p\n", lpWaveHdr
, lpWaveHdr
->lpNext
);
1808 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1809 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1811 widNotifyClient(wwi
, WIM_DATA
, (DWORD_PTR
)lpWaveHdr
, 0);
1813 wwi
->lpQueuePtr
= NULL
;
1816 case WINE_WM_CLOSING
:
1818 wwi
->state
= WINE_WS_CLOSED
;
1821 /* shouldn't go here */
1823 FIXME("unknown message %d\n", msg
);
1829 /* just for not generating compilation warnings... should never be executed */
1833 /**************************************************************************
1834 * widOpen [internal]
1836 static DWORD
widOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1839 /* input esound... */
1840 int in_bits
= ESD_BITS16
, in_channels
= ESD_STEREO
, in_rate
;
1841 #ifdef WID_USE_ESDMON
1842 int in_mode
= ESD_STREAM
, in_func
= ESD_PLAY
;
1844 int in_mode
= ESD_STREAM
, in_func
= ESD_RECORD
;
1846 esd_format_t in_format
;
1849 TRACE("(%u, %p %08X);\n",wDevID
, lpDesc
, dwFlags
);
1850 if (lpDesc
== NULL
) {
1851 WARN("Invalid Parametr (lpDesc == NULL)!\n");
1852 return MMSYSERR_INVALPARAM
;
1855 if (wDevID
>= MAX_WAVEINDRV
) {
1856 TRACE ("MAX_WAVEINDRV reached !\n");
1857 return MMSYSERR_BADDEVICEID
;
1860 /* if this device is already open tell the app that it is allocated */
1861 if(WInDev
[wDevID
].esd_fd
!= -1)
1863 TRACE("device already allocated\n");
1864 return MMSYSERR_ALLOCATED
;
1867 /* only PCM format is support so far... */
1868 if (!supportedFormat(lpDesc
->lpFormat
)) {
1869 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1870 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1871 lpDesc
->lpFormat
->nSamplesPerSec
);
1872 return WAVERR_BADFORMAT
;
1875 if (dwFlags
& WAVE_FORMAT_QUERY
) {
1876 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1877 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1878 lpDesc
->lpFormat
->nSamplesPerSec
);
1879 return MMSYSERR_NOERROR
;
1882 wwi
= &WInDev
[wDevID
];
1884 /* direct sound not supported, ignore the flag */
1885 dwFlags
&= ~WAVE_DIRECTSOUND
;
1887 wwi
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1889 wwi
->waveDesc
= *lpDesc
;
1890 copy_format(lpDesc
->lpFormat
, &wwi
->waveFormat
);
1892 if (wwi
->waveFormat
.Format
.wBitsPerSample
== 0) {
1893 WARN("Resetting zerod wBitsPerSample\n");
1894 wwi
->waveFormat
.Format
.wBitsPerSample
= 8 *
1895 (wwi
->waveFormat
.Format
.nAvgBytesPerSec
/
1896 wwi
->waveFormat
.Format
.nSamplesPerSec
) /
1897 wwi
->waveFormat
.Format
.nChannels
;
1900 if (wwi
->waveFormat
.Format
.wBitsPerSample
== 8)
1901 in_bits
= ESD_BITS8
;
1902 else if (wwi
->waveFormat
.Format
.wBitsPerSample
== 16)
1903 in_bits
= ESD_BITS16
;
1905 if (wwi
->waveFormat
.Format
.nChannels
== 1)
1906 in_channels
= ESD_MONO
;
1907 else if (wwi
->waveFormat
.Format
.nChannels
== 2)
1908 in_channels
= ESD_STEREO
;
1910 in_format
= in_bits
| in_channels
| in_mode
| in_func
;
1911 in_rate
= (int) wwi
->waveFormat
.Format
.nSamplesPerSec
;
1912 TRACE("esd input format = 0x%08x, rate = %d\n", in_format
, in_rate
);
1914 #ifdef WID_USE_ESDMON
1915 wwi
->esd_fd
= esd_monitor_stream(in_format
, in_rate
, NULL
, "wineesd");
1917 wwi
->esd_fd
= esd_record_stream(in_format
, in_rate
, NULL
, "wineesd");
1919 TRACE("(wwi->esd_fd=%d)\n",wwi
->esd_fd
);
1920 wwi
->state
= WINE_WS_STOPPED
;
1922 if (wwi
->lpQueuePtr
) {
1923 WARN("Should have an empty queue (%p)\n", wwi
->lpQueuePtr
);
1924 wwi
->lpQueuePtr
= NULL
;
1927 if(wwi
->esd_fd
< 0) return MMSYSERR_ALLOCATED
;
1929 /* Set the esd socket O_NONBLOCK, so we can stop recording smoothly */
1930 mode
= fcntl(wwi
->esd_fd
, F_GETFL
);
1932 fcntl(wwi
->esd_fd
, F_SETFL
, mode
);
1934 wwi
->dwRecordedTotal
= 0;
1935 wwi
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1937 ESD_InitRingMessage(&wwi
->msgRing
);
1939 /* create recorder thread */
1940 if (!(dwFlags
& WAVE_DIRECTSOUND
)) {
1941 wwi
->hStartUpEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1942 wwi
->hThread
= CreateThread(NULL
, 0, widRecorder
, (LPVOID
)(DWORD_PTR
)wDevID
,
1944 WaitForSingleObject(wwi
->hStartUpEvent
, INFINITE
);
1945 CloseHandle(wwi
->hStartUpEvent
);
1947 wwi
->hThread
= INVALID_HANDLE_VALUE
;
1949 wwi
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
1951 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
1952 wwi
->waveFormat
.Format
.wBitsPerSample
, wwi
->waveFormat
.Format
.nAvgBytesPerSec
,
1953 wwi
->waveFormat
.Format
.nSamplesPerSec
, wwi
->waveFormat
.Format
.nChannels
,
1954 wwi
->waveFormat
.Format
.nBlockAlign
);
1955 return widNotifyClient(wwi
, WIM_OPEN
, 0L, 0L);
1958 /**************************************************************************
1959 * widClose [internal]
1961 static DWORD
widClose(WORD wDevID
)
1965 TRACE("(%u);\n", wDevID
);
1966 if (wDevID
>= MAX_WAVEINDRV
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1967 WARN("can't close !\n");
1968 return MMSYSERR_INVALHANDLE
;
1971 wwi
= &WInDev
[wDevID
];
1973 if (wwi
->lpQueuePtr
!= NULL
) {
1974 WARN("still buffers open !\n");
1975 return WAVERR_STILLPLAYING
;
1978 ESD_AddRingMessage(&wwi
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
1979 ESD_CloseWaveInDevice(wwi
);
1980 wwi
->state
= WINE_WS_CLOSED
;
1981 ESD_DestroyRingMessage(&wwi
->msgRing
);
1982 return widNotifyClient(wwi
, WIM_CLOSE
, 0L, 0L);
1985 /**************************************************************************
1986 * widAddBuffer [internal]
1988 static DWORD
widAddBuffer(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1990 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
1992 if (wDevID
>= MAX_WAVEINDRV
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
1993 WARN("can't do it !\n");
1994 return MMSYSERR_INVALHANDLE
;
1996 if (!(lpWaveHdr
->dwFlags
& WHDR_PREPARED
)) {
1997 TRACE("never been prepared !\n");
1998 return WAVERR_UNPREPARED
;
2000 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
) {
2001 TRACE("header already in use !\n");
2002 return WAVERR_STILLPLAYING
;
2005 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
2006 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
2007 lpWaveHdr
->dwBytesRecorded
= 0;
2008 lpWaveHdr
->lpNext
= NULL
;
2010 ESD_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_HEADER
,
2011 (DWORD_PTR
)lpWaveHdr
, FALSE
);
2012 return MMSYSERR_NOERROR
;
2015 /**************************************************************************
2016 * widStart [internal]
2018 static DWORD
widStart(WORD wDevID
)
2020 TRACE("(%u);\n", wDevID
);
2021 if (wDevID
>= MAX_WAVEINDRV
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2022 WARN("can't start recording !\n");
2023 return MMSYSERR_INVALHANDLE
;
2026 ESD_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STARTING
, 0, TRUE
);
2027 return MMSYSERR_NOERROR
;
2030 /**************************************************************************
2031 * widStop [internal]
2033 static DWORD
widStop(WORD wDevID
)
2035 TRACE("(%u);\n", wDevID
);
2036 if (wDevID
>= MAX_WAVEINDRV
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2037 WARN("can't stop !\n");
2038 return MMSYSERR_INVALHANDLE
;
2041 ESD_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_STOPPING
, 0, TRUE
);
2043 return MMSYSERR_NOERROR
;
2046 /**************************************************************************
2047 * widReset [internal]
2049 static DWORD
widReset(WORD wDevID
)
2051 TRACE("(%u);\n", wDevID
);
2052 if (wDevID
>= MAX_WAVEINDRV
|| WInDev
[wDevID
].state
== WINE_WS_CLOSED
) {
2053 WARN("can't reset !\n");
2054 return MMSYSERR_INVALHANDLE
;
2056 ESD_AddRingMessage(&WInDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
2057 return MMSYSERR_NOERROR
;
2060 /**************************************************************************
2061 * widMessage (WINEESD.6)
2063 DWORD WINAPI
ESD_widMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
2064 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
2066 TRACE("(%u, %04X, %08X, %08lX, %08lX);\n",
2067 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2073 /* FIXME: Pretend this is supported */
2075 case WIDM_OPEN
: return widOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
2076 case WIDM_CLOSE
: return widClose (wDevID
);
2077 case WIDM_ADDBUFFER
: return widAddBuffer (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
2078 case WIDM_PREPARE
: return MMSYSERR_NOTSUPPORTED
;
2079 case WIDM_UNPREPARE
: return MMSYSERR_NOTSUPPORTED
;
2080 case WIDM_GETDEVCAPS
: return widGetDevCaps (wDevID
, (LPWAVEINCAPSW
)dwParam1
, dwParam2
);
2081 case WIDM_GETNUMDEVS
: return widGetNumDevs ();
2082 case WIDM_RESET
: return widReset (wDevID
);
2083 case WIDM_START
: return widStart (wDevID
);
2084 case WIDM_STOP
: return widStop (wDevID
);
2085 case DRV_QUERYDEVICEINTERFACESIZE
: return widDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
2086 case DRV_QUERYDEVICEINTERFACE
: return widDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
2088 FIXME("unknown message %d!\n", wMsg
);
2090 return MMSYSERR_NOTSUPPORTED
;
2093 #else /* !HAVE_ESD */
2095 /**************************************************************************
2096 * wodMessage (WINEESD.@)
2098 DWORD WINAPI
ESD_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2099 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
2101 FIXME("(%u, %04X, %08X, %08lX, %08lX):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2102 return MMSYSERR_NOTENABLED
;
2105 /**************************************************************************
2106 * widMessage (WINEESD.6)
2108 DWORD WINAPI
ESD_widMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
2109 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
2111 FIXME("(%u, %04X, %08X, %08lX, %08lX):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2112 return MMSYSERR_NOTENABLED
;
2115 #endif /* HAVE_ESD */