1 /*
2 * Copyright (c) 2022 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 "audio_ctrl_channel.h"
17 #include "audio_ctrl_transport.h"
18
19 #undef DH_LOG_TAG
20 #define DH_LOG_TAG "AudioCtrlTransport"
21
22 namespace OHOS {
23 namespace DistributedHardware {
SetUp(const std::shared_ptr<IAudioCtrlTransCallback> & callback)24 int32_t AudioCtrlTransport::SetUp(const std::shared_ptr<IAudioCtrlTransCallback> &callback)
25 {
26 DHLOGI("Set up audio control transport.");
27 if (callback == nullptr) {
28 DHLOGE("callback is null.");
29 return ERR_DH_AUDIO_TRANS_ERROR;
30 }
31
32 ctrlTransCallback_ = callback;
33 int32_t ret = InitAudioCtrlTrans(devId_);
34 if (ret != DH_SUCCESS) {
35 DHLOGE("Set up failed ret: %d.", ret);
36 return ret;
37 }
38
39 DHLOGI("Set up audio control transport success.");
40 return DH_SUCCESS;
41 }
42
Release()43 int32_t AudioCtrlTransport::Release()
44 {
45 DHLOGI("Release audio control transport.");
46 if (audioChannel_ == nullptr) {
47 DHLOGE("Channel is already release.");
48 return DH_SUCCESS;
49 }
50
51 int32_t ret = audioChannel_->ReleaseSession();
52 if (ret != DH_SUCCESS) {
53 DHLOGE("Release channel session failed ret: %d.", ret);
54 return ERR_DH_AUDIO_TRANS_ERROR;
55 }
56 audioChannel_ = nullptr;
57
58 DHLOGI("Release success.");
59 return DH_SUCCESS;
60 }
61
Start()62 int32_t AudioCtrlTransport::Start()
63 {
64 DHLOGI("Start audio control transport.");
65 if (audioChannel_ == nullptr) {
66 DHLOGE("Channel is null.");
67 return ERR_DH_AUDIO_TRANS_NULL_VALUE;
68 }
69
70 int ret = audioChannel_->OpenSession();
71 if (ret != DH_SUCCESS) {
72 DHLOGE("Open channel session failed ret: %d.", ret);
73 return ret;
74 }
75
76 DHLOGI("Start success.");
77 return DH_SUCCESS;
78 }
79
Stop()80 int32_t AudioCtrlTransport::Stop()
81 {
82 DHLOGI("Stop audio control transport.");
83 if (audioChannel_ == nullptr) {
84 DHLOGE("Channel is already release.");
85 return DH_SUCCESS;
86 }
87
88 int32_t ret = audioChannel_->CloseSession();
89 if (ret != DH_SUCCESS) {
90 DHLOGE("Close Session failed ret: %d.", ret);
91 return ERR_DH_AUDIO_TRANS_ERROR;
92 }
93
94 DHLOGI("Stop success.");
95 return DH_SUCCESS;
96 }
97
SendAudioEvent(const AudioEvent & event)98 int32_t AudioCtrlTransport::SendAudioEvent(const AudioEvent &event)
99 {
100 DHLOGI("Send audio event.");
101 if (audioChannel_ == nullptr) {
102 DHLOGE("Channel is null.");
103 return ERR_DH_AUDIO_TRANS_NULL_VALUE;
104 }
105
106 int32_t ret = audioChannel_->SendEvent(event);
107 if (ret != DH_SUCCESS) {
108 DHLOGE("Send data failed.");
109 }
110
111 DHLOGI("Send Audio Event success.");
112 return DH_SUCCESS;
113 }
114
OnSessionOpened()115 void AudioCtrlTransport::OnSessionOpened()
116 {
117 DHLOGI("On channel session opened.");
118 auto callback = ctrlTransCallback_.lock();
119 if (callback == nullptr) {
120 DHLOGE("Callback is nullptr.");
121 return;
122 }
123 callback->OnStateChange(AudioEventType::CTRL_OPENED);
124 }
125
OnSessionClosed()126 void AudioCtrlTransport::OnSessionClosed()
127 {
128 DHLOGD("On channel session closed.");
129 auto callback = ctrlTransCallback_.lock();
130 if (callback == nullptr) {
131 DHLOGE("Callback is nullptr.");
132 return;
133 }
134 callback->OnStateChange(AudioEventType::CTRL_CLOSED);
135 }
136
OnDataReceived(const std::shared_ptr<AudioData> & data)137 void AudioCtrlTransport::OnDataReceived(const std::shared_ptr<AudioData> &data)
138 {
139 (void)data;
140 }
141
OnEventReceived(const AudioEvent & event)142 void AudioCtrlTransport::OnEventReceived(const AudioEvent &event)
143 {
144 DHLOGD("Audio event received.");
145 auto callback = ctrlTransCallback_.lock();
146 if (callback == nullptr) {
147 DHLOGE("Callback is null.");
148 return;
149 }
150 callback->OnEventReceived(event);
151 }
152
InitAudioCtrlTrans(const std::string & netWordId)153 int32_t AudioCtrlTransport::InitAudioCtrlTrans(const std::string &netWordId)
154 {
155 if (audioChannel_ == nullptr) {
156 audioChannel_ = std::make_shared<AudioCtrlChannel>(netWordId);
157 }
158
159 int32_t ret = RegisterChannelListener();
160 if (ret != DH_SUCCESS) {
161 DHLOGE("Register channel listener failed ret: %d.", ret);
162 audioChannel_ = nullptr;
163 return ret;
164 }
165 return DH_SUCCESS;
166 }
167
RegisterChannelListener()168 int32_t AudioCtrlTransport::RegisterChannelListener()
169 {
170 DHLOGI("Register channel listener.");
171 if (audioChannel_ == nullptr) {
172 DHLOGE("Channel is null.");
173 return ERR_DH_AUDIO_TRANS_NULL_VALUE;
174 }
175
176 int32_t ret = audioChannel_->CreateSession(shared_from_this(), CTRL_SESSION_NAME);
177 if (ret != DH_SUCCESS) {
178 DHLOGE("Create session failed ret: %d.", ret);
179 return ret;
180 }
181 return DH_SUCCESS;
182 }
183 } // namespace DistributedHardware
184 } // namespace OHOS
185