[XMP] Fixed error caused by incorrect audio allocation for FFMPEG

This commit is contained in:
Gliniak
2026-03-06 20:48:25 +01:00
parent 6446150a5e
commit 4b0007bfe5
2 changed files with 24 additions and 15 deletions

View File

@@ -33,11 +33,11 @@ DEFINE_int32(xmp_default_volume, 70,
namespace xe { namespace xe {
namespace apu { namespace apu {
int32_t InitializeAndOpenAvCodec(std::vector<uint8_t>* song_data, int32_t InitializeAndOpenAvCodec(std::span<uint8_t> song_data,
AVFormatContext*& format_context, AVFormatContext*& format_context,
AVCodecContext*& av_context) { AVCodecContext*& av_context) {
AVIOContext* io_ctx = AVIOContext* io_ctx =
avio_alloc_context(song_data->data(), (int)song_data->size(), 0, nullptr, avio_alloc_context(song_data.data(), (int)song_data.size(), 0, nullptr,
nullptr, nullptr, nullptr); nullptr, nullptr, nullptr);
format_context = avformat_alloc_context(); format_context = avformat_alloc_context();
@@ -220,9 +220,8 @@ X_STATUS AudioMediaPlayer::Play(uint32_t playlist_handle, uint32_t song_handle,
} }
void AudioMediaPlayer::Play() { void AudioMediaPlayer::Play() {
std::vector<uint8_t>* song_buffer = new std::vector<uint8_t>(); std::span<uint8_t> song_buffer = LoadSongToMemory();
if (song_buffer.empty()) {
if (!LoadSongToMemory(song_buffer)) {
return; return;
} }
@@ -396,9 +395,9 @@ X_STATUS AudioMediaPlayer::Previous() {
return X_STATUS_SUCCESS; return X_STATUS_SUCCESS;
} }
bool AudioMediaPlayer::LoadSongToMemory(std::vector<uint8_t>* buffer) { std::span<uint8_t> AudioMediaPlayer::LoadSongToMemory() {
if (!active_song_) { if (!active_song_) {
return false; return {};
} }
// Find file based on provided path? // Find file based on provided path?
@@ -410,16 +409,26 @@ bool AudioMediaPlayer::LoadSongToMemory(std::vector<uint8_t>* buffer) {
&vfs_file, &file_action); &vfs_file, &file_action);
if (result) { if (result) {
return false; return {};
} }
buffer->resize(vfs_file->entry()->size()); std::span<uint8_t> buffer = {
size_t bytes_read = 0; static_cast<uint8_t*>(av_malloc(vfs_file->entry()->size())),
result = vfs_file->ReadSync( vfs_file->entry()->size()};
std::span<uint8_t>(buffer->data(), vfs_file->entry()->size()), 0,
&bytes_read);
return !result; if (buffer.empty()) {
return {};
}
size_t bytes_read = 0;
result = vfs_file->ReadSync(buffer, 0, &bytes_read);
if (result != X_ERROR_SUCCESS) {
// Read failed. We need to manually release resources from av_malloc.
av_freep(buffer.data());
return {};
}
return buffer;
} }
void AudioMediaPlayer::AddPlaylist(uint32_t handle, void AudioMediaPlayer::AddPlaylist(uint32_t handle,

View File

@@ -99,7 +99,7 @@ class AudioMediaPlayer {
void Play(); void Play();
void WorkerThreadMain(); void WorkerThreadMain();
bool LoadSongToMemory(std::vector<uint8_t>* buffer); std::span<uint8_t> LoadSongToMemory();
XmpApp::State state_ = XmpApp::State::kIdle; XmpApp::State state_ = XmpApp::State::kIdle;
XmpApp::PlaybackClient playback_client_ = XmpApp::PlaybackClient::kSystem; XmpApp::PlaybackClient playback_client_ = XmpApp::PlaybackClient::kSystem;