From: Sangchul Lee Date: Mon, 11 Jan 2021 10:03:00 +0000 (+0900) Subject: webrtc_websocket: Add support for websocket service X-Git-Tag: submit/tizen/20210729.023123~158 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f451453dcd20627f4d4c8cf2a0219b59ddcaf182;p=platform%2Fcore%2Fapi%2Fwebrtc.git webrtc_websocket: Add support for websocket service 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 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 70fe5504..0001fac1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/webrtc_private.h b/include/webrtc_private.h index 9f230d97..15c49bb5 100644 --- a/include/webrtc_private.h +++ b/include/webrtc_private.h @@ -27,6 +27,7 @@ #include #include #include +#include #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 */ diff --git a/packaging/capi-media-webrtc.spec b/packaging/capi-media-webrtc.spec index 6017b001..9763151f 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.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 index 00000000..4b238b86 --- /dev/null +++ b/src/webrtc_websocket.c @@ -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; +}