Esempio n. 1
0
        public static SDP Parse(string data)
        {
            TimeDescription  currentTd = null;
            MediaDescription currentMd = null;

            try
            {
                SDP sdp = new SDP();

                var lines = Regex.Split(data, CRLF);

                foreach (var line in lines)
                {
                    var trimmed = line.Trim();

                    if (trimmed.StartsWith("v="))
                    {
                        var value = ParseKeyValueLine(trimmed, "Session Version");

                        decimal version;
                        if (!decimal.TryParse(value, out version))
                        {
                            // TODO(frank.lamar): warn.
                        }
                        else
                        {
                            sdp.Version = version;
                        }
                    }
                    else if (trimmed.StartsWith("o="))
                    {
                        sdp.Origin = SessionOriginator.Parse(trimmed);
                    }
                    else if (trimmed.StartsWith("s="))
                    {
                        sdp.SessionName = ParseKeyValueLine(trimmed, "Session Name");
                    }
                    else if (trimmed.StartsWith("i="))
                    {
                        var info = ParseKeyValueLine(trimmed, "Session Information");

                        if (currentMd != null)
                        {
                            // Media Title at the media level
                            currentMd.MediaTitle = info;
                        }
                        else
                        {
                            // Session information at the Session level.
                            sdp.SessionInformation = info;
                        }
                    }
                    else if (trimmed.StartsWith("u="))
                    {
                        var value = ParseKeyValueLine(trimmed, "Session URI");

                        Uri uri;
                        if (!Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out uri))
                        {
                            throw new SdpParseException($"Unable to parse malformed Session URI {value}");
                        }

                        sdp.URI = uri;
                    }
                    else if (trimmed.StartsWith("e="))
                    {
                        sdp.Email = ParseKeyValueLine(trimmed, "Session Email");
                    }
                    else if (trimmed.StartsWith("p="))
                    {
                        sdp.PhoneNumber = ParseKeyValueLine(trimmed, "Session Phone Number");
                    }
                    else if (trimmed.StartsWith("c="))
                    {
                        var connection = ConnectionInfo.Parse(trimmed);

                        if (currentMd != null)
                        {
                            // Media Description connection
                            currentMd.Connection = connection;
                        }
                        else
                        {
                            // Session level connection
                            sdp.Connection = connection;
                        }
                    }
                    else if (trimmed.StartsWith("b="))
                    {
                        var bandwidth = BandwidthInfo.Parse(trimmed);

                        if (currentMd != null)
                        {
                            // Media Description bandwidth
                            currentMd.Bandwidths.Add(bandwidth);
                        }
                        else
                        {
                            // Session level bandwidth
                            sdp.Bandwidths.Add(bandwidth);
                        }
                    }
                    else if (trimmed.StartsWith("t="))
                    {
                        currentTd = TimeDescription.Parse(trimmed);
                        sdp.TimeDescriptions.Add(currentTd);
                    }
                    else if (trimmed.StartsWith("r="))
                    {
                        if (currentTd != null)
                        {
                            currentTd.RepeatTimes.Add(RepeatTime.Parse(trimmed));
                        }

                        // Just ignore the repeate time if no current time was defined.
                    }
                    else if (trimmed.StartsWith("z="))
                    {
                        sdp.TimeZone = TimeZone.Parse(trimmed);
                    }
                    else if (trimmed.StartsWith("a="))
                    {
                        if (currentMd != null)
                        {
                            // Media Description level attribute
                            currentMd.Attributes.Add(Attribute.Parse(trimmed));
                        }
                        else
                        {
                            // Session level attribute
                            sdp.Attributes.Add(Attribute.Parse(trimmed));
                        }
                    }
                    else if (trimmed.StartsWith("m="))
                    {
                        currentMd = MediaDescription.Parse(trimmed);
                        sdp.MediaDescriptions.Add(currentMd);
                    }
                } // end foreach...

                return(sdp);
            }
            catch (Exception e)
            {
                if (e is SdpParseException)
                {
                    throw e;
                }

                throw new SdpParseException("Failed to parse session description", e);
            }
        }