John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "PluginMetricsReporting" |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 19 | |
| 20 | #include <media/PluginMetricsReporting.h> |
| 21 | |
Adam Stone | 32494f5 | 2018-02-26 22:53:27 -0800 | [diff] [blame] | 22 | #include <inttypes.h> |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 23 | |
Robert Shih | 82ea6be | 2019-11-07 17:47:23 -0800 | [diff] [blame] | 24 | #include <media/MediaMetrics.h> |
Adam Stone | 32494f5 | 2018-02-26 22:53:27 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | namespace { |
| 31 | |
Adam Stone | 32494f5 | 2018-02-26 22:53:27 -0800 | [diff] [blame] | 32 | constexpr char kSerializedMetricsField[] = "serialized_metrics"; |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 33 | |
Adam Stone | 32494f5 | 2018-02-26 22:53:27 -0800 | [diff] [blame] | 34 | status_t reportVendorMetrics(const std::string& metrics, |
| 35 | const String8& name, |
Robert Shih | 82ea6be | 2019-11-07 17:47:23 -0800 | [diff] [blame] | 36 | uid_t appUid) { |
| 37 | mediametrics_handle_t analyticsItem(mediametrics_create(name.c_str())); |
| 38 | mediametrics_setUid(analyticsItem, appUid); |
Adam Stone | 32494f5 | 2018-02-26 22:53:27 -0800 | [diff] [blame] | 39 | if (metrics.size() > 0) { |
Robert Shih | 82ea6be | 2019-11-07 17:47:23 -0800 | [diff] [blame] | 40 | mediametrics_setCString(analyticsItem, kSerializedMetricsField, metrics.c_str()); |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Robert Shih | 82ea6be | 2019-11-07 17:47:23 -0800 | [diff] [blame] | 43 | if (!mediametrics_selfRecord(analyticsItem)) { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 44 | ALOGE("%s: selfrecord() returned false", __func__); |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Robert Shih | 82ea6be | 2019-11-07 17:47:23 -0800 | [diff] [blame] | 47 | mediametrics_delete(analyticsItem); |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 48 | return OK; |
| 49 | } |
| 50 | |
| 51 | String8 sanitize(const String8& input) { |
| 52 | // Filters the input string down to just alphanumeric characters. |
| 53 | String8 output; |
| 54 | for (size_t i = 0; i < input.size(); ++i) { |
| 55 | char candidate = input[i]; |
| 56 | if ((candidate >= 'a' && candidate <= 'z') || |
| 57 | (candidate >= 'A' && candidate <= 'Z') || |
| 58 | (candidate >= '0' && candidate <= '9')) { |
| 59 | output.append(&candidate, 1); |
| 60 | } |
| 61 | } |
| 62 | return output; |
| 63 | } |
| 64 | |
| 65 | } // namespace |
| 66 | |
Adam Stone | 32494f5 | 2018-02-26 22:53:27 -0800 | [diff] [blame] | 67 | status_t reportDrmPluginMetrics(const std::string& b64EncodedMetrics, |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 68 | const String8& vendor, |
Adam Stone | 32494f5 | 2018-02-26 22:53:27 -0800 | [diff] [blame] | 69 | const String8& description, |
Robert Shih | 82ea6be | 2019-11-07 17:47:23 -0800 | [diff] [blame] | 70 | uid_t appUid) { |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 71 | |
| 72 | String8 name = String8::format("drm.vendor.%s.%s", |
| 73 | sanitize(vendor).c_str(), |
| 74 | sanitize(description).c_str()); |
| 75 | |
Robert Shih | 82ea6be | 2019-11-07 17:47:23 -0800 | [diff] [blame] | 76 | return reportVendorMetrics(b64EncodedMetrics, name, appUid); |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } // namespace android |