2 * Wine Driver for CoreAudio based on Jack Driver
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999 Eric Pouech (async playing in waveOut/waveIn)
6 * Copyright 2000 Eric Pouech (loops in waveOut)
7 * Copyright 2002 Chris Morgan (jack version of this file)
8 * Copyright 2005, 2006 Emmanuel Maillard
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
45 #include "coreaudio.h"
46 #include "wine/unicode.h"
47 #include "wine/library.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(wave
);
53 #if defined(HAVE_COREAUDIO_COREAUDIO_H) && defined(HAVE_AUDIOUNIT_AUDIOUNIT_H)
54 #include <CoreAudio/CoreAudio.h>
55 #include <CoreFoundation/CoreFoundation.h>
56 #include <libkern/OSAtomic.h>
59 Due to AudioUnit headers conflict define some needed types.
62 typedef void *AudioUnit
;
64 /* From AudioUnit/AUComponents.h */
67 kAudioUnitRenderAction_OutputIsSilence
= (1 << 4),
68 /* provides hint on return from Render(): if set the buffer contains all zeroes */
70 typedef UInt32 AudioUnitRenderActionFlags
;
72 typedef long ComponentResult
;
73 extern ComponentResult
74 AudioUnitRender( AudioUnit ci
,
75 AudioUnitRenderActionFlags
* ioActionFlags
,
76 const AudioTimeStamp
* inTimeStamp
,
77 UInt32 inOutputBusNumber
,
78 UInt32 inNumberFrames
,
79 AudioBufferList
* ioData
) AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER
;
81 /* only allow 10 output devices through this driver, this ought to be adequate */
82 #define MAX_WAVEOUTDRV (1)
83 #define MAX_WAVEINDRV (1)
85 /* state diagram for waveOut writing:
87 * +---------+-------------+---------------+---------------------------------+
88 * | state | function | event | new state |
89 * +---------+-------------+---------------+---------------------------------+
90 * | | open() | | STOPPED |
91 * | PAUSED | write() | | PAUSED |
92 * | STOPPED | write() | <thrd create> | PLAYING |
93 * | PLAYING | write() | HEADER | PLAYING |
94 * | (other) | write() | <error> | |
95 * | (any) | pause() | PAUSING | PAUSED |
96 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
97 * | (any) | reset() | RESETTING | STOPPED |
98 * | (any) | close() | CLOSING | CLOSED |
99 * +---------+-------------+---------------+---------------------------------+
102 /* states of the playing device */
103 #define WINE_WS_PLAYING 0
104 #define WINE_WS_PAUSED 1
105 #define WINE_WS_STOPPED 2
106 #define WINE_WS_CLOSED 3
107 #define WINE_WS_OPENING 4
109 typedef struct tagCoreAudio_Device
{
113 char* interface_name
;
115 WAVEOUTCAPSW out_caps
;
117 DWORD in_caps_support
;
121 unsigned audio_fragment
;
123 BOOL bTriggerSupport
;
126 DSDRIVERDESC ds_desc
;
127 DSDRIVERCAPS ds_caps
;
128 DSCDRIVERCAPS dsc_caps
;
132 AudioDeviceID outputDeviceID
;
133 AudioDeviceID inputDeviceID
;
134 AudioStreamBasicDescription streamDescription
;
137 /* for now use the default device */
138 static CoreAudio_Device CoreAudio_DefaultDevice
;
141 volatile int state
; /* one of the WINE_WS_ manifest constants */
142 CoreAudio_Device
*cadev
;
143 WAVEOPENDESC waveDesc
;
145 PCMWAVEFORMAT format
;
148 AudioStreamBasicDescription streamDescription
;
151 char interface_name
[32];
152 LPWAVEHDR lpQueuePtr
; /* start of queued WAVEHDRs (waiting to be notified) */
153 LPWAVEHDR lpPlayPtr
; /* start of not yet fully played buffers */
154 DWORD dwPartialOffset
; /* Offset of not yet written bytes in lpPlayPtr */
156 LPWAVEHDR lpLoopPtr
; /* pointer of first buffer in loop, if any */
157 DWORD dwLoops
; /* private copy of loop counter */
159 DWORD dwPlayedTotal
; /* number of bytes actually played since opening */
160 DWORD dwWrittenTotal
; /* number of bytes written to OSS buffer since opening */
162 DWORD tickCountMS
; /* time in MS of last AudioUnit callback */
164 OSSpinLock lock
; /* synchronization stuff */
172 /* This device's device number */
175 /* Access to the following fields is synchronized across threads. */
177 LPWAVEHDR lpQueuePtr
;
178 DWORD dwTotalRecorded
;
180 /* Synchronization mechanism to protect above fields */
183 /* Capabilities description */
185 char interface_name
[32];
187 /* Record the arguments used when opening the device. */
188 WAVEOPENDESC waveDesc
;
190 PCMWAVEFORMAT format
;
193 AudioBufferList
*bufferList
;
194 AudioBufferList
*bufferListCopy
;
196 /* Record state of debug channels at open. Used to control fprintf's since
197 * we can't use Wine debug channel calls in non-Wine AudioUnit threads. */
202 /* These fields aren't used. */
204 CoreAudio_Device
*cadev
;
206 AudioStreamBasicDescription streamDescription
;
210 static WINE_WAVEOUT WOutDev
[MAX_WAVEOUTDRV
];
211 static WINE_WAVEIN WInDev
[MAX_WAVEINDRV
];
213 static HANDLE hThread
= NULL
; /* Track the thread we create so we can clean it up later */
214 static CFMessagePortRef Port_SendToMessageThread
;
216 static void wodHelper_PlayPtrNext(WINE_WAVEOUT
* wwo
);
217 static void wodHelper_NotifyDoneForList(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
);
218 static void wodHelper_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
);
219 static void widHelper_NotifyCompletions(WINE_WAVEIN
* wwi
);
221 extern int AudioUnit_CreateDefaultAudioUnit(void *wwo
, AudioUnit
*au
);
222 extern int AudioUnit_CloseAudioUnit(AudioUnit au
);
223 extern int AudioUnit_InitializeWithStreamDescription(AudioUnit au
, AudioStreamBasicDescription
*streamFormat
);
225 extern OSStatus
AudioOutputUnitStart(AudioUnit au
);
226 extern OSStatus
AudioOutputUnitStop(AudioUnit au
);
227 extern OSStatus
AudioUnitUninitialize(AudioUnit au
);
229 extern int AudioUnit_SetVolume(AudioUnit au
, float left
, float right
);
230 extern int AudioUnit_GetVolume(AudioUnit au
, float *left
, float *right
);
232 extern int AudioUnit_GetInputDeviceSampleRate(void);
234 extern int AudioUnit_CreateInputUnit(void* wwi
, AudioUnit
* out_au
,
235 WORD nChannels
, DWORD nSamplesPerSec
, WORD wBitsPerSample
,
236 UInt32
* outFrameCount
);
238 OSStatus
CoreAudio_woAudioUnitIOProc(void *inRefCon
,
239 AudioUnitRenderActionFlags
*ioActionFlags
,
240 const AudioTimeStamp
*inTimeStamp
,
242 UInt32 inNumberFrames
,
243 AudioBufferList
*ioData
);
244 OSStatus
CoreAudio_wiAudioUnitIOProc(void *inRefCon
,
245 AudioUnitRenderActionFlags
*ioActionFlags
,
246 const AudioTimeStamp
*inTimeStamp
,
248 UInt32 inNumberFrames
,
249 AudioBufferList
*ioData
);
251 /* These strings used only for tracing */
253 static const char * getMessage(UINT msg
)
255 static char unknown
[32];
256 #define MSG_TO_STR(x) case x: return #x
258 MSG_TO_STR(DRVM_INIT
);
259 MSG_TO_STR(DRVM_EXIT
);
260 MSG_TO_STR(DRVM_ENABLE
);
261 MSG_TO_STR(DRVM_DISABLE
);
262 MSG_TO_STR(WIDM_OPEN
);
263 MSG_TO_STR(WIDM_CLOSE
);
264 MSG_TO_STR(WIDM_ADDBUFFER
);
265 MSG_TO_STR(WIDM_PREPARE
);
266 MSG_TO_STR(WIDM_UNPREPARE
);
267 MSG_TO_STR(WIDM_GETDEVCAPS
);
268 MSG_TO_STR(WIDM_GETNUMDEVS
);
269 MSG_TO_STR(WIDM_GETPOS
);
270 MSG_TO_STR(WIDM_RESET
);
271 MSG_TO_STR(WIDM_START
);
272 MSG_TO_STR(WIDM_STOP
);
273 MSG_TO_STR(WODM_OPEN
);
274 MSG_TO_STR(WODM_CLOSE
);
275 MSG_TO_STR(WODM_WRITE
);
276 MSG_TO_STR(WODM_PAUSE
);
277 MSG_TO_STR(WODM_GETPOS
);
278 MSG_TO_STR(WODM_BREAKLOOP
);
279 MSG_TO_STR(WODM_PREPARE
);
280 MSG_TO_STR(WODM_UNPREPARE
);
281 MSG_TO_STR(WODM_GETDEVCAPS
);
282 MSG_TO_STR(WODM_GETNUMDEVS
);
283 MSG_TO_STR(WODM_GETPITCH
);
284 MSG_TO_STR(WODM_SETPITCH
);
285 MSG_TO_STR(WODM_GETPLAYBACKRATE
);
286 MSG_TO_STR(WODM_SETPLAYBACKRATE
);
287 MSG_TO_STR(WODM_GETVOLUME
);
288 MSG_TO_STR(WODM_SETVOLUME
);
289 MSG_TO_STR(WODM_RESTART
);
290 MSG_TO_STR(WODM_RESET
);
291 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE
);
292 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE
);
293 MSG_TO_STR(DRV_QUERYDSOUNDIFACE
);
294 MSG_TO_STR(DRV_QUERYDSOUNDDESC
);
297 sprintf(unknown
, "UNKNOWN(0x%04x)", msg
);
301 #define kStopLoopMessage 0
302 #define kWaveOutNotifyCompletionsMessage 1
303 #define kWaveInNotifyCompletionsMessage 2
305 /* Mach Message Handling */
306 static CFDataRef
wodMessageHandler(CFMessagePortRef port_ReceiveInMessageThread
, SInt32 msgid
, CFDataRef data
, void *info
)
308 UInt32
*buffer
= NULL
;
312 case kWaveOutNotifyCompletionsMessage
:
313 buffer
= (UInt32
*) CFDataGetBytePtr(data
);
314 wodHelper_NotifyCompletions(&WOutDev
[buffer
[0]], FALSE
);
316 case kWaveInNotifyCompletionsMessage
:
317 buffer
= (UInt32
*) CFDataGetBytePtr(data
);
318 widHelper_NotifyCompletions(&WInDev
[buffer
[0]]);
321 CFRunLoopStop(CFRunLoopGetCurrent());
328 static DWORD WINAPI
messageThread(LPVOID p
)
330 CFMessagePortRef port_ReceiveInMessageThread
= (CFMessagePortRef
) p
;
331 CFRunLoopSourceRef source
;
333 source
= CFMessagePortCreateRunLoopSource(kCFAllocatorDefault
, port_ReceiveInMessageThread
, (CFIndex
)0);
334 CFRunLoopAddSource(CFRunLoopGetCurrent(), source
, kCFRunLoopDefaultMode
);
338 CFRunLoopSourceInvalidate(source
);
340 CFRelease(port_ReceiveInMessageThread
);
345 /**************************************************************************
346 * wodSendNotifyCompletionsMessage [internal]
347 * Call from AudioUnit IO thread can't use Wine debug channels.
349 static void wodSendNotifyCompletionsMessage(WINE_WAVEOUT
* wwo
)
354 if (!Port_SendToMessageThread
)
357 buffer
= (UInt32
) wwo
->woID
;
359 data
= CFDataCreate(kCFAllocatorDefault
, (UInt8
*)&buffer
, sizeof(buffer
));
363 CFMessagePortSendRequest(Port_SendToMessageThread
, kWaveOutNotifyCompletionsMessage
, data
, 0.0, 0.0, NULL
, NULL
);
367 /**************************************************************************
368 * wodSendNotifyInputCompletionsMessage [internal]
369 * Call from AudioUnit IO thread can't use Wine debug channels.
371 static void wodSendNotifyInputCompletionsMessage(WINE_WAVEIN
* wwi
)
376 if (!Port_SendToMessageThread
)
379 buffer
= (UInt32
) wwi
->wiID
;
381 data
= CFDataCreate(kCFAllocatorDefault
, (UInt8
*)&buffer
, sizeof(buffer
));
385 CFMessagePortSendRequest(Port_SendToMessageThread
, kWaveInNotifyCompletionsMessage
, data
, 0.0, 0.0, NULL
, NULL
);
389 static DWORD
bytes_to_mmtime(LPMMTIME lpTime
, DWORD position
,
390 PCMWAVEFORMAT
* format
)
392 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
393 lpTime
->wType
, format
->wBitsPerSample
, format
->wf
.nSamplesPerSec
,
394 format
->wf
.nChannels
, format
->wf
.nAvgBytesPerSec
);
395 TRACE("Position in bytes=%u\n", position
);
397 switch (lpTime
->wType
) {
399 lpTime
->u
.sample
= position
/ (format
->wBitsPerSample
/ 8 * format
->wf
.nChannels
);
400 TRACE("TIME_SAMPLES=%u\n", lpTime
->u
.sample
);
403 lpTime
->u
.ms
= 1000.0 * position
/ (format
->wBitsPerSample
/ 8 * format
->wf
.nChannels
* format
->wf
.nSamplesPerSec
);
404 TRACE("TIME_MS=%u\n", lpTime
->u
.ms
);
407 lpTime
->u
.smpte
.fps
= 30;
408 position
= position
/ (format
->wBitsPerSample
/ 8 * format
->wf
.nChannels
);
409 position
+= (format
->wf
.nSamplesPerSec
/ lpTime
->u
.smpte
.fps
) - 1; /* round up */
410 lpTime
->u
.smpte
.sec
= position
/ format
->wf
.nSamplesPerSec
;
411 position
-= lpTime
->u
.smpte
.sec
* format
->wf
.nSamplesPerSec
;
412 lpTime
->u
.smpte
.min
= lpTime
->u
.smpte
.sec
/ 60;
413 lpTime
->u
.smpte
.sec
-= 60 * lpTime
->u
.smpte
.min
;
414 lpTime
->u
.smpte
.hour
= lpTime
->u
.smpte
.min
/ 60;
415 lpTime
->u
.smpte
.min
-= 60 * lpTime
->u
.smpte
.hour
;
416 lpTime
->u
.smpte
.fps
= 30;
417 lpTime
->u
.smpte
.frame
= position
* lpTime
->u
.smpte
.fps
/ format
->wf
.nSamplesPerSec
;
418 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
419 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
420 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
423 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime
->wType
);
424 lpTime
->wType
= TIME_BYTES
;
427 lpTime
->u
.cb
= position
;
428 TRACE("TIME_BYTES=%u\n", lpTime
->u
.cb
);
431 return MMSYSERR_NOERROR
;
434 /**************************************************************************
435 * CoreAudio_GetDevCaps [internal]
437 BOOL
CoreAudio_GetDevCaps (void)
441 AudioDeviceID devId
= CoreAudio_DefaultDevice
.outputDeviceID
;
443 char name
[MAXPNAMELEN
];
445 propertySize
= MAXPNAMELEN
;
446 status
= AudioDeviceGetProperty(devId
, 0 , FALSE
, kAudioDevicePropertyDeviceName
, &propertySize
, name
);
448 ERR("AudioHardwareGetProperty for kAudioDevicePropertyDeviceName return %c%c%c%c\n", (char) (status
>> 24),
449 (char) (status
>> 16),
450 (char) (status
>> 8),
455 memcpy(CoreAudio_DefaultDevice
.ds_desc
.szDesc
, name
, sizeof(name
));
456 strcpy(CoreAudio_DefaultDevice
.ds_desc
.szDrvname
, "winecoreaudio.drv");
457 MultiByteToWideChar(CP_UNIXCP
, 0, name
, sizeof(name
),
458 CoreAudio_DefaultDevice
.out_caps
.szPname
,
459 sizeof(CoreAudio_DefaultDevice
.out_caps
.szPname
) / sizeof(WCHAR
));
460 memcpy(CoreAudio_DefaultDevice
.dev_name
, name
, 32);
462 propertySize
= sizeof(CoreAudio_DefaultDevice
.streamDescription
);
463 status
= AudioDeviceGetProperty(devId
, 0, FALSE
, kAudioDevicePropertyStreamFormat
, &propertySize
, &CoreAudio_DefaultDevice
.streamDescription
);
464 if (status
!= noErr
) {
465 ERR("AudioHardwareGetProperty for kAudioDevicePropertyStreamFormat return %c%c%c%c\n", (char) (status
>> 24),
466 (char) (status
>> 16),
467 (char) (status
>> 8),
472 TRACE("Device Stream Description mSampleRate : %f\n mFormatID : %c%c%c%c\n"
473 "mFormatFlags : %lX\n mBytesPerPacket : %lu\n mFramesPerPacket : %lu\n"
474 "mBytesPerFrame : %lu\n mChannelsPerFrame : %lu\n mBitsPerChannel : %lu\n",
475 CoreAudio_DefaultDevice
.streamDescription
.mSampleRate
,
476 (char) (CoreAudio_DefaultDevice
.streamDescription
.mFormatID
>> 24),
477 (char) (CoreAudio_DefaultDevice
.streamDescription
.mFormatID
>> 16),
478 (char) (CoreAudio_DefaultDevice
.streamDescription
.mFormatID
>> 8),
479 (char) CoreAudio_DefaultDevice
.streamDescription
.mFormatID
,
480 CoreAudio_DefaultDevice
.streamDescription
.mFormatFlags
,
481 CoreAudio_DefaultDevice
.streamDescription
.mBytesPerPacket
,
482 CoreAudio_DefaultDevice
.streamDescription
.mFramesPerPacket
,
483 CoreAudio_DefaultDevice
.streamDescription
.mBytesPerFrame
,
484 CoreAudio_DefaultDevice
.streamDescription
.mChannelsPerFrame
,
485 CoreAudio_DefaultDevice
.streamDescription
.mBitsPerChannel
);
487 CoreAudio_DefaultDevice
.out_caps
.wMid
= 0xcafe;
488 CoreAudio_DefaultDevice
.out_caps
.wPid
= 0x0001;
490 CoreAudio_DefaultDevice
.out_caps
.vDriverVersion
= 0x0001;
491 CoreAudio_DefaultDevice
.out_caps
.dwFormats
= 0x00000000;
492 CoreAudio_DefaultDevice
.out_caps
.wReserved1
= 0;
493 CoreAudio_DefaultDevice
.out_caps
.dwSupport
= WAVECAPS_VOLUME
;
494 CoreAudio_DefaultDevice
.out_caps
.dwSupport
|= WAVECAPS_LRVOLUME
;
496 CoreAudio_DefaultDevice
.out_caps
.wChannels
= 2;
497 CoreAudio_DefaultDevice
.out_caps
.dwFormats
|= WAVE_FORMAT_4S16
;
502 /******************************************************************
505 * Initialize CoreAudio_DefaultDevice
507 LONG
CoreAudio_WaveInit(void)
512 CFStringRef messageThreadPortName
;
513 CFMessagePortRef port_ReceiveInMessageThread
;
518 /* number of sound cards */
519 AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices
, &propertySize
, NULL
);
520 propertySize
/= sizeof(AudioDeviceID
);
521 TRACE("sound cards : %lu\n", propertySize
);
523 /* Get the output device */
524 propertySize
= sizeof(CoreAudio_DefaultDevice
.outputDeviceID
);
525 status
= AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice
, &propertySize
, &CoreAudio_DefaultDevice
.outputDeviceID
);
527 ERR("AudioHardwareGetProperty return %c%c%c%c for kAudioHardwarePropertyDefaultOutputDevice\n", (char) (status
>> 24),
528 (char) (status
>> 16),
529 (char) (status
>> 8),
533 if (CoreAudio_DefaultDevice
.outputDeviceID
== kAudioDeviceUnknown
) {
534 ERR("AudioHardwareGetProperty: CoreAudio_DefaultDevice.outputDeviceID == kAudioDeviceUnknown\n");
538 if ( ! CoreAudio_GetDevCaps() )
541 CoreAudio_DefaultDevice
.interface_name
=HeapAlloc(GetProcessHeap(),0,strlen(CoreAudio_DefaultDevice
.dev_name
)+1);
542 strcpy(CoreAudio_DefaultDevice
.interface_name
, CoreAudio_DefaultDevice
.dev_name
);
544 for (i
= 0; i
< MAX_WAVEOUTDRV
; ++i
)
546 static const WCHAR wszWaveOutFormat
[] =
547 {'C','o','r','e','A','u','d','i','o',' ','W','a','v','e','O','u','t',' ','%','d',0};
549 WOutDev
[i
].state
= WINE_WS_CLOSED
;
550 WOutDev
[i
].cadev
= &CoreAudio_DefaultDevice
;
553 memset(&WOutDev
[i
].caps
, 0, sizeof(WOutDev
[i
].caps
));
555 WOutDev
[i
].caps
.wMid
= 0xcafe; /* Manufac ID */
556 WOutDev
[i
].caps
.wPid
= 0x0001; /* Product ID */
557 snprintfW(WOutDev
[i
].caps
.szPname
, sizeof(WOutDev
[i
].caps
.szPname
)/sizeof(WCHAR
), wszWaveOutFormat
, i
);
558 snprintf(WOutDev
[i
].interface_name
, sizeof(WOutDev
[i
].interface_name
), "winecoreaudio: %d", i
);
560 WOutDev
[i
].caps
.vDriverVersion
= 0x0001;
561 WOutDev
[i
].caps
.dwFormats
= 0x00000000;
562 WOutDev
[i
].caps
.dwSupport
= WAVECAPS_VOLUME
;
564 WOutDev
[i
].caps
.wChannels
= 2;
565 /* WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME; */ /* FIXME */
567 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_96M08
;
568 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_96S08
;
569 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_96M16
;
570 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_96S16
;
571 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_48M08
;
572 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_48S08
;
573 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_48M16
;
574 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_48S16
;
575 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M08
;
576 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S08
;
577 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S16
;
578 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M16
;
579 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M08
;
580 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S08
;
581 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M16
;
582 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S16
;
583 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M08
;
584 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S08
;
585 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M16
;
586 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S16
;
588 WOutDev
[i
].lock
= 0; /* initialize the mutex */
591 /* FIXME: implement sample rate conversion on input */
592 inputSampleRate
= AudioUnit_GetInputDeviceSampleRate();
594 for (i
= 0; i
< MAX_WAVEINDRV
; ++i
)
596 static const WCHAR wszWaveInFormat
[] =
597 {'C','o','r','e','A','u','d','i','o',' ','W','a','v','e','I','n',' ','%','d',0};
599 memset(&WInDev
[i
], 0, sizeof(WInDev
[i
]));
602 /* Establish preconditions for widOpen */
603 WInDev
[i
].state
= WINE_WS_CLOSED
;
604 WInDev
[i
].lock
= 0; /* initialize the mutex */
606 /* Fill in capabilities. widGetDevCaps can be called at any time. */
607 WInDev
[i
].caps
.wMid
= 0xcafe; /* Manufac ID */
608 WInDev
[i
].caps
.wPid
= 0x0001; /* Product ID */
609 WInDev
[i
].caps
.vDriverVersion
= 0x0001;
611 snprintfW(WInDev
[i
].caps
.szPname
, sizeof(WInDev
[i
].caps
.szPname
)/sizeof(WCHAR
), wszWaveInFormat
, i
);
612 snprintf(WInDev
[i
].interface_name
, sizeof(WInDev
[i
].interface_name
), "winecoreaudio in: %d", i
);
614 if (inputSampleRate
== 96000)
616 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_96M08
;
617 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_96S08
;
618 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_96M16
;
619 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_96S16
;
621 if (inputSampleRate
== 48000)
623 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_48M08
;
624 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_48S08
;
625 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_48M16
;
626 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_48S16
;
628 if (inputSampleRate
== 44100)
630 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M08
;
631 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S08
;
632 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M16
;
633 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S16
;
635 if (inputSampleRate
== 22050)
637 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M08
;
638 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S08
;
639 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M16
;
640 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S16
;
642 if (inputSampleRate
== 11025)
644 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M08
;
645 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S08
;
646 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M16
;
647 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S16
;
650 WInDev
[i
].caps
.wChannels
= 2;
653 /* create mach messages handler */
655 messageThreadPortName
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
656 CFSTR("WaveMessagePort.%d.%lu"), getpid(), (unsigned long)random());
657 if (!messageThreadPortName
)
659 ERR("Can't create message thread port name\n");
663 port_ReceiveInMessageThread
= CFMessagePortCreateLocal(kCFAllocatorDefault
, messageThreadPortName
,
664 &wodMessageHandler
, NULL
, NULL
);
665 if (!port_ReceiveInMessageThread
)
667 ERR("Can't create message thread local port\n");
668 CFRelease(messageThreadPortName
);
672 Port_SendToMessageThread
= CFMessagePortCreateRemote(kCFAllocatorDefault
, messageThreadPortName
);
673 CFRelease(messageThreadPortName
);
674 if (!Port_SendToMessageThread
)
676 ERR("Can't create port for sending to message thread\n");
677 CFRelease(port_ReceiveInMessageThread
);
681 /* Cannot WAIT for any events because we are called from the loader (which has a lock on loading stuff) */
682 /* We might want to wait for this thread to be created -- but we cannot -- not here at least */
683 /* Instead track the thread so we can clean it up later */
686 ERR("Message thread already started -- expect problems\n");
688 hThread
= CreateThread(NULL
, 0, messageThread
, (LPVOID
)port_ReceiveInMessageThread
, 0, NULL
);
691 ERR("Can't create message thread\n");
692 CFRelease(port_ReceiveInMessageThread
);
693 CFRelease(Port_SendToMessageThread
);
694 Port_SendToMessageThread
= NULL
;
698 /* The message thread is responsible for releasing port_ReceiveInMessageThread. */
703 void CoreAudio_WaveRelease(void)
705 /* Stop CFRunLoop in messageThread */
708 if (!Port_SendToMessageThread
)
711 CFMessagePortSendRequest(Port_SendToMessageThread
, kStopLoopMessage
, NULL
, 0.0, 0.0, NULL
, NULL
);
712 CFRelease(Port_SendToMessageThread
);
713 Port_SendToMessageThread
= NULL
;
715 /* Wait for the thread to finish and clean it up */
716 /* This rids us of any quick start/shutdown driver crashes */
717 WaitForSingleObject(hThread
, INFINITE
);
718 CloseHandle(hThread
);
722 /*======================================================================*
723 * Low level WAVE OUT implementation *
724 *======================================================================*/
726 /**************************************************************************
727 * wodNotifyClient [internal]
729 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
735 if (wwo
->wFlags
!= DCB_NULL
&&
736 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
,
737 (HDRVR
)wwo
->waveDesc
.hWave
, wMsg
, wwo
->waveDesc
.dwInstance
,
740 return MMSYSERR_ERROR
;
744 return MMSYSERR_INVALPARAM
;
746 return MMSYSERR_NOERROR
;
750 /**************************************************************************
751 * wodGetDevCaps [internal]
753 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSW lpCaps
, DWORD dwSize
)
755 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
757 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
759 if (wDevID
>= MAX_WAVEOUTDRV
)
761 TRACE("MAX_WAVOUTDRV reached !\n");
762 return MMSYSERR_BADDEVICEID
;
765 TRACE("dwSupport=(0x%x), dwFormats=(0x%x)\n", WOutDev
[wDevID
].caps
.dwSupport
, WOutDev
[wDevID
].caps
.dwFormats
);
766 memcpy(lpCaps
, &WOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
767 return MMSYSERR_NOERROR
;
770 /**************************************************************************
773 static DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
777 AudioStreamBasicDescription streamFormat
;
778 AudioUnit audioUnit
= NULL
;
779 BOOL auInited
= FALSE
;
781 TRACE("(%u, %p, %08x);\n", wDevID
, lpDesc
, dwFlags
);
784 WARN("Invalid Parameter !\n");
785 return MMSYSERR_INVALPARAM
;
787 if (wDevID
>= MAX_WAVEOUTDRV
) {
788 TRACE("MAX_WAVOUTDRV reached !\n");
789 return MMSYSERR_BADDEVICEID
;
792 TRACE("Format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
793 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
794 lpDesc
->lpFormat
->nSamplesPerSec
, lpDesc
->lpFormat
->wBitsPerSample
);
796 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
797 lpDesc
->lpFormat
->nChannels
== 0 ||
798 lpDesc
->lpFormat
->nSamplesPerSec
== 0
801 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
802 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
803 lpDesc
->lpFormat
->nSamplesPerSec
, lpDesc
->lpFormat
->wBitsPerSample
);
804 return WAVERR_BADFORMAT
;
807 if (dwFlags
& WAVE_FORMAT_QUERY
)
809 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
810 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
811 lpDesc
->lpFormat
->nSamplesPerSec
);
812 return MMSYSERR_NOERROR
;
815 /* We proceed in three phases:
816 * o Reserve the device for us, marking it as unavailable (not closed)
817 * o Create, configure, and start the Audio Unit. To avoid deadlock,
818 * this has to be done without holding wwo->lock.
819 * o If that was successful, finish setting up our device info and
820 * mark the device as ready.
821 * Otherwise, clean up and mark the device as available.
823 wwo
= &WOutDev
[wDevID
];
824 if (!OSSpinLockTry(&wwo
->lock
))
825 return MMSYSERR_ALLOCATED
;
827 if (wwo
->state
!= WINE_WS_CLOSED
)
829 OSSpinLockUnlock(&wwo
->lock
);
830 return MMSYSERR_ALLOCATED
;
833 wwo
->state
= WINE_WS_OPENING
;
834 wwo
->audioUnit
= NULL
;
835 OSSpinLockUnlock(&wwo
->lock
);
838 if (!AudioUnit_CreateDefaultAudioUnit((void *) wwo
, &audioUnit
))
840 ERR("CoreAudio_CreateDefaultAudioUnit(%p) failed\n", wwo
);
841 ret
= MMSYSERR_ERROR
;
845 streamFormat
.mFormatID
= kAudioFormatLinearPCM
;
846 streamFormat
.mFormatFlags
= kLinearPCMFormatFlagIsPacked
;
847 /* FIXME check for 32bits float -> kLinearPCMFormatFlagIsFloat */
848 if (lpDesc
->lpFormat
->wBitsPerSample
!= 8)
849 streamFormat
.mFormatFlags
|= kLinearPCMFormatFlagIsSignedInteger
;
850 # ifdef WORDS_BIGENDIAN
851 streamFormat
.mFormatFlags
|= kLinearPCMFormatFlagIsBigEndian
; /* FIXME Wave format is little endian */
854 streamFormat
.mSampleRate
= lpDesc
->lpFormat
->nSamplesPerSec
;
855 streamFormat
.mChannelsPerFrame
= lpDesc
->lpFormat
->nChannels
;
856 streamFormat
.mFramesPerPacket
= 1;
857 streamFormat
.mBitsPerChannel
= lpDesc
->lpFormat
->wBitsPerSample
;
858 streamFormat
.mBytesPerFrame
= streamFormat
.mBitsPerChannel
* streamFormat
.mChannelsPerFrame
/ 8;
859 streamFormat
.mBytesPerPacket
= streamFormat
.mBytesPerFrame
* streamFormat
.mFramesPerPacket
;
861 ret
= AudioUnit_InitializeWithStreamDescription(audioUnit
, &streamFormat
);
864 ret
= WAVERR_BADFORMAT
; /* FIXME return an error based on the OSStatus */
869 /* Our render callback CoreAudio_woAudioUnitIOProc may be called before
870 * AudioOutputUnitStart returns. Core Audio will grab its own internal
871 * lock before calling it and the callback grabs wwo->lock. This would
872 * deadlock if we were holding wwo->lock.
873 * Also, the callback has to safely do nothing in that case, because
874 * wwo hasn't been completely filled out, yet. */
875 ret
= AudioOutputUnitStart(audioUnit
);
878 ERR("AudioOutputUnitStart failed: %08x\n", ret
);
879 ret
= MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
884 OSSpinLockLock(&wwo
->lock
);
885 assert(wwo
->state
== WINE_WS_OPENING
);
887 wwo
->audioUnit
= audioUnit
;
888 wwo
->streamDescription
= streamFormat
;
890 wwo
->state
= WINE_WS_STOPPED
;
892 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
894 wwo
->waveDesc
= *lpDesc
;
895 memcpy(&wwo
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
897 if (wwo
->format
.wBitsPerSample
== 0) {
898 WARN("Resetting zeroed wBitsPerSample\n");
899 wwo
->format
.wBitsPerSample
= 8 *
900 (wwo
->format
.wf
.nAvgBytesPerSec
/
901 wwo
->format
.wf
.nSamplesPerSec
) /
902 wwo
->format
.wf
.nChannels
;
905 wwo
->dwPlayedTotal
= 0;
906 wwo
->dwWrittenTotal
= 0;
908 wwo
->trace_on
= TRACE_ON(wave
);
909 wwo
->warn_on
= WARN_ON(wave
);
910 wwo
->err_on
= ERR_ON(wave
);
912 OSSpinLockUnlock(&wwo
->lock
);
914 ret
= wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
922 AudioUnitUninitialize(audioUnit
);
923 AudioUnit_CloseAudioUnit(audioUnit
);
926 OSSpinLockLock(&wwo
->lock
);
927 assert(wwo
->state
== WINE_WS_OPENING
);
928 wwo
->state
= WINE_WS_CLOSED
;
929 OSSpinLockUnlock(&wwo
->lock
);
934 /**************************************************************************
935 * wodClose [internal]
937 static DWORD
wodClose(WORD wDevID
)
939 DWORD ret
= MMSYSERR_NOERROR
;
942 TRACE("(%u);\n", wDevID
);
944 if (wDevID
>= MAX_WAVEOUTDRV
)
946 WARN("bad device ID !\n");
947 return MMSYSERR_BADDEVICEID
;
950 wwo
= &WOutDev
[wDevID
];
951 OSSpinLockLock(&wwo
->lock
);
954 WARN("buffers still playing !\n");
955 OSSpinLockUnlock(&wwo
->lock
);
956 ret
= WAVERR_STILLPLAYING
;
960 /* sanity check: this should not happen since the device must have been reset before */
961 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
963 wwo
->state
= WINE_WS_CLOSED
; /* mark the device as closed */
965 OSSpinLockUnlock(&wwo
->lock
);
967 err
= AudioUnitUninitialize(wwo
->audioUnit
);
969 ERR("AudioUnitUninitialize return %c%c%c%c\n", (char) (err
>> 24),
973 return MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
976 if ( !AudioUnit_CloseAudioUnit(wwo
->audioUnit
) )
978 ERR("Can't close AudioUnit\n");
979 return MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
982 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
988 /**************************************************************************
989 * wodPrepare [internal]
991 static DWORD
wodPrepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
993 TRACE("(%u, %p, %08x);\n", wDevID
, lpWaveHdr
, dwSize
);
995 if (wDevID
>= MAX_WAVEOUTDRV
) {
996 WARN("bad device ID !\n");
997 return MMSYSERR_BADDEVICEID
;
1000 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1001 return WAVERR_STILLPLAYING
;
1003 lpWaveHdr
->dwFlags
|= WHDR_PREPARED
;
1004 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1006 return MMSYSERR_NOERROR
;
1009 /**************************************************************************
1010 * wodUnprepare [internal]
1012 static DWORD
wodUnprepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1014 TRACE("(%u, %p, %08x);\n", wDevID
, lpWaveHdr
, dwSize
);
1016 if (wDevID
>= MAX_WAVEOUTDRV
) {
1017 WARN("bad device ID !\n");
1018 return MMSYSERR_BADDEVICEID
;
1021 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1022 return WAVERR_STILLPLAYING
;
1024 lpWaveHdr
->dwFlags
&= ~WHDR_PREPARED
;
1025 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1027 return MMSYSERR_NOERROR
;
1031 /**************************************************************************
1032 * wodHelper_CheckForLoopBegin [internal]
1034 * Check if the new waveheader is the beginning of a loop, and set up
1036 * This is called with the WAVEOUT lock held.
1037 * Call from AudioUnit IO thread can't use Wine debug channels.
1039 static void wodHelper_CheckForLoopBegin(WINE_WAVEOUT
* wwo
)
1041 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
1043 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)
1048 fprintf(stderr
, "warn:winecoreaudio:wodHelper_CheckForLoopBegin Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
1053 fprintf(stderr
, "trace:winecoreaudio:wodHelper_CheckForLoopBegin Starting loop (%dx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
1055 wwo
->lpLoopPtr
= lpWaveHdr
;
1056 /* Windows does not touch WAVEHDR.dwLoops,
1057 * so we need to make an internal copy */
1058 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
1064 /**************************************************************************
1065 * wodHelper_PlayPtrNext [internal]
1067 * Advance the play pointer to the next waveheader, looping if required.
1068 * This is called with the WAVEOUT lock held.
1069 * Call from AudioUnit IO thread can't use Wine debug channels.
1071 static void wodHelper_PlayPtrNext(WINE_WAVEOUT
* wwo
)
1073 BOOL didLoopBack
= FALSE
;
1075 wwo
->dwPartialOffset
= 0;
1076 if ((wwo
->lpPlayPtr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
)
1078 /* We're at the end of a loop, loop if required */
1079 if (wwo
->dwLoops
> 1)
1082 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
1087 wwo
->lpLoopPtr
= NULL
;
1092 /* We didn't loop back. Advance to the next wave header */
1093 wwo
->lpPlayPtr
= wwo
->lpPlayPtr
->lpNext
;
1095 if (!wwo
->lpPlayPtr
)
1096 wwo
->state
= WINE_WS_STOPPED
;
1098 wodHelper_CheckForLoopBegin(wwo
);
1102 /* Send the "done" notification for each WAVEHDR in a list. The list must be
1103 * free-standing. It should not be part of a device's queue.
1104 * This function must be called with the WAVEOUT lock *not* held. Furthermore,
1105 * it does not lock it, itself. That's because the callback to the application
1106 * may prompt the application to operate on the device, and we don't want to
1109 static void wodHelper_NotifyDoneForList(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
1113 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
1115 lpWaveHdr
->lpNext
= NULL
;
1116 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1117 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1118 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
1124 /* if force is TRUE then notify the client that all the headers were completed
1126 static void wodHelper_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
1128 LPWAVEHDR lpFirstDoneWaveHdr
= NULL
;
1130 OSSpinLockLock(&wwo
->lock
);
1132 /* First, excise all of the done headers from the queue into
1133 * a free-standing list. */
1136 lpFirstDoneWaveHdr
= wwo
->lpQueuePtr
;
1137 wwo
->lpQueuePtr
= NULL
;
1141 LPWAVEHDR lpWaveHdr
;
1142 LPWAVEHDR lpLastDoneWaveHdr
= NULL
;
1144 /* Start from lpQueuePtr and keep notifying until:
1145 * - we hit an unwritten wavehdr
1146 * - we hit the beginning of a running loop
1147 * - we hit a wavehdr which hasn't finished playing
1150 lpWaveHdr
= wwo
->lpQueuePtr
;
1152 lpWaveHdr
!= wwo
->lpPlayPtr
&&
1153 lpWaveHdr
!= wwo
->lpLoopPtr
;
1154 lpWaveHdr
= lpWaveHdr
->lpNext
1157 if (!lpFirstDoneWaveHdr
)
1158 lpFirstDoneWaveHdr
= lpWaveHdr
;
1159 lpLastDoneWaveHdr
= lpWaveHdr
;
1162 if (lpLastDoneWaveHdr
)
1164 wwo
->lpQueuePtr
= lpLastDoneWaveHdr
->lpNext
;
1165 lpLastDoneWaveHdr
->lpNext
= NULL
;
1169 OSSpinLockUnlock(&wwo
->lock
);
1171 /* Now, send the "done" notification for each header in our list. */
1172 wodHelper_NotifyDoneForList(wwo
, lpFirstDoneWaveHdr
);
1176 /**************************************************************************
1177 * wodWrite [internal]
1180 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1185 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
1187 /* first, do the sanity checks... */
1188 if (wDevID
>= MAX_WAVEOUTDRV
)
1190 WARN("bad dev ID !\n");
1191 return MMSYSERR_BADDEVICEID
;
1194 wwo
= &WOutDev
[wDevID
];
1196 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
1198 TRACE("unprepared\n");
1199 return WAVERR_UNPREPARED
;
1202 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1204 TRACE("still playing\n");
1205 return WAVERR_STILLPLAYING
;
1208 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1209 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
1210 lpWaveHdr
->lpNext
= 0;
1212 OSSpinLockLock(&wwo
->lock
);
1213 /* insert buffer at the end of queue */
1214 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
))
1218 if (!wwo
->lpPlayPtr
)
1220 wwo
->lpPlayPtr
= lpWaveHdr
;
1222 if (wwo
->state
== WINE_WS_STOPPED
)
1223 wwo
->state
= WINE_WS_PLAYING
;
1225 wodHelper_CheckForLoopBegin(wwo
);
1227 wwo
->dwPartialOffset
= 0;
1229 OSSpinLockUnlock(&wwo
->lock
);
1231 return MMSYSERR_NOERROR
;
1234 /**************************************************************************
1235 * wodPause [internal]
1237 static DWORD
wodPause(WORD wDevID
)
1241 TRACE("(%u);!\n", wDevID
);
1243 if (wDevID
>= MAX_WAVEOUTDRV
)
1245 WARN("bad device ID !\n");
1246 return MMSYSERR_BADDEVICEID
;
1249 /* The order of the following operations is important since we can't hold
1250 * the mutex while we make an Audio Unit call. Stop the Audio Unit before
1251 * setting the PAUSED state. In wodRestart, the order is reversed. This
1252 * guarantees that we can't get into a situation where the state is
1253 * PLAYING or STOPPED but the Audio Unit isn't running. Although we can
1254 * be in PAUSED state with the Audio Unit still running, that's harmless
1255 * because the render callback will just produce silence.
1257 status
= AudioOutputUnitStop(WOutDev
[wDevID
].audioUnit
);
1259 WARN("AudioOutputUnitStop return %c%c%c%c\n",
1260 (char) (status
>> 24), (char) (status
>> 16), (char) (status
>> 8), (char) status
);
1263 OSSpinLockLock(&WOutDev
[wDevID
].lock
);
1264 if (WOutDev
[wDevID
].state
== WINE_WS_PLAYING
|| WOutDev
[wDevID
].state
== WINE_WS_STOPPED
)
1265 WOutDev
[wDevID
].state
= WINE_WS_PAUSED
;
1266 OSSpinLockUnlock(&WOutDev
[wDevID
].lock
);
1268 return MMSYSERR_NOERROR
;
1271 /**************************************************************************
1272 * wodRestart [internal]
1274 static DWORD
wodRestart(WORD wDevID
)
1278 TRACE("(%u);\n", wDevID
);
1280 if (wDevID
>= MAX_WAVEOUTDRV
)
1282 WARN("bad device ID !\n");
1283 return MMSYSERR_BADDEVICEID
;
1286 /* The order of the following operations is important since we can't hold
1287 * the mutex while we make an Audio Unit call. Set the PLAYING/STOPPED
1288 * state before starting the Audio Unit. In wodPause, the order is
1289 * reversed. This guarantees that we can't get into a situation where
1290 * the state is PLAYING or STOPPED but the Audio Unit isn't running.
1291 * Although we can be in PAUSED state with the Audio Unit still running,
1292 * that's harmless because the render callback will just produce silence.
1294 OSSpinLockLock(&WOutDev
[wDevID
].lock
);
1295 if (WOutDev
[wDevID
].state
== WINE_WS_PAUSED
)
1297 if (WOutDev
[wDevID
].lpPlayPtr
)
1298 WOutDev
[wDevID
].state
= WINE_WS_PLAYING
;
1300 WOutDev
[wDevID
].state
= WINE_WS_STOPPED
;
1302 OSSpinLockUnlock(&WOutDev
[wDevID
].lock
);
1304 status
= AudioOutputUnitStart(WOutDev
[wDevID
].audioUnit
);
1306 ERR("AudioOutputUnitStart return %c%c%c%c\n",
1307 (char) (status
>> 24), (char) (status
>> 16), (char) (status
>> 8), (char) status
);
1308 return MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
1311 return MMSYSERR_NOERROR
;
1314 /**************************************************************************
1315 * wodReset [internal]
1317 static DWORD
wodReset(WORD wDevID
)
1321 LPWAVEHDR lpSavedQueuePtr
;
1323 TRACE("(%u);\n", wDevID
);
1325 if (wDevID
>= MAX_WAVEOUTDRV
)
1327 WARN("bad device ID !\n");
1328 return MMSYSERR_BADDEVICEID
;
1331 wwo
= &WOutDev
[wDevID
];
1333 OSSpinLockLock(&wwo
->lock
);
1335 if (wwo
->state
== WINE_WS_CLOSED
|| wwo
->state
== WINE_WS_OPENING
)
1337 OSSpinLockUnlock(&wwo
->lock
);
1338 WARN("resetting a closed device\n");
1339 return MMSYSERR_INVALHANDLE
;
1342 lpSavedQueuePtr
= wwo
->lpQueuePtr
;
1343 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
1344 wwo
->state
= WINE_WS_STOPPED
;
1345 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
= 0;
1347 wwo
->dwPartialOffset
= 0; /* Clear partial wavehdr */
1349 OSSpinLockUnlock(&wwo
->lock
);
1351 status
= AudioOutputUnitStart(wwo
->audioUnit
);
1354 ERR( "AudioOutputUnitStart return %c%c%c%c\n",
1355 (char) (status
>> 24), (char) (status
>> 16), (char) (status
>> 8), (char) status
);
1356 return MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
1359 /* Now, send the "done" notification for each header in our list. */
1360 /* Do this last so the reset operation is effectively complete before the
1361 * app does whatever it's going to do in response to these notifications. */
1362 wodHelper_NotifyDoneForList(wwo
, lpSavedQueuePtr
);
1364 return MMSYSERR_NOERROR
;
1367 /**************************************************************************
1368 * wodBreakLoop [internal]
1370 static DWORD
wodBreakLoop(WORD wDevID
)
1374 TRACE("(%u);\n", wDevID
);
1376 if (wDevID
>= MAX_WAVEOUTDRV
)
1378 WARN("bad device ID !\n");
1379 return MMSYSERR_BADDEVICEID
;
1382 wwo
= &WOutDev
[wDevID
];
1384 OSSpinLockLock(&wwo
->lock
);
1386 if (wwo
->lpLoopPtr
!= NULL
)
1388 /* ensure exit at end of current loop */
1392 OSSpinLockUnlock(&wwo
->lock
);
1394 return MMSYSERR_NOERROR
;
1397 /**************************************************************************
1398 * wodGetPosition [internal]
1400 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
1405 TRACE("(%u, %p, %u);\n", wDevID
, lpTime
, uSize
);
1407 if (wDevID
>= MAX_WAVEOUTDRV
)
1409 WARN("bad device ID !\n");
1410 return MMSYSERR_BADDEVICEID
;
1413 /* if null pointer to time structure return error */
1414 if (lpTime
== NULL
) return MMSYSERR_INVALPARAM
;
1416 wwo
= &WOutDev
[wDevID
];
1418 OSSpinLockLock(&WOutDev
[wDevID
].lock
);
1419 val
= wwo
->dwPlayedTotal
;
1420 OSSpinLockUnlock(&WOutDev
[wDevID
].lock
);
1422 return bytes_to_mmtime(lpTime
, val
, &wwo
->format
);
1425 /**************************************************************************
1426 * wodGetVolume [internal]
1428 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
1433 if (wDevID
>= MAX_WAVEOUTDRV
)
1435 WARN("bad device ID !\n");
1436 return MMSYSERR_BADDEVICEID
;
1439 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
1441 AudioUnit_GetVolume(WOutDev
[wDevID
].audioUnit
, &left
, &right
);
1443 *lpdwVol
= ((WORD
) left
* 0xFFFFl
) + (((WORD
) right
* 0xFFFFl
) << 16);
1445 return MMSYSERR_NOERROR
;
1448 /**************************************************************************
1449 * wodSetVolume [internal]
1451 static DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
1456 if (wDevID
>= MAX_WAVEOUTDRV
)
1458 WARN("bad device ID !\n");
1459 return MMSYSERR_BADDEVICEID
;
1462 left
= LOWORD(dwParam
) / 65535.0f
;
1463 right
= HIWORD(dwParam
) / 65535.0f
;
1465 TRACE("(%u, %08x);\n", wDevID
, dwParam
);
1467 AudioUnit_SetVolume(WOutDev
[wDevID
].audioUnit
, left
, right
);
1469 return MMSYSERR_NOERROR
;
1472 /**************************************************************************
1473 * wodGetNumDevs [internal]
1475 static DWORD
wodGetNumDevs(void)
1478 return MAX_WAVEOUTDRV
;
1481 /**************************************************************************
1482 * wodDevInterfaceSize [internal]
1484 static DWORD
wodDevInterfaceSize(UINT wDevID
, LPDWORD dwParam1
)
1486 TRACE("(%u, %p)\n", wDevID
, dwParam1
);
1488 *dwParam1
= MultiByteToWideChar(CP_UNIXCP
, 0, WOutDev
[wDevID
].cadev
->interface_name
, -1,
1489 NULL
, 0 ) * sizeof(WCHAR
);
1490 return MMSYSERR_NOERROR
;
1493 /**************************************************************************
1494 * wodDevInterface [internal]
1496 static DWORD
wodDevInterface(UINT wDevID
, PWCHAR dwParam1
, DWORD dwParam2
)
1499 if (dwParam2
>= MultiByteToWideChar(CP_UNIXCP
, 0, WOutDev
[wDevID
].cadev
->interface_name
, -1,
1500 NULL
, 0 ) * sizeof(WCHAR
))
1502 MultiByteToWideChar(CP_UNIXCP
, 0, WOutDev
[wDevID
].cadev
->interface_name
, -1,
1503 dwParam1
, dwParam2
/ sizeof(WCHAR
));
1504 return MMSYSERR_NOERROR
;
1506 return MMSYSERR_INVALPARAM
;
1509 /**************************************************************************
1510 * widDsCreate [internal]
1512 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
1514 TRACE("(%d,%p)\n",wDevID
,drv
);
1516 FIXME("DirectSound not implemented\n");
1517 FIXME("The (slower) DirectSound HEL mode will be used instead.\n");
1518 return MMSYSERR_NOTSUPPORTED
;
1521 /**************************************************************************
1522 * wodDsDesc [internal]
1524 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
1526 /* The DirectSound HEL will automatically wrap a non-DirectSound-capable
1527 * driver in a DirectSound adaptor, thus allowing the driver to be used by
1528 * DirectSound clients. However, it only does this if we respond
1529 * successfully to the DRV_QUERYDSOUNDDESC message. It's enough to fill in
1530 * the driver and device names of the description output parameter. */
1531 *desc
= WOutDev
[wDevID
].cadev
->ds_desc
;
1532 return MMSYSERR_NOERROR
;
1535 /**************************************************************************
1536 * wodMessage (WINECOREAUDIO.7)
1538 DWORD WINAPI
CoreAudio_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1539 DWORD dwParam1
, DWORD dwParam2
)
1541 TRACE("(%u, %s, %08x, %08x, %08x);\n",
1542 wDevID
, getMessage(wMsg
), dwUser
, dwParam1
, dwParam2
);
1550 /* FIXME: Pretend this is supported */
1552 case WODM_OPEN
: return wodOpen(wDevID
, (LPWAVEOPENDESC
) dwParam1
, dwParam2
);
1553 case WODM_CLOSE
: return wodClose(wDevID
);
1554 case WODM_WRITE
: return wodWrite(wDevID
, (LPWAVEHDR
) dwParam1
, dwParam2
);
1555 case WODM_PAUSE
: return wodPause(wDevID
);
1556 case WODM_GETPOS
: return wodGetPosition(wDevID
, (LPMMTIME
) dwParam1
, dwParam2
);
1557 case WODM_BREAKLOOP
: return wodBreakLoop(wDevID
);
1558 case WODM_PREPARE
: return wodPrepare(wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1559 case WODM_UNPREPARE
: return wodUnprepare(wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1561 case WODM_GETDEVCAPS
: return wodGetDevCaps(wDevID
, (LPWAVEOUTCAPSW
) dwParam1
, dwParam2
);
1562 case WODM_GETNUMDEVS
: return wodGetNumDevs();
1566 case WODM_GETPLAYBACKRATE
:
1567 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1568 case WODM_GETVOLUME
: return wodGetVolume(wDevID
, (LPDWORD
)dwParam1
);
1569 case WODM_SETVOLUME
: return wodSetVolume(wDevID
, dwParam1
);
1570 case WODM_RESTART
: return wodRestart(wDevID
);
1571 case WODM_RESET
: return wodReset(wDevID
);
1573 case DRV_QUERYDEVICEINTERFACESIZE
: return wodDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
1574 case DRV_QUERYDEVICEINTERFACE
: return wodDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
1575 case DRV_QUERYDSOUNDIFACE
: return wodDsCreate (wDevID
, (PIDSDRIVER
*)dwParam1
);
1576 case DRV_QUERYDSOUNDDESC
: return wodDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
1579 FIXME("unknown message %d!\n", wMsg
);
1582 return MMSYSERR_NOTSUPPORTED
;
1585 /*======================================================================*
1586 * Low level DSOUND implementation *
1587 *======================================================================*/
1589 typedef struct IDsDriverImpl IDsDriverImpl
;
1590 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
1592 struct IDsDriverImpl
1594 /* IUnknown fields */
1595 const IDsDriverVtbl
*lpVtbl
;
1597 /* IDsDriverImpl fields */
1599 IDsDriverBufferImpl
*primary
;
1602 struct IDsDriverBufferImpl
1604 /* IUnknown fields */
1605 const IDsDriverBufferVtbl
*lpVtbl
;
1607 /* IDsDriverBufferImpl fields */
1614 CoreAudio IO threaded callback,
1615 we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
1617 OSStatus
CoreAudio_woAudioUnitIOProc(void *inRefCon
,
1618 AudioUnitRenderActionFlags
*ioActionFlags
,
1619 const AudioTimeStamp
*inTimeStamp
,
1621 UInt32 inNumberFrames
,
1622 AudioBufferList
*ioData
)
1625 WINE_WAVEOUT
*wwo
= (WINE_WAVEOUT
*) inRefCon
;
1628 unsigned int dataNeeded
= ioData
->mBuffers
[0].mDataByteSize
;
1629 unsigned int dataProvided
= 0;
1631 OSSpinLockLock(&wwo
->lock
);
1633 /* We might have been called before wwo has been completely filled out by
1634 * wodOpen. We have to do nothing in that case. The check of wwo->state
1635 * below ensures that. */
1636 while (dataNeeded
> 0 && wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpPlayPtr
)
1638 unsigned int available
= wwo
->lpPlayPtr
->dwBufferLength
- wwo
->dwPartialOffset
;
1639 unsigned int toCopy
;
1641 if (available
>= dataNeeded
)
1642 toCopy
= dataNeeded
;
1648 memcpy((char*)ioData
->mBuffers
[0].mData
+ dataProvided
,
1649 wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
, toCopy
);
1650 wwo
->dwPartialOffset
+= toCopy
;
1651 wwo
->dwPlayedTotal
+= toCopy
;
1652 dataProvided
+= toCopy
;
1653 dataNeeded
-= toCopy
;
1654 available
-= toCopy
;
1659 wodHelper_PlayPtrNext(wwo
);
1663 ioData
->mBuffers
[0].mDataByteSize
= dataProvided
;
1665 OSSpinLockUnlock(&wwo
->lock
);
1667 /* We can't provide any more wave data. Fill the rest with silence. */
1671 *ioActionFlags
|= kAudioUnitRenderAction_OutputIsSilence
;
1672 memset((char*)ioData
->mBuffers
[0].mData
+ dataProvided
, 0, dataNeeded
);
1673 dataProvided
+= dataNeeded
;
1677 /* We only fill buffer 0. Set any others that might be requested to 0. */
1678 for (buffer
= 1; buffer
< ioData
->mNumberBuffers
; buffer
++)
1680 memset(ioData
->mBuffers
[buffer
].mData
, 0, ioData
->mBuffers
[buffer
].mDataByteSize
);
1683 if (needNotify
) wodSendNotifyCompletionsMessage(wwo
);
1688 /*======================================================================*
1689 * Low level WAVE IN implementation *
1690 *======================================================================*/
1692 /**************************************************************************
1693 * widNotifyClient [internal]
1695 static DWORD
widNotifyClient(WINE_WAVEIN
* wwi
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
1697 TRACE("wMsg = 0x%04x dwParm1 = %04X dwParam2 = %04X\n", wMsg
, dwParam1
, dwParam2
);
1704 if (wwi
->wFlags
!= DCB_NULL
&&
1705 !DriverCallback(wwi
->waveDesc
.dwCallback
, wwi
->wFlags
,
1706 (HDRVR
)wwi
->waveDesc
.hWave
, wMsg
, wwi
->waveDesc
.dwInstance
,
1707 dwParam1
, dwParam2
))
1709 WARN("can't notify client !\n");
1710 return MMSYSERR_ERROR
;
1714 FIXME("Unknown callback message %u\n", wMsg
);
1715 return MMSYSERR_INVALPARAM
;
1717 return MMSYSERR_NOERROR
;
1721 /**************************************************************************
1722 * widHelper_NotifyCompletions [internal]
1724 static void widHelper_NotifyCompletions(WINE_WAVEIN
* wwi
)
1726 LPWAVEHDR lpWaveHdr
;
1727 LPWAVEHDR lpFirstDoneWaveHdr
= NULL
;
1728 LPWAVEHDR lpLastDoneWaveHdr
= NULL
;
1730 OSSpinLockLock(&wwi
->lock
);
1732 /* First, excise all of the done headers from the queue into
1733 * a free-standing list. */
1735 /* Start from lpQueuePtr and keep notifying until:
1736 * - we hit an unfilled wavehdr
1737 * - we hit the end of the list
1740 lpWaveHdr
= wwi
->lpQueuePtr
;
1742 lpWaveHdr
->dwBytesRecorded
>= lpWaveHdr
->dwBufferLength
;
1743 lpWaveHdr
= lpWaveHdr
->lpNext
1746 if (!lpFirstDoneWaveHdr
)
1747 lpFirstDoneWaveHdr
= lpWaveHdr
;
1748 lpLastDoneWaveHdr
= lpWaveHdr
;
1751 if (lpLastDoneWaveHdr
)
1753 wwi
->lpQueuePtr
= lpLastDoneWaveHdr
->lpNext
;
1754 lpLastDoneWaveHdr
->lpNext
= NULL
;
1757 OSSpinLockUnlock(&wwi
->lock
);
1759 /* Now, send the "done" notification for each header in our list. */
1760 lpWaveHdr
= lpFirstDoneWaveHdr
;
1763 LPWAVEHDR lpNext
= lpWaveHdr
->lpNext
;
1765 lpWaveHdr
->lpNext
= NULL
;
1766 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1767 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1768 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
1775 /**************************************************************************
1776 * widGetDevCaps [internal]
1778 static DWORD
widGetDevCaps(WORD wDevID
, LPWAVEINCAPSW lpCaps
, DWORD dwSize
)
1780 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
1782 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
1784 if (wDevID
>= MAX_WAVEINDRV
)
1786 TRACE("MAX_WAVEINDRV reached !\n");
1787 return MMSYSERR_BADDEVICEID
;
1790 memcpy(lpCaps
, &WInDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1791 return MMSYSERR_NOERROR
;
1795 /**************************************************************************
1796 * widHelper_DestroyAudioBufferList [internal]
1797 * Convenience function to dispose of our audio buffers
1799 static void widHelper_DestroyAudioBufferList(AudioBufferList
* list
)
1804 for (i
= 0; i
< list
->mNumberBuffers
; i
++)
1806 if (list
->mBuffers
[i
].mData
)
1807 HeapFree(GetProcessHeap(), 0, list
->mBuffers
[i
].mData
);
1809 HeapFree(GetProcessHeap(), 0, list
);
1814 #define AUDIOBUFFERLISTSIZE(numBuffers) (offsetof(AudioBufferList, mBuffers) + (numBuffers) * sizeof(AudioBuffer))
1816 /**************************************************************************
1817 * widHelper_AllocateAudioBufferList [internal]
1818 * Convenience function to allocate our audio buffers
1820 static AudioBufferList
* widHelper_AllocateAudioBufferList(UInt32 numChannels
, UInt32 bitsPerChannel
, UInt32 bufferFrames
, BOOL interleaved
)
1823 UInt32 channelsPerFrame
;
1824 UInt32 bytesPerFrame
;
1825 UInt32 bytesPerBuffer
;
1826 AudioBufferList
* list
;
1831 /* For interleaved audio, we allocate one buffer for all channels. */
1833 channelsPerFrame
= numChannels
;
1837 numBuffers
= numChannels
;
1838 channelsPerFrame
= 1;
1841 bytesPerFrame
= bitsPerChannel
* channelsPerFrame
/ 8;
1842 bytesPerBuffer
= bytesPerFrame
* bufferFrames
;
1844 list
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, AUDIOBUFFERLISTSIZE(numBuffers
));
1848 list
->mNumberBuffers
= numBuffers
;
1849 for (i
= 0; i
< numBuffers
; ++i
)
1851 list
->mBuffers
[i
].mNumberChannels
= channelsPerFrame
;
1852 list
->mBuffers
[i
].mDataByteSize
= bytesPerBuffer
;
1853 list
->mBuffers
[i
].mData
= HeapAlloc(GetProcessHeap(), 0, bytesPerBuffer
);
1854 if (list
->mBuffers
[i
].mData
== NULL
)
1856 widHelper_DestroyAudioBufferList(list
);
1864 /**************************************************************************
1865 * widOpen [internal]
1867 static DWORD
widOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1872 TRACE("(%u, %p, %08X);\n", wDevID
, lpDesc
, dwFlags
);
1875 WARN("Invalid Parameter !\n");
1876 return MMSYSERR_INVALPARAM
;
1878 if (wDevID
>= MAX_WAVEINDRV
)
1880 TRACE ("MAX_WAVEINDRV reached !\n");
1881 return MMSYSERR_BADDEVICEID
;
1884 TRACE("Format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
1885 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1886 lpDesc
->lpFormat
->nSamplesPerSec
, lpDesc
->lpFormat
->wBitsPerSample
);
1888 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
1889 lpDesc
->lpFormat
->nChannels
== 0 ||
1890 lpDesc
->lpFormat
->nSamplesPerSec
== 0 ||
1891 lpDesc
->lpFormat
->nSamplesPerSec
!= AudioUnit_GetInputDeviceSampleRate()
1894 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
1895 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1896 lpDesc
->lpFormat
->nSamplesPerSec
, lpDesc
->lpFormat
->wBitsPerSample
);
1897 return WAVERR_BADFORMAT
;
1900 if (dwFlags
& WAVE_FORMAT_QUERY
)
1902 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1903 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1904 lpDesc
->lpFormat
->nSamplesPerSec
);
1905 return MMSYSERR_NOERROR
;
1908 wwi
= &WInDev
[wDevID
];
1909 if (!OSSpinLockTry(&wwi
->lock
))
1910 return MMSYSERR_ALLOCATED
;
1912 if (wwi
->state
!= WINE_WS_CLOSED
)
1914 OSSpinLockUnlock(&wwi
->lock
);
1915 return MMSYSERR_ALLOCATED
;
1918 wwi
->state
= WINE_WS_STOPPED
;
1919 wwi
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1921 wwi
->waveDesc
= *lpDesc
;
1922 memcpy(&wwi
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
1924 if (wwi
->format
.wBitsPerSample
== 0)
1926 WARN("Resetting zeroed wBitsPerSample\n");
1927 wwi
->format
.wBitsPerSample
= 8 *
1928 (wwi
->format
.wf
.nAvgBytesPerSec
/
1929 wwi
->format
.wf
.nSamplesPerSec
) /
1930 wwi
->format
.wf
.nChannels
;
1933 wwi
->dwTotalRecorded
= 0;
1935 wwi
->trace_on
= TRACE_ON(wave
);
1936 wwi
->warn_on
= WARN_ON(wave
);
1937 wwi
->err_on
= ERR_ON(wave
);
1939 if (!AudioUnit_CreateInputUnit(wwi
, &wwi
->audioUnit
,
1940 wwi
->format
.wf
.nChannels
, wwi
->format
.wf
.nSamplesPerSec
,
1941 wwi
->format
.wBitsPerSample
, &frameCount
))
1943 ERR("AudioUnit_CreateInputUnit failed\n");
1944 OSSpinLockUnlock(&wwi
->lock
);
1945 return MMSYSERR_ERROR
;
1948 /* Allocate our audio buffers */
1949 wwi
->bufferList
= widHelper_AllocateAudioBufferList(wwi
->format
.wf
.nChannels
,
1950 wwi
->format
.wBitsPerSample
, frameCount
, TRUE
);
1951 if (wwi
->bufferList
== NULL
)
1953 ERR("Failed to allocate buffer list\n");
1954 AudioUnitUninitialize(wwi
->audioUnit
);
1955 AudioUnit_CloseAudioUnit(wwi
->audioUnit
);
1956 OSSpinLockUnlock(&wwi
->lock
);
1957 return MMSYSERR_NOMEM
;
1960 /* Keep a copy of the buffer list structure (but not the buffers themselves)
1961 * in case AudioUnitRender clobbers the original, as it won't to do. */
1962 wwi
->bufferListCopy
= HeapAlloc(GetProcessHeap(), 0, AUDIOBUFFERLISTSIZE(wwi
->bufferList
->mNumberBuffers
));
1963 if (wwi
->bufferListCopy
== NULL
)
1965 ERR("Failed to allocate buffer list copy\n");
1966 widHelper_DestroyAudioBufferList(wwi
->bufferList
);
1967 AudioUnitUninitialize(wwi
->audioUnit
);
1968 AudioUnit_CloseAudioUnit(wwi
->audioUnit
);
1969 OSSpinLockUnlock(&wwi
->lock
);
1970 return MMSYSERR_NOMEM
;
1972 memcpy(wwi
->bufferListCopy
, wwi
->bufferList
, AUDIOBUFFERLISTSIZE(wwi
->bufferList
->mNumberBuffers
));
1974 OSSpinLockUnlock(&wwi
->lock
);
1976 return widNotifyClient(wwi
, WIM_OPEN
, 0L, 0L);
1980 /**************************************************************************
1981 * widClose [internal]
1983 static DWORD
widClose(WORD wDevID
)
1985 DWORD ret
= MMSYSERR_NOERROR
;
1988 TRACE("(%u);\n", wDevID
);
1990 if (wDevID
>= MAX_WAVEINDRV
)
1992 WARN("bad device ID !\n");
1993 return MMSYSERR_BADDEVICEID
;
1996 wwi
= &WInDev
[wDevID
];
1997 OSSpinLockLock(&wwi
->lock
);
1998 if (wwi
->state
== WINE_WS_CLOSED
)
2000 WARN("Device already closed.\n");
2001 ret
= MMSYSERR_INVALHANDLE
;
2003 else if (wwi
->lpQueuePtr
)
2005 WARN("Buffers in queue.\n");
2006 ret
= WAVERR_STILLPLAYING
;
2010 wwi
->state
= WINE_WS_CLOSED
;
2013 OSSpinLockUnlock(&wwi
->lock
);
2015 if (ret
== MMSYSERR_NOERROR
)
2017 OSStatus err
= AudioUnitUninitialize(wwi
->audioUnit
);
2020 ERR("AudioUnitUninitialize return %c%c%c%c\n", (char) (err
>> 24),
2026 if (!AudioUnit_CloseAudioUnit(wwi
->audioUnit
))
2028 ERR("Can't close AudioUnit\n");
2031 /* Dellocate our audio buffers */
2032 widHelper_DestroyAudioBufferList(wwi
->bufferList
);
2033 wwi
->bufferList
= NULL
;
2034 HeapFree(GetProcessHeap(), 0, wwi
->bufferListCopy
);
2035 wwi
->bufferListCopy
= NULL
;
2037 ret
= widNotifyClient(wwi
, WIM_CLOSE
, 0L, 0L);
2044 /**************************************************************************
2045 * widAddBuffer [internal]
2047 static DWORD
widAddBuffer(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
2049 DWORD ret
= MMSYSERR_NOERROR
;
2052 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
2054 if (wDevID
>= MAX_WAVEINDRV
)
2056 WARN("invalid device ID\n");
2057 return MMSYSERR_INVALHANDLE
;
2059 if (!(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
2061 TRACE("never been prepared !\n");
2062 return WAVERR_UNPREPARED
;
2064 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
2066 TRACE("header already in use !\n");
2067 return WAVERR_STILLPLAYING
;
2070 wwi
= &WInDev
[wDevID
];
2071 OSSpinLockLock(&wwi
->lock
);
2073 if (wwi
->state
== WINE_WS_CLOSED
)
2075 WARN("Trying to add buffer to closed device.\n");
2076 ret
= MMSYSERR_INVALHANDLE
;
2082 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
2083 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
2084 lpWaveHdr
->dwBytesRecorded
= 0;
2085 lpWaveHdr
->lpNext
= NULL
;
2087 /* insert buffer at end of queue */
2088 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
))
2093 OSSpinLockUnlock(&wwi
->lock
);
2099 /**************************************************************************
2100 * widStart [internal]
2102 static DWORD
widStart(WORD wDevID
)
2104 DWORD ret
= MMSYSERR_NOERROR
;
2107 TRACE("(%u);\n", wDevID
);
2108 if (wDevID
>= MAX_WAVEINDRV
)
2110 WARN("invalid device ID\n");
2111 return MMSYSERR_INVALHANDLE
;
2114 /* The order of the following operations is important since we can't hold
2115 * the mutex while we make an Audio Unit call. Set the PLAYING state
2116 * before starting the Audio Unit. In widStop, the order is reversed.
2117 * This guarantees that we can't get into a situation where the state is
2118 * PLAYING but the Audio Unit isn't running. Although we can be in STOPPED
2119 * state with the Audio Unit still running, that's harmless because the
2120 * input callback will just throw away the sound data.
2122 wwi
= &WInDev
[wDevID
];
2123 OSSpinLockLock(&wwi
->lock
);
2125 if (wwi
->state
== WINE_WS_CLOSED
)
2127 WARN("Trying to start closed device.\n");
2128 ret
= MMSYSERR_INVALHANDLE
;
2131 wwi
->state
= WINE_WS_PLAYING
;
2133 OSSpinLockUnlock(&wwi
->lock
);
2135 if (ret
== MMSYSERR_NOERROR
)
2137 /* Start pulling for audio data */
2138 OSStatus err
= AudioOutputUnitStart(wwi
->audioUnit
);
2140 ERR("Failed to start AU: %08lx\n", err
);
2142 TRACE("Recording started...\n");
2149 /**************************************************************************
2150 * widStop [internal]
2152 static DWORD
widStop(WORD wDevID
)
2154 DWORD ret
= MMSYSERR_NOERROR
;
2156 WAVEHDR
* lpWaveHdr
= NULL
;
2159 TRACE("(%u);\n", wDevID
);
2160 if (wDevID
>= MAX_WAVEINDRV
)
2162 WARN("invalid device ID\n");
2163 return MMSYSERR_INVALHANDLE
;
2166 wwi
= &WInDev
[wDevID
];
2168 /* The order of the following operations is important since we can't hold
2169 * the mutex while we make an Audio Unit call. Stop the Audio Unit before
2170 * setting the STOPPED state. In widStart, the order is reversed. This
2171 * guarantees that we can't get into a situation where the state is
2172 * PLAYING but the Audio Unit isn't running. Although we can be in STOPPED
2173 * state with the Audio Unit still running, that's harmless because the
2174 * input callback will just throw away the sound data.
2176 err
= AudioOutputUnitStop(wwi
->audioUnit
);
2178 WARN("Failed to stop AU: %08lx\n", err
);
2180 TRACE("Recording stopped.\n");
2182 OSSpinLockLock(&wwi
->lock
);
2184 if (wwi
->state
== WINE_WS_CLOSED
)
2186 WARN("Trying to stop closed device.\n");
2187 ret
= MMSYSERR_INVALHANDLE
;
2189 else if (wwi
->state
!= WINE_WS_STOPPED
)
2191 wwi
->state
= WINE_WS_STOPPED
;
2192 /* If there's a buffer in progress, it's done. Remove it from the
2193 * queue so that we can return it to the app, below. */
2194 if (wwi
->lpQueuePtr
)
2196 lpWaveHdr
= wwi
->lpQueuePtr
;
2197 wwi
->lpQueuePtr
= lpWaveHdr
->lpNext
;
2201 OSSpinLockUnlock(&wwi
->lock
);
2205 lpWaveHdr
->lpNext
= NULL
;
2206 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2207 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2208 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2214 /**************************************************************************
2215 * widGetPos [internal]
2217 static DWORD
widGetPos(WORD wDevID
, LPMMTIME lpTime
, UINT size
)
2222 TRACE("(%u);\n", wDevID
);
2223 if (wDevID
>= MAX_WAVEINDRV
)
2225 WARN("invalid device ID\n");
2226 return MMSYSERR_INVALHANDLE
;
2229 wwi
= &WInDev
[wDevID
];
2231 OSSpinLockLock(&WInDev
[wDevID
].lock
);
2232 val
= wwi
->dwTotalRecorded
;
2233 OSSpinLockUnlock(&WInDev
[wDevID
].lock
);
2235 return bytes_to_mmtime(lpTime
, val
, &wwi
->format
);
2238 /**************************************************************************
2239 * widReset [internal]
2241 static DWORD
widReset(WORD wDevID
)
2243 DWORD ret
= MMSYSERR_NOERROR
;
2245 WAVEHDR
* lpWaveHdr
= NULL
;
2247 TRACE("(%u);\n", wDevID
);
2248 if (wDevID
>= MAX_WAVEINDRV
)
2250 WARN("invalid device ID\n");
2251 return MMSYSERR_INVALHANDLE
;
2254 wwi
= &WInDev
[wDevID
];
2255 OSSpinLockLock(&wwi
->lock
);
2257 if (wwi
->state
== WINE_WS_CLOSED
)
2259 WARN("Trying to reset a closed device.\n");
2260 ret
= MMSYSERR_INVALHANDLE
;
2264 lpWaveHdr
= wwi
->lpQueuePtr
;
2265 wwi
->lpQueuePtr
= NULL
;
2266 wwi
->state
= WINE_WS_STOPPED
;
2267 wwi
->dwTotalRecorded
= 0;
2270 OSSpinLockUnlock(&wwi
->lock
);
2272 if (ret
== MMSYSERR_NOERROR
)
2274 OSStatus err
= AudioOutputUnitStop(wwi
->audioUnit
);
2276 WARN("Failed to stop AU: %08lx\n", err
);
2278 TRACE("Recording stopped.\n");
2283 WAVEHDR
* lpNext
= lpWaveHdr
->lpNext
;
2285 lpWaveHdr
->lpNext
= NULL
;
2286 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2287 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2288 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2297 /**************************************************************************
2298 * widGetNumDevs [internal]
2300 static DWORD
widGetNumDevs(void)
2302 return MAX_WAVEINDRV
;
2306 /**************************************************************************
2307 * widDevInterfaceSize [internal]
2309 static DWORD
widDevInterfaceSize(UINT wDevID
, LPDWORD dwParam1
)
2311 TRACE("(%u, %p)\n", wDevID
, dwParam1
);
2313 *dwParam1
= MultiByteToWideChar(CP_UNIXCP
, 0, WInDev
[wDevID
].interface_name
, -1,
2314 NULL
, 0 ) * sizeof(WCHAR
);
2315 return MMSYSERR_NOERROR
;
2319 /**************************************************************************
2320 * widDevInterface [internal]
2322 static DWORD
widDevInterface(UINT wDevID
, PWCHAR dwParam1
, DWORD dwParam2
)
2324 if (dwParam2
>= MultiByteToWideChar(CP_UNIXCP
, 0, WInDev
[wDevID
].interface_name
, -1,
2325 NULL
, 0 ) * sizeof(WCHAR
))
2327 MultiByteToWideChar(CP_UNIXCP
, 0, WInDev
[wDevID
].interface_name
, -1,
2328 dwParam1
, dwParam2
/ sizeof(WCHAR
));
2329 return MMSYSERR_NOERROR
;
2331 return MMSYSERR_INVALPARAM
;
2335 /**************************************************************************
2336 * widDsCreate [internal]
2338 static DWORD
widDsCreate(UINT wDevID
, PIDSCDRIVER
* drv
)
2340 TRACE("(%d,%p)\n",wDevID
,drv
);
2342 FIXME("DirectSoundCapture not implemented\n");
2343 FIXME("The (slower) DirectSound HEL mode will be used instead.\n");
2344 return MMSYSERR_NOTSUPPORTED
;
2347 /**************************************************************************
2348 * widDsDesc [internal]
2350 static DWORD
widDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
2352 /* The DirectSound HEL will automatically wrap a non-DirectSound-capable
2353 * driver in a DirectSound adaptor, thus allowing the driver to be used by
2354 * DirectSound clients. However, it only does this if we respond
2355 * successfully to the DRV_QUERYDSOUNDDESC message. It's enough to fill in
2356 * the driver and device names of the description output parameter. */
2357 memset(desc
, 0, sizeof(*desc
));
2358 lstrcpynA(desc
->szDrvname
, "winecoreaudio.drv", sizeof(desc
->szDrvname
) - 1);
2359 lstrcpynA(desc
->szDesc
, WInDev
[wDevID
].interface_name
, sizeof(desc
->szDesc
) - 1);
2360 return MMSYSERR_NOERROR
;
2364 /**************************************************************************
2365 * widMessage (WINECOREAUDIO.6)
2367 DWORD WINAPI
CoreAudio_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2368 DWORD dwParam1
, DWORD dwParam2
)
2370 TRACE("(%u, %04X, %08X, %08X, %08X);\n",
2371 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2379 /* FIXME: Pretend this is supported */
2381 case WIDM_OPEN
: return widOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
2382 case WIDM_CLOSE
: return widClose (wDevID
);
2383 case WIDM_ADDBUFFER
: return widAddBuffer (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
2384 case WIDM_PREPARE
: return MMSYSERR_NOTSUPPORTED
;
2385 case WIDM_UNPREPARE
: return MMSYSERR_NOTSUPPORTED
;
2386 case WIDM_GETDEVCAPS
: return widGetDevCaps (wDevID
, (LPWAVEINCAPSW
)dwParam1
, dwParam2
);
2387 case WIDM_GETNUMDEVS
: return widGetNumDevs ();
2388 case WIDM_RESET
: return widReset (wDevID
);
2389 case WIDM_START
: return widStart (wDevID
);
2390 case WIDM_STOP
: return widStop (wDevID
);
2391 case WIDM_GETPOS
: return widGetPos (wDevID
, (LPMMTIME
)dwParam1
, (UINT
)dwParam2
);
2392 case DRV_QUERYDEVICEINTERFACESIZE
: return widDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
2393 case DRV_QUERYDEVICEINTERFACE
: return widDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
2394 case DRV_QUERYDSOUNDIFACE
: return widDsCreate (wDevID
, (PIDSCDRIVER
*)dwParam1
);
2395 case DRV_QUERYDSOUNDDESC
: return widDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
2397 FIXME("unknown message %d!\n", wMsg
);
2400 return MMSYSERR_NOTSUPPORTED
;
2404 OSStatus
CoreAudio_wiAudioUnitIOProc(void *inRefCon
,
2405 AudioUnitRenderActionFlags
*ioActionFlags
,
2406 const AudioTimeStamp
*inTimeStamp
,
2408 UInt32 inNumberFrames
,
2409 AudioBufferList
*ioData
)
2411 WINE_WAVEIN
* wwi
= (WINE_WAVEIN
*)inRefCon
;
2412 OSStatus err
= noErr
;
2413 BOOL needNotify
= FALSE
;
2414 WAVEHDR
* lpStorePtr
;
2415 unsigned int dataToStore
;
2416 unsigned int dataStored
= 0;
2420 fprintf(stderr
, "trace:wave:CoreAudio_wiAudioUnitIOProc (ioActionFlags = %08lx, "
2421 "inTimeStamp = { %f, %x%08x, %f, %x%08x, %08lx }, inBusNumber = %lu, inNumberFrames = %lu)\n",
2422 *ioActionFlags
, inTimeStamp
->mSampleTime
, (DWORD
)(inTimeStamp
->mHostTime
>>32),
2423 (DWORD
)inTimeStamp
->mHostTime
, inTimeStamp
->mRateScalar
, (DWORD
)(inTimeStamp
->mWordClockTime
>> 32),
2424 (DWORD
)inTimeStamp
->mWordClockTime
, inTimeStamp
->mFlags
, inBusNumber
, inNumberFrames
);
2426 /* Render into audio buffer */
2427 /* FIXME: implement sample rate conversion on input. This will require
2428 * a different render strategy. We'll need to buffer the sound data
2429 * received here and pass it off to an AUConverter in another thread. */
2430 err
= AudioUnitRender(wwi
->audioUnit
, ioActionFlags
, inTimeStamp
, inBusNumber
, inNumberFrames
, wwi
->bufferList
);
2434 fprintf(stderr
, "err:wave:CoreAudio_wiAudioUnitIOProc AudioUnitRender failed with error %li\n", err
);
2438 /* Copy from audio buffer to the wavehdrs */
2439 dataToStore
= wwi
->bufferList
->mBuffers
[0].mDataByteSize
;
2441 OSSpinLockLock(&wwi
->lock
);
2443 lpStorePtr
= wwi
->lpQueuePtr
;
2445 while (dataToStore
> 0 && wwi
->state
== WINE_WS_PLAYING
&& lpStorePtr
)
2447 unsigned int room
= lpStorePtr
->dwBufferLength
- lpStorePtr
->dwBytesRecorded
;
2448 unsigned int toCopy
;
2451 fprintf(stderr
, "trace:wave:CoreAudio_wiAudioUnitIOProc Looking to store %u bytes to wavehdr %p, which has room for %u\n",
2452 dataToStore
, lpStorePtr
, room
);
2454 if (room
>= dataToStore
)
2455 toCopy
= dataToStore
;
2461 memcpy(lpStorePtr
->lpData
+ lpStorePtr
->dwBytesRecorded
,
2462 (char*)wwi
->bufferList
->mBuffers
[0].mData
+ dataStored
, toCopy
);
2463 lpStorePtr
->dwBytesRecorded
+= toCopy
;
2464 wwi
->dwTotalRecorded
+= toCopy
;
2465 dataStored
+= toCopy
;
2466 dataToStore
-= toCopy
;
2472 lpStorePtr
= lpStorePtr
->lpNext
;
2477 OSSpinLockUnlock(&wwi
->lock
);
2479 /* Restore the audio buffer list structure from backup, in case
2480 * AudioUnitRender clobbered it. (It modifies mDataByteSize and may even
2481 * give us a different mData buffer to avoid a copy.) */
2482 memcpy(wwi
->bufferList
, wwi
->bufferListCopy
, AUDIOBUFFERLISTSIZE(wwi
->bufferList
->mNumberBuffers
));
2484 if (needNotify
) wodSendNotifyInputCompletionsMessage(wwi
);
2490 /**************************************************************************
2491 * widMessage (WINECOREAUDIO.6)
2493 DWORD WINAPI
CoreAudio_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2494 DWORD dwParam1
, DWORD dwParam2
)
2496 FIXME("(%u, %04X, %08X, %08X, %08X): CoreAudio support not compiled into wine\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2497 return MMSYSERR_NOTENABLED
;
2500 /**************************************************************************
2501 * wodMessage (WINECOREAUDIO.7)
2503 DWORD WINAPI
CoreAudio_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2504 DWORD dwParam1
, DWORD dwParam2
)
2506 FIXME("(%u, %04X, %08X, %08X, %08X): CoreAudio support not compiled into wine\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2507 return MMSYSERR_NOTENABLED
;