From 295d0dfae4aec9ac546897717b4201db1841ad11 Mon Sep 17 00:00:00 2001 From: Sangchul Lee Date: Thu, 4 Jun 2020 15:58:51 +0900 Subject: [PATCH] Add internal APIs to get SDP/ICE from text message - int ms_webrtc_get_sdp_from_message(const char *sdp_msg, gchar **sdp) - int ms_webrtc_get_ice_candidate_from_message(const char *ice_msg, gchar **candidate, gint *mlineindex) [Version] 0.1.53 [Issue Type] New feature Change-Id: Id8a6c9c1fad0dadcaf1456c80db8ebcaa00683a1 Signed-off-by: Sangchul Lee --- include/media_streamer_util.h | 7 +++ packaging/capi-media-streamer.spec | 2 +- src/media_streamer_util.c | 119 +++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) diff --git a/include/media_streamer_util.h b/include/media_streamer_util.h index 683600f..910283c 100644 --- a/include/media_streamer_util.h +++ b/include/media_streamer_util.h @@ -309,8 +309,15 @@ void ms_param_value_destroy(gpointer data); */ int ms_util_uri_path_check(const char *file_uri); +/* Use g_free() to free the return value. */ gchar* ms_get_string_from_json_object(JsonObject *object); +/* Use g_free() to free the sdp parameter. */ +int ms_webrtc_get_sdp_from_message(const char *sdp_msg, gchar **sdp); + +/* Use g_free() to free the candidate parameter. */ +int ms_webrtc_get_ice_candidate_from_message(const char *ice_msg, gchar **candidate, gint *mlineindex); + /** * @brief Iterates func over all elements contained within a bin. */ diff --git a/packaging/capi-media-streamer.spec b/packaging/capi-media-streamer.spec index d3a9a81..40ef7b7 100644 --- a/packaging/capi-media-streamer.spec +++ b/packaging/capi-media-streamer.spec @@ -1,6 +1,6 @@ Name: capi-media-streamer Summary: A Media Streamer API -Version: 0.1.52 +Version: 0.1.53 Release: 0 Group: Multimedia/API License: Apache-2.0 diff --git a/src/media_streamer_util.c b/src/media_streamer_util.c index 4a70abd..44c71f2 100644 --- a/src/media_streamer_util.c +++ b/src/media_streamer_util.c @@ -424,6 +424,125 @@ gchar* ms_get_string_from_json_object(JsonObject *object) return text; } +/* Use g_free() to free the sdp parameter. */ +int ms_webrtc_get_sdp_from_message(const char *sdp_msg, gchar **sdp) +{ + int ret = MEDIA_STREAMER_ERROR_NONE; + JsonNode *root; + JsonObject *object; + JsonObject *child; + JsonParser *parser; + const gchar *member_sdp; + const gchar *member_type; + + ms_retvm_if(sdp_msg == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "sdp_msg is NULL"); + ms_retvm_if(sdp == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "sdp is NULL"); + + parser = json_parser_new(); + if (!JSON_IS_PARSER(parser)) + return MEDIA_STREAMER_ERROR_INVALID_OPERATION; + + if (!json_parser_load_from_data(parser, sdp_msg, -1, NULL)) { + ms_error("Unknown message: %s", sdp_msg); + ret = MEDIA_STREAMER_ERROR_INVALID_PARAMETER; + goto end; + } + + root = json_parser_get_root(parser); + if (!JSON_NODE_HOLDS_OBJECT(root)) { + ms_error("It does not contain a JsonObject: %s", sdp_msg); + ret = MEDIA_STREAMER_ERROR_INVALID_PARAMETER; + goto end; + } + + object = json_node_get_object(root); + if (!json_object_has_member(object, "sdp")) { + ms_error("It does not contain 'sdp' member: %s", sdp_msg); + ret = MEDIA_STREAMER_ERROR_INVALID_PARAMETER; + goto end; + } + + child = json_object_get_object_member(object, "sdp"); + + member_type = json_object_get_string_member(child, "type"); + if (!member_type || !(g_str_equal(member_type, "answer") || g_str_equal(member_type, "offer"))) { + ms_error("Could not find valid type member: %s", sdp_msg); + ret = MEDIA_STREAMER_ERROR_INVALID_PARAMETER; + goto end; + } + + member_sdp = json_object_get_string_member(child, "sdp"); + if (!member_sdp) { + ms_error("Could not find sdb member: %s", sdp_msg); + ret = MEDIA_STREAMER_ERROR_INVALID_PARAMETER; + goto end; + } + + *sdp = g_strdup(member_sdp); + + ms_debug("sdp: %s", *sdp); +end: + g_object_unref (parser); + return ret; +} + +/* Use g_free() to free the candidate parameter. */ +int ms_webrtc_get_ice_candidate_from_message(const char *ice_msg, gchar **candidate, gint *mlineindex) +{ + int ret = MEDIA_STREAMER_ERROR_NONE; + JsonNode *root; + JsonObject *object; + JsonObject *child; + JsonParser *parser; + const gchar *_candidate; + + ms_retvm_if(ice_msg == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "ice_msg is NULL"); + ms_retvm_if(candidate == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "candidate is NULL"); + ms_retvm_if(mlineindex == NULL, MEDIA_STREAMER_ERROR_INVALID_PARAMETER, "mlineindex is NULL"); + + parser = json_parser_new(); + if (!JSON_IS_PARSER(parser)) + return MEDIA_STREAMER_ERROR_INVALID_OPERATION; + + if (!json_parser_load_from_data(parser, ice_msg, -1, NULL)) { + ms_error("Unknown message: %s", ice_msg); + ret = MEDIA_STREAMER_ERROR_INVALID_PARAMETER; + goto end; + } + + root = json_parser_get_root(parser); + if (!JSON_NODE_HOLDS_OBJECT(root)) { + ms_error("It does not contain a JsonObject: %s", ice_msg); + ret = MEDIA_STREAMER_ERROR_INVALID_PARAMETER; + goto end; + } + + object = json_node_get_object(root); + if (!json_object_has_member(object, "ice")) { + ms_error("It does not contain 'ice' member: %s", ice_msg); + ret = MEDIA_STREAMER_ERROR_INVALID_PARAMETER; + goto end; + } + + child = json_object_get_object_member(object, "ice"); + + _candidate = json_object_get_string_member(child, "candidate"); + if (!_candidate) { + ms_error("Could not find candidate member: %s", ice_msg); + ret = MEDIA_STREAMER_ERROR_INVALID_PARAMETER; + goto end; + } + + *candidate = g_strdup(_candidate); + *mlineindex = json_object_get_int_member(child, "sdpMLineIndex"); + + ms_debug("candidate: %s", *candidate); + ms_debug("sdpMLineIndex: %d", *mlineindex); +end: + g_object_unref(parser); + return ret; +} + //LCOV_EXCL_START static void __global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) -- 2.7.4