本文相关的完整源码下载:http://bbs.rosoo.net/forum.php?mod=viewthread&tid=8535
Recall that RTSP is the actual protocol over which streaming commands are initiated, through which the RTP packets are received. The RTSP protocol is like a command initiator, a bit like HTTP. For a really good explanation of a typical RTSP session, please see these specifications for a simple RTSP client. For the purposes of this article, I am going to oversimplify the protocol implementation. Figure 1 shows the typical RTSP session between a client and a streaming server.
Figure 1. A typical RTSP session between a RTSP client and a streaming server (click for full-size image).
In a nutshell, an RTSP client initiates a session by sending a DESCRIBE request to the streaming server which means that the client wants more information about a media file. An example DESCRIBE request may look like this:
DESCRIBE rtsp://localhost:554/media.3gp rtsp/1.0 CSeq: 1
The URL for the media file is followed by the RTSP version that the client is following, and a carriage return/line feed (CRLF). The next line contains the sequence number of this request and increments for each subsequent request sent to the server. The command is terminated by a single line on its own (as are all RTSP commands).
All client commands that are successful receive a response that starts with RTSP/1.0 200 OK . For the DESCRIBE request, the server responds with several parameters, and if the file is present and streamable, this response contains any information for any tracks in special control strings that start with a a=control:trackID= String. The trackID is important and is used to create the next requests to the server.
Once described, the media file's separate tracks are set up for streaming using the SETUP command, and these commands should indicate the transport properties for the subsequent RTP packets. This is shown here:
SETUP rtsp://localhost:554/media.3gp/trackID=3 rtsp/1.0 CSeq: 2 TRANSPORT: UDP;unicast;client_port=8080-8081
The previous command indicates to the server to set up to stream trackID 3 of the media.3gp file, to send the packets via UDP, and to send them to port 8080 on the client (8081 is for RTCP commands). The response to the first SETUP command (if it is okay) will contain the session information for subsequent commands and must be included as shown here:
SETUP rtsp://localhost:554/media.3gp/trackID=3 rtsp/1.0
CSeq: 3
Session: 556372992204
TRANSPORT: UDP;unicast;client_port=8080-8081
An OK response from the server indicates that you can send the PLAY command, which will make the server start sending the RTP packets:
PLAY rtsp://localhost:554/media.3gp rtsp/1.0 CSeq: 3 Session: 556372992204
Notice that the PLAY command is issued only on the main media file, and not on any individual tracks. The same is true for the PAUSE and TEARDOWN commands, which are identical to the PLAY command, except for the command itself.
The following listing contains the RTSPProtocolHandler class. The comments in the code and the brief information so far should help with understanding how this protocol handler works:
- import java.util.Vector;
- import java.io.InputStream;
- import java.io.IOException;
- import java.io.OutputStream;
-
- public class RTSPProtocolHandler {
-
- private String address;
-
-
- private InputStream is;
-
-
- private OutputStream os;
-
-
-
- private static int CSeq = 1;
-
-
- private String sessionId;
-
-
- private Vector tracks = new Vector(2);
-
-
- private boolean described, setup, playing;
- private Boolean stopped = true;
-
-
- private static final String CRLF = " ";
- private static final String VERSION = "rtsp/1.0";
- private static final String TRACK_LINE = "a=control:trackID=";
- private static final String TRANSPORT_DATA =
- "TRANSPORT: UDP;unicast;client_port=8080-8081";
- private static final String RTSP_OK = "RTSP/1.0 200 OK";
-
-
- public RTSPProtocolHandler(
- String address, InputStream is, OutputStream Os) {
-
- this.address = address;
- this.is = is;
- this.os = Os;
- }
-
-
- public void doDescribe() throws IOException {
-
-
- if(described) return;
-
-
- String baseCommand = getBaseCommand("DESCRIBE " + address);
-
-
- String response = doCommand(baseCommand);
-
-
- parseTrackInformation(response);
-
-
- described = true;
- }
-
-
- public void doSetup() throws IOException {
-
-
- if(!described) throw new IOException("Not Described!");
-
-
- String baseCommand =
- getBaseCommand(
- "SETUP " + address + "/trackID=" + tracks.elementAt(0));
-
-
- baseCommand += CRLF + TRANSPORT_DATA;
-
-
- String response = doCommand(baseCommand);
-
-
- parseSessionInfo(response);
-
-
- if(sessionId == null)
- throw new IOException("Could not find session info");
-
-
- int cntOfTracks = tracks.size();
- for(int i = 1; i < cntOfTracks; i++) {
- baseCommand =
- getBaseCommand(
- "SETUP " + address + "/trackID=" + tracks.elementAt(i));
- baseCommand += CRLF + "Session: " + sessionId + CRLF + TRANSPORT_DATA;
- doCommand(baseCommand);
- }
-
-
- setup = true;
- }
-
-
- public void doPlay() throws IOException {
-
-
- if(!setup) throw new IOException("Not Setup!");
-
-
- String baseCommand = getBaseCommand("PLAY " + address);
-
-
- baseCommand += CRLF + "Session: " + sessionId;
-
-
- doCommand(baseCommand);
-
-
- playing = true;
- stopped = false;
- }
-
-
- public void doPause() throws IOException {
-
-
- if(!playing) return;
-
-
- String baseCommand = getBaseCommand("PAUSE " + address);
-
-
- baseCommand += CRLF + "Session: " + sessionId;
-
-
- doCommand(baseCommand);
-
-
- stopped = true;
- playing = false;
- }
-
-
- public void doTeardown() throws IOException {
-
-
- if(!setup) return;
-
-
- String baseCommand = getBaseCommand("TEARDOWN " + address);
-
-
- baseCommand += CRLF + "Session: " + sessionId;
-
-
- doCommand(baseCommand);
-
-
- described = setup = playing = false;
- stopped = true;
- }
-
-
- private String getBaseCommand(String command) {
-
- return(
- command +
- " " +
- VERSION +
- CRLF +
- "CSeq: " + (CSeq++)
- );
- }
-
-
- private String doCommand(String fullCommand) throws IOException {
-
-
- byte[] buffer = new byte[2048];
-
-
- System.err.println(" ====== CLIENT REQUEST ====== ");
- System.err.println(fullCommand + CRLF + CRLF);
- System.err.println(" ============================ ");
-
-
- os.write((fullCommand + CRLF + CRLF).getBytes());
-
-
- int length = is.read(buffer);
-
- String response = new String(buffer, 0, length);
-
-
- buffer = null;
-
-
- if(!response.startsWith(RTSP_OK))
- throw new IOException("Server returned invalid code: " + response);
-
-
- System.err.println(" ====== SERVER RESPONSE ====== ");
- System.err.println(response.trim());
- System.err.println(" =============================");
-
- return response;
- }
-
-
-
- private void parseTrackInformation(String response) {
-
- String localRef = response;
- String trackId = "";
- int index = localRef.indexOf(TRACK_LINE);
-
-
-
-
- while(index != -1) {
- int baseIdx = index + TRACK_LINE.length();
- trackId = localRef.substring(baseIdx, baseIdx + 1);
- localRef = localRef.substring(baseIdx + 1, localRef.length());
- index = localRef.indexOf(TRACK_LINE);
- tracks.addElement(trackId);
- }
-
- }
-
-
- private void parseSessionInfo(String response) {
-
- sessionId =
- response.substring(
- response.indexOf("Session: ") + "Session: ".length(),
- response.indexOf("Date:")).trim();
- }
- }
(feidragon319) |