summaryrefslogtreecommitdiffstats
path: root/tests/Camera2Tests/SmartCamera/SimpleCamera/src/androidx/media/filterfw/ThroughputFilter.java
blob: 25243a76e5f6dc8139e4e9753f5ebe8b6b493827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package androidx.media.filterpacks.performance;

import android.util.Log;
import android.os.SystemClock;

import androidx.media.filterfw.*;

public class ThroughputFilter extends Filter {

    private int mPeriod = 3;
    private long mLastTime = 0;
    private int mTotalFrameCount = 0;
    private int mPeriodFrameCount = 0;

    public ThroughputFilter(MffContext context, String name) {
        super(context, name);
    }


    @Override
    public Signature getSignature() {
        FrameType throughputType = FrameType.single(Throughput.class);
        return new Signature()
            .addInputPort("frame", Signature.PORT_REQUIRED, FrameType.any())
            .addOutputPort("throughput", Signature.PORT_REQUIRED, throughputType)
            .addOutputPort("frame", Signature.PORT_REQUIRED, FrameType.any())
            .addInputPort("period", Signature.PORT_OPTIONAL, FrameType.single(int.class))
            .disallowOtherPorts();
    }

    @Override
    public void onInputPortOpen(InputPort port) {
        if (port.getName().equals("period")) {
            port.bindToFieldNamed("mPeriod");
        } else {
            port.attachToOutputPort(getConnectedOutputPort("frame"));
        }
    }

    @Override
    protected void onOpen() {
        mTotalFrameCount = 0;
        mPeriodFrameCount = 0;
        mLastTime = 0;
    }

    @Override
    protected synchronized void onProcess() {
        Frame inputFrame = getConnectedInputPort("frame").pullFrame();

        // Update stats
        ++mTotalFrameCount;
        ++mPeriodFrameCount;

        // Check clock
        if (mLastTime == 0) {
            mLastTime = SystemClock.elapsedRealtime();
        }
        long curTime = SystemClock.elapsedRealtime();

        // Output throughput info if time period is up
        if ((curTime - mLastTime) >= (mPeriod * 1000)) {
            Log.i("Thru", "It is time!");
            OutputPort tpPort = getConnectedOutputPort("throughput");
            Throughput throughput = new Throughput(mTotalFrameCount,
                                                   mPeriodFrameCount,
                                                   curTime - mLastTime,
                                                   inputFrame.getElementCount());
            FrameValue throughputFrame = tpPort.fetchAvailableFrame(null).asFrameValue();
            throughputFrame.setValue(throughput);
            tpPort.pushFrame(throughputFrame);
            mLastTime = curTime;
            mPeriodFrameCount = 0;
        }

        getConnectedOutputPort("frame").pushFrame(inputFrame);
    }
}