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 package androidx.media.filterpacks.base; 18 19 import androidx.media.filterfw.Filter; 20 import androidx.media.filterfw.Frame; 21 import androidx.media.filterfw.FrameType; 22 import androidx.media.filterfw.InputPort; 23 import androidx.media.filterfw.MffContext; 24 import androidx.media.filterfw.OutputPort; 25 import androidx.media.filterfw.Signature; 26 27 public final class BranchFilter extends Filter { 28 29 private boolean mSynchronized = true; 30 31 public BranchFilter(MffContext context, String name) { 32 super(context, name); 33 } 34 35 public BranchFilter(MffContext context, String name, boolean synced) { 36 super(context, name); 37 mSynchronized = synced; 38 } 39 40 @Override 41 public Signature getSignature() { 42 return new Signature() 43 .addInputPort("input", Signature.PORT_REQUIRED, FrameType.any()) 44 .addInputPort("synchronized", Signature.PORT_OPTIONAL,FrameType.single(boolean.class)) 45 .disallowOtherInputs(); 46 } 47 48 @Override 49 public void onInputPortOpen(InputPort port) { 50 if (port.getName().equals("input")) { 51 for (OutputPort outputPort : getConnectedOutputPorts()) { 52 port.attachToOutputPort(outputPort); 53 } 54 } else if (port.getName().equals("synchronized")) { 55 port.bindToFieldNamed("mSynchronized"); 56 port.setAutoPullEnabled(true); 57 } 58 } 59 60 @Override 61 protected void onOpen() { 62 updateSynchronization(); 63 } 64 65 @Override 66 protected void onProcess() { 67 Frame inputFrame = getConnectedInputPort("input").pullFrame(); 68 for (OutputPort outputPort : getConnectedOutputPorts()) { 69 if (outputPort.isAvailable()) { 70 outputPort.pushFrame(inputFrame); 71 } 72 } 73 } 74 75 private void updateSynchronization() { 76 if (mSynchronized) { 77 for (OutputPort port : getConnectedOutputPorts()) { 78 port.setWaitsUntilAvailable(true); 79 } 80 } else { 81 for (OutputPort port : getConnectedOutputPorts()) { 82 port.setWaitsUntilAvailable(false); 83 } 84 } 85 } 86 87 } 88 89