winecoreaudio: Ignore failure to notify winmm wave clients.
[wine.git] / dlls / winecoreaudio.drv / audio.c
bloba2158efe8b6cb58ea20953c0a40522967ffadc0f
1 /*
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
25 #include "config.h"
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #include <fcntl.h>
35 #include <assert.h>
37 #ifdef HAVE_COREAUDIO_COREAUDIO_H
38 #include <CoreAudio/CoreAudio.h>
39 #include <CoreFoundation/CoreFoundation.h>
40 #include <libkern/OSAtomic.h>
41 #endif
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnls.h"
46 #include "wingdi.h"
47 #include "winerror.h"
48 #include "mmddk.h"
49 #include "mmreg.h"
50 #include "dsound.h"
51 #include "dsdriver.h"
52 #include "ks.h"
53 #include "ksguid.h"
54 #include "ksmedia.h"
55 #include "coreaudio.h"
56 #include "wine/unicode.h"
57 #include "wine/library.h"
58 #include "wine/debug.h"
59 #include "wine/list.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(wave);
63 #if defined(HAVE_COREAUDIO_COREAUDIO_H) && defined(HAVE_AUDIOUNIT_AUDIOUNIT_H)
65 WINE_DECLARE_DEBUG_CHANNEL(coreaudio);
68 Due to AudioUnit headers conflict define some needed types.
71 typedef void *AudioUnit;
73 /* From AudioUnit/AUComponents.h */
74 enum
76 kAudioUnitRenderAction_OutputIsSilence = (1 << 4),
77 /* provides hint on return from Render(): if set the buffer contains all zeroes */
79 typedef UInt32 AudioUnitRenderActionFlags;
81 typedef long ComponentResult;
82 extern ComponentResult
83 AudioUnitRender( AudioUnit ci,
84 AudioUnitRenderActionFlags * ioActionFlags,
85 const AudioTimeStamp * inTimeStamp,
86 UInt32 inOutputBusNumber,
87 UInt32 inNumberFrames,
88 AudioBufferList * ioData) AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER;
90 /* only allow 10 output devices through this driver, this ought to be adequate */
91 #define MAX_WAVEOUTDRV (1)
92 #define MAX_WAVEINDRV (1)
94 /* state diagram for waveOut writing:
96 * +---------+-------------+---------------+---------------------------------+
97 * | state | function | event | new state |
98 * +---------+-------------+---------------+---------------------------------+
99 * | | open() | | PLAYING |
100 * | PAUSED | write() | | PAUSED |
101 * | PLAYING | write() | HEADER | PLAYING |
102 * | (other) | write() | <error> | |
103 * | (any) | pause() | PAUSING | PAUSED |
104 * | PAUSED | restart() | RESTARTING | PLAYING |
105 * | (any) | reset() | RESETTING | PLAYING |
106 * | (any) | close() | CLOSING | <deallocated> |
107 * +---------+-------------+---------------+---------------------------------+
110 /* states of the playing device */
111 #define WINE_WS_PLAYING 0 /* for waveOut: lpPlayPtr == NULL -> stopped */
112 #define WINE_WS_PAUSED 1
113 #define WINE_WS_STOPPED 2 /* Not used for waveOut */
114 #define WINE_WS_CLOSED 3 /* Not used for waveOut */
115 #define WINE_WS_OPENING 4
116 #define WINE_WS_CLOSING 5
118 typedef struct tagCoreAudio_Device {
119 char dev_name[32];
120 char mixer_name[32];
121 unsigned open_count;
122 char* interface_name;
124 WAVEOUTCAPSW out_caps;
125 WAVEINCAPSW in_caps;
126 DWORD in_caps_support;
127 int sample_rate;
128 int stereo;
129 int format;
130 unsigned audio_fragment;
131 BOOL full_duplex;
132 BOOL bTriggerSupport;
133 BOOL bOutputEnabled;
134 BOOL bInputEnabled;
135 DSDRIVERDESC ds_desc;
136 DSDRIVERCAPS ds_caps;
137 DSCDRIVERCAPS dsc_caps;
138 GUID ds_guid;
139 GUID dsc_guid;
141 AudioDeviceID outputDeviceID;
142 AudioDeviceID inputDeviceID;
143 AudioStreamBasicDescription streamDescription;
144 } CoreAudio_Device;
146 /* for now use the default device */
147 static CoreAudio_Device CoreAudio_DefaultDevice;
149 typedef struct {
150 struct list entry;
152 volatile int state; /* one of the WINE_WS_ manifest constants */
153 WAVEOPENDESC waveDesc;
154 WORD wFlags;
155 PCMWAVEFORMAT format;
156 DWORD woID;
157 AudioUnit audioUnit;
158 AudioStreamBasicDescription streamDescription;
160 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
161 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
162 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
164 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
165 DWORD dwLoops; /* private copy of loop counter */
167 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
169 OSSpinLock lock; /* synchronization stuff */
170 } WINE_WAVEOUT_INSTANCE;
172 typedef struct {
173 CoreAudio_Device *cadev;
174 WAVEOUTCAPSW caps;
175 char interface_name[32];
176 DWORD device_volume;
178 BOOL trace_on;
179 BOOL warn_on;
180 BOOL err_on;
182 struct list instances;
183 OSSpinLock lock; /* guards the instances list */
184 } WINE_WAVEOUT;
186 typedef struct {
187 /* This device's device number */
188 DWORD wiID;
190 /* Access to the following fields is synchronized across threads. */
191 volatile int state;
192 LPWAVEHDR lpQueuePtr;
193 DWORD dwTotalRecorded;
195 /* Synchronization mechanism to protect above fields */
196 OSSpinLock lock;
198 /* Capabilities description */
199 WAVEINCAPSW caps;
200 char interface_name[32];
202 /* Record the arguments used when opening the device. */
203 WAVEOPENDESC waveDesc;
204 WORD wFlags;
205 PCMWAVEFORMAT format;
207 AudioUnit audioUnit;
208 AudioBufferList*bufferList;
209 AudioBufferList*bufferListCopy;
211 /* Record state of debug channels at open. Used to control fprintf's since
212 * we can't use Wine debug channel calls in non-Wine AudioUnit threads. */
213 BOOL trace_on;
214 BOOL warn_on;
215 BOOL err_on;
217 /* These fields aren't used. */
218 #if 0
219 CoreAudio_Device *cadev;
221 AudioStreamBasicDescription streamDescription;
222 #endif
223 } WINE_WAVEIN;
225 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
226 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
228 static HANDLE hThread = NULL; /* Track the thread we create so we can clean it up later */
229 static CFMessagePortRef Port_SendToMessageThread;
231 static void wodHelper_PlayPtrNext(WINE_WAVEOUT_INSTANCE* wwo);
232 static void wodHelper_NotifyDoneForList(WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr);
233 static void wodHelper_NotifyCompletions(WINE_WAVEOUT_INSTANCE* wwo, BOOL force);
234 static void widHelper_NotifyCompletions(WINE_WAVEIN* wwi);
236 extern int AudioUnit_CreateDefaultAudioUnit(void *wwo, AudioUnit *au);
237 extern int AudioUnit_CloseAudioUnit(AudioUnit au);
238 extern int AudioUnit_InitializeWithStreamDescription(AudioUnit au, AudioStreamBasicDescription *streamFormat);
240 extern OSStatus AudioOutputUnitStart(AudioUnit au);
241 extern OSStatus AudioOutputUnitStop(AudioUnit au);
242 extern OSStatus AudioUnitUninitialize(AudioUnit au);
244 extern int AudioUnit_SetVolume(AudioUnit au, float left, float right);
245 extern int AudioUnit_GetVolume(AudioUnit au, float *left, float *right);
247 extern int AudioUnit_GetInputDeviceSampleRate(void);
249 extern int AudioUnit_CreateInputUnit(void* wwi, AudioUnit* out_au,
250 WORD nChannels, DWORD nSamplesPerSec, WORD wBitsPerSample,
251 UInt32* outFrameCount);
253 OSStatus CoreAudio_woAudioUnitIOProc(void *inRefCon,
254 AudioUnitRenderActionFlags *ioActionFlags,
255 const AudioTimeStamp *inTimeStamp,
256 UInt32 inBusNumber,
257 UInt32 inNumberFrames,
258 AudioBufferList *ioData);
259 OSStatus CoreAudio_wiAudioUnitIOProc(void *inRefCon,
260 AudioUnitRenderActionFlags *ioActionFlags,
261 const AudioTimeStamp *inTimeStamp,
262 UInt32 inBusNumber,
263 UInt32 inNumberFrames,
264 AudioBufferList *ioData);
266 /* These strings used only for tracing */
268 static const char * getMessage(UINT msg)
270 #define MSG_TO_STR(x) case x: return #x
271 switch(msg) {
272 MSG_TO_STR(DRVM_INIT);
273 MSG_TO_STR(DRVM_EXIT);
274 MSG_TO_STR(DRVM_ENABLE);
275 MSG_TO_STR(DRVM_DISABLE);
276 MSG_TO_STR(WIDM_OPEN);
277 MSG_TO_STR(WIDM_CLOSE);
278 MSG_TO_STR(WIDM_ADDBUFFER);
279 MSG_TO_STR(WIDM_PREPARE);
280 MSG_TO_STR(WIDM_UNPREPARE);
281 MSG_TO_STR(WIDM_GETDEVCAPS);
282 MSG_TO_STR(WIDM_GETNUMDEVS);
283 MSG_TO_STR(WIDM_GETPOS);
284 MSG_TO_STR(WIDM_RESET);
285 MSG_TO_STR(WIDM_START);
286 MSG_TO_STR(WIDM_STOP);
287 MSG_TO_STR(WODM_OPEN);
288 MSG_TO_STR(WODM_CLOSE);
289 MSG_TO_STR(WODM_WRITE);
290 MSG_TO_STR(WODM_PAUSE);
291 MSG_TO_STR(WODM_GETPOS);
292 MSG_TO_STR(WODM_BREAKLOOP);
293 MSG_TO_STR(WODM_PREPARE);
294 MSG_TO_STR(WODM_UNPREPARE);
295 MSG_TO_STR(WODM_GETDEVCAPS);
296 MSG_TO_STR(WODM_GETNUMDEVS);
297 MSG_TO_STR(WODM_GETPITCH);
298 MSG_TO_STR(WODM_SETPITCH);
299 MSG_TO_STR(WODM_GETPLAYBACKRATE);
300 MSG_TO_STR(WODM_SETPLAYBACKRATE);
301 MSG_TO_STR(WODM_GETVOLUME);
302 MSG_TO_STR(WODM_SETVOLUME);
303 MSG_TO_STR(WODM_RESTART);
304 MSG_TO_STR(WODM_RESET);
305 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
306 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
307 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
308 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
310 #undef MSG_TO_STR
311 return wine_dbg_sprintf("UNKNOWN(0x%04x)", msg);
314 #define kStopLoopMessage 0
315 #define kWaveOutNotifyCompletionsMessage 1
316 #define kWaveInNotifyCompletionsMessage 2
318 /* Mach Message Handling */
319 static CFDataRef wodMessageHandler(CFMessagePortRef port_ReceiveInMessageThread, SInt32 msgid, CFDataRef data, void *info)
321 UInt32 *buffer = NULL;
323 switch (msgid)
325 case kWaveOutNotifyCompletionsMessage:
326 wodHelper_NotifyCompletions(*(WINE_WAVEOUT_INSTANCE**)CFDataGetBytePtr(data), FALSE);
327 break;
328 case kWaveInNotifyCompletionsMessage:
329 buffer = (UInt32 *) CFDataGetBytePtr(data);
330 widHelper_NotifyCompletions(&WInDev[buffer[0]]);
331 break;
332 default:
333 CFRunLoopStop(CFRunLoopGetCurrent());
334 break;
337 return NULL;
340 static DWORD WINAPI messageThread(LPVOID p)
342 CFMessagePortRef port_ReceiveInMessageThread = (CFMessagePortRef) p;
343 CFRunLoopSourceRef source;
345 source = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, port_ReceiveInMessageThread, 0);
346 CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
348 CFRunLoopRun();
350 CFRunLoopSourceInvalidate(source);
351 CFRelease(source);
352 CFRelease(port_ReceiveInMessageThread);
354 return 0;
357 /**************************************************************************
358 * wodSendNotifyCompletionsMessage [internal]
359 * Call from AudioUnit IO thread can't use Wine debug channels.
361 static void wodSendNotifyCompletionsMessage(WINE_WAVEOUT_INSTANCE* wwo)
363 CFDataRef data;
365 if (!Port_SendToMessageThread)
366 return;
368 data = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&wwo, sizeof(wwo));
369 if (!data)
370 return;
372 CFMessagePortSendRequest(Port_SendToMessageThread, kWaveOutNotifyCompletionsMessage, data, 0.0, 0.0, NULL, NULL);
373 CFRelease(data);
376 /**************************************************************************
377 * wodSendNotifyInputCompletionsMessage [internal]
378 * Call from AudioUnit IO thread can't use Wine debug channels.
380 static void wodSendNotifyInputCompletionsMessage(WINE_WAVEIN* wwi)
382 CFDataRef data;
383 UInt32 buffer;
385 if (!Port_SendToMessageThread)
386 return;
388 buffer = (UInt32) wwi->wiID;
390 data = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&buffer, sizeof(buffer));
391 if (!data)
392 return;
394 CFMessagePortSendRequest(Port_SendToMessageThread, kWaveInNotifyCompletionsMessage, data, 0.0, 0.0, NULL, NULL);
395 CFRelease(data);
398 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
399 PCMWAVEFORMAT* format)
401 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
402 lpTime->wType, format->wBitsPerSample, format->wf.nSamplesPerSec,
403 format->wf.nChannels, format->wf.nAvgBytesPerSec);
404 TRACE("Position in bytes=%u\n", position);
406 switch (lpTime->wType) {
407 case TIME_SAMPLES:
408 lpTime->u.sample = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
409 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
410 break;
411 case TIME_MS:
412 lpTime->u.ms = 1000.0 * position / (format->wBitsPerSample / 8 * format->wf.nChannels * format->wf.nSamplesPerSec);
413 TRACE("TIME_MS=%u\n", lpTime->u.ms);
414 break;
415 case TIME_SMPTE:
416 lpTime->u.smpte.fps = 30;
417 position = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
418 position += (format->wf.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
419 lpTime->u.smpte.sec = position / format->wf.nSamplesPerSec;
420 position -= lpTime->u.smpte.sec * format->wf.nSamplesPerSec;
421 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
422 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
423 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
424 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
425 lpTime->u.smpte.fps = 30;
426 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->wf.nSamplesPerSec;
427 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
428 lpTime->u.smpte.hour, lpTime->u.smpte.min,
429 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
430 break;
431 default:
432 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
433 lpTime->wType = TIME_BYTES;
434 /* fall through */
435 case TIME_BYTES:
436 lpTime->u.cb = position;
437 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
438 break;
440 return MMSYSERR_NOERROR;
443 static BOOL supportedFormat(LPWAVEFORMATEX wf)
445 if (wf->nSamplesPerSec < DSBFREQUENCY_MIN || wf->nSamplesPerSec > DSBFREQUENCY_MAX)
446 return FALSE;
448 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
449 if (wf->nChannels >= 1 && wf->nChannels <= 2) {
450 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
451 return TRUE;
453 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
454 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
456 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
457 if (wf->nChannels >=1 && wf->nChannels <= 8) {
458 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
459 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
460 return TRUE;
461 } else
462 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
464 } else
465 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
466 } else
467 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
469 return FALSE;
472 void copyFormat(LPWAVEFORMATEX wf1, LPPCMWAVEFORMAT wf2)
474 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
475 /* Downgrade WAVE_FORMAT_EXTENSIBLE KSDATAFORMAT_SUBTYPE_PCM
476 * to smaller yet compatible WAVE_FORMAT_PCM structure */
477 if (wf2->wf.wFormatTag == WAVE_FORMAT_EXTENSIBLE)
478 wf2->wf.wFormatTag = WAVE_FORMAT_PCM;
481 /**************************************************************************
482 * CoreAudio_GetDevCaps [internal]
484 BOOL CoreAudio_GetDevCaps (void)
486 OSStatus status;
487 UInt32 propertySize;
488 AudioDeviceID devId = CoreAudio_DefaultDevice.outputDeviceID;
489 AudioObjectPropertyAddress propertyAddress;
491 CFStringRef name;
492 CFRange range;
494 propertySize = sizeof(name);
495 propertyAddress.mSelector = kAudioObjectPropertyName;
496 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
497 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
498 status = AudioObjectGetPropertyData(devId, &propertyAddress, 0, NULL, &propertySize, &name);
499 if (status) {
500 ERR("AudioObjectGetPropertyData for kAudioObjectPropertyName return %s\n", wine_dbgstr_fourcc(status));
501 return FALSE;
504 CFStringGetCString(name, CoreAudio_DefaultDevice.ds_desc.szDesc,
505 sizeof(CoreAudio_DefaultDevice.ds_desc.szDesc),
506 kCFStringEncodingUTF8);
507 strcpy(CoreAudio_DefaultDevice.ds_desc.szDrvname, "winecoreaudio.drv");
508 range = CFRangeMake(0, min(sizeof(CoreAudio_DefaultDevice.out_caps.szPname) / sizeof(WCHAR) - 1, CFStringGetLength(name)));
509 CFStringGetCharacters(name, range, CoreAudio_DefaultDevice.out_caps.szPname);
510 CoreAudio_DefaultDevice.out_caps.szPname[range.length] = 0;
511 CFStringGetCString(name, CoreAudio_DefaultDevice.dev_name, 32, kCFStringEncodingUTF8);
512 CFRelease(name);
514 propertySize = sizeof(CoreAudio_DefaultDevice.streamDescription);
515 /* FIXME: kAudioDevicePropertyStreamFormat is deprecated. We're
516 * "supposed" to get an AudioStream object from the AudioDevice,
517 * then query it for the format with kAudioStreamPropertyVirtualFormat.
518 * Apple says that this is for our own good, because this property
519 * "has been shown to lead to programming mistakes by clients when
520 * working with devices with multiple streams." Only one problem:
521 * which stream? For now, just query the device.
523 propertyAddress.mSelector = kAudioDevicePropertyStreamFormat;
524 status = AudioObjectGetPropertyData(devId, &propertyAddress, 0, NULL, &propertySize, &CoreAudio_DefaultDevice.streamDescription);
525 if (status != noErr) {
526 ERR("AudioObjectGetPropertyData for kAudioDevicePropertyStreamFormat return %s\n", wine_dbgstr_fourcc(status));
527 return FALSE;
530 TRACE("Device Stream Description mSampleRate : %f\n mFormatID : %s\n"
531 "mFormatFlags : %lX\n mBytesPerPacket : %lu\n mFramesPerPacket : %lu\n"
532 "mBytesPerFrame : %lu\n mChannelsPerFrame : %lu\n mBitsPerChannel : %lu\n",
533 CoreAudio_DefaultDevice.streamDescription.mSampleRate,
534 wine_dbgstr_fourcc(CoreAudio_DefaultDevice.streamDescription.mFormatID),
535 CoreAudio_DefaultDevice.streamDescription.mFormatFlags,
536 CoreAudio_DefaultDevice.streamDescription.mBytesPerPacket,
537 CoreAudio_DefaultDevice.streamDescription.mFramesPerPacket,
538 CoreAudio_DefaultDevice.streamDescription.mBytesPerFrame,
539 CoreAudio_DefaultDevice.streamDescription.mChannelsPerFrame,
540 CoreAudio_DefaultDevice.streamDescription.mBitsPerChannel);
542 CoreAudio_DefaultDevice.out_caps.wMid = 0xcafe;
543 CoreAudio_DefaultDevice.out_caps.wPid = 0x0001;
545 CoreAudio_DefaultDevice.out_caps.vDriverVersion = 0x0001;
546 CoreAudio_DefaultDevice.out_caps.dwFormats = 0x00000000;
547 CoreAudio_DefaultDevice.out_caps.wReserved1 = 0;
548 CoreAudio_DefaultDevice.out_caps.dwSupport = WAVECAPS_VOLUME;
549 CoreAudio_DefaultDevice.out_caps.dwSupport |= WAVECAPS_LRVOLUME;
551 CoreAudio_DefaultDevice.out_caps.wChannels = 2;
552 CoreAudio_DefaultDevice.out_caps.dwFormats|= WAVE_FORMAT_4S16;
554 TRACE_(coreaudio)("out dwFormats = %08x, dwSupport = %08x\n",
555 CoreAudio_DefaultDevice.out_caps.dwFormats, CoreAudio_DefaultDevice.out_caps.dwSupport);
557 return TRUE;
560 /******************************************************************
561 * CoreAudio_WaveInit
563 * Initialize CoreAudio_DefaultDevice
565 LONG CoreAudio_WaveInit(void)
567 OSStatus status;
568 UInt32 propertySize;
569 AudioObjectPropertyAddress propertyAddress;
570 int i;
571 CFStringRef messageThreadPortName;
572 CFMessagePortRef port_ReceiveInMessageThread;
573 int inputSampleRate;
575 TRACE("()\n");
577 /* number of sound cards */
578 propertyAddress.mSelector = kAudioHardwarePropertyDevices;
579 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
580 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
581 AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize);
582 propertySize /= sizeof(AudioDeviceID);
583 TRACE("sound cards : %lu\n", propertySize);
585 /* Get the output device */
586 propertySize = sizeof(CoreAudio_DefaultDevice.outputDeviceID);
587 propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
588 status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, &CoreAudio_DefaultDevice.outputDeviceID);
589 if (status) {
590 ERR("AudioObjectGetPropertyData return %s for kAudioHardwarePropertyDefaultOutputDevice\n", wine_dbgstr_fourcc(status));
591 return DRV_FAILURE;
593 if (CoreAudio_DefaultDevice.outputDeviceID == kAudioDeviceUnknown) {
594 ERR("AudioObjectGetPropertyData: CoreAudio_DefaultDevice.outputDeviceID == kAudioDeviceUnknown\n");
595 return DRV_FAILURE;
598 if ( ! CoreAudio_GetDevCaps() )
599 return DRV_FAILURE;
601 CoreAudio_DefaultDevice.interface_name=HeapAlloc(GetProcessHeap(),0,strlen(CoreAudio_DefaultDevice.dev_name)+1);
602 strcpy(CoreAudio_DefaultDevice.interface_name, CoreAudio_DefaultDevice.dev_name);
604 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
606 static const WCHAR wszWaveOutFormat[] =
607 {'C','o','r','e','A','u','d','i','o',' ','W','a','v','e','O','u','t',' ','%','d',0};
609 list_init(&WOutDev[i].instances);
610 WOutDev[i].cadev = &CoreAudio_DefaultDevice;
612 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps));
614 WOutDev[i].caps.wMid = 0xcafe; /* Manufac ID */
615 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
616 snprintfW(WOutDev[i].caps.szPname, sizeof(WOutDev[i].caps.szPname)/sizeof(WCHAR), wszWaveOutFormat, i);
617 snprintf(WOutDev[i].interface_name, sizeof(WOutDev[i].interface_name), "winecoreaudio: %d", i);
619 WOutDev[i].caps.vDriverVersion = 0x0001;
620 WOutDev[i].caps.dwFormats = 0x00000000;
621 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
623 WOutDev[i].caps.wChannels = 2;
624 /* WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME; */ /* FIXME */
626 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_96M08;
627 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_96S08;
628 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_96M16;
629 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_96S16;
630 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_48M08;
631 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_48S08;
632 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_48M16;
633 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_48S16;
634 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
635 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
636 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
637 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
638 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
639 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
640 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
641 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
642 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
643 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
644 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
645 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
647 WOutDev[i].device_volume = 0xffffffff;
649 WOutDev[i].lock = 0; /* initialize the mutex */
652 /* FIXME: implement sample rate conversion on input */
653 inputSampleRate = AudioUnit_GetInputDeviceSampleRate();
655 for (i = 0; i < MAX_WAVEINDRV; ++i)
657 static const WCHAR wszWaveInFormat[] =
658 {'C','o','r','e','A','u','d','i','o',' ','W','a','v','e','I','n',' ','%','d',0};
660 memset(&WInDev[i], 0, sizeof(WInDev[i]));
661 WInDev[i].wiID = i;
663 /* Establish preconditions for widOpen */
664 WInDev[i].state = WINE_WS_CLOSED;
665 WInDev[i].lock = 0; /* initialize the mutex */
667 /* Fill in capabilities. widGetDevCaps can be called at any time. */
668 WInDev[i].caps.wMid = 0xcafe; /* Manufac ID */
669 WInDev[i].caps.wPid = 0x0001; /* Product ID */
670 WInDev[i].caps.vDriverVersion = 0x0001;
672 snprintfW(WInDev[i].caps.szPname, sizeof(WInDev[i].caps.szPname)/sizeof(WCHAR), wszWaveInFormat, i);
673 snprintf(WInDev[i].interface_name, sizeof(WInDev[i].interface_name), "winecoreaudio in: %d", i);
675 if (inputSampleRate == 96000)
677 WInDev[i].caps.dwFormats |= WAVE_FORMAT_96M08;
678 WInDev[i].caps.dwFormats |= WAVE_FORMAT_96S08;
679 WInDev[i].caps.dwFormats |= WAVE_FORMAT_96M16;
680 WInDev[i].caps.dwFormats |= WAVE_FORMAT_96S16;
682 if (inputSampleRate == 48000)
684 WInDev[i].caps.dwFormats |= WAVE_FORMAT_48M08;
685 WInDev[i].caps.dwFormats |= WAVE_FORMAT_48S08;
686 WInDev[i].caps.dwFormats |= WAVE_FORMAT_48M16;
687 WInDev[i].caps.dwFormats |= WAVE_FORMAT_48S16;
689 if (inputSampleRate == 44100)
691 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
692 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
693 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
694 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
696 if (inputSampleRate == 22050)
698 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
699 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
700 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
701 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
703 if (inputSampleRate == 11025)
705 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
706 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
707 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
708 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
711 WInDev[i].caps.wChannels = 2;
714 /* create mach messages handler */
715 srandomdev();
716 messageThreadPortName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
717 CFSTR("WaveMessagePort.%d.%lu"), getpid(), (unsigned long)random());
718 if (!messageThreadPortName)
720 ERR("Can't create message thread port name\n");
721 return DRV_FAILURE;
724 port_ReceiveInMessageThread = CFMessagePortCreateLocal(kCFAllocatorDefault, messageThreadPortName,
725 &wodMessageHandler, NULL, NULL);
726 if (!port_ReceiveInMessageThread)
728 ERR("Can't create message thread local port\n");
729 CFRelease(messageThreadPortName);
730 return DRV_FAILURE;
733 Port_SendToMessageThread = CFMessagePortCreateRemote(kCFAllocatorDefault, messageThreadPortName);
734 CFRelease(messageThreadPortName);
735 if (!Port_SendToMessageThread)
737 ERR("Can't create port for sending to message thread\n");
738 CFRelease(port_ReceiveInMessageThread);
739 return DRV_FAILURE;
742 /* Cannot WAIT for any events because we are called from the loader (which has a lock on loading stuff) */
743 /* We might want to wait for this thread to be created -- but we cannot -- not here at least */
744 /* Instead track the thread so we can clean it up later */
745 if ( hThread )
747 ERR("Message thread already started -- expect problems\n");
749 hThread = CreateThread(NULL, 0, messageThread, (LPVOID)port_ReceiveInMessageThread, 0, NULL);
750 if ( !hThread )
752 ERR("Can't create message thread\n");
753 CFRelease(port_ReceiveInMessageThread);
754 CFRelease(Port_SendToMessageThread);
755 Port_SendToMessageThread = NULL;
756 return DRV_FAILURE;
759 /* The message thread is responsible for releasing port_ReceiveInMessageThread. */
761 return DRV_SUCCESS;
764 void CoreAudio_WaveRelease(void)
766 /* Stop CFRunLoop in messageThread */
767 TRACE("()\n");
769 if (!Port_SendToMessageThread)
770 return;
772 CFMessagePortSendRequest(Port_SendToMessageThread, kStopLoopMessage, NULL, 0.0, 0.0, NULL, NULL);
773 CFRelease(Port_SendToMessageThread);
774 Port_SendToMessageThread = NULL;
776 /* Wait for the thread to finish and clean it up */
777 /* This rids us of any quick start/shutdown driver crashes */
778 WaitForSingleObject(hThread, INFINITE);
779 CloseHandle(hThread);
780 hThread = NULL;
783 /*======================================================================*
784 * Low level WAVE OUT implementation *
785 *======================================================================*/
787 /**************************************************************************
788 * wodNotifyClient [internal]
790 static void wodNotifyClient(WINE_WAVEOUT_INSTANCE* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
792 TRACE("wMsg = 0x%04x dwParm1 = %04x dwParam2 = %04x\n", wMsg, dwParam1, dwParam2);
794 switch (wMsg) {
795 case WOM_OPEN:
796 case WOM_CLOSE:
797 case WOM_DONE:
798 if (wwo->wFlags != DCB_NULL &&
799 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
800 (HDRVR)wwo->waveDesc.hWave, wMsg, wwo->waveDesc.dwInstance,
801 dwParam1, dwParam2))
803 WARN("can't notify client !\n");
805 break;
806 default:
807 FIXME("Unknown callback message %u\n", wMsg);
812 /**************************************************************************
813 * wodGetDevCaps [internal]
815 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
817 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
819 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
821 if (wDevID >= MAX_WAVEOUTDRV)
823 TRACE("MAX_WAVOUTDRV reached !\n");
824 return MMSYSERR_BADDEVICEID;
827 TRACE("dwSupport=(0x%x), dwFormats=(0x%x)\n", WOutDev[wDevID].caps.dwSupport, WOutDev[wDevID].caps.dwFormats);
828 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
829 return MMSYSERR_NOERROR;
832 /**************************************************************************
833 * wodOpen [internal]
835 static DWORD wodOpen(WORD wDevID, WINE_WAVEOUT_INSTANCE** pInstance, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
837 WINE_WAVEOUT_INSTANCE* wwo;
838 DWORD ret;
839 AudioStreamBasicDescription streamFormat;
840 AudioUnit audioUnit = NULL;
841 BOOL auInited = FALSE;
843 TRACE("(%u, %p, %p, %08x);\n", wDevID, pInstance, lpDesc, dwFlags);
844 if (lpDesc == NULL)
846 WARN("Invalid Parameter !\n");
847 return MMSYSERR_INVALPARAM;
849 if (wDevID >= MAX_WAVEOUTDRV) {
850 TRACE("MAX_WAVOUTDRV reached !\n");
851 return MMSYSERR_BADDEVICEID;
854 TRACE("Format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
855 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
856 lpDesc->lpFormat->nSamplesPerSec, lpDesc->lpFormat->wBitsPerSample);
858 if (!supportedFormat(lpDesc->lpFormat))
860 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
861 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
862 lpDesc->lpFormat->nSamplesPerSec, lpDesc->lpFormat->wBitsPerSample);
863 return WAVERR_BADFORMAT;
866 if (dwFlags & WAVE_FORMAT_QUERY)
868 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
869 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
870 lpDesc->lpFormat->nSamplesPerSec);
871 return MMSYSERR_NOERROR;
874 /* nBlockAlign and nAvgBytesPerSec are output variables for dsound */
875 if (lpDesc->lpFormat->nBlockAlign != lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8) {
876 lpDesc->lpFormat->nBlockAlign = lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
877 WARN("Fixing nBlockAlign\n");
879 if (lpDesc->lpFormat->nAvgBytesPerSec!= lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign) {
880 lpDesc->lpFormat->nAvgBytesPerSec = lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
881 WARN("Fixing nAvgBytesPerSec\n");
884 /* We proceed in three phases:
885 * o Allocate the device instance, marking it as opening
886 * o Create, configure, and start the Audio Unit. To avoid deadlock,
887 * this has to be done without holding wwo->lock.
888 * o If that was successful, finish setting up our instance and add it
889 * to the device's list.
890 * Otherwise, clean up and deallocate the instance.
892 wwo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wwo));
893 if (!wwo)
894 return MMSYSERR_NOMEM;
896 wwo->woID = wDevID;
897 wwo->state = WINE_WS_OPENING;
899 if (!AudioUnit_CreateDefaultAudioUnit((void *) wwo, &audioUnit))
901 ERR("CoreAudio_CreateDefaultAudioUnit(0x%04x) failed\n", wDevID);
902 ret = MMSYSERR_ERROR;
903 goto error;
906 streamFormat.mFormatID = kAudioFormatLinearPCM;
907 streamFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked;
908 /* FIXME check for 32bits float -> kLinearPCMFormatFlagIsFloat */
909 if (lpDesc->lpFormat->wBitsPerSample != 8)
910 streamFormat.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
911 # ifdef WORDS_BIGENDIAN
912 streamFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; /* FIXME Wave format is little endian */
913 # endif
915 streamFormat.mSampleRate = lpDesc->lpFormat->nSamplesPerSec;
916 streamFormat.mChannelsPerFrame = lpDesc->lpFormat->nChannels;
917 streamFormat.mFramesPerPacket = 1;
918 streamFormat.mBitsPerChannel = lpDesc->lpFormat->wBitsPerSample;
919 streamFormat.mBytesPerFrame = streamFormat.mBitsPerChannel * streamFormat.mChannelsPerFrame / 8;
920 streamFormat.mBytesPerPacket = streamFormat.mBytesPerFrame * streamFormat.mFramesPerPacket;
922 ret = AudioUnit_InitializeWithStreamDescription(audioUnit, &streamFormat);
923 if (!ret)
925 ret = WAVERR_BADFORMAT; /* FIXME return an error based on the OSStatus */
926 goto error;
928 auInited = TRUE;
930 AudioUnit_SetVolume(audioUnit, LOWORD(WOutDev[wDevID].device_volume) / 65535.0f,
931 HIWORD(WOutDev[wDevID].device_volume) / 65535.0f);
933 /* Our render callback CoreAudio_woAudioUnitIOProc may be called before
934 * AudioOutputUnitStart returns. Core Audio will grab its own internal
935 * lock before calling it and the callback grabs wwo->lock. This would
936 * deadlock if we were holding wwo->lock.
937 * Also, the callback has to safely do nothing in that case, because
938 * wwo hasn't been completely filled out, yet. This is assured by state
939 * being WINE_WS_OPENING. */
940 ret = AudioOutputUnitStart(audioUnit);
941 if (ret)
943 ERR("AudioOutputUnitStart failed: %08x\n", ret);
944 ret = MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
945 goto error;
949 OSSpinLockLock(&wwo->lock);
950 assert(wwo->state == WINE_WS_OPENING);
952 wwo->audioUnit = audioUnit;
953 wwo->streamDescription = streamFormat;
955 wwo->state = WINE_WS_PLAYING;
957 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
959 wwo->waveDesc = *lpDesc;
960 copyFormat(lpDesc->lpFormat, &wwo->format);
962 WOutDev[wDevID].trace_on = TRACE_ON(wave);
963 WOutDev[wDevID].warn_on = WARN_ON(wave);
964 WOutDev[wDevID].err_on = ERR_ON(wave);
966 OSSpinLockUnlock(&wwo->lock);
968 OSSpinLockLock(&WOutDev[wDevID].lock);
969 list_add_head(&WOutDev[wDevID].instances, &wwo->entry);
970 OSSpinLockUnlock(&WOutDev[wDevID].lock);
972 *pInstance = wwo;
973 TRACE("opened instance %p\n", wwo);
975 wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
977 return MMSYSERR_NOERROR;
979 error:
980 if (audioUnit)
982 if (auInited)
983 AudioUnitUninitialize(audioUnit);
984 AudioUnit_CloseAudioUnit(audioUnit);
987 OSSpinLockLock(&wwo->lock);
988 assert(wwo->state == WINE_WS_OPENING);
989 /* OSSpinLockUnlock(&wwo->lock); *//* No need, about to free */
990 HeapFree(GetProcessHeap(), 0, wwo);
992 return ret;
995 /**************************************************************************
996 * wodClose [internal]
998 static DWORD wodClose(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
1000 DWORD ret = MMSYSERR_NOERROR;
1002 TRACE("(%u, %p);\n", wDevID, wwo);
1004 if (wDevID >= MAX_WAVEOUTDRV)
1006 WARN("bad device ID !\n");
1007 return MMSYSERR_BADDEVICEID;
1010 OSSpinLockLock(&wwo->lock);
1011 if (wwo->lpQueuePtr)
1013 OSSpinLockUnlock(&wwo->lock);
1014 WARN("buffers still playing !\n");
1015 return WAVERR_STILLPLAYING;
1016 } else
1018 OSStatus err;
1019 AudioUnit audioUnit = wwo->audioUnit;
1021 /* sanity check: this should not happen since the device must have been reset before */
1022 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1024 wwo->state = WINE_WS_CLOSING; /* mark the device as closing */
1025 wwo->audioUnit = NULL;
1027 OSSpinLockUnlock(&wwo->lock);
1029 err = AudioUnitUninitialize(audioUnit);
1030 if (err) {
1031 ERR("AudioUnitUninitialize return %s\n", wine_dbgstr_fourcc(err));
1032 return MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
1035 if ( !AudioUnit_CloseAudioUnit(audioUnit) )
1037 ERR("Can't close AudioUnit\n");
1038 return MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
1041 OSSpinLockLock(&WOutDev[wDevID].lock);
1042 list_remove(&wwo->entry);
1043 OSSpinLockUnlock(&WOutDev[wDevID].lock);
1045 wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1047 HeapFree(GetProcessHeap(), 0, wwo);
1050 return ret;
1053 /**************************************************************************
1054 * wodPrepare [internal]
1056 static DWORD wodPrepare(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1058 TRACE("(%u, %p, %p, %08x);\n", wDevID, wwo, lpWaveHdr, dwSize);
1060 if (wDevID >= MAX_WAVEOUTDRV) {
1061 WARN("bad device ID !\n");
1062 return MMSYSERR_BADDEVICEID;
1065 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1066 return WAVERR_STILLPLAYING;
1068 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1069 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1071 return MMSYSERR_NOERROR;
1074 /**************************************************************************
1075 * wodUnprepare [internal]
1077 static DWORD wodUnprepare(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1079 TRACE("(%u, %p, %p, %08x);\n", wDevID, wwo, lpWaveHdr, dwSize);
1081 if (wDevID >= MAX_WAVEOUTDRV) {
1082 WARN("bad device ID !\n");
1083 return MMSYSERR_BADDEVICEID;
1086 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1087 return WAVERR_STILLPLAYING;
1089 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1090 lpWaveHdr->dwFlags |= WHDR_DONE;
1092 return MMSYSERR_NOERROR;
1096 /**************************************************************************
1097 * wodHelper_CheckForLoopBegin [internal]
1099 * Check if the new waveheader is the beginning of a loop, and set up
1100 * state if so.
1101 * This is called with the WAVEOUT_INSTANCE lock held.
1102 * Call from AudioUnit IO thread can't use Wine debug channels.
1104 static void wodHelper_CheckForLoopBegin(WINE_WAVEOUT_INSTANCE* wwo)
1106 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1108 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)
1110 if (wwo->lpLoopPtr)
1112 if (WOutDev[wwo->woID].warn_on)
1113 fprintf(stderr, "warn:winecoreaudio:wodHelper_CheckForLoopBegin Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1115 else
1117 if (WOutDev[wwo->woID].trace_on)
1118 fprintf(stderr, "trace:winecoreaudio:wodHelper_CheckForLoopBegin Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1120 wwo->lpLoopPtr = lpWaveHdr;
1121 /* Windows does not touch WAVEHDR.dwLoops,
1122 * so we need to make an internal copy */
1123 wwo->dwLoops = lpWaveHdr->dwLoops;
1129 /**************************************************************************
1130 * wodHelper_PlayPtrNext [internal]
1132 * Advance the play pointer to the next waveheader, looping if required.
1133 * This is called with the WAVEOUT_INSTANCE lock held.
1134 * Call from AudioUnit IO thread can't use Wine debug channels.
1136 static void wodHelper_PlayPtrNext(WINE_WAVEOUT_INSTANCE* wwo)
1138 BOOL didLoopBack = FALSE;
1140 wwo->dwPartialOffset = 0;
1141 if ((wwo->lpPlayPtr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr)
1143 /* We're at the end of a loop, loop if required */
1144 if (wwo->dwLoops > 1)
1146 wwo->dwLoops--;
1147 wwo->lpPlayPtr = wwo->lpLoopPtr;
1148 didLoopBack = TRUE;
1150 else
1152 wwo->lpLoopPtr = NULL;
1155 if (!didLoopBack)
1157 /* We didn't loop back. Advance to the next wave header */
1158 wwo->lpPlayPtr = wwo->lpPlayPtr->lpNext;
1160 if (wwo->lpPlayPtr)
1161 wodHelper_CheckForLoopBegin(wwo);
1165 /* Send the "done" notification for each WAVEHDR in a list. The list must be
1166 * free-standing. It should not be part of a device instance's queue.
1167 * This function must be called with the WAVEOUT_INSTANCE lock *not* held.
1168 * Furthermore, it does not lock it, itself. That's because the callback to the
1169 * application may prompt the application to operate on the device, and we don't
1170 * want to deadlock.
1172 static void wodHelper_NotifyDoneForList(WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr)
1174 while (lpWaveHdr)
1176 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1178 lpWaveHdr->lpNext = NULL;
1179 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1180 lpWaveHdr->dwFlags |= WHDR_DONE;
1181 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1183 lpWaveHdr = lpNext;
1187 /* if force is TRUE then notify the client that all the headers were completed
1189 static void wodHelper_NotifyCompletions(WINE_WAVEOUT_INSTANCE* wwo, BOOL force)
1191 LPWAVEHDR lpFirstDoneWaveHdr = NULL;
1193 OSSpinLockLock(&wwo->lock);
1195 /* First, excise all of the done headers from the queue into
1196 * a free-standing list. */
1197 if (force)
1199 lpFirstDoneWaveHdr = wwo->lpQueuePtr;
1200 wwo->lpQueuePtr = NULL;
1202 else
1204 LPWAVEHDR lpWaveHdr;
1205 LPWAVEHDR lpLastDoneWaveHdr = NULL;
1207 /* Start from lpQueuePtr and keep notifying until:
1208 * - we hit an unwritten wavehdr
1209 * - we hit the beginning of a running loop
1210 * - we hit a wavehdr which hasn't finished playing
1212 for (
1213 lpWaveHdr = wwo->lpQueuePtr;
1214 lpWaveHdr &&
1215 lpWaveHdr != wwo->lpPlayPtr &&
1216 lpWaveHdr != wwo->lpLoopPtr;
1217 lpWaveHdr = lpWaveHdr->lpNext
1220 if (!lpFirstDoneWaveHdr)
1221 lpFirstDoneWaveHdr = lpWaveHdr;
1222 lpLastDoneWaveHdr = lpWaveHdr;
1225 if (lpLastDoneWaveHdr)
1227 wwo->lpQueuePtr = lpLastDoneWaveHdr->lpNext;
1228 lpLastDoneWaveHdr->lpNext = NULL;
1232 OSSpinLockUnlock(&wwo->lock);
1234 /* Now, send the "done" notification for each header in our list. */
1235 wodHelper_NotifyDoneForList(wwo, lpFirstDoneWaveHdr);
1239 /**************************************************************************
1240 * wodWrite [internal]
1243 static DWORD wodWrite(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1245 LPWAVEHDR*wh;
1247 TRACE("(%u, %p, %p, %lu, %08X);\n", wDevID, wwo, lpWaveHdr, (unsigned long)lpWaveHdr->dwBufferLength, dwSize);
1249 /* first, do the sanity checks... */
1250 if (wDevID >= MAX_WAVEOUTDRV)
1252 WARN("bad dev ID !\n");
1253 return MMSYSERR_BADDEVICEID;
1256 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1258 TRACE("unprepared\n");
1259 return WAVERR_UNPREPARED;
1262 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1264 TRACE("still playing\n");
1265 return WAVERR_STILLPLAYING;
1268 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1269 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1270 lpWaveHdr->lpNext = 0;
1272 OSSpinLockLock(&wwo->lock);
1273 /* insert buffer at the end of queue */
1274 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
1275 /* Do nothing */;
1276 *wh = lpWaveHdr;
1278 if (!wwo->lpPlayPtr)
1280 wwo->lpPlayPtr = lpWaveHdr;
1282 wodHelper_CheckForLoopBegin(wwo);
1284 wwo->dwPartialOffset = 0;
1286 OSSpinLockUnlock(&wwo->lock);
1288 return MMSYSERR_NOERROR;
1291 /**************************************************************************
1292 * wodPause [internal]
1294 static DWORD wodPause(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
1296 OSStatus status;
1298 TRACE("(%u, %p);!\n", wDevID, wwo);
1300 if (wDevID >= MAX_WAVEOUTDRV)
1302 WARN("bad device ID !\n");
1303 return MMSYSERR_BADDEVICEID;
1306 /* The order of the following operations is important since we can't hold
1307 * the mutex while we make an Audio Unit call. Stop the Audio Unit before
1308 * setting the PAUSED state. In wodRestart, the order is reversed. This
1309 * guarantees that we can't get into a situation where the state is
1310 * PLAYING but the Audio Unit isn't running. Although we can be in PAUSED
1311 * state with the Audio Unit still running, that's harmless because the
1312 * render callback will just produce silence.
1314 status = AudioOutputUnitStop(wwo->audioUnit);
1315 if (status)
1316 WARN("AudioOutputUnitStop return %s\n", wine_dbgstr_fourcc(status));
1318 OSSpinLockLock(&wwo->lock);
1319 if (wwo->state == WINE_WS_PLAYING)
1320 wwo->state = WINE_WS_PAUSED;
1321 OSSpinLockUnlock(&wwo->lock);
1323 return MMSYSERR_NOERROR;
1326 /**************************************************************************
1327 * wodRestart [internal]
1329 static DWORD wodRestart(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
1331 OSStatus status;
1333 TRACE("(%u, %p);\n", wDevID, wwo);
1335 if (wDevID >= MAX_WAVEOUTDRV )
1337 WARN("bad device ID !\n");
1338 return MMSYSERR_BADDEVICEID;
1341 /* The order of the following operations is important since we can't hold
1342 * the mutex while we make an Audio Unit call. Set the PLAYING
1343 * state before starting the Audio Unit. In wodPause, the order is
1344 * reversed. This guarantees that we can't get into a situation where
1345 * the state is PLAYING but the Audio Unit isn't running.
1346 * Although we can be in PAUSED state with the Audio Unit still running,
1347 * that's harmless because the render callback will just produce silence.
1349 OSSpinLockLock(&wwo->lock);
1350 if (wwo->state == WINE_WS_PAUSED)
1351 wwo->state = WINE_WS_PLAYING;
1352 OSSpinLockUnlock(&wwo->lock);
1354 status = AudioOutputUnitStart(wwo->audioUnit);
1355 if (status) {
1356 ERR("AudioOutputUnitStart return %s\n", wine_dbgstr_fourcc(status));
1357 return MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
1360 return MMSYSERR_NOERROR;
1363 /**************************************************************************
1364 * wodReset [internal]
1366 static DWORD wodReset(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
1368 OSStatus status;
1369 LPWAVEHDR lpSavedQueuePtr;
1371 TRACE("(%u, %p);\n", wDevID, wwo);
1373 if (wDevID >= MAX_WAVEOUTDRV)
1375 WARN("bad device ID !\n");
1376 return MMSYSERR_BADDEVICEID;
1379 OSSpinLockLock(&wwo->lock);
1381 if (wwo->state == WINE_WS_CLOSING || wwo->state == WINE_WS_OPENING)
1383 OSSpinLockUnlock(&wwo->lock);
1384 WARN("resetting a closed device\n");
1385 return MMSYSERR_INVALHANDLE;
1388 lpSavedQueuePtr = wwo->lpQueuePtr;
1389 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1390 wwo->state = WINE_WS_PLAYING;
1391 wwo->dwPlayedTotal = 0;
1393 wwo->dwPartialOffset = 0; /* Clear partial wavehdr */
1395 OSSpinLockUnlock(&wwo->lock);
1397 status = AudioOutputUnitStart(wwo->audioUnit);
1399 if (status) {
1400 ERR( "AudioOutputUnitStart return %s\n", wine_dbgstr_fourcc(status));
1401 return MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
1404 /* Now, send the "done" notification for each header in our list. */
1405 /* Do this last so the reset operation is effectively complete before the
1406 * app does whatever it's going to do in response to these notifications. */
1407 wodHelper_NotifyDoneForList(wwo, lpSavedQueuePtr);
1409 return MMSYSERR_NOERROR;
1412 /**************************************************************************
1413 * wodBreakLoop [internal]
1415 static DWORD wodBreakLoop(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
1417 TRACE("(%u, %p);\n", wDevID, wwo);
1419 if (wDevID >= MAX_WAVEOUTDRV)
1421 WARN("bad device ID !\n");
1422 return MMSYSERR_BADDEVICEID;
1425 OSSpinLockLock(&wwo->lock);
1427 if (wwo->lpLoopPtr != NULL)
1429 /* ensure exit at end of current loop */
1430 wwo->dwLoops = 1;
1433 OSSpinLockUnlock(&wwo->lock);
1435 return MMSYSERR_NOERROR;
1438 /**************************************************************************
1439 * wodGetPosition [internal]
1441 static DWORD wodGetPosition(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPMMTIME lpTime, DWORD uSize)
1443 DWORD val;
1445 TRACE("(%u, %p, %p, %u);\n", wDevID, wwo, lpTime, uSize);
1447 if (wDevID >= MAX_WAVEOUTDRV)
1449 WARN("bad device ID !\n");
1450 return MMSYSERR_BADDEVICEID;
1453 /* if null pointer to time structure return error */
1454 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1456 OSSpinLockLock(&wwo->lock);
1457 val = wwo->dwPlayedTotal;
1458 OSSpinLockUnlock(&wwo->lock);
1460 return bytes_to_mmtime(lpTime, val, &wwo->format);
1463 /**************************************************************************
1464 * wodGetVolume [internal]
1466 static DWORD wodGetVolume(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPDWORD lpdwVol)
1468 if (wDevID >= MAX_WAVEOUTDRV)
1470 WARN("bad device ID !\n");
1471 return MMSYSERR_BADDEVICEID;
1474 TRACE("(%u, %p, %p);\n", wDevID, wwo, lpdwVol);
1476 if (wwo)
1478 float left;
1479 float right;
1481 AudioUnit_GetVolume(wwo->audioUnit, &left, &right);
1482 *lpdwVol = (WORD)(left * 0xFFFFl) + ((WORD)(right * 0xFFFFl) << 16);
1484 else
1485 *lpdwVol = WOutDev[wDevID].device_volume;
1487 return MMSYSERR_NOERROR;
1490 /**************************************************************************
1491 * wodSetVolume [internal]
1493 static DWORD wodSetVolume(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, DWORD dwParam)
1495 float left;
1496 float right;
1498 if (wDevID >= MAX_WAVEOUTDRV)
1500 WARN("bad device ID !\n");
1501 return MMSYSERR_BADDEVICEID;
1504 left = LOWORD(dwParam) / 65535.0f;
1505 right = HIWORD(dwParam) / 65535.0f;
1507 TRACE("(%u, %p, %08x);\n", wDevID, wwo, dwParam);
1509 if (wwo)
1510 AudioUnit_SetVolume(wwo->audioUnit, left, right);
1511 else
1513 OSSpinLockLock(&WOutDev[wDevID].lock);
1514 LIST_FOR_EACH_ENTRY(wwo, &WOutDev[wDevID].instances, WINE_WAVEOUT_INSTANCE, entry)
1515 AudioUnit_SetVolume(wwo->audioUnit, left, right);
1516 OSSpinLockUnlock(&WOutDev[wDevID].lock);
1518 WOutDev[wDevID].device_volume = dwParam;
1521 return MMSYSERR_NOERROR;
1524 /**************************************************************************
1525 * wodGetNumDevs [internal]
1527 static DWORD wodGetNumDevs(void)
1529 TRACE("\n");
1530 return MAX_WAVEOUTDRV;
1533 /**************************************************************************
1534 * wodDevInterfaceSize [internal]
1536 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1538 TRACE("(%u, %p)\n", wDevID, dwParam1);
1540 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].cadev->interface_name, -1,
1541 NULL, 0 ) * sizeof(WCHAR);
1542 return MMSYSERR_NOERROR;
1545 /**************************************************************************
1546 * wodDevInterface [internal]
1548 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1550 TRACE("\n");
1551 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].cadev->interface_name, -1,
1552 NULL, 0 ) * sizeof(WCHAR))
1554 MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].cadev->interface_name, -1,
1555 dwParam1, dwParam2 / sizeof(WCHAR));
1556 return MMSYSERR_NOERROR;
1558 return MMSYSERR_INVALPARAM;
1561 /**************************************************************************
1562 * widDsCreate [internal]
1564 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1566 TRACE("(%d,%p)\n",wDevID,drv);
1568 FIXME("DirectSound not implemented\n");
1569 FIXME("The (slower) DirectSound HEL mode will be used instead.\n");
1570 return MMSYSERR_NOTSUPPORTED;
1573 /**************************************************************************
1574 * wodDsDesc [internal]
1576 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1578 /* The DirectSound HEL will automatically wrap a non-DirectSound-capable
1579 * driver in a DirectSound adaptor, thus allowing the driver to be used by
1580 * DirectSound clients. However, it only does this if we respond
1581 * successfully to the DRV_QUERYDSOUNDDESC message. It's enough to fill in
1582 * the driver and device names of the description output parameter. */
1583 *desc = WOutDev[wDevID].cadev->ds_desc;
1584 return MMSYSERR_NOERROR;
1587 /**************************************************************************
1588 * wodMessage (WINECOREAUDIO.7)
1590 DWORD WINAPI CoreAudio_wodMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1591 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1593 WINE_WAVEOUT_INSTANCE* wwo = (WINE_WAVEOUT_INSTANCE*)dwUser;
1595 TRACE("(%u, %s, %p, %p, %p);\n",
1596 wDevID, getMessage(wMsg), (void*)dwUser, (void*)dwParam1, (void*)dwParam2);
1598 switch (wMsg) {
1599 case DRVM_INIT:
1600 case DRVM_EXIT:
1601 case DRVM_ENABLE:
1602 case DRVM_DISABLE:
1604 /* FIXME: Pretend this is supported */
1605 return 0;
1606 case WODM_OPEN: return wodOpen(wDevID, (WINE_WAVEOUT_INSTANCE**)dwUser, (LPWAVEOPENDESC) dwParam1, dwParam2);
1607 case WODM_CLOSE: return wodClose(wDevID, wwo);
1608 case WODM_WRITE: return wodWrite(wDevID, wwo, (LPWAVEHDR) dwParam1, dwParam2);
1609 case WODM_PAUSE: return wodPause(wDevID, wwo);
1610 case WODM_GETPOS: return wodGetPosition(wDevID, wwo, (LPMMTIME) dwParam1, dwParam2);
1611 case WODM_BREAKLOOP: return wodBreakLoop(wDevID, wwo);
1612 case WODM_PREPARE: return wodPrepare(wDevID, wwo, (LPWAVEHDR)dwParam1, dwParam2);
1613 case WODM_UNPREPARE: return wodUnprepare(wDevID, wwo, (LPWAVEHDR)dwParam1, dwParam2);
1615 case WODM_GETDEVCAPS: return wodGetDevCaps(wDevID, (LPWAVEOUTCAPSW) dwParam1, dwParam2);
1616 case WODM_GETNUMDEVS: return wodGetNumDevs();
1618 case WODM_GETPITCH:
1619 case WODM_SETPITCH:
1620 case WODM_GETPLAYBACKRATE:
1621 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1622 case WODM_GETVOLUME: return wodGetVolume(wDevID, wwo, (LPDWORD)dwParam1);
1623 case WODM_SETVOLUME: return wodSetVolume(wDevID, wwo, dwParam1);
1624 case WODM_RESTART: return wodRestart(wDevID, wwo);
1625 case WODM_RESET: return wodReset(wDevID, wwo);
1627 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1628 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1629 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1630 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1632 default:
1633 FIXME("unknown message %d!\n", wMsg);
1636 return MMSYSERR_NOTSUPPORTED;
1639 /*======================================================================*
1640 * Low level DSOUND implementation *
1641 *======================================================================*/
1643 typedef struct IDsDriverImpl IDsDriverImpl;
1644 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1646 struct IDsDriverImpl
1648 /* IUnknown fields */
1649 const IDsDriverVtbl *lpVtbl;
1650 DWORD ref;
1651 /* IDsDriverImpl fields */
1652 UINT wDevID;
1653 IDsDriverBufferImpl*primary;
1656 struct IDsDriverBufferImpl
1658 /* IUnknown fields */
1659 const IDsDriverBufferVtbl *lpVtbl;
1660 DWORD ref;
1661 /* IDsDriverBufferImpl fields */
1662 IDsDriverImpl* drv;
1663 DWORD buflen;
1668 CoreAudio IO threaded callback,
1669 we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
1671 OSStatus CoreAudio_woAudioUnitIOProc(void *inRefCon,
1672 AudioUnitRenderActionFlags *ioActionFlags,
1673 const AudioTimeStamp *inTimeStamp,
1674 UInt32 inBusNumber,
1675 UInt32 inNumberFrames,
1676 AudioBufferList *ioData)
1678 UInt32 buffer;
1679 WINE_WAVEOUT_INSTANCE* wwo = (WINE_WAVEOUT_INSTANCE*)inRefCon;
1680 int needNotify = 0;
1682 unsigned int dataNeeded = ioData->mBuffers[0].mDataByteSize;
1683 unsigned int dataProvided = 0;
1685 OSSpinLockLock(&wwo->lock);
1687 /* We might have been called before wwo has been completely filled out by
1688 * wodOpen, or while it's being closed in wodClose. We have to do nothing
1689 * in that case. The check of wwo->state below ensures that. */
1690 while (dataNeeded > 0 && wwo->state == WINE_WS_PLAYING && wwo->lpPlayPtr)
1692 unsigned int available = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1693 unsigned int toCopy;
1695 if (available >= dataNeeded)
1696 toCopy = dataNeeded;
1697 else
1698 toCopy = available;
1700 if (toCopy > 0)
1702 memcpy((char*)ioData->mBuffers[0].mData + dataProvided,
1703 wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toCopy);
1704 wwo->dwPartialOffset += toCopy;
1705 wwo->dwPlayedTotal += toCopy;
1706 dataProvided += toCopy;
1707 dataNeeded -= toCopy;
1708 available -= toCopy;
1711 if (available == 0)
1713 wodHelper_PlayPtrNext(wwo);
1714 needNotify = 1;
1717 ioData->mBuffers[0].mDataByteSize = dataProvided;
1719 OSSpinLockUnlock(&wwo->lock);
1721 /* We can't provide any more wave data. Fill the rest with silence. */
1722 if (dataNeeded > 0)
1724 if (!dataProvided)
1725 *ioActionFlags |= kAudioUnitRenderAction_OutputIsSilence;
1726 memset((char*)ioData->mBuffers[0].mData + dataProvided, 0, dataNeeded);
1727 dataProvided += dataNeeded;
1728 dataNeeded = 0;
1731 /* We only fill buffer 0. Set any others that might be requested to 0. */
1732 for (buffer = 1; buffer < ioData->mNumberBuffers; buffer++)
1734 memset(ioData->mBuffers[buffer].mData, 0, ioData->mBuffers[buffer].mDataByteSize);
1737 if (needNotify) wodSendNotifyCompletionsMessage(wwo);
1738 return noErr;
1742 /*======================================================================*
1743 * Low level WAVE IN implementation *
1744 *======================================================================*/
1746 /**************************************************************************
1747 * widNotifyClient [internal]
1749 static void widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1751 TRACE("wMsg = 0x%04x dwParm1 = %04X dwParam2 = %04X\n", wMsg, dwParam1, dwParam2);
1753 switch (wMsg)
1755 case WIM_OPEN:
1756 case WIM_CLOSE:
1757 case WIM_DATA:
1758 if (wwi->wFlags != DCB_NULL &&
1759 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
1760 (HDRVR)wwi->waveDesc.hWave, wMsg, wwi->waveDesc.dwInstance,
1761 dwParam1, dwParam2))
1763 WARN("can't notify client !\n");
1765 break;
1766 default:
1767 FIXME("Unknown callback message %u\n", wMsg);
1772 /**************************************************************************
1773 * widHelper_NotifyCompletions [internal]
1775 static void widHelper_NotifyCompletions(WINE_WAVEIN* wwi)
1777 LPWAVEHDR lpWaveHdr;
1778 LPWAVEHDR lpFirstDoneWaveHdr = NULL;
1779 LPWAVEHDR lpLastDoneWaveHdr = NULL;
1781 OSSpinLockLock(&wwi->lock);
1783 /* First, excise all of the done headers from the queue into
1784 * a free-standing list. */
1786 /* Start from lpQueuePtr and keep notifying until:
1787 * - we hit an unfilled wavehdr
1788 * - we hit the end of the list
1790 for (
1791 lpWaveHdr = wwi->lpQueuePtr;
1792 lpWaveHdr &&
1793 lpWaveHdr->dwBytesRecorded >= lpWaveHdr->dwBufferLength;
1794 lpWaveHdr = lpWaveHdr->lpNext
1797 if (!lpFirstDoneWaveHdr)
1798 lpFirstDoneWaveHdr = lpWaveHdr;
1799 lpLastDoneWaveHdr = lpWaveHdr;
1802 if (lpLastDoneWaveHdr)
1804 wwi->lpQueuePtr = lpLastDoneWaveHdr->lpNext;
1805 lpLastDoneWaveHdr->lpNext = NULL;
1808 OSSpinLockUnlock(&wwi->lock);
1810 /* Now, send the "done" notification for each header in our list. */
1811 lpWaveHdr = lpFirstDoneWaveHdr;
1812 while (lpWaveHdr)
1814 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1816 lpWaveHdr->lpNext = NULL;
1817 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1818 lpWaveHdr->dwFlags |= WHDR_DONE;
1819 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1821 lpWaveHdr = lpNext;
1826 /**************************************************************************
1827 * widGetDevCaps [internal]
1829 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
1831 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1833 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1835 if (wDevID >= MAX_WAVEINDRV)
1837 TRACE("MAX_WAVEINDRV reached !\n");
1838 return MMSYSERR_BADDEVICEID;
1841 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1842 return MMSYSERR_NOERROR;
1846 /**************************************************************************
1847 * widHelper_DestroyAudioBufferList [internal]
1848 * Convenience function to dispose of our audio buffers
1850 static void widHelper_DestroyAudioBufferList(AudioBufferList* list)
1852 if (list)
1854 UInt32 i;
1855 for (i = 0; i < list->mNumberBuffers; i++)
1857 if (list->mBuffers[i].mData)
1858 HeapFree(GetProcessHeap(), 0, list->mBuffers[i].mData);
1860 HeapFree(GetProcessHeap(), 0, list);
1865 #define AUDIOBUFFERLISTSIZE(numBuffers) (offsetof(AudioBufferList, mBuffers) + (numBuffers) * sizeof(AudioBuffer))
1867 /**************************************************************************
1868 * widHelper_AllocateAudioBufferList [internal]
1869 * Convenience function to allocate our audio buffers
1871 static AudioBufferList* widHelper_AllocateAudioBufferList(UInt32 numChannels, UInt32 bitsPerChannel, UInt32 bufferFrames, BOOL interleaved)
1873 UInt32 numBuffers;
1874 UInt32 channelsPerFrame;
1875 UInt32 bytesPerFrame;
1876 UInt32 bytesPerBuffer;
1877 AudioBufferList* list;
1878 UInt32 i;
1880 if (interleaved)
1882 /* For interleaved audio, we allocate one buffer for all channels. */
1883 numBuffers = 1;
1884 channelsPerFrame = numChannels;
1886 else
1888 numBuffers = numChannels;
1889 channelsPerFrame = 1;
1892 bytesPerFrame = bitsPerChannel * channelsPerFrame / 8;
1893 bytesPerBuffer = bytesPerFrame * bufferFrames;
1895 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, AUDIOBUFFERLISTSIZE(numBuffers));
1896 if (list == NULL)
1897 return NULL;
1899 list->mNumberBuffers = numBuffers;
1900 for (i = 0; i < numBuffers; ++i)
1902 list->mBuffers[i].mNumberChannels = channelsPerFrame;
1903 list->mBuffers[i].mDataByteSize = bytesPerBuffer;
1904 list->mBuffers[i].mData = HeapAlloc(GetProcessHeap(), 0, bytesPerBuffer);
1905 if (list->mBuffers[i].mData == NULL)
1907 widHelper_DestroyAudioBufferList(list);
1908 return NULL;
1911 return list;
1915 /**************************************************************************
1916 * widOpen [internal]
1918 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1920 WINE_WAVEIN* wwi;
1921 UInt32 frameCount;
1923 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1924 if (lpDesc == NULL)
1926 WARN("Invalid Parameter !\n");
1927 return MMSYSERR_INVALPARAM;
1929 if (wDevID >= MAX_WAVEINDRV)
1931 TRACE ("MAX_WAVEINDRV reached !\n");
1932 return MMSYSERR_BADDEVICEID;
1935 TRACE("Format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
1936 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1937 lpDesc->lpFormat->nSamplesPerSec, lpDesc->lpFormat->wBitsPerSample);
1939 if (!supportedFormat(lpDesc->lpFormat) ||
1940 lpDesc->lpFormat->nSamplesPerSec != AudioUnit_GetInputDeviceSampleRate()
1943 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
1944 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1945 lpDesc->lpFormat->nSamplesPerSec, lpDesc->lpFormat->wBitsPerSample);
1946 return WAVERR_BADFORMAT;
1949 if (dwFlags & WAVE_FORMAT_QUERY)
1951 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1952 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1953 lpDesc->lpFormat->nSamplesPerSec);
1954 return MMSYSERR_NOERROR;
1957 /* nBlockAlign and nAvgBytesPerSec are output variables for dsound */
1958 if (lpDesc->lpFormat->nBlockAlign != lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8) {
1959 lpDesc->lpFormat->nBlockAlign = lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
1960 WARN("Fixing nBlockAlign\n");
1962 if (lpDesc->lpFormat->nAvgBytesPerSec!= lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign) {
1963 lpDesc->lpFormat->nAvgBytesPerSec = lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
1964 WARN("Fixing nAvgBytesPerSec\n");
1967 wwi = &WInDev[wDevID];
1968 if (!OSSpinLockTry(&wwi->lock))
1969 return MMSYSERR_ALLOCATED;
1971 if (wwi->state != WINE_WS_CLOSED)
1973 OSSpinLockUnlock(&wwi->lock);
1974 return MMSYSERR_ALLOCATED;
1977 wwi->state = WINE_WS_STOPPED;
1978 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1980 wwi->waveDesc = *lpDesc;
1981 copyFormat(lpDesc->lpFormat, &wwi->format);
1983 wwi->dwTotalRecorded = 0;
1985 wwi->trace_on = TRACE_ON(wave);
1986 wwi->warn_on = WARN_ON(wave);
1987 wwi->err_on = ERR_ON(wave);
1989 if (!AudioUnit_CreateInputUnit(wwi, &wwi->audioUnit,
1990 wwi->format.wf.nChannels, wwi->format.wf.nSamplesPerSec,
1991 wwi->format.wBitsPerSample, &frameCount))
1993 OSSpinLockUnlock(&wwi->lock);
1994 ERR("AudioUnit_CreateInputUnit failed\n");
1995 return MMSYSERR_ERROR;
1998 /* Allocate our audio buffers */
1999 wwi->bufferList = widHelper_AllocateAudioBufferList(wwi->format.wf.nChannels,
2000 wwi->format.wBitsPerSample, frameCount, TRUE);
2001 if (wwi->bufferList == NULL)
2003 AudioUnitUninitialize(wwi->audioUnit);
2004 AudioUnit_CloseAudioUnit(wwi->audioUnit);
2005 OSSpinLockUnlock(&wwi->lock);
2006 ERR("Failed to allocate buffer list\n");
2007 return MMSYSERR_NOMEM;
2010 /* Keep a copy of the buffer list structure (but not the buffers themselves)
2011 * in case AudioUnitRender clobbers the original, as it tends to do. */
2012 wwi->bufferListCopy = HeapAlloc(GetProcessHeap(), 0, AUDIOBUFFERLISTSIZE(wwi->bufferList->mNumberBuffers));
2013 if (wwi->bufferListCopy == NULL)
2015 widHelper_DestroyAudioBufferList(wwi->bufferList);
2016 AudioUnitUninitialize(wwi->audioUnit);
2017 AudioUnit_CloseAudioUnit(wwi->audioUnit);
2018 OSSpinLockUnlock(&wwi->lock);
2019 ERR("Failed to allocate buffer list copy\n");
2020 return MMSYSERR_NOMEM;
2022 memcpy(wwi->bufferListCopy, wwi->bufferList, AUDIOBUFFERLISTSIZE(wwi->bufferList->mNumberBuffers));
2024 OSSpinLockUnlock(&wwi->lock);
2026 widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2028 return MMSYSERR_NOERROR;
2032 /**************************************************************************
2033 * widClose [internal]
2035 static DWORD widClose(WORD wDevID)
2037 DWORD ret = MMSYSERR_NOERROR;
2038 WINE_WAVEIN* wwi;
2039 OSStatus err;
2041 TRACE("(%u);\n", wDevID);
2043 if (wDevID >= MAX_WAVEINDRV)
2045 WARN("bad device ID !\n");
2046 return MMSYSERR_BADDEVICEID;
2049 wwi = &WInDev[wDevID];
2050 OSSpinLockLock(&wwi->lock);
2051 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2053 WARN("Device already closed.\n");
2054 ret = MMSYSERR_INVALHANDLE;
2056 else if (wwi->lpQueuePtr)
2058 WARN("Buffers in queue.\n");
2059 ret = WAVERR_STILLPLAYING;
2061 else
2063 wwi->state = WINE_WS_CLOSING;
2066 OSSpinLockUnlock(&wwi->lock);
2068 if (ret != MMSYSERR_NOERROR)
2069 return ret;
2072 /* Clean up and close the audio unit. This has to be done without
2073 * wwi->lock being held to avoid deadlock. AudioUnitUninitialize will
2074 * grab an internal Core Audio lock while waiting for the device work
2075 * thread to exit. Meanwhile the device work thread may be holding
2076 * that lock and trying to grab the wwi->lock in the callback. */
2077 err = AudioUnitUninitialize(wwi->audioUnit);
2078 if (err)
2079 ERR("AudioUnitUninitialize return %s\n", wine_dbgstr_fourcc(err));
2081 if (!AudioUnit_CloseAudioUnit(wwi->audioUnit))
2082 ERR("Can't close AudioUnit\n");
2085 OSSpinLockLock(&wwi->lock);
2086 assert(wwi->state == WINE_WS_CLOSING);
2088 /* Dellocate our audio buffers */
2089 widHelper_DestroyAudioBufferList(wwi->bufferList);
2090 wwi->bufferList = NULL;
2091 HeapFree(GetProcessHeap(), 0, wwi->bufferListCopy);
2092 wwi->bufferListCopy = NULL;
2094 wwi->audioUnit = NULL;
2095 wwi->state = WINE_WS_CLOSED;
2096 OSSpinLockUnlock(&wwi->lock);
2098 widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
2100 return ret;
2104 /**************************************************************************
2105 * widAddBuffer [internal]
2107 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2109 DWORD ret = MMSYSERR_NOERROR;
2110 WINE_WAVEIN* wwi;
2112 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
2114 if (wDevID >= MAX_WAVEINDRV)
2116 WARN("invalid device ID\n");
2117 return MMSYSERR_INVALHANDLE;
2119 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED))
2121 TRACE("never been prepared !\n");
2122 return WAVERR_UNPREPARED;
2124 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2126 TRACE("header already in use !\n");
2127 return WAVERR_STILLPLAYING;
2130 wwi = &WInDev[wDevID];
2131 OSSpinLockLock(&wwi->lock);
2133 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2135 WARN("Trying to add buffer to closed device.\n");
2136 ret = MMSYSERR_INVALHANDLE;
2138 else
2140 LPWAVEHDR* wh;
2142 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2143 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2144 lpWaveHdr->dwBytesRecorded = 0;
2145 lpWaveHdr->lpNext = NULL;
2147 /* insert buffer at end of queue */
2148 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
2149 /* Do nothing */;
2150 *wh = lpWaveHdr;
2153 OSSpinLockUnlock(&wwi->lock);
2155 return ret;
2159 /**************************************************************************
2160 * widStart [internal]
2162 static DWORD widStart(WORD wDevID)
2164 DWORD ret = MMSYSERR_NOERROR;
2165 WINE_WAVEIN* wwi;
2167 TRACE("(%u);\n", wDevID);
2168 if (wDevID >= MAX_WAVEINDRV)
2170 WARN("invalid device ID\n");
2171 return MMSYSERR_INVALHANDLE;
2174 /* The order of the following operations is important since we can't hold
2175 * the mutex while we make an Audio Unit call. Set the PLAYING state
2176 * before starting the Audio Unit. In widStop, the order is reversed.
2177 * This guarantees that we can't get into a situation where the state is
2178 * PLAYING but the Audio Unit isn't running. Although we can be in STOPPED
2179 * state with the Audio Unit still running, that's harmless because the
2180 * input callback will just throw away the sound data.
2182 wwi = &WInDev[wDevID];
2183 OSSpinLockLock(&wwi->lock);
2185 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2187 WARN("Trying to start closed device.\n");
2188 ret = MMSYSERR_INVALHANDLE;
2190 else
2191 wwi->state = WINE_WS_PLAYING;
2193 OSSpinLockUnlock(&wwi->lock);
2195 if (ret == MMSYSERR_NOERROR)
2197 /* Start pulling for audio data */
2198 OSStatus err = AudioOutputUnitStart(wwi->audioUnit);
2199 if (err != noErr)
2200 ERR("Failed to start AU: %08lx\n", err);
2202 TRACE("Recording started...\n");
2205 return ret;
2209 /**************************************************************************
2210 * widStop [internal]
2212 static DWORD widStop(WORD wDevID)
2214 DWORD ret = MMSYSERR_NOERROR;
2215 WINE_WAVEIN* wwi;
2216 WAVEHDR* lpWaveHdr = NULL;
2217 OSStatus err;
2219 TRACE("(%u);\n", wDevID);
2220 if (wDevID >= MAX_WAVEINDRV)
2222 WARN("invalid device ID\n");
2223 return MMSYSERR_INVALHANDLE;
2226 wwi = &WInDev[wDevID];
2228 /* The order of the following operations is important since we can't hold
2229 * the mutex while we make an Audio Unit call. Stop the Audio Unit before
2230 * setting the STOPPED state. In widStart, the order is reversed. This
2231 * guarantees that we can't get into a situation where the state is
2232 * PLAYING but the Audio Unit isn't running. Although we can be in STOPPED
2233 * state with the Audio Unit still running, that's harmless because the
2234 * input callback will just throw away the sound data.
2236 err = AudioOutputUnitStop(wwi->audioUnit);
2237 if (err != noErr)
2238 WARN("Failed to stop AU: %08lx\n", err);
2240 TRACE("Recording stopped.\n");
2242 OSSpinLockLock(&wwi->lock);
2244 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2246 WARN("Trying to stop closed device.\n");
2247 ret = MMSYSERR_INVALHANDLE;
2249 else if (wwi->state != WINE_WS_STOPPED)
2251 wwi->state = WINE_WS_STOPPED;
2252 /* If there's a buffer in progress, it's done. Remove it from the
2253 * queue so that we can return it to the app, below. */
2254 if (wwi->lpQueuePtr)
2256 lpWaveHdr = wwi->lpQueuePtr;
2257 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2261 OSSpinLockUnlock(&wwi->lock);
2263 if (lpWaveHdr)
2265 lpWaveHdr->lpNext = NULL;
2266 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2267 lpWaveHdr->dwFlags |= WHDR_DONE;
2268 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2271 return ret;
2274 /**************************************************************************
2275 * widGetPos [internal]
2277 static DWORD widGetPos(WORD wDevID, LPMMTIME lpTime, UINT size)
2279 DWORD val;
2280 WINE_WAVEIN* wwi;
2282 TRACE("(%u);\n", wDevID);
2283 if (wDevID >= MAX_WAVEINDRV)
2285 WARN("invalid device ID\n");
2286 return MMSYSERR_INVALHANDLE;
2289 wwi = &WInDev[wDevID];
2291 OSSpinLockLock(&WInDev[wDevID].lock);
2292 val = wwi->dwTotalRecorded;
2293 OSSpinLockUnlock(&WInDev[wDevID].lock);
2295 return bytes_to_mmtime(lpTime, val, &wwi->format);
2298 /**************************************************************************
2299 * widReset [internal]
2301 static DWORD widReset(WORD wDevID)
2303 DWORD ret = MMSYSERR_NOERROR;
2304 WINE_WAVEIN* wwi;
2305 WAVEHDR* lpWaveHdr = NULL;
2307 TRACE("(%u);\n", wDevID);
2308 if (wDevID >= MAX_WAVEINDRV)
2310 WARN("invalid device ID\n");
2311 return MMSYSERR_INVALHANDLE;
2314 wwi = &WInDev[wDevID];
2315 OSSpinLockLock(&wwi->lock);
2317 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2319 WARN("Trying to reset a closed device.\n");
2320 ret = MMSYSERR_INVALHANDLE;
2322 else
2324 lpWaveHdr = wwi->lpQueuePtr;
2325 wwi->lpQueuePtr = NULL;
2326 wwi->state = WINE_WS_STOPPED;
2327 wwi->dwTotalRecorded = 0;
2330 OSSpinLockUnlock(&wwi->lock);
2332 if (ret == MMSYSERR_NOERROR)
2334 OSStatus err = AudioOutputUnitStop(wwi->audioUnit);
2335 if (err != noErr)
2336 WARN("Failed to stop AU: %08lx\n", err);
2338 TRACE("Recording stopped.\n");
2341 while (lpWaveHdr)
2343 WAVEHDR* lpNext = lpWaveHdr->lpNext;
2345 lpWaveHdr->lpNext = NULL;
2346 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2347 lpWaveHdr->dwFlags |= WHDR_DONE;
2348 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2350 lpWaveHdr = lpNext;
2353 return ret;
2357 /**************************************************************************
2358 * widGetNumDevs [internal]
2360 static DWORD widGetNumDevs(void)
2362 return MAX_WAVEINDRV;
2366 /**************************************************************************
2367 * widDevInterfaceSize [internal]
2369 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
2371 TRACE("(%u, %p)\n", wDevID, dwParam1);
2373 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].interface_name, -1,
2374 NULL, 0 ) * sizeof(WCHAR);
2375 return MMSYSERR_NOERROR;
2379 /**************************************************************************
2380 * widDevInterface [internal]
2382 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
2384 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].interface_name, -1,
2385 NULL, 0 ) * sizeof(WCHAR))
2387 MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].interface_name, -1,
2388 dwParam1, dwParam2 / sizeof(WCHAR));
2389 return MMSYSERR_NOERROR;
2391 return MMSYSERR_INVALPARAM;
2395 /**************************************************************************
2396 * widDsCreate [internal]
2398 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
2400 TRACE("(%d,%p)\n",wDevID,drv);
2402 FIXME("DirectSoundCapture not implemented\n");
2403 FIXME("The (slower) DirectSound HEL mode will be used instead.\n");
2404 return MMSYSERR_NOTSUPPORTED;
2407 /**************************************************************************
2408 * widDsDesc [internal]
2410 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2412 /* The DirectSound HEL will automatically wrap a non-DirectSound-capable
2413 * driver in a DirectSound adaptor, thus allowing the driver to be used by
2414 * DirectSound clients. However, it only does this if we respond
2415 * successfully to the DRV_QUERYDSOUNDDESC message. It's enough to fill in
2416 * the driver and device names of the description output parameter. */
2417 memset(desc, 0, sizeof(*desc));
2418 lstrcpynA(desc->szDrvname, "winecoreaudio.drv", sizeof(desc->szDrvname) - 1);
2419 lstrcpynA(desc->szDesc, WInDev[wDevID].interface_name, sizeof(desc->szDesc) - 1);
2420 return MMSYSERR_NOERROR;
2424 /**************************************************************************
2425 * widMessage (WINECOREAUDIO.6)
2427 DWORD WINAPI CoreAudio_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2428 DWORD dwParam1, DWORD dwParam2)
2430 TRACE("(%u, %04X, %08X, %08X, %08X);\n",
2431 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2433 switch (wMsg)
2435 case DRVM_INIT:
2436 case DRVM_EXIT:
2437 case DRVM_ENABLE:
2438 case DRVM_DISABLE:
2439 /* FIXME: Pretend this is supported */
2440 return 0;
2441 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2442 case WIDM_CLOSE: return widClose (wDevID);
2443 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2444 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2445 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2446 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
2447 case WIDM_GETNUMDEVS: return widGetNumDevs ();
2448 case WIDM_RESET: return widReset (wDevID);
2449 case WIDM_START: return widStart (wDevID);
2450 case WIDM_STOP: return widStop (wDevID);
2451 case WIDM_GETPOS: return widGetPos (wDevID, (LPMMTIME)dwParam1, (UINT)dwParam2 );
2452 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2453 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2454 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
2455 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2456 default:
2457 FIXME("unknown message %d!\n", wMsg);
2460 return MMSYSERR_NOTSUPPORTED;
2464 OSStatus CoreAudio_wiAudioUnitIOProc(void *inRefCon,
2465 AudioUnitRenderActionFlags *ioActionFlags,
2466 const AudioTimeStamp *inTimeStamp,
2467 UInt32 inBusNumber,
2468 UInt32 inNumberFrames,
2469 AudioBufferList *ioData)
2471 WINE_WAVEIN* wwi = (WINE_WAVEIN*)inRefCon;
2472 OSStatus err = noErr;
2473 BOOL needNotify = FALSE;
2474 WAVEHDR* lpStorePtr;
2475 unsigned int dataToStore;
2476 unsigned int dataStored = 0;
2479 if (wwi->trace_on)
2480 fprintf(stderr, "trace:wave:CoreAudio_wiAudioUnitIOProc (ioActionFlags = %08lx, "
2481 "inTimeStamp = { %f, %x%08x, %f, %x%08x, %08lx }, inBusNumber = %lu, inNumberFrames = %lu)\n",
2482 *ioActionFlags, inTimeStamp->mSampleTime, (DWORD)(inTimeStamp->mHostTime >>32),
2483 (DWORD)inTimeStamp->mHostTime, inTimeStamp->mRateScalar, (DWORD)(inTimeStamp->mWordClockTime >> 32),
2484 (DWORD)inTimeStamp->mWordClockTime, inTimeStamp->mFlags, inBusNumber, inNumberFrames);
2486 /* Render into audio buffer */
2487 /* FIXME: implement sample rate conversion on input. This will require
2488 * a different render strategy. We'll need to buffer the sound data
2489 * received here and pass it off to an AUConverter in another thread. */
2490 err = AudioUnitRender(wwi->audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, wwi->bufferList);
2491 if (err)
2493 if (wwi->err_on)
2494 fprintf(stderr, "err:wave:CoreAudio_wiAudioUnitIOProc AudioUnitRender failed with error %li\n", err);
2495 return err;
2498 /* Copy from audio buffer to the wavehdrs */
2499 dataToStore = wwi->bufferList->mBuffers[0].mDataByteSize;
2501 OSSpinLockLock(&wwi->lock);
2503 lpStorePtr = wwi->lpQueuePtr;
2505 /* We might have been called while the waveIn device is being closed in
2506 * widClose. We have to do nothing in that case. The check of wwi->state
2507 * below ensures that. */
2508 while (dataToStore > 0 && wwi->state == WINE_WS_PLAYING && lpStorePtr)
2510 unsigned int room = lpStorePtr->dwBufferLength - lpStorePtr->dwBytesRecorded;
2511 unsigned int toCopy;
2513 if (wwi->trace_on)
2514 fprintf(stderr, "trace:wave:CoreAudio_wiAudioUnitIOProc Looking to store %u bytes to wavehdr %p, which has room for %u\n",
2515 dataToStore, lpStorePtr, room);
2517 if (room >= dataToStore)
2518 toCopy = dataToStore;
2519 else
2520 toCopy = room;
2522 if (toCopy > 0)
2524 memcpy(lpStorePtr->lpData + lpStorePtr->dwBytesRecorded,
2525 (char*)wwi->bufferList->mBuffers[0].mData + dataStored, toCopy);
2526 lpStorePtr->dwBytesRecorded += toCopy;
2527 wwi->dwTotalRecorded += toCopy;
2528 dataStored += toCopy;
2529 dataToStore -= toCopy;
2530 room -= toCopy;
2533 if (room == 0)
2535 lpStorePtr = lpStorePtr->lpNext;
2536 needNotify = TRUE;
2540 OSSpinLockUnlock(&wwi->lock);
2542 /* Restore the audio buffer list structure from backup, in case
2543 * AudioUnitRender clobbered it. (It modifies mDataByteSize and may even
2544 * give us a different mData buffer to avoid a copy.) */
2545 memcpy(wwi->bufferList, wwi->bufferListCopy, AUDIOBUFFERLISTSIZE(wwi->bufferList->mNumberBuffers));
2547 if (needNotify) wodSendNotifyInputCompletionsMessage(wwi);
2548 return err;
2551 #else
2553 /**************************************************************************
2554 * widMessage (WINECOREAUDIO.6)
2556 DWORD WINAPI CoreAudio_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2557 DWORD dwParam1, DWORD dwParam2)
2559 FIXME("(%u, %04X, %08X, %08X, %08X): CoreAudio support not compiled into wine\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2560 return MMSYSERR_NOTENABLED;
2563 /**************************************************************************
2564 * wodMessage (WINECOREAUDIO.7)
2566 DWORD WINAPI CoreAudio_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2567 DWORD dwParam1, DWORD dwParam2)
2569 FIXME("(%u, %04X, %08X, %08X, %08X): CoreAudio support not compiled into wine\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2570 return MMSYSERR_NOTENABLED;
2573 #endif