1// Copyright 2012 Google Inc. All Rights Reserved.
2
3package androidx.media.filterpacks.base;
4
5import androidx.media.filterfw.Filter;
6import androidx.media.filterfw.Frame;
7import androidx.media.filterfw.FrameType;
8import androidx.media.filterfw.MffContext;
9import androidx.media.filterfw.Signature;
10
11public class GraphInputSource extends Filter {
12
13    private Frame mFrame = null;
14
15    public GraphInputSource(MffContext context, String name) {
16        super(context, name);
17    }
18
19    @Override
20    public Signature getSignature() {
21        return new Signature()
22            .addOutputPort("frame", Signature.PORT_REQUIRED, FrameType.any())
23            .disallowOtherInputs();
24    }
25
26    public void pushFrame(Frame frame) {
27        if (mFrame != null) {
28            mFrame.release();
29        }
30        if (frame == null) {
31            throw new RuntimeException("Attempting to assign null-frame!");
32        }
33        mFrame = frame.retain();
34    }
35
36    @Override
37    protected void onProcess() {
38        if (mFrame != null) {
39            getConnectedOutputPort("frame").pushFrame(mFrame);
40            mFrame.release();
41            mFrame = null;
42        }
43    }
44
45    @Override
46    protected void onTearDown() {
47        if (mFrame != null) {
48            mFrame.release();
49            mFrame = null;
50        }
51    }
52
53    @Override
54    protected boolean canSchedule() {
55        return super.canSchedule() && mFrame != null;
56    }
57
58}
59