Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -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 "statsd_audiotrack" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <dirent.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <pthread.h> |
| 24 | #include <pwd.h> |
| 25 | #include <stdint.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <statslog.h> |
| 33 | |
Ray Essick | 40e8e5e | 2019-12-05 20:19:40 -0800 | [diff] [blame] | 34 | #include "MediaMetricsService.h" |
Andy Hung | cbcfaa2 | 2021-02-23 13:54:49 -0800 | [diff] [blame] | 35 | #include "StringUtils.h" |
Jeffrey Huang | 6adacdb | 2020-11-25 02:49:32 -0800 | [diff] [blame] | 36 | #include "frameworks/proto_logging/stats/enums/stats/mediametrics/mediametrics.pb.h" |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 37 | #include "iface_statsd.h" |
| 38 | |
| 39 | namespace android { |
| 40 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 41 | bool statsd_audiotrack(const std::shared_ptr<const mediametrics::Item>& item, |
| 42 | const std::shared_ptr<mediametrics::StatsdLog>& statsdLog) |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 43 | { |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 44 | if (item == nullptr) return false; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 45 | |
| 46 | // these go into the statsd wrapper |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 47 | const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp()); |
| 48 | const std::string package_name = item->getPkgName(); |
| 49 | const int64_t package_version_code = item->getPkgVersionCode(); |
| 50 | const int64_t media_apex_version = 0; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 51 | |
| 52 | // the rest into our own proto |
| 53 | // |
| 54 | ::android::stats::mediametrics::AudioTrackData metrics_proto; |
| 55 | |
| 56 | // flesh out the protobuf we'll hand off with our data |
| 57 | // |
| 58 | |
| 59 | // static constexpr char kAudioTrackStreamType[] = "android.media.audiotrack.streamtype"; |
| 60 | // optional string streamType; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 61 | std::string stream_type; |
| 62 | if (item->getString("android.media.audiotrack.streamtype", &stream_type)) { |
| 63 | metrics_proto.set_stream_type(stream_type); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // static constexpr char kAudioTrackContentType[] = "android.media.audiotrack.type"; |
| 67 | // optional string contentType; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 68 | std::string content_type; |
| 69 | if (item->getString("android.media.audiotrack.type", &content_type)) { |
| 70 | metrics_proto.set_content_type(content_type); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // static constexpr char kAudioTrackUsage[] = "android.media.audiotrack.usage"; |
| 74 | // optional string trackUsage; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 75 | std::string track_usage; |
| 76 | if (item->getString("android.media.audiotrack.usage", &track_usage)) { |
| 77 | metrics_proto.set_track_usage(track_usage); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // static constexpr char kAudioTrackSampleRate[] = "android.media.audiotrack.samplerate"; |
| 81 | // optional int32 samplerate; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 82 | int32_t sample_rate = -1; |
| 83 | if (item->getInt32("android.media.audiotrack.samplerate", &sample_rate)) { |
| 84 | metrics_proto.set_sample_rate(sample_rate); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // static constexpr char kAudioTrackChannelMask[] = "android.media.audiotrack.channelmask"; |
| 88 | // optional int64 channelMask; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 89 | int64_t channel_mask = -1; |
| 90 | if (item->getInt64("android.media.audiotrack.channelmask", &channel_mask)) { |
| 91 | metrics_proto.set_channel_mask(channel_mask); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // NB: These are not yet exposed as public Java API constants. |
| 95 | // static constexpr char kAudioTrackUnderrunFrames[] = "android.media.audiotrack.underrunframes"; |
| 96 | // optional int32 underrunframes; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 97 | int32_t underrun_frames = -1; |
| 98 | if (item->getInt32("android.media.audiotrack.underrunframes", &underrun_frames)) { |
| 99 | metrics_proto.set_underrun_frames(underrun_frames); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // static constexpr char kAudioTrackStartupGlitch[] = "android.media.audiotrack.glitch.startup"; |
| 103 | // optional int32 startupglitch; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 104 | int32_t startup_glitch = -1; |
| 105 | if (item->getInt32("android.media.audiotrack.glitch.startup", &startup_glitch)) { |
| 106 | metrics_proto.set_startup_glitch(startup_glitch); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // portId (int32) |
| 110 | int32_t port_id = -1; |
| 111 | if (item->getInt32("android.media.audiotrack.portId", &port_id)) { |
| 112 | metrics_proto.set_port_id(port_id); |
| 113 | } |
| 114 | // encoding (string) |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 115 | std::string encoding; |
| 116 | if (item->getString("android.media.audiotrack.encoding", &encoding)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 117 | metrics_proto.set_encoding(encoding); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 118 | } |
| 119 | // frameCount (int32) |
| 120 | int32_t frame_count = -1; |
| 121 | if (item->getInt32("android.media.audiotrack.frameCount", &frame_count)) { |
| 122 | metrics_proto.set_frame_count(frame_count); |
| 123 | } |
| 124 | // attributes (string) |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 125 | std::string attributes; |
| 126 | if (item->getString("android.media.audiotrack.attributes", &attributes)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 127 | metrics_proto.set_attributes(attributes); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | std::string serialized; |
| 131 | if (!metrics_proto.SerializeToString(&serialized)) { |
| 132 | ALOGE("Failed to serialize audiotrack metrics"); |
| 133 | return false; |
| 134 | } |
| 135 | |
Andy Hung | cbcfaa2 | 2021-02-23 13:54:49 -0800 | [diff] [blame] | 136 | // Android S |
| 137 | // log_session_id (string) |
| 138 | std::string logSessionId; |
| 139 | (void)item->getString("android.media.audiotrack.logSessionId", &logSessionId); |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 140 | const auto log_session_id = |
Andy Hung | cbcfaa2 | 2021-02-23 13:54:49 -0800 | [diff] [blame] | 141 | mediametrics::stringutils::sanitizeLogSessionId(logSessionId); |
| 142 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 143 | android::util::BytesField bf_serialized( serialized.c_str(), serialized.size()); |
| 144 | int result = android::util::stats_write(android::util::MEDIAMETRICS_AUDIOTRACK_REPORTED, |
| 145 | timestamp_nanos, package_name.c_str(), package_version_code, |
| 146 | media_apex_version, |
| 147 | bf_serialized, |
| 148 | log_session_id.c_str()); |
| 149 | std::stringstream log; |
| 150 | log << "result:" << result << " {" |
| 151 | << " mediametrics_audiotrack_reported:" |
| 152 | << android::util::MEDIAMETRICS_AUDIOTRACK_REPORTED |
| 153 | << " timestamp_nanos:" << timestamp_nanos |
| 154 | << " package_name:" << package_name |
| 155 | << " package_version_code:" << package_version_code |
| 156 | << " media_apex_version:" << media_apex_version |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 157 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 158 | << " stream_type:" << stream_type |
| 159 | << " content_type:" << content_type |
| 160 | << " track_usage:" << track_usage |
| 161 | << " sample_rate:" << sample_rate |
| 162 | << " channel_mask:" << channel_mask |
| 163 | << " underrun_frames:" << underrun_frames |
| 164 | << " startup_glitch:" << startup_glitch |
| 165 | << " port_id:" << port_id |
| 166 | << " encoding:" << encoding |
| 167 | << " frame_count:" << frame_count |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 168 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 169 | << " attributes:" << attributes |
| 170 | |
| 171 | << " log_session_id:" << log_session_id |
| 172 | << " }"; |
| 173 | statsdLog->log(android::util::MEDIAMETRICS_AUDIOTRACK_REPORTED, log.str()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 177 | } // namespace android |