• 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 #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         StreamUsage streamUsage {};
32     };
33     using FocusSessionChangeCallback = std::function<void(const FocusSessionChangeInfo&, bool)>;
34     using FocusSessionSelector = std::function<bool(const FocusSessionChangeInfo&)>;
35 
36     FocusSessionStrategy();
37     ~FocusSessionStrategy();
38 
39     void Init();
40 
41     void RegisterFocusSessionChangeCallback(const FocusSessionChangeCallback& callback);
42     void RegisterFocusSessionSelector(const FocusSessionSelector& selector);
43 
44 private:
45     void HandleAudioRenderStateChangeEvent(const AudioRendererChangeInfos& infos);
46 
47     bool IsFocusSession(const int32_t uid);
48     bool CheckFocusSessionStop(const int32_t uid);
49     void UpdateFocusSession(const int32_t uid);
50     void DelayStopFocusSession(const int32_t uid);
51     void ProcAudioRenderChange(const AudioRendererChangeInfos& infos);
52 
53     FocusSessionChangeCallback callback_;
54     FocusSessionSelector selector_;
55     std::shared_ptr<AudioStandard::AudioRendererStateChangeCallback> audioRendererStateChangeCallback_;
56     std::recursive_mutex stateLock_;
57     std::map<int32_t, int32_t> lastStates_;
58     std::map<int32_t, int32_t> currentStates_;
59     const int32_t cancelTimeout = 5000;
60     const int32_t runningState = 2;
61     const int32_t stopState = 0;
62     const int32_t ancoUid = 1041;
63     const std::vector<AudioStandard::StreamUsage> ALLOWED_ANCO_STREAM_USAGE {
64         AudioStandard::STREAM_USAGE_MUSIC,
65         AudioStandard::STREAM_USAGE_MOVIE,
66         AudioStandard::STREAM_USAGE_VOICE_COMMUNICATION
67     };
68 };
69 }
70 #endif // OHOS_FOCUS_SESSION_STRATEGY_H