• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_SOURCE_MANAGER_H
17 #define OHOS_DAUDIO_SOURCE_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <thread>
22 
23 #include "event_handler.h"
24 
25 #include "daudio_hdi_handler.h"
26 #include "daudio_source_dev.h"
27 #include "daudio_source_mgr_callback.h"
28 #include "idaudio_sink.h"
29 #include "single_instance.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 class DAudioSourceManager {
34     DECLARE_SINGLE_INSTANCE_BASE(DAudioSourceManager);
35 
36 public:
37     int32_t Init(const sptr<IDAudioIpcCallback> &callback);
38     int32_t UnInit();
39     int32_t EnableDAudio(const std::string &devId, const std::string &dhId, const std::string &version,
40         const std::string &attrs, const std::string &reqId);
41     int32_t DisableDAudio(const std::string &devId, const std::string &dhId, const std::string &reqId);
42     int32_t HandleDAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType,
43         const std::string &eventContent);
44     int32_t DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType,
45         const std::string &eventContent);
46     int32_t OnEnableDAudio(const std::string &devId, const std::string &dhId, const int32_t result);
47     int32_t OnDisableDAudio(const std::string &devId, const std::string &dhId, const int32_t result);
48     int32_t OnHardwareStateChanged(const std::string &devId, const std::string &dhId, const int32_t state);
49     int32_t OnDataSyncTrigger(const std::string &devId);
50     int32_t LoadAVSenderEngineProvider();
51     int32_t UnloadAVSenderEngineProvider();
52     int32_t LoadAVReceiverEngineProvider();
53     int32_t UnloadAVReceiverEngineProvider();
54     IAVEngineProvider *getSenderProvider();
55     IAVEngineProvider *getReceiverProvider();
56     void SetCallerTokenId(uint64_t tokenId);
57 
58 private:
59     DAudioSourceManager();
60     ~DAudioSourceManager();
61     int32_t CreateAudioDevice(const std::string &devId);
62     void DeleteAudioDevice(const std::string &devId, const std::string &dhId);
63     std::string GetRequestId(const std::string &devId, const std::string &dhId);
64     void ClearAudioDev(const std::string &devId);
65     void ListenAudioDev();
66     void RestoreThreadStatus();
67     int32_t DoEnableDAudio(const std::string &args);
68     int32_t DoDisableDAudio(const std::string &args);
69 
70     typedef struct {
71         std::string devId;
72         std::shared_ptr<DAudioSourceDev> dev;
73         std::map<std::string, std::string> ports;
74     } AudioDevice;
75 
76 private:
77     static constexpr const char* DEVCLEAR_THREAD = "sourceClearTh";
78     static constexpr const char* LISTEN_THREAD = "sourceListenTh";
79     static constexpr int32_t WATCHDOG_INTERVAL_TIME = 20000;
80     static constexpr int32_t WATCHDOG_DELAY_TIME = 5000;
81     static constexpr size_t SLEEP_TIME = 1000000;
82     static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000;
83 
84     std::string localDevId_;
85     std::mutex devMapMtx_;
86     std::map<std::string, AudioDevice> audioDevMap_;
87     std::mutex remoteSvrMutex_;
88     std::mutex ipcCallbackMutex_;
89     std::map<std::string, sptr<IDAudioSink>> sinkServiceMap_;
90     sptr<IDAudioIpcCallback> ipcCallback_ = nullptr;
91     std::shared_ptr<DAudioSourceMgrCallback> daudioMgrCallback_ = nullptr;
92     std::thread devClearThread_;
93     std::thread listenThread_;
94     IAVEngineProvider *sendProviderPtr_ = nullptr;
95     IAVEngineProvider *rcvProviderPtr_ = nullptr;
96     void *pSHandler_ = nullptr;
97     void *pRHandler_ = nullptr;
98     std::atomic<bool> isHicollieRunning_ = true;
99     uint64_t callerTokenId_ = 0;
100 
101     class SourceManagerHandler : public AppExecFwk::EventHandler {
102     public:
103         SourceManagerHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner);
104         ~SourceManagerHandler() override;
105         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
106 
107     private:
108         void EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event);
109         void DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event);
110         int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam);
111 
112     private:
113         using SourceManagerFunc = void (SourceManagerHandler::*)(const AppExecFwk::InnerEvent::Pointer &event);
114         std::map<uint32_t, SourceManagerFunc> mapEventFuncs_;
115     };
116     std::shared_ptr<SourceManagerHandler> handler_;
117 };
118 } // DistributedHardware
119 } // OHOS
120 #endif // OHOS_DAUDIO_SINK_MANAGER_H
121