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 "iface_statsd"
19 #include <utils/Log.h>
20 
21 #include <stdint.h>
22 #include <inttypes.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <dirent.h>
27 #include <pthread.h>
28 #include <unistd.h>
29 
30 #include <memory>
31 #include <string.h>
32 #include <pwd.h>
33 
34 #include "MediaMetricsService.h"
35 #include "iface_statsd.h"
36 
37 #include <statslog.h>
38 
39 namespace android {
40 
41 // set of routines that crack a mediametrics::Item
42 // and send it off to statsd with the appropriate hooks
43 //
44 // each mediametrics::Item type (extractor, codec, nuplayer, etc)
45 // has its own routine to handle this.
46 //
47 
48 bool enabled_statsd = true;
49 
50 struct statsd_hooks {
51     const char *key;
52     bool (*handler)(const mediametrics::Item *);
53 };
54 
55 // keep this sorted, so we can do binary searches
56 static constexpr struct statsd_hooks statsd_handlers[] =
57 {
58     { "audiopolicy", statsd_audiopolicy },
59     { "audiorecord", statsd_audiorecord },
60     { "audiothread", statsd_audiothread },
61     { "audiotrack", statsd_audiotrack },
62     { "codec", statsd_codec},
63     { "drm.vendor.Google.WidevineCDM", statsd_widevineCDM },
64     { "drmmanager", statsd_drmmanager },
65     { "extractor", statsd_extractor },
66     { "mediadrm", statsd_mediadrm },
67     { "nuplayer", statsd_nuplayer },
68     { "nuplayer2", statsd_nuplayer },
69     { "recorder", statsd_recorder },
70 };
71 
72 // give me a record, i'll look at the type and upload appropriately
dump2Statsd(const std::shared_ptr<const mediametrics::Item> & item)73 bool dump2Statsd(const std::shared_ptr<const mediametrics::Item>& item) {
74     if (item == nullptr) return false;
75 
76     // get the key
77     std::string key = item->getKey();
78 
79     if (!enabled_statsd) {
80         ALOGV("statsd logging disabled for record key=%s", key.c_str());
81         return false;
82     }
83 
84     for (const auto &statsd_handler : statsd_handlers) {
85         if (key == statsd_handler.key) {
86             return statsd_handler.handler(item.get());
87         }
88     }
89     return false;
90 }
91 
92 } // namespace android
93