Streaming video with RTSP and RTP

Source: Internet
Author: User
Tags rfc
The Code

In this lab you will implement a streaming video server and client that communicate using the Real-Time Streaming Protocol (RTSP) and send data using the Real-Time Transfer Protocol (RTP ). your task is to implement the RTSP protocol in the client and implement the RTP packetization in the server.

We will provide you code that implements the RTSP protocol in the server, the RTP de-packetization in the client, and takes care of displaying the transmitted video. you do not need to touch this code.

Classes

There are 4 classes in the assignment.

Client
This class implements the client and the user interface which you use to send RTSP commands and which is used to display the video. below is what the interface looks like. You will need to implement the actions that are taken when the buttons are pressed.
Server
This class implements the server which responds to the RTSP requests and streams back the video. the RTSP interaction is already implemented and the server callroutines in the rtppacket class to packetize the video data. you do not need to modify this class.
Rtppacket
This class is used to handle the RTP packets. it has separate routines for handling the specified ed packets at the client side which is given and you do not need to modify it (but see optional exercises ). You will need to complete the first constructor of this class to implement RTP-packetization of the video data. The second constructor is used by the client to de-packetize the data. You do not need to modify that.
Videostream
This class is used to read video data from the file on disk. You do not need to modify this class.
Running the code

After completing the code, you can run it as follows.

First, start the server with the command

        java Server server_port

WhereServer_portIs the port your server listens to for incoming RTSP connections. The standard RTSP port is 554, but you will need to choose a port number greater than 1024.

Then, start the client with the command

        java Client server_name server_port video_file

WhereServer_hostIs the name of the machine where the server is running,Server_portIs the port the server is listening on, andVideo_fileIs the name of the file you want to request (we have provided one example FileMovie. MJPEG). The file format is described in the Appendix.

The client opens a connection to the server and pops up a window like this:

You can send RTSP commands to the server by pressing the buttons. A normal RTSP interaction goes as follows.

  1. Client sends setup. This command is used to set up the session and transport parameters.
  2. Client sends play. This starts the playback.
  3. Client may send pause if it wants to pause during playback.
  4. Client sends teardown. This terminates the session and closes the connection.

The server alway replies to all the messages the client sends. the reply codes are roughly the same as in HTTP. the Code 200 means that the request was successful. in this lab you do not need to implement any other reply codes. for more information about RTSP, please see RFC 2326.

1. Client

Your first task is to implement the RTSP on the client side. to do this, you need to complete the functions that are called when the user clicks on the buttons in the user interface. for each button in the interface there is a handler function in the code. you will need to implement the following actions in each handler function.

When the client starts, it also opens the RTSP socket to the server. Use this socket for sending all RTSP requests.

Setup
  • Create a socket for processing ing RTP data and set the timeout on the socket to 5 milliseconds.
  • Send SETUP Request to server. You will need to insert the Transport Header in which you specify the port for the RTP data socket you just created.
  • Read reply from server and parse the session header in the response to get the session ID.
Play
  • Send play request. You must insert the session header and use the session ID returned in the setup response. You must not put the Transport Header in this request.
  • Read server's response.
Pause
  • Send pause request. You must insert the session header and use the session ID returned in the setup response. You must not put the Transport Header in this request.
  • Read server's response.
Teardown
  • Send teardown request. You must insert the session header and use the session ID returned in the setup response. You must not put the Transport Header in this request.
  • Read server's response.

Note: You must insert the CSeq Header in every request you send. The value of the CSeq Header is a number which is incremented by one for each request you send.

Example

Here is a sample interaction between the client and server. The client's requestes are marked with C: And server's replies with S:. In this lab both the client and the server areVery simple. They do not have sophisticated parsing routines and theyPlease CT the header fields to be in the order you see below. That is, in a request, the first header is CSeq, and the second one is either transport (for setup) or session (for all other requests ). in the reply, CSeq is again the first and session is the second.

C: SETUP movie.Mjpeg RTSP/1.0C: CSeq: 1C: Transport: RTP/UDP; client_port= 25000S: RTSP/1.0 200 OKS: CSeq: 1S: Session: 123456C: PLAY movie.Mjpeg RTSP/1.0C: CSeq: 2C: Session: 123456S: RTSP/1.0 200 OKS: CSeq: 2S: Session: 123456C: PAUSE movie.Mjpeg RTSP/1.0C: CSeq: 3C: Session: 123456S: RTSP/1.0 200 OKS: CSeq: 3S: Session: 123456C: PLAY movie.Mjpeg RTSP/1.0C: CSeq: 4C: Session: 123456S: RTSP/1.0 200 OKS: CSeq: 4S: Session: 123456C: TEARDOWN movie.Mjpeg RTSP/1.0C: CSeq: 5C: Session: 123456S: RTSP/1.0 200 OKS: CSeq: 5S: Session: 123456
CLIENT STATE

One of the key differences between HTTP and RTSP is that in RTSP each session has a state. in this lab you will need to keep the client's state up-to-date. client changes state when it has es a reply from the server according to the following State disince.

2. Server

On the server you will need to implement the packetization of the video data into RTP packets. for this you will need to create the packet, set the fields in the packet header, and copy the payload (I. E ., one video frame) into the packet.

When the server has es the play-request from the client, it starts a timer which is triggered every 100 ms. at these times the server will read one video frame from the file and send it to the client. the server creates an rtppacket-object which is the RTP-encapsulation of the video frame.

The server callthe first constructor of the class rtppacket to perform the encapsulation. your task is to write this function. you will need to do the following: (the letters in parenthesis refer to the fields in the RTP packet format below)

  1. Set the RTP-version field (V). You must set this to 2.
  2. Set padding (P), extension (x), number of contributing sources (CC), and marker (m) fields.These are all set to zero in this lab.
  3. Set payload type field (PT). In this lab we use MJPEG and the type for that is 26.
  4. Set the sequence number. The server gives this the sequence number asFramenbArgument to the constructor.
  5. Set the timestamp. The server gives this number asTimeArgument to the constructor.
  6. Set the source identifier (SSRC). This field identifies the server. You can pick any integer value you like.

Because we have no other contributing sources (field cc = 0), the CSRC-field does not exist. the length of the packet header is therefore 12 bytes, or the first three lines from the dimo-below.

    0                   1                   2                   3    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   |V=2|P|X|  CC   |M|     PT      |       sequence number         |   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   |                           timestamp                           |   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   |           synchronization source (SSRC) identifier            |   +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+   |            contributing source (CSRC) identifiers             |   |                             ....                              |   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

You must fill in the header in the arrayHeaderOf the rtppacket-class. You will also need to copy the payload (given as argumentData) To the variablePayload. The length of the payload is given in the argumentData_length.

The above digoal is in the network byte order (also known as Big-Endian ). the Java virtual machine uses the same byte order so you do not need to transform your packet header into the network byte order.

For more details on RTP, please see RFC 1889.

Twiddling the bits

Here are some examples on how to set and check individual bits or groups of bits. note that in the RTP packet header format smaller bit-Numbers refer to higher order bits, that is, bit number 0 of a byte is 2 ^ 7 and bit number 7 is 1 (or 2 ^ 0 ). in the examples below, the bit numbers refer to the numbers in the upper limit.

Because the header-field of the rtppacket class is an array of TypeByte, You will need to set the header one byte at a time, that is in groups of 8 bits. the first byte has bits 0-7, the second byte has bits 8-15, and so on. in Java an int is 32 bits or 4 bytes.

To set bit numberNIn VariableMybyteOf type byte:

        mybyte = mybyte | 1 << (7 - n);

To set bitsNAndN + 1To the valueFooIn VariableMybyte:

       mybyte = mybyte | foo << (7 - n);

Note thatFooMust have a value that can be expressed with 2 bits, that is, 0, 1, 2, or 3.

To copy a 16-bit integerFooInto 2 bytes,B1AndB2:

       b1 = foo >> 8;       b2 = foo & 0xFF;

After this,B1Will have the 8 high-order bitsFooAndB2Will have the 8 low-order bitsFoo.

You can copy a 32-bit integer into 4 bytes in a similar way.

If you're not comfortable setting bits, you can find more information in the Java tutorial.

Bit example

Suppose we want to fill in the first byte of the RTP packet header with the following values:

  • V = 2
  • P = 0
  • X = 0
  • Cc = 3

In binary this wocould be represented

        1 0 | 0 | 0 | 0 0 1 1        V=2   P   X   CC = 3       2^7 . . . . . . . . 2^0
Optional Exercises
  • Instead of the normal server given to you, use the class calledFunkyserver(Download also the class funkyserver $1. Class), I. e., run itJava funkyserver server_port. What do you see at the client? Explain what happens, why, and fix it.
  • Calculate statistics about the session. You will need to calculate RTP packet loss rate, video data rate (in bits or bytes per second), and any other interesting statistics you can think.
  • The user interface on the client has 4 buttons for the 4 actions. if you compare this to a standard media player, such as RealPlayer or Windows Media Player, you can see that they have only 3 buttons for the same actions, namely, play, pause, and stop (roughly corresponding to teardown ). there is no setup-button available to the user. given that setup is mandatory in an RTSP-interaction, how wowould You implement that in a media player? When does the client sends the setup? Come up with a solution and implement it. Is it appropriate to send teardown when user clicks on the Stop-button?
  • Currently the client and server only implement the minimum necessary RTSP interactions and pause. implement the method describe which is used to pass information about the media stream. when the server has es a describe-request, it sends back a session description file which tells the client what kinds of streams are in the session and what encodings are used.
Appendix

Lab's proprietary MJPEG (motion JPEG) format.

In this lab, the server streams a video which has been encoded into a proprietary MJPEG file format. this format stores the video as concatenated JPEG-encoded images, with each image being preceded by a 5-byte header which indicates the bit size of the image. the server parses the bitstream of the mjpeg file to extract the JPEG images on the fly. the server sends the images to the client at periodic intervals. the client then displays the individual JPEG images as they arrive from the server.


Http://www.csee.umbc.edu /~ Pmundur/courses/cmsc691c/lab5-kurose-ross.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.