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:
035: package com.db.media.out;
036:
037: import javax.media.*;
038:
039: public class StateHelper implements javax.media.ControllerListener {
040:
041: Player player = null;
042: boolean configured = false;
043: boolean realized = false;
044: boolean prefetched = false;
045: boolean eom = false;
046: boolean failed = false;
047: boolean closed = false;
048:
049: public StateHelper(Player p) {
050:
051: player = p;
052: p.addControllerListener(this );
053:
054: }
055:
056: public boolean configure(int timeOutMillis) {
057:
058: long startTime = System.currentTimeMillis();
059:
060: synchronized (this ) {
061: if (player instanceof Processor)
062: ((Processor) player).configure();
063: else
064: return false;
065: while (!configured && !failed) {
066: try {
067: wait(timeOutMillis);
068: } catch (InterruptedException ie) {
069: }
070: if (System.currentTimeMillis() - startTime > timeOutMillis)
071: break;
072: }
073: }
074: return configured;
075:
076: }
077:
078: public boolean realize(int timeOutMillis) {
079:
080: long startTime = System.currentTimeMillis();
081:
082: synchronized (this ) {
083: player.realize();
084: while (!realized && !failed) {
085: try {
086: wait(timeOutMillis);
087: } catch (InterruptedException ie) {
088: }
089: if (System.currentTimeMillis() - startTime > timeOutMillis)
090: break;
091: }
092: }
093: return realized;
094:
095: }
096:
097: public boolean prefetch(int timeOutMillis) {
098:
099: long startTime = System.currentTimeMillis();
100:
101: synchronized (this ) {
102: player.prefetch();
103: while (!prefetched && !failed) {
104: try {
105: wait(timeOutMillis);
106: } catch (InterruptedException ie) {
107: }
108: if (System.currentTimeMillis() - startTime > timeOutMillis)
109: break;
110: }
111: }
112: return prefetched && !failed;
113:
114: }
115:
116: public boolean playToEndOfMedia(int timeOutMillis) {
117:
118: long startTime = System.currentTimeMillis();
119: eom = false;
120:
121: synchronized (this ) {
122: player.start();
123: while (!eom && !failed) {
124: try {
125: wait(timeOutMillis);
126: } catch (InterruptedException ie) {
127: }
128: if (System.currentTimeMillis() - startTime > timeOutMillis)
129: break;
130: }
131: }
132: return eom && !failed;
133:
134: }
135:
136: public void close() {
137:
138: synchronized (this ) {
139: player.close();
140: while (!closed) {
141: try {
142: wait(100);
143: } catch (InterruptedException ie) {
144: }
145: }
146: }
147: player.removeControllerListener(this );
148:
149: }
150:
151: public synchronized void controllerUpdate(ControllerEvent ce) {
152:
153: if (ce instanceof RealizeCompleteEvent) {
154: realized = true;
155: } else if (ce instanceof ConfigureCompleteEvent) {
156: configured = true;
157: } else if (ce instanceof PrefetchCompleteEvent) {
158: prefetched = true;
159: } else if (ce instanceof EndOfMediaEvent) {
160: eom = true;
161: } else if (ce instanceof ControllerErrorEvent) {
162: failed = true;
163: } else if (ce instanceof ControllerClosedEvent) {
164: closed = true;
165: } else {
166: return;
167: }
168: notifyAll();
169:
170: }
171:
172: }
|