• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_encoder_processor.h"
17 
18 #include "daudio_errorcode.h"
19 #include "daudio_hisysevent.h"
20 #include "daudio_hitrace.h"
21 #include "daudio_log.h"
22 #include "audio_encoder.h"
23 
24 #undef DH_LOG_TAG
25 #define DH_LOG_TAG "AudioEncoderProcessor"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
~AudioEncoderProcessor()29 AudioEncoderProcessor::~AudioEncoderProcessor()
30 {
31     if (audioEncoder_ != nullptr) {
32         DHLOGD("Release audio processor.");
33         StopAudioProcessor();
34         ReleaseAudioProcessor();
35     }
36 }
37 
ConfigureAudioProcessor(const AudioCommonParam & localDevParam,const AudioCommonParam & remoteDevParam,const std::shared_ptr<IAudioProcessorCallback> & procCallback)38 int32_t AudioEncoderProcessor::ConfigureAudioProcessor(const AudioCommonParam &localDevParam,
39     const AudioCommonParam &remoteDevParam, const std::shared_ptr<IAudioProcessorCallback> &procCallback)
40 {
41     DHLOGI("Configure audio processor.");
42     if (procCallback == nullptr) {
43         DHLOGE("Processor callback is null.");
44         return ERR_DH_AUDIO_BAD_VALUE;
45     }
46     localDevParam_ = localDevParam;
47     remoteDevParam_ = remoteDevParam;
48     procCallback_ = procCallback;
49 
50     audioEncoder_ = std::make_shared<AudioEncoder>();
51     int32_t ret = audioEncoder_->ConfigureAudioCodec(localDevParam, shared_from_this());
52     if (ret != DH_SUCCESS) {
53         DHLOGE("Configure encoder fail. Error code: %d.", ret);
54         ReleaseAudioProcessor();
55         return ret;
56     }
57     return DH_SUCCESS;
58 }
59 
ReleaseAudioProcessor()60 int32_t AudioEncoderProcessor::ReleaseAudioProcessor()
61 {
62     DHLOGI("Release audio processor.");
63     if (audioEncoder_ == nullptr) {
64         DHLOGE("Encoder is null.");
65         return DH_SUCCESS;
66     }
67 
68     DAUDIO_SYNC_TRACE(DAUDIO_RELEASE_ENCODER_PROCESSOR);
69     int32_t ret = audioEncoder_->ReleaseAudioCodec();
70     if (ret != DH_SUCCESS) {
71         DHLOGE("Release encoder fail. Error code: %d.", ret);
72     }
73 
74     audioEncoder_ = nullptr;
75     return DH_SUCCESS;
76 }
77 
StartAudioProcessor()78 int32_t AudioEncoderProcessor::StartAudioProcessor()
79 {
80     DHLOGI("Start audio processor.");
81     if (audioEncoder_ == nullptr) {
82         DHLOGE("Encoder is null.");
83         DAudioHisysevent::GetInstance().SysEventWriteFault(DAUDIO_OPT_FAIL, ERR_DH_AUDIO_BAD_VALUE,
84             "daudio encoder is null.");
85         return ERR_DH_AUDIO_BAD_VALUE;
86     }
87 
88     DAUDIO_SYNC_TRACE(DAUDIO_START_ENCODER_PROCESSOR);
89     int32_t ret = audioEncoder_->StartAudioCodec();
90     if (ret != DH_SUCCESS) {
91         DHLOGE("Start encoder fail. Error code: %d.", ret);
92         DAudioHisysevent::GetInstance().SysEventWriteFault(DAUDIO_OPT_FAIL, ret,
93             "daudio start encoder fail.");
94         return ret;
95     }
96 
97     return DH_SUCCESS;
98 }
99 
StopAudioProcessor()100 int32_t AudioEncoderProcessor::StopAudioProcessor()
101 {
102     DHLOGI("Stop audio processor.");
103     if (audioEncoder_ == nullptr) {
104         DHLOGE("Encoder is null.");
105         DAudioHisysevent::GetInstance().SysEventWriteFault(DAUDIO_OPT_FAIL, ERR_DH_AUDIO_BAD_VALUE,
106             "daudio encoder is null.");
107         return DH_SUCCESS;
108     }
109 
110     DAUDIO_SYNC_TRACE(DAUDIO_STOP_ENCODER_PROCESSOR);
111     int32_t ret = audioEncoder_->StopAudioCodec();
112     if (ret != DH_SUCCESS) {
113         DHLOGE("Stop encoder fail. Error code: %d.", ret);
114         DAudioHisysevent::GetInstance().SysEventWriteFault(DAUDIO_OPT_FAIL, ret,
115             "daudio stop decoder fail.");
116         return ret;
117     }
118 
119     return DH_SUCCESS;
120 }
121 
FeedAudioProcessor(const std::shared_ptr<AudioData> & inputData)122 int32_t AudioEncoderProcessor::FeedAudioProcessor(const std::shared_ptr<AudioData> &inputData)
123 {
124     DHLOGD("Feed audio processor.");
125     if (inputData == nullptr) {
126         DHLOGE("Input data is null.");
127         return ERR_DH_AUDIO_BAD_VALUE;
128     }
129 
130     if (audioEncoder_ == nullptr) {
131         DHLOGE("Encoder is null.");
132         return ERR_DH_AUDIO_BAD_VALUE;
133     }
134     return audioEncoder_->FeedAudioData(inputData);
135 }
136 
OnCodecDataDone(const std::shared_ptr<AudioData> & outputData)137 void AudioEncoderProcessor::OnCodecDataDone(const std::shared_ptr<AudioData> &outputData)
138 {
139     if (outputData == nullptr) {
140         DHLOGE("Output data is null.");
141         return;
142     }
143     DHLOGD("Codec done. Output data size %zu.", outputData->Size());
144 
145     auto cbObj = procCallback_.lock();
146     if (cbObj == nullptr) {
147         DHLOGE("Processor callback is null.");
148         return;
149     }
150     cbObj->OnAudioDataDone(outputData);
151 }
152 
OnCodecStateNotify(const AudioEvent & event)153 void AudioEncoderProcessor::OnCodecStateNotify(const AudioEvent &event)
154 {
155     DHLOGD("Codec state notify.");
156     auto cbObj = procCallback_.lock();
157     if (cbObj == nullptr) {
158         DHLOGE("Processor callback is null.");
159         return;
160     }
161     cbObj->OnStateNotify(event);
162 }
163 } // namespace DistributedHardware
164 } // namespace OHOS