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&)>;
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 AudioStandard::AudioRendererChangeInfo& info);
48     bool SelectFocusSession(const AudioRendererChangeInfos& infos, FocusSessionChangeInfo& sessionInfo);
49 
50     FocusSessionChangeCallback callback_;
51     FocusSessionSelector selector_;
52     std::shared_ptr<AudioStandard::AudioRendererStateChangeCallback> audioRendererStateChangeCallback_;
53     std::recursive_mutex stateLock_;
54     std::map<int32_t, AudioStandard::RendererState> lastStates_;
55 };
56 }
57 #endif // OHOS_FOCUS_SESSION_STRATEGY_H