Skip to content
Snippets Groups Projects
Commit 2269b76f authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Kateryna Kostiuk
Browse files

coreaudio: handle the case when no audio device is connected

jami-client-qt#671

Change-Id: If0f2237042c746214cadb33ef8e56c22fe327a37
parent 7634487e
No related branches found
No related tags found
No related merge requests found
...@@ -104,13 +104,6 @@ CoreLayer::initAudioLayerIO(AudioDeviceType stream) ...@@ -104,13 +104,6 @@ CoreLayer::initAudioLayerIO(AudioDeviceType stream)
// 5) Profit... // 5) Profit...
JAMI_DBG("INIT AUDIO IO"); JAMI_DBG("INIT AUDIO IO");
// get capture divice
auto captureList = getDeviceList(true);
AudioDeviceID inputDeviceID = captureList[indexIn_].id_;
// get playback device
auto playbackList = getDeviceList(false);
AudioDeviceID playbackDeviceID = playbackList[indexOut_].id_;
AudioUnitScope outputBus = 0; AudioUnitScope outputBus = 0;
AudioUnitScope inputBus = 1; AudioUnitScope inputBus = 1;
AudioComponentDescription desc = {0}; AudioComponentDescription desc = {0};
...@@ -134,16 +127,26 @@ CoreLayer::initAudioLayerIO(AudioDeviceType stream) ...@@ -134,16 +127,26 @@ CoreLayer::initAudioLayerIO(AudioDeviceType stream)
return; return;
} }
// set capture device AudioDeviceID inputDeviceID;
UInt32 size = sizeof(inputDeviceID); AudioDeviceID playbackDeviceID;
UInt32 size = sizeof(AudioDeviceID);
if (stream == AudioDeviceType::CAPTURE || stream == AudioDeviceType::ALL) {
auto captureList = getDeviceList(true);
bool useFallbackDevice = true;
// try to set the device selected by the user. Otherwise, the default device will be set automatically.
if(indexIn_ < captureList.size()) {
inputDeviceID = captureList[indexIn_].id_;
auto error = AudioUnitSetProperty(ioUnit_, auto error = AudioUnitSetProperty(ioUnit_,
kAudioOutputUnitProperty_CurrentDevice, kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global, kAudioUnitScope_Global,
inputBus, inputBus,
&inputDeviceID, &inputDeviceID,
size); size);
// if failed get default device useFallbackDevice = error != kAudioServicesNoError;
if (error != kAudioServicesNoError) { }
// get a fallback capture device id so we could listen when the device disconnect.
if (useFallbackDevice) {
const AudioObjectPropertyAddress inputInfo = {kAudioHardwarePropertyDefaultInputDevice, const AudioObjectPropertyAddress inputInfo = {kAudioHardwarePropertyDefaultInputDevice,
kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster}; kAudioObjectPropertyElementMaster};
...@@ -153,18 +156,29 @@ CoreLayer::initAudioLayerIO(AudioDeviceType stream) ...@@ -153,18 +156,29 @@ CoreLayer::initAudioLayerIO(AudioDeviceType stream)
NULL, NULL,
&size, &size,
&inputDeviceID); &inputDeviceID);
if (status != kAudioServicesNoError) {
JAMI_ERR() << "failed to set audio input device";
return;
}
}
} }
// set playback device if (stream == AudioDeviceType::PLAYBACK || stream == AudioDeviceType::ALL || stream == AudioDeviceType::RINGTONE) {
size = sizeof(playbackDeviceID); auto playbackList = getDeviceList(false);
error = AudioUnitSetProperty(ioUnit_, auto index = stream == AudioDeviceType::RINGTONE ? indexRing_ : indexOut_;
bool useFallbackDevice = true;
if(index < playbackList.size()) {
playbackDeviceID = playbackList[index].id_;
auto error = AudioUnitSetProperty(ioUnit_,
kAudioOutputUnitProperty_CurrentDevice, kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global, kAudioUnitScope_Global,
outputBus, outputBus,
&playbackDeviceID, &playbackDeviceID,
size); size);
// if failed get default device useFallbackDevice = error != kAudioServicesNoError;
if (error != kAudioServicesNoError) { }
// get fallback output device id.
if (useFallbackDevice) {
const AudioObjectPropertyAddress outputInfo = {kAudioHardwarePropertyDefaultOutputDevice, const AudioObjectPropertyAddress outputInfo = {kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster}; kAudioObjectPropertyElementMaster};
...@@ -174,6 +188,11 @@ CoreLayer::initAudioLayerIO(AudioDeviceType stream) ...@@ -174,6 +188,11 @@ CoreLayer::initAudioLayerIO(AudioDeviceType stream)
NULL, NULL,
&size, &size,
&playbackDeviceID); &playbackDeviceID);
if (status != kAudioServicesNoError) {
JAMI_ERR() << "failed to set audio output device";
return;
}
}
} }
// add listener for detecting when devices are removed // add listener for detecting when devices are removed
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment