1 /*
2  * Copyright (c) 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_AVROUTER_IMPL_H
17 #define OHOS_AVROUTER_IMPL_H
18 
19 #include "av_router.h"
20 #include "avcast_provider_manager.h"
21 
22 namespace OHOS::AVSession {
23 class AVRouterImpl : public AVRouter {
24 public:
25     AVRouterImpl();
26 
27     void Init(IAVSessionServiceListener *servicePtr) override;
28 
29     bool Release() override;
30 
31     int32_t StartDeviceLogging(int32_t fd, uint32_t maxSize) override;
32 
33     int32_t StopDeviceLogging() override;
34 
35     int32_t StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes) override;
36 
37     int32_t StopCastDiscovery() override;
38 
39     int32_t SetDiscoverable(const bool enable) override;
40 
41     int32_t OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo) override;
42 
43     int32_t OnDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param) override;
44 
45     int32_t OnDeviceOffline(const std::string& deviceId) override;
46 
47     void ReleaseCurrentCastSession() override;
48 
49     int32_t OnCastSessionCreated(const int32_t castId) override;
50 
51     int32_t OnCastServerDied(int32_t providerNumber) override;
52 
53     std::shared_ptr<IAVCastControllerProxy> GetRemoteController(const int64_t castHandle) override;
54 
55     int64_t StartCast(const OutputDeviceInfo& outputDeviceInfo,
56         std::map<std::string, std::string>& serviceNameMapState) override;
57 
58     int32_t AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo) override;
59 
60     int32_t StopCast(const int64_t castHandle, int32_t removeTimes) override;
61 
62     int32_t StopCastSession(const int64_t castHandle) override;
63 
64     int32_t RegisterCallback(int64_t castHandleconst,
65         std::shared_ptr<IAVCastSessionStateListener> callback) override;
66 
67     int32_t UnRegisterCallback(int64_t castHandleconst,
68         std::shared_ptr<IAVCastSessionStateListener> callback) override;
69 
70     int32_t SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo) override;
71 
72     int32_t GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId) override;
73 
74 protected:
75 
76 private:
77     std::recursive_mutex servicePtrLock_;
78     IAVSessionServiceListener *servicePtr_ = nullptr;
79     std::recursive_mutex providerManagerLock_;
80     std::map<int32_t, std::shared_ptr<AVCastProviderManager>> providerManagerMap_;
81     std::map<std::string, std::string> castServiceNameMapState_;
82     const std::string deviceStateConnection = "CONNECT_SUCC";
83     int32_t providerNumber_ = 0;
84     std::map<int32_t, OutputDeviceInfo> castHandleToOutputDeviceMap_;
85     bool hasSessionAlive_ = false;
86     int32_t providerNumberEnableDefault_ = 1;
87     int32_t providerNumberDisable_ = 0;
88     OutputDeviceInfo castOutputDeviceInfo_;
89     bool cacheStartDiscovery_ = false;
90     bool cacheStartDeviceLogging_ = false;
91     int32_t cacheCastDeviceCapability_ = -1;
92     std::vector<std::string> cacheDrmSchemes_;
93 };
94 } // namespace OHOS::AVSession
95 #endif // OHOS_AVROUTER_IMPL_H
96