diff --git a/frameworks/native/audiodefinitions/include/audio_stream_enum.h b/frameworks/native/audiodefinitions/include/audio_stream_enum.h index f10d0dd78a7ba45339fccae7c0b81505637d67fd..d848812e22d74bd63d2ef0b2221af0ffb8e806e4 100644 --- a/frameworks/native/audiodefinitions/include/audio_stream_enum.h +++ b/frameworks/native/audiodefinitions/include/audio_stream_enum.h @@ -46,7 +46,7 @@ enum AudioFlag : uint32_t { enum AudioStreamStatus : uint32_t { STREAM_STATUS_NEW = 0, - STREAM_STATUS_STARTTING, + STREAM_STATUS_STARTED, STREAM_STATUS_PAUSED, STREAM_STATUS_STOPPED, STREAM_STATUS_RELEASED, diff --git a/services/audio_policy/services/core/include/audio_core_service.h b/services/audio_policy/services/core/include/audio_core_service.h index a9990113b519d1df1f5f561aa9492e892e16fa9e..afb4439f1ffef8b0a5f314b820cff9a42d1b3b1d 100644 --- a/services/audio_policy/services/core/include/audio_core_service.h +++ b/services/audio_policy/services/core/include/audio_core_service.h @@ -279,11 +279,14 @@ private: void ProcessInputPipeNew(std::shared_ptr pipeInfo, uint32_t &flag); void ProcessInputPipeUpdate(std::shared_ptr pipeInfo, uint32_t &flag); void RemoveUnusedPipe(); + void MoveStreamSink(std::shared_ptr streamDesc, + std::shared_ptr pipeInfo, const AudioStreamDeviceChangeReasonExt reason); void MoveToNewOutputDevice(std::shared_ptr streamDesc, std::shared_ptr pipeInfo, const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN); int32_t MoveToRemoteOutputDevice( std::vector sinkInputIds, std::shared_ptr remoteDeviceDescriptor); + void MoveStreamSource(std::shared_ptr streamDesc); void MoveToNewInputDevice(std::shared_ptr streamDesc); int32_t MoveToLocalInputDevice( std::vector sourceOutputs, std::shared_ptr localDeviceDescriptor); @@ -361,6 +364,8 @@ private: void MutePrimaryOrOffloadSink(const std::string &sinkName, int64_t muteTime); void MuteSinkPortLogic(const std::string &oldSinkName, const std::string &newSinkName, AudioStreamDeviceChangeReasonExt reason); + int32_t ActivateOutputDevice(std::shared_ptr &deviceDesc); + int32_t ActivateInputDevice(std::shared_ptr &streamDesc); private: std::shared_ptr eventEntry_; std::shared_ptr audioPolicyServerHandler_ = nullptr; diff --git a/services/audio_policy/services/core/src/audio_core_service.cpp b/services/audio_policy/services/core/src/audio_core_service.cpp index 7575b8d3104abc85d896b385513c182b5b863a13..8ead9328fabc42f88e87359ef37a4b94549ddf4d 100644 --- a/services/audio_policy/services/core/src/audio_core_service.cpp +++ b/services/audio_policy/services/core/src/audio_core_service.cpp @@ -148,17 +148,7 @@ int32_t AudioCoreService::CreateRendererClient( for (auto device : streamDesc->newDeviceDescs_) { AUDIO_INFO_LOG("Device type %{public}d", device->deviceType_); } - { - // handle a2dp - std::string encryptMacAddr = - GetEncryptAddr(streamDesc->newDeviceDescs_.front()->macAddress_); - int32_t bluetoothFetchResult = BluetoothDeviceFetchOutputHandle(streamDesc->newDeviceDescs_.front(), - AudioStreamDeviceChangeReason::UNKNOWN, encryptMacAddr); - if (bluetoothFetchResult == BLUETOOTH_FETCH_RESULT_CONTINUE || - bluetoothFetchResult == BLUETOOTH_FETCH_RESULT_ERROR) { - return ERROR; - } - } + SetPlaybackStreamFlag(streamDesc); AUDIO_INFO_LOG("Will use audio flag: %{public}u", streamDesc->audioFlag_); @@ -182,13 +172,6 @@ int32_t AudioCoreService::CreateCapturerClient( streamDesc->newDeviceDescs_.push_back(inputDeviceDesc); AUDIO_INFO_LOG("New stream device type %{public}d", inputDeviceDesc->deviceType_); - { - // handle a2dp - if (streamDesc->newDeviceDescs_[0]->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { - BluetoothScoFetch(streamDesc); - } - } - SetRecordStreamFlag(streamDesc); AUDIO_INFO_LOG("Will use audio flag: %{public}u", streamDesc->audioFlag_); @@ -346,6 +329,8 @@ int32_t AudioCoreService::StartClient(uint32_t sessionId) } if (streamDesc->audioMode_ == AUDIO_MODE_PLAYBACK) { + int32_t outputRet = ActivateOutputDevice(streamDesc->newDeviceDescs_.front()); + CHECK_AND_RETURN_RET_LOG(outputRet == SUCCESS, outputRet, "Activate output device failed"); std::vector> activeDevices; if (streamDesc->newDeviceDescs_.size() == 2) { // 2 for dual use std::string firstSinkName = @@ -373,6 +358,8 @@ int32_t AudioCoreService::StartClient(uint32_t sessionId) audioActiveDevice_.UpdateActiveDevicesRoute(activeDevices); } } else { + int32_t inputRet = ActivateInputDevice(streamDesc); + CHECK_AND_RETURN_RET_LOG(inputRet == SUCCESS, inputRet, "Activate input device failed"); audioActiveDevice_.UpdateActiveDeviceRoute( streamDesc->newDeviceDescs_[0]->deviceType_, DeviceFlag::INPUT_DEVICES_FLAG); streamCollector_.UpdateCapturerDeviceInfo(streamDesc->newDeviceDescs_.front()); @@ -384,32 +371,12 @@ int32_t AudioCoreService::StartClient(uint32_t sessionId) int32_t AudioCoreService::PauseClient(uint32_t sessionId) { - std::shared_ptr streamDesc = pipeManager_->GetStreamDescById(sessionId); - if (streamDesc->audioMode_ == AUDIO_MODE_PLAYBACK) { - audioActiveDevice_.UpdateActiveDeviceRoute( - streamDesc->newDeviceDescs_[0]->deviceType_, DeviceFlag::OUTPUT_DEVICES_FLAG); - } else { - audioActiveDevice_.UpdateActiveDeviceRoute( - streamDesc->newDeviceDescs_[0]->deviceType_, DeviceFlag::INPUT_DEVICES_FLAG); - } - audioActiveDevice_.UpdateActiveDeviceRoute(DEVICE_TYPE_SPEAKER, DeviceFlag::OUTPUT_DEVICES_FLAG); - pipeManager_->PauseClient(sessionId); return SUCCESS; } int32_t AudioCoreService::StopClient(uint32_t sessionId) { - std::shared_ptr streamDesc = pipeManager_->GetStreamDescById(sessionId); - if (streamDesc->audioMode_ == AUDIO_MODE_PLAYBACK) { - audioActiveDevice_.UpdateActiveDeviceRoute( - streamDesc->newDeviceDescs_[0]->deviceType_, DeviceFlag::OUTPUT_DEVICES_FLAG); - } else { - audioActiveDevice_.UpdateActiveDeviceRoute( - streamDesc->newDeviceDescs_[0]->deviceType_, DeviceFlag::INPUT_DEVICES_FLAG); - } - audioActiveDevice_.UpdateActiveDeviceRoute(DEVICE_TYPE_SPEAKER, DeviceFlag::OUTPUT_DEVICES_FLAG); - pipeManager_->StopClient(sessionId); return SUCCESS; } @@ -1002,32 +969,25 @@ int32_t AudioCoreService::FetchOutputDeviceAndRoute(const AudioStreamDeviceChang streamDesc->oldDeviceDescs_ = streamDesc->newDeviceDescs_; streamDesc->newDeviceDescs_ = audioRouterCenter_.FetchOutputDevices(streamDesc->rendererInfo_.streamUsage, GetRealUid(streamDesc)); - AUDIO_INFO_LOG("DeviceType %{public}d", streamDesc->newDeviceDescs_[0]->deviceType_); + AUDIO_INFO_LOG("DeviceType %{public}d, state: %{public}u", + streamDesc->newDeviceDescs_[0]->deviceType_, streamDesc->streamStatus_); if (HandleDeviceChangeForFetchOutputDevice(streamDesc) == ERR_NEED_NOT_SWITCH_DEVICE && !Util::IsRingerOrAlarmerStreamUsage(streamDesc->rendererInfo_.streamUsage)) { continue; } - - MuteSinkForSwitchBluetoothDevice(streamDesc, reason); - MuteSinkForSwitchDistributedDevice(streamDesc, reason); - // handle a2dp - std::string encryptMacAddr = GetEncryptAddr(streamDesc->newDeviceDescs_.front()->macAddress_); - int32_t bluetoothFetchResult = - BluetoothDeviceFetchOutputHandle(streamDesc->newDeviceDescs_.front(), reason, encryptMacAddr); - if (bluetoothFetchResult == BLUETOOTH_FETCH_RESULT_CONTINUE || - bluetoothFetchResult == BLUETOOTH_FETCH_RESULT_ERROR) { - continue; - } - if (streamDesc->newDeviceDescs_.front()->deviceType_ == DEVICE_TYPE_USB_ARM_HEADSET) { - audioEcManager_.ActivateArmDevice( - streamDesc->newDeviceDescs_.front()->macAddress_, streamDesc->newDeviceDescs_.front()->deviceRole_); - } SetPlaybackStreamFlag(streamDesc); - if (needUpdateActiveDevice) { - isUpdateActiveDevice = UpdateOutputDevice(streamDesc->newDeviceDescs_.front(), GetRealUid(streamDesc), - reason); - needUpdateActiveDevice = !isUpdateActiveDevice; + + if (streamDesc->streamStatus_ == STREAM_STATUS_STARTED) { + MuteSinkForSwitchBluetoothDevice(streamDesc, reason); + MuteSinkForSwitchDistributedDevice(streamDesc, reason); + int32_t outputRet = ActivateOutputDevice(streamDesc->newDeviceDescs_.front()); + CHECK_AND_CONTINUE_LOG(outputRet == SUCCESS, "Activate output device failed"); + if (needUpdateActiveDevice) { + isUpdateActiveDevice = UpdateOutputDevice(streamDesc->newDeviceDescs_.front(), GetRealUid(streamDesc), + reason); + needUpdateActiveDevice = !isUpdateActiveDevice; + } } AUDIO_INFO_LOG("Will use audio flag: %{public}u", streamDesc->audioFlag_); } diff --git a/services/audio_policy/services/core/src/audio_core_service_entry.cpp b/services/audio_policy/services/core/src/audio_core_service_entry.cpp index bde1280149e4b729c167c1b0dd646ca003ff5002..04978f78ceda2e2e444e2b78731a3bad51e0d3c9 100644 --- a/services/audio_policy/services/core/src/audio_core_service_entry.cpp +++ b/services/audio_policy/services/core/src/audio_core_service_entry.cpp @@ -62,6 +62,7 @@ int32_t AudioCoreService::EventEntry::CreateCapturerClient( int32_t AudioCoreService::EventEntry::UpdateSessionOperation(uint32_t sessionId, SessionOperation operation) { + AUDIO_INFO_LOG("Session: %{public}u, operation: %{public}u", sessionId, operation); std::lock_guard lock(eventMutex_); switch (operation) { case SESSION_OPERATION_START: diff --git a/services/audio_policy/services/core/src/audio_core_service_private.cpp b/services/audio_policy/services/core/src/audio_core_service_private.cpp index 2c72d686acbd6ac7e92193f4310cc7d3cb4842ef..2c19a5fc4ef9fd1146b74f04a86fb270dbf38ddc 100644 --- a/services/audio_policy/services/core/src/audio_core_service_private.cpp +++ b/services/audio_policy/services/core/src/audio_core_service_private.cpp @@ -534,7 +534,11 @@ void AudioCoreService::ProcessOutputPipeUpdate(std::shared_ptr pi break; case AUDIO_STREAM_ACTION_DEFAULT: case AUDIO_STREAM_ACTION_MOVE: - MoveToNewOutputDevice(desc, pipeInfo, reason); + if (desc->streamStatus_ != STREAM_STATUS_STARTED) { + MoveStreamSink(desc, pipeInfo, reason); + } else { + MoveToNewOutputDevice(desc, pipeInfo, reason); + } break; case AUDIO_STREAM_ACTION_RECREATE: TriggerRecreateRendererStreamCallback(desc->appInfo_.appPid, @@ -597,7 +601,11 @@ void AudioCoreService::ProcessInputPipeNew(std::shared_ptr pipeIn break; case AUDIO_STREAM_ACTION_DEFAULT: case AUDIO_STREAM_ACTION_MOVE: - MoveToNewInputDevice(desc); + if (desc->streamStatus_ != STREAM_STATUS_STARTED) { + MoveStreamSource(desc); + } else { + MoveToNewInputDevice(desc); + } break; case AUDIO_STREAM_ACTION_RECREATE: TriggerRecreateCapturerStreamCallback(desc->appInfo_.appPid, @@ -713,6 +721,27 @@ void AudioCoreService::OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop audioDeviceStatus_.OnDeviceStatusUpdated(statusInfo, isStop); } +void AudioCoreService::MoveStreamSink(std::shared_ptr streamDesc, + std::shared_ptr pipeInfo, const AudioStreamDeviceChangeReasonExt reason) +{ + AUDIO_INFO_LOG("session %{public}u", streamDesc->sessionId_); + Trace trace("AudioCoreService::MoveStreamSink"); + DeviceType oldDeviceType = DEVICE_TYPE_NONE; + std::shared_ptr newDeviceDesc = streamDesc->newDeviceDescs_.front(); + AUDIO_INFO_LOG("Move sink, [][]->[%{public}d][%{public}s], reason %{public}d", newDeviceDesc->deviceType_, + GetEncryptAddr(newDeviceDesc->macAddress_).c_str(), static_cast(reason)); + + std::vector sinkInputs; + audioPolicyManager_.GetAllSinkInputs(sinkInputs); + std::vector targetSinkInputs = audioOffloadStream_.FilterSinkInputs(streamDesc->sessionId_, sinkInputs); + + auto ret = (newDeviceDesc->networkId_ == LOCAL_NETWORK_ID) + ? MoveToLocalOutputDevice(targetSinkInputs, pipeInfo, newDeviceDesc) + : MoveToRemoteOutputDevice(targetSinkInputs, newDeviceDesc); + CHECK_AND_RETURN_LOG(ret == SUCCESS, "Move sink input %{public}d to device %{public}d failed!", + streamDesc->sessionId_, newDeviceDesc->deviceType_); +} + void AudioCoreService::MoveToNewOutputDevice(std::shared_ptr streamDesc, std::shared_ptr pipeInfo, const AudioStreamDeviceChangeReasonExt reason) { @@ -838,6 +867,23 @@ int32_t AudioCoreService::MoveToRemoteOutputDevice(std::vector sinkIn return SUCCESS; } +void AudioCoreService::MoveStreamSource(std::shared_ptr streamDesc) +{ + Trace trace("AudioCoreService::MoveStreamSource"); + std::vector targetSourceOutputs = FilterSourceOutputs(streamDesc->sessionId_); + + AUDIO_INFO_LOG("Move session %{public}u to [%{public}d][%{public}s]", + streamDesc->sessionId_, streamDesc->newDeviceDescs_.front()->deviceType_, + GetEncryptAddr(streamDesc->newDeviceDescs_.front()->macAddress_).c_str()); + + // MoveSourceOuputByIndexName + auto ret = (streamDesc->newDeviceDescs_.front()->networkId_ == LOCAL_NETWORK_ID) + ? MoveToLocalInputDevice(targetSourceOutputs, streamDesc->newDeviceDescs_.front()) + : MoveToRemoteInputDevice(targetSourceOutputs, streamDesc->newDeviceDescs_.front()); + CHECK_AND_RETURN_LOG((ret == SUCCESS), "Move source output %{public}d to device %{public}d failed!", + streamDesc->sessionId_, streamDesc->newDeviceDescs_.front()->deviceType_); +} + void AudioCoreService::MoveToNewInputDevice(std::shared_ptr streamDesc) { Trace trace("AudioCoreService::MoveToNewInputDevice"); @@ -1823,5 +1869,28 @@ void AudioCoreService::MuteSinkPortLogic(const std::string &oldSinkName, const s audioIOHandleMap_.MuteSinkPort(newSinkName, NEW_DEVICE_REMOTE_CAST_AVALIABLE_MUTE_MS, true); } } + +int32_t AudioCoreService::ActivateOutputDevice(std::shared_ptr &deviceDesc) +{ + CHECK_AND_RETURN_RET_LOG(deviceDesc != nullptr, ERR_INVALID_PARAM, "Device desc is nullptr"); + std::string encryptMacAddr = GetEncryptAddr(deviceDesc->macAddress_); + int32_t bluetoothFetchResult = BluetoothDeviceFetchOutputHandle(deviceDesc, + AudioStreamDeviceChangeReason::UNKNOWN, encryptMacAddr); + CHECK_AND_RETURN_RET(bluetoothFetchResult == BLUETOOTH_FETCH_RESULT_DEFAULT, ERR_OPERATION_FAILED); + if (deviceDesc->deviceType_ == DEVICE_TYPE_USB_ARM_HEADSET) { + audioEcManager_.ActivateArmDevice(deviceDesc->macAddress_, deviceDesc->deviceRole_); + } + return SUCCESS; +} + +int32_t AudioCoreService::ActivateInputDevice(std::shared_ptr &streamDesc) +{ + CHECK_AND_RETURN_RET_LOG(streamDesc != nullptr && streamDesc->newDeviceDescs_.size() > 0, + ERR_INVALID_PARAM, "Invalid stream desc"); + if (streamDesc->newDeviceDescs_[0]->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { + BluetoothScoFetch(streamDesc); + } + return SUCCESS; +} } } diff --git a/services/audio_policy/services/pipe/src/audio_pipe_manager.cpp b/services/audio_policy/services/pipe/src/audio_pipe_manager.cpp index 1aaa8ddfd0fbca364784d19767c1d11223fc0f11..7bb6a57e1f8da83f3455cadd0a1ab8c6dee103ed 100644 --- a/services/audio_policy/services/pipe/src/audio_pipe_manager.cpp +++ b/services/audio_policy/services/pipe/src/audio_pipe_manager.cpp @@ -95,13 +95,15 @@ void AudioPipeManager::StartClient(uint32_t sessionId) { std::unique_lock pLock(pipeListLock_); std::shared_ptr streamDesc = GetStreamDescByIdInner(sessionId); - streamDesc->streamStatus_ = STREAM_STATUS_STARTTING; + CHECK_AND_RETURN_LOG(streamDesc != nullptr, "StreamDesc is nullptr"); + streamDesc->streamStatus_ = STREAM_STATUS_STARTED; } void AudioPipeManager::PauseClient(uint32_t sessionId) { std::unique_lock pLock(pipeListLock_); std::shared_ptr streamDesc = GetStreamDescByIdInner(sessionId); + CHECK_AND_RETURN_LOG(streamDesc != nullptr, "StreamDesc is nullptr"); streamDesc->streamStatus_ = STREAM_STATUS_PAUSED; } @@ -109,6 +111,7 @@ void AudioPipeManager::StopClient(uint32_t sessionId) { std::unique_lock pLock(pipeListLock_); std::shared_ptr streamDesc = GetStreamDescByIdInner(sessionId); + CHECK_AND_RETURN_LOG(streamDesc != nullptr, "StreamDesc is nullptr"); streamDesc->streamStatus_ = STREAM_STATUS_STOPPED; } diff --git a/services/audio_service/server/src/audio_process_in_server.cpp b/services/audio_service/server/src/audio_process_in_server.cpp index 856b5cec1883050ced22c7ec22f7a6b9f3defdcf..58424bd4451185a492fe538d255e7ff6b893cdea 100644 --- a/services/audio_service/server/src/audio_process_in_server.cpp +++ b/services/audio_service/server/src/audio_process_in_server.cpp @@ -296,6 +296,7 @@ int32_t AudioProcessInServer::Pause(bool isFlush) recorderDfx_->WriteDfxStopMsg(sessionId_, CAPTURER_STAGE_PAUSE_OK, GetLastAudioDuration(), processConfig_); } + CoreServiceHandler::GetInstance().UpdateSessionOperation(sessionId_, SESSION_OPERATION_PAUSE); AUDIO_PRERELEASE_LOGI("Pause in server success!"); return SUCCESS; @@ -351,6 +352,7 @@ int32_t AudioProcessInServer::Stop() recorderDfx_->WriteDfxStopMsg(sessionId_, CAPTURER_STAGE_STOP_OK, GetLastAudioDuration(), processConfig_); } + CoreServiceHandler::GetInstance().UpdateSessionOperation(sessionId_, SESSION_OPERATION_STOP); AUDIO_INFO_LOG("Stop in server success!"); return SUCCESS; diff --git a/services/audio_service/server/src/capturer_in_server.cpp b/services/audio_service/server/src/capturer_in_server.cpp index 52c2ec79db0870a839430054eb19dce107a049ab..f06d31ad1cf3ad94814374ba8b5764154d5cbc60 100644 --- a/services/audio_service/server/src/capturer_in_server.cpp +++ b/services/audio_service/server/src/capturer_in_server.cpp @@ -429,6 +429,7 @@ int32_t CapturerInServer::Pause() status_ = I_STATUS_PAUSING; int ret = stream_->Pause(); CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Pause stream failed, reason: %{public}d", ret); + CoreServiceHandler::GetInstance().UpdateSessionOperation(streamIndex_, SESSION_OPERATION_PAUSE); return SUCCESS; } @@ -488,6 +489,7 @@ int32_t CapturerInServer::Stop() int ret = stream_->Stop(); CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Stop stream failed, reason: %{public}d", ret); + CoreServiceHandler::GetInstance().UpdateSessionOperation(streamIndex_, SESSION_OPERATION_STOP); return SUCCESS; } diff --git a/services/audio_service/server/src/renderer_in_server.cpp b/services/audio_service/server/src/renderer_in_server.cpp index d216670aa1489d533d95daab5042f7e6368c3815..487ee443b1dee9b909f6e79d4f6f0b9316ea1294 100644 --- a/services/audio_service/server/src/renderer_in_server.cpp +++ b/services/audio_service/server/src/renderer_in_server.cpp @@ -864,6 +864,7 @@ int32_t RendererInServer::Pause() } } CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Pause stream failed, reason: %{public}d", ret); + CoreServiceHandler::GetInstance().UpdateSessionOperation(streamIndex_, SESSION_OPERATION_PAUSE); return SUCCESS; } @@ -1013,6 +1014,7 @@ int32_t RendererInServer::Stop() } } CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Stop stream failed, reason: %{public}d", ret); + CoreServiceHandler::GetInstance().UpdateSessionOperation(streamIndex_, SESSION_OPERATION_STOP); return SUCCESS; }