Skip to content

Commit

Permalink
Adding displayPosition to AVPlayer
Browse files Browse the repository at this point in the history
This remembers the last seek pos of the player. Prevents the display position from jumping around wildly as we seek.
  • Loading branch information
mike-odom committed Oct 5, 2019
1 parent a59049c commit b1c4a6d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/AVDemuxThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 82,7 @@ AVDemuxThread::AVDemuxThread(QObject *parent) :
, audio_thread(0)
, video_thread(0)
, clock_type(-1)
, last_seek_pos(0)
{
seek_tasks.setCapacity(1);
seek_tasks.blockFull(false);
Expand All @@ -95,6 96,7 @@ AVDemuxThread::AVDemuxThread(AVDemuxer *dmx, QObject *parent) :
, m_buffer(0)
, audio_thread(0)
, video_thread(0)
, last_seek_pos(0)
{
setDemuxer(dmx);
seek_tasks.setCapacity(1);
Expand Down Expand Up @@ -278,6 280,7 @@ void AVDemuxThread::seekInternal(qint64 pos, SeekType type, qint64 external_pos)
if (external_pos != std::numeric_limits < qint64 >::min() )
t->clock()->updateExternalClock(qMax(qint64(0), external_pos));
t->clock()->updateValue(double(pos)/1000.0);
last_seek_pos = pos;
t->requestSeek();
// TODO: the first frame (key frame) will not be decoded correctly if flush() is called.
//PacketBuffer *pb = t->packetQueue();
Expand Down Expand Up @@ -323,6 326,11 @@ void AVDemuxThread::processNextSeekTask()
delete task;
}

qint64 AVDemuxThread::lastSeekPos()
{
return last_seek_pos;
}

void AVDemuxThread::pauseInternal(bool value)
{
paused = value;
Expand Down Expand Up @@ -486,6 494,10 @@ void AVDemuxThread::frameDeliveredOnStepForward()
clock_type = -1;
thread->clock()->updateExternalClock((thread->previousHistoryPts() - thread->clock()->initialValue())*1000.0);
}

// Fudge the bit at the end 33ms so that step forward and step backwards present different values
last_seek_pos = (thread->previousHistoryPts() - thread->clock()->initialValue())*1000.0 33;

Q_EMIT stepFinished();
}

Expand Down
1 change: 1 addition & 0 deletions src/AVDemuxThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 100,7 @@ private slots:
QMutex buffer_mutex;
QWaitCondition cond;
BlockingQueue<QRunnable*> seek_tasks;
qint64 last_seek_pos;

QSemaphore sem;
QMutex next_frame_mutex;
Expand Down
21 changes: 21 additions & 0 deletions src/AVPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 848,27 @@ qint64 AVPlayer::position() const
return pts;
}

qint64 AVPlayer::displayPosition() const
{
//return d->read_thread->lastSeekPos();

// Return a cached value if there are seek tasks
if (d->seeking || (d->read_thread->buffer() && d->read_thread->buffer()->isBuffering())) {
//return d->last_known_good_pts;
return d->last_known_good_pts = d->read_thread->lastSeekPos();
}

// TODO: videoTime()?
qint64 pts = d->clock->videoTime()*1000.0;

if (pts < 0) {
return d->last_known_good_pts;
}
d->last_known_good_pts = pts;

return pts;
}

void AVPlayer::setPosition(qint64 position)
{
// FIXME: strange things happen if seek out of eof
Expand Down
1 change: 1 addition & 0 deletions src/AVPlayerPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 111,7 @@ AVPlayer::Private::Private()
, status(NoMedia)
, state(AVPlayer::StoppedState)
, end_action(MediaEndAction_Default)
, last_known_good_pts(0)
{
demuxer.setInterruptTimeout(interrupt_timeout);
/*
Expand Down
1 change: 1 addition & 0 deletions src/AVPlayerPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 111,7 @@ class AVPlayer::Private
bool reset_state;
qint64 start_position, stop_position;
qint64 start_position_norm, stop_position_norm; // real position
qint64 last_known_good_pts;
int repeat_max, repeat_current;
int timer_id; //notify position change and check AB repeat range. active when playing

Expand Down
3 changes: 2 additions & 1 deletion src/QtAV/AVPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 178,8 @@ class Q_AV_EXPORT AVPlayer : public QObject
*/
qint64 stopPosition() const; //unit: ms
qint64 position() const; //unit: ms
//0: play once. N: play N 1 times. <0: infinity
qint64 displayPosition() const;
//0: play once. N: play N 1 times. <0: infinity
int repeat() const; //or repeatMax()?
/*!
* \brief currentRepeat
Expand Down

0 comments on commit b1c4a6d

Please sign in to comment.