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 "migrate_avsession_manager.h"
17
18 namespace OHOS::AVSession {
GetInstance()19 MigrateAVSessionManager& MigrateAVSessionManager::GetInstance()
20 {
21 static MigrateAVSessionManager migrateAVSessionManager;
22 return migrateAVSessionManager;
23 }
24
CreateLocalSessionStub(std::string scene,std::shared_ptr<MigrateAVSessionServer> server)25 void MigrateAVSessionManager::CreateLocalSessionStub(std::string scene, std::shared_ptr<MigrateAVSessionServer> server)
26 {
27 SLOGI("enter CreateLocalSessionStub");
28 if ("SuperLauncher-Dual" != scene && migrateSceneNext != scene) {
29 SLOGW("CreateLocalSessionStub error, scene: %{public}s", scene.c_str());
30 return;
31 }
32 std::lock_guard lockGuard(migrateServerMapLock_);
33 auto it = serverMap_.find(scene);
34 if (it != serverMap_.end()) {
35 SLOGW("find and return");
36 return;
37 }
38 softBusDistributedDataMgr_->Init();
39 IncSoftBusRef();
40 softBusDistributedDataMgr_->CreateServer(server);
41 serverMap_.insert({ scene, server });
42 }
43
ReleaseLocalSessionStub(std::string scene)44 void MigrateAVSessionManager::ReleaseLocalSessionStub(std::string scene)
45 {
46 SLOGI("enter ReleaseLocalSessionStub");
47 if ("SuperLauncher-Dual" != scene && migrateSceneNext != scene) {
48 SLOGW("not ReleaseLocalSessionStub: scene: %{public}s", scene.c_str());
49 return;
50 }
51 std::lock_guard lockGuard(migrateServerMapLock_);
52 auto it = serverMap_.find(scene);
53 if (it == serverMap_.end()) {
54 SLOGW("not find and return");
55 return;
56 }
57
58 softBusDistributedDataMgr_->ReleaseServer(it->second);
59 DecSoftBusRef();
60 serverMap_.erase(scene);
61 }
62
CreateRemoteSessionProxy(std::string & networkId,std::string scene,std::shared_ptr<MigrateAVSessionProxy> proxy)63 void MigrateAVSessionManager::CreateRemoteSessionProxy(std::string &networkId, std::string scene,
64 std::shared_ptr<MigrateAVSessionProxy> proxy)
65 {
66 SLOGI("enter CreateRemoteSessionProxy");
67 if (proxy == nullptr || networkId.c_str() == nullptr || networkId.empty()) {
68 SLOGW("CreateRemoteSessionProxy error, invalid params");
69 return;
70 }
71 if ("SuperLauncher-Dual" != scene && migrateSceneNext != scene) {
72 SLOGW("CreateRemoteSessionProxy error, scene: %{public}s", scene.c_str());
73 return;
74 }
75 if (proxyMap_.find(scene) != proxyMap_.end()) {
76 SLOGW("CreateRemoteSessionProxy error, %{public}s scene already exist", scene.c_str());
77 return;
78 }
79 softBusDistributedDataMgr_->Init();
80 refs_++;
81 softBusDistributedDataMgr_->CreateProxy(proxy, networkId, "AVSession");
82 proxyMap_.insert({scene, proxy});
83 }
84
ReleaseRemoteSessionProxy(std::string & networkId,std::string scene)85 void MigrateAVSessionManager::ReleaseRemoteSessionProxy(std::string &networkId, std::string scene)
86 {
87 SLOGI("enter ReleaseRemoteSessionProxy");
88 if (networkId.c_str() == nullptr || networkId.empty()) {
89 SLOGW("ReleaseRemoteSessionProxy error, invalid params");
90 return;
91 }
92 if ("SuperLauncher-Dual" != scene && migrateSceneNext != scene) {
93 SLOGW("not ReleaseRemoteSessionProxy: scene: %{public}s", scene.c_str());
94 return;
95 }
96 std::lock_guard lockGuard(migrateServerMapLock_);
97 auto it = proxyMap_.find(scene);
98 if (it == proxyMap_.end()) {
99 SLOGW("not find and return");
100 return;
101 }
102 softBusDistributedDataMgr_->ReleaseProxy(it->second, networkId);
103 DecSoftBusRef();
104 proxyMap_.erase(scene);
105 }
106
IncSoftBusRef()107 void MigrateAVSessionManager::IncSoftBusRef()
108 {
109 if (refs_ == 0) {
110 softBusDistributedDataMgr_->InitSessionServer("AVSession");
111 }
112 refs_++;
113 }
114
115 // LCOV_EXCL_START
DecSoftBusRef()116 void MigrateAVSessionManager::DecSoftBusRef()
117 {
118 refs_--;
119 if (refs_ <= 0) {
120 softBusDistributedDataMgr_->DestroySessionServer("AVSession");
121 refs_ = 0;
122 }
123 }
124 // LCOV_EXCL_STOP
125 } // namespace OHOS::AVSession