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 #ifndef OHOS_FOCUS_SESSION_STRATEGY_H 17 #define OHOS_FOCUS_SESSION_STRATEGY_H 18 19 #include <functional> 20 #include <map> 21 #include <memory> 22 #include "audio_stream_manager.h" 23 #include "audio_adapter.h" 24 25 namespace OHOS::AVSession { 26 class FocusSessionStrategy { 27 public: 28 using StreamUsage = AudioStandard::StreamUsage; 29 struct FocusSessionChangeInfo { 30 int32_t uid {}; 31 int32_t pid {}; 32 StreamUsage streamUsage {}; 33 }; 34 using FocusSessionChangeCallback = std::function<void(const FocusSessionChangeInfo&, bool)>; 35 using FocusSessionSelector = std::function<bool(const FocusSessionChangeInfo&)>; 36 37 FocusSessionStrategy(); 38 ~FocusSessionStrategy(); 39 40 void Init(); 41 42 void RegisterFocusSessionChangeCallback(const FocusSessionChangeCallback& callback); 43 void RegisterFocusSessionSelector(const FocusSessionSelector& selector); 44 std::vector<int> GetAudioPlayingUids(); 45 void SetAudioPlayingUids(std::vector<int> audioPlayingUids); 46 47 private: 48 void HandleAudioRenderStateChangeEvent(const AudioRendererChangeInfos& infos); 49 50 bool IsFocusSession(const std::pair<int32_t, int32_t> key); 51 bool CheckFocusSessionStop(const std::pair<int32_t, int32_t> key); 52 void UpdateFocusSession(const std::pair<int32_t, int32_t> key); 53 void DelayStopFocusSession(const std::pair<int32_t, int32_t> key); 54 void ProcAudioRenderChange(const AudioRendererChangeInfos& infos); 55 56 FocusSessionChangeCallback callback_; 57 FocusSessionSelector selector_; 58 std::shared_ptr<AudioStandard::AudioRendererStateChangeCallback> audioRendererStateChangeCallback_; 59 std::recursive_mutex stateLock_; 60 std::map<std::pair<int32_t, int32_t>, int32_t> lastStates_; //<<uid,pid>, state> 61 std::map<std::pair<int32_t, int32_t>, int32_t> currentStates_; 62 const int32_t cancelTimeout = 5000; 63 const int32_t runningState = 2; 64 const int32_t stopState = 0; 65 const int32_t releaseState = 4; 66 const int32_t ancoUid = 1041; 67 const std::vector<AudioStandard::StreamUsage> ALLOWED_ANCO_STREAM_USAGE { 68 AudioStandard::STREAM_USAGE_MUSIC, 69 AudioStandard::STREAM_USAGE_MOVIE, 70 AudioStandard::STREAM_USAGE_VOICE_COMMUNICATION 71 }; 72 const std::vector<AudioStandard::StreamUsage> ALLOWED_MEDIA_STREAM_USAGE { 73 AudioStandard::STREAM_USAGE_MUSIC, 74 AudioStandard::STREAM_USAGE_MOVIE, 75 AudioStandard::STREAM_USAGE_AUDIOBOOK 76 }; 77 const std::vector<AudioStandard::StreamUsage> AUDIO_PLAYING_STREAM_USAGE { 78 AudioStandard::STREAM_USAGE_MUSIC, 79 AudioStandard::STREAM_USAGE_MOVIE, 80 AudioStandard::STREAM_USAGE_AUDIOBOOK, 81 AudioStandard::STREAM_USAGE_VOICE_COMMUNICATION, 82 AudioStandard::STREAM_USAGE_VIDEO_COMMUNICATION, 83 AudioStandard::STREAM_USAGE_RINGTONE 84 }; 85 const std::vector<AudioStandard::RendererState> AUDIO_PLAYING_STREAM_STATE { 86 AudioStandard::RendererState::RENDERER_RUNNING 87 }; 88 std::vector<int> audioPlayingUids_; 89 std::recursive_mutex audioPlayingLock_; 90 }; 91 } 92 #endif // OHOS_FOCUS_SESSION_STRATEGY_H