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 HW_CAST_PROVIDER_H
17 #define HW_CAST_PROVIDER_H
18 
19 #include <mutex>
20 #include "cast_session_manager.h"
21 #include "i_avcast_controller_proxy.h"
22 #include "i_cast_session.h"
23 #include "i_stream_player.h"
24 #include "i_cast_session_manager_listener.h"
25 #include "hw_cast_provider_session.h"
26 #include "avsession_info.h"
27 #include "av_cast_provider.h"
28 
29 namespace OHOS::AVSession {
30 class HwCastProvider : public AVCastProvider, public CastEngine::ICastSessionManagerListener,
31     public std::enable_shared_from_this<HwCastProvider> {
32 public:
33     HwCastProvider() = default;
34     ~HwCastProvider() override;
35 
36     void Init() override;
37     int32_t StartDeviceLogging(int32_t fd, uint32_t maxSize) override;
38     int32_t StopDeviceLogging() override;
39     bool StartDiscovery(int castCapability, std::vector<std::string> drmSchemes) override;
40     void StopDiscovery() override;
41     int32_t SetDiscoverable(const bool enable) override;
42     void Release() override;
43     int StartCastSession() override;
44     void StopCastSession(int castId) override;
45     bool AddCastDevice(int castId, DeviceInfo deviceInfo) override;
46     bool RemoveCastDevice(int castId, DeviceInfo deviceInfo) override;
47     std::shared_ptr<IAVCastControllerProxy> GetRemoteController(int castId) override;
48     bool RegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener) override;
49     bool UnRegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener) override;
50     bool RegisterCastSessionStateListener(int castId, std::shared_ptr<IAVCastSessionStateListener> listener) override;
51     bool UnRegisterCastSessionStateListener(int castId, std::shared_ptr<IAVCastSessionStateListener> listener) override;
52 
53     void OnDeviceFound(const std::vector<CastEngine::CastRemoteDevice> &deviceList) override;
54     void OnLogEvent(const int32_t eventId, const int64_t param) override;
55     void OnDeviceOffline(const std::string &deviceId) override;
56     void OnSessionCreated(const std::shared_ptr<CastEngine::ICastSession> &castSession) override;
57     void OnServiceDied() override;
58     bool SetStreamState(int32_t castId, DeviceInfo deviceInfo) override;
59     int GetMirrorCastId() override;
60     bool GetRemoteNetWorkId(int32_t castId, std::string deviceId, std::string &networkId) override;
61 
62 private:
63     static const int maxCastSessionSize = 256;
64     std::vector<bool> castFlag_ = std::vector<bool>(maxCastSessionSize, false);
65     std::map<int, std::shared_ptr<HwCastProviderSession>> hwCastProviderSessionMap_;
66     std::map<int, std::shared_ptr<IAVCastControllerProxy>> avCastControllerMap_;
67     std::vector<std::shared_ptr<IAVCastStateListener>> castStateListenerList_;
68     std::recursive_mutex mutexLock_;
69     bool isRelease_ = false;
70     int mirrorCastId = -1;
71 
72     const int32_t deviceStateConnection = 4;
73     std::map<CastEngine::CapabilityType, int32_t> castPlusTypeToAVSessionType_ = {
74         {CastEngine::CapabilityType::CAST_PLUS, ProtocolType::TYPE_CAST_PLUS_STREAM},
75         {CastEngine::CapabilityType::DLNA, ProtocolType::TYPE_DLNA},
76         {CastEngine::CapabilityType::CAST_AND_DLNA, ProtocolType::TYPE_CAST_PLUS_STREAM | ProtocolType::TYPE_DLNA}
77     };
78 };
79 } // namespace OHOS::AVSession
80 
81 #endif