• 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 "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         SLOGI("set background observe for uid %{public}d", uid);
43         HandleAppBackgroundState(uid);
44     });
45 }
46 
OnSessionCreate(const AVSessionDescriptor & descriptor)47 void BackgroundAudioController::OnSessionCreate(const AVSessionDescriptor& descriptor)
48 {
49     std::lock_guard lockGuard(lock_);
50     sessionUIDs_.insert(descriptor.uid_);
51     AppManagerAdapter::GetInstance().RemoveObservedApp(descriptor.uid_);
52     SLOGI("OnSessionCreate remove observe for uid %{public}d", descriptor.uid_);
53 }
54 
OnSessionRelease(const AVSessionDescriptor & descriptor)55 void BackgroundAudioController::OnSessionRelease(const AVSessionDescriptor& descriptor)
56 {
57     {
58         std::lock_guard lockGuard(lock_);
59         sessionUIDs_.erase(descriptor.uid_);
60     }
61 
62     if (descriptor.isThirdPartyApp_) {
63         if (!AppManagerAdapter::GetInstance().IsAppBackground(descriptor.uid_)) {
64             AppManagerAdapter::GetInstance().AddObservedApp(descriptor.uid_);
65             SLOGI("OnSessionRelease add observe for uid %{public}d", descriptor.uid_);
66             return;
67         }
68         int32_t uid = descriptor.uid_;
69         AudioStandard::RendererState rendererState = AudioStandard::RENDERER_PAUSED;
70         bool isSuccess = AudioAdapter::GetInstance().GetRendererState(uid, rendererState);
71         if (isSuccess && rendererState != AudioStandard::RENDERER_RUNNING) {
72             SLOGI("renderer state is not AudioStandard::RENDERER_RUNNING");
73             return;
74         }
75         SLOGI("pause uid=%{public}d", descriptor.uid_);
76         ptr_->NotifyAudioSessionCheckTrigger(descriptor.uid_);
77         AudioAdapter::GetInstance().PauseAudioStream(descriptor.uid_);
78     }
79 }
80 
HandleAudioStreamRendererStateChange(const AudioRendererChangeInfos & infos)81 void BackgroundAudioController::HandleAudioStreamRendererStateChange(const AudioRendererChangeInfos& infos)
82 {
83     for (const auto& info : infos) {
84         if (info->rendererState != AudioStandard::RENDERER_RUNNING) {
85             continue;
86         }
87         {
88             std::lock_guard lockGuard(lock_);
89             auto it = sessionUIDs_.find(info->clientUID);
90             if (it != sessionUIDs_.end()) {
91                 SLOGD("uid=%{public}d has session", info->clientUID);
92                 continue;
93             }
94         }
95         if (PermissionChecker::GetInstance().CheckSystemPermissionByUid(info->clientUID)) {
96             SLOGD("uid=%{public}d is system app", info->clientUID);
97             continue;
98         }
99 
100         if (!AppManagerAdapter::GetInstance().IsAppBackground(info->clientUID)) {
101             AppManagerAdapter::GetInstance().AddObservedApp(info->clientUID);
102             SLOGD("AudioStreamRendererStateChange add observe for uid %{public}d", info->clientUID);
103             continue;
104         }
105         SLOGI("pause uid=%{public}d", info->clientUID);
106         ptr_->NotifyAudioSessionCheckTrigger(info->clientUID);
107         AudioAdapter::GetInstance().PauseAudioStream(info->clientUID);
108     }
109 }
110 
HandleAppBackgroundState(int32_t uid) const111 void BackgroundAudioController::HandleAppBackgroundState(int32_t uid) const
112 {
113     AudioStandard::RendererState rendererState = AudioStandard::RENDERER_PAUSED;
114     bool isSuccess = AudioAdapter::GetInstance().GetRendererState(uid, rendererState);
115     if (isSuccess) {
116         SLOGI("pause uid=%{public}d", uid);
117         ptr_->NotifyAudioSessionCheckTrigger(uid);
118         AudioAdapter::GetInstance().PauseAudioStream(uid);
119     }
120     SLOGI("renderer state is not AudioStandard::RENDERER_RUNNING");
121 }
122 }