From b28734aa909b7670191df753b772cc1b42f2642c Mon Sep 17 00:00:00 2001 From: byndyx Date: Wed, 27 Nov 2024 16:58:31 +0800 Subject: [PATCH] ctrl to sa Signed-off-by: byndyx --- common/include/daudio_constants.h | 6 +- services/audioclient/interface/imic_client.h | 3 +- services/audioclient/interface/ispk_client.h | 3 +- .../micclient/include/dmic_client.h | 6 + .../audioclient/micclient/src/dmic_client.cpp | 42 +++++- .../spkclient/include/dspeaker_client.h | 7 + .../spkclient/src/dspeaker_client.cpp | 42 +++++- .../test/unittest/micclient/BUILD.gn | 3 +- .../micclient/src/dmic_client_test.cpp | 3 +- .../test/unittest/spkclient/BUILD.gn | 3 +- .../spkclient/src/dspeaker_client_test.cpp | 12 +- .../managersink/include/daudio_sink_manager.h | 11 ++ .../managersink/src/daudio_sink_dev.cpp | 5 +- .../managersink/src/daudio_sink_manager.cpp | 27 ++++ .../managersource/include/daudio_io_dev.h | 3 +- .../managersource/include/daudio_source_dev.h | 3 +- .../managersource/include/dmic_dev.h | 7 + .../managersource/include/dspeaker_dev.h | 7 + .../managersource/src/daudio_source_dev.cpp | 38 ++++-- .../managersource/src/dmic_dev.cpp | 46 ++++++- .../managersource/src/dspeaker_dev.cpp | 43 +++++- services/audiomanager/servicesink/BUILD.gn | 3 + services/audiomanager/servicesource/BUILD.gn | 2 + .../test/unittest/managersink/BUILD.gn | 3 +- .../test/unittest/sourcedevice/BUILD.gn | 1 + .../src/daudio_source_dev_test.cpp | 3 +- .../sourcedevice/src/dmic_dev_test.cpp | 1 + .../sourcedevice/src/dspeaker_dev_test.cpp | 3 +- .../test/unittest/sourcemanager/BUILD.gn | 1 + .../include/daudio_ctrl_channel_listener.h | 50 +++++++ .../include/daudio_sink_ctrl_trans.h | 51 ++++++++ .../include/daudio_source_ctrl_trans.h | 56 ++++++++ .../src/daudio_ctrl_channel_listener.cpp | 73 +++++++++++ .../src/daudio_sink_ctrl_trans.cpp | 102 +++++++++++++++ .../src/daudio_source_ctrl_trans.cpp | 123 ++++++++++++++++++ .../interface/iaudio_ctrl_transport.h | 39 ++++++ .../interface/iaudio_ctrltrans_callback.h | 34 +++++ .../include/av_receiver_engine_adapter.h | 4 +- .../include/av_receiver_engine_transport.h | 4 +- .../src/av_receiver_engine_adapter.cpp | 18 ++- .../src/av_receiver_engine_transport.cpp | 17 ++- .../include/av_sender_engine_adapter.h | 4 +- .../include/av_sender_engine_transport.h | 4 +- .../src/av_sender_engine_adapter.cpp | 13 +- .../src/av_sender_engine_transport.cpp | 12 +- .../audiotransport/test/unittest/BUILD.gn | 3 +- .../test/unittest/audioctrltransport/BUILD.gn | 66 ++++++++++ .../daudio_ctrl_channel_listener_test.h | 46 +++++++ .../include/daudio_sink_ctrl_trans_test.h | 47 +++++++ .../include/daudio_source_ctrl_trans_test.h | 47 +++++++ .../src/daudio_ctrl_channel_listener_test.cpp | 69 ++++++++++ .../src/daudio_sink_ctrl_trans_test.cpp | 110 ++++++++++++++++ .../src/daudio_source_ctrl_trans_test.cpp | 110 ++++++++++++++++ 53 files changed, 1368 insertions(+), 71 deletions(-) create mode 100644 services/audiotransport/audioctrltransport/include/daudio_ctrl_channel_listener.h create mode 100644 services/audiotransport/audioctrltransport/include/daudio_sink_ctrl_trans.h create mode 100644 services/audiotransport/audioctrltransport/include/daudio_source_ctrl_trans.h create mode 100644 services/audiotransport/audioctrltransport/src/daudio_ctrl_channel_listener.cpp create mode 100644 services/audiotransport/audioctrltransport/src/daudio_sink_ctrl_trans.cpp create mode 100644 services/audiotransport/audioctrltransport/src/daudio_source_ctrl_trans.cpp create mode 100644 services/audiotransport/interface/iaudio_ctrl_transport.h create mode 100644 services/audiotransport/interface/iaudio_ctrltrans_callback.h create mode 100644 services/audiotransport/test/unittest/audioctrltransport/BUILD.gn create mode 100644 services/audiotransport/test/unittest/audioctrltransport/include/daudio_ctrl_channel_listener_test.h create mode 100644 services/audiotransport/test/unittest/audioctrltransport/include/daudio_sink_ctrl_trans_test.h create mode 100644 services/audiotransport/test/unittest/audioctrltransport/include/daudio_source_ctrl_trans_test.h create mode 100644 services/audiotransport/test/unittest/audioctrltransport/src/daudio_ctrl_channel_listener_test.cpp create mode 100644 services/audiotransport/test/unittest/audioctrltransport/src/daudio_sink_ctrl_trans_test.cpp create mode 100644 services/audiotransport/test/unittest/audioctrltransport/src/daudio_source_ctrl_trans_test.cpp diff --git a/common/include/daudio_constants.h b/common/include/daudio_constants.h index 35ad9b46..c0136d31 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 7247a5e7..72ba86bf 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 662dcea3..6a3f5586 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 f1b09b55..c2f2ae91 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 22ed7016..c1abce7b 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 052a5140..f4825913 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 4d31a477..dfe0d8c4 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 eefb95b0..86e87c51 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 9f2b2d7c..7f710397 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 8c506328..428be8c9 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 c2737951..c9300223 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 07a4e734..561f6e7b 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 b87c8e44..bb705dad 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 99d7aeea..92fe86bc 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 299a6d8a..19758a0f 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 b97c6625..c7ffff18 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 0bfd1a84..313eb85b 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 973ee699..62aba99e 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 375a9b1a..141653a8 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 1dd07fd9..c988cbd1 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 a1e2622c..2393f07a 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 fd205626..4b3e5a17 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 60f94d1f..81fb51fc 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 790b5ad1..5bbf2314 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 0542a837..638aa1b8 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 1915e4a5..cbbd327e 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 5e333186..f30bde9a 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 8ec23d7c..f597508c 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 7f32080a..31767111 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 00000000..3fce3581 --- /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 00000000..03d90e96 --- /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 00000000..3fd86266 --- /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 00000000..6a8725bd --- /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 00000000..c678dac0 --- /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 00000000..b024f880 --- /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 00000000..00c8d0c6 --- /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 00000000..0adaf832 --- /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 43b34149..0b7966a6 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 bac76247..35db9c4d 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 436ce9cd..b67ba92b 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 f5ab6aa1..3067f96f 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 fff731b6..db88733c 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 7007d555..d646def1 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 11e5ae43..958f634f 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 0875d800..31ed0286 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 98a95999..04b4989b 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 00000000..2fb7a63a --- /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 00000000..f6e67105 --- /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 00000000..ff05587d --- /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 00000000..49aa309c --- /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 00000000..7abcd9b9 --- /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 00000000..53055b37 --- /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 00000000..21380f98 --- /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 -- Gitee