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 EVENT_AGGREGATE_H
17 #define EVENT_AGGREGATE_H
18 
19 #include <mutex>
20 #include "event_bean.h"
21 #include "audio_memo.h"
22 #include "media_monitor_policy.h"
23 #include "audio_info.h"
24 
25 using OHOS::AudioStandard::AudioStreamPolicyServiceDiedCallback;
26 
27 namespace OHOS {
28 namespace Media {
29 namespace MediaMonitor {
30 
31 enum DeviceChangeEvent {
32     FOR_DEVICE_EVENT = 0,
33     FOR_CAPTURE_MUTE_EVENT,
34     FOR_VOLUME_CHANGE_EVENT
35 };
36 
37 class EventAggregate {
38 public:
GetEventAggregate()39     static EventAggregate& GetEventAggregate()
40     {
41         static EventAggregate eventAggregate;
42         return eventAggregate;
43     }
44 
45     EventAggregate();
46     ~EventAggregate();
47 
48     void WriteEvent(std::shared_ptr<EventBean> &bean);
49 
50     void WriteInfo(int32_t fd, std::string &dumpString);
51 
52 private:
53     void UpdateAggregateEventList(std::shared_ptr<EventBean> &bean);
54     void HandleDeviceChangeEvent(std::shared_ptr<EventBean> &bean);
55     void HandleStreamChangeEvent(std::shared_ptr<EventBean> &bean);
56 
57     void HandleStreamExhaustedErrorEvent(std::shared_ptr<EventBean> &bean);
58     void HandleStreamCreateErrorEvent(std::shared_ptr<EventBean> &bean);
59     void HandleBackgroundSilentPlayback(std::shared_ptr<EventBean> &bean);
60     void HandleUnderrunStatistic(std::shared_ptr<EventBean> &bean);
61     void HandleForceUseDevice(std::shared_ptr<EventBean> &bean);
62     void HandleCaptureMutedStatusChange(std::shared_ptr<EventBean> &bean);
63     void HandleVolumeChange(std::shared_ptr<EventBean> &bean);
64 
65     void HandleCaptureMuted(std::shared_ptr<EventBean> &bean);
66     void HandleDeviceUsage(std::shared_ptr<EventBean> &bean);
67     void HandleStreamUsage(std::shared_ptr<EventBean> &bean);
68     void HandleStreamChangeForVolume(std::shared_ptr<EventBean> &bean);
69     void HandlePipeChange(std::shared_ptr<EventBean> &bean);
70     void HandleFocusMigrate(std::shared_ptr<EventBean> &bean);
71 
72     void AddToDeviceUsage(std::shared_ptr<EventBean> &bean, uint64_t curruntTime);
73     void AddToStreamUsage(std::shared_ptr<EventBean> &bean, uint64_t curruntTime);
74     void AddToCaptureMuteUsage(std::shared_ptr<EventBean> &bean, uint64_t curruntTime);
75     void AddToVolumeVector(std::shared_ptr<EventBean> &bean, uint64_t curruntTime);
76     void AddToStreamPropertyVector(std::shared_ptr<EventBean> &bean, uint64_t curruntTime);
77 
78     void HandleDeviceChangeForDeviceUsage(std::shared_ptr<EventBean> &bean);
79     void HandleDeviceChangeForCaptureMuted(std::shared_ptr<EventBean> &bean);
80     void HandleDeviceChangeForVolume(std::shared_ptr<EventBean> &bean);
81     void HandleDeviceChangeForDuration(const DeviceChangeEvent &event,
82         std::shared_ptr<EventBean> &bean, std::shared_ptr<EventBean> &beanInVector);
83     void HandleStreamPropertyStats(std::shared_ptr<EventBean> &bean);
84 
85     AudioMemo& audioMemo_;
86     MediaMonitorPolicy& mediaMonitorPolicy_;
87 
88     std::vector<std::shared_ptr<EventBean>> deviceUsageVector_;
89     std::vector<std::shared_ptr<EventBean>> streamUsageVector_;
90     std::vector<std::shared_ptr<EventBean>> captureMutedVector_;
91     std::vector<std::shared_ptr<EventBean>> volumeVector_;
92     std::vector<std::shared_ptr<EventBean>> streamPropertyVector_;
93 
94     // Record volume for stream state 2->5->2
95     int32_t systemVol_ = -1;
96 };
97 } // namespace MediaMonitor
98 } // namespace Media
99 } // namespace OHOS
100 #endif // EVENT_AGGREGATE_H