From fbbbc44353faca74f8c27dc8d9ec2e21e5457b14 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Wed, 24 Jul 2024 20:32:16 +0800 Subject: [PATCH 01/23] =?UTF-8?q?start=E7=AD=89=E5=BE=85=E8=93=9D=E7=89=99?= =?UTF-8?q?offload=E9=80=9A=E8=B7=AF=E5=BB=BA=E7=AB=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../audio_bluetooth_manager.cpp | 11 ++++ .../bluetoothclient/audio_bluetooth_manager.h | 2 + .../include/service/audio_policy_service.h | 43 ++++++++++++++ .../src/service/audio_policy_service.cpp | 59 +++++++++++++++---- .../client/include/audio_manager_base.h | 7 +++ .../client/include/audio_manager_proxy.h | 1 + .../include/pulseaudio_ipc_interface_code.h | 3 +- .../include/renderer_in_client_private.h | 3 + .../client/src/audio_manager_proxy.cpp | 16 +++++ .../client/src/renderer_in_client.cpp | 28 ++++++++- .../server/include/audio_server.h | 2 + .../server/include/audio_service.h | 1 + .../audio_service/server/include/i_stream.h | 2 + .../server/include/renderer_in_server.h | 2 + .../server/src/audio_manager_stub.cpp | 9 +++ .../audio_service/server/src/audio_server.cpp | 15 +++++ .../server/src/audio_service.cpp | 9 +++ .../server/src/renderer_in_server.cpp | 18 ++++++ 18 files changed, 215 insertions(+), 16 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index 92eb6b2a5b..792a10a049 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -38,6 +38,7 @@ AudioScene AudioHfpManager::scene_ = AUDIO_SCENE_DEFAULT; AudioScene AudioHfpManager::sceneFromPolicy_ = AUDIO_SCENE_DEFAULT; OHOS::Bluetooth::ScoCategory AudioHfpManager::scoCategory = OHOS::Bluetooth::ScoCategory::SCO_DEFAULT; BluetoothRemoteDevice AudioHfpManager::activeHfpDevice_; +std::vector AudioA2dpManager::stateChangedListeners_; std::mutex g_activehfpDeviceLock; std::mutex g_audioSceneLock; std::mutex g_hfpInstanceLock; @@ -211,6 +212,13 @@ int32_t AudioA2dpManager::OffloadStopPlaying(const std::vector &session return a2dpInstance_->OffloadStopPlaying(activeA2dpDevice_, sessionsID); } +int32_t AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(AudioA2dpPlayingStateChangedListener *listener) +{ + stateChangedListeners_.push_back(listener); + AUDIO_INFO_LOG("CXX: listener pushed."); + return SUCCESS; +} + void AudioA2dpManager::CheckA2dpDeviceReconnect() { if (a2dpInstance_ == nullptr) { @@ -262,6 +270,9 @@ void AudioA2dpListener::OnConfigurationChanged(const BluetoothRemoteDevice &devi void AudioA2dpListener::OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error) { AUDIO_INFO_LOG("OnPlayingStatusChanged, state: %{public}d, error: %{public}d", playingState, error); + for (auto listener : AudioA2dpManager::stateChangedListeners_) { + listener->OnA2dpPlayingStateChanged(device.GetDeviceAddr(), playingState, error); + } } void AudioA2dpListener::OnMediaStackChanged(const BluetoothRemoteDevice &device, int action) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h index 2493a3047d..f5cf455026 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -58,6 +58,8 @@ public: static int32_t A2dpOffloadSessionRequest(const std::vector &info); static int32_t OffloadStartPlaying(const std::vector &sessionsID); static int32_t OffloadStopPlaying(const std::vector &sessionsID); + static int32_t RegisterA2dpPlayingStateChangedListener(); + static std::vector stateChangedListeners_; static void SetConnectionState(int state) { diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 510ca6f056..b2dac6c614 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -63,6 +63,15 @@ namespace OHOS { namespace AudioStandard { +enum A2dpOffloadConnectionState : int32_t { + CONNECTION_STATUS_DISCONNECTED = 0, + CONNECTION_STATUS_CONNECTING = 1, + CONNECTION_STATUS_CONNECTED = 2, + CONNECTION_STATUS_DISCONNECTING = 3, + CONNECTION_STATUS_TIMEOUT = 4, +}; + +class AudioA2dpOffloadManager; class AudioPolicyService : public IPortObserver, public IDeviceStatusObserver, public IAudioAccessibilityConfigObserver, public IPolicyProvider { @@ -527,6 +536,9 @@ public: AudioScene GetLastAudioScene() const; + void FetchStreamForA2dpOffload(bool requireReset); + + void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state); private: AudioPolicyService() :audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()), @@ -964,6 +976,8 @@ private: void ResetOffloadModeOnSpatializationChanged(std::vector &allSessions); + bool IsA2dpOffloadConnected(); + bool isUpdateRouteSupported_ = true; bool isCurrentRemoteRenderer = false; bool remoteCapturerSwitch_ = false; @@ -1129,6 +1143,35 @@ private: std::condition_variable offloadCloseCondition_; bool ringerModeMute_ = true; + AudioA2dpOffloadManager *audioA2dpOffloadManager_ = nullptr; +}; + +class AudioA2dpOffloadManager : public Bluetooth::AudioA2dpPlayingStateChangedListener { +private: + A2dpOffloadConnectionState currentOffloadconnectionState_ = CONNECTION_STATUS_DISCONNECTED; + std::vector connectionTriggerSessionIds_; + std::string a2dpOffloadDeviceAddress_ = ""; + AudioPolicyService *audioPolicyService_ = nullptr; + std::mutex connectionMutex_; + std::condition_variable connectionCV_; + static const int32_t CONNECTION_TIMEOUT_IN_MS = 300; // 300ms +public: + AudioA2dpOffloadManager(AudioPolicyService *audioPolicyService) : audioPolicyService_(audioPolicyService) { + Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(this); + return; + }; + void Init(); + A2dpOffloadConnectionState getA2dOffloadConnectionState() {return currentOffloadconnectionState_;}; + std::vector& GetConnectTriggerSessionIds() {return connectionTriggerSessionIds_;}; + std::string GetBluetoothAddress() {return a2dpOffloadDeviceAddress_;}; + + void ConnectA2dpOffload(const std::string &deviceAddress, const vector &sessionIds); + void DisconnectA2dpOffload(); + void OnA2dpPlayingStateChanged(std::string deviceAddress, int playingState, int error) override; + + void WaitForConnectionCompleted(); + bool IsA2dpOffloadConnecting(int32_t sessionId); + bool IsA2dpOffloadConnected(); }; } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 96ddb63a64..a31885faa4 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -161,6 +161,8 @@ const int RECORD_CAPTURE_ID = 3; const int32_t ONE_MINUTE = 60; constexpr int32_t MS_PER_S = 1000; constexpr int32_t NS_PER_MS = 1000000; +const int32_t DATA_LINK_CONNECTING = 10; +const int32_t DATA_LINK_CONNECTED = 11; std::shared_ptr g_dataShareHelper = nullptr; static sptr g_adProxy = nullptr; #ifdef BLUETOOTH_ENABLE @@ -346,6 +348,7 @@ bool AudioPolicyService::Init(void) audioEffectManager_.EffectManagerInit(); audioDeviceManager_.ParseDeviceXml(); audioPnpServer_.init(); + audioA2dpOffloadManager_ = new AudioA2dpOffloadManager(this); bool ret = audioPolicyConfigParser_.LoadConfiguration(); if (!ret) { @@ -1562,7 +1565,7 @@ std::string AudioPolicyService::GetSinkPortName(InternalDeviceType deviceType, A } switch (deviceType) { case InternalDeviceType::DEVICE_TYPE_BLUETOOTH_A2DP: - if (a2dpOffloadFlag_ == A2DP_OFFLOAD) { + if (a2dpOffloadFlag_ == A2DP_OFFLOAD && IsA2dpOffloadConnected()) { if (pipeType == PIPE_TYPE_OFFLOAD) { portName = OFFLOAD_PRIMARY_SPEAKER; } else if (pipeType == PIPE_TYPE_MULTICHANNEL) { @@ -2442,8 +2445,10 @@ void AudioPolicyService::FetchStreamForA2dpMchStream(std::unique_ptr> &rendererChangeInfos) -{ +void AudioPolicyService::FetchStreamForA2dpOffload(bool requireReset) + { + vector> rendererChangeInfos; + streamCollector_.GetCurrentRendererChangeInfos(rendererChangeInfos); AUDIO_INFO_LOG("start for %{public}zu stream", rendererChangeInfos.size()); for (auto &rendererChangeInfo : rendererChangeInfos) { if (!IsRendererStreamRunning(rendererChangeInfo)) { @@ -2454,9 +2459,11 @@ void AudioPolicyService::FetchStreamForA2dpOffload(vectorclientUID); if (descs.front()->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) { - int32_t ret = ActivateA2dpDevice(descs.front(), rendererChangeInfos); - CHECK_AND_RETURN_LOG(ret == SUCCESS, "activate a2dp [%{public}s] failed", - GetEncryptAddr(descs.front()->macAddress_).c_str()); + if (requireReset) { + int32_t ret = ActivateA2dpDevice(descs.front(), rendererChangeInfos); + CHECK_AND_RETURN_LOG(ret == SUCCESS, "activate a2dp [%{public}s] failed", + GetEncryptAddr(descs.front()->macAddress_).c_str()); + } if (rendererChangeInfo->rendererInfo.rendererFlags == AUDIO_FLAG_MMAP) { const sptr gsp = GetAudioServerProxy(); CHECK_AND_RETURN_LOG(gsp != nullptr, "Service proxy unavailable"); @@ -4854,6 +4861,14 @@ int32_t AudioPolicyService::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo } UpdateA2dpOffloadFlagForAllStream(currentActiveDevice_.deviceType_); + + if ((rendererState == RENDERER_RUNNING) && + !audioA2dpOffloadManager_->IsA2dpOffloadConnecting(streamChangeInfo.audioRendererChangeInfo.sessionId)) { + AUDIO_INFO_LOG("Notify client not to block."); + std::thread sendConnectedToClient(&AudioPolicyService::UpdateSessionConnectionState, + this, streamChangeInfo.audioRendererChangeInfo.sessionId, DATA_LINK_CONNECTED); + sendConnectedToClient.detach(); + } return ret; } @@ -6822,7 +6837,13 @@ int32_t AudioPolicyService::OffloadStartPlaying(const std::vector &sess if (a2dpOffloadFlag_ != A2DP_OFFLOAD || sessionIds.size() == 0) { return SUCCESS; } - return Bluetooth::AudioA2dpManager::OffloadStartPlaying(sessionIds); + int32_t ret = Bluetooth::AudioA2dpManager::OffloadStartPlaying(sessionIds); + A2dpOffloadConnectionState state = audioA2dpOffloadManager_->getA2dOffloadConnectionState(); + if (ret == SUCCESS && (state == CONNECTION_STATUS_DISCONNECTED || state == CONNECTION_STATUS_DISCONNECTING)) { + audioA2dpOffloadManager_->ConnectA2dpOffload( + Bluetooth::AudioA2dpManager::GetActiveA2dpDevice(), sessionIds); + } + return ret; #else return SUCCESS; #endif @@ -6836,7 +6857,9 @@ int32_t AudioPolicyService::OffloadStopPlaying(const std::vector &sessi if (a2dpOffloadFlag_ != A2DP_OFFLOAD || sessionIds.size() == 0) { return SUCCESS; } - return Bluetooth::AudioA2dpManager::OffloadStopPlaying(sessionIds); + int32_t ret = Bluetooth::AudioA2dpManager::OffloadStopPlaying(sessionIds); + audioA2dpOffloadManager_->DisconnectA2dpOffload(); + return ret; #else return SUCCESS; #endif @@ -6974,9 +6997,7 @@ int32_t AudioPolicyService::HandleA2dpDeviceOutOffload(BluetoothOffloadState a2d UpdateEffectDefaultSink(dev); AUDIO_INFO_LOG("Handle A2dpDevice Out Offload"); - vector> rendererChangeInfos; - streamCollector_.GetCurrentRendererChangeInfos(rendererChangeInfos); - FetchStreamForA2dpOffload(rendererChangeInfos); + FetchStreamForA2dpOffload(true); if (currentActiveDevice_.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) { return HandleActiveDevice(DEVICE_TYPE_BLUETOOTH_A2DP); @@ -6996,7 +7017,7 @@ int32_t AudioPolicyService::HandleA2dpDeviceInOffload(BluetoothOffloadState a2dp GetA2dpOffloadCodecAndSendToDsp(); std::vector allSessions; GetAllRunningStreamSession(allSessions); - OffloadStartPlaying(allSessions); + int32_t ret = OffloadStartPlaying(allSessions); DeviceType dev = GetActiveOutputDevice(); UpdateEffectDefaultSink(dev); @@ -7009,6 +7030,10 @@ int32_t AudioPolicyService::HandleA2dpDeviceInOffload(BluetoothOffloadState a2dp std::string activePort = BLUETOOTH_SPEAKER; audioPolicyManager_.SuspendAudioDevice(activePort, true); + if (ret == SUCCESS && IsA2dpOffloadConnected()) { + AUDIO_INFO_LOG("A2dpOffload has been connected, Fetch stream"); + FetchStreamForA2dpOffload(true); + } return SUCCESS; #else return ERROR; @@ -8231,5 +8256,15 @@ void AudioPolicyService::ResetOffloadModeOnSpatializationChanged(std::vector gsp = GetAudioServerProxy(); + CHECK_AND_RETURN_LOG(gsp != nullptr, "error for g_adProxy null"); + + std::string identity = IPCSkeleton::ResetCallingIdentity(); + gsp->UpdateSessionConnectionState(sessionID, state); + IPCSkeleton::SetCallingIdentity(identity); +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_service/client/include/audio_manager_base.h b/services/audio_service/client/include/audio_manager_base.h index dfd8ed4316..88811a3f40 100644 --- a/services/audio_service/client/include/audio_manager_base.h +++ b/services/audio_service/client/include/audio_manager_base.h @@ -427,6 +427,11 @@ public: * Update Effect BtOffload Supported state. */ virtual void UpdateEffectBtOffloadSupported(const bool &isSupported) = 0; + + /** + * Update Session Connection State. + */ + virtual void UpdateSessionConnectionState(const bool &isSupported) = 0; public: DECLARE_INTERFACE_DESCRIPTOR(u"IStandardAudioService"); }; @@ -493,6 +498,7 @@ private: int HandleRestoreRenderSink(MessageParcel &data, MessageParcel &reply); int HandleLoadHdiEffectModel(MessageParcel &data, MessageParcel &reply); int HandleUpdateEffectBtOffloadSupported(MessageParcel &data, MessageParcel &reply); + int HandleUpdateSessionConnectionState(MessageParcel &data, MessageParcel &reply); using HandlerFunc = int (AudioManagerStub::*)(MessageParcel &data, MessageParcel &reply); static inline HandlerFunc handlers[] = { @@ -552,6 +558,7 @@ private: &AudioManagerStub::HandleRestoreRenderSink, &AudioManagerStub::HandleLoadHdiEffectModel, &AudioManagerStub::HandleUpdateEffectBtOffloadSupported, + &AudioManagerStub::HandleUpdateSessionConnectionState, }; static constexpr size_t handlersNums = sizeof(handlers) / sizeof(HandlerFunc); static_assert(handlersNums == (static_cast (AudioServerInterfaceCode::AUDIO_SERVER_CODE_MAX) + 1), diff --git a/services/audio_service/client/include/audio_manager_proxy.h b/services/audio_service/client/include/audio_manager_proxy.h index 0c254fa3ac..09010bac90 100644 --- a/services/audio_service/client/include/audio_manager_proxy.h +++ b/services/audio_service/client/include/audio_manager_proxy.h @@ -95,6 +95,7 @@ public: bool GetEffectOffloadEnabled() override; void LoadHdiEffectModel() override; void UpdateEffectBtOffloadSupported(const bool &isSupported) override; + void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) override; private: static inline BrokerDelegator delegator_; }; diff --git a/services/audio_service/client/include/pulseaudio_ipc_interface_code.h b/services/audio_service/client/include/pulseaudio_ipc_interface_code.h index b3f3746fbb..c71e7db68c 100644 --- a/services/audio_service/client/include/pulseaudio_ipc_interface_code.h +++ b/services/audio_service/client/include/pulseaudio_ipc_interface_code.h @@ -78,7 +78,8 @@ namespace AudioStandard { RESTORE_RENDERSINK, LOAD_HDI_EFFECT_MODEL, UPDATE_EFFECT_BT_OFFLOAD_SUPPORTED, - AUDIO_SERVER_CODE_MAX = UPDATE_EFFECT_BT_OFFLOAD_SUPPORTED, + UPDATE_SESSION_CONNECTION_STATE, + AUDIO_SERVER_CODE_MAX = UPDATE_SESSION_CONNECTION_STATE, }; } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_service/client/include/renderer_in_client_private.h b/services/audio_service/client/include/renderer_in_client_private.h index 6fc8542a8a..95d5f5277e 100644 --- a/services/audio_service/client/include/renderer_in_client_private.h +++ b/services/audio_service/client/include/renderer_in_client_private.h @@ -319,6 +319,8 @@ private: // for status operation wait and notify std::mutex callServerMutex_; std::condition_variable callServerCV_; + std::mutex dataConnectionMutex_; + std::condition_variable dataConnectionCV_; Operation notifiedOperation_ = MAX_OPERATION_CODE; int64_t notifiedResult_ = 0; @@ -387,6 +389,7 @@ private: std::shared_ptr proxyObj_ = nullptr; uint64_t lastFlushPosition_ = 0; + bool isDataLinkConnected_ = false; enum { STATE_CHANGE_EVENT = 0, diff --git a/services/audio_service/client/src/audio_manager_proxy.cpp b/services/audio_service/client/src/audio_manager_proxy.cpp index 51ccb7079f..84ad339507 100644 --- a/services/audio_service/client/src/audio_manager_proxy.cpp +++ b/services/audio_service/client/src/audio_manager_proxy.cpp @@ -1162,5 +1162,21 @@ void AudioManagerProxy::UpdateEffectBtOffloadSupported(const bool &isSupported) static_cast(AudioServerInterfaceCode::UPDATE_EFFECT_BT_OFFLOAD_SUPPORTED), data, reply, option); CHECK_AND_RETURN_LOG(error == ERR_NONE, "failed, error:%{public}d", error); } + +void AudioManagerProxy::UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + bool ret = data.WriteInterfaceToken(GetDescriptor()); + CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed"); + data.WriteInt32(sessionID); + data.WriteInt32(state); + + int32_t error = Remote()->SendRequest( + static_cast(AudioServerInterfaceCode::UPDATE_SESSION_CONNECTION_STATE), data, reply, option); + CHECK_AND_RETURN_LOG(error == ERR_NONE, "failed, error:%{public}d", error); +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_service/client/src/renderer_in_client.cpp b/services/audio_service/client/src/renderer_in_client.cpp index e3313ba298..70c0dd2da0 100644 --- a/services/audio_service/client/src/renderer_in_client.cpp +++ b/services/audio_service/client/src/renderer_in_client.cpp @@ -85,6 +85,7 @@ static const int32_t OFFLOAD_OPERATION_TIMEOUT_IN_MS = 8000; // 8000ms for offlo static const int32_t WRITE_CACHE_TIMEOUT_IN_MS = 3000; // 3000ms static const int32_t WRITE_BUFFER_TIMEOUT_IN_MS = 20; // ms static const int32_t SHORT_TIMEOUT_IN_MS = 20; // ms +static const int32_t DATA_CONNECTION_TIMEOUT_IN_MS = 300; // ms static constexpr int CB_QUEUE_CAPACITY = 3; constexpr int32_t MAX_BUFFER_SIZE = 100000; static constexpr int32_t ONE_MINUTE = 60; @@ -136,6 +137,16 @@ int32_t RendererInClientInner::OnOperationHandled(Operation operation, int64_t r return SUCCESS; } + if (operation == DATA_LINK_CONNECTING || operation == DATA_LINK_CONNECTED) { + if (operation == DATA_LINK_CONNECTING) { + isDataLinkConnected_ = false; + } else { + isDataLinkConnected_ = true; + dataConnectionCV_.notify_all(); + } + return SUCCESS; + } + std::unique_lock lock(callServerMutex_); notifiedOperation_ = operation; notifiedResult_ = result; @@ -1237,6 +1248,20 @@ bool RendererInClientInner::StartAudioStream(StateChangeCmdType cmdType, waitLock.unlock(); + AUDIO_INFO_LOG("Start SUCCESS, sessionId: %{public}d, uid: %{public}d", sessionId_, clientUid_); + UpdateTracker("RUNNING"); + + std::unique_lock dataConnectionWaitLock(dataConnectionMutex_); + if (!isDataLinkConnected_) { + AUDIO_INFO_LOG("data-connection blocking starts."); + stopWaiting = dataConnectionCV_.wait_for( + dataConnectionWaitLock, std::chrono::milliseconds(DATA_CONNECTION_TIMEOUT_IN_MS), [this] { + return isDataLinkConnected_; + }); + AUDIO_INFO_LOG("data-connection blocking stops."); + } + dataConnectionWaitLock.unlock(); + offloadStartReadPos_ = 0; if (renderMode_ == RENDER_MODE_CALLBACK) { // start the callback-write thread @@ -1247,9 +1272,6 @@ bool RendererInClientInner::StartAudioStream(StateChangeCmdType cmdType, int64_t param = -1; StateCmdTypeToParams(param, state_, cmdType); SafeSendCallbackEvent(STATE_CHANGE_EVENT, param); - - AUDIO_INFO_LOG("Start SUCCESS, sessionId: %{public}d, uid: %{public}d", sessionId_, clientUid_); - UpdateTracker("RUNNING"); return true; } diff --git a/services/audio_service/server/include/audio_server.h b/services/audio_service/server/include/audio_server.h index a66ea65eb3..5d990a6d4d 100644 --- a/services/audio_service/server/include/audio_server.h +++ b/services/audio_service/server/include/audio_server.h @@ -157,6 +157,8 @@ public: void LoadHdiEffectModel() override; void UpdateEffectBtOffloadSupported(const bool &isSupported) override; + + void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) override; protected: void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; diff --git a/services/audio_service/server/include/audio_service.h b/services/audio_service/server/include/audio_service.h index fc8e7a9f10..69a308d0d2 100644 --- a/services/audio_service/server/include/audio_service.h +++ b/services/audio_service/server/include/audio_service.h @@ -67,6 +67,7 @@ public: void RemoveRenderer(uint32_t sessionId); int32_t EnableDualToneList(uint32_t sessionId); int32_t DisableDualToneList(uint32_t sessionId); + std::shared_ptr GetRendererBySessionID(const uint32_t &session); private: AudioService(); diff --git a/services/audio_service/server/include/i_stream.h b/services/audio_service/server/include/i_stream.h index 1692aca585..6becb8c99f 100644 --- a/services/audio_service/server/include/i_stream.h +++ b/services/audio_service/server/include/i_stream.h @@ -33,6 +33,8 @@ enum IOperation { OPERATION_UNDERFLOW, OPERATION_SET_OFFLOAD_ENABLE, OPERATION_UNSET_OFFLOAD_ENABLE, + OPERATION_DATA_LINK_CONNECTING, + OPERATION_DATA_LINK_CONNECTED, }; enum IStatus { diff --git a/services/audio_service/server/include/renderer_in_server.h b/services/audio_service/server/include/renderer_in_server.h index 1b60c93e49..bfb0d48c7d 100644 --- a/services/audio_service/server/include/renderer_in_server.h +++ b/services/audio_service/server/include/renderer_in_server.h @@ -93,6 +93,8 @@ public: int32_t GetStreamManagerType() const noexcept; int32_t SetSilentModeAndMixWithOthers(bool on); int32_t SetClientVolume(); + + void OnDataLinkConnectionUpdate(IOperation operation); public: const AudioProcessConfig processConfig_; private: diff --git a/services/audio_service/server/src/audio_manager_stub.cpp b/services/audio_service/server/src/audio_manager_stub.cpp index cd26d4a352..64d99188f2 100644 --- a/services/audio_service/server/src/audio_manager_stub.cpp +++ b/services/audio_service/server/src/audio_manager_stub.cpp @@ -86,6 +86,7 @@ const char *g_audioServerCodeStrs[] = { "RESTORE_RENDERSINK", "LOAD_HDI_EFFECT_MODEL", "UPDATE_EFFECT_BT_OFFLOAD_SUPPORTED", + "UPDATE_SESSION_CONNECTION_STATE", }; constexpr size_t codeNums = sizeof(g_audioServerCodeStrs) / sizeof(const char *); static_assert(codeNums == (static_cast (AudioServerInterfaceCode::AUDIO_SERVER_CODE_MAX) + 1), @@ -724,5 +725,13 @@ int AudioManagerStub::HandleUpdateEffectBtOffloadSupported(MessageParcel &data, UpdateEffectBtOffloadSupported(data.ReadBool()); return AUDIO_OK; } + +int AudioManagerStub::HandleUpdateSessionConnectionState(MessageParcel &data, MessageParcel &reply) +{ + int32_t sessionID = data.ReadInt32(); + int32_t state = data.ReadInt32(); + UpdateSessionConnectionState(sessionID, state); + return AUDIO_OK; +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_service/server/src/audio_server.cpp b/services/audio_service/server/src/audio_server.cpp index 1f74801c30..807858aa1d 100644 --- a/services/audio_service/server/src/audio_server.cpp +++ b/services/audio_service/server/src/audio_server.cpp @@ -2065,5 +2065,20 @@ void AudioServer::UpdateEffectBtOffloadSupported(const bool &isSupported) CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr"); audioEffectChainManager->UpdateEffectBtOffloadSupported(isSupported); } + +void AudioServer::UpdateSessionConnectionState(const int32_t &sessionId, const int32_t &state) +{ + AUDIO_INFO_LOG("Server get sessionID: %{public}d, state: %{public}d", sessionId, state); + int32_t callingUid = IPCSkeleton::GetCallingUid(); + CHECK_AND_RETURN_LOG(callingUid == audioUid_ || callingUid == ROOT_UID, "refused for %{public}d", callingUid); + std::shared_ptr renderer = + AudioService::GetInstance()->GetRendererBySessionID(static_cast(sessionId)); + + if (renderer == nullptr) { + AUDIO_ERR_LOG("No render in server has sessionID"); + return; + } + renderer->OnDataLinkConnectionUpdate(static_cast(state)); +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_service/server/src/audio_service.cpp b/services/audio_service/server/src/audio_service.cpp index c43267754e..5ff3c510b8 100644 --- a/services/audio_service/server/src/audio_service.cpp +++ b/services/audio_service/server/src/audio_service.cpp @@ -682,5 +682,14 @@ float AudioService::GetMaxAmplitude(bool isOutputDevice) } return fastAudioMaxAmplitude; } + +std::shared_ptr AudioService::GetRendererBySessionID(const uint32_t &sessionID) +{ + if (allRendererMap_.count(sessionID)) { + return allRendererMap_[sessionID].lock(); + } else { + return std::shared_ptr(); + } +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_service/server/src/renderer_in_server.cpp b/services/audio_service/server/src/renderer_in_server.cpp index 5d7d950fbb..76febe79e9 100644 --- a/services/audio_service/server/src/renderer_in_server.cpp +++ b/services/audio_service/server/src/renderer_in_server.cpp @@ -1013,5 +1013,23 @@ int32_t RendererInServer::SetClientVolume() int32_t ret = stream_->SetClientVolume(clientVolume); return ret; } + +void RendererInServer::OnDataLinkConnectionUpdate(IOperation operation) +{ + std::shared_ptr stateListener = streamListener_.lock(); + CHECK_AND_RETURN_LOG(stateListener != nullptr, "StreamListener is nullptr"); + switch (operation) { + case OPERATION_DATA_LINK_CONNECTING: + AUDIO_DEBUG_LOG("OPERATION_DATA_LINK_CONNECTING received"); + stateListener->OnOperationHandled(DATA_LINK_CONNECTING, 0); + break; + case OPERATION_DATA_LINK_CONNECTED: + AUDIO_DEBUG_LOG("OPERATION_DATA_LINK_CONNECTED received"); + stateListener->OnOperationHandled(DATA_LINK_CONNECTED, 0); + break; + default: + return; + } +} } // namespace AudioStandard } // namespace OHOS -- Gitee From 25ef06efa0c1dc84c37b00257a61cd4da58a2a54 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Wed, 24 Jul 2024 20:38:07 +0800 Subject: [PATCH 02/23] =?UTF-8?q?start=E7=AD=89=E5=BE=85=E8=93=9D=E7=89=99?= =?UTF-8?q?offload=E9=80=9A=E8=B7=AF=E5=BB=BA=E7=AB=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../audio_bluetooth_manager.cpp | 1 - .../bluetoothclient/audio_bluetooth_manager.h | 4 + .../src/service/audio_policy_service.cpp | 93 +++++++++++++++++-- .../common/include/i_stream_listener.h | 2 + .../server/src/audio_manager_stub.cpp | 1 + 5 files changed, 94 insertions(+), 7 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index 792a10a049..844ccab471 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -215,7 +215,6 @@ int32_t AudioA2dpManager::OffloadStopPlaying(const std::vector &session int32_t AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(AudioA2dpPlayingStateChangedListener *listener) { stateChangedListeners_.push_back(listener); - AUDIO_INFO_LOG("CXX: listener pushed."); return SUCCESS; } diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h index f5cf455026..0a5306a9b3 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -25,6 +25,10 @@ namespace OHOS { namespace Bluetooth { +class AudioA2dpPlayingStateChangedListener { +public: + virtual void OnA2dpPlayingStateChanged(std::string deviceAddress, int playingState, int error) = 0; +}; // Audio bluetooth a2dp feature support class AudioA2dpListener : public A2dpSourceObserver { diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index a31885faa4..2183b78a1f 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -163,6 +163,7 @@ constexpr int32_t MS_PER_S = 1000; constexpr int32_t NS_PER_MS = 1000000; const int32_t DATA_LINK_CONNECTING = 10; const int32_t DATA_LINK_CONNECTED = 11; +const int32_t A2DP_PLAYING = 2; std::shared_ptr g_dataShareHelper = nullptr; static sptr g_adProxy = nullptr; #ifdef BLUETOOTH_ENABLE @@ -7024,16 +7025,13 @@ int32_t AudioPolicyService::HandleA2dpDeviceInOffload(BluetoothOffloadState a2dp AUDIO_INFO_LOG("Handle A2dpDevice In Offload"); UpdateEffectBtOffloadSupported(true); - vector> rendererChangeInfos; - streamCollector_.GetCurrentRendererChangeInfos(rendererChangeInfos); - FetchStreamForA2dpOffload(rendererChangeInfos); - - std::string activePort = BLUETOOTH_SPEAKER; - audioPolicyManager_.SuspendAudioDevice(activePort, true); if (ret == SUCCESS && IsA2dpOffloadConnected()) { AUDIO_INFO_LOG("A2dpOffload has been connected, Fetch stream"); FetchStreamForA2dpOffload(true); } + + std::string activePort = BLUETOOTH_SPEAKER; + audioPolicyManager_.SuspendAudioDevice(activePort, true); return SUCCESS; #else return ERROR; @@ -8257,6 +8255,11 @@ void AudioPolicyService::ResetOffloadModeOnSpatializationChanged(std::vectorgetA2dOffloadConnectionState() == CONNECTION_STATUS_CONNECTED; +} + void AudioPolicyService::UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) { const sptr gsp = GetAudioServerProxy(); @@ -8266,5 +8269,83 @@ void AudioPolicyService::UpdateSessionConnectionState(const int32_t &sessionID, gsp->UpdateSessionConnectionState(sessionID, state); IPCSkeleton::SetCallingIdentity(identity); } + +void AudioA2dpOffloadManager::OnA2dpPlayingStateChanged(std::string deviceAddress, int playingState, int error) +{ + if (deviceAddress == a2dpOffloadDeviceAddress_) { + if (playingState == A2DP_PLAYING && currentOffloadconnectionState_ == CONNECTION_STATUS_CONNECTING) { + AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", + currentOffloadconnectionState_, CONNECTION_STATUS_CONNECTED); + currentOffloadconnectionState_ = CONNECTION_STATUS_CONNECTED; + for (int32_t sessionId : connectionTriggerSessionIds_) { + audioPolicyService_->UpdateSessionConnectionState(sessionId, DATA_LINK_CONNECTED); + } + std::vector().swap(connectionTriggerSessionIds_); + connectionCV_.notify_all(); + } + } else { + AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", + currentOffloadconnectionState_, CONNECTION_STATUS_DISCONNECTED); + currentOffloadconnectionState_ = CONNECTION_STATUS_DISCONNECTED; + } +} + +void AudioA2dpOffloadManager::ConnectA2dpOffload(const std::string &deviceAddress, const vector &sessionIds) +{ + AUDIO_INFO_LOG("start connecting a2dpOffload."); + a2dpOffloadDeviceAddress_ = deviceAddress; + connectionTriggerSessionIds_.assign(sessionIds.begin(), sessionIds.end()); + + for (int32_t sessionId : connectionTriggerSessionIds_) { + audioPolicyService_->UpdateSessionConnectionState(sessionId, DATA_LINK_CONNECTING); + } + + std::thread switchThread(&AudioA2dpOffloadManager::WaitForConnectionCompleted, this); + switchThread.detach(); + AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", + currentOffloadconnectionState_, CONNECTION_STATUS_CONNECTING); + currentOffloadconnectionState_ = CONNECTION_STATUS_CONNECTING; +} + +void AudioA2dpOffloadManager::DisconnectA2dpOffload() +{ + a2dpOffloadDeviceAddress_ = ""; + AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", + currentOffloadconnectionState_, CONNECTION_STATUS_DISCONNECTING); + currentOffloadconnectionState_ = CONNECTION_STATUS_DISCONNECTING; +} + +void AudioA2dpOffloadManager::WaitForConnectionCompleted() +{ + std::unique_lock waitLock(connectionMutex_); + bool connectionCompleted = connectionCV_.wait_for(waitLock, + std::chrono::milliseconds(AudioA2dpOffloadManager::CONNECTION_TIMEOUT_IN_MS), [this] { + return currentOffloadconnectionState_ == CONNECTION_STATUS_CONNECTED; + }); + // a2dp connection timeout, anyway we should notify client dataLink OK in order to allow the data flow begin + if (!connectionCompleted) { + AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", + currentOffloadconnectionState_, CONNECTION_STATUS_TIMEOUT); + currentOffloadconnectionState_ = CONNECTION_STATUS_CONNECTED; + for (int32_t sessionId : connectionTriggerSessionIds_) { + audioPolicyService_->UpdateSessionConnectionState(sessionId, DATA_LINK_CONNECTED); + } + std::vector().swap(connectionTriggerSessionIds_); + } + waitLock.unlock(); + audioPolicyService_->FetchStreamForA2dpOffload(false); + return; +} + +bool AudioA2dpOffloadManager::IsA2dpOffloadConnecting(int32_t sessionId) +{ + if (currentOffloadconnectionState_ == CONNECTION_STATUS_CONNECTING) { + if (std::find(connectionTriggerSessionIds_.begin(), connectionTriggerSessionIds_.end(), sessionId) != + connectionTriggerSessionIds_.end()) { + return true; + } + } + return false; +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_service/common/include/i_stream_listener.h b/services/audio_service/common/include/i_stream_listener.h index fe3b558f2e..ae98466352 100644 --- a/services/audio_service/common/include/i_stream_listener.h +++ b/services/audio_service/common/include/i_stream_listener.h @@ -32,6 +32,8 @@ enum Operation : int32_t { BUFFER_OVERFLOW, SET_OFFLOAD_ENABLE, UNDERFLOW_COUNT_ADD, // notify client underflow count increment + DATA_LINK_CONNECTING, // a2dp offload connecting + DATA_LINK_CONNECTED, MAX_OPERATION_CODE // in plan add underrun overflow }; class IStreamListener { diff --git a/services/audio_service/server/src/audio_manager_stub.cpp b/services/audio_service/server/src/audio_manager_stub.cpp index 64d99188f2..f7b7c2799b 100644 --- a/services/audio_service/server/src/audio_manager_stub.cpp +++ b/services/audio_service/server/src/audio_manager_stub.cpp @@ -73,6 +73,7 @@ const char *g_audioServerCodeStrs[] = { "GET_EFFECT_LATENCY", "UPDATE_LATENCY_TIMESTAMP", "SET_ASR_AEC_MODE", + "GET_ASR_AEC_MODE", "SET_ASR_NOISE_SUPPRESSION_MODE", "GET_ASR_NOISE_SUPPRESSION_MODE", -- Gitee From d3bd0919e75634860614d4379937230b68ff070c Mon Sep 17 00:00:00 2001 From: chuxixue Date: Wed, 24 Jul 2024 21:04:46 +0800 Subject: [PATCH 03/23] =?UTF-8?q?start=E7=AD=89=E5=BE=85=E8=93=9D=E7=89=99?= =?UTF-8?q?offload=E9=80=9A=E8=B7=AF=E5=BB=BA=E7=AB=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- frameworks/native/bluetoothclient/audio_bluetooth_manager.h | 6 ++++-- .../server/include/service/audio_policy_service.h | 4 ---- services/audio_service/server/src/audio_manager_stub.cpp | 1 - 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h index 0a5306a9b3..0b15559f75 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -49,6 +49,9 @@ class AudioA2dpManager { public: AudioA2dpManager() = default; virtual ~AudioA2dpManager() = default; + + static std::vector stateChangedListeners_; + static void RegisterBluetoothA2dpListener(); static void UnregisterBluetoothA2dpListener(); static void DisconnectBluetoothA2dpSink(); @@ -62,8 +65,7 @@ public: static int32_t A2dpOffloadSessionRequest(const std::vector &info); static int32_t OffloadStartPlaying(const std::vector &sessionsID); static int32_t OffloadStopPlaying(const std::vector &sessionsID); - static int32_t RegisterA2dpPlayingStateChangedListener(); - static std::vector stateChangedListeners_; + static int32_t RegisterA2dpPlayingStateChangedListener(AudioA2dpPlayingStateChangedListener *listener); static void SetConnectionState(int state) { diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index b2dac6c614..c85a9735ac 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -667,8 +667,6 @@ private: void FetchStreamForA2dpMchStream(std::unique_ptr &rendererChangeInfo, vector> &descs); - void FetchStreamForA2dpOffload(vector> &rendererChangeInfos); - int32_t HandleScoInputDeviceFetched(unique_ptr &desc, vector> &capturerChangeInfos); @@ -1160,7 +1158,6 @@ public: Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(this); return; }; - void Init(); A2dpOffloadConnectionState getA2dOffloadConnectionState() {return currentOffloadconnectionState_;}; std::vector& GetConnectTriggerSessionIds() {return connectionTriggerSessionIds_;}; std::string GetBluetoothAddress() {return a2dpOffloadDeviceAddress_;}; @@ -1171,7 +1168,6 @@ public: void WaitForConnectionCompleted(); bool IsA2dpOffloadConnecting(int32_t sessionId); - bool IsA2dpOffloadConnected(); }; } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_service/server/src/audio_manager_stub.cpp b/services/audio_service/server/src/audio_manager_stub.cpp index f7b7c2799b..64d99188f2 100644 --- a/services/audio_service/server/src/audio_manager_stub.cpp +++ b/services/audio_service/server/src/audio_manager_stub.cpp @@ -73,7 +73,6 @@ const char *g_audioServerCodeStrs[] = { "GET_EFFECT_LATENCY", "UPDATE_LATENCY_TIMESTAMP", "SET_ASR_AEC_MODE", - "GET_ASR_AEC_MODE", "SET_ASR_NOISE_SUPPRESSION_MODE", "GET_ASR_NOISE_SUPPRESSION_MODE", -- Gitee From 214f748fe27ee17ae63870668d60f2723c79642b Mon Sep 17 00:00:00 2001 From: chuxixue Date: Thu, 25 Jul 2024 09:57:28 +0800 Subject: [PATCH 04/23] =?UTF-8?q?=E9=97=A8=E7=A6=81=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../server/include/service/audio_policy_service.h | 3 ++- .../server/src/service/audio_policy_service.cpp | 8 +++----- .../audio_service/client/include/audio_manager_base.h | 2 +- services/audio_service/client/src/renderer_in_client.cpp | 1 - 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index c85a9735ac..76554be323 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -1154,7 +1154,8 @@ private: std::condition_variable connectionCV_; static const int32_t CONNECTION_TIMEOUT_IN_MS = 300; // 300ms public: - AudioA2dpOffloadManager(AudioPolicyService *audioPolicyService) : audioPolicyService_(audioPolicyService) { + AudioA2dpOffloadManager(AudioPolicyService *audioPolicyService) : audioPolicyService_(audioPolicyService) + { Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(this); return; }; diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 2183b78a1f..079317106e 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -2447,7 +2447,7 @@ void AudioPolicyService::FetchStreamForA2dpMchStream(std::unique_ptr> rendererChangeInfos; streamCollector_.GetCurrentRendererChangeInfos(rendererChangeInfos); AUDIO_INFO_LOG("start for %{public}zu stream", rendererChangeInfos.size()); @@ -4826,10 +4826,8 @@ int32_t AudioPolicyService::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo std::lock_guard deviceLock(deviceStatusUpdateSharedMutex_); AUDIO_INFO_LOG("Entered AudioPolicyService UpdateTracker"); - if (mode == AUDIO_MODE_RECORD) { - if (streamChangeInfo.audioCapturerChangeInfo.capturerState == CAPTURER_RELEASED) { - audioCaptureMicrophoneDescriptor_.erase(streamChangeInfo.audioCapturerChangeInfo.sessionId); - } + if (mode == AUDIO_MODE_RECORD && streamChangeInfo.audioCapturerChangeInfo.capturerState == CAPTURER_RELEASED) { + audioCaptureMicrophoneDescriptor_.erase(streamChangeInfo.audioCapturerChangeInfo.sessionId); } int32_t ret = streamCollector_.UpdateTracker(mode, streamChangeInfo); diff --git a/services/audio_service/client/include/audio_manager_base.h b/services/audio_service/client/include/audio_manager_base.h index 88811a3f40..6e69bcc710 100644 --- a/services/audio_service/client/include/audio_manager_base.h +++ b/services/audio_service/client/include/audio_manager_base.h @@ -431,7 +431,7 @@ public: /** * Update Session Connection State. */ - virtual void UpdateSessionConnectionState(const bool &isSupported) = 0; + virtual void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) = 0; public: DECLARE_INTERFACE_DESCRIPTOR(u"IStandardAudioService"); }; diff --git a/services/audio_service/client/src/renderer_in_client.cpp b/services/audio_service/client/src/renderer_in_client.cpp index 70c0dd2da0..5452c643dd 100644 --- a/services/audio_service/client/src/renderer_in_client.cpp +++ b/services/audio_service/client/src/renderer_in_client.cpp @@ -1258,7 +1258,6 @@ bool RendererInClientInner::StartAudioStream(StateChangeCmdType cmdType, dataConnectionWaitLock, std::chrono::milliseconds(DATA_CONNECTION_TIMEOUT_IN_MS), [this] { return isDataLinkConnected_; }); - AUDIO_INFO_LOG("data-connection blocking stops."); } dataConnectionWaitLock.unlock(); -- Gitee From 1dd7198d82c5498696c67c003372eed38246880a Mon Sep 17 00:00:00 2001 From: chuxixue Date: Thu, 25 Jul 2024 13:35:11 +0800 Subject: [PATCH 05/23] =?UTF-8?q?=E9=97=A8=E7=A6=81=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../server/src/service/audio_policy_service.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 079317106e..807043f3d4 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -4836,13 +4836,15 @@ int32_t AudioPolicyService::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo if (rendererState == RENDERER_PREPARED || rendererState == RENDERER_NEW || rendererState == RENDERER_INVALID) { return ret; // only update tracker in new and prepared } + + if (rendererState == RENDERER_RELEASED) { + if (!streamCollector_.ExistStreamForPipe(PIPE_TYPE_MULTICHANNEL)) { + DynamicUnloadModule(PIPE_TYPE_MULTICHANNEL); + } - if (rendererState == RENDERER_RELEASED && !streamCollector_.ExistStreamForPipe(PIPE_TYPE_MULTICHANNEL)) { - DynamicUnloadModule(PIPE_TYPE_MULTICHANNEL); - } - - if (rendererState == RENDERER_RELEASED && !streamCollector_.ExistStreamForPipe(PIPE_TYPE_OFFLOAD)) { - DynamicUnloadModule(PIPE_TYPE_OFFLOAD); + if (!streamCollector_.ExistStreamForPipe(PIPE_TYPE_OFFLOAD)) { + DynamicUnloadModule(PIPE_TYPE_OFFLOAD); + } } if (mode == AUDIO_MODE_PLAYBACK && (rendererState == RENDERER_STOPPED || rendererState == RENDERER_PAUSED)) { -- Gitee From 457acb10385b2fa8362b789928b632d7e68864ce Mon Sep 17 00:00:00 2001 From: chuxixue Date: Mon, 29 Jul 2024 16:38:26 +0800 Subject: [PATCH 06/23] =?UTF-8?q?=E8=A7=A3=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- services/audio_service/server/src/audio_manager_stub.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/audio_service/server/src/audio_manager_stub.cpp b/services/audio_service/server/src/audio_manager_stub.cpp index 39f2c75325..8671182a92 100644 --- a/services/audio_service/server/src/audio_manager_stub.cpp +++ b/services/audio_service/server/src/audio_manager_stub.cpp @@ -734,6 +734,8 @@ int AudioManagerStub::HandleFourthPartCode(uint32_t code, MessageParcel &data, M return HandleLoadHdiEffectModel(data, reply); case static_cast(AudioServerInterfaceCode::UPDATE_EFFECT_BT_OFFLOAD_SUPPORTED): return HandleUpdateEffectBtOffloadSupported(data, reply); + case static_cast(AudioServerInterfaceCode::UPDATE_SESSION_CONNECTION_STATE): + return HandleUpdateSessionConnectionState(data, reply); default: AUDIO_ERR_LOG("default case, need check AudioManagerStub"); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); -- Gitee From 15f84f70846bee41674968f9ba6d922eaa51fa6d Mon Sep 17 00:00:00 2001 From: chuxixue Date: Tue, 30 Jul 2024 10:31:06 +0800 Subject: [PATCH 07/23] =?UTF-8?q?=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../audio_policy/server/include/service/audio_policy_service.h | 2 +- .../audio_policy/server/src/service/audio_policy_service.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 5b54c3c88d..475329779d 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -536,7 +536,7 @@ public: AudioScene GetLastAudioScene() const; - void FetchStreamForA2dpOffload(bool requireReset); + void FetchStreamForA2dpOffload(const bool &requireReset); void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state); private: diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index a9ded10dbd..63181ec88b 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -2482,7 +2482,7 @@ void AudioPolicyService::FetchStreamForA2dpMchStream(std::unique_ptr> rendererChangeInfos; streamCollector_.GetCurrentRendererChangeInfos(rendererChangeInfos); -- Gitee From 2e8df657c43daa99f9d44509e20fc4d1537cc90f Mon Sep 17 00:00:00 2001 From: chuxixue Date: Tue, 30 Jul 2024 10:56:29 +0800 Subject: [PATCH 08/23] conflicts Signed-off-by: chuxixue --- services/audio_service/client/src/renderer_in_client.cpp | 1 + services/audio_service/server/src/audio_manager_stub.cpp | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/services/audio_service/client/src/renderer_in_client.cpp b/services/audio_service/client/src/renderer_in_client.cpp index 31d9f8149f..7931d86483 100644 --- a/services/audio_service/client/src/renderer_in_client.cpp +++ b/services/audio_service/client/src/renderer_in_client.cpp @@ -1259,6 +1259,7 @@ bool RendererInClientInner::StartAudioStream(StateChangeCmdType cmdType, dataConnectionWaitLock, std::chrono::milliseconds(DATA_CONNECTION_TIMEOUT_IN_MS), [this] { return isDataLinkConnected_; }); + AUDIO_DEBUG_LOG("data-connection blocking ends."); } dataConnectionWaitLock.unlock(); diff --git a/services/audio_service/server/src/audio_manager_stub.cpp b/services/audio_service/server/src/audio_manager_stub.cpp index 8a16eb03b3..763a1513bd 100644 --- a/services/audio_service/server/src/audio_manager_stub.cpp +++ b/services/audio_service/server/src/audio_manager_stub.cpp @@ -86,11 +86,8 @@ const char *g_audioServerCodeStrs[] = { "RESTORE_RENDERSINK", "LOAD_HDI_EFFECT_MODEL", "UPDATE_EFFECT_BT_OFFLOAD_SUPPORTED", -<<<<<<< HEAD - "UPDATE_SESSION_CONNECTION_STATE", -======= "SET_SINK_MUTE_FOR_SWITCH_DEVICE", ->>>>>>> upstream/master + "UPDATE_SESSION_CONNECTION_STATE", }; constexpr size_t codeNums = sizeof(g_audioServerCodeStrs) / sizeof(const char *); static_assert(codeNums == (static_cast (AudioServerInterfaceCode::AUDIO_SERVER_CODE_MAX) + 1), -- Gitee From a6cbd01454a94a2f2ed152eec47bebc3ca170500 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Tue, 30 Jul 2024 15:49:04 +0800 Subject: [PATCH 09/23] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../audio_bluetooth_manager.cpp | 5 +++-- .../bluetoothclient/audio_bluetooth_manager.h | 5 +++-- .../include/service/audio_policy_service.h | 14 +++++------- .../src/service/audio_policy_service.cpp | 22 ++++++++++++------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index 844ccab471..6f3448c26e 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -38,7 +38,7 @@ AudioScene AudioHfpManager::scene_ = AUDIO_SCENE_DEFAULT; AudioScene AudioHfpManager::sceneFromPolicy_ = AUDIO_SCENE_DEFAULT; OHOS::Bluetooth::ScoCategory AudioHfpManager::scoCategory = OHOS::Bluetooth::ScoCategory::SCO_DEFAULT; BluetoothRemoteDevice AudioHfpManager::activeHfpDevice_; -std::vector AudioA2dpManager::stateChangedListeners_; +std::vector> AudioA2dpManager::stateChangedListeners_; std::mutex g_activehfpDeviceLock; std::mutex g_audioSceneLock; std::mutex g_hfpInstanceLock; @@ -212,7 +212,8 @@ int32_t AudioA2dpManager::OffloadStopPlaying(const std::vector &session return a2dpInstance_->OffloadStopPlaying(activeA2dpDevice_, sessionsID); } -int32_t AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(AudioA2dpPlayingStateChangedListener *listener) +int32_t AudioA2dpManager::RegisterA2dpPlayingStateChangedListener( + std::shared_ptr listener) { stateChangedListeners_.push_back(listener); return SUCCESS; diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h index 0b15559f75..5d361bfe8a 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -50,7 +50,7 @@ public: AudioA2dpManager() = default; virtual ~AudioA2dpManager() = default; - static std::vector stateChangedListeners_; + static std::vector> stateChangedListeners_; static void RegisterBluetoothA2dpListener(); static void UnregisterBluetoothA2dpListener(); @@ -65,7 +65,8 @@ public: static int32_t A2dpOffloadSessionRequest(const std::vector &info); static int32_t OffloadStartPlaying(const std::vector &sessionsID); static int32_t OffloadStopPlaying(const std::vector &sessionsID); - static int32_t RegisterA2dpPlayingStateChangedListener(AudioA2dpPlayingStateChangedListener *listener); + static int32_t RegisterA2dpPlayingStateChangedListener( + std::shared_ptr listener); static void SetConnectionState(int state) { diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 7e1e481c11..3c670ab47f 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -1149,10 +1149,11 @@ private: bool ringerModeMute_ = true; std::atomic isPolicyConfigParsered_ = false; - AudioA2dpOffloadManager *audioA2dpOffloadManager_ = nullptr; + std::share_ptr audioA2dpOffloadManager_ = nullptr; }; -class AudioA2dpOffloadManager : public Bluetooth::AudioA2dpPlayingStateChangedListener { +class AudioA2dpOffloadManager final : public Bluetooth::AudioA2dpPlayingStateChangedListener, + public enable_shared_from_this { private: A2dpOffloadConnectionState currentOffloadconnectionState_ = CONNECTION_STATUS_DISCONNECTED; std::vector connectionTriggerSessionIds_; @@ -1162,12 +1163,9 @@ private: std::condition_variable connectionCV_; static const int32_t CONNECTION_TIMEOUT_IN_MS = 300; // 300ms public: - AudioA2dpOffloadManager(AudioPolicyService *audioPolicyService) : audioPolicyService_(audioPolicyService) - { - Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(this); - return; - }; - A2dpOffloadConnectionState getA2dOffloadConnectionState() {return currentOffloadconnectionState_;}; + AudioA2dpOffloadManager(AudioPolicyService *audioPolicyService) : audioPolicyService_(audioPolicyService) {}; + void Init() {Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(shard_from_this());}; + A2dpOffloadConnectionState GetA2dOffloadConnectionState() {return currentOffloadconnectionState_;}; std::vector& GetConnectTriggerSessionIds() {return connectionTriggerSessionIds_;}; std::string GetBluetoothAddress() {return a2dpOffloadDeviceAddress_;}; diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 924d8db521..32c0c5e57d 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -350,7 +350,8 @@ bool AudioPolicyService::Init(void) audioEffectManager_.EffectManagerInit(); audioDeviceManager_.ParseDeviceXml(); audioPnpServer_.init(); - audioA2dpOffloadManager_ = new AudioA2dpOffloadManager(this); + audioA2dpOffloadManager_ = std::make_shared(this); + if (audioA2dpOffloadManager_ != nullptr) {audioA2dpOffloadManager_->Init();} bool ret = audioPolicyConfigParser_.LoadConfiguration(); if (!ret) { @@ -4940,7 +4941,7 @@ int32_t AudioPolicyService::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo UpdateA2dpOffloadFlagForAllStream(currentActiveDevice_.deviceType_); - if ((rendererState == RENDERER_RUNNING) && + if ((rendererState == RENDERER_RUNNING) && (audioA2dpOffloadManager_ != nullptr) && !audioA2dpOffloadManager_->IsA2dpOffloadConnecting(streamChangeInfo.audioRendererChangeInfo.sessionId)) { AUDIO_INFO_LOG("Notify client not to block."); std::thread sendConnectedToClient(&AudioPolicyService::UpdateSessionConnectionState, @@ -6953,10 +6954,12 @@ int32_t AudioPolicyService::OffloadStartPlaying(const std::vector &sess return SUCCESS; } int32_t ret = Bluetooth::AudioA2dpManager::OffloadStartPlaying(sessionIds); - A2dpOffloadConnectionState state = audioA2dpOffloadManager_->getA2dOffloadConnectionState(); - if (ret == SUCCESS && (state == CONNECTION_STATUS_DISCONNECTED || state == CONNECTION_STATUS_DISCONNECTING)) { - audioA2dpOffloadManager_->ConnectA2dpOffload( - Bluetooth::AudioA2dpManager::GetActiveA2dpDevice(), sessionIds); + if (audioA2dpOffloadManager_ != nullptr) { + A2dpOffloadConnectionState state = audioA2dpOffloadManager_->GetA2dOffloadConnectionState(); + if (ret == SUCCESS && (state == CONNECTION_STATUS_DISCONNECTED || state == CONNECTION_STATUS_DISCONNECTING)) { + audioA2dpOffloadManager_->ConnectA2dpOffload( + Bluetooth::AudioA2dpManager::GetActiveA2dpDevice(), sessionIds); + } } return ret; #else @@ -6973,7 +6976,9 @@ int32_t AudioPolicyService::OffloadStopPlaying(const std::vector &sessi return SUCCESS; } int32_t ret = Bluetooth::AudioA2dpManager::OffloadStopPlaying(sessionIds); - audioA2dpOffloadManager_->DisconnectA2dpOffload(); + if (audioA2dpOffloadManager_ != nullptr) { + audioA2dpOffloadManager_->DisconnectA2dpOffload(); + } return ret; #else return SUCCESS; @@ -8379,7 +8384,8 @@ void AudioPolicyService::ResetOffloadModeOnSpatializationChanged(std::vectorgetA2dOffloadConnectionState() == CONNECTION_STATUS_CONNECTED; + return audioA2dpOffloadManager_ != nullptr && + audioA2dpOffloadManager_->GetA2dOffloadConnectionState() == CONNECTION_STATUS_CONNECTED; } void AudioPolicyService::UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) -- Gitee From c0943f80cdf91698a6346db8fd727e841c7f1b58 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Tue, 30 Jul 2024 16:08:12 +0800 Subject: [PATCH 10/23] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../server/include/service/audio_policy_service.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 3c670ab47f..4cb1a25c7b 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -1149,7 +1149,7 @@ private: bool ringerModeMute_ = true; std::atomic isPolicyConfigParsered_ = false; - std::share_ptr audioA2dpOffloadManager_ = nullptr; + std::shared_ptr audioA2dpOffloadManager_ = nullptr; }; class AudioA2dpOffloadManager final : public Bluetooth::AudioA2dpPlayingStateChangedListener, @@ -1164,7 +1164,7 @@ private: static const int32_t CONNECTION_TIMEOUT_IN_MS = 300; // 300ms public: AudioA2dpOffloadManager(AudioPolicyService *audioPolicyService) : audioPolicyService_(audioPolicyService) {}; - void Init() {Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(shard_from_this());}; + void Init() {Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(shared_from_this());}; A2dpOffloadConnectionState GetA2dOffloadConnectionState() {return currentOffloadconnectionState_;}; std::vector& GetConnectTriggerSessionIds() {return connectionTriggerSessionIds_;}; std::string GetBluetoothAddress() {return a2dpOffloadDeviceAddress_;}; -- Gitee From 662f98e8bd1cccf58af45225ecbee9f5ea013f99 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Tue, 30 Jul 2024 16:51:16 +0800 Subject: [PATCH 11/23] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../include/service/audio_policy_service.h | 2 ++ .../server/src/service/audio_policy_service.cpp | 16 ++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 4cb1a25c7b..cbf357c10f 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -981,6 +981,8 @@ private: bool IsA2dpOffloadConnected(); + void SendA2dpConnectedWhileRunning(const RendererState &rendererState, const uint32_t &sessionId); + bool isUpdateRouteSupported_ = true; bool isCurrentRemoteRenderer = false; bool remoteCapturerSwitch_ = false; diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 32c0c5e57d..552cdf2a1f 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -4940,15 +4940,19 @@ int32_t AudioPolicyService::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo } UpdateA2dpOffloadFlagForAllStream(currentActiveDevice_.deviceType_); + SendA2dpConnectedWhileRunning(rendererState, streamChangeInfo.audioRendererChangeInfo.sessionId); + return ret; +} +void AudioPolicyService::SendA2dpConnectedWhileRunning(const RendererState &rendererState, const uint32_t &sessionId) +{ if ((rendererState == RENDERER_RUNNING) && (audioA2dpOffloadManager_ != nullptr) && - !audioA2dpOffloadManager_->IsA2dpOffloadConnecting(streamChangeInfo.audioRendererChangeInfo.sessionId)) { + !audioA2dpOffloadManager_->IsA2dpOffloadConnecting(sessionId)) { AUDIO_INFO_LOG("Notify client not to block."); - std::thread sendConnectedToClient(&AudioPolicyService::UpdateSessionConnectionState, - this, streamChangeInfo.audioRendererChangeInfo.sessionId, DATA_LINK_CONNECTED); + std::thread sendConnectedToClient(&AudioPolicyService::UpdateSessionConnectionState, this, sessionId, + DATA_LINK_CONNECTED); sendConnectedToClient.detach(); } - return ret; } void AudioPolicyService::FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo, @@ -7137,14 +7141,14 @@ int32_t AudioPolicyService::HandleA2dpDeviceInOffload(BluetoothOffloadState a2dp GetA2dpOffloadCodecAndSendToDsp(); std::vector allSessions; GetAllRunningStreamSession(allSessions); - int32_t ret = OffloadStartPlaying(allSessions); + OffloadStartPlaying(allSessions); DeviceType dev = GetActiveOutputDevice(); UpdateEffectDefaultSink(dev); AUDIO_INFO_LOG("Handle A2dpDevice In Offload"); UpdateEffectBtOffloadSupported(true); - if (ret == SUCCESS && IsA2dpOffloadConnected()) { + if (IsA2dpOffloadConnected()) { AUDIO_INFO_LOG("A2dpOffload has been connected, Fetch stream"); FetchStreamForA2dpOffload(true); } -- Gitee From c97e4595425368adf4b1820318d2fc52c26e71a9 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Thu, 1 Aug 2024 19:38:50 +0800 Subject: [PATCH 12/23] conflicts Signed-off-by: chuxixue --- services/audio_service/server/include/audio_server.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/services/audio_service/server/include/audio_server.h b/services/audio_service/server/include/audio_server.h index fc69090ad6..168baafe41 100644 --- a/services/audio_service/server/include/audio_server.h +++ b/services/audio_service/server/include/audio_server.h @@ -159,12 +159,10 @@ public: void LoadHdiEffectModel() override; void UpdateEffectBtOffloadSupported(const bool &isSupported) override; + + void SetRotationToEffect(const uint32_t rotate) override; -<<<<<<< HEAD void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) override; -======= - void SetRotationToEffect(const uint32_t rotate) override; ->>>>>>> upstream/master protected: void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; -- Gitee From ad41f4ce130e3fb3a764d3b46d5be6c6399965e7 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Sun, 4 Aug 2024 21:21:04 +0800 Subject: [PATCH 13/23] =?UTF-8?q?=E9=9B=86=E4=BD=93=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../bluetoothclient/audio_bluetooth_manager.cpp | 15 +++++++++++---- .../bluetoothclient/audio_bluetooth_manager.h | 7 +++---- .../server/include/service/audio_policy_service.h | 2 +- .../server/src/service/audio_policy_service.cpp | 9 ++++----- .../audio_service/server/include/audio_server.h | 2 +- .../audio_service/server/src/audio_server.cpp | 3 ++- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index b889d33815..b58fc56a31 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -39,7 +39,7 @@ AudioScene AudioHfpManager::scene_ = AUDIO_SCENE_DEFAULT; AudioScene AudioHfpManager::sceneFromPolicy_ = AUDIO_SCENE_DEFAULT; OHOS::Bluetooth::ScoCategory AudioHfpManager::scoCategory = OHOS::Bluetooth::ScoCategory::SCO_DEFAULT; BluetoothRemoteDevice AudioHfpManager::activeHfpDevice_; -std::vector> AudioA2dpManager::stateChangedListeners_; +std::vector> AudioA2dpManager::a2dpPlayingStateChangedListeners_; std::mutex g_activehfpDeviceLock; std::mutex g_audioSceneLock; std::mutex g_hfpInstanceLock; @@ -217,10 +217,17 @@ int32_t AudioA2dpManager::OffloadStopPlaying(const std::vector &session int32_t AudioA2dpManager::RegisterA2dpPlayingStateChangedListener( std::shared_ptr listener) { - stateChangedListeners_.push_back(listener); + a2dpPlayingStateChangedListeners_.push_back(listener); return SUCCESS; } +void AudioA2dpManager::OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState) +{ + for (auto listener : a2dpPlayingStateChangedListeners_) { + listener->OnA2dpPlayingStateChanged(deviceAddress, playingState); + } +} + void AudioA2dpManager::CheckA2dpDeviceReconnect() { if (a2dpInstance_ == nullptr) { @@ -272,8 +279,8 @@ void AudioA2dpListener::OnConfigurationChanged(const BluetoothRemoteDevice &devi void AudioA2dpListener::OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error) { AUDIO_INFO_LOG("OnPlayingStatusChanged, state: %{public}d, error: %{public}d", playingState, error); - for (auto listener : AudioA2dpManager::stateChangedListeners_) { - listener->OnA2dpPlayingStateChanged(device.GetDeviceAddr(), playingState, error); + if (error == SUCCESS) { + AudioA2dpManager::OnA2dpPlayingStateChanged(device.GetDeviceAddr(), playingState); } } diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h index 5d361bfe8a..805db13052 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -27,7 +27,7 @@ namespace OHOS { namespace Bluetooth { class AudioA2dpPlayingStateChangedListener { public: - virtual void OnA2dpPlayingStateChanged(std::string deviceAddress, int playingState, int error) = 0; + virtual void OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState) = 0; }; // Audio bluetooth a2dp feature support @@ -49,9 +49,6 @@ class AudioA2dpManager { public: AudioA2dpManager() = default; virtual ~AudioA2dpManager() = default; - - static std::vector> stateChangedListeners_; - static void RegisterBluetoothA2dpListener(); static void UnregisterBluetoothA2dpListener(); static void DisconnectBluetoothA2dpSink(); @@ -67,6 +64,7 @@ public: static int32_t OffloadStopPlaying(const std::vector &sessionsID); static int32_t RegisterA2dpPlayingStateChangedListener( std::shared_ptr listener); + staitc void OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState); static void SetConnectionState(int state) { @@ -86,6 +84,7 @@ private: static std::shared_ptr a2dpListener_; static int connectionState_; static BluetoothRemoteDevice activeA2dpDevice_; + static std::vector> a2dpPlayingStateChangedListeners_; }; // Audio bluetooth sco feature support diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 554725e33f..24ca88591a 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -1175,7 +1175,7 @@ public: void ConnectA2dpOffload(const std::string &deviceAddress, const vector &sessionIds); void DisconnectA2dpOffload(); - void OnA2dpPlayingStateChanged(std::string deviceAddress, int playingState, int error) override; + void OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState) override; void WaitForConnectionCompleted(); bool IsA2dpOffloadConnecting(int32_t sessionId); diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 99afddb625..6d18eed7a6 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -4910,10 +4910,9 @@ int32_t AudioPolicyService::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo return ret; // only update tracker in new and prepared } - if (rendererState == RENDERER_RELEASED) { - if (!streamCollector_.ExistStreamForPipe(PIPE_TYPE_MULTICHANNEL)) { - DynamicUnloadModule(PIPE_TYPE_MULTICHANNEL); - } + if (rendererState == RENDERER_RELEASED && !streamCollector_.ExistStreamForPipe(PIPE_TYPE_MULTICHANNEL)) { + DynamicUnloadModule(PIPE_TYPE_MULTICHANNEL); + } if (mode == AUDIO_MODE_PLAYBACK && (rendererState == RENDERER_STOPPED || rendererState == RENDERER_PAUSED)) { FetchDevice(true); @@ -8414,7 +8413,7 @@ void AudioPolicyService::UpdateSessionConnectionState(const int32_t &sessionID, IPCSkeleton::SetCallingIdentity(identity); } -void AudioA2dpOffloadManager::OnA2dpPlayingStateChanged(std::string deviceAddress, int playingState, int error) +void AudioA2dpOffloadManager::OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState) { if (deviceAddress == a2dpOffloadDeviceAddress_) { if (playingState == A2DP_PLAYING && currentOffloadconnectionState_ == CONNECTION_STATUS_CONNECTING) { diff --git a/services/audio_service/server/include/audio_server.h b/services/audio_service/server/include/audio_server.h index 36e31fff26..ac0895d2c0 100644 --- a/services/audio_service/server/include/audio_server.h +++ b/services/audio_service/server/include/audio_server.h @@ -148,7 +148,7 @@ public: void LoadHdiEffectModel() override; void UpdateEffectBtOffloadSupported(const bool &isSupported) override; - + void SetRotationToEffect(const uint32_t rotate) override; void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) override; diff --git a/services/audio_service/server/src/audio_server.cpp b/services/audio_service/server/src/audio_server.cpp index e2b7d1bb45..7ae443db23 100644 --- a/services/audio_service/server/src/audio_server.cpp +++ b/services/audio_service/server/src/audio_server.cpp @@ -2100,7 +2100,8 @@ void AudioServer::UpdateSessionConnectionState(const int32_t &sessionId, const i { AUDIO_INFO_LOG("Server get sessionID: %{public}d, state: %{public}d", sessionId, state); int32_t callingUid = IPCSkeleton::GetCallingUid(); - CHECK_AND_RETURN_LOG(callingUid == audioUid_ || callingUid == ROOT_UID, "refused for %{public}d", callingUid); + CHECK_AND_RETURN_LOG(PermissionUtil::VerifyIsAudio(), + "Update session connection state refused for %{public}d", callingUid); std::shared_ptr renderer = AudioService::GetInstance()->GetRendererBySessionID(static_cast(sessionId)); -- Gitee From 7af0648107f5f44f21454a78c9cc483780cb3656 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Sun, 4 Aug 2024 21:26:14 +0800 Subject: [PATCH 14/23] =?UTF-8?q?=E9=9B=86=E4=BD=93=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../audio_policy/server/src/service/audio_policy_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 6d18eed7a6..e9f309fa06 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -4909,7 +4909,7 @@ int32_t AudioPolicyService::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo if (rendererState == RENDERER_PREPARED || rendererState == RENDERER_NEW || rendererState == RENDERER_INVALID) { return ret; // only update tracker in new and prepared } - + if (rendererState == RENDERER_RELEASED && !streamCollector_.ExistStreamForPipe(PIPE_TYPE_MULTICHANNEL)) { DynamicUnloadModule(PIPE_TYPE_MULTICHANNEL); } -- Gitee From 4e0f42b4d962f874d63f0229bbb7ad3c4a86e5de Mon Sep 17 00:00:00 2001 From: chuxixue Date: Mon, 5 Aug 2024 14:23:20 +0800 Subject: [PATCH 15/23] =?UTF-8?q?seek=E5=92=8C=E5=88=87=E6=AD=8C=E4=B8=8D?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E9=98=BB=E5=A1=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../bluetoothclient/audio_bluetooth_manager.h | 2 +- .../server/src/service/audio_policy_service.cpp | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h index 805db13052..e7bff5bcde 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.h +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.h @@ -64,7 +64,7 @@ public: static int32_t OffloadStopPlaying(const std::vector &sessionsID); static int32_t RegisterA2dpPlayingStateChangedListener( std::shared_ptr listener); - staitc void OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState); + static void OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState); static void SetConnectionState(int state) { diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index e9f309fa06..63acc6b803 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -8387,6 +8387,7 @@ void AudioPolicyService::ResetOffloadModeOnSpatializationChanged(std::vector gsp = GetAudioServerProxy(); @@ -8399,8 +8400,12 @@ void AudioPolicyService::SetRotationToEffect(const uint32_t rotate) bool AudioPolicyService::IsA2dpOffloadConnected() { - return audioA2dpOffloadManager_ != nullptr && - audioA2dpOffloadManager_->GetA2dOffloadConnectionState() == CONNECTION_STATUS_CONNECTED; + if (audioA2dpOffloadManager_ == nullptr) { + AUDIO_WARNING_LOG("Null audioA2dpOffloadManager"); + return true; + } + A2dpOffloadConnectionState state = audioA2dpOffloadManager_->GetA2dOffloadConnectionState(); + return state == CONNECTION_STATUS_CONNECTED || state == CONNECTION_STATUS_DISCONNECTING; } void AudioPolicyService::UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) @@ -8439,6 +8444,13 @@ void AudioA2dpOffloadManager::ConnectA2dpOffload(const std::string &deviceAddres a2dpOffloadDeviceAddress_ = deviceAddress; connectionTriggerSessionIds_.assign(sessionIds.begin(), sessionIds.end()); + if (currentOffloadconnectionState_ == CONNECTION_STATUS_DISCONNECTING) { + AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", + currentOffloadconnectionState_, CONNECTION_STATUS_CONNECTED); + currentOffloadconnectionState_ = CONNECTION_STATUS_CONNECTED; + return; + } + for (int32_t sessionId : connectionTriggerSessionIds_) { audioPolicyService_->UpdateSessionConnectionState(sessionId, DATA_LINK_CONNECTING); } -- Gitee From b2e97d51fab67c794a60bdd678b063e38e7cb3a5 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Mon, 5 Aug 2024 14:32:22 +0800 Subject: [PATCH 16/23] =?UTF-8?q?seek=E5=92=8C=E5=88=87=E6=AD=8C=E4=B8=8D?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E9=98=BB=E5=A1=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../audio_policy/server/src/service/audio_policy_service.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index 63acc6b803..a7e2c5fd79 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -8387,7 +8387,6 @@ void AudioPolicyService::ResetOffloadModeOnSpatializationChanged(std::vector gsp = GetAudioServerProxy(); -- Gitee From f6e0d2f1cd257a157347d3c2eaf5080463b8d08b Mon Sep 17 00:00:00 2001 From: chuxixue Date: Mon, 5 Aug 2024 15:26:40 +0800 Subject: [PATCH 17/23] =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- services/audio_service/client/include/audio_manager_base.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/services/audio_service/client/include/audio_manager_base.h b/services/audio_service/client/include/audio_manager_base.h index 80b3e6298b..b8447d76d9 100644 --- a/services/audio_service/client/include/audio_manager_base.h +++ b/services/audio_service/client/include/audio_manager_base.h @@ -428,6 +428,11 @@ public: * Set Rotation To Effect. */ virtual void SetRotationToEffect(const uint32_t rotate) = 0; + + /** + * Update Session Connection State + */ + virtual void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state) = 0; public: DECLARE_INTERFACE_DESCRIPTOR(u"IStandardAudioService"); }; -- Gitee From cbfbfcdaa926630d18e43fc19a25e172dadd3197 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Mon, 5 Aug 2024 19:02:24 +0800 Subject: [PATCH 18/23] =?UTF-8?q?commiter=E6=A3=80=E8=A7=86=E6=84=8F?= =?UTF-8?q?=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../audio_bluetooth_manager.cpp | 3 ++ .../include/service/audio_policy_service.h | 4 +- .../src/service/audio_policy_service.cpp | 44 +++++++++---------- .../client/src/renderer_in_client.cpp | 2 +- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index b6601eb2e2..547ea75550 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -43,6 +43,7 @@ std::vector> AudioA2dpMana std::mutex g_activehfpDeviceLock; std::mutex g_audioSceneLock; std::mutex g_hfpInstanceLock; +std::mutex g_a2dpPlayingStateChangedLock; static bool GetAudioStreamInfo(A2dpCodecInfo codecInfo, AudioStreamInfo &audioStreamInfo) { @@ -226,12 +227,14 @@ int32_t AudioA2dpManager::GetRenderPosition(uint32_t &delayValue, uint64_t &send int32_t AudioA2dpManager::RegisterA2dpPlayingStateChangedListener( std::shared_ptr listener) { + std::lock_guard lock(g_a2dpPlayingStateChangedLock); a2dpPlayingStateChangedListeners_.push_back(listener); return SUCCESS; } void AudioA2dpManager::OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState) { + std::lock_guard lock(g_a2dpPlayingStateChangedLock); for (auto listener : a2dpPlayingStateChangedListeners_) { listener->OnA2dpPlayingStateChanged(deviceAddress, playingState); } diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index aa6defbb07..41322718fb 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -1161,7 +1161,7 @@ private: class AudioA2dpOffloadManager final : public Bluetooth::AudioA2dpPlayingStateChangedListener, public enable_shared_from_this { private: - A2dpOffloadConnectionState currentOffloadconnectionState_ = CONNECTION_STATUS_DISCONNECTED; + A2dpOffloadConnectionState currentOffloadConnectionState_ = CONNECTION_STATUS_DISCONNECTED; std::vector connectionTriggerSessionIds_; std::string a2dpOffloadDeviceAddress_ = ""; AudioPolicyService *audioPolicyService_ = nullptr; @@ -1171,7 +1171,7 @@ private: public: AudioA2dpOffloadManager(AudioPolicyService *audioPolicyService) : audioPolicyService_(audioPolicyService) {}; void Init() {Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(shared_from_this());}; - A2dpOffloadConnectionState GetA2dOffloadConnectionState() {return currentOffloadconnectionState_;}; + A2dpOffloadConnectionState GetA2dOffloadConnectionState() {return currentOffloadConnectionState_;}; std::vector& GetConnectTriggerSessionIds() {return connectionTriggerSessionIds_;}; std::string GetBluetoothAddress() {return a2dpOffloadDeviceAddress_;}; diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index eb702011e1..efaee78035 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -8441,10 +8441,10 @@ void AudioPolicyService::UpdateSessionConnectionState(const int32_t &sessionID, void AudioA2dpOffloadManager::OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState) { if (deviceAddress == a2dpOffloadDeviceAddress_) { - if (playingState == A2DP_PLAYING && currentOffloadconnectionState_ == CONNECTION_STATUS_CONNECTING) { - AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", - currentOffloadconnectionState_, CONNECTION_STATUS_CONNECTED); - currentOffloadconnectionState_ = CONNECTION_STATUS_CONNECTED; + if (playingState == A2DP_PLAYING && currentOffloadConnectionState_ == CONNECTION_STATUS_CONNECTING) { + AUDIO_INFO_LOG("currentOffloadConnectionState_ change from %{public}d to %{public}d", + currentOffloadConnectionState_, CONNECTION_STATUS_CONNECTED); + currentOffloadConnectionState_ = CONNECTION_STATUS_CONNECTED; for (int32_t sessionId : connectionTriggerSessionIds_) { audioPolicyService_->UpdateSessionConnectionState(sessionId, DATA_LINK_CONNECTED); } @@ -8452,9 +8452,9 @@ void AudioA2dpOffloadManager::OnA2dpPlayingStateChanged(const std::string &devic connectionCV_.notify_all(); } } else { - AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", - currentOffloadconnectionState_, CONNECTION_STATUS_DISCONNECTED); - currentOffloadconnectionState_ = CONNECTION_STATUS_DISCONNECTED; + AUDIO_INFO_LOG("currentOffloadConnectionState_ change from %{public}d to %{public}d", + currentOffloadConnectionState_, CONNECTION_STATUS_DISCONNECTED); + currentOffloadConnectionState_ = CONNECTION_STATUS_DISCONNECTED; } } @@ -8464,10 +8464,10 @@ void AudioA2dpOffloadManager::ConnectA2dpOffload(const std::string &deviceAddres a2dpOffloadDeviceAddress_ = deviceAddress; connectionTriggerSessionIds_.assign(sessionIds.begin(), sessionIds.end()); - if (currentOffloadconnectionState_ == CONNECTION_STATUS_DISCONNECTING) { - AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", - currentOffloadconnectionState_, CONNECTION_STATUS_CONNECTED); - currentOffloadconnectionState_ = CONNECTION_STATUS_CONNECTED; + if (currentOffloadConnectionState_ == CONNECTION_STATUS_DISCONNECTING) { + AUDIO_INFO_LOG("currentOffloadConnectionState_ change from %{public}d to %{public}d", + currentOffloadConnectionState_, CONNECTION_STATUS_CONNECTED); + currentOffloadConnectionState_ = CONNECTION_STATUS_CONNECTED; return; } @@ -8477,17 +8477,17 @@ void AudioA2dpOffloadManager::ConnectA2dpOffload(const std::string &deviceAddres std::thread switchThread(&AudioA2dpOffloadManager::WaitForConnectionCompleted, this); switchThread.detach(); - AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", - currentOffloadconnectionState_, CONNECTION_STATUS_CONNECTING); - currentOffloadconnectionState_ = CONNECTION_STATUS_CONNECTING; + AUDIO_INFO_LOG("currentOffloadConnectionState_ change from %{public}d to %{public}d", + currentOffloadConnectionState_, CONNECTION_STATUS_CONNECTING); + currentOffloadConnectionState_ = CONNECTION_STATUS_CONNECTING; } void AudioA2dpOffloadManager::DisconnectA2dpOffload() { a2dpOffloadDeviceAddress_ = ""; - AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", - currentOffloadconnectionState_, CONNECTION_STATUS_DISCONNECTING); - currentOffloadconnectionState_ = CONNECTION_STATUS_DISCONNECTING; + AUDIO_INFO_LOG("currentOffloadConnectionState_ change from %{public}d to %{public}d", + currentOffloadConnectionState_, CONNECTION_STATUS_DISCONNECTING); + currentOffloadConnectionState_ = CONNECTION_STATUS_DISCONNECTING; } void AudioA2dpOffloadManager::WaitForConnectionCompleted() @@ -8495,13 +8495,13 @@ void AudioA2dpOffloadManager::WaitForConnectionCompleted() std::unique_lock waitLock(connectionMutex_); bool connectionCompleted = connectionCV_.wait_for(waitLock, std::chrono::milliseconds(AudioA2dpOffloadManager::CONNECTION_TIMEOUT_IN_MS), [this] { - return currentOffloadconnectionState_ == CONNECTION_STATUS_CONNECTED; + return currentOffloadConnectionState_ == CONNECTION_STATUS_CONNECTED; }); // a2dp connection timeout, anyway we should notify client dataLink OK in order to allow the data flow begin if (!connectionCompleted) { - AUDIO_INFO_LOG("currentOffloadconnectionState_ change from %{public}d to %{public}d", - currentOffloadconnectionState_, CONNECTION_STATUS_TIMEOUT); - currentOffloadconnectionState_ = CONNECTION_STATUS_CONNECTED; + AUDIO_INFO_LOG("currentOffloadConnectionState_ change from %{public}d to %{public}d", + currentOffloadConnectionState_, CONNECTION_STATUS_TIMEOUT); + currentOffloadConnectionState_ = CONNECTION_STATUS_CONNECTED; for (int32_t sessionId : connectionTriggerSessionIds_) { audioPolicyService_->UpdateSessionConnectionState(sessionId, DATA_LINK_CONNECTED); } @@ -8514,7 +8514,7 @@ void AudioA2dpOffloadManager::WaitForConnectionCompleted() bool AudioA2dpOffloadManager::IsA2dpOffloadConnecting(int32_t sessionId) { - if (currentOffloadconnectionState_ == CONNECTION_STATUS_CONNECTING) { + if (currentOffloadConnectionState_ == CONNECTION_STATUS_CONNECTING) { if (std::find(connectionTriggerSessionIds_.begin(), connectionTriggerSessionIds_.end(), sessionId) != connectionTriggerSessionIds_.end()) { return true; diff --git a/services/audio_service/client/src/renderer_in_client.cpp b/services/audio_service/client/src/renderer_in_client.cpp index 5b18747c51..6fbf64488b 100644 --- a/services/audio_service/client/src/renderer_in_client.cpp +++ b/services/audio_service/client/src/renderer_in_client.cpp @@ -1253,7 +1253,7 @@ bool RendererInClientInner::StartAudioStream(StateChangeCmdType cmdType, dataConnectionWaitLock, std::chrono::milliseconds(DATA_CONNECTION_TIMEOUT_IN_MS), [this] { return isDataLinkConnected_; }); - AUDIO_DEBUG_LOG("data-connection blocking ends."); + AUDIO_INFO_LOG("data-connection blocking ends."); } dataConnectionWaitLock.unlock(); -- Gitee From 57b5f2eeae11cd328fc057df9ab5b872e7070603 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Mon, 5 Aug 2024 20:00:19 +0800 Subject: [PATCH 19/23] =?UTF-8?q?commiter=E6=A3=80=E8=A7=86=E6=84=8F?= =?UTF-8?q?=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../audio_policy/server/src/service/audio_policy_service.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index efaee78035..e20821314a 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -8484,6 +8484,10 @@ void AudioA2dpOffloadManager::ConnectA2dpOffload(const std::string &deviceAddres void AudioA2dpOffloadManager::DisconnectA2dpOffload() { + if (currentOffloadConnectionState_ == CONNECTION_STATUS_DISCONNECTED || + currentOffloadConnectionState_ == CONNECTION_STATUS_DISCONNECTING) { + return; + } a2dpOffloadDeviceAddress_ = ""; AUDIO_INFO_LOG("currentOffloadConnectionState_ change from %{public}d to %{public}d", currentOffloadConnectionState_, CONNECTION_STATUS_DISCONNECTING); -- Gitee From 5aa0e8246ca617bef6d4d4e6585d2f39048008d0 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Tue, 6 Aug 2024 09:38:42 +0800 Subject: [PATCH 20/23] =?UTF-8?q?=E9=9B=86=E4=BD=93=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../include/service/audio_policy_service.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 41322718fb..a799721cb0 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -1160,20 +1160,10 @@ private: class AudioA2dpOffloadManager final : public Bluetooth::AudioA2dpPlayingStateChangedListener, public enable_shared_from_this { -private: - A2dpOffloadConnectionState currentOffloadConnectionState_ = CONNECTION_STATUS_DISCONNECTED; - std::vector connectionTriggerSessionIds_; - std::string a2dpOffloadDeviceAddress_ = ""; - AudioPolicyService *audioPolicyService_ = nullptr; - std::mutex connectionMutex_; - std::condition_variable connectionCV_; - static const int32_t CONNECTION_TIMEOUT_IN_MS = 300; // 300ms public: AudioA2dpOffloadManager(AudioPolicyService *audioPolicyService) : audioPolicyService_(audioPolicyService) {}; void Init() {Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(shared_from_this());}; A2dpOffloadConnectionState GetA2dOffloadConnectionState() {return currentOffloadConnectionState_;}; - std::vector& GetConnectTriggerSessionIds() {return connectionTriggerSessionIds_;}; - std::string GetBluetoothAddress() {return a2dpOffloadDeviceAddress_;}; void ConnectA2dpOffload(const std::string &deviceAddress, const vector &sessionIds); void DisconnectA2dpOffload(); @@ -1181,6 +1171,14 @@ public: void WaitForConnectionCompleted(); bool IsA2dpOffloadConnecting(int32_t sessionId); +private: + A2dpOffloadConnectionState currentOffloadConnectionState_ = CONNECTION_STATUS_DISCONNECTED; + std::vector connectionTriggerSessionIds_; + std::string a2dpOffloadDeviceAddress_ = ""; + AudioPolicyService *audioPolicyService_ = nullptr; + std::mutex connectionMutex_; + std::condition_variable connectionCV_; + static const int32_t CONNECTION_TIMEOUT_IN_MS = 300; // 300ms }; } // namespace AudioStandard } // namespace OHOS -- Gitee From 6771207967fea096e5d74499554a00bc07f40169 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Tue, 6 Aug 2024 21:37:53 +0800 Subject: [PATCH 21/23] =?UTF-8?q?=E7=BC=A9=E7=9F=ADrenderer=5Fin=5Fclient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- services/audio_service/BUILD.gn | 1 + .../include/renderer_in_client_private.h | 14 ----- .../renderer_in_client_service_died_cb.h | 43 +++++++++++++ .../client/src/renderer_in_client.cpp | 36 ----------- .../renderer_in_client_service_died_cb.cpp | 61 +++++++++++++++++++ 5 files changed, 105 insertions(+), 50 deletions(-) create mode 100644 services/audio_service/client/include/renderer_in_client_service_died_cb.h create mode 100644 services/audio_service/client/src/renderer_in_client_service_died_cb.cpp diff --git a/services/audio_service/BUILD.gn b/services/audio_service/BUILD.gn index 4cb77d3364..df80554262 100644 --- a/services/audio_service/BUILD.gn +++ b/services/audio_service/BUILD.gn @@ -168,6 +168,7 @@ ohos_shared_library("audio_client") { "client/src/microphone_descriptor.cpp", "client/src/policy_provider_stub.cpp", "client/src/renderer_in_client.cpp", + "client/src/renderer_in_client_service_died_cb.cpp", ] public_configs = [ ":audio_client_public_config" ] diff --git a/services/audio_service/client/include/renderer_in_client_private.h b/services/audio_service/client/include/renderer_in_client_private.h index e20c8ef987..15b81b74a0 100644 --- a/services/audio_service/client/include/renderer_in_client_private.h +++ b/services/audio_service/client/include/renderer_in_client_private.h @@ -39,20 +39,6 @@ namespace OHOS { namespace AudioStandard { class SpatializationStateChangeCallbackImpl; -class RendererInClientPolicyServiceDiedCallbackImpl : public AudioStreamPolicyServiceDiedCallback { -public: - RendererInClientPolicyServiceDiedCallbackImpl(); - virtual ~RendererInClientPolicyServiceDiedCallbackImpl(); - void OnAudioPolicyServiceDied() override; - void SaveRendererOrCapturerPolicyServiceDiedCB( - const std::shared_ptr &callback); - void RemoveRendererOrCapturerPolicyServiceDiedCB(); - -private: - std::mutex mutex_; - std::shared_ptr policyServiceDiedCallback_; -}; - class RendererInClientInner : public RendererInClient, public IStreamListener, public IHandler, public std::enable_shared_from_this { public: diff --git a/services/audio_service/client/include/renderer_in_client_service_died_cb.h b/services/audio_service/client/include/renderer_in_client_service_died_cb.h new file mode 100644 index 0000000000..b2649fb9e7 --- /dev/null +++ b/services/audio_service/client/include/renderer_in_client_service_died_cb.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RENDERER_IN_CLIENT_SERVICE_DIED_CB_H +#define RENDERER_IN_CLIENT_SERVICE_DIED_CB_H + +#include +#include + +#include "audio_log.h" +#include "audio_info.h" +#include "i_audio_stream.h" + +namespace OHOS { +namespace AudioStandard { +class RendererInClientPolicyServiceDiedCallbackImpl : public AudioStreamPolicyServiceDiedCallback { +public: + RendererInClientPolicyServiceDiedCallbackImpl(); + virtual ~RendererInClientPolicyServiceDiedCallbackImpl(); + void OnAudioPolicyServiceDied() override; + void SaveRendererOrCapturerPolicyServiceDiedCB( + const std::shared_ptr &callback); + void RemoveRendererOrCapturerPolicyServiceDiedCB(); + +private: + std::mutex mutex_; + std::shared_ptr policyServiceDiedCallback_; +}; +} // namespace AudioStandard +} // namespace OHOS +#endif // RENDERER_IN_CLIENT_SERVICE_DIED_CB_H \ No newline at end of file diff --git a/services/audio_service/client/src/renderer_in_client.cpp b/services/audio_service/client/src/renderer_in_client.cpp index 83c023a41b..3ce148e993 100644 --- a/services/audio_service/client/src/renderer_in_client.cpp +++ b/services/audio_service/client/src/renderer_in_client.cpp @@ -2337,42 +2337,6 @@ void RendererInClientInner::ResetRingerModeMute() AudioPolicyManager::GetInstance().ResetRingerModeMute(); } } - -RendererInClientPolicyServiceDiedCallbackImpl::RendererInClientPolicyServiceDiedCallbackImpl() -{ - AUDIO_DEBUG_LOG("instance create"); -} - -RendererInClientPolicyServiceDiedCallbackImpl::~RendererInClientPolicyServiceDiedCallbackImpl() -{ - AUDIO_DEBUG_LOG("instance destory"); -} - -void RendererInClientPolicyServiceDiedCallbackImpl::OnAudioPolicyServiceDied() -{ - std::lock_guard lock(mutex_); - AUDIO_INFO_LOG("OnAudioPolicyServiceDied"); - if (policyServiceDiedCallback_ != nullptr) { - policyServiceDiedCallback_->OnAudioPolicyServiceDied(); - } -} - -void RendererInClientPolicyServiceDiedCallbackImpl::SaveRendererOrCapturerPolicyServiceDiedCB( - const std::shared_ptr &callback) -{ - std::lock_guard lock(mutex_); - if (callback != nullptr) { - policyServiceDiedCallback_ = callback; - } -} - -void RendererInClientPolicyServiceDiedCallbackImpl::RemoveRendererOrCapturerPolicyServiceDiedCB() -{ - std::lock_guard lock(mutex_); - if (policyServiceDiedCallback_ != nullptr) { - policyServiceDiedCallback_ = nullptr; - } -} } // namespace AudioStandard } // namespace OHOS #endif // FAST_AUDIO_STREAM_H diff --git a/services/audio_service/client/src/renderer_in_client_service_died_cb.cpp b/services/audio_service/client/src/renderer_in_client_service_died_cb.cpp new file mode 100644 index 0000000000..c9e788da87 --- /dev/null +++ b/services/audio_service/client/src/renderer_in_client_service_died_cb.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LOG_TAG +#define LOG_TAG "RendererInClientPolicyServiceDiedCb" +#endif + +#include "renderer_in_client_service_died_cb.h" + +namespace OHOS { +namespace AudioStandard { +RendererInClientPolicyServiceDiedCallbackImpl::RendererInClientPolicyServiceDiedCallbackImpl() +{ + AUDIO_DEBUG_LOG("instance create"); +} + +RendererInClientPolicyServiceDiedCallbackImpl::~RendererInClientPolicyServiceDiedCallbackImpl() +{ + AUDIO_DEBUG_LOG("instance destory"); +} + +void RendererInClientPolicyServiceDiedCallbackImpl::OnAudioPolicyServiceDied() +{ + std::lock_guard lock(mutex_); + AUDIO_INFO_LOG("OnAudioPolicyServiceDied"); + if (policyServiceDiedCallback_ != nullptr) { + policyServiceDiedCallback_->OnAudioPolicyServiceDied(); + } +} + +void RendererInClientPolicyServiceDiedCallbackImpl::SaveRendererOrCapturerPolicyServiceDiedCB( + const std::shared_ptr &callback) +{ + std::lock_guard lock(mutex_); + if (callback != nullptr) { + policyServiceDiedCallback_ = callback; + } +} + +void RendererInClientPolicyServiceDiedCallbackImpl::RemoveRendererOrCapturerPolicyServiceDiedCB() +{ + std::lock_guard lock(mutex_); + if (policyServiceDiedCallback_ != nullptr) { + policyServiceDiedCallback_ = nullptr; + } +} +} // namespace AudioStandard +} // namespace OHOS +#endif \ No newline at end of file -- Gitee From 6eba4fad680c252aaa1a9a260d3862d377530949 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Tue, 6 Aug 2024 21:47:53 +0800 Subject: [PATCH 22/23] =?UTF-8?q?=E7=BC=A9=E7=9F=ADrenderer=5Fin=5Fclient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../client/src/renderer_in_client_service_died_cb.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/audio_service/client/src/renderer_in_client_service_died_cb.cpp b/services/audio_service/client/src/renderer_in_client_service_died_cb.cpp index c9e788da87..676092d413 100644 --- a/services/audio_service/client/src/renderer_in_client_service_died_cb.cpp +++ b/services/audio_service/client/src/renderer_in_client_service_died_cb.cpp @@ -57,5 +57,4 @@ void RendererInClientPolicyServiceDiedCallbackImpl::RemoveRendererOrCapturerPoli } } } // namespace AudioStandard -} // namespace OHOS -#endif \ No newline at end of file +} // namespace OHOS \ No newline at end of file -- Gitee From 057fdf5ac9b1c57021757dcb99d70b394812a237 Mon Sep 17 00:00:00 2001 From: chuxixue Date: Tue, 6 Aug 2024 22:01:50 +0800 Subject: [PATCH 23/23] =?UTF-8?q?=E7=BC=A9=E7=9F=ADrenderer=5Fin=5Fclient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chuxixue --- .../audio_service/client/include/renderer_in_client_private.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/audio_service/client/include/renderer_in_client_private.h b/services/audio_service/client/include/renderer_in_client_private.h index 15b81b74a0..a898be7930 100644 --- a/services/audio_service/client/include/renderer_in_client_private.h +++ b/services/audio_service/client/include/renderer_in_client_private.h @@ -34,6 +34,7 @@ #include "audio_spatial_channel_converter.h" #include "audio_policy_manager.h" #include "audio_spatialization_manager.h" +#include "renderer_in_client_service_died_cb.h" namespace OHOS { namespace AudioStandard { -- Gitee