1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "daudio_source_ctrl_trans.h"
17
18 #include <dlfcn.h>
19
20 #include "daudio_constants.h"
21 #include "daudio_errorcode.h"
22 #include "daudio_log.h"
23 #include "daudio_util.h"
24
25 #undef DH_LOG_TAG
26 #define DH_LOG_TAG "SourceCtrlTrans"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 constexpr int32_t WAIT_TIMEOUT_MS = 5000;
SetUp(const std::shared_ptr<IAudioCtrlTransCallback> & callback)31 int32_t DaudioSourceCtrlTrans::SetUp(const std::shared_ptr<IAudioCtrlTransCallback> &callback)
32 {
33 DHLOGI("SetUp.");
34 CHECK_NULL_RETURN(callback, ERR_DH_AUDIO_NULLPTR);
35 ctrlTransCallback_ = callback;
36 SoftbusChannelAdapter::GetInstance().RegisterChannelListener(sessionName_, devId_, this);
37 return DH_SUCCESS;
38 }
39
Release()40 int32_t DaudioSourceCtrlTrans::Release()
41 {
42 DHLOGI("Release.");
43 SoftbusChannelAdapter::GetInstance().CloseSoftbusChannel(sessionName_, devId_);
44 SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, devId_);
45 chnCreateSuccess_.store(false);
46 return DH_SUCCESS;
47 }
48
Start()49 int32_t DaudioSourceCtrlTrans::Start()
50 {
51 DHLOGI("Start.");
52 int32_t ret = SoftbusChannelAdapter::GetInstance().OpenSoftbusChannel(sessionName_, peerSessName_, devId_);
53 CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "OpenSoftbusChannel failed");
54 ret = WaitForChannelCreated();
55 CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "Wait for create ctrlChannel failed ret: %{public}d", ret);
56 return DH_SUCCESS;
57 }
58
Stop()59 int32_t DaudioSourceCtrlTrans::Stop()
60 {
61 DHLOGI("Stop.");
62 return DH_SUCCESS;
63 }
64
SendAudioEvent(uint32_t type,const std::string & content,const std::string & dstDevId)65 int32_t DaudioSourceCtrlTrans::SendAudioEvent(uint32_t type, const std::string &content, const std::string &dstDevId)
66 {
67 DHLOGI("SendAudioEvent, type: %{public}u.", type);
68 auto message = std::make_shared<AVTransMessage>(type, content, dstDevId);
69 std::string msgData = message->MarshalMessage();
70 return SoftbusChannelAdapter::GetInstance().SendBytesData(sessionName_, message->dstDevId_, msgData);
71 }
72
OnChannelEvent(const AVTransEvent & event)73 void DaudioSourceCtrlTrans::OnChannelEvent(const AVTransEvent &event)
74 {
75 DHLOGI("OnChannelEvent, type: %{public}d", event.type);
76 auto sourceDevObj = ctrlTransCallback_.lock();
77 CHECK_NULL_VOID(sourceDevObj);
78 switch (event.type) {
79 case EventType::EVENT_CHANNEL_OPEN_FAIL:
80 case EventType::EVENT_CHANNEL_OPENED: {
81 chnCreateSuccess_ = (event.type == EventType::EVENT_CHANNEL_OPENED);
82 chnCreatedCondVar_.notify_one();
83 break;
84 }
85 case EventType::EVENT_CHANNEL_CLOSED:
86 case EventType::EVENT_START_FAIL:
87 case EventType::EVENT_START_SUCCESS:
88 case EventType::EVENT_STOP_SUCCESS:
89 case EventType::EVENT_ENGINE_ERROR:
90 case EventType::EVENT_REMOTE_ERROR:
91 sourceDevObj->OnCtrlTransEvent(event);
92 break;
93 case EventType::EVENT_DATA_RECEIVED: {
94 auto avMessage = std::make_shared<AVTransMessage>();
95 CHECK_AND_RETURN_LOG(!avMessage->UnmarshalMessage(event.content,
96 event.peerDevId), "unmarshal message failed");
97 sourceDevObj->OnCtrlTransMessage(avMessage);
98 break;
99 }
100 default:
101 DHLOGE("Invaild event type.");
102 break;
103 }
104 }
105
OnStreamReceived(const StreamData * data,const StreamData * ext)106 void DaudioSourceCtrlTrans::OnStreamReceived(const StreamData *data, const StreamData *ext)
107 {
108 (void)data;
109 (void)ext;
110 }
111
WaitForChannelCreated()112 int32_t DaudioSourceCtrlTrans::WaitForChannelCreated()
113 {
114 std::unique_lock<std::mutex> lock(chnCreatedMtx_);
115 auto status = chnCreatedCondVar_.wait_for(lock, std::chrono::milliseconds(WAIT_TIMEOUT_MS),
116 [this]() { return chnCreateSuccess_.load(); });
117 CHECK_AND_RETURN_RET_LOG(!status, ERR_DH_AUDIO_SA_WAIT_TIMEOUT, "Wait timeout.");
118 CHECK_AND_RETURN_RET_LOG(!chnCreateSuccess_.load(), ERR_DH_AV_TRANS_CREATE_CHANNEL_FAILED,
119 "Create ctrl channel failed.");
120 return DH_SUCCESS;
121 }
122 } // namespace DistributedHardware
123 } // namespace OHOS