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 "daudio_manager_callback.h"
17
18 #include <string>
19 #include <hdf_base.h>
20 #include <cstdlib>
21 #include "iservice_registry.h"
22 #include "iservmgr_hdi.h"
23 #include "iproxy_broker.h"
24
25 #include "daudio_constants.h"
26 #include "daudio_errorcode.h"
27 #include "daudio_hdf_operate.h"
28 #include "daudio_hdi_handler.h"
29 #include "daudio_hitrace.h"
30 #include "daudio_log.h"
31 #include "daudio_util.h"
32
33 #undef DH_LOG_TAG
34 #define DH_LOG_TAG "DAudioHdiHandler"
35
36 namespace OHOS {
37 namespace DistributedHardware {
38 IMPLEMENT_SINGLE_INSTANCE(DAudioHdiHandler);
39
DAudioHdiHandler()40 DAudioHdiHandler::DAudioHdiHandler()
41 {
42 DHLOGD("Distributed audio hdi handler construct.");
43 audioHdiRecipient_ = new AudioHdiRecipient();
44 }
45
~DAudioHdiHandler()46 DAudioHdiHandler::~DAudioHdiHandler()
47 {
48 DHLOGD("Distributed audio hdi handler deconstructed.");
49 }
50
InitHdiHandler()51 int32_t DAudioHdiHandler::InitHdiHandler()
52 {
53 DHLOGI("Init hdi handler.");
54 if (audioSrvHdf_ != nullptr) {
55 return DH_SUCCESS;
56 }
57
58 DHLOGD("Load hdf driver start.");
59 int32_t ret = DaudioHdfOperate::GetInstance().LoadDaudioHDFImpl();
60 if (ret != DH_SUCCESS) {
61 DHLOGE("Load hdf driver failed, ret: %d", ret);
62 return ret;
63 }
64 DHLOGD("Load hdf driver end.");
65
66 audioSrvHdf_ = IDAudioManager::Get(HDF_AUDIO_SERVICE_NAME.c_str(), false);
67 CHECK_NULL_RETURN(audioSrvHdf_, ERR_DH_AUDIO_NULLPTR);
68 remote_ = OHOS::HDI::hdi_objcast<IDAudioManager>(audioSrvHdf_);
69 remote_->AddDeathRecipient(audioHdiRecipient_);
70 DHLOGI("Init hdi handler success.");
71 return DH_SUCCESS;
72 }
73
UninitHdiHandler()74 int32_t DAudioHdiHandler::UninitHdiHandler()
75 {
76 DHLOGI("Unload hdf driver start.");
77 CHECK_NULL_RETURN(remote_, ERR_DH_AUDIO_NULLPTR);
78 remote_->RemoveDeathRecipient(audioHdiRecipient_);
79 CHECK_NULL_RETURN(audioSrvHdf_, DH_SUCCESS);
80
81 int32_t ret = DaudioHdfOperate::GetInstance().UnLoadDaudioHDFImpl();
82 if (ret != DH_SUCCESS) {
83 DHLOGE("Unload hdf driver failed, ret: %d", ret);
84 return ret;
85 }
86 DHLOGI("Uninit hdi handler success.");
87 return DH_SUCCESS;
88 }
89
RegisterAudioDevice(const std::string & devId,const int32_t dhId,const std::string & capability,const std::shared_ptr<IDAudioHdiCallback> & callbackObjParam)90 int32_t DAudioHdiHandler::RegisterAudioDevice(const std::string &devId, const int32_t dhId,
91 const std::string &capability, const std::shared_ptr<IDAudioHdiCallback> &callbackObjParam)
92 {
93 DHLOGI("Register audio device, adpname: %s, dhId: %d", GetAnonyString(devId).c_str(), dhId);
94 CHECK_NULL_RETURN(audioSrvHdf_, ERR_DH_AUDIO_NULLPTR);
95 std::string searchKey;
96 switch (GetDevTypeByDHId(dhId)) {
97 case AUDIO_DEVICE_TYPE_SPEAKER:
98 searchKey = devId + "Speaker" + std::to_string(dhId);
99 break;
100 case AUDIO_DEVICE_TYPE_MIC:
101 searchKey = devId + "Mic" + std::to_string(dhId);
102 break;
103 case AUDIO_DEVICE_TYPE_UNKNOWN:
104 default:
105 DHLOGE("Unknown audio device.");
106 return ERR_DH_AUDIO_NOT_SUPPORT;
107 }
108 {
109 std::lock_guard<std::mutex> devLck(devMapMtx_);
110 auto call = mapAudioMgrCallback_.find(searchKey);
111 if (call == mapAudioMgrCallback_.end()) {
112 const sptr<DAudioManagerCallback> callbackptr(new DAudioManagerCallback(callbackObjParam));
113 mapAudioMgrCallback_.emplace(searchKey, callbackptr);
114 }
115 auto dhIds = mapAudioMgrDhIds_.find(devId);
116 if (dhIds != mapAudioMgrDhIds_.end()) {
117 dhIds->second.insert(dhId);
118 } else {
119 std::set<int32_t> newDhIds;
120 newDhIds.insert(dhId);
121 mapAudioMgrDhIds_.emplace(devId, newDhIds);
122 }
123 }
124
125 auto iter = mapAudioMgrCallback_.find(searchKey);
126 int32_t res = audioSrvHdf_->RegisterAudioDevice(devId, dhId, capability, iter->second);
127 if (res != HDF_SUCCESS) {
128 DHLOGE("Call hdf proxy register failed, res: %d", res);
129 return ERR_DH_AUDIO_HDI_CALL_FAILED;
130 }
131 return DH_SUCCESS;
132 }
133
UnRegisterAudioDevice(const std::string & devId,const int32_t dhId)134 int32_t DAudioHdiHandler::UnRegisterAudioDevice(const std::string &devId, const int32_t dhId)
135 {
136 DHLOGI("Unregister audio device, adpname: %s, dhId: %d", GetAnonyString(devId).c_str(), dhId);
137 CHECK_NULL_RETURN(audioSrvHdf_, ERR_DH_AUDIO_NULLPTR);
138 int32_t res = audioSrvHdf_->UnRegisterAudioDevice(devId, dhId);
139 if (res != HDF_SUCCESS) {
140 DHLOGE("Call hdf proxy unregister failed, res: %d", res);
141 return ERR_DH_AUDIO_HDI_CALL_FAILED;
142 }
143
144 {
145 std::lock_guard<std::mutex> devLck(devMapMtx_);
146 auto iter = mapAudioMgrDhIds_.find(devId);
147 if (iter == mapAudioMgrDhIds_.end()) {
148 DHLOGE("Can not find register devId. devId: %s", GetAnonyString(devId).c_str());
149 return ERR_DH_AUDIO_SA_CALLBACK_NOT_FOUND;
150 }
151
152 iter->second.erase(dhId);
153 if (iter->second.empty()) {
154 mapAudioMgrDhIds_.erase(devId);
155 }
156 }
157 return DH_SUCCESS;
158 }
159
ProcessEventMsg(const AudioEvent & audioEvent,DAudioEvent & newEvent)160 void DAudioHdiHandler::ProcessEventMsg(const AudioEvent &audioEvent, DAudioEvent &newEvent)
161 {
162 switch (audioEvent.type) {
163 case AudioEventType::NOTIFY_OPEN_SPEAKER_RESULT:
164 newEvent.type = AUDIO_EVENT_OPEN_SPK_RESULT;
165 break;
166 case AudioEventType::NOTIFY_CLOSE_SPEAKER_RESULT:
167 newEvent.type = AUDIO_EVENT_CLOSE_SPK_RESULT;
168 break;
169 case AudioEventType::NOTIFY_OPEN_MIC_RESULT:
170 newEvent.type = AUDIO_EVENT_OPEN_MIC_RESULT;
171 break;
172 case AudioEventType::NOTIFY_CLOSE_MIC_RESULT:
173 newEvent.type = AUDIO_EVENT_CLOSE_MIC_RESULT;
174 break;
175 case AudioEventType::VOLUME_CHANGE:
176 newEvent.type = AUDIO_EVENT_VOLUME_CHANGE;
177 break;
178 case AudioEventType::SPEAKER_CLOSED:
179 newEvent.type = AUDIO_EVENT_SPK_CLOSED;
180 break;
181 case AudioEventType::MIC_CLOSED:
182 newEvent.type = AUDIO_EVENT_MIC_CLOSED;
183 break;
184 case AudioEventType::AUDIO_FOCUS_CHANGE:
185 newEvent.type = AUDIO_EVENT_FOCUS_CHANGE;
186 break;
187 case AudioEventType::AUDIO_RENDER_STATE_CHANGE:
188 newEvent.type = AUDIO_EVENT_RENDER_STATE_CHANGE;
189 break;
190 case AudioEventType::NOTIFY_HDF_SPK_DUMP:
191 newEvent.type = AUDIO_EVENT_SPK_DUMP;
192 break;
193 case AudioEventType::NOTIFY_HDF_MIC_DUMP:
194 newEvent.type = AUDIO_EVENT_MIC_DUMP;
195 break;
196 default:
197 DHLOGE("Unsupport audio event.");
198 break;
199 }
200 }
201
NotifyEvent(const std::string & devId,const int32_t dhId,const AudioEvent & audioEvent)202 int32_t DAudioHdiHandler::NotifyEvent(const std::string &devId, const int32_t dhId,
203 const AudioEvent &audioEvent)
204 {
205 DHLOGD("Notify event adpname: %s, dhId: %d, event type: %d, event content: %s.",
206 GetAnonyString(devId).c_str(), dhId, audioEvent.type, audioEvent.content.c_str());
207 DAudioEvent newEvent = {AUDIO_EVENT_UNKNOWN, audioEvent.content};
208 ProcessEventMsg(audioEvent, newEvent);
209
210 CHECK_NULL_RETURN(audioSrvHdf_, ERR_DH_AUDIO_NULLPTR);
211 if (audioSrvHdf_->NotifyEvent(devId, dhId, newEvent) != HDF_SUCCESS) {
212 DHLOGE("Call hdf proxy NotifyEvent failed.");
213 return ERR_DH_AUDIO_HDI_CALL_FAILED;
214 }
215 return DH_SUCCESS;
216 }
217
OnRemoteDied(const wptr<IRemoteObject> & remote)218 void DAudioHdiHandler::AudioHdiRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
219 {
220 DHLOGE("Exit the current process.");
221 _Exit(0);
222 }
223 } // namespace DistributedHardware
224 } // namespace OHOS
225