summaryrefslogtreecommitdiff
path: root/main/sdp.c
diff options
context:
space:
mode:
authorRichard Mudgett <rmudgett@digium.com>2017-05-02 18:51:56 -0500
committerRichard Mudgett <rmudgett@digium.com>2017-06-20 18:15:52 -0500
commit3a18a090309271420516ea345ec32c7afa9b332b (patch)
tree402e3df66d84876dd33141c870fe84477eaf970f /main/sdp.c
parent0c0d69d4f38756c2f83d2c9007033e0ca05652c4 (diff)
SDP: Rework SDP offer/answer model and update capabilities merges.
The SDP offer/answer model requires an answer to an offer before a new SDP can be processed. This allows our local SDP creation to be deferred until we know that we need to create an offer or an answer SDP. Once the local SDP is created it won't change until the SDP negotiation is restarted. An offer SDP in an initial SIP INVITE can receive more than one answer SDP. In this case, we need to merge each answer SDP with our original offer capabilities to get the currently negotiated capabilities. To satisfy this requirement means that we cannot update our proposed capabilities until the negotiations are restarted. Local topology updates from ast_sdp_state_update_local_topology() are merged together until the next offer SDP is created. These accumulated updates are then merged with the current negotiated capabilities to create the new proposed capabilities that the offer SDP is built. Local topology updates are merged in several passes to attempt to be smart about how streams from the system are matched with the previously negotiated stream slots. To allow for T.38 support when merging, type matching considers audio and image types to be equivalent. First streams are matched by stream name and type. Then streams are matched by stream type only. Any remaining unmatched existing streams are declined. Any new active streams are either backfilled into pre-merge declined slots or appended onto the end of the merged topology. Any excess new streams above the maximum supported number of streams are simply discarded. Remote topology negotiation merges depend if the topology is an offer or answer. An offer remote topology negotiation dictates the stream slot ordering and new streams can be added. A remote offer can do anything to the previously negotiated streams except reduce the number of stream slots. An answer remote topology negotiation is limited to what our offer requested. The answer can only decline streams, pick codecs from the offered list, or indicate the remote's stream hold state. I had originally kept the RTP instance if the remote offer SDP changed a stream type between audio and video since they both use RTP. However, I later removed this support in favor of simply creating a new RTP instance since the stream's purpose has to be changing anyway. Any RTP packets from the old stream type might cause mischief for the bridged peer. * Added ast_sdp_state_restart_negotiations() to restart the SDP offer/answer negotiations. We will thus know to create a new local SDP when it is time to create an offer or answer. * Removed ast_sdp_state_reset(). Save the current topology before starting T.38. To recover from T.38 simply update the local topology to the saved topology and restart the SDP negotiations to get the offer SDP renegotiating the previous configuration. * Allow initial topology for ast_sdp_state_alloc() to be NULL so an initial remote offer SDP can dictate the streams we start with. We can always update the local topology later if it turns out we need to offer SDP first because the remote chose to defer sending us a SDP. * Made the ast_sdp_state_alloc() initial topology limit to max_streams, limit to configured codecs, handle declined streams, and discard unsupported types. * Convert struct ast_sdp to ao2 object. Needed to easily save off a remote SDP to refer to later for various reasons such as generating declined m= lines in the local SDP. * Improve converting remote SDP streams to a topology including stream state. A stream state of AST_STREAM_STATE_REMOVED indicates the stream is declined/dead. * Improve merging streams to take into account the stream state. * Added query for remote hold state. * Added maximum streams allowed SDP config option. * Added ability to create new streams as needed. New streams are created with configured default audio, video, or image codecs depending on stream type. * Added global locally_held state along with a per stream local hold state. Historically, Asterisk only has a global locally held state because when the we put the remote on hold we do it for all active streams. * Added queries for a rejected offer and current SDP negotiation role. The rejected query allows the using module to know how to respond to a failed remote SDP set. Should the using module respond with a 488 Not Acceptable Here or 500 Internal Error to the offer SDP? * Moved sdp_state_capabilities.connection_address to ast_sdp_state. There seems no reason to keep it in the sdp_state_capabilities struct since it was only used by the ast_sdp_state.proposed_capabilities instance. * Callbacks are now available to allow the using module some customization of negotiated streams and to complete setting up streams for use. See the typedef doxygen for each callback for what is allowable and when they are called. * Added topology answerer modify callback. * Added topology pre and post apply callbacks. * Added topology offerer modify callback. * Added topology offerer configure callback. * Had to rework the unit tests because I changed how SDP topologies are merged. Replaced several unit tests with new negotiation tests. Change-Id: If07fe6d79fbdce33968a9401d41d908385043a06
Diffstat (limited to 'main/sdp.c')
-rw-r--r--main/sdp.c99
1 files changed, 85 insertions, 14 deletions
diff --git a/main/sdp.c b/main/sdp.c
index bfb83e82f..fd10ba8c3 100644
--- a/main/sdp.c
+++ b/main/sdp.c
@@ -109,11 +109,9 @@ void ast_sdp_t_free(struct ast_sdp_t_line *t_line)
ast_free(t_line);
}
-void ast_sdp_free(struct ast_sdp *sdp)
+static void ast_sdp_dtor(void *vdoomed)
{
- if (!sdp) {
- return;
- }
+ struct ast_sdp *sdp = vdoomed;
ast_sdp_o_free(sdp->o_line);
ast_sdp_s_free(sdp->s_line);
@@ -121,7 +119,6 @@ void ast_sdp_free(struct ast_sdp *sdp)
ast_sdp_t_free(sdp->t_line);
ast_sdp_a_lines_free(sdp->a_lines);
ast_sdp_m_lines_free(sdp->m_lines);
- ast_free(sdp);
}
#define COPY_STR_AND_ADVANCE(p, dest, source) \
@@ -314,28 +311,28 @@ struct ast_sdp *ast_sdp_alloc(struct ast_sdp_o_line *o_line,
{
struct ast_sdp *new_sdp;
- new_sdp = ast_calloc(1, sizeof *new_sdp);
+ new_sdp = ao2_alloc_options(sizeof(*new_sdp), ast_sdp_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!new_sdp) {
return NULL;
}
new_sdp->a_lines = ast_calloc(1, sizeof(*new_sdp->a_lines));
if (!new_sdp->a_lines) {
- ast_sdp_free(new_sdp);
+ ao2_ref(new_sdp, -1);
return NULL;
}
if (AST_VECTOR_INIT(new_sdp->a_lines, 20)) {
- ast_sdp_free(new_sdp);
+ ao2_ref(new_sdp, -1);
return NULL;
}
new_sdp->m_lines = ast_calloc(1, sizeof(*new_sdp->m_lines));
if (!new_sdp->m_lines) {
- ast_sdp_free(new_sdp);
+ ao2_ref(new_sdp, -1);
return NULL;
}
if (AST_VECTOR_INIT(new_sdp->m_lines, 20)) {
- ast_sdp_free(new_sdp);
+ ao2_ref(new_sdp, -1);
return NULL;
}
@@ -741,6 +738,62 @@ static void process_fmtp_lines(const struct ast_sdp_m_line *m_line, int payload,
}
}
+/*!
+ * \internal
+ * \brief Determine the RTP stream direction in the given a lines.
+ * \since 15.0.0
+ *
+ * \param a_lines Attribute lines to search.
+ *
+ * \retval Stream direction on success.
+ * \retval AST_STREAM_STATE_REMOVED on failure.
+ *
+ * \return Nothing
+ */
+static enum ast_stream_state get_a_line_direction(const struct ast_sdp_a_lines *a_lines)
+{
+ if (a_lines) {
+ enum ast_stream_state direction;
+ int idx;
+ const struct ast_sdp_a_line *a_line;
+
+ for (idx = 0; idx < AST_VECTOR_SIZE(a_lines); ++idx) {
+ a_line = AST_VECTOR_GET(a_lines, idx);
+ direction = ast_stream_str2state(a_line->name);
+ if (direction != AST_STREAM_STATE_REMOVED) {
+ return direction;
+ }
+ }
+ }
+
+ return AST_STREAM_STATE_REMOVED;
+}
+
+/*!
+ * \internal
+ * \brief Determine the RTP stream direction.
+ * \since 15.0.0
+ *
+ * \param a_lines The SDP media global attributes
+ * \param m_line The SDP media section to convert
+ *
+ * \return Stream direction
+ */
+static enum ast_stream_state get_stream_direction(const struct ast_sdp_a_lines *a_lines, const struct ast_sdp_m_line *m_line)
+{
+ enum ast_stream_state direction;
+
+ direction = get_a_line_direction(m_line->a_lines);
+ if (direction != AST_STREAM_STATE_REMOVED) {
+ return direction;
+ }
+ direction = get_a_line_direction(a_lines);
+ if (direction != AST_STREAM_STATE_REMOVED) {
+ return direction;
+ }
+ return AST_STREAM_STATE_SENDRECV;
+}
+
/*
* Needed so we don't have an external function referenced as data.
* The dynamic linker doesn't handle that very well.
@@ -758,13 +811,14 @@ static void rtp_codecs_free(struct ast_rtp_codecs *codecs)
* Given an m-line from an SDP, convert it into an ast_stream structure.
* This takes formats, as well as clock-rate and fmtp attributes into account.
*
+ * \param a_lines The SDP media global attributes
* \param m_line The SDP media section to convert
* \param g726_non_standard Non-zero if G.726 is non-standard
*
* \retval NULL An error occurred
* \retval non-NULL The converted stream
*/
-static struct ast_stream *get_stream_from_m(const struct ast_sdp_m_line *m_line, int g726_non_standard)
+static struct ast_stream *get_stream_from_m(const struct ast_sdp_a_lines *a_lines, const struct ast_sdp_m_line *m_line, int g726_non_standard)
{
int i;
int non_ast_fmts;
@@ -793,6 +847,14 @@ static struct ast_stream *get_stream_from_m(const struct ast_sdp_m_line *m_line,
ao2_ref(caps, -1);
return NULL;
}
+ ast_stream_set_data(stream, AST_STREAM_DATA_RTP_CODECS, codecs,
+ (ast_stream_data_free_fn) rtp_codecs_free);
+
+ if (!m_line->port) {
+ /* Stream is declined. There may not be any attributes. */
+ ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
+ break;
+ }
options = g726_non_standard ? AST_RTP_OPT_G726_NONSTANDARD : 0;
for (i = 0; i < ast_sdp_m_get_payload_count(m_line); ++i) {
@@ -819,10 +881,16 @@ static struct ast_stream *get_stream_from_m(const struct ast_sdp_m_line *m_line,
}
ast_rtp_codecs_payload_formats(codecs, caps, &non_ast_fmts);
- ast_stream_set_data(stream, AST_STREAM_DATA_RTP_CODECS, codecs,
- (ast_stream_data_free_fn) rtp_codecs_free);
+
+ ast_stream_set_state(stream, get_stream_direction(a_lines, m_line));
break;
case AST_MEDIA_TYPE_IMAGE:
+ if (!m_line->port) {
+ /* Stream is declined. There may not be any attributes. */
+ ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
+ break;
+ }
+
for (i = 0; i < ast_sdp_m_get_payload_count(m_line); ++i) {
struct ast_sdp_payload *payload;
@@ -830,12 +898,15 @@ static struct ast_stream *get_stream_from_m(const struct ast_sdp_m_line *m_line,
payload = ast_sdp_m_get_payload(m_line, i);
if (!strcasecmp(payload->fmt, "t38")) {
ast_format_cap_append(caps, ast_format_t38, 0);
+ ast_stream_set_state(stream, AST_STREAM_STATE_SENDRECV);
}
}
break;
case AST_MEDIA_TYPE_UNKNOWN:
case AST_MEDIA_TYPE_TEXT:
case AST_MEDIA_TYPE_END:
+ /* Consider these unsupported streams as declined */
+ ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
break;
}
@@ -858,7 +929,7 @@ struct ast_stream_topology *ast_get_topology_from_sdp(const struct ast_sdp *sdp,
for (i = 0; i < ast_sdp_get_m_count(sdp); ++i) {
struct ast_stream *stream;
- stream = get_stream_from_m(ast_sdp_get_m(sdp, i), g726_non_standard);
+ stream = get_stream_from_m(sdp->a_lines, ast_sdp_get_m(sdp, i), g726_non_standard);
if (!stream) {
/*
* The topology cannot match the SDP because