• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 RESSCHED_SCHED_CONTROLLER_OBSERVER_INCLUDE_AUDIO_OBSERVER_H
17 #define RESSCHED_SCHED_CONTROLLER_OBSERVER_INCLUDE_AUDIO_OBSERVER_H
18 #ifdef RESSCHED_AUDIO_FRAMEWORK_ENABLE
19 #include <string>
20 #include <unordered_map>
21 
22 #include "nlohmann/json.hpp"
23 #include "audio_stream_manager.h"
24 #include "audio_system_manager.h"
25 #include "audio_routing_manager.h"
26 
27 namespace OHOS {
28 namespace ResourceSchedule {
29 struct ProcessRenderState {
30     int32_t pid;
31     int32_t uid;
32     std::unordered_set<int32_t> renderRunningSessionSet;
33 };
34 class AudioObserver :
35     public AudioStandard::AudioRendererStateChangeCallback,
36     public AudioStandard::AudioRingerModeCallback,
37     public AudioStandard::VolumeKeyEventCallback,
38     public AudioStandard::AudioManagerAudioSceneChangedCallback,
39     public AudioStandard::AudioPreferredOutputDeviceChangeCallback {
40 public:
41     void OnRendererStateChange(
42         const std::vector<std::shared_ptr<AudioStandard::AudioRendererChangeInfo>> &audioRendererChangeInfos) override;
43     void OnRingerModeUpdated(const AudioStandard::AudioRingerMode &ringerMode) override;
44     void OnVolumeKeyEvent(AudioStandard::VolumeEvent volumeEvent) override;
45     void OnAudioSceneChange(const AudioStandard::AudioScene audioScene) override;
46     void OnPreferredOutputDeviceUpdated(
47         const std::vector<std::shared_ptr<AudioStandard::AudioDeviceDescriptor>> &descs)  override;
48     void Init();
49 private:
50     int32_t mode_ = -1;
51     void MarshallingAudioRendererChangeInfo(
52         const std::shared_ptr<AudioStandard::AudioRendererChangeInfo> &audioRendererChangeInfo,
53         nlohmann::json &payload);
54     bool IsRenderStateChange(const std::shared_ptr<AudioStandard::AudioRendererChangeInfo>& info);
55     void HandleInnerAudioStateChange(
56         const std::shared_ptr<AudioStandard::AudioRendererChangeInfo> &audioRendererChangeInfo,
57         std::unordered_set<int32_t>& newRunningPid, std::unordered_set<int32_t>& newStopSessionPid);
58     void ProcessRunningSessionState(
59         const std::shared_ptr<AudioStandard::AudioRendererChangeInfo> &audioRendererChangeInfo,
60         std::unordered_set<int32_t>& newRunningPid);
61     void ProcessStopSessionState(
62         const std::shared_ptr<AudioStandard::AudioRendererChangeInfo> &audioRendererChangeInfo,
63         std::unordered_set<int32_t>& newStopSessionPid);
64     void HandleStartAudioStateEvent(const std::unordered_set<int32_t>& newRunningPid);
65     void HandleStopAudioStateEvent(const std::unordered_set<int32_t>& newStopSessionPid);
66     void MarshallingInnerAudioRendererChangeInfo(int32_t pid, nlohmann::json &payload);
67     bool IsValidPid(pid_t pid);
68 
69     std::unordered_map<int32_t, AudioStandard::RendererState> renderState_;
70     std::unordered_map<int32_t, int32_t> volumeState_;
71     std::unordered_map<int32_t, ProcessRenderState> processRenderStateMap_;
72 };
73 } // namespace ResourceSchedule
74 } // namespace OHOS
75 #endif
76 #endif // RESSCHED_SCHED_CONTROLLER_OBSERVER_INCLUDE_AUDIO_OBSERVER_H
77