From 9a984c5ee8658498dc3fed5c991562b08f174777 Mon Sep 17 00:00:00 2001 From: zhangwenting Date: Mon, 26 Aug 2024 10:16:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=98=AF=E5=90=A6=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=9F=B3=E9=A2=91=E8=AE=BE=E5=A4=87=E5=BC=B9=E5=87=BA?= =?UTF-8?q?=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangwenting --- .../include/audio_policy_manager.h | 4 ++ .../include/audio_routing_manager.h | 3 + .../include/audio_system_manager.h | 7 +++ .../client/include/audio_policy_base.h | 4 ++ .../include/audio_policy_manager_stub.h | 3 + .../client/include/audio_policy_proxy.h | 4 ++ .../audio_routing_manager_listener_stub.h | 4 ++ .../client/src/audio_policy_manager.cpp | 31 ++++++++++ .../audio_routing_manager_listener_stub.cpp | 26 +++++++++ .../client/src/proxy/audio_policy_proxy.cpp | 33 +++++++++++ .../include/audio_policy_ipc_interface_code.h | 4 +- ..._standard_audio_routing_manager_listener.h | 3 +- .../server/include/audio_policy_server.h | 4 ++ .../audio_routing_manager_listener_proxy.h | 1 + .../include/service/audio_policy_service.h | 4 ++ .../service/listener/device_status_listener.h | 9 +++ .../manager/pnp_server/audio_pnp_param.h | 8 ++- .../manager/pnp_server/audio_socket_thread.h | 2 + .../server/src/audio_policy_manager_stub.cpp | 35 +++++++++++- .../server/src/audio_policy_server.cpp | 19 +++++++ .../audio_routing_manager_listener_proxy.cpp | 20 +++++++ .../src/service/audio_policy_service.cpp | 19 +++++++ .../listener/device_status_listener.cpp | 33 +++++++++++ .../manager/pnp_server/audio_pnp_server.cpp | 7 ++- .../pnp_server/audio_socket_thread.cpp | 56 +++++++++++++++++-- .../client/src/audio_routing_manager.cpp | 11 ++++ 26 files changed, 342 insertions(+), 12 deletions(-) diff --git a/frameworks/native/audiopolicy/include/audio_policy_manager.h b/frameworks/native/audiopolicy/include/audio_policy_manager.h index 125b351f0..5c34c9e5f 100644 --- a/frameworks/native/audiopolicy/include/audio_policy_manager.h +++ b/frameworks/native/audiopolicy/include/audio_policy_manager.h @@ -408,6 +408,10 @@ public: int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason); + int32_t SetAudioDeviceAnahsCallback(const std::shared_ptr &callback); + + int32_t UnsetAudioDeviceAnahsCallback(); + int32_t MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType); int32_t SetAudioConcurrencyCallback(const uint32_t sessionID, diff --git a/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h b/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h index 4e0c642d3..7bada1118 100644 --- a/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h +++ b/interfaces/inner_api/native/audiomanager/include/audio_routing_manager.h @@ -29,6 +29,7 @@ namespace AudioStandard { class AudioDeviceDescriptor; class AudioRendererFilter; class AudioDeviceRefiner; +class AudioDeviceAnahs; class AudioPreferredOutputDeviceChangeCallback { public: virtual ~AudioPreferredOutputDeviceChangeCallback() = default; @@ -74,6 +75,8 @@ public: int32_t SetAudioDeviceRefinerCallback(const std::shared_ptr &callback); int32_t UnsetAudioDeviceRefinerCallback(); int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN); + int32_t SetAudioDeviceAnahsCallback(const std::shared_ptr &callback); + int32_t UnsetAudioDeviceAnahsCallback(); private: int32_t GetCallingPid(); }; diff --git a/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h b/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h index f0f007866..6cbd0d261 100644 --- a/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h +++ b/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h @@ -408,6 +408,13 @@ public: RouterType routerType, SourceType sourceType, int32_t clientUid, AudioPipeType audioPipeType) = 0; }; +class AudioDeviceAnahs { +public: + virtual ~AudioDeviceAnahs() = default; + + virtual int32_t OnExtPnpDeviceStatusChanged(std::string anahsStatus) = 0; +}; + /** * @brief The AudioSystemManager class is an abstract definition of audio manager. * Provides a series of client/interfaces for audio management diff --git a/services/audio_policy/client/include/audio_policy_base.h b/services/audio_policy/client/include/audio_policy_base.h index 9e0986be8..5efbb8d94 100644 --- a/services/audio_policy/client/include/audio_policy_base.h +++ b/services/audio_policy/client/include/audio_policy_base.h @@ -324,6 +324,10 @@ public: virtual int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason) = 0; + virtual int32_t SetAudioDeviceAnahsCallback(const sptr &object) = 0; + + virtual int32_t UnsetAudioDeviceAnahsCallback() = 0; + virtual int32_t SetAudioConcurrencyCallback(const uint32_t sessionID, const sptr &object) = 0; virtual int32_t UnsetAudioConcurrencyCallback(const uint32_t sessionID) = 0; diff --git a/services/audio_policy/client/include/audio_policy_manager_stub.h b/services/audio_policy/client/include/audio_policy_manager_stub.h index 921b4bcb5..8c49d71e4 100644 --- a/services/audio_policy/client/include/audio_policy_manager_stub.h +++ b/services/audio_policy/client/include/audio_policy_manager_stub.h @@ -155,6 +155,8 @@ private: void SetAudioDeviceRefinerCallbackInternal(MessageParcel &data, MessageParcel &reply); void UnsetAudioDeviceRefinerCallbackInternal(MessageParcel &data, MessageParcel &reply); void TriggerFetchDeviceInternal(MessageParcel &data, MessageParcel &reply); + void SetAudioDeviceAnahsCallbackInternal(MessageParcel &data, MessageParcel &reply); + void UnsetAudioDeviceAnahsCallbackInternal(MessageParcel &data, MessageParcel &reply); void MoveToNewTypeInternal(MessageParcel &data, MessageParcel &reply); void GetDevicesInnerInternal(MessageParcel &data, MessageParcel &reply); void SetConcurrencyCallbackInternal(MessageParcel &data, MessageParcel &reply); @@ -171,6 +173,7 @@ private: void SetDefaultOutputDeviceInternal(MessageParcel &data, MessageParcel &reply); void SetQueryClientTypeCallbackInternal(MessageParcel &data, MessageParcel &reply); + void OnMiddleTenRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); void OnMiddleNinRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); void OnMiddleEigRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); void OnMiddleSevRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); diff --git a/services/audio_policy/client/include/audio_policy_proxy.h b/services/audio_policy/client/include/audio_policy_proxy.h index cc697184b..55252a2c1 100644 --- a/services/audio_policy/client/include/audio_policy_proxy.h +++ b/services/audio_policy/client/include/audio_policy_proxy.h @@ -308,6 +308,10 @@ public: int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason) override; + int32_t SetAudioDeviceAnahsCallback(const sptr &object) override; + + int32_t UnsetAudioDeviceAnahsCallback() override; + int32_t MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType) override; int32_t SetAudioConcurrencyCallback(const uint32_t sessionID, const sptr &object) override; diff --git a/services/audio_policy/client/include/audio_routing_manager_listener_stub.h b/services/audio_policy/client/include/audio_routing_manager_listener_stub.h index 5b8aadca2..1c84b05f0 100644 --- a/services/audio_policy/client/include/audio_routing_manager_listener_stub.h +++ b/services/audio_policy/client/include/audio_routing_manager_listener_stub.h @@ -31,15 +31,19 @@ public: void OnDistributedRoutingRoleChange(const sptr descriptor, const CastType type) override; void SetDistributedRoutingRoleCallback(const std::weak_ptr &callback); void SetAudioDeviceRefinerCallback(const std::weak_ptr &callback); + void SetAudioDeviceAnahsCallback(const std::weak_ptr &callback); int32_t OnAudioOutputDeviceRefined(std::vector> &descs, RouterType routerType, StreamUsage streamUsage, int32_t clientUid, AudioPipeType audioPipeType) override; int32_t OnAudioInputDeviceRefined(std::vector> &descs, RouterType routerType, SourceType sourceType, int32_t clientUid, AudioPipeType audioPipeType) override; + int32_t OnExtPnpDeviceStatusChanged(std::string anahsStatus) override; private: void OnAudioOutputDeviceRefinedInternal(MessageParcel &data, MessageParcel &reply); void OnAudioInputDeviceRefinedInternal(MessageParcel &data, MessageParcel &reply); + void OnExtPnpDeviceStatusChangedInternal(MessageParcel &data, MessageParcel &reply); std::weak_ptr audioDistributedRoutingRoleCallback_; std::weak_ptr audioDeviceRefinerCallback_; + std::weak_ptr audioDeviceAnahsCallback_; }; } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/client/src/audio_policy_manager.cpp b/services/audio_policy/client/src/audio_policy_manager.cpp index 76fab62d5..6f3e27242 100644 --- a/services/audio_policy/client/src/audio_policy_manager.cpp +++ b/services/audio_policy/client/src/audio_policy_manager.cpp @@ -1904,6 +1904,37 @@ int32_t AudioPolicyManager::TriggerFetchDevice(AudioStreamDeviceChangeReasonExt return gsp->TriggerFetchDevice(reason); } +int32_t AudioPolicyManager::SetAudioDeviceAnahsCallback(const std::shared_ptr &callback) +{ + const sptr gsp = GetAudioPolicyManagerProxy(); + CHECK_AND_RETURN_RET_LOG(gsp != nullptr, ERROR, "audio policy manager proxy is NULL."); + if (callback == nullptr) { + return ERR_INVALID_PARAM; + }; + + std::unique_lock lock(listenerStubMutex_); + auto activeDistributedRoutingRoleCb = new (std::nothrow) AudioRoutingManagerListenerStub(); + if (activeDistributedRoutingRoleCb == nullptr) { + AUDIO_ERR_LOG("object is nullptr"); + return ERROR; + } + activeDistributedRoutingRoleCb->SetAudioDeviceAnahsCallback(callback); + sptr object = activeDistributedRoutingRoleCb->AsObject(); + if (object == nullptr) { + AUDIO_ERR_LOG("listenerStub is nullptr"); + delete activeDistributedRoutingRoleCb; + return ERROR; + } + return gsp->SetAudioDeviceAnahsCallback(object); +} + +int32_t AudioPolicyManager::UnsetAudioDeviceAnahsCallback() +{ + const sptr gsp = GetAudioPolicyManagerProxy(); + CHECK_AND_RETURN_RET_LOG(gsp != nullptr, ERROR, "audio policy manager proxy is NULL."); + return gsp->UnsetAudioDeviceAnahsCallback(); +} + int32_t AudioPolicyManager::MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType) { const sptr gsp = GetAudioPolicyManagerProxy(); diff --git a/services/audio_policy/client/src/audio_routing_manager_listener_stub.cpp b/services/audio_policy/client/src/audio_routing_manager_listener_stub.cpp index 40814eb5f..df5066907 100644 --- a/services/audio_policy/client/src/audio_routing_manager_listener_stub.cpp +++ b/services/audio_policy/client/src/audio_routing_manager_listener_stub.cpp @@ -56,6 +56,10 @@ int AudioRoutingManagerListenerStub::OnRemoteRequest( OnAudioInputDeviceRefinedInternal(data, reply); return AUDIO_OK; } + case ON_AUDIO_ANAHS_DEVICE_CHANGE: { + OnExtPnpDeviceStatusChangedInternal(data, reply); + return AUDIO_OK; + } default: { AUDIO_ERR_LOG("default case, need check AudioListenerStub"); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -88,6 +92,13 @@ void AudioRoutingManagerListenerStub::SetAudioDeviceRefinerCallback(const std::w CHECK_AND_RETURN_LOG(audioDeviceRefinerCallback != nullptr, "audioDeviceRefinerCallback_ is nullptr"); } +void AudioRoutingManagerListenerStub::SetAudioDeviceAnahsCallback(const std::weak_ptr &callback) +{ + audioDeviceAnahsCallback_ = callback; + std::shared_ptr audioDeviceAnahsCallback = audioDeviceAnahsCallback_.lock(); + CHECK_AND_RETURN_LOG(audioDeviceAnahsCallback != nullptr, "audioDeviceAnahsCallback_ is nullptr"); +} + void AudioRoutingManagerListenerStub::OnAudioOutputDeviceRefinedInternal(MessageParcel &data, MessageParcel &reply) { std::vector> descs; @@ -162,5 +173,20 @@ int32_t AudioRoutingManagerListenerStub::OnAudioInputDeviceRefined( sourceType, clientUid, audioPipeType); } +void AudioRoutingManagerListenerStub::OnExtPnpDeviceStatusChangedInternal(MessageParcel &data, MessageParcel &reply) +{ + std::string anahsName = data.ReadString(); + int32_t result = OnExtPnpDeviceStatusChanged(anahsName); + reply.WriteInt32(result); +} + +int32_t AudioRoutingManagerListenerStub::OnExtPnpDeviceStatusChanged(std::string anahsStatus) +{ + std::shared_ptr audioDeviceAnahsCallback = audioDeviceAnahsCallback_.lock(); + CHECK_AND_RETURN_RET_LOG(audioDeviceAnahsCallback != nullptr, ERR_CALLBACK_NOT_REGISTERED, + "audioDeviceAnahsCallback_ is nullptr"); + return audioDeviceAnahsCallback->OnExtPnpDeviceStatusChanged(anahsStatus); +} + } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/client/src/proxy/audio_policy_proxy.cpp b/services/audio_policy/client/src/proxy/audio_policy_proxy.cpp index 723dd1286..bb9412e60 100644 --- a/services/audio_policy/client/src/proxy/audio_policy_proxy.cpp +++ b/services/audio_policy/client/src/proxy/audio_policy_proxy.cpp @@ -1842,6 +1842,39 @@ int32_t AudioPolicyProxy::TriggerFetchDevice(AudioStreamDeviceChangeReasonExt re return reply.ReadInt32(); } +int32_t AudioPolicyProxy::SetAudioDeviceAnahsCallback(const sptr &object) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "object is null"); + + bool ret = data.WriteInterfaceToken(GetDescriptor()); + CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed"); + + (void)data.WriteRemoteObject(object); + int32_t error = Remote()->SendRequest( + static_cast(AudioPolicyInterfaceCode::SET_AUDIO_DEVICE_ANAHS_CALLBACK), data, reply, option); + CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error); + return reply.ReadInt32(); +} + +int32_t AudioPolicyProxy::UnsetAudioDeviceAnahsCallback() +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + bool ret = data.WriteInterfaceToken(GetDescriptor()); + CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed"); + + int32_t error = Remote()->SendRequest( + static_cast(AudioPolicyInterfaceCode::UNSET_AUDIO_DEVICE_ANAHS_CALLBACK), data, reply, option); + CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error); + return reply.ReadInt32(); +} + int32_t AudioPolicyProxy::MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType) { MessageParcel data; diff --git a/services/audio_policy/common/include/audio_policy_ipc_interface_code.h b/services/audio_policy/common/include/audio_policy_ipc_interface_code.h index ee4a0372e..d4bb5d24b 100644 --- a/services/audio_policy/common/include/audio_policy_ipc_interface_code.h +++ b/services/audio_policy/common/include/audio_policy_ipc_interface_code.h @@ -159,7 +159,9 @@ enum class AudioPolicyInterfaceCode { SET_DEFAULT_OUTPUT_DEVICE, GET_OUTPUT_DEVICE, GET_INPUT_DEVICE, - AUDIO_POLICY_MANAGER_CODE_MAX = GET_INPUT_DEVICE, + SET_AUDIO_DEVICE_ANAHS_CALLBACK, + UNSET_AUDIO_DEVICE_ANAHS_CALLBACK, + AUDIO_POLICY_MANAGER_CODE_MAX = UNSET_AUDIO_DEVICE_ANAHS_CALLBACK, }; } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/common/include/i_standard_audio_routing_manager_listener.h b/services/audio_policy/common/include/i_standard_audio_routing_manager_listener.h index 33829f2a5..ed086aef7 100644 --- a/services/audio_policy/common/include/i_standard_audio_routing_manager_listener.h +++ b/services/audio_policy/common/include/i_standard_audio_routing_manager_listener.h @@ -23,7 +23,7 @@ namespace OHOS { namespace AudioStandard { -class IStandardAudioRoutingManagerListener : public IRemoteBroker, public AudioDeviceRefiner { +class IStandardAudioRoutingManagerListener : public IRemoteBroker, public AudioDeviceRefiner, public AudioDeviceAnahs { public: virtual ~IStandardAudioRoutingManagerListener() = default; virtual void OnDistributedRoutingRoleChange(const sptr descriptor, const CastType type) = 0; @@ -36,6 +36,7 @@ public: ON_DISTRIBUTED_ROUTING_ROLE_CHANGE, ON_AUDIO_OUTPUT_DEVICE_REFINERD, ON_AUDIO_INPUT_DEVICE_REFINERD, + ON_AUDIO_ANAHS_DEVICE_CHANGE, }; DECLARE_INTERFACE_DESCRIPTOR(u"IStandardAudioRoutingManagerListener"); diff --git a/services/audio_policy/server/include/audio_policy_server.h b/services/audio_policy/server/include/audio_policy_server.h index aaaddbdfb..8601cd54f 100644 --- a/services/audio_policy/server/include/audio_policy_server.h +++ b/services/audio_policy/server/include/audio_policy_server.h @@ -395,6 +395,10 @@ public: int32_t TriggerFetchDevice( AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN) override; + int32_t SetAudioDeviceAnahsCallback(const sptr &object) override; + + int32_t UnsetAudioDeviceAnahsCallback() override; + int32_t MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType) override; int32_t SetAudioConcurrencyCallback(const uint32_t sessionID, const sptr &object) override; diff --git a/services/audio_policy/server/include/audio_routing_manager_listener_proxy.h b/services/audio_policy/server/include/audio_routing_manager_listener_proxy.h index bcc0906a9..a00be656c 100644 --- a/services/audio_policy/server/include/audio_routing_manager_listener_proxy.h +++ b/services/audio_policy/server/include/audio_routing_manager_listener_proxy.h @@ -31,6 +31,7 @@ public: RouterType routerType, StreamUsage streamUsage, int32_t clientUid, AudioPipeType audioPipeType) override; int32_t OnAudioInputDeviceRefined(std::vector> &descs, RouterType routerType, SourceType sourceType, int32_t clientUid, AudioPipeType audioPipeType) override; + int32_t OnExtPnpDeviceStatusChanged(std::string anahsStatus) override; private: static inline BrokerDelegator delegator_; 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 3c7be70a8..946d3dfb5 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -562,6 +562,10 @@ public: int32_t SetDefaultOutputDevice(const DeviceType deviceType, const uint32_t sessionID, const StreamUsage streamUsage, bool isRunning); + + int32_t SetAudioDeviceAnahsCallback(const sptr &object); + + int32_t UnsetAudioDeviceAnahsCallback(); private: AudioPolicyService() diff --git a/services/audio_policy/server/include/service/listener/device_status_listener.h b/services/audio_policy/server/include/service/listener/device_status_listener.h index cb2e2f8b4..64ab42df7 100644 --- a/services/audio_policy/server/include/service/listener/device_status_listener.h +++ b/services/audio_policy/server/include/service/listener/device_status_listener.h @@ -21,6 +21,11 @@ #include "audio_adapter_info.h" #include "idevice_status_observer.h" #include "audio_pnp_server.h" +#include "ipc_types.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" +#include "i_standard_audio_routing_manager_listener.h" namespace OHOS { namespace AudioStandard { @@ -36,11 +41,15 @@ public: IDeviceStatusObserver &deviceObserver_; void OnPnpDeviceStatusChanged(const std::string &info); + int32_t SetAudioDeviceAnahsCallback(const sptr &object); + int32_t UnsetAudioDeviceAnahsCallback(); + private: AudioPnpServer *audioPnpServer_; std::shared_ptr pnpDeviceCB_ = nullptr; struct HDIServiceManager *hdiServiceManager_; struct ServiceStatusListener *listener_; + sptr audioDeviceAnahsCb_; }; class AudioPnpStatusCallback : public AudioPnpDeviceChangeCallback { diff --git a/services/audio_policy/server/include/service/manager/pnp_server/audio_pnp_param.h b/services/audio_policy/server/include/service/manager/pnp_server/audio_pnp_param.h index 42904a30b..1c4fe46e3 100644 --- a/services/audio_policy/server/include/service/manager/pnp_server/audio_pnp_param.h +++ b/services/audio_policy/server/include/service/manager/pnp_server/audio_pnp_param.h @@ -52,6 +52,7 @@ #define UEVENT_SWITCH_NAME "SWITCH_NAME=" #define UEVENT_SWITCH_STATE "SWITCH_STATE=" #define UEVENT_HDI_NAME "HID_NAME=" +#define UEVENT_ANAHS "ANAHS=" #define UEVENT_ACTION_ADD "add" #define UEVENT_ACTION_REMOVE "remove" #define UEVENT_ACTION_CHANGE "change" @@ -69,7 +70,10 @@ #define UEVENT_ID_MODEL "ID_MODEL=" #define UEVENT_USB_AUDIO "USB Audio" #define UEVENT_USB_HEADSET "HEADSET" -#define UEVENT_ARR_SIZE 9 +#define UEVENT_PLATFORM "platform" +#define UEVENT_INSERT "INSERT" +#define UEVENT_REMOVE "REMOVE" +#define UEVENT_ARR_SIZE 10 #define UEVENT_POLL_WAIT_TIME 100 #define WAIT_THREAD_END_TIME_MS 1 @@ -95,6 +99,7 @@ struct AudioPnpUevent { const char *switchState; const char *hidName; const char *devName; + const char *anahsName; }; struct AudioDevBusUsbDevice { @@ -107,6 +112,7 @@ struct AudioEvent { uint32_t deviceType; std::string name; std::string address; + std::string anahsName; }; } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/server/include/service/manager/pnp_server/audio_socket_thread.h b/services/audio_policy/server/include/service/manager/pnp_server/audio_socket_thread.h index a0dc02ebf..f10e99078 100644 --- a/services/audio_policy/server/include/service/manager/pnp_server/audio_socket_thread.h +++ b/services/audio_policy/server/include/service/manager/pnp_server/audio_socket_thread.h @@ -53,6 +53,8 @@ private: static inline bool IsBadName(const char *name); static int32_t ScanUsbBusSubDir(const char *subDir); static int32_t AudioDpDetectDevice(struct AudioPnpUevent *audioPnpUevent); + static int32_t AudioAnahsDetectDevice(struct AudioPnpUevent *audioPnpUevent); + static int32_t SetAudioAnahsEventValue(AudioEvent *audioEvent, struct AudioPnpUevent *audioPnpUevent); }; } // namespace AudioStandard diff --git a/services/audio_policy/server/src/audio_policy_manager_stub.cpp b/services/audio_policy/server/src/audio_policy_manager_stub.cpp index e331618d3..ac98c47ea 100644 --- a/services/audio_policy/server/src/audio_policy_manager_stub.cpp +++ b/services/audio_policy/server/src/audio_policy_manager_stub.cpp @@ -166,6 +166,8 @@ const char *g_audioPolicyCodeStrs[] = { "SET_DEFAULT_OUTPUT_DEVICE", "GET_OUTPUT_DEVICE", "GET_INPUT_DEVICE", + "SET_AUDIO_DEVICE_ANAHS_CALLBACK", + "UNSET_AUDIO_DEVICE_ANAHS_CALLBACK", }; constexpr size_t codeNums = sizeof(g_audioPolicyCodeStrs) / sizeof(const char *); @@ -1307,6 +1309,23 @@ void AudioPolicyManagerStub::SetQueryClientTypeCallbackInternal(MessageParcel &d reply.WriteInt32(result); } +void AudioPolicyManagerStub::OnMiddleTenRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + switch (code) { + case static_cast(AudioPolicyInterfaceCode::SET_AUDIO_DEVICE_ANAHS_CALLBACK): + SetAudioDeviceAnahsCallbackInternal(data, reply); + break; + case static_cast(AudioPolicyInterfaceCode::UNSET_AUDIO_DEVICE_ANAHS_CALLBACK): + UnsetAudioDeviceAnahsCallbackInternal(data, reply); + break; + default: + AUDIO_ERR_LOG("default case, need check AudioPolicyManagerStub"); + IPCObjectStub::OnRemoteRequest(code, data, reply, option); + break; + } +} + void AudioPolicyManagerStub::OnMiddleNinRemoteRequest( uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { @@ -1336,8 +1355,7 @@ void AudioPolicyManagerStub::OnMiddleNinRemoteRequest( SetQueryClientTypeCallbackInternal(data, reply); break; default: - AUDIO_ERR_LOG("default case, need check AudioPolicyManagerStub"); - IPCObjectStub::OnRemoteRequest(code, data, reply, option); + OnMiddleTenRemoteRequest(code, data, reply, option); break; } } @@ -1934,6 +1952,19 @@ void AudioPolicyManagerStub::TriggerFetchDeviceInternal(MessageParcel &data, Mes reply.WriteInt32(result); } +void AudioPolicyManagerStub::SetAudioDeviceAnahsCallbackInternal(MessageParcel &data, MessageParcel &reply) +{ + sptr object = data.ReadRemoteObject(); + int32_t result = SetAudioDeviceAnahsCallback(object); + reply.WriteInt32(result); +} + +void AudioPolicyManagerStub::UnsetAudioDeviceAnahsCallbackInternal(MessageParcel &data, MessageParcel &reply) +{ + int32_t result = UnsetAudioDeviceAnahsCallback(); + reply.WriteInt32(result); +} + void AudioPolicyManagerStub::MoveToNewTypeInternal(MessageParcel &data, MessageParcel &reply) { uint32_t sessionId = data.ReadUint32(); diff --git a/services/audio_policy/server/src/audio_policy_server.cpp b/services/audio_policy/server/src/audio_policy_server.cpp index fec756d2d..993924b3b 100644 --- a/services/audio_policy/server/src/audio_policy_server.cpp +++ b/services/audio_policy/server/src/audio_policy_server.cpp @@ -2788,6 +2788,25 @@ int32_t AudioPolicyServer::TriggerFetchDevice(AudioStreamDeviceChangeReasonExt r return audioPolicyService_.TriggerFetchDevice(reason); } +int32_t AudioPolicyServer::SetAudioDeviceAnahsCallback(const sptr &object) +{ + CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_INVALID_PARAM, "SetAudioDeviceAnahsCallback object is nullptr"); + auto callerUid = IPCSkeleton::GetCallingUid(); + if (callerUid != UID_AUDIO) { + return ERROR; + } + return audioPolicyService_.SetAudioDeviceAnahsCallback(object); +} + +int32_t AudioPolicyServer::UnsetAudioDeviceAnahsCallback() +{ + auto callerUid = IPCSkeleton::GetCallingUid(); + if (callerUid != UID_AUDIO) { + return ERROR; + } + return audioPolicyService_.UnsetAudioDeviceAnahsCallback(); +} + void AudioPolicyServer::NotifyAccountsChanged(const int &id) { audioPolicyService_.NotifyAccountsChanged(id); diff --git a/services/audio_policy/server/src/audio_routing_manager_listener_proxy.cpp b/services/audio_policy/server/src/audio_routing_manager_listener_proxy.cpp index 465cab06d..ba04283cd 100644 --- a/services/audio_policy/server/src/audio_routing_manager_listener_proxy.cpp +++ b/services/audio_policy/server/src/audio_routing_manager_listener_proxy.cpp @@ -123,5 +123,25 @@ int32_t AudioRoutingManagerListenerProxy::OnAudioInputDeviceRefined( } return SUCCESS; } + +int32_t AudioRoutingManagerListenerProxy::OnExtPnpDeviceStatusChanged(std::string anahsStatus) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, + "OnAudioOutputDeviceRefined: WriteInterfaceToken failed"); + data.WriteString(anahsStatus); + + int error = Remote()->SendRequest(ON_AUDIO_ANAHS_DEVICE_CHANGE, data, reply, option); + CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "OnExtPnpDeviceStatusChanged, error: %{public}d", error); + + int32_t result = reply.ReadInt32(); + CHECK_AND_RETURN_RET_LOG(result == SUCCESS, result, + "OnExtPnpDeviceStatusChanged callback failed, error %{public}d", result); + + return SUCCESS; +} } // 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 0a96763b1..f217a2c9d 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -534,6 +534,25 @@ int32_t AudioPolicyService::SetAudioStreamRemovedCallback(AudioStreamRemovedCall return audioPolicyManager_.SetAudioStreamRemovedCallback(callback); } +int32_t AudioPolicyService::SetAudioDeviceAnahsCallback(const sptr &object) +{ + CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_INVALID_PARAM, "SetAudioDeviceRefinerCallback object is nullptr"); + auto callerUid = IPCSkeleton::GetCallingUid(); + if (callerUid != UID_AUDIO) { + return ERROR; + } + return deviceStatusListener_->SetAudioDeviceAnahsCallback(object); +} + +int32_t AudioPolicyService::UnsetAudioDeviceAnahsCallback() +{ + auto callerUid = IPCSkeleton::GetCallingUid(); + if (callerUid != UID_AUDIO) { + return ERROR; + } + return deviceStatusListener_->UnsetAudioDeviceAnahsCallback(); +} + int32_t AudioPolicyService::GetMaxVolumeLevel(AudioVolumeType volumeType) const { if (volumeType == STREAM_ALL) { diff --git a/services/audio_policy/server/src/service/listener/device_status_listener.cpp b/services/audio_policy/server/src/service/listener/device_status_listener.cpp index afecfbd61..ba2354f39 100644 --- a/services/audio_policy/server/src/service/listener/device_status_listener.cpp +++ b/services/audio_policy/server/src/service/listener/device_status_listener.cpp @@ -18,6 +18,7 @@ #include "device_status_listener.h" #include +#include #include "hdf_device_class.h" #include "v4_0/audio_types.h" @@ -209,6 +210,21 @@ int32_t DeviceStatusListener::UnRegisterDeviceStatusListener() void DeviceStatusListener::OnPnpDeviceStatusChanged(const std::string &info) { CHECK_AND_RETURN_LOG(!info.empty(), "OnPnpDeviceStatusChange invalid info"); + + if (audioDeviceAnahsCb_ != nullptr) { + std::string anahsName = ""; + auto anahsBegin = info.find("ANAHS_NAME="); + auto anahsEnd = info.find_first_of(";", anahsBegin); + anahsName = info.substr(anahsBegin + std::strlen("ANAHS_NAME="), + anahsEnd - anahsBegin - std::strlen("ANAHS_NAME=")); + if (strncmp(anahsName.c_str(), UEVENT_INSERT, strlen(UEVENT_INSERT)) == 0 || + strncmp(anahsName.c_str(), UEVENT_REMOVE, strlen(UEVENT_REMOVE)) == 0) { + AUDIO_INFO_LOG("parse anahsName = %{public}s", anahsName.c_str()); + audioDeviceAnahsCb_->OnExtPnpDeviceStatusChanged(anahsName); + return; + } + } + AudioDeviceType hdiDeviceType = AUDIO_DEVICE_UNKNOWN; AudioEventType hdiEventType = AUDIO_EVENT_UNKNOWN; std::string name = ""; @@ -241,6 +257,23 @@ void DeviceStatusListener::OnPnpDeviceStatusChanged(const std::string &info) deviceObserver_.OnPnpDeviceStatusUpdated(internalDevice, isConnected, name, address); } +int32_t DeviceStatusListener::SetAudioDeviceAnahsCallback(const sptr &object) +{ + sptr listener = iface_cast(object); + if (listener != nullptr) { + audioDeviceAnahsCb_ = listener; + return SUCCESS; + } else { + return ERROR; + } +} + +int32_t DeviceStatusListener::UnsetAudioDeviceAnahsCallback() +{ + audioDeviceAnahsCb_ = nullptr; + return SUCCESS; +} + AudioPnpStatusCallback::AudioPnpStatusCallback() { AUDIO_INFO_LOG("ctor"); diff --git a/services/audio_policy/server/src/service/manager/pnp_server/audio_pnp_server.cpp b/services/audio_policy/server/src/service/manager/pnp_server/audio_pnp_server.cpp index bccc62d1f..9a1c8d342 100644 --- a/services/audio_policy/server/src/service/manager/pnp_server/audio_pnp_server.cpp +++ b/services/audio_policy/server/src/service/manager/pnp_server/audio_pnp_server.cpp @@ -39,13 +39,14 @@ static std::string GetAudioEventInfo(const AudioEvent audioEvent) { int32_t ret; char event[AUDIO_PNP_INFO_LEN_MAX] = {0}; - if (audioEvent.eventType == AUDIO_EVENT_UNKNOWN || audioEvent.deviceType == AUDIO_DEVICE_UNKNOWN) { + if ((audioEvent.eventType == AUDIO_EVENT_UNKNOWN && audioEvent.anahsName == "") || + (audioEvent.deviceType == AUDIO_DEVICE_UNKNOWN && audioEvent.anahsName == "")) { AUDIO_ERR_LOG("audio event is not updated"); return event; } ret = snprintf_s(event, AUDIO_PNP_INFO_LEN_MAX, AUDIO_PNP_INFO_LEN_MAX - 1, - "EVENT_TYPE=%u;DEVICE_TYPE=%u;EVENT_NAME=%s;DEVICE_ADDRESS=%s", - audioEvent.eventType, audioEvent.deviceType, audioEvent.name.c_str(), audioEvent.address.c_str()); + "EVENT_TYPE=%u;DEVICE_TYPE=%u;EVENT_NAME=%s;DEVICE_ADDRESS=%s;ANAHS_NAME=%s", audioEvent.eventType, + audioEvent.deviceType, audioEvent.name.c_str(), audioEvent.address.c_str(), audioEvent.anahsName.c_str()); if (ret < 0) { AUDIO_ERR_LOG("snprintf_s failed"); return event; diff --git a/services/audio_policy/server/src/service/manager/pnp_server/audio_socket_thread.cpp b/services/audio_policy/server/src/service/manager/pnp_server/audio_socket_thread.cpp index 64f24dd9b..f9f46afb3 100644 --- a/services/audio_policy/server/src/service/manager/pnp_server/audio_socket_thread.cpp +++ b/services/audio_policy/server/src/service/manager/pnp_server/audio_socket_thread.cpp @@ -44,7 +44,8 @@ bool AudioSocketThread::IsUpdatePnpDeviceState(AudioEvent *pnpDeviceEvent) if (pnpDeviceEvent->eventType == audioSocketEvent_.eventType && pnpDeviceEvent->deviceType == audioSocketEvent_.deviceType && pnpDeviceEvent->name == audioSocketEvent_.name && - pnpDeviceEvent->address == audioSocketEvent_.address) { + pnpDeviceEvent->address == audioSocketEvent_.address && + pnpDeviceEvent->anahsName == audioSocketEvent_.anahsName) { return false; } return true; @@ -56,6 +57,7 @@ void AudioSocketThread::UpdatePnpDeviceState(AudioEvent *pnpDeviceEvent) audioSocketEvent_.deviceType = pnpDeviceEvent->deviceType; audioSocketEvent_.name = pnpDeviceEvent->name; audioSocketEvent_.address = pnpDeviceEvent->address; + audioSocketEvent_.anahsName = pnpDeviceEvent->anahsName; } int AudioSocketThread::AudioPnpUeventOpen(int *fd) @@ -133,6 +135,26 @@ ssize_t AudioSocketThread::AudioPnpReadUeventMsg(int sockFd, char *buffer, size_ return len; } +int32_t AudioSocketThread::SetAudioAnahsEventValue(AudioEvent *audioEvent, struct AudioPnpUevent *audioPnpUevent) +{ + if (strncmp(audioPnpUevent->subSystem, UEVENT_PLATFORM, strlen(UEVENT_PLATFORM)) == 0) { + if (strncmp(audioPnpUevent->anahsName, UEVENT_INSERT, strlen(UEVENT_INSERT)) == 0) { + AUDIO_INFO_LOG("set anahs event to insert."); + audioEvent->anahsName = UEVENT_INSERT; + return SUCCESS; + } else if (strncmp(audioPnpUevent->anahsName, UEVENT_REMOVE, strlen(UEVENT_REMOVE)) == 0) { + AUDIO_INFO_LOG("set anahs event to remove."); + audioEvent->anahsName = UEVENT_REMOVE; + return SUCCESS; + } else { + AUDIO_ERR_LOG("set anahs event error."); + return ERROR; + } + } + AUDIO_ERR_LOG("set anahs event error and subSystem is not platform."); + return ERROR; +} + int32_t AudioSocketThread::SetAudioPnpServerEventValue(AudioEvent *audioEvent, struct AudioPnpUevent *audioPnpUevent) { if (strncmp(audioPnpUevent->subSystem, UEVENT_SUBSYSTEM_SWITCH, strlen(UEVENT_SUBSYSTEM_SWITCH)) == 0) { @@ -185,6 +207,27 @@ int32_t AudioSocketThread::SetAudioPnpServerEventValue(AudioEvent *audioEvent, s return SUCCESS; } +int32_t AudioSocketThread::AudioAnahsDetectDevice(struct AudioPnpUevent *audioPnpUevent) +{ + AudioEvent audioEvent; + if (audioPnpUevent == NULL) { + AUDIO_ERR_LOG("audioPnpUevent is null!"); + return HDF_ERR_INVALID_PARAM; + } + if (SetAudioAnahsEventValue(&audioEvent, audioPnpUevent) != SUCCESS) { + AUDIO_ERR_LOG("set audio anahs event failed."); + return ERROR; + } + + if (audioEvent.anahsName == audioSocketEvent_.anahsName) { + AUDIO_ERR_LOG("audio anahs device[%{public}u] state[%{public}u] not need flush !", audioEvent.deviceType, + audioEvent.eventType); + return SUCCESS; + } + audioSocketEvent_.anahsName = audioEvent.anahsName; + return SUCCESS; +} + int32_t AudioSocketThread::AudioAnalogHeadsetDetectDevice(struct AudioPnpUevent *audioPnpUevent) { AudioEvent audioEvent; @@ -486,7 +529,7 @@ int32_t AudioSocketThread::AudioUsbHeadsetDetectDevice(struct AudioPnpUevent *au bool AudioSocketThread::AudioPnpUeventParse(const char *msg, const ssize_t strLength) { - struct AudioPnpUevent audioPnpUevent = {"", "", "", "", "", "", "", "", ""}; + struct AudioPnpUevent audioPnpUevent = {"", "", "", "", "", "", "", "", "", ""}; if (strncmp(msg, "libudev", strlen("libudev")) == 0) { return false; @@ -505,12 +548,14 @@ bool AudioSocketThread::AudioPnpUeventParse(const char *msg, const ssize_t strLe AUDIO_DEBUG_LOG("Param msgTmp:[%{public}s] len:[%{public}zu]", msgTmp, strlen(msgTmp)); const char *arrStrTmp[UEVENT_ARR_SIZE] = { UEVENT_ACTION, UEVENT_DEV_NAME, UEVENT_NAME, UEVENT_STATE, UEVENT_DEVTYPE, - UEVENT_SUBSYSTEM, UEVENT_SWITCH_NAME, UEVENT_SWITCH_STATE, UEVENT_HDI_NAME + UEVENT_SUBSYSTEM, UEVENT_SWITCH_NAME, UEVENT_SWITCH_STATE, UEVENT_HDI_NAME, + UEVENT_ANAHS }; const char **arrVarTmp[UEVENT_ARR_SIZE] = { &audioPnpUevent.action, &audioPnpUevent.devName, &audioPnpUevent.name, &audioPnpUevent.state, &audioPnpUevent.devType, &audioPnpUevent.subSystem, - &audioPnpUevent.switchName, &audioPnpUevent.switchState, &audioPnpUevent.hidName + &audioPnpUevent.switchName, &audioPnpUevent.switchState, &audioPnpUevent.hidName, + &audioPnpUevent.anahsName }; for (int count = 0; count < UEVENT_ARR_SIZE; count++) { if (strncmp(msgTmp, arrStrTmp[count], strlen(arrStrTmp[count])) == 0) { @@ -531,6 +576,9 @@ bool AudioSocketThread::AudioPnpUeventParse(const char *msg, const ssize_t strLe if (AudioDpDetectDevice(&audioPnpUevent) == SUCCESS) { return true; } + if (AudioAnahsDetectDevice(&audioPnpUevent) == SUCCESS) { + return true; + } return false; } diff --git a/services/audio_service/client/src/audio_routing_manager.cpp b/services/audio_service/client/src/audio_routing_manager.cpp index e58b32216..31527c16a 100644 --- a/services/audio_service/client/src/audio_routing_manager.cpp +++ b/services/audio_service/client/src/audio_routing_manager.cpp @@ -128,5 +128,16 @@ int32_t AudioRoutingManager::TriggerFetchDevice(AudioStreamDeviceChangeReasonExt { return AudioPolicyManager::GetInstance().TriggerFetchDevice(reason); } + +int32_t AudioRoutingManager::SetAudioDeviceAnahsCallback(const std::shared_ptr &callback) +{ + CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM, "callback is nullptr"); + return AudioPolicyManager::GetInstance().SetAudioDeviceAnahsCallback(callback); +} + +int32_t AudioRoutingManager::UnsetAudioDeviceAnahsCallback() +{ + return AudioPolicyManager::GetInstance().UnsetAudioDeviceAnahsCallback(); +} } // namespace AudioStandard } // namespace OHOS -- Gitee