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