webrtc_websocket: Add support for websocket service 18/251218/12
authorSangchul Lee <sc11.lee@samsung.com>
Mon, 11 Jan 2021 10:03:00 +0000 (19:03 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Thu, 21 Jan 2021 07:33:49 +0000 (16:33 +0900)
These internal API set will be called by the signaling server
added by patches coming up next.

[Version] 0.1.83
[Issue Type] New feature

Change-Id: I9f047750aa36bbaa67625ac440de73f157c7aaa9
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
CMakeLists.txt
include/webrtc_private.h
packaging/capi-media-webrtc.spec
src/webrtc_websocket.c [new file with mode: 0644]

index 70fe5504d7f213337ec291dae9f2e92e5293e301..0001fac18b224904df254a21127d693a76d10ae4 100644 (file)
@@ -12,7 +12,7 @@ SET(INC_DIR include)
 INCLUDE_DIRECTORIES(${INC_DIR})
 
 SET(dependents "dlog glib-2.0 gstreamer-1.0 gstreamer-webrtc-1.0 gstreamer-video-1.0 gstreamer-audio-1.0 \
-                json-glib-1.0 iniparser mm-common mm-display-interface capi-media-tool libtbm")
+                json-glib-1.0 iniparser mm-common mm-display-interface capi-media-tool libtbm libwebsockets")
 SET(pc_dependents "capi-base-common" )
 
 INCLUDE(FindPkgConfig)
index 9f230d976c4a29fc03c0a9f9d8f42fcb81b6cbaa..15c49bb5bc3c97ac6f90b54b001e968cc658e0c8 100644 (file)
@@ -27,6 +27,7 @@
 #include <iniparser.h>
 #include <mm_display_interface.h>
 #include <tbm_bufmgr.h>
+#include <libwebsockets.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -328,6 +329,11 @@ typedef struct _webrtc_signal_s {
        gulong signal_id;
 } webrtc_signal_s;
 
+typedef struct _webrtc_websocket_s {
+       GMutex mutex;
+       struct lws_context *context;
+} webrtc_websocket_s;
+
 int _load_ini(webrtc_s *webrtc);
 void _unload_ini(webrtc_s *webrtc);
 ini_item_media_source_s* _ini_get_source_by_type(webrtc_ini_s *ini, webrtc_media_source_type_e type);
@@ -387,6 +393,12 @@ int _destroy_data_channel(webrtc_data_channel_s *channel);
 int _data_channel_send_string(webrtc_data_channel_s *channel, const char *string);
 int _data_channel_send_bytes(webrtc_data_channel_s *channel, const char *data, unsigned int size);
 
+typedef int (*_websocket_cb)(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
+webrtc_websocket_s *_alloc_websocket(const int port, const char *ssl_cert_path, const char *ssl_private_key_path, const char *ssl_ca_path, _websocket_cb callback, void *user_data);
+void _release_websocket(webrtc_websocket_s *ws);
+int _start_websocket(webrtc_websocket_s *ws, int timeout_ms);
+int _stop_websocket(webrtc_websocket_s *ws);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 6017b00105c7dbf70178465bb80199031166d382..9763151f2f59523a29520f967ca926807ca03e74 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-webrtc
 Summary:    A WebRTC library in Tizen Native API
-Version:    0.1.82
+Version:    0.1.83
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
@@ -24,6 +24,7 @@ BuildRequires:  pkgconfig(iniparser)
 BuildRequires:  pkgconfig(mm-display-interface)
 BuildRequires:  pkgconfig(mm-common)
 BuildRequires:  pkgconfig(libtbm)
+BuildRequires:  pkgconfig(libwebsockets)
 
 %description
 A WebRTC library in Tizen Native API.
diff --git a/src/webrtc_websocket.c b/src/webrtc_websocket.c
new file mode 100644 (file)
index 0000000..4b238b8
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "webrtc.h"
+#include "webrtc_private.h"
+
+webrtc_websocket_s *_alloc_websocket(const int port, const char *ssl_cert_path, const char *ssl_private_key_path, const char *ssl_ca_path, _websocket_cb callback, void *user_data)
+{
+       struct lws_protocols protocols[] = {
+               {
+                       "signaling-protocol",
+                       callback,
+                       0,
+                       0,
+                       0,
+               },
+               {
+                       NULL, NULL, 0, 0 /* end of list */
+               }
+       };
+
+       struct lws_context_creation_info info = {
+               .port = port,
+               .iface = NULL,
+               .protocols = protocols,
+               .extensions = NULL,
+               .ssl_cert_filepath = ssl_cert_path,
+               .ssl_private_key_filepath = ssl_private_key_path,
+               .ssl_ca_filepath = ssl_ca_path,
+               .gid = -1,
+               .uid = -1,
+               .user = user_data
+       };
+
+       webrtc_websocket_s *ws = g_new0(webrtc_websocket_s, 1);
+
+       ws->context = lws_create_context(&info);
+       if (!ws->context) {
+               LOG_ERROR("failed to lws_create_context()");
+               g_free(ws);
+               return NULL;
+       }
+
+       g_mutex_init(&ws->mutex);
+
+       LOG_DEBUG("ws[%p, context:%p]", ws, ws->context);
+
+       return ws;
+}
+
+void _release_websocket(webrtc_websocket_s *ws)
+{
+       RET_IF(ws == NULL, "ws is NULL");
+
+       g_mutex_lock(&ws->mutex);
+
+       LOG_DEBUG("ws[%p, context:%p]", ws, ws->context);
+
+       lws_context_destroy(ws->context);
+
+       g_mutex_unlock(&ws->mutex);
+       g_mutex_clear(&ws->mutex);
+
+       g_free(ws);
+
+       return;
+}
+
+int _start_websocket(webrtc_websocket_s *ws, int timeout_ms)
+{
+       int ret;
+
+       RET_VAL_IF(ws == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "ws is NULL");
+       RET_VAL_IF(timeout_ms < 0, WEBRTC_ERROR_INVALID_PARAMETER, "invalid timeout_ms(%d)", timeout_ms);
+
+       g_mutex_lock(&ws->mutex);
+
+       ret = lws_service(ws->context, timeout_ms);
+       if (ret != 0) {
+               LOG_ERROR("failed to lws_service(), ret[%d]", ret);
+               g_mutex_unlock(&ws->mutex);
+               return WEBRTC_ERROR_INVALID_OPERATION;
+       }
+
+       LOG_DEBUG("ws[%p, context:%p], timeout_ms[%d]", ws, ws->context, timeout_ms);
+
+       g_mutex_unlock(&ws->mutex);
+
+       return WEBRTC_ERROR_NONE;
+}
+
+int _stop_websocket(webrtc_websocket_s *ws)
+{
+       RET_VAL_IF(ws == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "ws is NULL");
+
+       g_mutex_lock(&ws->mutex);
+
+       lws_cancel_service(ws->context);
+
+       LOG_DEBUG("ws[%p, context:%p]", ws, ws->context);
+
+       g_mutex_unlock(&ws->mutex);
+
+       return WEBRTC_ERROR_NONE;
+}