Home | History | Annotate | Download | only in filterfw
      1 /*
      2  * Copyright (C) 2011 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 
     18 package androidx.media.filterpacks.performance;
     19 
     20 import android.util.Log;
     21 import android.os.SystemClock;
     22 
     23 import androidx.media.filterfw.*;
     24 
     25 public class ThroughputFilter extends Filter {
     26 
     27     private int mPeriod = 3;
     28     private long mLastTime = 0;
     29     private int mTotalFrameCount = 0;
     30     private int mPeriodFrameCount = 0;
     31 
     32     public ThroughputFilter(MffContext context, String name) {
     33         super(context, name);
     34     }
     35 
     36 
     37     @Override
     38     public Signature getSignature() {
     39         FrameType throughputType = FrameType.single(Throughput.class);
     40         return new Signature()
     41             .addInputPort("frame", Signature.PORT_REQUIRED, FrameType.any())
     42             .addOutputPort("throughput", Signature.PORT_REQUIRED, throughputType)
     43             .addOutputPort("frame", Signature.PORT_REQUIRED, FrameType.any())
     44             .addInputPort("period", Signature.PORT_OPTIONAL, FrameType.single(int.class))
     45             .disallowOtherPorts();
     46     }
     47 
     48     @Override
     49     public void onInputPortOpen(InputPort port) {
     50         if (port.getName().equals("period")) {
     51             port.bindToFieldNamed("mPeriod");
     52         } else {
     53             port.attachToOutputPort(getConnectedOutputPort("frame"));
     54         }
     55     }
     56 
     57     @Override
     58     protected void onOpen() {
     59         mTotalFrameCount = 0;
     60         mPeriodFrameCount = 0;
     61         mLastTime = 0;
     62     }
     63 
     64     @Override
     65     protected synchronized void onProcess() {
     66         Frame inputFrame = getConnectedInputPort("frame").pullFrame();
     67 
     68         // Update stats
     69         ++mTotalFrameCount;
     70         ++mPeriodFrameCount;
     71 
     72         // Check clock
     73         if (mLastTime == 0) {
     74             mLastTime = SystemClock.elapsedRealtime();
     75         }
     76         long curTime = SystemClock.elapsedRealtime();
     77 
     78         // Output throughput info if time period is up
     79         if ((curTime - mLastTime) >= (mPeriod * 1000)) {
     80             Log.i("Thru", "It is time!");
     81             OutputPort tpPort = getConnectedOutputPort("throughput");
     82             Throughput throughput = new Throughput(mTotalFrameCount,
     83                                                    mPeriodFrameCount,
     84                                                    curTime - mLastTime,
     85                                                    inputFrame.getElementCount());
     86             FrameValue throughputFrame = tpPort.fetchAvailableFrame(null).asFrameValue();
     87             throughputFrame.setValue(throughput);
     88             tpPort.pushFrame(throughputFrame);
     89             mLastTime = curTime;
     90             mPeriodFrameCount = 0;
     91         }
     92 
     93         getConnectedOutputPort("frame").pushFrame(inputFrame);
     94     }
     95 }
     96