public bool Options(RTSPRequestParser parser)
        {
            // TODO : Take care of the Uri format
            string media = parser.Media;


            ServerMediaSession session = Server.lookupServerMediaSession(media);

            if (session == null)
            {
                HandleNotFound(parser);
                return(false);
            }



            StringBuilder sb = new StringBuilder();

            sb.Append("RTSP/1.0 200 OK\r\n");
            sb.AppendFormat("Server: {0}\r\n", RTSPServer.ServerVersion);
            sb.AppendFormat("Cseq: {0}\r\n", parser.Headers["CSEQ"]);
            sb.AppendFormat("Public: {0}\r\n", _supportedCommands);
            sb.Append("\r\n");
            SendResponse(sb.ToString());
            return(true);
        }
Exemplo n.º 2
0
        public bool GetParameter(RTSPRequestParser parser)
        {
            string media = parser.Media;


            ServerMediaSession session = _connection.Server.lookupServerMediaSession(media);

            if (session == null)
            {
                _connection.HandleNotFound(parser);
                return(false);
            }
            // Look up information for the specified subsession (track):


            ServerMediaSubsession subsession = session.LookupSubSession(media);

            // Not yet implemented
            //ParseGetParameter(parser);

            StringBuilder sb = new StringBuilder();

            sb.Append("RTSP/1.0 200 OK\r\n");
            sb.AppendFormat("Server: {0}\r\n", RTSPServer.ServerVersion);
            sb.AppendFormat("Cseq: {0}\r\n", parser.Headers["CSEQ"]);
            sb.AppendFormat("Session: {0}\r\n", SessionId);
            sb.Append("Last-Modified: Tue, 30 Aug 2016 01:33:17 GMT\r\n");
            sb.Append("Date: Sat, 22 Dec 2018 16:18:03 GMT\r\n");
            sb.Append("Expires: Sun, 23 Dec 2018 12:01:28 GMT\r\n\r\n");

            string reply = sb.ToString();

            _connection.SendResponse(reply);
            return(true);
        }
Exemplo n.º 3
0
        public async Task <bool> Play(RTSPRequestParser parser)
        {
            string stream = parser.Media;


            ServerMediaSession session = _connection.Server.lookupServerMediaSession(stream);

            if (session == null)
            {
                _connection.HandleNotFound(parser);
                return(false);
            }
            // Look up information for the specified subsession (track):
            ServerMediaSubsession subsession;

            subsession = session.LookupSubSession(stream);
            ushort rtpSeqNum    = 0;
            uint   rtpTimestamp = 0;

            _rtpStream = subsession.GetStreamInstance();

            rtpSeqNum    = _rtpStream.CurrentSeqNo();
            rtpTimestamp = _rtpStream.CurrentTimeStamp();            // presetNextTimestamp();
            Playing      = true;

            await _rtpStream.AddDestination(this, _destinations);

            if (_destinations.isTCP)
            {
                _connection.RegisterFilter(_rtpStream);
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("RTSP/1.0 200 OK\r\n");
            sb.AppendFormat("Server: {0}\r\n", RTSPServer.ServerVersion);
            sb.AppendFormat("Cseq: {0}\r\n", parser.Headers["CSEQ"]);
            sb.AppendFormat("Session: {0}\r\n", parser.Headers["SESSION"]);
            sb.Append("Last-Modified: Tue, 30 Aug 2016 01:33:17 GMT\r\n");
            sb.Append("Cache-Control: must-revalidate\r\n");
            sb.Append("Date: Sat, 22 Dec 2018 16:18:03 GMT\r\n");
            sb.Append("Expires: Sun, 23 Dec 2018 12:01:28 GMT\r\n");
            sb.Append("Range: npt=0.000-\r\n").
            AppendFormat("RTP-Info: url=rtsp://{0}:{1}/{2}/track1;seq={3};rtptime={4}\r\n\r\n",
                         SocketExtensions.GetLocalIPV4Address(),
                         _connection.Server.Port,
                         subsession.Name,
                         rtpSeqNum, rtpTimestamp);

            string reply = sb.ToString();

            _connection.SendResponse(reply);
            return(true);
        }
        private bool SetParameter(RTSPRequestParser parser)
        {
            // Begin by looking up the "ServerMediaSession" object for the specified "urlTotalSuffix":
            string media = parser.Media;


            ServerMediaSession session = Server.lookupServerMediaSession(media);

            if (session == null)
            {
                HandleNotFound(parser);
                return(false);
            }

            // Look up information for the specified subsession (track):
            ServerMediaSubsession subsession;

            media      = media.TrimEnd('/');       // parser.Uri.Segments[2].TrimStart('/');
            subsession = session.LookupSubSession(media);

            string body = parser.Body;

            if (String.IsNullOrEmpty(body))
            {
                HandleInvalidParameter(parser);
            }

            Dictionary <string, string> parameters = new Dictionary <string, string>();

            try
            {
                StringParser bodyParser = new StringParser(body.ToUpper());
                ParseParameters(parser, bodyParser, parameters);
                Server.NewParameters(parameters);
            }
            catch
            {
                return(true);
            }
            StringBuilder sb = new StringBuilder();

            sb.Append("RTSP/1.0 200 OK\r\n");
            sb.AppendFormat("Server: {0}\r\n", RTSPServer.ServerVersion);
            sb.AppendFormat("Cseq: {0}\r\n", parser.Headers["CSEQ"]);
            sb.Append("\r\n");
            SendResponse(sb.ToString());
            return(true);
        }
        private bool Describes(RTSPRequestParser parser)
        {
            // Begin by looking up the "ServerMediaSession" object for the specified "urlTotalSuffix":
            string media = parser.Media;


            ServerMediaSession session = Server.lookupServerMediaSession(media);

            if (session == null)
            {
                HandleNotFound(parser);
                return(false);
            }

            // Look up information for the specified subsession (track):
            ServerMediaSubsession subsession;

            media      = media.TrimEnd('/');       // parser.Uri.Segments[2].TrimStart('/');
            subsession = session.LookupSubSession(media);

            var sdp = subsession.GenerateSDPDescription();


            StringBuilder sb = new StringBuilder();

            sb.Append("RTSP/1.0 200 OK\r\n");
            sb.AppendFormat("Server: {0}\r\n", RTSPServer.ServerVersion);
            sb.AppendFormat("Cseq: {0}\r\n", parser.Headers["CSEQ"]);
            sb.Append("Last-Modified: Tue, 30 Aug 2016 01:33:17 GMT\r\n");
            sb.Append("Cache-Control: must-revalidate\r\n");

            sb.AppendFormat("Content-length: {0}\r\n", sdp.Length);

            sb.Append("Date: Sat, 22 Dec 2018 16:18:03 GMT\r\n");
            sb.Append("Expires: Sat, 22 Dec 2018 16:18:03 GMT\r\n");
            sb.Append("Content-Type: application/sdp\r\n").
            Append("x-Accept-Retransmit: our-retransmit\r\n").
            Append("x-Accept-Dynamic-Rate: 1\r\n").
            AppendFormat("Content-Base: rtsp://{0}:{1}/{2}\r\n\r\n", SocketExtensions.GetLocalIPV4Address(), Server.Port, subsession.Name).
            Append(sdp);

            var str = sb.ToString();

            SendResponse(str);
            return(true);
        }
Exemplo n.º 6
0
        public bool Setup(TcpClient tcpSocket, RTSPRequestParser parser)
        {
            string media = parser.Media;


            ServerMediaSession session = _connection.Server.lookupServerMediaSession(media);

            if (session == null)
            {
                _connection.HandleNotFound(parser);
                return(false);
            }
            // Look up information for the specified subsession (track):


            ServerMediaSubsession subsession = session.LookupSubSession(media);


            Socket socket = null;

            if (String.Compare(parser.TransportType, "RTP/AVP/TCP") == 0)
            {
                socket       = tcpSocket.Client;
                IsRTPOverTCP = true;
            }

            if ((subsession.IsMulticast && parser.TransportMode == "UNICAST") || (!subsession.IsMulticast && parser.TransportMode == "MULTICAST"))
            {
                _connection.HandleUnsupportedTransport(parser);
                return(false);
            }

            int clientRTPPort  = parser.ClientPort1;
            int clientRTCPPort = parser.ClientPort2;

            int    serverRTPPort;
            int    serverRTCPPort;
            string multiCastAddress;
            var    clientAddress = tcpSocket.Client.RemoteEndPoint as IPEndPoint;

            subsession.GetStreamParameters(out serverRTPPort, out serverRTCPPort, out multiCastAddress);

            SetupDestinations(socket, clientAddress, null, clientRTPPort, clientRTCPPort, (byte)parser.Channel1, (byte)parser.Channel2);

            StringBuilder sb = new StringBuilder();

            sb.Append("RTSP/1.0 200 OK\r\n");
            sb.AppendFormat("Server: {0}\r\n", RTSPServer.ServerVersion);
            sb.AppendFormat("Cseq: {0}\r\n", parser.Headers["CSEQ"]);

            var localIP = SocketExtensions.GetLocalIPV4Address();

            if (parser.TransportMode == "UNICAST")
            {
                sb.AppendFormat("Session: {0}\r\n", SessionId);
                sb.Append("Last-Modified: Tue, 30 Aug 2016 01:33:17 GMT\r\n");
                sb.Append("Cache-Control: must-revalidate\r\n");
                sb.Append("Date: Sat, 22 Dec 2018 16:18:03 GMT\r\n");
                sb.Append("Expires: Sun, 23 Dec 2018 12:01:28 GMT\r\n");

                if (socket == null)
                {
                    // UDP
                    sb.AppendFormat("Transport: RTP/AVP;unicast;destination={0};source={1};client_port={2}-{3};server_port={4}-{5};\r\n\r\n",
                                    clientAddress.Address,
                                    localIP,
                                    parser.ClientPort1,
                                    parser.ClientPort2,
                                    serverRTPPort,
                                    serverRTCPPort);
                }
                else
                {
                    // TCP
                    sb.AppendFormat("Transport: RTP/AVP/TCP;unicast;destination={0};source={1};interleaved={2}-{3}\r\n\r\n",
                                    clientAddress.Address,
                                    localIP,
                                    parser.Channel1,
                                    parser.Channel2);
                }
            }
            // Multicast
            else
            {
                sb.Append("Date: Sat, 22 Dec 2018 16:18:03 GMT\r\n");
                sb.AppendFormat("Transport: RTP/AVP;multicast;destination={0};source={1};port={2}-{3};ttl=255\r\n",
                                multiCastAddress,
                                localIP,
                                serverRTPPort,
                                serverRTCPPort).
                AppendFormat("Session: {0};timeout=65\r\n\r\n", SessionId);
            }

            var reply = sb.ToString();

            _connection.SendResponse(reply);
            return(true);
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            var    appSettings = ConfigurationManager.AppSettings;
            string rtspPort    = appSettings["rtspPort"] ?? "8554";
            int    rtsp        = 8554;

            try
            {
                rtsp = Convert.ToInt32(rtspPort);
            }
            catch
            { }
            string resolution = appSettings["cameraResolution1"] ?? "800;600";

            Console.WriteLine("Camera resolution: {0}", resolution);

            var xy = resolution.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            if (xy.Length != 2)
            {
                xy = new string[] { "800", "600" }
            }
            ;
            int width  = 800;
            int height = 600;

            try
            {
                width = Convert.ToInt32(xy[0]);
            }
            catch
            {
                width = 800;
            }

            try
            {
                height = Convert.ToInt32(xy[1]);
            }
            catch
            {
                height = 600;
            }

            Console.WriteLine("Width {0} Height {1}", width, height);


            Server = new RTSPServer(rtsp, (width, height));

            var streamName = "picamera";

            ServerMediaSession unicast = new ServerMediaSession(streamName, streamName, String.Format("Session streamed by \"{0}\"", RTSPServer.ServerVersion));

            unicast.AddSubsession(new H264VideoCaptureMediaSubsession());
            Server.AddServerMediaSession(unicast);

            var multicast = new ServerMediaSession("pimulticast", "pimulticast", String.Format("Session streamed by \"{0}\"", RTSPServer.ServerVersion));

            multicast.AddSubsession(new H264MulticastVideoCaptureSubsession());

            Server.AddServerMediaSession(multicast);


            Server.Run();



            //Server.Shutdown();
        }
    }