wineoss.drv: Remove wave, mixer, and dsound driver code.
[wine/wine-gecko.git] / dlls / winecoreaudio.drv / audio.c
blobf7e2dca6431f527115174cd6e668a9f5e30c5576
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 "coreaudio.h"
54 #include "wine/unicode.h"
55 #include "wine/library.h"
56 #include "wine/debug.h"
57 #include "wine/list.h"
59 #include "initguid.h"
60 #include "ksmedia.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(wave);
63 WINE_DECLARE_DEBUG_CHANNEL(coreaudio);
66 Due to AudioUnit headers conflict define some needed types.
69 typedef void *AudioUnit;
71 /* From AudioUnit/AUComponents.h */
72 enum
74 kAudioUnitRenderAction_OutputIsSilence = (1 << 4),
75 /* provides hint on return from Render(): if set the buffer contains all zeroes */
77 typedef UInt32 AudioUnitRenderActionFlags;
79 typedef long ComponentResult;
80 extern ComponentResult
81 AudioUnitRender( AudioUnit ci,
82 AudioUnitRenderActionFlags * ioActionFlags,
83 const AudioTimeStamp * inTimeStamp,
84 UInt32 inOutputBusNumber,
85 UInt32 inNumberFrames,
86 AudioBufferList * ioData) AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER;
88 /* only allow 10 output devices through this driver, this ought to be adequate */
89 #define MAX_WAVEOUTDRV (1)
90 #define MAX_WAVEINDRV (1)
92 /* state diagram for waveOut writing:
94 * +---------+-------------+---------------+---------------------------------+
95 * | state | function | event | new state |
96 * +---------+-------------+---------------+---------------------------------+
97 * | | open() | | PLAYING |
98 * | PAUSED | write() | | PAUSED |
99 * | PLAYING | write() | HEADER | PLAYING |
100 * | (other) | write() | <error> | |
101 * | (any) | pause() | PAUSING | PAUSED |
102 * | PAUSED | restart() | RESTARTING | PLAYING |
103 * | (any) | reset() | RESETTING | PLAYING |
104 * | (any) | close() | CLOSING | <deallocated> |
105 * +---------+-------------+---------------+---------------------------------+
108 /* states of the playing device */
109 #define WINE_WS_PLAYING 0 /* for waveOut: lpPlayPtr == NULL -> stopped */
110 #define WINE_WS_PAUSED 1
111 #define WINE_WS_STOPPED 2 /* Not used for waveOut */
112 #define WINE_WS_CLOSED 3 /* Not used for waveOut */
113 #define WINE_WS_OPENING 4
114 #define WINE_WS_CLOSING 5
116 typedef struct tagCoreAudio_Device {
117 char dev_name[32];
118 char mixer_name[32];
119 unsigned open_count;
120 char* interface_name;
122 WAVEOUTCAPSW out_caps;
123 WAVEINCAPSW in_caps;
124 DWORD in_caps_support;
125 int sample_rate;
126 int stereo;
127 int format;
128 unsigned audio_fragment;
129 BOOL full_duplex;
130 BOOL bTriggerSupport;
131 BOOL bOutputEnabled;
132 BOOL bInputEnabled;
133 DSDRIVERDESC ds_desc;
134 DSDRIVERCAPS ds_caps;
135 DSCDRIVERCAPS dsc_caps;
136 GUID ds_guid;
137 GUID dsc_guid;
139 AudioDeviceID outputDeviceID;
140 AudioDeviceID inputDeviceID;
141 AudioStreamBasicDescription streamDescription;
142 } CoreAudio_Device;
144 /* for now use the default device */
145 static CoreAudio_Device CoreAudio_DefaultDevice;
147 typedef struct {
148 struct list entry;
150 volatile int state; /* one of the WINE_WS_ manifest constants */
151 WAVEOPENDESC waveDesc;
152 WORD wFlags;
153 PCMWAVEFORMAT format;
154 DWORD woID;
155 AudioUnit audioUnit;
156 AudioStreamBasicDescription streamDescription;
158 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
159 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
160 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
162 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
163 DWORD dwLoops; /* private copy of loop counter */
165 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
167 OSSpinLock lock; /* synchronization stuff */
168 } WINE_WAVEOUT_INSTANCE;
170 typedef struct {
171 CoreAudio_Device *cadev;
172 WAVEOUTCAPSW caps;
173 char interface_name[32];
174 DWORD device_volume;
176 BOOL trace_on;
177 BOOL warn_on;
178 BOOL err_on;
180 struct list instances;
181 OSSpinLock lock; /* guards the instances list */
182 } WINE_WAVEOUT;
184 typedef struct {
185 /* This device's device number */
186 DWORD wiID;
188 /* Access to the following fields is synchronized across threads. */
189 volatile int state;
190 LPWAVEHDR lpQueuePtr;
191 DWORD dwTotalRecorded;
193 /* Synchronization mechanism to protect above fields */
194 OSSpinLock lock;
196 /* Capabilities description */
197 WAVEINCAPSW caps;
198 char interface_name[32];
200 /* Record the arguments used when opening the device. */
201 WAVEOPENDESC waveDesc;
202 WORD wFlags;
203 PCMWAVEFORMAT format;
205 AudioUnit audioUnit;
206 AudioBufferList*bufferList;
207 AudioBufferList*bufferListCopy;
209 /* Record state of debug channels at open. Used to control fprintf's since
210 * we can't use Wine debug channel calls in non-Wine AudioUnit threads. */
211 BOOL trace_on;
212 BOOL warn_on;
213 BOOL err_on;
215 /* These fields aren't used. */
216 #if 0
217 CoreAudio_Device *cadev;
219 AudioStreamBasicDescription streamDescription;
220 #endif
221 } WINE_WAVEIN;
223 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
224 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
226 static HANDLE hThread = NULL; /* Track the thread we create so we can clean it up later */
227 static CFMessagePortRef Port_SendToMessageThread;
229 static void wodHelper_PlayPtrNext(WINE_WAVEOUT_INSTANCE* wwo);
230 static void wodHelper_NotifyDoneForList(WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr);
231 static void wodHelper_NotifyCompletions(WINE_WAVEOUT_INSTANCE* wwo, BOOL force);
232 static void widHelper_NotifyCompletions(WINE_WAVEIN* wwi);
234 extern int AudioUnit_CreateDefaultAudioUnit(void *wwo, AudioUnit *au);
235 extern int AudioUnit_CloseAudioUnit(AudioUnit au);
236 extern int AudioUnit_InitializeWithStreamDescription(AudioUnit au, AudioStreamBasicDescription *streamFormat);
238 extern OSStatus AudioOutputUnitStart(AudioUnit au);
239 extern OSStatus AudioOutputUnitStop(AudioUnit au);
240 extern OSStatus AudioUnitUninitialize(AudioUnit au);
242 extern int AudioUnit_SetVolume(AudioUnit au, float left, float right);
243 extern int AudioUnit_GetVolume(AudioUnit au, float *left, float *right);
245 extern int AudioUnit_GetInputDeviceSampleRate(void);
247 extern int AudioUnit_CreateInputUnit(void* wwi, AudioUnit* out_au,
248 WORD nChannels, DWORD nSamplesPerSec, WORD wBitsPerSample,
249 UInt32* outFrameCount);
251 OSStatus CoreAudio_woAudioUnitIOProc(void *inRefCon,
252 AudioUnitRenderActionFlags *ioActionFlags,
253 const AudioTimeStamp *inTimeStamp,
254 UInt32 inBusNumber,
255 UInt32 inNumberFrames,
256 AudioBufferList *ioData);
257 OSStatus CoreAudio_wiAudioUnitIOProc(void *inRefCon,
258 AudioUnitRenderActionFlags *ioActionFlags,
259 const AudioTimeStamp *inTimeStamp,
260 UInt32 inBusNumber,
261 UInt32 inNumberFrames,
262 AudioBufferList *ioData);
264 /* These strings used only for tracing */
266 static const char * getMessage(UINT msg)
268 #define MSG_TO_STR(x) case x: return #x
269 switch(msg) {
270 MSG_TO_STR(DRVM_INIT);
271 MSG_TO_STR(DRVM_EXIT);
272 MSG_TO_STR(DRVM_ENABLE);
273 MSG_TO_STR(DRVM_DISABLE);
274 MSG_TO_STR(WIDM_OPEN);
275 MSG_TO_STR(WIDM_CLOSE);
276 MSG_TO_STR(WIDM_ADDBUFFER);
277 MSG_TO_STR(WIDM_PREPARE);
278 MSG_TO_STR(WIDM_UNPREPARE);
279 MSG_TO_STR(WIDM_GETDEVCAPS);
280 MSG_TO_STR(WIDM_GETNUMDEVS);
281 MSG_TO_STR(WIDM_GETPOS);
282 MSG_TO_STR(WIDM_RESET);
283 MSG_TO_STR(WIDM_START);
284 MSG_TO_STR(WIDM_STOP);
285 MSG_TO_STR(WODM_OPEN);
286 MSG_TO_STR(WODM_CLOSE);
287 MSG_TO_STR(WODM_WRITE);
288 MSG_TO_STR(WODM_PAUSE);
289 MSG_TO_STR(WODM_GETPOS);
290 MSG_TO_STR(WODM_BREAKLOOP);
291 MSG_TO_STR(WODM_PREPARE);
292 MSG_TO_STR(WODM_UNPREPARE);
293 MSG_TO_STR(WODM_GETDEVCAPS);
294 MSG_TO_STR(WODM_GETNUMDEVS);
295 MSG_TO_STR(WODM_GETPITCH);
296 MSG_TO_STR(WODM_SETPITCH);
297 MSG_TO_STR(WODM_GETPLAYBACKRATE);
298 MSG_TO_STR(WODM_SETPLAYBACKRATE);
299 MSG_TO_STR(WODM_GETVOLUME);
300 MSG_TO_STR(WODM_SETVOLUME);
301 MSG_TO_STR(WODM_RESTART);
302 MSG_TO_STR(WODM_RESET);
303 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
304 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
305 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
306 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
308 #undef MSG_TO_STR
309 return wine_dbg_sprintf("UNKNOWN(0x%04x)", msg);
312 #define kStopLoopMessage 0
313 #define kWaveOutNotifyCompletionsMessage 1
314 #define kWaveInNotifyCompletionsMessage 2
316 /* Mach Message Handling */
317 static CFDataRef wodMessageHandler(CFMessagePortRef port_ReceiveInMessageThread, SInt32 msgid, CFDataRef data, void *info)
319 UInt32 *buffer = NULL;
321 switch (msgid)
323 case kWaveOutNotifyCompletionsMessage:
324 wodHelper_NotifyCompletions(*(WINE_WAVEOUT_INSTANCE**)CFDataGetBytePtr(data), FALSE);
325 break;
326 case kWaveInNotifyCompletionsMessage:
327 buffer = (UInt32 *) CFDataGetBytePtr(data);
328 widHelper_NotifyCompletions(&WInDev[buffer[0]]);
329 break;
330 default:
331 CFRunLoopStop(CFRunLoopGetCurrent());
332 break;
335 return NULL;
338 static DWORD WINAPI messageThread(LPVOID p)
340 CFMessagePortRef port_ReceiveInMessageThread = (CFMessagePortRef) p;
341 CFRunLoopSourceRef source;
343 source = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, port_ReceiveInMessageThread, 0);
344 CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
346 CFRunLoopRun();
348 CFRunLoopSourceInvalidate(source);
349 CFRelease(source);
350 CFRelease(port_ReceiveInMessageThread);
352 return 0;
355 /**************************************************************************
356 * wodSendNotifyCompletionsMessage [internal]
357 * Call from AudioUnit IO thread can't use Wine debug channels.
359 static void wodSendNotifyCompletionsMessage(WINE_WAVEOUT_INSTANCE* wwo)
361 CFDataRef data;
363 if (!Port_SendToMessageThread)
364 return;
366 data = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&wwo, sizeof(wwo));
367 if (!data)
368 return;
370 CFMessagePortSendRequest(Port_SendToMessageThread, kWaveOutNotifyCompletionsMessage, data, 0.0, 0.0, NULL, NULL);
371 CFRelease(data);
374 /**************************************************************************
375 * wodSendNotifyInputCompletionsMessage [internal]
376 * Call from AudioUnit IO thread can't use Wine debug channels.
378 static void wodSendNotifyInputCompletionsMessage(WINE_WAVEIN* wwi)
380 CFDataRef data;
381 UInt32 buffer;
383 if (!Port_SendToMessageThread)
384 return;
386 buffer = (UInt32) wwi->wiID;
388 data = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&buffer, sizeof(buffer));
389 if (!data)
390 return;
392 CFMessagePortSendRequest(Port_SendToMessageThread, kWaveInNotifyCompletionsMessage, data, 0.0, 0.0, NULL, NULL);
393 CFRelease(data);
396 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
397 PCMWAVEFORMAT* format)
399 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
400 lpTime->wType, format->wBitsPerSample, format->wf.nSamplesPerSec,
401 format->wf.nChannels, format->wf.nAvgBytesPerSec);
402 TRACE("Position in bytes=%u\n", position);
404 switch (lpTime->wType) {
405 case TIME_SAMPLES:
406 lpTime->u.sample = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
407 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
408 break;
409 case TIME_MS:
410 lpTime->u.ms = 1000.0 * position / (format->wBitsPerSample / 8 * format->wf.nChannels * format->wf.nSamplesPerSec);
411 TRACE("TIME_MS=%u\n", lpTime->u.ms);
412 break;
413 case TIME_SMPTE:
414 lpTime->u.smpte.fps = 30;
415 position = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
416 position += (format->wf.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
417 lpTime->u.smpte.sec = position / format->wf.nSamplesPerSec;
418 position -= lpTime->u.smpte.sec * format->wf.nSamplesPerSec;
419 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
420 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
421 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
422 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
423 lpTime->u.smpte.fps = 30;
424 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->wf.nSamplesPerSec;
425 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
426 lpTime->u.smpte.hour, lpTime->u.smpte.min,
427 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
428 break;
429 default:
430 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
431 lpTime->wType = TIME_BYTES;
432 /* fall through */
433 case TIME_BYTES:
434 lpTime->u.cb = position;
435 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
436 break;
438 return MMSYSERR_NOERROR;
441 static BOOL supportedFormat(LPWAVEFORMATEX wf)
443 if (wf->nSamplesPerSec < DSBFREQUENCY_MIN || wf->nSamplesPerSec > DSBFREQUENCY_MAX)
444 return FALSE;
446 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
447 if (wf->nChannels >= 1 && wf->nChannels <= 2) {
448 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
449 return TRUE;
451 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
452 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
454 if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
455 if (wf->nChannels >=1 && wf->nChannels <= 8) {
456 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
457 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
458 return TRUE;
459 } else
460 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
462 } else
463 WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
464 } else
465 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
467 return FALSE;
470 void copyFormat(LPWAVEFORMATEX wf1, LPPCMWAVEFORMAT wf2)
472 memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
473 /* Downgrade WAVE_FORMAT_EXTENSIBLE KSDATAFORMAT_SUBTYPE_PCM
474 * to smaller yet compatible WAVE_FORMAT_PCM structure */
475 if (wf2->wf.wFormatTag == WAVE_FORMAT_EXTENSIBLE)
476 wf2->wf.wFormatTag = WAVE_FORMAT_PCM;
479 /**************************************************************************
480 * CoreAudio_GetDevCaps [internal]
482 BOOL CoreAudio_GetDevCaps (void)
484 OSStatus status;
485 UInt32 propertySize;
486 AudioDeviceID devId = CoreAudio_DefaultDevice.outputDeviceID;
487 AudioObjectPropertyAddress propertyAddress;
489 CFStringRef name;
490 CFRange range;
492 propertySize = sizeof(name);
493 propertyAddress.mSelector = kAudioObjectPropertyName;
494 propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
495 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
496 status = AudioObjectGetPropertyData(devId, &propertyAddress, 0, NULL, &propertySize, &name);
497 if (status) {
498 ERR("AudioObjectGetPropertyData for kAudioObjectPropertyName return %s\n", wine_dbgstr_fourcc(status));
499 return FALSE;
502 CFStringGetCString(name, CoreAudio_DefaultDevice.ds_desc.szDesc,
503 sizeof(CoreAudio_DefaultDevice.ds_desc.szDesc),
504 kCFStringEncodingUTF8);
505 strcpy(CoreAudio_DefaultDevice.ds_desc.szDrvname, "winecoreaudio.drv");
506 range = CFRangeMake(0, min(sizeof(CoreAudio_DefaultDevice.out_caps.szPname) / sizeof(WCHAR) - 1, CFStringGetLength(name)));
507 CFStringGetCharacters(name, range, CoreAudio_DefaultDevice.out_caps.szPname);
508 CoreAudio_DefaultDevice.out_caps.szPname[range.length] = 0;
509 CFStringGetCString(name, CoreAudio_DefaultDevice.dev_name, 32, kCFStringEncodingUTF8);
510 CFRelease(name);
512 propertySize = sizeof(CoreAudio_DefaultDevice.streamDescription);
513 /* FIXME: kAudioDevicePropertyStreamFormat is deprecated. We're
514 * "supposed" to get an AudioStream object from the AudioDevice,
515 * then query it for the format with kAudioStreamPropertyVirtualFormat.
516 * Apple says that this is for our own good, because this property
517 * "has been shown to lead to programming mistakes by clients when
518 * working with devices with multiple streams." Only one problem:
519 * which stream? For now, just query the device.
521 propertyAddress.mSelector = kAudioDevicePropertyStreamFormat;
522 status = AudioObjectGetPropertyData(devId, &propertyAddress, 0, NULL, &propertySize, &CoreAudio_DefaultDevice.streamDescription);
523 if (status != noErr) {
524 ERR("AudioObjectGetPropertyData for kAudioDevicePropertyStreamFormat return %s\n", wine_dbgstr_fourcc(status));
525 return FALSE;
528 TRACE("Device Stream Description mSampleRate : %f\n mFormatID : %s\n"
529 "mFormatFlags : %lX\n mBytesPerPacket : %lu\n mFramesPerPacket : %lu\n"
530 "mBytesPerFrame : %lu\n mChannelsPerFrame : %lu\n mBitsPerChannel : %lu\n",
531 CoreAudio_DefaultDevice.streamDescription.mSampleRate,
532 wine_dbgstr_fourcc(CoreAudio_DefaultDevice.streamDescription.mFormatID),
533 CoreAudio_DefaultDevice.streamDescription.mFormatFlags,
534 CoreAudio_DefaultDevice.streamDescription.mBytesPerPacket,
535 CoreAudio_DefaultDevice.streamDescription.mFramesPerPacket,
536 CoreAudio_DefaultDevice.streamDescription.mBytesPerFrame,
537 CoreAudio_DefaultDevice.streamDescription.mChannelsPerFrame,
538 CoreAudio_DefaultDevice.streamDescription.mBitsPerChannel);
540 CoreAudio_DefaultDevice.out_caps.wMid = 0xcafe;
541 CoreAudio_DefaultDevice.out_caps.wPid = 0x0001;
543 CoreAudio_DefaultDevice.out_caps.vDriverVersion = 0x0001;
544 CoreAudio_DefaultDevice.out_caps.dwFormats = 0x00000000;
545 CoreAudio_DefaultDevice.out_caps.wReserved1 = 0;
546 CoreAudio_DefaultDevice.out_caps.dwSupport = WAVECAPS_VOLUME;
547 CoreAudio_DefaultDevice.out_caps.dwSupport |= WAVECAPS_LRVOLUME;
549 CoreAudio_DefaultDevice.out_caps.wChannels = 2;
550 CoreAudio_DefaultDevice.out_caps.dwFormats|= WAVE_FORMAT_4S16;
552 TRACE_(coreaudio)("out dwFormats = %08x, dwSupport = %08x\n",
553 CoreAudio_DefaultDevice.out_caps.dwFormats, CoreAudio_DefaultDevice.out_caps.dwSupport);
555 return TRUE;
558 /******************************************************************
559 * CoreAudio_WaveInit
561 * Initialize CoreAudio_DefaultDevice
563 LONG CoreAudio_WaveInit(void)
565 OSStatus status;
566 UInt32 propertySize;
567 AudioObjectPropertyAddress propertyAddress;
568 int i;
569 CFStringRef messageThreadPortName;
570 CFMessagePortRef port_ReceiveInMessageThread;
571 int inputSampleRate;
573 TRACE("()\n");
575 /* number of sound cards */
576 propertyAddress.mSelector = kAudioHardwarePropertyDevices;
577 propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
578 propertyAddress.mElement = kAudioObjectPropertyElementMaster;
579 AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize);
580 propertySize /= sizeof(AudioDeviceID);
581 TRACE("sound cards : %lu\n", propertySize);
583 /* Get the output device */
584 propertySize = sizeof(CoreAudio_DefaultDevice.outputDeviceID);
585 propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
586 status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, &CoreAudio_DefaultDevice.outputDeviceID);
587 if (status) {
588 ERR("AudioObjectGetPropertyData return %s for kAudioHardwarePropertyDefaultOutputDevice\n", wine_dbgstr_fourcc(status));
589 return DRV_FAILURE;
591 if (CoreAudio_DefaultDevice.outputDeviceID == kAudioDeviceUnknown) {
592 ERR("AudioObjectGetPropertyData: CoreAudio_DefaultDevice.outputDeviceID == kAudioDeviceUnknown\n");
593 return DRV_FAILURE;
596 if ( ! CoreAudio_GetDevCaps() )
597 return DRV_FAILURE;
599 CoreAudio_DefaultDevice.interface_name=HeapAlloc(GetProcessHeap(),0,strlen(CoreAudio_DefaultDevice.dev_name)+1);
600 strcpy(CoreAudio_DefaultDevice.interface_name, CoreAudio_DefaultDevice.dev_name);
602 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
604 static const WCHAR wszWaveOutFormat[] =
605 {'C','o','r','e','A','u','d','i','o',' ','W','a','v','e','O','u','t',' ','%','d',0};
607 list_init(&WOutDev[i].instances);
608 WOutDev[i].cadev = &CoreAudio_DefaultDevice;
610 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps));
612 WOutDev[i].caps.wMid = 0xcafe; /* Manufac ID */
613 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
614 snprintfW(WOutDev[i].caps.szPname, sizeof(WOutDev[i].caps.szPname)/sizeof(WCHAR), wszWaveOutFormat, i);
615 snprintf(WOutDev[i].interface_name, sizeof(WOutDev[i].interface_name), "winecoreaudio: %d", i);
617 WOutDev[i].caps.vDriverVersion = 0x0001;
618 WOutDev[i].caps.dwFormats = 0x00000000;
619 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
621 WOutDev[i].caps.wChannels = 2;
622 /* WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME; */ /* FIXME */
624 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_96M08;
625 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_96S08;
626 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_96M16;
627 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_96S16;
628 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_48M08;
629 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_48S08;
630 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_48M16;
631 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_48S16;
632 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
633 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
634 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
635 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
636 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
637 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
638 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
639 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
640 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
641 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
642 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
643 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
645 WOutDev[i].device_volume = 0xffffffff;
647 WOutDev[i].lock = 0; /* initialize the mutex */
650 /* FIXME: implement sample rate conversion on input */
651 inputSampleRate = AudioUnit_GetInputDeviceSampleRate();
653 for (i = 0; i < MAX_WAVEINDRV; ++i)
655 static const WCHAR wszWaveInFormat[] =
656 {'C','o','r','e','A','u','d','i','o',' ','W','a','v','e','I','n',' ','%','d',0};
658 memset(&WInDev[i], 0, sizeof(WInDev[i]));
659 WInDev[i].wiID = i;
661 /* Establish preconditions for widOpen */
662 WInDev[i].state = WINE_WS_CLOSED;
663 WInDev[i].lock = 0; /* initialize the mutex */
665 /* Fill in capabilities. widGetDevCaps can be called at any time. */
666 WInDev[i].caps.wMid = 0xcafe; /* Manufac ID */
667 WInDev[i].caps.wPid = 0x0001; /* Product ID */
668 WInDev[i].caps.vDriverVersion = 0x0001;
670 snprintfW(WInDev[i].caps.szPname, sizeof(WInDev[i].caps.szPname)/sizeof(WCHAR), wszWaveInFormat, i);
671 snprintf(WInDev[i].interface_name, sizeof(WInDev[i].interface_name), "winecoreaudio in: %d", i);
673 if (inputSampleRate == 96000)
675 WInDev[i].caps.dwFormats |= WAVE_FORMAT_96M08;
676 WInDev[i].caps.dwFormats |= WAVE_FORMAT_96S08;
677 WInDev[i].caps.dwFormats |= WAVE_FORMAT_96M16;
678 WInDev[i].caps.dwFormats |= WAVE_FORMAT_96S16;
680 if (inputSampleRate == 48000)
682 WInDev[i].caps.dwFormats |= WAVE_FORMAT_48M08;
683 WInDev[i].caps.dwFormats |= WAVE_FORMAT_48S08;
684 WInDev[i].caps.dwFormats |= WAVE_FORMAT_48M16;
685 WInDev[i].caps.dwFormats |= WAVE_FORMAT_48S16;
687 if (inputSampleRate == 44100)
689 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
690 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
691 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
692 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
694 if (inputSampleRate == 22050)
696 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
697 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
698 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
699 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
701 if (inputSampleRate == 11025)
703 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
704 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
705 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
706 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
709 WInDev[i].caps.wChannels = 2;
712 /* create mach messages handler */
713 srandomdev();
714 messageThreadPortName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
715 CFSTR("WaveMessagePort.%d.%lu"), getpid(), (unsigned long)random());
716 if (!messageThreadPortName)
718 ERR("Can't create message thread port name\n");
719 return DRV_FAILURE;
722 port_ReceiveInMessageThread = CFMessagePortCreateLocal(kCFAllocatorDefault, messageThreadPortName,
723 &wodMessageHandler, NULL, NULL);
724 if (!port_ReceiveInMessageThread)
726 ERR("Can't create message thread local port\n");
727 CFRelease(messageThreadPortName);
728 return DRV_FAILURE;
731 Port_SendToMessageThread = CFMessagePortCreateRemote(kCFAllocatorDefault, messageThreadPortName);
732 CFRelease(messageThreadPortName);
733 if (!Port_SendToMessageThread)
735 ERR("Can't create port for sending to message thread\n");
736 CFRelease(port_ReceiveInMessageThread);
737 return DRV_FAILURE;
740 /* Cannot WAIT for any events because we are called from the loader (which has a lock on loading stuff) */
741 /* We might want to wait for this thread to be created -- but we cannot -- not here at least */
742 /* Instead track the thread so we can clean it up later */
743 if ( hThread )
745 ERR("Message thread already started -- expect problems\n");
747 hThread = CreateThread(NULL, 0, messageThread, (LPVOID)port_ReceiveInMessageThread, 0, NULL);
748 if ( !hThread )
750 ERR("Can't create message thread\n");
751 CFRelease(port_ReceiveInMessageThread);
752 CFRelease(Port_SendToMessageThread);
753 Port_SendToMessageThread = NULL;
754 return DRV_FAILURE;
757 /* The message thread is responsible for releasing port_ReceiveInMessageThread. */
759 return DRV_SUCCESS;
762 void CoreAudio_WaveRelease(void)
764 /* Stop CFRunLoop in messageThread */
765 TRACE("()\n");
767 if (!Port_SendToMessageThread)
768 return;
770 CFMessagePortSendRequest(Port_SendToMessageThread, kStopLoopMessage, NULL, 0.0, 0.0, NULL, NULL);
771 CFRelease(Port_SendToMessageThread);
772 Port_SendToMessageThread = NULL;
774 /* Wait for the thread to finish and clean it up */
775 /* This rids us of any quick start/shutdown driver crashes */
776 WaitForSingleObject(hThread, INFINITE);
777 CloseHandle(hThread);
778 hThread = NULL;
781 /*======================================================================*
782 * Low level WAVE OUT implementation *
783 *======================================================================*/
785 /**************************************************************************
786 * wodNotifyClient [internal]
788 static void wodNotifyClient(WINE_WAVEOUT_INSTANCE* wwo, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
790 TRACE("wMsg = 0x%04x dwParm1 = %04lx dwParam2 = %04lx\n", wMsg, dwParam1, dwParam2);
792 switch (wMsg) {
793 case WOM_OPEN:
794 case WOM_CLOSE:
795 case WOM_DONE:
796 DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags,
797 (HDRVR)wwo->waveDesc.hWave, wMsg, wwo->waveDesc.dwInstance,
798 dwParam1, dwParam2);
799 break;
800 default:
801 FIXME("Unknown callback message %u\n", wMsg);
806 /**************************************************************************
807 * wodGetDevCaps [internal]
809 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
811 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
813 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
815 if (wDevID >= MAX_WAVEOUTDRV)
817 TRACE("MAX_WAVOUTDRV reached !\n");
818 return MMSYSERR_BADDEVICEID;
821 TRACE("dwSupport=(0x%x), dwFormats=(0x%x)\n", WOutDev[wDevID].caps.dwSupport, WOutDev[wDevID].caps.dwFormats);
822 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
823 return MMSYSERR_NOERROR;
826 /**************************************************************************
827 * wodOpen [internal]
829 static DWORD wodOpen(WORD wDevID, WINE_WAVEOUT_INSTANCE** pInstance, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
831 WINE_WAVEOUT_INSTANCE* wwo;
832 DWORD ret;
833 AudioStreamBasicDescription streamFormat;
834 AudioUnit audioUnit = NULL;
835 BOOL auInited = FALSE;
837 TRACE("(%u, %p, %p, %08x);\n", wDevID, pInstance, lpDesc, dwFlags);
838 if (lpDesc == NULL)
840 WARN("Invalid Parameter !\n");
841 return MMSYSERR_INVALPARAM;
843 if (wDevID >= MAX_WAVEOUTDRV) {
844 TRACE("MAX_WAVOUTDRV reached !\n");
845 return MMSYSERR_BADDEVICEID;
848 TRACE("Format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
849 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
850 lpDesc->lpFormat->nSamplesPerSec, lpDesc->lpFormat->wBitsPerSample);
852 if (!supportedFormat(lpDesc->lpFormat))
854 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
855 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
856 lpDesc->lpFormat->nSamplesPerSec, lpDesc->lpFormat->wBitsPerSample);
857 return WAVERR_BADFORMAT;
860 if (dwFlags & WAVE_FORMAT_QUERY)
862 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
863 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
864 lpDesc->lpFormat->nSamplesPerSec);
865 return MMSYSERR_NOERROR;
868 /* nBlockAlign and nAvgBytesPerSec are output variables for dsound */
869 if (lpDesc->lpFormat->nBlockAlign != lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8) {
870 lpDesc->lpFormat->nBlockAlign = lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
871 WARN("Fixing nBlockAlign\n");
873 if (lpDesc->lpFormat->nAvgBytesPerSec!= lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign) {
874 lpDesc->lpFormat->nAvgBytesPerSec = lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
875 WARN("Fixing nAvgBytesPerSec\n");
878 /* We proceed in three phases:
879 * o Allocate the device instance, marking it as opening
880 * o Create, configure, and start the Audio Unit. To avoid deadlock,
881 * this has to be done without holding wwo->lock.
882 * o If that was successful, finish setting up our instance and add it
883 * to the device's list.
884 * Otherwise, clean up and deallocate the instance.
886 wwo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wwo));
887 if (!wwo)
888 return MMSYSERR_NOMEM;
890 wwo->woID = wDevID;
891 wwo->state = WINE_WS_OPENING;
893 if (!AudioUnit_CreateDefaultAudioUnit((void *) wwo, &audioUnit))
895 ERR("CoreAudio_CreateDefaultAudioUnit(0x%04x) failed\n", wDevID);
896 ret = MMSYSERR_ERROR;
897 goto error;
900 streamFormat.mFormatID = kAudioFormatLinearPCM;
901 streamFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked;
902 /* FIXME check for 32bits float -> kLinearPCMFormatFlagIsFloat */
903 if (lpDesc->lpFormat->wBitsPerSample != 8)
904 streamFormat.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
905 # ifdef WORDS_BIGENDIAN
906 streamFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; /* FIXME Wave format is little endian */
907 # endif
909 streamFormat.mSampleRate = lpDesc->lpFormat->nSamplesPerSec;
910 streamFormat.mChannelsPerFrame = lpDesc->lpFormat->nChannels;
911 streamFormat.mFramesPerPacket = 1;
912 streamFormat.mBitsPerChannel = lpDesc->lpFormat->wBitsPerSample;
913 streamFormat.mBytesPerFrame = streamFormat.mBitsPerChannel * streamFormat.mChannelsPerFrame / 8;
914 streamFormat.mBytesPerPacket = streamFormat.mBytesPerFrame * streamFormat.mFramesPerPacket;
916 ret = AudioUnit_InitializeWithStreamDescription(audioUnit, &streamFormat);
917 if (!ret)
919 ret = WAVERR_BADFORMAT; /* FIXME return an error based on the OSStatus */
920 goto error;
922 auInited = TRUE;
924 AudioUnit_SetVolume(audioUnit, LOWORD(WOutDev[wDevID].device_volume) / 65535.0f,
925 HIWORD(WOutDev[wDevID].device_volume) / 65535.0f);
927 /* Our render callback CoreAudio_woAudioUnitIOProc may be called before
928 * AudioOutputUnitStart returns. Core Audio will grab its own internal
929 * lock before calling it and the callback grabs wwo->lock. This would
930 * deadlock if we were holding wwo->lock.
931 * Also, the callback has to safely do nothing in that case, because
932 * wwo hasn't been completely filled out, yet. This is assured by state
933 * being WINE_WS_OPENING. */
934 ret = AudioOutputUnitStart(audioUnit);
935 if (ret)
937 ERR("AudioOutputUnitStart failed: %08x\n", ret);
938 ret = MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
939 goto error;
943 OSSpinLockLock(&wwo->lock);
944 assert(wwo->state == WINE_WS_OPENING);
946 wwo->audioUnit = audioUnit;
947 wwo->streamDescription = streamFormat;
949 wwo->state = WINE_WS_PLAYING;
951 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
953 wwo->waveDesc = *lpDesc;
954 copyFormat(lpDesc->lpFormat, &wwo->format);
956 WOutDev[wDevID].trace_on = TRACE_ON(wave);
957 WOutDev[wDevID].warn_on = WARN_ON(wave);
958 WOutDev[wDevID].err_on = ERR_ON(wave);
960 OSSpinLockUnlock(&wwo->lock);
962 OSSpinLockLock(&WOutDev[wDevID].lock);
963 list_add_head(&WOutDev[wDevID].instances, &wwo->entry);
964 OSSpinLockUnlock(&WOutDev[wDevID].lock);
966 *pInstance = wwo;
967 TRACE("opened instance %p\n", wwo);
969 wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
971 return MMSYSERR_NOERROR;
973 error:
974 if (audioUnit)
976 if (auInited)
977 AudioUnitUninitialize(audioUnit);
978 AudioUnit_CloseAudioUnit(audioUnit);
981 OSSpinLockLock(&wwo->lock);
982 assert(wwo->state == WINE_WS_OPENING);
983 /* OSSpinLockUnlock(&wwo->lock); *//* No need, about to free */
984 HeapFree(GetProcessHeap(), 0, wwo);
986 return ret;
989 /**************************************************************************
990 * wodClose [internal]
992 static DWORD wodClose(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
994 DWORD ret = MMSYSERR_NOERROR;
996 TRACE("(%u, %p);\n", wDevID, wwo);
998 if (wDevID >= MAX_WAVEOUTDRV)
1000 WARN("bad device ID !\n");
1001 return MMSYSERR_BADDEVICEID;
1004 OSSpinLockLock(&wwo->lock);
1005 if (wwo->lpQueuePtr)
1007 OSSpinLockUnlock(&wwo->lock);
1008 WARN("buffers still playing !\n");
1009 return WAVERR_STILLPLAYING;
1010 } else
1012 OSStatus err;
1013 AudioUnit audioUnit = wwo->audioUnit;
1015 /* sanity check: this should not happen since the device must have been reset before */
1016 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1018 wwo->state = WINE_WS_CLOSING; /* mark the device as closing */
1019 wwo->audioUnit = NULL;
1021 OSSpinLockUnlock(&wwo->lock);
1023 err = AudioUnitUninitialize(audioUnit);
1024 if (err) {
1025 ERR("AudioUnitUninitialize return %s\n", wine_dbgstr_fourcc(err));
1026 return MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
1029 if ( !AudioUnit_CloseAudioUnit(audioUnit) )
1031 ERR("Can't close AudioUnit\n");
1032 return MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
1035 OSSpinLockLock(&WOutDev[wDevID].lock);
1036 list_remove(&wwo->entry);
1037 OSSpinLockUnlock(&WOutDev[wDevID].lock);
1039 wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1041 HeapFree(GetProcessHeap(), 0, wwo);
1044 return ret;
1047 /**************************************************************************
1048 * wodPrepare [internal]
1050 static DWORD wodPrepare(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1052 TRACE("(%u, %p, %p, %08x);\n", wDevID, wwo, lpWaveHdr, dwSize);
1054 if (wDevID >= MAX_WAVEOUTDRV) {
1055 WARN("bad device ID !\n");
1056 return MMSYSERR_BADDEVICEID;
1059 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1060 return WAVERR_STILLPLAYING;
1062 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1063 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1065 return MMSYSERR_NOERROR;
1068 /**************************************************************************
1069 * wodUnprepare [internal]
1071 static DWORD wodUnprepare(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1073 TRACE("(%u, %p, %p, %08x);\n", wDevID, wwo, lpWaveHdr, dwSize);
1075 if (wDevID >= MAX_WAVEOUTDRV) {
1076 WARN("bad device ID !\n");
1077 return MMSYSERR_BADDEVICEID;
1080 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1081 return WAVERR_STILLPLAYING;
1083 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1084 lpWaveHdr->dwFlags |= WHDR_DONE;
1086 return MMSYSERR_NOERROR;
1090 /**************************************************************************
1091 * wodHelper_CheckForLoopBegin [internal]
1093 * Check if the new waveheader is the beginning of a loop, and set up
1094 * state if so.
1095 * This is called with the WAVEOUT_INSTANCE lock held.
1096 * Call from AudioUnit IO thread can't use Wine debug channels.
1098 static void wodHelper_CheckForLoopBegin(WINE_WAVEOUT_INSTANCE* wwo)
1100 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1102 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)
1104 if (wwo->lpLoopPtr)
1106 if (WOutDev[wwo->woID].warn_on)
1107 fprintf(stderr, "warn:winecoreaudio:wodHelper_CheckForLoopBegin Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1109 else
1111 if (WOutDev[wwo->woID].trace_on)
1112 fprintf(stderr, "trace:winecoreaudio:wodHelper_CheckForLoopBegin Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1114 wwo->lpLoopPtr = lpWaveHdr;
1115 /* Windows does not touch WAVEHDR.dwLoops,
1116 * so we need to make an internal copy */
1117 wwo->dwLoops = lpWaveHdr->dwLoops;
1123 /**************************************************************************
1124 * wodHelper_PlayPtrNext [internal]
1126 * Advance the play pointer to the next waveheader, looping if required.
1127 * This is called with the WAVEOUT_INSTANCE lock held.
1128 * Call from AudioUnit IO thread can't use Wine debug channels.
1130 static void wodHelper_PlayPtrNext(WINE_WAVEOUT_INSTANCE* wwo)
1132 BOOL didLoopBack = FALSE;
1134 wwo->dwPartialOffset = 0;
1135 if ((wwo->lpPlayPtr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr)
1137 /* We're at the end of a loop, loop if required */
1138 if (wwo->dwLoops > 1)
1140 wwo->dwLoops--;
1141 wwo->lpPlayPtr = wwo->lpLoopPtr;
1142 didLoopBack = TRUE;
1144 else
1146 wwo->lpLoopPtr = NULL;
1149 if (!didLoopBack)
1151 /* We didn't loop back. Advance to the next wave header */
1152 wwo->lpPlayPtr = wwo->lpPlayPtr->lpNext;
1154 if (wwo->lpPlayPtr)
1155 wodHelper_CheckForLoopBegin(wwo);
1159 /* Send the "done" notification for each WAVEHDR in a list. The list must be
1160 * free-standing. It should not be part of a device instance's queue.
1161 * This function must be called with the WAVEOUT_INSTANCE lock *not* held.
1162 * Furthermore, it does not lock it, itself. That's because the callback to the
1163 * application may prompt the application to operate on the device, and we don't
1164 * want to deadlock.
1166 static void wodHelper_NotifyDoneForList(WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr)
1168 while (lpWaveHdr)
1170 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1172 lpWaveHdr->lpNext = NULL;
1173 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1174 lpWaveHdr->dwFlags |= WHDR_DONE;
1175 wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
1177 lpWaveHdr = lpNext;
1181 /* if force is TRUE then notify the client that all the headers were completed
1183 static void wodHelper_NotifyCompletions(WINE_WAVEOUT_INSTANCE* wwo, BOOL force)
1185 LPWAVEHDR lpFirstDoneWaveHdr = NULL;
1187 OSSpinLockLock(&wwo->lock);
1189 /* First, excise all of the done headers from the queue into
1190 * a free-standing list. */
1191 if (force)
1193 lpFirstDoneWaveHdr = wwo->lpQueuePtr;
1194 wwo->lpQueuePtr = NULL;
1196 else
1198 LPWAVEHDR lpWaveHdr;
1199 LPWAVEHDR lpLastDoneWaveHdr = NULL;
1201 /* Start from lpQueuePtr and keep notifying until:
1202 * - we hit an unwritten wavehdr
1203 * - we hit the beginning of a running loop
1204 * - we hit a wavehdr which hasn't finished playing
1206 for (
1207 lpWaveHdr = wwo->lpQueuePtr;
1208 lpWaveHdr &&
1209 lpWaveHdr != wwo->lpPlayPtr &&
1210 lpWaveHdr != wwo->lpLoopPtr;
1211 lpWaveHdr = lpWaveHdr->lpNext
1214 if (!lpFirstDoneWaveHdr)
1215 lpFirstDoneWaveHdr = lpWaveHdr;
1216 lpLastDoneWaveHdr = lpWaveHdr;
1219 if (lpLastDoneWaveHdr)
1221 wwo->lpQueuePtr = lpLastDoneWaveHdr->lpNext;
1222 lpLastDoneWaveHdr->lpNext = NULL;
1226 OSSpinLockUnlock(&wwo->lock);
1228 /* Now, send the "done" notification for each header in our list. */
1229 wodHelper_NotifyDoneForList(wwo, lpFirstDoneWaveHdr);
1233 /**************************************************************************
1234 * wodWrite [internal]
1237 static DWORD wodWrite(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1239 LPWAVEHDR*wh;
1241 TRACE("(%u, %p, %p, %lu, %08X);\n", wDevID, wwo, lpWaveHdr, (unsigned long)lpWaveHdr->dwBufferLength, dwSize);
1243 /* first, do the sanity checks... */
1244 if (wDevID >= MAX_WAVEOUTDRV)
1246 WARN("bad dev ID !\n");
1247 return MMSYSERR_BADDEVICEID;
1250 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1252 TRACE("unprepared\n");
1253 return WAVERR_UNPREPARED;
1256 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1258 TRACE("still playing\n");
1259 return WAVERR_STILLPLAYING;
1262 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1263 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1264 lpWaveHdr->lpNext = 0;
1266 OSSpinLockLock(&wwo->lock);
1267 /* insert buffer at the end of queue */
1268 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
1269 /* Do nothing */;
1270 *wh = lpWaveHdr;
1272 if (!wwo->lpPlayPtr)
1274 wwo->lpPlayPtr = lpWaveHdr;
1276 wodHelper_CheckForLoopBegin(wwo);
1278 wwo->dwPartialOffset = 0;
1280 OSSpinLockUnlock(&wwo->lock);
1282 return MMSYSERR_NOERROR;
1285 /**************************************************************************
1286 * wodPause [internal]
1288 static DWORD wodPause(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
1290 OSStatus status;
1292 TRACE("(%u, %p);!\n", wDevID, wwo);
1294 if (wDevID >= MAX_WAVEOUTDRV)
1296 WARN("bad device ID !\n");
1297 return MMSYSERR_BADDEVICEID;
1300 /* The order of the following operations is important since we can't hold
1301 * the mutex while we make an Audio Unit call. Stop the Audio Unit before
1302 * setting the PAUSED state. In wodRestart, the order is reversed. This
1303 * guarantees that we can't get into a situation where the state is
1304 * PLAYING but the Audio Unit isn't running. Although we can be in PAUSED
1305 * state with the Audio Unit still running, that's harmless because the
1306 * render callback will just produce silence.
1308 status = AudioOutputUnitStop(wwo->audioUnit);
1309 if (status)
1310 WARN("AudioOutputUnitStop return %s\n", wine_dbgstr_fourcc(status));
1312 OSSpinLockLock(&wwo->lock);
1313 if (wwo->state == WINE_WS_PLAYING)
1314 wwo->state = WINE_WS_PAUSED;
1315 OSSpinLockUnlock(&wwo->lock);
1317 return MMSYSERR_NOERROR;
1320 /**************************************************************************
1321 * wodRestart [internal]
1323 static DWORD wodRestart(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
1325 OSStatus status;
1327 TRACE("(%u, %p);\n", wDevID, wwo);
1329 if (wDevID >= MAX_WAVEOUTDRV )
1331 WARN("bad device ID !\n");
1332 return MMSYSERR_BADDEVICEID;
1335 /* The order of the following operations is important since we can't hold
1336 * the mutex while we make an Audio Unit call. Set the PLAYING
1337 * state before starting the Audio Unit. In wodPause, the order is
1338 * reversed. This guarantees that we can't get into a situation where
1339 * the state is PLAYING but the Audio Unit isn't running.
1340 * Although we can be in PAUSED state with the Audio Unit still running,
1341 * that's harmless because the render callback will just produce silence.
1343 OSSpinLockLock(&wwo->lock);
1344 if (wwo->state == WINE_WS_PAUSED)
1345 wwo->state = WINE_WS_PLAYING;
1346 OSSpinLockUnlock(&wwo->lock);
1348 status = AudioOutputUnitStart(wwo->audioUnit);
1349 if (status) {
1350 ERR("AudioOutputUnitStart return %s\n", wine_dbgstr_fourcc(status));
1351 return MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
1354 return MMSYSERR_NOERROR;
1357 /**************************************************************************
1358 * wodReset [internal]
1360 static DWORD wodReset(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
1362 OSStatus status;
1363 LPWAVEHDR lpSavedQueuePtr;
1365 TRACE("(%u, %p);\n", wDevID, wwo);
1367 if (wDevID >= MAX_WAVEOUTDRV)
1369 WARN("bad device ID !\n");
1370 return MMSYSERR_BADDEVICEID;
1373 OSSpinLockLock(&wwo->lock);
1375 if (wwo->state == WINE_WS_CLOSING || wwo->state == WINE_WS_OPENING)
1377 OSSpinLockUnlock(&wwo->lock);
1378 WARN("resetting a closed device\n");
1379 return MMSYSERR_INVALHANDLE;
1382 lpSavedQueuePtr = wwo->lpQueuePtr;
1383 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1384 wwo->state = WINE_WS_PLAYING;
1385 wwo->dwPlayedTotal = 0;
1387 wwo->dwPartialOffset = 0; /* Clear partial wavehdr */
1389 OSSpinLockUnlock(&wwo->lock);
1391 status = AudioOutputUnitStart(wwo->audioUnit);
1393 if (status) {
1394 ERR( "AudioOutputUnitStart return %s\n", wine_dbgstr_fourcc(status));
1395 return MMSYSERR_ERROR; /* FIXME return an error based on the OSStatus */
1398 /* Now, send the "done" notification for each header in our list. */
1399 /* Do this last so the reset operation is effectively complete before the
1400 * app does whatever it's going to do in response to these notifications. */
1401 wodHelper_NotifyDoneForList(wwo, lpSavedQueuePtr);
1403 return MMSYSERR_NOERROR;
1406 /**************************************************************************
1407 * wodBreakLoop [internal]
1409 static DWORD wodBreakLoop(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo)
1411 TRACE("(%u, %p);\n", wDevID, wwo);
1413 if (wDevID >= MAX_WAVEOUTDRV)
1415 WARN("bad device ID !\n");
1416 return MMSYSERR_BADDEVICEID;
1419 OSSpinLockLock(&wwo->lock);
1421 if (wwo->lpLoopPtr != NULL)
1423 /* ensure exit at end of current loop */
1424 wwo->dwLoops = 1;
1427 OSSpinLockUnlock(&wwo->lock);
1429 return MMSYSERR_NOERROR;
1432 /**************************************************************************
1433 * wodGetPosition [internal]
1435 static DWORD wodGetPosition(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPMMTIME lpTime, DWORD uSize)
1437 DWORD val;
1439 TRACE("(%u, %p, %p, %u);\n", wDevID, wwo, lpTime, uSize);
1441 if (wDevID >= MAX_WAVEOUTDRV)
1443 WARN("bad device ID !\n");
1444 return MMSYSERR_BADDEVICEID;
1447 /* if null pointer to time structure return error */
1448 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1450 OSSpinLockLock(&wwo->lock);
1451 val = wwo->dwPlayedTotal;
1452 OSSpinLockUnlock(&wwo->lock);
1454 return bytes_to_mmtime(lpTime, val, &wwo->format);
1457 /**************************************************************************
1458 * wodGetVolume [internal]
1460 static DWORD wodGetVolume(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, LPDWORD lpdwVol)
1462 if (wDevID >= MAX_WAVEOUTDRV)
1464 WARN("bad device ID !\n");
1465 return MMSYSERR_BADDEVICEID;
1468 TRACE("(%u, %p, %p);\n", wDevID, wwo, lpdwVol);
1470 if (wwo)
1472 float left;
1473 float right;
1475 AudioUnit_GetVolume(wwo->audioUnit, &left, &right);
1476 *lpdwVol = (WORD)(left * 0xFFFFl) + ((WORD)(right * 0xFFFFl) << 16);
1478 else
1479 *lpdwVol = WOutDev[wDevID].device_volume;
1481 return MMSYSERR_NOERROR;
1484 /**************************************************************************
1485 * wodSetVolume [internal]
1487 static DWORD wodSetVolume(WORD wDevID, WINE_WAVEOUT_INSTANCE* wwo, DWORD dwParam)
1489 float left;
1490 float right;
1492 if (wDevID >= MAX_WAVEOUTDRV)
1494 WARN("bad device ID !\n");
1495 return MMSYSERR_BADDEVICEID;
1498 left = LOWORD(dwParam) / 65535.0f;
1499 right = HIWORD(dwParam) / 65535.0f;
1501 TRACE("(%u, %p, %08x);\n", wDevID, wwo, dwParam);
1503 if (wwo)
1504 AudioUnit_SetVolume(wwo->audioUnit, left, right);
1505 else
1507 OSSpinLockLock(&WOutDev[wDevID].lock);
1508 LIST_FOR_EACH_ENTRY(wwo, &WOutDev[wDevID].instances, WINE_WAVEOUT_INSTANCE, entry)
1509 AudioUnit_SetVolume(wwo->audioUnit, left, right);
1510 OSSpinLockUnlock(&WOutDev[wDevID].lock);
1512 WOutDev[wDevID].device_volume = dwParam;
1515 return MMSYSERR_NOERROR;
1518 /**************************************************************************
1519 * wodGetNumDevs [internal]
1521 static DWORD wodGetNumDevs(void)
1523 TRACE("\n");
1524 return MAX_WAVEOUTDRV;
1527 /**************************************************************************
1528 * wodDevInterfaceSize [internal]
1530 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1532 TRACE("(%u, %p)\n", wDevID, dwParam1);
1534 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].cadev->interface_name, -1,
1535 NULL, 0 ) * sizeof(WCHAR);
1536 return MMSYSERR_NOERROR;
1539 /**************************************************************************
1540 * wodDevInterface [internal]
1542 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1544 TRACE("\n");
1545 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].cadev->interface_name, -1,
1546 NULL, 0 ) * sizeof(WCHAR))
1548 MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].cadev->interface_name, -1,
1549 dwParam1, dwParam2 / sizeof(WCHAR));
1550 return MMSYSERR_NOERROR;
1552 return MMSYSERR_INVALPARAM;
1555 /**************************************************************************
1556 * widDsCreate [internal]
1558 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1560 TRACE("(%d,%p)\n",wDevID,drv);
1562 FIXME("DirectSound not implemented\n");
1563 FIXME("The (slower) DirectSound HEL mode will be used instead.\n");
1564 return MMSYSERR_NOTSUPPORTED;
1567 /**************************************************************************
1568 * wodDsDesc [internal]
1570 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1572 /* The DirectSound HEL will automatically wrap a non-DirectSound-capable
1573 * driver in a DirectSound adaptor, thus allowing the driver to be used by
1574 * DirectSound clients. However, it only does this if we respond
1575 * successfully to the DRV_QUERYDSOUNDDESC message. It's enough to fill in
1576 * the driver and device names of the description output parameter. */
1577 *desc = WOutDev[wDevID].cadev->ds_desc;
1578 return MMSYSERR_NOERROR;
1581 /**************************************************************************
1582 * wodMessage (WINECOREAUDIO.7)
1584 DWORD WINAPI CoreAudio_wodMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1585 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1587 WINE_WAVEOUT_INSTANCE* wwo = (WINE_WAVEOUT_INSTANCE*)dwUser;
1589 TRACE("(%u, %s, %p, %p, %p);\n",
1590 wDevID, getMessage(wMsg), (void*)dwUser, (void*)dwParam1, (void*)dwParam2);
1592 switch (wMsg) {
1593 case DRVM_INIT:
1594 case DRVM_EXIT:
1595 case DRVM_ENABLE:
1596 case DRVM_DISABLE:
1598 /* FIXME: Pretend this is supported */
1599 return 0;
1600 case WODM_OPEN: return wodOpen(wDevID, (WINE_WAVEOUT_INSTANCE**)dwUser, (LPWAVEOPENDESC) dwParam1, dwParam2);
1601 case WODM_CLOSE: return wodClose(wDevID, wwo);
1602 case WODM_WRITE: return wodWrite(wDevID, wwo, (LPWAVEHDR) dwParam1, dwParam2);
1603 case WODM_PAUSE: return wodPause(wDevID, wwo);
1604 case WODM_GETPOS: return wodGetPosition(wDevID, wwo, (LPMMTIME) dwParam1, dwParam2);
1605 case WODM_BREAKLOOP: return wodBreakLoop(wDevID, wwo);
1606 case WODM_PREPARE: return wodPrepare(wDevID, wwo, (LPWAVEHDR)dwParam1, dwParam2);
1607 case WODM_UNPREPARE: return wodUnprepare(wDevID, wwo, (LPWAVEHDR)dwParam1, dwParam2);
1609 case WODM_GETDEVCAPS: return wodGetDevCaps(wDevID, (LPWAVEOUTCAPSW) dwParam1, dwParam2);
1610 case WODM_GETNUMDEVS: return wodGetNumDevs();
1612 case WODM_GETPITCH:
1613 case WODM_SETPITCH:
1614 case WODM_GETPLAYBACKRATE:
1615 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1616 case WODM_GETVOLUME: return wodGetVolume(wDevID, wwo, (LPDWORD)dwParam1);
1617 case WODM_SETVOLUME: return wodSetVolume(wDevID, wwo, dwParam1);
1618 case WODM_RESTART: return wodRestart(wDevID, wwo);
1619 case WODM_RESET: return wodReset(wDevID, wwo);
1621 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1622 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1623 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1624 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1626 default:
1627 FIXME("unknown message %d!\n", wMsg);
1630 return MMSYSERR_NOTSUPPORTED;
1633 /*======================================================================*
1634 * Low level DSOUND implementation *
1635 *======================================================================*/
1637 typedef struct IDsDriverImpl IDsDriverImpl;
1638 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1640 struct IDsDriverImpl
1642 /* IUnknown fields */
1643 const IDsDriverVtbl *lpVtbl;
1644 DWORD ref;
1645 /* IDsDriverImpl fields */
1646 UINT wDevID;
1647 IDsDriverBufferImpl*primary;
1650 struct IDsDriverBufferImpl
1652 /* IUnknown fields */
1653 const IDsDriverBufferVtbl *lpVtbl;
1654 DWORD ref;
1655 /* IDsDriverBufferImpl fields */
1656 IDsDriverImpl* drv;
1657 DWORD buflen;
1662 CoreAudio IO threaded callback,
1663 we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
1665 OSStatus CoreAudio_woAudioUnitIOProc(void *inRefCon,
1666 AudioUnitRenderActionFlags *ioActionFlags,
1667 const AudioTimeStamp *inTimeStamp,
1668 UInt32 inBusNumber,
1669 UInt32 inNumberFrames,
1670 AudioBufferList *ioData)
1672 UInt32 buffer;
1673 WINE_WAVEOUT_INSTANCE* wwo = (WINE_WAVEOUT_INSTANCE*)inRefCon;
1674 int needNotify = 0;
1676 unsigned int dataNeeded = ioData->mBuffers[0].mDataByteSize;
1677 unsigned int dataProvided = 0;
1679 OSSpinLockLock(&wwo->lock);
1681 /* We might have been called before wwo has been completely filled out by
1682 * wodOpen, or while it's being closed in wodClose. We have to do nothing
1683 * in that case. The check of wwo->state below ensures that. */
1684 while (dataNeeded > 0 && wwo->state == WINE_WS_PLAYING && wwo->lpPlayPtr)
1686 unsigned int available = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
1687 unsigned int toCopy;
1689 if (available >= dataNeeded)
1690 toCopy = dataNeeded;
1691 else
1692 toCopy = available;
1694 if (toCopy > 0)
1696 memcpy((char*)ioData->mBuffers[0].mData + dataProvided,
1697 wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, toCopy);
1698 wwo->dwPartialOffset += toCopy;
1699 wwo->dwPlayedTotal += toCopy;
1700 dataProvided += toCopy;
1701 dataNeeded -= toCopy;
1702 available -= toCopy;
1705 if (available == 0)
1707 wodHelper_PlayPtrNext(wwo);
1708 needNotify = 1;
1711 ioData->mBuffers[0].mDataByteSize = dataProvided;
1713 OSSpinLockUnlock(&wwo->lock);
1715 /* We can't provide any more wave data. Fill the rest with silence. */
1716 if (dataNeeded > 0)
1718 if (!dataProvided)
1719 *ioActionFlags |= kAudioUnitRenderAction_OutputIsSilence;
1720 memset((char*)ioData->mBuffers[0].mData + dataProvided, 0, dataNeeded);
1721 dataProvided += dataNeeded;
1722 dataNeeded = 0;
1725 /* We only fill buffer 0. Set any others that might be requested to 0. */
1726 for (buffer = 1; buffer < ioData->mNumberBuffers; buffer++)
1728 memset(ioData->mBuffers[buffer].mData, 0, ioData->mBuffers[buffer].mDataByteSize);
1731 if (needNotify) wodSendNotifyCompletionsMessage(wwo);
1732 return noErr;
1736 /*======================================================================*
1737 * Low level WAVE IN implementation *
1738 *======================================================================*/
1740 /**************************************************************************
1741 * widNotifyClient [internal]
1743 static void widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1745 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
1747 switch (wMsg)
1749 case WIM_OPEN:
1750 case WIM_CLOSE:
1751 case WIM_DATA:
1752 DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
1753 (HDRVR)wwi->waveDesc.hWave, wMsg, wwi->waveDesc.dwInstance,
1754 dwParam1, dwParam2);
1755 break;
1756 default:
1757 FIXME("Unknown callback message %u\n", wMsg);
1762 /**************************************************************************
1763 * widHelper_NotifyCompletions [internal]
1765 static void widHelper_NotifyCompletions(WINE_WAVEIN* wwi)
1767 LPWAVEHDR lpWaveHdr;
1768 LPWAVEHDR lpFirstDoneWaveHdr = NULL;
1769 LPWAVEHDR lpLastDoneWaveHdr = NULL;
1771 OSSpinLockLock(&wwi->lock);
1773 /* First, excise all of the done headers from the queue into
1774 * a free-standing list. */
1776 /* Start from lpQueuePtr and keep notifying until:
1777 * - we hit an unfilled wavehdr
1778 * - we hit the end of the list
1780 for (
1781 lpWaveHdr = wwi->lpQueuePtr;
1782 lpWaveHdr &&
1783 lpWaveHdr->dwBytesRecorded >= lpWaveHdr->dwBufferLength;
1784 lpWaveHdr = lpWaveHdr->lpNext
1787 if (!lpFirstDoneWaveHdr)
1788 lpFirstDoneWaveHdr = lpWaveHdr;
1789 lpLastDoneWaveHdr = lpWaveHdr;
1792 if (lpLastDoneWaveHdr)
1794 wwi->lpQueuePtr = lpLastDoneWaveHdr->lpNext;
1795 lpLastDoneWaveHdr->lpNext = NULL;
1798 OSSpinLockUnlock(&wwi->lock);
1800 /* Now, send the "done" notification for each header in our list. */
1801 lpWaveHdr = lpFirstDoneWaveHdr;
1802 while (lpWaveHdr)
1804 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1806 lpWaveHdr->lpNext = NULL;
1807 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1808 lpWaveHdr->dwFlags |= WHDR_DONE;
1809 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
1811 lpWaveHdr = lpNext;
1816 /**************************************************************************
1817 * widGetDevCaps [internal]
1819 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
1821 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1823 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1825 if (wDevID >= MAX_WAVEINDRV)
1827 TRACE("MAX_WAVEINDRV reached !\n");
1828 return MMSYSERR_BADDEVICEID;
1831 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1832 return MMSYSERR_NOERROR;
1836 /**************************************************************************
1837 * widHelper_DestroyAudioBufferList [internal]
1838 * Convenience function to dispose of our audio buffers
1840 static void widHelper_DestroyAudioBufferList(AudioBufferList* list)
1842 if (list)
1844 UInt32 i;
1845 for (i = 0; i < list->mNumberBuffers; i++)
1847 if (list->mBuffers[i].mData)
1848 HeapFree(GetProcessHeap(), 0, list->mBuffers[i].mData);
1850 HeapFree(GetProcessHeap(), 0, list);
1855 #define AUDIOBUFFERLISTSIZE(numBuffers) (offsetof(AudioBufferList, mBuffers) + (numBuffers) * sizeof(AudioBuffer))
1857 /**************************************************************************
1858 * widHelper_AllocateAudioBufferList [internal]
1859 * Convenience function to allocate our audio buffers
1861 static AudioBufferList* widHelper_AllocateAudioBufferList(UInt32 numChannels, UInt32 bitsPerChannel, UInt32 bufferFrames, BOOL interleaved)
1863 UInt32 numBuffers;
1864 UInt32 channelsPerFrame;
1865 UInt32 bytesPerFrame;
1866 UInt32 bytesPerBuffer;
1867 AudioBufferList* list;
1868 UInt32 i;
1870 if (interleaved)
1872 /* For interleaved audio, we allocate one buffer for all channels. */
1873 numBuffers = 1;
1874 channelsPerFrame = numChannels;
1876 else
1878 numBuffers = numChannels;
1879 channelsPerFrame = 1;
1882 bytesPerFrame = bitsPerChannel * channelsPerFrame / 8;
1883 bytesPerBuffer = bytesPerFrame * bufferFrames;
1885 list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, AUDIOBUFFERLISTSIZE(numBuffers));
1886 if (list == NULL)
1887 return NULL;
1889 list->mNumberBuffers = numBuffers;
1890 for (i = 0; i < numBuffers; ++i)
1892 list->mBuffers[i].mNumberChannels = channelsPerFrame;
1893 list->mBuffers[i].mDataByteSize = bytesPerBuffer;
1894 list->mBuffers[i].mData = HeapAlloc(GetProcessHeap(), 0, bytesPerBuffer);
1895 if (list->mBuffers[i].mData == NULL)
1897 widHelper_DestroyAudioBufferList(list);
1898 return NULL;
1901 return list;
1905 /**************************************************************************
1906 * widOpen [internal]
1908 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1910 WINE_WAVEIN* wwi;
1911 UInt32 frameCount;
1913 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1914 if (lpDesc == NULL)
1916 WARN("Invalid Parameter !\n");
1917 return MMSYSERR_INVALPARAM;
1919 if (wDevID >= MAX_WAVEINDRV)
1921 TRACE ("MAX_WAVEINDRV reached !\n");
1922 return MMSYSERR_BADDEVICEID;
1925 TRACE("Format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
1926 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1927 lpDesc->lpFormat->nSamplesPerSec, lpDesc->lpFormat->wBitsPerSample);
1929 if (!supportedFormat(lpDesc->lpFormat) ||
1930 lpDesc->lpFormat->nSamplesPerSec != AudioUnit_GetInputDeviceSampleRate()
1933 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
1934 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1935 lpDesc->lpFormat->nSamplesPerSec, lpDesc->lpFormat->wBitsPerSample);
1936 return WAVERR_BADFORMAT;
1939 if (dwFlags & WAVE_FORMAT_QUERY)
1941 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1942 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1943 lpDesc->lpFormat->nSamplesPerSec);
1944 return MMSYSERR_NOERROR;
1947 /* nBlockAlign and nAvgBytesPerSec are output variables for dsound */
1948 if (lpDesc->lpFormat->nBlockAlign != lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8) {
1949 lpDesc->lpFormat->nBlockAlign = lpDesc->lpFormat->nChannels*lpDesc->lpFormat->wBitsPerSample/8;
1950 WARN("Fixing nBlockAlign\n");
1952 if (lpDesc->lpFormat->nAvgBytesPerSec!= lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign) {
1953 lpDesc->lpFormat->nAvgBytesPerSec = lpDesc->lpFormat->nSamplesPerSec*lpDesc->lpFormat->nBlockAlign;
1954 WARN("Fixing nAvgBytesPerSec\n");
1957 wwi = &WInDev[wDevID];
1958 if (!OSSpinLockTry(&wwi->lock))
1959 return MMSYSERR_ALLOCATED;
1961 if (wwi->state != WINE_WS_CLOSED)
1963 OSSpinLockUnlock(&wwi->lock);
1964 return MMSYSERR_ALLOCATED;
1967 wwi->state = WINE_WS_STOPPED;
1968 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1970 wwi->waveDesc = *lpDesc;
1971 copyFormat(lpDesc->lpFormat, &wwi->format);
1973 wwi->dwTotalRecorded = 0;
1975 wwi->trace_on = TRACE_ON(wave);
1976 wwi->warn_on = WARN_ON(wave);
1977 wwi->err_on = ERR_ON(wave);
1979 if (!AudioUnit_CreateInputUnit(wwi, &wwi->audioUnit,
1980 wwi->format.wf.nChannels, wwi->format.wf.nSamplesPerSec,
1981 wwi->format.wBitsPerSample, &frameCount))
1983 OSSpinLockUnlock(&wwi->lock);
1984 ERR("AudioUnit_CreateInputUnit failed\n");
1985 return MMSYSERR_ERROR;
1988 /* Allocate our audio buffers */
1989 wwi->bufferList = widHelper_AllocateAudioBufferList(wwi->format.wf.nChannels,
1990 wwi->format.wBitsPerSample, frameCount, TRUE);
1991 if (wwi->bufferList == NULL)
1993 AudioUnitUninitialize(wwi->audioUnit);
1994 AudioUnit_CloseAudioUnit(wwi->audioUnit);
1995 OSSpinLockUnlock(&wwi->lock);
1996 ERR("Failed to allocate buffer list\n");
1997 return MMSYSERR_NOMEM;
2000 /* Keep a copy of the buffer list structure (but not the buffers themselves)
2001 * in case AudioUnitRender clobbers the original, as it tends to do. */
2002 wwi->bufferListCopy = HeapAlloc(GetProcessHeap(), 0, AUDIOBUFFERLISTSIZE(wwi->bufferList->mNumberBuffers));
2003 if (wwi->bufferListCopy == NULL)
2005 widHelper_DestroyAudioBufferList(wwi->bufferList);
2006 AudioUnitUninitialize(wwi->audioUnit);
2007 AudioUnit_CloseAudioUnit(wwi->audioUnit);
2008 OSSpinLockUnlock(&wwi->lock);
2009 ERR("Failed to allocate buffer list copy\n");
2010 return MMSYSERR_NOMEM;
2012 memcpy(wwi->bufferListCopy, wwi->bufferList, AUDIOBUFFERLISTSIZE(wwi->bufferList->mNumberBuffers));
2014 OSSpinLockUnlock(&wwi->lock);
2016 widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
2018 return MMSYSERR_NOERROR;
2022 /**************************************************************************
2023 * widClose [internal]
2025 static DWORD widClose(WORD wDevID)
2027 DWORD ret = MMSYSERR_NOERROR;
2028 WINE_WAVEIN* wwi;
2029 OSStatus err;
2031 TRACE("(%u);\n", wDevID);
2033 if (wDevID >= MAX_WAVEINDRV)
2035 WARN("bad device ID !\n");
2036 return MMSYSERR_BADDEVICEID;
2039 wwi = &WInDev[wDevID];
2040 OSSpinLockLock(&wwi->lock);
2041 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2043 WARN("Device already closed.\n");
2044 ret = MMSYSERR_INVALHANDLE;
2046 else if (wwi->lpQueuePtr)
2048 WARN("Buffers in queue.\n");
2049 ret = WAVERR_STILLPLAYING;
2051 else
2053 wwi->state = WINE_WS_CLOSING;
2056 OSSpinLockUnlock(&wwi->lock);
2058 if (ret != MMSYSERR_NOERROR)
2059 return ret;
2062 /* Clean up and close the audio unit. This has to be done without
2063 * wwi->lock being held to avoid deadlock. AudioUnitUninitialize will
2064 * grab an internal Core Audio lock while waiting for the device work
2065 * thread to exit. Meanwhile the device work thread may be holding
2066 * that lock and trying to grab the wwi->lock in the callback. */
2067 err = AudioUnitUninitialize(wwi->audioUnit);
2068 if (err)
2069 ERR("AudioUnitUninitialize return %s\n", wine_dbgstr_fourcc(err));
2071 if (!AudioUnit_CloseAudioUnit(wwi->audioUnit))
2072 ERR("Can't close AudioUnit\n");
2075 OSSpinLockLock(&wwi->lock);
2076 assert(wwi->state == WINE_WS_CLOSING);
2078 /* Dellocate our audio buffers */
2079 widHelper_DestroyAudioBufferList(wwi->bufferList);
2080 wwi->bufferList = NULL;
2081 HeapFree(GetProcessHeap(), 0, wwi->bufferListCopy);
2082 wwi->bufferListCopy = NULL;
2084 wwi->audioUnit = NULL;
2085 wwi->state = WINE_WS_CLOSED;
2086 OSSpinLockUnlock(&wwi->lock);
2088 widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
2090 return ret;
2094 /**************************************************************************
2095 * widAddBuffer [internal]
2097 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2099 DWORD ret = MMSYSERR_NOERROR;
2100 WINE_WAVEIN* wwi;
2102 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
2104 if (wDevID >= MAX_WAVEINDRV)
2106 WARN("invalid device ID\n");
2107 return MMSYSERR_INVALHANDLE;
2109 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED))
2111 TRACE("never been prepared !\n");
2112 return WAVERR_UNPREPARED;
2114 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2116 TRACE("header already in use !\n");
2117 return WAVERR_STILLPLAYING;
2120 wwi = &WInDev[wDevID];
2121 OSSpinLockLock(&wwi->lock);
2123 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2125 WARN("Trying to add buffer to closed device.\n");
2126 ret = MMSYSERR_INVALHANDLE;
2128 else
2130 LPWAVEHDR* wh;
2132 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2133 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2134 lpWaveHdr->dwBytesRecorded = 0;
2135 lpWaveHdr->lpNext = NULL;
2137 /* insert buffer at end of queue */
2138 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
2139 /* Do nothing */;
2140 *wh = lpWaveHdr;
2143 OSSpinLockUnlock(&wwi->lock);
2145 return ret;
2149 /**************************************************************************
2150 * widStart [internal]
2152 static DWORD widStart(WORD wDevID)
2154 DWORD ret = MMSYSERR_NOERROR;
2155 WINE_WAVEIN* wwi;
2157 TRACE("(%u);\n", wDevID);
2158 if (wDevID >= MAX_WAVEINDRV)
2160 WARN("invalid device ID\n");
2161 return MMSYSERR_INVALHANDLE;
2164 /* The order of the following operations is important since we can't hold
2165 * the mutex while we make an Audio Unit call. Set the PLAYING state
2166 * before starting the Audio Unit. In widStop, the order is reversed.
2167 * This guarantees that we can't get into a situation where the state is
2168 * PLAYING but the Audio Unit isn't running. Although we can be in STOPPED
2169 * state with the Audio Unit still running, that's harmless because the
2170 * input callback will just throw away the sound data.
2172 wwi = &WInDev[wDevID];
2173 OSSpinLockLock(&wwi->lock);
2175 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2177 WARN("Trying to start closed device.\n");
2178 ret = MMSYSERR_INVALHANDLE;
2180 else
2181 wwi->state = WINE_WS_PLAYING;
2183 OSSpinLockUnlock(&wwi->lock);
2185 if (ret == MMSYSERR_NOERROR)
2187 /* Start pulling for audio data */
2188 OSStatus err = AudioOutputUnitStart(wwi->audioUnit);
2189 if (err != noErr)
2190 ERR("Failed to start AU: %08lx\n", err);
2192 TRACE("Recording started...\n");
2195 return ret;
2199 /**************************************************************************
2200 * widStop [internal]
2202 static DWORD widStop(WORD wDevID)
2204 DWORD ret = MMSYSERR_NOERROR;
2205 WINE_WAVEIN* wwi;
2206 WAVEHDR* lpWaveHdr = NULL;
2207 OSStatus err;
2209 TRACE("(%u);\n", wDevID);
2210 if (wDevID >= MAX_WAVEINDRV)
2212 WARN("invalid device ID\n");
2213 return MMSYSERR_INVALHANDLE;
2216 wwi = &WInDev[wDevID];
2218 /* The order of the following operations is important since we can't hold
2219 * the mutex while we make an Audio Unit call. Stop the Audio Unit before
2220 * setting the STOPPED state. In widStart, the order is reversed. This
2221 * guarantees that we can't get into a situation where the state is
2222 * PLAYING but the Audio Unit isn't running. Although we can be in STOPPED
2223 * state with the Audio Unit still running, that's harmless because the
2224 * input callback will just throw away the sound data.
2226 err = AudioOutputUnitStop(wwi->audioUnit);
2227 if (err != noErr)
2228 WARN("Failed to stop AU: %08lx\n", err);
2230 TRACE("Recording stopped.\n");
2232 OSSpinLockLock(&wwi->lock);
2234 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2236 WARN("Trying to stop closed device.\n");
2237 ret = MMSYSERR_INVALHANDLE;
2239 else if (wwi->state != WINE_WS_STOPPED)
2241 wwi->state = WINE_WS_STOPPED;
2242 /* If there's a buffer in progress, it's done. Remove it from the
2243 * queue so that we can return it to the app, below. */
2244 if (wwi->lpQueuePtr)
2246 lpWaveHdr = wwi->lpQueuePtr;
2247 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2251 OSSpinLockUnlock(&wwi->lock);
2253 if (lpWaveHdr)
2255 lpWaveHdr->lpNext = NULL;
2256 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2257 lpWaveHdr->dwFlags |= WHDR_DONE;
2258 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2261 return ret;
2264 /**************************************************************************
2265 * widGetPos [internal]
2267 static DWORD widGetPos(WORD wDevID, LPMMTIME lpTime, UINT size)
2269 DWORD val;
2270 WINE_WAVEIN* wwi;
2272 TRACE("(%u);\n", wDevID);
2273 if (wDevID >= MAX_WAVEINDRV)
2275 WARN("invalid device ID\n");
2276 return MMSYSERR_INVALHANDLE;
2279 wwi = &WInDev[wDevID];
2281 OSSpinLockLock(&WInDev[wDevID].lock);
2282 val = wwi->dwTotalRecorded;
2283 OSSpinLockUnlock(&WInDev[wDevID].lock);
2285 return bytes_to_mmtime(lpTime, val, &wwi->format);
2288 /**************************************************************************
2289 * widReset [internal]
2291 static DWORD widReset(WORD wDevID)
2293 DWORD ret = MMSYSERR_NOERROR;
2294 WINE_WAVEIN* wwi;
2295 WAVEHDR* lpWaveHdr = NULL;
2297 TRACE("(%u);\n", wDevID);
2298 if (wDevID >= MAX_WAVEINDRV)
2300 WARN("invalid device ID\n");
2301 return MMSYSERR_INVALHANDLE;
2304 wwi = &WInDev[wDevID];
2305 OSSpinLockLock(&wwi->lock);
2307 if (wwi->state == WINE_WS_CLOSED || wwi->state == WINE_WS_CLOSING)
2309 WARN("Trying to reset a closed device.\n");
2310 ret = MMSYSERR_INVALHANDLE;
2312 else
2314 lpWaveHdr = wwi->lpQueuePtr;
2315 wwi->lpQueuePtr = NULL;
2316 wwi->state = WINE_WS_STOPPED;
2317 wwi->dwTotalRecorded = 0;
2320 OSSpinLockUnlock(&wwi->lock);
2322 if (ret == MMSYSERR_NOERROR)
2324 OSStatus err = AudioOutputUnitStop(wwi->audioUnit);
2325 if (err != noErr)
2326 WARN("Failed to stop AU: %08lx\n", err);
2328 TRACE("Recording stopped.\n");
2331 while (lpWaveHdr)
2333 WAVEHDR* lpNext = lpWaveHdr->lpNext;
2335 lpWaveHdr->lpNext = NULL;
2336 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2337 lpWaveHdr->dwFlags |= WHDR_DONE;
2338 widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
2340 lpWaveHdr = lpNext;
2343 return ret;
2347 /**************************************************************************
2348 * widGetNumDevs [internal]
2350 static DWORD widGetNumDevs(void)
2352 return MAX_WAVEINDRV;
2356 /**************************************************************************
2357 * widDevInterfaceSize [internal]
2359 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
2361 TRACE("(%u, %p)\n", wDevID, dwParam1);
2363 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].interface_name, -1,
2364 NULL, 0 ) * sizeof(WCHAR);
2365 return MMSYSERR_NOERROR;
2369 /**************************************************************************
2370 * widDevInterface [internal]
2372 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
2374 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].interface_name, -1,
2375 NULL, 0 ) * sizeof(WCHAR))
2377 MultiByteToWideChar(CP_UNIXCP, 0, WInDev[wDevID].interface_name, -1,
2378 dwParam1, dwParam2 / sizeof(WCHAR));
2379 return MMSYSERR_NOERROR;
2381 return MMSYSERR_INVALPARAM;
2385 /**************************************************************************
2386 * widDsCreate [internal]
2388 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
2390 TRACE("(%d,%p)\n",wDevID,drv);
2392 FIXME("DirectSoundCapture not implemented\n");
2393 FIXME("The (slower) DirectSound HEL mode will be used instead.\n");
2394 return MMSYSERR_NOTSUPPORTED;
2397 /**************************************************************************
2398 * widDsDesc [internal]
2400 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2402 /* The DirectSound HEL will automatically wrap a non-DirectSound-capable
2403 * driver in a DirectSound adaptor, thus allowing the driver to be used by
2404 * DirectSound clients. However, it only does this if we respond
2405 * successfully to the DRV_QUERYDSOUNDDESC message. It's enough to fill in
2406 * the driver and device names of the description output parameter. */
2407 memset(desc, 0, sizeof(*desc));
2408 lstrcpynA(desc->szDrvname, "winecoreaudio.drv", sizeof(desc->szDrvname) - 1);
2409 lstrcpynA(desc->szDesc, WInDev[wDevID].interface_name, sizeof(desc->szDesc) - 1);
2410 return MMSYSERR_NOERROR;
2414 /**************************************************************************
2415 * widMessage (WINECOREAUDIO.6)
2417 DWORD WINAPI CoreAudio_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2418 DWORD dwParam1, DWORD dwParam2)
2420 TRACE("(%u, %04X, %08X, %08X, %08X);\n",
2421 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2423 switch (wMsg)
2425 case DRVM_INIT:
2426 case DRVM_EXIT:
2427 case DRVM_ENABLE:
2428 case DRVM_DISABLE:
2429 /* FIXME: Pretend this is supported */
2430 return 0;
2431 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2432 case WIDM_CLOSE: return widClose (wDevID);
2433 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2434 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
2435 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
2436 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
2437 case WIDM_GETNUMDEVS: return widGetNumDevs ();
2438 case WIDM_RESET: return widReset (wDevID);
2439 case WIDM_START: return widStart (wDevID);
2440 case WIDM_STOP: return widStop (wDevID);
2441 case WIDM_GETPOS: return widGetPos (wDevID, (LPMMTIME)dwParam1, (UINT)dwParam2 );
2442 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2443 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2444 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
2445 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2446 default:
2447 FIXME("unknown message %d!\n", wMsg);
2450 return MMSYSERR_NOTSUPPORTED;
2454 OSStatus CoreAudio_wiAudioUnitIOProc(void *inRefCon,
2455 AudioUnitRenderActionFlags *ioActionFlags,
2456 const AudioTimeStamp *inTimeStamp,
2457 UInt32 inBusNumber,
2458 UInt32 inNumberFrames,
2459 AudioBufferList *ioData)
2461 WINE_WAVEIN* wwi = (WINE_WAVEIN*)inRefCon;
2462 OSStatus err = noErr;
2463 BOOL needNotify = FALSE;
2464 WAVEHDR* lpStorePtr;
2465 unsigned int dataToStore;
2466 unsigned int dataStored = 0;
2469 if (wwi->trace_on)
2470 fprintf(stderr, "trace:wave:CoreAudio_wiAudioUnitIOProc (ioActionFlags = %08lx, "
2471 "inTimeStamp = { %f, %x%08x, %f, %x%08x, %08lx }, inBusNumber = %lu, inNumberFrames = %lu)\n",
2472 *ioActionFlags, inTimeStamp->mSampleTime, (DWORD)(inTimeStamp->mHostTime >>32),
2473 (DWORD)inTimeStamp->mHostTime, inTimeStamp->mRateScalar, (DWORD)(inTimeStamp->mWordClockTime >> 32),
2474 (DWORD)inTimeStamp->mWordClockTime, inTimeStamp->mFlags, inBusNumber, inNumberFrames);
2476 /* Render into audio buffer */
2477 /* FIXME: implement sample rate conversion on input. This will require
2478 * a different render strategy. We'll need to buffer the sound data
2479 * received here and pass it off to an AUConverter in another thread. */
2480 err = AudioUnitRender(wwi->audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, wwi->bufferList);
2481 if (err)
2483 if (wwi->err_on)
2484 fprintf(stderr, "err:wave:CoreAudio_wiAudioUnitIOProc AudioUnitRender failed with error %li\n", err);
2485 return err;
2488 /* Copy from audio buffer to the wavehdrs */
2489 dataToStore = wwi->bufferList->mBuffers[0].mDataByteSize;
2491 OSSpinLockLock(&wwi->lock);
2493 lpStorePtr = wwi->lpQueuePtr;
2495 /* We might have been called while the waveIn device is being closed in
2496 * widClose. We have to do nothing in that case. The check of wwi->state
2497 * below ensures that. */
2498 while (dataToStore > 0 && wwi->state == WINE_WS_PLAYING && lpStorePtr)
2500 unsigned int room = lpStorePtr->dwBufferLength - lpStorePtr->dwBytesRecorded;
2501 unsigned int toCopy;
2503 if (wwi->trace_on)
2504 fprintf(stderr, "trace:wave:CoreAudio_wiAudioUnitIOProc Looking to store %u bytes to wavehdr %p, which has room for %u\n",
2505 dataToStore, lpStorePtr, room);
2507 if (room >= dataToStore)
2508 toCopy = dataToStore;
2509 else
2510 toCopy = room;
2512 if (toCopy > 0)
2514 memcpy(lpStorePtr->lpData + lpStorePtr->dwBytesRecorded,
2515 (char*)wwi->bufferList->mBuffers[0].mData + dataStored, toCopy);
2516 lpStorePtr->dwBytesRecorded += toCopy;
2517 wwi->dwTotalRecorded += toCopy;
2518 dataStored += toCopy;
2519 dataToStore -= toCopy;
2520 room -= toCopy;
2523 if (room == 0)
2525 lpStorePtr = lpStorePtr->lpNext;
2526 needNotify = TRUE;
2530 OSSpinLockUnlock(&wwi->lock);
2532 /* Restore the audio buffer list structure from backup, in case
2533 * AudioUnitRender clobbered it. (It modifies mDataByteSize and may even
2534 * give us a different mData buffer to avoid a copy.) */
2535 memcpy(wwi->bufferList, wwi->bufferListCopy, AUDIOBUFFERLISTSIZE(wwi->bufferList->mNumberBuffers));
2537 if (needNotify) wodSendNotifyInputCompletionsMessage(wwi);
2538 return err;