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 #include "hw_cast_provider.h"
22 #include "avsession_event_handler.h"
23 
24 namespace OHOS::AVSession {
25 class AVRouterImpl : public AVRouter {
26 class CastSessionListener : public IAVCastSessionStateListener {
27 public:
CastSessionListener(AVRouterImpl * ptr)28     explicit CastSessionListener(AVRouterImpl *ptr)
29     {
30         ptr_ = ptr;
31     }
32 
OnCastStateChange(int32_t castState,DeviceInfo deviceInfo)33     void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo)
34     {
35         ptr_->OnCastStateChange(castState, deviceInfo);
36     }
37 
OnCastEventRecv(int32_t errorCode,std::string & errorMsg)38     void OnCastEventRecv(int32_t errorCode, std::string& errorMsg)
39     {
40         ptr_->OnCastEventRecv(errorCode, errorMsg);
41     }
42 
43     AVRouterImpl *ptr_;
44 };
45 public:
46     AVRouterImpl();
47 
48     int32_t Init(IAVSessionServiceListener *servicePtr) override;
49 
50     bool Release() override;
51 
52     int32_t StartDeviceLogging(int32_t fd, uint32_t maxSize) override;
53 
54     int32_t StopDeviceLogging() override;
55 
56     int32_t StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes) override;
57 
58     int32_t StopCastDiscovery() override;
59 
60     int32_t SetDiscoverable(const bool enable) override;
61 
62     int32_t OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo) override;
63 
64     int32_t OnDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param) override;
65 
66     int32_t OnDeviceOffline(const std::string& deviceId) override;
67 
68     void ReleaseCurrentCastSession() override;
69 
70     int32_t OnCastSessionCreated(const int32_t castId) override;
71 
72     int32_t OnCastServerDied(int32_t providerNumber) override;
73 
74     std::shared_ptr<IAVCastControllerProxy> GetRemoteController(const int64_t castHandle) override;
75 
76     int64_t StartCast(const OutputDeviceInfo& outputDeviceInfo,
77         std::map<std::string, std::string>& serviceNameMapState, std::string sessionId) override;
78 
79     int32_t AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo) override;
80 
81     int32_t StopCast(const int64_t castHandle) override;
82 
83     int32_t StopCastSession(const int64_t castHandle) override;
84 
85     int32_t RegisterCallback(int64_t castHandleconst,
86         std::shared_ptr<IAVRouterListener> callback, std::string sessionId, DeviceInfo deviceInfo) override;
87 
88     int32_t UnRegisterCallback(int64_t castHandleconst,
89         std::shared_ptr<IAVRouterListener> callback, std::string sessionId) override;
90 
91     int32_t SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo) override;
92 
93     int32_t GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId) override;
94 
95     int64_t GetMirrorCastHandle() override;
96 
97     void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo);
98 
99     void OnCastEventRecv(int32_t errorCode, std::string& errorMsg);
100 
101     void DisconnetOtherSession(std::string sessionId, DeviceInfo deviceInfo) override;
102 
103 protected:
104 
105 private:
106     std::recursive_mutex servicePtrLock_;
107     IAVSessionServiceListener *servicePtr_ = nullptr;
108     std::recursive_mutex providerManagerLock_;
109     std::map<int32_t, std::shared_ptr<AVCastProviderManager>> providerManagerMap_;
110     std::map<std::string, std::string> castServiceNameMapState_;
111     const std::string deviceStateConnection = "CONNECT_SUCC";
112     int32_t providerNumber_ = 0;
113     std::map<int64_t, CastHandleInfo> castHandleToInfoMap_;
114     std::map<std::string, std::shared_ptr<IAVRouterListener>> mirrorSessionMap_;
115     bool hasSessionAlive_ = false;
116     int32_t providerNumberEnableDefault_ = 1;
117     int32_t providerNumberDisable_ = 0;
118     bool cacheStartDiscovery_ = false;
119     bool cacheStartDeviceLogging_ = false;
120     int32_t cacheCastDeviceCapability_ = -1;
121     std::vector<std::string> cacheDrmSchemes_;
122     std::shared_ptr<CastSessionListener> castSessionListener_;
123     int32_t castConnectStateForDisconnect_ = 5;
124     int32_t castConnectStateForConnected_ = 6;
125 };
126 } // namespace OHOS::AVSession
127 #endif // OHOS_AVROUTER_IMPL_H
128