/// <summary>
        /// Parses media from "m" SDP message field.
        /// </summary>
        /// <param name="mValue">"m" SDP message field.</param>
        /// <returns></returns>
        public static SDP_MediaDescription Parse(string mValue)
        {
            SDP_MediaDescription media = new SDP_MediaDescription();

            // m=<media> <port>/<number of ports> <proto> <fmt> ...
            StringReader r = new StringReader(mValue);
            r.QuotedReadToDelimiter('=');

            //--- <media> ------------------------------------------------------------
            string word = r.ReadWord();
            if (word == null)
            {
                throw new Exception("SDP message \"m\" field <media> value is missing !");
            }
            media.m_MediaType = word;

            //--- <port>/<number of ports> -------------------------------------------
            word = r.ReadWord();
            if (word == null)
            {
                throw new Exception("SDP message \"m\" field <port> value is missing !");
            }
            if (word.IndexOf('/') > -1)
            {
                string[] port_nPorts = word.Split('/');
                media.m_Port = Convert.ToInt32(port_nPorts[0]);
                media.m_NumberOfPorts = Convert.ToInt32(port_nPorts[1]);
            }
            else
            {
                media.m_Port = Convert.ToInt32(word);
                media.m_NumberOfPorts = 1;
            }

            //--- <proto> --------------------------------------------------------------
            word = r.ReadWord();
            if (word == null)
            {
                throw new Exception("SDP message \"m\" field <proto> value is missing !");
            }
            media.m_Protocol = word;

            //--- <fmt> ----------------------------------------------------------------
            word = r.ReadWord();
            if (word == null)
            {
                media.m_MediaFormat = "";
            }
            else
            {
                media.m_MediaFormat = word;
            }

            return media;
        }
Esempio n. 2
0
        /// <summary>
        /// Parses SDP from raw data.
        /// </summary>
        /// <param name="data">Raw SDP data.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>data</b> is null reference.</exception>
        public static SDP_Message Parse(string data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            SDP_Message sdp = new SDP_Message();

            StringReader r = new StringReader(data);

            string line = r.ReadLine();

            //--- Read global fields ---------------------------------------------
            while (line != null)
            {
                line = line.Trim();

                // We reached to media descriptions
                if (line.ToLower().StartsWith("m"))
                {
                    /*
                     *  m=  (media name and transport address)
                     *  i=* (media title)
                     *  c=* (connection information -- optional if included at session level)
                     *  b=* (zero or more bandwidth information lines)
                     *  k=* (encryption key)
                     *  a=* (zero or more media attribute lines)
                     */

                    SDP_Media media = new SDP_Media();
                    media.MediaDescription = SDP_MediaDescription.Parse(line);
                    sdp.Media.Add(media);
                    line = r.ReadLine();
                    // Pasrse media fields and attributes
                    while (line != null)
                    {
                        line = line.Trim();

                        // Next media descrition, just stop active media description parsing,
                        // fall through main while, allow next while loop to process it.
                        if (line.ToLower().StartsWith("m"))
                        {
                            break;
                        }
                        // i media title
                        else if (line.ToLower().StartsWith("i"))
                        {
                            media.Title = line.Split(new[] { '=' }, 2)[1].Trim();
                        }
                        // c connection information
                        else if (line.ToLower().StartsWith("c"))
                        {
                            media.ConnectionData = SDP_ConnectionData.Parse(line);
                        }
                        // a Attributes
                        else if (line.ToLower().StartsWith("a"))
                        {
                            media.Attributes.Add(SDP_Attribute.Parse(line));
                        }

                        line = r.ReadLine();
                    }
                    break;
                }
                // v Protocol Version
                else if (line.ToLower().StartsWith("v"))
                {
                    sdp.Version = line.Split(new[] { '=' }, 2)[1].Trim();
                }
                // o Origin
                else if (line.ToLower().StartsWith("o"))
                {
                    sdp.Originator = line.Split(new[] { '=' }, 2)[1].Trim();
                }
                // s Session Name
                else if (line.ToLower().StartsWith("s"))
                {
                    sdp.SessionName = line.Split(new[] { '=' }, 2)[1].Trim();
                }
                // i Session Information
                else if (line.ToLower().StartsWith("i"))
                {
                    sdp.SessionDescription = line.Split(new[] { '=' }, 2)[1].Trim();
                }
                // u URI
                else if (line.ToLower().StartsWith("u"))
                {
                    sdp.Uri = line.Split(new[] { '=' }, 2)[1].Trim();
                }
                // c Connection Data
                else if (line.ToLower().StartsWith("c"))
                {
                    sdp.ConnectionData = SDP_ConnectionData.Parse(line);
                }
                // t Timing
                else if (line.ToLower().StartsWith("t"))
                {
                    sdp.Times.Add(SDP_Time.Parse(line));
                }
                // a Attributes
                else if (line.ToLower().StartsWith("a"))
                {
                    sdp.Attributes.Add(SDP_Attribute.Parse(line));
                }

                line = r.ReadLine().Trim();
            }

            return(sdp);
        }