Programming Interfaces Guide

Creating the SDP Session Structure

The first step in creating a new SDP session structure is allocating memory for the new structure by calling the sdp_new_session() function. This function returns a pointer to the new session structure. The other functions in this section use that pointer to construct the new session structure. Once you are done constructing the new session structure, convert it to a string representation with the sdp_session_to_str() function.

Creating a New SDP Session Structure

sdp_session_t *sdp_new_session();

The sdp_new_session() function allocates memory for a new SDP session structure that is specified by the session parameter and assigns a version number to that new structure. You can free the memory that is allocated to the session structure by calling the sdp_free_session() function.

Return Values: The sdp_new_session() function returns the newly allocated SDP session structure when the function completes successfully. The function returns NULL in the case of failure.

Adding an Origin Field to the SDP Session Structure

int sdp_add_origin(sdp_session_t *session, const char *name, uint64_t id, uint64_t ver, const char *nettype, const char *addrtype, const char *address);

The sdp_add_origin() function adds the ORIGIN (o=) SDP field to the session structure that is specified by the value of the session parameter (sdp_session_t) using the name, id, ver, nettype, addrtype, and address parameters.

Return Values: The sdp_add_origin() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a Name Field to the SDP Session Structure

int sdp_add_name(sdp_session_t *session, const char *name);

The sdp_add_name() function adds the NAME (s=) SDP field to the session structure that is specified by the value of the session parameter (sdp_session_t) using the name parameter.

Return Values: The sdp_add_name() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding an Information Field to the SDP Session Structure

int sdp_add_information(char **information, const char *value);

The sdp_add_information() function adds the INFO (i=) SDP field to the session structure (sdp_session_t) or the media structure (sdp_media_t) using the value parameter. This field can go into the media or the session section of an SDP description. You must pass either &session->s_info or &media->m_info as the first argument to specify the section.

Return Values: The sdp_add_information() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a URI Field to the SDP Session Structure

int sdp_add_uri(sdp_session_t *session, const char *uri);

The sdp_add_uri() function adds the URI (u=) SDP field to the session structure that is specified by the value of the session parameter (sdp_session_t) using the uri parameter.

Return Values: The sdp_add_uri() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding an Email Field to the SDP Session Structure

int sdp_add_email(sdp_session_t *session, const char *email);

The sdp_add_email() function adds the EMAIL (e=) SDP field to the session structure that is specified by the value of the session parameter (sdp_session_t) using the email parameter.

Return Values: The sdp_add_email() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a Telephone Field to the SDP Session Structure

int sdp_add_phone(sdp_session_t *session, const char *email);

The sdp_add_phone() function adds the PHONE (p=) SDP field to the session structure that is specified by the value of the session parameter (sdp_session_t) using the phone parameter.

Return Values: The sdp_add_phone() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a Connection Field to the SDP Session Structure

int sdp_add_connection(sdp_conn_t **conn, const char *nettype, const char *addrtype, const char *address, uint8_t ttl, int addrcount);

The sdp_add_connection() function adds the CONNECTION (c=) SDP field to either the session structure (sdp_session_t) or the media structure (sdp_media_t) using the nettype, addrtype, address, ttl, and addrcount parameters. For IPv4 or IPv6 unicast addresses, set the values of the ttl and addrcount parameters to zero. For multicast addresses, set the value of the ttl parameter between zero and 255. A multicast address cannot have an addrcount parameter with a value of zero.

This field can go into the media or the session section of an SDP description. You must pass either &session->s_info or &media->m_info as the first argument to specify the section.

Return Values: The sdp_add_connection() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a Bandwidth Field to the SDP Session Structure

int sdp_add_bandwidth(sdp_bandwidth_t **bw, const char *type, uint64_t value);

The sdp_add_bandwidth() function adds the BANDWIDTH (b=) SDP field to either the session structure (sdp_session_t) or the media structure (sdp_media_t) using the type and value parameters.

This field can go into the media or the session section of an SDP description. You must pass either &session->s_info or &media->m_info as the first argument to specify the section.

Return Values: The sdp_add_bandwidth() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a Time Field to the SDP Session Structure

int sdp_add_time(sdp_session_t *session, uint64_t starttime, uint64_t stoptime, sdp_time_t **time);

The sdp_add_time() function adds the TIME (t=) SDP field to the session structure using the values of the starttime and stoptime parameters. This function creates a new time structure and returns the pointer to that structure in the time parameter.

Return Values: The sdp_add_time() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a Repeat Field to the SDP Session Structure

int sdp_add_repeat(sdp_time_t *time, uint64_t interval, uint64_t duration, const char *offset);

The sdp_add_repeat() function adds the REPEAT (r=) SDP field to the session structure using the values of the interval, duration, and offset parameters. The value of the offset parameter is a string that holds one or more offset values, such as 60 or 60 1d 3h. The value of the time parameter is the pointer to the time structure that the sdp_add_time() function creates.

Return Values: The sdp_add_repeat() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a Zone Field to the SDP Session Structure

int sdp_add_zone(sdp_session_t *session, uint64_t time, const char *offset);

The sdp_add_zone() function adds the ZONE (z=) SDP field to the session structure that is specified by the value of the session parameter (sdp_session_t) using the time and offset parameters. You can add multiple time and offset values for a single zone field by calling this function for each time/offset value pair.

Return Values: The sdp_add_zone() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a Key Field to the SDP Session Structure

int sdp_add_key(sdp_key_t **key, const char *method, const char *enckey);

The sdp_add_key() function adds the KEY (k=) SDP field to the session structure (sdp_session_t) or the media structure (sdp_media_t) using the method and enckey parameters. This field can go into the media or the session section of an SDP description. You must pass either &session->s_info or &media->m_info as the first argument to specify the section.

Return Values: The sdp_add_key() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding an Attribute Field to the SDP Session Structure

int sdp_add_attribute(sdp_attr_t **attr, const char *name, const char *value);

The sdp_add_attribute() function adds the ATTRIBUTE (a=) SDP field to the session structure (sdp_session_t) or the media structure (sdp_media_t) using the name and value parameters. This field can go into the media or the session section of an SDP description. You must pass either &session->s_info or &media->m_info as the first argument to specify the section.

Return Values: The sdp_add_attribute() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Adding a Media Field to the SDP Session Structure

int sdp_add_media(sdp_session_t *session, const char *name, uint_t port, int portcount, const char *protocol, const char *format, sdp_media_t **media);

The sdp_add_media() function adds the MEDIA (m=) SDP field to the session structure that is specified by the value of the session parameter (sdp_session_t) using the values of the name, port, portcount, protocol, and format parameters. The format parameter is a string that holds one or more values, such as the string 0 32 97.

This function creates a new media structure and returns a pointer to that structure in the media parameter. Functions that add SDP fields to the media structure use this pointer.

Return Values: The sdp_add_media() function returns 0 when the function completes successfully. When mandatory parameters are not present, the function returns EINVAL. When memory allocation fails, the function returns ENOMEM. The value of errno does not change in the event of an error.

Code Sample: Building an SDP Session Structure

This example uses the functions in this section to create a new SDP session structure, add fields to the structure, and convert a finished structure to its string representation. At the end of the example, the program calls the sdp_free_session() function to free the session.


Example 3–1 Building an SDP Session Structure

/* SDP Message we will be building
"v=0\r\n\
o=Alice 2890844526 2890842807 IN IP4 10.47.16.5\r\n\
s=-\r\n\
i=A Seminar on the session description protocol\r\n\
u=http://www.example.com/seminars/sdp.pdf\r\n\
e=alice@example.com (Alice Smith)\r\n\
p=+1 911-345-1160\r\n\
c=IN IP4 10.47.16.5\r\n\
b=CT:1024\r\n\
t=2854678930 2854679000\r\n\
r=604800 3600 0 90000\r\n\
z=2882844526 -1h 2898848070 0h\r\n\
a=recvonly\r\n\
m=audio 49170 RTP/AVP 0\r\n\
i=audio media\r\n\
b=CT:1000\r\n\
k=prompt\r\n\
m=video 51372 RTP/AVP 99 90\r\n\
i=video media\r\n\
a=rtpmap:99 h232-199/90000\r\n\
a=rtpmap:90 h263-1998/90000\r\n"
*/

#include stdio.h>
#include string.h>
#include errno.h>
#include sdp.h>

int main ()
{
   sdp_session_t  *my_sess;
   sdp_media_t    *my_media;
   sdp_time_t     *my_time;
   char *b_sdp;

   my_sess = sdp_new_session();
   if (my_sess == NULL) {
return (ENOMEM);
   }
   my_sess->version = 0;
   if (sdp_add_name(my_sess, "-") != 0)
goto err_ret;
   if (sdp_add_origin(my_sess, "Alice", 2890844526ULL, 2890842807ULL,
 "IN", "IP4", "10.47.16.5") != 0)
goto err_ret;
   if (sdp_add_information(&my_sess->s_info, "A Seminar on the session"
 "description protocol") != 0)
goto err_ret;
   if (sdp_add_uri (my_sess, "http://www.example.com/seminars/sdp.pdf")
 != 0)
goto err_ret;
   if (sdp_add_email(my_sess, "alice@example.com (Alice smith)") != 0)
goto err_ret;
   if (sdp_add_phone(my_sess, "+1 911-345-1160") != 0)
goto err_ret;
   if (sdp_add_connection(&my_sess->s_conn, "IN", "IP4", "10.47.16.5",
0, 0) != 0)
goto err_ret;
   if (sdp_add_bandwidth(&my_sess->s_bw, "CT", 1024) != 0)
goto err_ret;
   if (sdp_add_time(my_sess, 2854678930ULL, 2854679000ULL, &my_time)
!= 0)
goto err_ret;
   if (sdp_add_repeat(my_time, 604800ULL, 3600ULL, "0 90000") != 0)
goto err_ret;
   if (sdp_add_zone(my_sess, 2882844526ULL, "-1h") != 0)
goto err_ret;
   if (sdp_add_zone(my_sess, 2898848070ULL, "0h") != 0)
goto err_ret;
   if (sdp_add_attribute(&my_sess->s_attr, "sendrecv", NULL) != 0)
goto err_ret;
   if (sdp_add_media(my_sess, "audio", 49170, 1, "RTP/AVP",
"0", &my_media) != 0)
goto err_ret;
   if (sdp_add_information(&my_media->m_info, "audio media") != 0)
goto err_ret;
   if (sdp_add_bandwidth(&my_media->m_bw, "CT", 1000) != 0)
goto err_ret;
   if (sdp_add_key(&my_media->m_key, "prompt", NULL) != 0)
goto err_ret;
   if (sdp_add_media(my_sess, "video", 51732, 1, "RTP/AVP",
 "99 90", &my_media) != 0)
goto err_ret;
   if (sdp_add_information(&my_media->m_info, "video media") != 0)
goto err_ret;
   if (sdp_add_attribute(&my_media->m_attr, "rtpmap",
      "99 h232-199/90000") != 0)
goto err_ret;
   if (sdp_add_attribute(&my_media->m_attr, "rtpmap",
      "90 h263-1998/90000") != 0)
goto err_ret;
   b_sdp = sdp_session_to_str(my_sess, &error);

   /*
    * b_sdp is the string representation of my_sess structure
    */

   free(b_sdp);
   sdp_free_session(my_sess);
   return (0);
err_ret:
   free(b_sdp);
   sdp_free_session(my_sess);
   return (1);
}