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
18package android.filterfw.core;
19
20import android.filterfw.core.Filter;
21import android.filterfw.core.RoundRobinScheduler;
22import android.util.Log;
23
24import java.util.HashMap;
25
26/**
27 * This OneShotScheduler only schedules source filters at most once. All other
28 * filters will be scheduled, and possibly repeatedly, until there is no filter
29 * that can be scheduled.
30 *
31 * @hide
32 */
33public class OneShotScheduler extends RoundRobinScheduler {
34    private HashMap <String, Integer> scheduled;
35
36    private final boolean mLogVerbose;
37    private static final String TAG = "OneShotScheduler";
38
39    public OneShotScheduler(FilterGraph graph) {
40        super(graph);
41        scheduled = new HashMap<String, Integer>();
42        mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);
43    }
44
45    @Override
46    public void reset() {
47        super.reset();
48        scheduled.clear();
49    }
50
51    @Override
52    public Filter scheduleNextNode() {
53        Filter first = null;
54        // return the first filter that is not scheduled before.
55        while (true) {
56            Filter filter = super.scheduleNextNode();
57            if (filter == null) {
58                if (mLogVerbose) Log.v(TAG, "No filters available to run.");
59                return null;
60            }
61            if (!scheduled.containsKey(filter.getName())) {
62                if (filter.getNumberOfConnectedInputs() == 0)
63                    scheduled.put(filter.getName(),1);
64                if (mLogVerbose) Log.v(TAG, "Scheduling filter \"" + filter.getName() + "\" of type " + filter.getFilterClassName());
65                return filter;
66            }
67            // if loop back, nothing available
68            if (first == filter) {
69                break;
70            }
71            // save the first scheduled one
72            if (first == null) first = filter;
73        }
74        if (mLogVerbose) Log.v(TAG, "One pass through graph completed.");
75        return null;
76    }
77}
78