blob: 490b2bc03b0ae4d36d1503f240423fde1469b01c [file] [log] [blame]
Constantin Kaplinsky1215b992008-04-18 09:51:44 +00001//
2// Copyright (C) 2001,2002 HorizonLive.com, Inc. All Rights Reserved.
3// Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
4//
5// This is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This software is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this software; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18// USA.
19//
20
wimba.comc23aeb02004-09-16 00:00:00 +000021package com.HorizonLive.RfbPlayer;
22
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000023import java.awt.*;
24import java.awt.event.*;
25import java.io.*;
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +000026import java.net.*;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000027
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000028public class RfbPlayer extends java.applet.Applet
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000029 implements java.lang.Runnable, WindowListener {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000030
31 boolean inAnApplet = true;
32 boolean inSeparateFrame = false;
33
wimba.com252eb3b2004-09-21 21:27:54 +000034 /** current applet width */
35 int dispW = 300;
36 /** current applet height */
37 int dispH = 200;
38
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000039 //
40 // main() is called when run as a java program from the command line.
41 // It simply runs the applet inside a newly-created frame.
42 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000043 public static void main(String[] argv) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000044 RfbPlayer p = new RfbPlayer();
45 p.mainArgs = argv;
46 p.inAnApplet = false;
47 p.inSeparateFrame = true;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000048
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000049 p.init();
50 p.start();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000051 }
52
53 String[] mainArgs;
54
55 RfbProto rfb;
56 Thread rfbThread;
57
58 Frame vncFrame;
59 Container vncContainer;
60 ScrollPane desktopScrollPane;
61 GridBagLayout gridbag;
62 ButtonPanel buttonPanel;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000063 VncCanvas vc;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000064
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +000065 String sessionURL;
Constantin Kaplinskyc833e012002-05-30 17:59:22 +000066 URL url;
Constantin Kaplinsky972c2572002-05-30 14:19:02 +000067 long initialTimeOffset;
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +000068 double playbackSpeed;
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +000069 boolean autoPlay;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000070 boolean showControls;
wimba.comd1f56df2004-11-01 16:18:54 +000071 boolean isQuitting = false;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000072 int deferScreenUpdates;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000073
74 //
75 // init()
76 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000077 public void init() {
78
wimba.comc23aeb02004-09-16 00:00:00 +000079 // LiveConnect work-a-round
80 RfbSharedStatic.refApplet = this;
81
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000082 readParameters();
83
84 if (inSeparateFrame) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000085 vncFrame = new Frame("RFB Session Player");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000086 if (!inAnApplet) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000087 vncFrame.add("Center", this);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000088 }
89 vncContainer = vncFrame;
90 } else {
91 vncContainer = this;
92 }
93
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000094 if (inSeparateFrame)
95 vncFrame.addWindowListener(this);
96
wimba.comd1f56df2004-11-01 16:18:54 +000097 rfbThread = new Thread(this, "RfbThread");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000098 rfbThread.start();
99 }
100
101 public void update(Graphics g) {
102 }
103
104 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000105 // run() - executed by the rfbThread to read RFB data.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000106 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000107 public void run() {
108
109 gridbag = new GridBagLayout();
110 vncContainer.setLayout(gridbag);
111
112 GridBagConstraints gbc = new GridBagConstraints();
113 gbc.gridwidth = GridBagConstraints.REMAINDER;
114 gbc.anchor = GridBagConstraints.NORTHWEST;
115
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000116 if (showControls) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000117 buttonPanel = new ButtonPanel(this);
118 buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
119 gridbag.setConstraints(buttonPanel, gbc);
120 vncContainer.add(buttonPanel);
121 }
122
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000123 if (inSeparateFrame) {
124 vncFrame.pack();
125 vncFrame.show();
126 } else {
127 validate();
128 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000129
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000130 try {
Constantin Kaplinsky40ad1712002-09-22 08:36:43 +0000131 if (inAnApplet) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000132 url = new URL(getCodeBase(), sessionURL);
Constantin Kaplinsky40ad1712002-09-22 08:36:43 +0000133 } else {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000134 url = new URL(sessionURL);
Constantin Kaplinsky40ad1712002-09-22 08:36:43 +0000135 }
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000136 rfb = new RfbProto(url);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000137
138 vc = new VncCanvas(this);
139 gbc.weightx = 1.0;
140 gbc.weighty = 1.0;
141
wimba.com252eb3b2004-09-21 21:27:54 +0000142 // Create a panel which itself is resizeable and can hold
143 // non-resizeable VncCanvas component at the top left corner.
144 Panel canvasPanel = new Panel();
145 canvasPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
146 canvasPanel.add(vc);
147
148 // Create a ScrollPane which will hold a panel with VncCanvas
149 // inside.
150 desktopScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
151 gbc.fill = GridBagConstraints.BOTH;
152 gridbag.setConstraints(canvasPanel, gbc);
153 desktopScrollPane.add(canvasPanel);
154
155 // Now add the scroll bar to the container.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000156 if (inSeparateFrame) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000157 gridbag.setConstraints(desktopScrollPane, gbc);
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000158 vncFrame.add(desktopScrollPane);
159 vncFrame.setTitle(rfb.desktopName);
160 vncFrame.pack();
161 vc.resizeDesktopFrame();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000162 } else {
wimba.com252eb3b2004-09-21 21:27:54 +0000163 // Size the scroll pane to display size.
164 desktopScrollPane.setSize(dispW, dispH);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000165
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000166 // Just add the VncCanvas component to the Applet.
wimba.com252eb3b2004-09-21 21:27:54 +0000167 gbc.fill = GridBagConstraints.NONE;
168 gridbag.setConstraints(desktopScrollPane, gbc);
169 add(desktopScrollPane);
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000170 validate();
wimba.com252eb3b2004-09-21 21:27:54 +0000171 vc.resizeEmbeddedApplet();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000172 }
173
wimba.comd1f56df2004-11-01 16:18:54 +0000174 while (!isQuitting) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000175 try {
176 setPaused(!autoPlay);
177 rfb.fbs.setSpeed(playbackSpeed);
178 if (initialTimeOffset > rfb.fbs.getTimeOffset())
179 setPos(initialTimeOffset); // don't seek backwards here
180 vc.processNormalProtocol();
181 } catch (EOFException e) {
182 if (e.getMessage() != null && e.getMessage().equals("[REWIND]")) {
183 // A special type of EOFException allowing us to seek backwards.
184 initialTimeOffset = rfb.fbs.getSeekOffset();
185 autoPlay = !rfb.fbs.isPaused();
186 } else {
187 // Return to the beginning after the playback is finished.
188 initialTimeOffset = 0;
189 autoPlay = false;
190 }
Constantin Kaplinsky282aaa12002-09-22 11:33:22 +0000191 rfb.newSession(url);
192 vc.updateFramebufferSize();
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000193 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000194 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000195
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000196 } catch (FileNotFoundException e) {
197 fatalError(e.toString());
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000198 } catch (Exception e) {
199 e.printStackTrace();
200 fatalError(e.toString());
201 }
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000202
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000203 }
204
Constantin Kaplinskyac6420d2002-05-29 17:05:39 +0000205 public void setPaused(boolean paused) {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000206 if (showControls)
207 buttonPanel.setPaused(paused);
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000208 if (paused) {
209 rfb.fbs.pausePlayback();
210 } else {
211 rfb.fbs.resumePlayback();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000212 }
213 }
214
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000215 public double getSpeed() {
216 return playbackSpeed;
217 }
218
Constantin Kaplinskyce39d3b2002-05-30 15:54:56 +0000219 public void setSpeed(double speed) {
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000220 playbackSpeed = speed;
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000221 rfb.fbs.setSpeed(speed);
Constantin Kaplinskyce39d3b2002-05-30 15:54:56 +0000222 }
223
wimba.comc23aeb02004-09-16 00:00:00 +0000224 public void jumpTo(long pos) {
225 long diff = Math.abs(pos - rfb.fbs.getTimeOffset());
226
227 // Current threshold is 5 seconds
228 if (diff > 5000) {
229 rfb.fbs.pausePlayback();
230 setPos(pos);
231 rfb.fbs.resumePlayback();
232 }
233 }
234
Constantin Kaplinskyc833e012002-05-30 17:59:22 +0000235 public void setPos(long pos) {
Constantin Kaplinsky5b1b92a2002-05-30 18:02:34 +0000236 rfb.fbs.setTimeOffset(pos);
Constantin Kaplinsky30f786a2002-05-29 10:59:52 +0000237 }
238
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000239 public void updatePos() {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000240 if (showControls)
Constantin Kaplinskyc833e012002-05-30 17:59:22 +0000241 buttonPanel.setPos(rfb.fbs.getTimeOffset());
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000242 }
243
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000244 //
245 // readParameters() - read parameters from the html source or from the
246 // command line. On the command line, the arguments are just a sequence of
247 // param_name/param_value pairs where the names and values correspond to
248 // those expected in the html applet tag source.
249 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000250 public void readParameters() {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000251
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +0000252 sessionURL = readParameter("URL", true);
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000253
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000254 initialTimeOffset = readLongParameter("Position", 0);
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000255 if (initialTimeOffset < 0)
256 initialTimeOffset = 0;
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000257
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000258 playbackSpeed = readDoubleParameter("Speed", 1.0);
259 if (playbackSpeed <= 0.0)
260 playbackSpeed = 1.0;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000261
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000262 autoPlay = false;
263 String str = readParameter("Autoplay", false);
264 if (str != null && str.equalsIgnoreCase("Yes"))
265 autoPlay = true;
266
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000267 showControls = true;
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000268 str = readParameter("Show Controls", false);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000269 if (str != null && str.equalsIgnoreCase("No"))
270 showControls = false;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000271
272 if (inAnApplet) {
273 str = readParameter("Open New Window", false);
274 if (str != null && str.equalsIgnoreCase("Yes"))
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000275 inSeparateFrame = true;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000276 }
277
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000278 // Fine tuning options.
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000279 deferScreenUpdates = (int)readLongParameter("Defer screen updates", 20);
280 if (deferScreenUpdates < 0)
281 deferScreenUpdates = 0; // Just in case.
wimba.com252eb3b2004-09-21 21:27:54 +0000282
283 // Display width and height.
284 dispW = readIntParameter("DISPLAY_WIDTH", dispW);
285 dispH = readIntParameter("DISPLAY_HEIGHT", dispH);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000286 }
287
288 public String readParameter(String name, boolean required) {
289 if (inAnApplet) {
290 String s = getParameter(name);
291 if ((s == null) && required) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000292 fatalError(name + " parameter not specified");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000293 }
294 return s;
295 }
296
297 for (int i = 0; i < mainArgs.length; i += 2) {
298 if (mainArgs[i].equalsIgnoreCase(name)) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000299 try {
300 return mainArgs[i + 1];
301 } catch (Exception e) {
302 if (required) {
303 fatalError(name + " parameter not specified");
304 }
305 return null;
306 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000307 }
308 }
309 if (required) {
310 fatalError(name + " parameter not specified");
311 }
312 return null;
313 }
314
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000315 long readLongParameter(String name, long defaultValue) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000316 String str = readParameter(name, false);
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000317 long result = defaultValue;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000318 if (str != null) {
319 try {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000320 result = Long.parseLong(str);
321 } catch (NumberFormatException e) {
322 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000323 }
324 return result;
325 }
326
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000327 double readDoubleParameter(String name, double defaultValue) {
328 String str = readParameter(name, false);
329 double result = defaultValue;
330 if (str != null) {
331 try {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000332 result = Double.valueOf(str).doubleValue();
333 } catch (NumberFormatException e) {
334 }
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000335 }
336 return result;
337 }
338
wimba.com252eb3b2004-09-21 21:27:54 +0000339 int readIntParameter(String name, int defaultValue) {
340 String str = readParameter(name, false);
341 int result = defaultValue;
342 if (str != null) {
343 try {
344 result = Integer.parseInt(str);
345 } catch (NumberFormatException e) {
346 }
347 }
348 return result;
349 }
350
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000351 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000352 // fatalError() - print out a fatal error message.
353 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000354 public void fatalError(String str) {
355 System.out.println(str);
356
357 if (inAnApplet) {
358 vncContainer.removeAll();
359 if (rfb != null) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000360 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000361 }
362 Label errLabel = new Label(str);
363 errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
364 vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
365 vncContainer.add(errLabel);
366 if (inSeparateFrame) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000367 vncFrame.pack();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000368 } else {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000369 validate();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000370 }
371 Thread.currentThread().stop();
372 } else {
373 System.exit(1);
374 }
375 }
376
377
378 //
379 // This method is called before the applet is destroyed.
380 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000381 public void destroy() {
wimba.comd1f56df2004-11-01 16:18:54 +0000382 isQuitting = true;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000383 vncContainer.removeAll();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000384 if (rfb != null) {
wimba.comd1f56df2004-11-01 16:18:54 +0000385 rfb.quit();
386 }
387 try {
388 rfbThread.join();
389 } catch (InterruptedException e) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000390 }
391 if (inSeparateFrame) {
392 vncFrame.dispose();
393 }
394 }
395
wimba.com252eb3b2004-09-21 21:27:54 +0000396 //
397 // Set the new width and height of the applet. Call when browser is resized to
398 // resize the viewer.
399 //
400 public void displaySize(int width, int height) {
401 dispW = width;
402 dispH = height;
403 if (!inSeparateFrame) {
404 vc.resizeEmbeddedApplet();
405 }
406 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000407
408 //
409 // Close application properly on window close event.
410 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000411 public void windowClosing(WindowEvent evt) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000412 vncContainer.removeAll();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000413 if (rfb != null)
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000414 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000415
416 vncFrame.dispose();
417 if (!inAnApplet) {
418 System.exit(0);
419 }
420 }
421
422 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000423 // Ignore window events we're not interested in.
424 //
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000425 public void windowActivated(WindowEvent evt) {
426 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000427
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000428 public void windowDeactivated(WindowEvent evt) {
429 }
430
431 public void windowOpened(WindowEvent evt) {
432 }
433
434 public void windowClosed(WindowEvent evt) {
435 }
436
437 public void windowIconified(WindowEvent evt) {
438 }
439
440 public void windowDeiconified(WindowEvent evt) {
441 }
442
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000443}