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