Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more standalone testing codes for C++/python #251

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 31 additions & 36 deletions core/include/ten_runtime/binding/cpp/internal/test/env_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
#include <memory>

#include "ten_runtime/binding/common.h"
#include "ten_runtime/binding/cpp/internal/msg/audio_frame.h"
#include "ten_runtime/binding/cpp/internal/msg/cmd/cmd.h"
#include "ten_runtime/binding/cpp/internal/msg/cmd_result.h"
#include "ten_runtime/binding/cpp/internal/msg/data.h"
#include "ten_runtime/binding/cpp/internal/msg/video_frame.h"
#include "ten_runtime/test/env_tester.h"
#include "ten_utils/lang/cpp/lib/error.h"

Expand All @@ -34,24 +37,6 @@ class ten_env_tester_t {
ten_env_tester_t &operator=(const ten_env_tester_t &&) = delete;
// @}};

virtual bool on_configure_done(error_t *err) {
TEN_ASSERT(c_ten_env_tester, "Should not happen.");
return ten_env_tester_on_configure_done(
c_ten_env_tester,
err != nullptr ? err->get_internal_representation() : nullptr);
}

bool on_configure_done() { return on_configure_done(nullptr); }

virtual bool on_init_done(error_t *err) {
TEN_ASSERT(c_ten_env_tester, "Should not happen.");
return ten_env_tester_on_init_done(
c_ten_env_tester,
err != nullptr ? err->get_internal_representation() : nullptr);
}

bool on_init_done() { return on_init_done(nullptr); }

virtual bool on_start_done(error_t *err) {
TEN_ASSERT(c_ten_env_tester, "Should not happen.");
return ten_env_tester_on_start_done(
Expand All @@ -61,24 +46,6 @@ class ten_env_tester_t {

bool on_start_done() { return on_start_done(nullptr); }

virtual bool on_stop_done(error_t *err) {
TEN_ASSERT(c_ten_env_tester, "Should not happen.");
return ten_env_tester_on_stop_done(
c_ten_env_tester,
err != nullptr ? err->get_internal_representation() : nullptr);
}

bool on_stop_done() { return on_stop_done(nullptr); }

virtual bool on_deinit_done(error_t *err) {
TEN_ASSERT(c_ten_env_tester, "Should not happen.");
return ten_env_tester_on_deinit_done(
c_ten_env_tester,
err != nullptr ? err->get_internal_representation() : nullptr);
}

bool on_deinit_done() { return on_deinit_done(nullptr); }

virtual bool send_cmd(
std::unique_ptr<cmd_t> &&cmd,
ten_env_tester_send_cmd_result_handler_func_t &&result_handler,
Expand Down Expand Up @@ -133,6 +100,34 @@ class ten_env_tester_t {
return send_cmd(std::move(cmd), nullptr, nullptr);
}

virtual bool send_data(std::unique_ptr<data_t> &&data, error_t *err) {
TEN_ASSERT(c_ten_env_tester, "Should not happen.");

bool rc = false;

if (!data) {
TEN_ASSERT(0, "Invalid argument.");
return rc;
}

rc = ten_env_tester_send_data(c_ten_env_tester, data->get_underlying_msg());

if (rc) {
// Only when the data has been sent successfully, we should give back the
// ownership of the data to the TEN runtime.
auto *cpp_data_ptr = data.release();
delete cpp_data_ptr;
} else {
TEN_LOGE("Failed to send_data: %s", data->get_name());
}

return rc;
}

bool send_data(std::unique_ptr<data_t> &&data) {
return send_data(std::move(data), nullptr);
}

void stop_test() {
TEN_ASSERT(c_ten_env_tester, "Should not happen.");
ten_env_tester_stop_test(c_ten_env_tester);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ class extension_tester_t {
: c_extension_tester(::ten_extension_tester_create(
reinterpret_cast<ten_extension_tester_on_start_func_t>(
&proxy_on_start),
reinterpret_cast<ten_extension_tester_on_cmd_func_t>(
&proxy_on_cmd))) {
reinterpret_cast<ten_extension_tester_on_cmd_func_t>(&proxy_on_cmd),
reinterpret_cast<ten_extension_tester_on_data_func_t>(
&proxy_on_data),
reinterpret_cast<ten_extension_tester_on_audio_frame_func_t>(
&proxy_on_audio_frame),
reinterpret_cast<ten_extension_tester_on_video_frame_func_t>(
&proxy_on_video_frame))) {
TEN_ASSERT(c_extension_tester, "Should not happen.");

ten_binding_handle_set_me_in_target_lang(
Expand All @@ -69,26 +74,10 @@ class extension_tester_t {
TEN_ASSERT(cpp_ten_env_tester, "Should not happen.");
}

virtual void on_configure(ten_env_tester_t &ten_env_tester) {
ten_env_tester.on_configure_done();
}

virtual void on_init(ten_env_tester_t &ten_env_tester) {
ten_env_tester.on_init_done();
}

virtual void on_start(ten_env_tester_t &ten_env_tester) {
ten_env_tester.on_start_done();
}

virtual void on_stop(ten_env_tester_t &ten_env_tester) {
ten_env_tester.on_stop_done();
}

virtual void on_deinit(ten_env_tester_t &ten_env_tester) {
ten_env_tester.on_deinit_done();
}

virtual void on_cmd(ten_env_tester_t &ten_env_tester,
std::unique_ptr<cmd_t> cmd) {}

Expand All @@ -102,13 +91,13 @@ class extension_tester_t {
std::unique_ptr<video_frame_t> video_frame) {}

private:
void invoke_cpp_extension_tester_on_configure(
void invoke_cpp_extension_tester_on_start(
ten_env_tester_t &cpp_ten_env_tester) {
on_configure(cpp_ten_env_tester);
on_start(cpp_ten_env_tester);
}

static void proxy_on_configure(ten_extension_tester_t *tester,
::ten_env_tester_t *c_ten_env_tester) {
static void proxy_on_start(ten_extension_tester_t *tester,
::ten_env_tester_t *c_ten_env_tester) {
TEN_ASSERT(tester && c_ten_env_tester, "Should not happen.");

auto *cpp_extension_tester = static_cast<extension_tester_t *>(
Expand All @@ -118,99 +107,106 @@ class extension_tester_t {
ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(c_ten_env_tester)));

cpp_extension_tester->invoke_cpp_extension_tester_on_configure(
cpp_extension_tester->invoke_cpp_extension_tester_on_start(
*cpp_ten_env_tester);
}

void invoke_cpp_extension_tester_on_init(
ten_env_tester_t &cpp_ten_env_tester) {
on_init(cpp_ten_env_tester);
void invoke_cpp_extension_on_cmd(ten_env_tester_t &cpp_ten_env_tester,
std::unique_ptr<cmd_t> cmd) {
on_cmd(cpp_ten_env_tester, std::move(cmd));
}

static void proxy_on_init(ten_extension_tester_t *tester,
::ten_env_tester_t *c_ten_env_tester) {
TEN_ASSERT(tester && c_ten_env_tester, "Should not happen.");
static void proxy_on_cmd(ten_extension_tester_t *extension_tester,
::ten_env_tester_t *c_ten_env_tester,
ten_shared_ptr_t *cmd) {
TEN_ASSERT(extension_tester && c_ten_env_tester && cmd,
"Should not happen.");

auto *cpp_extension_tester = static_cast<extension_tester_t *>(
ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(tester)));
reinterpret_cast<ten_binding_handle_t *>(extension_tester)));
auto *cpp_ten_env_tester = static_cast<ten_env_tester_t *>(
ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(c_ten_env_tester)));

cpp_extension_tester->invoke_cpp_extension_tester_on_init(
*cpp_ten_env_tester);
}

void invoke_cpp_extension_tester_on_start(
ten_env_tester_t &cpp_ten_env_tester) {
on_start(cpp_ten_env_tester);
}

static void proxy_on_start(ten_extension_tester_t *tester,
::ten_env_tester_t *c_ten_env_tester) {
TEN_ASSERT(tester && c_ten_env_tester, "Should not happen.");
// Clone a C shared_ptr to be owned by the C++ instance.
cmd = ten_shared_ptr_clone(cmd);

auto *cpp_extension_tester = static_cast<extension_tester_t *>(
ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(tester)));
auto *cpp_ten_env_tester = static_cast<ten_env_tester_t *>(
ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(c_ten_env_tester)));
auto *cpp_cmd_ptr = new cmd_t(cmd);
auto cpp_cmd_unique_ptr = std::unique_ptr<cmd_t>(cpp_cmd_ptr);

cpp_extension_tester->invoke_cpp_extension_tester_on_start(
*cpp_ten_env_tester);
cpp_extension_tester->invoke_cpp_extension_on_cmd(
*cpp_ten_env_tester, std::move(cpp_cmd_unique_ptr));
}

void invoke_cpp_extension_tester_on_stop(
ten_env_tester_t &cpp_ten_env_tester) {
on_stop(cpp_ten_env_tester);
void invoke_cpp_extension_on_data(ten_env_tester_t &cpp_ten_env_tester,
std::unique_ptr<data_t> data) {
on_data(cpp_ten_env_tester, std::move(data));
}

static void proxy_on_stop(ten_extension_tester_t *tester,
::ten_env_tester_t *c_ten_env_tester) {
TEN_ASSERT(tester && c_ten_env_tester, "Should not happen.");
static void proxy_on_data(ten_extension_tester_t *extension_tester,
::ten_env_tester_t *c_ten_env_tester,
ten_shared_ptr_t *data) {
TEN_ASSERT(extension_tester && c_ten_env_tester && data,
"Should not happen.");

auto *cpp_extension_tester = static_cast<extension_tester_t *>(
ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(tester)));
reinterpret_cast<ten_binding_handle_t *>(extension_tester)));
auto *cpp_ten_env_tester = static_cast<ten_env_tester_t *>(
ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(c_ten_env_tester)));

cpp_extension_tester->invoke_cpp_extension_tester_on_stop(
*cpp_ten_env_tester);
// Clone a C shared_ptr to be owned by the C++ instance.
data = ten_shared_ptr_clone(data);

auto *cpp_data_ptr = new data_t(data);
auto cpp_data_unique_ptr = std::unique_ptr<data_t>(cpp_data_ptr);

cpp_extension_tester->invoke_cpp_extension_on_data(
*cpp_ten_env_tester, std::move(cpp_data_unique_ptr));
}

void invoke_cpp_extension_tester_on_deinit(
ten_env_tester_t &cpp_ten_env_tester) {
on_deinit(cpp_ten_env_tester);
void invoke_cpp_extension_on_audio_frame(
ten_env_tester_t &cpp_ten_env_tester,
std::unique_ptr<audio_frame_t> audio_frame) {
on_audio_frame(cpp_ten_env_tester, std::move(audio_frame));
}

static void proxy_on_deinit(ten_extension_tester_t *tester,
::ten_env_tester_t *c_ten_env_tester) {
TEN_ASSERT(tester && c_ten_env_tester, "Should not happen.");
static void proxy_on_audio_frame(ten_extension_tester_t *extension_tester,
::ten_env_tester_t *c_ten_env_tester,
ten_shared_ptr_t *audio_frame) {
TEN_ASSERT(extension_tester && c_ten_env_tester && audio_frame,
"Should not happen.");

auto *cpp_extension_tester = static_cast<extension_tester_t *>(
ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(tester)));
reinterpret_cast<ten_binding_handle_t *>(extension_tester)));
auto *cpp_ten_env_tester = static_cast<ten_env_tester_t *>(
ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(c_ten_env_tester)));

cpp_extension_tester->invoke_cpp_extension_tester_on_deinit(
*cpp_ten_env_tester);
// Clone a C shared_ptr to be owned by the C++ instance.
audio_frame = ten_shared_ptr_clone(audio_frame);

auto *cpp_audio_frame_ptr = new audio_frame_t(audio_frame);
auto cpp_audio_frame_unique_ptr =
std::unique_ptr<audio_frame_t>(cpp_audio_frame_ptr);

cpp_extension_tester->invoke_cpp_extension_on_audio_frame(
*cpp_ten_env_tester, std::move(cpp_audio_frame_unique_ptr));
}

void invoke_cpp_extension_on_cmd(ten_env_tester_t &cpp_ten_env_tester,
std::unique_ptr<cmd_t> cmd) {
on_cmd(cpp_ten_env_tester, std::move(cmd));
void invoke_cpp_extension_on_video_frame(
ten_env_tester_t &cpp_ten_env_tester,
std::unique_ptr<video_frame_t> video_frame) {
on_video_frame(cpp_ten_env_tester, std::move(video_frame));
}

static void proxy_on_cmd(ten_extension_tester_t *extension_tester,
::ten_env_tester_t *c_ten_env_tester,
ten_shared_ptr_t *cmd) {
TEN_ASSERT(extension_tester && c_ten_env_tester && cmd,
static void proxy_on_video_frame(ten_extension_tester_t *extension_tester,
::ten_env_tester_t *c_ten_env_tester,
ten_shared_ptr_t *video_frame) {
TEN_ASSERT(extension_tester && c_ten_env_tester && video_frame,
"Should not happen.");

auto *cpp_extension_tester = static_cast<extension_tester_t *>(
Expand All @@ -221,13 +217,14 @@ class extension_tester_t {
reinterpret_cast<ten_binding_handle_t *>(c_ten_env_tester)));

// Clone a C shared_ptr to be owned by the C++ instance.
cmd = ten_shared_ptr_clone(cmd);
video_frame = ten_shared_ptr_clone(video_frame);

auto *cpp_cmd_ptr = new cmd_t(cmd);
auto cpp_cmd_unique_ptr = std::unique_ptr<cmd_t>(cpp_cmd_ptr);
auto *cpp_video_frame_ptr = new video_frame_t(video_frame);
auto cpp_video_frame_unique_ptr =
std::unique_ptr<video_frame_t>(cpp_video_frame_ptr);

cpp_extension_tester->invoke_cpp_extension_on_cmd(
*cpp_ten_env_tester, std::move(cpp_cmd_unique_ptr));
cpp_extension_tester->invoke_cpp_extension_on_video_frame(
*cpp_ten_env_tester, std::move(cpp_video_frame_unique_ptr));
}

::ten_extension_tester_t *c_extension_tester;
Expand Down
21 changes: 9 additions & 12 deletions core/include/ten_runtime/test/env_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@

typedef struct ten_env_tester_t ten_env_tester_t;

TEN_RUNTIME_API bool ten_env_tester_on_configure_done(ten_env_tester_t *self,
ten_error_t *err);

TEN_RUNTIME_API bool ten_env_tester_on_init_done(ten_env_tester_t *self,
ten_error_t *err);

TEN_RUNTIME_API bool ten_env_tester_on_start_done(ten_env_tester_t *self,
ten_error_t *err);

TEN_RUNTIME_API bool ten_env_tester_on_stop_done(ten_env_tester_t *self,
ten_error_t *err);

TEN_RUNTIME_API bool ten_env_tester_on_deinit_done(ten_env_tester_t *self,
ten_error_t *err);

typedef void (*ten_env_tester_cmd_result_handler_func_t)(
ten_env_tester_t *self, ten_shared_ptr_t *cmd_result, void *user_data);

TEN_RUNTIME_API bool ten_env_tester_send_cmd(
ten_env_tester_t *self, ten_shared_ptr_t *cmd,
ten_env_tester_cmd_result_handler_func_t handler, void *user_data);

TEN_RUNTIME_API bool ten_env_tester_send_data(ten_env_tester_t *self,
ten_shared_ptr_t *data);

TEN_RUNTIME_API bool ten_env_tester_send_audio_frame(
ten_env_tester_t *self, ten_shared_ptr_t *audio_frame);

TEN_RUNTIME_API bool ten_env_tester_send_video_frame(
ten_env_tester_t *self, ten_shared_ptr_t *video_frame);

TEN_RUNTIME_API void ten_env_tester_stop_test(ten_env_tester_t *self);
Loading
Loading