diff --git a/runtime/core/bin/CMakeLists.txt b/runtime/core/bin/CMakeLists.txt index e11655f..27f98cc 100644 --- a/runtime/core/bin/CMakeLists.txt +++ b/runtime/core/bin/CMakeLists.txt @@ -3,5 +3,5 @@ target_link_libraries(tts_main PUBLIC gflags tts_model) if(BUILD_SERVER) add_executable(http_server_main http_server_main.cc) - target_link_libraries(http_server_main PUBLIC gflags http_server tts_model) + target_link_libraries(http_server_main PUBLIC gflags http_server tts_model jsoncpp_lib) endif() diff --git a/runtime/core/cmake/jsoncpp.cmake b/runtime/core/cmake/jsoncpp.cmake new file mode 100644 index 0000000..151d0f4 --- /dev/null +++ b/runtime/core/cmake/jsoncpp.cmake @@ -0,0 +1,6 @@ +FetchContent_Declare(jsoncpp + URL https://github.com/open-source-parsers/jsoncpp/archive/refs/tags/1.9.3.zip + URL_HASH SHA256=7853fe085ddd5da94b9795f4b520689c21f2753c4a8f7a5097410ee6136bf671 +) +FetchContent_MakeAvailable(jsoncpp) +include_directories(${jsoncpp_SOURCE_DIR}/include) diff --git a/runtime/core/http/http_server.cc b/runtime/core/http/http_server.cc index 696745b..f08ddc3 100644 --- a/runtime/core/http/http_server.cc +++ b/runtime/core/http/http_server.cc @@ -1,5 +1,5 @@ // Copyright (c) 2022 Zhendong Peng (pzd17@tsinghua.org.cn) -// +// 2024 Shengqiang Li (shengqiang.li96@gmail.com) // 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 @@ -17,11 +17,13 @@ #include #include "boost/beast/core.hpp" +#include "boost/beast/core/detail/base64.hpp" #include "boost/beast/version.hpp" #include "boost/url/src.hpp" #include "boost/uuid/uuid.hpp" #include "boost/uuid/uuid_generators.hpp" #include "boost/uuid/uuid_io.hpp" +#include "json/json.h" #include "frontend/wav.h" #include "utils/string.h" @@ -31,18 +33,17 @@ namespace wetts { namespace urls = boost::urls; namespace uuids = boost::uuids; +namespace base64 = boost::beast::detail::base64; http::message_generator ConnectionHandler::HandleRequest( - char* wav_data, int data_size) { + const std::string& json_data) { beast::error_code ec; - http::response res; + http::response res; res.result(http::status::ok); res.set(http::field::server, BOOST_BEAST_VERSION_STRING); - res.set(http::field::content_type, "audio/wav"); + res.set(http::field::content_type, "application/json"); res.keep_alive(request_.keep_alive()); - res.body().data = wav_data; - res.body().size = data_size; - res.body().more = false; + res.body() = json_data; res.prepare_payload(); return res; } @@ -104,9 +105,17 @@ void ConnectionHandler::operator()() { reinterpret_cast(&header) + 44); wav_data.insert(wav_data.end(), reinterpret_cast(audio.data()), reinterpret_cast(audio.data()) + audio_size); + // 4. Base64 encode + Json::Value response; + std::size_t encode_size = base64::encoded_size(data_size); + std::vector base64_wav(encode_size); + std::size_t encoded_size = + base64::encode(base64_wav.data(), wav_data.data(), data_size); + std::string encoded_wav(base64_wav.begin(), base64_wav.end()); + response["audio"] = encoded_wav; + std::string json_data = response.toStyledString(); // Handle request - http::message_generator msg = - HandleRequest(wav_data.data(), data_size); + http::message_generator msg = HandleRequest(json_data); // Determine if we should close the connection bool keep_alive = msg.keep_alive(); // Send the response diff --git a/runtime/core/http/http_server.h b/runtime/core/http/http_server.h index 30105eb..0db359d 100644 --- a/runtime/core/http/http_server.h +++ b/runtime/core/http/http_server.h @@ -38,7 +38,7 @@ class ConnectionHandler { ConnectionHandler(tcp::socket&& socket, std::shared_ptr tts_model) : socket_(std::move(socket)), tts_model_(std::move(tts_model)) {} void operator()(); - http::message_generator HandleRequest(char* wav_data, int data_size); + http::message_generator HandleRequest(const std::string& json_data); private: tcp::socket socket_; diff --git a/runtime/onnxruntime/CMakeLists.txt b/runtime/onnxruntime/CMakeLists.txt index c0acab6..de4cb22 100644 --- a/runtime/onnxruntime/CMakeLists.txt +++ b/runtime/onnxruntime/CMakeLists.txt @@ -28,6 +28,7 @@ add_subdirectory(frontend) if(BUILD_SERVER) include(boost) + include(jsoncpp) add_subdirectory(http) endif()