Add API set for the peer connection state 83/256983/10
authorSangchul Lee <sc11.lee@samsung.com>
Fri, 16 Apr 2021 02:51:41 +0000 (11:51 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Tue, 4 May 2021 02:52:48 +0000 (11:52 +0900)
These correspond to the 'connectionState' and 'onconnectionstatechange'
property of the RTCPeerConnection respectively.

Enums are added as below.
 - WEBRTC_PEER_CONNECTION_STATE_NEW
 - WEBRTC_PEER_CONNECTION_STATE_CONNECTING
 - WEBRTC_PEER_CONNECTION_STATE_CONNECTED
 - WEBRTC_PEER_CONNECTION_STATE_DISCONNECTED
 - WEBRTC_PEER_CONNECTION_STATE_FAILED
 - WEBRTC_PEER_CONNECTION_STATE_CLOSED

Functions are added as below.
 - webrtc_set[unset]_peer_connection_state_change_cb()
 - webrtc_get_peer_connection_state()

[Version] 0.1.161
[Issue Type] API

Change-Id: I271413364e4e3704d1df021d9174aac6a18cd225
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

index e80bd2a9c54496e7c6b0264c1fcc064b8bef85b8..2bcc6e910abb307a98e12610dbe9f68c9f59e274 100644 (file)
@@ -93,6 +93,19 @@ typedef enum
        WEBRTC_STATE_PLAYING,     /**<  Started all streams */
 } webrtc_state_e;
 
+/**
+ * @brief Enumeration for WebRTC peer connection state.
+ * @since_tizen 6.5
+ */
+typedef enum {
+    WEBRTC_PEER_CONNECTION_STATE_NEW,          /**<  New */
+    WEBRTC_PEER_CONNECTION_STATE_CONNECTING,   /**<  Connecting */
+    WEBRTC_PEER_CONNECTION_STATE_CONNECTED,    /**<  Connected */
+    WEBRTC_PEER_CONNECTION_STATE_DISCONNECTED, /**<  Disconnected */
+    WEBRTC_PEER_CONNECTION_STATE_FAILED,       /**<  Failed */
+    WEBRTC_PEER_CONNECTION_STATE_CLOSED,       /**<  Closed */
+} webrtc_peer_connection_state_e;
+
 /**
  * @brief Enumeration for WebRTC media source type.
  * @since_tizen 6.5
@@ -186,6 +199,18 @@ typedef void (*webrtc_error_cb)(webrtc_h webrtc, webrtc_error_e error, webrtc_st
  */
 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 peer connection state is changed.
+ * @since_tizen 6.5
+ * @remarks The @a webrtc is the same object for which the callback was set.\nThe @a webrtc should not be released.
+ * @param[in] webrtc     WebRTC handle
+ * @param[in] state      The peer connection state
+ * @param[in] user_data  The user data passed from the callback registration function
+ * @see webrtc_set_peer_connection_state_change_cb()
+ * @see webrtc_unset_peer_connection_state_change_cb()
+ */
+typedef void (*webrtc_peer_connection_state_change_cb)(webrtc_h webrtc, webrtc_peer_connection_state_e state, void *user_data);
+
 /**
  * @brief Called when the buffer state of media packet source is changed.
  * @since_tizen 6.5
@@ -759,6 +784,52 @@ int webrtc_add_turn_server(webrtc_h webrtc, const char *turn_server);
  */
 int webrtc_foreach_turn_server(webrtc_h webrtc, webrtc_turn_server_cb callback, void *user_data);
 
+/**
+ * @brief Sets a callback function to be invoked when the WebRTC peer connection state is changed.
+ * @since_tizen 6.5
+ * @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_peer_connection_state_change_cb() will be invoked.
+ * @see webrtc_unset_peer_connection_state_change_cb()
+ * @see webrtc_peer_connection_state_change_cb()
+ * @see webrtc_get_peer_connection_state()
+ */
+int webrtc_set_peer_connection_state_change_cb(webrtc_h webrtc, webrtc_peer_connection_state_change_cb callback, void *user_data);
+
+/**
+ * @brief Unsets the peer connection state change callback function.
+ * @since_tizen 6.5
+ * @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_peer_connection_state_change_cb()
+ * @see webrtc_get_peer_connection_state()
+ */
+int webrtc_unset_peer_connection_state_change_cb(webrtc_h webrtc);
+
+/**
+ * @brief Gets the peer connection state.
+ * @since_tizen 6.5
+ * @remarks The default value is #WEBRTC_PEER_CONNECTION_STATE_NEW.
+ * @param[in] webrtc      WebRTC handle
+ * @param[out] state      The peer connection state
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #WEBRTC_ERROR_NONE    Successful
+ * @retval #WEBRTC_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see webrtc_set_peer_connection_state_change_cb()
+ * @see webrtc_unset_peer_connection_state_change_cb()
+ */
+int webrtc_get_peer_connection_state(webrtc_h webrtc, webrtc_peer_connection_state_e *state);
+
 /**
  * @brief Sets a negotiation needed callback function to be invoked when a change has occurred which requires session negotiation.
  * @since_tizen 6.5
index 1d20e1bf6f58720b9c46fbd9061e891107020da1..73125af4883c618855486193a71e1678dfd63cc2 100644 (file)
@@ -355,6 +355,7 @@ typedef struct _webrtc_s {
 
        webrtc_callbacks_s error_cb;
        webrtc_callbacks_s state_changed_cb;
+       webrtc_callbacks_s peer_connection_state_change_cb;
        webrtc_callbacks_s negotiation_needed_cb;
        webrtc_callbacks_s ice_candidate_cb;
        webrtc_callbacks_s track_added_cb;
@@ -363,8 +364,8 @@ typedef struct _webrtc_s {
        webrtc_callbacks_s data_channel_cb;
 
        struct {
+               webrtc_peer_connection_state_e peer_connection_state;
                const gchar *signaling_state;
-               const gchar *peer_connection_state;
                const gchar *ice_connection_state;
                const gchar *ice_gathering_state;
        } internal_states;
index d539c9b73a30d3208e1501e505507fb8e20cb5de..1da958b1fd2e6f756f8e9de98b124b698b87e45a 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-webrtc
 Summary:    A WebRTC library in Tizen Native API
-Version:    0.1.160
+Version:    0.1.161
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 168dbcff8cc69452e5155f92ab937c6e75850daa..1ffb617724b288c443d469ff711d1fd58a9b9b32 100644 (file)
@@ -593,6 +593,69 @@ int webrtc_foreach_turn_server(webrtc_h webrtc, webrtc_turn_server_cb callback,
        return WEBRTC_ERROR_NONE;
 }
 
+int webrtc_set_peer_connection_state_change_cb(webrtc_h webrtc, webrtc_peer_connection_state_change_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);
+
+       RET_VAL_WITH_UNLOCK_IF(_webrtc->state != WEBRTC_STATE_IDLE, WEBRTC_ERROR_INVALID_STATE, &_webrtc->mutex, "the state should be IDLE");
+
+       LOG_WARNING_IF_CALLBACK_EXISTS(_webrtc->peer_connection_state_change_cb);
+
+       _webrtc->peer_connection_state_change_cb.callback = callback;
+       _webrtc->peer_connection_state_change_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_peer_connection_state_change_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 != WEBRTC_STATE_IDLE, WEBRTC_ERROR_INVALID_STATE, &_webrtc->mutex, "the state should be IDLE");
+       RET_VAL_WITH_UNLOCK_IF(_webrtc->peer_connection_state_change_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->peer_connection_state_change_cb.callback, _webrtc->peer_connection_state_change_cb.user_data);
+
+       _webrtc->peer_connection_state_change_cb.callback = NULL;
+       _webrtc->peer_connection_state_change_cb.user_data = NULL;
+
+       g_mutex_unlock(&_webrtc->mutex);
+
+       return WEBRTC_ERROR_NONE;
+}
+
+int webrtc_get_peer_connection_state(webrtc_h webrtc, webrtc_peer_connection_state_e *state)
+{
+       webrtc_s *_webrtc = (webrtc_s*)webrtc;
+
+       RET_VAL_IF(_webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
+       RET_VAL_IF(state == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "state is NULL");
+
+       g_mutex_lock(&_webrtc->mutex);
+
+       *state = _webrtc->internal_states.peer_connection_state;
+
+       LOG_INFO("state[%d]", *state);
+
+       g_mutex_unlock(&_webrtc->mutex);
+
+       return WEBRTC_ERROR_NONE;
+}
+
 int webrtc_set_negotiation_needed_cb(webrtc_h webrtc, webrtc_negotiation_needed_cb callback, void *user_data)
 {
        webrtc_s *_webrtc = (webrtc_s*)webrtc;
index fd303f72dfbdf5aa9323ab65593403c0e2b53bb5..f1e39888582e99f6b3d43707f8eb13e204a0b12d 100644 (file)
@@ -31,12 +31,26 @@ typedef struct {
        GstWebRTCRTPTransceiverDirection gst;
 } direction_info_s;
 
+typedef struct {
+       const char *str;
+       webrtc_peer_connection_state_e state;
+} peer_connection_state_info_s;
+
 static direction_info_s __direction_info[] = {
        [WEBRTC_TRANSCEIVER_DIRECTION_SENDONLY] = { "SENDONLY", GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY },
        [WEBRTC_TRANSCEIVER_DIRECTION_RECVONLY] = { "RECVONLY", GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_RECVONLY },
        [WEBRTC_TRANSCEIVER_DIRECTION_SENDRECV] = { "SENDRECV", GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_SENDRECV }
 };
 
+static peer_connection_state_info_s __peer_connection_state_info[] = {
+       [GST_WEBRTC_PEER_CONNECTION_STATE_NEW] = { "NEW", WEBRTC_PEER_CONNECTION_STATE_NEW },
+       [GST_WEBRTC_PEER_CONNECTION_STATE_CONNECTING] = { "CONNECTING", WEBRTC_PEER_CONNECTION_STATE_CONNECTING },
+       [GST_WEBRTC_PEER_CONNECTION_STATE_CONNECTED] = { "CONNECTED", WEBRTC_PEER_CONNECTION_STATE_CONNECTED },
+       [GST_WEBRTC_PEER_CONNECTION_STATE_DISCONNECTED] = { "DISCONNECTED", WEBRTC_PEER_CONNECTION_STATE_DISCONNECTED },
+       [GST_WEBRTC_PEER_CONNECTION_STATE_FAILED] = { "FAILED", WEBRTC_PEER_CONNECTION_STATE_FAILED },
+       [GST_WEBRTC_PEER_CONNECTION_STATE_CLOSED] = { "CLOSED", WEBRTC_PEER_CONNECTION_STATE_CLOSED },
+};
+
 static const char *__get_error_string(webrtc_error_e error)
 {
        switch (error) {
@@ -202,6 +216,23 @@ static void __invoke_error_cb(webrtc_s *webrtc, webrtc_error_e error)
        }
 }
 
+static void __invoke_peer_connection_state_change_cb(webrtc_s *webrtc, GstWebRTCPeerConnectionState state)
+{
+       webrtc_callbacks_s *cb;
+
+       RET_IF(webrtc == NULL, "webrtc is NULL");
+
+       cb = &webrtc->peer_connection_state_change_cb;
+
+       if (cb->callback) {
+               LOG_DEBUG(">>> callback[%p], user_data[%p]", cb->callback, cb->user_data);
+               ((webrtc_peer_connection_state_change_cb)(cb->callback))((webrtc_h)webrtc, __peer_connection_state_info[state].state, cb->user_data);
+               LOG_DEBUG("<<< end of the callback");
+       }
+
+       GENERATE_DOT(webrtc, "PEER_CONNECTION_STATE_%s", __peer_connection_state_info[state].str);
+}
+
 static gboolean __bus_watch_cb(GstBus *bus, GstMessage *message, gpointer user_data)
 {
        webrtc_s *webrtc = (webrtc_s *)user_data;
@@ -599,51 +630,33 @@ static void __webrtcbin_on_ice_candidate_cb(GstElement *webrtcbin, guint mlinein
 static void __webrtcbin_peer_connection_state_cb(GstElement *webrtcbin, GParamSpec * pspec, gpointer user_data)
 {
        webrtc_s *webrtc = (webrtc_s *)user_data;
-       GstWebRTCPeerConnectionState peer_connection_state;
-       const gchar *new_state = "UNKNOWN";
+       GstWebRTCPeerConnectionState state;
 
        RET_IF(webrtcbin == NULL, "webrtcbin is NULL");
        RET_IF(webrtc == NULL, "webrtc is NULL");
 
-       g_object_get(webrtcbin, "connection-state", &peer_connection_state, NULL);
+       g_object_get(webrtcbin, "connection-state", &state, NULL);
 
-       switch (peer_connection_state) {
-       case GST_WEBRTC_PEER_CONNECTION_STATE_NEW:
-               new_state = "NEW";
+       LOG_DEBUG("[PeerConnectionState] is changed to [%s]", __peer_connection_state_info[state].str);
+       webrtc->internal_states.peer_connection_state = __peer_connection_state_info[state].state;
+
+       __invoke_peer_connection_state_change_cb(webrtc, state);
 
+       switch (state) {
+       case GST_WEBRTC_PEER_CONNECTION_STATE_NEW:
                g_mutex_lock(&webrtc->mutex);
                if (webrtc->state == WEBRTC_STATE_NEGOTIATING)
                        _post_state_cb_in_idle(webrtc, WEBRTC_STATE_PLAYING);
                g_mutex_unlock(&webrtc->mutex);
                break;
 
-       case GST_WEBRTC_PEER_CONNECTION_STATE_CONNECTING:
-               new_state = "CONNECTING";
-               break;
-
-       case GST_WEBRTC_PEER_CONNECTION_STATE_CONNECTED:
-               new_state = "CONNECTED";
-               break;
-
-       /* FIXME: error forwarding in case of disconnection/failed via error callback */
-       case GST_WEBRTC_PEER_CONNECTION_STATE_DISCONNECTED:
-               new_state = "DISCONNECTED";
-               break;
-
        case GST_WEBRTC_PEER_CONNECTION_STATE_FAILED:
-               new_state = "FAILED";
-
                __invoke_error_cb(webrtc, WEBRTC_ERROR_CONNECTION_FAILED);
                break;
 
-       case GST_WEBRTC_PEER_CONNECTION_STATE_CLOSED:
-               new_state = "CLOSED";
+       default:
                break;
        }
-
-       LOG_DEBUG("[PeerConnectionState] is changed to [%s]", new_state);
-
-       webrtc->internal_states.peer_connection_state = new_state;
 }
 
 static void __webrtcbin_signaling_state_cb(GstElement *webrtcbin, GParamSpec * pspec, gpointer user_data)