You are on page 1of 66

Streaming Video Using RTP and RTSP 1

STREAMING VIDEO USING RTP AND RTSP


DESIGN DOCUMENT

Submitted by:

Shivangi Harit (1238)


M.Sc.(Informatics) 4th Sem
Institute of Informatics & Communication
UNIVERSITY OF DELHI ,SOUTH CAMPUS
( 2009- 11)
Streaming Video Using RTP and RTSP 2

ACKNOWLEDGEMENT

This project has been a learning ground for all of us, not only in terms of
learning new aspects of things we were already acknowledged with, not
only in terms of learning new techniques of getting things done, but also in
the terms of working as a team and working towards a common objective,
and seeing it perform.
We would like to take this opportunity to extend our heartfelt
acknowledgements and regards, to our guide and coordinator, in this regard
and others, Mr. Rajat Budhiraja, under whose guidance and mentoring, we
were able to accomplish the task and the objectives, we initially began
with. The project would not have been possible without the support
provided to us, in terms of infrastructure, and other amenities, by the
campus and its members. We would also like to thank them for the support
and the faith, they invested in us.
We also wish to express our delight, in working with our fine batch of
classmates, with whom you can always find some light moments during
the project.
We also wish to thank each other, for being so supportive a team member and seeing
the project completed, in whatever objectives, that were envisioned in the very
beginning.
Streaming Video Using RTP and RTSP 3

CERTIFICATE

This is to certify that the project titled "Streaming Video Using RTP
& RTSP" submitted by:

Santanu Pal(1237)
Shivangi Harit(1238)
Shoikat Das(1239)
Sumit Kr. Baberwal(1240)

in partial fulfillment of the requirement of the degree of M. Sc. Informatics


is a record of bona-fide work carried out by them under my supervision
and guidance.

Dated April 8, 2011


Mr. Rajat Budhiraja
Institute of Informatics & Communication
University of Delhi South Campus
Streaming Video Using RTP and RTSP 4

TABLE OF CONTENTS
1. Objective……………………………………. 5
2. Abstract…………………………………….. 6
3. List of Figures……………………………….. 7
4. List of Abbreviations……………………….. 7
5. Requirements……………………………….. 8
6. Code………………….……………………. 13
6.1. Protocols Implemented..........……… 14
6.2. Classes……………………………… 19
6.3. Client……………………………….. 21
6.4. Server………………………………. 23
7. Database Requirements……………………. 25
8. Data Flow Diagrams..……………………… 26
9. State Diagram………………………………. 30
10. Designing Modules………………………. 31
11. FCAPS……………………………… . . . . 58
12. How we implemented FCAPS……….……… 61
13. Screenshots……………………………… 62
14. Future Scope………….…………………...64
15. Conclusions………………………………..65
16. References………………………………….66
Streaming Video Using RTP and RTSP 5

1. OBJECTIVE

In this project, we are developing a STREAMING


VIDEO SERVER AND CLIENT which would help
user to view videos without actually downloading it
to user-end. The client can view the database
videos actually transferring the data to his end.

we 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). We are going to
implement the RTSP protocol in the client and
implement the RTP packetization in the server, using java .
Streaming Video Using RTP and RTSP 6

2. ABSTRACT
In this project, we 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). We are going to implement the RTSP protocol in the client and
implement the RTP packetization in the server, using Java. 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 bit stream 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.
Streaming Video Using RTP and RTSP 7

3. List of Figures

Figure 1: Our Interface…………………………...


Figure 2: Length of Packet Header………………
Figure 3: Level 0 DFD………...…………………
Figure 4: Level 1 DFD………...…………………
Figure 5: Level 2 DFDs………...…………………
Figure 6: State Diagram………………………….
Figure 7: Modules and their relations…………….
Figure 8: Description of Modules……………….

4. List of Abbreviations

RTP: Real-time Transfer Protocol


RTSP: Real- Time Streaming Protocol
JPEG: Joint Photographic Experts Group
MJPEG: Motion JPEG
G.U.I.: Graphical User Interface
TCP: Transmission Control
Protocol IP: Internet Protocol
UDP: User Datagram Protocol
SQL: Structure Query Language
PT: Payload Type field
SSRC: Synchronization Source (telephony, real-
time control protocol)
CSRC: Contributing Source
Streaming Video Using RTP and RTSP 8

5. REQUIREMENTS

5.1. Hardware:
 INTEL PENTIUM-IV Processor
 At least 128 MB RAM
 Microprocessor-486 or above

5.2. Software:
 JAVA (jdk 1.6)
 MS Windows Platform
 JAVA SWING

5.3. User Interface:


The user could interact with the software easily as it has very good
G.U.I. (Graphical User Interface). The user has to just perform
clicking events on the functionalities which he has to use.

5.4. Tools and Technology Used:


 Swing (Java)
Swing is a widget toolkit for Java. It is part of Sun
Microsystems' Java Foundation Classes (JFC) — an API for
providing a graphical user interface (GUI) for Java programs.
Swing was developed to provide a more sophisticated set of
GUI components than the earlier Abstract Window Toolkit.
Swing provides a native look and feel that emulates the look
and feel of several platforms, and also supports a pluggable
look and feel that allows applications to have a look and feel
unrelated to the underlying platform. Swing is built on top of
AWT and is entirely written in Java, using AWT’s lightweight component
support. In particular, unlike AWT, t he architecture of Swing components makes
it easy to customize both their appearance and behavior. Components from AWT
and Swing can be mixed, allowing you to add
Streaming Video Using RTP and RTSP 9

Swing support to existing AWT-based programs. For


example, swing components such as JSlider, JButton and
JCheckbox could be used in the same program with standard
AWT labels, textfields and scrollbars. You could subclass the
existing Swing UI, model, or change listener classes without
having to reinvent the entire implementation. Swing also has
the ability to replace these objects on-the-fly.

Anatomy of an Application GUI

GUI Internal Structure

• Socket Programming
In computing, network programming, essentially identical to socket programming
or client-server programming, involves
Streaming Video Using RTP and RTSP 10

writing computer programs that communicate with other


programs across a computer network. The program or
process initiating the communication is called a client
process, and the program waiting for the communication to
be initiated is the server process. The client and server
processes together form a distributed system. The
communication between the client and server process may
either be connection-oriented (such as an established TCP
virtual circuit or session), or connectionless (based on UDP
datagrams).

Sockets are usually implemented by an API library such as


Berkeley sockets, first introduced in 1983. Most
implementations are based on Berkeley sockets, for example
Winsock introduced 1991. Other socket API implementations
exist, such as the STREAMS-based Transport Layer
Interface (TLI).
These are examples of functions or methods typically provided by the API library:
Streaming Video Using RTP and RTSP 11

 socket() creates a new socket of a certain socket type,


identified by an integer number, and allocates system
resources to it.
 bind() is typically used on the server side, and
associates a socket with a socket address structure, i.e.
a specified local port number and IP address.
 listen() is used on the server side, and causes a bound
TCP socket to enter listening state.
 connect() is used on the client side, and assigns a free
local port number to a socket. In case of a TCP socket, it
causes an attempt to establish a new TCP connection.
 accept() is used on the server side. It accepts a received
incoming attempt to create a new TCP connection from
the remote client, and creates a new socket associated
with the socket address pair of this connection.
 send() and recv(), or write() and read(), or recvfrom()
and sendto(), are used for sending and receiving data
to/from a remote socket.
 close() causes the system to release resources allocated
to a socket. In case of TCP, the connection is
terminated.

 TCP/IP/UDP Networking
The TCP/IP model, or Internet Protocol Suite, describes a set
of general design guidelines and implementations of specific
networking protocols to enable computers to communicate
over a network. TCP/IP provides end-to-end connectivity
specifying how data should be formatted, addressed,
transmitted, routed and received at the destination. Protocols
exist for a variety of different types of communication
services between computers.
Streaming Video Using RTP and RTSP 12

TCP/IP is generally described as having four abstraction


layers. Starting from the lowest level, following are the four
layers:
Link Layer
Internet Layer
Transport Layer
Application Layer

User Datagram Protocol is a connectionless datagram


protocol. Like IP, it is a best effort, "unreliable" protocol.
Reliability is addressed through error detection using a weak
checksum algorithm. UDP is typically used for applications
such as streaming media (audio, video, Voice over IP etc)
where on-time arrival is more important than reliability, or
for simple query/response applications like DNS lookups,
where the overhead of setting up a reliable connection is
disproportionately large. Real-time Transport Protocol (RTP)
is a datagram protocol that is designed for real-time data such
as streaming audio and video.
TCP and UDP are used to carry an assortment of higher-level
applications. The appropriate transport protocol is chosen
based on the higher-layer protocol application. For example,
the File Transfer Protocol expects a reliable connection, but
the Network File System (NFS) assumes that the subordinate
Remote Procedure Call protocol, not transport, will guarantee
reliable transfer. Other applications, such as VoIP, can
tolerate some loss of packets, but not the reordering or delay
that could be caused by retransmission.
The applications at any given network address are distinguished by their TCP or
UDP port. By convention certain well known ports are associated with specific
applications.
Streaming Video Using RTP and RTSP 13

Transport Service Requirements of Common Apps

5.5. Front End:


Java (Programming Language)

5.6. Back End:


SQL

6. CODE
We 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), using Java.
Streaming Video Using RTP and RTSP 14

We will implement the RTSP protocol and RTSP de-packetization


in the client and implement the RTSP protocol and RTP
packetization in the server.

6.1. PROTOCOLS IMPLEMENTED


6.1.1. RTP (Real-Time Transport Protocol)

Tool Used: Swing

The Real-time Transport Protocol (RTP) defines a standardized


packet format for delivering audio and video over the Internet.
RTP is used extensively in communication and entertainment
systems that involve streaming media, such as telephony, video
teleconference applications and web-based push to talk features.
For these it carries media streams controlled by H.323, MGCP,
Megaco, SCCP, or Session Initiation Protocol (SIP) signaling
protocols, making it one of the technical foundations of the Voice
over IP industry.

RTP is usually used in conjunction with the RTP Control Protocol (RTCP). While RTP
carries the media streams (e.g., audio and video) or out-of-band events signaling (DTMF
in separate payload type), RTCP is used to monitor transmission statistics and quality
Streaming Video Using RTP and RTSP 15

of service (QoS) information. When both protocols are used in


conjunction, RTP is usually originated and received on even port
numbers, whereas RTCP uses the next higher odd port number.

 Protocol components

The RTP specification describes two sub-protocols:

The data transfer protocol, which deals with the transfer of real-
time multimedia data. Information provided by this protocol
include timestamps (for synchronization), sequence numbers
(for packet loss detection) and the payload format which
indicates the encoded format of the data.

The Real Time Control Protocol (RTCP) is used to specify


Quality of Service (QOS) feedback and synchronization
between the media streams. The bandwidth of RTCP traffic
compared to RTP is small, typically around 5%.

 Sessions

An RTP Session is established for each multimedia stream. A


session consists of an IP address with a pair of ports for RTP
and RTCP. For example, audio and video streams will have
separate RTP sessions, enabling a receiver to deselect a
particular stream.[9] The ports which form a session are
negotiated using other protocols such as RTSP (using SDP in
the setup method) and SIP. According to the specification, an
RTP port should be even and the RTCP port is the next higher
odd port number. RTP and RTCP typically use unprivileged
UDP ports (1024 to 65535),but may use other transport
protocols (most notably, SCTP and DCCP) as well, as the
protocol design is transport independent.
6.1.1. RTSP (Real Time Streaming Protocol)
Streaming Video Using RTP and RTSP 16

The Real Time Streaming Protocol (RTSP) is a network control


protocol designed for use in entertainment and communications
systems to control streaming media servers. The protocol is used to
establish and control media sessions between end points. Clients of
media servers issue VCR-like commands, such as play and pause,
to facilitate real-time control of playback of media files from the
server.

The transmission of streaming data itself is not a task of the RTSP


protocol. Most RTSP servers use the Real-time Transport Protocol
(RTP) for media stream delivery, however some vendors
implement proprietary transport protocols. The RTSP server from
RealNetworks, for example, also features RealNetworks'
proprietary RDT stream transport.
 Protocol directives
Streaming Video Using RTP and RTSP 17

The RTSP protocol has similarities to HTTP, but RTSP adds


new requests. While HTTP is stateless, RTSP is a stateful
protocol. A session identifier is used to keep track of sessions
when needed; thus, no permanent TCP connection is required.
RTSP messages are sent from client to server, although some
exceptions exist where the server will send to the client.

Presented here are the basic RTSP requests. Some typical HTTP
requests, like the OPTIONS request, are also available. The
default transport layer port number is 554.
OPTIONS

An OPTIONS request returns the request types the server will


accept.
DESCRIBE

A DESCRIBE request includes an RTSP URL (rtsp://...), and


the type of reply data that can be handled. The default port for
the RTSP protocol is 554 for both UDP and TCP transports.
This reply includes the presentation description, typically in
Session Description Protocol (SDP) format. Among other
things, the presentation description lists the media streams
controlled with the aggregate URL. In the typical case, there is
one media stream each for audio and video.

SETUP

A SETUP request specifies how a single media stream must be transported. This must
be done before a PLAY request is sent. The request contains the media stream URL
and a transport specifier. This specifier typically includes a local port for receiving
RTP data (audio or video), and another for RTCP data (meta information). The server
reply usually confirms the chosen parameters, and fills in the missing parts, such as
the
Streaming Video Using RTP and RTSP 18

server's chosen ports. Each media stream must be configured


using SETUP before an aggregate play request may be sent.
PLAY

A PLAY request will cause one or all media streams to be


played. Play requests can be stacked by sending multiple PLAY
requests. The URL may be the aggregate URL (to play all
media streams), or a single media stream URL (to play only that
stream). A range can be specified. If no range is specified, the
stream is played from the beginning and plays to the end, or, if
the stream is paused, it is resumed at the point it was paused.

PAUSE

A PAUSE request temporarily halts one or all media streams, so


it can later be resumed with a PLAY request. The request
contains an aggregate or media stream URL. When to pause can
be specified with a range parameter. The range parameter can be
left out to pause immediately.

RECORD

The RECORD request can be used to send a stream to the


server for storage.
TEARDOWN

A TEARDOWN request is used to terminate the session. It stops all media streams
and frees all session related data on the server.
Streaming Video Using RTP and RTSP 19

Protocol stack for multimedia services

6.2. CLASSES
Our code implements the RTSP protocol in the client as well as server and implements
the RTP packetization in the server and
Streaming Video Using RTP and RTSP 20

RTP de-packetization in the client, using Java (Programming


Language).

There are 4 classes in our code.

1. Client

2. Server

3. RTPpacket

4. VideoStream

6.2.1. Client
This class implements the client and the user interface which we
use to send RTSP commands and which is used to display the
video. Below is what the interface look like. Here, we implement
the actions that are taken when the buttons (Setup, Play, Pause, and
Teardown) are pressed.

6.2.2. Server
This class implements the server which responds to the RTSP
requests and streams back the video. Here, we implement the
RTSP interaction and the server calls routines in the RTPpacket
class to packetize the video data.

6.2.3. RTPpacket
This class is used to handle the RTP packets. It has separate
routines for handling the received packets at the client side. The
first constructor of this class implements RTP-packetization of the
video data. The second constructor is used by the client to de-
packetize the data.

6.2.4. VideoStream
Streaming Video Using RTP and RTSP 21

This class is used to read video data from the file on disk.

6.3. CLIENT
Our first task is to implement the RTSP on the client side. To do
this, we will be defining 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. We will
implement the following actions in each handler function.

When the client starts, it also opens the RTSP socket to the server.
This socket is used for sending all RTSP requests.
SETUP

 Create a socket for receiving RTP data and set the timeout on
the socket to 5 milliseconds.
 Send SETUP request to server. We need to insert the
Transport header in which we specify the port for the RTP data
socket we have created.
 Read reply from server and parse the Session header in the
response to get the session ID.
PLAY

 Send PLAY request. We need to insert the Session header


and use the session ID returned in the SETUP response. We do not
put the Transport header in this request.
 Read server's response.

PAUSE
 Send PAUSE request. We insert the Session header and use
the session ID returned in the SETUP response. We do not put the
Transport header in this request.
Streaming Video Using RTP and RTSP 22

• Read server's response.

TEARDOWN

 Send TEARDOWN request. We insert the Session header


and use the session ID returned in the SETUP response. We do not
put the Transport header in this request.
 Read server's response.

It is appropriate to send TEARDOWN when user clicks on the stop-button, because


The TEARDOWN request stops the stream delivery for the given URI, freeing the
resources associated with it. If the URI is the presentation URI for this presentation,
any RTSP session identifier associated with the session is no longer valid. Unless all
transport parameters are defined by the session description, a SETUP request has to
be issued before the session can be played again.
Streaming Video Using RTP and RTSP 23

As per our designed code, the interface will resemble the


following, taking in account the general Windows display:

Fig. 1

6.4. SERVER
On the server we will implement the packetization of the video data into RTP packets.
For this we will creat the packet, set the fields in the packet header, and copy the payload
(i.e., one video frame) into the packet.
Streaming Video Using RTP and RTSP 24

When the server receives 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 calls the first constructor of the class RTPpacket to


perform the encapsulation. Following are the tasks at server’s end:
(the letters in parenthesis refer to the fields in the RTP packet
format below)
1. Set the RTP-version field (V). This will be set to 2.
2. Set padding (P), extension (X), number of contributing
sources (CC), and marker (M) fields. These are all set to zero in
our project.
3. Set payload type field (PT). In this project we use MJPEG
and the type for that is 26.
4. Set the sequence number. The server gives this the sequence
number as the Framenb argument to the constructor.
5. Set the timestamp. The server gives this number as
the Time argument to the constructor.
6. Set the source identifier (SSRC). This field identifies the
server. Any integer value can be taken.

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 diagram below.
Streaming Video Using RTP and RTSP 25

Fig.2

The diagram is in the network byte order (also known as big-


endian). The Java Virtual Machine uses the same byte order.
7. DATABASE REQUIREMENTS

MJPEG (Motion JPEG) format files.

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.
Streaming Video Using RTP and RTSP 26

8. DATA FLOW DIAGRAMS

8.1. Level 0

Fig. 3
Streaming Video Using RTP and RTSP 27

8.2. Level 1

Fig. 4
Streaming Video Using RTP and RTSP 28

8.3. Level 3

8.3.1. Setup

Fig. 5.1
Streaming Video Using RTP and RTSP 29

8.3.2. Play

Fig. 5.2
8.3.3. Pause

Fig. 5.3
Streaming Video Using RTP and RTSP 30

8.3.4. Teardown

Fig. 5.4
9. STATE DIAGRAM

Fig. 6
Streaming Video Using RTP and RTSP 31

10. DESIGNING MODULES

 Module 1: Client
 Module 2: Server
 Module 3: RTPpacket
 Module 4: VideoStream

10.1: Modules and their relations:

Fig. 7
Streaming Video Using RTP and RTSP 32

Description of Modules:
Module 1: Client
Streaming Video Using RTP and RTSP 33

Fig. 8.1
Module 2: Server

Fig. 8.2

Module 3: RTPpacket

Fig. 8.3
Streaming Video Using RTP and RTSP 34

Module 4: VideoStream

Fig. 8.4
CODE:-

Server.java

import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;

public class Server extends JFrame implements ActionListener {


DatagramSocket RTPsocket;
DatagramPacket senddp;
InetAddress ClientIPAddr;
int RTP_dest_port = 0;

//Layout JLabel
label;
Streaming Video Using RTP and RTSP 35

int imagenb = 0;
VideoStream video;
static int MJPEG_TYPE = 26;
static int FRAME_PERIOD = 100;
static int VIDEO_LENGTH = 500;
Timer timer;
byte[] buf;
final static int INIT = 0;
final static int READY = 1;
final static int PLAYING = 2;

final static int SETUP = 3;


final static int PLAY = 4;
final static int PAUSE = 5;
final static int TEARDOWN = 6;

static int state;


Socket RTSPsocket;
static BufferedReader RTSPBufferedReader;
static BufferedWriter RTSPBufferedWriter;
static String VideoFileName;
static int RTSP_ID =
123456; int RTSPSeqNb = 0;
final static String CRLF = "\r\n";

public Server(){
super("Server");
timer = new Timer(FRAME_PERIOD, this);
timer.setInitialDelay(0);
timer.setCoalesce(true);
buf = new byte[15000];

addWindowListener(new WindowAdapter() { public void


windowClosing(WindowEvent e) { timer.stop();
Streaming Video Using RTP and RTSP 36

System.exit(0);
}
};

label = new JLabel("Send frame # ", JLabel.CENTER);


getContentPane().add(label, BorderLayout.CENTER);
}

public static void main(String args[]) throws Exception


{
Server theServer = new Server();
theServer.pack();
theServer.setVisible(true);
int RTSPport = 25000;

ServerSocket listenSocket = new ServerSocket(RTSPport);


theServer.RTSPsocket = listenSocket.accept();
listenSocket.close();
theServer.ClientIPAddr =
theServer.RTSPsocket.getInetAddress();
state = INIT;
RTSPBufferedReader = new BufferedReader(new
InputStreamReader(theServer.RTSPsocket.getInputStream()) );
RTSPBufferedWriter = new BufferedWriter(new
OutputStreamWriter(theServer.RTSPsocket.getOutputStream()) );

int request_type;
boolean done =
false; while(!done)
{
request_type = theServer.parse_RTSP_request(); //blocking

if (request_type == SETUP)
{
done = true;
Streaming Video Using RTP and RTSP 37

state = READY;
System.out.println("New RTSP state:
READY"); theServer.send_RTSP_response();
theServer.video = new VideoStream(VideoFileName);
theServer.RTPsocket = new DatagramSocket();
}
}

while(true)
{
request_type = theServer.parse_RTSP_request(); //blocking

if ((request_type == PLAY) && (state == READY))


{

theServer.send_RTSP_response();
theServer.timer.start();
state = PLAYING;
System.out.println("New RTSP state: PLAYING");
}
else if
((request_type == PAUSE) && (state == PLAYING))
{
theServer.send_RTSP_response();
theServer.timer.stop();
state = READY;
System.out.println("New RTSP state: READY");
}
else if (request_type == TEARDOWN)
{
theServer.send_RTSP_response();
theServer.timer.stop();
theServer.RTSPsocket.close();
theServer.RTPsocket.close(); System.exit(0);
Streaming Video Using RTP and RTSP 38

}
}
}

public void actionPerformed(ActionEvent e)


{ if (imagenb < VIDEO_LENGTH)
{
imagenb++;
try {
int image_length = video.getnextframe(buf);
RTPpacket rtp_packet = new RTPpacket(MJPEG_TYPE,
imagenb, imagenb*FRAME_PERIOD, buf, image_length);

int packet_length = rtp_packet.getlength();

byte[] packet_bits = new byte[packet_length];


rtp_packet.getpacket(packet_bits);
senddp = new DatagramPacket(packet_bits, packet_length,
ClientIPAddr, RTP_dest_port);
RTPsocket.send(senddp);
rtp_packet.printheader();
label.setText("Send frame #" + imagenb);
}
catch(Exception ex)
{
System.out.println("Exception caught:
"+ex); System.exit(0);
}
}
else
{
timer.stop();
}
}
private int parse_RTSP_request()
Streaming Video Using RTP and RTSP 39

{
int request_type = -1;
try
{
String RequestLine = RTSPBufferedReader.readLine();
System.out.println(RequestLine);
StringTokenizer tokens = new
StringTokenizer(RequestLine);
String request_type_string = tokens.nextToken();

if ((new String(request_type_string)).compareTo("SETUP")
== 0)
request_type = SETUP;
else if ((new String(request_type_string)).compareTo("PLAY")
== 0)
request_type = PLAY;
else if ((new
String(request_type_string)).compareTo("PAUSE") == 0)
request_type = PAUSE;
else if ((new
String(request_type_string)).compareTo("TEARDOWN") == 0)
request_type = TEARDOWN;

if (request_type == SETUP)
{
VideoFileName = tokens.nextToken();
}

String SeqNumLine = RTSPBufferedReader.readLine();


System.out.println(SeqNumLine);
tokens = new StringTokenizer(SeqNumLine);
tokens.nextToken();
RTSPSeqNb = Integer.parseInt(tokens.nextToken());

String LastLine = RTSPBufferedReader.readLine();


Streaming Video Using RTP and RTSP 40

System.out.println(LastLine);
if (request_type == SETUP)
{
tokens = new StringTokenizer(LastLine);
for (int i=0; i<3; i++)
tokens.nextToken(); //skip unused stuff
RTP_dest_port = Integer.parseInt(tokens.nextToken());
}
}
catch(Exception ex)
{
System.out.println("Exception caught:
"+ex); System.exit(0);
}
return(request_type);}

private void send_RTSP_response()


{
try{
RTSPBufferedWriter.write("RTSP/1.0 200 OK"+CRLF);
RTSPBufferedWriter.write("CSeq: "+RTSPSeqNb+CRLF);
RTSPBufferedWriter.write("Session: "+RTSP_ID+CRLF);
RTSPBufferedWriter.flush();
}
catch(Exception ex)
{
System.out.println("Exception caught:
"+ex); System.exit(0);
}
}
}
Streaming Video Using RTP and RTSP 41

Client.java import
java.io.*; import
java.net.*; import
java.util.*; import
java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;

public class Client{


JFrame f = new JFrame("Client");
JButton setupButton = new JButton("Setup");
JButton playButton = new JButton("Play");
JButton pauseButton = new JButton("Pause");
JButton tearButton = new JButton("Teardown");
JPanel mainPanel = new JPanel();
JPanel buttonPanel = new JPanel(); JLabel
iconLabel = new JLabel(); ImageIcon icon;
Streaming Video Using RTP and RTSP 42

DatagramPacket rcvdp;
DatagramSocket RTPsocket;
static int RTP_RCV_PORT =
25000; Timer timer;
byte[] buf;

final static int INIT = 0;


final static int READY = 1;
final static int PLAYING = 2;
static int state;
Socket RTSPsocket;

static BufferedReader RTSPBufferedReader;


static BufferedWriter RTSPBufferedWriter;
static String VideoFileName;
int RTSPSeqNb =
0; int RTSPid = 0;
final static String CRLF = "\r\n";
static int MJPEG_TYPE = 26;

public Client() {
f.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};

buttonPanel.setLayout(new GridLayout(1,0)); buttonPanel.add(setupButton);


buttonPanel.add(playButton); buttonPanel.add(pauseButton);
buttonPanel.add(tearButton); setupButton.addActionListener(new
setupButtonListener()); playButton.addActionListener(new
playButtonListener());
Streaming Video Using RTP and RTSP 43

pauseButton.addActionListener(new pauseButtonListener());
tearButton.addActionListener(new tearButtonListener());

iconLabel.setIcon(null);
mainPanel.setLayout(null);
mainPanel.add(iconLabel);
mainPanel.add(buttonPanel);
iconLabel.setBounds(0,0,380,280);
buttonPanel.setBounds(0,280,380,50);

f.getContentPane().add(mainPanel, BorderLayout.CENTER);
f.setSize(new Dimension(390,370));
f.setVisible(true);

timer = new Timer(20, new


timerListener()); timer.setInitialDelay(0);
timer.setCoalesce(true);
buf = new byte[15000];
}

public static void main(String argv[]) throws Exception


{
Client theClient = new Client();
int RTSP_server_port = 25000;
String ServerHost = "127.0.0.1";
InetAddress ServerIPAddr =
InetAddress.getByName(ServerHost);
VideoFileName = "movie.Mjpeg";
theClient.RTSPsocket = new Socket(ServerIPAddr,
RTSP_server_port);

RTSPBufferedReader = new BufferedReader(new


InputStreamReader(theClient.RTSPsocket.getInputStream()) );
Streaming Video Using RTP and RTSP 44

RTSPBufferedWriter = new BufferedWriter(new


OutputStreamWriter(theClient.RTSPsocket.getOutputStream()) );
state = INIT;
}

class setupButtonListener implements


ActionListener{ public void
actionPerformed(ActionEvent e){

if (state == INIT)
{
try{
RTPsocket = new DatagramSocket(RTP_RCV_PORT);
RTPsocket.setSoTimeout(5);
}
catch (SocketException se)
{
System.out.println("Socket exception: "+se);
System.exit(0);
}
RTSPSeqNb = 1;
send_RTSP_request("SETUP");
if (parse_server_response() != 200)
System.out.println("Invalid Server Response");
else
{
state = READY;
System.out.println("New RTSP state: READY");
}
}
}
}

class playButtonListener implements ActionListener { public void


actionPerformed(ActionEvent e){
Streaming Video Using RTP and RTSP 45

if (state == READY)
{
RTSPSeqNb++;
send_RTSP_request("PLAY");
if (parse_server_response() != 200)
System.out.println("Invalid Server Response");
else
{
state = PLAYING;
System.out.println("New RTSP state:
PLAYING"); timer.start();
}
}
}
}

class pauseButtonListener implements ActionListener {


public void actionPerformed(ActionEvent e)
{ System.out.println("Pause Button pressed !");
if (state == PLAYING)
{
RTSPSeqNb++;
send_RTSP_request("PAUSE");
if (parse_server_response() != 200)
System.out.println("Invalid Server Response");
else
{
state = READY;
System.out.println("New RTSP state:
READY"); timer.stop();
}
}
}
}
Streaming Video Using RTP and RTSP 46

class tearButtonListener implements ActionListener


{ public void actionPerformed(ActionEvent e){
RTSPSeqNb++;
send_RTSP_request("TEARDOWN");
if (parse_server_response() != 200)
System.out.println("Invalid Server Response");
else
{
state = INIT;
System.out.println("New RTSP state:
INIT"); timer.stop();
System.exit(0);
}
}
}

class timerListener implements ActionListener


{ public void actionPerformed(ActionEvent e) {
rcvdp = new DatagramPacket(buf, buf.length);

try{ RTPsocket.receive(rcvd
p);
RTPpacket rtp_packet = new RTPpacket(rcvdp.getData(),
rcvdp.getLength());

System.out.println("Got RTP packet with SeqNum #


"+rtp_packet.getsequencenumber()+" TimeStamp
"+rtp_packet.gettimestamp()+" ms, of type
"+rtp_packet.getpayloadtype());

rtp_packet.printheader();
int payload_length = rtp_packet.getpayload_length(); byte [] payload
= new byte[payload_length]; rtp_packet.getpayload(payload);
Streaming Video Using RTP and RTSP 47

Toolkit toolkit = Toolkit.getDefaultToolkit();


Image image = toolkit.createImage(payload, 0,
payload_length);

icon = new ImageIcon(image);


iconLabel.setIcon(icon);
}
catch (InterruptedIOException iioe){
}
catch (IOException ioe)
{ System.out.println("Exception caught:
"+ioe);
}
}
}
private int parse_server_response()
{
int reply_code = 0;

try{
String StatusLine = RTSPBufferedReader.readLine();
System.out.println("RTSP Client - Received from Server:");
System.out.println(StatusLine);

StringTokenizer tokens = new StringTokenizer(StatusLine);


tokens.nextToken();
reply_code = Integer.parseInt(tokens.nextToken());
if (reply_code == 200)
{
String SeqNumLine = RTSPBufferedReader.readLine();
System.out.println(SeqNumLine);

String SessionLine = RTSPBufferedReader.readLine();


System.out.println(SessionLine);

tokens = new StringTokenizer(SessionLine);


Streaming Video Using RTP and RTSP 48

tokens.nextToken(); //skip over the Session:


RTSPid = Integer.parseInt(tokens.nextToken());
}
}
catch(Exception ex)
{
System.out.println("Exception caught :
"+ex); System.exit(0);
}

return(reply_code);
}

private void send_RTSP_request(String request_type)


{
try{ RTSPBufferedWriter.write(request_type
+"
"+VideoFileName+" RTSP/1.0"+CRLF);
RTSPBufferedWriter.write("CSeq: "+RTSPSeqNb+CRLF);

if ((new String(request_type)).compareTo("SETUP") == 0)
RTSPBufferedWriter.write("Transport: RTP/UDP;
client_port=
"+RTP_RCV_PORT+CRLF); else
RTSPBufferedWriter.write("Session: "+RTSPid+"\n");
RTSPBufferedWriter.flush();
}
catch(Exception ex)
{
System.out.println("Exception caught :
"+ex); System.exit(0);
}
}

}
Streaming Video Using RTP and RTSP 49

RTPpacket.java:-

public class RTPpacket{


static int HEADER_SIZE = 12;
public int Version;
public int Padding;
public int Extension;
public int CC; public
int Marker;
public int PayloadType;
public int SequenceNumber;
public int TimeStamp; public
int Ssrc;

public byte[] header; public int


payload_size; public byte[]
payload;
Streaming Video Using RTP and RTSP 50

public RTPpacket(int PType, int Framenb, int Time, byte[] data,


int data_length){
Version = 2;
Padding = 0;
Extension = 0;
CC = 0;
Marker = 0;
Ssrc = 0;
SequenceNumber =
Framenb; TimeStamp =
Time; PayloadType = PType;

header = new byte[HEADER_SIZE];


header[0] = new Integer((Version<<6)|(Padding<<5)|
(Extension<<4)|CC).byteValu e();
header[1] = new Integer((Marker<<7)|
PayloadType).byteValue();

header[2] = new Integer(SequenceNumber>>8).byteValue();


header[3] = new Integer(SequenceNumber).byteValue();
for (int i=0; i < 4; i++)
header[7-i] = new Integer(TimeStamp>>(8*i)).byteValue();

for (int i=0; i < 4; i++)


header[11-i] = new Integer(Ssrc>>(8*i)).byteValue();
payload_size = data_length;
payload = new byte[data_length];
for (int i=0; i < data_length; i++)
payload[i] = data[i];
}

public RTPpacket(byte[] packet, int packet_size)


{
Version = 2;
Streaming Video Using RTP and RTSP 51

Padding = 0;
Extension = 0;
CC = 0;
Marker = 0;
Ssrc = 0;
if (packet_size >= HEADER_SIZE)
{
header = new byte[HEADER_SIZE];
for (int i=0; i < HEADER_SIZE; i++)
header[i] = packet[i];
payload_size = packet_size - HEADER_SIZE;
payload = new byte[payload_size];
for (int i=HEADER_SIZE; i < packet_size; i++)
payload[i-HEADER_SIZE] = packet[i];
PayloadType = header[1] & 127;
SequenceNumber = unsigned_int(header[3]) +
256*unsigned_int(header[2]);
TimeStamp = unsigned_int(header[7]) +
256*unsigned_int(header[6]) + 65536*unsigned_int(header[5]) +
16777216*unsigned_int(header[4]);
}
}

public int getpayload(byte[] data) {

for (int i=0; i < payload_size; i+


+) data[i] = payload[i];

return(payload_size);
}

public int getpayload_length() {


return(payload_size);
}
Streaming Video Using RTP and RTSP 52

public int getlength()


{ return(payload_size +
HEADER_SIZE);
}

public int getpacket(byte[] packet)


{
for (int i=0; i < HEADER_SIZE; i+
+) packet[i] = header[i];
for (int i=0; i < payload_size; i++)
packet[i+HEADER_SIZE] = payload[i];
return(payload_size + HEADER_SIZE);
}

public int gettimestamp() {


return(TimeStamp);
}
public int getsequencenumber()
{ return(SequenceNumber);
}
public int getpayloadtype()
{
return(PayloadType);
}

public void printheader()


{
//TO DO :
uncomment /*
for (int i=0; i < (HEADER_SIZE-4); i++)
{
for (int j = 7; j>=0 ; j--)
if (((1<<j) & header[i] ) != 0)
System.out.print("1");
Streaming Video Using RTP and RTSP 53

else
System.out.print("0");
System.out.print(" ");
}

System.out.println();
*/
}
static int unsigned_int(int nb) {
if (nb >= 0)
return(nb);
else
return(256+nb);
}
}
VideoStream.java:-

import java.io.*;

public class VideoStream


{ FileInputStream fis;
int frame_nb;
public VideoStream(String filename) throws
Exception{ fis = new FileInputStream(filename);
frame_nb = 0;
}

public int getnextframe(byte[] frame) throws Exception


{
int length = 0; String
length_string;
byte[] frame_length = new byte[5];
fis.read(frame_length,0,5);
length_string = new String(frame_length); length =
Integer.parseInt(length_string);
Streaming Video Using RTP and RTSP 54

return(fis.read(frame,0,length));
}
}

GUI.java:-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.net.*;

public class GUI implements ActionListener{


JFrame frame1;
JButton sign_in;
JTextField text;
JPasswordField passwd2;
static boolean valid ;

public GUI() {
valid = false ;
JPanel panel = new JPanel(); panel.setOpaque( false );
panel.setPreferredSize( new Dimension() );
Streaming Video Using RTP and RTSP 55

frame1 = new JFrame("Welcome To Create a New


Account"); frame1.setResizable(false);
Container cp = frame1.getContentPane();
cp.add(panel);
panel.setLayout(null);

passwd2 = new JPasswordField();


passwd2.setBounds(280,375,200,30);
passwd2.setForeground(Color.BLUE);
panel.add(passwd2);

JLabel label1 = new JLabel("user name") ;


label1.setBounds(140,175,200,30);
label1.setForeground(Color.BLUE); label1.setFont(new
Font("Algerian", Font.PLAIN,22));
panel.add(label1);
JLabel label2 = new JLabel("password") ;
label2.setBounds(140,350,310,80);
label2.setForeground(Color.BLUE);
label2.setFont(new Font("Algerian",
Font.PLAIN,25)); panel.add(label2);

text = new JTextField(); // change color


panel.add(text);
//text1.setForeground(Color.BLUE);
//text1.setFont(new Font("Algerian",
Font.PLAIN,19)); text.setBounds(280,175,200,30);

sign_in = new JButton("sign_in");


sign_in.setBounds(290,500,190,35);
//button1.setForeground(Color.BLUE);
panel.add(sign_in);
sign_in.addActionListener(this);

frame1.setSize(550,600);
Streaming Video Using RTP and RTSP 56

frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public void actionPerformed(ActionEvent e)


{ try{
String firstname=””,passwrd="";
String status = "";
passwrd=passwd2.getText();
firstname=text.getText();
System.out.println(firstname);
System.out.println(passwrd);

if(firstname.equals("") ||passwrd.equals(""))
{
JOptionPane.showMessageDialog(null,"One or more fields were
left empty","Error message",JOptionPane.ERROR_MESSAGE);
frame1.dispose();
}
Statement stmt1;
Class.forName("com.mysql.jdbc.Driver");
Connection con = null;
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
AVStreaming","root","root");
stmt1=con.createStatement();

String query1,query2,name,pass1;
ResultSet rs = stmt1.executeQuery("select DesiredLoginName,
passwd FROM userinfo");
while ( rs.next()) {
Streaming Video Using RTP and RTSP 57

//System.out.println( rs.getString("loginame") + ":" +


rs.getString("passwrd"));
name=rs.getString("DesiredLoginName");
pass1=rs.getString("passwd");
if(name.equals(firstname) && pass1.equals(passwrd)){
valid = true ;
System.out.println("invalid user name name or passwd");
break;
}
}

if(valid){

JOptionPane.showMessageDialog(null,"you are
authenticated","Error
message",JOptionPane.ERROR_MESSAGE);
new Client();
frame1.dispose();
}
else{
JOptionPane.showMessageDialog(null,”authentication
failure","Error message",JOptionPane.ERROR_MESSAGE);

}
}catch(Exception m){}
}
public static void main(String args[]){

GUI obj = new GUI();


}
}
Streaming Video Using RTP and RTSP 58

11. FCAPS
FCAPS is the ISO Telecommunications Management Network
model and framework for network management. FCAPS is an
acronym for Fault, Configuration, Accounting, Performance,
Security, the management categories into which the ISO model
defines network management tasks. In non-billing organizations
Accounting is sometimes replaced with Administration.

The comprehensive management of an organization's


information technology (IT) infrastructure is a fundamental
requirement. Employees and customers rely on IT services where
availability and performance are mandated, and problems can be
quickly identified and resolved. Mean time to repair (MTTR)
must be as short as possible to avoid system downtimes where a
loss of revenue or lives is possible.

Fault management

A fault is an event that has a negative significance. The goal of


fault management is to recognize, isolate, correct and log faults
that occur in the network. Furthermore, it uses trend analysis to
predict errors so that the network is always available. This can be
established by monitoring different things for abnormal behavior.

When a fault or event occurs, a network component will often


send a notification to the network operator using a proprietary or
open protocol such as SNMP, or at least write a message to its
console for a console server to catch and log/page. This
notification is supposed to trigger manual or automatic activities.
For example, the gathering of more data to identify the nature and
severity of the problem or to bring backup equipment on-line.

Fault logs are one input used to compile statistics to determine the provided service
level of individual network elements, as well as sub-networks or the whole network.
They are also used to
Streaming Video Using RTP and RTSP 59

determine apparently fragile network components that


require further attention.

Fault Isolation tools like Delphi are also available, which are
basically used to isolate the fault in any telecom network.
Configuration management

The goals of configuration management include:

 To gather and store configurations from network devices


(this can be done locally or remotely).
 to simplify the configuration of the device
 to track changes that are made to the configuration
 to configure ('provision') circuits or paths through non-
switched networks

Accounting management

Accounting is often referred to as billing management. The goal


is to gather usage statistics for users.

Using the statistics the users can be billed and usage quota can
be enforced.
Examples:

 Disk usage
 Link utilization
 CPU time
Streaming Video Using RTP and RTSP 60

For non-billed networks, "administration" replaces "accounting".


The goals of administration are to administer the set of authorized
users by establishing users, passwords, and permissions, and to
administer the operations of the equipment such as by performing
software backup and synchronization.
Performance management

Performance management enables the manager to prepare the


network for the future, as well as to determine the efficiency of the
current network, for example, in relation to the investments done to
set it up. The network performance addresses the throughput,
percentage utilization, error rates and response time areas.

By collecting and analyzing performance data, the network health


can be monitored. Trends can indicate capacity or reliability
issues before they become service affecting.

Performance thresholds can be set in order to trigger an alarm. The


alarm would be handled by the normal fault management process
.Alarms vary depending upon the severity.

Security management

Security management is the process of controlling access to assets in the network. Data
security can be achieved mainly with authentication and encryption. Authorization to it
configured with OS and DBMS access control settings. This level of management will
help reduce the possibility of sabotage or unauthorized access into the network by illegal
users. This part involves the authentication and authorization of users as well as the
encryption of data to protect the confidentiality of user information and rights.
Streaming Video Using RTP and RTSP 61

12. How we implemented FCAPS


Security management
in our project, a video can only be streamed by the user when he
logins. If the user is not a eligible person, then that persons
login fails and that person is not authorized to see the video.
Configuration Management
In our project we can define the port at which the socket is to be
formed. Also we can define the server address at which the
streaming is going on. We can also define the videos which are to
be made available for streaming.

Accounting
We can determine which all person are using our server to stream the video from the
list of names from database which we are maintaining.
Streaming Video Using RTP and RTSP 62

13. SCREEN SHOTS


Streaming Video Using RTP and RTSP 63
Streaming Video Using RTP and RTSP 64

14. FUTURE SCOPE

 The standard media player, such as RealPlayer or Windows


Media Player, allows only three 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, we can implement
that in a media player in future.

 Currently the client and server only implement the minimum


necessary RTSP interactions and PAUSE. Implementation of a
method DESCRIBE can be used to pass information about the
media stream. When the server receives 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.
Streaming Video Using RTP and RTSP 65

15. CONCLUSION

Our project will make video and audio data sharing convenient and
faster due to facility of streaming instead of (actually)
downloading the video.

Live video streaming can also help monitor remote locations. At


first glance, this is similar to typical security cameras, except that
streaming technology does not need closed circuits. The video can
be monitored from virtually anywhere in the world, providing there
is an Internet connection nearby.

The advantages of using a streaming server include detecting the client's connection
speed so that video files of an appropriate bit rate are delivered. This spares the end user
the necessity of choosing between various formats.
Streaming Video Using RTP and RTSP 66

16. REFERENCES

Books-
 Computer Networks, Andrew S Tanenbaum, Fourth
Edition PHI.

Websites-
http://java.sun.com/docs/books/tutorial/networking/ov
erview/networking.html
http://tools.ietf.org/html/rfc2326 , Real Time
Streaming Protocol (RTSP), IETF, 1998
 http://all-streaming-media.com/faq/streaming-
media/faqstreaming-media-protocols.htm
 http://java.sun.com/developer/Books/jdbc/ch07.pdf
PDF to Word

You might also like