Audio Manager API Sample Reference for voice session volume/mute control
This Section demonstrates how to use the Audio Manager API for voice session volume/mute control.
1. Get the AudioFactory instance
auto &audioFactory = audioFactory::getInstance();
2. Get the AudioManager object and check for audio subsystem Readiness
std::promise<ServiceStatus> prom{};
audioManager = audioFactory.getAudioManager([&prom](ServiceStatus serviceStatus) {
prom.set_value(serviceStatus);
});
if (!audioManager) {
std::cout << "Failed to get AudioManager object" << std::endl;
return;
}
ServiceStatus managerStatus = audioManager->getServiceStatus();
if (managerStatus != ServiceStatus::SERVICE_AVAILABLE) {
std::cout << "\nAudio subsystem is not ready, Please wait ..." << std::endl;
managerStatus = prom.get_future().get();
}
if (managerStatus == ServiceStatus::SERVICE_AVAILABLE) {
std::cout << "Audio Subsytem is Ready << std::endl;
} else {
std::cout << "ERROR - Unable to initialize audio subsystem" << std::endl;
return;
}
3. Create an Audio Stream (Voice Call Session)
void createStreamCallback(std::shared_ptr<IAudioStream> &stream, ErrorCode error)
{
if (error != ErrorCode::SUCCESS) {
std::cout << "createStream() returned with error " << static_cast<unsigned int>(error)
<< std::endl;
return;
}
std::cout << "createStream() succeeded." << std::endl;
audioVoiceStream = std::dynamic_pointer_cast<IAudioVoiceStream>(stream);
}
StreamConfig config;
config.type = StreamType::VOICE_CALL;
config.slotId = DEFAULT_SLOT_ID;
config.sampleRate = 16000;
config.format = AudioFormat::PCM_16BIT_SIGNED;
config.channelTypeMask = ChannelType::LEFT;
config.deviceTypes.emplace_back(DeviceType::DEVICE_TYPE_SPEAKER);
status = audioManager->createStream(config, createStreamCallback);
4. Start Created Audio Stream (Voice Call Session)
void startAudioCallback(ErrorCode error)
{
if (error != ErrorCode::SUCCESS) {
std::cout << "startAudio() returned with error " << static_cast<unsigned int>(error)
<< std::endl;
return;
}
std::cout << "startAudio() succeeded." << std::endl;
}
status = audioVoiceStream->startAudio(startAudioCallback);
5. Set volume on Started Audio Stream (Voice Call Session) for specified direction
void setStreamVolumeCallback(ErrorCode error)
{
if (error != ErrorCode::SUCCESS) {
std::cout << "setVolume() returned with error " << static_cast<unsigned int>(error)
<< std::endl;
return;
}
std::cout << "setVolume() succeeded." << std::endl;
}
StreamVolume streamVol;
ChannelVolume channelVol;
streamVol.dir = StreamDirection::RX;
channelVol.channelType = ChannelType::LEFT;
channelVol.vol = 0.5;
streamVol.volume.emplace_back(channelVol);
status = audioVoiceStream->setVolume(streamVol, setStreamVolumeCallback);
6. Get volume on Started Audio Stream (Voice Call Session)
void getStreamVolumeCallback(StreamVolume volume, ErrorCode error)
{
if (error != ErrorCode::SUCCESS) {
std::cout << "getVolume() returned with error " << static_cast<unsigned int>(error)
<< std::endl;
return;
}
std::cout << "Volume direction: " << static_cast<uint32_t>(volume.dir) << std::endl;
int i = 0;
for (auto channel_volume : volume.volume) {
std::cout << "ChannelVolume [" << i << "] channel type: "
<< static_cast<uint32_t>(channel_volume.channelType) << ", " << "volume: "
<< channel_volume.vol << std::endl;
}
}
status = audioVoiceStream->getVolume(StreamDirection::RX, getStreamVolumeCallback);
7. Set Mute on Started Audio Stream (Voice Call Session) for specified direction
void setStreamMuteCallback(ErrorCode error)
{
if (error != ErrorCode::SUCCESS) {
std::cout << "setMute() returned with error " << static_cast<unsigned int>(error)
<< std::endl;
return;
}
std::cout << "setMute() succeeded." << std::endl;
}
StreamMute mute;
mute.dir = StreamDirection::TX;
mute.enable = true;
status = audioVoiceStream->setMute(mute, setStreamMuteCallback);
8 Get Mute on Started Audio Stream (Voice Call Session) for specified direction
void getStreamMuteCallback(StreamMute mute, ErrorCode error)
{
if (error != ErrorCode::SUCCESS) {
std::cout << "getMute() returned with error " << static_cast<unsigned int>(error)
<< std::endl;
return;
}
std::cout << "Mute enable: " << mute.enable << ", direction: "
<< static_cast<uint32_t>(mute.dir) << std::endl;
}
status = audioVoiceStream->getMute(StreamDirection::TX, getStreamMuteCallback);
9. Stop Created Audio Stream (Voice Call Session)
void stopAudioCallback(ErrorCode error)
{
if (error != ErrorCode::SUCCESS) {
std::cout << "stopAudio() returned with error " << static_cast<unsigned int>(error)
<< std::endl;
return;
}
std::cout << "stopAudio() succeeded." << std::endl;
}
status = audioVoiceStream->stopAudio(stopAudioCallback);
10. Delete an Audio Stream (Voice Call Session), which was created earlier
void deleteStreamCallback(ErrorCode error) {
if (error != ErrorCode::SUCCESS) {
std::cout << "deleteStream() returned with error " << static_cast<unsigned int>(error)
<< std::endl;
return;
}
std::cout << "deleteStream() succeeded." << std::endl;
audioVoiceStream.reset();
}
status = audioManager->deleteStream(std::dynamic_pointer_cast<IAudioStream>(audioVoiceStream),
deleteStreamCallback);