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