1 /*
2 * Copyright (c) 2022 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 "background_audio_controller.h"
17 #include "avsession_log.h"
18 #include "avsession_service.h"
19 #include "avsession_item.h"
20 #include "permission_checker.h"
21
22 namespace OHOS::AVSession {
23 using AudioStandard::RendererState;
24
BackgroundAudioController()25 BackgroundAudioController::BackgroundAudioController() : ptr_(nullptr)
26 {
27 SLOGI("construct");
28 }
29
~BackgroundAudioController()30 BackgroundAudioController::~BackgroundAudioController()
31 {
32 SLOGI("destroy");
33 }
34
Init(AVSessionService * ptr)35 void BackgroundAudioController::Init(AVSessionService *ptr)
36 {
37 ptr_ = ptr;
38 AudioAdapter::GetInstance().AddStreamRendererStateListener([this](const auto& infos) {
39 HandleAudioStreamRendererStateChange(infos);
40 });
41 AppManagerAdapter::GetInstance().SetAppBackgroundStateObserver([this](int32_t uid) {
42 HandleAppBackgroundState(uid);
43 });
44 }
45
OnSessionCreate(const AVSessionDescriptor & descriptor)46 void BackgroundAudioController::OnSessionCreate(const AVSessionDescriptor& descriptor)
47 {
48 std::lock_guard lockGuard(lock_);
49 sessionUIDs_.insert(descriptor.uid_);
50 AppManagerAdapter::GetInstance().RemoveObservedApp(descriptor.uid_);
51 }
52
OnSessionRelease(const AVSessionDescriptor & descriptor)53 void BackgroundAudioController::OnSessionRelease(const AVSessionDescriptor& descriptor)
54 {
55 {
56 std::lock_guard lockGuard(lock_);
57 sessionUIDs_.erase(descriptor.uid_);
58 }
59
60 if (descriptor.isThirdPartyApp_) {
61 if (!AppManagerAdapter::GetInstance().IsAppBackground(descriptor.uid_)) {
62 AppManagerAdapter::GetInstance().AddObservedApp(descriptor.uid_);
63 return;
64 }
65 int32_t uid = descriptor.uid_;
66 AudioStandard::RendererState rendererState = AudioStandard::RENDERER_PAUSED;
67 bool isSuccess = AudioAdapter::GetInstance().GetRendererState(uid, rendererState);
68 if (isSuccess && rendererState != AudioStandard::RENDERER_RUNNING) {
69 SLOGI("renderer state is not AudioStandard::RENDERER_RUNNING");
70 return;
71 }
72 SLOGI("pause uid=%{public}d", descriptor.uid_);
73 ptr_->NotifyAudioSessionCheckTrigger(descriptor.uid_);
74 AudioAdapter::GetInstance().PauseAudioStream(descriptor.uid_);
75 }
76 }
77
HandleAudioStreamRendererStateChange(const AudioRendererChangeInfos & infos)78 void BackgroundAudioController::HandleAudioStreamRendererStateChange(const AudioRendererChangeInfos& infos)
79 {
80 for (const auto& info : infos) {
81 if (info->rendererState != AudioStandard::RENDERER_RUNNING) {
82 continue;
83 }
84 {
85 std::lock_guard lockGuard(lock_);
86 auto it = sessionUIDs_.find(info->clientUID);
87 if (it != sessionUIDs_.end()) {
88 SLOGI("uid=%{public}d has session", info->clientUID);
89 continue;
90 }
91 }
92 if (PermissionChecker::GetInstance().CheckSystemPermissionByUid(info->clientUID)) {
93 SLOGI("uid=%{public}d is system app", info->clientUID);
94 continue;
95 }
96
97 if (!AppManagerAdapter::GetInstance().IsAppBackground(info->clientUID)) {
98 AppManagerAdapter::GetInstance().AddObservedApp(info->clientUID);
99 continue;
100 }
101 SLOGI("pause uid=%{public}d", info->clientUID);
102 ptr_->NotifyAudioSessionCheckTrigger(info->clientUID);
103 AudioAdapter::GetInstance().PauseAudioStream(info->clientUID);
104 }
105 }
106
HandleAppBackgroundState(int32_t uid) const107 void BackgroundAudioController::HandleAppBackgroundState(int32_t uid) const
108 {
109 AudioStandard::RendererState rendererState = AudioStandard::RENDERER_PAUSED;
110 bool isSuccess = AudioAdapter::GetInstance().GetRendererState(uid, rendererState);
111 if (isSuccess) {
112 SLOGI("pause uid=%{public}d", uid);
113 ptr_->NotifyAudioSessionCheckTrigger(uid);
114 AudioAdapter::GetInstance().PauseAudioStream(uid);
115 }
116 SLOGI("renderer state is not AudioStandard::RENDERER_RUNNING");
117 }
118 }