Skip to content

Commit 934bd18

Browse files
committed
feat: add startGraphCmd in go binding
1 parent dca5367 commit 934bd18

File tree

24 files changed

+1104
-0
lines changed

24 files changed

+1104
-0
lines changed

core/src/ten_runtime/binding/go/interface/ten/cmd.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ type Cmd interface {
2626
Clone() (Cmd, error)
2727
}
2828

29+
type StartGraphCmd interface {
30+
Cmd
31+
32+
SetPredefinedGraphName(predefinedGraphName string) error
33+
SetGraphFromJSONBytes(graphJsonBytes []byte) error
34+
SetLongRunningMode(longRunningMode bool) error
35+
}
36+
2937
// NewCmd creates a custom cmd which is intended to be sent to another
3038
// extension using tenEnv.SendCmd().
3139
func NewCmd(cmdName string) (Cmd, error) {
@@ -106,6 +114,23 @@ func NewCmd(cmdName string) (Cmd, error) {
106114
return newCmd(bridge), nil
107115
}
108116

117+
func NewStartGraphCmd() (StartGraphCmd, error) {
118+
var bridge C.uintptr_t
119+
err := withCGOLimiter(func() error {
120+
cStatus := C.ten_go_cmd_create_start_graph_cmd(
121+
&bridge,
122+
)
123+
e := withCGoError(&cStatus)
124+
125+
return e
126+
})
127+
if err != nil {
128+
return nil, err
129+
}
130+
131+
return newStartGraphCmd(bridge), nil
132+
}
133+
109134
type cmd struct {
110135
*msg
111136
}
@@ -116,6 +141,16 @@ func newCmd(bridge C.uintptr_t) *cmd {
116141
}
117142
}
118143

144+
type startGraphCmd struct {
145+
*cmd
146+
}
147+
148+
func newStartGraphCmd(bridge C.uintptr_t) *startGraphCmd {
149+
return &startGraphCmd{
150+
cmd: newCmd(bridge),
151+
}
152+
}
153+
119154
func (p *cmd) Clone() (Cmd, error) {
120155
var bridge C.uintptr_t
121156
err := withCGOLimiter(func() error {
@@ -141,4 +176,49 @@ func (p *cmd) Clone() (Cmd, error) {
141176
return newCmd(bridge), nil
142177
}
143178

179+
func (p *startGraphCmd) SetPredefinedGraphName(predefinedGraphName string) error {
180+
defer p.keepAlive()
181+
182+
err := withCGOLimiter(func() error {
183+
apiStatus := C.ten_go_cmd_start_graph_set_predefined_graph_name(
184+
p.getCPtr(),
185+
unsafe.Pointer(unsafe.StringData(predefinedGraphName)),
186+
C.int(len(predefinedGraphName)),
187+
)
188+
return withCGoError(&apiStatus)
189+
})
190+
191+
return err
192+
}
193+
194+
func (p *startGraphCmd) SetGraphFromJSONBytes(graphJsonBytes []byte) error {
195+
defer p.keepAlive()
196+
197+
err := withCGOLimiter(func() error {
198+
apiStatus := C.ten_go_cmd_start_graph_set_graph_from_json_bytes(
199+
p.getCPtr(),
200+
unsafe.Pointer(unsafe.SliceData(graphJsonBytes)),
201+
C.int(len(graphJsonBytes)),
202+
)
203+
return withCGoError(&apiStatus)
204+
})
205+
206+
return err
207+
}
208+
209+
func (p *startGraphCmd) SetLongRunningMode(longRunningMode bool) error {
210+
defer p.keepAlive()
211+
212+
err := withCGOLimiter(func() error {
213+
apiStatus := C.ten_go_cmd_start_graph_set_long_running_mode(
214+
p.getCPtr(),
215+
C.bool(longRunningMode),
216+
)
217+
return withCGoError(&apiStatus)
218+
})
219+
220+
return err
221+
}
222+
144223
var _ Cmd = new(cmd)
224+
var _ StartGraphCmd = new(startGraphCmd)

core/src/ten_runtime/binding/go/interface/ten/cmd.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,15 @@ ten_go_handle_t ten_go_cmd_result_get_detail(uintptr_t bridge_addr);
5454

5555
ten_go_error_t ten_go_cmd_result_get_detail_json_and_size(
5656
uintptr_t bridge_addr, uintptr_t *json_str_len, const char **json_str);
57+
58+
ten_go_error_t ten_go_cmd_create_start_graph_cmd(uintptr_t *bridge);
59+
60+
ten_go_error_t ten_go_cmd_start_graph_set_predefined_graph_name(
61+
uintptr_t bridge_addr, const void *predefined_graph_name,
62+
int predefined_graph_name_len);
63+
64+
ten_go_error_t ten_go_cmd_start_graph_set_graph_from_json_bytes(
65+
uintptr_t bridge_addr, const void *json_bytes, int json_bytes_len);
66+
67+
ten_go_error_t ten_go_cmd_start_graph_set_long_running_mode(
68+
uintptr_t bridge_addr, bool long_running_mode);

core/src/ten_runtime/binding/go/native/msg/cmd/cmd.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "ten_runtime/binding/go/interface/ten/msg.h"
1515
#include "ten_runtime/common/status_code.h"
1616
#include "ten_runtime/msg/cmd/cmd.h"
17+
#include "ten_runtime/msg/cmd/start_graph/cmd.h"
1718
#include "ten_runtime/msg/cmd_result/cmd_result.h"
1819
#include "ten_utils/lib/error.h"
1920
#include "ten_utils/lib/smart_ptr.h"
@@ -213,3 +214,105 @@ ten_go_error_t ten_go_cmd_result_clone(uintptr_t bridge_addr,
213214

214215
return cgo_error;
215216
}
217+
218+
ten_go_error_t ten_go_cmd_create_start_graph_cmd(uintptr_t *bridge) {
219+
ten_go_error_t cgo_error;
220+
ten_go_error_init_with_error_code(&cgo_error, TEN_ERROR_CODE_OK);
221+
222+
ten_shared_ptr_t *c_cmd = ten_cmd_start_graph_create();
223+
TEN_ASSERT(c_cmd && ten_cmd_check_integrity(c_cmd), "Should not happen.");
224+
225+
ten_go_msg_t *msg_bridge = ten_go_msg_create(c_cmd);
226+
TEN_ASSERT(msg_bridge, "Should not happen.");
227+
228+
*bridge = (uintptr_t)msg_bridge;
229+
ten_shared_ptr_destroy(c_cmd);
230+
231+
return cgo_error;
232+
}
233+
234+
ten_go_error_t ten_go_cmd_start_graph_set_predefined_graph_name(
235+
uintptr_t bridge_addr, const void *predefined_graph_name,
236+
int predefined_graph_name_len) {
237+
ten_go_msg_t *msg_bridge = ten_go_msg_reinterpret(bridge_addr);
238+
TEN_ASSERT(msg_bridge && ten_go_msg_check_integrity(msg_bridge),
239+
"Should not happen.");
240+
241+
ten_go_error_t cgo_error;
242+
ten_go_error_init_with_error_code(&cgo_error, TEN_ERROR_CODE_OK);
243+
244+
ten_string_t predefined_graph_name_str;
245+
ten_string_init_from_c_str_with_size(&predefined_graph_name_str,
246+
predefined_graph_name,
247+
predefined_graph_name_len);
248+
249+
ten_error_t err;
250+
TEN_ERROR_INIT(err);
251+
252+
bool success = ten_cmd_start_graph_set_predefined_graph_name(
253+
ten_go_msg_c_msg(msg_bridge),
254+
ten_string_get_raw_str(&predefined_graph_name_str), &err);
255+
256+
if (!success) {
257+
ten_go_error_set(&cgo_error, ten_error_code(&err), ten_error_message(&err));
258+
}
259+
260+
ten_error_deinit(&err);
261+
ten_string_deinit(&predefined_graph_name_str);
262+
263+
return cgo_error;
264+
}
265+
266+
ten_go_error_t ten_go_cmd_start_graph_set_graph_from_json_bytes(
267+
uintptr_t bridge_addr, const void *json_bytes, int json_bytes_len) {
268+
ten_go_msg_t *msg_bridge = ten_go_msg_reinterpret(bridge_addr);
269+
TEN_ASSERT(msg_bridge && ten_go_msg_check_integrity(msg_bridge),
270+
"Should not happen.");
271+
272+
ten_go_error_t cgo_error;
273+
ten_go_error_init_with_error_code(&cgo_error, TEN_ERROR_CODE_OK);
274+
275+
ten_string_t json_str_str;
276+
ten_string_init_from_c_str_with_size(&json_str_str, json_bytes,
277+
json_bytes_len);
278+
279+
ten_error_t err;
280+
TEN_ERROR_INIT(err);
281+
282+
bool success = ten_cmd_start_graph_set_graph_from_json_str(
283+
ten_go_msg_c_msg(msg_bridge), ten_string_get_raw_str(&json_str_str),
284+
&err);
285+
286+
if (!success) {
287+
ten_go_error_set(&cgo_error, ten_error_code(&err), ten_error_message(&err));
288+
}
289+
290+
ten_error_deinit(&err);
291+
ten_string_deinit(&json_str_str);
292+
293+
return cgo_error;
294+
}
295+
296+
ten_go_error_t ten_go_cmd_start_graph_set_long_running_mode(
297+
uintptr_t bridge_addr, bool long_running_mode) {
298+
ten_go_msg_t *msg_bridge = ten_go_msg_reinterpret(bridge_addr);
299+
TEN_ASSERT(msg_bridge && ten_go_msg_check_integrity(msg_bridge),
300+
"Should not happen.");
301+
302+
ten_go_error_t cgo_error;
303+
ten_go_error_init_with_error_code(&cgo_error, TEN_ERROR_CODE_OK);
304+
305+
ten_error_t err;
306+
TEN_ERROR_INIT(err);
307+
308+
bool success = ten_cmd_start_graph_set_long_running_mode(
309+
ten_go_msg_c_msg(msg_bridge), long_running_mode, &err);
310+
311+
if (!success) {
312+
ten_go_error_set(&cgo_error, ten_error_code(&err), ten_error_message(&err));
313+
}
314+
315+
ten_error_deinit(&err);
316+
317+
return cgo_error;
318+
}

tests/ten_runtime/integration/go/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ group("go") {
3232
"send_data_go",
3333
"send_video_frame_go",
3434
"start_app_sync_go",
35+
"start_graph_go",
36+
"start_predefined_graph_go",
3537
"three_extension_cmd_go",
3638
"transfer_pointer_go",
3739
"two_extension_one_group_cmd_go",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#
2+
# Copyright © 2025 Agora
3+
# This file is part of TEN Framework, an open source project.
4+
# Licensed under the Apache License, Version 2.0, with certain conditions.
5+
# Refer to the "LICENSE" file in the root directory for more information.
6+
#
7+
import("//build/ten_runtime/feature/test.gni")
8+
import("//build/ten_runtime/ten.gni")
9+
10+
ten_package_test_prepare_app("start_graph_go_app") {
11+
src_app = "default_app_go"
12+
src_app_language = "go"
13+
generated_app_src_root_dir_name = "start_graph_go_app"
14+
15+
replace_paths_after_install_app = [
16+
"start_graph_go_app/manifest.json",
17+
"start_graph_go_app/property.json",
18+
"start_graph_go_app/ten_packages/extension/default_extension_go/extension.go",
19+
"start_graph_go_app/ten_packages/extension/default_extension_go/go.mod",
20+
"start_graph_go_app/ten_packages/extension/default_extension_go/manifest.json",
21+
"start_graph_go_app/ten_packages/extension/default_extension_go/property.json",
22+
]
23+
24+
if (ten_enable_go_app_leak_check) {
25+
replace_paths_after_install_app += [ "start_graph_go_app/main.go" ]
26+
}
27+
28+
deps = [
29+
"//core/src/ten_manager",
30+
"//packages/core_apps/default_app_go:upload_default_app_go_to_server",
31+
"//packages/core_extensions/default_extension_go:upload_default_extension_go_to_server",
32+
"//packages/example_extensions/simple_http_server_cpp:upload_simple_http_server_cpp_to_server",
33+
]
34+
}
35+
36+
ten_package_test_prepare_auxiliary_resources("start_graph_go_app_test_files") {
37+
resources = [
38+
"__init__.py",
39+
"test_case.py",
40+
]
41+
42+
common_files =
43+
exec_script("//.gnfiles/build/scripts/glob_file.py",
44+
[
45+
"--dir",
46+
rebase_path("//tests/ten_runtime/integration/common/**/*"),
47+
"--dir-base",
48+
rebase_path("//tests/ten_runtime/integration/common"),
49+
"--recursive",
50+
"--only-output-file",
51+
],
52+
"json")
53+
54+
foreach(common_file, common_files) {
55+
common_file_rel_path = common_file.relative_path
56+
resources += [ "//tests/ten_runtime/integration/common/${common_file_rel_path}=>common/${common_file_rel_path}" ]
57+
}
58+
}
59+
60+
group("start_graph_go") {
61+
deps = [
62+
":start_graph_go_app",
63+
":start_graph_go_app_test_files",
64+
]
65+
}

tests/ten_runtime/integration/go/start_graph_go/__init__.py

Whitespace-only changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// This file is part of TEN Framework, an open source project.
3+
// Licensed under the Apache License, Version 2.0.
4+
// See the LICENSE file for more information.
5+
//
6+
7+
package main
8+
9+
// void __lsan_do_leak_check(void);
10+
import "C"
11+
12+
import (
13+
"fmt"
14+
"runtime"
15+
"runtime/debug"
16+
"time"
17+
18+
"ten_framework/ten"
19+
)
20+
21+
type defaultApp struct {
22+
ten.DefaultApp
23+
}
24+
25+
func (p *defaultApp) OnInit(tenEnv ten.TenEnv) {
26+
tenEnv.LogDebug("onInit")
27+
tenEnv.OnInitDone()
28+
}
29+
30+
func (p *defaultApp) OnDeinit(tenEnv ten.TenEnv) {
31+
tenEnv.LogDebug("onDeinit")
32+
tenEnv.OnDeinitDone()
33+
}
34+
35+
func main() {
36+
// test app
37+
app, err := ten.NewApp(&defaultApp{})
38+
if err != nil {
39+
fmt.Println("Failed to create app.")
40+
}
41+
42+
app.Run(true)
43+
app.Wait()
44+
45+
// A single GC is not enough; multiple rounds of GC are needed to clean up
46+
// as thoroughly as possible.
47+
for i := 0; i < 10; i++ {
48+
// Explicitly trigger GC to increase the likelihood of finalizer
49+
// execution.
50+
debug.FreeOSMemory()
51+
runtime.GC()
52+
53+
// Wait for a short period to give the GC time to run.
54+
runtime.Gosched()
55+
time.Sleep(1 * time.Second)
56+
}
57+
58+
// To detect memory leaks with ASan, need to enable the following cgo code.
59+
C.__lsan_do_leak_check()
60+
}

0 commit comments

Comments
 (0)