From: Sangchul Lee Date: Fri, 4 Sep 2020 01:45:39 +0000 (+0900) Subject: Add API set for state changed callback X-Git-Tag: submit/tizen/20210729.023123~231 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=59f1eb29127f175f8beae0568c8bf6d4c0438b2e;p=platform%2Fcore%2Fapi%2Fwebrtc.git Add API set for state changed callback Functions are added as below. - webrtc_set_state_changed_cb() - webrtc_unset_state_changed_cb() Test cases for these functions are added to webrtc_test. [Version] 0.1.11 [Issue Type] API Change-Id: I384ce3da148cd6181795a998abb33b98855543c2 Signed-off-by: Sangchul Lee --- diff --git a/include/webrtc.h b/include/webrtc.h index 53e3c3bc..84b30d01 100644 --- a/include/webrtc.h +++ b/include/webrtc.h @@ -80,6 +80,20 @@ typedef enum { WEBRTC_MEDIA_SOURCE_TYPE_VIDEOTEST } webrtc_media_source_type_e; + +/** + * @brief Called when the WebRTC state is changed. + * @since_tizen 6.0 + * @remarks The @a webrtc is the same object for which the callback was set. + * @param[in] webrtc WebRTC handle + * @param[in] previous The previous state of the WebRTC handle + * @param[in] current The current state of the WebRTC handle + * @param[in] user_data The user data passed from the callback registration function + * @see webrtc_set_state_changed_cb() + * @see webrtc_unset_state_changed_cb() + */ +typedef void (*webrtc_state_changed_cb)(webrtc_h webrtc, webrtc_state_e previous, webrtc_state_e current, void *user_data); + /** * @brief Called when the WebRTC needs session negotiation. * @since_tizen 6.0 @@ -90,6 +104,35 @@ typedef enum { */ typedef void (*webrtc_negotiation_needed_cb)(webrtc_h webrtc, void *user_data); +/** + * @brief Sets a callback function to be invoked when the WebRTC state is changed. + * @since_tizen 6.0 + * @param [in] webrtc WebRTC handle + * @param [in] callback Callback function pointer + * @param [in] user_data The user data to be passed to the callback function + * @return @c 0 on success, + * otherwise a negative error value + * @retval #WEBRTC_ERROR_NONE Successful + * @retval #WEBRTC_ERROR_INVALID_PARAMETER Invalid parameter + * @post webrtc_state_changed_cb() will be invoked. + * @see webrtc_unset_state_changed_cb() + * @see webrtc_state_changed_cb() + */ +int webrtc_set_state_changed_cb(webrtc_h webrtc, webrtc_state_changed_cb callback, void *user_data); + +/** + * @brief Unsets the state changed callback function. + * @since_tizen 6.0 + * @param [in] webrtc WebRTC handle + * @return @c 0 on success, + * otherwise a negative error value + * @retval #WEBRTC_ERROR_NONE Successful + * @retval #WEBRTC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #WEBRTC_ERROR_INVALID_OPERATION Invalid operation + * @see webrtc_set_state_changed_cb() + */ +int webrtc_unset_state_changed_cb(webrtc_h webrtc); + /** * @brief Creates an instance of WebRTC. * @since_tizen 6.0 diff --git a/include/webrtc_private.h b/include/webrtc_private.h index 4b1c4edb..e96fa154 100644 --- a/include/webrtc_private.h +++ b/include/webrtc_private.h @@ -149,6 +149,7 @@ typedef struct _webrtc_s { GList *signals; + webrtc_callbacks_s state_changed_cb; webrtc_callbacks_s negotiation_needed_cb; } webrtc_s; diff --git a/packaging/capi-media-webrtc.spec b/packaging/capi-media-webrtc.spec index 2f7fb69b..f691a8d9 100644 --- a/packaging/capi-media-webrtc.spec +++ b/packaging/capi-media-webrtc.spec @@ -1,6 +1,6 @@ Name: capi-media-webrtc Summary: A WebRTC library in Tizen Native API -Version: 0.1.10 +Version: 0.1.11 Release: 0 Group: Multimedia/API License: Apache-2.0 diff --git a/src/webrtc.c b/src/webrtc.c index f5a48c2c..d6ca5607 100644 --- a/src/webrtc.c +++ b/src/webrtc.c @@ -21,6 +21,46 @@ #include "webrtc.h" #include "webrtc_private.h" +int webrtc_set_state_changed_cb(webrtc_h webrtc, webrtc_state_changed_cb callback, void *user_data) +{ + webrtc_s *_webrtc = (webrtc_s*)webrtc; + + RET_VAL_IF(_webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL"); + RET_VAL_IF(callback == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "callback is NULL"); + + g_mutex_lock(&_webrtc->mutex); + + _webrtc->state_changed_cb.callback = callback; + _webrtc->state_changed_cb.user_data = user_data; + + LOG_INFO("callback[%p] user_data[%p]", callback, user_data); + + g_mutex_unlock(&_webrtc->mutex); + + return WEBRTC_ERROR_NONE; +} + +int webrtc_unset_state_changed_cb(webrtc_h webrtc) +{ + webrtc_s *_webrtc = (webrtc_s*)webrtc; + + RET_VAL_IF(_webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL"); + + g_mutex_lock(&_webrtc->mutex); + + RET_VAL_WITH_UNLOCK_IF(_webrtc->state_changed_cb.callback == NULL, WEBRTC_ERROR_INVALID_OPERATION, &_webrtc->mutex, "callback was not set"); + + LOG_INFO("callback[%p] user_data[%p] is reset to NULL", + _webrtc->state_changed_cb.callback, _webrtc->state_changed_cb.user_data); + + _webrtc->state_changed_cb.callback = NULL; + _webrtc->state_changed_cb.user_data = NULL; + + g_mutex_unlock(&_webrtc->mutex); + + return WEBRTC_ERROR_NONE; +} + int webrtc_create(webrtc_h *webrtc) { int ret = WEBRTC_ERROR_NONE; diff --git a/src/webrtc_private.c b/src/webrtc_private.c index 0b5e85d3..856a9f2d 100644 --- a/src/webrtc_private.c +++ b/src/webrtc_private.c @@ -174,9 +174,17 @@ static gboolean __bus_watch_cb(GstBus *bus, GstMessage *message, gpointer user_d } if (__meet_gst_state(webrtc->pend_state, gst_state_new)) { + webrtc_state_e old_state = webrtc->state; + LOG_INFO("webrtc state is changed [%d] -> [%d]", webrtc->state, webrtc->pend_state); webrtc->state = webrtc->pend_state; - /* TODO: trigger state changed cb */ + + if (webrtc->state_changed_cb.callback) { + LOG_DEBUG(">>> invoke state_changed_cb[%p], user_data[%p]", + webrtc->state_changed_cb.callback, webrtc->state_changed_cb.user_data); + ((webrtc_state_changed_cb)(webrtc->state_changed_cb.callback))((webrtc_h)webrtc, old_state, webrtc->state, webrtc->state_changed_cb.user_data); + LOG_DEBUG("<<< end of the callback"); + } } g_mutex_unlock(&webrtc->mutex); diff --git a/test/webrtc_test.c b/test/webrtc_test.c index 6dc9dbd6..828cae7f 100644 --- a/test/webrtc_test.c +++ b/test/webrtc_test.c @@ -315,9 +315,37 @@ static void _webrtc_set_stun_server(char *uri) g_print("webrtc_set_stun_server() success, uri[%s]\n", g_stun_server); } +static void __state_changed_cb(webrtc_h webrtc, webrtc_state_e previous, webrtc_state_e current, void *user_data) +{ + g_print("__state_changed_cb() is invoked, webrtc[%p], previous(%d) -> current(%d), user_data[%p]\n", + webrtc, previous, current, user_data); +} + +static void _webrtc_set_state_changed_cb() +{ + int ret = WEBRTC_ERROR_NONE; + + ret = webrtc_set_state_changed_cb(g_webrtc, __state_changed_cb, g_webrtc); + if (ret != WEBRTC_ERROR_NONE) + g_print("failed to webrtc_set_state_changed_cb()\n"); + else + g_print("webrtc_set_state_changed_cb() success\n"); +} + +static void _webrtc_unset_state_changed_cb() +{ + int ret = WEBRTC_ERROR_NONE; + + ret = webrtc_unset_state_changed_cb(g_webrtc); + if (ret != WEBRTC_ERROR_NONE) + g_print("failed to webrtc_unset_state_changed_cb()\n"); + else + g_print("webrtc_unset_state_changed_cb() success\n"); +} + static void __negotiation_needed_cb(webrtc_h webrtc, void *user_data) { - g_print("__negotiation_needed_cb() is invoked\n"); + g_print("__negotiation_needed_cb() is invoked, webrtc[%p], user_data[%p]\n", webrtc, user_data); } static void _webrtc_set_negotiation_needed_cb() @@ -469,6 +497,12 @@ void _interpret_main_menu(char *cmd) } else if (strncmp(cmd, "px", 2) == 0) { g_menu_state = CURRENT_STATUS_SETTING_PROXY; + } else if (strncmp(cmd, "sc", 2) == 0) { + _webrtc_set_state_changed_cb(); + + } else if (strncmp(cmd, "us", 2) == 0) { + _webrtc_unset_state_changed_cb(); + } else if (strncmp(cmd, "sn", 2) == 0) { _webrtc_set_negotiation_needed_cb(); @@ -509,6 +543,8 @@ void display_sub_basic() g_print("g. Get state\n"); g_print("a. Add media source\t"); g_print("r. Remove media source\n"); + g_print("sc. Set state changed callback\t"); + g_print("us. Unset state changed callback\n"); g_print("sn. Set negotiation needed callback\t"); g_print("un. Unset negotiation needed callback\n"); g_print("co. Create offer\t");