Skip to content

Commit 2beb0bf

Browse files
Gliniakhas207
authored andcommitted
[XMP] Fixed error caused by incorrect audio allocation for FFMPEG
1 parent 266f2d4 commit 2beb0bf

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/xenia/apu/audio_media_player.cc

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ DEFINE_int32(xmp_default_volume, 70,
3737
namespace xe {
3838
namespace apu {
3939

40-
int32_t InitializeAndOpenAvCodec(std::vector<uint8_t>* song_data,
40+
int32_t InitializeAndOpenAvCodec(std::span<uint8_t> song_data,
4141
AVFormatContext*& format_context,
4242
AVCodecContext*& av_context) {
4343
AVIOContext* io_ctx =
44-
avio_alloc_context(song_data->data(), (int)song_data->size(), 0, nullptr,
44+
avio_alloc_context(song_data.data(), (int)song_data.size(), 0, nullptr,
4545
nullptr, nullptr, nullptr);
4646

4747
format_context = avformat_alloc_context();
@@ -224,9 +224,8 @@ X_STATUS AudioMediaPlayer::Play(uint32_t playlist_handle, uint32_t song_handle,
224224
}
225225

226226
void AudioMediaPlayer::Play() {
227-
std::vector<uint8_t>* song_buffer = new std::vector<uint8_t>();
228-
229-
if (!LoadSongToMemory(song_buffer)) {
227+
std::span<uint8_t> song_buffer = LoadSongToMemory();
228+
if (song_buffer.empty()) {
230229
return;
231230
}
232231

@@ -400,9 +399,9 @@ X_STATUS AudioMediaPlayer::Previous() {
400399
return X_STATUS_SUCCESS;
401400
}
402401

403-
bool AudioMediaPlayer::LoadSongToMemory(std::vector<uint8_t>* buffer) {
402+
std::span<uint8_t> AudioMediaPlayer::LoadSongToMemory() {
404403
if (!active_song_) {
405-
return false;
404+
return {};
406405
}
407406

408407
// Find file based on provided path?
@@ -414,16 +413,26 @@ bool AudioMediaPlayer::LoadSongToMemory(std::vector<uint8_t>* buffer) {
414413
&vfs_file, &file_action);
415414

416415
if (result) {
417-
return false;
416+
return {};
417+
}
418+
419+
std::span<uint8_t> buffer = {
420+
static_cast<uint8_t*>(av_malloc(vfs_file->entry()->size())),
421+
vfs_file->entry()->size()};
422+
423+
if (buffer.empty()) {
424+
return {};
418425
}
419426

420-
buffer->resize(vfs_file->entry()->size());
421427
size_t bytes_read = 0;
422-
result = vfs_file->ReadSync(
423-
std::span<uint8_t>(buffer->data(), vfs_file->entry()->size()), 0,
424-
&bytes_read);
428+
result = vfs_file->ReadSync(buffer, 0, &bytes_read);
429+
if (result != X_ERROR_SUCCESS) {
430+
// Read failed. We need to manually release resources from av_malloc.
431+
av_freep(buffer.data());
432+
return {};
433+
}
425434

426-
return !result;
435+
return buffer;
427436
}
428437

429438
void AudioMediaPlayer::AddPlaylist(uint32_t handle,

src/xenia/apu/audio_media_player.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class AudioMediaPlayer {
9999

100100
void Play();
101101
void WorkerThreadMain();
102-
bool LoadSongToMemory(std::vector<uint8_t>* buffer);
102+
std::span<uint8_t> LoadSongToMemory();
103103

104104
XmpApp::State state_ = XmpApp::State::kIdle;
105105
XmpApp::PlaybackClient playback_client_ = XmpApp::PlaybackClient::kSystem;

0 commit comments

Comments
 (0)