Andy Hung | 953608f | 2017-06-13 15:21:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #ifndef ANDROID_HARDWARE_STREAM_POWER_LOG_H |
| 18 | #define ANDROID_HARDWARE_STREAM_POWER_LOG_H |
| 19 | |
| 20 | #include <audio_utils/clock.h> |
| 21 | #include <audio_utils/PowerLog.h> |
Glenn Kasten | 76a1344 | 2020-07-01 12:10:59 -0700 | [diff] [blame] | 22 | #include <cutils/bitops.h> |
Andy Hung | 953608f | 2017-06-13 15:21:49 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
| 24 | #include <system/audio.h> |
| 25 | |
| 26 | namespace android { |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 27 | namespace CPP_VERSION { |
Andy Hung | 953608f | 2017-06-13 15:21:49 -0700 | [diff] [blame] | 28 | |
| 29 | class StreamPowerLog { |
| 30 | public: |
| 31 | StreamPowerLog() : |
| 32 | mIsUserDebugOrEngBuild(is_userdebug_or_eng_build()), |
| 33 | mPowerLog(nullptr), |
| 34 | mFrameSize(0) { |
| 35 | // use init() to set up the power log. |
| 36 | } |
| 37 | |
| 38 | ~StreamPowerLog() { |
| 39 | power_log_destroy(mPowerLog); // OK for null mPowerLog |
| 40 | mPowerLog = nullptr; |
| 41 | } |
| 42 | |
| 43 | // A one-time initialization (do not call twice) before using StreamPowerLog. |
| 44 | void init(uint32_t sampleRate, audio_channel_mask_t channelMask, audio_format_t format) { |
| 45 | if (mPowerLog == nullptr) { |
| 46 | // Note: A way to get channel count for both input and output channel masks |
| 47 | // but does not check validity of the channel mask. |
| 48 | const uint32_t channelCount = popcount(audio_channel_mask_get_bits(channelMask)); |
| 49 | mFrameSize = channelCount * audio_bytes_per_sample(format); |
| 50 | if (mFrameSize > 0) { |
| 51 | const size_t kPowerLogFramesPerEntry = |
| 52 | (long long)sampleRate * kPowerLogSamplingIntervalMs / 1000; |
| 53 | mPowerLog = power_log_create( |
| 54 | sampleRate, |
| 55 | channelCount, |
| 56 | format, |
| 57 | kPowerLogEntries, |
| 58 | kPowerLogFramesPerEntry); |
| 59 | } |
| 60 | } |
| 61 | // mPowerLog may be NULL (not the right build, format not accepted, etc.). |
| 62 | } |
| 63 | |
| 64 | // Dump the power log to fd. |
| 65 | void dump(int fd) const { |
| 66 | // OK for null mPowerLog |
| 67 | (void)power_log_dump( |
| 68 | mPowerLog, fd, " " /* prefix */, kPowerLogLines, 0 /* limit_ns */); |
| 69 | } |
| 70 | |
| 71 | // Log the audio data contained in buffer. |
| 72 | void log(const void *buffer, size_t sizeInBytes) const { |
| 73 | if (mPowerLog != nullptr) { // mFrameSize is always nonzero if mPowerLog exists. |
| 74 | power_log_log( |
| 75 | mPowerLog, buffer, sizeInBytes / mFrameSize, audio_utils_get_real_time_ns()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | bool isUserDebugOrEngBuild() const { |
| 80 | return mIsUserDebugOrEngBuild; |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | |
| 85 | static inline bool is_userdebug_or_eng_build() { |
| 86 | char value[PROPERTY_VALUE_MAX]; |
| 87 | (void)property_get("ro.build.type", value, "unknown"); // ignore actual length |
| 88 | return strcmp(value, "userdebug") == 0 || strcmp(value, "eng") == 0; |
| 89 | } |
| 90 | |
| 91 | // Audio signal power log configuration. |
| 92 | static const size_t kPowerLogLines = 40; |
| 93 | static const size_t kPowerLogSamplingIntervalMs = 50; |
| 94 | static const size_t kPowerLogEntries = (1 /* minutes */ * 60 /* seconds */ * 1000 /* msec */ |
| 95 | / kPowerLogSamplingIntervalMs); |
| 96 | |
| 97 | const bool mIsUserDebugOrEngBuild; |
| 98 | power_log_t *mPowerLog; |
| 99 | size_t mFrameSize; |
| 100 | }; |
| 101 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 102 | } // namespace CPP_VERSION |
Andy Hung | 953608f | 2017-06-13 15:21:49 -0700 | [diff] [blame] | 103 | } // namespace android |
| 104 | |
| 105 | #endif // ANDROID_HARDWARE_STREAM_POWER_LOG_H |