• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "daudio_handler.h"
17 
18 #include <vector>
19 
20 #include "audio_system_manager.h"
21 #include "avcodec_list.h"
22 #include "nlohmann/json.hpp"
23 #include "string_ex.h"
24 
25 #include "histreamer_query_tool.h"
26 #include "daudio_constants.h"
27 #include "daudio_errorcode.h"
28 #include "daudio_log.h"
29 #include "daudio_util.h"
30 
31 #undef DH_LOG_TAG
32 #define DH_LOG_TAG "DAudioHandler"
33 
34 using json = nlohmann::json;
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 IMPLEMENT_SINGLE_INSTANCE(DAudioHandler);
39 
DAudioHandler()40 DAudioHandler::DAudioHandler()
41 {
42     encoderInfos_.channelMaxVal = 0;
43     encoderInfos_.channelMinVal = 0;
44     decoderInfos_.channelMaxVal = 0;
45     decoderInfos_.channelMinVal = 0;
46     spkInfos_.channelMaxVal = 0;
47     spkInfos_.channelMinVal = 0;
48     micInfos_.channelMaxVal = 0;
49     micInfos_.channelMinVal = 0;
50     DHLOGD("Distributed audio handler constructed.");
51 }
52 
~DAudioHandler()53 DAudioHandler::~DAudioHandler()
54 {
55     DHLOGD("Distributed audio handler deconstructed.");
56 }
57 
Initialize()58 int32_t DAudioHandler::Initialize()
59 {
60     DHLOGI("Distributed audio handler initialize.");
61     int32_t ret = QueryCodecInfo();
62     if (ret != DH_SUCCESS) {
63         DHLOGE("Failed to query the codec information.");
64         return ret;
65     }
66     ret = QueryAudioInfo();
67     GetSupportAudioInfo(audioInfos_, encoderInfos_, decoderInfos_);
68     return ret;
69 }
70 
Query()71 std::vector<DHItem> DAudioHandler::Query()
72 {
73     DHLOGI("Query distributed hardware information.");
74     auto audioSrv = AudioStandard::AudioSystemManager::GetInstance();
75     std::vector<DHItem> dhItemVec;
76     if (audioSrv == nullptr) {
77         DHLOGE("Unable to get audio system manager.");
78         return dhItemVec;
79     }
80 
81     auto audioDevices = audioSrv->GetDevices(AudioStandard::DeviceFlag::ALL_DEVICES_FLAG);
82     for (auto dev : audioDevices) {
83         auto dhId = audioSrv->GetPinValueFromType(dev->deviceType_, dev->deviceRole_);
84 
85         json infoJson;
86         int32_t deviceType = GetDevTypeByDHId(dhId);
87         if (deviceType == AUDIO_DEVICE_TYPE_MIC) {
88             infoJson["SampleRates"] = micInfos_.sampleRates;
89             infoJson["ChannelMasks"] = micInfos_.channels;
90             infoJson["Formats"] = micInfos_.formats;
91         } else if (deviceType == AUDIO_DEVICE_TYPE_SPEAKER) {
92             infoJson["SampleRates"] = spkInfos_.sampleRates;
93             infoJson["ChannelMasks"] = spkInfos_.channels;
94             infoJson["Formats"] = spkInfos_.formats;
95         }
96         infoJson["INTERRUPT_GROUP_ID"] = dev->interruptGroupId_;
97         infoJson["VOLUME_GROUP_ID"] = dev->volumeGroupId_;
98 
99         std::string audioEncoders =
100             HiStreamerQueryTool::GetInstance().QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE::AUDIO_ENCODER);
101         DHLOGI("DScreen QueryAudioEncoderAbility info: %s", audioEncoders.c_str());
102         infoJson[KEY_HISTREAMER_AUDIO_ENCODER] = audioEncoders;
103 
104         std::string audioDecoders =
105             HiStreamerQueryTool::GetInstance().QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE::AUDIO_DECODER);
106         DHLOGI("DScreen QueryAudioDecoderAbility info: %s", audioDecoders.c_str());
107         infoJson[KEY_HISTREAMER_AUDIO_DECODER] = audioDecoders;
108 
109         DHItem dhItem;
110         dhItem.dhId = std::to_string(dhId);
111         dhItem.attrs = infoJson.dump();
112         dhItemVec.push_back(dhItem);
113         DHLOGD("Query result: dhId: %d, attrs: %s.", dhId, infoJson.dump().c_str());
114     }
115     ablityForDumpVec_ = dhItemVec;
116     return dhItemVec;
117 }
118 
ablityForDump()119 std::vector<DHItem> DAudioHandler::ablityForDump()
120 {
121     DHLOGD("Get audio ablity for dump.");
122     if (ablityForDumpVec_.size() > 0) {
123         return ablityForDumpVec_;
124     }
125     Initialize();
126     Query();
127     return ablityForDumpVec_;
128 }
QueryCodecInfo()129 int32_t DAudioHandler::QueryCodecInfo()
130 {
131     DHLOGD("Query codec information.");
132     auto avCodecList = Media::AVCodecListFactory::CreateAVCodecList();
133     if (avCodecList == nullptr) {
134         DHLOGE("Failed to query the avcodec information.");
135         return ERR_DH_AUDIO_NULLPTR;
136     }
137 
138     bool queryFlag = false;
139     for (auto codec : avCodecList->GetAudioEncoderCaps()) {
140         if (codec == nullptr || codec->GetCodecInfo() == nullptr || codec->GetCodecInfo()->GetName() != AVENC_AAC) {
141             continue;
142         }
143         encoderInfos_.sampleRates = codec->GetSupportedSampleRates();
144         encoderInfos_.formats = codec->GetSupportedFormats();
145         encoderInfos_.channelMaxVal = codec->GetSupportedChannel().maxVal;
146         encoderInfos_.channelMinVal = codec->GetSupportedChannel().minVal;
147         queryFlag = true;
148     }
149 
150     for (auto codec : avCodecList->GetAudioDecoderCaps()) {
151         if (codec == nullptr || codec->GetCodecInfo() == nullptr || codec->GetCodecInfo()->GetName() != AVENC_AAC) {
152             continue;
153         }
154         decoderInfos_.sampleRates = codec->GetSupportedSampleRates();
155         decoderInfos_.formats = codec->GetSupportedFormats();
156         decoderInfos_.channelMaxVal = codec->GetSupportedChannel().maxVal;
157         decoderInfos_.channelMinVal = codec->GetSupportedChannel().minVal;
158         queryFlag = true;
159     }
160 
161     if (!queryFlag) {
162         DHLOGE("Failed to query the codec information.");
163         return ERR_DH_AUDIO_FAILED;
164     }
165     return DH_SUCCESS;
166 }
167 
QueryAudioInfo()168 int32_t DAudioHandler::QueryAudioInfo()
169 {
170     DHLOGD("Start to query codec information.");
171     audioInfos_.sampleRates = OHOS::AudioStandard::AudioCapturer::GetSupportedSamplingRates();
172     audioInfos_.formats = OHOS::AudioStandard::AudioCapturer::GetSupportedFormats();
173     audioInfos_.channels = OHOS::AudioStandard::AudioCapturer::GetSupportedChannels();
174     return DH_SUCCESS;
175 }
176 
GetSupportAudioInfo(AudioInfo & audioInfos,CoderInfo & encoderInfos,CoderInfo & decoderInfos)177 void DAudioHandler::GetSupportAudioInfo(AudioInfo &audioInfos, CoderInfo &encoderInfos,
178     CoderInfo &decoderInfos)
179 {
180     for (auto iter = audioInfos.sampleRates.begin(); iter != audioInfos.sampleRates.end(); iter++) {
181         if (std::find(encoderInfos.sampleRates.begin(), encoderInfos.sampleRates.end(), *iter) !=
182             encoderInfos.sampleRates.end()) {
183             micInfos_.sampleRates.push_back(*iter);
184         }
185         if (std::find(decoderInfos.sampleRates.begin(), decoderInfos.sampleRates.end(), *iter) !=
186             decoderInfos.sampleRates.end()) {
187             spkInfos_.sampleRates.push_back(*iter);
188         }
189     }
190 
191     for (auto iter = audioInfos.formats.begin(); iter != audioInfos.formats.end(); iter++) {
192         if (std::find(encoderInfos.formats.begin(), encoderInfos.formats.end(), *iter) != encoderInfos.formats.end()) {
193             micInfos_.formats.push_back(*iter);
194         }
195         if (std::find(decoderInfos.formats.begin(), decoderInfos.formats.end(), *iter) != decoderInfos.formats.end()) {
196             spkInfos_.formats.push_back(*iter);
197         }
198     }
199 
200     for (auto iter = audioInfos.channels.begin(); iter != audioInfos.channels.end(); iter++) {
201         if (*iter <= encoderInfos.channelMaxVal && *iter >= encoderInfos.channelMinVal) {
202             micInfos_.channels.push_back(*iter);
203         }
204         if (*iter <= decoderInfos.channelMaxVal && *iter >= decoderInfos.channelMinVal) {
205             spkInfos_.channels.push_back(*iter);
206         }
207     }
208     if (micInfos_.sampleRates.empty()) {
209         micInfos_.sampleRates.push_back(SAMPLE_RATE_DEFAULT);
210     }
211     if (spkInfos_.sampleRates.empty()) {
212         spkInfos_.sampleRates.push_back(SAMPLE_RATE_DEFAULT);
213     }
214     if (micInfos_.channels.empty()) {
215         micInfos_.channels.push_back(CHANNEL_COUNT_DEFAULT);
216     }
217     if (spkInfos_.channels.empty()) {
218         spkInfos_.channels.push_back(CHANNEL_COUNT_DEFAULT);
219     }
220     if (micInfos_.formats.empty()) {
221         micInfos_.formats.push_back(SAMPLE_FORMAT_DEFAULT);
222     }
223     if (spkInfos_.formats.empty()) {
224         spkInfos_.formats.push_back(SAMPLE_FORMAT_DEFAULT);
225     }
226 }
227 
QueryExtraInfo()228 std::map<std::string, std::string> DAudioHandler::QueryExtraInfo()
229 {
230     DHLOGD("Query extra information");
231     std::map<std::string, std::string> extraInfo;
232     return extraInfo;
233 }
234 
IsSupportPlugin()235 bool DAudioHandler::IsSupportPlugin()
236 {
237     DHLOGD("Is support plug in");
238     return false;
239 }
240 
RegisterPluginListener(std::shared_ptr<PluginListener> listener)241 void DAudioHandler::RegisterPluginListener(std::shared_ptr<PluginListener> listener)
242 {
243     DHLOGI("Register plugin listener");
244     if (listener == nullptr) {
245         DHLOGE("The parameter is empty.");
246         return;
247     }
248     listener_ = listener;
249 }
250 
UnRegisterPluginListener()251 void DAudioHandler::UnRegisterPluginListener()
252 {
253     DHLOGI("UnRegister plugin listener");
254     listener_ = nullptr;
255 }
256 
GetHardwareHandler()257 IHardwareHandler* GetHardwareHandler()
258 {
259     return &DAudioHandler::GetInstance();
260 }
261 } // namespace DistributedHardware
262 } // namespace OHOS
263