001: /*
002: *
003: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
004: *
005: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
006: * royalty free, license to use, modify and redistribute this
007: * software in source and binary code form,
008: * provided that i) this copyright notice and license appear on all copies of
009: * the software; and ii) Licensee does not utilize the software in a manner
010: * which is disparaging to Silvere Martin-Michiellot.
011: *
012: * This software is provided "AS IS," without a warranty of any kind. ALL
013: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
014: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
015: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
016: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
017: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
018: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
019: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
020: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
024: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: */
033:
034: package com.db.media.in;
035:
036: import javax.media.*;
037:
038: public class StateHelper implements javax.media.ControllerListener {
039:
040: Player player = null;
041: boolean configured = false;
042: boolean realized = false;
043: boolean prefetched = false;
044: boolean eom = false;
045: boolean failed = false;
046: boolean closed = false;
047:
048: public StateHelper(Player p) {
049:
050: player = p;
051: p.addControllerListener(this );
052:
053: }
054:
055: public boolean configure(int timeOutMillis) {
056:
057: long startTime = System.currentTimeMillis();
058:
059: synchronized (this ) {
060: if (player instanceof Processor)
061: ((Processor) player).configure();
062: else
063: return false;
064: while (!configured && !failed) {
065: try {
066: wait(timeOutMillis);
067: } catch (InterruptedException ie) {
068: }
069: if (System.currentTimeMillis() - startTime > timeOutMillis)
070: break;
071: }
072: }
073: return configured;
074:
075: }
076:
077: public boolean realize(int timeOutMillis) {
078:
079: long startTime = System.currentTimeMillis();
080:
081: synchronized (this ) {
082: player.realize();
083: while (!realized && !failed) {
084: try {
085: wait(timeOutMillis);
086: } catch (InterruptedException ie) {
087: }
088: if (System.currentTimeMillis() - startTime > timeOutMillis)
089: break;
090: }
091: }
092: return realized;
093:
094: }
095:
096: public boolean prefetch(int timeOutMillis) {
097:
098: long startTime = System.currentTimeMillis();
099:
100: synchronized (this ) {
101: player.prefetch();
102: while (!prefetched && !failed) {
103: try {
104: wait(timeOutMillis);
105: } catch (InterruptedException ie) {
106: }
107: if (System.currentTimeMillis() - startTime > timeOutMillis)
108: break;
109: }
110: }
111: return prefetched && !failed;
112:
113: }
114:
115: public boolean playToEndOfMedia(int timeOutMillis) {
116:
117: long startTime = System.currentTimeMillis();
118: eom = false;
119:
120: synchronized (this ) {
121: player.start();
122: while (!eom && !failed) {
123: try {
124: wait(timeOutMillis);
125: } catch (InterruptedException ie) {
126: }
127: if (System.currentTimeMillis() - startTime > timeOutMillis)
128: break;
129: }
130: }
131: return eom && !failed;
132:
133: }
134:
135: public void close() {
136:
137: synchronized (this ) {
138: player.close();
139: while (!closed) {
140: try {
141: wait(100);
142: } catch (InterruptedException ie) {
143: }
144: }
145: }
146: player.removeControllerListener(this );
147:
148: }
149:
150: public synchronized void controllerUpdate(ControllerEvent ce) {
151:
152: if (ce instanceof RealizeCompleteEvent) {
153: realized = true;
154: } else if (ce instanceof ConfigureCompleteEvent) {
155: configured = true;
156: } else if (ce instanceof PrefetchCompleteEvent) {
157: prefetched = true;
158: } else if (ce instanceof EndOfMediaEvent) {
159: eom = true;
160: } else if (ce instanceof ControllerErrorEvent) {
161: failed = true;
162: } else if (ce instanceof ControllerClosedEvent) {
163: closed = true;
164: } else {
165: return;
166: }
167: notifyAll();
168:
169: }
170:
171: }
|