Skip to content

Commit

Permalink
optimization: don't seek to 0 the first time
Browse files Browse the repository at this point in the history
mitigates #105 but does not solve it.

After we open a file, we are at the beginning of the file.
So if we get a seek request for 0 and we haven't seeked yet,
we ignore it. This works around glitches caused by ffmpeg's
seeking algorithm.

However, it does not solve the case where the user seeks to
a previous item on the playlist and then a file which has already
been played comes up again.
  • Loading branch information
andrewrk committed Aug 29, 2015
1 parent 70fd295 commit b7c3d6c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct GrooveFilePrivate {
pthread_mutex_t seek_mutex;
int64_t seek_pos; // -1 if no seek request
int seek_flush; // whether the seek request wants us to flush the buffer
bool ever_seeked;

int eof;
double audio_clock; // position of the decode head
Expand Down
4 changes: 1 addition & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,9 @@ static void audio_callback(struct SoundIoOutStream *outstream,

int ret = groove_sink_buffer_get(sink, &p->audio_buf, 0);
if (ret == GROOVE_BUFFER_END) {
emit_event(p->eventq, GROOVE_EVENT_NOWPLAYING);
p->play_head = NULL;
p->play_pos = -1.0;
// emit the event after setting play head so user can check
// the play head
emit_event(p->eventq, GROOVE_EVENT_NOWPLAYING);
} else if (ret == GROOVE_BUFFER_YES) {
if (p->play_head != p->audio_buf->item)
emit_event(p->eventq, GROOVE_EVENT_NOWPLAYING);
Expand Down
19 changes: 11 additions & 8 deletions src/playlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,15 +560,18 @@ static int decode_one_frame(struct GroovePlaylist *playlist, struct GrooveFile *
// handle seek requests
pthread_mutex_lock(&f->seek_mutex);
if (f->seek_pos >= 0) {
int64_t seek_pos = f->seek_pos;
if (seek_pos == 0 && f->audio_st->start_time != AV_NOPTS_VALUE)
seek_pos = f->audio_st->start_time;
if (av_seek_frame(f->ic, f->audio_stream_index, seek_pos, 0) < 0) {
av_log(NULL, AV_LOG_ERROR, "%s: error while seeking\n", f->ic->filename);
} else if (f->seek_flush) {
every_sink_flush(playlist);
if (f->seek_pos != 0 || f->seek_flush || f->ever_seeked) {
int64_t seek_pos = f->seek_pos;
if (seek_pos == 0 && f->audio_st->start_time != AV_NOPTS_VALUE)
seek_pos = f->audio_st->start_time;
if (av_seek_frame(f->ic, f->audio_stream_index, seek_pos, 0) < 0) {
av_log(NULL, AV_LOG_ERROR, "%s: error while seeking\n", f->ic->filename);
} else if (f->seek_flush) {
every_sink_flush(playlist);
}
avcodec_flush_buffers(f->audio_st->codec);
}
avcodec_flush_buffers(f->audio_st->codec);
f->ever_seeked = true;
f->seek_pos = -1;
f->eof = 0;
}
Expand Down

0 comments on commit b7c3d6c

Please sign in to comment.