1 /*
2 * Copyright (c) 2021 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_wrapper.h"
17
18 #include <cstdio>
19 #include <unistd.h>
20
21 #include "aie_log.h"
22 #include "audio_retcode.h"
23 #include "decoder_wrapper.h"
24 #include "hal_display.h"
25
26 namespace OHOS {
27 namespace AI {
28 using namespace OHOS::Media;
29
30 IMPLEMENT_SINGLE_INSTANCE(AudioWrapper);
31
AudioWrapper()32 AudioWrapper::AudioWrapper()
33 {
34 HalPlayerSysInit();
35 HILOGD("[AudioWrapper]ctor");
36 }
37
~AudioWrapper()38 AudioWrapper::~AudioWrapper()
39 {
40 HILOGD("[AudioWrapper]dtor");
41 }
42
SetCodecMode(bool needCodec)43 void AudioWrapper::SetCodecMode(bool needCodec)
44 {
45 std::lock_guard<std::mutex> lock(mutex_);
46 needCodec_ = needCodec;
47 }
48
Init(ConvertType typeId,intptr_t & handler)49 int32_t AudioWrapper::Init(ConvertType typeId, intptr_t &handler)
50 {
51 if (handler != INVALID_HANDLER) {
52 return AUDIO_INIT_FAILURE;
53 }
54 std::lock_guard<std::mutex> lock(mutex_);
55 if (needCodec_ && !hasInitCodec_) {
56 CodecInit();
57 hasInitCodec_ = true;
58 }
59 switch (typeId) {
60 case CONVERT_AAC_TO_PCM:
61 InitAudioDecoder(handler);
62 break;
63 case CONVERT_PCM_TO_AAC:
64 HILOGE("[AudioWrapper]Don't support convert pcm to aac now");
65 return AUDIO_ILLEGAL_TYPE;
66 default:
67 return AUDIO_ILLEGAL_TYPE;
68 }
69 return AUDIO_SUCCESS;
70 }
71
InitAudioDecoder(intptr_t & handler)72 void AudioWrapper::InitAudioDecoder(intptr_t &handler)
73 {
74 CoderWrapper *converter = nullptr;
75 AIE_NEW(converter, DecoderWrapper);
76 if (converter == nullptr) {
77 HILOGE("[AudioWrapper]InitAudioDecoder failed");
78 return;
79 }
80 handler = (intptr_t)(converter);
81 coders_.emplace(handler, converter);
82 }
83
Release()84 void AudioWrapper::Release()
85 {
86 std::lock_guard<std::mutex> lock(mutex_);
87 for (auto iter = coders_.begin(); iter != coders_.end(); ++iter) {
88 if (iter->second->Stop() != AUDIO_SUCCESS) {
89 HILOGE("[AudioWrapper]Release %lld fail", (long long)iter->first);
90 }
91 AIE_DELETE(iter->second);
92 iter->second = nullptr;
93 }
94 coders_.clear();
95 if (needCodec_ && hasInitCodec_) {
96 CodecDeinit();
97 hasInitCodec_ = false;
98 }
99 }
100
Deinit(intptr_t handler)101 int32_t AudioWrapper::Deinit(intptr_t handler)
102 {
103 std::lock_guard<std::mutex> lock(mutex_);
104 auto iter = coders_.find(handler);
105 if (iter == coders_.end()) {
106 return AUDIO_NO_KEY;
107 }
108 int32_t retCode = iter->second->Stop();
109 if (retCode != AUDIO_SUCCESS) {
110 HILOGE("[AudioWrapper]Release %lld fail", (long long)iter->first);
111 }
112 AIE_DELETE(iter->second);
113 iter->second = nullptr;
114 coders_.erase(iter);
115 if (coders_.empty() && needCodec_ && hasInitCodec_) {
116 CodecDeinit();
117 hasInitCodec_ = false;
118 }
119 return retCode;
120 }
121
SetConfig(intptr_t handler,const CoderConfig & config)122 int32_t AudioWrapper::SetConfig(intptr_t handler, const CoderConfig &config)
123 {
124 std::lock_guard<std::mutex> lock(mutex_);
125 auto iter = coders_.find(handler);
126 if (iter == coders_.end()) {
127 return AUDIO_NO_KEY;
128 }
129 return iter->second->Initialize(config);
130 }
131
StartCodec(intptr_t handler)132 int32_t AudioWrapper::StartCodec(intptr_t handler)
133 {
134 std::lock_guard<std::mutex> lock(mutex_);
135 auto iter = coders_.find(handler);
136 if (iter == coders_.end()) {
137 HILOGE("[AudioWrapper]Convert: AUDIO_NO_KEY");
138 return AUDIO_NO_KEY;
139 }
140 if (iter->second->Start() != AUDIO_SUCCESS) {
141 HILOGE("[AudioWrapper]Convert fail to start coder");
142 return AUDIO_CONVERT_FAILURE;
143 }
144 return AUDIO_SUCCESS;
145 }
146
Convert(intptr_t handler,const CoderStream & source,CoderStream & converted)147 int32_t AudioWrapper::Convert(intptr_t handler, const CoderStream &source, CoderStream &converted)
148 {
149 std::lock_guard<std::mutex> lock(mutex_);
150 auto iter = coders_.find(handler);
151 if (iter == coders_.end()) {
152 HILOGE("[AudioWrapper]Convert: AUDIO_NO_KEY");
153 return AUDIO_NO_KEY;
154 }
155 int32_t ret = iter->second->PushSourceStream(source);
156 if (ret != AUDIO_SUCCESS) {
157 HILOGE("[AudioWrapper]Convert: AUDIO_CONVERT_FAILURE");
158 return AUDIO_CONVERT_FAILURE;
159 }
160 return iter->second->PullCodedStream(converted);
161 }
162 } // namespace AI
163 } // namespace OHOS