• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef LOG_TAG
17 #define LOG_TAG "AudioRunningLock"
18 #endif
19 
20 #include "util/audio_running_lock.h"
21 #include "audio_hdi_log.h"
22 #include "audio_errors.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
AudioRunningLock(const std::string & lockName)26 AudioRunningLock::AudioRunningLock(const std::string &lockName)
27 {
28     runningLock_ = PowerMgr::PowerMgrClient::GetInstance().CreateRunningLock(lockName,
29         PowerMgr::RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
30     CHECK_AND_RETURN_LOG(runningLock_ != nullptr, "create lock fail");
31 }
32 
Lock(const int32_t timeoutMs)33 int32_t AudioRunningLock::Lock(const int32_t timeoutMs)
34 {
35     CHECK_AND_RETURN_RET_LOG(runningLock_ != nullptr, -1, "lock is nullptr");
36     Trace lockTrace("AudioRunningLock::Lock");
37     std::lock_guard<std::mutex> lock(mutex_);
38     lastAppsUid_ = {};
39 
40     Trace innerLockTrace("AudioRunningLock::runningLock_->Lock");
41     AudioXCollie audioXCollie("PowerMgr::RunningLock::Lock", LOCK_TIMEOUT_SECONDS,
42         [](void *) {
43             AUDIO_ERR_LOG("PowerMgr lock timeout");
44         }, nullptr, AUDIO_XCOLLIE_FLAG_LOG);
45     WatchTimeout guard("PowerMgr Lock timeout");
46     int32_t ret = runningLock_->Lock(timeoutMs);
47     isLocked_ = true;
48     AUDIO_INFO_LOG("lock end, ret: %{public}d", ret);
49     return ret;
50 }
51 
UnLock(void)52 int32_t AudioRunningLock::UnLock(void)
53 {
54     CHECK_AND_RETURN_RET_LOG(runningLock_ != nullptr, -1, "lock is nullptr");
55     Trace unlockTrace("AudioRunningLock::UnLock");
56     std::lock_guard<std::mutex> lock(mutex_);
57     isLocked_ = false;
58     currentAppsUid_ = {};
59     lastAppsUid_ = {};
60 
61     Trace innerUpdateWorkSourceTrace("AudioRunningLock::runningLock_->UpdateWorkSource");
62     int32_t ret = runningLock_->UpdateWorkSource({});
63     AUDIO_INFO_LOG("update work source end, ret: %{public}d", ret);
64     Trace innerUnlockTrace("AudioRunningLock::runningLock_->UnLock");
65     ret = runningLock_->UnLock();
66     AUDIO_INFO_LOG("unlock end, ret: %{public}d", ret);
67     return ret;
68 }
69 
UpdateAppsUidToPowerMgr(void)70 int32_t AudioRunningLock::UpdateAppsUidToPowerMgr(void)
71 {
72     CHECK_AND_RETURN_RET_LOG(runningLock_ != nullptr, -1, "lock is nullptr");
73     Trace trace("AudioRunningLock::UpdateAppsUidToPowerMgr");
74     std::lock_guard<std::mutex> lock(mutex_);
75     if ((!isLocked_) || currentAppsUid_ == lastAppsUid_) {
76         return SUCCESS;
77     }
78     lastAppsUid_ = currentAppsUid_;
79     std::vector<int32_t> appsUid;
80     appsUid.insert(appsUid.end(), currentAppsUid_.begin(), currentAppsUid_.end());
81 
82     std::string appsUidInfo;
83     for (auto uid : appsUid) {
84         appsUidInfo += (std::to_string(uid) + ',');
85     }
86 
87     Trace innerUpdateWorkSourceTrace("AudioRunningLock::runningLock_->UpdateWorkSource");
88     int32_t ret = runningLock_->UpdateWorkSource(appsUid);
89     AUDIO_INFO_LOG("uidInfo: %{public}s, size: %{public}zu, ret: %{public}d", appsUidInfo.c_str(),
90         appsUid.size(), ret);
91     return ret;
92 }
93 
94 } // namespace AudioStandard
95 } // namespace OHOS
96