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" != 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" != 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 
IncSoftBusRef()63 void MigrateAVSessionManager::IncSoftBusRef()
64 {
65     if (refs_ == 0) {
66         softBusDistributedDataMgr_->InitSessionServer("AVSession");
67     }
68     refs_++;
69 }
70 
71 // LCOV_EXCL_START
DecSoftBusRef()72 void MigrateAVSessionManager::DecSoftBusRef()
73 {
74     refs_--;
75     if (refs_ <= 0) {
76         softBusDistributedDataMgr_->DestroySessionServer("AVSession");
77         refs_ = 0;
78     }
79 }
80 // LCOV_EXCL_STOP
81 } // namespace OHOS::AVSession