Joey Poomarin | 5298998 | 2020-03-05 17:40:49 +0800 | [diff] [blame] | 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 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 25 | #include "StatsdLog.h" |
| 26 | |
Joey Poomarin | 5298998 | 2020-03-05 17:40:49 +0800 | [diff] [blame] | 27 | namespace android::mediametrics { |
| 28 | |
| 29 | class AudioAnalytics; |
| 30 | |
| 31 | class AudioPowerUsage { |
| 32 | public: |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 33 | AudioPowerUsage(AudioAnalytics *audioAnalytics, const std::shared_ptr<StatsdLog>& statsdLog); |
Joey Poomarin | 5298998 | 2020-03-05 17:40:49 +0800 | [diff] [blame] | 34 | ~AudioPowerUsage(); |
| 35 | |
| 36 | void checkTrackRecord(const std::shared_ptr<const mediametrics::Item>& item, bool isTrack); |
| 37 | void checkMode(const std::shared_ptr<const mediametrics::Item>& item); |
| 38 | void checkVoiceVolume(const std::shared_ptr<const mediametrics::Item>& item); |
| 39 | void checkCreatePatch(const std::shared_ptr<const mediametrics::Item>& item); |
| 40 | void clear(); |
| 41 | |
| 42 | /** |
| 43 | * Returns a pair consisting of the dump string, and the number of lines in the string. |
| 44 | * |
| 45 | * The number of lines in the returned pair is used as an optimization |
| 46 | * for subsequent line limiting. |
| 47 | * |
| 48 | * \param lines the maximum number of lines in the string returned. |
| 49 | */ |
| 50 | std::pair<std::string, int32_t> dump(int32_t lines = INT32_MAX) const; |
| 51 | |
| 52 | // align with message AudioUsageDataReported in frameworks/base/cmds/statsd/src/atoms.proto |
| 53 | enum AudioType { |
| 54 | UNKNOWN_TYPE = 0, |
| 55 | VOICE_CALL_TYPE = 1, // voice call |
| 56 | VOIP_CALL_TYPE = 2, // voip call, including uplink and downlink |
| 57 | MEDIA_TYPE = 3, // music and system sound |
| 58 | RINGTONE_NOTIFICATION_TYPE = 4, // ringtone and notification |
| 59 | ALARM_TYPE = 5, // alarm type |
| 60 | // record type |
| 61 | CAMCORDER_TYPE = 6, // camcorder |
| 62 | RECORD_TYPE = 7, // other recording |
| 63 | }; |
| 64 | |
| 65 | enum AudioDevice { |
| 66 | OUTPUT_EARPIECE = 0x1, |
| 67 | OUTPUT_SPEAKER = 0x2, |
| 68 | OUTPUT_WIRED_HEADSET = 0x4, |
| 69 | OUTPUT_USB_HEADSET = 0x8, |
| 70 | OUTPUT_BLUETOOTH_SCO = 0x10, |
| 71 | OUTPUT_BLUETOOTH_A2DP = 0x20, |
| 72 | OUTPUT_SPEAKER_SAFE = 0x40, |
| 73 | |
| 74 | INPUT_DEVICE_BIT = 0x40000000, |
| 75 | INPUT_BUILTIN_MIC = INPUT_DEVICE_BIT | 0x1, // non-negative positive int32. |
| 76 | INPUT_BUILTIN_BACK_MIC = INPUT_DEVICE_BIT | 0x2, |
| 77 | INPUT_WIRED_HEADSET_MIC = INPUT_DEVICE_BIT | 0x4, |
| 78 | INPUT_USB_HEADSET_MIC = INPUT_DEVICE_BIT | 0x8, |
| 79 | INPUT_BLUETOOTH_SCO = INPUT_DEVICE_BIT | 0x10, |
| 80 | }; |
| 81 | |
| 82 | static bool typeFromString(const std::string& type_string, int32_t& type); |
| 83 | static bool deviceFromString(const std::string& device_string, int32_t& device); |
| 84 | static int32_t deviceFromStringPairs(const std::string& device_strings); |
| 85 | private: |
| 86 | bool saveAsItem_l(int32_t device, int64_t duration, int32_t type, double average_vol) |
| 87 | REQUIRES(mLock); |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 88 | void sendItem(const std::shared_ptr<const mediametrics::Item>& item) const; |
Joey Poomarin | 5298998 | 2020-03-05 17:40:49 +0800 | [diff] [blame] | 89 | void collect(); |
rogerfang | f1192d2 | 2020-07-16 16:27:16 +0800 | [diff] [blame] | 90 | bool saveAsItems_l(int32_t device, int64_t duration, int32_t type, double average_vol) |
| 91 | REQUIRES(mLock); |
Joey Poomarin | 5298998 | 2020-03-05 17:40:49 +0800 | [diff] [blame] | 92 | |
| 93 | AudioAnalytics * const mAudioAnalytics; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 94 | const std::shared_ptr<StatsdLog> mStatsdLog; // mStatsdLog is internally locked |
Joey Poomarin | 5298998 | 2020-03-05 17:40:49 +0800 | [diff] [blame] | 95 | const bool mDisabled; |
| 96 | const int32_t mIntervalHours; |
| 97 | |
| 98 | mutable std::mutex mLock; |
| 99 | std::deque<std::shared_ptr<mediametrics::Item>> mItems GUARDED_BY(mLock); |
| 100 | |
| 101 | double mVoiceVolume GUARDED_BY(mLock) = 0.; |
| 102 | double mDeviceVolume GUARDED_BY(mLock) = 0.; |
| 103 | int64_t mStartCallNs GUARDED_BY(mLock) = 0; // advisory only |
| 104 | int64_t mVolumeTimeNs GUARDED_BY(mLock) = 0; |
| 105 | int64_t mDeviceTimeNs GUARDED_BY(mLock) = 0; |
| 106 | int32_t mPrimaryDevice GUARDED_BY(mLock) = OUTPUT_SPEAKER; |
| 107 | std::string mMode GUARDED_BY(mLock) {"AUDIO_MODE_NORMAL"}; |
| 108 | }; |
| 109 | |
| 110 | } // namespace android::mediametrics |