1 /*
2 * Copyright (c) 2022-2023 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_encode_transport.h"
17
18 #include "audio_data_channel.h"
19 #include "audio_encoder_processor.h"
20 #include "audio_direct_processor.h"
21 #include "audio_param.h"
22 #include "daudio_errorcode.h"
23 #include "daudio_log.h"
24
25 #undef DH_LOG_TAG
26 #define DH_LOG_TAG "AudioEncodeTransport"
27
28 namespace OHOS {
29 namespace DistributedHardware {
SetUp(const AudioParam & localParam,const AudioParam & remoteParam,const std::shared_ptr<IAudioDataTransCallback> & callback,const PortCapType capType)30 int32_t AudioEncodeTransport::SetUp(const AudioParam &localParam, const AudioParam &remoteParam,
31 const std::shared_ptr<IAudioDataTransCallback> &callback, const PortCapType capType)
32 {
33 if (callback == nullptr) {
34 DHLOGE("The parameter is empty.");
35 return ERR_DH_AUDIO_TRANS_ERROR;
36 }
37 dataTransCallback_ = callback;
38 context_ = std::make_shared<AudioTransportContext>();
39 context_->SetTransportStatus(TRANSPORT_STATE_STOP);
40 auto ret = InitAudioEncodeTrans(localParam, remoteParam, capType);
41 if (ret != DH_SUCCESS) {
42 DHLOGE("Init audio encode transport, ret: %d.", ret);
43 return ERR_DH_AUDIO_TRANS_ERROR;
44 }
45 capType_ = capType;
46 DHLOGI("SetUp success.");
47 return DH_SUCCESS;
48 }
49
Start()50 int32_t AudioEncodeTransport::Start()
51 {
52 DHLOGI("Start audio encode transport.");
53 if (audioChannel_ == nullptr || context_ == nullptr) {
54 DHLOGE("Audio channel or context is null.");
55 return ERR_DH_AUDIO_NULLPTR;
56 }
57 if (capType_ == CAP_SPK && audioChannel_->OpenSession() != DH_SUCCESS) {
58 DHLOGE("Audio channel open session failed.");
59 return ERR_DH_AUDIO_TRANS_SESSION_NOT_OPEN;
60 }
61 int32_t ret = context_->Start();
62 if (ret != DH_SUCCESS) {
63 DHLOGE("Start failed ret: %d.", ret);
64 audioChannel_->CloseSession();
65 return ret;
66 }
67 DHLOGI("Start success.");
68 return DH_SUCCESS;
69 }
70
Stop()71 int32_t AudioEncodeTransport::Stop()
72 {
73 DHLOGI("Stop audio encode transport.");
74 if (context_ == nullptr) {
75 DHLOGE("Context is null.");
76 return ERR_DH_AUDIO_NULLPTR;
77 }
78 return context_->Stop();
79 }
80
Pause()81 int32_t AudioEncodeTransport::Pause()
82 {
83 DHLOGI("Pause.");
84 if (context_ == nullptr) {
85 DHLOGE("Context is null.");
86 return ERR_DH_AUDIO_NULLPTR;
87 }
88 return context_->Pause();
89 }
90
Restart(const AudioParam & localParam,const AudioParam & remoteParam)91 int32_t AudioEncodeTransport::Restart(const AudioParam &localParam, const AudioParam &remoteParam)
92 {
93 DHLOGI("Restart.");
94 int32_t ret = RegisterProcessorListener(localParam, remoteParam);
95 if (ret != DH_SUCCESS) {
96 DHLOGE("Restart failed, register processor listener failed ret: %d.", ret);
97 processor_ = nullptr;
98 return ERR_DH_AUDIO_TRANS_ERROR;
99 }
100 if (context_ == nullptr) {
101 DHLOGE("Context is null.");
102 return ERR_DH_AUDIO_NULLPTR;
103 }
104 return context_->Restart(localParam, remoteParam);
105 }
106
Release()107 int32_t AudioEncodeTransport::Release()
108 {
109 DHLOGI("Release audio encode transport.");
110 bool releaseStatus = true;
111 int32_t ret;
112 if (processor_ != nullptr) {
113 ret = processor_->ReleaseAudioProcessor();
114 if (ret != DH_SUCCESS) {
115 DHLOGE("Release audio processor failed, ret: %d.", ret);
116 releaseStatus = false;
117 }
118 }
119 if (audioChannel_ != nullptr) {
120 ret = audioChannel_->ReleaseSession();
121 if (ret != DH_SUCCESS) {
122 DHLOGE("Release session failed, ret: %d.", ret);
123 releaseStatus = false;
124 }
125 }
126 if (!releaseStatus) {
127 DHLOGE("The releaseStatus is false: %d.");
128 return ERR_DH_AUDIO_TRANS_ERROR;
129 }
130 DHLOGI("Release success.");
131 return DH_SUCCESS;
132 }
133
FeedAudioData(std::shared_ptr<AudioData> & audioData)134 int32_t AudioEncodeTransport::FeedAudioData(std::shared_ptr<AudioData> &audioData)
135 {
136 DHLOGI("Feed audio data.");
137 if (!processor_) {
138 DHLOGE("Processor is null, setup first.");
139 return ERR_DH_AUDIO_TRANS_NULL_VALUE;
140 }
141
142 int32_t ret = processor_->FeedAudioProcessor(audioData);
143 if (ret != DH_SUCCESS) {
144 DHLOGE("Feed audio processor failed, ret: %d.", ret);
145 return ERR_DH_AUDIO_TRANS_ERROR;
146 }
147 return DH_SUCCESS;
148 }
CreateCtrl()149 int32_t AudioEncodeTransport::CreateCtrl()
150 {
151 DHLOGI("create ctrl not support.");
152 return DH_SUCCESS;
153 }
154
InitEngine(IAVEngineProvider * providerPtr)155 int32_t AudioEncodeTransport::InitEngine(IAVEngineProvider *providerPtr)
156 {
157 (void)providerPtr;
158 DHLOGI("Init engine not support.");
159 return DH_SUCCESS;
160 }
161
SendMessage(uint32_t type,std::string content,std::string dstDevId)162 int32_t AudioEncodeTransport::SendMessage(uint32_t type, std::string content, std::string dstDevId)
163 {
164 (void)type;
165 (void)content;
166 (void)dstDevId;
167 DHLOGI("Send message not support.");
168 return DH_SUCCESS;
169 }
170
InitAudioEncodeTrans(const AudioParam & localParam,const AudioParam & remoteParam,const PortCapType capType)171 int32_t AudioEncodeTransport::InitAudioEncodeTrans(const AudioParam &localParam,
172 const AudioParam &remoteParam, const PortCapType capType)
173 {
174 int32_t ret = RegisterChannelListener(capType);
175 if (ret != DH_SUCCESS) {
176 DHLOGE("Register channel listener failed, ret: %d.", ret);
177 audioChannel_ = nullptr;
178 return ERR_DH_AUDIO_TRANS_ERROR;
179 }
180
181 ret = RegisterProcessorListener(localParam, remoteParam);
182 if (ret != DH_SUCCESS) {
183 DHLOGE("Register processor listener failed, ret: %d.", ret);
184 processor_ = nullptr;
185 return ERR_DH_AUDIO_TRANS_ERROR;
186 }
187 return DH_SUCCESS;
188 }
189
RegisterChannelListener(const PortCapType capType)190 int32_t AudioEncodeTransport::RegisterChannelListener(const PortCapType capType)
191 {
192 DHLOGI("Register channel listener.");
193 audioChannel_ = std::make_shared<AudioDataChannel>(peerDevId_);
194 int32_t result = (capType == CAP_SPK) ?
195 audioChannel_->CreateSession(shared_from_this(), DATA_SPEAKER_SESSION_NAME) :
196 audioChannel_->CreateSession(shared_from_this(), DATA_MIC_SESSION_NAME);
197 if (result != DH_SUCCESS) {
198 DHLOGE("CreateSession failed.");
199 return ERR_DH_AUDIO_TRANS_ERROR;
200 }
201 if (context_ == nullptr) {
202 DHLOGE("Register channel listener error. state Context is null");
203 return ERR_DH_AUDIO_NULLPTR;
204 }
205 context_->SetAudioChannel(audioChannel_);
206 return DH_SUCCESS;
207 }
208
RegisterProcessorListener(const AudioParam & localParam,const AudioParam & remoteParam)209 int32_t AudioEncodeTransport::RegisterProcessorListener(const AudioParam &localParam, const AudioParam &remoteParam)
210 {
211 DHLOGI("Register processor listener.");
212 if (localParam.renderOpts.renderFlags == MMAP_MODE || localParam.captureOpts.capturerFlags == MMAP_MODE) {
213 DHLOGI("Use direct processor, renderFlags: %d, capturerFlags: %d.",
214 localParam.renderOpts.renderFlags, localParam.captureOpts.capturerFlags);
215 processor_ = std::make_shared<AudioDirectProcessor>();
216 } else {
217 processor_ = std::make_shared<AudioEncoderProcessor>();
218 }
219 if (audioChannel_ == nullptr) {
220 DHLOGE("Create audio processor failed.");
221 return ERR_DH_AUDIO_TRANS_ERROR;
222 }
223
224 auto ret = processor_->ConfigureAudioProcessor(localParam.comParam, remoteParam.comParam, shared_from_this());
225 if (ret != DH_SUCCESS) {
226 DHLOGE("Configure audio processor failed.");
227 return ERR_DH_AUDIO_TRANS_ERROR;
228 }
229 if (context_ == nullptr) {
230 DHLOGE("Register processor listener error. state Context is null");
231 return ERR_DH_AUDIO_NULLPTR;
232 }
233 context_->SetAudioProcessor(processor_);
234 return DH_SUCCESS;
235 }
236
OnSessionOpened()237 void AudioEncodeTransport::OnSessionOpened()
238 {
239 DHLOGD("On channel session opened.");
240 auto cbObj = dataTransCallback_.lock();
241 if (cbObj == nullptr) {
242 DHLOGE("On channel session opened. callback is nullptr.");
243 return;
244 }
245 cbObj->OnStateChange(AudioEventType::DATA_OPENED);
246 }
247
OnSessionClosed()248 void AudioEncodeTransport::OnSessionClosed()
249 {
250 DHLOGD("On channel session close.");
251 auto cbObj = dataTransCallback_.lock();
252 if (cbObj == nullptr) {
253 DHLOGE("On channel session closed. callback is nullptr.");
254 return;
255 }
256 cbObj->OnStateChange(AudioEventType::DATA_CLOSED);
257 }
258
OnDataReceived(const std::shared_ptr<AudioData> & data)259 void AudioEncodeTransport::OnDataReceived(const std::shared_ptr<AudioData> &data)
260 {
261 (void)data;
262 }
263
OnEventReceived(const AudioEvent & event)264 void AudioEncodeTransport::OnEventReceived(const AudioEvent &event)
265 {
266 (void)event;
267 }
268
OnAudioDataDone(const std::shared_ptr<AudioData> & outputData)269 void AudioEncodeTransport::OnAudioDataDone(const std::shared_ptr<AudioData> &outputData)
270 {
271 DHLOGD("On audio data done.");
272 if (!audioChannel_) {
273 DHLOGE("Channel is null, setup first.");
274 return;
275 }
276 int32_t ret = audioChannel_->SendData(outputData);
277 if (ret != DH_SUCCESS) {
278 DHLOGE("Send data failed ret: %d.", ret);
279 return;
280 }
281 }
282
OnStateNotify(const AudioEvent & event)283 void AudioEncodeTransport::OnStateNotify(const AudioEvent &event)
284 {
285 (void)event;
286 }
287 } // namespace DistributedHardware
288 } // namespace OHOS
289