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