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#ifndef OHOS_DAUDIO_HDI_HANDLER_H
17#define OHOS_DAUDIO_HDI_HANDLER_H
18
19#include <map>
20#include <mutex>
21#include <set>
22
23#include <v2_0/id_audio_callback.h>
24#include <v2_0/id_audio_manager.h>
25#include "iremote_object.h"
26
27#include "audio_event.h"
28#include "daudio_manager_callback.h"
29#include "idaudio_hdi_callback.h"
30#include "single_instance.h"
31
32namespace OHOS {
33namespace DistributedHardware {
34using OHOS::HDI::DistributedAudio::Audioext::V2_0::DAudioEvent;
35using OHOS::HDI::DistributedAudio::Audioext::V2_0::IDAudioCallback;
36using OHOS::HDI::DistributedAudio::Audioext::V2_0::IDAudioManager;
37class DAudioHdiHandler {
38    DECLARE_SINGLE_INSTANCE_BASE(DAudioHdiHandler);
39
40public:
41    int32_t InitHdiHandler();
42
43    int32_t UninitHdiHandler();
44
45    int32_t RegisterAudioDevice(const std::string &devId, const int32_t dhId, const std::string &capability,
46        const std::shared_ptr<IDAudioHdiCallback> &callbackObjParam);
47
48    int32_t UnRegisterAudioDevice(const std::string &devId, const int32_t dhId);
49
50    int32_t NotifyEvent(const std::string &devId, const int32_t dhId,
51        const int32_t streamId, const AudioEvent &audioEvent);
52
53private:
54    DAudioHdiHandler();
55    ~DAudioHdiHandler();
56    void ProcessEventMsg(const AudioEvent &audioEvent, DAudioEvent &newEvent);
57
58    class AudioHdiRecipient : public IRemoteObject::DeathRecipient {
59    public:
60        void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
61    };
62    sptr<AudioHdiRecipient> audioHdiRecipient_;
63
64    const std::string HDF_AUDIO_SERVICE_NAME = "daudio_ext_service";
65    std::mutex devMapMtx_;
66    sptr<IDAudioManager> audioSrvHdf_;
67    std::map<std::string, sptr<DAudioManagerCallback>> mapAudioMgrCallback_;
68    std::map<std::string, std::set<int32_t>> mapAudioMgrDhIds_;
69    sptr<IRemoteObject> remote_;
70};
71} // DistributedHardware
72} // OHOS
73#endif // OHOS_DAUDIO_HDI_HANDLER_H
74