• 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     AUDIO_INFO_LOG("in");
36     CHECK_AND_RETURN_RET_LOG(runningLock_ != nullptr, -1, "lock is nullptr");
37     Trace lockTrace("AudioRunningLock::Lock");
38     std::lock_guard<std::mutex> lock(mutex_);
39     lastAppsUid_ = {};
40 
41     Trace innerLockTrace("AudioRunningLock::runningLock_->Lock");
42     AudioXCollie audioXCollie("PowerMgr::RunningLock::Lock", LOCK_TIMEOUT_SECONDS,
43         [](void *) {
44             AUDIO_ERR_LOG("PowerMgr lock timeout");
45         }, nullptr, AUDIO_XCOLLIE_FLAG_LOG | AUDIO_XCOLLIE_FLAG_RECOVERY);
46     WatchTimeout guard("PowerMgr Lock timeout");
47     int32_t ret = runningLock_->Lock(timeoutMs);
48     isLocked_ = true;
49     AUDIO_INFO_LOG("lock end, ret: %{public}d", ret);
50     return ret;
51 }
52 
UnLock(void)53 int32_t AudioRunningLock::UnLock(void)
54 {
55     AUDIO_INFO_LOG("in");
56     CHECK_AND_RETURN_RET_LOG(runningLock_ != nullptr, -1, "lock is nullptr");
57     Trace unlockTrace("AudioRunningLock::UnLock");
58     std::lock_guard<std::mutex> lock(mutex_);
59     isLocked_ = false;
60     currentAppsUid_ = {};
61     lastAppsUid_ = {};
62 
63     Trace innerUpdateWorkSourceTrace("AudioRunningLock::runningLock_->UpdateWorkSource");
64     int32_t ret = runningLock_->UpdateWorkSource({});
65     AUDIO_INFO_LOG("update work source end, ret: %{public}d", ret);
66     Trace innerUnlockTrace("AudioRunningLock::runningLock_->UnLock");
67     ret = runningLock_->UnLock();
68     AUDIO_INFO_LOG("unlock end, ret: %{public}d", ret);
69     return ret;
70 }
71 
UpdateAppsUidToPowerMgr(void)72 int32_t AudioRunningLock::UpdateAppsUidToPowerMgr(void)
73 {
74     CHECK_AND_RETURN_RET_LOG(runningLock_ != nullptr, -1, "lock is nullptr");
75     Trace trace("AudioRunningLock::UpdateAppsUidToPowerMgr");
76     std::lock_guard<std::mutex> lock(mutex_);
77     if ((!isLocked_) || currentAppsUid_ == lastAppsUid_) {
78         return SUCCESS;
79     }
80     lastAppsUid_ = currentAppsUid_;
81     std::vector<int32_t> appsUid;
82     appsUid.insert(appsUid.end(), currentAppsUid_.begin(), currentAppsUid_.end());
83 
84     std::string appsUidInfo;
85     for (auto uid : appsUid) {
86         appsUidInfo += (std::to_string(uid) + ',');
87     }
88 
89     Trace innerUpdateWorkSourceTrace("AudioRunningLock::runningLock_->UpdateWorkSource");
90     int32_t ret = runningLock_->UpdateWorkSource(appsUid);
91     AUDIO_INFO_LOG("update end, uidInfo: %{public}s, size: %{public}zu, ret: %{public}d", appsUidInfo.c_str(),
92         appsUid.size(), ret);
93     return ret;
94 }
95 
96 } // namespace AudioStandard
97 } // namespace OHOS
98