Add the basic state change logic 21/242621/5
authorSangchul Lee <sc11.lee@samsung.com>
Fri, 28 Aug 2020 08:48:22 +0000 (17:48 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Fri, 4 Sep 2020 03:14:48 +0000 (12:14 +0900)
[Version] 0.1.3
[Issue Type] Implementation

Change-Id: Ib2ecbfde9b7791753c8c2f56c34bbda7ed8dd616
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
include/webrtc_private.h
packaging/capi-media-webrtc.spec
src/webrtc.c
src/webrtc_private.c

index 5a7fc2444964bffaa389bab37d4a7067c4b8c111..7c1a5e85116c2d18661dfed569d2858f3ce50db2 100644 (file)
@@ -134,6 +134,7 @@ int _ini_load(webrtc_s *webrtc);
 int _gst_init(webrtc_s *webrtc);
 int _gst_build_pipeline(webrtc_s *webrtc);
 void _gst_destroy_pipeline(webrtc_s *webrtc);
+int _gst_pipeline_set_state(webrtc_s *webrtc, GstState state);
 
 #ifdef __cplusplus
 }
index 95e1e2433ef61172ff8ee5687d64a424c88655be..9405a89b5032ae91b239c0b5036700233d9b1bd6 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-webrtc
 Summary:    A WebRTC library in Tizen Native API
-Version:    0.1.2
+Version:    0.1.3
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 6ee3ce4865b459ac872ac2182f41d1f73724ab04..700dfb8ed206ce6bfe23d156fe1607dbcd64f612 100644 (file)
@@ -37,7 +37,8 @@ int webrtc_create(webrtc_h *webrtc)
        _gst_init(_webrtc);
        _gst_build_pipeline(_webrtc);
 
-       _webrtc->state = WEBRTC_STATE_IDLE;
+       _webrtc->pend_state = WEBRTC_STATE_IDLE;
+       _webrtc->state = _webrtc->pend_state;
 
        *webrtc = _webrtc;
 
@@ -56,7 +57,10 @@ int webrtc_destroy(webrtc_h webrtc)
 
        g_mutex_lock(&_webrtc->mutex);
 
-       /* set state to null here */
+       _gst_pipeline_set_state(webrtc, GST_STATE_NULL);
+
+       _webrtc->pend_state = WEBRTC_STATE_IDLE;
+       _webrtc->state = _webrtc->pend_state;
 
        _gst_destroy_pipeline(_webrtc);
 
@@ -82,6 +86,9 @@ int webrtc_start(webrtc_h webrtc)
 
        /* Implementation */
 
+       _gst_pipeline_set_state(webrtc, GST_STATE_PLAYING);
+       _webrtc->pend_state = WEBRTC_STATE_PLAYING;
+
        g_mutex_unlock(&_webrtc->mutex);
 
        LOG_INFO("webrtc[%p] is started", webrtc);
@@ -95,14 +102,15 @@ int webrtc_stop(webrtc_h webrtc)
 
        RET_VAL_IF(_webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
 
-       LOG_DEBUG_ENTER();
-
        g_mutex_lock(&_webrtc->mutex);
 
        RET_VAL_WITH_UNLOCK_IF(_webrtc->state != WEBRTC_STATE_PLAYING, WEBRTC_ERROR_INVALID_STATE, &_webrtc->mutex, "the state should be PLAYING");
 
        /* Implementation */
 
+       _gst_pipeline_set_state(webrtc, GST_STATE_READY);
+       _webrtc->pend_state = WEBRTC_STATE_IDLE;
+
        g_mutex_unlock(&_webrtc->mutex);
 
        LOG_INFO("webrtc[%p] is stopped", webrtc);
@@ -116,8 +124,12 @@ int webrtc_get_state(webrtc_h webrtc, webrtc_state_e *state)
 
        RET_VAL_IF(_webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
 
+       g_mutex_lock(&_webrtc->mutex);
+
        *state = _webrtc->state;
 
+       g_mutex_unlock(&_webrtc->mutex);
+
        LOG_INFO("state[%d]", *state);
 
        return WEBRTC_ERROR_NONE;
@@ -180,4 +192,4 @@ int webrtc_set_stun_server(webrtc_h webrtc, const char *stun_server)
        LOG_INFO("stun_server[%s]", stun_server);
 
        return WEBRTC_ERROR_NONE;
-}
\ No newline at end of file
+}
index 8b400d12a870daa27204cb032576af2b5eb8ff59..c584bb43392b9657eee6e301ca62203710e9fdf1 100644 (file)
 #include "webrtc.h"
 #include "webrtc_private.h"
 
+static gboolean __meet_gst_state(webrtc_state_e state, GstState gst_state)
+{
+       if (state == WEBRTC_STATE_IDLE && gst_state == GST_STATE_READY)
+               return TRUE;
+
+       if (state == WEBRTC_STATE_PLAYING && gst_state == GST_STATE_PLAYING)
+               return TRUE;
+
+       return FALSE;
+}
+
 static gboolean __bus_watch_cb(GstBus *bus, GstMessage *message, gpointer user_data)
 {
        webrtc_s *webrtc = (webrtc_s *)user_data;
@@ -54,10 +65,24 @@ static gboolean __bus_watch_cb(GstBus *bus, GstMessage *message, gpointer user_d
                                                                gst_element_state_get_name(gst_state_pending));
 
                LOG_INFO("GST_MESSAGE_STATE_CHANGED: %s", state_transition_name);
+               g_free(state_transition_name);
 
-               /* FIXME : set pipeline state */
+               /* TODO: generate dot */
+
+               g_mutex_lock(&webrtc->mutex);
+               if (webrtc->pend_state == webrtc->state) {
+                       LOG_DEBUG("pend_state(%d) is same with current state(%d)", webrtc->pend_state, webrtc->state);
+                       g_mutex_unlock(&webrtc->mutex);
+                       break;
+               }
+
+               if (__meet_gst_state(webrtc->pend_state, gst_state_new)) {
+                       LOG_INFO("webrtc state is changed [%d] -> [%d]", webrtc->state, webrtc->pend_state);
+                       webrtc->state = webrtc->pend_state;
+                       /* TODO: trigger state changed cb */
+               }
+               g_mutex_unlock(&webrtc->mutex);
 
-               g_free(state_transition_name);
                break;
 
        case GST_MESSAGE_ASYNC_DONE:
@@ -65,12 +90,10 @@ static gboolean __bus_watch_cb(GstBus *bus, GstMessage *message, gpointer user_d
                        return TRUE;
 
                LOG_INFO("GST_MESSAGE_ASYNC_DONE");
-
                break;
 
        case GST_MESSAGE_EOS:
                LOG_INFO("GST_MESSAGE_EOS end-of-stream");
-
                break;
 
        default:
@@ -211,3 +234,19 @@ void _gst_destroy_pipeline(webrtc_s *webrtc)
        }
 }
 
+int _gst_pipeline_set_state(webrtc_s *webrtc, GstState state)
+{
+       GstStateChangeReturn ret;
+
+       RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
+       RET_VAL_IF(webrtc->gst.pipeline == NULL, WEBRTC_ERROR_INVALID_OPERATION, "pipeline is NULL");
+
+       ret = gst_element_set_state(webrtc->gst.pipeline, state);
+       if (ret == GST_STATE_CHANGE_FAILURE) {
+               LOG_ERROR("failed to gst_element_set_state(), state[%s]", gst_element_state_get_name(state));
+               return WEBRTC_ERROR_INVALID_OPERATION;
+       }
+
+       return WEBRTC_ERROR_NONE;
+}
+