/* * @(#)DemoJMFJ3D.java 1.2 01/03/13 * * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */ import javax.media.*; import javax.media.format.*; import javax.media.control.*; import java.awt.*; import java.awt.event.*; import java.io.File; /* * This is the demo program to render live video on a 3D cylinder * surface */ public class DemoJMFJ3D extends Frame implements ControllerListener, Runnable { Processor p; int[] waitSync = new int[0]; boolean stateTransOK = true; boolean dispCtrl = true; boolean dispMessage = true; TickerCanvas tc = null; public static void main(String[] args) { MediaLocator ml; String url = null; boolean dc = true; int i = 0; while ( i < args.length ) { if ( args[i].equals("-m") ){ i++; if ( i >= args.length){ printUsuage(); System.exit(0); } url = args[i]; if ( !(url.startsWith("file:") || url.startsWith("http:"))) { url = "file:" + url; } } else if ( args[i].equals("-c") ) { i++; if ( i >= args.length){ printUsuage(); System.exit(0); } dc = new Boolean(args[i]).booleanValue(); } i++; } if ( url == null ) { try { Class cls = Class.forName("DemoJMFJ3D"); url = cls.getResource ("Behfar.mov").toString(); } catch (Exception ex) { ex.printStackTrace(); } } System.out.println("url = " + url ); try { ml = new MediaLocator(url); if ( ml == null ) { System.out.println("can not access url = " + url); printUsuage(); System.exit(0); } DemoJMFJ3D tj3d = new DemoJMFJ3D("JMF J3DRenderer Demo"); tj3d.setDispCtrl(dc); if ( !tj3d.open(ml)) System.exit(0); tj3d.init(); } catch (Exception ex) { ex.printStackTrace(); System.exit(0); } } public static void printUsuage() { System.out.println("java DemoJMFJ3D [-m ]"); System.out.println("e.g. java DemoJMFJ3D -m file:/home/hsy/media/video/cinepak/gun1.mov"); } public DemoJMFJ3D(String title) { super(title); this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e ) { System.exit(0); }}); } public void setDispCtrl (boolean dispCtrl) { this.dispCtrl = dispCtrl; } public boolean open(MediaLocator ml) { try { p = Manager.createProcessor(ml); } catch (Exception ex) { System.out.println("failed to create a processor for movie " + ml); printUsuage(); return false; } p.addControllerListener(this); p.configure(); if ( !waitForState(p.Configured)) { System.out.println("Failed to configure the processor"); return false; } // use processor as a player p.setContentDescriptor(null); // obtain the track control TrackControl[] tc = p.getTrackControls(); if ( tc == null ) { System.out.println("Failed to get the track control from processor"); return false; } TrackControl vtc = null; for ( int i =0; i < tc.length; i++ ) { if (tc[i].getFormat() instanceof VideoFormat ) { vtc = tc[i]; break; } } if ( vtc == null ) { System.out.println("can't find video track"); return false; } try { J3DRenderer j3dRenderer = new J3DRenderer(); j3dRenderer.getComponent().setSize(296, 240); vtc.setRenderer(j3dRenderer); } catch ( Exception ex) { ex.printStackTrace(); System.out.println("the processor does not support effect"); return false; } // prefetch p.prefetch(); if ( !waitForState(p.Prefetched)) { System.out.println("Failed to prefech the processor"); return false; } System.out.println("end of prefetch"); return true; } public void init() { setLayout(new BorderLayout()); Component cc, vc; if ( (vc = p.getVisualComponent()) != null ){ // System.out.println("vc = " + vc); add("Center", vc); } // if ( dispCtrl && (cc = p.getControlPanelComponent()) != null ) { // add("South", cc); // } // create Ticker canvas tc = new TickerCanvas(); tc.setSize(296, 20); add("South", tc); setVisible(true); //create ticker thread Thread t = new Thread(this); t.start(); p.start(); System.out.println("p start"); } public void addNotify() { super.addNotify(); pack(); } public void run() { while (true) { if ( dispMessage ) { tc.updateCoord(); tc.repaint(); } dispMessage = tc.getDisplayFlag(); try { Thread.currentThread().sleep(250); } catch (Exception ex) { } } } // ------------------- public boolean waitForState(int state) { synchronized (waitSync) { try { while ( p.getState() != state && stateTransOK ) { waitSync.wait(); } } catch (Exception ex) {} return stateTransOK; } } // ----------------------- public void controllerUpdate(ControllerEvent evt) { if ( evt instanceof ConfigureCompleteEvent || evt instanceof RealizeCompleteEvent || evt instanceof PrefetchCompleteEvent ) { synchronized (waitSync) { stateTransOK = true; waitSync.notifyAll(); } } else if ( evt instanceof ResourceUnavailableEvent) { synchronized (waitSync) { stateTransOK = false; waitSync.notifyAll(); } } else if ( evt instanceof EndOfMediaEvent) { p.setMediaTime(new Time(0)); p.start(); // p.close(); // System.exit(0); } } }