Skip to content

Commit

Permalink
wip: timestamp properly saved, still doesn't seek on load
Browse files Browse the repository at this point in the history
  • Loading branch information
walkawayy committed Jul 20, 2023
1 parent 4518944 commit 4dd58d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/game/savegame/savegame_bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions src/specific/s_audio_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 4dd58d2

Please sign in to comment.