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 GetBuf to AudioFrame #20

Merged
merged 1 commit into from
Sep 17, 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
39 changes: 36 additions & 3 deletions core/src/ten_runtime/binding/go/interface/ten/audio_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,35 @@ const (
type AudioFrame interface {
Msg

AllocBuf(size int) error
LockBuf() ([]byte, error)
UnlockBuf(buf *[]byte) error
GetBuf() ([]byte, error)

SetTimestamp(timestamp int64) error
GetTimestamp() (int64, error)

SetSampleRate(sampleRate int32) error
GetSampleRate() (int32, error)

SetChannelLayout(channelLayout uint64) error
GetChannelLayout() (uint64, error)

SetSamplesPerChannel(samplesPerChannel int32) error
GetSamplesPerChannel() (int32, error)

SetBytesPerSample(bytesPerSample int32) error
GetBytesPerSample() (int32, error)

SetNumberOfChannels(numberOfChannels int32) error
GetNumberOfChannels() (int32, error)

SetDataFmt(dataFmt AudioFrameDataFmt) error
GetDataFmt() (AudioFrameDataFmt, error)

SetLineSize(lineSize int32) error
GetLineSize() (int32, error)
AllocBuf(size int) error
LockBuf() ([]byte, error)
UnlockBuf(buf *[]byte) error

IsEOF() (bool, error)
SetIsEOF(isEOF bool) error
}
Expand Down Expand Up @@ -333,6 +343,29 @@ func (p *audioFrame) AllocBuf(size int) error {
return err
}

func (p *audioFrame) GetBuf() ([]byte, error) {
if p.size == 0 {
return nil, newTenError(ErrnoInvalidArgument, "call AllocBuf() first")
}

buf := make([]byte, p.size)
err := withCGOLimiter(func() error {
apiStatus := C.ten_go_audio_frame_get_buf(
p.getCPtr(),
unsafe.Pointer(&buf[0]),
C.int(p.size),
)

return withGoStatus(&apiStatus)
})

if err != nil {
return nil, err
}

return buf, nil
}

func (p *audioFrame) LockBuf() ([]byte, error) {
var bufAddr *C.uint8_t
var bufSize C.uint64_t
Expand Down
3 changes: 3 additions & 0 deletions core/src/ten_runtime/binding/go/interface/ten/audio_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ ten_go_status_t ten_go_audio_frame_is_eof(uintptr_t bridge_addr, bool *is_eof);

ten_go_status_t ten_go_audio_frame_alloc_buf(uintptr_t bridge_addr, int size);

ten_go_status_t ten_go_audio_frame_get_buf(uintptr_t bridge_addr,
const void *buf_addr, int buf_size);

ten_go_status_t ten_go_audio_frame_lock_buf(uintptr_t bridge_addr,
uint8_t **buf_addr,
uint64_t *buf_size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ type VideoFrame interface {
LockBuf() ([]byte, error)
UnlockBuf(buf *[]byte) error
GetBuf() ([]byte, error)

SetWidth(width int32) error
GetWidth() (int32, error)

SetHeight(height int32) error
GetHeight() (int32, error)

SetTimestamp(timestamp int64) error
GetTimestamp() (int64, error)

IsEOF() (bool, error)
SetIsEOF(isEOF bool) error

GetPixelFmt() (PixelFmt, error)
SetPixelFmt(pixelFmt PixelFmt) error
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#include "include_internal/ten_runtime/binding/go/internal/common.h"
#include "include_internal/ten_runtime/binding/go/msg/msg.h"
#include "include_internal/ten_runtime/msg/msg.h"
#include "ten_utils/macro/check.h"
#include "ten_runtime/binding/go/interface/ten/msg.h"
#include "ten_runtime/common/errno.h"
#include "ten_runtime/msg/audio_frame/audio_frame.h"
#include "ten_runtime/msg/msg.h"
#include "ten_utils/lib/error.h"
#include "ten_utils/macro/check.h"

ten_go_status_t ten_go_audio_frame_create(const void *msg_name,
int msg_name_len,
Expand Down Expand Up @@ -402,3 +402,27 @@ ten_go_status_t ten_go_audio_frame_unlock_buf(uintptr_t bridge_addr,

return status;
}

ten_go_status_t ten_go_audio_frame_get_buf(uintptr_t bridge_addr,
const void *buf_addr, int buf_size) {
TEN_ASSERT(bridge_addr > 0 && buf_addr && buf_size > 0, "Invalid argument.");

ten_go_status_t status;
ten_go_status_init_with_errno(&status, TEN_ERRNO_OK);

ten_go_msg_t *audio_frame_bridge = ten_go_msg_reinterpret(bridge_addr);
TEN_ASSERT(
audio_frame_bridge && ten_go_msg_check_integrity(audio_frame_bridge),
"Invalid argument.");

ten_shared_ptr_t *c_audio_frame = ten_go_msg_c_msg(audio_frame_bridge);
uint64_t size = ten_audio_frame_peek_data(c_audio_frame)->size;
if (buf_size < size) {
ten_go_status_set(&status, TEN_ERRNO_GENERIC, "buffer is not enough");
} else {
ten_buf_t *data = ten_audio_frame_peek_data(c_audio_frame);
memcpy((void *)buf_addr, data->data, size);
}

return status;
}