Exemple #1
0
        /// <summary>
        /// OPTIONS
        /// </summary>
        /// <returns></returns>
        private bool OPTIONS()
        {
            string OPTIONS = $@"OPTIONS rtsp://{ServerIP}:{ServerPort}/{UrlFlag} RTSP/1.0
CSeq:1 
User-Agent:{UserAgent}

";

            Console.WriteLine($"发送{OPTIONS}");
            RTSPServer.Send(Encoding.ASCII.GetBytes(OPTIONS));

            byte[] receiveBuffer = new byte[4056];
            int    receiveCount  = RTSPServer.Receive(receiveBuffer);

            if (receiveCount <= 0)
            {
                Console.WriteLine("OPTIONS 没有返回数据");
                return(false);
            }

            byte[] data = new byte[receiveCount];
            Array.Copy(receiveBuffer, 0, data, 0, receiveCount);
            string optionsResponse = Encoding.ASCII.GetString(data);

            Console.WriteLine($"Options回复:{optionsResponse}");
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// TEARDOWN
        /// </summary>
        /// <returns></returns>
        private bool TEARDOWN()
        {
            string TEARDOWN = $@"TEARDOWN rtsp://{ServerIP}:{ServerPort}/{UrlFlag} RTSP/1.0
CSeq: 5
User-Agent:{UserAgent}
Session:{Session}

";

            Console.WriteLine($"发送{TEARDOWN}");
            RTSPServer.Send(Encoding.ASCII.GetBytes(TEARDOWN));

            byte[] receiveBuffer = new byte[4056];
            int    receiveCount  = RTSPServer.Receive(receiveBuffer);

            if (receiveCount <= 0)
            {
                Console.WriteLine("TEARDOWN 没有返回数据");
                return(false);
            }

            byte[] data = new byte[receiveCount];
            Array.Copy(receiveBuffer, 0, data, 0, receiveCount);
            string RECORDResponse = Encoding.ASCII.GetString(data);

            Console.WriteLine($"TEARDOWN:{RECORDResponse}");

            RTSPServer.Close();

            return(true);
        }
Exemple #3
0
        /// <summary>
        /// RECORD
        /// </summary>
        /// <returns></returns>
        private bool RECORD()
        {
            if (string.IsNullOrEmpty(Session))
            {
                return(false);
            }

            string RECORD = $@"RECORD rtsp://{ServerIP}:{ServerPort}/{UrlFlag} RTSP/1.0
Range: npt=0.000-
CSeq:4
User-Agent:{UserAgent}
Session: {Session}

";

            Console.WriteLine($"发送{RECORD}");
            RTSPServer.Send(Encoding.ASCII.GetBytes(RECORD));

            byte[] receiveBuffer = new byte[4056];
            int    receiveCount  = RTSPServer.Receive(receiveBuffer);

            if (receiveCount <= 0)
            {
                Console.WriteLine("RECORD 没有返回数据");
                return(false);
            }

            byte[] data = new byte[receiveCount];
            Array.Copy(receiveBuffer, 0, data, 0, receiveCount);
            string RECORDResponse = Encoding.ASCII.GetString(data);

            Console.WriteLine($"RECORD回复:{RECORDResponse}");

            return(true);
        }
Exemple #4
0
        /// <summary>
        /// ANNOUNCE
        /// </summary>
        /// <returns></returns>
        private bool ANNOUNCE()
        {
            string ANNOUNCEContent = $@"v=0
o=- 0 0 IN IP4 0.0.0.0
s={UrlFlag}
c=IN IP4 {ServerIP}
t=0 0
a=tool:libc6
m=audio 0 RTP/AVP 14
b=AS:320
a=control:streamid=0";

            //长度+2是因为Content前后各有一个换行
            string ANNOUNCE = $@"ANNOUNCE rtsp://{ServerIP}:{ServerPort}/{UrlFlag} RTSP/1.0
Content-Type: application/sdp
CSeq: 2
User-Agent:{UserAgent}
Content-Length:{Encoding.ASCII.GetByteCount(ANNOUNCEContent) + 2}

{ANNOUNCEContent}
";

            Console.WriteLine($"发送{ANNOUNCE}");

            RTSPServer.Send(Encoding.ASCII.GetBytes(ANNOUNCE));

            byte[] receiveBuffer = new byte[4056];
            int    receiveCount  = RTSPServer.Receive(receiveBuffer);

            if (receiveCount <= 0)
            {
                Console.WriteLine("ANNOUNCE 没有返回数据");
                return(false);
            }

            byte[] data = new byte[receiveCount];
            Array.Copy(receiveBuffer, 0, data, 0, receiveCount);
            string ANNOUNCEResponse = Encoding.ASCII.GetString(data);

            Console.WriteLine($"ANNOUNCE回复:{ANNOUNCEResponse}");
            return(true);
        }
Exemple #5
0
        /// <summary>
        /// SETUP
        /// </summary>
        /// <returns></returns>
        private bool SETUP()
        {
            //此处注意..使用了@不转义字符串, 并且RTSP指定每行都需要使用\r\n换行, 最后使用两个\r\n, 前面不要输入Tab, 否则将报错
            string SETUP = $@"SETUP rtsp://{ServerIP}:{ServerPort}/{UrlFlag}/streamid=0 RTSP/1.0
CSeq:3
Transport:RTP/AVP/TCP;unicast;interleaved=0-1;mode=record
User-Agent:{UserAgent}

";

            Console.WriteLine($"发送{SETUP}");
            RTSPServer.Send(Encoding.ASCII.GetBytes(SETUP));

            byte[] receiveBuffer = new byte[4056];
            int    receiveCount  = RTSPServer.Receive(receiveBuffer);

            if (receiveCount <= 0)
            {
                Console.WriteLine("SETUP 没有返回数据");
                return(false);
            }

            byte[] data = new byte[receiveCount];
            Array.Copy(receiveBuffer, 0, data, 0, receiveCount);
            string SETUPResponse = Encoding.ASCII.GetString(data);

            Console.WriteLine($"SETUP回复:{SETUPResponse}");

            StringReader stringReader = new StringReader(SETUPResponse);
            string       line         = "";

            while ((line = stringReader.ReadLine()) != null)
            {
                if (line.StartsWith("Session"))
                {
                    this.Session = line.Split(":")[1].TrimStart().TrimEnd();
                }
            }

            return(true);
        }
Exemple #6
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();
        }
    }