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