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