Ejemplo n.º 1
0
        public string Play()
        {
            string res     = string.Empty;
            string options = SendCommand(BuildCommand("OPTIONS"));

            if (options.Contains("DESCRIBE"))
            {
                string       describe         = SendCommand(BuildCommand("DESCRIBE"));
                RtspResponse responseDescribe = RtspResponse.Parse(describe);
                ParseDescribeResponse(describe);

                string transport = GetTranspotParam();
                string setup     = SendCommand(BuildCommand("SETUP", _videoUri, transport));

                RtspResponse response = RtspResponse.Parse(setup);
                if (response.Status != 200)
                {
                    throw new Exception("RTSP failure in SETUP with status code " + response.Status.ToString());
                }

                _session = response.Session;
                if (response.TransportFields.ContainsKey("SERVER_PORT"))
                {
                    string   serverPortString = response.TransportFields["SERVER_PORT"].ToString();
                    string[] serverPorts      = serverPortString.Split('-');
                    res = string.Format("rtp://{0}:{1}@192.168.10.79:{2}", _uriHost.Host, serverPorts[0], RtpReceivePort);
                    //res = string.Format("rtp://192.168.10.79:{2}", _uriHost.Host, serverPorts[0], RtpReceivePort);
                }
                int sessionTimeout = response.SessionTimeout;
                if (sessionTimeout != -1)
                {
                    _sessionWatchdog.Stop();
                    _sessionWatchdog.Interval = (sessionTimeout * 5000) / 6;
                    _sessionWatchdog.Start();
                }
                //InitRtp();
                string playParam = string.Format("Session: {0}\r\nRange:npt=0.000-\r\n\r\n", _session);
                string play      = SendCommand(BuildCommand("PLAY", _uriHost.ToString(), playParam));
            }
            return(res);
        }
Ejemplo n.º 2
0
        public static RtspResponse Parse(string rawResponse)
        {
            RtspResponse response = new RtspResponse();

            if (rawResponse.Length == 0)
            {
                return(response);
            }

            rawResponse = rawResponse.Replace("\r\n", "\n");

            string firstLine = rawResponse.Substring(0, rawResponse.IndexOf("\n"));

            string[] firstLineParts = firstLine.Split(' ');

            response.Status = Convert.ToInt32(firstLineParts[1]);

            rawResponse = rawResponse.Substring(firstLine.Length + 1);

            int posEndOfHeader = rawResponse.IndexOf("\n\n");

            if (posEndOfHeader == -1)
            {
                return(response);                       //not valid
            }
            string header = rawResponse.Substring(0, posEndOfHeader);

            response.Content = rawResponse.Substring(posEndOfHeader + 2);


            string[] responseLines = header.Split('\n');

            foreach (string responseLine in responseLines)
            {
                if (responseLine.Length < 1)
                {
                    continue;
                }

                string[] headerLineParts = responseLine.Split(':');

                if (headerLineParts.Length < 2)
                {
                    continue;
                }

                switch (headerLineParts[0].ToUpper())
                {
                case "CSEQ":
                    response.CSeq = Convert.ToInt32(headerLineParts[1]);
                    break;

                case "AUTHORIZATION":
                    response.AuthorizationType = headerLineParts[1];
                    break;

                case "TRANSPORT":
                    string[] transportFields = headerLineParts[1].Trim().Split(';');
                    if (transportFields.Length > 0)
                    {
                        response.TransportFields = new Hashtable();
                    }
                    foreach (string p in transportFields)
                    {
                        string[] transportFieldParts = p.Split('=');
                        if (transportFieldParts.Length > 1)
                        {
                            response.TransportFields[transportFieldParts[0].ToUpper()] = transportFieldParts[1];
                        }
                        else
                        {
                            response.TransportFields[transportFieldParts[0].ToUpper()] = null;
                        }
                    }
                    break;

                case "SESSION":
                    string[] sessionLineParts = headerLineParts[1].Trim().Split(';');
                    response.Session = sessionLineParts[0].Trim();
                    if (sessionLineParts.Length > 1 && sessionLineParts[1].ToUpper().Contains("TIMEOUT"))
                    {
                        response.SessionTimeout = Convert.ToInt32(sessionLineParts[1].Substring(sessionLineParts[1].IndexOf('=') + 1));
                    }
                    break;

                default:
                    string key = headerLineParts[0].ToUpper();
                    if (!response.HeaderFields.ContainsKey(key))
                    {
                        response.HeaderFields[key] = headerLineParts[1].Trim();
                    }
                    break;
                }
            }

            if (response.HeaderFields.ContainsKey("CONTENT-TYPE") && response.HeaderFields["CONTENT-TYPE"].ToString() == "application/sdp")
            {
                response.SdpFields = new Sdp();

                //parse sdp
                string[] sdpFields = response.Content.Split('\n');
                foreach (string field in sdpFields)
                {
                    if (field.Length < 3)
                    {
                        continue;
                    }
                    char   code  = field.ToUpper()[0];
                    string value = field.Substring(2);

                    switch (code)
                    {
                    case 'O':
                        response.SdpFields.Origin = value;
                        break;

                    case 'V':
                        response.SdpFields.Version = Convert.ToInt32(value);
                        break;

                    case 'S':
                        response.SdpFields.Subject = value;
                        break;

                    case 'K':
                    {
                        string[] sa = value.Split(':');
                        if (sa.Length == 2)
                        {
                            switch (sa[0])
                            {
                            case "base64":
                                byte[] b = Convert.FromBase64String(sa[1]);
                                response.SdpFields.Key = System.Text.UTF8Encoding.UTF8.GetString(b);
                                break;
                            }
                        }
                    }
                    break;

                    case 'M':
                        string[] mediaDefParts = value.Split(' ');
                        if (mediaDefParts.Length == 4)
                        {
                            response._currentMediaDef = new MediaDef(mediaDefParts[0], Convert.ToInt32(mediaDefParts[1]), mediaDefParts[2], Convert.ToInt32(mediaDefParts[3]));
                            response.SdpFields.MediaDefs.Add(response._currentMediaDef);
                        }
                        break;

                    case 'A':
                        int pos = value.IndexOf(':');

                        if (pos > 0)
                        {
                            string[] attParts = new string[2];
                            attParts[0] = value.Substring(0, pos);
                            attParts[1] = value.Substring(pos + 1);

                            if (response._currentMediaDef != null)
                            {
                                response._currentMediaDef.Attributes[attParts[0].ToUpper()] = attParts[1];
                            }
                            else
                            {
                                response.SdpFields.Attributes[attParts[0].ToUpper()] = attParts[1];
                            }
                        }
                        break;
                    }
                }
            }


            return(response);
        }