Skip to content

Commit

Permalink
[http] Support base64 encode
Browse files Browse the repository at this point in the history
  • Loading branch information
ShengqiangLi authored and ShengqiangLi committed Jan 19, 2024
1 parent 3de1f26 commit f699f80
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion runtime/core/bin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 6 additions & 0 deletions runtime/core/cmake/jsoncpp.cmake
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 18 additions & 9 deletions runtime/core/http/http_server.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2022 Zhendong Peng ([email protected])
//
// 2024 Shengqiang Li ([email protected])
// 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
Expand All @@ -17,11 +17,13 @@
#include <vector>

#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"
Expand All @@ -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<http::buffer_body> res;
http::response<http::string_body> 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;
}
Expand Down Expand Up @@ -104,9 +105,17 @@ void ConnectionHandler::operator()() {
reinterpret_cast<char*>(&header) + 44);
wav_data.insert(wav_data.end(), reinterpret_cast<char*>(audio.data()),
reinterpret_cast<char*>(audio.data()) + audio_size);
// 4. Base64 encode
Json::Value response;
std::size_t encode_size = base64::encoded_size(data_size);
std::vector<char> 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
Expand Down
2 changes: 1 addition & 1 deletion runtime/core/http/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ConnectionHandler {
ConnectionHandler(tcp::socket&& socket, std::shared_ptr<TtsModel> 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_;
Expand Down
1 change: 1 addition & 0 deletions runtime/onnxruntime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ add_subdirectory(frontend)

if(BUILD_SERVER)
include(boost)
include(jsoncpp)
add_subdirectory(http)
endif()

Expand Down

0 comments on commit f699f80

Please sign in to comment.