1 /*
2 * Copyright (c) 2022-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 "avsession_service.h"
17
18 #include "migrate_avsession_manager.h"
19
20 namespace OHOS::AVSession {
SuperLauncher(std::string deviceId,std::string serviceName,std::string extraInfo,const std::string & state)21 void AVSessionService::SuperLauncher(std::string deviceId, std::string serviceName,
22 std::string extraInfo, const std::string& state)
23 {
24 SLOGI("SuperLauncher serviceName: %{public}s, state: %{public}s, no extraInfo for private",
25 serviceName.c_str(), state.c_str());
26
27 if (state == "IDLE" && serviceName == "SuperLauncher") {
28 MigrateAVSessionManager::GetInstance().ReleaseLocalSessionStub(serviceName);
29 if (migrateAVSession_ != nullptr) {
30 RemoveInnerSessionListener(migrateAVSession_.get());
31 }
32 } else if (state == "CONNECTING" && serviceName == "SuperLauncher") {
33 if (migrateAVSession_ == nullptr) {
34 migrateAVSession_ = std::make_shared<MigrateAVSessionServer>();
35 }
36 migrateAVSession_->Init(this);
37 MigrateAVSessionManager::GetInstance().CreateLocalSessionStub(serviceName, migrateAVSession_);
38 AddInnerSessionListener(migrateAVSession_.get());
39 }
40 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
41 if ((serviceName == "HuaweiCast" || serviceName == "HuaweiCast-Dual") &&
42 (state == "IDLE" || state == "CONNECT_SUCC")) {
43 castServiceNameMapState_[serviceName] = state;
44 isSupportMirrorToStream_ = false;
45 castDeviceId_ = "0";
46 castDeviceName_ = " ";
47 castDeviceType_ = 0;
48 std::string info;
49 std::string::size_type beginPos = 0;
50 std::string::size_type endPos = extraInfo.find(seperator);
51 while (endPos != std::string::npos) {
52 info = extraInfo.substr(beginPos, endPos - beginPos);
53 beginPos = endPos + seperator.size();
54 endPos = extraInfo.find(seperator, beginPos);
55 SplitExtraInfo(info);
56 }
57 if (beginPos != extraInfo.length()) {
58 info = extraInfo.substr(beginPos);
59 SplitExtraInfo(info);
60 }
61 NotifyMirrorToStreamCast();
62 }
63 #endif
64 }
65
NotifyMigrateStop(const std::string & deviceId)66 void AVSessionService::NotifyMigrateStop(const std::string &deviceId)
67 {
68 if (migrateAVSession_ == nullptr) {
69 SLOGI("NotifyMigrateStop without migrate, create new");
70 migrateAVSession_ = std::make_shared<MigrateAVSessionServer>();
71 }
72 std::lock_guard lockGuard(sessionAndControllerLock_);
73 migrateAVSession_->StopObserveControllerChanged(deviceId);
74 }
75
76 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
SplitExtraInfo(std::string info)77 void AVSessionService::SplitExtraInfo(std::string info)
78 {
79 if (info.find("SUPPORT_MIRROR_TO_STREAM") != std::string::npos && info.find("true") != std::string::npos) {
80 isSupportMirrorToStream_ = true;
81 }
82 if (info.find("deviceId") != std::string::npos && info.find(":") != std::string::npos) {
83 std::string::size_type idBeginPos = info.find(":");
84 castDeviceId_ = info.substr(idBeginPos + beginAddPos,
85 info.length() -idBeginPos - endDecPos); // "deviceId" : "xxxx"
86 }
87 if (info.find("deviceName") != std::string::npos && info.find(":") != std::string::npos) {
88 std::string::size_type nameBeginPos = info.find(":");
89 castDeviceName_ = info.substr(nameBeginPos + beginAddPos,
90 info.length() - nameBeginPos - endDecPos); // "deviceName" : "xxxx"
91 }
92 if (info.find("deviceType") != std::string::npos && info.find(":") != std::string::npos) {
93 std::string::size_type typeBeginPos = info.find(":");
94 std::string tmpType = info.substr(typeBeginPos + typeAddPos, info.length()); // "deviceType" : xxx
95 castDeviceType_ = atoi(tmpType.c_str());
96 }
97 }
98 #endif
99
AddInnerSessionListener(SessionListener * listener)100 void AVSessionService::AddInnerSessionListener(SessionListener *listener)
101 {
102 std::lock_guard lockGuard(sessionListenersLock_);
103 innerSessionListeners_.push_back(listener);
104 }
105
RemoveInnerSessionListener(SessionListener * listener)106 void AVSessionService::RemoveInnerSessionListener(SessionListener *listener)
107 {
108 std::lock_guard lockGuard(sessionListenersLock_);
109 for (auto it = innerSessionListeners_.begin(); it != innerSessionListeners_.end();) {
110 if (*it == listener) {
111 SLOGI("RemoveInnerSessionListener");
112 innerSessionListeners_.erase(it++);
113 } else {
114 it++;
115 }
116 }
117 }
118 }