This documentation is archived and is not being maintained.

Parsing and Constructing SDP Documents

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Use the Sdp class in the Unified Communications Managed API version 1.0 SDK to parse and construct session descriptions to be carried in a Session Initiation Protocol (SIP) request or response. These session descriptions are defined by the Session Description Protocol (SDP). For more information about SDP, see the SDP: Session Description Protocol RFC.

An application working directly with the signaling layer of Unified Communications Managed API version 1.0 needs to parse and serialize SDPs in order to establish an audio/video session with its peer. The application receives a media description from the remote, and verifies that the content type is application/sdp. At this point it is necessary to understand what the remote is offering, but the author has only an array of bytes. Use the Sdp class to parse the array of bytes, so that it can be validated and interpreted.

Once the application determines an appropriate response, use the Sdp class to generate an array of bytes containing a properly formatted SDP to send as an answer.

Use the TryParse method to determine if the SDP is valid. Subclass the SdpMediaDescription and SdpGlobalDescription classes to get the values of session description attributes. See the following sample code.

public void SetAnswer(object sender, ContentDescription answer)
{
  SignalingSession Session = sender as SignalingSession;

  // Verify that the answer received is consistent
  byte[] Answer = answer.GetBody();

  if (Answer != null)
  {
    Sdp<SdpGlobalDescription, SdpMediaDescription> SessionDescription = new Sdp<SdpGlobalDescription, SdpMediaDescription>();

    if (!SessionDescription.TryParse(Answer))
    {
      Session.BeginTerminate(null, Session);
      return;
    }

    else
    {
      IList<SdpMediaDescription> ActiveMediaTypes = SessionDescription.MediaDescriptions;
      if ((ActiveMediaTypes.Count == 1) && (ActiveMediaTypes[0].MediaName.Equals("message", StringComparison.Ordinal)) && (ActiveMediaTypes[0].Port > 0) && (ActiveMediaTypes[0].TransportProtocol.Equals("sip", StringComparison.OrdinalIgnoreCase)))
      {
      }

      else
      {
        Session.BeginTerminate(null, Session);
      }
    }
  }
}

Use the Sdp, SdpMediaDescription, and SdpGlobalDescription classes to set the values of session description attributes. When the Sdp class is initially constructed it will have default values as specified in the Microsoft.Rtc.Signaling Help file. See the following sample code.

public ContentDescription GetOffer(object sender)
{
  SignalingSession Session = sender as SignalingSession;
  IPAddress IpAddress;

  // This method is called back every time an outbound INVITE is sent.
  if (Session.Connection != null)
  {
    IpAddress = Session.Connection.LocalEndpoint.Address;
  }

  else
  {
    IpAddress = IPAddress.Any;
  }

  Sdp<SdpGlobalDescription, SdpMediaDescription> SessionDescription = new Sdp<SdpGlobalDescription, SdpMediaDescription>();

  //Set the origin line of the SDP
  //s, t, and v lines are automatically constructed
  SessionDescription.GlobalDescription.Origin.Version = 0;
  SessionDescription.GlobalDescription.Origin.SessionId = "0";
  SessionDescription.GlobalDescription.Origin.UserName = "-";
  SessionDescription.GlobalDescription.Origin.Connection.Set(IpAddress.ToString());

  //Set the connection line
  SessionDescription.GlobalDescription.Connection.TrySet(IpAddress.ToString());

  SdpMediaDescription mditem = new SdpMediaDescription("message");

  mditem.Port = 5060;
  mditem.TransportProtocol = "sip";
  mditem.Formats = "null";

  SdpAttribute aitem = new SdpAttribute("accept-types", "text/plain");

  mditem.Attributes.Add(aitem);

  //Append the Media description to the Global description
  SessionDescription.MediaDescriptions.Add(mditem);

  ContentType ct = new ContentType("application/sdp");

  return new ContentDescription(ct, SessionDescription.GetBytes());
}
Show: