Home | History | Annotate | Download | only in mediaanalytics
      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_codec"
     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 
     34 #include "MediaAnalyticsService.h"
     35 #include "frameworks/base/core/proto/android/stats/mediametrics/mediametrics.pb.h"
     36 #include "iface_statsd.h"
     37 
     38 namespace android {
     39 
     40 bool statsd_codec(MediaAnalyticsItem *item)
     41 {
     42     if (item == NULL) return false;
     43 
     44     // these go into the statsd wrapper
     45     nsecs_t timestamp = item->getTimestamp();
     46     std::string pkgName = item->getPkgName();
     47     int64_t pkgVersionCode = item->getPkgVersionCode();
     48     int64_t mediaApexVersion = 0;
     49 
     50 
     51     // the rest into our own proto
     52     //
     53     ::android::stats::mediametrics::CodecData metrics_proto;
     54 
     55     // flesh out the protobuf we'll hand off with our data
     56     //
     57     // android.media.mediacodec.codec   string
     58     char *codec = NULL;
     59     if (item->getCString("android.media.mediacodec.codec", &codec)) {
     60         metrics_proto.set_codec(codec);
     61     }
     62     // android.media.mediacodec.mime    string
     63     char *mime = NULL;
     64     if (item->getCString("android.media.mediacodec.mime", &mime)) {
     65         metrics_proto.set_mime(mime);
     66     }
     67     // android.media.mediacodec.mode    string
     68     char *mode = NULL;
     69     if ( item->getCString("android.media.mediacodec.mode", &mode)) {
     70         metrics_proto.set_mode(mode);
     71     }
     72     // android.media.mediacodec.encoder int32
     73     int32_t encoder = -1;
     74     if ( item->getInt32("android.media.mediacodec.encoder", &encoder)) {
     75         metrics_proto.set_encoder(encoder);
     76     }
     77     // android.media.mediacodec.secure  int32
     78     int32_t secure = -1;
     79     if ( item->getInt32("android.media.mediacodec.secure", &secure)) {
     80         metrics_proto.set_secure(secure);
     81     }
     82     // android.media.mediacodec.width   int32
     83     int32_t width = -1;
     84     if ( item->getInt32("android.media.mediacodec.width", &width)) {
     85         metrics_proto.set_width(width);
     86     }
     87     // android.media.mediacodec.height  int32
     88     int32_t height = -1;
     89     if ( item->getInt32("android.media.mediacodec.height", &height)) {
     90         metrics_proto.set_height(height);
     91     }
     92     // android.media.mediacodec.rotation-degrees        int32
     93     int32_t rotation = -1;
     94     if ( item->getInt32("android.media.mediacodec.rotation-degrees", &rotation)) {
     95         metrics_proto.set_rotation(rotation);
     96     }
     97     // android.media.mediacodec.crypto  int32 (although missing if not needed
     98     int32_t crypto = -1;
     99     if ( item->getInt32("android.media.mediacodec.crypto", &crypto)) {
    100         metrics_proto.set_crypto(crypto);
    101     }
    102     // android.media.mediacodec.profile int32
    103     int32_t profile = -1;
    104     if ( item->getInt32("android.media.mediacodec.profile", &profile)) {
    105         metrics_proto.set_profile(profile);
    106     }
    107     // android.media.mediacodec.level   int32
    108     int32_t level = -1;
    109     if ( item->getInt32("android.media.mediacodec.level", &level)) {
    110         metrics_proto.set_level(level);
    111     }
    112     // android.media.mediacodec.maxwidth        int32
    113     int32_t maxwidth = -1;
    114     if ( item->getInt32("android.media.mediacodec.maxwidth", &maxwidth)) {
    115         metrics_proto.set_max_width(maxwidth);
    116     }
    117     // android.media.mediacodec.maxheight       int32
    118     int32_t maxheight = -1;
    119     if ( item->getInt32("android.media.mediacodec.maxheight", &maxheight)) {
    120         metrics_proto.set_max_height(maxheight);
    121     }
    122     // android.media.mediacodec.errcode         int32
    123     int32_t errcode = -1;
    124     if ( item->getInt32("android.media.mediacodec.errcode", &errcode)) {
    125         metrics_proto.set_error_code(errcode);
    126     }
    127     // android.media.mediacodec.errstate        string
    128     char *errstate = NULL;
    129     if ( item->getCString("android.media.mediacodec.errstate", &errstate)) {
    130         metrics_proto.set_error_state(errstate);
    131     }
    132     // android.media.mediacodec.latency.max  int64
    133     int64_t latency_max = -1;
    134     if ( item->getInt64("android.media.mediacodec.latency.max", &latency_max)) {
    135         metrics_proto.set_latency_max(latency_max);
    136     }
    137     // android.media.mediacodec.latency.min  int64
    138     int64_t latency_min = -1;
    139     if ( item->getInt64("android.media.mediacodec.latency.min", &latency_min)) {
    140         metrics_proto.set_latency_min(latency_min);
    141     }
    142     // android.media.mediacodec.latency.avg  int64
    143     int64_t latency_avg = -1;
    144     if ( item->getInt64("android.media.mediacodec.latency.avg", &latency_avg)) {
    145         metrics_proto.set_latency_avg(latency_avg);
    146     }
    147     // android.media.mediacodec.latency.n    int64
    148     int64_t latency_count = -1;
    149     if ( item->getInt64("android.media.mediacodec.latency.n", &latency_count)) {
    150         metrics_proto.set_latency_count(latency_count);
    151     }
    152     // android.media.mediacodec.latency.unknown    int64
    153     int64_t latency_unknown = -1;
    154     if ( item->getInt64("android.media.mediacodec.latency.unknown", &latency_unknown)) {
    155         metrics_proto.set_latency_unknown(latency_unknown);
    156     }
    157     // android.media.mediacodec.latency.hist    NOT EMITTED
    158 
    159     std::string serialized;
    160     if (!metrics_proto.SerializeToString(&serialized)) {
    161         ALOGE("Failed to serialize codec metrics");
    162         return false;
    163     }
    164 
    165     if (enabled_statsd) {
    166         android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
    167         (void)android::util::stats_write(android::util::MEDIAMETRICS_CODEC_REPORTED,
    168                                    timestamp, pkgName.c_str(), pkgVersionCode,
    169                                    mediaApexVersion,
    170                                    bf_serialized);
    171 
    172     } else {
    173         ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str()));
    174     }
    175 
    176     // must free the strings that we were given
    177     free(codec);
    178     free(mime);
    179     free(mode);
    180     free(errstate);
    181 
    182     return true;
    183 }
    184 
    185 };
    186