java实现实时视频传送

用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
  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实时视频传输需要使用网络传输技术和视频编解码技术,实现起来比较复杂。下面是一个简单的Java实现实时视频传输的示例代码,仅供参考。 1. 服务端代码 ```java import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务器已启动,等待客户端连接..."); while (true) { Socket socket = serverSocket.accept(); System.out.println("客户端连接成功:" + socket.getInetAddress()); new Thread(new ServerThread(socket)).start(); } } } class ServerThread implements Runnable { private Socket socket; public ServerThread(Socket socket) { this.socket = socket; } @Override public void run() { try { // TODO: 读取视频流数据并发送到客户端 } catch (IOException e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 2. 客户端代码 ```java import java.io.DataInputStream; import java.io.IOException; import java.net.Socket; public class Client { public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost", 8888); System.out.println("连接服务器成功:" + socket.getInetAddress()); DataInputStream dis = new DataInputStream(socket.getInputStream()); // TODO: 接收视频流数据并播放 } } ``` 以上代码仅提供了服务端和客户端的基本框架,具体实现还需要使用视频编解码技术和网络传输技术。建议使用成熟的开源框架,如FFmpeg、Netty等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值