ObjectSource.java revision 262184714f14ddbff3b8d418091c687416d2a14a
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 java.util.Set;
21
22import android.filterfw.core.Filter;
23import android.filterfw.core.FilterContext;
24import android.filterfw.core.Frame;
25import android.filterfw.core.FrameFormat;
26import android.filterfw.core.GenerateFieldPort;
27import android.filterfw.core.GenerateFinalPort;
28import android.filterfw.core.MutableFrameFormat;
29import android.filterfw.format.ObjectFormat;
30
31/**
32 * @hide
33 */
34public 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_JAVA);
64            mFrame = context.getFrameManager().newFrame(outputFormat);
65            mFrame.setObjectValue(mObject);
66        }
67
68        // Push output
69        pushOutput("frame", mFrame);
70
71        // Wait for free output
72        if (!mRepeatFrame) {
73            closeOutputPort("frame");
74        }
75    }
76
77    @Override
78    public void tearDown(FilterContext context) {
79        mFrame.release();
80    }
81
82    @Override
83    public void fieldPortValueUpdated(String name, FilterContext context) {
84        // Release our internal frame, so that it is regenerated on the next call to process().
85        if (name.equals("object")) {
86            if (mFrame != null) {
87                mFrame.release();
88                mFrame = null;
89            }
90        }
91    }
92}
93