1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /********************************************************************
25 * Video Tools for Zeus *
26 * Provides functionality to use record and playback feature *
27 *******************************************************************/
28
29 package zeus.visualiser.basic;
30
31 import java.util.*;
32 import javax.swing.*;
33
34 import zeus.util.*;
35 import zeus.gui.*;
36 import zeus.concepts.*;
37 import zeus.actors.*;
38
39
40 public class Player extends Thread {
41 static final int COUNT = 0;
42 static final int LIST = 1;
43 static final int NEXT = 2;
44 static final int PRIOR = 3;
45 static final int FIRST = 4;
46 static final int LAST = 5;
47 static final int OPEN = 6;
48 static final int CLOSE = 7;
49 static final int READY = 8;
50 static final int STOP = 9;
51
52 private static long CDEFAULT_SPEED = 1000;
53 private long DEFAULT_SPEED = CDEFAULT_SPEED;
54
55 protected VideoTool video;
56
57 private int max_count = -1;
58 private int cmd = OPEN;
59 private boolean playback = true;
60 private boolean single_step = false;
61 private long timeout;
62
63 void setSpeed(long speed) { DEFAULT_SPEED = speed; }
64 long getSpeed() { return DEFAULT_SPEED; }
65
66 static long getDefaultSpeed() { return CDEFAULT_SPEED; }
67 static void setDefaultSpeed(long s) { CDEFAULT_SPEED = s; }
68
69 Player(VideoTool video) {
70 Assert.notNull(video);
71 this.video = video;
72
73 this.setPriority(Thread.NORM_PRIORITY-2);
74 this.start();
75 }
76
77 private boolean ready() {
78 switch( cmd ) {
79 case READY:
80 case NEXT:
81 case PRIOR:
82 return true;
83 default:
84 return false;
85 }
86 }
87
88 synchronized void first() {
89 if ( !ready() ) return;
90 cmd = FIRST;
91 single_step = false;
92 timeout = DEFAULT_SPEED;
93 notify();
94 }
95
96 synchronized void last() {
97 if ( !ready() ) return;
98 cmd = LAST;
99 single_step = false;
100 timeout = DEFAULT_SPEED;
101 notify();
102 }
103
104 synchronized void forward_step() {
105 if ( !ready() ) return;
106 cmd = NEXT;
107 single_step = true;
108 timeout = DEFAULT_SPEED;
109 notify();
110 }
111
112 synchronized void rewind_step() {
113 if ( !ready() ) return;
114 cmd = PRIOR;
115 single_step = true;
116 timeout = DEFAULT_SPEED;
117 notify();
118 }
119
120 synchronized void forward() {
121 if ( !ready() ) return;
122 cmd = NEXT;
123 single_step = false;
124 timeout = DEFAULT_SPEED;
125 notify();
126 }
127
128 synchronized void rewind() {
129 if ( !ready() ) return;
130 cmd = PRIOR;
131 single_step = false;
132 timeout = DEFAULT_SPEED;
133 notify();
134 }
135
136 synchronized void fforward() {
137 if ( !ready() ) return;
138 cmd = NEXT;
139 single_step = false;
140 timeout = DEFAULT_SPEED/2;
141 notify();
142 }
143
144 synchronized void frewind() {
145 if ( !ready() ) return;
146 cmd = PRIOR;
147 single_step = false;
148 timeout = DEFAULT_SPEED/2;
149 notify();
150 }
151
152 synchronized void pause() {
153 if ( !ready() ) return;
154 cmd = READY;
155 single_step = false;
156 timeout = DEFAULT_SPEED;
157 }
158
159 synchronized void terminate() {
160 playback = false;
161 cmd = STOP;
162 notify();
163 }
164
165 private boolean running() {
166 switch( cmd ) {
167 case NEXT:
168 case PRIOR:
169 case FIRST:
170 case LAST:
171 case STOP:
172 return true;
173 default:
174 return false;
175 }
176 }
177
178 void setCommand(int cmd) {
179 this.cmd = cmd;
180 }
181
182 void setCount(int max) {
183 this.max_count = max;
184 }
185
186 public void run() {
187 int count = 0;
188 while( playback ) {
189 try {
190 synchronized(this) {
191 while( !running() )
192 wait();
193 }
194 if ( cmd == STOP ) return;
195
196 if ( (cmd == NEXT && count < max_count) ||
197 (cmd == PRIOR && count > 0) ) {
198
199 if ( cmd == NEXT )
200 count++;
201 else if ( cmd == PRIOR )
202 count--;
203
204 if ( cmd == NEXT )
205 video.doPlayerCommand("db_next");
206 else
207 video.doPlayerCommand("db_prior");
208
209 if ( single_step )
210 cmd = READY;
211 else
212 sleep(timeout);
213 }
214 else if ( cmd == FIRST || cmd == LAST ) {
215 count = (cmd == FIRST) ? 0 : max_count;
216
217 if ( cmd == FIRST )
218 video.doPlayerCommand("db_first");
219 else
220 video.doPlayerCommand("db_last");
221
222 single_step = false;
223 cmd = READY;
224 }
225 else {
226 synchronized(this) {
227 wait();
228 }
229 }
230 }
231 catch(InterruptedException e) {
232 }
233 }
234 }
235 }