From: Sangchul Lee Date: Fri, 28 Aug 2020 08:37:54 +0000 (+0900) Subject: Add initial APIs and test application X-Git-Tag: submit/tizen/20210729.023123~241 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F11%2F242611%2F5;p=platform%2Fcore%2Fapi%2Fwebrtc.git Add initial APIs and test application [Version] 0.1.1 [Issue Type] Initial code Change-Id: I398b849a97e7d88d0569ee74385754c59264d768 Signed-off-by: Sangchul Lee --- diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 00000000..ff39aba7 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,3 @@ +Sangchul Lee +Hyunil Park + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..354f6a63 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,104 @@ + +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(fw_name "capi-media-webrtc") + +PROJECT(${fw_name}) + +SET(CMAKE_INSTALL_PREFIX /usr) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib") + +SET(INC_DIR include) +INCLUDE_DIRECTORIES(${INC_DIR}) + +SET(dependents "dlog glib-2.0 gstreamer-1.0" ) +SET(pc_dependents "capi-base-common" ) + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_name} REQUIRED ${dependents}) +FOREACH(flag ${${fw_name}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC -Wall -Werror") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") + +IF("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-DTARGET") +ENDIF("${ARCH}" STREQUAL "arm") + +ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") + +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=${LIB_INSTALL_DIR}") + +AUX_SOURCE_DIRECTORY (src MAIN_SRC) + +LIST (APPEND SOURCES + ${MAIN_SRC} +) + +ADD_LIBRARY(${fw_name} SHARED ${SOURCES}) + +TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS}) + +SET_TARGET_PROPERTIES(${fw_name} + PROPERTIES + VERSION ${FULLVER} + SOVERSION ${MAJORVER} + CLEAN_DIRECT_OUTPUT 1 +) + +INSTALL(TARGETS ${fw_name} DESTINATION ${LIB_INSTALL_DIR}) +INSTALL( + DIRECTORY ${INC_DIR}/ DESTINATION include/media + FILES_MATCHING + PATTERN "webrtc_*.h" EXCLUDE + PATTERN "${INC_DIR}/*.h" +) + + +SET(PC_NAME ${fw_name}) +SET(PC_REQUIRED ${pc_dependents}) +SET(PC_LDFLAGS -l${fw_name}) +SET(PC_CFLAGS -I\${includedir}/media) + +CONFIGURE_FILE( + ${fw_name}.pc.in + ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc + @ONLY +) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) + +ADD_SUBDIRECTORY(test) + +IF(UNIX) + +ADD_CUSTOM_TARGET (distclean @echo cleaning for source distribution) +ADD_CUSTOM_COMMAND( + DEPENDS clean + COMMENT "distribution clean" + COMMAND find + ARGS . + -not -name config.cmake -and \( + -name tester.c -or + -name Testing -or + -name CMakeFiles -or + -name cmake.depends -or + -name cmake.check_depends -or + -name CMakeCache.txt -or + -name cmake.check_cache -or + -name *.cmake -or + -name Makefile -or + -name core -or + -name core.* -or + -name gmon.out -or + -name install_manifest.txt -or + -name *.pc -or + -name *~ \) + | grep -v TC | xargs rm -rf + TARGET distclean + VERBATIM +) + +ENDIF(UNIX) + diff --git a/LICENSE.APLv2 b/LICENSE.APLv2 new file mode 100644 index 00000000..483e1e9c --- /dev/null +++ b/LICENSE.APLv2 @@ -0,0 +1,204 @@ +Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. + diff --git a/NOTICE b/NOTICE new file mode 100644 index 00000000..ccdad52e --- /dev/null +++ b/NOTICE @@ -0,0 +1,3 @@ +Copyright (c) Samsung Electronics Co., Ltd. All rights reserved. +Except as noted, this software is licensed under Apache License, Version 2. +Please, see the LICENSE file for Apache License terms and conditions. diff --git a/capi-media-webrtc.pc.in b/capi-media-webrtc.pc.in new file mode 100644 index 00000000..8cb4d53d --- /dev/null +++ b/capi-media-webrtc.pc.in @@ -0,0 +1,14 @@ +# Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=/usr +libdir=@LIB_INSTALL_DIR@ +includedir=@INCLUDE_INSTALL_DIR@/media + +Name: @PC_NAME@ +Description: @PACKAGE_DESCRIPTION@ +Version: @VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} @PC_LDFLAGS@ +Cflags: -I${includedir} + diff --git a/include/webrtc.h b/include/webrtc.h new file mode 100644 index 00000000..62c46009 --- /dev/null +++ b/include/webrtc.h @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef __TIZEN_MEDIA_WEBRTC_H__ +#define __TIZEN_MEDIA_WEBRTC_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifndef TIZEN_ERROR_MEDIA_WEBRTC +#define TIZEN_ERROR_MEDIA_WEBRTC -0x03000000 +#endif + +/** +* @file webrtc.h +* @brief This file contains the WebRTC API. +*/ + +/** +* @addtogroup CAPI_MEDIA_WEBRTC_MODULE +* @{ +*/ + +/** + * @brief WebRTC handle type. + * @since_tizen 6.0 + */ +typedef void *webrtc_h; + +/** + * @brief Enumeration for WebRTC error. + * @since_tizen 6.0 + */ +typedef enum +{ + WEBRTC_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ + WEBRTC_ERROR_NOT_SUPPORTED = TIZEN_ERROR_NOT_SUPPORTED, /**< Not supported */ + WEBRTC_ERROR_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */ + WEBRTC_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ + WEBRTC_ERROR_INVALID_OPERATION = TIZEN_ERROR_INVALID_OPERATION, /**< Invalid operation */ + WEBRTC_ERROR_INVALID_STATE = TIZEN_ERROR_MEDIA_WEBRTC | 0x01, /**< Invalid state */ + WEBRTC_ERROR_CONNECTION_FAILED = TIZEN_ERROR_MEDIA_WEBRTC | 0x02, /**< Connection failed */ + WEBRTC_ERROR_RESOURCE_CONFLICT = TIZEN_ERROR_MEDIA_WEBRTC | 0x03, /**< Resource conflict */ +} webrtc_error_e; + +/** + * @brief Enumeration for WebRTC state. + * @since_tizen 6.0 + */ +typedef enum +{ + WEBRTC_STATE_IDLE, /**< Created but not started */ + WEBRTC_STATE_PLAYING, /**< Started */ +} webrtc_state_e; + +/** + * @brief Enumeration for WebRTC media source type. + * @since_tizen 6.0 + */ +typedef enum { + WEBRTC_MEDIA_SOURCE_TYPE_CAMERA, + WEBRTC_MEDIA_SOURCE_TYPE_MIC, + WEBRTC_MEDIA_SOURCE_TYPE_AUDIOTEST, + WEBRTC_MEDIA_SOURCE_TYPE_VIDEOTEST +} webrtc_media_source_type_e; + +/** + * @brief Creates an instance of WebRTC. + * @since_tizen 6.0 + * @remarks You must release @a webrtc using webrtc_destroy(). + * @param [out] 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 + * @post @a webrtc state will be #WEBRTC_STATE_IDLE. + * @see webrtc_destroy() + */ +int webrtc_create(webrtc_h *webrtc); + +/** + * @brief Starts the WebRTC. + * @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 + * @retval #WEBRTC_ERROR_INVALID_STATE Invalid state + * @pre @a webrtc state must be set to #WEBRTC_STATE_IDLE. + * @post @a webrtc state will be #WEBRTC_STATE_PLAYING. + * @see webrtc_create() + * @see webrtc_stop() + */ +int webrtc_start(webrtc_h webrtc); + +/** + * @brief Stops the WebRTC. + * @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 + * @retval #WEBRTC_ERROR_INVALID_STATE Invalid state + * @pre @a webrtc state will be #WEBRTC_STATE_PLAYING. + * @post @a webrtc state will be #WEBRTC_STATE_IDLE. + * @see webrtc_create() + * @see webrtc_start() + */ +int webrtc_stop(webrtc_h webrtc); + +/** + * @brief Destroys the WebRTC. + * @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 + * @retval #WEBRTC_ERROR_INVALID_STATE Invalid state + * @see webrtc_create() + */ +int webrtc_destroy(webrtc_h webrtc); + +/** + * @brief Gets the WebRTC state. + * @since_tizen 6.0 + * @param [in] webrtc WebRTC handle + * @param [out] state WebRTC state + * @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_state_e + */ +int webrtc_get_state(webrtc_h webrtc, webrtc_state_e *state); + +/** + * @brief Adds a media source. + * @since_tizen 6.0 + * @param [in] webrtc WebRTC handle + * @param [in] type The media source type to be added + * @param [out] source_id The media source id + * @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 + * @retval #WEBRTC_ERROR_INVALID_STATE Invalid state + * @pre @a webrtc state must be set to #WEBRTC_STATE_IDLE. + * @see webrtc_remove_media_source() + */ +int webrtc_add_media_source(webrtc_h webrtc, webrtc_media_source_type_e type, int *source_id); + +/** + * @brief Removes the media source. + * @since_tizen 6.0 + * @param [in] webrtc WebRTC handle + * @param [in] source_id The media source id to be removed + * @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 + * @retval #WEBRTC_ERROR_INVALID_STATE Invalid state + * @pre @a webrtc state must be set to #WEBRTC_STATE_IDLE. + * @pre Add media source to @a webrtc by calling webrtc_add_media_source(). + * @see webrtc_add_media_source() + */ +int webrtc_remove_media_source(webrtc_h webrtc, int source_id); + +/** + * @brief Sets a STUN server URL. + * @since_tizen 6.0 + * @param [in] webrtc WebRTC handle + * @param [in] stun_server The STUN server URL + * @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_STATE Invalid state + * @pre @a webrtc state must be set to #WEBRTC_STATE_IDLE. + */ +int webrtc_set_stun_server(webrtc_h webrtc, const char *stun_server); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __TIZEN_MEDIA_WEBRTC_H__ */ diff --git a/include/webrtc_private.h b/include/webrtc_private.h new file mode 100644 index 00000000..6de5cf9f --- /dev/null +++ b/include/webrtc_private.h @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef __TIZEN_MEDIA_WEBRTC_PRIVATE_H__ +#define __TIZEN_MEDIA_WEBRTC_PRIVATE_H__ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "TIZEN_N_WEBRTC" + +#define FONT_COLOR_RESET "\033[0m" +#define FONT_COLOR_RED "\033[31m" +#define FONT_COLOR_GREEN "\033[32m" +#define FONT_COLOR_YELLOW "\033[33m" +#define FONT_COLOR_BLUE "\033[34m" +#define FONT_COLOR_PURPLE "\033[35m" +#define FONT_COLOR_CYAN "\033[36m" +#define FONT_COLOR_GRAY "\033[37m" + +#define LOG_DEBUG(fmt, arg...) \ +do { \ + LOGD(FONT_COLOR_RESET""fmt""FONT_COLOR_RESET, ##arg); \ +} while (0) + +#define LOG_INFO(fmt, arg...) \ +do { \ + LOGI(FONT_COLOR_GREEN""fmt""FONT_COLOR_RESET, ##arg); \ +} while (0) + +#define LOG_WARNING(fmt, arg...) \ +do { \ + LOGW(FONT_COLOR_YELLOW""fmt""FONT_COLOR_RESET, ##arg); \ +} while (0) + +#define LOG_ERROR(fmt, arg...) \ +do { \ + LOGE(FONT_COLOR_RED""fmt""FONT_COLOR_RESET, ##arg); \ +} while (0) + +#define LOG_DEBUG_ENTER() \ +do { \ + LOGD(FONT_COLOR_PURPLE""FONT_COLOR_RESET); \ +} while (0) + +#define LOG_DEBUG_LEAVE() \ +do { \ + LOGD(FONT_COLOR_PURPLE""FONT_COLOR_RESET); \ +} while (0) + +#define RET_IF(expr, fmt, arg...) \ +do { \ + if ((expr)) { \ + LOG_ERROR(""fmt"", ##arg); \ + return; \ + } \ +} while (0) + +#define RET_VAL_IF(expr, val, fmt, arg...) \ +do { \ + if ((expr)) { \ + LOG_ERROR(""fmt"", ##arg); \ + return (val);\ + } \ +} while (0) + +#define RET_VAL_WITH_UNLOCK_IF(expr, val, mutex, fmt, arg...) \ +do { \ + if ((expr)) { \ + LOG_ERROR(""fmt"", ##arg); \ + g_mutex_unlock(mutex); \ + return (val);\ + } \ +} while (0) + +#define SAFE_FREE(src) { if (src) { free(src); src = NULL; } } + +#define SOURCES_MAX 8 + +typedef struct _webrtc_ini_s { + gboolean generate_dot; + gchar **gst_args; +} webrtc_ini_s; + +typedef struct _webrtc_gst_src_s { + int source_id; + GstElement *bin; +} webrtc_gst_src_s; + +typedef struct _webrtc_gst_s { + GstElement *pipeline; + GstBus *bus; + guint bus_watcher; + webrtc_gst_src_s *audiosrc[SOURCES_MAX]; + webrtc_gst_src_s *videosrc[SOURCES_MAX]; +} webrtc_gst_s; + +typedef struct _webrtc_s { + webrtc_ini_s ini; + + GMutex mutex; + + webrtc_gst_s gst; + + webrtc_state_e state; + webrtc_state_e pend_state; +} webrtc_s; + +int _ini_load(webrtc_s *webrtc); +int _gst_init(webrtc_s *webrtc); +int _gst_build_pipeline(webrtc_s *webrtc); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __TIZEN_MEDIA_WEBRTC_PRIVATE_H__ */ diff --git a/packaging/capi-media-webrtc.manifest b/packaging/capi-media-webrtc.manifest new file mode 100644 index 00000000..f29441a2 --- /dev/null +++ b/packaging/capi-media-webrtc.manifest @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/packaging/capi-media-webrtc.spec b/packaging/capi-media-webrtc.spec new file mode 100644 index 00000000..701f3b87 --- /dev/null +++ b/packaging/capi-media-webrtc.spec @@ -0,0 +1,65 @@ +Name: capi-media-webrtc +Summary: A WebRTC library in Tizen Native API +Version: 0.1.1 +Release: 0 +Group: Multimedia/API +License: Apache-2.0 +URL: http://source.tizen.org +Source0: %{name}-%{version}.tar.gz +Source1001: capi-media-webrtc.manifest +BuildRequires: cmake +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(capi-base-common) +BuildRequires: pkgconfig(gstreamer-1.0) +BuildRequires: pkgconfig(appcore-efl) +BuildRequires: pkgconfig(elementary) + +%description +A WebRTC library in Tizen Native API. + +%package devel +Summary: Multimedia WebRTC Library in Tizen Native API (Development) +Group: TO_BE/FILLED_IN +Requires: %{name} = %{version}-%{release} + +%description devel +WebRTC Library in Tizen Native API (DEV). + +%prep +%setup -q +cp %{SOURCE1001} . + +%build +MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` +cmake . -DCMAKE_INSTALL_PREFIX=/usr -DFULLVER=%{version} -DMAJORVER=${MAJORVER} + +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +mkdir -p %{buildroot}/usr/share/license +mkdir -p %{buildroot}/usr/bin +cp LICENSE.APLv2 %{buildroot}/usr/share/license/%{name} +cp test/webrtc_test %{buildroot}/usr/bin + +%make_install + +%post +/sbin/ldconfig + +%postun -p /sbin/ldconfig + + +%files +%manifest capi-media-webrtc.manifest +%{_libdir}/libcapi-media-webrtc.so.* +%{_datadir}/license/%{name} +/usr/bin/* + +%files devel +%{_includedir}/media/*.h +%{_libdir}/pkgconfig/*.pc +%{_libdir}/libcapi-media-webrtc.so + + diff --git a/src/webrtc.c b/src/webrtc.c new file mode 100644 index 00000000..1a624711 --- /dev/null +++ b/src/webrtc.c @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2020 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 +#include +#include + +#include "webrtc.h" +#include "webrtc_private.h" + +int webrtc_create(webrtc_h *webrtc) +{ + int ret = WEBRTC_ERROR_NONE; + webrtc_s *_webrtc = NULL; + + RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL"); + + _webrtc = g_new0(webrtc_s, 1); + + g_mutex_init(&_webrtc->mutex); + g_mutex_lock(&_webrtc->mutex); + + _ini_load(_webrtc); + _gst_init(_webrtc); + _gst_build_pipeline(_webrtc); + + _webrtc->state = WEBRTC_STATE_IDLE; + + *webrtc = _webrtc; + + g_mutex_unlock(&_webrtc->mutex); + + LOG_INFO("webrtc[%p] is created", *webrtc); + + return ret; +} + +int webrtc_destroy(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); + + /* set state to null here */ + + if (_webrtc->gst.bus) + gst_object_unref(_webrtc->gst.bus); + if (_webrtc->gst.pipeline) + gst_object_unref(_webrtc->gst.pipeline); + + g_mutex_unlock(&_webrtc->mutex); + g_mutex_clear(&_webrtc->mutex); + + g_free(_webrtc); + + LOG_INFO("webrtc[%p] is destroyed", webrtc); + + return WEBRTC_ERROR_NONE; +} + +int webrtc_start(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"); + + /* Implementation */ + + g_mutex_unlock(&_webrtc->mutex); + + LOG_INFO("webrtc[%p] is started", webrtc); + + return WEBRTC_ERROR_NONE; +} + +int webrtc_stop(webrtc_h webrtc) +{ + webrtc_s *_webrtc = (webrtc_s*)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 */ + + g_mutex_unlock(&_webrtc->mutex); + + LOG_INFO("webrtc[%p] is stopped", webrtc); + + return WEBRTC_ERROR_NONE; +} + +int webrtc_get_state(webrtc_h webrtc, webrtc_state_e *state) +{ + webrtc_s *_webrtc = (webrtc_s*)webrtc; + + RET_VAL_IF(_webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL"); + + *state = _webrtc->state; + + LOG_INFO("state[%d]", *state); + + return WEBRTC_ERROR_NONE; +} + +int webrtc_add_media_source(webrtc_h webrtc, webrtc_media_source_type_e type, int *source_id) +{ + webrtc_s *_webrtc = (webrtc_s*)webrtc; + + RET_VAL_IF(_webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL"); + RET_VAL_IF(source_id == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "source_id 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"); + + /* Implementation */ + + g_mutex_unlock(&_webrtc->mutex); + + LOG_INFO("source_id[%d]", *source_id); + + return WEBRTC_ERROR_NONE; +} + +int webrtc_remove_media_source(webrtc_h webrtc, int source_id) +{ + 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"); + + /* Implementation */ + + g_mutex_unlock(&_webrtc->mutex); + + LOG_INFO("source_id[%d]", source_id); + + return WEBRTC_ERROR_NONE; +} + +int webrtc_set_stun_server(webrtc_h webrtc, const char *stun_server) +{ + webrtc_s *_webrtc = (webrtc_s*)webrtc; + + RET_VAL_IF(_webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL"); + RET_VAL_IF(stun_server == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "stun_server 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"); + + /* Implementation */ + + g_mutex_unlock(&_webrtc->mutex); + + LOG_INFO("stun_server[%s]", stun_server); + + return WEBRTC_ERROR_NONE; +} \ No newline at end of file diff --git a/src/webrtc_private.c b/src/webrtc_private.c new file mode 100644 index 00000000..2c1dc30b --- /dev/null +++ b/src/webrtc_private.c @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2020 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" + +static gboolean __bus_watch_cb(GstBus *bus, GstMessage *message, gpointer user_data) +{ + webrtc_s *webrtc = (webrtc_s *)user_data; + GError *err = NULL; + GstState gst_state_old = GST_STATE_VOID_PENDING; + GstState gst_state_new = GST_STATE_VOID_PENDING; + GstState gst_state_pending = GST_STATE_VOID_PENDING; + gchar *state_transition_name = NULL; + + RET_VAL_IF(webrtc == NULL, FALSE, "webrtc is NULL"); + RET_VAL_IF(webrtc->gst.pipeline == NULL, FALSE, "pipeline is NULL"); + + if (message == NULL) { + LOG_DEBUG("message is null"); + return TRUE; + } + + switch (GST_MESSAGE_TYPE(message)) { + case GST_MESSAGE_ERROR: + gst_message_parse_error(message, &err, NULL); + + LOG_ERROR("Error from [%s]: %s", GST_OBJECT_NAME(GST_OBJECT_CAST(GST_ELEMENT(GST_MESSAGE_SRC(message)))), err->message); + /* FIXME : invoke error cb */ + + g_error_free(err); + break; + + case GST_MESSAGE_STATE_CHANGED: + if (GST_MESSAGE_SRC(message) != GST_OBJECT(webrtc->gst.pipeline)) + return TRUE; + + gst_message_parse_state_changed(message, &gst_state_old, &gst_state_new, &gst_state_pending); + state_transition_name = g_strdup_printf("Old[GST_STATE_%s] New[GST_STATE_%s] Pending[GST_STATE_%s]", + gst_element_state_get_name(gst_state_old), gst_element_state_get_name(gst_state_new), + gst_element_state_get_name(gst_state_pending)); + + LOG_INFO("GST_MESSAGE_STATE_CHANGED: %s", state_transition_name); + + /* FIXME : set pipeline state */ + + g_free(state_transition_name); + break; + + case GST_MESSAGE_ASYNC_DONE: + if (GST_MESSAGE_SRC(message) != GST_OBJECT(webrtc->gst.pipeline)) + return TRUE; + + LOG_INFO("GST_MESSAGE_ASYNC_DONE"); + + break; + + case GST_MESSAGE_EOS: + LOG_INFO("GST_MESSAGE_EOS end-of-stream"); + + break; + + default: + break; + } + + return TRUE; +} + +int _ini_load(webrtc_s *webrtc) +{ + RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL"); + + memset(&webrtc->ini, 0, sizeof(webrtc_ini_s)); + + /* FIXME: load from ini file */ + + webrtc->ini.generate_dot = FALSE; + + return WEBRTC_ERROR_NONE; +} + +int _gst_init(webrtc_s *webrtc) +{ + int i = 0; + int argc = 1; + char **argv = NULL; + GError *err = NULL; + gboolean gst_ret = 0; + + RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL"); + + /* get argc(number of command line option), argc is always one without option */ + if (webrtc->ini.gst_args) + argc += g_strv_length(webrtc->ini.gst_args); + + argv = (char **)calloc(argc, sizeof(char*)); + RET_VAL_IF(argv == NULL, WEBRTC_ERROR_INVALID_OPERATION, "failed to calloc()"); + + argv[0] = g_strdup("capi-media-webrtc"); + + if (webrtc->ini.gst_args) { + for ( ; webrtc->ini.gst_args[i]; ++i) { + if (argc <= i+1) { + LOG_ERROR("need to check, prevent overrun"); + break; + } + argv[i+1] = webrtc->ini.gst_args[i]; + LOG_DEBUG("add [%s] gstreamer parameter", argv[i+1]); + } + } + + gst_ret = gst_init_check(&argc, &argv, &err); + + for (i = 1; i < argc; i++) + argv[i] = NULL; + + SAFE_FREE(argv[0]); + SAFE_FREE(argv); + + if (!gst_ret) { + LOG_ERROR("failed to gst_init_check(), err[%s]", err->message); + g_clear_error(&err); + return WEBRTC_ERROR_INVALID_OPERATION; + } + + return WEBRTC_ERROR_NONE; +} + +int _gst_build_pipeline(webrtc_s *webrtc) +{ + int ret = WEBRTC_ERROR_NONE; + + RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL"); + + webrtc->gst.pipeline = gst_pipeline_new("native-webrtc-pipeline"); + RET_VAL_IF(webrtc->gst.pipeline == NULL, WEBRTC_ERROR_INVALID_OPERATION, "pipeline is NULL"); + + webrtc->gst.bus = gst_pipeline_get_bus(GST_PIPELINE(webrtc->gst.pipeline)); + RET_VAL_IF(webrtc->gst.bus == NULL, WEBRTC_ERROR_INVALID_OPERATION, "bus is NULL"); + + webrtc->gst.bus_watcher = gst_bus_add_watch(webrtc->gst.bus, (GstBusFunc) __bus_watch_cb, webrtc); + + /* FIXME : initial bin setting */ + + return ret; +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 00000000..05a5d9c4 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,24 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(fw_test "${fw_name}-test") + +INCLUDE_DIRECTORIES(../include) + +link_directories(${CMAKE_SOURCE_DIR}/../) + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_test} REQUIRED glib-2.0 appcore-efl elementary) +FOREACH(flag ${${fw_test}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -pie") + +aux_source_directory(. sources) + +FOREACH(src ${sources}) + GET_FILENAME_COMPONENT(src_name ${src} NAME_WE) + MESSAGE("${src_name}") + ADD_EXECUTABLE(${src_name} ${src}) + TARGET_LINK_LIBRARIES(${src_name} capi-media-webrtc ${${fw_test}_LDFLAGS}) +ENDFOREACH() + diff --git a/test/webrtc_test.c b/test/webrtc_test.c new file mode 100644 index 00000000..0e1d6c30 --- /dev/null +++ b/test/webrtc_test.c @@ -0,0 +1,521 @@ +/* +* Copyright (c) 2020 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. +*/ +#define USE_EVENT_HANDLER + +#include +#include +#include +#include + +#ifdef PACKAGE +#undef PACKAGE +#endif +#define PACKAGE "webrtc_test" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "WEBRTC_TEST" + +#define MAX_STRING_LEN 512 + +enum { + CURRENT_STATUS_MAINMENU, + CURRENT_STATUS_ADD_MEDIA_SOURCE, + CURRENT_STATUS_REMOVE_MEDIA_SOURCE, + CURRENT_STATUS_SET_STUN_SERVER, + CURRENT_STATUS_SETTING_SIGNALLING_SERVER, + CURRENT_STATUS_SETTING_PROXY, +}; + +/* for video display */ +static Evas_Object *g_eo; +static Evas_Object *g_win_id; +static Evas_Object *g_selected_win_id; + +typedef struct { + Evas_Object *win; + Evas_Object *layout_main; +} appdata; + +static appdata ad; +static webrtc_h g_webrtc; +static gchar g_signalling_server[MAX_STRING_LEN]; +static gchar g_stun_server[MAX_STRING_LEN]; +static gchar g_proxy[MAX_STRING_LEN]; +int g_menu_state = CURRENT_STATUS_MAINMENU; + +static void win_del(void *data, Evas_Object *obj, void *event) +{ + elm_exit(); +} + +static Evas_Object *create_win(const char *name) +{ + Evas_Object *eo = NULL; + int w = 0; + int h = 0; + + g_print("[%s][%d] name=%s\n", __func__, __LINE__, name); + + eo = elm_win_add(NULL, name, ELM_WIN_BASIC); + if (eo) { + elm_win_title_set(eo, name); + elm_win_borderless_set(eo, EINA_TRUE); + evas_object_smart_callback_add(eo, "delete,request", win_del, NULL); + elm_win_screen_size_get(eo, NULL, NULL, &w, &h); + g_print("window size :%d,%d", w, h); + evas_object_resize(eo, w, h); + elm_win_autodel_set(eo, EINA_TRUE); + elm_win_alpha_set(eo, EINA_TRUE); + } + return eo; +} + +static Evas_Object *create_image_object(Evas_Object *eo_parent) +{ + if (!eo_parent) + return NULL; + + Evas *evas = evas_object_evas_get(eo_parent); + Evas_Object *eo = NULL; + + eo = evas_object_image_add(evas); + + return eo; +} + +void create_render_rect_and_bg(Evas_Object *win) +{ + if (!win) { + g_print("no win"); + return; + } + Evas_Object *bg, *rect; + + bg = elm_bg_add(win); + elm_win_resize_object_add(win, bg); + evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + evas_object_show(bg); + + rect = evas_object_rectangle_add(evas_object_evas_get(win)); + if (!rect) { + g_print("no rect"); + return; + } + evas_object_color_set(rect, 0, 0, 0, 0); + evas_object_render_op_set(rect, EVAS_RENDER_COPY); + + elm_win_resize_object_add(win, rect); + evas_object_size_hint_weight_set(rect, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + evas_object_show(rect); + evas_object_show(win); +} + +static int app_create(void *data) +{ + appdata *ad = data; + Evas_Object *win = NULL; + + /* use gl backend */ + elm_config_accel_preference_set("opengl"); + + /* create window */ + win = create_win(PACKAGE); + if (win == NULL) + return -1; + ad->win = win; + g_win_id = win; + g_selected_win_id = g_win_id; + create_render_rect_and_bg(ad->win); + /* Create evas image object for EVAS surface */ + g_eo = create_image_object(ad->win); + evas_object_image_size_set(g_eo, 500, 500); + evas_object_image_fill_set(g_eo, 0, 0, 500, 500); + evas_object_resize(g_eo, 500, 500); + + elm_win_activate(win); + evas_object_show(win); + + return 0; +} + +static int app_terminate(void *data) +{ + appdata *ad = data; + + if (g_win_id) { + evas_object_del(g_win_id); + g_win_id = NULL; + } + + ad->win = NULL; + g_selected_win_id = NULL; + + return 0; +} + +struct appcore_ops ops = { + .create = app_create, + .terminate = app_terminate, +}; + +static void _webrtc_create() +{ + int ret = WEBRTC_ERROR_NONE; + + if (g_webrtc) { + g_print("failed to _webrtc_create(), already created\n"); + return; + } + + ret = webrtc_create(&g_webrtc); + if (ret != WEBRTC_ERROR_NONE) + g_print("webrtc_create() returned [0x%x]\n", ret); + else + g_print("webrtc_create() success, g_webrtc[%p]\n", g_webrtc); +} + +static void _webrtc_destroy() +{ + int ret = WEBRTC_ERROR_NONE; + + if (!g_webrtc) { + g_print("failed to _webrtc_destroy(), g_webrtc is NULL\n"); + return; + } + + ret = webrtc_destroy(g_webrtc); + if (ret != WEBRTC_ERROR_NONE) { + g_print("webrtc_destroy() returned [0x%x]\n", ret); + } else { + g_print("webrtc_destroy() success\n"); + g_webrtc = NULL; + } +} + +static void _webrtc_start() +{ + int ret = WEBRTC_ERROR_NONE; + + if (!g_webrtc) { + g_print("failed to _webrtc_start(), g_webrtc is NULL\n"); + return; + } + + ret = webrtc_start(g_webrtc); + if (ret != WEBRTC_ERROR_NONE) + g_print("webrtc_start() returned [0x%x]\n", ret); + else + g_print("webrtc_start() success\n"); +} + +static void _webrtc_stop() +{ + int ret = WEBRTC_ERROR_NONE; + + if (!g_webrtc) { + g_print("failed to _webrtc_stop(), g_webrtc is NULL\n"); + return; + } + + ret = webrtc_stop(g_webrtc); + if (ret != WEBRTC_ERROR_NONE) + g_print("webrtc_stop() returned [0x%x]\n", ret); + else + g_print("webrtc_stop() success\n"); +} + +static void _webrtc_get_state() +{ + int ret = WEBRTC_ERROR_NONE; + webrtc_state_e state; + + ret = webrtc_get_state(g_webrtc, &state); + if (ret != WEBRTC_ERROR_NONE) + g_print("webrtc_get_state() returned [0x%x]\n", ret); + else + g_print("state: [%d]\n", state); +} + +static void _webrtc_add_media_source(int type) +{ + int ret = WEBRTC_ERROR_NONE; + int source_id = 0; + + ret = webrtc_add_media_source(g_webrtc, (webrtc_media_source_type_e)type, &source_id); + if (ret != WEBRTC_ERROR_NONE) + g_print("failed to webrtc_add_media_source(), ret[0x%x]\n", ret); + else + g_print("webrtc_add_media_source() success, source_id[%d]\n", source_id); +} + +static void _webrtc_remove_media_source(int source_id) +{ + int ret = WEBRTC_ERROR_NONE; + + ret = webrtc_remove_media_source(g_webrtc, source_id); + if (ret != WEBRTC_ERROR_NONE) + g_print("failed to webrtc_remove_media_source(), source_id[%d], ret[0x%x]\n", source_id, ret); + else + g_print("webrtc_remove_media_source() success, source_id[%d]\n", source_id); +} + +static int __copy_string_arr(gchar *dest_arr, char *string) +{ + int len = 0; + gsize src_size = 0; + + if (!string) + return -1; + + len = strlen(string); + + src_size = g_strlcpy(dest_arr, string, MAX_STRING_LEN); + if (src_size != len) { + g_print("failed to g_strlcpy()\n"); + return -1; + } + + return 0; +} + +static void _webrtc_set_stun_server(char *uri) +{ + int ret = 0; + + if (!uri) + return; + + ret = __copy_string_arr(g_stun_server, uri); + if (ret != 0) + return; + + ret = webrtc_set_stun_server(g_webrtc, g_stun_server); + if (ret != WEBRTC_ERROR_NONE) + g_print("failed to webrtc_set_stun_server(), uri[%s]\n", g_stun_server); + else + g_print("webrtc_set_stun_server() success, uri[%s]\n", g_stun_server); +} + +static void _setting_signalling_server(char *uri) +{ + int ret = 0; + + if (!uri) + return; + + ret = __copy_string_arr(g_signalling_server, uri); + if (ret != 0) + return; +} + +static void _setting_proxy(char *uri) +{ + int ret = 0; + + if (!uri) + return; + + ret = __copy_string_arr(g_proxy, uri); + if (ret != 0) + return; +} + +void quit_program() +{ + if (g_webrtc != NULL) { + _webrtc_stop(); + _webrtc_destroy(); + } + + elm_exit(); +} + +void _interpret_main_menu(char *cmd) +{ + int len = strlen(cmd); + if (len == 1) { + if (strncmp(cmd, "c", 1) == 0) { + _webrtc_create(); + + } else if (strncmp(cmd, "a", 1) == 0) { + g_menu_state = CURRENT_STATUS_ADD_MEDIA_SOURCE; + + } else if (strncmp(cmd, "r", 1) == 0) { + g_menu_state = CURRENT_STATUS_REMOVE_MEDIA_SOURCE; + + } else if (strncmp(cmd, "s", 1) == 0) { + _webrtc_start(); + + } else if (strncmp(cmd, "t", 1) == 0) { + _webrtc_stop(); + + } else if (strncmp(cmd, "d", 1) == 0) { + _webrtc_destroy(); + + } else if (strncmp(cmd, "g", 1) == 0) { + _webrtc_get_state(); + + } else { + g_print("unknown menu \n"); + } + + } else if (len == 2) { + if (strncmp(cmd, "st", 2) == 0) { + g_menu_state = CURRENT_STATUS_SET_STUN_SERVER; + + } else if (strncmp(cmd, "ss", 2) == 0) { + g_menu_state = CURRENT_STATUS_SETTING_SIGNALLING_SERVER; + + } else if (strncmp(cmd, "px", 2) == 0) { + g_menu_state = CURRENT_STATUS_SETTING_PROXY; + + } else { + g_print("unknown menu \n"); + } + + } else { + g_print("unknown menu \n"); + } +} + +void display_sub_basic() +{ + g_print("\n"); + g_print("=========================================================================================\n"); + g_print(" Native WebRTC Test (press q to quit) \n"); + g_print("-----------------------------------------------------------------------------------------\n"); + g_print("c. Create\t"); + g_print("d. Destroy\n"); + g_print("s. Start\t"); + g_print("t. Stop\n"); + g_print("a. Add media source\t"); + g_print("r. Remove media source\n"); + g_print("st. Set STUN server\n"); + g_print("g. Get state\n"); + g_print("----------------------------------- App. Setting ----------------------------------------\n"); + g_print("ss. Signalling server\n"); + g_print("px. Proxy\n"); + g_print("-----------------------------------------------------------------------------------------\n"); + g_print("=========================================================================================\n"); +} + +static void displaymenu() +{ + if (g_menu_state == CURRENT_STATUS_MAINMENU) { + display_sub_basic(); + } else if (g_menu_state == CURRENT_STATUS_ADD_MEDIA_SOURCE) { + g_print("*** input media source type.(1:camera, 2:mic, 3:audiotest, 4:videotest)\n"); + } else if (g_menu_state == CURRENT_STATUS_REMOVE_MEDIA_SOURCE) { + g_print("*** input media source id to remove.\n"); + } else if (g_menu_state == CURRENT_STATUS_SET_STUN_SERVER) { + g_print("*** input STUN server address.\n"); + } else if (g_menu_state == CURRENT_STATUS_SETTING_SIGNALLING_SERVER) { + g_print("*** input signalling server url.\n"); + } else if (g_menu_state == CURRENT_STATUS_SETTING_PROXY) { + g_print("*** input proxy address.\n"); + } else { + g_print("*** unknown status.\n"); + quit_program(); + } + g_print(" >>> "); +} + +gboolean timeout_menu_display(void *data) +{ + displaymenu(); + return FALSE; +} + +gboolean timeout_quit_program(void *data) +{ + quit_program(); + return FALSE; +} + +void reset_menu_state(void) +{ + g_menu_state = CURRENT_STATUS_MAINMENU; +} + +static void interpret(char *cmd) +{ + int value; + + switch (g_menu_state) { + case CURRENT_STATUS_MAINMENU: + _interpret_main_menu(cmd); + break; + case CURRENT_STATUS_ADD_MEDIA_SOURCE: { + value = atoi(cmd); + _webrtc_add_media_source(value); + reset_menu_state(); + break; + } + case CURRENT_STATUS_REMOVE_MEDIA_SOURCE: { + value = atoi(cmd); + _webrtc_remove_media_source(value); + reset_menu_state(); + break; + } + case CURRENT_STATUS_SET_STUN_SERVER: { + _webrtc_set_stun_server(cmd); + reset_menu_state(); + break; + } + case CURRENT_STATUS_SETTING_SIGNALLING_SERVER: { + _setting_signalling_server(cmd); + reset_menu_state(); + break; + } + case CURRENT_STATUS_SETTING_PROXY: { + _setting_proxy(cmd); + reset_menu_state(); + break; + } + } + + g_timeout_add(100, timeout_menu_display, 0); +} + +gboolean input(GIOChannel *channel) +{ + gchar buf[MAX_STRING_LEN]; + gsize read; + GError *error = NULL; + + g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error); + buf[read] = '\0'; + g_strstrip(buf); + interpret(buf); + + return TRUE; +} + +int main(int argc, char *argv[]) +{ + GIOChannel *stdin_channel; + stdin_channel = g_io_channel_unix_new(0); + g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL); + g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc)input, NULL); + + displaymenu(); + memset(&ad, 0x0, sizeof(appdata)); + ops.data = &ad; + + return appcore_efl_main(PACKAGE, &argc, &argv, &ops); +}