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 int32_t sessionSize = static_cast<int32_t>(GetUsersManager().GetContainerFromAll().GetAllSessions().size());
63 if ((sessionSize == 0 || (sessionSize == 1 && CheckAncoAudio())) && !is2in1_ && state == "IDLE") {
64 SLOGI("call disable cast for cast idle");
65 checkEnableCast(false);
66 }
67 }
68 #endif
69 }
70
NotifyMigrateStop(const std::string & deviceId)71 void AVSessionService::NotifyMigrateStop(const std::string &deviceId)
72 {
73 if (migrateAVSession_ == nullptr) {
74 SLOGI("NotifyMigrateStop without migrate, create new");
75 migrateAVSession_ = std::make_shared<MigrateAVSessionServer>();
76 }
77 std::lock_guard lockGuard(sessionAndControllerLock_);
78 migrateAVSession_->StopObserveControllerChanged(deviceId);
79 }
80
81 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
SplitExtraInfo(std::string info)82 void AVSessionService::SplitExtraInfo(std::string info)
83 {
84 if (info.find("SUPPORT_MIRROR_TO_STREAM") != std::string::npos && info.find("true") != std::string::npos) {
85 isSupportMirrorToStream_ = true;
86 }
87 if (info.find("deviceId") != std::string::npos && info.find(":") != std::string::npos) {
88 std::string::size_type idBeginPos = info.find(":");
89 castDeviceId_ = info.substr(idBeginPos + beginAddPos,
90 info.length() -idBeginPos - endDecPos); // "deviceId" : "xxxx"
91 }
92 if (info.find("deviceName") != std::string::npos && info.find(":") != std::string::npos) {
93 std::string::size_type nameBeginPos = info.find(":");
94 castDeviceName_ = info.substr(nameBeginPos + beginAddPos,
95 info.length() - nameBeginPos - endDecPos); // "deviceName" : "xxxx"
96 }
97 if (info.find("deviceType") != std::string::npos && info.find(":") != std::string::npos) {
98 std::string::size_type typeBeginPos = info.find(":");
99 std::string tmpType = info.substr(typeBeginPos + typeAddPos, info.length()); // "deviceType" : xxx
100 castDeviceType_ = atoi(tmpType.c_str());
101 }
102 }
103 #endif
104
AddInnerSessionListener(SessionListener * listener)105 void AVSessionService::AddInnerSessionListener(SessionListener *listener)
106 {
107 std::lock_guard lockGuard(sessionListenersLock_);
108 innerSessionListeners_.push_back(listener);
109 }
110
RemoveInnerSessionListener(SessionListener * listener)111 void AVSessionService::RemoveInnerSessionListener(SessionListener *listener)
112 {
113 std::lock_guard lockGuard(sessionListenersLock_);
114 for (auto it = innerSessionListeners_.begin(); it != innerSessionListeners_.end();) {
115 if (*it == listener) {
116 SLOGI("RemoveInnerSessionListener");
117 innerSessionListeners_.erase(it++);
118 } else {
119 it++;
120 }
121 }
122 }
123 }