diff --git a/src/xenia/apu/audio_media_player.cc b/src/xenia/apu/audio_media_player.cc index 0a7f75b0a..f8451c03f 100644 --- a/src/xenia/apu/audio_media_player.cc +++ b/src/xenia/apu/audio_media_player.cc @@ -33,11 +33,11 @@ DEFINE_int32(xmp_default_volume, 70, namespace xe { namespace apu { -int32_t InitializeAndOpenAvCodec(std::vector* song_data, +int32_t InitializeAndOpenAvCodec(std::span song_data, AVFormatContext*& format_context, AVCodecContext*& av_context) { 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); format_context = avformat_alloc_context(); @@ -220,9 +220,8 @@ X_STATUS AudioMediaPlayer::Play(uint32_t playlist_handle, uint32_t song_handle, } void AudioMediaPlayer::Play() { - std::vector* song_buffer = new std::vector(); - - if (!LoadSongToMemory(song_buffer)) { + std::span song_buffer = LoadSongToMemory(); + if (song_buffer.empty()) { return; } @@ -396,9 +395,9 @@ X_STATUS AudioMediaPlayer::Previous() { return X_STATUS_SUCCESS; } -bool AudioMediaPlayer::LoadSongToMemory(std::vector* buffer) { +std::span AudioMediaPlayer::LoadSongToMemory() { if (!active_song_) { - return false; + return {}; } // Find file based on provided path? @@ -410,16 +409,26 @@ bool AudioMediaPlayer::LoadSongToMemory(std::vector* buffer) { &vfs_file, &file_action); if (result) { - return false; + return {}; } - buffer->resize(vfs_file->entry()->size()); - size_t bytes_read = 0; - result = vfs_file->ReadSync( - std::span(buffer->data(), vfs_file->entry()->size()), 0, - &bytes_read); + std::span buffer = { + static_cast(av_malloc(vfs_file->entry()->size())), + vfs_file->entry()->size()}; - 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, diff --git a/src/xenia/apu/audio_media_player.h b/src/xenia/apu/audio_media_player.h index 6d0745dd5..293b7f85a 100644 --- a/src/xenia/apu/audio_media_player.h +++ b/src/xenia/apu/audio_media_player.h @@ -99,7 +99,7 @@ class AudioMediaPlayer { void Play(); void WorkerThreadMain(); - bool LoadSongToMemory(std::vector* buffer); + std::span LoadSongToMemory(); XmpApp::State state_ = XmpApp::State::kIdle; XmpApp::PlaybackClient playback_client_ = XmpApp::PlaybackClient::kSystem;