001package ca.bc.webarts.tools; 002 003/* 004 * javac livesave.java 005 * java livesave 006 * Check the path is found 007 * String path1="file:///c:\\sam.avi" path where the file has to be saved. 008 * Install device driver and java media frame work before running this 009 * program 010 */ 011 012import java.applet.*; 013import java.awt.*; 014import java.awt.event.*; 015import java.util.*; 016import javax.media.*; 017import javax.media.Buffer; 018import javax.media.control.*; 019import javax.media.format.*; 020import javax.media.protocol.*; 021import javax.media.protocol.BufferTransferHandler; 022import javax.media.protocol.PushBufferDataSource; 023import javax.media.protocol.PushBufferStream; 024 025 026 027 028/** 029 * Description of the Class 030 * 031 * @author tgutwin 032 */ 033public class LiveWebCam 034{ 035 /** 036 * Description of the Method 037 * 038 * @param arg Description of the Parameter 039 */ 040 public static void main( String arg[] ) 041 { 042 WebCam2 l1 = new WebCam2(); 043 l1.setVisible( true ); 044 l1.addWindowListener( 045 new WindowAdapter() 046 { 047 public void windowClosing( WindowEvent we ) 048 { 049 System.exit( 0 ); 050 } 051 } ); 052 } 053} 054 055 056/** 057 * Description of the Class 058 * 059 * @author tgutwin 060 */ 061class WebCam2 extends Frame 062{ 063 064 065 /** Description of the Field */ 066 Player aplayer, vplayer; 067 /** Description of the Field */ 068 Panel panel; 069 /** Description of the Field */ 070 CaptureDeviceInfo audiodevice; 071 /** Description of the Field */ 072 CaptureDeviceInfo videodevice, deviceinfo; 073 /** Description of the Field */ 074 Format videoaudioformat = null; 075 /** Description of the Field */ 076 String devicename[] = new String[10]; 077 /** Description of the Field */ 078 Format audioformat = null; 079 /** Description of the Field */ 080 Format videoformat = null; 081 /** Description of the Field */ 082 Processor processor1, processor2; 083 /** Description of the Field */ 084 ProcessorModel processormodel; 085 /** Description of the Field */ 086 Object state = new Object(); 087 /** Description of the Field */ 088 DataSource ds; 089 /** Description of the Field */ 090 int a = 1; 091 /** Description of the Field */ 092 MediaLocator videolocator, audiolocator; 093 /** Description of the Field */ 094 DataSource datasource[] = new DataSource[2]; 095 /** Description of the Field */ 096 Vector devicelist; 097 098 099 /** Constructor for the LiveWebCam object */ 100 public WebCam2() 101 { 102 try 103 { 104 System.out.print("Getting Device List "); 105 devicelist = CaptureDeviceManager.getDeviceList( videoaudioformat ); 106 System.out.println("("+devicelist.size()+")"); 107 for ( int i = 0; i < devicelist.size(); i++ ) 108 { 109 deviceinfo = (CaptureDeviceInfo)devicelist.elementAt( i ); 110 devicename[i] = deviceinfo.getName(); 111 System.out.println( "Device name is " + devicename[i] ); 112 Format sfmt[] = deviceinfo.getFormats(); 113 for ( int i1 = 0; i1 < sfmt.length; i1++ ) 114 { 115 if ( sfmt[i1] instanceof VideoFormat ) 116 { 117 videodevice = CaptureDeviceManager.getDevice( devicename[i] ); 118 videoformat = sfmt[i1]; 119 } 120 if ( sfmt[i1] instanceof AudioFormat ) 121 { 122 audiodevice = CaptureDeviceManager.getDevice( devicename[i] ); 123 audioformat = sfmt[i1]; 124 } 125 } 126 } 127 MediaLocator m = videodevice.getLocator(); 128 System.out.println( "Video device is " + m ); 129 MediaLocator m1 = audiodevice.getLocator(); 130 System.out.println( "Audio device is " + m1 ); 131 132 videolocator = videodevice.getLocator(); 133 System.out.println( videolocator ); 134 MediaLocator audiolocator = audiodevice.getLocator(); 135 System.out.println( audiolocator ); 136 137 datasource[0] = Manager.createDataSource( videolocator ); 138 datasource[1] = Manager.createDataSource( audiolocator ); 139 ds = Manager.createMergingDataSource( datasource ); 140 141 processor1 = Manager.createProcessor( ds ); 142 143 processor1.configure(); 144 while ( processor1.getState() < processor1.Configured ) 145 { 146 synchronized ( state ) 147 { 148 state.wait( 100 ); 149 } 150 } 151 152 processor1.setContentDescriptor( new FileTypeDescriptor( FileTypeDescriptor.MSVIDEO ) ); 153 TrackControl[] tcs1 = processor1.getTrackControls(); 154 for ( int i = 0; i < tcs1.length; i++ ) 155 { 156 Format frmt = tcs1[i].getFormat(); 157 System.out.println( "Format of track " + i + " is " + frmt ); 158 if ( tcs1[i] instanceof VideoFormat ) 159 { 160 tcs1[i].setFormat( new VideoFormat( VideoFormat.JPEG ) ); 161 System.out.println( "The Format of track " + i + " is " + frmt ); 162 } 163 if ( tcs1[i] instanceof AudioFormat ) 164 { 165 tcs1[i].setFormat( new AudioFormat( AudioFormat.LINEAR ) ); 166 System.out.println( "The Format of track " + i + " is " + frmt ); 167 } 168 } 169 170 processor1.realize(); 171 while ( processor1.getState() < processor1.Realized ) 172 { 173 synchronized ( state ) 174 { 175 state.wait( 100 ); 176 } 177 } 178 179 processor1.prefetch(); 180 while ( processor1.getState() < processor1.Prefetched ) 181 { 182 synchronized ( state ) 183 { 184 state.wait( 100 ); 185 } 186 } 187 188 DataSource ds1 = processor1.getDataOutput(); 189 190 System.out.println( "Data Source Content Type " + ds1.getContentType() ); 191 System.out.println( "Data Source Content Type " + ds.getContentType() ); 192 193 DataSink datasink; 194 MediaLocator out; 195 String path1 = "file:///c:\\sam.avi"; 196 out = new MediaLocator( path1 ); 197 datasink = Manager.createDataSink( ds1, out ); 198 datasink.open(); 199 datasink.start(); 200 ds1.start(); 201 processor1.start(); 202 /* 203 * processor1.close(); 204 * datasink.close(); 205 * ds1.stop(); 206 * System.exit(0); 207 */ 208 } 209 catch ( Exception e ) 210 { 211 System.out.println( e.getMessage() ); 212 } 213 } 214 215} 216