• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <android-base/thread_annotations.h>
20 #include <deque>
21 #include <media/MediaMetricsItem.h>
22 #include <mutex>
23 #include <thread>
24 
25 #include "StatsdLog.h"
26 
27 namespace android::mediametrics {
28 
29 
30 class AudioAnalytics;
31 
32 class AudioPowerUsage {
33 public:
34     AudioPowerUsage(AudioAnalytics *audioAnalytics, const std::shared_ptr<StatsdLog>& statsdLog);
35     ~AudioPowerUsage();
36 
37     void checkTrackRecord(const std::shared_ptr<const mediametrics::Item>& item, bool isTrack);
38     void checkMode(const std::shared_ptr<const mediametrics::Item>& item);
39     void checkVoiceVolume(const std::shared_ptr<const mediametrics::Item>& item);
40     void checkCreatePatch(const std::shared_ptr<const mediametrics::Item>& item);
41     void clear();
42 
43     /**
44      * Returns a pair consisting of the dump string, and the number of lines in the string.
45      *
46      * The number of lines in the returned pair is used as an optimization
47      * for subsequent line limiting.
48      *
49      * \param lines the maximum number of lines in the string returned.
50      */
51     std::pair<std::string, int32_t> dump(int32_t lines = INT32_MAX) const;
52 
53     // align with message AudioUsageDataReported in frameworks/base/cmds/statsd/src/atoms.proto
54     enum AudioType {
55         UNKNOWN_TYPE = 0,
56         VOICE_CALL_TYPE = 1,            // voice call
57         VOIP_CALL_TYPE = 2,             // voip call, including uplink and downlink
58         MEDIA_TYPE = 3,                 // music and system sound
59         RINGTONE_NOTIFICATION_TYPE = 4, // ringtone and notification
60         ALARM_TYPE = 5,                 // alarm type
61         // record type
62         CAMCORDER_TYPE = 6,             // camcorder
63         RECORD_TYPE = 7,                // other recording
64     };
65 
66     enum AudioDevice {
67         OUTPUT_EARPIECE         = 0x1,
68         OUTPUT_SPEAKER          = 0x2,
69         OUTPUT_WIRED_HEADSET    = 0x4,
70         OUTPUT_USB_HEADSET      = 0x8,
71         OUTPUT_BLUETOOTH_SCO    = 0x10,
72         OUTPUT_BLUETOOTH_A2DP   = 0x20,
73         OUTPUT_SPEAKER_SAFE     = 0x40,
74 
75         INPUT_DEVICE_BIT        = 0x40000000,
76         INPUT_BUILTIN_MIC       = INPUT_DEVICE_BIT | 0x1, // non-negative positive int32.
77         INPUT_BUILTIN_BACK_MIC  = INPUT_DEVICE_BIT | 0x2,
78         INPUT_WIRED_HEADSET_MIC = INPUT_DEVICE_BIT | 0x4,
79         INPUT_USB_HEADSET_MIC   = INPUT_DEVICE_BIT | 0x8,
80         INPUT_BLUETOOTH_SCO     = INPUT_DEVICE_BIT | 0x10,
81     };
82 
83     static bool typeFromString(const std::string& type_string, int32_t& type);
84     static bool deviceFromString(const std::string& device_string, int32_t& device);
85     static int32_t deviceFromStringPairs(const std::string& device_strings);
86 private:
87     bool saveAsItem_l(int32_t device, int64_t duration, int32_t type, double average_vol,
88                       int64_t max_volume_duration, double max_volume,
89                       int64_t min_volume_duration, double min_volume)
90                       REQUIRES(mLock);
91     void sendItem(const std::shared_ptr<const mediametrics::Item>& item) const;
92     void collect();
93     bool saveAsItems_l(int32_t device, int64_t duration, int32_t type, double average_vol,
94                       int64_t max_volume_duration, double max_volume,
95                       int64_t min_volume_duration, double min_volume)
96                       REQUIRES(mLock);
97     void updateMinMaxVolumeAndDuration(
98             const int64_t cur_max_volume_duration_ns, const double cur_max_volume,
99             const int64_t cur_min_volume_duration_ns, const double cur_min_volume,
100             int64_t& f_max_volume_duration_ns, double& f_max_volume,
101             int64_t& f_min_volume_duration_ns, double& f_min_volume);
102     AudioAnalytics * const mAudioAnalytics;
103     const std::shared_ptr<StatsdLog> mStatsdLog;  // mStatsdLog is internally locked
104     const bool mDisabled;
105     const int32_t mIntervalHours;
106 
107     mutable std::mutex mLock;
108     std::deque<std::shared_ptr<mediametrics::Item>> mItems GUARDED_BY(mLock);
109 
110     double mVoiceVolume GUARDED_BY(mLock) = 0.;
111     double mDeviceVolume GUARDED_BY(mLock) = 0.;
112     double mMaxVoiceVolume GUARDED_BY(mLock) = AMEDIAMETRICS_INITIAL_MAX_VOLUME;
113     double mMinVoiceVolume GUARDED_BY(mLock) = AMEDIAMETRICS_INITIAL_MIN_VOLUME;
114     int64_t mMaxVoiceVolumeDurationNs GUARDED_BY(mLock) = 0;
115     int64_t mMinVoiceVolumeDurationNs GUARDED_BY(mLock) = 0;
116     int64_t mStartCallNs GUARDED_BY(mLock) = 0; // advisory only
117     int64_t mVolumeTimeNs GUARDED_BY(mLock) = 0;
118     int64_t mDeviceTimeNs GUARDED_BY(mLock) = 0;
119     int32_t mPrimaryDevice GUARDED_BY(mLock) = OUTPUT_SPEAKER;
GUARDED_BY(mLock)120     std::string mMode GUARDED_BY(mLock) {"AUDIO_MODE_NORMAL"};
121 };
122 
123 } // namespace android::mediametrics
124