• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef LOG_TAG
17 #define LOG_TAG "AudioSceneManager"
18 #endif
19 
20 #include "audio_scene_manager.h"
21 #include <ability_manager_client.h>
22 #include "iservice_registry.h"
23 #include "parameter.h"
24 #include "parameters.h"
25 #include "audio_policy_log.h"
26 #include "audio_manager_listener_stub.h"
27 #include "audio_inner_call.h"
28 #include "media_monitor_manager.h"
29 
30 #include "audio_policy_utils.h"
31 #include "audio_server_proxy.h"
32 
33 #ifdef BLUETOOTH_ENABLE
34 #include "audio_server_death_recipient.h"
35 #include "audio_bluetooth_manager.h"
36 #include "bluetooth_device_manager.h"
37 #endif
38 
39 namespace OHOS {
40 namespace AudioStandard {
41 
42 static const int64_t MEDIA_TO_RING_MUTE_DURATION_TIME_US = 200000; // 200ms
43 static const int64_t HEADSET_SWITCH_DELAY_US = 100000; //100ms
44 
SetAudioScenePre(AudioScene audioScene,const int32_t uid,const int32_t pid)45 void AudioSceneManager::SetAudioScenePre(AudioScene audioScene, const int32_t uid, const int32_t pid)
46 {
47     lastAudioScene_ = audioScene_;
48     audioScene_ = audioScene;
49     Bluetooth::AudioHfpManager::SetAudioSceneFromPolicy(audioScene_);
50     if (lastAudioScene_ != AUDIO_SCENE_DEFAULT && audioScene_ == AUDIO_SCENE_DEFAULT) {
51         AudioPolicyUtils::GetInstance().SetPreferredDevice(AUDIO_CALL_RENDER,
52             std::make_shared<AudioDeviceDescriptor>(), CLEAR_UID, "SetAudioScenePre");
53         AudioPolicyUtils::GetInstance().SetPreferredDevice(AUDIO_CALL_CAPTURE,
54             std::make_shared<AudioDeviceDescriptor>());
55 #ifdef BLUETOOTH_ENABLE
56         Bluetooth::AudioHfpManager::DisconnectSco();
57         AudioPolicyUtils::GetInstance().SetScoExcluded(false);
58 #endif
59     }
60     if (audioScene_ == AUDIO_SCENE_DEFAULT) {
61         AudioPolicyUtils::GetInstance().ClearScoDeviceSuspendState();
62     }
63 }
64 
IsStreamActive(AudioStreamType streamType) const65 bool AudioSceneManager::IsStreamActive(AudioStreamType streamType) const
66 {
67     CHECK_AND_RETURN_RET(streamType != STREAM_VOICE_CALL ||
68         GetAudioScene(true) != AUDIO_SCENE_PHONE_CALL, true);
69 
70     return streamCollector_.IsStreamActive(streamType);
71 }
72 
SetAudioSceneAfter(AudioScene audioScene,BluetoothOffloadState state)73 int32_t AudioSceneManager::SetAudioSceneAfter(AudioScene audioScene, BluetoothOffloadState state)
74 {
75     std::vector<DeviceType> activeOutputDevices;
76     bool haveArmUsbDevice = false;
77     DealAudioSceneOutputDevices(audioScene, activeOutputDevices, haveArmUsbDevice);
78     // mute primary when play media and ring
79     if (activeOutputDevices.size() > 1 && streamCollector_.IsMediaPlaying()) {
80         audioIOHandleMap_.MuteSinkPort(PRIMARY_SPEAKER, MEDIA_TO_RING_MUTE_DURATION_TIME_US, true);
81         // Wait for the audio data in the cache to be drained before moving the stream
82         // Increase the delay time for the headset device
83         DeviceType mainDeviceType = activeOutputDevices.front();
84         if (mainDeviceType == DEVICE_TYPE_USB_HEADSET || mainDeviceType == DEVICE_TYPE_USB_ARM_HEADSET) {
85             usleep(HEADSET_SWITCH_DELAY_US); // sleep fix data cache pop.
86         }
87     }
88     int32_t result = SUCCESS;
89     if (AudioPolicyUtils::GetInstance().GetScoExcluded()) {
90         return result;
91     }
92     if (haveArmUsbDevice) {
93         result = AudioServerProxy::GetInstance().SetAudioSceneProxy(audioScene, activeOutputDevices,
94             DEVICE_TYPE_USB_ARM_HEADSET, state);
95     } else {
96         result = AudioServerProxy::GetInstance().SetAudioSceneProxy(audioScene, activeOutputDevices,
97             audioActiveDevice_.GetCurrentInputDeviceType(), state);
98     }
99     return result;
100 }
101 
DealAudioSceneOutputDevices(const AudioScene & audioScene,std::vector<DeviceType> & activeOutputDevices,bool & haveArmUsbDevice)102 void AudioSceneManager::DealAudioSceneOutputDevices(const AudioScene &audioScene,
103     std::vector<DeviceType> &activeOutputDevices, bool &haveArmUsbDevice)
104 {
105     vector<std::shared_ptr<AudioDeviceDescriptor>> descs {};
106     switch (audioScene) {
107         case AUDIO_SCENE_RINGING:
108             descs = audioRouterCenter_.FetchOutputDevices(STREAM_USAGE_RINGTONE, -1);
109             if (!descs.empty()) {
110                 audioActiveDevice_.SetCurrentInputDeviceType(descs.front()->getType());
111             }
112             break;
113         case AUDIO_SCENE_VOICE_RINGING:
114             descs = audioRouterCenter_.FetchOutputDevices(STREAM_USAGE_VOICE_RINGTONE, -1);
115             if (!descs.empty()) {
116                 audioActiveDevice_.SetCurrentInputDeviceType(descs.front()->getType());
117             }
118             break;
119         default:
120             AUDIO_INFO_LOG("No ringing scene:%{public}d", audioScene);
121             break;
122     }
123 
124     if (!descs.empty()) {
125         for (size_t i = 0; i < descs.size(); i++) {
126             if (descs[i]->getType() == DEVICE_TYPE_USB_ARM_HEADSET) {
127                 AUDIO_INFO_LOG("usb headset is arm device.");
128                 activeOutputDevices.push_back(DEVICE_TYPE_USB_ARM_HEADSET);
129                 haveArmUsbDevice = true;
130             } else {
131                 activeOutputDevices.push_back(descs[i]->getType());
132             }
133         }
134     } else {
135         DeviceType activeDeviceType = audioActiveDevice_.GetCurrentOutputDeviceType();
136         if (activeDeviceType == DEVICE_TYPE_USB_ARM_HEADSET) {
137             activeOutputDevices.push_back(DEVICE_TYPE_USB_ARM_HEADSET);
138             haveArmUsbDevice = true;
139         } else {
140             activeOutputDevices.push_back(audioActiveDevice_.GetCurrentOutputDeviceType());
141         }
142     }
143 }
144 
GetAudioScene(bool hasSystemPermission) const145 AudioScene AudioSceneManager::GetAudioScene(bool hasSystemPermission) const
146 {
147     AUDIO_DEBUG_LOG("GetAudioScene return value: %{public}d", audioScene_);
148     if (!hasSystemPermission) {
149         switch (audioScene_) {
150             case AUDIO_SCENE_CALL_START:
151             case AUDIO_SCENE_CALL_END:
152                 return AUDIO_SCENE_DEFAULT;
153             default:
154                 break;
155         }
156     }
157     return audioScene_;
158 }
159 
GetLastAudioScene() const160 AudioScene AudioSceneManager::GetLastAudioScene() const
161 {
162     return lastAudioScene_;
163 }
164 
IsSameAudioScene()165 bool AudioSceneManager::IsSameAudioScene()
166 {
167     return lastAudioScene_ == audioScene_;
168 }
169 
IsVoiceCallRelatedScene()170 bool AudioSceneManager::IsVoiceCallRelatedScene()
171 {
172     return audioScene_ == AUDIO_SCENE_RINGING ||
173         audioScene_ == AUDIO_SCENE_PHONE_CALL ||
174         audioScene_ == AUDIO_SCENE_PHONE_CHAT ||
175         audioScene_ == AUDIO_SCENE_VOICE_RINGING;
176 }
177 }
178 }