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 android.media.effect.effects;
18 
19 import android.filterfw.core.Filter;
20 import android.filterfw.core.OneShotScheduler;
21 import android.media.effect.EffectContext;
22 import android.media.effect.FilterGraphEffect;
23 import android.media.effect.EffectUpdateListener;
24 
25 import android.filterpacks.videoproc.BackDropperFilter;
26 import android.filterpacks.videoproc.BackDropperFilter.LearningDoneListener;
27 
28 /**
29  * Background replacement Effect.
30  *
31  * Replaces the background of the input video stream with a selected video
32  * Learns the background when it first starts up;
33  * needs unobstructed view of background when this happens.
34  *
35  * Effect parameters:
36  *   source: A URI for the background video
37  * Listener: Called when learning period is complete
38  *
39  * @hide
40  */
41 public class BackDropperEffect extends FilterGraphEffect {
42     private static final String mGraphDefinition =
43             "@import android.filterpacks.base;\n" +
44             "@import android.filterpacks.videoproc;\n" +
45             "@import android.filterpacks.videosrc;\n" +
46             "\n" +
47             "@filter GLTextureSource foreground {\n" +
48             "  texId = 0;\n" + // Will be set by base class
49             "  width = 0;\n" +
50             "  height = 0;\n" +
51             "  repeatFrame = true;\n" +
52             "}\n" +
53             "\n" +
54             "@filter MediaSource background {\n" +
55             "  sourceUrl = \"no_file_specified\";\n" +
56             "  waitForNewFrame = false;\n" +
57             "  sourceIsUrl = true;\n" +
58             "}\n" +
59             "\n" +
60             "@filter BackDropperFilter replacer {\n" +
61             "  autowbToggle = 1;\n" +
62             "}\n" +
63             "\n" +
64             "@filter GLTextureTarget output {\n" +
65             "  texId = 0;\n" +
66             "}\n" +
67             "\n" +
68             "@connect foreground[frame]  => replacer[video];\n" +
69             "@connect background[video]  => replacer[background];\n" +
70             "@connect replacer[video]    => output[frame];\n";
71 
72     private EffectUpdateListener mEffectListener = null;
73 
74     private LearningDoneListener mLearningListener = new LearningDoneListener() {
75         public void onLearningDone(BackDropperFilter filter) {
76             if (mEffectListener != null) {
77                 mEffectListener.onEffectUpdated(BackDropperEffect.this, null);
78             }
79         }
80     };
81 
BackDropperEffect(EffectContext context, String name)82     public BackDropperEffect(EffectContext context, String name) {
83         super(context, name, mGraphDefinition, "foreground", "output", OneShotScheduler.class);
84 
85         Filter replacer = mGraph.getFilter("replacer");
86         replacer.setInputValue("learningDoneListener", mLearningListener);
87     }
88 
89     @Override
setParameter(String parameterKey, Object value)90     public void setParameter(String parameterKey, Object value) {
91         if (parameterKey.equals("source")) {
92             Filter background = mGraph.getFilter("background");
93             background.setInputValue("sourceUrl", value);
94         } else if (parameterKey.equals("context")) {
95             Filter background = mGraph.getFilter("background");
96             background.setInputValue("context", value);
97         }
98     }
99 
100     @Override
setUpdateListener(EffectUpdateListener listener)101     public void setUpdateListener(EffectUpdateListener listener) {
102         mEffectListener = listener;
103     }
104 
105 }