@@ -37,11 +37,11 @@ DEFINE_int32(xmp_default_volume, 70,
3737namespace xe {
3838namespace 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
226226void 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
429438void AudioMediaPlayer::AddPlaylist (uint32_t handle,
0 commit comments