RTSP使用TCP控制套接字进行握手和发送控制消息。实际的视频数据通过TCP或UDP在单独的端口中发送。RTSP控制信息也可以通过HTTP或HTTPS进行隧道传输。我在使用Android MediaPlayer时遇到的问题是它不支持RTSP身份验证。然而,我通过创建本地RTSP代理添加了身份验证信息来解决这个限制。幸运的是,RTSP标头非常简单和明文,因此添加必要的身份验证很容易。
以下是我是如何实现的:
- 创建一个本地ServerSocket作为代理。
- 从用户密码生成身份验证字符串
- 将MediaPlayer连接到您的本地代理
- 对于输出数据,将身份验证添加到输出TCP数据包中
请注意,RTSP数据相当稀疏,因此解析和更改所有RTSP数据的开销通常可以忽略不计。
以下是一些示例代码,以说明我的意思:
import android.util.Base64;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
public class RTSPConnection implements Runnable {
private final IRTSPListener listener;
private String targetRtspServerHost;
private String targetRtspServerProto;
private String targetRtspUserInfo;
private String targetRtspServerPath;
private int targetRtspServerPort = -1;
public RTSPConnection(IRTSPListener l, String u) throws RTSPException {
this.listener = l;
if (u == null) {
throw new RTSPException();
}
URI parsedURL;
try {
parsedURL = new URI(u);
} catch (URISyntaxException e) {
throw new RTSPException();
}
targetRtspServerHost = parsedURL.getHost();
if (targetRtspServerHost == null || targetRtspServerHost.equals("")) {
throw new RTSPException();
}
targetRtspServerProto = parsedURL.getScheme();
if (targetRtspServerProto == null) {
targetRtspServerProto = "rtsp";
}
if (!targetRtspServerProto.equalsIgnoreCase("rtsp")) {
throw new RTSPException();
}
targetRtspServerPort = parsedURL.getPort();
if (targetRtspServerPort == -1) {
targetRtspServerPort = 554;
}
targetRtspUserInfo = parsedURL.getUserInfo();
targetRtspServerPath = parsedURL.getPath();
}
@Override
public void run() {
ServerSocket listen;
try {
listen = new ServerSocket(0);
} catch (IOException e) {
}
Socket remote;
try {
remote = new Socket(targetRtspServerHost, targetRtspServerPort);
} catch (IOException e) {
try {
listen.close();
} catch (IOException ignored) {}
return;
}
int listeningOnPort = listen.getLocalPort();
String connectUrl = targetRtspServerProto + "://localhost:" + listeningOnPort + targetRtspServerPath;
listener.onRtspProxyReady(connectUrl);
Socket local;
try {
listen.setSoTimeout(5000);
local = listen.accept();
} catch (IOException e) {
try {
listen.close();
} catch (IOException ignored) {}
try {
remote.close();
} catch (IOException ignored) {}
listener.onRtspProxyError(RTSPError.ProxyTimedOut);
return;
}
Map<String, String> headers = null;
if (targetRtspUserInfo != null) {
String authHeader = "Basic " + Base64.encodeToString(targetRtspUserInfo.getBytes(),
Base64.URL_SAFE | Base64.NO_WRAP);
headers = new HashMap<String, String>();
headers.put("Authorization", authHeader);
}
try {
new ForwardAndAddHeadersThread(local, remote, headers).start();
new PassThru(remote, local).start();
} catch (IOException e) {
try {
local.close();
} catch (IOException ignored) {}
try {
listen.close();
} catch (IOException ignored) {}
try {
remote.close();
} catch (IOException ignored) {}
listener.onRtspProxyError(RTSPError.CouldNotStartProxy);
}
}
private abstract class ForwardTCPThread extends Thread {
protected InputStream in;
protected OutputStream out;
private Socket socket_in, socket_out;
public ForwardTCPThread(Socket i, Socket o) throws IOException {
socket_in = i;
socket_out = o;
in = socket_in.getInputStream();
out = socket_out.getOutputStream();
}
protected void shutdown() {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
if (out != null) {
try {
out.close();
} catch (IOException ignored) {}
}
try {
socket_in.close();
} catch (IOException ignored) {}
try {
socket_out.close();
} catch (IOException ignored) {}
}
}
private class PassThru extends ForwardTCPThread {
public PassThru(Socket in, Socket out) throws IOException {
super(in, out);
setName("Forward TCP");
}
public void run() {
byte[] buf = new byte[4096];
try {
int count;
while ((count = in.read(buf)) > 0) {
out.write(buf, 0, count);
}
} catch (IOException e) {
listener.onRtspProxyError(RTSPError.RemoteConnectionDropped);
}
shutdown();
}
}
public class ForwardAndAddHeadersThread extends ForwardTCPThread {
private final Map<String,String> headers;
public ForwardAndAddHeadersThread(Socket in, Socket out, Map<String,String> headers) throws IOException {
super(in, out);
this.headers = headers;
setName("Forward TCP Add Headers");
}
public void run() {
byte[] buf = new byte[4096];
try {
int count;
while ((count = in.read(buf)) > 0) {
String temp = new String(buf, 0, count, "UTF-8");
String strings[] = temp.split("\r\n",-1);
String str_buf = "";
for (String s: strings) {
str_buf += s + "\r\n";
if (headers != null) {
if (s.contains("CSeq:")) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
str_buf += entry.getKey() + ": " + entry.getValue() + "\r\n";
}
}
}
}
out.write(str_buf.getBytes("UTF-8"), 0, str_buf.length());
}
} catch (IOException e) {
listener.onRtspProxyError(RTSPError.LocalConnectionDropped);
}
shutdown();
}
}
}