Merge pull request #3450 from wangxiuwen/fix/audio-frame-copy-ownership

fix(runtime): make AudioFrame copies own their sample data
This commit is contained in:
zhifu gao
2026-08-03 13:51:15 +08:00
committed by GitHub
2 changed files with 31 additions and 1 deletions
+1
View File
@@ -24,6 +24,7 @@ class AudioFrame {
AudioFrame();
AudioFrame(int len);
AudioFrame(const AudioFrame &other);
AudioFrame& operator=(const AudioFrame &other);
AudioFrame(int start, int end, bool is_final);
~AudioFrame();
+30 -1
View File
@@ -163,11 +163,40 @@ AudioFrame::AudioFrame(int len) : len(len)
start = 0;
}
AudioFrame::AudioFrame(const AudioFrame &other)
: start(other.start), end(other.end), is_final(other.is_final), len(other.len),
global_start(other.global_start), global_end(other.global_end)
{
if (other.data != nullptr && len > 0) {
data = static_cast<float*>(malloc(sizeof(float) * len));
if (data != nullptr) {
memcpy(data, other.data, sizeof(float) * len);
}
}
}
AudioFrame& AudioFrame::operator=(const AudioFrame &other)
{
if (this == &other) {
return *this;
}
float* copied_data = nullptr;
if (other.data != nullptr && other.len > 0) {
copied_data = static_cast<float*>(malloc(sizeof(float) * other.len));
if (copied_data == nullptr) {
return *this;
}
memcpy(copied_data, other.data, sizeof(float) * other.len);
}
free(data);
start = other.start;
end = other.end;
len = other.len;
is_final = other.is_final;
data = copied_data;
len = other.len;
global_start = other.global_start;
global_end = other.global_end;
return *this;
}
AudioFrame::AudioFrame(int start, int end, bool is_final):start(start),end(end),is_final(is_final){
len = end - start;