• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hw_cast_provider.h"
17 #include <thread>
18 #include "cast_session_manager.h"
19 #include "hw_cast_stream_player.h"
20 #include "avsession_log.h"
21 #include "avsession_errors.h"
22 
23 using namespace OHOS::CastEngine::CastEngineClient;
24 using namespace OHOS::CastEngine;
25 
26 namespace OHOS::AVSession {
~HwCastProvider()27 HwCastProvider::~HwCastProvider()
28 {
29     SLOGI("destruct the HwCastProvider");
30     Release();
31 }
32 
Init()33 void HwCastProvider::Init()
34 {
35     SLOGI("Init the HwCastProvider");
36     CastSessionManager::GetInstance().RegisterListener(shared_from_this());
37 }
38 
StartDiscovery(int castCapability)39 bool HwCastProvider::StartDiscovery(int castCapability)
40 {
41     SLOGI("start discovery and the castCapability is %{public}d", castCapability);
42     return CastSessionManager::GetInstance().StartDiscovery(castCapability);
43 }
44 
StopDiscovery()45 void HwCastProvider::StopDiscovery()
46 {
47     SLOGI("stop discovery");
48     CastSessionManager::GetInstance().StopDiscovery();
49 }
50 
SetDiscoverable(const bool enable)51 int32_t HwCastProvider::SetDiscoverable(const bool enable)
52 {
53     return CastSessionManager::GetInstance().SetDiscoverable(enable);
54 }
55 
Release()56 void HwCastProvider::Release()
57 {
58     {
59         std::lock_guard lockGuard(mutexLock_);
60         hwCastProviderSessionMap_.clear();
61         avCastControllerMap_.clear();
62         castStateListenerList_.clear();
63         castFlag_.clear();
64     }
65     CastSessionManager::GetInstance().UnregisterListener();
66     CastSessionManager::GetInstance().Release();
67 }
68 
StartCastSession()69 int HwCastProvider::StartCastSession()
70 {
71     SLOGI("StartCastSession begin");
72     CastSessionProperty property = {CastEngine::ProtocolType::CAST_PLUS_STREAM, CastEngine::EndType::CAST_SOURCE};
73     std::shared_ptr<ICastSession> castSession = nullptr;
74     CastSessionManager::GetInstance().CreateCastSession(property, castSession);
75     int castId;
76     {
77         std::lock_guard lockGuard(mutexLock_);
78         std::vector<bool>::iterator iter = find(castFlag_.begin(), castFlag_.end(), false);
79         if (iter == castFlag_.end()) {
80             SLOGE("StartCastSession faileded");
81             return AVSESSION_ERROR;
82         }
83         *iter = true;
84         castId = iter - castFlag_.begin();
85         auto hwCastProviderSession = std::make_shared<HwCastProviderSession>(castSession);
86         if (hwCastProviderSession) {
87             hwCastProviderSession->Init();
88         }
89         hwCastProviderSessionMap_[castId] = hwCastProviderSession;
90     }
91     SLOGI("StartCastSession successed and return the castId is %{public}d", castId);
92 
93     return castId;
94 }
StopCastSession(int castId)95 void HwCastProvider::StopCastSession(int castId)
96 {
97     SLOGI("StopCastSession begin");
98     std::lock_guard lockGuard(mutexLock_);
99 
100     auto hwCastStreamPlayer = avCastControllerMap_[castId];
101     if (hwCastStreamPlayer) {
102         hwCastStreamPlayer->Release();
103     }
104 
105     if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
106         SLOGE("no need to release castSession for castId %{public}d is not exit in hwCastProviderSessionMap_", castId);
107         return;
108     }
109     auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
110     if (hwCastProviderSession) {
111         hwCastProviderSession->Release();
112     }
113     hwCastProviderSessionMap_.erase(castId);
114     castFlag_[castId] = false;
115     avCastControllerMap_.erase(castId);
116 }
117 
AddCastDevice(int castId,DeviceInfo deviceInfo)118 bool HwCastProvider::AddCastDevice(int castId, DeviceInfo deviceInfo)
119 {
120     SLOGI("AddCastDevice with config castSession and corresonding castId is %{public}d", castId);
121     std::lock_guard lockGuard(mutexLock_);
122     if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
123         SLOGE("the castId corresonding to castSession is not exist");
124         return false;
125     }
126     auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
127     if (!hwCastProviderSession) {
128         SLOGE("the castId corresonding to castSession is nullptr");
129         return false;
130     }
131 
132     return hwCastProviderSession->AddDevice(deviceInfo.deviceId_);
133 }
134 
RemoveCastDevice(int castId,DeviceInfo deviceInfo)135 bool HwCastProvider::RemoveCastDevice(int castId, DeviceInfo deviceInfo)
136 {
137     SLOGI("RemoveCastDevice with config castSession and corresonding castId is %{public}d", castId);
138     std::lock_guard lockGuard(mutexLock_);
139     if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
140         SLOGE("the castId corresonding to castSession is not exist");
141         return false;
142     }
143     auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
144     if (!hwCastProviderSession) {
145         SLOGE("the castId corresonding to castSession is nullptr");
146         return false;
147     }
148 
149     return hwCastProviderSession->RemoveDevice(deviceInfo.deviceId_);
150 }
151 
RegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)152 bool HwCastProvider::RegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)
153 {
154     std::lock_guard lockGuard(mutexLock_);
155     if (listener == nullptr) {
156         SLOGE("RegisterCastStateListener the listener is nullptr");
157         return false;
158     }
159     if (find(castStateListenerList_.begin(), castStateListenerList_.end(), listener) != castStateListenerList_.end()) {
160         SLOGE("RegisterCastStateListener the listener is already be registered");
161         return false;
162     }
163     SLOGI("RegisterCastStateListener successed, and save it in the castStateListenerList_");
164     castStateListenerList_.emplace_back(listener);
165 
166     return true;
167 }
168 
UnRegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)169 bool HwCastProvider::UnRegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)
170 {
171     std::lock_guard lockGuard(mutexLock_);
172     if (listener == nullptr) {
173         SLOGE("UnRegisterCastStateListener the listener is nullptr");
174         return false;
175     }
176     for (auto iter = castStateListenerList_.begin(); iter != castStateListenerList_.end();) {
177         if (*iter == listener) {
178             castStateListenerList_.erase(iter);
179             SLOGI("UnRegisterCastStateListener successed, and erase it from castStateListenerList_");
180             return true;
181         } else {
182             ++iter;
183         }
184     }
185     SLOGE("listener is not found in castStateListenerList_, so UnRegisterCastStateListener failed");
186 
187     return false;
188 }
189 
GetRemoteController(int castId)190 std::shared_ptr<IAVCastControllerProxy> HwCastProvider::GetRemoteController(int castId)
191 {
192     SLOGI("Provider get remote controller");
193     std::lock_guard lockGuard(mutexLock_);
194     if (avCastControllerMap_.find(castId) != avCastControllerMap_.end()) {
195         SLOGI("the castId corresonding to streamPlayer is already exist");
196         return avCastControllerMap_[castId];
197     }
198     if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
199         SLOGE("No castSession corresonding to castId exists");
200         return nullptr;
201     }
202     auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
203     if (hwCastProviderSession == nullptr) {
204         SLOGE("castSession corresonding to castId is nullptr");
205         return nullptr;
206     }
207     std::shared_ptr<IStreamPlayer> streamPlayer = hwCastProviderSession->CreateStreamPlayer();
208     std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(streamPlayer);
209     if (!hwCastStreamPlayer) {
210         SLOGE("the created hwCastStreamPlayer is nullptr");
211         return nullptr;
212     }
213     hwCastStreamPlayer->Init();
214     avCastControllerMap_[castId] = hwCastStreamPlayer;
215     SLOGI("Create streamPlayer finished");
216     return hwCastStreamPlayer;
217 }
218 
RegisterCastSessionStateListener(int castId,std::shared_ptr<IAVCastSessionStateListener> listener)219 bool HwCastProvider::RegisterCastSessionStateListener(int castId,
220     std::shared_ptr<IAVCastSessionStateListener> listener)
221 {
222     SLOGD("RegisterCastSessionStateListener for castId %{public}d", castId);
223     if (listener == nullptr) {
224         SLOGE("RegisterCastSessionStateListener failed for the listener is nullptr");
225         return false;
226     }
227     std::lock_guard lockGuard(mutexLock_);
228     if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
229         SLOGE("RegisterCastSessionStateListener failed for the castSession corresponding to castId is not exit");
230         return false;
231     }
232     auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
233     if (hwCastProviderSession == nullptr) {
234         SLOGE("RegisterCastSessionStateListener failed for the hwCastProviderSession is nullptr");
235         return false;
236     }
237 
238     return hwCastProviderSession->RegisterCastSessionStateListener(listener);
239 }
240 
UnRegisterCastSessionStateListener(int castId,std::shared_ptr<IAVCastSessionStateListener> listener)241 bool HwCastProvider::UnRegisterCastSessionStateListener(int castId,
242     std::shared_ptr<IAVCastSessionStateListener> listener)
243 {
244     if (listener == nullptr) {
245         SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr");
246         return false;
247     }
248     std::lock_guard lockGuard(mutexLock_);
249     if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
250         SLOGE("UnRegisterCastSessionStateListener failed for the castSession corresponding to castId is not exit");
251         return false;
252     }
253     auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
254     if (hwCastProviderSession == nullptr) {
255         SLOGE("UnRegisterCastSessionStateListener failed for the hwCastProviderSession is nullptr");
256         return false;
257     }
258 
259     return hwCastProviderSession->UnRegisterCastSessionStateListener(listener);
260 }
261 
262 
OnDeviceFound(const std::vector<CastRemoteDevice> & deviceList)263 void HwCastProvider::OnDeviceFound(const std::vector<CastRemoteDevice> &deviceList)
264 {
265     std::vector<DeviceInfo> deviceInfoList;
266     for (CastRemoteDevice castRemoteDevice : deviceList) {
267         if (castRemoteDevice.subDeviceType == CastEngine::SubDeviceType::SUB_DEVICE_MATEBOOK_PAD) {
268             SLOGW("Found untrusted devices.");
269             continue;
270         }
271         DeviceInfo deviceInfo;
272         deviceInfo.castCategory_ = AVCastCategory::CATEGORY_REMOTE;
273         deviceInfo.deviceId_ = castRemoteDevice.deviceId;
274         deviceInfo.deviceName_ = castRemoteDevice.deviceName;
275         deviceInfo.deviceType_ = static_cast<int>(castRemoteDevice.deviceType);
276         deviceInfo.ipAddress_ = castRemoteDevice.ipAddress;
277         deviceInfoList.emplace_back(deviceInfo);
278     }
279     for (auto listener : castStateListenerList_) {
280         if (listener != nullptr) {
281             SLOGI("trigger the OnDeviceAvailable for registered listeners");
282             listener->OnDeviceAvailable(deviceInfoList);
283         }
284     }
285 }
286 
OnDeviceOffline(const std::string & deviceId)287 void HwCastProvider::OnDeviceOffline(const std::string& deviceId)
288 {
289     SLOGI("Received on device offline event");
290     for (auto listener : castStateListenerList_) {
291         if (listener != nullptr) {
292             SLOGI("trigger the OnDeviceOffline for registered listeners");
293             listener->OnDeviceOffline(deviceId);
294         }
295     }
296 }
297 
OnSessionCreated(const std::shared_ptr<CastEngine::ICastSession> & castSession)298 void HwCastProvider::OnSessionCreated(const std::shared_ptr<CastEngine::ICastSession> &castSession)
299 {
300     std::thread([this, castSession]() {
301         SLOGI("Cast provider received session create event");
302         for (auto listener : castStateListenerList_) {
303             listener->OnSessionNeedDestroy();
304         }
305         int32_t castId;
306         {
307             std::lock_guard lockGuard(mutexLock_);
308             std::vector<bool>::iterator iter = find(castFlag_.begin(), castFlag_.end(), false);
309             if (iter == castFlag_.end()) {
310                 SLOGE("Do not trigger callback due to the castFlag_ used up.");
311                 return;
312             }
313             *iter = true;
314             castId = iter - castFlag_.begin();
315         }
316         auto hwCastProviderSession = std::make_shared<HwCastProviderSession>(castSession);
317         if (hwCastProviderSession) {
318             hwCastProviderSession->Init();
319         }
320         {
321             std::lock_guard lockGuard(mutexLock_);
322             hwCastProviderSessionMap_[castId] = hwCastProviderSession;
323             std::shared_ptr<IStreamPlayer> streamPlayer = hwCastProviderSession->CreateStreamPlayer();
324             std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(streamPlayer);
325             if (!hwCastStreamPlayer) {
326                 SLOGE("the created hwCastStreamPlayer is nullptr");
327                 return;
328             }
329             hwCastStreamPlayer->Init();
330             avCastControllerMap_[castId] = hwCastStreamPlayer;
331         }
332         SLOGI("Create streamPlayer finished %{public}d", castId);
333         for (auto listener : castStateListenerList_) {
334             listener->OnSessionCreated(castId);
335         }
336     }).detach();
337 }
338 
OnServiceDied()339 void HwCastProvider::OnServiceDied()
340 {
341     for (auto listener : castStateListenerList_) {
342         if (listener != nullptr) {
343             SLOGI("trigger the OnServiceDied for registered listeners");
344             listener->OnCastServerDied();
345         }
346     }
347 }
348 } // namespace OHOS::AVSession
349