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