Skip to content

Commit

Permalink
feat!: finalize new addon registration flow for C++/go/python (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Dec 1, 2024
1 parent 4dff756 commit 0d78527
Show file tree
Hide file tree
Showing 74 changed files with 757 additions and 881 deletions.
36 changes: 33 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
"args": [
"--gtest_filter=StandaloneTest.BasicGraphCrossApp"
"--gtest_filter=AudioFrameTest.Basic"
],
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"env": {
Expand Down Expand Up @@ -440,9 +440,9 @@
"name": "app (C/C++) (lldb, launch)",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/standalone_test_cpp/default_extension_cpp/out/linux/x64/tests/default_extension_cpp_test",
"program": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/cpp_app_multi_process_python/cpp_app_multi_process_python_app/bin/cpp_app_multi_process_python_app_source",
"args": [],
"cwd": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/standalone_test_cpp/default_extension_cpp/out/linux/x64/tests/",
"cwd": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/cpp_app_multi_process_python/cpp_app_multi_process_python_app/",
"env": {
"ASAN_OPTIONS": "use_sigaltstack=0",
},
Expand Down Expand Up @@ -565,6 +565,36 @@
}
},
},
{
"name": "app (Python Standalone Test) (cppdbg, launch)",
"type": "cppdbg",
"request": "launch",
"program": "/usr/bin/python3",
"args": [
"tests/test_basic.py"
],
"environment": [
{
"name": "PYTHONPATH",
"value": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime_python/lib:${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime_python/interface"
},
{
"name": "LD_PRELOAD",
"value": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime/lib/libasan.so"
},
{
"name": "PYTHONMALLOC",
"value": "malloc"
},
{
"name": "PYTHONDEVMODE",
"value": "1"
}
],
"cwd": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python",
"additionalSOLibSearchPath": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime_python/lib:${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/python/standalone_test_python/default_extension_python/.ten/app/ten_packages/system/ten_runtime/lib",
"MIMode": "gdb",
},
{
"name": "app (Java) (cppdbg, attach)",
"type": "cppdbg",
Expand Down
8 changes: 6 additions & 2 deletions core/include/ten_runtime/addon/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ typedef void (*ten_addon_on_destroy_instance_func_t)(ten_addon_t *addon,
void *instance,
void *context);

typedef void (*ten_addon_on_destroy_func_t)(ten_addon_t *addon);

TEN_RUNTIME_API ten_addon_t *ten_addon_create(
ten_addon_on_init_func_t on_init, ten_addon_on_deinit_func_t on_deinit,
ten_addon_on_create_instance_func_t on_create_instance,
ten_addon_on_destroy_instance_func_t on_destroy_instance);
ten_addon_on_destroy_instance_func_t on_destroy_instance,
ten_addon_on_destroy_func_t on_destroy);

TEN_RUNTIME_API void ten_addon_init(
ten_addon_t *self, ten_addon_on_init_func_t on_init,
ten_addon_on_deinit_func_t on_deinit,
ten_addon_on_create_instance_func_t on_create_instance,
ten_addon_on_destroy_instance_func_t on_destroy_instance);
ten_addon_on_destroy_instance_func_t on_destroy_instance,
ten_addon_on_destroy_func_t on_destroy);

TEN_RUNTIME_API void ten_addon_destroy(ten_addon_t *self);
19 changes: 19 additions & 0 deletions core/include/ten_runtime/addon/addon_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright © 2024 Agora
// This file is part of TEN Framework, an open source project.
// Licensed under the Apache License, Version 2.0, with certain conditions.
// Refer to the "LICENSE" file in the root directory for more information.
//
#pragma once

#include "ten_runtime/ten_config.h"

typedef struct ten_addon_manager_t ten_addon_manager_t;

typedef void (*ten_addon_registration_func_t)(void *register_ctx);

TEN_RUNTIME_API ten_addon_manager_t *ten_addon_manager_get_instance(void);

TEN_RUNTIME_API void ten_addon_manager_add_addon(
ten_addon_manager_t *self, const char *name,
ten_addon_registration_func_t func);
10 changes: 2 additions & 8 deletions core/include/ten_runtime/addon/extension/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,21 @@ typedef struct ten_extension_t ten_extension_t;
typedef struct ten_addon_host_t ten_addon_host_t;

typedef ten_addon_host_t *(*ten_addon_register_extension_func_t)(
const char *name, const char *base_dir, ten_addon_t *addon);

typedef ten_addon_host_t *(*ten_addon_register_extension_v2_func_t)(
const char *name, const char *base_dir, ten_addon_t *addon,
void *register_ctx);

TEN_RUNTIME_API ten_addon_host_t *ten_addon_register_extension(
const char *name, const char *base_dir, ten_addon_t *addon);

TEN_RUNTIME_API ten_addon_host_t *ten_addon_register_extension_v2(
const char *name, const char *base_dir, ten_addon_t *addon,
void *register_ctx);

TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_extension(const char *name);

TEN_RUNTIME_API bool ten_addon_create_extension(
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
ten_env_addon_on_create_instance_async_cb_t cb, void *user_data,
ten_env_addon_create_instance_done_cb_t cb, void *user_data,
ten_error_t *err);

TEN_RUNTIME_API bool ten_addon_destroy_extension(
ten_env_t *ten_env, ten_extension_t *extension,
ten_env_addon_on_destroy_instance_async_cb_t cb, void *user_data,
ten_env_addon_destroy_instance_done_cb_t cb, void *user_data,
ten_error_t *err);
12 changes: 11 additions & 1 deletion core/include/ten_runtime/binding/cpp/detail/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class addon_t {
addon_t()
: c_addon(ten_addon_create(proxy_on_init, proxy_on_deinit,
proxy_on_create_instance,
proxy_on_destroy_instance)) {
proxy_on_destroy_instance, proxy_on_destroy)) {
ten_binding_handle_set_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(c_addon), this);
}
Expand Down Expand Up @@ -174,6 +174,16 @@ class addon_t {
cpp_addon->invoke_cpp_addon_on_destroy_instance(*cpp_ten_env, cpp_instance,
context);
}

static void proxy_on_destroy(ten_addon_t *addon) {
TEN_ASSERT(addon, "Invalid argument.");

auto *cpp_addon =
static_cast<addon_t *>(ten_binding_handle_get_me_in_target_lang(
reinterpret_cast<ten_binding_handle_t *>(addon)));

delete cpp_addon;
}
};

namespace {
Expand Down
93 changes: 31 additions & 62 deletions core/include/ten_runtime/binding/cpp/detail/addon_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,35 @@
#include <cassert>
#include <cstddef>

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION(NAME, CLASS) \
class NAME##_default_extension_addon_t : public ten::extension_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static ten::addon_t *g_##NAME##_default_extension_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_addon____) { \
g_##NAME##_default_extension_addon = \
new NAME##_default_extension_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ctor_ten_declare_##NAME##_extension_addon____); \
ten_addon_register_extension( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_extension_addon____) { \
ten_addon_unregister_extension(#NAME); \
delete g_##NAME##_default_extension_addon; \
}
#include "ten_runtime/addon/addon_manager.h" // IWYU pragma: export

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION_V2(NAME, CLASS) \
class NAME##_default_extension_addon_t : public ten::extension_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static ten::addon_t *g_##NAME##_default_extension_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_addon____) { \
g_##NAME##_default_extension_addon = \
new NAME##_default_extension_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ctor_ten_declare_##NAME##_extension_addon____); \
ten_addon_register_extension( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_extension_addon____) { \
ten_addon_unregister_extension(#NAME); \
delete g_##NAME##_default_extension_addon; \
}
#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION(NAME, CLASS) \
class NAME##_default_extension_addon_t : public ten::extension_addon_t { \
public: \
void on_create_instance(ten::ten_env_t &ten_env, const char *name, \
void *context) override { \
auto *instance = new CLASS(name); \
ten_env.on_create_instance_done(instance, context); \
} \
void on_destroy_instance(ten::ten_env_t &ten_env, void *instance, \
void *context) override { \
delete static_cast<CLASS *>(instance); \
ten_env.on_destroy_instance_done(context); \
} \
}; \
static void ____ten_addon_##NAME##_register_handler__(void *register_ctx) { \
auto *addon_instance = new NAME##_default_extension_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ten_addon_##NAME##_register_handler__); \
ten_addon_register_extension(#NAME, ten_string_get_raw_str(base_dir), \
addon_instance->get_c_addon(), register_ctx); \
ten_string_destroy(base_dir); \
} \
TEN_CONSTRUCTOR(____ten_addon_##NAME##_registrar____) { \
/* Add addon registration function into addon manager. */ \
ten_addon_manager_t *manager = ten_addon_manager_get_instance(); \
ten_addon_manager_add_addon(manager, #NAME, \
____ten_addon_##NAME##_register_handler__); \
}\
10 changes: 5 additions & 5 deletions core/include/ten_runtime/ten_env/ten_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
typedef struct ten_env_t ten_env_t;
typedef struct ten_extension_group_t ten_extension_group_t;

typedef void (*ten_env_addon_on_create_instance_async_cb_t)(ten_env_t *ten_env,
void *instance,
void *cb_data);
typedef void (*ten_env_addon_create_instance_done_cb_t)(ten_env_t *ten_env,
void *instance,
void *cb_data);

typedef void (*ten_env_addon_on_destroy_instance_async_cb_t)(ten_env_t *ten_env,
void *cb_data);
typedef void (*ten_env_addon_destroy_instance_done_cb_t)(ten_env_t *ten_env,
void *cb_data);

typedef void (*ten_env_is_cmd_connected_async_cb_t)(ten_env_t *ten_env,
bool result, void *cb_data,
Expand Down
9 changes: 9 additions & 0 deletions core/include/ten_utils/lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@

#define TEN_STRING_SIGNATURE 0x178445C0402E320DU

#define MAX_BUFFER_SIZE (10 * 1024 * 1024) // 10 M

#if defined(NDEBUG)
#define TEN_STRING_PRE_BUF_SIZE 256
#define BUFFER_ENLARGE_RATIO 2
#else
// In debug mode, significantly reduce the size of `prebuf` so that
// `ten_string_reserve` is actually triggered. This way, we can test that even
// if `malloc` occurs within `ten_string_reserve`, there will be no memory leak.
#define TEN_STRING_PRE_BUF_SIZE 8

// Because the initial buffer size of a string in debug mode is relatively
// small, the enlargement ratio is set higher each time capacity needs to be
// reserved. This helps avoid frequent capacity reservations, which could
// otherwise lead to poor performance.
#define BUFFER_ENLARGE_RATIO 30
#endif

typedef struct ten_list_t ten_list_t;
Expand Down
23 changes: 12 additions & 11 deletions core/include_internal/ten_runtime/addon/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ typedef enum TEN_ADDON_TYPE {
typedef struct ten_addon_context_t {
ten_env_t *caller_ten;

ten_env_addon_on_create_instance_async_cb_t addon_on_create_instance_async_cb;
void *addon_on_create_instance_async_cb_data;
ten_env_addon_create_instance_done_cb_t create_instance_done_cb;
void *create_instance_done_cb_data;

ten_env_addon_on_destroy_instance_async_cb_t
addon_on_destroy_instance_async_cb;
void *addon_on_destroy_instance_async_cb_data;
ten_env_addon_destroy_instance_done_cb_t destroy_instance_done_cb;
void *destroy_instance_done_cb_data;
} ten_addon_context_t;

typedef struct ten_addon_t {
Expand All @@ -51,6 +50,8 @@ typedef struct ten_addon_t {
ten_addon_on_create_instance_func_t on_create_instance;
ten_addon_on_destroy_instance_func_t on_destroy_instance;

ten_addon_on_destroy_func_t on_destroy;

void *user_data;
} ten_addon_t;

Expand Down Expand Up @@ -81,14 +82,14 @@ typedef struct ten_addon_on_create_instance_info_t {
ten_string_t addon_name;
ten_string_t instance_name;
TEN_ADDON_TYPE addon_type; // Used to retrieve addon from the correct store.
ten_env_addon_on_create_instance_async_cb_t cb;
ten_env_addon_create_instance_done_cb_t cb;
void *cb_data;
} ten_addon_on_create_instance_info_t;

typedef struct ten_addon_on_destroy_instance_info_t {
ten_addon_host_t *addon_host;
void *instance;
ten_env_addon_on_destroy_instance_async_cb_t cb;
ten_env_addon_destroy_instance_done_cb_t cb;
void *cb_data;
} ten_addon_on_destroy_instance_info_t;

Expand Down Expand Up @@ -125,7 +126,7 @@ TEN_RUNTIME_API ten_addon_host_t *ten_addon_host_find(const char *addon_name,
TEN_RUNTIME_PRIVATE_API ten_addon_on_create_instance_info_t *
ten_addon_on_create_instance_info_create(
const char *addon_name, const char *instance_name,
TEN_ADDON_TYPE addon_type, ten_env_addon_on_create_instance_async_cb_t cb,
TEN_ADDON_TYPE addon_type, ten_env_addon_create_instance_done_cb_t cb,
void *cb_data);

TEN_RUNTIME_PRIVATE_API void ten_addon_on_create_instance_info_destroy(
Expand All @@ -134,7 +135,7 @@ TEN_RUNTIME_PRIVATE_API void ten_addon_on_create_instance_info_destroy(
TEN_RUNTIME_PRIVATE_API ten_addon_on_destroy_instance_info_t *
ten_addon_host_on_destroy_instance_info_create(
ten_addon_host_t *self, void *instance,
ten_env_addon_on_destroy_instance_async_cb_t cb, void *cb_data);
ten_env_addon_destroy_instance_done_cb_t cb, void *cb_data);

TEN_RUNTIME_PRIVATE_API void ten_addon_on_destroy_instance_info_destroy(
ten_addon_on_destroy_instance_info_t *self);
Expand All @@ -143,12 +144,12 @@ TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_addon_get_store(void);

TEN_RUNTIME_PRIVATE_API bool ten_addon_create_instance_async(
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
TEN_ADDON_TYPE type, ten_env_addon_on_create_instance_async_cb_t cb,
TEN_ADDON_TYPE type, ten_env_addon_create_instance_done_cb_t cb,
void *cb_data);

TEN_RUNTIME_PRIVATE_API bool ten_addon_host_destroy_instance_async(
ten_addon_host_t *self, ten_env_t *ten_env, void *instance,
ten_env_addon_on_destroy_instance_async_cb_t cb, void *cb_data);
ten_env_addon_destroy_instance_done_cb_t cb, void *cb_data);

TEN_RUNTIME_PRIVATE_API bool ten_addon_host_destroy_instance(
ten_addon_host_t *self, ten_env_t *ten_env, void *instance);
Expand Down
Loading

0 comments on commit 0d78527

Please sign in to comment.