1/* 2 * Copyright (C) 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 */ 16package androidx.media.filterfw; 17 18import java.util.LinkedList; 19import java.util.Queue; 20 21/** 22 * A {@link Filter} that pushes out externally injected frames. 23 * <p> When a frame is injected using {@link #injectFrame(Frame)}, this source will push it on its 24 * output port and then sleep until another frame is injected. 25 * <p> Multiple frames may be injected before any frame is pushed out. In this case they will be 26 * queued and pushed in FIFO order. 27 */ 28class FrameSourceFilter extends Filter { 29 30 private final Queue<Frame> mFrames = new LinkedList<Frame>(); 31 32 FrameSourceFilter(MffContext context, String name) { 33 super(context, name); 34 } 35 36 @Override 37 public Signature getSignature() { 38 return new Signature() 39 .addOutputPort("output", Signature.PORT_REQUIRED, FrameType.any()) 40 .disallowOtherPorts(); 41 } 42 43 private synchronized Frame obtainFrame() { 44 if (mFrames.isEmpty()) { 45 enterSleepState(); 46 return null; 47 } else { 48 return mFrames.poll(); 49 } 50 } 51 52 /** 53 * Call this method to inject a frame that will be pushed in a future execution of the filter. 54 * <p> If multiple frames are injected then they will be pushed one per execution in FIFO order. 55 */ 56 public synchronized void injectFrame(Frame frame) { 57 mFrames.add(frame); 58 wakeUp(); 59 } 60 61 @Override 62 protected void onProcess() { 63 Frame frame = obtainFrame(); 64 if (frame != null) { 65 getConnectedOutputPort("output").pushFrame(frame); 66 } 67 } 68 69} 70