用RTP协议,首先你需要下载JMF。
下面的关于RTP的介绍:
LANs based on ethernet are not "real time" with guaranteed time of delivery of packets, due to the collision algorithm used (back off a random amount of time)
The IP protocol does not guarantee delivery between hosts, let alone a guaranteed time of delivery
TCP gives reliable delivery between ports, or at least a failure message
UDP gives unreliable delivery between ports, with no time guarantee. But in practise, on LANs, UDP is reliable and time is not usually too much of a problem
RTP sits above UDP, and offers limited guarantees of delivery and timeliness
看完介绍了吧,然后我给你两段代码,分别是客户端和服务器端的:
RTP client
This has not been tested, since the server doesn´t work
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
public class RTPClient implements ControllerListener {
public static void main(String [] args) {
new RTPClient();
}
public RTPClient() {
Player p;
String srcUrl = "rtp://192.168.2.1:49150/audio";
// String srcUrl = "rtp://224.144.251.104:49150/audio/1";
DataSink sink;
MediaLocator src = new MediaLocator(srcUrl);
try {
p = Manager.createPlayer(src);
p.addControllerListener(this);
p.start();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public synchronized void controllerUpdate(ControllerEvent evt) {
if (evt instanceof EndOfMediaEvent) {
System.exit(0);
} else {
System.out.println(evt.toString());
}
}
}
RTP server
This throws an exception trying to open a random port (for RTP control?): "IOException: Can´t open local data port: 40414"
import java.io.*;
import java.util.Vector;
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
public class RTPServer implements ControllerListener {
private boolean realized = false;
private boolean configured = false;
public static void main(String [] args) {
new RTPServer();
}
public RTPServer() {
Processor p;
String srcFile = "long.wav";
// String destUrl = "rtp://224.144.251.104:49150/audio/1";
String destUrl = "rtp://128.0.0.1:49150/audio";
DataSink rtpSink;
MediaLocator src = new MediaLocator("file:" + srcFile);
MediaLocator dest = new MediaLocator(destUrl);
// MediaLocator dest = new MediaLocator("rtpraw://");
try {
p = Manager.createProcessor(src);
p.addControllerListener(this);
p.configure();
while (! configured) {
try {
Thread.currentThread().sleep(100L);;
} catch (InterruptedException e) {
// ignore
}
}
p.setContentDescriptor(new ContentDescriptor(FileTypeDescriptor.RAW_RTP));
p.realize();
while (! realized) {
try {
Thread.currentThread().sleep(100L);;
} catch (InterruptedException e) {
// ignore
}
}
DataSource output = p.getDataOutput();
rtpSink = Manager.createDataSink(output, dest);
System.out.println("Sink content type: " + rtpSink.getContentType());
System.out.println("Sink media type: " + rtpSink.getOutputLocator().toString());
rtpSink.open();
rtpSink.start();
p.start();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public synchronized void controllerUpdate(ControllerEvent evt) {
if (evt instanceof RealizeCompleteEvent) {
realized = true;
} else if (evt instanceof ConfigureCompleteEvent) {
configured = true;
} else if (evt instanceof EndOfMediaEvent) {
System.exit(0);
} else {
// System.out.println(evt.toString());
}
}
}
最后给你一个SUN上关于实时媒体流传输的地址(事实上里面包括了视、音频捕捉,播放等和JMF有关的所有资料):
http://java.sun.com/products/java-media/jmf/2.1.1/solutions/index.html
下面的关于RTP的介绍:
LANs based on ethernet are not "real time" with guaranteed time of delivery of packets, due to the collision algorithm used (back off a random amount of time)
The IP protocol does not guarantee delivery between hosts, let alone a guaranteed time of delivery
TCP gives reliable delivery between ports, or at least a failure message
UDP gives unreliable delivery between ports, with no time guarantee. But in practise, on LANs, UDP is reliable and time is not usually too much of a problem
RTP sits above UDP, and offers limited guarantees of delivery and timeliness
看完介绍了吧,然后我给你两段代码,分别是客户端和服务器端的:
RTP client
This has not been tested, since the server doesn´t work
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
public class RTPClient implements ControllerListener {
public static void main(String [] args) {
new RTPClient();
}
public RTPClient() {
Player p;
String srcUrl = "rtp://192.168.2.1:49150/audio";
// String srcUrl = "rtp://224.144.251.104:49150/audio/1";
DataSink sink;
MediaLocator src = new MediaLocator(srcUrl);
try {
p = Manager.createPlayer(src);
p.addControllerListener(this);
p.start();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public synchronized void controllerUpdate(ControllerEvent evt) {
if (evt instanceof EndOfMediaEvent) {
System.exit(0);
} else {
System.out.println(evt.toString());
}
}
}
RTP server
This throws an exception trying to open a random port (for RTP control?): "IOException: Can´t open local data port: 40414"
import java.io.*;
import java.util.Vector;
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
public class RTPServer implements ControllerListener {
private boolean realized = false;
private boolean configured = false;
public static void main(String [] args) {
new RTPServer();
}
public RTPServer() {
Processor p;
String srcFile = "long.wav";
// String destUrl = "rtp://224.144.251.104:49150/audio/1";
String destUrl = "rtp://128.0.0.1:49150/audio";
DataSink rtpSink;
MediaLocator src = new MediaLocator("file:" + srcFile);
MediaLocator dest = new MediaLocator(destUrl);
// MediaLocator dest = new MediaLocator("rtpraw://");
try {
p = Manager.createProcessor(src);
p.addControllerListener(this);
p.configure();
while (! configured) {
try {
Thread.currentThread().sleep(100L);;
} catch (InterruptedException e) {
// ignore
}
}
p.setContentDescriptor(new ContentDescriptor(FileTypeDescriptor.RAW_RTP));
p.realize();
while (! realized) {
try {
Thread.currentThread().sleep(100L);;
} catch (InterruptedException e) {
// ignore
}
}
DataSource output = p.getDataOutput();
rtpSink = Manager.createDataSink(output, dest);
System.out.println("Sink content type: " + rtpSink.getContentType());
System.out.println("Sink media type: " + rtpSink.getOutputLocator().toString());
rtpSink.open();
rtpSink.start();
p.start();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public synchronized void controllerUpdate(ControllerEvent evt) {
if (evt instanceof RealizeCompleteEvent) {
realized = true;
} else if (evt instanceof ConfigureCompleteEvent) {
configured = true;
} else if (evt instanceof EndOfMediaEvent) {
System.exit(0);
} else {
// System.out.println(evt.toString());
}
}
}
最后给你一个SUN上关于实时媒体流传输的地址(事实上里面包括了视、音频捕捉,播放等和JMF有关的所有资料):
http://java.sun.com/products/java-media/jmf/2.1.1/solutions/index.html