From 4dd58d248e96dc3628c4737fc4a4547db12477fd Mon Sep 17 00:00:00 2001 From: walkawayy <81546780+walkawayy@users.noreply.github.com> Date: Wed, 19 Jul 2023 21:35:50 -0400 Subject: [PATCH] wip: timestamp properly saved, still doesn't seek on load --- src/game/savegame/savegame_bson.c | 3 +++ src/specific/s_audio_stream.c | 17 ++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/game/savegame/savegame_bson.c b/src/game/savegame/savegame_bson.c index 4178d45fd..8e3283bfe 100644 --- a/src/game/savegame/savegame_bson.c +++ b/src/game/savegame/savegame_bson.c @@ -1229,6 +1229,7 @@ bool Savegame_BSON_LoadFromFile(MYFILE *fp, GAME_INFO *game_info) LOG_DEBUG("music_timestamp2 %d", timestamp_arr[1]); int64_t load_timestamp = *(int64_t *)timestamp_arr; LOG_DEBUG("load_timestamp %d", load_timestamp); + LOG_DEBUG("load_track %d", load_track); if (load_track) { Music_Play(load_track); @@ -1311,8 +1312,10 @@ void Savegame_BSON_SaveToFile(MYFILE *fp, GAME_INFO *game_info) int64_t *timestamp_arr = (int64_t *)&save_timestamp; json_object_append_int(root_obj, "music_timestamp1", timestamp_arr[0]); json_object_append_int(root_obj, "music_timestamp2", timestamp_arr[1]); + LOG_DEBUG("save_timestamp %d", save_timestamp); LOG_DEBUG("music_timestamp1 %d", timestamp_arr[0]); LOG_DEBUG("music_timestamp2 %d", timestamp_arr[1]); + LOG_DEBUG("save_track %d", save_track); struct json_value_s *root = json_value_from_object(root_obj); SaveGame_BSON_SaveRaw(fp, root, SAVEGAME_CURRENT_VERSION); diff --git a/src/specific/s_audio_stream.c b/src/specific/s_audio_stream.c index b9ce5b9d4..03e771758 100644 --- a/src/specific/s_audio_stream.c +++ b/src/specific/s_audio_stream.c @@ -31,6 +31,7 @@ typedef struct AUDIO_STREAM_SOUND { bool is_read_done; bool is_looped; float volume; + int64_t timestamp; void (*finish_callback)(int sound_id, void *user_data); void *finish_callback_user_data; @@ -139,6 +140,8 @@ static bool S_Audio_StreamSoundEnqueueFrame(AUDIO_STREAM_SOUND *stream) break; } + stream->timestamp = stream->av.frame->best_effort_timestamp; + LOG_DEBUG("Enqueue timestamp: %d.", stream->timestamp); av_frame_unref(stream->av.frame); } @@ -240,6 +243,7 @@ static bool S_Audio_StreamSoundInitialiseFromPath( stream->is_playing = true; stream->is_looped = false; stream->volume = 1.0f; + stream->timestamp = 0; stream->finish_callback = NULL; stream->sdl.stream = SDL_NewAudioStream( @@ -277,6 +281,7 @@ void S_Audio_StreamSoundInit(void) stream->is_playing = false; stream->is_read_done = true; stream->volume = 0.0f; + stream->timestamp = 0; stream->sdl.stream = NULL; stream->finish_callback = NULL; } @@ -390,6 +395,7 @@ bool S_Audio_StreamSoundClose(int sound_id) stream->is_playing = false; stream->is_looped = false; stream->volume = 0.0f; + stream->timestamp = 0; SDL_UnlockAudioDevice(g_AudioDeviceID); if (stream->finish_callback) { @@ -528,13 +534,9 @@ int64_t S_Audio_StreamGetTimestamp(int sound_id) LOG_DEBUG("Getting timestamp for sound_id %d.", sound_id); SDL_LockAudioDevice(g_AudioDeviceID); AUDIO_STREAM_SOUND *stream = &m_StreamSounds[sound_id]; - int64_t timestamp = 0; - int ret = avcodec_receive_frame(stream->av.codec_ctx, stream->av.frame); - if (ret >= 0) { - timestamp = stream->av.frame->best_effort_timestamp; - } - SDL_UnlockAudioDevice(g_AudioDeviceID); + int64_t timestamp = stream->timestamp; LOG_DEBUG("Timestamp: %d.", timestamp); + SDL_UnlockAudioDevice(g_AudioDeviceID); return timestamp; } @@ -549,10 +551,11 @@ bool S_Audio_StreamSeekTimestamp(int sound_id, int64_t timestamp) } if (m_StreamSounds[sound_id].is_playing) { - LOG_DEBUG("Seeking timestamp on sound_id %d.", sound_id); + LOG_DEBUG("Seeking timestamp %d on sound_id %d.", timestamp, sound_id); SDL_LockAudioDevice(g_AudioDeviceID); AUDIO_STREAM_SOUND *stream = &m_StreamSounds[sound_id]; av_seek_frame(stream->av.format_ctx, -1, timestamp, AVSEEK_FLAG_ANY); + avcodec_flush_buffers(stream->av.codec_ctx); // ? SDL_UnlockAudioDevice(g_AudioDeviceID); return true; }