Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3435b897 authored by Ray Essick's avatar Ray Essick Committed by Android (Google) Code Review
Browse files

Merge "additional codec-related media metrics" into sc-dev

parents 259b21a4 87913314
Loading
Loading
Loading
Loading
+63 −6
Original line number Diff line number Diff line
@@ -118,6 +118,11 @@ static const char *kCodecQueueInputBufferError = "android.media.mediacodec.queue
static const char *kCodecNumLowLatencyModeOn = "android.media.mediacodec.low-latency.on";  /* 0..n */
static const char *kCodecNumLowLatencyModeOff = "android.media.mediacodec.low-latency.off";  /* 0..n */
static const char *kCodecFirstFrameIndexLowLatencyModeOn = "android.media.mediacodec.low-latency.first-frame";  /* 0..n */
static const char *kCodecChannelCount = "android.media.mediacodec.channelCount";
static const char *kCodecSampleRate = "android.media.mediacodec.sampleRate";
static const char *kCodecVideoEncodedBytes = "android.media.mediacodec.vencode.bytes";
static const char *kCodecVideoEncodedFrames = "android.media.mediacodec.vencode.frames";
static const char *kCodecVideoEncodedDurationUs = "android.media.mediacodec.vencode.durationUs";

// the kCodecRecent* fields appear only in getMetrics() results
static const char *kCodecRecentLatencyMax = "android.media.mediacodec.recent.max";      /* in us */
@@ -695,6 +700,10 @@ MediaCodec::MediaCodec(
      mHavePendingInputBuffers(false),
      mCpuBoostRequested(false),
      mLatencyUnknown(0),
      mBytesEncoded(0),
      mEarliestEncodedPtsUs(INT64_MAX),
      mLatestEncodedPtsUs(INT64_MIN),
      mFramesEncoded(0),
      mNumLowLatencyEnables(0),
      mNumLowLatencyDisables(0),
      mIsLowLatencyModeOn(false),
@@ -802,6 +811,18 @@ void MediaCodec::updateMediametrics() {
        mediametrics_setInt64(mMetricsHandle, kCodecLifetimeMs, lifetime);
    }

    if (mBytesEncoded) {
        Mutex::Autolock al(mOutputStatsLock);

        mediametrics_setInt64(mMetricsHandle, kCodecVideoEncodedBytes, mBytesEncoded);
        int64_t duration = 0;
        if (mLatestEncodedPtsUs > mEarliestEncodedPtsUs) {
            duration = mLatestEncodedPtsUs - mEarliestEncodedPtsUs;
        }
        mediametrics_setInt64(mMetricsHandle, kCodecVideoEncodedDurationUs, duration);
        mediametrics_setInt64(mMetricsHandle, kCodecVideoEncodedFrames, mFramesEncoded);
    }

    {
        Mutex::Autolock al(mLatencyLock);
        mediametrics_setInt64(mMetricsHandle, kCodecNumLowLatencyModeOn, mNumLowLatencyEnables);
@@ -1005,10 +1026,34 @@ void MediaCodec::statsBufferSent(int64_t presentationUs) {
}

// when we get a buffer back from the codec
void MediaCodec::statsBufferReceived(int64_t presentationUs) {
void MediaCodec::statsBufferReceived(int64_t presentationUs, const sp<MediaCodecBuffer> &buffer) {

    CHECK_NE(mState, UNINITIALIZED);

    if (mIsVideo && (mFlags & kFlagIsEncoder)) {
        int32_t flags = 0;
        (void) buffer->meta()->findInt32("flags", &flags);

        // some of these frames, we don't want to count
        // standalone EOS.... has an invalid timestamp
        if ((flags & (BUFFER_FLAG_CODECCONFIG|BUFFER_FLAG_EOS)) == 0) {
            mBytesEncoded += buffer->size();
            mFramesEncoded++;

            Mutex::Autolock al(mOutputStatsLock);
            int64_t timeUs = 0;
            if (buffer->meta()->findInt64("timeUs", &timeUs)) {
                if (timeUs > mLatestEncodedPtsUs) {
                    mLatestEncodedPtsUs = timeUs;
                }
                // can't chain as an else-if or this never triggers
                if (timeUs < mEarliestEncodedPtsUs) {
                    mEarliestEncodedPtsUs = timeUs;
                }
            }
        }
    }

    // mutex access to mBuffersInFlight and other stats
    Mutex::Autolock al(mLatencyLock);

@@ -1064,7 +1109,7 @@ void MediaCodec::statsBufferReceived(int64_t presentationUs) {
        return;
    }

    // nowNs start our calculations
    // now start our calculations
    const int64_t nowNs = systemTime(SYSTEM_TIME_MONOTONIC);
    int64_t latencyUs = (nowNs - startdata.startedNs + 500) / 1000;

@@ -1337,6 +1382,17 @@ status_t MediaCodec::configure(
            ALOGE("Invalid size(s), width=%d, height=%d", mVideoWidth, mVideoHeight);
            return BAD_VALUE;
        }
    } else {
        if (mMetricsHandle != 0) {
            int32_t channelCount;
            if (format->findInt32(KEY_CHANNEL_COUNT, &channelCount)) {
                mediametrics_setInt32(mMetricsHandle, kCodecChannelCount, channelCount);
            }
            int32_t sampleRate;
            if (format->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
                mediametrics_setInt32(mMetricsHandle, kCodecSampleRate, sampleRate);
            }
        }
    }

    updateLowLatency(format);
@@ -2183,14 +2239,15 @@ bool MediaCodec::handleDequeueOutputBuffer(const sp<AReplyToken> &replyID, bool
        int64_t timeUs;
        CHECK(buffer->meta()->findInt64("timeUs", &timeUs));

        statsBufferReceived(timeUs);

        response->setInt64("timeUs", timeUs);

        int32_t flags;
        CHECK(buffer->meta()->findInt32("flags", &flags));

        response->setInt32("flags", flags);

        statsBufferReceived(timeUs, buffer);

        response->postReply(replyID);
    }

@@ -4337,13 +4394,13 @@ void MediaCodec::onOutputBufferAvailable() {

        msg->setInt64("timeUs", timeUs);

        statsBufferReceived(timeUs);

        int32_t flags;
        CHECK(buffer->meta()->findInt32("flags", &flags));

        msg->setInt32("flags", flags);

        statsBufferReceived(timeUs, buffer);

        msg->post();
    }
}
+9 −1
Original line number Diff line number Diff line
@@ -528,6 +528,14 @@ private:
    std::deque<BufferFlightTiming_t> mBuffersInFlight;
    Mutex mLatencyLock;
    int64_t mLatencyUnknown;    // buffers for which we couldn't calculate latency

    Mutex mOutputStatsLock;
    int64_t mBytesEncoded = 0;
    int64_t mEarliestEncodedPtsUs = INT64_MAX;
    int64_t mLatestEncodedPtsUs = INT64_MIN;
    int32_t mFramesEncoded = 0;


    int64_t mNumLowLatencyEnables;  // how many times low latency mode is enabled
    int64_t mNumLowLatencyDisables;  // how many times low latency mode is disabled
    bool mIsLowLatencyModeOn;  // is low latency mode on currently
@@ -544,7 +552,7 @@ private:
    sp<BatteryChecker> mBatteryChecker;

    void statsBufferSent(int64_t presentationUs);
    void statsBufferReceived(int64_t presentationUs);
    void statsBufferReceived(int64_t presentationUs, const sp<MediaCodecBuffer> &buffer);

    enum {
        // the default shape of our latency histogram buckets
+10 −0
Original line number Diff line number Diff line
@@ -186,6 +186,16 @@ bool statsd_codec(const mediametrics::Item *item)
        metrics_proto.set_lifetime_millis(lifetimeMs);
    }

    // new for S; need to plumb through to westworld
    // android.media.mediacodec.channelCount int32
    // android.media.mediacodec.sampleRate int32

    // new for S; need to plumb through to westworld
    // TODO PWG may want these fuzzed up a bit to obscure some precision
    // android.media.mediacodec.vencode.bytes int64
    // android.media.mediacodec.vencode.frames int64
    // android.media.mediacodec.vencode.durationUs int64

    std::string serialized;
    if (!metrics_proto.SerializeToString(&serialized)) {
        ALOGE("Failed to serialize codec metrics");