<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> 
<base href="https://wiki.asterisk.org/wiki" /> 
<title>Message Title</title>  
<style type="text/css">@media only screen and (max-device-width: 480px) {.mobile-only {
        width: auto !important;
        height: auto !important;
        overflow: visible !important;
        line-height: normal !important;
        font-size: inherit !important;
        mso-hide: all;
}

.desktop-only {
        display: none !important;
}

/* iPhone 3GS fix for unwanted 20px right margin */
body { min-width: 100% !important; padding: 0; margin: 0; }

#center-content-table { max-width: none; !important; }
#header-pattern-container { padding: 10px 10px 10px 10px !important; line-height: 20px !important; }
#header-avatar-image-container { padding-right: 8px !important; }
#email-content-container { padding: 0 !important; }
.mobile-expand { border-radius: 0 !important; border-left: 0 !important; border-right: 0 !important; padding-left: 26px !important;}
.mobile-resize-text { font-size: 16px !important; line-height: 22px !important; }
#page-title-pattern-header { font-size: 20px !important; line-height: 28px !important; }
#page-title-pattern-icon-image-container-cell { padding-top: 7px !important; }
#inline-user-pattern { display: block !important; }
#inline-user-pattern-avatar { padding-top: 3px !important; }
.contextual-area-pattern { border-bottom: 1px solid #ccc !important; padding: 15px 10px 0 10px !important;}
.users-involved-pattern-column-table { width: 100% !important;  }
.users-involved-pattern-avatar-table-cell { padding: 3px 5px 5px 0 !important; }
.users-involved-pattern-column-container { padding-right: 0 !important; }
.contextual-excerpt-pattern, #users-involved-pattern { border: 0 !important; }

/** Aui Typography upsized for mobile **/
#content-excerpt-pattern-container, #contextual-excerpt-pattern-text-container { font-size: 16px !important; line-height: 22px !important; }
#content-excerpt-pattern-container h1, #contextual-excerpt-pattern-text-container h1 { font-size: 24px !important; line-height: 28px !important; }
#content-excerpt-pattern-container h2, #contextual-excerpt-pattern-text-container h2 { font-size: 20px !important; line-height: 28px !important; }
#content-excerpt-pattern-container h3, #contextual-excerpt-pattern-text-container h3 { font-size: 18px !important; line-height: 24px !important; }
#content-excerpt-pattern-container h4, #contextual-excerpt-pattern-text-container h4 { font-size: 16px !important; line-height: 22px !important; }
#content-excerpt-pattern-container h5, #contextual-excerpt-pattern-text-container h5 { font-size: 14px !important; line-height: 20px !important; }
#content-excerpt-pattern-container h6, #contextual-excerpt-pattern-text-container h6 { font-size: 14px !important; line-height: 20px !important; }
.user-mention { line-height: 18px !important; }
/** Aui Typography end **/

/* Show appropriate footer logo on mobile, display links vertically */
#footer-pattern { padding: 15px 10px !important; }
#footer-pattern-logo-desktop-container { padding: 0 !important; }
#footer-pattern-logo-desktop { width: 0 !important; height: 0 !important; }
#footer-pattern-logo-mobile {
    padding-top: 10px !important;
    width: 30px !important;
    height: 27px !important;
    display: inline !important;
}
#footer-pattern-text {
    display: block !important;
}
#footer-pattern-links-container { line-height: 0 !important;}
.footer-pattern-links.mobile-resize-text,
.footer-pattern-links.mobile-resize-text,
#footer-pattern-text.mobile-resize-text,
#footer-pattern-links-container.no-footer-links {
    font-size: 14px !important;
    line-height: 20px !important;
}
.footer-link { display: block !important; }
#footer-pattern-links-container table { display: inline-block !important; float: none !important; }
#footer-pattern-links-container, #footer-pattern-text { text-align: center !important; }
#footer-pattern-links { padding-bottom: 5px !important; }

/** Team Calendar overrides, these should be removed when notifications are updated in Team Calendars. For now CSS
    overrides are being used because the structure of the content can't change without rereleasing the plugin */
.mail-calendar-container .day-header + table tr td:first-child {
    vertical-align: top !important;
    padding-top: 5px !important;
}}
@media (min-width: 900px) {#center-content-table { width: 900px; }}
@media all {#outlook a {padding:0;} /* Force Outlook to provide a "view in browser" menu link. */
/* Prevent Webkit and Windows Mobile platforms from changing default font sizes.*/
body{-webkit-text-size-adjust:100%; -ms-text-size-adjust:100%;}
.ExternalClass {width:100%;} /* Force Hotmail to display emails at full width */
#background-table {margin:0; padding:0; width:100% !important; }
/* Needed to override highlighting on date and time links in iOS */
.grey a {color: #707070; text-decoration: none; }/* These styles are appended to the head element of a notification in order to prevent Apple Mail and similar
   clients from underlining the due dates with a blue hyperlink */
/* a lozenge outside an inline task should always be #333, lozenges inside an inline task should be
   colored according to their upcoming due dates, a completed task date lozenge or deleted task date
   lozenge should always be #707070 */
.date-time-lozenge a {color: #333333; text-decoration: none; }
.inline-task-text-container .date-time-lozenge.date-upcoming a {color: #DF6F00; text-decoration: none; }
.inline-task-text-container .date-time-lozenge.date-past a {color: #D04437; text-decoration: none; }
.inline-task-text-container.content-deleted-color .date-time-lozenge a,
.inline-task-text-container.checked .date-time-lozenge a {
    color: #707070; text-decoration: none;
}}
</style> 
</head>
<body>
<table id="background-table" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333; background-color: #f5f5f5"> 
<tbody> 
<tr> 
<td id="header-pattern-container" style="padding: 0px; border-collapse: collapse; padding: 10px 20px"> 
<table id="header-pattern" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333"> 
<tbody> 
<tr> 
<td id="header-avatar-image-container" valign="top" style="padding: 0px; border-collapse: collapse; vertical-align: top; width: 32px; padding-right: 9px"><a href="https://wiki.asterisk.org/wiki/display/~mmichelson?src=email" style="color: #3b73af; text-decoration: none"><img id="header-avatar-image" class="image_fix" src="cid:avatar_99ed0aa29d6f204db4785296f8170422" height="32" width="32" border="0" style="border-radius: 3px; vertical-align: top" /></a></td>
<td id="header-text-container" valign="middle" style="padding: 0px; border-collapse: collapse; vertical-align: middle; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px">Mark Michelson <strong>created</strong> a page</td> 
</tr> 
</tbody> 
</table> </td> 
</tr> 
<!-- End Header pattern --> 
<tr> 
<td id="email-content-container" style="padding: 0px; border-collapse: collapse; padding: 0 20px"> 
<table id="email-content-table" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333; border-spacing: 0; border-collapse: separate"> 
<tbody> 
<tr> 
<td class="email-content-rounded-top mobile-expand" style="padding: 0px; border-collapse: collapse; color: #fff; padding: 0 15px 0 16px; height: 15px; background-color: #fff; border-left: 1px solid #ccc; border-top: 1px solid #ccc; border-right: 1px solid #ccc; border-bottom: 0; border-top-right-radius: 5px; border-top-left-radius: 5px"> </td> 
</tr> 
<tr> 
<td class="email-content-main mobile-expand" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 15px 16px; background-color: #fff"> 
<table id="page-title-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333"> 
<tbody> 
<tr> 
<td id="page-title-pattern-icon-image-container" valign="top" style="padding: 0px; border-collapse: collapse; width: 16px; vertical-align: top"> 
<table cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333"> 
<tbody> 
<tr> 
<td id="page-title-pattern-icon-image-container-cell" style="padding: 0px; border-collapse: collapse; width: 16px; padding: 9px 8px 0px 0px; mso-text-raise: 5px; mso-line-height-rule: exactly"><a href="https://wiki.asterisk.org/wiki/display/AST/SDP+Work?src=email" title="page icon" style="vertical-align: top;; color: #3b73af; text-decoration: none"><img style="vertical-align: top; display: block;" src="cid:page-icon" alt="page icon" title="page icon" height="16" width="16" border="0" /></a></td> 
</tr> 
</tbody> 
</table> </td>
<td style="vertical-align: top;; padding: 0px; border-collapse: collapse; padding-right: 5px; font-size: 20px; line-height: 30px; mso-line-height-rule: exactly" id="page-title-pattern-header-container"><span id="page-title-pattern-header" style="font-family: Arial, sans-serif; padding: 0; font-size: 20px; line-height: 30px; mso-text-raise: 2px; mso-line-height-rule: exactly; vertical-align: middle"><a href="https://wiki.asterisk.org/wiki/display/AST/SDP+Work?src=email" title="SDP Work" style="color: #3b73af; text-decoration: none">SDP Work</a></span></td> 
</tr> 
</tbody> 
</table> </td> 
</tr> 
<tr> 
<td class="email-content-main mobile-expand" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 15px 16px; background-color: #fff"> 
<table class="content-excerpt-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px"> 
<tbody> 
<tr> 
<td class="content-excerpt-pattern-container mobile-resize-text " style="padding: 0px; border-collapse: collapse; padding: 0 0 0 24px"> <p style="margin: 10px 0 0 0; margin-top: 0">Asterisk currently has at least 3 channel drivers that make use of SDP in order to determine properties of RTP. Currently, each has independent code for parsing, negotiating, and applying the negotiated SDP to the resultant RTP session. The core Asterisk team is currently moving towards a goal of providing a better video experience in the upcoming releases of Asterisk. The new video work that we're planning to do involves SDP in a few ways:</p> 
<ul style="margin: 10px 0 0 0"> 
<li>We're going to be supporting a bunch of new SDP parameters. For the most part, channel drivers should not care about understanding these.</li> 
<li>The most common targets for video are WebRTC endpoints. Despite their differences in signaling, they all use SDP.</li> 
</ul> <p style="margin: 10px 0 0 0">To this end, separating SDP processing from channel drivers would make a ton of sense. Instead, we want to provide an SDP API. This API would allow for channel drivers to no longer be responsible for:</p> 
<ul style="margin: 10px 0 0 0"> 
<li>SDP parsing</li> 
<li>SDP generation</li> 
<li>SDP negotiation</li> 
<li>RTP session creation</li> 
<li>RTP session modification</li> 
</ul> <h1 id="SDPWork-Howthingsworkrightnow(PJSIP)" style="margin: 10px 0 0 0; font-size: 24px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">How things work right now (PJSIP)</h1> <p style="margin: 10px 0 0 0">As a gauge for how an SDP API should work, we can look at what is currently being done in the newest channel driver that uses SDP, chan_pjsip. Let's examine the process based on our role during SDP negotiation. Apologies for the roughshod manner in which this is written.</p> <h2 id="SDPWork-Astheanswerer(UAS)" style="margin: 10px 0 0 0; font-size: 20px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">As the answerer (UAS)</h2> <p style="margin: 10px 0 0 0">A new INVITE arrives. We "handle" the incoming SDP first by calling the negotiate_incoming_sdp_stream() callback on each media stream. This callback does the following:</p> 
<ul style="margin: 10px 0 0 0"> 
<li>Basic validation: Ensure there is a port. Ensure that the session can handle this media type. Ensure that address provided is valid (i.e. an actual address)</li> 
<li>Create the RTP session (allocate, set ICE-related stuff, set NAT-related stuff, set qos)</li> 
<li>Set up encryption (SDES or DTLS if options are set for that)</li> 
<li>Set up format caps on the session (set_caps). This does the following: 
<ul style="margin: 10px 0 0 0; margin-top: 0"> 
<li>Normally, gets configured codecs for endpoint.</li> 
<li>Iterate through SDP, retrieving the offered codecs.</li> 
<li>Translate the offered RTP codecs into format_cap.</li> 
<li>Get the join capabilities between us and the peer.</li> 
<li>If there's a channel: 
<ul style="margin: 10px 0 0 0; margin-top: 0"> 
<li>Removes all caps from the "caps" variable (our configured capabilities)</li> 
<li>Appends channel native formats of all types to "caps"</li> 
<li>Removes all formats of the type on this stream from "caps"</li> 
<li>Appends joint capabilities to "caps"</li> 
<li>Sets channel native formats to be "caps" (note: the gist of these previous steps was to add this particular type's joint capabilities to the channel native formats. Seems a bit long-winded?)</li> 
<li>Sets read/write formats on channel if dealing with audio</li> 
<li>Sets DTMF-related options on channel depending on some factors</li> 
</ul> </li> 
</ul> </li> 
</ul> <p style="margin: 10px 0 0 0">Next, it calls create_local_sdp() This does the following:</p> 
<ul style="margin: 10px 0 0 0"> 
<li>Sets SDP origin version, id, and user. Also sets SDP name.</li> 
<li>Calls handler's create_outgoing_sdp_stream() for each stream. This does the following: 
<ul style="margin: 10px 0 0 0; margin-top: 0"> 
<li>Some validation (making sure that we actually want to add a stream of this type)</li> 
<li>Add crypto stuff to the stream if configured for it</li> 
<li>Construct the media line: type, transport; and connection line: IP address</li> 
<li>Add ice attributes and candidates</li> 
<li>Create list of format caps. In this circumstance, it's the joint capabilities, since we've already had an incoming request.</li> 
<li>Add attributes for each format (payload number, fmtp, framing)</li> 
<li>Generate non-format RTP attributes (e.g. RFC 3766 DTMF)</li> 
<li>Add ptime if configured</li> 
<li>Add sendrecv/sendonly</li> 
</ul> </li> 
<li>Use connection details from top stream for the global settings (connection, origin address)</li> 
</ul> <p style="margin: 10px 0 0 0">Nothing happens again until we answer the call. At that point, we get the on_media_update() callback from PJSIP, which will end up calling apply_negotiated_sdp_stream() for each stream on the sdp. That does:</p> 
<ul style="margin: 10px 0 0 0"> 
<li>Validation (Make sure we have a channel. Make sure that ports have been specified in both the local and remote SDP)</li> 
<li>Set up encryption</li> 
<li>Ensure remote stream has a connection address, and be sure we can resolve it</li> 
<li>set_caps() (yes, again)</li> 
<li>set channel file descriptors</li> 
<li>process ICE attributes from remote stream</li> 
<li>Some hold logic (this is more a thing when this is called not during the initial offer/answer)</li> 
<li>Handle RTP keepalive and RTP timeout options</li> 
</ul> <h2 id="SDPWork-AstheOfferer(UAC)" style="margin: 10px 0 0 0; font-size: 20px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">As the Offerer (UAC)</h2> <p style="margin: 10px 0 0 0">During channel requestation, we get the joint caps based on the request caps and the configured formats on the endpoint.</p> <p style="margin: 10px 0 0 0">When calling, we create a local SDP, much the same as was done in the Answerer-UAS scenario, and we create the RTP instance at this point.</p> <p style="margin: 10px 0 0 0">When we receive an incoming SDP (such as 183, 200 response), we end up calling the apply_negotiated_sdp_stream for each media stream. This is the same as in the answerer situation.</p> <h2 id="SDPWork-AsanOfferer(UAS)" style="margin: 10px 0 0 0; font-size: 20px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">As an Offerer (UAS)</h2> <p style="margin: 10px 0 0 0">This time when a new INVITE arrives, we don't call handle_incoming_sdp(). We instead create a local SDP offer, the same as if we were the offerer as a UAC.</p> <p style="margin: 10px 0 0 0">We place the outgoing offer in the 200 OK.</p> <p style="margin: 10px 0 0 0">When we receive an ACK, the on_media_update() callback is called just like the other offerer flow.</p> <h2 id="SDPWork-AsanAnswerer(UAC)" style="margin: 10px 0 0 0; font-size: 20px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">As an Answerer (UAC)</h2> <p style="margin: 10px 0 0 0">This never happens, because we always will offer an SDP in our INVITE.</p> <h2 id="SDPWork-Thetakeaway:" style="margin: 10px 0 0 0; font-size: 20px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">The takeaway:</h2> <p style="margin: 10px 0 0 0">There's actually pretty decent separation of concerns here, in that most of the SDP-related code is in its own module. The biggest violation is that within res_pjsip_session, a lot of the local SDP gets created there rather than elsewhere. This makes sense with the current model since res_pjsip_sdp_rtp is concerned only with the RTP aspects of the SDP. Most of what would need to be done would be to move the work being done in res_pjsip_sdp_rtp() to its own area and divorce it of any dependencies on PJMedia and res_pjsip_session-defined structures.</p> <p style="margin: 10px 0 0 0">So structures like: ast_sip_session, ast_sip_session_media, pjmedia_sdp_session, and pjmedia_sdp_media would need to be replaced at the API level.</p> <h1 id="SDPWork-AstabatanAPI" style="margin: 10px 0 0 0; font-size: 24px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">A stab at an API</h1> <p style="margin: 10px 0 0 0">An SDP API should be dead simple. The basic operations on it should be:</p> 
<ul style="margin: 10px 0 0 0"> 
<li>Please make me an SDP that I can send out, based on the world we know.</li> 
<li>Here is a remote SDP. Apply it to the world we know.</li> 
<li>I need information (joint formats, negotiated DTMF, etc.) so I can apply it to my channel.</li> 
</ul> <p style="margin: 10px 0 0 0">The majority of the heavy lifting will be taken care of in the internals of the SDP API. The callers of the SDP API will not need to be concerned with whether they are an offerer or answerer, and they mostly won't need to be concerned about the current SDP state.</p> <p style="margin: 10px 0 0 0">The SDP layer needs to provide similar functionality that res_pjsip_sdp_rtp already provides, but with generic structures rather than ones specific to the chan_pjsip channel driver.</p> <p style="margin: 10px 0 0 0"> </p> 
<div class="code panel pdl" style="border-width: 1px;"> 
<div class="codeContent panelContent pdl"> 
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px;; margin: 10px 0 0 0; margin-top: 0">/*!
 * \brief Forward declaration of an SDP options structure.
 *
 * SDP options will allow for the control of features such as:
 * rtcpmux: is it enabled?
 * BUNDLE: is it enabled? Do we require bundle-only?
 * ICE: is it enabled? Is it standard or trickle-ICE?
 * Telephone events: Are they enabled?
 * In what format should SDPs be in when interacting with the SDP API user?
 */
struct ast_sdp_options; 

/*!
 * Simple allocation for SDP options.
 * Initializes with sane defaults
 */
struct ast_sdp_options *ast_sdp_options_alloc(void);
 
/*!
 * \brief Free SDP options.
 * 
 * You'll only ever have to call this if an error occurs
 * between allocating options and allocating the SDP state
 * that uses these options. Otherwise, freeing the SDP state
 * will free the SDP options it inherited.
 */
void ast_sdp_options_free(struct ast_sdp_options *options);
 
/*!
 * \brief General template for setting SDP options
 * 
 * The types are going to differ for each individual option, hence
 * the "whatever" second parameter.
 */
int ast_sdp_options_set_<whatever>(struct ast_sdp_options *sdp_options, <whatever>);
 
/*!
 *\brief General template for retrieving SDP options
 *
 * The type being retrieved is going to be dependent on the option being retrieved,
 * thus the return type of "whatever" here.
 */
<whatever> ast_sdp_options_get_<whatever>(struct ast_sdp_options *sdp_options);
 
/*!
 * \brief Allocate a new SDP state
 * 
 * SDP state keeps tabs on everything SDP-related for a media session.
 * Most SDP operations will require the state to be provided.
 * Ownership of the SDP options is taken on by the SDP state.
 * A good strategy is to call this during session creation.
 */
struct ast_sdp_state* ast_sdp_state_alloc(struct ast_stream_topology *streams, const struct ast_sdp_options *options);
 
/*!
 * \brief Free the SDP state.
 *
 * A good strategy is to call this during session destruction
 */
void ast_sdp_state_free(struct ast_sdp_state *sdp_state);
 
/*!
 * \brief Get the local SDP.
 *
 * If we have not received a remote SDP yet, this will be an SDP offer based on known streams and options
 * If we have received a remote SDP, this will be the negotiated SDP based on the joint capabilities.
 * The return type is a void pointer because the representation of the SDP is going to be determined based
 * on the SDP options when allocating the SDP state.
 * This function will allocate RTP instances if RTP instances have not already
 * been allocated for the streams.
 *
 * The return here is const. The use case for this is so that a channel can add the SDP to an outgoing
 * message. The API user should not attempt to modify the SDP. SDP modification should only be done through
 * the API.
 */
const void *ast_sdp_state_get_local(const struct ast_sdp_state *sdp_state);
 
/*!
 * \brief Set the remote SDP.
 *
 * This can be used for either a remote offer or answer.
 * This can also be used whenever an UPDATE, re-INVITE, etc. arrives.
 * The type of the "remote" parameter is dictated by whatever SDP representation
 * was set in the ast_sdp_options used during ast_sdp_state allocation
 *
 * This function will allocate RTP instances if RTP instances have not already
 * been allocated for the streams.
 */
int ast_sdp_state_set_remote(struct ast_sdp_state *sdp, void *remote);
 
/*!
 * \brief Reset the SDP state and stream capabilities as if the SDP state had just been allocated.
 *
 * This is most useful for when a channel driver is sending a session refresh message
 * and needs to re-advertise its initial capabilities instead of the previously-negotiated
 * joint capabilities.
 */
int ast_sdp_state_reset(struct ast_sdp_state *sdp);
 
/*!
 * \brief Get the associated RTP instance for a particular stream on the SDP state.
 *
 * Stream numbers correspond to the streams in the topology of the associated channel
 */
struct ast_rtp_instance *ast_sdp_state_get_rtp_instance(const struct ast_sdp_state *sdp_state, int stream_index);

/*!
 * \brief Get the joint negotiated streams based on local and remote capabilities.
 * If this is called prior to receiving a remote SDP, then this will just mirror the local configured endpoint capabilities.
 */
struct ast_stream_topology *ast_sdp_state_get_joint_topology(const struct ast_sdp_state *sdp_state);

/*!
 * \brief Update remote and local stream capabilities
 *
 * If something outside SDP negotiation updates channel capabilities, use this to make sure that
 * any SDP we create will have the appropriate new capabilities present. Direct media is something
 * that could cause capabilities to be altered, as an example.
 *
 * Retrieval of the local SDP after calling either of these functions will result in the appropriate
 * joint stream capabilities being represented.
 */
int ast_sdp_state_update_local_topology(struct ast_sdp_state *state, struct ast_stream_topology *new_topology);
int ast_sdp_state_update_remote_topology(struct ast_sdp_state *state, struct ast_stream_topology *new_topology);

/*
 * Override the default connection address for SDP.
 * This is useful for NAT operations and for direct media.
 */
int ast_sdp_state_set_connection_address(struct ast_sdp_state *state, struct ast_sockaddr *addr);</pre> 
</div> 
</div> <p style="margin: 10px 0 0 0">Let's talk about the API a bit. The API introduces two new structures: <code style="font-family: monospace">ast_sdp_state</code>, and <code style="font-family: monospace">ast_sdp_options</code>.</p> <h2 id="SDPWork-ast_sdp_options" style="margin: 10px 0 0 0; font-size: 20px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0"> <code style="font-family: monospace">ast_sdp_options</code> </h2> <p style="margin: 10px 0 0 0">The first to talk about is <code style="font-family: monospace">ast_sdp_options</code>. This is vaguely defined in the API above because there will likely be a lot of options, and trying to make sure all are covered at this point is futile. The options here will be used to influence behavior of the SDP layer. Have a look at the sample code sections to see some hypothetical uses of SDP options.</p> 
<div class="aui-message warning shadowed information-macro"> 
<span class="aui-icon icon-warning">Icon</span> 
<div class="message-content"> 
<p style="margin: 10px 0 0 0; margin-top: 0">It may be a good idea to have some shortcut methods for options. For instance, have an <code style="font-family: monospace">ast_sdp_options_set_webrtc()</code>, which will set up bundle, ICE, RTCP-mux, DTLS, and anything else that WebRTC requires.</p> 
</div> 
</div> <h2 id="SDPWork-ast_sdp_state" style="margin: 10px 0 0 0; font-size: 20px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0"> <code style="font-family: monospace">ast_sdp_state</code> </h2> <p style="margin: 10px 0 0 0">This structure is 100% opaque to callers, and basically is used as a way for the SDP API to understand the situation and respond appropriately to requests. Internally, this will keep track of things such as our role in SDP negotiation, progress of SDP negotiation, formats, and options. It's recommended that SDP-using channel drivers allocate this early during session allocation and free it when the session is freed.</p> <p style="margin: 10px 0 0 0">A note from the API: notice that <code style="font-family: monospace">ast_sdp_state_alloc()</code> gains ownership of the <code style="font-family: monospace">ast_sdp_options</code> passed in. Also notice that there is no method of accessing the options from the state. This is on purpose, because SDP options become set in stone once they have been passed to the SDP state and cannot be changed. This is done for a few reasons:</p> 
<ul style="margin: 10px 0 0 0"> 
<li> <span>Declaring ownership this way prevents data races due to outside threads potentially trying to change options out from under the SDP state.</span> </li> 
<li> <span>SDP options are intended to be derived from endpoint configuration and not be based on dynamic changes that happen during a session. Anything that can change during a session should not be an SDP option.</span> </li> 
</ul> <p style="margin: 10px 0 0 0">Another note from the API: Notice that there is a function for retrieving the RTP instance. This is because the <code style="font-family: monospace">ast_sdp_state</code> is responsible for allocating the RTP instance. Users of the SDP API should not allocate their own RTP instances, but rather retrieve them from the SDP state. This way, users can set RTP options directly, like RTP timeout, RTP keepalive, etc. RTP properties that are derived from the SDP should not be addressed by users of the SDP API. That's taken care of automatically.</p> <h2 id="SDPWork-ICE" style="margin: 10px 0 0 0; font-size: 20px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">ICE</h2> <p style="margin: 10px 0 0 0">ICE plays a vital role in WebRTC. Unfortunately, it's also a bit on the complicated side, especially with the interactions it has between RTP and SDP. It can be hard to divide the duties up so that the proper layer is in charge of what it should be. Realistically, ICE could exist as its own subsystem, separate from RTP. In Asterisk, the duties are a bit mixed; there's an ICE engine structure, but it's tied closely together with RTP instances. When RTP is allocated, if ICE is enabled, then the RTP layer will perform the necessary STUN requests in order to gather all ICE candidates before returning the allocated RTP instance. From here, the user of the RTP instance can call public functions that allow for iterating over the gathered candidates and adding those to the SDP as necessary.</p> <p style="margin: 10px 0 0 0">For stage one of the SDP API, we will continue to operate this way. That is, if ICE is enabled, then the SDP layer can iterate over the gathered local ICE candidates and add them to the resultant SDP. Longer term, though, we want to support both standard ICE and trickle-ICE. With trickle-ICE, candidates are learned asynchronously. When we send out our initial SDP, we may not have learned of all server-reflexive, peer-reflexive, and relay candidates. As individual new ICE candidates are learned, the channel driver uses some protocol-dependent method to "trickle" out the new candidates. This means that the ICE layer needs to be able to send alerts to the channel driver to say when a new ICE candidate is learned. In addition, the SDP layer needs to know about the new candidate. This way, if Asterisk needs to generate a session refresh SDP, the ICE candidates can be advertised in the new SDP.</p> <p style="margin: 10px 0 0 0">Being that we need to support such a thing, it may be beneficial to move towards a more unified model for determining ICE candidates. Instead of calling an ICE engine's get_local_candidates() callback to retrieve all candidates for a given instance, the ICE layer could always call back to interested parties each time that a new candidate is learned. If using standard ICE, the callbacks would be invoked while blocking on the call to allocate an RTP instance. If using Trickle-ICE, then the callbacks would be called as the candidates are learned. The SDP layer will react the same way no matter which type of ICE is in use: Add the newly-learned ICE candidate to the SDP. The channel driver, though, would only act on this callback if Trickle-ICE were in use. Otherwise, the callbacks will be ignored.</p> <p style="margin: 10px 0 0 0">The callback would look something like the following:</p> 
<div class="code panel pdl" style="border-width: 1px;"> 
<div class="codeContent panelContent pdl"> 
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px;; margin: 10px 0 0 0; margin-top: 0">/*!
 * \brief Callback type for discovery of new ICE candidates
 *
 * This will be called each time a new ICE candidate is discovered on an RTP instance.
 * The opaque pointer is the same data that was passed in when registering the callback.
 */
typedef int (*new_candidate_fn)(struct ast_rtp_instance *rtp, struct ast_rtp_engine_ice_candidate *candidate, void *data);
 
/*
 * Indicate interest in being told of new ICE candidates.
 * 
int ast_rtp_instance_register_ice_new_candidate_cb(struct ast_rtp_instance *rtp, new_candidate_fn cb, void *data);</pre> 
</div> 
</div> <p style="margin: 10px 0 0 0">This way, an RTP instance can be told by interested parties to be alerted whenever a new ICE candidate is learned. The data parameter is a way to quickly associate the RTP instance with another piece of data the callback cares about (like an SDP state or a PJSIP session).</p> <p style="margin: 10px 0 0 0">You may be saying to yourself that there's a possible race condition here. What happens if ICE candidates are discovered before someone registers their callback function? This API provides the guarantee that interested parties will be told of every ICE candidate. If candidates have been discovered prior to when the candidate callback is registered, those candidates will be presented in the callback immediately.</p> <p style="margin: 10px 0 0 0">To reiterate, this ICE change would be saved for a milestone beyond the initial rollout of the SDP layer. For the initial rollout, the old method will still be used.</p> <h1 id="SDPWork-Codesamples" style="margin: 10px 0 0 0; font-size: 24px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">Code samples</h1> <p style="margin: 10px 0 0 0">Here is a hypothetical allocation of an <code style="font-family: monospace">ast_sdp_state</code>.</p> 
<div class="code panel pdl" style="border-width: 1px;"> 
<div class="codeContent panelContent pdl"> 
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px;; margin: 10px 0 0 0; margin-top: 0">int init_session(struct my_channel_driver_session *session)
{
    struct ast_sdp_options *sdp_options;
    struct ast_sdp_state *sdp_state;

    sdp_options = ast_sdp_options_alloc();
    if (!sdp_options) {
        return -1;
    }

    /* Set us up so BUNDLE offers bundle-only */
    if (ast_sdp_options_set_bundle(sdp_options, AST_SDP_BUNDLE_ONLY)) {
        goto fail;
    }

    /* Enable RTCPmux on all RTP instances allocated */
    if (ast_sdp_options_set_rtcpmux(sdp_options, 1)) {
        goto fail;
    }

    /* When setting/retrieving an SDP, represent it as a string */
    if (ast_sdp_options_set_repr(sdp_options, AST_SDP_REPR_STRING)) {
        goto fail;
    }

    sdp_state = ast_sdp_state_alloc(session->endpoint->stream_topology, sdp_options);
    if (!sdp_state) {
        /* ast_sdp_state_alloc() will free the options on failure */
        return -1;
    }

    session->sdp_state = sdp_state;
    return 0;

fail:
    ast_sdp_options_free(sdp_options);
    return -1;
}</pre> 
</div> 
</div> <p style="margin: 10px 0 0 0">In this example, we enable several SDP options and then use those to allocate the SDP state. The SDP state is saved onto the session for future use.</p> <p style="margin: 10px 0 0 0">Now let's make a call.</p> 
<div class="code panel pdl" style="border-width: 1px;"> 
<div class="codeContent panelContent pdl"> 
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px;; margin: 10px 0 0 0; margin-top: 0">int make_a_call(struct my_channel_driver_session *session, char *dest)
{
    struct my_channel_driver_message *message;

    message = make_call_message(dest);

    message->sdp = ast_sdp_state_get_local(session->sdp_state);

    return send_message(message);
}</pre> 
</div> 
</div> <p style="margin: 10px 0 0 0">When it comes time to make a call, all we have to do is request our local SDP, translate it into the appropriate representation, and then send our message out. The SDP that we retrieve in this case is based on the formats and options that we passed into SDP state creation.</p> <p style="margin: 10px 0 0 0">Now what about receiving an incoming call.</p> 
<div class="code panel pdl" style="border-width: 1px;"> 
<div class="codeContent panelContent pdl"> 
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px;; margin: 10px 0 0 0; margin-top: 0">int incoming_call(struct my_channel_driver_session *session, struct my_channel_driver_message *message)
{   
    struct my_channel_driver_message *response;

    if (message->sdp) {
        struct ast_stream_topology *joint_topology;
        struct ast_stream_topology *old_channel_topology;
        struct ast_stream_topology *channel_topology;
        ast_sdp_state_set_remote(session->sdp_state, message->sdp);
        joint_topology = ast_stream_topology_copy(ast_sdp_state_get_joint_topology(session->sdp_state));
        
        /* Since we're receiving an initial offer, we can just modify the channel stream topology directly. */
        ast_channel_lock(session->channel);
        old_channel_topology = channel_topology = ast_channel_stream_topology_get(session->channel));
        channel_topology = joint_topology;
        ast_channel_unlock(session->channel);
 
        ast_stream_topology_destroy(old_channel_topology);
    }

    /* Let's pretend that my_channel_driver requires us to send a provisional response immediately upon receipt */
    response = make_provisional_response(message);

    response->sdp = ast_sdp_state_get_local(session->sdp_state);
 
    return send_message(response);
}</pre> 
</div> 
</div> <p style="margin: 10px 0 0 0">This is very similar to what we did when creating an outgoing call. The interesting difference here is that we now potentially call <code style="font-family: monospace">ast_sdp_state_set_remote()</code> if the incoming message has an SDP. This causes the subsequent call to <code style="font-family: monospace">ast_sdp_state_get_local()</code> to behave differently. If the incoming message had an SDP, then <code style="font-family: monospace">ast_sdp_state_get_local()</code> will return the negotiated SDP that we should use as an answer. If the incoming message had no SDP, then <code style="font-family: monospace">ast_sdp_state_get_local()</code> will return the exact same SDP offer we use when making an outgoing call.</p> <p style="margin: 10px 0 0 0">Now let's look at a hypothetical switchover to direct media.</p> 
<div class="code panel pdl" style="border-width: 1px;"> 
<div class="codeContent panelContent pdl"> 
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px;; margin: 10px 0 0 0; margin-top: 0">int direct_media_enabled(struct my_channel_driver_session *session, struct ast_stream_topology *peer_topology, struct ast_sockaddr *peer_addr)
{
    struct my_channel_driver_message *message;
    struct ast_format_cap *joint_topology;

    ast_sdp_state_update_local_topology(session->sdp_state, peer_topology);
    joint_topology = ast_sdp_state_get_joint_topology(session->sdp_state);
    ast_channel_stream_topology_request_change(session->channel, join_topology);
 
    ast_sdp_state_set_connection_address(session->sdp_state, peer_addr);

    message = make_media_update_message(session);
    
    message->sdp = ast_sdp_state_get_local(session->sdp_state);

    return send_message(message);
}</pre> 
</div> 
</div> <p style="margin: 10px 0 0 0">It may be a bit confusing what's going on with the format_cap structures here. If we assume that Alice and Bob are going to be doing direct media, then let's pretend that this is the session with Alice. The peer_topology is Bob's topology. By making Bob's topology our local topology, it results in the joint topology being that of Bob and Alice. We update the SDP state to use this joint topology. We then also update the connection address so we place the correct address in it place. The subsequent call to get the local SDP now will properly reflect the updated capabilities and peer address.</p> <h1 id="SDPWork-Lingeringquestions:" style="margin: 10px 0 0 0; font-size: 24px; font-weight: normal; line-height: 30px; margin: 40px 0 0 0">Lingering questions:</h1> 
<ol style="margin: 10px 0 0 0"> 
<li>Is anything critical missing from the API? Obviously, individual SDP options are ill-defined at this point, and it may be that we need a few more small functions here and there.</li> 
<li>Similarly, is there anything defined in the API that we won't need? I was having trouble coming up with code snippets to get RTP options, for instance, but I didn't want to remove them.</li> 
<li>Is the current idea for SDP representation a good one? Or should the API settle on a specific representation of SDPs for getting/setting, leaving conversion to channel drivers?</li> 
<li>The API is ambiguous about when it allocates an RTP instance. My current thought is that this is on purpose since new features may require us to allocate the RTP session at a different time than we currently do. Should we be more explicit, though?</li> 
<li>The ownership model of <code style="font-family: monospace">ast_sdp_options</code> may be wasteful. Currently, you allocate the options each time you create a new SDP state, and the SDP state inherits ownership of the options. It may be more prudent to allocate SDP options at endpoint creation time. Then just pass the same SDP options structure into each allocation of an SDP state. Each SDP state would just gain a reference to the (immutable) SDP options.</li> 
</ol> </td> 
</tr> 
</tbody> 
</table> </td> 
</tr> 
<tr> 
<td class="email-content-main mobile-expand action-padding last-row-padding" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 15px 16px; background-color: #fff; padding-bottom: 10px; padding-bottom: 10px"> 
<table id="actions-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px"> 
<tbody> 
<tr> 
<td id="actions-pattern-container" valign="middle" style="padding: 0px; border-collapse: collapse; padding: 15px 0 0 24px; vertical-align: middle"> 
<table align="left" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333"> 
<tbody> 
<tr> 
<td class="actions-pattern-action-icon-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 0px; vertical-align: middle"><a href="https://wiki.asterisk.org/wiki/display/AST/SDP+Work?src=email" title="View page Icon" style="color: #3b73af; text-decoration: none"><img class="actions-pattern-action-icon-image" height="16" width="16" border="0" title="View page Icon" src="cid:com.atlassian.confluence.plugins.confluence-email-resources%3Aview-page-email-adg-footer-item%3Aicon" alt="View page Icon" style="vertical-align: middle" /></a></td>
<td class="actions-pattern-action-text-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 4px; padding-left: 5px; white-space: nowrap"><a href="https://wiki.asterisk.org/wiki/display/AST/SDP+Work?src=email" title="View page" style="color: #3b73af; text-decoration: none">View page</a></td>
<td class="actions-pattern-action-bull" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 4px; color: #999; padding: 0 5px">•</td> 
</tr> 
</tbody> 
</table> 
<table align="left" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333"> 
<tbody> 
<tr> 
<td class="actions-pattern-action-icon-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 0px; vertical-align: middle"><a href="https://wiki.asterisk.org/wiki/display/AST/SDP+Work?showComments=true&showCommentArea=true&src=email#addcomment" title="Add comment Icon" style="color: #3b73af; text-decoration: none"><img class="actions-pattern-action-icon-image" height="16" width="16" border="0" title="Add comment Icon" src="cid:com.atlassian.confluence.plugins.confluence-email-resources%3Aadd-comment-to-content-email-adg-footer-item%3Aicon" alt="Add comment Icon" style="vertical-align: middle" /></a></td>
<td class="actions-pattern-action-text-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 4px; padding-left: 5px; white-space: nowrap"><a href="https://wiki.asterisk.org/wiki/display/AST/SDP+Work?showComments=true&showCommentArea=true&src=email#addcomment" title="Add comment" style="color: #3b73af; text-decoration: none">Add comment</a></td>
<td class="actions-pattern-action-bull" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 4px; color: #999; padding: 0 5px">•</td> 
</tr> 
</tbody> 
</table> 
<table style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333"> 
<tbody> 
<tr> 
<td class="actions-pattern-action-icon-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 0px; vertical-align: middle"><a href="https://wiki.asterisk.org/wiki/plugins/likes/like.action?contentId=36800311&src=email" title="Like Icon" style="color: #3b73af; text-decoration: none"><img class="actions-pattern-action-icon-image" height="16" width="16" border="0" title="Like Icon" src="cid:com.atlassian.confluence.plugins.confluence-like%3Aview-email-adg-content-item%3Aicon" alt="Like Icon" style="vertical-align: middle" /></a></td>
<td class="actions-pattern-action-text-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 4px; padding-left: 5px; white-space: nowrap"><a href="https://wiki.asterisk.org/wiki/plugins/likes/like.action?contentId=36800311&src=email" title="Like" style="color: #3b73af; text-decoration: none">Like</a></td> 
</tr> 
</tbody> 
</table> </td> 
</tr> 
</tbody> 
</table> </td> 
</tr> 
<tr> 
<td class="email-content-rounded-bottom mobile-expand" style="padding: 0px; border-collapse: collapse; color: #fff; height: 5px; line-height: 5px; padding: 0 15px 0 16px; background-color: #fff; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; border-top: 0; border-left: 1px solid #ccc; border-bottom: 1px solid #ccc; border-right: 1px solid #ccc; mso-line-height-rule: exactly"> </td> 
</tr> 
</tbody> 
</table> </td> 
</tr> 
<tr> 
<td id="footer-pattern" style="padding: 0px; border-collapse: collapse; padding: 12px 20px"> 
<table id="footer-pattern-container" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333"> 
<tbody> 
<tr> 
<td id="footer-pattern-links-container" width="100%" style="padding: 0px; border-collapse: collapse; color: #999; font-size: 12px; line-height: 18px; font-family: Arial, sans-serif; mso-line-height-rule: exactly; mso-text-raise: 2px"> 
<table align="left" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333; font-size: 12px; line-height: 18px; font-family: Arial, sans-serif; mso-line-height-rule: exactly; mso-text-raise: 2px"> 
<tbody> 
<tr> 
<td class="footer-pattern-links mobile-resize-text" style="padding: 0px; border-collapse: collapse"><a href="https://wiki.asterisk.org/wiki/users/removespacenotification.action?spaceKey=AST&src=email" title="" style="color: #3b73af; text-decoration: none">Stop watching space</a></td>
<td class="footer-pattern-links-bull" style="padding: 0px; border-collapse: collapse; padding: 0 5px; color: #999">•</td> 
</tr> 
</tbody> 
</table> 
<table style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333; font-size: 12px; line-height: 18px; font-family: Arial, sans-serif; mso-line-height-rule: exactly; mso-text-raise: 2px"> 
<tbody> 
<tr> 
<td class="footer-pattern-links mobile-resize-text" style="padding: 0px; border-collapse: collapse"><a href="https://wiki.asterisk.org/wiki/users/editmyemailsettings.action?src=email" title="" style="color: #3b73af; text-decoration: none">Manage notifications</a></td> 
</tr> 
</tbody> 
</table> </td> 
</tr> 
<tr> 
<td id="footer-pattern-text" class="mobile-resize-text" width="100%" style="padding: 0px; border-collapse: collapse; color: #999; font-size: 12px; line-height: 18px; font-family: Arial, sans-serif; mso-line-height-rule: exactly; mso-text-raise: 2px; display: none">This message was sent by Atlassian Confluence 5.6.6</td> 
</tr> 
</tbody> 
</table> </td> 
</tr> 
</tbody> 
</table> 
<table id="sealed-section" border="0" cellpadding="0" cellspacing="0" width="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; color: #333; display: none"> 
<tbody> 
<tr> 
<td style="padding: 0px; border-collapse: collapse; border: 0; font-size: 0px; line-height: 0; mso-line-height-rule: exactly"></td> 
</tr> 
</tbody> 
</table>
</body>
</html>