1/*
2 * Copyright (C) 2012 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
17package androidx.media.filterfw.samples.simplecamera;
18
19import androidx.media.filterfw.Filter;
20import androidx.media.filterfw.FrameType;
21import androidx.media.filterfw.FrameValue;
22import androidx.media.filterfw.InputPort;
23import androidx.media.filterfw.MffContext;
24import androidx.media.filterfw.OutputPort;
25import androidx.media.filterfw.Signature;
26
27// The Filter is used to generate the camera snap effect.
28// The effect is to give the image a sudden white appearance.
29public final class WaveTriggerFilter extends Filter {
30
31    private boolean mTrigger = false;
32    private boolean mInWaveMode = false;
33    private float mTime = 0f;
34
35    public WaveTriggerFilter(MffContext context, String name) {
36        super(context, name);
37    }
38
39    @Override
40    public Signature getSignature() {
41        return new Signature()
42            .addOutputPort("value", Signature.PORT_REQUIRED, FrameType.single())
43            .disallowOtherPorts();
44    }
45
46    public synchronized void trigger() {
47        mTrigger = true;
48    }
49
50    @Override
51    public void onInputPortOpen(InputPort port) {
52        if (port.getName().equals("trigger")) {
53            port.bindToFieldNamed("mTrigger");
54            port.setAutoPullEnabled(true);
55        }
56    }
57
58    @Override
59    protected synchronized void onProcess() {
60        // Check if we were triggered
61        if (mTrigger) {
62            mInWaveMode = true;
63            mTrigger = false;
64            mTime = 0.5f;
65        }
66
67        // Calculate output value
68        float value = 0.5f;
69        if (mInWaveMode) {
70            value = -Math.abs(mTime - 1f) + 1f;
71            mTime += 0.2f;
72            if (mTime >= 2f) {
73                mInWaveMode = false;
74            }
75        }
76
77        // Push Value
78        OutputPort outPort = getConnectedOutputPort("value");
79        FrameValue frame = outPort.fetchAvailableFrame(null).asFrameValue();
80        frame.setValue(value);
81        outPort.pushFrame(frame);
82    }
83}
84