Home | History | Annotate | Download | only in base
      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 android.filterpacks.base;
     19 
     20 import java.util.Set;
     21 
     22 import android.filterfw.core.Filter;
     23 import android.filterfw.core.FilterContext;
     24 import android.filterfw.core.Frame;
     25 import android.filterfw.core.FrameFormat;
     26 import android.filterfw.core.GenerateFieldPort;
     27 import android.filterfw.core.GenerateFinalPort;
     28 import android.filterfw.core.MutableFrameFormat;
     29 import android.filterfw.format.ObjectFormat;
     30 
     31 /**
     32  * @hide
     33  */
     34 public class ObjectSource extends Filter {
     35 
     36     @GenerateFieldPort(name = "object")
     37     private Object mObject;
     38 
     39     @GenerateFinalPort(name = "format", hasDefault = true)
     40     private FrameFormat mOutputFormat = FrameFormat.unspecified();
     41 
     42     @GenerateFieldPort(name = "repeatFrame", hasDefault = true)
     43     boolean mRepeatFrame = false;
     44 
     45     private Frame mFrame;
     46 
     47     public ObjectSource(String name) {
     48         super(name);
     49     }
     50 
     51     @Override
     52     public void setupPorts() {
     53         addOutputPort("frame", mOutputFormat);
     54     }
     55 
     56     @Override
     57     public void process(FilterContext context) {
     58         // If no frame has been created, create one now.
     59         if (mFrame == null) {
     60             if (mObject == null) {
     61                 throw new NullPointerException("ObjectSource producing frame with no object set!");
     62             }
     63             FrameFormat outputFormat = ObjectFormat.fromObject(mObject, FrameFormat.TARGET_SIMPLE);
     64             mFrame = context.getFrameManager().newFrame(outputFormat);
     65             mFrame.setObjectValue(mObject);
     66             mFrame.setTimestamp(Frame.TIMESTAMP_UNKNOWN);
     67         }
     68 
     69         // Push output
     70         pushOutput("frame", mFrame);
     71 
     72         // Wait for free output
     73         if (!mRepeatFrame) {
     74             closeOutputPort("frame");
     75         }
     76     }
     77 
     78     @Override
     79     public void tearDown(FilterContext context) {
     80         mFrame.release();
     81     }
     82 
     83     @Override
     84     public void fieldPortValueUpdated(String name, FilterContext context) {
     85         // Release our internal frame, so that it is regenerated on the next call to process().
     86         if (name.equals("object")) {
     87             if (mFrame != null) {
     88                 mFrame.release();
     89                 mFrame = null;
     90             }
     91         }
     92     }
     93 }
     94