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 #include "avsession_radar.h"
23
24 using namespace OHOS::CastEngine::CastEngineClient;
25 using namespace OHOS::CastEngine;
26
27 namespace OHOS::AVSession {
28 const uint32_t UNTRUSTED_DEVICE = 0;
29 const uint32_t TRUSTED_DEVICE = 1;
30
HwCastProvider()31 HwCastProvider::HwCastProvider()
32 {
33 SLOGD("pre construct the HwCastProvider");
34 std::lock_guard lockGuard(mutexLock_);
35 SLOGI("construct the HwCastProvider");
36 }
37
~HwCastProvider()38 HwCastProvider::~HwCastProvider()
39 {
40 SLOGD("pre destruct the HwCastProvider");
41 std::lock_guard lockGuard(mutexLock_);
42 SLOGI("destruct the HwCastProvider");
43 Release();
44 }
45
Init()46 void HwCastProvider::Init()
47 {
48 SLOGD("pre init the HwCastProvider");
49 std::lock_guard lockGuard(mutexLock_);
50 SLOGI("Init the HwCastProvider");
51 CastSessionManager::GetInstance().RegisterListener(shared_from_this());
52 }
53
StartDeviceLogging(int32_t fd,uint32_t maxSize)54 int32_t HwCastProvider::StartDeviceLogging(int32_t fd, uint32_t maxSize)
55 {
56 SLOGI("start StartDeviceLogging, fd is %{public}d and maxSize is %{public}d", fd, maxSize);
57 return CastSessionManager::GetInstance().StartDeviceLogging(fd, maxSize);
58 }
59
StopDeviceLogging()60 int32_t HwCastProvider::StopDeviceLogging()
61 {
62 SLOGI("StopDeviceLogging");
63 return CastSessionManager::GetInstance().StartDeviceLogging(-1, 0);
64 }
65
StartDiscovery(int castCapability,std::vector<std::string> drmSchemes)66 bool HwCastProvider::StartDiscovery(int castCapability, std::vector<std::string> drmSchemes)
67 {
68 SLOGI("start discovery and the castCapability is %{public}d", castCapability);
69 AVSessionRadarInfo info("HwCastProvider::StartDiscovery");
70 AVSessionRadar::GetInstance().StartCastDiscoveryBegin(info);
71 auto ret = CastSessionManager::GetInstance().StartDiscovery(castCapability, drmSchemes);
72 if (ret != 0) {
73 info.errorCode_ = ret;
74 AVSessionRadar::GetInstance().FailToStartCastDiscovery(info);
75 } else {
76 AVSessionRadar::GetInstance().StartCastDiscoveryEnd(info);
77 }
78 return ret;
79 }
80
StopDiscovery()81 void HwCastProvider::StopDiscovery()
82 {
83 SLOGI("stop discovery");
84 AVSessionRadarInfo info("HwCastProvider::StopDiscovery");
85 AVSessionRadar::GetInstance().StopCastDiscoveryBegin(info);
86 auto ret = CastSessionManager::GetInstance().StopDiscovery();
87 if (ret != 0) {
88 info.errorCode_ = ret;
89 AVSessionRadar::GetInstance().FailToStopCastDiscovery(info);
90 } else {
91 AVSessionRadar::GetInstance().StopCastDiscoveryEnd(info);
92 }
93 }
94
SetDiscoverable(const bool enable)95 int32_t HwCastProvider::SetDiscoverable(const bool enable)
96 {
97 SLOGI("SetDiscoverable in %{public}d", static_cast<int32_t>(enable));
98 return CastSessionManager::GetInstance().SetDiscoverable(enable);
99 }
100
Release()101 void HwCastProvider::Release()
102 {
103 SLOGI("cast provider release");
104 {
105 std::lock_guard lockGuard(mutexLock_);
106 hwCastProviderSessionMap_.clear();
107 avCastControllerMap_.clear();
108 castStateListenerList_.clear();
109 castFlag_.clear();
110 }
111 if (!isRelease_) {
112 SLOGI("release in with check pass");
113 isRelease_ = true;
114 } else {
115 SLOGW("already in release, check return");
116 return;
117 }
118 CastSessionManager::GetInstance().UnregisterListener();
119 SLOGD("provider release done");
120 }
121
StartCastSession()122 int HwCastProvider::StartCastSession()
123 {
124 SLOGI("StartCastSession begin");
125 CastSessionProperty property = {CastEngine::ProtocolType::CAST_PLUS_STREAM, CastEngine::EndType::CAST_SOURCE};
126 std::shared_ptr<ICastSession> castSession = nullptr;
127 int ret = CastSessionManager::GetInstance().CreateCastSession(property, castSession);
128 if (ret != AVSESSION_SUCCESS) {
129 AVSessionRadarInfo info("HwCastProvider::StartCastSession");
130 info.errorCode_ = ret;
131 AVSessionRadar::GetInstance().FailToStartCast(info);
132 }
133 int castId;
134 {
135 SLOGI("StartCastSession pre check lock");
136 std::lock_guard lockGuard(mutexLock_);
137 SLOGI("StartCastSession check lock");
138 std::vector<bool>::iterator iter = find(castFlag_.begin(), castFlag_.end(), false);
139 if (iter == castFlag_.end()) {
140 SLOGE("StartCastSession faileded");
141 return AVSESSION_ERROR;
142 }
143 *iter = true;
144 castId = iter - castFlag_.begin();
145 auto hwCastProviderSession = std::make_shared<HwCastProviderSession>(castSession);
146 if (hwCastProviderSession) {
147 hwCastProviderSession->Init();
148 }
149 hwCastProviderSessionMap_[castId] = hwCastProviderSession;
150 }
151 SLOGI("StartCastSession successed and return the castId is %{public}d", castId);
152
153 return castId;
154 }
StopCastSession(int castId)155 void HwCastProvider::StopCastSession(int castId)
156 {
157 SLOGI("StopCastSession begin");
158 std::lock_guard lockGuard(mutexLock_);
159 SLOGI("StopCastSession check lock");
160
161 auto hwCastStreamPlayer = avCastControllerMap_[castId];
162 if (hwCastStreamPlayer) {
163 hwCastStreamPlayer->Release();
164 }
165
166 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
167 SLOGE("no need to release castSession for castId %{public}d is not exit in hwCastProviderSessionMap_", castId);
168 return;
169 }
170 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
171 if (hwCastProviderSession && castId != mirrorCastId) {
172 hwCastProviderSession->Release();
173 }
174 if (castId != mirrorCastId) {
175 hwCastProviderSessionMap_.erase(castId);
176 castFlag_[castId] = false;
177 }
178 avCastControllerMap_.erase(castId);
179 }
180
AddCastDevice(int castId,DeviceInfo deviceInfo)181 bool HwCastProvider::AddCastDevice(int castId, DeviceInfo deviceInfo)
182 {
183 SLOGI("AddCastDevice with config castSession and corresonding castId is %{public}d", castId);
184 std::lock_guard lockGuard(mutexLock_);
185 SLOGI("add device check lock done");
186
187 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
188 SLOGE("the castId corresonding to castSession is not exist");
189 return false;
190 }
191 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
192 if (!hwCastProviderSession) {
193 SLOGE("the castId corresonding to castSession is nullptr");
194 return false;
195 }
196
197 return hwCastProviderSession->AddDevice(deviceInfo.deviceId_);
198 }
199
RemoveCastDevice(int castId,DeviceInfo deviceInfo)200 bool HwCastProvider::RemoveCastDevice(int castId, DeviceInfo deviceInfo)
201 {
202 SLOGI("RemoveCastDevice with config castSession and corresonding castId is %{public}d", castId);
203 std::lock_guard lockGuard(mutexLock_);
204 SLOGI("remove device check lock");
205 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
206 SLOGE("the castId corresonding to castSession is not exist");
207 return false;
208 }
209 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
210 if (!hwCastProviderSession) {
211 SLOGE("the castId corresonding to castSession is nullptr");
212 return false;
213 }
214
215 return hwCastProviderSession->RemoveDevice(deviceInfo.deviceId_);
216 }
217
RegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)218 bool HwCastProvider::RegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)
219 {
220 SLOGI("RegisterCastStateListener in");
221 std::lock_guard lockGuard(mutexLock_);
222 SLOGI("RegisterCastStateListener in pass lock");
223 if (listener == nullptr) {
224 SLOGE("RegisterCastStateListener the listener is nullptr");
225 return false;
226 }
227 if (find(castStateListenerList_.begin(), castStateListenerList_.end(), listener) != castStateListenerList_.end()) {
228 SLOGE("RegisterCastStateListener the listener is already be registered");
229 return false;
230 }
231 SLOGI("RegisterCastStateListener successed, and save it in the castStateListenerList_");
232 castStateListenerList_.emplace_back(listener);
233
234 return true;
235 }
236
UnRegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)237 bool HwCastProvider::UnRegisterCastStateListener(std::shared_ptr<IAVCastStateListener> listener)
238 {
239 SLOGI("UnRegisterCastStateListener in");
240 std::lock_guard lockGuard(mutexLock_);
241 SLOGI("UnRegisterCastStateListener in pass lock");
242 if (listener == nullptr) {
243 SLOGE("UnRegisterCastStateListener the listener is nullptr");
244 return false;
245 }
246 for (auto iter = castStateListenerList_.begin(); iter != castStateListenerList_.end();) {
247 if (*iter == listener) {
248 castStateListenerList_.erase(iter);
249 SLOGI("UnRegisterCastStateListener successed, and erase it from castStateListenerList_");
250 return true;
251 } else {
252 ++iter;
253 }
254 }
255 SLOGE("listener is not found in castStateListenerList_, so UnRegisterCastStateListener failed");
256
257 return false;
258 }
259
GetRemoteController(int castId)260 std::shared_ptr<IAVCastControllerProxy> HwCastProvider::GetRemoteController(int castId)
261 {
262 SLOGI("get remote controller with castId %{public}d", static_cast<int32_t>(castId));
263 std::lock_guard lockGuard(mutexLock_);
264 SLOGI("get remote controller check lock with castId %{public}d", static_cast<int32_t>(castId));
265 if (avCastControllerMap_.find(castId) != avCastControllerMap_.end()) {
266 SLOGI("the castId corresonding to streamPlayer is already exist");
267 return avCastControllerMap_[castId];
268 }
269 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
270 SLOGE("No castSession corresonding to castId exists");
271 return nullptr;
272 }
273 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
274 if (hwCastProviderSession == nullptr) {
275 SLOGE("castSession corresonding to castId is nullptr");
276 return nullptr;
277 }
278 std::shared_ptr<IStreamPlayer> streamPlayer = hwCastProviderSession->CreateStreamPlayer();
279 std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(streamPlayer);
280 if (!hwCastStreamPlayer) {
281 SLOGE("the created hwCastStreamPlayer is nullptr");
282 return nullptr;
283 }
284 hwCastStreamPlayer->Init();
285 avCastControllerMap_[castId] = hwCastStreamPlayer;
286 SLOGI("Create streamPlayer finished");
287 return hwCastStreamPlayer;
288 }
289
SetStreamState(int32_t castId,DeviceInfo deviceInfo)290 bool HwCastProvider::SetStreamState(int32_t castId, DeviceInfo deviceInfo)
291 {
292 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
293 SLOGE("SetStreamState failed for the castSession corresponding to castId is not exit");
294 return false;
295 }
296 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
297 if (hwCastProviderSession == nullptr) {
298 SLOGE("SetStreamState failed for the hwCastProviderSession is nullptr");
299 return false;
300 }
301 mirrorCastId = castId;
302 SLOGI("mirrorCastId is %{public}d", mirrorCastId);
303 return hwCastProviderSession->SetStreamState(deviceInfo);
304 }
305
GetRemoteNetWorkId(int32_t castId,std::string deviceId,std::string & networkId)306 bool HwCastProvider::GetRemoteNetWorkId(int32_t castId, std::string deviceId, std::string &networkId)
307 {
308 SLOGI("enter GetRemoteNetWorkId");
309 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
310 SLOGE("GetRemoteNetWorkId failed for the castSession corresponding to castId is not exit");
311 return false;
312 }
313 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
314 if (hwCastProviderSession == nullptr) {
315 SLOGE("GetRemoteNetWorkId failed for the hwCastProviderSession is nullptr");
316 return false;
317 }
318 return hwCastProviderSession->GetRemoteNetWorkId(deviceId, networkId);
319 }
320
GetMirrorCastId()321 int HwCastProvider::GetMirrorCastId()
322 {
323 return mirrorCastId;
324 }
325
RegisterCastSessionStateListener(int castId,std::shared_ptr<IAVCastSessionStateListener> listener)326 bool HwCastProvider::RegisterCastSessionStateListener(int castId,
327 std::shared_ptr<IAVCastSessionStateListener> listener)
328 {
329 SLOGD("RegisterCastSessionStateListener for castId %{public}d", castId);
330 if (listener == nullptr) {
331 SLOGE("RegisterCastSessionStateListener failed for the listener is nullptr");
332 return false;
333 }
334 std::lock_guard lockGuard(mutexLock_);
335 SLOGI("register castsession state listener check lock with castId %{public}d", static_cast<int32_t>(castId));
336 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
337 SLOGE("RegisterCastSessionStateListener failed for the castSession corresponding to castId is not exit");
338 return false;
339 }
340 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
341 if (hwCastProviderSession == nullptr) {
342 SLOGE("RegisterCastSessionStateListener failed for the hwCastProviderSession is nullptr");
343 return false;
344 }
345
346 return hwCastProviderSession->RegisterCastSessionStateListener(listener);
347 }
348
UnRegisterCastSessionStateListener(int castId,std::shared_ptr<IAVCastSessionStateListener> listener)349 bool HwCastProvider::UnRegisterCastSessionStateListener(int castId,
350 std::shared_ptr<IAVCastSessionStateListener> listener)
351 {
352 if (listener == nullptr) {
353 SLOGE("UnRegisterCastSessionStateListener failed for the listener is nullptr");
354 return false;
355 }
356 std::lock_guard lockGuard(mutexLock_);
357 SLOGI("unregister castsession state listener check lock with castId %{public}d", static_cast<int32_t>(castId));
358 if (hwCastProviderSessionMap_.find(castId) == hwCastProviderSessionMap_.end()) {
359 SLOGE("UnRegisterCastSessionStateListener failed for the castSession corresponding to castId is not exit");
360 return false;
361 }
362 auto hwCastProviderSession = hwCastProviderSessionMap_[castId];
363 if (hwCastProviderSession == nullptr) {
364 SLOGE("UnRegisterCastSessionStateListener failed for the hwCastProviderSession is nullptr");
365 return false;
366 }
367
368 return hwCastProviderSession->UnRegisterCastSessionStateListener(listener);
369 }
370
371
OnDeviceFound(const std::vector<CastRemoteDevice> & deviceList)372 void HwCastProvider::OnDeviceFound(const std::vector<CastRemoteDevice> &deviceList)
373 {
374 std::vector<DeviceInfo> deviceInfoList;
375 if (deviceList.empty()) {
376 SLOGW("recv empty deviceList, return");
377 return;
378 }
379 SLOGI("get deviceList size %{public}zu", deviceList.size());
380 for (CastRemoteDevice castRemoteDevice : deviceList) {
381 SLOGI("get devices with deviceName %{public}s", castRemoteDevice.deviceName.c_str());
382 DeviceInfo deviceInfo;
383 deviceInfo.castCategory_ = AVCastCategory::CATEGORY_REMOTE;
384 deviceInfo.deviceId_ = castRemoteDevice.deviceId;
385 deviceInfo.deviceName_ = castRemoteDevice.deviceName;
386 deviceInfo.deviceType_ = static_cast<int>(castRemoteDevice.deviceType);
387 deviceInfo.ipAddress_ = castRemoteDevice.ipAddress;
388 deviceInfo.networkId_ = castRemoteDevice.networkId;
389 deviceInfo.manufacturer_ = castRemoteDevice.dlnaDeviceManufacturerStr;
390 deviceInfo.modelName_ = castRemoteDevice.dlnaDeviceModelNameStr;
391 deviceInfo.supportedProtocols_ = castPlusTypeToAVSessionType_ [castRemoteDevice.capability];
392 deviceInfo.authenticationStatus_ = static_cast<int>(castRemoteDevice.subDeviceType) == 0 ?
393 TRUSTED_DEVICE : UNTRUSTED_DEVICE;
394 deviceInfo.supportedDrmCapabilities_ = castRemoteDevice.drmCapabilities;
395 deviceInfo.isLegacy_ = castRemoteDevice.isLeagacy;
396 deviceInfo.mediumTypes_ = static_cast<int32_t>(castRemoteDevice.mediumTypes);
397 deviceInfoList.emplace_back(deviceInfo);
398 }
399 for (auto listener : castStateListenerList_) {
400 if (listener != nullptr) {
401 SLOGI("trigger the OnDeviceAvailable for registered listeners");
402 listener->OnDeviceAvailable(deviceInfoList);
403 }
404 }
405 }
406
OnLogEvent(const int32_t eventId,const int64_t param)407 void HwCastProvider::OnLogEvent(const int32_t eventId, const int64_t param)
408 {
409 SLOGI("eventId is %{public}d, param is %{public}ld", eventId, param);
410 std::lock_guard lockGuard(mutexLock_);
411 for (auto listener : castStateListenerList_) {
412 if (listener != nullptr) {
413 SLOGI("trigger the OnDeviceLogEvent for registered listeners");
414 if (eventId == DeviceLogEventCode::DEVICE_LOG_FULL) {
415 listener->OnDeviceLogEvent(DeviceLogEventCode::DEVICE_LOG_FULL, param);
416 } else {
417 listener->OnDeviceLogEvent(DeviceLogEventCode::DEVICE_LOG_EXCEPTION, param);
418 }
419 }
420 }
421 }
422
OnDeviceOffline(const std::string & deviceId)423 void HwCastProvider::OnDeviceOffline(const std::string& deviceId)
424 {
425 SLOGI("Received on device offline event");
426 for (auto listener : castStateListenerList_) {
427 if (listener != nullptr) {
428 SLOGI("trigger the OnDeviceOffline for registered listeners");
429 listener->OnDeviceOffline(deviceId);
430 }
431 }
432 }
433
OnSessionCreated(const std::shared_ptr<CastEngine::ICastSession> & castSession)434 void HwCastProvider::OnSessionCreated(const std::shared_ptr<CastEngine::ICastSession> &castSession)
435 {
436 SLOGI("Cast provider received session create event");
437 std::thread([this, castSession]() {
438 SLOGI("Cast pvd received session create event and create task thread");
439 for (auto listener : castStateListenerList_) {
440 listener->OnSessionNeedDestroy();
441 SLOGI("Cast pvd received session create event and session destroy check done");
442 }
443 int32_t castId;
444 {
445 std::lock_guard lockGuard(mutexLock_);
446 std::vector<bool>::iterator iter = find(castFlag_.begin(), castFlag_.end(), false);
447 if (iter == castFlag_.end()) {
448 SLOGE("Do not trigger callback due to the castFlag_ used up.");
449 return;
450 }
451 *iter = true;
452 castId = iter - castFlag_.begin();
453 SLOGI("Cast task thread to find flag");
454 }
455 auto hwCastProviderSession = std::make_shared<HwCastProviderSession>(castSession);
456 hwCastProviderSession->Init();
457 {
458 std::lock_guard lockGuard(mutexLock_);
459 hwCastProviderSessionMap_[castId] = hwCastProviderSession;
460 SLOGI("Cast task thread to create player");
461 std::shared_ptr<IStreamPlayer> streamPlayer = hwCastProviderSession->CreateStreamPlayer();
462 std::shared_ptr<HwCastStreamPlayer> hwCastStreamPlayer = std::make_shared<HwCastStreamPlayer>(streamPlayer);
463 hwCastStreamPlayer->Init();
464 avCastControllerMap_[castId] = hwCastStreamPlayer;
465 }
466 SLOGI("Create streamPlayer finished %{public}d", castId);
467 for (auto listener : castStateListenerList_) {
468 listener->OnSessionCreated(castId);
469 }
470 SLOGI("do session create notify finished %{public}d", castId);
471 }).detach();
472 }
473
OnServiceDied()474 void HwCastProvider::OnServiceDied()
475 {
476 for (auto listener : castStateListenerList_) {
477 if (listener != nullptr) {
478 SLOGI("trigger the OnServiceDied for registered listeners");
479 listener->OnCastServerDied();
480 }
481 }
482 }
483 } // namespace OHOS::AVSession
484