diff --git a/common/include/daudio_constants.h b/common/include/daudio_constants.h index 35ad9b46f4184951a8305d4e15639aa8784f03cf..c0136d319d9afb450dba5629e7d926f3ac16d5d7 100644 --- a/common/include/daudio_constants.h +++ b/common/include/daudio_constants.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -151,6 +151,10 @@ constexpr const char *INTERRUPT_GROUP_ID = "INTERRUPT_GROUP_ID"; const std::string DUMP_FILE_PATH = "/data/data/daudio"; const std::string AUDIO_PERMISSION_NAME = "ohos.permission.ENABLE_DISTRIBUTED_HARDWARE"; +const std::string SESSIONNAME_SPK_SINK = "ohos.dhardware.daudio.dspeaker_receiver.avtrans.control"; +const std::string SESSIONNAME_SPK_SOURCE = "ohos.dhardware.daudio.dspeaker_sender.avtrans.control"; +const std::string SESSIONNAME_MIC_SOURCE = "ohos.dhardware.daudio.dmic_receiver.avtrans.control"; +const std::string SESSIONNAME_MIC_SINK = "ohos.dhardware.daudio.dmic_sender.avtrans.control"; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DAUDIO_CONSTANTS_H diff --git a/services/audioclient/interface/imic_client.h b/services/audioclient/interface/imic_client.h index 7247a5e75ce6d168bf64373311b7d4c559cb0da5..72ba86bf0b4b78ca2f74b5169b0b24fc6483b845 100644 --- a/services/audioclient/interface/imic_client.h +++ b/services/audioclient/interface/imic_client.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -34,6 +34,7 @@ public: virtual int32_t StopCapture() = 0; virtual void SetAttrs(const std::string &devId, const std::shared_ptr &callback) = 0; virtual int32_t InitSenderEngine(IAVEngineProvider *providerPtr) = 0; + virtual int32_t InitCtrlTrans() = 0; virtual int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) = 0; }; } // namespace DistributedHardware diff --git a/services/audioclient/interface/ispk_client.h b/services/audioclient/interface/ispk_client.h index 662dcea328a2012b7b776c524ce338167761e9e4..6a3f55861860a6dfdcc55f0cc6751170da6f9d63 100644 --- a/services/audioclient/interface/ispk_client.h +++ b/services/audioclient/interface/ispk_client.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -37,6 +37,7 @@ public: virtual void PlayStatusChange(const std::string &args) = 0; virtual void SetAttrs(const std::string &devId, const std::shared_ptr &callback) = 0; virtual int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) = 0; + virtual int32_t InitCtrlTrans() = 0; virtual int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) = 0; }; } // namespace DistributedHardware diff --git a/services/audioclient/micclient/include/dmic_client.h b/services/audioclient/micclient/include/dmic_client.h index f1b09b55c35f83b7be1896c80cb465f6aa344f4a..c2f2ae9122758088f405f999006e6047a1d0912a 100644 --- a/services/audioclient/micclient/include/dmic_client.h +++ b/services/audioclient/micclient/include/dmic_client.h @@ -36,6 +36,7 @@ #include "daudio_constants.h" #include "daudio_errorcode.h" #include "daudio_log.h" +#include "daudio_sink_ctrl_trans.h" #include "iaudio_data_transport.h" #include "iaudio_datatrans_callback.h" #include "iaudio_event_callback.h" @@ -46,6 +47,7 @@ namespace DistributedHardware { class DMicClient : public IAudioDataTransCallback, public AudioStandard::AudioCapturerReadCallback, public IMicClient, public AVSenderTransportCallback, + public IAudioCtrlTransCallback, public std::enable_shared_from_this { public: DMicClient(const std::string &devId, const int32_t dhId, const std::shared_ptr &callback) @@ -55,7 +57,10 @@ public: int32_t OnDecodeTransDataDone(const std::shared_ptr &audioData) override; void OnEngineTransEvent(const AVTransEvent &event) override; void OnEngineTransMessage(const std::shared_ptr &message) override; + void OnCtrlTransEvent(const AVTransEvent &event) override; + void OnCtrlTransMessage(const std::shared_ptr &message) override; int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override; + int32_t InitCtrlTrans() override; int32_t SetUp(const AudioParam ¶m) override; int32_t Release() override; int32_t StartCapture() override; @@ -89,6 +94,7 @@ private: std::weak_ptr eventCallback_; std::unique_ptr audioCapturer_ = nullptr; std::shared_ptr micTrans_ = nullptr; + std::shared_ptr micCtrlTrans_ = nullptr; int64_t lastCaptureStartTime_ = 0; int64_t lastTransStartTime_ = 0; std::atomic isPauseStatus_ = false; diff --git a/services/audioclient/micclient/src/dmic_client.cpp b/services/audioclient/micclient/src/dmic_client.cpp index 22ed70167ad59f8c234e46d1fa85719885bffaac..c1abce7b414995a5cace2396d6e30e0d5079fe9f 100644 --- a/services/audioclient/micclient/src/dmic_client.cpp +++ b/services/audioclient/micclient/src/dmic_client.cpp @@ -70,6 +70,37 @@ int32_t DMicClient::InitSenderEngine(IAVEngineProvider *providerPtr) return DH_SUCCESS; } +int32_t DMicClient::InitCtrlTrans() +{ + DHLOGI("InitCtrlTrans enter"); + if (micCtrlTrans_ == nullptr) { + micCtrlTrans_ = std::make_shared(devId_, + SESSIONNAME_MIC_SINK, SESSIONNAME_MIC_SOURCE, shared_from_this()); + } + int32_t ret = micCtrlTrans_->SetUp(shared_from_this()); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "Mic ctrl SetUp failed."); + return DH_SUCCESS; +} + +void DMicClient::OnCtrlTransEvent(const AVTransEvent &event) +{ + if (event.type == EventType::EVENT_START_SUCCESS) { + OnStateChange(DATA_OPENED); + } else if ((event.type == EventType::EVENT_STOP_SUCCESS) || + (event.type == EventType::EVENT_CHANNEL_CLOSED) || + (event.type == EventType::EVENT_START_FAIL)) { + OnStateChange(DATA_CLOSED); + } +} + +void DMicClient::OnCtrlTransMessage(const std::shared_ptr &message) +{ + CHECK_NULL_VOID(message); + DHLOGI("On Engine message, type : %{public}s.", GetEventNameByType(message->type_).c_str()); + DAudioSinkManager::GetInstance().HandleDAudioNotify(message->dstDevId_, message->dstDevId_, + static_cast(message->type_), message->content_); +} + int32_t DMicClient::OnStateChange(const AudioEventType type) { DHLOGD("On state change type: %{public}d.", type); @@ -173,8 +204,8 @@ int32_t DMicClient::SendMessage(uint32_t type, std::string content, std::string "CLOSE_MIC or OPEN_CTRL. type: %{public}u", type); return ERR_DH_AUDIO_NULLPTR; } - CHECK_NULL_RETURN(micTrans_, ERR_DH_AUDIO_NULLPTR); - micTrans_->SendMessage(type, content, dstDevId); + CHECK_NULL_RETURN(micCtrlTrans_, ERR_DH_AUDIO_NULLPTR); + micCtrlTrans_->SendAudioEvent(type, content, dstDevId); return DH_SUCCESS; } @@ -198,6 +229,13 @@ int32_t DMicClient::Release() isReleaseError = true; } micTrans_ = nullptr; + if (micCtrlTrans_ != nullptr) { + if (micCtrlTrans_->Release() != DH_SUCCESS) { + DHLOGE("Mic trans release failed."); + isReleaseError = true; + } + micCtrlTrans_ = nullptr; + } clientStatus_ = AudioStatus::STATUS_IDLE; if (isReleaseError) { return ERR_DH_AUDIO_FAILED; diff --git a/services/audioclient/spkclient/include/dspeaker_client.h b/services/audioclient/spkclient/include/dspeaker_client.h index 052a514052a63f9e0d1f66090d5a096e5f5e4b4a..f4825913f08526dea4d6d6a3c23c1849abf3c96d 100644 --- a/services/audioclient/spkclient/include/dspeaker_client.h +++ b/services/audioclient/spkclient/include/dspeaker_client.h @@ -39,6 +39,7 @@ #include "daudio_constants.h" #include "daudio_errorcode.h" #include "daudio_log.h" +#include "daudio_sink_ctrl_trans.h" #include "iaudio_data_transport.h" #include "iaudio_datatrans_callback.h" #include "iaudio_event_callback.h" @@ -48,6 +49,7 @@ namespace OHOS { namespace DistributedHardware { class DSpeakerClient : public IAudioDataTransCallback, public ISpkClient, public AVReceiverTransportCallback, + public IAudioCtrlTransCallback, public AudioStandard::VolumeKeyEventCallback, public AudioStandard::AudioRendererCallback, public AudioStandard::AudioRendererWriteCallback, @@ -64,6 +66,7 @@ public: void OnVolumeKeyEvent(AudioStandard::VolumeEvent volumeEvent) override; void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override; + int32_t InitCtrlTrans() override; int32_t SetUp(const AudioParam ¶m) override; int32_t Release() override; int32_t StartRender() override; @@ -78,6 +81,9 @@ public: void OnEngineTransMessage(const std::shared_ptr &message) override; void OnEngineTransDataAvailable(const std::shared_ptr &audioData) override; + void OnCtrlTransEvent(const AVTransEvent &event) override; + void OnCtrlTransMessage(const std::shared_ptr &message) override; + void OnWriteData(size_t length) override; private: std::string GetVolumeLevel(); @@ -109,6 +115,7 @@ private: std::unique_ptr audioRenderer_ = nullptr; std::shared_ptr speakerTrans_ = nullptr; + std::shared_ptr speakerCtrlTrans_ = nullptr; std::weak_ptr eventCallback_; int64_t lastPlayStartTime_ = 0; int64_t lastReceiveStartTime_ = 0; diff --git a/services/audioclient/spkclient/src/dspeaker_client.cpp b/services/audioclient/spkclient/src/dspeaker_client.cpp index 4d31a477cb4bcefd6381ff34de23a3d201c49241..dfe0d8c4fb2d60b72e01b5d15dd0ac10433323b0 100644 --- a/services/audioclient/spkclient/src/dspeaker_client.cpp +++ b/services/audioclient/spkclient/src/dspeaker_client.cpp @@ -72,6 +72,37 @@ int32_t DSpeakerClient::InitReceiverEngine(IAVEngineProvider *providerPtr) return DH_SUCCESS; } +int32_t DSpeakerClient::InitCtrlTrans() +{ + DHLOGI("InitCtrlTrans enter"); + if (speakerCtrlTrans_ == nullptr) { + speakerCtrlTrans_ = std::make_shared(devId_, + SESSIONNAME_SPK_SINK, SESSIONNAME_SPK_SOURCE, shared_from_this()); + } + int32_t ret = speakerCtrlTrans_->SetUp(shared_from_this()); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "Speaker ctrl SetUp failed."); + return DH_SUCCESS; +} + +void DSpeakerClient::OnCtrlTransEvent(const AVTransEvent &event) +{ + if (event.type == EventType::EVENT_START_SUCCESS) { + OnStateChange(DATA_OPENED); + } else if ((event.type == EventType::EVENT_STOP_SUCCESS) || + (event.type == EventType::EVENT_CHANNEL_CLOSED) || + (event.type == EventType::EVENT_START_FAIL)) { + OnStateChange(DATA_CLOSED); + } +} + +void DSpeakerClient::OnCtrlTransMessage(const std::shared_ptr &message) +{ + CHECK_NULL_VOID(message); + DHLOGI("On Engine message, type : %{public}s.", GetEventNameByType(message->type_).c_str()); + DAudioSinkManager::GetInstance().HandleDAudioNotify(message->dstDevId_, message->dstDevId_, + static_cast(message->type_), message->content_); +} + int32_t DSpeakerClient::CreateAudioRenderer(const AudioParam ¶m) { DHLOGD("Set up spk client: {sampleRate: %{public}d, bitFormat: %{public}d, channelMask: %{public}d," @@ -192,6 +223,13 @@ int32_t DSpeakerClient::Release() } speakerTrans_ = nullptr; } + if (speakerCtrlTrans_ != nullptr) { + if (speakerCtrlTrans_->Release() != DH_SUCCESS) { + DHLOGE("Speaker trans release failed."); + isSucess = false; + } + speakerCtrlTrans_ = nullptr; + } int32_t ret = AudioStandard::AudioSystemManager::GetInstance()->UnregisterVolumeKeyEventCallback(getprocpid()); if (ret != DH_SUCCESS) { @@ -620,8 +658,8 @@ int32_t DSpeakerClient::SendMessage(uint32_t type, std::string content, std::str DHLOGE("event type is not NOTIFY_OPEN_SPK or NOTIFY_CLOSE_SPK or OPEN_CTRL. type:%{public}u", type); return ERR_DH_AUDIO_NULLPTR; } - CHECK_NULL_RETURN(speakerTrans_, ERR_DH_AUDIO_NULLPTR); - speakerTrans_->SendMessage(type, content, dstDevId); + CHECK_NULL_RETURN(speakerCtrlTrans_, ERR_DH_AUDIO_NULLPTR); + speakerCtrlTrans_->SendAudioEvent(type, content, dstDevId); return DH_SUCCESS; } diff --git a/services/audioclient/test/unittest/micclient/BUILD.gn b/services/audioclient/test/unittest/micclient/BUILD.gn index eefb95b0fad74f10e7bbf7ec6cb50e19ed91cd31..86e87c515512bbc2d037d13b13ecea237b5e9133 100644 --- a/services/audioclient/test/unittest/micclient/BUILD.gn +++ b/services/audioclient/test/unittest/micclient/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022-2023 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -28,6 +28,7 @@ config("module_private_config") { "${audio_client_path}/test/unittest/audioclienttestutils/include", "${audio_processor_path}/interface", "${audio_transport_path}/interface", + "${audio_transport_path}/audioctrltransport/include", "${audio_transport_path}/receiverengine/include", "${audio_transport_path}/senderengine/include", "${common_path}/include", diff --git a/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp b/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp index 9f2b2d7c1e43fccbba667e7883af0c1c93ce3b5a..7f7103979ebb8656c6e3c21f71903c1b5e0d5015 100644 --- a/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp +++ b/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp @@ -225,8 +225,9 @@ HWTEST_F(DMicClientTest, SendMessage_001, TestSize.Level1) std::string content = "content"; std::string dstDevId = "dstDevId"; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, micClient_->SendMessage(EVENT_UNKNOWN, content, dstDevId)); + micClient_->InitCtrlTrans(); EXPECT_EQ(DH_SUCCESS, micClient_->SendMessage(NOTIFY_OPEN_MIC_RESULT, content, dstDevId)); - micClient_->micTrans_ = nullptr; + micClient_->micCtrlTrans_ = nullptr; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, micClient_->SendMessage(NOTIFY_OPEN_MIC_RESULT, content, dstDevId)); } diff --git a/services/audioclient/test/unittest/spkclient/BUILD.gn b/services/audioclient/test/unittest/spkclient/BUILD.gn index 8c506328d463c66d77d557e72d06d492b4728ee2..428be8c9873e62e9ee8e797eff68c08aeafcef64 100644 --- a/services/audioclient/test/unittest/spkclient/BUILD.gn +++ b/services/audioclient/test/unittest/spkclient/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022-2023 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -28,6 +28,7 @@ config("module_private_config") { "${audio_client_path}/test/unittest/audioclienttestutils/include", "${audio_processor_path}/interface", "${audio_transport_path}/interface", + "${audio_transport_path}/audioctrltransport/include", "${audio_transport_path}/receiverengine/include", "${audio_transport_path}/senderengine/include", "${common_path}/include", diff --git a/services/audioclient/test/unittest/spkclient/src/dspeaker_client_test.cpp b/services/audioclient/test/unittest/spkclient/src/dspeaker_client_test.cpp index c2737951c20740f911d59c2b8ff0922d537d20f7..c93002231338441fe80d5d19ae10fb1cea101a14 100644 --- a/services/audioclient/test/unittest/spkclient/src/dspeaker_client_test.cpp +++ b/services/audioclient/test/unittest/spkclient/src/dspeaker_client_test.cpp @@ -63,10 +63,19 @@ HWTEST_F(DSpeakerClientTest, InitReceiverEngine_001, TestSize.Level1) AVTransEvent event1 = { EventType::EVENT_START_SUCCESS, "", ""}; speakerClient_->OnEngineTransEvent(event1); + speakerClient_->OnCtrlTransEvent(event1); AVTransEvent event2 = { EventType::EVENT_STOP_SUCCESS, "", ""}; speakerClient_->OnEngineTransEvent(event2); + speakerClient_->OnCtrlTransEvent(event2); + AVTransEvent event3 = { EventType::EVENT_CHANNEL_CLOSED, "", ""}; + speakerClient_->OnEngineTransEvent(event3); + speakerClient_->OnCtrlTransEvent(event3); + AVTransEvent event4 = { EventType::EVENT_START_FAIL, "", ""}; + speakerClient_->OnEngineTransEvent(event4); + speakerClient_->OnCtrlTransEvent(event4); auto message = std::make_shared(); speakerClient_->OnEngineTransMessage(message); + speakerClient_->OnCtrlTransMessage(message); auto data = std::make_shared(4096); speakerClient_->OnEngineTransDataAvailable(data); EXPECT_EQ(DH_SUCCESS, speakerClient_->InitReceiverEngine(providerPtr)); @@ -236,9 +245,10 @@ HWTEST_F(DSpeakerClientTest, SendMessage_001, TestSize.Level1) audioParam_.renderOpts.renderFlags = MMAP_MODE; speakerClient_->speakerTrans_ = std::make_shared(); speakerClient_->Pause(); + speakerClient_->InitCtrlTrans(); EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, speakerClient_->SendMessage(EVENT_UNKNOWN, content, dstDevId)); EXPECT_EQ(DH_SUCCESS, speakerClient_->SendMessage(NOTIFY_OPEN_SPEAKER_RESULT, content, dstDevId)); - speakerClient_->speakerTrans_ = nullptr; + speakerClient_->speakerCtrlTrans_ = nullptr; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, speakerClient_->SendMessage(NOTIFY_OPEN_SPEAKER_RESULT, content, dstDevId)); } } // DistributedHardware diff --git a/services/audiomanager/managersink/include/daudio_sink_manager.h b/services/audiomanager/managersink/include/daudio_sink_manager.h index 07a4e73469f6dd8dfe0f0e5cbee9dcd9aea46c3d..561f6e7bb84ef488fd984b5123158aab38f2aeee 100644 --- a/services/audiomanager/managersink/include/daudio_sink_manager.h +++ b/services/audiomanager/managersink/include/daudio_sink_manager.h @@ -27,6 +27,7 @@ #include "device_security_info.h" #endif +#include "daudio_ctrl_channel_listener.h" #include "daudio_sink_dev.h" #include "idaudio_source.h" #include "idaudio_sink_ipc_callback.h" @@ -42,6 +43,14 @@ public: int32_t OnProviderEvent(const AVTransEvent &event) override; }; +class CtrlChannelListener : public CtrlChannelListenerCallback { +public: + CtrlChannelListener() {}; + ~CtrlChannelListener() override {}; + + void OnCtrlChannelEvent(const AVTransEvent &event) override; +}; + class DeviceInitCallback : public DmInitCallback { void OnRemoteDied() override; }; @@ -89,6 +98,8 @@ private: ChannelState channelState_ = ChannelState::UNKNOWN; std::shared_ptr providerListener_; + std::shared_ptr ctrlListenerCallback_ = nullptr; + std::shared_ptr ctrlListener_ = nullptr; IAVEngineProvider *sendProviderPtr_ = nullptr; IAVEngineProvider *rcvProviderPtr_ = nullptr; void *pSHandler_ = nullptr; diff --git a/services/audiomanager/managersink/src/daudio_sink_dev.cpp b/services/audiomanager/managersink/src/daudio_sink_dev.cpp index b87c8e44ccd16f1db136f2e1a356039b02da1913..bb705dad52f1d4f8c5fbf13dbb88a2559300e602 100644 --- a/services/audiomanager/managersink/src/daudio_sink_dev.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_dev.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -75,6 +75,7 @@ int32_t DAudioSinkDev::InitAVTransEngines(const ChannelState channelState, IAVEn micClientMap_[DEFAULT_CAPTURE_ID] = std::make_shared(devId_, DEFAULT_CAPTURE_ID, shared_from_this()); micClientMap_[DEFAULT_CAPTURE_ID]->InitSenderEngine(providerPtr); + micClientMap_[DEFAULT_CAPTURE_ID]->InitCtrlTrans(); } if (channelState == ChannelState::SPK_CONTROL_OPENED) { @@ -82,9 +83,11 @@ int32_t DAudioSinkDev::InitAVTransEngines(const ChannelState channelState, IAVEn spkClientMap_[DEFAULT_RENDER_ID] = std::make_shared(devId_, DEFAULT_RENDER_ID, shared_from_this()); spkClientMap_[DEFAULT_RENDER_ID]->InitReceiverEngine(providerPtr); + spkClientMap_[DEFAULT_RENDER_ID]->InitCtrlTrans(); spkClientMap_[LOW_LATENCY_RENDER_ID] = std::make_shared(devId_, LOW_LATENCY_RENDER_ID, shared_from_this()); spkClientMap_[LOW_LATENCY_RENDER_ID]->InitReceiverEngine(providerPtr); + spkClientMap_[LOW_LATENCY_RENDER_ID]->InitCtrlTrans(); } return DH_SUCCESS; } diff --git a/services/audiomanager/managersink/src/daudio_sink_manager.cpp b/services/audiomanager/managersink/src/daudio_sink_manager.cpp index 99d7aeead4f4ce6fe50322eb38ab9b680a30b715..92fe86bc5cefb077fa4cd8059fca2c4d5cb694f0 100644 --- a/services/audiomanager/managersink/src/daudio_sink_manager.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_manager.cpp @@ -87,6 +87,10 @@ int32_t DAudioSinkManager::Init(const sptr &sinkCallback CHECK_AND_RETURN_RET_LOG(sendProviderPtr_->RegisterProviderCallback(providerListener_) != DH_SUCCESS, ERR_DH_AUDIO_FAILED, "%{public}s", "Register av sender engine callback failed."); DHLOGI("Load av sender engine success."); + ctrlListenerCallback_ = std::make_shared(); + ctrlListener_ = std::make_shared(ctrlListenerCallback_); + CHECK_AND_RETURN_RET_LOG(ctrlListener_->Init() != DH_SUCCESS, ERR_DH_AUDIO_FAILED, "ctrlListener init failed"); + DHLOGI("Load ctrl trans success."); return DH_SUCCESS; } @@ -95,6 +99,11 @@ int32_t DAudioSinkManager::UnInit() DHLOGI("UnInit audio sink manager."); UnloadAVSenderEngineProvider(); UnloadAVReceiverEngineProvider(); + if (ctrlListener_ != nullptr) { + ctrlListener_->UnInit(); + ctrlListener_ = nullptr; + ctrlListenerCallback_ = nullptr; + } { std::lock_guard remoteSvrLock(remoteSvrMutex_); sourceServiceMap_.clear(); @@ -390,6 +399,24 @@ int32_t EngineProviderListener::OnProviderEvent(const AVTransEvent &event) return DH_SUCCESS; } +void CtrlChannelListener::OnCtrlChannelEvent(const AVTransEvent &event) +{ + DHLOGI("OnCtrlChannelEvent :%{public}d, eventContent: %{public}s.", event.type, event.content.c_str()); + if (event.type == EventType::EVENT_CHANNEL_OPENED) { + DHLOGI("Received control channel opened event, create audio device for peerDevId=%{public}s, " + "content=%{public}s.", GetAnonyString(event.peerDevId).c_str(), event.content.c_str()); + DAudioSinkManager::GetInstance().SetChannelState(event.content); + DAudioSinkManager::GetInstance().CreateAudioDevice(event.peerDevId); + } else if (event.type == EventType::EVENT_CHANNEL_CLOSED) { + DHLOGI("Received control channel closed event, clear audio device for peerDevId=%{public}s", + GetAnonyString(event.peerDevId).c_str()); + std::string eventStr = event.content; + DAudioSinkManager::GetInstance().NotifyEvent(event.peerDevId, DISABLE_DEVICE, eventStr); + } else { + DHLOGE("Invaild event type."); + } +} + int32_t DAudioSinkManager::PauseDistributedHardware(const std::string &networkId) { std::lock_guard lock(devMapMutex_); diff --git a/services/audiomanager/managersource/include/daudio_io_dev.h b/services/audiomanager/managersource/include/daudio_io_dev.h index 299a6d8a911b1d054f39a20a4dd110ae293a16f7..19758a0f1fea4c9baa4c2b527149ebf44404cb3f 100644 --- a/services/audiomanager/managersource/include/daudio_io_dev.h +++ b/services/audiomanager/managersource/include/daudio_io_dev.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -39,6 +39,7 @@ public: ~DAudioIoDev() override = default; virtual int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) = 0; virtual int32_t InitSenderEngine(IAVEngineProvider *providerPtr) = 0; + virtual int32_t InitCtrlTrans() = 0; virtual int32_t EnableDevice(const int32_t dhId, const std::string &capability) = 0; virtual int32_t DisableDevice(const int32_t dhId) = 0; diff --git a/services/audiomanager/managersource/include/daudio_source_dev.h b/services/audiomanager/managersource/include/daudio_source_dev.h index b97c6625ede798c94345e111567206f919cd8e2b..c7ffff187122c51fc84a4a7a1eb1350ddd49412e 100644 --- a/services/audiomanager/managersource/include/daudio_source_dev.h +++ b/services/audiomanager/managersource/include/daudio_source_dev.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -119,6 +119,7 @@ private: std::shared_ptr FindIoDevImpl(std::string args); int32_t ParseDhidFromEvent(std::string args); int32_t ConvertString2Int(std::string val); + int32_t CreateSpkEngine(std::shared_ptr speaker); int32_t CreateMicEngine(std::shared_ptr mic); void SetRegDataType(const std::string &capability); void NotifyEventInner(const AudioEvent &event); diff --git a/services/audiomanager/managersource/include/dmic_dev.h b/services/audiomanager/managersource/include/dmic_dev.h index 0bfd1a84d19f94e9618cfef69e92197d0aac9cb9..313eb85b79be62b7c7799e83268455a56b006d08 100644 --- a/services/audiomanager/managersource/include/dmic_dev.h +++ b/services/audiomanager/managersource/include/dmic_dev.h @@ -31,6 +31,7 @@ #endif #include "daudio_hdi_handler.h" #include "daudio_io_dev.h" +#include "daudio_source_ctrl_trans.h" #include "iaudio_data_transport.h" #include "iaudio_datatrans_callback.h" #include "iaudio_event_callback.h" @@ -40,6 +41,7 @@ namespace OHOS { namespace DistributedHardware { class DMicDev : public DAudioIoDev, public IAudioDataTransCallback, + public IAudioCtrlTransCallback, public AVReceiverTransportCallback, public std::enable_shared_from_this { public: @@ -51,8 +53,12 @@ public: void OnEngineTransMessage(const std::shared_ptr &message) override; void OnEngineTransDataAvailable(const std::shared_ptr &audioData) override; + void OnCtrlTransEvent(const AVTransEvent &event) override; + void OnCtrlTransMessage(const std::shared_ptr &message) override; + int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override; int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override; + int32_t InitCtrlTrans() override; int32_t EnableDevice(const int32_t dhId, const std::string &capability) override; int32_t DisableDevice(const int32_t dhId) override; @@ -111,6 +117,7 @@ private: std::atomic isTransReady_ = false; std::atomic isOpened_ = false; std::shared_ptr micTrans_ = nullptr; + std::shared_ptr micCtrlTrans_ = nullptr; #ifdef ECHO_CANNEL_ENABLE std::shared_ptr echoManager_ = nullptr; #endif diff --git a/services/audiomanager/managersource/include/dspeaker_dev.h b/services/audiomanager/managersource/include/dspeaker_dev.h index 973ee699e4b87597454ab2d5e20f990c32959711..62aba99ee5bf43d8de1c78daa88d63899c1354db 100644 --- a/services/audiomanager/managersource/include/dspeaker_dev.h +++ b/services/audiomanager/managersource/include/dspeaker_dev.h @@ -27,6 +27,7 @@ #include "daudio_constants.h" #include "daudio_hdi_handler.h" #include "daudio_io_dev.h" +#include "daudio_source_ctrl_trans.h" #include "iaudio_event_callback.h" #include "iaudio_data_transport.h" #include "iaudio_datatrans_callback.h" @@ -37,6 +38,7 @@ namespace DistributedHardware { class DSpeakerDev : public DAudioIoDev, public IAudioDataTransCallback, public AVSenderTransportCallback, + public IAudioCtrlTransCallback, public std::enable_shared_from_this { public: DSpeakerDev(const std::string &devId, std::shared_ptr callback) @@ -46,8 +48,12 @@ public: void OnEngineTransEvent(const AVTransEvent &event) override; void OnEngineTransMessage(const std::shared_ptr &message) override; + void OnCtrlTransEvent(const AVTransEvent &event) override; + void OnCtrlTransMessage(const std::shared_ptr &message) override; + int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override; int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override; + int32_t InitCtrlTrans() override; int32_t EnableDevice(const int32_t dhId, const std::string &capability) override; int32_t DisableDevice(const int32_t dhId) override; @@ -96,6 +102,7 @@ private: int32_t curPort_ = 0; int32_t streamId_ = 0; std::shared_ptr speakerTrans_ = nullptr; + std::shared_ptr speakerCtrlTrans_ = nullptr; // Speaker render parameters AudioParamHDF paramHDF_; diff --git a/services/audiomanager/managersource/src/daudio_source_dev.cpp b/services/audiomanager/managersource/src/daudio_source_dev.cpp index 375a9b1a51590a413f77a8b1c2da232b6794cfbf..141653a80766a835a5124b63352a31dee88b1bc4 100644 --- a/services/audiomanager/managersource/src/daudio_source_dev.cpp +++ b/services/audiomanager/managersource/src/daudio_source_dev.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -825,6 +825,27 @@ void DAudioSourceDev::NotifyFwkIdle(const std::string &devId, const std::string DAudioSourceManager::GetInstance().OnHardwareStateChanged(devId, dhId, DaudioBusinessState::IDLE); } +int32_t DAudioSourceDev::CreateSpkEngine(std::shared_ptr speaker) +{ + CHECK_NULL_RETURN(speaker, ERR_DH_AUDIO_NULLPTR); + int32_t ret = speaker->InitSenderEngine(DAudioSourceManager::GetInstance().getSenderProvider()); + if (ret != DH_SUCCESS) { + DHLOGE("Speaker init sender Engine, error code %{public}d.", ret); + return ret; + } + ret = speaker->InitCtrlTrans(); + if (ret != DH_SUCCESS) { + DHLOGE("Speaker InitCtrlTrans, error code %{public}d.", ret); + return ret; + } + ret = WaitForRPC(NOTIFY_OPEN_CTRL_RESULT); + if (ret != DH_SUCCESS) { + DHLOGE("Speaker init sender engine, create ctrl error."); + return ret; + } + return DH_SUCCESS; +} + int32_t DAudioSourceDev::TaskOpenDSpeaker(const std::string &args) { DAudioHitrace trace("DAudioSourceDev::TaskOpenDSpeaker"); @@ -843,15 +864,9 @@ int32_t DAudioSourceDev::TaskOpenDSpeaker(const std::string &args) NotifyHDF(NOTIFY_OPEN_SPEAKER_RESULT, HDF_EVENT_RESULT_FAILED, dhId); return ERR_DH_AUDIO_NULLPTR; } - int32_t ret = speaker->InitSenderEngine(DAudioSourceManager::GetInstance().getSenderProvider()); - if (ret != DH_SUCCESS) { - DHLOGE("Speaker init sender Engine, error code %{public}d.", ret); - NotifyHDF(NOTIFY_OPEN_SPEAKER_RESULT, HDF_EVENT_INIT_ENGINE_FAILED, dhId); - return ret; - } - ret = WaitForRPC(NOTIFY_OPEN_CTRL_RESULT); + int32_t ret = CreateSpkEngine(speaker); if (ret != DH_SUCCESS) { - DHLOGE("Speaker init sender engine, create ctrl error."); + DHLOGE("Create speaker engine failed."); NotifyHDF(NOTIFY_OPEN_SPEAKER_RESULT, HDF_EVENT_INIT_ENGINE_FAILED, dhId); return ret; } @@ -994,6 +1009,11 @@ int32_t DAudioSourceDev::CreateMicEngine(std::shared_ptr mic) DHLOGE("Init receiver engine failed."); return ret; } + ret = mic->InitCtrlTrans(); + if (ret != DH_SUCCESS) { + DHLOGE("mic InitCtrlTrans, error code %{public}d.", ret); + return ret; + } ret = WaitForRPC(NOTIFY_OPEN_CTRL_RESULT); if (ret != DH_SUCCESS) { DHLOGE("Mic init sender engine, create ctrl error."); diff --git a/services/audiomanager/managersource/src/dmic_dev.cpp b/services/audiomanager/managersource/src/dmic_dev.cpp index 1dd07fd93c4bafbdc4b124fb762c7115f11afd72..c988cbd169a191881cc6fd5ad62df69e7c3ae437 100644 --- a/services/audiomanager/managersource/src/dmic_dev.cpp +++ b/services/audiomanager/managersource/src/dmic_dev.cpp @@ -81,11 +81,6 @@ int32_t DMicDev::InitReceiverEngine(IAVEngineProvider *providerPtr) DHLOGE("Mic dev initialize av receiver adapter failed."); return ret; } - ret = micTrans_->CreateCtrl(); - if (ret != DH_SUCCESS) { - DHLOGE("Create ctrl channel failed. micdev"); - return ret; - } return DH_SUCCESS; } @@ -95,6 +90,39 @@ int32_t DMicDev::InitSenderEngine(IAVEngineProvider *providerPtr) return DH_SUCCESS; } +int32_t DMicDev::InitCtrlTrans() +{ + DHLOGI("InitCtrlTrans enter"); + if (micCtrlTrans_ == nullptr) { + micCtrlTrans_ = std::make_shared(devId_, + SESSIONNAME_MIC_SOURCE, SESSIONNAME_MIC_SINK, shared_from_this()); + } + int32_t ret = micCtrlTrans_->SetUp(shared_from_this()); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "Mic ctrl SetUp failed."); + ret = micCtrlTrans_->Start(); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "Mic ctrl Start failed."); + return ret; +} + +void DMicDev::OnCtrlTransEvent(const AVTransEvent &event) +{ + if (event.type == EventType::EVENT_START_SUCCESS) { + OnStateChange(DATA_OPENED); + } else if ((event.type == EventType::EVENT_STOP_SUCCESS) || + (event.type == EventType::EVENT_CHANNEL_CLOSED) || + (event.type == EventType::EVENT_START_FAIL)) { + OnStateChange(DATA_CLOSED); + } +} + +void DMicDev::OnCtrlTransMessage(const std::shared_ptr &message) +{ + CHECK_NULL_VOID(message); + DHLOGI("On Engine message, type : %{public}s.", GetEventNameByType(message->type_).c_str()); + DAudioSourceManager::GetInstance().HandleDAudioNotify(message->dstDevId_, message->dstDevId_, + message->type_, message->content_); +} + int32_t DMicDev::EnableDevice(const int32_t dhId, const std::string &capability) { DHLOGI("Enable IO device, device pin: %{public}d.", dhId); @@ -316,6 +344,10 @@ int32_t DMicDev::Release() ashmem_ = nullptr; DHLOGI("UnInit ashmem success."); } + if (micCtrlTrans_ != nullptr) { + int32_t res = micCtrlTrans_->Release(); + CHECK_AND_RETURN_RET_LOG(res != DH_SUCCESS, res, "Mic ctrl Release failed."); + } CHECK_NULL_RETURN(micTrans_, DH_SUCCESS); int32_t ret = micTrans_->Release(); @@ -546,8 +578,8 @@ int32_t DMicDev::SendMessage(uint32_t type, std::string content, std::string dst DHLOGE("Send message to remote. not OPEN_MIC or CLOSE_MIC. type: %{public}u", type); return ERR_DH_AUDIO_NULLPTR; } - CHECK_NULL_RETURN(micTrans_, ERR_DH_AUDIO_NULLPTR); - micTrans_->SendMessage(type, content, dstDevId); + CHECK_NULL_RETURN(micCtrlTrans_, ERR_DH_AUDIO_NULLPTR); + micCtrlTrans_->SendAudioEvent(type, content, dstDevId); return DH_SUCCESS; } diff --git a/services/audiomanager/managersource/src/dspeaker_dev.cpp b/services/audiomanager/managersource/src/dspeaker_dev.cpp index a1e2622c73fbd39792a741364f8f7a496a85836a..2393f07ab4b9f318d92f2a96665849e05cc3043a 100644 --- a/services/audiomanager/managersource/src/dspeaker_dev.cpp +++ b/services/audiomanager/managersource/src/dspeaker_dev.cpp @@ -80,13 +80,42 @@ int32_t DSpeakerDev::InitSenderEngine(IAVEngineProvider *providerPtr) DHLOGE("Speaker dev initialize av sender adapter failed."); return ret; } - ret = speakerTrans_->CreateCtrl(); - if (ret != DH_SUCCESS) { - DHLOGE("Create ctrl channel failed."); + return ret; +} + +int32_t DSpeakerDev::InitCtrlTrans() +{ + DHLOGI("InitCtrlTrans enter"); + if (speakerCtrlTrans_ == nullptr) { + speakerCtrlTrans_ = std::make_shared(devId_, + SESSIONNAME_SPK_SOURCE, SESSIONNAME_SPK_SINK, shared_from_this()); } + int32_t ret = speakerCtrlTrans_->SetUp(shared_from_this()); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "Speaker ctrl SetUp failed."); + ret = speakerCtrlTrans_->Start(); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "Speaker ctrl Start failed."); return ret; } +void DSpeakerDev::OnCtrlTransEvent(const AVTransEvent &event) +{ + if (event.type == EventType::EVENT_START_SUCCESS) { + OnStateChange(DATA_OPENED); + } else if ((event.type == EventType::EVENT_STOP_SUCCESS) || + (event.type == EventType::EVENT_CHANNEL_CLOSED) || + (event.type == EventType::EVENT_START_FAIL)) { + OnStateChange(DATA_CLOSED); + } +} + +void DSpeakerDev::OnCtrlTransMessage(const std::shared_ptr &message) +{ + CHECK_NULL_VOID(message); + DHLOGI("On Engine message, type : %{public}s.", GetEventNameByType(message->type_).c_str()); + DAudioSourceManager::GetInstance().HandleDAudioNotify(message->dstDevId_, message->dstDevId_, + message->type_, message->content_); +} + void DSpeakerDev::OnEngineTransEvent(const AVTransEvent &event) { if (event.type == EventType::EVENT_START_SUCCESS) { @@ -246,6 +275,10 @@ int32_t DSpeakerDev::Release() ashmem_ = nullptr; DHLOGI("UnInit ashmem success."); } + if (speakerCtrlTrans_ != nullptr) { + int32_t res = speakerCtrlTrans_->Release(); + CHECK_AND_RETURN_RET_LOG(res != DH_SUCCESS, res, "Speaker ctrl Release failed."); + } CHECK_NULL_RETURN(speakerTrans_, DH_SUCCESS); int32_t ret = speakerTrans_->Release(); if (ret != DH_SUCCESS) { @@ -426,8 +459,8 @@ int32_t DSpeakerDev::SendMessage(uint32_t type, std::string content, std::string DHLOGE("Send message to remote. not OPEN_SPK or CLOSE_SPK. type: %{public}u", type); return ERR_DH_AUDIO_NULLPTR; } - CHECK_NULL_RETURN(speakerTrans_, ERR_DH_AUDIO_NULLPTR); - speakerTrans_->SendMessage(type, content, dstDevId); + CHECK_NULL_RETURN(speakerCtrlTrans_, ERR_DH_AUDIO_NULLPTR); + speakerCtrlTrans_->SendAudioEvent(type, content, dstDevId); return DH_SUCCESS; } diff --git a/services/audiomanager/servicesink/BUILD.gn b/services/audiomanager/servicesink/BUILD.gn index fd205626c84c3246295e2be178f6ec0018389591..4b3e5a17a591cf36392beea2555ab087ac79db40 100755 --- a/services/audiomanager/servicesink/BUILD.gn +++ b/services/audiomanager/servicesink/BUILD.gn @@ -35,6 +35,7 @@ ohos_shared_library("distributed_audio_sink") { "${audio_control_path}/controlsink/include", "${audio_processor_path}/interface", "${audio_transport_path}/interface", + "${audio_transport_path}/audioctrltransport/include", "${audio_transport_path}/receiverengine/include", "${audio_transport_path}/senderengine/include", "${common_path}/dfx_utils/include", @@ -51,6 +52,8 @@ ohos_shared_library("distributed_audio_sink") { "${audio_client_path}/micclient/src/dmic_client.cpp", "${audio_client_path}/spkclient/src/dspeaker_client.cpp", "${audio_control_path}/controlsink/src/daudio_sink_dev_ctrl_manager.cpp", + "${audio_transport_path}/audioctrltransport/src/daudio_ctrl_channel_listener.cpp", + "${audio_transport_path}/audioctrltransport/src/daudio_sink_ctrl_trans.cpp", "${innerkits_path}/native_cpp/audio_sink/src/daudio_sink_handler.cpp", "${innerkits_path}/native_cpp/audio_sink/src/daudio_sink_load_callback.cpp", "${innerkits_path}/native_cpp/audio_sink/src/daudio_sink_proxy.cpp", diff --git a/services/audiomanager/servicesource/BUILD.gn b/services/audiomanager/servicesource/BUILD.gn index 60f94d1fa606378978b70f48a608dd55a9ee82ce..81fb51fc4f908277b2d32c1c2b5f799f0735c5ed 100755 --- a/services/audiomanager/servicesource/BUILD.gn +++ b/services/audiomanager/servicesource/BUILD.gn @@ -34,6 +34,7 @@ ohos_shared_library("distributed_audio_source") { "${audio_control_path}/controlsource/include", "${audio_hdi_proxy_path}/include", "${audio_processor_path}/interface", + "${audio_transport_path}/audioctrltransport/include", "${audio_transport_path}/interface", "${audio_transport_path}/receiverengine/include", "${audio_transport_path}/senderengine/include", @@ -66,6 +67,7 @@ ohos_shared_library("distributed_audio_source") { "${audio_hdi_proxy_path}/src/daudio_hdf_operate.cpp", "${audio_hdi_proxy_path}/src/daudio_hdi_handler.cpp", "${audio_hdi_proxy_path}/src/daudio_manager_callback.cpp", + "${audio_transport_path}/audioctrltransport/src/daudio_source_ctrl_trans.cpp", "${common_path}/dfx_utils/src/daudio_hidumper.cpp", "${interfaces_path}/inner_kits/native_cpp/audio_sink/src/daudio_sink_proxy.cpp", "${interfaces_path}/inner_kits/native_cpp/audio_source/src/daudio_source_proxy.cpp", diff --git a/services/audiomanager/test/unittest/managersink/BUILD.gn b/services/audiomanager/test/unittest/managersink/BUILD.gn index 790b5ad1cdfb9146142b57990989b5a2f6ff42d5..5bbf231484f3b6fba3d9c5a7515f935ff1e1a947 100644 --- a/services/audiomanager/test/unittest/managersink/BUILD.gn +++ b/services/audiomanager/test/unittest/managersink/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022-2023 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -27,6 +27,7 @@ config("module_private_config") { "${audio_client_path}/interface", "${audio_control_path}/controlsink/include", "${audio_processor_path}/interface", + "${audio_transport_path}/audioctrltransport/include", "${audio_transport_path}/interface", "${audio_transport_path}/receiverengine/include", "${audio_transport_path}/senderengine/include", diff --git a/services/audiomanager/test/unittest/sourcedevice/BUILD.gn b/services/audiomanager/test/unittest/sourcedevice/BUILD.gn index 0542a837fbd0b3f8b74bca6449681002e7d60909..638aa1b85a8d4e95a1c6fae832f99fd2fdde712a 100644 --- a/services/audiomanager/test/unittest/sourcedevice/BUILD.gn +++ b/services/audiomanager/test/unittest/sourcedevice/BUILD.gn @@ -24,6 +24,7 @@ config("module_private_config") { "${audio_control_path}/controlsource/include", "${audio_hdi_proxy_path}/include", "${audio_processor_path}/interface", + "${audio_transport_path}/audioctrltransport/include", "${audio_transport_path}/interface", "${audio_transport_path}/receiverengine/include", "${audio_transport_path}/senderengine/include", diff --git a/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp b/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp index 1915e4a5256e095917295cd5f3e785ecba10c6b0..cbbd327e7e2336c018afa7def2128443ace5a7a8 100644 --- a/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp +++ b/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -818,6 +818,7 @@ HWTEST_F(DAudioSourceDevTest, TaskSetVolume_001, TestSize.Level1) auto speaker = std::make_shared(DEV_ID, sourceDev_); sourceDev_->deviceMap_[dhId] = speaker; speaker->speakerTrans_ = std::make_shared(DEV_ID, speaker); + speaker->InitCtrlTrans(); std::string param = "dhId=3;" + std::string(jsonString); EXPECT_EQ(DH_SUCCESS, sourceDev_->TaskSetVolume(param)); cJSON_Delete(jParam); diff --git a/services/audiomanager/test/unittest/sourcedevice/src/dmic_dev_test.cpp b/services/audiomanager/test/unittest/sourcedevice/src/dmic_dev_test.cpp index 5e333186ffd2ec8453574f7230b514c97da5b924..f30bde9aa8c0d88e7208478d133d617fdb89b864 100644 --- a/services/audiomanager/test/unittest/sourcedevice/src/dmic_dev_test.cpp +++ b/services/audiomanager/test/unittest/sourcedevice/src/dmic_dev_test.cpp @@ -413,6 +413,7 @@ HWTEST_F(DMicDevTest, SendMessage_001, TestSize.Level1) EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, mic_->SendMessage(MIC_OPENED, content, dstDevId)); EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, mic_->SendMessage(OPEN_MIC, content, dstDevId)); mic_->micTrans_ = std::make_shared(); + mic_->InitCtrlTrans(); EXPECT_EQ(DH_SUCCESS, mic_->SendMessage(OPEN_MIC, content, dstDevId)); } } // namespace DistributedHardware diff --git a/services/audiomanager/test/unittest/sourcedevice/src/dspeaker_dev_test.cpp b/services/audiomanager/test/unittest/sourcedevice/src/dspeaker_dev_test.cpp index 8ec23d7c2101d6e5c3962c4e848ec842e918e297..f597508c2002e59656e6ca4da2dea58329794d04 100644 --- a/services/audiomanager/test/unittest/sourcedevice/src/dspeaker_dev_test.cpp +++ b/services/audiomanager/test/unittest/sourcedevice/src/dspeaker_dev_test.cpp @@ -330,7 +330,7 @@ HWTEST_F(DSpeakerDevTest, Release_001, TestSize.Level1) spk_->speakerTrans_ = std::make_shared(); EXPECT_EQ(DH_SUCCESS, spk_->Release()); - int32_t fd = 1; + int32_t fd = 10; int32_t ashmemLength = 10; int32_t streamId = 1; int32_t lengthPerTrans = 10; @@ -452,6 +452,7 @@ HWTEST_F(DSpeakerDevTest, SendMessage_001, TestSize.Level1) EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->SendMessage(MIC_OPENED, content, dstDevId)); EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->SendMessage(OPEN_SPEAKER, content, dstDevId)); spk_->speakerTrans_ = std::make_shared(); + spk_->InitCtrlTrans(); EXPECT_EQ(DH_SUCCESS, spk_->SendMessage(OPEN_SPEAKER, content, dstDevId)); } } // namespace DistributedHardware diff --git a/services/audiomanager/test/unittest/sourcemanager/BUILD.gn b/services/audiomanager/test/unittest/sourcemanager/BUILD.gn index 7f32080a0c8da54710ca0442026a140d3c07803e..31767111546b0f4203ab58ec71bbcaee82e9a60d 100644 --- a/services/audiomanager/test/unittest/sourcemanager/BUILD.gn +++ b/services/audiomanager/test/unittest/sourcemanager/BUILD.gn @@ -24,6 +24,7 @@ config("module_private_config") { "${audio_control_path}/controlsource/include", "${audio_hdi_proxy_path}/include", "${audio_processor_path}/interface", + "${audio_transport_path}/audioctrltransport/include", "${audio_transport_path}/audioctrltransport/interface", "${audio_transport_path}/audiotransportstatus/include", "${audio_transport_path}/audiotransportstatus/interface", diff --git a/services/audiotransport/audioctrltransport/include/daudio_ctrl_channel_listener.h b/services/audiotransport/audioctrltransport/include/daudio_ctrl_channel_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..3fce35817029482aa2bcce525d291a56021b6a96 --- /dev/null +++ b/services/audiotransport/audioctrltransport/include/daudio_ctrl_channel_listener.h @@ -0,0 +1,50 @@ +/* + * 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 OHOS_DAUDIO_CTRL_CHANNEL_LISTENER_H +#define OHOS_DAUDIO_CTRL_CHANNEL_LISTENER_H + +#include +#include + +#include "softbus_channel_adapter.h" + +namespace OHOS { +namespace DistributedHardware { +class CtrlChannelListenerCallback { +public: + CtrlChannelListenerCallback() {}; + virtual ~CtrlChannelListenerCallback() = default; + virtual void OnCtrlChannelEvent(const AVTransEvent &event) = 0; +}; +class DaudioCtrlChannelListener : public ISoftbusChannelListener { +public: + DaudioCtrlChannelListener(const std::shared_ptr &callback) + : ctrlTransCallback_(callback) {}; + ~DaudioCtrlChannelListener() override {}; + + int32_t Init(); + int32_t UnInit(); + + void OnChannelEvent(const AVTransEvent &event) override; + void OnStreamReceived(const StreamData *data, const StreamData *ext) override; + +private: + std::shared_ptr ctrlTransCallback_; + std::string sessionName_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/audiotransport/audioctrltransport/include/daudio_sink_ctrl_trans.h b/services/audiotransport/audioctrltransport/include/daudio_sink_ctrl_trans.h new file mode 100644 index 0000000000000000000000000000000000000000..03d90e9619d27d3d55b24fb4cc20ec81f3875700 --- /dev/null +++ b/services/audiotransport/audioctrltransport/include/daudio_sink_ctrl_trans.h @@ -0,0 +1,51 @@ +/* + * 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 OHOS_DAUDIO_SINK_CTRL_TRANS_H +#define OHOS_DAUDIO_SINK_CTRL_TRANS_H + +#include +#include + +#include "iaudio_ctrl_transport.h" + +namespace OHOS { +namespace DistributedHardware { +class DaudioSinkCtrlTrans : public IAudioCtrlTransport, + public ISoftbusChannelListener { +public: + DaudioSinkCtrlTrans(const std::string &devId, const std::string &sessionName, + const std::string &peerSessName, const std::shared_ptr &callback) + : ctrlTransCallback_(callback), devId_(devId), sessionName_(sessionName), peerSessName_(peerSessName) {}; + ~DaudioSinkCtrlTrans() override {}; + + int32_t SetUp(const std::shared_ptr &callback) override; + int32_t Start() override; + int32_t Stop() override; + int32_t Release() override; + int32_t SendAudioEvent(uint32_t type, const std::string &content, const std::string &dstDevId) override; + + void OnChannelEvent(const AVTransEvent &event) override; + void OnStreamReceived(const StreamData *data, const StreamData *ext) override; + +private: + std::weak_ptr ctrlTransCallback_; + std::string devId_; + std::string sessionName_; + std::string peerSessName_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/audiotransport/audioctrltransport/include/daudio_source_ctrl_trans.h b/services/audiotransport/audioctrltransport/include/daudio_source_ctrl_trans.h new file mode 100644 index 0000000000000000000000000000000000000000..3fd862669968ffde10347348a09335ca46b09352 --- /dev/null +++ b/services/audiotransport/audioctrltransport/include/daudio_source_ctrl_trans.h @@ -0,0 +1,56 @@ +/* + * 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 OHOS_DAUDIO_SOURCE_CTRL_TRANS_H +#define OHOS_DAUDIO_SOURCE_CTRL_TRANS_H + +#include +#include + +#include "iaudio_ctrl_transport.h" + +namespace OHOS { +namespace DistributedHardware { +class DaudioSourceCtrlTrans : public IAudioCtrlTransport, + public ISoftbusChannelListener { +public: + DaudioSourceCtrlTrans(const std::string &devId, const std::string &sessionName, + const std::string &peerSessName, const std::shared_ptr &callback) + : ctrlTransCallback_(callback), devId_(devId), sessionName_(sessionName), peerSessName_(peerSessName) {}; + ~DaudioSourceCtrlTrans() override {}; + + int32_t SetUp(const std::shared_ptr &callback) override; + int32_t Start() override; + int32_t Stop() override; + int32_t Release() override; + int32_t SendAudioEvent(uint32_t type, const std::string &content, const std::string &dstDevId) override; + + void OnChannelEvent(const AVTransEvent &event) override; + void OnStreamReceived(const StreamData *data, const StreamData *ext) override; +private: + int32_t WaitForChannelCreated(); + +private: + std::weak_ptr ctrlTransCallback_; + std::string devId_; + std::string sessionName_; + std::string peerSessName_; + std::mutex chnCreatedMtx_; + std::condition_variable chnCreatedCondVar_; + std::atomic chnCreateSuccess_ = false; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/audiotransport/audioctrltransport/src/daudio_ctrl_channel_listener.cpp b/services/audiotransport/audioctrltransport/src/daudio_ctrl_channel_listener.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a8725bd287b632b3deaabc9aaa643c887c2e3db --- /dev/null +++ b/services/audiotransport/audioctrltransport/src/daudio_ctrl_channel_listener.cpp @@ -0,0 +1,73 @@ +/* + * 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. + */ + +#include "daudio_ctrl_channel_listener.h" + +#include + +#include "daudio_constants.h" +#include "daudio_errorcode.h" +#include "daudio_log.h" +#include "daudio_util.h" + +#undef DH_LOG_TAG +#define DH_LOG_TAG "CtrlChannelListener" + +namespace OHOS { +namespace DistributedHardware { +int32_t DaudioCtrlChannelListener::Init() +{ + DHLOGI("Init."); + int32_t ret = SoftbusChannelAdapter::GetInstance().CreateChannelServer(PKG_NAME_D_AUDIO, SESSIONNAME_SPK_SINK); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "spk createChannelServer failed"); + SoftbusChannelAdapter::GetInstance().RegisterChannelListener(SESSIONNAME_SPK_SINK, + AV_TRANS_SPECIAL_DEVICE_ID, this); + + ret = SoftbusChannelAdapter::GetInstance().CreateChannelServer(PKG_NAME_D_AUDIO, SESSIONNAME_MIC_SINK); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "mic createChannelServer failed"); + SoftbusChannelAdapter::GetInstance().RegisterChannelListener(SESSIONNAME_MIC_SINK, + AV_TRANS_SPECIAL_DEVICE_ID, this); + return DH_SUCCESS; +} + +int32_t DaudioCtrlChannelListener::UnInit() +{ + DHLOGI("UnInit."); + SoftbusChannelAdapter::GetInstance().RemoveChannelServer(PKG_NAME_D_AUDIO, SESSIONNAME_SPK_SINK); + SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(SESSIONNAME_SPK_SINK, AV_TRANS_SPECIAL_DEVICE_ID); + + SoftbusChannelAdapter::GetInstance().RemoveChannelServer(PKG_NAME_D_AUDIO, SESSIONNAME_MIC_SINK); + SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(SESSIONNAME_MIC_SINK, AV_TRANS_SPECIAL_DEVICE_ID); + sessionName_ = ""; + ctrlTransCallback_ = nullptr; + return DH_SUCCESS; +} + +void DaudioCtrlChannelListener::OnChannelEvent(const AVTransEvent &event) +{ + CHECK_NULL_VOID(ctrlTransCallback_); + if ((event.type == EventType::EVENT_CHANNEL_OPENED) || (event.type == EventType::EVENT_CHANNEL_CLOSED)) { + DHLOGI("on receiver channel event. event type:%{public}" PRId32, event.type); + ctrlTransCallback_->OnCtrlChannelEvent(event); + } +} + +void DaudioCtrlChannelListener::OnStreamReceived(const StreamData *data, const StreamData *ext) +{ + (void)data; + (void)ext; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/audiotransport/audioctrltransport/src/daudio_sink_ctrl_trans.cpp b/services/audiotransport/audioctrltransport/src/daudio_sink_ctrl_trans.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c678dac097f73313250f613b9d6507e8817a59db --- /dev/null +++ b/services/audiotransport/audioctrltransport/src/daudio_sink_ctrl_trans.cpp @@ -0,0 +1,102 @@ +/* + * 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. + */ + +#include "daudio_sink_ctrl_trans.h" + +#include + +#include "daudio_constants.h" +#include "daudio_errorcode.h" +#include "daudio_log.h" +#include "daudio_util.h" + +#undef DH_LOG_TAG +#define DH_LOG_TAG "SinkCtrlTrans" + +namespace OHOS { +namespace DistributedHardware { +int32_t DaudioSinkCtrlTrans::SetUp(const std::shared_ptr &callback) +{ + DHLOGI("SetUp."); + CHECK_NULL_RETURN(callback, ERR_DH_AUDIO_NULLPTR); + ctrlTransCallback_ = callback; + SoftbusChannelAdapter::GetInstance().RegisterChannelListener(sessionName_, devId_, this); + return DH_SUCCESS; +} + +int32_t DaudioSinkCtrlTrans::Release() +{ + DHLOGI("Release."); + SoftbusChannelAdapter::GetInstance().CloseSoftbusChannel(sessionName_, devId_); + SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, devId_); + return DH_SUCCESS; +} + +int32_t DaudioSinkCtrlTrans::Start() +{ + DHLOGI("Start."); + return DH_SUCCESS; +} + +int32_t DaudioSinkCtrlTrans::Stop() +{ + DHLOGI("Stop."); + return DH_SUCCESS; +} + +int32_t DaudioSinkCtrlTrans::SendAudioEvent(uint32_t type, const std::string &content, const std::string &dstDevId) +{ + DHLOGI("SendAudioEvent, type: %{public}u, content: %{public}s.", type, content.c_str()); + auto message = std::make_shared(type, content, dstDevId); + std::string msgData = message->MarshalMessage(); + return SoftbusChannelAdapter::GetInstance().SendBytesData(sessionName_, message->dstDevId_, msgData); +} + +void DaudioSinkCtrlTrans::OnChannelEvent(const AVTransEvent &event) +{ + DHLOGI("OnChannelEvent, type: %{public}d", event.type); + auto sourceDevObj = ctrlTransCallback_.lock(); + CHECK_NULL_VOID(sourceDevObj); + switch (event.type) { + case EventType::EVENT_CHANNEL_OPEN_FAIL: + case EventType::EVENT_CHANNEL_OPENED: + case EventType::EVENT_CHANNEL_CLOSED: + case EventType::EVENT_START_FAIL: + case EventType::EVENT_START_SUCCESS: + case EventType::EVENT_STOP_SUCCESS: + case EventType::EVENT_ENGINE_ERROR: + case EventType::EVENT_REMOTE_ERROR: + sourceDevObj->OnCtrlTransEvent(event); + break; + case EventType::EVENT_DATA_RECEIVED: { + auto avMessage = std::make_shared(); + CHECK_AND_RETURN_LOG(!avMessage->UnmarshalMessage(event.content, + event.peerDevId), "unmarshal message failed"); + sourceDevObj->OnCtrlTransMessage(avMessage); + break; + } + default: + DHLOGE("Invaild event type."); + break; + } +} + +void DaudioSinkCtrlTrans::OnStreamReceived(const StreamData *data, const StreamData *ext) +{ + (void)data; + (void)ext; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/audiotransport/audioctrltransport/src/daudio_source_ctrl_trans.cpp b/services/audiotransport/audioctrltransport/src/daudio_source_ctrl_trans.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b024f88075da8582c35de51cd0a6d16bdc1df292 --- /dev/null +++ b/services/audiotransport/audioctrltransport/src/daudio_source_ctrl_trans.cpp @@ -0,0 +1,123 @@ +/* + * 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. + */ + +#include "daudio_source_ctrl_trans.h" + +#include + +#include "daudio_constants.h" +#include "daudio_errorcode.h" +#include "daudio_log.h" +#include "daudio_util.h" + +#undef DH_LOG_TAG +#define DH_LOG_TAG "SourceCtrlTrans" + +namespace OHOS { +namespace DistributedHardware { +constexpr int32_t WAIT_TIMEOUT_MS = 5000; +int32_t DaudioSourceCtrlTrans::SetUp(const std::shared_ptr &callback) +{ + DHLOGI("SetUp."); + CHECK_NULL_RETURN(callback, ERR_DH_AUDIO_NULLPTR); + ctrlTransCallback_ = callback; + SoftbusChannelAdapter::GetInstance().RegisterChannelListener(sessionName_, devId_, this); + return DH_SUCCESS; +} + +int32_t DaudioSourceCtrlTrans::Release() +{ + DHLOGI("Release."); + SoftbusChannelAdapter::GetInstance().CloseSoftbusChannel(sessionName_, devId_); + SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, devId_); + chnCreateSuccess_.store(false); + return DH_SUCCESS; +} + +int32_t DaudioSourceCtrlTrans::Start() +{ + DHLOGI("Start."); + int32_t ret = SoftbusChannelAdapter::GetInstance().OpenSoftbusChannel(sessionName_, peerSessName_, devId_); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "OpenSoftbusChannel failed"); + ret = WaitForChannelCreated(); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "Wait for create ctrlChannel failed ret: %{public}d", ret); + return DH_SUCCESS; +} + +int32_t DaudioSourceCtrlTrans::Stop() +{ + DHLOGI("Stop."); + return DH_SUCCESS; +} + +int32_t DaudioSourceCtrlTrans::SendAudioEvent(uint32_t type, const std::string &content, const std::string &dstDevId) +{ + DHLOGI("SendAudioEvent, type: %{public}u, content: %{public}s.", type, content.c_str()); + auto message = std::make_shared(type, content, dstDevId); + std::string msgData = message->MarshalMessage(); + return SoftbusChannelAdapter::GetInstance().SendBytesData(sessionName_, message->dstDevId_, msgData); +} + +void DaudioSourceCtrlTrans::OnChannelEvent(const AVTransEvent &event) +{ + DHLOGI("OnChannelEvent, type: %{public}d", event.type); + auto sourceDevObj = ctrlTransCallback_.lock(); + CHECK_NULL_VOID(sourceDevObj); + switch (event.type) { + case EventType::EVENT_CHANNEL_OPEN_FAIL: + case EventType::EVENT_CHANNEL_OPENED: { + chnCreateSuccess_ = (event.type == EventType::EVENT_CHANNEL_OPENED); + chnCreatedCondVar_.notify_one(); + break; + } + case EventType::EVENT_CHANNEL_CLOSED: + case EventType::EVENT_START_FAIL: + case EventType::EVENT_START_SUCCESS: + case EventType::EVENT_STOP_SUCCESS: + case EventType::EVENT_ENGINE_ERROR: + case EventType::EVENT_REMOTE_ERROR: + sourceDevObj->OnCtrlTransEvent(event); + break; + case EventType::EVENT_DATA_RECEIVED: { + auto avMessage = std::make_shared(); + CHECK_AND_RETURN_LOG(!avMessage->UnmarshalMessage(event.content, + event.peerDevId), "unmarshal message failed"); + sourceDevObj->OnCtrlTransMessage(avMessage); + break; + } + default: + DHLOGE("Invaild event type."); + break; + } +} + +void DaudioSourceCtrlTrans::OnStreamReceived(const StreamData *data, const StreamData *ext) +{ + (void)data; + (void)ext; +} + +int32_t DaudioSourceCtrlTrans::WaitForChannelCreated() +{ + std::unique_lock lock(chnCreatedMtx_); + auto status = chnCreatedCondVar_.wait_for(lock, std::chrono::milliseconds(WAIT_TIMEOUT_MS), + [this]() { return chnCreateSuccess_.load(); }); + CHECK_AND_RETURN_RET_LOG(!status, ERR_DH_AUDIO_SA_WAIT_TIMEOUT, "Wait timeout."); + CHECK_AND_RETURN_RET_LOG(!chnCreateSuccess_.load(), ERR_DH_AV_TRANS_CREATE_CHANNEL_FAILED, + "Create ctrl channel failed."); + return DH_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/audiotransport/interface/iaudio_ctrl_transport.h b/services/audiotransport/interface/iaudio_ctrl_transport.h new file mode 100644 index 0000000000000000000000000000000000000000..00c8d0c6b742e82115efd9cef8bfc925221505e5 --- /dev/null +++ b/services/audiotransport/interface/iaudio_ctrl_transport.h @@ -0,0 +1,39 @@ +/* + * 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 IAUDIO_CTRL_TRANSPORT_H +#define IAUDIO_CTRL_TRANSPORT_H + +#include "audio_data.h" +#include "audio_param.h" +#include "av_trans_message.h" +#include "softbus_channel_adapter.h" +#include "iaudio_ctrltrans_callback.h" + +namespace OHOS { +namespace DistributedHardware { +class IAudioCtrlTransport { +public: + IAudioCtrlTransport() = default; + virtual ~IAudioCtrlTransport() = default; + virtual int32_t SetUp(const std::shared_ptr &callback) = 0; + virtual int32_t Start() = 0; + virtual int32_t Stop() = 0; + virtual int32_t Release() = 0; + virtual int32_t SendAudioEvent(uint32_t type, const std::string &content, const std::string &dstDevId) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // IAUDIO_CTRL_TRANSPORT_H diff --git a/services/audiotransport/interface/iaudio_ctrltrans_callback.h b/services/audiotransport/interface/iaudio_ctrltrans_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..0adaf8320b310cdb50c17a5741e5a16af830c191 --- /dev/null +++ b/services/audiotransport/interface/iaudio_ctrltrans_callback.h @@ -0,0 +1,34 @@ +/* + * 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 IAUDIO_CTRLTRANS_CALLBACK_H +#define IAUDIO_CTRLTRANS_CALLBACK_H + +#include "audio_data.h" +#include "audio_event.h" +#include "audio_param.h" + +namespace OHOS { +namespace DistributedHardware { +class IAudioCtrlTransCallback { +public: + IAudioCtrlTransCallback() = default; + virtual ~IAudioCtrlTransCallback() = default; + virtual void OnCtrlTransEvent(const AVTransEvent &event) = 0; + virtual void OnCtrlTransMessage(const std::shared_ptr &message) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // IAUDIO_CTRLTRANS_CALLBACK_H diff --git a/services/audiotransport/receiverengine/include/av_receiver_engine_adapter.h b/services/audiotransport/receiverengine/include/av_receiver_engine_adapter.h index 43b34149d63b10e300bc87320448b675938fc3d5..0b7966a651bb6ee32ec9ce128ae7044d5f6ac48e 100644 --- a/services/audiotransport/receiverengine/include/av_receiver_engine_adapter.h +++ b/services/audiotransport/receiverengine/include/av_receiver_engine_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -61,7 +61,7 @@ private: std::atomic initialized_ = false; std::mutex chnCreatedMtx_; std::shared_ptr receiverEngine_; - std::shared_ptr adapterCallback_; + std::weak_ptr adapterCallback_; std::atomic chnCreateSuccess_ = false; std::condition_variable chnCreatedCondVar_; }; diff --git a/services/audiotransport/receiverengine/include/av_receiver_engine_transport.h b/services/audiotransport/receiverengine/include/av_receiver_engine_transport.h index bac76247d0f8a0c6ac6ea89c891adee8d917bb11..35db9c4df7b580e357d5ba70d6013afaff172c74 100644 --- a/services/audiotransport/receiverengine/include/av_receiver_engine_transport.h +++ b/services/audiotransport/receiverengine/include/av_receiver_engine_transport.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -70,7 +70,7 @@ private: private: std::shared_ptr receiverAdapter_; - std::shared_ptr transCallback_; + std::weak_ptr transCallback_; std::string devId_; }; } // namespace DistributedHardware diff --git a/services/audiotransport/receiverengine/src/av_receiver_engine_adapter.cpp b/services/audiotransport/receiverengine/src/av_receiver_engine_adapter.cpp index 436ce9cd293b5bc5f0ac795426a82ac61627a425..b67ba92bffe13cfd95d66e7fb930a13b40004cf4 100644 --- a/services/audiotransport/receiverengine/src/av_receiver_engine_adapter.cpp +++ b/services/audiotransport/receiverengine/src/av_receiver_engine_adapter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -136,6 +136,7 @@ int32_t AVTransReceiverAdapter::WaitForChannelCreated() int32_t AVTransReceiverAdapter::OnReceiverEvent(const AVTransEvent &event) { DHLOGI("On receive event, type: %{public}d", event.type); + std::shared_ptr transportObj = nullptr; switch (event.type) { case EventType::EVENT_CHANNEL_OPEN_FAIL: case EventType::EVENT_CHANNEL_OPENED: { @@ -149,9 +150,10 @@ int32_t AVTransReceiverAdapter::OnReceiverEvent(const AVTransEvent &event) case EventType::EVENT_STOP_SUCCESS: case EventType::EVENT_ENGINE_ERROR: case EventType::EVENT_REMOTE_ERROR: - if (adapterCallback_ != nullptr) { + transportObj = adapterCallback_.lock(); + if (transportObj != nullptr) { DHLOGD("On receive event."); - adapterCallback_->OnEngineEvent(event); + transportObj->OnEngineEvent(event); } break; default: @@ -163,16 +165,18 @@ int32_t AVTransReceiverAdapter::OnReceiverEvent(const AVTransEvent &event) int32_t AVTransReceiverAdapter::OnMessageReceived(const std::shared_ptr &message) { - if (adapterCallback_ != nullptr) { - adapterCallback_->OnEngineMessage(message); + auto transportObj = adapterCallback_.lock(); + if (transportObj != nullptr) { + transportObj->OnEngineMessage(message); } return DH_SUCCESS; } int32_t AVTransReceiverAdapter::OnDataAvailable(const std::shared_ptr &buffer) { - if (adapterCallback_ != nullptr) { - adapterCallback_->OnEngineDataAvailable(buffer); + auto transportObj = adapterCallback_.lock(); + if (transportObj != nullptr) { + transportObj->OnEngineDataAvailable(buffer); } return DH_SUCCESS; } diff --git a/services/audiotransport/receiverengine/src/av_receiver_engine_transport.cpp b/services/audiotransport/receiverengine/src/av_receiver_engine_transport.cpp index f5ab6aa187346774d2beac4972512fbda2b7dc68..3067f96f94d36aa0f4903a7bb8885d7b21f48a2c 100644 --- a/services/audiotransport/receiverengine/src/av_receiver_engine_transport.cpp +++ b/services/audiotransport/receiverengine/src/av_receiver_engine_transport.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -113,15 +113,17 @@ int32_t AVTransReceiverTransport::SendMessage(uint32_t type, std::string content void AVTransReceiverTransport::OnEngineEvent(const AVTransEvent &event) { - CHECK_NULL_VOID(transCallback_); - transCallback_->OnEngineTransEvent(event); + auto sourceDevObj = transCallback_.lock(); + CHECK_NULL_VOID(sourceDevObj); + sourceDevObj->OnEngineTransEvent(event); } void AVTransReceiverTransport::OnEngineMessage(const std::shared_ptr &message) { CHECK_NULL_VOID(message); - CHECK_NULL_VOID(transCallback_); - transCallback_->OnEngineTransMessage(message); + auto sourceDevObj = transCallback_.lock(); + CHECK_NULL_VOID(sourceDevObj); + sourceDevObj->OnEngineTransMessage(message); } void AVTransReceiverTransport::OnEngineDataAvailable(const std::shared_ptr &buffer) @@ -136,8 +138,9 @@ void AVTransReceiverTransport::OnEngineDataAvailable(const std::shared_ptrOnEngineTransDataAvailable(audioData); + auto sourceDevObj = transCallback_.lock(); + CHECK_NULL_VOID(sourceDevObj); + sourceDevObj->OnEngineTransDataAvailable(audioData); } int32_t AVTransReceiverTransport::SetParameter(const AudioParam &audioParam) diff --git a/services/audiotransport/senderengine/include/av_sender_engine_adapter.h b/services/audiotransport/senderengine/include/av_sender_engine_adapter.h index fff731b6f0a3340a9800e0c51f5bd96c00bcd655..db88733cc14a55c39afaacec378ca0661f377d29 100644 --- a/services/audiotransport/senderengine/include/av_sender_engine_adapter.h +++ b/services/audiotransport/senderengine/include/av_sender_engine_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -68,7 +68,7 @@ private: std::condition_variable chnCreatedCondVar_; std::atomic chnCreateSuccess_ = false; std::shared_ptr senderEngine_; - std::shared_ptr adapterCallback_; + std::weak_ptr adapterCallback_; }; } // DistributedHardware } // OHOS diff --git a/services/audiotransport/senderengine/include/av_sender_engine_transport.h b/services/audiotransport/senderengine/include/av_sender_engine_transport.h index 7007d555de26b6ebcddc60a2ad7dfc2b3de7c8fe..d646def1c2b057d8781d746cf8bc831cd7cf908f 100644 --- a/services/audiotransport/senderengine/include/av_sender_engine_transport.h +++ b/services/audiotransport/senderengine/include/av_sender_engine_transport.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -69,7 +69,7 @@ private: private: std::shared_ptr senderAdapter_; - std::shared_ptr transCallback_; + std::weak_ptr transCallback_; std::string devId_; }; } // namespace DistributedHardware diff --git a/services/audiotransport/senderengine/src/av_sender_engine_adapter.cpp b/services/audiotransport/senderengine/src/av_sender_engine_adapter.cpp index 11e5ae43ec90513d242609a632410e6f73e3cb0e..958f634f98fff5891d367dfc9d9de73f5b36c6f1 100644 --- a/services/audiotransport/senderengine/src/av_sender_engine_adapter.cpp +++ b/services/audiotransport/senderengine/src/av_sender_engine_adapter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -149,6 +149,7 @@ int32_t AVTransSenderAdapter::WaitForChannelCreated() int32_t AVTransSenderAdapter::OnSenderEvent(const AVTransEvent &event) { DHLOGI("On sender event, type: %{public}d", event.type); + std::shared_ptr transportObj = nullptr; switch (event.type) { case EventType::EVENT_CHANNEL_OPEN_FAIL: case EventType::EVENT_CHANNEL_OPENED: { @@ -162,9 +163,10 @@ int32_t AVTransSenderAdapter::OnSenderEvent(const AVTransEvent &event) case EventType::EVENT_STOP_SUCCESS: case EventType::EVENT_ENGINE_ERROR: case EventType::EVENT_REMOTE_ERROR: - if (adapterCallback_ != nullptr) { + transportObj = adapterCallback_.lock(); + if (transportObj != nullptr) { DHLOGI("Send event."); - adapterCallback_->OnEngineEvent(event); + transportObj->OnEngineEvent(event); } break; default: @@ -176,8 +178,9 @@ int32_t AVTransSenderAdapter::OnSenderEvent(const AVTransEvent &event) int32_t AVTransSenderAdapter::OnMessageReceived(const std::shared_ptr &message) { - if (adapterCallback_ != nullptr) { - adapterCallback_->OnEngineMessage(message); + auto transportObj = adapterCallback_.lock(); + if (transportObj != nullptr) { + transportObj->OnEngineMessage(message); } return DH_SUCCESS; } diff --git a/services/audiotransport/senderengine/src/av_sender_engine_transport.cpp b/services/audiotransport/senderengine/src/av_sender_engine_transport.cpp index 0875d800898d3ab4b79c9af2a877fbacaed695a1..31ed028654ef19e6e1ce2602e4104a72f2578c35 100644 --- a/services/audiotransport/senderengine/src/av_sender_engine_transport.cpp +++ b/services/audiotransport/senderengine/src/av_sender_engine_transport.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -112,15 +112,17 @@ int32_t AVTransSenderTransport::SendMessage(uint32_t type, std::string content, void AVTransSenderTransport::OnEngineEvent(const AVTransEvent &event) { - CHECK_NULL_VOID(transCallback_); - transCallback_->OnEngineTransEvent(event); + auto sourceDevObj = transCallback_.lock(); + CHECK_NULL_VOID(sourceDevObj); + sourceDevObj->OnEngineTransEvent(event); } void AVTransSenderTransport::OnEngineMessage(const std::shared_ptr &message) { CHECK_NULL_VOID(message); - CHECK_NULL_VOID(transCallback_); - transCallback_->OnEngineTransMessage(message); + auto sourceDevObj = transCallback_.lock(); + CHECK_NULL_VOID(sourceDevObj); + sourceDevObj->OnEngineTransMessage(message); } int32_t AVTransSenderTransport::SetParameter(const AudioParam &audioParam) diff --git a/services/audiotransport/test/unittest/BUILD.gn b/services/audiotransport/test/unittest/BUILD.gn index 98a959999321f8552f7d9708cd001d184e29c52f..04b4989be6a9e11c72960f0e93382ccc696fc9f4 100644 --- a/services/audiotransport/test/unittest/BUILD.gn +++ b/services/audiotransport/test/unittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -18,6 +18,7 @@ import("../../../../distributedaudio.gni") group("daudio_transport_test") { testonly = true deps = [ + "${audio_transport_path}/test/unittest/audioctrltransport:ctrl_channel_trans_test", "${audio_transport_path}/test/unittest/receiverengine:av_receiver_engine_adapter_test", "${audio_transport_path}/test/unittest/senderengine:av_sender_engine_adapter_test", ] diff --git a/services/audiotransport/test/unittest/audioctrltransport/BUILD.gn b/services/audiotransport/test/unittest/audioctrltransport/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..2fb7a63aae6d676a35b14892f25e4e13b881b959 --- /dev/null +++ b/services/audiotransport/test/unittest/audioctrltransport/BUILD.gn @@ -0,0 +1,66 @@ +# 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. + +import("//build/test.gni") +import("../../../../../distributedaudio.gni") + +module_out_path = "distributed_audio/services/audiotransport/ctrl_trans_test" + +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "include", + "${common_path}/include", + "${common_path}/dfx_utils/include", + "${audio_transport_path}/audioctrltransport/include", + "${audio_transport_path}/test/unittest/audioctrltransport/include", + "${services_path}/common/audiodata/include", + "${services_path}/common/audioparam", + ] +} + +## UnitTest ctrl_trans_test +ohos_unittest("CtrlTransTest") { + module_out_path = module_out_path + + sources = [ + "src/daudio_ctrl_channel_listener_test.cpp", + "src/daudio_sink_ctrl_trans_test.cpp", + "src/daudio_source_ctrl_trans_test.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "${audio_transport_path}/senderengine:distributed_audio_encode_transport", + "${services_path}/audiomanager/servicesink:distributed_audio_sink", + "${services_path}/audiomanager/servicesource:distributed_audio_source", + "${services_path}/common:distributed_audio_utils", + ] + + external_deps = [ + "audio_framework:audio_capturer", + "audio_framework:audio_client", + "audio_framework:audio_renderer", + "cJSON:cjson", + "distributed_hardware_fwk:distributed_av_sender", + "dsoftbus:softbus_client", + "googletest:gmock", + ] +} + +group("ctrl_channel_trans_test") { + testonly = true + deps = [ ":CtrlTransTest" ] +} diff --git a/services/audiotransport/test/unittest/audioctrltransport/include/daudio_ctrl_channel_listener_test.h b/services/audiotransport/test/unittest/audioctrltransport/include/daudio_ctrl_channel_listener_test.h new file mode 100644 index 0000000000000000000000000000000000000000..f6e671058b6e374885bb60f20cf5c5bee594f821 --- /dev/null +++ b/services/audiotransport/test/unittest/audioctrltransport/include/daudio_ctrl_channel_listener_test.h @@ -0,0 +1,46 @@ +/* + * 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 OHOS_DAUDIO_CTRL_CHANNEL_LISTENER_TEST_H +#define OHOS_DAUDIO_CTRL_CHANNEL_LISTENER_TEST_H + +#include + +#define private public +#include "daudio_ctrl_channel_listener.h" +#undef private + +namespace OHOS { +namespace DistributedHardware { +class CtrlChannelListener : public CtrlChannelListenerCallback { +public: + CtrlChannelListener() {}; + ~CtrlChannelListener() override {}; + + void OnCtrlChannelEvent(const AVTransEvent &event) override; +}; + +class DaudioCtrlChannelListenerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + std::shared_ptr ctrlListenerCallback_ = nullptr; + std::shared_ptr ctrlChannelListener_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DAUDIO_CTRL_CHANNEL_LISTENER_TEST_H diff --git a/services/audiotransport/test/unittest/audioctrltransport/include/daudio_sink_ctrl_trans_test.h b/services/audiotransport/test/unittest/audioctrltransport/include/daudio_sink_ctrl_trans_test.h new file mode 100644 index 0000000000000000000000000000000000000000..ff05587d94bfc50ec18c05a4738d122aee009134 --- /dev/null +++ b/services/audiotransport/test/unittest/audioctrltransport/include/daudio_sink_ctrl_trans_test.h @@ -0,0 +1,47 @@ +/* + * 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 OHOS_DAUDIO_SINK_CTRL_TRANS_TEST_H +#define OHOS_DAUDIO_SINK_CTRL_TRANS_TEST_H + +#include + +#define private public +#include "daudio_sink_ctrl_trans.h" +#undef private + +namespace OHOS { +namespace DistributedHardware { +class SinkCtrlTransCallback : public IAudioCtrlTransCallback { +public: + SinkCtrlTransCallback() {}; + ~SinkCtrlTransCallback() override {}; + + void OnCtrlTransEvent(const AVTransEvent &event) override; + void OnCtrlTransMessage(const std::shared_ptr &message) override; +}; + +class DaudioSinkCtrlTransTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + std::shared_ptr sinkCtrlTransCallback_ = nullptr; + std::shared_ptr ctrlTrans_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DAUDIO_SINK_CTRL_TRANS_TEST_H diff --git a/services/audiotransport/test/unittest/audioctrltransport/include/daudio_source_ctrl_trans_test.h b/services/audiotransport/test/unittest/audioctrltransport/include/daudio_source_ctrl_trans_test.h new file mode 100644 index 0000000000000000000000000000000000000000..49aa309c0a4f583861951b4c32427235cc994707 --- /dev/null +++ b/services/audiotransport/test/unittest/audioctrltransport/include/daudio_source_ctrl_trans_test.h @@ -0,0 +1,47 @@ +/* + * 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 OHOS_DAUDIO_SOURCE_CTRL_TRANS_TEST_H +#define OHOS_DAUDIO_SOURCE_CTRL_TRANS_TEST_H + +#include + +#define private public +#include "daudio_source_ctrl_trans.h" +#undef private + +namespace OHOS { +namespace DistributedHardware { +class CtrlTransCallback : public IAudioCtrlTransCallback { +public: + CtrlTransCallback() {}; + ~CtrlTransCallback() override {}; + + void OnCtrlTransEvent(const AVTransEvent &event) override; + void OnCtrlTransMessage(const std::shared_ptr &message) override; +}; + +class DaudioSourceCtrlTransTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + std::shared_ptr ctrlTransCallback_ = nullptr; + std::shared_ptr ctrlTrans_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DAUDIO_SOURCE_CTRL_TRANS_TEST_H diff --git a/services/audiotransport/test/unittest/audioctrltransport/src/daudio_ctrl_channel_listener_test.cpp b/services/audiotransport/test/unittest/audioctrltransport/src/daudio_ctrl_channel_listener_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7abcd9b972489a2fe18d8ab39de7159d1cd9dd31 --- /dev/null +++ b/services/audiotransport/test/unittest/audioctrltransport/src/daudio_ctrl_channel_listener_test.cpp @@ -0,0 +1,69 @@ +/* + * 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. + */ + +#include "daudio_ctrl_channel_listener_test.h" + +#include "audio_data.h" +#include "daudio_constants.h" +#include "daudio_errorcode.h" +#include "daudio_log.h" +#include "daudio_util.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void DaudioCtrlChannelListenerTest::SetUpTestCase(void) {} + +void DaudioCtrlChannelListenerTest::TearDownTestCase(void) {} + +void DaudioCtrlChannelListenerTest::SetUp() +{ + ctrlListenerCallback_ = std::make_shared(); + ctrlChannelListener_ = std::make_shared(ctrlListenerCallback_); +} + +void DaudioCtrlChannelListenerTest::TearDown() +{ + ctrlChannelListener_ = nullptr; + ctrlListenerCallback_ = nullptr; +} + +void CtrlChannelListener::OnCtrlChannelEvent(const AVTransEvent &event) +{ + (void)event; +} + +/** + * @tc.name: Initialize_001 + * @tc.desc: Verify the Initialize function. + * @tc.type: FUNC + * @tc.require: AR000HTAPM + */ +HWTEST_F(DaudioCtrlChannelListenerTest, Initialize_001, TestSize.Level1) +{ + StreamData data; + StreamData ext; + std::string dataStr = "event"; + std::string peerDevId = "1"; + ctrlChannelListener_->OnStreamReceived(&data, &ext); + AVTransEvent eventOpend = {EventType::EVENT_CHANNEL_OPENED, dataStr, peerDevId}; + AVTransEvent eventClosed = {EventType::EVENT_CHANNEL_CLOSED, dataStr, peerDevId}; + ctrlChannelListener_->OnChannelEvent(eventOpend); + ctrlChannelListener_->OnChannelEvent(eventClosed); + EXPECT_NE(DH_SUCCESS, ctrlChannelListener_->Init()); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/audiotransport/test/unittest/audioctrltransport/src/daudio_sink_ctrl_trans_test.cpp b/services/audiotransport/test/unittest/audioctrltransport/src/daudio_sink_ctrl_trans_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53055b37c9e63bae10c677e037c21dbe42f2e682 --- /dev/null +++ b/services/audiotransport/test/unittest/audioctrltransport/src/daudio_sink_ctrl_trans_test.cpp @@ -0,0 +1,110 @@ +/* + * 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. + */ + +#include "daudio_sink_ctrl_trans_test.h" + +#include "audio_data.h" +#include "daudio_constants.h" +#include "daudio_errorcode.h" +#include "daudio_log.h" +#include "daudio_util.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void DaudioSinkCtrlTransTest::SetUpTestCase(void) {} + +void DaudioSinkCtrlTransTest::TearDownTestCase(void) {} + +void DaudioSinkCtrlTransTest::SetUp() +{ + std::string devId = "devId"; + std::string sessionName = "sessionName"; + std::string peerSessName = "peerSessName"; + sinkCtrlTransCallback_ = std::make_shared(); + ctrlTrans_ = std::make_shared(devId, sessionName, peerSessName, sinkCtrlTransCallback_); +} + +void DaudioSinkCtrlTransTest::TearDown() +{ + ctrlTrans_ = nullptr; + sinkCtrlTransCallback_ = nullptr; +} + +void SinkCtrlTransCallback::OnCtrlTransEvent(const AVTransEvent &event) +{ + (void)event; +} + +void SinkCtrlTransCallback::OnCtrlTransMessage(const std::shared_ptr &message) +{ + (void)message; +} + +/** + * @tc.name: SendAudioEvent_001 + * @tc.desc: Verify the SendAudioEvent function. + * @tc.type: FUNC + * @tc.require: AR000HTAPM + */ +HWTEST_F(DaudioSinkCtrlTransTest, SendAudioEvent_001, TestSize.Level1) +{ + sinkCtrlTransCallback_ = nullptr; + uint32_t type = 0; + EventType eventType = EventType::EVENT_CHANNEL_OPEN_FAIL; + std::string content = "content"; + std::string devId = "devId"; + AVTransEvent event = {eventType, content, devId}; + ctrlTrans_->OnChannelEvent(event); + EXPECT_NE(DH_SUCCESS, ctrlTrans_->SendAudioEvent(type, content, devId)); +} + +/** + * @tc.name: OnChannelEvent_001 + * @tc.desc: Verify the OnChannelEvent function. + * @tc.type: FUNC + * @tc.require: AR000HTAPM + */ +HWTEST_F(DaudioSinkCtrlTransTest, OnChannelEvent_001, TestSize.Level1) +{ + uint32_t eventType = 0; + EventType type = EventType::EVENT_CHANNEL_OPEN_FAIL; + std::string content = "content"; + std::string devId = "devId"; + AVTransEvent event = {type, content, devId}; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_CHANNEL_OPENED; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_CHANNEL_CLOSED; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_START_FAIL; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_START_SUCCESS; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_STOP_SUCCESS; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_ENGINE_ERROR; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_REMOTE_ERROR; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_DATA_RECEIVED; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_ADD_STREAM; + ctrlTrans_->OnChannelEvent(event); + EXPECT_NE(DH_SUCCESS, ctrlTrans_->SendAudioEvent(eventType, content, devId)); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/audiotransport/test/unittest/audioctrltransport/src/daudio_source_ctrl_trans_test.cpp b/services/audiotransport/test/unittest/audioctrltransport/src/daudio_source_ctrl_trans_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21380f984d2c734e932216573ebb17555ff9c2b8 --- /dev/null +++ b/services/audiotransport/test/unittest/audioctrltransport/src/daudio_source_ctrl_trans_test.cpp @@ -0,0 +1,110 @@ +/* + * 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. + */ + +#include "daudio_source_ctrl_trans_test.h" + +#include "audio_data.h" +#include "daudio_constants.h" +#include "daudio_errorcode.h" +#include "daudio_log.h" +#include "daudio_util.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void DaudioSourceCtrlTransTest::SetUpTestCase(void) {} + +void DaudioSourceCtrlTransTest::TearDownTestCase(void) {} + +void DaudioSourceCtrlTransTest::SetUp() +{ + std::string devId = "devId"; + std::string sessionName = "sessionName"; + std::string peerSessName = "peerSessName"; + ctrlTransCallback_ = std::make_shared(); + ctrlTrans_ = std::make_shared(devId, sessionName, peerSessName, ctrlTransCallback_); +} + +void DaudioSourceCtrlTransTest::TearDown() +{ + ctrlTrans_ = nullptr; + ctrlTransCallback_ = nullptr; +} + +void CtrlTransCallback::OnCtrlTransEvent(const AVTransEvent &event) +{ + (void)event; +} + +void CtrlTransCallback::OnCtrlTransMessage(const std::shared_ptr &message) +{ + (void)message; +} + +/** + * @tc.name: SendAudioEvent_001 + * @tc.desc: Verify the SendAudioEvent function. + * @tc.type: FUNC + * @tc.require: AR000HTAPM + */ +HWTEST_F(DaudioSourceCtrlTransTest, SendAudioEvent_001, TestSize.Level1) +{ + ctrlTransCallback_ = nullptr; + uint32_t type = 0; + EventType eventType = EventType::EVENT_CHANNEL_OPEN_FAIL; + std::string content = "content"; + std::string devId = "devId"; + AVTransEvent event = {eventType, content, devId}; + ctrlTrans_->OnChannelEvent(event); + EXPECT_NE(DH_SUCCESS, ctrlTrans_->SendAudioEvent(type, content, devId)); +} + +/** + * @tc.name: OnChannelEvent_001 + * @tc.desc: Verify the OnChannelEvent function. + * @tc.type: FUNC + * @tc.require: AR000HTAPM + */ +HWTEST_F(DaudioSourceCtrlTransTest, OnChannelEvent_001, TestSize.Level1) +{ + uint32_t eventType = 0; + EventType type = EventType::EVENT_CHANNEL_OPEN_FAIL; + std::string content = "content"; + std::string devId = "devId"; + AVTransEvent event = {type, content, devId}; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_CHANNEL_OPENED; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_CHANNEL_CLOSED; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_START_FAIL; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_START_SUCCESS; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_STOP_SUCCESS; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_ENGINE_ERROR; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_REMOTE_ERROR; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_DATA_RECEIVED; + ctrlTrans_->OnChannelEvent(event); + event.type = EventType::EVENT_ADD_STREAM; + ctrlTrans_->OnChannelEvent(event); + EXPECT_NE(DH_SUCCESS, ctrlTrans_->SendAudioEvent(eventType, content, devId)); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file