Video Transmit Using JMF
Here is One example for video transmit using JMF:
Instructin:
import java.awt.*;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
import javax.media.format.*;
import javax.media.control.TrackControl;
import javax.media.control.QualityControl;
import java.io.*;
public class VideoTransmit {
private MediaLocator locator;
private static MediaLocator ml;
private String ipAddress;
private String port;
private Processor processor = null;
private DataSink rtptransmitter = null;
private DataSource dataOutput = null;
public static CaptureDeviceInfo di = null;
public VideoTransmit(MediaLocator locator,
String ipAddress,
String port) {
this.locator = locator;
this.ipAddress = ipAddress;
this.port = port;
}
public synchronized String start() {
String result;
result = createProcessor();
if (result != null)
return result;
result = createTransmitter();
if (result != null) {
processor.close();
processor = null;
return result;
}
processor.start();
return null;
}
public void stop() {
synchronized (this) {
if (processor != null) {
processor.stop();
processor.close();
processor = null;
rtptransmitter.close();
rtptransmitter = null;
}
}
}
private String createProcessor() {
if (locator == null)
return "Locator is null";
DataSource ds;
DataSource clone;
try {
ds = Manager.createDataSource(locator);
} catch (Exception e) {
return "Couldn't create DataSource";
}
try {
processor = Manager.createProcessor(ds);
} catch (NoProcessorException npe) {
return "Couldn't create processor";
} catch (IOException ioe) {
return "IOException creating processor";
}
boolean result = waitForState(processor, Processor.Configured);
if (result == false)
return "Couldn't configure processor";
TrackControl [] tracks = processor.getTrackControls();
if (tracks == null || tracks.length < 1) return "Couldn't find tracks in processor"; boolean programmed = false;
for (int i = 0; i < tracks.length; i++) {
Format format = tracks[i].getFormat();
if ( tracks[i].isEnabled() && format instanceof VideoFormat && !programmed) { Dimension size = ((VideoFormat)format).getSize();
float frameRate = ((VideoFormat)format).getFrameRate();
int w = (size.width % 8 == 0 ? size.width : (int)(size.width / 8) * 8);
int h = (size.height % 8 == 0 ? size.height : (int)(size.height / 8) * 8);
VideoFormat jpegFormat = new VideoFormat(VideoFormat.JPEG_RTP, new Dimension(w, h), Format.NOT_SPECIFIED, Format.byteArray, frameRate);
tracks[i].setFormat(jpegFormat); System.err.println("Video transmitted as:"); System.err.println(" " + jpegFormat); programmed = true; } else tracks[i].setEnabled(false); } if (!programmed) return "Couldn't find video track";
ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
processor.setContentDescriptor(cd); result = waitForState(processor, Controller.Realized); if (result == false) return "Couldn't realize processor";
setJPEGQuality(processor, 0.5f); dataOutput = processor.getDataOutput(); return null; } private String createTransmitter() { // rtp://129.130.131.132:42050/video String rtpURL = "rtp://" + ipAddress + ":" + port + "/video"; MediaLocator outputLocator = new MediaLocator(rtpURL); try { rtptransmitter = Manager.createDataSink(dataOutput, outputLocator); rtptransmitter.open();
rtptransmitter.start(); dataOutput.start(); } catch (MediaException me) { return "Couldn't create RTP data sink"; } catch (IOException ioe) { return "Couldn't create RTP data sink"; } return null; } void setJPEGQuality(Player p, float val) { Control cs[] = p.getControls(); QualityControl qc = null; VideoFormat jpegFmt = new VideoFormat(VideoFormat.JPEG); for (int i = 0; i < owner =" ((Owned)cs[i]).getOwner();" j =" 0;" qc =" (QualityControl)cs[i];" statelock =" new" failed =" false;" failed =" true;" failed =" false;" state ="="" state ="="">");
System.exit(-1);
}*/
ml = args[0];
String url1 = args[1];
String port1 = args[2];
VideoTransmit vt = new VideoTransmit(ml, url1, port1);
String result = vt.start();
if (result != null) {
System.err.println("Error : " + result);
System.exit(0);
}
System.err.println("Start transmission for 60 seconds...");
try {
Thread.currentThread().sleep(60000);
} catch (InterruptedException ie) {
}
vt.stop();
System.err.println("...transmission ended.");
System.exit(0);
}
}
Instructin:
For run: java media_location IPaddress Port
JMF download: SUN
Hardware Requirements
* 166 MHz Pentium, 160 MHz PowerPC, or 166 MHz UltraSparc
* 32 MB RAM or greater
* Optional: An appropriate sound card for audio play back, if necessary. For example, a SoundBlaster-compatible card for Windows machines without built-in audio support, or an Ultimedia sound card for AIX machines without built-in audio support.
Installation Instructions
1. Download the version of JMF you want to install by selecting a download format and and clicking the continue button.
The downloaded file is an executable. For Windows, it's an InstallShield executable; for Solaris, it's a self-extracting shell script; otherwise it's a generic zip file.
2. If you are installing the JMF Performance Pack for Windows
* Run the executable by double-clicking on the
jmf-2_1_1e-windows-i586.exe
NOTE: The Windows installation program automatically detects whether or not Netscape Communicator 4.03 or later is installed and then installs JMF for its use! If you are using Netscape Communicator 4.04 or 4.05, then you must install the JDK 1.1 patch for Netscape Communicator before you can run any JMF applets. Netscape Communicator 4.06 or greater already supports JDK 1.1 without the patch.
If you are installing the JMF Performance Pack for Solaris SPARC
* Change directories to the install location.
* Run the command % /bin/sh ./jmf-2_1_1e-solaris-sparc.bin
If you are installing the JMF Performance Pack for Linux
* Change directories to the install location.
* Run the command % /bin/sh ./jmf-2_1_1e-linux-i586.bin
If you are installing a zip file
* Run the appropriate zip command for your system, e.g. winzip or unzip to extract JMF onto your system.
3. IMPORTANT: You must finish setting up JMF by following the setup instructions included in the JMF Implementation Documentation. You can download the documentation package separately or view it online. ----->
Comments
I downloaded your code and it's very interesting but I have a Problem, I can't compile the file because this method can't be found: boolean result = waitForState(processor, Processor.Configured);
Thank's a lot.
java.io.IOException: Could not connect to capture device
javax.media.NoDataSourceException: Error instantiating class: com.sun.media.prot
ocol.vfw.DataSource : java.io.IOException: Could not connect to capture device
at javax.media.Manager.createDataSource(Manager.java:1012)
at VideoTransmit.createProcessor(VideoTransmit.java:112)
at VideoTransmit.start(VideoTransmit.java:70)
at VideoTransmit.main(VideoTransmit.java:343)
Exception in thread "main" java.lang.NullPointerException
at VideoTransmit.waitForState(VideoTransmit.java:271)
at VideoTransmit.createProcessor(VideoTransmit.java:122)
at VideoTransmit.start(VideoTransmit.java:70)
at VideoTransmit.main(VideoTransmit.java:343)
Can you please tell me what i need to do?