• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 "string_ex.h"
22 
23 #include "daudio_constants.h"
24 #include "daudio_errorcode.h"
25 #include "daudio_log.h"
26 #include "daudio_util.h"
27 
28 #undef DH_LOG_TAG
29 #define DH_LOG_TAG "DAudioHandler"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 IMPLEMENT_SINGLE_INSTANCE(DAudioHandler);
34 
DAudioHandler()35 DAudioHandler::DAudioHandler()
36 {
37     DHLOGD("Distributed audio handler constructed.");
38 }
39 
~DAudioHandler()40 DAudioHandler::~DAudioHandler()
41 {
42     DHLOGD("Distributed audio handler deconstructed.");
43 }
44 
Initialize()45 int32_t DAudioHandler::Initialize()
46 {
47     DHLOGI("Distributed audio handler initialize.");
48     return QueryAudioInfo();
49 }
50 
AddItemsToObject(DHItem & dhItem,cJSON * infoJson,const int32_t & dhId)51 bool DAudioHandler::AddItemsToObject(DHItem &dhItem, cJSON* infoJson, const int32_t &dhId)
52 {
53     DHLOGD("Get dhId and then add other items into cjson object");
54     int32_t deviceType = GetDevTypeByDHId(dhId);
55     if (deviceType == AUDIO_DEVICE_TYPE_MIC) {
56         return AddParamsToJson(dhItem, infoJson, MIC, micInfos_);
57     } else if (deviceType == AUDIO_DEVICE_TYPE_SPEAKER) {
58         return AddParamsToJson(dhItem, infoJson, SPEAKER, spkInfos_);
59     }
60     return true;
61 }
62 
AddParamsToJson(DHItem & dhItem,cJSON * infoJson,const std::string & subtype,const AudioInfo & infos)63 bool DAudioHandler::AddParamsToJson(DHItem &dhItem, cJSON* infoJson, const std::string &subtype, const AudioInfo &infos)
64 {
65     dhItem.subtype = subtype;
66     cJSON *sampleArray = cJSON_CreateArray();
67     CHECK_NULL_RETURN(sampleArray, false);
68     cJSON_AddItemToObject(infoJson, SAMPLERATES, sampleArray);
69     for (const auto &value : infos.sampleRates) {
70         cJSON_AddItemToArray(sampleArray, cJSON_CreateNumber(static_cast<uint32_t>(value)));
71     }
72     cJSON *channelArray = cJSON_CreateArray();
73     CHECK_NULL_RETURN(channelArray, false);
74     cJSON_AddItemToObject(infoJson, CHANNELMASKS, channelArray);
75     for (const auto &value : infos.channels) {
76         cJSON_AddItemToArray(channelArray, cJSON_CreateNumber(static_cast<uint32_t>(value)));
77     }
78     cJSON *formatsArray = cJSON_CreateArray();
79     CHECK_NULL_RETURN(formatsArray, false);
80     cJSON_AddItemToObject(infoJson, FORMATS, formatsArray);
81     for (const auto &value : infos.formats) {
82         cJSON_AddItemToArray(formatsArray, cJSON_CreateNumber(static_cast<uint32_t>(value)));
83     }
84     cJSON *usageArray = cJSON_CreateArray();
85     CHECK_NULL_RETURN(usageArray, false);
86     cJSON_AddItemToObject(infoJson, SUPPORTEDSTREAM, usageArray);
87     for (const auto &value : supportedStream_) {
88         cJSON_AddItemToArray(usageArray, cJSON_CreateString(value.c_str()));
89     }
90     cJSON *codecArray = cJSON_CreateArray();
91     CHECK_NULL_RETURN(codecArray, false);
92     cJSON_AddItemToObject(infoJson, CODEC, codecArray);
93     for (const auto &value : codec_) {
94         cJSON_AddItemToArray(codecArray, cJSON_CreateString(value.c_str()));
95     }
96     cJSON_AddStringToObject(infoJson, PROTOCOLVER, VERSION_TWO);
97     return true;
98 }
99 
QueryMeta()100 std::vector<DHItem> DAudioHandler::QueryMeta()
101 {
102     DHLOGI("Query meta distributed hardware information.");
103     return RealQuery(KEY_TYPE_META);
104 }
105 
Query()106 std::vector<DHItem> DAudioHandler::Query()
107 {
108     DHLOGI("Query full distributed hardware information.");
109     return RealQuery(KEY_TYPE_FULL);
110 }
111 
RealQuery(const std::string & dataType)112 std::vector<DHItem> DAudioHandler::RealQuery(const std::string &dataType)
113 {
114     auto audioSrv = AudioStandard::AudioSystemManager::GetInstance();
115     std::vector<DHItem> dhItemVec;
116     CHECK_AND_RETURN_RET_LOG(audioSrv == nullptr, dhItemVec, "Unable to get audio system manager.");
117     auto audioDevices = audioSrv->GetDevices(AudioStandard::DeviceFlag::ALL_DEVICES_FLAG);
118     for (auto dev : audioDevices) {
119         if (dev == nullptr) {
120             continue;
121         }
122         auto dhId = audioSrv->GetPinValueFromType(dev->deviceType_, dev->deviceRole_);
123         if (dhId != DEFAULT_RENDER_ID && dhId != DEFAULT_CAPTURE_ID) {
124             continue;
125         }
126 
127         cJSON* infoJson = cJSON_CreateObject();
128         if (infoJson == nullptr) {
129             DHLOGE("Failed to create cJSON object.");
130             return dhItemVec;
131         }
132         DHItem dhItem;
133         if (!AddItemsToObject(dhItem, infoJson, dhId)) {
134             cJSON_Delete(infoJson);
135             return dhItemVec;
136         }
137         cJSON_AddNumberToObject(infoJson, INTERRUPT_GROUP_ID, dev->interruptGroupId_);
138         cJSON_AddNumberToObject(infoJson, VOLUME_GROUP_ID, dev->volumeGroupId_);
139         cJSON_AddStringToObject(infoJson, KEY_DATATYPE, dataType.c_str());
140         dhItem.dhId = std::to_string(dhId);
141         char *jsonInfo = cJSON_Print(infoJson);
142         if (jsonInfo == NULL) {
143             DHLOGE("Failed to create JSON data.");
144             cJSON_Delete(infoJson);
145             return dhItemVec;
146         }
147         dhItem.attrs = jsonInfo;
148         dhItemVec.push_back(dhItem);
149         DHLOGD("Query result: dhId: %{public}d, subtype: %{public}s, attrs: %{public}s.",
150             dhId, dhItem.subtype.c_str(), jsonInfo);
151         if (dhId == DEFAULT_RENDER_ID) {
152             dhItem.dhId = std::to_string(LOW_LATENCY_RENDER_ID);
153             dhItemVec.push_back(dhItem);
154             DHLOGD("Query result: dhId: %{public}d, attrs: %{public}s.", LOW_LATENCY_RENDER_ID, jsonInfo);
155         }
156         cJSON_Delete(infoJson);
157         cJSON_free(jsonInfo);
158     }
159     DHLOGD("Query result: size: (%{public}zu).", dhItemVec.size());
160     ablityForDumpVec_ = dhItemVec;
161     return dhItemVec;
162 }
163 
ablityForDump()164 std::vector<DHItem> DAudioHandler::ablityForDump()
165 {
166     DHLOGD("Get audio ablity for dump.");
167     if (ablityForDumpVec_.size() > 0) {
168         return ablityForDumpVec_;
169     }
170     Initialize();
171     Query();
172     return ablityForDumpVec_;
173 }
174 
QueryAudioInfo()175 int32_t DAudioHandler::QueryAudioInfo()
176 {
177     DHLOGD("Start to query codec information.");
178     micInfos_.sampleRates = OHOS::AudioStandard::AudioCapturer::GetSupportedSamplingRates();
179     micInfos_.formats = OHOS::AudioStandard::AudioCapturer::GetSupportedFormats();
180     micInfos_.channels = OHOS::AudioStandard::AudioCapturer::GetSupportedChannels();
181     spkInfos_.sampleRates = OHOS::AudioStandard::AudioRenderer::GetSupportedSamplingRates();
182     spkInfos_.formats = OHOS::AudioStandard::AudioRenderer::GetSupportedFormats();
183     spkInfos_.channels = OHOS::AudioStandard::AudioRenderer::GetSupportedChannels();
184     supportedStream_.push_back(MUSIC);
185     codec_.push_back(AAC);
186     codec_.push_back(PCM);
187     return DH_SUCCESS;
188 }
189 
QueryExtraInfo()190 std::map<std::string, std::string> DAudioHandler::QueryExtraInfo()
191 {
192     DHLOGD("Query extra information");
193     std::map<std::string, std::string> extraInfo;
194     return extraInfo;
195 }
196 
IsSupportPlugin()197 bool DAudioHandler::IsSupportPlugin()
198 {
199     DHLOGD("Is support plug in");
200     return false;
201 }
202 
RegisterPluginListener(std::shared_ptr<PluginListener> listener)203 void DAudioHandler::RegisterPluginListener(std::shared_ptr<PluginListener> listener)
204 {
205     DHLOGI("Register plugin listener");
206     CHECK_NULL_VOID(listener);
207     listener_ = listener;
208 }
209 
UnRegisterPluginListener()210 void DAudioHandler::UnRegisterPluginListener()
211 {
212     DHLOGI("UnRegister plugin listener");
213     listener_ = nullptr;
214 }
215 
GetHardwareHandler()216 IHardwareHandler* GetHardwareHandler()
217 {
218     return &DAudioHandler::GetInstance();
219 }
220 } // namespace DistributedHardware
221 } // namespace OHOS
222