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