1 /* 2 * Copyright (c) 2024 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 ST_AUDIO_SESSION_SERVICE_H 17 #define ST_AUDIO_SESSION_SERVICE_H 18 19 #include <mutex> 20 #include <vector> 21 #include "audio_session.h" 22 #include "audio_session_state_monitor.h" 23 #include "audio_device_info.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 class SessionTimeOutCallback { 28 public: 29 virtual ~SessionTimeOutCallback() = default; 30 31 virtual void OnSessionTimeout(const int32_t pid) = 0; 32 }; 33 34 class AudioSessionService : public AudioSessionStateMonitor, public std::enable_shared_from_this<AudioSessionService> { 35 public: 36 AudioSessionService(); 37 ~AudioSessionService() override; 38 39 // Audio session manager interfaces 40 static std::shared_ptr<AudioSessionService> GetAudioSessionService(void); 41 int32_t ActivateAudioSession(const int32_t callerPid, const AudioSessionStrategy &strategy); 42 int32_t DeactivateAudioSession(const int32_t callerPid); 43 bool IsAudioSessionActivated(const int32_t callerPid); 44 45 // Audio session timer callback 46 void OnAudioSessionTimeOut(int32_t callerPid) override; 47 48 // other public interfaces 49 int32_t SetSessionTimeOutCallback(const std::shared_ptr<SessionTimeOutCallback> &timeOutCallback); 50 std::shared_ptr<AudioSession> GetAudioSessionByPid(const int32_t callerPid); 51 52 // Dump AudioSession Info 53 void AudioSessionInfoDump(std::string &dumpString); 54 int32_t SetSessionDefaultOutputDevice(const int32_t callerPid, const DeviceType &deviceType); 55 bool IsStreamAllowedToSetDevice(const uint32_t streamId); 56 DeviceType GetSessionDefaultOutputDevice(const int32_t callerPid); 57 bool IsSessionNeedToFetchOutputDevice(const int32_t callerPid); 58 59 int32_t SetAudioSessionScene(int32_t callerPid, AudioSessionScene scene); 60 StreamUsage GetAudioSessionStreamUsage(int32_t callerPid); 61 bool IsAudioSessionFocusMode(int32_t callerPid); 62 bool ShouldBypassFocusForStream(const AudioInterrupt &audioInterrupt); 63 bool ShouldExcludeStreamType(const AudioInterrupt &audioInterrupt); 64 std::vector<AudioInterrupt> GetStreams(int32_t callerPid); 65 AudioInterrupt GenerateFakeAudioInterrupt(int32_t callerPid); 66 void RemoveStreamInfo(const AudioInterrupt &audioInterrupt); 67 void ClearStreamInfo(const int32_t callerPid); 68 bool ShouldAudioSessionProcessHintType(InterruptHint hintType); 69 bool ShouldAudioStreamProcessHintType(InterruptHint hintType); 70 static bool IsSameTypeForAudioSession(const AudioStreamType incomingType, const AudioStreamType existedType); 71 void NotifyAppStateChange(const int32_t pid, bool isBackState); 72 bool HasStreamForDeviceType(int32_t callerPid, DeviceType deviceType); 73 74 private: 75 int32_t DeactivateAudioSessionInternal(const int32_t callerPid, bool isSessionTimeout = false); 76 std::shared_ptr<AudioSessionStateMonitor> GetSelfSharedPtr() override; 77 void GenerateFakeStreamId(int32_t callerPid); 78 79 private: 80 std::mutex sessionServiceMutex_; 81 std::unordered_map<int32_t, std::shared_ptr<AudioSession>> sessionMap_; 82 std::weak_ptr<SessionTimeOutCallback> timeOutCallback_; 83 }; 84 } // namespace AudioStandard 85 } // namespace OHOS 86 87 #endif // ST_AUDIO_SESSION_SERVICE_H