From 844ba7978c8b458a0061ba1cbcd8fd49e95ee025 Mon Sep 17 00:00:00 2001 From: wang-bin Date: Mon, 30 May 2016 23:20:29 +0800 Subject: [PATCH] AVPlayer: fix normalized position. remove dead code --- src/AVPlayer.cpp | 29 ++++++++++++----------------- src/AVPlayerPrivate.cpp | 1 - src/AVPlayerPrivate.h | 7 ------- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/AVPlayer.cpp b/src/AVPlayer.cpp index 176bd5ff..2b1d4532 100644 --- a/src/AVPlayer.cpp +++ b/src/AVPlayer.cpp @@ -766,13 +766,20 @@ qint64 AVPlayer::normalizedPosition(qint64 pos) { if (!isLoaded()) return pos; + qint64 p0 = mediaStartPosition(); + qint64 p1 = mediaStopPosition(); + if (relativeTimeMode()) { + p0 = 0; + if (p1 != kInvalidPosition) + p1 -= p0; //duration + } if (pos < 0) { - if (mediaStopPosition() == kInvalidPosition) + if (p1 == kInvalidPosition) pos = kInvalidPosition; else - pos += mediaStopPosition(); + pos += p1; } - return qMax(qMin(pos, mediaStopPosition()), mediaStartPosition()); + return qMax(qMin(pos, p1), p0); } qint64 AVPlayer::startPosition() const @@ -1180,11 +1187,7 @@ void AVPlayer::playInternal() return; } // setup clock before avthread.start() becuase avthreads use clock. after avthreads setup because of ao check - if (d->last_position > 0) {//start_last) { - masterClock()->pause(false); //external clock - } else { - masterClock()->reset(); - } + masterClock()->reset(); // TODO: add isVideo() or hasVideo()? qreal vfps = d->force_fps; bool force_fps = vfps > 0; @@ -1227,7 +1230,7 @@ void AVPlayer::playInternal() qDebug("Starting video thread..."); d->vthread->start(); } - if (d->start_position_norm > 0 && d->last_position <= 0) { + if (d->start_position_norm > 0) { if (relativeTimeMode()) d->demuxer.seek(qint64((d->start_position_norm + absoluteMediaStartPosition()))); else @@ -1246,12 +1249,6 @@ void AVPlayer::playInternal() //d->timer_id = startNotifyTimer(); //may fail if not in this thread QMetaObject::invokeMethod(this, "startNotifyTimer", Qt::AutoConnection); } -// ffplay does not seek to stream's start position. usually it's 0, maybe < 1. seeking will result in a non-key frame position and it's bad. - //if (d->last_position <= 0) - // d->last_position = mediaStartPosition(); - if (d->last_position > 0) - setPosition(d->last_position); //just use d->demuxer.startTime()/duration()? - d->state = PlayingState; } //end lock scoped here to avoid dead lock if connect started() to a slot that call unload()/play() Q_EMIT stateChanged(PlayingState); @@ -1287,7 +1284,6 @@ void AVPlayer::stopFromDemuxerThread() unload(); //TODO: invoke? } else { d->repeat_current++; - d->last_position = d->start_position_norm; // for seeking to d->start_position_norm if seekable. already set in stop() QMetaObject::invokeMethod(this, "play"); //ensure play() is called from player thread } } @@ -1383,7 +1379,6 @@ void AVPlayer::tryClearVideoRenderers() void AVPlayer::stop() { - d->last_position = mediaStopPosition() != kInvalidPosition ? d->start_position_norm : 0; // check d->timer_id, <0 return? if (d->reset_state) { /* diff --git a/src/AVPlayerPrivate.cpp b/src/AVPlayerPrivate.cpp index 16161ece..208c8f30 100644 --- a/src/AVPlayerPrivate.cpp +++ b/src/AVPlayerPrivate.cpp @@ -70,7 +70,6 @@ AVPlayer::Private::Private() , relative_time_mode(true) , media_start_pts(0) , media_end(kInvalidPosition) - , last_position(0) , reset_state(true) , start_position(0) , stop_position(kInvalidPosition) diff --git a/src/AVPlayerPrivate.h b/src/AVPlayerPrivate.h index 8bf1e435..41f7bcd2 100644 --- a/src/AVPlayerPrivate.h +++ b/src/AVPlayerPrivate.h @@ -107,13 +107,6 @@ public: bool relative_time_mode; qint64 media_start_pts; // read from media stream qint64 media_end; - /* - * unit: s. 0~1. stream's start time/duration(). or last position/duration() if change to new stream - * auto set to 0 if stop(). to stream start time if load() - * - * -1: used by play() to get current playing position - */ - qint64 last_position; //last_pos bool reset_state; qint64 start_position, stop_position; qint64 start_position_norm, stop_position_norm; // real position