Add API set for state changed callback 01/243201/12
authorSangchul Lee <sc11.lee@samsung.com>
Fri, 4 Sep 2020 01:45:39 +0000 (10:45 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Fri, 11 Sep 2020 05:10:14 +0000 (14:10 +0900)
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 <sc11.lee@samsung.com>
include/webrtc.h
include/webrtc_private.h
packaging/capi-media-webrtc.spec
src/webrtc.c
src/webrtc_private.c
test/webrtc_test.c

index 53e3c3bc9e5ef2fc994ee76fc08b3bcfcace7cb6..84b30d018eaa772ab3ecda9a15dc907a3dd97f33 100644 (file)
@@ -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
index 4b1c4edb67948f28cf2af0274cff7ba7a9d155c7..e96fa1540ed4a2ce66b850cdcd46d5fb9237a7dc 100644 (file)
@@ -149,6 +149,7 @@ typedef struct _webrtc_s {
 
        GList *signals;
 
+       webrtc_callbacks_s state_changed_cb;
        webrtc_callbacks_s negotiation_needed_cb;
 } webrtc_s;
 
index 2f7fb69b644cf2a5b1823d90e8f8d4dc3d4a8a4e..f691a8d9f2eb2798b0918b0bf8fba7fd95990a86 100644 (file)
@@ -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
index f5a48c2c429e34c480bf12a59536de41707004a7..d6ca56075c86ad9ce3b90482b5d9c69860e0a8d3 100644 (file)
 #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;
index 0b5e85d3dfff156602f65dd8f8718f5aa3a8a047..856a9f2d1bbc8ed8183d68e2eb8cbf3eafd3dcc4 100644 (file)
@@ -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);
 
index 6dc9dbd618b0f3f4eb8034422e045ecadaf1a51f..828cae7f36005eed935efe7a709a87a8ee8d1d50 100644 (file)
@@ -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");