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 package androidx.media.filterpacks.base;
     18 
     19 import android.os.Handler;
     20 import android.os.Looper;
     21 
     22 import androidx.media.filterfw.*;
     23 
     24 public final class ValueTarget extends Filter {
     25 
     26     public static interface ValueListener {
     27         public void onReceivedValue(Object value);
     28     }
     29 
     30     private ValueListener mListener = null;
     31     private Handler mHandler = null;
     32 
     33     public ValueTarget(MffContext context, String name) {
     34         super(context, name);
     35     }
     36 
     37     public void setListener(ValueListener listener, boolean onCallerThread) {
     38         if (isRunning()) {
     39             throw new IllegalStateException("Attempting to bind filter to callback while it is "
     40                 + "running!");
     41         }
     42         mListener = listener;
     43         if (onCallerThread) {
     44             if (Looper.myLooper() == null) {
     45                 throw new IllegalArgumentException("Attempting to set callback on thread which "
     46                     + "has no looper!");
     47             }
     48             mHandler = new Handler();
     49         }
     50     }
     51 
     52     @Override
     53     public Signature getSignature() {
     54         return new Signature()
     55             .addInputPort("value", Signature.PORT_REQUIRED, FrameType.single())
     56             .disallowOtherPorts();
     57     }
     58 
     59     @Override
     60     protected void onProcess() {
     61         FrameValue valueFrame = getConnectedInputPort("value").pullFrame().asFrameValue();
     62         if (mListener != null) {
     63             if (mHandler != null) {
     64                 postValueToUiThread(valueFrame.getValue());
     65             } else {
     66                 mListener.onReceivedValue(valueFrame.getValue());
     67             }
     68         }
     69     }
     70 
     71     private void postValueToUiThread(final Object value) {
     72         mHandler.post(new Runnable() {
     73             @Override
     74             public void run() {
     75                 mListener.onReceivedValue(value);
     76             }
     77         });
     78     }
     79 
     80 }
     81 
     82