Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "AudioAnalytics" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "AudioAnalytics.h" |
| 22 | |
| 23 | #include <audio_utils/clock.h> // clock conversions |
| 24 | |
| 25 | namespace android::mediametrics { |
| 26 | |
| 27 | AudioAnalytics::AudioAnalytics() |
| 28 | { |
| 29 | ALOGD("%s", __func__); |
Andy Hung | 0f7ad8c | 2020-01-03 13:24:34 -0800 | [diff] [blame] | 30 | |
| 31 | // Add action to save AnalyticsState if audioserver is restarted. |
| 32 | // This triggers on an item of "audio.flinger" |
| 33 | // with a property "event" set to "AudioFlinger" (the constructor). |
| 34 | mActions.addAction( |
Andy Hung | ea186fa | 2020-01-09 18:13:15 -0800 | [diff] [blame] | 35 | AMEDIAMETRICS_KEY_AUDIO_FLINGER "." AMEDIAMETRICS_PROP_EVENT, |
| 36 | std::string(AMEDIAMETRICS_PROP_EVENT_VALUE_CTOR), |
Andy Hung | 0f7ad8c | 2020-01-03 13:24:34 -0800 | [diff] [blame] | 37 | std::make_shared<AnalyticsActions::Function>( |
Andy Hung | ea186fa | 2020-01-09 18:13:15 -0800 | [diff] [blame] | 38 | [this](const std::shared_ptr<const android::mediametrics::Item> &item){ |
| 39 | ALOGW("(key=%s) Audioflinger constructor event detected", item->getKey().c_str()); |
Andy Hung | 0f7ad8c | 2020-01-03 13:24:34 -0800 | [diff] [blame] | 40 | mPreviousAnalyticsState.set(std::make_shared<AnalyticsState>( |
| 41 | *mAnalyticsState.get())); |
| 42 | // Note: get returns shared_ptr temp, whose lifetime is extended |
| 43 | // to end of full expression. |
| 44 | mAnalyticsState->clear(); // TODO: filter the analytics state. |
Andy Hung | ea186fa | 2020-01-09 18:13:15 -0800 | [diff] [blame] | 45 | // Perhaps report this. |
| 46 | })); |
| 47 | |
| 48 | // Check underruns |
| 49 | mActions.addAction( |
| 50 | AMEDIAMETRICS_KEY_PREFIX_AUDIO_THREAD "*." AMEDIAMETRICS_PROP_EVENT, |
| 51 | std::string(AMEDIAMETRICS_PROP_EVENT_VALUE_UNDERRUN), |
| 52 | std::make_shared<AnalyticsActions::Function>( |
| 53 | [this](const std::shared_ptr<const android::mediametrics::Item> &item){ |
| 54 | std::string threadId = item->getKey().substr( |
| 55 | sizeof(AMEDIAMETRICS_KEY_PREFIX_AUDIO_THREAD) - 1); |
| 56 | std::string outputDevices; |
| 57 | mAnalyticsState->timeMachine().get( |
| 58 | item->getKey(), AMEDIAMETRICS_PROP_OUTPUTDEVICES, &outputDevices); |
| 59 | ALOGD("(key=%s) Thread underrun event detected on io handle:%s device:%s", |
| 60 | item->getKey().c_str(), threadId.c_str(), outputDevices.c_str()); |
| 61 | if (outputDevices.find("AUDIO_DEVICE_OUT_BLUETOOTH") != std::string::npos) { |
| 62 | // report this for Bluetooth |
| 63 | } |
| 64 | })); |
| 65 | |
| 66 | // Check latencies, playback and startup |
| 67 | mActions.addAction( |
| 68 | AMEDIAMETRICS_KEY_PREFIX_AUDIO_TRACK "*." AMEDIAMETRICS_PROP_LATENCYMS, |
| 69 | std::monostate{}, // accept any value |
| 70 | std::make_shared<AnalyticsActions::Function>( |
| 71 | [this](const std::shared_ptr<const android::mediametrics::Item> &item){ |
| 72 | double latencyMs{}; |
| 73 | double startupMs{}; |
| 74 | if (!item->get(AMEDIAMETRICS_PROP_LATENCYMS, &latencyMs) |
| 75 | || !item->get(AMEDIAMETRICS_PROP_STARTUPMS, &startupMs)) return; |
| 76 | |
| 77 | std::string trackId = item->getKey().substr( |
| 78 | sizeof(AMEDIAMETRICS_KEY_PREFIX_AUDIO_TRACK) - 1); |
| 79 | std::string thread = getThreadFromTrack(item->getKey()); |
| 80 | std::string outputDevices; |
| 81 | mAnalyticsState->timeMachine().get( |
| 82 | thread, AMEDIAMETRICS_PROP_OUTPUTDEVICES, &outputDevices); |
| 83 | ALOGD("(key=%s) Track latencyMs:%lf startupMs:%lf detected on port:%s device:%s", |
| 84 | item->getKey().c_str(), latencyMs, startupMs, |
| 85 | trackId.c_str(), outputDevices.c_str()); |
| 86 | if (outputDevices.find("AUDIO_DEVICE_OUT_BLUETOOTH") != std::string::npos) { |
| 87 | // report this for Bluetooth |
| 88 | } |
Andy Hung | 0f7ad8c | 2020-01-03 13:24:34 -0800 | [diff] [blame] | 89 | })); |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | AudioAnalytics::~AudioAnalytics() |
| 93 | { |
| 94 | ALOGD("%s", __func__); |
| 95 | } |
| 96 | |
| 97 | status_t AudioAnalytics::submit( |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 98 | const std::shared_ptr<const mediametrics::Item>& item, bool isTrusted) |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 99 | { |
Andy Hung | ea186fa | 2020-01-09 18:13:15 -0800 | [diff] [blame] | 100 | if (!startsWith(item->getKey(), AMEDIAMETRICS_KEY_PREFIX_AUDIO)) return BAD_VALUE; |
Andy Hung | 0f7ad8c | 2020-01-03 13:24:34 -0800 | [diff] [blame] | 101 | status_t status = mAnalyticsState->submit(item, isTrusted); |
| 102 | if (status != NO_ERROR) return status; // may not be permitted. |
| 103 | |
| 104 | // Only if the item was successfully submitted (permission) |
| 105 | // do we check triggered actions. |
| 106 | checkActions(item); |
| 107 | return NO_ERROR; |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Andy Hung | 709b91e | 2020-04-04 14:23:36 -0700 | [diff] [blame] | 110 | std::pair<std::string, int32_t> AudioAnalytics::dump( |
| 111 | int32_t lines, int64_t sinceNs, const char *prefix) const |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 112 | { |
| 113 | std::stringstream ss; |
| 114 | int32_t ll = lines; |
| 115 | |
| 116 | if (ll > 0) { |
Andy Hung | 709b91e | 2020-04-04 14:23:36 -0700 | [diff] [blame] | 117 | auto [s, l] = mAnalyticsState->dump(ll, sinceNs, prefix); |
Andy Hung | b744faf | 2020-04-09 13:09:26 -0700 | [diff] [blame] | 118 | ss << s; |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 119 | ll -= l; |
| 120 | } |
| 121 | if (ll > 0) { |
Andy Hung | 0f7ad8c | 2020-01-03 13:24:34 -0800 | [diff] [blame] | 122 | ss << "Prior audioserver state:\n"; |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 123 | --ll; |
| 124 | } |
| 125 | if (ll > 0) { |
Andy Hung | 709b91e | 2020-04-04 14:23:36 -0700 | [diff] [blame] | 126 | auto [s, l] = mPreviousAnalyticsState->dump(ll, sinceNs, prefix); |
Andy Hung | b744faf | 2020-04-09 13:09:26 -0700 | [diff] [blame] | 127 | ss << s; |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 128 | ll -= l; |
| 129 | } |
| 130 | return { ss.str(), lines - ll }; |
| 131 | } |
| 132 | |
Andy Hung | 0f7ad8c | 2020-01-03 13:24:34 -0800 | [diff] [blame] | 133 | void AudioAnalytics::checkActions(const std::shared_ptr<const mediametrics::Item>& item) |
| 134 | { |
| 135 | auto actions = mActions.getActionsForItem(item); // internally locked. |
| 136 | // Execute actions with no lock held. |
| 137 | for (const auto& action : actions) { |
| 138 | (*action)(item); |
| 139 | } |
| 140 | } |
| 141 | |
Andy Hung | ea186fa | 2020-01-09 18:13:15 -0800 | [diff] [blame] | 142 | // HELPER METHODS |
| 143 | |
| 144 | std::string AudioAnalytics::getThreadFromTrack(const std::string& track) const |
| 145 | { |
| 146 | int32_t threadId_int32{}; |
| 147 | if (mAnalyticsState->timeMachine().get( |
| 148 | track, AMEDIAMETRICS_PROP_THREADID, &threadId_int32) != NO_ERROR) { |
| 149 | return {}; |
| 150 | } |
| 151 | return std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_THREAD) + std::to_string(threadId_int32); |
| 152 | } |
| 153 | |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 154 | } // namespace android |