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();
34     ~HwCastProvider() override;
35 
36     int32_t 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(int64_t castHandle, DeviceInfo deviceInfo) override;
59     int64_t GetMirrorCastHandle() override;
60     bool GetRemoteNetWorkId(int32_t castId, std::string deviceId, std::string &networkId) override;
61     int32_t GetProtocolType(uint32_t castProtocolType) override;
62 
63 private:
64     static const int maxCastSessionSize = 256;
65     std::vector<bool> castFlag_ = std::vector<bool>(maxCastSessionSize, false);
66     std::map<int, std::shared_ptr<HwCastProviderSession>> hwCastProviderSessionMap_;
67     std::map<int, std::shared_ptr<IAVCastControllerProxy>> avCastControllerMap_;
68     std::vector<std::shared_ptr<IAVCastStateListener>> castStateListenerList_;
69     std::recursive_mutex mutexLock_;
70     bool isRelease_ = false;
71     int64_t mirrorCastHandle = -1;
72 };
73 } // namespace OHOS::AVSession
74 
75 #endif