From 2e602801b372b0b68111316b89f567213e3ea378 Mon Sep 17 00:00:00 2001 From: Kazumasa Mitsunari Date: Tue, 29 Aug 2017 20:58:47 +0900 Subject: Add sound manager initial source code First version 2017/08/29 SoundManager consists of main 4 parts 1. doc Document of sound manager and audio manager architecture This describes application sequence Please open ApplicationGuide.md with browser 2. include Contains API of libsoundmanager LibSoundmanager API is going to be modified, so please don't use for now Update will be done in near future 3. soundmanager_binding Contains soundmanager binding. This is the service midleware If installing this, soundmanager is automatically launched by AppFW(systemd) 4. sample Contains sample applications. These applications follow the AudioManager sequence and using soundmanager binding Change-Id: I75b6c5c652e1dc00b39bc499a2ee6bf1e2aeaf6d Signed-off-by: Kazumasa Mitsunari --- libsoundmanager/CMakeLists.txt | 57 ++++ libsoundmanager/libsoundmanager.cpp | 447 +++++++++++++++++++++++++ libsoundmanager/test.cpp | 165 +++++++++ libsoundmanager/test/CMakeLists.txt | 30 ++ libsoundmanager/test/gtest_libsoundmanager.cpp | 49 +++ 5 files changed, 748 insertions(+) create mode 100644 libsoundmanager/CMakeLists.txt create mode 100644 libsoundmanager/libsoundmanager.cpp create mode 100644 libsoundmanager/test.cpp create mode 100644 libsoundmanager/test/CMakeLists.txt create mode 100644 libsoundmanager/test/gtest_libsoundmanager.cpp (limited to 'libsoundmanager') diff --git a/libsoundmanager/CMakeLists.txt b/libsoundmanager/CMakeLists.txt new file mode 100644 index 0000000..acf98de --- /dev/null +++ b/libsoundmanager/CMakeLists.txt @@ -0,0 +1,57 @@ +# +# Copyright (c) 2017 TOYOTA MOTOR CORPORATION +# +# 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. +# + + +cmake_minimum_required(VERSION 3.0) + +set(TARGET_LIBSM soundmanager) +add_definitions("-std=c++11") + +pkg_check_modules(libsm_depends json-c libafbwsc libsystemd audiomanager) +set(libsm_sources libsoundmanager.cpp) + +include_directories(${LIBSM_INCLUDE_DIR}) +link_libraries(-Wl,--as-needed -Wl,--gc-sections -Wl,--no-undefined) + +add_library(${TARGET_LIBSM} SHARED ${libsm_sources}) +target_compile_options(${TARGET_LIBSM} PUBLIC ${libsm_depends_CFLAGS}) + +if(DEFINED DEBUGMODE) + target_compile_options(${TARGET_LIBSM} PRIVATE -g -O0) +else(DEFINED DEBUGMODE) + target_compile_options(${TARGET_LIBSM} PRIVATE -g -O2) +endif(DEFINED DEBUGMODE) + +include_directories(${TARGET_LIBSM} ${libsm_depends_INCLUDE_DIRS}) +target_link_libraries(${TARGET_LIBSM} afbwsc -lpthread ${link_libraries} ${libsm_depends_LIBRARIES}) + +INSTALL(TARGETS ${TARGET_LIBSM} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) + +##################### +pkg_check_modules(test_depends glib-2.0 gio-2.0 gio-unix-2.0) +add_executable(libsoundmanager-demo test.cpp) +include_directories(libsoundmanager-demo ${test_depends_INCLUDE_DIRS}) +TARGET_LINK_LIBRARIES(libsoundmanager-demo + soundmanager + ${link_libraries} + -lpthread + ${test_depends_LIBRARIES} + ${libsm_depends_LIBRARIES} +) +##################### +add_subdirectory(test) + diff --git a/libsoundmanager/libsoundmanager.cpp b/libsoundmanager/libsoundmanager.cpp new file mode 100644 index 0000000..9f77723 --- /dev/null +++ b/libsoundmanager/libsoundmanager.cpp @@ -0,0 +1,447 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * 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 +#include +#include +#include + +#define ELOG(args,...) _ELOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__) +#define DLOG(args,...) _DLOG(__FUNCTION__,__LINE__,args,##__VA_ARGS__) + +using namespace std; + +static void _DLOG(const char* func, const int line, const char* log, ...); +static void _ELOG(const char* func, const int line, const char* log, ...); +static bool has_verb(const string& verb); +static const char API[] = "soundmanager"; + +static void _on_hangup_static(void *closure, struct afb_wsj1 *wsj) +{ + static_cast(closure)->on_hangup(NULL,wsj); +} + +static void _on_call_static(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg) +{ + /* LibSoundmanager is not called from other process */ +} + +static void _on_event_static(void* closure, const char* event, struct afb_wsj1_msg *msg) +{ + static_cast(closure)->on_event(NULL,event,msg); +} + +static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg) +{ + static_cast(closure)->on_reply(NULL,msg); +} + + +/** + * This function is constructor + * + * #### Parameters + * - port [in] : This argument should be specified to the port number to be used for websocket + * - token [in] : This argument should be specified to the token to be used for websocket + * + * #### Rreturn + * Nothing + * + * #### Note + * Use this constructor + * + */ +LibSoundmanager::LibSoundmanager(const int port, const string& token) +{ + int ret; + if(port > 0 && token.size() > 0) + { + mport = port; + mtoken = token; + } + else + { + ELOG("port and token should be > 0, Initial port and token uses."); + } + + ret = initialize_websocket(); + if(ret != 0 ) + { + ELOG("Failed to initialize websocket"); + } + else{ + DLOG("Initialized"); + } +} + +LibSoundmanager::~LibSoundmanager() +{ + if(mploop) + { + sd_event_unref(mploop); + } + if(sp_websock != NULL) + { + free(sp_websock); + } +} + +/** + * This function register callback function for reply/event message from sound manager + * + * #### Parameters + * - event_cb [in] : This argument should be specified to the callback for subscribed event + * - reply_cb [in] : This argument should be specified to the reply callback for call function + * + * #### Rreturn + * - Returns 0 on success or -1 in case of error. + * + * #### Note + * Event callback is invoked by sound manager for event you subscribed. + * If you would like to get event, please call subscribe function before/after this function + */ +void LibSoundmanager::register_callback( + void (*event_cb)(const std::string& event, struct json_object* event_contents), + void (*reply_cb)(struct json_object* reply_contents), + void (*hangup_cb)(void)) +{ + onEvent = event_cb; + onReply = reply_cb; + onHangup = hangup_cb; +} + +int LibSoundmanager::initialize_websocket() +{ + mploop = NULL; + onEvent = nullptr; + onReply = nullptr; + int ret = sd_event_default(&mploop); + if(ret < 0) + { + ELOG("Failed to create event loop"); + goto END; + } + /* Initialize interface from websocket */ + + minterface.on_hangup = _on_hangup_static; + minterface.on_call = _on_call_static; /* Is this necessary? */ + minterface.on_event = _on_event_static; + muri += "ws://localhost:" + to_string(mport) + "/api?token=" + mtoken; /*To be modified*/ + sp_websock = afb_ws_client_connect_wsj1(mploop, muri.c_str(), &minterface, this); + if(sp_websock == NULL) + { + ELOG("Failed to create websocket connection"); + goto END; + } + + /* creates the evsrc */ + //ret = sd_event_add_io(mploop,&mevent_src, sp_websock->fd, EPOLLIN, event_callback, NULL); + + return 0; +END: + if(mploop) + { + sd_event_unref(mploop); + } + return -1; +} + +static void *event_loop_run(void *args) +{ + struct sd_event* loop = (struct sd_event*)(args); + DLOG("start eventloop"); + for(;;) + sd_event_run(loop, 30000000); +} + +/** + * This function start receiving the reply/event message from sound manager + * + * #### Parameters + * Nothing + * #### Rreturn + * - Returns thread_id on success or -1 in case of error. + * + * #### Note + * + */ +int LibSoundmanager::run_eventloop() +{ + if(mploop && sp_websock) + { + pthread_t thread_id; + int ret = pthread_create(&thread_id, NULL, event_loop_run, mploop); + if(ret != 0) + { + ELOG("Cannot run eventloop due to error:%d", errno); + return -1; + } + else + return thread_id; + } + else + { + ELOG("Connecting is not established yet"); + return -1; + } +} + +/** + * This function calls the API of Audio Manager via WebSocket + * + * #### Parameters + * - verb [in] : This argument should be specified to the API name (e.g. "connect") + * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object + * + * #### Rreturn + * - Returns 0 on success or -1 in case of error. + * + * #### Note + * To call Audio Manager's APIs, the application should set its function name, arguments to JSON format. + * + */ +int LibSoundmanager::call(const string& verb, struct json_object* arg) +{ + int ret; + if(!sp_websock) + { + return -1; + } + if (!has_verb(verb)) + { + ELOG("verb doesn't exit"); + return -1; + } + ret = afb_wsj1_call_j(sp_websock, API, verb.c_str(), arg, _on_reply_static, this); + if (ret < 0) { + ELOG("Failed to call verb:%s",verb.c_str()); + } + return ret; +} + +/** + * This function calls the API of Audio Manager via WebSocket + * This function is overload function of "call" + * + * #### Parameters + * - verb [in] : This argument should be specified to the API name (e.g. "connect") + * - arg [in] : This argument should be specified to the argument of API. And this argument expects JSON object + * + * #### Rreturn + * - Returns 0 on success or -1 in case of error. + * + * #### Note + * To call Audio Manager's APIs, the application should set its function name, arguments to JSON format. + * + */ +int LibSoundmanager::call(const char* verb, struct json_object* arg) +{ + int ret; + if(!sp_websock) + { + return -1; + } + if (!has_verb(string(verb))) + { + ELOG("verb doesn't exit"); + return -1; + } + ret = afb_wsj1_call_j(sp_websock, API, verb, arg, _on_reply_static, this); + if (ret < 0) { + ELOG("Failed to call verb:%s",verb); + } + return ret; +} + +/** + * Register callback function for each event + * + * #### Parameters + * - event_name [in] : This argument should be specified to the event name + * + * #### Rreturn + * - Returns 0 on success or -1 in case of error. + * + * #### Note + * This function enables to get an event to your callback function. + * Regarding the list of event name, please refer to CommandSender API and RountingSender API. + * + */ +int LibSoundmanager::subscribe(const string& event_name) +{ + if(!sp_websock) + { + return -1; + } + struct json_object* j_obj = json_object_new_object(); + json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str())); + + int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", j_obj, _on_reply_static, this); + if (ret < 0) { + ELOG("Failed to call verb:%s",__FUNCTION__); + } + return ret; +} + +/** + * Unregister callback function for each event + * + * #### Parameters + * - event_name [in] : This argument should be specified to the event name + * + * #### Rreturn + * - Returns 0 on success or -1 in case of error. + * + * #### Note + * This function disables to get an event to your callback function. + * + */ +int LibSoundmanager::unsubscribe(const string& event_name) +{ + if(!sp_websock) + { + return -1; + } + struct json_object* j_obj = json_object_new_object(); + json_object_object_add(j_obj, "event", json_object_new_string(event_name.c_str())); + + int ret = afb_wsj1_call_j(sp_websock, API, "unsubscribe", j_obj, _on_reply_static, this); + if (ret < 0) { + ELOG("Failed to call verb:%s",__FUNCTION__); + } + return ret; +} + +am_Error_e LibSoundmanager::connect(const am_sourceID_t sourceID, const am_sinkID_t sinkID, am_mainConnectionID_t& mainConnectionID) +{ + /*int ret; + char *key; + rc = asprintf(&key, "%d:%s/%s", ++num, api, "connect"); + ret = afb_wsj1_call_s(wsj1, api, verb, object, on_reply, key); + if(ret < 0) + { + fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object); + + }*/ + /* open the json scripts */ + // get mainconnedction ID */ + //mainConnectionID = xx; + return E_OK; +} + +am_Error_e LibSoundmanager::disconnect(const am_mainConnectionID_t mainConnectionID) +{ + return E_OK; +} + +/*const struct afb_wsj1* LibSoundmanager::get_websocket_handler() +{ + if(sp_websock) + { + return sp_websock; + } + return nullptr; +} + +const struct sd_event* LibSoundmanager::get_sd_event() +{ + if(mploop) + { + return mploop; + } + return nullptr; +}*/ + +/************* Callback Function *************/ + +void LibSoundmanager::on_hangup(void *closure, struct afb_wsj1 *wsj) +{ + DLOG("%s called", __FUNCTION__); + if(onHangup != nullptr) + { + onHangup(); + } +} + +void LibSoundmanager::on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg) +{ +} + +/* +* event is like "soundmanager/newMainConnection" +* msg is like {"event":"soundmanager\/newMainConnection","data":{"mainConnectionID":3,"sourceID":101,"sinkID":100,"delay":0,"connectionState":4},"jtype":"afb-event"})} +* ^key^ ^^^^^^^^^^^^ value ^^^^^^^^^^^^ +* so you can get + event name : struct json_object obj = json_object_object_get(msg,"event") +*/ +void LibSoundmanager::on_event(void *closure, const char *event, struct afb_wsj1_msg *msg) +{ + cout << "ON-EVENT:" << event << "(" << afb_wsj1_msg_object_s(msg) << ")" << endl; + if(onEvent != nullptr) + { + const string ev(event); + struct json_object* ev_contents = afb_wsj1_msg_object_j(msg); + onEvent(ev, ev_contents); + } +} + +void LibSoundmanager::on_reply(void *closure, struct afb_wsj1_msg *msg) +{ + cout << "ON-REPLY:" << "(" << afb_wsj1_msg_object_s(msg) << ")" << endl; + if(onReply != nullptr) + { + struct json_object* reply = afb_wsj1_msg_object_j(msg); + onReply(reply); + } +} + +/* Internal Function in libsoundmanager */ + +static void _ELOG(const char* func, const int line, const char* log, ...) +{ + char *message; + va_list args; + va_start(args, log); + if (log == NULL || vasprintf(&message, log, args) < 0) + message = NULL; + cout << "[ERROR]" << func << "(" << line << "):" << message << endl; + va_end(args); + free(message); +} + +static void _DLOG(const char* func, const int line, const char* log, ...) +{ + char *message; + va_list args; + va_start(args, log); + if (log == NULL || vasprintf(&message, log, args) < 0) + message = NULL; + cout << "[DEBUG]" << func << "(" << line << "):" << message << endl; + va_end(args); + free(message); +} + +static bool has_verb(const string& verb) +{ + DLOG("verb is %s", verb.c_str()); + if(find(api_list.begin(), api_list.end(), verb) != api_list.end()) + return true; + else + return false; +} diff --git a/libsoundmanager/test.cpp b/libsoundmanager/test.cpp new file mode 100644 index 0000000..aa3f51e --- /dev/null +++ b/libsoundmanager/test.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +static vector split(const string& str, char sep); +LibSoundmanager* sm; + +static void usage() +{ + cout << "verb "<< "key:arg" << endl; + cout << "example:" << endl; + cout << "connect sourceID 100 sinkID 100" << endl; + cout << "------- -------- --- " << endl; + cout << " verb key value" << endl; + cout << "verb list:" << endl; + for(auto itr = api_list.begin(); itr != api_list.end(); ++itr) + { + cout << " " << *itr << endl; + } + // Todo output api list + exit(0); +} + +static void call_test() +{ + string command; + + cout << "input verb and argments" << endl; + + /* read the buffer */ + for(;;){ + char line[1023]; + cin.getline(line, sizeof(line)); + command = line; + if(command.empty()){ + continue; + } + + vector v_command = split(command, ' '); + /*for(auto itr = v_command.begin(); itr != v_command.end(); ++itr) + { + cout << *itr < in argument part" << endl; + continue; + } + /* create json object */ + struct json_object* j_obj = json_object_new_object(); + for(int i = 1;i < (v_command.size()) ;++i){ + struct json_object* val = json_object_new_string(v_command[i+1].c_str()); + json_object_object_add(j_obj, v_command[i].c_str(), val); + ++i; + } + /* call verb via libsoundmanager */ + sm->call(v_command[0], j_obj); + /* free vector */ + vector().swap(v_command); + string().swap(command); + } +} + +static void onRep(struct json_object* reply_contents) +{ + const char* str = json_object_to_json_string(reply_contents); + cout << "[CB onRep]: " << str << endl; + json_object_put(reply_contents); +} + +static void onEv(const string& event, struct json_object* event_contents) +{ + const char* str = json_object_to_json_string(event_contents); + cout << "[CB onEvent]: event" << event.c_str() << " contents:" << str << endl; + json_object_put(event_contents); +} + +static vector split(const string& str, char sep) +{ + vector v; + stringstream ss(str); + string buffer; + while( getline(ss, buffer, sep) ) { + if(!buffer.empty()) + v.push_back(buffer); + } + return v; +} + +int main(int argc, char **argv) +{ + int ret; + if(argc == 1) + { + printf("Please input port num in first argument, and token in second argument"); + usage(); + return 0; + } + if(argc == 2) + { + string av(argv[1]); + if( (av == "-h") || (av == "--help")) + { + usage(); + return 0; + } + } + + string port_string(argv[1]); + string token(argv[2]); + char* endptr; + long port = strtol(port_string.c_str(),&endptr,10); + + /* error check of range */ + if( (port > 20000) || (port < 0) ) + { + printf("input under 20000(temporary number)"); + return 0; + } + if(*endptr != '\0') + { + printf("not number"); + return 0; + } + + cout << "Call test for libsoundmanager" << endl; + cout << "Call example: registerSource appname radio" << endl; + sm = new LibSoundmanager(port, token); + sm->register_callback(&onEv, &onRep); + + if (ret < 0) { + printf("failed to create event loop"); + return -1; + } + sm->run_eventloop(); + call_test(); + + return 0; +} \ No newline at end of file diff --git a/libsoundmanager/test/CMakeLists.txt b/libsoundmanager/test/CMakeLists.txt new file mode 100644 index 0000000..818ebeb --- /dev/null +++ b/libsoundmanager/test/CMakeLists.txt @@ -0,0 +1,30 @@ + + +SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/modules) +FIND_PACKAGE(gtest QUIET) + +#FIND_PACKAGE(gtest REQUIRED) +if(NOT gtest_FOUND) + MESSAGE(STATUS "gtest not found, disabling unit tests (BUILD_SM_API_TESTS=OFF)") + SET(BUILD_SM_API_TESTS FALSE) +else(NOT gtest_FOUND) + MESSAGE(STATUS "gtest found, enabling unit tests (BUILD_SM_API_TESTS=ON)") + SET(BUILD_SM_API_TESTS TRUE) +endif() + +IF(BUILD_SM_API_TESTS) +set(gtest_souces gtest_libsoundmanager.cpp) +include_directories(gtest_libsoundmanager PRIVEATE ${gtest_INCLUDE_DIRS}) +add_executable(gtest_libsoundmanager ${gtest_souces}) + +#add_dependencies(gtest_libsoundmanager gtest) +target_link_libraries(gtest_libsoundmanager + soundmanager + gtest_main.a + ${link_libraries} + ${libsm_depends_LIBRARIES} + ${gtest_LIBRARIES} + ) +add_test(gtest_libsoundmanager gtest_libsoundmanager) +ENDIF() +################################### diff --git a/libsoundmanager/test/gtest_libsoundmanager.cpp b/libsoundmanager/test/gtest_libsoundmanager.cpp new file mode 100644 index 0000000..44663ff --- /dev/null +++ b/libsoundmanager/test/gtest_libsoundmanager.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017 TOYOTA MOTOR CORPORATION + * + * 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 +using namespace std; +namespace { + +class LibSoundmanagerTest : public ::testing::Test{ +public: + void SetUp() + { + /* Launch sound manager binding */ + + } + void TearDown() + { + + } +}; + +TEST_F(LibSoundmanagerTest, call) +{ + LibSoundmanager tester = LibSoundmanager(12345,"123456"); + + struct json_object *verb = json_object_new_object(); + EXPECT_EQ(0, tester.call(string("getListMainConnections"), NULL)); + json_object_object_add(verb, "sourceID", json_object_new_int(1000)); + json_object_object_add(verb, "sinkID", json_object_new_int(1000)); + string test2("connect"); + EXPECT_EQ(0, tester.call(test2, verb)); +} + +} // namespace -- cgit 1.2.3-korg