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