Skip to content

Commit

Permalink
fix: support calling ten_env_tester api on outer thread in python
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxilin committed Jan 9, 2025
1 parent 37429a7 commit 7d42fc4
Show file tree
Hide file tree
Showing 22 changed files with 541 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class extension_tester_t {
: c_extension_tester(::ten_extension_tester_create(
reinterpret_cast<ten_extension_tester_on_start_func_t>(
&proxy_on_start),
nullptr,
reinterpret_cast<ten_extension_tester_on_cmd_func_t>(&proxy_on_cmd),
reinterpret_cast<ten_extension_tester_on_data_func_t>(
&proxy_on_data),
Expand Down
3 changes: 3 additions & 0 deletions core/include/ten_runtime/test/env_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ typedef struct ten_env_tester_t ten_env_tester_t;
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);

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_error_t *error);
Expand Down
9 changes: 9 additions & 0 deletions core/include/ten_runtime/test/extension_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ typedef enum TEN_EXTENSION_TESTER_TEST_MODE {

typedef void (*ten_extension_tester_on_start_func_t)(
ten_extension_tester_t *self, ten_env_tester_t *ten_env);
typedef void (*ten_extension_tester_on_stop_func_t)(
ten_extension_tester_t *self, ten_env_tester_t *ten_env);

typedef void (*ten_extension_tester_on_cmd_func_t)(ten_extension_tester_t *self,
ten_env_tester_t *ten_env,
Expand All @@ -41,6 +43,7 @@ typedef void (*ten_extension_tester_on_video_frame_func_t)(

TEN_RUNTIME_API ten_extension_tester_t *ten_extension_tester_create(
ten_extension_tester_on_start_func_t on_start,
ten_extension_tester_on_stop_func_t on_stop,
ten_extension_tester_on_cmd_func_t on_cmd,
ten_extension_tester_on_data_func_t on_data,
ten_extension_tester_on_audio_frame_func_t on_audio_frame,
Expand Down Expand Up @@ -75,8 +78,14 @@ TEN_RUNTIME_API ten_env_tester_t *ten_extension_tester_get_ten_env_tester(
TEN_RUNTIME_PRIVATE_API void ten_extension_tester_on_test_extension_start(
ten_extension_tester_t *self);

TEN_RUNTIME_PRIVATE_API void ten_extension_tester_on_test_extension_stop(
ten_extension_tester_t *self);

TEN_RUNTIME_PRIVATE_API void ten_extension_tester_on_test_extension_deinit(
ten_extension_tester_t *self);

TEN_RUNTIME_PRIVATE_API void ten_extension_tester_on_start_done(
ten_extension_tester_t *self);

TEN_RUNTIME_PRIVATE_API void ten_extension_tester_on_stop_done(
ten_extension_tester_t *self);
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "include_internal/ten_runtime/binding/python/common/python_stuff.h"
#include "include_internal/ten_runtime/test/env_tester.h"
#include "include_internal/ten_runtime/test/env_tester_proxy.h"
#include "ten_utils/lib/signature.h"

#define TEN_PY_TEN_ENV_TESTER_SIGNATURE 0x9DF807EAFAF9F6D5U
Expand All @@ -22,6 +23,8 @@ typedef struct ten_py_ten_env_tester_t {
ten_signature_t signature;

ten_env_tester_t *c_ten_env_tester;
ten_env_tester_proxy_t *c_ten_env_tester_proxy;

PyObject *actual_py_ten_env_tester;
} ten_py_ten_env_tester_t;

Expand Down
1 change: 1 addition & 0 deletions core/include_internal/ten_runtime/test/extension_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct ten_extension_tester_t {
ten_string_t test_app_property_json;

ten_extension_tester_on_start_func_t on_start;
ten_extension_tester_on_stop_func_t on_stop;
ten_extension_tester_on_cmd_func_t on_cmd;
ten_extension_tester_on_data_func_t on_data;
ten_extension_tester_on_audio_frame_func_t on_audio_frame;
Expand Down
4 changes: 4 additions & 0 deletions core/include_internal/ten_runtime/test/test_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ TEN_RUNTIME_PRIVATE_API void ten_builtin_test_extension_addon_unregister(void);
TEN_RUNTIME_PRIVATE_API void
ten_builtin_test_extension_ten_env_notify_on_start_done(ten_env_t *ten_env,
void *user_data);

TEN_RUNTIME_PRIVATE_API void
ten_builtin_test_extension_ten_env_notify_on_stop_done(ten_env_t *ten_env,
void *user_data);
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ ten_go_extension_tester_t *ten_go_extension_tester_create_internal(
extension_tester_bridge, ten_go_extension_tester_bridge_destroy);
extension_tester_bridge->bridge.sp_ref_by_c = NULL;

extension_tester_bridge->c_extension_tester =
ten_extension_tester_create(proxy_on_start, proxy_on_cmd, proxy_on_data,
proxy_on_audio_frame, proxy_on_video_frame);
extension_tester_bridge->c_extension_tester = ten_extension_tester_create(
proxy_on_start, NULL, proxy_on_cmd, proxy_on_data, proxy_on_audio_frame,
proxy_on_video_frame);

ten_binding_handle_set_me_in_target_lang(
&extension_tester_bridge->c_extension_tester->binding_handle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "include_internal/ten_runtime/binding/python/common/error.h"
#include "ten_runtime/binding/common.h"
#include "ten_runtime/test/env_tester_proxy.h"
#include "ten_utils/macro/check.h"

bool ten_py_ten_env_tester_check_integrity(ten_py_ten_env_tester_t *self) {
Expand All @@ -33,6 +34,12 @@ static void ten_py_ten_env_tester_c_part_destroyed(
"Should not happen.");

ten_env_tester_bridge->c_ten_env_tester = NULL;
if (ten_env_tester_bridge->c_ten_env_tester_proxy) {
ten_env_tester_proxy_release(ten_env_tester_bridge->c_ten_env_tester_proxy,
NULL);
ten_env_tester_bridge->c_ten_env_tester_proxy = NULL;
}

ten_py_ten_env_tester_invalidate(ten_env_tester_bridge);
}

Expand Down Expand Up @@ -100,6 +107,7 @@ ten_py_ten_env_tester_t *ten_py_ten_env_tester_wrap(
ten_signature_set(&py_ten_env_tester->signature,
TEN_PY_TEN_ENV_TESTER_SIGNATURE);
py_ten_env_tester->c_ten_env_tester = ten_env_tester;
py_ten_env_tester->c_ten_env_tester_proxy = NULL;

py_ten_env_tester->actual_py_ten_env_tester =
create_actual_py_ten_env_tester_instance(py_ten_env_tester);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
//
#include "include_internal/ten_runtime/binding/python/test/env_tester.h"
#include "ten_runtime/test/env_tester.h"
#include "ten_runtime/test/env_tester_proxy.h"
#include "ten_utils/lib/error.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/mark.h"

static void ten_py_ten_env_tester_on_start_done_proxy_notify(
ten_env_tester_t *ten_env_tester, void *user_data) {
ten_env_tester_on_start_done(ten_env_tester, NULL);
}

PyObject *ten_py_ten_env_tester_on_start_done(PyObject *self,
TEN_UNUSED PyObject *args) {
ten_py_ten_env_tester_t *py_ten_env_tester = (ten_py_ten_env_tester_t *)self;
Expand All @@ -20,7 +26,10 @@ PyObject *ten_py_ten_env_tester_on_start_done(PyObject *self,
ten_error_t err;
ten_error_init(&err);

ten_env_tester_on_start_done(py_ten_env_tester->c_ten_env_tester, &err);
bool rc = ten_env_tester_proxy_notify(
py_ten_env_tester->c_ten_env_tester_proxy,
ten_py_ten_env_tester_on_start_done_proxy_notify, NULL, &err);
TEN_ASSERT(rc, "Should not happen.");

ten_error_deinit(&err);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@
//
#include "include_internal/ten_runtime/binding/python/test/env_tester.h"
#include "ten_runtime/test/env_tester.h"
#include "ten_runtime/test/env_tester_proxy.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/mark.h"

static void ten_py_ten_env_tester_stop_test_proxy_notify(
ten_env_tester_t *ten_env_tester, void *user_data) {
ten_env_tester_stop_test(ten_env_tester, NULL);
}

PyObject *ten_py_ten_env_tester_stop_test(PyObject *self,
TEN_UNUSED PyObject *args) {
ten_py_ten_env_tester_t *py_ten_env_tester = (ten_py_ten_env_tester_t *)self;
TEN_ASSERT(py_ten_env_tester &&
ten_py_ten_env_tester_check_integrity(py_ten_env_tester),
"Invalid argument.");

bool rc = ten_env_tester_stop_test(py_ten_env_tester->c_ten_env_tester, NULL);
ten_error_t err;
ten_error_init(&err);

bool rc = ten_env_tester_proxy_notify(
py_ten_env_tester->c_ten_env_tester_proxy,
ten_py_ten_env_tester_stop_test_proxy_notify, NULL, &err);
TEN_ASSERT(rc, "Should not happen.");

ten_error_deinit(&err);

if (rc) {
Py_RETURN_NONE;
} else {
return NULL;
}
Py_RETURN_NONE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,46 @@
#include "include_internal/ten_runtime/binding/python/test/env_tester.h"
#include "ten_runtime/msg/cmd_result/cmd_result.h"
#include "ten_runtime/test/env_tester.h"
#include "ten_runtime/test/env_tester_proxy.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/memory.h"

typedef struct ten_py_ten_env_tester_notify_return_result_ctx_t {
ten_shared_ptr_t *cmd_result;
ten_shared_ptr_t *target_cmd;
PyObject *cb_func;
} ten_py_ten_env_tester_notify_return_result_ctx_t;

static ten_py_ten_env_tester_notify_return_result_ctx_t *
ten_py_ten_env_tester_notify_return_result_ctx_create(
ten_shared_ptr_t *cmd_result, ten_shared_ptr_t *target_cmd,
PyObject *cb_func) {
ten_py_ten_env_tester_notify_return_result_ctx_t *ctx =
TEN_MALLOC(sizeof(ten_py_ten_env_tester_notify_return_result_ctx_t));
ctx->cmd_result = ten_shared_ptr_clone(cmd_result);
ctx->target_cmd = ten_shared_ptr_clone(target_cmd);

// Increase the reference count of the callback function to ensure that it
// will not be destroyed before the callback is called.
if (cb_func) {
Py_INCREF(cb_func);
}

ctx->cb_func = cb_func;
return ctx;
}

static void ten_py_ten_env_tester_notify_return_result_ctx_destroy(
ten_py_ten_env_tester_notify_return_result_ctx_t *ctx) {
ten_shared_ptr_destroy(ctx->cmd_result);
ten_shared_ptr_destroy(ctx->target_cmd);

if (ctx->cb_func) {
Py_XDECREF(ctx->cb_func);
}

TEN_FREE(ctx);
}

static void proxy_return_result_callback(ten_env_tester_t *self,
void *user_data, ten_error_t *error) {
Expand Down Expand Up @@ -56,6 +95,25 @@ static void proxy_return_result_callback(ten_env_tester_t *self,
ten_py_gil_state_release_internal(prev_state);
}

static void ten_py_ten_env_tester_notify_return_result_proxy_notify(
ten_env_tester_t *ten_env_tester, void *user_data) {
ten_py_ten_env_tester_notify_return_result_ctx_t *ctx =
(ten_py_ten_env_tester_notify_return_result_ctx_t *)user_data;

if (ctx->cb_func) {
Py_INCREF(ctx->cb_func);

ten_env_tester_return_result(ten_env_tester, ctx->cmd_result,
ctx->target_cmd, proxy_return_result_callback,
ctx->cb_func, NULL);
} else {
ten_env_tester_return_result(ten_env_tester, ctx->cmd_result,
ctx->target_cmd, NULL, NULL, NULL);
}

ten_py_ten_env_tester_notify_return_result_ctx_destroy(ctx);
}

PyObject *ten_py_ten_env_tester_return_result(PyObject *self, PyObject *args) {
ten_py_ten_env_tester_t *py_ten_env_tester = (ten_py_ten_env_tester_t *)self;
TEN_ASSERT(py_ten_env_tester &&
Expand Down Expand Up @@ -90,24 +148,16 @@ PyObject *ten_py_ten_env_tester_return_result(PyObject *self, PyObject *args) {
cb_func = NULL;
}

if (cb_func) {
// Increase the reference count of the callback function to ensure that it
// will not be destroyed before the callback is called.
Py_INCREF(cb_func);
ten_py_ten_env_tester_notify_return_result_ctx_t *ctx =
ten_py_ten_env_tester_notify_return_result_ctx_create(
py_cmd_result->msg.c_msg, py_target_cmd->msg.c_msg, cb_func);

success = ten_env_tester_return_result(
py_ten_env_tester->c_ten_env_tester, py_cmd_result->msg.c_msg,
py_target_cmd->msg.c_msg, proxy_return_result_callback, cb_func, &err);
} else {
success = ten_env_tester_return_result(
py_ten_env_tester->c_ten_env_tester, py_cmd_result->msg.c_msg,
py_target_cmd->msg.c_msg, NULL, NULL, &err);
}
success = ten_env_tester_proxy_notify(
py_ten_env_tester->c_ten_env_tester_proxy,
ten_py_ten_env_tester_notify_return_result_proxy_notify, ctx, &err);

if (!success) {
if (cb_func) {
Py_XDECREF(cb_func);
}
ten_py_ten_env_tester_notify_return_result_ctx_destroy(ctx);

ten_py_raise_py_runtime_error_exception("Failed to return result.");
goto done;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,40 @@
#include "include_internal/ten_runtime/binding/python/msg/msg.h"
#include "include_internal/ten_runtime/binding/python/test/env_tester.h"
#include "ten_runtime/test/env_tester.h"
#include "ten_runtime/test/env_tester_proxy.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/memory.h"

typedef struct ten_py_ten_env_tester_send_audio_frame_ctx_t {
ten_shared_ptr_t *audio_frame;
PyObject *cb_func;
} ten_py_ten_env_tester_send_audio_frame_ctx_t;

static ten_py_ten_env_tester_send_audio_frame_ctx_t *
ten_py_ten_env_tester_send_audio_frame_ctx_create(ten_shared_ptr_t *audio_frame,
PyObject *cb_func) {
ten_py_ten_env_tester_send_audio_frame_ctx_t *ctx =
TEN_MALLOC(sizeof(ten_py_ten_env_tester_send_audio_frame_ctx_t));
ctx->audio_frame = ten_shared_ptr_clone(audio_frame);

if (cb_func) {
Py_INCREF(cb_func);
}

ctx->cb_func = cb_func;
return ctx;
}

static void ten_py_ten_env_tester_send_audio_frame_ctx_destroy(
ten_py_ten_env_tester_send_audio_frame_ctx_t *ctx) {
ten_shared_ptr_destroy(ctx->audio_frame);

if (ctx->cb_func) {
Py_XDECREF(ctx->cb_func);
}

TEN_FREE(ctx);
}

static void proxy_send_xxx_callback(ten_env_tester_t *self,
void *user_audio_frame,
Expand Down Expand Up @@ -55,6 +88,25 @@ static void proxy_send_xxx_callback(ten_env_tester_t *self,
ten_py_gil_state_release_internal(prev_state);
}

static void ten_py_ten_env_tester_send_audio_frame_proxy_notify(
ten_env_tester_t *ten_env_tester, void *user_data) {
ten_py_ten_env_tester_send_audio_frame_ctx_t *ctx =
(ten_py_ten_env_tester_send_audio_frame_ctx_t *)user_data;

if (ctx->cb_func) {
Py_INCREF(ctx->cb_func);

ten_env_tester_send_audio_frame(ten_env_tester, ctx->audio_frame,
proxy_send_xxx_callback, ctx->cb_func,
NULL);
} else {
ten_env_tester_send_audio_frame(ten_env_tester, ctx->audio_frame, NULL,
NULL, NULL);
}

ten_py_ten_env_tester_send_audio_frame_ctx_destroy(ctx);
}

PyObject *ten_py_ten_env_tester_send_audio_frame(PyObject *self,
PyObject *args) {
ten_py_ten_env_tester_t *py_ten_env_tester = (ten_py_ten_env_tester_t *)self;
Expand Down Expand Up @@ -88,24 +140,16 @@ PyObject *ten_py_ten_env_tester_send_audio_frame(PyObject *self,
cb_func = NULL;
}

if (cb_func) {
// Increase the reference count of the callback function to ensure that it
// will not be destroyed before the callback is called.
Py_INCREF(cb_func);
ten_py_ten_env_tester_send_audio_frame_ctx_t *ctx =
ten_py_ten_env_tester_send_audio_frame_ctx_create(
py_audio_frame->msg.c_msg, cb_func);

success = ten_env_tester_send_audio_frame(
py_ten_env_tester->c_ten_env_tester, py_audio_frame->msg.c_msg,
proxy_send_xxx_callback, cb_func, &err);
} else {
success = ten_env_tester_send_audio_frame(
py_ten_env_tester->c_ten_env_tester, py_audio_frame->msg.c_msg, NULL,
NULL, &err);
}
success = ten_env_tester_proxy_notify(
py_ten_env_tester->c_ten_env_tester_proxy,
ten_py_ten_env_tester_send_audio_frame_proxy_notify, ctx, &err);

if (!success) {
if (cb_func) {
Py_XDECREF(cb_func);
}
ten_py_ten_env_tester_send_audio_frame_ctx_destroy(ctx);

ten_py_raise_py_runtime_error_exception("Failed to send audio_frame.");
goto done;
Expand Down
Loading

0 comments on commit 7d42fc4

Please sign in to comment.