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_SINK_MANAGER_H
17 #define OHOS_DAUDIO_SINK_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "single_instance.h"
23 #include "device_manager.h"
24 #include "device_manager_callback.h"
25 #ifdef DEVICE_SECURITY_LEVEL_ENABLE
26 #include "device_security_defines.h"
27 #include "device_security_info.h"
28 #endif
29 
30 #include "daudio_sink_dev.h"
31 #include "idaudio_source.h"
32 #include "idaudio_sink_ipc_callback.h"
33 #include "i_av_engine_provider_callback.h"
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 class EngineProviderListener : public IAVEngineProviderCallback {
38 public:
EngineProviderListener()39     EngineProviderListener() {};
40     ~EngineProviderListener() override {};
41 
42     int32_t OnProviderEvent(const AVTransEvent &event) override;
43 };
44 
45 class DeviceInitCallback : public DmInitCallback {
46     void OnRemoteDied() override;
47 };
48 
49 class DAudioSinkManager {
50 DECLARE_SINGLE_INSTANCE_BASE(DAudioSinkManager);
51 public:
52     int32_t Init(const sptr<IDAudioSinkIpcCallback> &sinkCallback);
53     int32_t UnInit();
54     int32_t HandleDAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType,
55         const std::string &eventContent);
56     int32_t DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType,
57         const std::string &eventContent);
58     void OnSinkDevReleased(const std::string &devId);
59     void NotifyEvent(const std::string &devId, const int32_t eventType, const std::string &eventContent);
60     void ClearAudioDev(const std::string &devId);
61     int32_t CreateAudioDevice(const std::string &devId);
62     void SetChannelState(const std::string &content);
63     int32_t PauseDistributedHardware(const std::string &networkId);
64     int32_t ResumeDistributedHardware(const std::string &networkId);
65     int32_t StopDistributedHardware(const std::string &networkId);
66 
67 private:
68     DAudioSinkManager();
69     ~DAudioSinkManager();
70     int32_t LoadAVSenderEngineProvider();
71     int32_t UnloadAVSenderEngineProvider();
72     int32_t LoadAVReceiverEngineProvider();
73     int32_t UnloadAVReceiverEngineProvider();
74     bool CheckDeviceSecurityLevel(const std::string &srcDeviceId, const std::string &dstDeviceId);
75     int32_t GetDeviceSecurityLevel(const std::string &udid);
76     std::string GetUdidByNetworkId(const std::string &networkId);
77     int32_t VerifySecurityLevel(const std::string &devId);
78     int32_t InitAudioDevice(std::shared_ptr<DAudioSinkDev> dev, const std::string &devId, bool isSpkOrMic);
79 
80 private:
81     static constexpr const char* DEVCLEAR_THREAD = "sinkClearTh";
82     std::mutex devMapMutex_;
83     std::unordered_map<std::string, std::shared_ptr<DAudioSinkDev>> audioDevMap_;
84     std::mutex remoteSvrMutex_;
85     std::mutex ipcCallbackMutex_;
86     std::map<std::string, sptr<IDAudioSource>> sourceServiceMap_;
87     std::thread devClearThread_;
88     std::string localNetworkId_;
89     ChannelState channelState_ = ChannelState::UNKNOWN;
90 
91     std::shared_ptr<EngineProviderListener> providerListener_;
92     IAVEngineProvider *sendProviderPtr_ = nullptr;
93     IAVEngineProvider *rcvProviderPtr_ = nullptr;
94     void *pSHandler_ = nullptr;
95     void *pRHandler_ = nullptr;
96     bool isSensitive_ = false;
97     bool isSameAccount_ = false;
98     bool isCheckSecLevel_ = false;
99     sptr<IDAudioSinkIpcCallback> ipcSinkCallback_ = nullptr;
100     std::shared_ptr<DmInitCallback> initCallback_;
101 };
102 } // DistributedHardware
103 } // OHOS
104 #endif // OHOS_DAUDIO_SINK_MANAGER_H
105