coordinate video playback with audio frame number

This commit is contained in:
Duzy Chan
2014-02-14 10:19:50 +08:00
parent 436b0edd27
commit 6464aa091d
4 changed files with 17 additions and 1 deletions

View File

@@ -112,6 +112,8 @@ namespace openshot
const ReaderInfo & getReaderInfo() const { return reader->info; }
tr1::shared_ptr<Frame> getFrame() const { return frame; }
int getFramePosition() const { return frame_position; }
int getEstimatedFrame() const { return int(estimated_frame); }
};
}

View File

@@ -73,6 +73,11 @@ namespace openshot
return tr1::shared_ptr<Frame>();
}
int AudioPlaybackThread::getCurrentFramePosition()
{
return source ? source->getEstimatedFrame() : 0;
}
void AudioPlaybackThread::run()
{
// Init audio device

View File

@@ -51,6 +51,7 @@ namespace openshot
void setReader(ReaderBase *reader);
tr1::shared_ptr<Frame> getFrame();
int getCurrentFramePosition();
void run();

View File

@@ -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);