[Asterisk-code-review] scope_trace: Add/update utilities (asterisk[16])

George Joseph asteriskteam at digium.com
Thu Aug 13 06:34:47 CDT 2020


George Joseph has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/14727 )

Change subject: scope_trace: Add/update utilities
......................................................................

scope_trace: Add/update utilities

* Added a AST_STREAM_STATE_END sentinel
* Add ast_stream_to_str()
* Add ast_stream_state_to_str()
* Add ast_stream_get_format_count()
* Add ast_stream_topology_to_str()
* Add ast_stream_topology_get_active_count()
* Add ast_format_cap_append_names()
* Add ast_sip_session_get_name()

Change-Id: I132eb5971ea41509c660f64e9113cda8c9013b0b
---
M include/asterisk/format_cap.h
M include/asterisk/res_pjsip_session.h
M include/asterisk/stream.h
M main/format_cap.c
M main/stream.c
M res/res_pjsip_session.c
6 files changed, 217 insertions(+), 2 deletions(-)

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



diff --git a/include/asterisk/format_cap.h b/include/asterisk/format_cap.h
index 37021eb..e9796bb 100644
--- a/include/asterisk/format_cap.h
+++ b/include/asterisk/format_cap.h
@@ -310,6 +310,17 @@
  */
 const char *ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf);
 
+/*!
+ * \brief Append the names of codecs of a set of formats to an ast_str buffer
+ * \since 18
+ *
+ * \param cap The capabilities structure containing the formats
+ * \param buf A \c ast_str buffer to append the names of the formats to
+ *
+ * \return The contents of the buffer in \c buf
+ */
+const char *ast_format_cap_append_names(const struct ast_format_cap *cap, struct ast_str **buf);
+
 #ifndef AST_FORMAT_CAP_NAMES_LEN
 /*! Buffer size for callers of ast_format_cap_get_names to allocate. */
 #define AST_FORMAT_CAP_NAMES_LEN 384
diff --git a/include/asterisk/res_pjsip_session.h b/include/asterisk/res_pjsip_session.h
index 1e1b7aa..4b5c664 100644
--- a/include/asterisk/res_pjsip_session.h
+++ b/include/asterisk/res_pjsip_session.h
@@ -912,4 +912,12 @@
  */
 struct ast_sip_session_media *ast_sip_session_media_get_transport(struct ast_sip_session *session, struct ast_sip_session_media *session_media);
 
+/*!
+ * \brief Get the channel or endpoint name associated with the session
+ *
+ * \param session
+ * \retval Channel name or endpoint name or "unknown"
+ */
+const char *ast_sip_session_get_name(const struct ast_sip_session *session);
+
 #endif /* _RES_PJSIP_SESSION_H */
diff --git a/include/asterisk/stream.h b/include/asterisk/stream.h
index 381ef36..3578100 100644
--- a/include/asterisk/stream.h
+++ b/include/asterisk/stream.h
@@ -75,9 +75,29 @@
 	 * \brief Set when the stream is not sending OR receiving media
 	 */
 	AST_STREAM_STATE_INACTIVE,
+    /*!
+     * \brief Sentinel
+     */
+    AST_STREAM_STATE_END
 };
 
 /*!
+ * \brief Stream state enum to string map
+ * \internal
+ */
+extern const char *ast_stream_state_map[AST_STREAM_STATE_END];
+
+/*!
+ * \brief Safely get the name of a stream state
+ * \since 18
+ *
+ * \param stream_state One of enum ast_stream_state
+ * \returns A constant string with the name of the state or an empty string
+ * if an invalid value was passed in.
+ */
+#define ast_stream_state_to_str(stream_state) _stream_maps_to_str(ast_stream_state_map, stream_state)
+
+/*!
  * \brief Create a new media stream representation
  *
  * \param name A name for the stream
@@ -153,6 +173,42 @@
 void ast_stream_set_type(struct ast_stream *stream, enum ast_media_type type);
 
 /*!
+ * \brief Get a string representing the stream for debugging/display purposes
+ * \since 18
+ *
+ * \param stream A stream
+ * \param buf A pointer to an ast_str* used for the output.
+ *
+ * \retval "" (empty string) if either buf or *buf are NULL
+ * \retval "(null stream)" if *stream was NULL
+ * \retval <stream_representation> otherwise
+ *
+ * \warning No attempt should ever be made to free the returned
+ * char * and it should be dup'd if needed after the ast_str is freed.
+ *
+ * \details
+ *
+ * Return format:
+ * <name>:<media_type>:<stream_state> (formats)
+ *
+ * Sample return:
+ * "audio:audio:sendrecv (ulaw,g722)"
+ *
+ */
+const char *ast_stream_to_str(const struct ast_stream *stream, struct ast_str **buf);
+
+/*!
+ * \brief Get the count of the current negotiated formats of a stream
+ *
+ * \param stream The media stream
+ *
+ * \return The count of negotiated formats
+ *
+ * \since 18
+ */
+int ast_stream_get_format_count(const struct ast_stream *stream);
+
+/*!
  * \brief Get the current negotiated formats of a stream
  *
  * \param stream The media stream
@@ -523,4 +579,35 @@
  */
 void ast_stream_set_group(struct ast_stream *stream, int group);
 
+/*!
+ * \brief Get the number of active (non-REMOVED) streams in a topology
+ *
+ * \param topology The topology of streams
+ *
+ * \return the number of active streams
+ */
+int ast_stream_topology_get_active_count(const struct ast_stream_topology *topology);
+
+/*!
+ * \brief Get a string representing the topology for debugging/display purposes
+ *
+ * \param topology A stream topology
+ * \param buf A pointer to an ast_str* used for the output.
+ *
+ * \retval "" (empty string) if either buf or *buf are NULL
+ * \retval "(null topology)" if *topology was NULL
+ * \retval <topology_representation> otherwise
+ *
+ * \warning No attempt should ever be made to free the returned
+ * char * and it should be dup'd if needed after the ast_str is freed.
+  *
+ * Return format:
+ * <stream> ...
+ *
+ * Sample return:
+ * "<audio:audio:sendrecv (ulaw,g722)> <video:video:sendonly (h264)>"
+ *
+ */
+const char *ast_stream_topology_to_str(const struct ast_stream_topology *topology, struct ast_str **buf);
+
 #endif /* _AST_STREAM_H */
diff --git a/main/format_cap.c b/main/format_cap.c
index ca60557..a97fd6d 100644
--- a/main/format_cap.c
+++ b/main/format_cap.c
@@ -699,11 +699,19 @@
 	return internal_format_cap_identical(cap2, cap1);
 }
 
-const char *ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf)
+static const char *__ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf, int append)
 {
 	int i;
 
-	ast_str_set(buf, 0, "(");
+	if (!buf || !*buf) {
+		return "";
+	}
+
+	if (append) {
+		ast_str_append(buf, 0, "(");
+	} else {
+		ast_str_set(buf, 0, "(");
+	}
 
 	if (!cap || !AST_VECTOR_SIZE(&cap->preference_order)) {
 		ast_str_append(buf, 0, "nothing)");
@@ -725,6 +733,16 @@
 	return ast_str_buffer(*buf);
 }
 
+const char *ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf)
+{
+	return __ast_format_cap_get_names(cap, buf, 0);
+}
+
+const char *ast_format_cap_append_names(const struct ast_format_cap *cap, struct ast_str **buf)
+{
+	return __ast_format_cap_get_names(cap, buf, 1);
+}
+
 int ast_format_cap_empty(const struct ast_format_cap *cap)
 {
 	int count = ast_format_cap_count(cap);
diff --git a/main/stream.c b/main/stream.c
index 21dcede..9c764d0 100644
--- a/main/stream.c
+++ b/main/stream.c
@@ -93,6 +93,14 @@
 	AST_VECTOR(, struct ast_stream *) streams;
 };
 
+const char *ast_stream_state_map[] = {
+       [AST_STREAM_STATE_REMOVED] = "removed",
+       [AST_STREAM_STATE_SENDRECV] = "sendrecv",
+       [AST_STREAM_STATE_SENDONLY] = "sendonly",
+       [AST_STREAM_STATE_RECVONLY] = "recvonly",
+       [AST_STREAM_STATE_INACTIVE] = "inactive",
+};
+
 struct ast_stream *ast_stream_alloc(const char *name, enum ast_media_type type)
 {
 	struct ast_stream *stream;
@@ -196,6 +204,33 @@
 	return stream->formats;
 }
 
+const char *ast_stream_to_str(const struct ast_stream *stream, struct ast_str **buf)
+{
+       if (!buf || !*buf) {
+               return "";
+       }
+
+       if (!stream) {
+               ast_str_append(buf, 0, "(null stream)");
+               return ast_str_buffer(*buf);
+       }
+
+       ast_str_append(buf, 0, "%s:%s:%s ",
+               S_OR(stream->name, "noname"),
+               ast_codec_media_type2str(stream->type),
+               ast_stream_state_map[stream->state]);
+       ast_format_cap_append_names(stream->formats, buf);
+
+       return ast_str_buffer(*buf);
+}
+
+int ast_stream_get_format_count(const struct ast_stream *stream)
+{
+       ast_assert(stream != NULL);
+
+       return stream->formats ? ast_format_cap_count(stream->formats) : 0;
+}
+
 void ast_stream_set_formats(struct ast_stream *stream, struct ast_format_cap *caps)
 {
 	ast_assert(stream != NULL);
@@ -495,6 +530,22 @@
 	return AST_VECTOR_SIZE(&topology->streams);
 }
 
+int ast_stream_topology_get_active_count(const struct ast_stream_topology *topology)
+{
+       int i;
+       int count = 0;
+       ast_assert(topology != NULL);
+
+       for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
+               struct ast_stream *stream = AST_VECTOR_GET(&topology->streams, i);
+               if (stream->state != AST_STREAM_STATE_REMOVED) {
+                       count++;
+               }
+       }
+
+       return count;
+}
+
 struct ast_stream *ast_stream_topology_get_stream(
 	const struct ast_stream_topology *topology, unsigned int stream_num)
 {
@@ -631,6 +682,32 @@
 	return caps;
 }
 
+const char *ast_stream_topology_to_str(const struct ast_stream_topology *topology,
+       struct ast_str **buf)
+{
+       int i;
+
+       if (!buf ||!*buf) {
+               return "";
+       }
+
+       if (!topology) {
+               ast_str_append(buf, 0, "(null topology)");
+               return ast_str_buffer(*buf);
+       }
+
+       for (i = 0; i < AST_VECTOR_SIZE(&topology->streams); i++) {
+               struct ast_stream *stream;
+
+               stream = AST_VECTOR_GET(&topology->streams, i);
+               ast_str_append(buf, 0, " <");
+               ast_stream_to_str(stream, buf);
+               ast_str_append(buf, 0, ">");
+       }
+
+       return ast_str_buffer(*buf);
+}
+
 struct ast_stream *ast_stream_topology_get_first_stream_by_type(
 	const struct ast_stream_topology *topology,
 	enum ast_media_type type)
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index ab558f6..7172e27b 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -110,6 +110,20 @@
 	return ast_str_hash(stream_type);
 }
 
+const char *ast_sip_session_get_name(const struct ast_sip_session *session)
+{
+	if (!session) {
+		return "(null session)";
+	}
+	if (session->channel) {
+		return ast_channel_name(session->channel);
+	} else if (session->endpoint) {
+		return ast_sorcery_object_get_id(session->endpoint);
+	} else {
+		return "unknown";
+	}
+}
+
 static int sdp_handler_list_cmp(void *obj, void *arg, int flags)
 {
 	struct sdp_handler_list *handler_list1 = obj;

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/14727
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: I132eb5971ea41509c660f64e9113cda8c9013b0b
Gerrit-Change-Number: 14727
Gerrit-PatchSet: 3
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200813/715faed5/attachment-0001.html>


More information about the asterisk-code-review mailing list