1背景
在11里分析到了用了qt自带的gl渲染,上一篇分析了yuv的渲染,但是从书上一般可以看到,视频帧和音频帧还是有个timer队列的,保证两边能同步,这一篇分析timer。
2 步骤
2.1 timer
在src/ui/HVideoWidget.cpp中有如下代码
void HVideoWidget::initConnect() {
timer = new QTimer(this);
timer->setTimerType(Qt::PreciseTimer);
connect(timer, SIGNAL(timeout()), this, SLOT(onTimerUpdate()));
上面的是初始化,然后这个是在start成功时调用的
void HVideoWidget::start() {
int ret = pImpl_player->start();
if (ret != 0) {
onOpenFailed();
}
else {
onOpenSucceed();
}
void HVideoWidget::onOpenSucceed() {
timer->start(1000 / (fps ? fps : pImpl_player->fps));
status = PLAY;
setAspectRatio(aspect_ratio);
从上面的代码可以看出来,timer的间隔就是每秒/fps,按正常速度触发时间。
再看前面timer触发的onTimerUpdate
void HVideoWidget::onTimerUpdate() {
if (pImpl_player == NULL) return;
if (pImpl_player->pop_frame(&videownd->last_frame) == 0) {
// update progress bar
if (toolbar->sldProgress->isVisible()) {
int progress = (videownd->last_frame.ts - pImpl_player->start_time) / 1000;
if (toolbar->sldProgress->value() != progress &&
!toolbar->sldProgress->isSliderDown()) {
toolbar->sldProgress->setValue(progress);
}
}
// update video frame
videownd->update();
}
}
只看最后的videownd->update();,之前13里提到的update事件在这里被触发了。