* Updated: October 2022
* This code may not be used without written consent of the authors.
package ca.yorku.rtsp.client.net;
Copyright By PowCoder代写 加微信 powcoder
import ca.yorku.rtsp.client.exception.RTSPException;
import ca.yorku.rtsp.client.model.Frame;
import ca.yorku.rtsp.client.model.Session;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
* This class represents a connection with an RTSP server.
public class RTSPConnection {
private static final int BUFFER_LENGTH = 0x10000;
private final Session session;
// TODO Add additional fields, if necessary
* Establishes a new connection with an RTSP server. No message is
* sent at this point, and no stream is set up.
* @param session The Session object to be used for connectivity with the UI.
* @param server The hostname or IP address of the server.
* @param port The TCP port number where the server is listening to.
* @throws RTSPException If the connection couldn’t be accepted,
* such as if the host name or port number
* are invalid or there is no connectivity.
public RTSPConnection(Session session, String server, int port) throws RTSPException {
this.session = session;
* Sets up a new video stream with the server. This method is
* responsible for sending the SETUP request, receiving the
* response and retrieving the session identification to be used
* in future messages. It is also responsible for establishing an
* RTP datagram socket to be used for data transmission by the
* server. The datagram socket should be created with a random
* available UDP port number, and the port number used in that
* connection has to be sent to the RTSP server for setup. This
* datagram socket should also be defined to timeout after 2
* seconds if no packet is received.
* @param videoName The name of the video to be setup.
* @throws RTSPException If there was an error sending or
* receiving the RTSP data, or if the RTP
* socket could not be created, or if the
* server did not return a successful
* response.
public synchronized void setup(String videoName) throws RTSPException {
* Starts (or resumes) the playback of a set up stream. This
* method is responsible for sending the request, receiving the
* response and, in case of a successful response, starting a
* separate thread responsible for receiving RTP packets with
* frames (achieved by calling start() on a new object of type
* RTPReceivingThread).
* @throws RTSPException If there was an error sending or
* receiving the RTSP data, or if the server
* did not return a successful response.
public synchronized void play() throws RTSPException {
private class RTPReceivingThread extends Thread {
* Continuously receives RTP packets until the thread is
* cancelled or until an RTP packet is received with a
* zero-length payload. Each packet received from the datagram
* socket is assumed to be no larger than BUFFER_LENGTH
* bytes. This data is then parsed into a Frame object (using
* the parseRTPPacket() method) and the method
* session.processReceivedFrame() is called with the resulting
* packet. The receiving process should be configured to
* timeout if no RTP packet is received after two seconds. If
* a frame with zero-length payload is received, indicating
* the end of the stream, the method session.videoEnded() is
* called, and the thread is terminated.
public void run() {
* Pauses the playback of a set up stream. This method is
* responsible for sending the request, receiving the response
* and, in case of a successful response, stopping the thread
* responsible for receiving RTP packets with frames.
* @throws RTSPException If there was an error sending or
* receiving the RTSP data, or if the server
* did not return a successful response.
public synchronized void pause() throws RTSPException {
* Terminates a set up stream. This method is responsible for
* sending the request, receiving the response and, in case of a
* successful response, closing the RTP socket. This method does
* not close the RTSP connection, and a further SETUP in the same
* connection should be accepted. Also, this method can be called
* both for a paused and for a playing stream, so the thread
* responsible for receiving RTP packets will also be cancelled,
* if active.
* @throws RTSPException If there was an error sending or
* receiving the RTSP data, or if the server
* did not return a successful response.
public synchronized void teardown() throws RTSPException {
* Closes the connection with the RTSP server. This method should
* also close any open resource associated to this connection,
* such as the RTP connection and thread, if it is still open.
public synchronized void closeConnection() {
* Parses an RTP packet into a Frame object. This method is
* intended to be a helper method in this class, but it is made
* public to facilitate testing.
* @param packet the byte representation of a frame, corresponding to the RTP packet.
* @return A Frame object.
public static Frame parseRTPPacket(DatagramPacket packet) {
return null;
* Reads and parses an RTSP response from the socket’s input. This
* method is intended to be a helper method in this class, but it
* is made public to facilitate testing.
* @return An RTSPResponse object if the response was read
* completely, or null if the end of the stream was reached.
* @throws IOException In case of an I/O error, such as loss of connectivity.
* @throws RTSPException If the response doesn’t match the expected format.
public RTSPResponse readRTSPResponse() throws IOException, RTSPException {
return null; // Replace with a proper RTSPResponse
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com