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
44 #include "coreaudio.h"
45 #include "wine/unicode.h"
46 #include "wine/library.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(wave
);
52 #if defined(HAVE_COREAUDIO_COREAUDIO_H) && defined(HAVE_AUDIOUNIT_AUDIOUNIT_H)
53 #include <CoreAudio/CoreAudio.h>
54 #include <CoreFoundation/CoreFoundation.h>
55 #include <libkern/OSAtomic.h>
58 Due to AudioUnit headers conflict define some needed types.
61 typedef void *AudioUnit
;
63 /* From AudioUnit/AUComponents.h */
66 kAudioUnitRenderAction_OutputIsSilence
= (1 << 4),
67 /* provides hint on return from Render(): if set the buffer contains all zeroes */
69 typedef UInt32 AudioUnitRenderActionFlags
;
71 typedef long ComponentResult
;
72 extern ComponentResult
73 AudioUnitRender( AudioUnit ci
,
74 AudioUnitRenderActionFlags
* ioActionFlags
,
75 const AudioTimeStamp
* inTimeStamp
,
76 UInt32 inOutputBusNumber
,
77 UInt32 inNumberFrames
,
78 AudioBufferList
* ioData
) AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER
;
80 /* only allow 10 output devices through this driver, this ought to be adequate */
81 #define MAX_WAVEOUTDRV (1)
82 #define MAX_WAVEINDRV (1)
84 /* state diagram for waveOut writing:
86 * +---------+-------------+---------------+---------------------------------+
87 * | state | function | event | new state |
88 * +---------+-------------+---------------+---------------------------------+
89 * | | open() | | STOPPED |
90 * | PAUSED | write() | | PAUSED |
91 * | STOPPED | write() | <thrd create> | PLAYING |
92 * | PLAYING | write() | HEADER | PLAYING |
93 * | (other) | write() | <error> | |
94 * | (any) | pause() | PAUSING | PAUSED |
95 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
96 * | (any) | reset() | RESETTING | STOPPED |
97 * | (any) | close() | CLOSING | CLOSED |
98 * +---------+-------------+---------------+---------------------------------+
101 /* states of the playing device */
102 #define WINE_WS_PLAYING 0
103 #define WINE_WS_PAUSED 1
104 #define WINE_WS_STOPPED 2
105 #define WINE_WS_CLOSED 3
107 typedef struct tagCoreAudio_Device
{
111 char* interface_name
;
113 WAVEOUTCAPSW out_caps
;
115 DWORD in_caps_support
;
119 unsigned audio_fragment
;
121 BOOL bTriggerSupport
;
124 DSDRIVERDESC ds_desc
;
125 DSDRIVERCAPS ds_caps
;
126 DSCDRIVERCAPS dsc_caps
;
130 AudioDeviceID outputDeviceID
;
131 AudioDeviceID inputDeviceID
;
132 AudioStreamBasicDescription streamDescription
;
135 /* for now use the default device */
136 static CoreAudio_Device CoreAudio_DefaultDevice
;
139 volatile int state
; /* one of the WINE_WS_ manifest constants */
140 CoreAudio_Device
*cadev
;
141 WAVEOPENDESC waveDesc
;
143 PCMWAVEFORMAT format
;
146 AudioStreamBasicDescription streamDescription
;
149 char interface_name
[32];
150 LPWAVEHDR lpQueuePtr
; /* start of queued WAVEHDRs (waiting to be notified) */
151 LPWAVEHDR lpPlayPtr
; /* start of not yet fully played buffers */
152 DWORD dwPartialOffset
; /* Offset of not yet written bytes in lpPlayPtr */
154 LPWAVEHDR lpLoopPtr
; /* pointer of first buffer in loop, if any */
155 DWORD dwLoops
; /* private copy of loop counter */
157 DWORD dwPlayedTotal
; /* number of bytes actually played since opening */
158 DWORD dwWrittenTotal
; /* number of bytes written to OSS buffer since opening */
160 DWORD tickCountMS
; /* time in MS of last AudioUnit callback */
162 OSSpinLock lock
; /* synchronization stuff */
170 /* This device's device number */
173 /* Access to the following fields is synchronized across threads. */
175 LPWAVEHDR lpQueuePtr
;
176 DWORD dwTotalRecorded
;
178 /* Synchronization mechanism to protect above fields */
181 /* Capabilities description */
183 char interface_name
[32];
185 /* Record the arguments used when opening the device. */
186 WAVEOPENDESC waveDesc
;
188 PCMWAVEFORMAT format
;
191 AudioBufferList
*bufferList
;
193 /* Record state of debug channels at open. Used to control fprintf's since
194 * we can't use Wine debug channel calls in non-Wine AudioUnit threads. */
199 /* These fields aren't used. */
201 CoreAudio_Device
*cadev
;
203 AudioStreamBasicDescription streamDescription
;
207 static WINE_WAVEOUT WOutDev
[MAX_WAVEOUTDRV
];
208 static WINE_WAVEIN WInDev
[MAX_WAVEINDRV
];
210 static CFMessagePortRef Port_SendToMessageThread
;
212 static void wodHelper_PlayPtrNext(WINE_WAVEOUT
* wwo
);
213 static void wodHelper_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
);
214 static void widHelper_NotifyCompletions(WINE_WAVEIN
* wwi
);
216 extern int AudioUnit_CreateDefaultAudioUnit(void *wwo
, AudioUnit
*au
);
217 extern int AudioUnit_CloseAudioUnit(AudioUnit au
);
218 extern int AudioUnit_InitializeWithStreamDescription(AudioUnit au
, AudioStreamBasicDescription
*streamFormat
);
220 extern OSStatus
AudioOutputUnitStart(AudioUnit au
);
221 extern OSStatus
AudioOutputUnitStop(AudioUnit au
);
222 extern OSStatus
AudioUnitUninitialize(AudioUnit au
);
224 extern int AudioUnit_SetVolume(AudioUnit au
, float left
, float right
);
225 extern int AudioUnit_GetVolume(AudioUnit au
, float *left
, float *right
);
227 extern int AudioUnit_CreateInputUnit(void* wwi
, AudioUnit
* out_au
,
228 WORD nChannels
, DWORD nSamplesPerSec
, WORD wBitsPerSample
,
229 UInt32
* outFrameCount
);
231 OSStatus
CoreAudio_woAudioUnitIOProc(void *inRefCon
,
232 AudioUnitRenderActionFlags
*ioActionFlags
,
233 const AudioTimeStamp
*inTimeStamp
,
235 UInt32 inNumberFrames
,
236 AudioBufferList
*ioData
);
237 OSStatus
CoreAudio_wiAudioUnitIOProc(void *inRefCon
,
238 AudioUnitRenderActionFlags
*ioActionFlags
,
239 const AudioTimeStamp
*inTimeStamp
,
241 UInt32 inNumberFrames
,
242 AudioBufferList
*ioData
);
244 /* These strings used only for tracing */
246 static const char * getMessage(UINT msg
)
248 static char unknown
[32];
249 #define MSG_TO_STR(x) case x: return #x
251 MSG_TO_STR(DRVM_INIT
);
252 MSG_TO_STR(DRVM_EXIT
);
253 MSG_TO_STR(DRVM_ENABLE
);
254 MSG_TO_STR(DRVM_DISABLE
);
255 MSG_TO_STR(WIDM_OPEN
);
256 MSG_TO_STR(WIDM_CLOSE
);
257 MSG_TO_STR(WIDM_ADDBUFFER
);
258 MSG_TO_STR(WIDM_PREPARE
);
259 MSG_TO_STR(WIDM_UNPREPARE
);
260 MSG_TO_STR(WIDM_GETDEVCAPS
);
261 MSG_TO_STR(WIDM_GETNUMDEVS
);
262 MSG_TO_STR(WIDM_GETPOS
);
263 MSG_TO_STR(WIDM_RESET
);
264 MSG_TO_STR(WIDM_START
);
265 MSG_TO_STR(WIDM_STOP
);
266 MSG_TO_STR(WODM_OPEN
);
267 MSG_TO_STR(WODM_CLOSE
);
268 MSG_TO_STR(WODM_WRITE
);
269 MSG_TO_STR(WODM_PAUSE
);
270 MSG_TO_STR(WODM_GETPOS
);
271 MSG_TO_STR(WODM_BREAKLOOP
);
272 MSG_TO_STR(WODM_PREPARE
);
273 MSG_TO_STR(WODM_UNPREPARE
);
274 MSG_TO_STR(WODM_GETDEVCAPS
);
275 MSG_TO_STR(WODM_GETNUMDEVS
);
276 MSG_TO_STR(WODM_GETPITCH
);
277 MSG_TO_STR(WODM_SETPITCH
);
278 MSG_TO_STR(WODM_GETPLAYBACKRATE
);
279 MSG_TO_STR(WODM_SETPLAYBACKRATE
);
280 MSG_TO_STR(WODM_GETVOLUME
);
281 MSG_TO_STR(WODM_SETVOLUME
);
282 MSG_TO_STR(WODM_RESTART
);
283 MSG_TO_STR(WODM_RESET
);
284 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE
);
285 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE
);
286 MSG_TO_STR(DRV_QUERYDSOUNDIFACE
);
287 MSG_TO_STR(DRV_QUERYDSOUNDDESC
);
290 sprintf(unknown
, "UNKNOWN(0x%04x)", msg
);
294 #define kStopLoopMessage 0
295 #define kWaveOutNotifyCompletionsMessage 1
296 #define kWaveInNotifyCompletionsMessage 2
298 /* Mach Message Handling */
299 static CFDataRef
wodMessageHandler(CFMessagePortRef port_ReceiveInMessageThread
, SInt32 msgid
, CFDataRef data
, void *info
)
301 UInt32
*buffer
= NULL
;
305 case kWaveOutNotifyCompletionsMessage
:
306 buffer
= (UInt32
*) CFDataGetBytePtr(data
);
307 wodHelper_NotifyCompletions(&WOutDev
[buffer
[0]], FALSE
);
309 case kWaveInNotifyCompletionsMessage
:
310 buffer
= (UInt32
*) CFDataGetBytePtr(data
);
311 widHelper_NotifyCompletions(&WInDev
[buffer
[0]]);
314 CFRunLoopStop(CFRunLoopGetCurrent());
321 static DWORD WINAPI
messageThread(LPVOID p
)
323 CFMessagePortRef port_ReceiveInMessageThread
= (CFMessagePortRef
) p
;
324 CFRunLoopSourceRef source
;
326 source
= CFMessagePortCreateRunLoopSource(kCFAllocatorDefault
, port_ReceiveInMessageThread
, (CFIndex
)0);
327 CFRunLoopAddSource(CFRunLoopGetCurrent(), source
, kCFRunLoopDefaultMode
);
331 CFRunLoopSourceInvalidate(source
);
333 CFRelease(port_ReceiveInMessageThread
);
338 /**************************************************************************
339 * wodSendNotifyCompletionsMessage [internal]
340 * Call from AudioUnit IO thread can't use Wine debug channels.
342 static void wodSendNotifyCompletionsMessage(WINE_WAVEOUT
* wwo
)
347 buffer
= (UInt32
) wwo
->woID
;
349 data
= CFDataCreate(kCFAllocatorDefault
, (UInt8
*)&buffer
, sizeof(buffer
));
353 CFMessagePortSendRequest(Port_SendToMessageThread
, kWaveOutNotifyCompletionsMessage
, data
, 0.0, 0.0, NULL
, NULL
);
357 /**************************************************************************
358 * wodSendNotifyInputCompletionsMessage [internal]
359 * Call from AudioUnit IO thread can't use Wine debug channels.
361 static void wodSendNotifyInputCompletionsMessage(WINE_WAVEIN
* wwi
)
366 buffer
= (UInt32
) wwi
->wiID
;
368 data
= CFDataCreate(kCFAllocatorDefault
, (UInt8
*)&buffer
, sizeof(buffer
));
372 CFMessagePortSendRequest(Port_SendToMessageThread
, kWaveInNotifyCompletionsMessage
, data
, 0.0, 0.0, NULL
, NULL
);
376 static DWORD
bytes_to_mmtime(LPMMTIME lpTime
, DWORD position
,
377 PCMWAVEFORMAT
* format
)
379 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
380 lpTime
->wType
, format
->wBitsPerSample
, format
->wf
.nSamplesPerSec
,
381 format
->wf
.nChannels
, format
->wf
.nAvgBytesPerSec
);
382 TRACE("Position in bytes=%u\n", position
);
384 switch (lpTime
->wType
) {
386 lpTime
->u
.sample
= position
/ (format
->wBitsPerSample
/ 8 * format
->wf
.nChannels
);
387 TRACE("TIME_SAMPLES=%u\n", lpTime
->u
.sample
);
390 lpTime
->u
.ms
= 1000.0 * position
/ (format
->wBitsPerSample
/ 8 * format
->wf
.nChannels
* format
->wf
.nSamplesPerSec
);
391 TRACE("TIME_MS=%u\n", lpTime
->u
.ms
);
394 lpTime
->u
.smpte
.fps
= 30;
395 position
= position
/ (format
->wBitsPerSample
/ 8 * format
->wf
.nChannels
);
396 position
+= (format
->wf
.nSamplesPerSec
/ lpTime
->u
.smpte
.fps
) - 1; /* round up */
397 lpTime
->u
.smpte
.sec
= position
/ format
->wf
.nSamplesPerSec
;
398 position
-= lpTime
->u
.smpte
.sec
* format
->wf
.nSamplesPerSec
;
399 lpTime
->u
.smpte
.min
= lpTime
->u
.smpte
.sec
/ 60;
400 lpTime
->u
.smpte
.sec
-= 60 * lpTime
->u
.smpte
.min
;
401 lpTime
->u
.smpte
.hour
= lpTime
->u
.smpte
.min
/ 60;
402 lpTime
->u
.smpte
.min
-= 60 * lpTime
->u
.smpte
.hour
;
403 lpTime
->u
.smpte
.fps
= 30;
404 lpTime
->u
.smpte
.frame
= position
* lpTime
->u
.smpte
.fps
/ format
->wf
.nSamplesPerSec
;
405 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
406 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
407 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
410 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime
->wType
);
411 lpTime
->wType
= TIME_BYTES
;
414 lpTime
->u
.cb
= position
;
415 TRACE("TIME_BYTES=%u\n", lpTime
->u
.cb
);
418 return MMSYSERR_NOERROR
;
421 /**************************************************************************
422 * CoreAudio_GetDevCaps [internal]
424 BOOL
CoreAudio_GetDevCaps (void)
428 AudioDeviceID devId
= CoreAudio_DefaultDevice
.outputDeviceID
;
430 char name
[MAXPNAMELEN
];
432 propertySize
= MAXPNAMELEN
;
433 status
= AudioDeviceGetProperty(devId
, 0 , FALSE
, kAudioDevicePropertyDeviceName
, &propertySize
, name
);
435 ERR("AudioHardwareGetProperty for kAudioDevicePropertyDeviceName return %c%c%c%c\n", (char) (status
>> 24),
436 (char) (status
>> 16),
437 (char) (status
>> 8),
442 memcpy(CoreAudio_DefaultDevice
.ds_desc
.szDesc
, name
, sizeof(name
));
443 strcpy(CoreAudio_DefaultDevice
.ds_desc
.szDrvname
, "winecoreaudio.drv");
444 MultiByteToWideChar(CP_ACP
, 0, name
, sizeof(name
),
445 CoreAudio_DefaultDevice
.out_caps
.szPname
,
446 sizeof(CoreAudio_DefaultDevice
.out_caps
.szPname
) / sizeof(WCHAR
));
447 memcpy(CoreAudio_DefaultDevice
.dev_name
, name
, 32);
449 propertySize
= sizeof(CoreAudio_DefaultDevice
.streamDescription
);
450 status
= AudioDeviceGetProperty(devId
, 0, FALSE
, kAudioDevicePropertyStreamFormat
, &propertySize
, &CoreAudio_DefaultDevice
.streamDescription
);
451 if (status
!= noErr
) {
452 ERR("AudioHardwareGetProperty for kAudioDevicePropertyStreamFormat return %c%c%c%c\n", (char) (status
>> 24),
453 (char) (status
>> 16),
454 (char) (status
>> 8),
459 TRACE("Device Stream Description mSampleRate : %f\n mFormatID : %c%c%c%c\n"
460 "mFormatFlags : %lX\n mBytesPerPacket : %lu\n mFramesPerPacket : %lu\n"
461 "mBytesPerFrame : %lu\n mChannelsPerFrame : %lu\n mBitsPerChannel : %lu\n",
462 CoreAudio_DefaultDevice
.streamDescription
.mSampleRate
,
463 (char) (CoreAudio_DefaultDevice
.streamDescription
.mFormatID
>> 24),
464 (char) (CoreAudio_DefaultDevice
.streamDescription
.mFormatID
>> 16),
465 (char) (CoreAudio_DefaultDevice
.streamDescription
.mFormatID
>> 8),
466 (char) CoreAudio_DefaultDevice
.streamDescription
.mFormatID
,
467 CoreAudio_DefaultDevice
.streamDescription
.mFormatFlags
,
468 CoreAudio_DefaultDevice
.streamDescription
.mBytesPerPacket
,
469 CoreAudio_DefaultDevice
.streamDescription
.mFramesPerPacket
,
470 CoreAudio_DefaultDevice
.streamDescription
.mBytesPerFrame
,
471 CoreAudio_DefaultDevice
.streamDescription
.mChannelsPerFrame
,
472 CoreAudio_DefaultDevice
.streamDescription
.mBitsPerChannel
);
474 CoreAudio_DefaultDevice
.out_caps
.wMid
= 0xcafe;
475 CoreAudio_DefaultDevice
.out_caps
.wPid
= 0x0001;
477 CoreAudio_DefaultDevice
.out_caps
.vDriverVersion
= 0x0001;
478 CoreAudio_DefaultDevice
.out_caps
.dwFormats
= 0x00000000;
479 CoreAudio_DefaultDevice
.out_caps
.wReserved1
= 0;
480 CoreAudio_DefaultDevice
.out_caps
.dwSupport
= WAVECAPS_VOLUME
;
481 CoreAudio_DefaultDevice
.out_caps
.dwSupport
|= WAVECAPS_LRVOLUME
;
483 CoreAudio_DefaultDevice
.out_caps
.wChannels
= 2;
484 CoreAudio_DefaultDevice
.out_caps
.dwFormats
|= WAVE_FORMAT_4S16
;
489 /******************************************************************
492 * Initialize CoreAudio_DefaultDevice
494 LONG
CoreAudio_WaveInit(void)
498 CHAR szPname
[MAXPNAMELEN
];
501 CFStringRef messageThreadPortName
;
502 CFMessagePortRef port_ReceiveInMessageThread
;
506 /* number of sound cards */
507 AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices
, &propertySize
, NULL
);
508 propertySize
/= sizeof(AudioDeviceID
);
509 TRACE("sound cards : %lu\n", propertySize
);
511 /* Get the output device */
512 propertySize
= sizeof(CoreAudio_DefaultDevice
.outputDeviceID
);
513 status
= AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice
, &propertySize
, &CoreAudio_DefaultDevice
.outputDeviceID
);
515 ERR("AudioHardwareGetProperty return %c%c%c%c for kAudioHardwarePropertyDefaultOutputDevice\n", (char) (status
>> 24),
516 (char) (status
>> 16),
517 (char) (status
>> 8),
521 if (CoreAudio_DefaultDevice
.outputDeviceID
== kAudioDeviceUnknown
) {
522 ERR("AudioHardwareGetProperty: CoreAudio_DefaultDevice.outputDeviceID == kAudioDeviceUnknown\n");
526 if ( ! CoreAudio_GetDevCaps() )
529 CoreAudio_DefaultDevice
.interface_name
=HeapAlloc(GetProcessHeap(),0,strlen(CoreAudio_DefaultDevice
.dev_name
)+1);
530 sprintf(CoreAudio_DefaultDevice
.interface_name
, "%s", CoreAudio_DefaultDevice
.dev_name
);
532 for (i
= 0; i
< MAX_WAVEOUTDRV
; ++i
)
534 WOutDev
[i
].state
= WINE_WS_CLOSED
;
535 WOutDev
[i
].cadev
= &CoreAudio_DefaultDevice
;
538 memset(&WOutDev
[i
].caps
, 0, sizeof(WOutDev
[i
].caps
));
540 WOutDev
[i
].caps
.wMid
= 0xcafe; /* Manufac ID */
541 WOutDev
[i
].caps
.wPid
= 0x0001; /* Product ID */
542 snprintf(szPname
, sizeof(szPname
), "CoreAudio WaveOut %d", i
);
543 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, WOutDev
[i
].caps
.szPname
, sizeof(WOutDev
[i
].caps
.szPname
)/sizeof(WCHAR
));
544 snprintf(WOutDev
[i
].interface_name
, sizeof(WOutDev
[i
].interface_name
), "winecoreaudio: %d", i
);
546 WOutDev
[i
].caps
.vDriverVersion
= 0x0001;
547 WOutDev
[i
].caps
.dwFormats
= 0x00000000;
548 WOutDev
[i
].caps
.dwSupport
= WAVECAPS_VOLUME
;
550 WOutDev
[i
].caps
.wChannels
= 2;
551 /* WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME; */ /* FIXME */
553 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M08
;
554 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S08
;
555 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S16
;
556 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M16
;
557 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M08
;
558 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S08
;
559 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M16
;
560 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S16
;
561 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M08
;
562 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S08
;
563 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M16
;
564 WOutDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S16
;
566 WOutDev
[i
].lock
= 0; /* initialize the mutex */
569 for (i
= 0; i
< MAX_WAVEINDRV
; ++i
)
571 memset(&WInDev
[i
], 0, sizeof(WInDev
[i
]));
574 /* Establish preconditions for widOpen */
575 WInDev
[i
].state
= WINE_WS_CLOSED
;
576 WInDev
[i
].lock
= 0; /* initialize the mutex */
578 /* Fill in capabilities. widGetDevCaps can be called at any time. */
579 WInDev
[i
].caps
.wMid
= 0xcafe; /* Manufac ID */
580 WInDev
[i
].caps
.wPid
= 0x0001; /* Product ID */
581 WInDev
[i
].caps
.vDriverVersion
= 0x0001;
583 snprintf(szPname
, sizeof(szPname
), "CoreAudio WaveIn %d", i
);
584 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, WInDev
[i
].caps
.szPname
, sizeof(WInDev
[i
].caps
.szPname
)/sizeof(WCHAR
));
585 snprintf(WInDev
[i
].interface_name
, sizeof(WInDev
[i
].interface_name
), "winecoreaudio in: %d", i
);
587 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M08
;
588 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S08
;
589 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4S16
;
590 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_4M16
;
591 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M08
;
592 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S08
;
593 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2M16
;
594 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_2S16
;
595 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M08
;
596 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S08
;
597 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1M16
;
598 WInDev
[i
].caps
.dwFormats
|= WAVE_FORMAT_1S16
;
600 WInDev
[i
].caps
.wChannels
= 2;
603 /* create mach messages handler */
605 messageThreadPortName
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
606 CFSTR("WaveMessagePort.%d.%lu"), getpid(), (unsigned long)random());
607 if (!messageThreadPortName
)
609 ERR("Can't create message thread port name\n");
613 port_ReceiveInMessageThread
= CFMessagePortCreateLocal(kCFAllocatorDefault
, messageThreadPortName
,
614 &wodMessageHandler
, NULL
, NULL
);
615 if (!port_ReceiveInMessageThread
)
617 ERR("Can't create message thread local port\n");
618 CFRelease(messageThreadPortName
);
622 Port_SendToMessageThread
= CFMessagePortCreateRemote(kCFAllocatorDefault
, messageThreadPortName
);
623 CFRelease(messageThreadPortName
);
624 if (!Port_SendToMessageThread
)
626 ERR("Can't create port for sending to message thread\n");
627 CFRelease(port_ReceiveInMessageThread
);
631 hThread
= CreateThread(NULL
, 0, messageThread
, (LPVOID
)port_ReceiveInMessageThread
, 0, NULL
);
634 ERR("Can't create message thread\n");
635 CFRelease(port_ReceiveInMessageThread
);
636 CFRelease(Port_SendToMessageThread
);
637 Port_SendToMessageThread
= NULL
;
641 /* The message thread is responsible for releasing port_ReceiveInMessageThread. */
646 void CoreAudio_WaveRelease(void)
648 /* Stop CFRunLoop in messageThread */
651 CFMessagePortSendRequest(Port_SendToMessageThread
, kStopLoopMessage
, NULL
, 0.0, 0.0, NULL
, NULL
);
652 CFRelease(Port_SendToMessageThread
);
653 Port_SendToMessageThread
= NULL
;
656 /*======================================================================*
657 * Low level WAVE OUT implementation *
658 *======================================================================*/
660 /**************************************************************************
661 * wodNotifyClient [internal]
663 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
669 if (wwo
->wFlags
!= DCB_NULL
&&
670 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
,
671 (HDRVR
)wwo
->waveDesc
.hWave
, wMsg
, wwo
->waveDesc
.dwInstance
,
674 return MMSYSERR_ERROR
;
678 return MMSYSERR_INVALPARAM
;
680 return MMSYSERR_NOERROR
;
684 /**************************************************************************
685 * wodGetDevCaps [internal]
687 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSW lpCaps
, DWORD dwSize
)
689 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
691 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
693 if (wDevID
>= MAX_WAVEOUTDRV
)
695 TRACE("MAX_WAVOUTDRV reached !\n");
696 return MMSYSERR_BADDEVICEID
;
699 TRACE("dwSupport=(0x%x), dwFormats=(0x%x)\n", WOutDev
[wDevID
].caps
.dwSupport
, WOutDev
[wDevID
].caps
.dwFormats
);
700 memcpy(lpCaps
, &WOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
701 return MMSYSERR_NOERROR
;
704 /**************************************************************************
707 static DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
712 AudioStreamBasicDescription streamFormat
;
714 TRACE("(%u, %p, %08x);\n", wDevID
, lpDesc
, dwFlags
);
717 WARN("Invalid Parameter !\n");
718 return MMSYSERR_INVALPARAM
;
720 if (wDevID
>= MAX_WAVEOUTDRV
) {
721 TRACE("MAX_WAVOUTDRV reached !\n");
722 return MMSYSERR_BADDEVICEID
;
725 TRACE("Format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
726 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
727 lpDesc
->lpFormat
->nSamplesPerSec
, lpDesc
->lpFormat
->wBitsPerSample
);
729 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
730 lpDesc
->lpFormat
->nChannels
== 0 ||
731 lpDesc
->lpFormat
->nSamplesPerSec
== 0
734 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
735 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
736 lpDesc
->lpFormat
->nSamplesPerSec
, lpDesc
->lpFormat
->wBitsPerSample
);
737 return WAVERR_BADFORMAT
;
740 if (dwFlags
& WAVE_FORMAT_QUERY
)
742 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
743 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
744 lpDesc
->lpFormat
->nSamplesPerSec
);
745 return MMSYSERR_NOERROR
;
748 wwo
= &WOutDev
[wDevID
];
749 if (!OSSpinLockTry(&wwo
->lock
))
750 return MMSYSERR_ALLOCATED
;
752 if (wwo
->state
!= WINE_WS_CLOSED
)
754 OSSpinLockUnlock(&wwo
->lock
);
755 return MMSYSERR_ALLOCATED
;
758 if (!AudioUnit_CreateDefaultAudioUnit((void *) wwo
, &wwo
->audioUnit
))
760 ERR("CoreAudio_CreateDefaultAudioUnit(%p) failed\n", wwo
);
761 OSSpinLockUnlock(&wwo
->lock
);
762 return MMSYSERR_ERROR
;
765 if ((dwFlags
& WAVE_DIRECTSOUND
) &&
766 !(wwo
->caps
.dwSupport
& WAVECAPS_DIRECTSOUND
))
767 /* not supported, ignore it */
768 dwFlags
&= ~WAVE_DIRECTSOUND
;
770 streamFormat
.mFormatID
= kAudioFormatLinearPCM
;
771 streamFormat
.mFormatFlags
= kLinearPCMFormatFlagIsPacked
;
772 /* FIXME check for 32bits float -> kLinearPCMFormatFlagIsFloat */
773 if (lpDesc
->lpFormat
->wBitsPerSample
!= 8)
774 streamFormat
.mFormatFlags
|= kLinearPCMFormatFlagIsSignedInteger
;
775 # ifdef WORDS_BIGENDIAN
776 streamFormat
.mFormatFlags
|= kLinearPCMFormatFlagIsBigEndian
; /* FIXME Wave format is little endian */
779 streamFormat
.mSampleRate
= lpDesc
->lpFormat
->nSamplesPerSec
;
780 streamFormat
.mChannelsPerFrame
= lpDesc
->lpFormat
->nChannels
;
781 streamFormat
.mFramesPerPacket
= 1;
782 streamFormat
.mBitsPerChannel
= lpDesc
->lpFormat
->wBitsPerSample
;
783 streamFormat
.mBytesPerFrame
= streamFormat
.mBitsPerChannel
* streamFormat
.mChannelsPerFrame
/ 8;
784 streamFormat
.mBytesPerPacket
= streamFormat
.mBytesPerFrame
* streamFormat
.mFramesPerPacket
;
786 ret
= AudioUnit_InitializeWithStreamDescription(wwo
->audioUnit
, &streamFormat
);
789 AudioUnit_CloseAudioUnit(wwo
->audioUnit
);
790 OSSpinLockUnlock(&wwo
->lock
);
791 return WAVERR_BADFORMAT
; /* FIXME return an error based on the OSStatus */
793 wwo
->streamDescription
= streamFormat
;
795 ret
= AudioOutputUnitStart(wwo
->audioUnit
);
798 ERR("AudioOutputUnitStart failed: %08x\n", ret
);
799 AudioUnitUninitialize(wwo
->audioUnit
);
800 AudioUnit_CloseAudioUnit(wwo
->audioUnit
);
801 OSSpinLockUnlock(&wwo
->lock
);
802 return MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
805 wwo
->state
= WINE_WS_STOPPED
;
807 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
809 memcpy(&wwo
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
810 memcpy(&wwo
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
812 if (wwo
->format
.wBitsPerSample
== 0) {
813 WARN("Resetting zeroed wBitsPerSample\n");
814 wwo
->format
.wBitsPerSample
= 8 *
815 (wwo
->format
.wf
.nAvgBytesPerSec
/
816 wwo
->format
.wf
.nSamplesPerSec
) /
817 wwo
->format
.wf
.nChannels
;
820 wwo
->dwPlayedTotal
= 0;
821 wwo
->dwWrittenTotal
= 0;
823 wwo
->trace_on
= TRACE_ON(wave
);
824 wwo
->warn_on
= WARN_ON(wave
);
825 wwo
->err_on
= ERR_ON(wave
);
827 OSSpinLockUnlock(&wwo
->lock
);
829 retval
= wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
834 /**************************************************************************
835 * wodClose [internal]
837 static DWORD
wodClose(WORD wDevID
)
839 DWORD ret
= MMSYSERR_NOERROR
;
842 TRACE("(%u);\n", wDevID
);
844 if (wDevID
>= MAX_WAVEOUTDRV
)
846 WARN("bad device ID !\n");
847 return MMSYSERR_BADDEVICEID
;
850 wwo
= &WOutDev
[wDevID
];
851 OSSpinLockLock(&wwo
->lock
);
854 WARN("buffers still playing !\n");
855 OSSpinLockUnlock(&wwo
->lock
);
856 ret
= WAVERR_STILLPLAYING
;
860 /* sanity check: this should not happen since the device must have been reset before */
861 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
863 wwo
->state
= WINE_WS_CLOSED
; /* mark the device as closed */
865 OSSpinLockUnlock(&wwo
->lock
);
867 err
= AudioUnitUninitialize(wwo
->audioUnit
);
869 ERR("AudioUnitUninitialize return %c%c%c%c\n", (char) (err
>> 24),
873 return MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
876 if ( !AudioUnit_CloseAudioUnit(wwo
->audioUnit
) )
878 ERR("Can't close AudioUnit\n");
879 return MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
882 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
888 /**************************************************************************
889 * wodPrepare [internal]
891 static DWORD
wodPrepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
893 TRACE("(%u, %p, %08x);\n", wDevID
, lpWaveHdr
, dwSize
);
895 if (wDevID
>= MAX_WAVEOUTDRV
) {
896 WARN("bad device ID !\n");
897 return MMSYSERR_BADDEVICEID
;
900 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
901 return WAVERR_STILLPLAYING
;
903 lpWaveHdr
->dwFlags
|= WHDR_PREPARED
;
904 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
906 return MMSYSERR_NOERROR
;
909 /**************************************************************************
910 * wodUnprepare [internal]
912 static DWORD
wodUnprepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
914 TRACE("(%u, %p, %08x);\n", wDevID
, lpWaveHdr
, dwSize
);
916 if (wDevID
>= MAX_WAVEOUTDRV
) {
917 WARN("bad device ID !\n");
918 return MMSYSERR_BADDEVICEID
;
921 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
922 return WAVERR_STILLPLAYING
;
924 lpWaveHdr
->dwFlags
&= ~WHDR_PREPARED
;
925 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
927 return MMSYSERR_NOERROR
;
931 /**************************************************************************
932 * wodHelper_CheckForLoopBegin [internal]
934 * Check if the new waveheader is the beginning of a loop, and set up
936 * This is called with the WAVEOUT lock held.
937 * Call from AudioUnit IO thread can't use Wine debug channels.
939 static void wodHelper_CheckForLoopBegin(WINE_WAVEOUT
* wwo
)
941 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
943 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)
948 fprintf(stderr
, "warn:winecoreaudio:wodHelper_CheckForLoopBegin Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
953 fprintf(stderr
, "trace:winecoreaudio:wodHelper_CheckForLoopBegin Starting loop (%dx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
955 wwo
->lpLoopPtr
= lpWaveHdr
;
956 /* Windows does not touch WAVEHDR.dwLoops,
957 * so we need to make an internal copy */
958 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
964 /**************************************************************************
965 * wodHelper_PlayPtrNext [internal]
967 * Advance the play pointer to the next waveheader, looping if required.
968 * This is called with the WAVEOUT lock held.
969 * Call from AudioUnit IO thread can't use Wine debug channels.
971 static void wodHelper_PlayPtrNext(WINE_WAVEOUT
* wwo
)
973 BOOL didLoopBack
= FALSE
;
975 wwo
->dwPartialOffset
= 0;
976 if ((wwo
->lpPlayPtr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
)
978 /* We're at the end of a loop, loop if required */
979 if (wwo
->dwLoops
> 1)
982 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
987 wwo
->lpLoopPtr
= NULL
;
992 /* We didn't loop back. Advance to the next wave header */
993 wwo
->lpPlayPtr
= wwo
->lpPlayPtr
->lpNext
;
996 wwo
->state
= WINE_WS_STOPPED
;
998 wodHelper_CheckForLoopBegin(wwo
);
1002 /* if force is TRUE then notify the client that all the headers were completed
1004 static void wodHelper_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
1006 LPWAVEHDR lpWaveHdr
;
1007 LPWAVEHDR lpFirstDoneWaveHdr
= NULL
;
1009 OSSpinLockLock(&wwo
->lock
);
1011 /* First, excise all of the done headers from the queue into
1012 * a free-standing list. */
1015 lpFirstDoneWaveHdr
= wwo
->lpQueuePtr
;
1016 wwo
->lpQueuePtr
= NULL
;
1020 LPWAVEHDR lpLastDoneWaveHdr
= NULL
;
1022 /* Start from lpQueuePtr and keep notifying until:
1023 * - we hit an unwritten wavehdr
1024 * - we hit the beginning of a running loop
1025 * - we hit a wavehdr which hasn't finished playing
1028 lpWaveHdr
= wwo
->lpQueuePtr
;
1030 lpWaveHdr
!= wwo
->lpPlayPtr
&&
1031 lpWaveHdr
!= wwo
->lpLoopPtr
;
1032 lpWaveHdr
= lpWaveHdr
->lpNext
1035 if (!lpFirstDoneWaveHdr
)
1036 lpFirstDoneWaveHdr
= lpWaveHdr
;
1037 lpLastDoneWaveHdr
= lpWaveHdr
;
1040 if (lpLastDoneWaveHdr
)
1042 wwo
->lpQueuePtr
= lpLastDoneWaveHdr
->lpNext
;
1043 lpLastDoneWaveHdr
->lpNext
= NULL
;
1047 OSSpinLockUnlock(&wwo
->lock
);
1049 /* Now, send the "done" notification for each header in our list. */
1050 for (lpWaveHdr
= lpFirstDoneWaveHdr
; lpWaveHdr
; lpWaveHdr
= lpWaveHdr
->lpNext
)
1052 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1053 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1055 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
1060 /**************************************************************************
1061 * wodWrite [internal]
1064 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1069 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
1071 /* first, do the sanity checks... */
1072 if (wDevID
>= MAX_WAVEOUTDRV
)
1074 WARN("bad dev ID !\n");
1075 return MMSYSERR_BADDEVICEID
;
1078 wwo
= &WOutDev
[wDevID
];
1080 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
1082 TRACE("unprepared\n");
1083 return WAVERR_UNPREPARED
;
1086 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1088 TRACE("still playing\n");
1089 return WAVERR_STILLPLAYING
;
1092 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1093 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
1094 lpWaveHdr
->lpNext
= 0;
1096 OSSpinLockLock(&wwo
->lock
);
1097 /* insert buffer at the end of queue */
1098 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
))
1102 if (!wwo
->lpPlayPtr
)
1104 wwo
->lpPlayPtr
= lpWaveHdr
;
1106 if (wwo
->state
== WINE_WS_STOPPED
)
1107 wwo
->state
= WINE_WS_PLAYING
;
1109 wodHelper_CheckForLoopBegin(wwo
);
1111 wwo
->dwPartialOffset
= 0;
1113 OSSpinLockUnlock(&wwo
->lock
);
1115 return MMSYSERR_NOERROR
;
1118 /**************************************************************************
1119 * wodPause [internal]
1121 static DWORD
wodPause(WORD wDevID
)
1125 TRACE("(%u);!\n", wDevID
);
1127 if (wDevID
>= MAX_WAVEOUTDRV
)
1129 WARN("bad device ID !\n");
1130 return MMSYSERR_BADDEVICEID
;
1133 /* The order of the following operations is important since we can't hold
1134 * the mutex while we make an Audio Unit call. Stop the Audio Unit before
1135 * setting the PAUSED state. In wodRestart, the order is reversed. This
1136 * guarantees that we can't get into a situation where the state is
1137 * PLAYING or STOPPED but the Audio Unit isn't running. Although we can
1138 * be in PAUSED state with the Audio Unit still running, that's harmless
1139 * because the render callback will just produce silence.
1141 status
= AudioOutputUnitStop(WOutDev
[wDevID
].audioUnit
);
1143 WARN("AudioOutputUnitStop return %c%c%c%c\n",
1144 (char) (status
>> 24), (char) (status
>> 16), (char) (status
>> 8), (char) status
);
1147 OSSpinLockLock(&WOutDev
[wDevID
].lock
);
1148 if (WOutDev
[wDevID
].state
== WINE_WS_PLAYING
|| WOutDev
[wDevID
].state
== WINE_WS_STOPPED
)
1149 WOutDev
[wDevID
].state
= WINE_WS_PAUSED
;
1150 OSSpinLockUnlock(&WOutDev
[wDevID
].lock
);
1152 return MMSYSERR_NOERROR
;
1155 /**************************************************************************
1156 * wodRestart [internal]
1158 static DWORD
wodRestart(WORD wDevID
)
1162 TRACE("(%u);\n", wDevID
);
1164 if (wDevID
>= MAX_WAVEOUTDRV
)
1166 WARN("bad device ID !\n");
1167 return MMSYSERR_BADDEVICEID
;
1170 /* The order of the following operations is important since we can't hold
1171 * the mutex while we make an Audio Unit call. Set the PLAYING/STOPPED
1172 * state before starting the Audio Unit. In wodPause, the order is
1173 * reversed. This guarantees that we can't get into a situation where
1174 * the state is PLAYING or STOPPED but the Audio Unit isn't running.
1175 * Although we can be in PAUSED state with the Audio Unit still running,
1176 * that's harmless because the render callback will just produce silence.
1178 OSSpinLockLock(&WOutDev
[wDevID
].lock
);
1179 if (WOutDev
[wDevID
].state
== WINE_WS_PAUSED
)
1181 if (WOutDev
[wDevID
].lpPlayPtr
)
1182 WOutDev
[wDevID
].state
= WINE_WS_PLAYING
;
1184 WOutDev
[wDevID
].state
= WINE_WS_STOPPED
;
1186 OSSpinLockUnlock(&WOutDev
[wDevID
].lock
);
1188 status
= AudioOutputUnitStart(WOutDev
[wDevID
].audioUnit
);
1190 ERR("AudioOutputUnitStart return %c%c%c%c\n",
1191 (char) (status
>> 24), (char) (status
>> 16), (char) (status
>> 8), (char) status
);
1192 return MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
1195 return MMSYSERR_NOERROR
;
1198 /**************************************************************************
1199 * wodReset [internal]
1201 static DWORD
wodReset(WORD wDevID
)
1206 TRACE("(%u);\n", wDevID
);
1208 if (wDevID
>= MAX_WAVEOUTDRV
)
1210 WARN("bad device ID !\n");
1211 return MMSYSERR_BADDEVICEID
;
1214 wwo
= &WOutDev
[wDevID
];
1218 /* updates current notify list */
1219 /* if resetting, remove all wave headers and notify client that all headers were completed */
1220 wodHelper_NotifyCompletions(wwo
, TRUE
);
1222 OSSpinLockLock(&wwo
->lock
);
1224 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
1225 wwo
->state
= WINE_WS_STOPPED
;
1226 wwo
->dwPlayedTotal
= wwo
->dwWrittenTotal
= 0;
1228 wwo
->dwPartialOffset
= 0; /* Clear partial wavehdr */
1230 OSSpinLockUnlock(&wwo
->lock
);
1232 status
= AudioOutputUnitStart(wwo
->audioUnit
);
1235 ERR( "AudioOutputUnitStart return %c%c%c%c\n",
1236 (char) (status
>> 24), (char) (status
>> 16), (char) (status
>> 8), (char) status
);
1237 return MMSYSERR_ERROR
; /* FIXME return an error based on the OSStatus */
1240 return MMSYSERR_NOERROR
;
1243 /**************************************************************************
1244 * wodGetPosition [internal]
1246 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
1251 TRACE("(%u, %p, %u);\n", wDevID
, lpTime
, uSize
);
1253 if (wDevID
>= MAX_WAVEOUTDRV
)
1255 WARN("bad device ID !\n");
1256 return MMSYSERR_BADDEVICEID
;
1259 /* if null pointer to time structure return error */
1260 if (lpTime
== NULL
) return MMSYSERR_INVALPARAM
;
1262 wwo
= &WOutDev
[wDevID
];
1264 OSSpinLockLock(&WOutDev
[wDevID
].lock
);
1265 val
= wwo
->dwPlayedTotal
;
1266 OSSpinLockUnlock(&WOutDev
[wDevID
].lock
);
1268 return bytes_to_mmtime(lpTime
, val
, &wwo
->format
);
1271 /**************************************************************************
1272 * wodGetVolume [internal]
1274 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
1279 if (wDevID
>= MAX_WAVEOUTDRV
)
1281 WARN("bad device ID !\n");
1282 return MMSYSERR_BADDEVICEID
;
1285 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
1287 AudioUnit_GetVolume(WOutDev
[wDevID
].audioUnit
, &left
, &right
);
1289 *lpdwVol
= ((WORD
) left
* 0xFFFFl
) + (((WORD
) right
* 0xFFFFl
) << 16);
1291 return MMSYSERR_NOERROR
;
1294 /**************************************************************************
1295 * wodSetVolume [internal]
1297 static DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
1302 if (wDevID
>= MAX_WAVEOUTDRV
)
1304 WARN("bad device ID !\n");
1305 return MMSYSERR_BADDEVICEID
;
1308 left
= LOWORD(dwParam
) / 65535.0f
;
1309 right
= HIWORD(dwParam
) / 65535.0f
;
1311 TRACE("(%u, %08x);\n", wDevID
, dwParam
);
1313 AudioUnit_SetVolume(WOutDev
[wDevID
].audioUnit
, left
, right
);
1315 return MMSYSERR_NOERROR
;
1318 /**************************************************************************
1319 * wodGetNumDevs [internal]
1321 static DWORD
wodGetNumDevs(void)
1324 return MAX_WAVEOUTDRV
;
1327 /**************************************************************************
1328 * wodDevInterfaceSize [internal]
1330 static DWORD
wodDevInterfaceSize(UINT wDevID
, LPDWORD dwParam1
)
1332 TRACE("(%u, %p)\n", wDevID
, dwParam1
);
1334 *dwParam1
= MultiByteToWideChar(CP_ACP
, 0, WOutDev
[wDevID
].cadev
->interface_name
, -1,
1335 NULL
, 0 ) * sizeof(WCHAR
);
1336 return MMSYSERR_NOERROR
;
1339 /**************************************************************************
1340 * wodDevInterface [internal]
1342 static DWORD
wodDevInterface(UINT wDevID
, PWCHAR dwParam1
, DWORD dwParam2
)
1345 if (dwParam2
>= MultiByteToWideChar(CP_ACP
, 0, WOutDev
[wDevID
].cadev
->interface_name
, -1,
1346 NULL
, 0 ) * sizeof(WCHAR
))
1348 MultiByteToWideChar(CP_ACP
, 0, WOutDev
[wDevID
].cadev
->interface_name
, -1,
1349 dwParam1
, dwParam2
/ sizeof(WCHAR
));
1350 return MMSYSERR_NOERROR
;
1352 return MMSYSERR_INVALPARAM
;
1355 /**************************************************************************
1356 * wodMessage (WINECOREAUDIO.7)
1358 DWORD WINAPI
CoreAudio_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1359 DWORD dwParam1
, DWORD dwParam2
)
1361 TRACE("(%u, %s, %08x, %08x, %08x);\n",
1362 wDevID
, getMessage(wMsg
), dwUser
, dwParam1
, dwParam2
);
1370 /* FIXME: Pretend this is supported */
1372 case WODM_OPEN
: return wodOpen(wDevID
, (LPWAVEOPENDESC
) dwParam1
, dwParam2
);
1373 case WODM_CLOSE
: return wodClose(wDevID
);
1374 case WODM_WRITE
: return wodWrite(wDevID
, (LPWAVEHDR
) dwParam1
, dwParam2
);
1375 case WODM_PAUSE
: return wodPause(wDevID
);
1376 case WODM_GETPOS
: return wodGetPosition(wDevID
, (LPMMTIME
) dwParam1
, dwParam2
);
1377 case WODM_BREAKLOOP
: return MMSYSERR_NOTSUPPORTED
;
1378 case WODM_PREPARE
: return wodPrepare(wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1379 case WODM_UNPREPARE
: return wodUnprepare(wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1381 case WODM_GETDEVCAPS
: return wodGetDevCaps(wDevID
, (LPWAVEOUTCAPSW
) dwParam1
, dwParam2
);
1382 case WODM_GETNUMDEVS
: return wodGetNumDevs();
1386 case WODM_GETPLAYBACKRATE
:
1387 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1388 case WODM_GETVOLUME
: return wodGetVolume(wDevID
, (LPDWORD
)dwParam1
);
1389 case WODM_SETVOLUME
: return wodSetVolume(wDevID
, dwParam1
);
1390 case WODM_RESTART
: return wodRestart(wDevID
);
1391 case WODM_RESET
: return wodReset(wDevID
);
1393 case DRV_QUERYDEVICEINTERFACESIZE
: return wodDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
1394 case DRV_QUERYDEVICEINTERFACE
: return wodDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
1395 case DRV_QUERYDSOUNDIFACE
:
1396 case DRV_QUERYDSOUNDDESC
:
1397 return MMSYSERR_NOTSUPPORTED
;
1400 FIXME("unknown message %d!\n", wMsg
);
1403 return MMSYSERR_NOTSUPPORTED
;
1406 /*======================================================================*
1407 * Low level DSOUND implementation *
1408 *======================================================================*/
1410 typedef struct IDsDriverImpl IDsDriverImpl
;
1411 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
1413 struct IDsDriverImpl
1415 /* IUnknown fields */
1416 const IDsDriverVtbl
*lpVtbl
;
1418 /* IDsDriverImpl fields */
1420 IDsDriverBufferImpl
*primary
;
1423 struct IDsDriverBufferImpl
1425 /* IUnknown fields */
1426 const IDsDriverBufferVtbl
*lpVtbl
;
1428 /* IDsDriverBufferImpl fields */
1435 CoreAudio IO threaded callback,
1436 we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
1438 OSStatus
CoreAudio_woAudioUnitIOProc(void *inRefCon
,
1439 AudioUnitRenderActionFlags
*ioActionFlags
,
1440 const AudioTimeStamp
*inTimeStamp
,
1442 UInt32 inNumberFrames
,
1443 AudioBufferList
*ioData
)
1446 WINE_WAVEOUT
*wwo
= (WINE_WAVEOUT
*) inRefCon
;
1449 unsigned int dataNeeded
= ioData
->mBuffers
[0].mDataByteSize
;
1450 unsigned int dataProvided
= 0;
1452 OSSpinLockLock(&wwo
->lock
);
1454 while (dataNeeded
> 0 && wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpPlayPtr
)
1456 unsigned int available
= wwo
->lpPlayPtr
->dwBufferLength
- wwo
->dwPartialOffset
;
1457 unsigned int toCopy
;
1459 if (available
>= dataNeeded
)
1460 toCopy
= dataNeeded
;
1466 memcpy((char*)ioData
->mBuffers
[0].mData
+ dataProvided
,
1467 wwo
->lpPlayPtr
->lpData
+ wwo
->dwPartialOffset
, toCopy
);
1468 wwo
->dwPartialOffset
+= toCopy
;
1469 wwo
->dwPlayedTotal
+= toCopy
;
1470 dataProvided
+= toCopy
;
1471 dataNeeded
-= toCopy
;
1472 available
-= toCopy
;
1477 wodHelper_PlayPtrNext(wwo
);
1482 OSSpinLockUnlock(&wwo
->lock
);
1484 /* We can't provide any more wave data. Fill the rest with silence. */
1488 *ioActionFlags
|= kAudioUnitRenderAction_OutputIsSilence
;
1489 memset((char*)ioData
->mBuffers
[0].mData
+ dataProvided
, 0, dataNeeded
);
1490 dataProvided
+= dataNeeded
;
1494 /* We only fill buffer 0. Set any others that might be requested to 0. */
1495 for (buffer
= 1; buffer
< ioData
->mNumberBuffers
; buffer
++)
1497 memset(ioData
->mBuffers
[buffer
].mData
, 0, ioData
->mBuffers
[buffer
].mDataByteSize
);
1500 if (needNotify
) wodSendNotifyCompletionsMessage(wwo
);
1505 /*======================================================================*
1506 * Low level WAVE IN implementation *
1507 *======================================================================*/
1509 /**************************************************************************
1510 * widNotifyClient [internal]
1512 static DWORD
widNotifyClient(WINE_WAVEIN
* wwi
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
1514 TRACE("wMsg = 0x%04x dwParm1 = %04X dwParam2 = %04X\n", wMsg
, dwParam1
, dwParam2
);
1521 if (wwi
->wFlags
!= DCB_NULL
&&
1522 !DriverCallback(wwi
->waveDesc
.dwCallback
, wwi
->wFlags
,
1523 (HDRVR
)wwi
->waveDesc
.hWave
, wMsg
, wwi
->waveDesc
.dwInstance
,
1524 dwParam1
, dwParam2
))
1526 WARN("can't notify client !\n");
1527 return MMSYSERR_ERROR
;
1531 FIXME("Unknown callback message %u\n", wMsg
);
1532 return MMSYSERR_INVALPARAM
;
1534 return MMSYSERR_NOERROR
;
1538 /**************************************************************************
1539 * widHelper_NotifyCompletions [internal]
1541 static void widHelper_NotifyCompletions(WINE_WAVEIN
* wwi
)
1543 LPWAVEHDR lpWaveHdr
;
1544 LPWAVEHDR lpFirstDoneWaveHdr
= NULL
;
1545 LPWAVEHDR lpLastDoneWaveHdr
= NULL
;
1547 OSSpinLockLock(&wwi
->lock
);
1549 /* First, excise all of the done headers from the queue into
1550 * a free-standing list. */
1552 /* Start from lpQueuePtr and keep notifying until:
1553 * - we hit an unfilled wavehdr
1554 * - we hit the end of the list
1557 lpWaveHdr
= wwi
->lpQueuePtr
;
1559 lpWaveHdr
->dwBytesRecorded
>= lpWaveHdr
->dwBufferLength
;
1560 lpWaveHdr
= lpWaveHdr
->lpNext
1563 if (!lpFirstDoneWaveHdr
)
1564 lpFirstDoneWaveHdr
= lpWaveHdr
;
1565 lpLastDoneWaveHdr
= lpWaveHdr
;
1568 if (lpLastDoneWaveHdr
)
1570 wwi
->lpQueuePtr
= lpLastDoneWaveHdr
->lpNext
;
1571 lpLastDoneWaveHdr
->lpNext
= NULL
;
1574 OSSpinLockUnlock(&wwi
->lock
);
1576 /* Now, send the "done" notification for each header in our list. */
1577 for (lpWaveHdr
= lpFirstDoneWaveHdr
; lpWaveHdr
; lpWaveHdr
= lpWaveHdr
->lpNext
)
1579 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1580 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1582 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
1587 /**************************************************************************
1588 * widGetDevCaps [internal]
1590 static DWORD
widGetDevCaps(WORD wDevID
, LPWAVEINCAPSW lpCaps
, DWORD dwSize
)
1592 TRACE("(%u, %p, %u);\n", wDevID
, lpCaps
, dwSize
);
1594 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
1596 if (wDevID
>= MAX_WAVEINDRV
)
1598 TRACE("MAX_WAVEINDRV reached !\n");
1599 return MMSYSERR_BADDEVICEID
;
1602 memcpy(lpCaps
, &WInDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1603 return MMSYSERR_NOERROR
;
1607 /**************************************************************************
1608 * widHelper_DestroyAudioBufferList [internal]
1609 * Convenience function to dispose of our audio buffers
1611 static void widHelper_DestroyAudioBufferList(AudioBufferList
* list
)
1616 for (i
= 0; i
< list
->mNumberBuffers
; i
++)
1618 if (list
->mBuffers
[i
].mData
)
1619 HeapFree(GetProcessHeap(), 0, list
->mBuffers
[i
].mData
);
1621 HeapFree(GetProcessHeap(), 0, list
);
1626 /**************************************************************************
1627 * widHelper_AllocateAudioBufferList [internal]
1628 * Convenience function to allocate our audio buffers
1630 static AudioBufferList
* widHelper_AllocateAudioBufferList(UInt32 numChannels
, UInt32 size
)
1632 AudioBufferList
* list
;
1635 list
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioBufferList
) + numChannels
* sizeof(AudioBuffer
));
1639 list
->mNumberBuffers
= numChannels
;
1640 for (i
= 0; i
< numChannels
; ++i
)
1642 list
->mBuffers
[i
].mNumberChannels
= 1;
1643 list
->mBuffers
[i
].mDataByteSize
= size
;
1644 list
->mBuffers
[i
].mData
= HeapAlloc(GetProcessHeap(), 0, size
);
1645 if (list
->mBuffers
[i
].mData
== NULL
)
1647 widHelper_DestroyAudioBufferList(list
);
1655 /**************************************************************************
1656 * widOpen [internal]
1658 static DWORD
widOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1662 UInt32 bytesPerFrame
;
1664 TRACE("(%u, %p, %08X);\n", wDevID
, lpDesc
, dwFlags
);
1667 WARN("Invalid Parameter !\n");
1668 return MMSYSERR_INVALPARAM
;
1670 if (wDevID
>= MAX_WAVEINDRV
)
1672 TRACE ("MAX_WAVEINDRV reached !\n");
1673 return MMSYSERR_BADDEVICEID
;
1676 TRACE("Format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
1677 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1678 lpDesc
->lpFormat
->nSamplesPerSec
, lpDesc
->lpFormat
->wBitsPerSample
);
1680 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
1681 lpDesc
->lpFormat
->nChannels
== 0 ||
1682 lpDesc
->lpFormat
->nSamplesPerSec
== 0
1685 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d wBitsPerSample=%d !\n",
1686 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1687 lpDesc
->lpFormat
->nSamplesPerSec
, lpDesc
->lpFormat
->wBitsPerSample
);
1688 return WAVERR_BADFORMAT
;
1691 if (dwFlags
& WAVE_FORMAT_QUERY
)
1693 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1694 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1695 lpDesc
->lpFormat
->nSamplesPerSec
);
1696 return MMSYSERR_NOERROR
;
1699 wwi
= &WInDev
[wDevID
];
1700 if (!OSSpinLockTry(&wwi
->lock
))
1701 return MMSYSERR_ALLOCATED
;
1703 if (wwi
->state
!= WINE_WS_CLOSED
)
1705 OSSpinLockUnlock(&wwi
->lock
);
1706 return MMSYSERR_ALLOCATED
;
1709 wwi
->state
= WINE_WS_STOPPED
;
1710 wwi
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1712 memcpy(&wwi
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
1713 memcpy(&wwi
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
1715 if (wwi
->format
.wBitsPerSample
== 0)
1717 WARN("Resetting zeroed wBitsPerSample\n");
1718 wwi
->format
.wBitsPerSample
= 8 *
1719 (wwi
->format
.wf
.nAvgBytesPerSec
/
1720 wwi
->format
.wf
.nSamplesPerSec
) /
1721 wwi
->format
.wf
.nChannels
;
1724 wwi
->dwTotalRecorded
= 0;
1726 wwi
->trace_on
= TRACE_ON(wave
);
1727 wwi
->warn_on
= WARN_ON(wave
);
1728 wwi
->err_on
= ERR_ON(wave
);
1730 if (!AudioUnit_CreateInputUnit(wwi
, &wwi
->audioUnit
,
1731 wwi
->format
.wf
.nChannels
, wwi
->format
.wf
.nSamplesPerSec
,
1732 wwi
->format
.wBitsPerSample
, &frameCount
))
1734 ERR("AudioUnit_CreateInputUnit failed\n");
1735 OSSpinLockUnlock(&wwi
->lock
);
1736 return MMSYSERR_ERROR
;
1739 /* Allocate our audio buffers */
1740 /* For interleaved audio, we allocate one buffer for all channels. */
1741 bytesPerFrame
= wwi
->format
.wBitsPerSample
* wwi
->format
.wf
.nChannels
/ 8;
1742 wwi
->bufferList
= widHelper_AllocateAudioBufferList(1, wwi
->format
.wf
.nChannels
* frameCount
* bytesPerFrame
);
1743 if (wwi
->bufferList
== NULL
)
1745 ERR("Failed to allocate buffer list\n");
1746 AudioUnitUninitialize(wwi
->audioUnit
);
1747 AudioUnit_CloseAudioUnit(wwi
->audioUnit
);
1748 OSSpinLockUnlock(&wwi
->lock
);
1749 return MMSYSERR_NOMEM
;
1752 OSSpinLockUnlock(&wwi
->lock
);
1754 return widNotifyClient(wwi
, WIM_OPEN
, 0L, 0L);
1758 /**************************************************************************
1759 * widClose [internal]
1761 static DWORD
widClose(WORD wDevID
)
1763 DWORD ret
= MMSYSERR_NOERROR
;
1766 TRACE("(%u);\n", wDevID
);
1768 if (wDevID
>= MAX_WAVEINDRV
)
1770 WARN("bad device ID !\n");
1771 return MMSYSERR_BADDEVICEID
;
1774 wwi
= &WInDev
[wDevID
];
1775 OSSpinLockLock(&wwi
->lock
);
1776 if (wwi
->state
== WINE_WS_CLOSED
)
1778 WARN("Device already closed.\n");
1779 ret
= MMSYSERR_INVALHANDLE
;
1781 else if (wwi
->lpQueuePtr
)
1783 WARN("Buffers in queue.\n");
1784 ret
= WAVERR_STILLPLAYING
;
1788 wwi
->state
= WINE_WS_CLOSED
;
1791 OSSpinLockUnlock(&wwi
->lock
);
1793 if (ret
== MMSYSERR_NOERROR
)
1795 OSStatus err
= AudioUnitUninitialize(wwi
->audioUnit
);
1798 ERR("AudioUnitUninitialize return %c%c%c%c\n", (char) (err
>> 24),
1804 if (!AudioUnit_CloseAudioUnit(wwi
->audioUnit
))
1806 ERR("Can't close AudioUnit\n");
1809 /* Dellocate our audio buffers */
1810 widHelper_DestroyAudioBufferList(wwi
->bufferList
);
1811 wwi
->bufferList
= NULL
;
1813 ret
= widNotifyClient(wwi
, WIM_CLOSE
, 0L, 0L);
1820 /**************************************************************************
1821 * widAddBuffer [internal]
1823 static DWORD
widAddBuffer(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1825 DWORD ret
= MMSYSERR_NOERROR
;
1828 TRACE("(%u, %p, %08X);\n", wDevID
, lpWaveHdr
, dwSize
);
1830 if (wDevID
>= MAX_WAVEINDRV
)
1832 WARN("invalid device ID\n");
1833 return MMSYSERR_INVALHANDLE
;
1835 if (!(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
1837 TRACE("never been prepared !\n");
1838 return WAVERR_UNPREPARED
;
1840 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1842 TRACE("header already in use !\n");
1843 return WAVERR_STILLPLAYING
;
1846 wwi
= &WInDev
[wDevID
];
1847 OSSpinLockLock(&wwi
->lock
);
1849 if (wwi
->state
== WINE_WS_CLOSED
)
1851 WARN("Trying to add buffer to closed device.\n");
1852 ret
= MMSYSERR_INVALHANDLE
;
1858 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
1859 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1860 lpWaveHdr
->dwBytesRecorded
= 0;
1861 lpWaveHdr
->lpNext
= NULL
;
1863 /* insert buffer at end of queue */
1864 for (wh
= &(wwi
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
))
1869 OSSpinLockUnlock(&wwi
->lock
);
1875 /**************************************************************************
1876 * widStart [internal]
1878 static DWORD
widStart(WORD wDevID
)
1880 DWORD ret
= MMSYSERR_NOERROR
;
1883 TRACE("(%u);\n", wDevID
);
1884 if (wDevID
>= MAX_WAVEINDRV
)
1886 WARN("invalid device ID\n");
1887 return MMSYSERR_INVALHANDLE
;
1890 /* The order of the following operations is important since we can't hold
1891 * the mutex while we make an Audio Unit call. Set the PLAYING state
1892 * before starting the Audio Unit. In widStop, the order is reversed.
1893 * This guarantees that we can't get into a situation where the state is
1894 * PLAYING but the Audio Unit isn't running. Although we can be in STOPPED
1895 * state with the Audio Unit still running, that's harmless because the
1896 * input callback will just throw away the sound data.
1898 wwi
= &WInDev
[wDevID
];
1899 OSSpinLockLock(&wwi
->lock
);
1901 if (wwi
->state
== WINE_WS_CLOSED
)
1903 WARN("Trying to start closed device.\n");
1904 ret
= MMSYSERR_INVALHANDLE
;
1907 wwi
->state
= WINE_WS_PLAYING
;
1909 OSSpinLockUnlock(&wwi
->lock
);
1911 if (ret
== MMSYSERR_NOERROR
)
1913 /* Start pulling for audio data */
1914 OSStatus err
= AudioOutputUnitStart(wwi
->audioUnit
);
1916 ERR("Failed to start AU: %08lx\n", err
);
1918 TRACE("Recording started...\n");
1925 /**************************************************************************
1926 * widStop [internal]
1928 static DWORD
widStop(WORD wDevID
)
1930 DWORD ret
= MMSYSERR_NOERROR
;
1932 WAVEHDR
* lpWaveHdr
= NULL
;
1935 TRACE("(%u);\n", wDevID
);
1936 if (wDevID
>= MAX_WAVEINDRV
)
1938 WARN("invalid device ID\n");
1939 return MMSYSERR_INVALHANDLE
;
1942 wwi
= &WInDev
[wDevID
];
1944 /* The order of the following operations is important since we can't hold
1945 * the mutex while we make an Audio Unit call. Stop the Audio Unit before
1946 * setting the STOPPED state. In widStart, the order is reversed. This
1947 * guarantees that we can't get into a situation where the state is
1948 * PLAYING but the Audio Unit isn't running. Although we can be in STOPPED
1949 * state with the Audio Unit still running, that's harmless because the
1950 * input callback will just throw away the sound data.
1952 err
= AudioOutputUnitStop(wwi
->audioUnit
);
1954 WARN("Failed to stop AU: %08lx\n", err
);
1956 TRACE("Recording stopped.\n");
1958 OSSpinLockLock(&wwi
->lock
);
1960 if (wwi
->state
== WINE_WS_CLOSED
)
1962 WARN("Trying to stop closed device.\n");
1963 ret
= MMSYSERR_INVALHANDLE
;
1965 else if (wwi
->state
!= WINE_WS_STOPPED
)
1967 wwi
->state
= WINE_WS_STOPPED
;
1968 /* If there's a buffer in progress, it's done. Remove it from the
1969 * queue so that we can return it to the app, below. */
1970 if (wwi
->lpQueuePtr
)
1972 lpWaveHdr
= wwi
->lpQueuePtr
;
1973 wwi
->lpQueuePtr
= lpWaveHdr
->lpNext
;
1977 OSSpinLockUnlock(&wwi
->lock
);
1981 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
1982 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1983 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
1990 /**************************************************************************
1991 * widReset [internal]
1993 static DWORD
widReset(WORD wDevID
)
1995 DWORD ret
= MMSYSERR_NOERROR
;
1997 WAVEHDR
* lpWaveHdr
= NULL
;
1999 TRACE("(%u);\n", wDevID
);
2000 if (wDevID
>= MAX_WAVEINDRV
)
2002 WARN("invalid device ID\n");
2003 return MMSYSERR_INVALHANDLE
;
2006 wwi
= &WInDev
[wDevID
];
2007 OSSpinLockLock(&wwi
->lock
);
2009 if (wwi
->state
== WINE_WS_CLOSED
)
2011 WARN("Trying to reset a closed device.\n");
2012 ret
= MMSYSERR_INVALHANDLE
;
2016 lpWaveHdr
= wwi
->lpQueuePtr
;
2017 wwi
->lpQueuePtr
= NULL
;
2018 wwi
->state
= WINE_WS_STOPPED
;
2019 wwi
->dwTotalRecorded
= 0;
2022 OSSpinLockUnlock(&wwi
->lock
);
2024 if (ret
== MMSYSERR_NOERROR
)
2026 OSStatus err
= AudioOutputUnitStop(wwi
->audioUnit
);
2028 WARN("Failed to stop AU: %08lx\n", err
);
2030 TRACE("Recording stopped.\n");
2035 WAVEHDR
* lpNext
= lpWaveHdr
->lpNext
;
2037 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
2038 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
2039 widNotifyClient(wwi
, WIM_DATA
, (DWORD
)lpWaveHdr
, 0);
2048 /**************************************************************************
2049 * widGetNumDevs [internal]
2051 static DWORD
widGetNumDevs(void)
2053 return MAX_WAVEINDRV
;
2057 /**************************************************************************
2058 * widDevInterfaceSize [internal]
2060 static DWORD
widDevInterfaceSize(UINT wDevID
, LPDWORD dwParam1
)
2062 TRACE("(%u, %p)\n", wDevID
, dwParam1
);
2064 *dwParam1
= MultiByteToWideChar(CP_ACP
, 0, WInDev
[wDevID
].interface_name
, -1,
2065 NULL
, 0 ) * sizeof(WCHAR
);
2066 return MMSYSERR_NOERROR
;
2070 /**************************************************************************
2071 * widDevInterface [internal]
2073 static DWORD
widDevInterface(UINT wDevID
, PWCHAR dwParam1
, DWORD dwParam2
)
2075 if (dwParam2
>= MultiByteToWideChar(CP_ACP
, 0, WInDev
[wDevID
].interface_name
, -1,
2076 NULL
, 0 ) * sizeof(WCHAR
))
2078 MultiByteToWideChar(CP_ACP
, 0, WInDev
[wDevID
].interface_name
, -1,
2079 dwParam1
, dwParam2
/ sizeof(WCHAR
));
2080 return MMSYSERR_NOERROR
;
2082 return MMSYSERR_INVALPARAM
;
2086 /**************************************************************************
2087 * widMessage (WINECOREAUDIO.6)
2089 DWORD WINAPI
CoreAudio_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2090 DWORD dwParam1
, DWORD dwParam2
)
2092 TRACE("(%u, %04X, %08X, %08X, %08X);\n",
2093 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2101 /* FIXME: Pretend this is supported */
2103 case WIDM_OPEN
: return widOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
2104 case WIDM_CLOSE
: return widClose (wDevID
);
2105 case WIDM_ADDBUFFER
: return widAddBuffer (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
2106 case WIDM_PREPARE
: return MMSYSERR_NOTSUPPORTED
;
2107 case WIDM_UNPREPARE
: return MMSYSERR_NOTSUPPORTED
;
2108 case WIDM_GETDEVCAPS
: return widGetDevCaps (wDevID
, (LPWAVEINCAPSW
)dwParam1
, dwParam2
);
2109 case WIDM_GETNUMDEVS
: return widGetNumDevs ();
2110 case WIDM_RESET
: return widReset (wDevID
);
2111 case WIDM_START
: return widStart (wDevID
);
2112 case WIDM_STOP
: return widStop (wDevID
);
2113 case DRV_QUERYDEVICEINTERFACESIZE
: return widDevInterfaceSize (wDevID
, (LPDWORD
)dwParam1
);
2114 case DRV_QUERYDEVICEINTERFACE
: return widDevInterface (wDevID
, (PWCHAR
)dwParam1
, dwParam2
);
2116 FIXME("unknown message %d!\n", wMsg
);
2119 return MMSYSERR_NOTSUPPORTED
;
2123 OSStatus
CoreAudio_wiAudioUnitIOProc(void *inRefCon
,
2124 AudioUnitRenderActionFlags
*ioActionFlags
,
2125 const AudioTimeStamp
*inTimeStamp
,
2127 UInt32 inNumberFrames
,
2128 AudioBufferList
*ioData
)
2130 WINE_WAVEIN
* wwi
= (WINE_WAVEIN
*)inRefCon
;
2131 OSStatus err
= noErr
;
2132 BOOL needNotify
= FALSE
;
2133 WAVEHDR
* lpStorePtr
;
2134 unsigned int dataToStore
;
2135 unsigned int dataStored
= 0;
2139 fprintf(stderr
, "trace:wave:CoreAudio_wiAudioUnitIOProc (ioActionFlags = %08lx, inTimeStamp = { %f, %llu, %f, %llu, %08lx }, inBusNumber = %lu, inNumberFrames = %lu)\n",
2140 *ioActionFlags
, inTimeStamp
->mSampleTime
, inTimeStamp
->mHostTime
, inTimeStamp
->mRateScalar
, inTimeStamp
->mWordClockTime
, inTimeStamp
->mFlags
, inBusNumber
, inNumberFrames
);
2142 /* Render into audio buffer */
2143 err
= AudioUnitRender(wwi
->audioUnit
, ioActionFlags
, inTimeStamp
, inBusNumber
, inNumberFrames
, wwi
->bufferList
);
2147 fprintf(stderr
, "err:wave:CoreAudio_wiAudioUnitIOProc AudioUnitRender failed with error %li\n", err
);
2151 /* Copy from audio buffer to the wavehdrs */
2152 dataToStore
= wwi
->bufferList
->mBuffers
[0].mDataByteSize
;
2154 OSSpinLockLock(&wwi
->lock
);
2156 lpStorePtr
= wwi
->lpQueuePtr
;
2158 while (dataToStore
> 0 && wwi
->state
== WINE_WS_PLAYING
&& lpStorePtr
)
2160 unsigned int room
= lpStorePtr
->dwBufferLength
- lpStorePtr
->dwBytesRecorded
;
2161 unsigned int toCopy
;
2164 fprintf(stderr
, "trace:wave:CoreAudio_wiAudioUnitIOProc Looking to store %u bytes to wavehdr %p, which has room for %u\n",
2165 dataToStore
, lpStorePtr
, room
);
2167 if (room
>= dataToStore
)
2168 toCopy
= dataToStore
;
2174 memcpy(lpStorePtr
->lpData
+ lpStorePtr
->dwBytesRecorded
,
2175 (char*)wwi
->bufferList
->mBuffers
[0].mData
+ dataStored
, toCopy
);
2176 lpStorePtr
->dwBytesRecorded
+= toCopy
;
2177 wwi
->dwTotalRecorded
+= toCopy
;
2178 dataStored
+= toCopy
;
2179 dataToStore
-= toCopy
;
2185 lpStorePtr
= lpStorePtr
->lpNext
;
2190 OSSpinLockUnlock(&wwi
->lock
);
2192 if (needNotify
) wodSendNotifyInputCompletionsMessage(wwi
);
2198 /**************************************************************************
2199 * widMessage (WINECOREAUDIO.6)
2201 DWORD WINAPI
CoreAudio_widMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2202 DWORD dwParam1
, DWORD dwParam2
)
2204 FIXME("(%u, %04X, %08X, %08X, %08X): CoreAudio support not compiled into wine\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2205 return MMSYSERR_NOTENABLED
;
2208 /**************************************************************************
2209 * wodMessage (WINECOREAUDIO.7)
2211 DWORD WINAPI
CoreAudio_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2212 DWORD dwParam1
, DWORD dwParam2
)
2214 FIXME("(%u, %04X, %08X, %08X, %08X): CoreAudio support not compiled into wine\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2215 return MMSYSERR_NOTENABLED
;