1/*
2 * Copyright 2013 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// Takes in an array, returns the size of the array
17
18package androidx.media.filterfw.samples.simplecamera;
19
20import android.util.Log;
21import androidx.media.filterfw.Filter;
22import androidx.media.filterfw.FrameType;
23import androidx.media.filterfw.FrameValue;
24import androidx.media.filterfw.MffContext;
25import androidx.media.filterfw.OutputPort;
26import androidx.media.filterfw.Signature;
27
28import java.lang.reflect.Array;
29
30public class FloatArrayToSizeFilter extends Filter {
31
32    private static final String TAG = "FloatArrayToSizeFilter";
33    private static boolean mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);
34    /**
35     * @param context
36     * @param name
37     */
38    public FloatArrayToSizeFilter(MffContext context, String name) {
39        super(context, name);
40    }
41
42    @Override
43    public Signature getSignature() {
44        FrameType intT = FrameType.single(int.class);
45        FrameType floatType = FrameType.array(float.class);
46
47        return new Signature()
48                .addInputPort("array", Signature.PORT_REQUIRED, floatType)
49                .addOutputPort("size", Signature.PORT_REQUIRED, intT)
50                .disallowOtherPorts();
51    }
52
53    /**
54     * @see androidx.media.filterfw.Filter#onProcess()
55     */
56    @Override
57    protected void onProcess() {
58        FrameValue arrayFrame = getConnectedInputPort("array").pullFrame().asFrameValues();
59        Object array = arrayFrame.getValue();
60        int size = Array.getLength(array);
61
62        OutputPort outPort = getConnectedOutputPort("size");
63        FrameValue sizeFrame = outPort.fetchAvailableFrame(null).asFrameValue();
64        sizeFrame.setValue(size);
65        outPort.pushFrame(sizeFrame);
66
67    }
68}
69