From 6464aa091d25927ee2aad4f95293d228d637e5cc Mon Sep 17 00:00:00 2001 From: Duzy Chan Date: Fri, 14 Feb 2014 10:19:50 +0800 Subject: [PATCH] coordinate video playback with audio frame number --- include/AudioReaderSource.h | 2 ++ src/Qt/AudioPlaybackThread.cpp | 5 +++++ src/Qt/AudioPlaybackThread.h | 1 + src/Qt/PlayerPrivate.cpp | 10 +++++++++- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/AudioReaderSource.h b/include/AudioReaderSource.h index 9b69f7ed..e5756afb 100644 --- a/include/AudioReaderSource.h +++ b/include/AudioReaderSource.h @@ -112,6 +112,8 @@ namespace openshot const ReaderInfo & getReaderInfo() const { return reader->info; } tr1::shared_ptr getFrame() const { return frame; } + int getFramePosition() const { return frame_position; } + int getEstimatedFrame() const { return int(estimated_frame); } }; } diff --git a/src/Qt/AudioPlaybackThread.cpp b/src/Qt/AudioPlaybackThread.cpp index a30d6858..6e1bbd27 100644 --- a/src/Qt/AudioPlaybackThread.cpp +++ b/src/Qt/AudioPlaybackThread.cpp @@ -73,6 +73,11 @@ namespace openshot return tr1::shared_ptr(); } + int AudioPlaybackThread::getCurrentFramePosition() + { + return source ? source->getEstimatedFrame() : 0; + } + void AudioPlaybackThread::run() { // Init audio device diff --git a/src/Qt/AudioPlaybackThread.h b/src/Qt/AudioPlaybackThread.h index fb35b1ee..864aa167 100644 --- a/src/Qt/AudioPlaybackThread.h +++ b/src/Qt/AudioPlaybackThread.h @@ -51,6 +51,7 @@ namespace openshot void setReader(ReaderBase *reader); tr1::shared_ptr getFrame(); + int getCurrentFramePosition(); void run(); diff --git a/src/Qt/PlayerPrivate.cpp b/src/Qt/PlayerPrivate.cpp index 7ff89e8f..42f883b7 100644 --- a/src/Qt/PlayerPrivate.cpp +++ b/src/Qt/PlayerPrivate.cpp @@ -72,15 +72,23 @@ namespace openshot videoPlayback->frame = frame; videoPlayback->render.signal(); + position = audioPlayback->getCurrentFramePosition(); frame = getFrame(); videoPlayback->rendered.wait(); + int fd = position - audioPlayback->getCurrentFramePosition(); + const Time t2 = Time::getCurrentTime(); double ft = (1000.0 / reader->info.fps.ToDouble()); int64 d = t2.toMilliseconds() - t1.toMilliseconds(); int st = int(ft - d + 0.5); - if (0 < ft - d) sleep(st); + + st += fd * reader->info.fps.ToDouble(); + + if (0 < st) sleep(st); + //std::cout << "frametime: " << ft << " - " << d << " = " << st << std::endl; + std::cout << "drift: " << fd << std::endl; } if (videoPlayback->isThreadRunning()) videoPlayback->stopThread(-1);