Skip to content

Commit

Permalink
feat!: load go addons through registration function (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Nov 29, 2024
1 parent 6441b08 commit 564e16d
Show file tree
Hide file tree
Showing 21 changed files with 440 additions and 384 deletions.
21 changes: 0 additions & 21 deletions core/include/ten_runtime/addon/extension_group/extension_group.h

This file was deleted.

45 changes: 0 additions & 45 deletions core/include/ten_runtime/binding/cpp/detail/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,6 @@ struct addon_context_t {

} // namespace

class extension_group_addon_t : public addon_t {
private:
void on_create_instance_impl(ten_env_t &ten_env, const char *name,
void *context) override {
auto *cpp_context = new addon_context_t();
cpp_context->task = ADDON_TASK_CREATE_EXTENSION_GROUP;
cpp_context->c_context = context;

on_create_instance(ten_env, name, cpp_context);
}
};

class extension_addon_t : public addon_t {
private:
void on_create_instance_impl(ten_env_t &ten_env, const char *name,
Expand All @@ -219,39 +207,6 @@ class extension_addon_t : public addon_t {

} // namespace ten

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION_GROUP(NAME, CLASS) \
class NAME##_default_extension_group_addon_t \
: public ten::extension_group_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_group_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_group_addon____) { \
g_##NAME##_default_extension_group_addon = \
new NAME##_default_extension_group_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ctor_ten_declare_##NAME##_extension_group_addon____); \
ten_addon_register_extension_group( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_group_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_extension_group_addon____) { \
ten_addon_unregister_extension_group(#NAME); \
delete g_##NAME##_default_extension_group_addon; \
}

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION(NAME, CLASS) \
class NAME##_default_extension_addon_t : public ten::extension_addon_t { \
public: \
Expand Down
5 changes: 2 additions & 3 deletions core/include/ten_runtime/binding/cpp/ten.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
// This header file should be the only header file where outside world should
// include in the C++ programming language.

#include "ten_runtime/addon/extension/extension.h" // IWYU pragma: export
#include "ten_runtime/addon/extension_group/extension_group.h" // IWYU pragma: export
#include "ten_runtime/addon/extension/extension.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/addon.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/app.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/common.h" // IWYU pragma: export
Expand All @@ -25,7 +24,7 @@
#include "ten_runtime/binding/cpp/detail/msg/data.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/msg/msg.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/msg/video_frame.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/ten_env.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/ten_env.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/ten_env_impl.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/ten_env_proxy.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/test/env_tester.h" // IWYU pragma: export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@

typedef struct ten_env_t ten_env_t;
typedef struct ten_addon_store_t ten_addon_store_t;
typedef struct ten_addon_t ten_addon_t;
typedef struct ten_addon_host_t ten_addon_host_t;

#define TEN_REGISTER_ADDON_AS_EXTENSION_GROUP(NAME, ADDON) \
TEN_ADDON_REGISTER(extension_group, NAME, ADDON)

TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_extension_group_get_global_store(
void);

TEN_RUNTIME_API bool ten_addon_create_extension_group(
TEN_RUNTIME_PRIVATE_API bool ten_addon_create_extension_group(
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_RUNTIME_API bool ten_addon_destroy_extension_group(
TEN_RUNTIME_PRIVATE_API bool ten_addon_destroy_extension_group(
ten_env_t *ten_env, ten_extension_group_t *extension_group,
ten_env_addon_on_destroy_instance_async_cb_t cb, void *user_data);

TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_register_extension_group(
const char *name, const char *base_dir, ten_addon_t *addon);

TEN_RUNTIME_PRIVATE_API ten_addon_t *ten_addon_unregister_extension_group(
const char *name);
52 changes: 52 additions & 0 deletions core/include_internal/ten_runtime/binding/cpp/detail/addon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// 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

class extension_group_addon_t : public addon_t {
private:
void on_create_instance_impl(ten_env_t &ten_env, const char *name,
void *context) override {
auto *cpp_context = new addon_context_t();
cpp_context->task = ADDON_TASK_CREATE_EXTENSION_GROUP;
cpp_context->c_context = context;

on_create_instance(ten_env, name, cpp_context);
}
};

#define TEN_CPP_REGISTER_ADDON_AS_EXTENSION_GROUP(NAME, CLASS) \
class NAME##_default_extension_group_addon_t \
: public ten::extension_group_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_group_addon = nullptr; \
TEN_CONSTRUCTOR(____ctor_ten_declare_##NAME##_extension_group_addon____) { \
g_##NAME##_default_extension_group_addon = \
new NAME##_default_extension_group_addon_t(); \
ten_string_t *base_dir = \
ten_path_get_module_path(/* NOLINTNEXTLINE */ \
(void *) \
____ctor_ten_declare_##NAME##_extension_group_addon____); \
ten_addon_register_extension_group( \
#NAME, ten_string_get_raw_str(base_dir), \
g_##NAME##_default_extension_group_addon->get_c_addon()); \
ten_string_destroy(base_dir); \
} \
TEN_DESTRUCTOR(____dtor_ten_declare_##NAME##_extension_group_addon____) { \
ten_addon_unregister_extension_group(#NAME); \
delete g_##NAME##_default_extension_group_addon; \
}
4 changes: 2 additions & 2 deletions core/include_internal/ten_runtime/binding/cpp/ten.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
// This header file should be the only header file where outside world should
// include in the C++ programming language.

#include "include_internal/ten_runtime/addon/extension_group/extension_group.h" // IWYU pragma: export
#include "include_internal/ten_runtime/binding/cpp/detail/extension_impl.h" // IWYU pragma: export
#include "include_internal/ten_runtime/binding/cpp/detail/msg/cmd/timeout.h" // IWYU pragma: export
#include "include_internal/ten_runtime/binding/cpp/detail/msg/cmd/timer.h" // IWYU pragma: export
#include "include_internal/ten_runtime/binding/cpp/detail/ten_env_impl.h" // IWYU pragma: export
#include "include_internal/ten_runtime/binding/cpp/detail/ten_env_internal_accessor.h" // IWYU pragma: export
#include "ten_runtime/addon/extension/extension.h" // IWYU pragma: export
#include "ten_runtime/addon/extension_group/extension_group.h" // IWYU pragma: export
#include "ten_runtime/addon/extension/extension.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/addon.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/app.h" // IWYU pragma: export
#include "ten_runtime/binding/cpp/detail/common.h" // IWYU pragma: export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "include_internal/ten_runtime/engine/engine.h"
#include "include_internal/ten_runtime/extension_group/extension_group.h"
#include "include_internal/ten_runtime/ten_env/ten_env.h"
#include "ten_runtime/addon/extension_group/extension_group.h"
#include "ten_utils/macro/check.h"

static ten_addon_store_t g_extension_group_store = {
Expand Down
115 changes: 0 additions & 115 deletions core/src/ten_runtime/binding/go/interface/ten/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import "C"

import (
"fmt"
"path/filepath"
"runtime"
"unsafe"
)

// Addon is the interface for the addon.
Expand Down Expand Up @@ -73,118 +70,6 @@ func NewDefaultExtensionAddon(constructor func(name string) Extension) Addon {
}
}

// RegisterAddonAsExtension registers the addon as an extension.
func RegisterAddonAsExtension(addonName string, instance Addon) error {
if len(addonName) == 0 {
return newTenError(
ErrnoInvalidArgument,
"addon name is empty",
)
}

_, file, _, ok := runtime.Caller(1)
if !ok {
return newTenError(ErrnoGeneric, "Failed to get the caller information")
}

baseDir := filepath.Dir(file)

absBaseDir, err := filepath.Abs(baseDir)
if err != nil {
return newTenError(
ErrnoGeneric,
fmt.Sprintf("Failed to get the absolute file path: %v", err),
)
}

addonWrapper := &addon{
Addon: instance,
}

addonID := newImmutableHandle(addonWrapper)

var bridge C.uintptr_t
status := C.ten_go_addon_register_extension(
unsafe.Pointer(unsafe.StringData(addonName)),
C.int(len(addonName)),
unsafe.Pointer(unsafe.StringData(absBaseDir)),
C.int(len(absBaseDir)),
cHandle(addonID),
&bridge,
)

if err := withCGoError(&status); err != nil {
loadAndDeleteImmutableHandle(addonID)
return err
}

addonWrapper.cPtr = bridge

return nil
}

// RegisterAddonAsExtensionGroup registers the addon as an extension group.
func RegisterAddonAsExtensionGroup(addonName string, instance Addon) error {
if len(addonName) == 0 {
return newTenError(
ErrnoInvalidArgument,
"addon name is empty",
)
}

_, file, _, ok := runtime.Caller(1)
if !ok {
return newTenError(ErrnoGeneric, "Failed to get the caller information")
}

baseDir := filepath.Dir(file)

absBaseDir, err := filepath.Abs(baseDir)
if err != nil {
return newTenError(
ErrnoGeneric,
fmt.Sprintf("Failed to get the absolute file path: %v",
err),
)
}

addonWrapper := &addon{
Addon: instance,
}

addonID := newImmutableHandle(addonWrapper)

var bridge C.uintptr_t
status := C.ten_go_addon_register_extension_group(
unsafe.Pointer(unsafe.StringData(addonName)),
C.int(len(addonName)),
unsafe.Pointer(unsafe.StringData(absBaseDir)),
C.int(len(absBaseDir)),
cHandle(addonID),
&bridge,
)

if err := withCGoError(&status); err != nil {
loadAndDeleteImmutableHandle(addonID)
return err
}

addonWrapper.cPtr = bridge

return nil
}

// unloadAllAddons unloads all addons.
func unloadAllAddons() error {
clearImmutableHandles(func(value any) {
if addon, ok := value.(*addon); ok {
C.ten_go_addon_unregister(addon.cPtr)
}
})

return nil
}

//export tenGoAddonOnInit
func tenGoAddonOnInit(
addonID C.uintptr_t,
Expand Down
5 changes: 3 additions & 2 deletions core/src/ten_runtime/binding/go/interface/ten/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ten_go_error_t ten_go_addon_register_extension(
const void *addon_name, int addon_name_len, const void *base_dir,
int base_dir_len, uintptr_t go_addon, uintptr_t *bridge_addr);

ten_go_error_t ten_go_addon_register_extension_group(
ten_go_error_t ten_go_addon_register_extension_v2(
const void *addon_name, int addon_name_len, const void *base_dir,
int base_dir_len, uintptr_t go_addon, uintptr_t *bridge_addr);
int base_dir_len, uintptr_t go_addon, uintptr_t *register_ctx,
uintptr_t *bridge_addr);
Loading

0 comments on commit 564e16d

Please sign in to comment.