[Asterisk-code-review] core / pjsip: Add support for grouping streams together. (asterisk[master])

Joshua Colp asteriskteam at digium.com
Tue Oct 31 13:27:41 CDT 2017


Joshua Colp has submitted this change and it was merged. ( https://gerrit.asterisk.org/6937 )

Change subject: core / pjsip: Add support for grouping streams together.
......................................................................

core / pjsip: Add support for grouping streams together.

In WebRTC streams (or media tracks in their world) can be grouped
together using the mslabel. This informs the browser that each
should be synchronized with each other.

This change extends the stream API so this information can
be stored with streams. The PJSIP support has been extended
to use the mslabel to determine grouped streams and store
this association on the streams. Finally when creating the
SDP the group information is used to cause each media stream
to use the same mslabel.

ASTERISK-27379

Change-Id: Id6299aa031efe46254edbdc7973c534d54d641ad
---
M include/asterisk/res_pjsip_session.h
M include/asterisk/stream.h
M main/cli.c
M main/stream.c
M res/res_pjsip_sdp_rtp.c
M res/res_pjsip_session.c
6 files changed, 121 insertions(+), 15 deletions(-)

Approvals:
  George Joseph: Looks good to me, but someone else must approve
  Kevin Harwell: Looks good to me, approved
  Joshua Colp: Approved for Submit



diff --git a/include/asterisk/res_pjsip_session.h b/include/asterisk/res_pjsip_session.h
index b7a22b93..a0fc965 100644
--- a/include/asterisk/res_pjsip_session.h
+++ b/include/asterisk/res_pjsip_session.h
@@ -111,6 +111,8 @@
 	char label[AST_UUID_STR_LEN];
 	/*! \brief The underlying session has been changed in some fashion */
 	unsigned int changed;
+	/*! \brief Remote media stream label */
+	char *remote_mslabel;
 };
 
 /*!
diff --git a/include/asterisk/stream.h b/include/asterisk/stream.h
index 2c1053a..c2d5a88 100644
--- a/include/asterisk/stream.h
+++ b/include/asterisk/stream.h
@@ -476,4 +476,25 @@
 void ast_stream_topology_map(const struct ast_stream_topology *topology,
 	struct ast_vector_int *types, struct ast_vector_int *v0, struct ast_vector_int *v1);
 
+/*!
+ * \brief Get the stream group that a stream is part of
+ *
+ * \param stream The stream
+ *
+ * \return the numerical stream group (-1 if not in a group)
+ *
+ * \since 15.2.0
+ */
+int ast_stream_get_group(const struct ast_stream *stream);
+
+/*!
+ * \brief Set the stream group for a stream
+ *
+ * \param stream The stream
+ * \param group The group the stream is part of
+ *
+ * \since 15.2.0
+ */
+void ast_stream_set_group(struct ast_stream *stream, int group);
+
 #endif /* _AST_STREAM_H */
diff --git a/main/cli.c b/main/cli.c
index ef86e25..d9aab85 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -1690,10 +1690,12 @@
 			"Name: %s\n"
 			"    Type: %s\n"
 			"    State: %s\n"
+			"    Group: %d\n"
 			"    Formats: %s\n",
 			ast_stream_get_name(stream),
 			ast_codec_media_type2str(ast_stream_get_type(stream)),
 			ast_stream_state2str(ast_stream_get_state(stream)),
+			ast_stream_get_group(stream),
 			ast_format_cap_get_names(ast_stream_get_formats(stream), &codec_buf)
 			);
 	}
diff --git a/main/stream.c b/main/stream.c
index 89ed0dc..c233b2f 100644
--- a/main/stream.c
+++ b/main/stream.c
@@ -67,6 +67,11 @@
 	ast_stream_data_free_fn data_free_fn[AST_STREAM_DATA_SLOT_MAX];
 
 	/*!
+	 * \brief The group that the stream is part of
+	 */
+	int group;
+
+	/*!
 	 * \brief Name for the stream within the context of the channel it is on
 	 */
 	char name[0];
@@ -90,6 +95,7 @@
 
 	stream->type = type;
 	stream->state = AST_STREAM_STATE_INACTIVE;
+	stream->group = -1;
 	strcpy(stream->name, S_OR(name, "")); /* Safe */
 
 	return stream;
@@ -115,6 +121,7 @@
 
 	memcpy(new_stream, stream, sizeof(*new_stream));
 	strcpy(new_stream->name, stream_name); /* Safe */
+	new_stream->group = -1;
 	if (new_stream->formats) {
 		ao2_ref(new_stream->formats, +1);
 	}
@@ -288,14 +295,16 @@
 	}
 
 	for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
-		struct ast_stream *stream =
-			ast_stream_clone(AST_VECTOR_GET(&topology->streams, i), NULL);
+		struct ast_stream *existing = AST_VECTOR_GET(&topology->streams, i);
+		struct ast_stream *stream = ast_stream_clone(existing, NULL);
 
 		if (!stream || AST_VECTOR_APPEND(&new_topology->streams, stream)) {
 			ast_stream_free(stream);
 			ast_stream_topology_free(new_topology);
 			return NULL;
 		}
+
+		ast_stream_set_group(stream, ast_stream_get_group(existing));
 	}
 
 	return new_topology;
@@ -580,3 +589,17 @@
 		AST_VECTOR_REPLACE(v1, index, i);
 	}
 }
+
+int ast_stream_get_group(const struct ast_stream *stream)
+{
+	ast_assert(stream != NULL);
+
+	return stream->group;
+}
+
+void ast_stream_set_group(struct ast_stream *stream, int group)
+{
+	ast_assert(stream != NULL);
+
+	stream->group = group;
+}
diff --git a/res/res_pjsip_sdp_rtp.c b/res/res_pjsip_sdp_rtp.c
index 604fd42..a877582 100644
--- a/res/res_pjsip_sdp_rtp.c
+++ b/res/res_pjsip_sdp_rtp.c
@@ -1052,20 +1052,11 @@
 	}
 
 	if (ast_strlen_zero(session_media->mslabel)) {
-		if (ast_sip_session_is_pending_stream_default(session, stream)) {
-			int index;
+		/* If this stream is grouped with another then use its media stream label if possible */
+		if (ast_stream_get_group(stream) != -1) {
+			struct ast_sip_session_media *group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, ast_stream_get_group(stream));
 
-			/* If this is a default stream we group them together under the same stream, but as different tracks */
-			for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
-				struct ast_sip_session_media *other_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
-
-				if (session_media == other_session_media) {
-					continue;
-				}
-
-				ast_copy_string(session_media->mslabel, other_session_media->mslabel, sizeof(session_media->mslabel));
-				break;
-			}
+			ast_copy_string(session_media->mslabel, group_session_media->mslabel, sizeof(session_media->mslabel));
 		}
 
 		if (ast_strlen_zero(session_media->mslabel)) {
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index 37ff531..4724d46 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -399,6 +399,7 @@
 	}
 
 	ast_free(session_media->mid);
+	ast_free(session_media->remote_mslabel);
 }
 
 struct ast_sip_session_media *ast_sip_session_media_state_add(struct ast_sip_session *session,
@@ -553,6 +554,70 @@
 	return 0;
 }
 
+static void set_remote_mslabel_and_stream_group(struct ast_sip_session *session,
+	struct ast_sip_session_media *session_media,
+	const pjmedia_sdp_session *sdp,
+	const struct pjmedia_sdp_media *stream,
+	struct ast_stream *asterisk_stream)
+{
+	int index;
+
+	ast_free(session_media->remote_mslabel);
+	session_media->remote_mslabel = NULL;
+
+	for (index = 0; index < stream->attr_count; ++index) {
+		pjmedia_sdp_attr *attr = stream->attr[index];
+		char attr_value[pj_strlen(&attr->value) + 1];
+		char *ssrc_attribute_name, *ssrc_attribute_value = NULL;
+		char *msid, *tmp = attr_value;
+		static const pj_str_t STR_msid = { "msid", 4 };
+		static const pj_str_t STR_ssrc = { "ssrc", 4 };
+
+		if (!pj_strcmp(&attr->name, &STR_msid)) {
+			ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
+			msid = strsep(&tmp, " ");
+			session_media->remote_mslabel = ast_strdup(msid);
+			break;
+		} else if (!pj_strcmp(&attr->name, &STR_ssrc)) {
+			ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
+
+			if ((ssrc_attribute_name = strchr(attr_value, ' '))) {
+				/* This has an actual attribute */
+				*ssrc_attribute_name++ = '\0';
+				ssrc_attribute_value = strchr(ssrc_attribute_name, ':');
+				if (ssrc_attribute_value) {
+					/* Values are actually optional according to the spec */
+					*ssrc_attribute_value++ = '\0';
+				}
+
+				if (!strcasecmp(ssrc_attribute_name, "mslabel") && !ast_strlen_zero(ssrc_attribute_value)) {
+					session_media->remote_mslabel = ast_strdup(ssrc_attribute_value);
+					break;
+				}
+			}
+		}
+	}
+
+	if (ast_strlen_zero(session_media->remote_mslabel)) {
+		return;
+	}
+
+	/* Iterate through the existing streams looking for a match and if so then group this with it */
+	for (index = 0; index < AST_VECTOR_SIZE(&session->pending_media_state->sessions); ++index) {
+		struct ast_sip_session_media *group_session_media;
+
+		group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
+
+		if (ast_strlen_zero(group_session_media->remote_mslabel) ||
+			strcmp(group_session_media->remote_mslabel, session_media->remote_mslabel)) {
+			continue;
+		}
+
+		ast_stream_set_group(asterisk_stream, index);
+		break;
+	}
+}
+
 static void remove_stream_from_bundle(struct ast_sip_session_media *session_media,
 	struct ast_stream *stream)
 {
@@ -630,6 +695,7 @@
 		}
 
 		set_mid_and_bundle_group(session, session_media, sdp, remote_stream);
+		set_remote_mslabel_and_stream_group(session, session_media, sdp, remote_stream, stream);
 
 		if (session_media->handler) {
 			handler = session_media->handler;
@@ -730,6 +796,7 @@
 	ast_copy_pj_str(media, &local->media[index]->desc.media, sizeof(media));
 
 	set_mid_and_bundle_group(session, session_media, remote, remote->media[index]);
+	set_remote_mslabel_and_stream_group(session, session_media, remote, remote->media[index], asterisk_stream);
 
 	handler = session_media->handler;
 	if (handler) {

-- 
To view, visit https://gerrit.asterisk.org/6937
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Id6299aa031efe46254edbdc7973c534d54d641ad
Gerrit-Change-Number: 6937
Gerrit-PatchSet: 3
Gerrit-Owner: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Sean Bright <sean.bright at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20171031/363154cd/attachment.html>


More information about the asterisk-code-review mailing list