/* synchronisation of audio/video (PTS) against system clock stamps (SCR) Copyright (C) 2000 Martin Vogt This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation. For more information look at the file COPYRIGHT in this package */ #include "syncClockMPEG.h" #include using namespace std; SyncClockMPEG::SyncClockMPEG() { syncMode=__SYNC_NONE; abs_thread_mutex_init(&scrMut); abs_thread_mutex_init(&changeMut); lastSCR=0.0; lastPTS=0.0; jitter=0.0; oldjitter=0.0; TimeWrapper::gettimeofday(&lastPTS_time); } SyncClockMPEG::~SyncClockMPEG() { abs_thread_mutex_destroy(&changeMut); abs_thread_mutex_destroy(&scrMut); } int SyncClockMPEG::getSyncMode() { return syncMode; } void SyncClockMPEG::setSyncMode(int syncMode) { this->syncMode=syncMode; } int SyncClockMPEG::syncAudio(double pts,double scr) { switch(syncMode) { case __SYNC_AUDIO: markLastPTSTime(scr,pts); break; default: cout << "syncMode not implemented:"<tv_usec=a->tv_usec; c->tv_sec=a->tv_sec; c->tv_usec-=b->tv_usec; c->tv_sec-=b->tv_sec; if (c->tv_usec <= 0) { c->tv_usec=c->tv_usec+1000000; c->tv_sec--; } if (c->tv_usec >= 1000000) { c->tv_usec=c->tv_usec-1000000; c->tv_sec++; } return true; } double SyncClockMPEG::timeval2Double(timeval_t* a) { return (double)a->tv_sec+(double)(a->tv_usec)/1000000.0; } void SyncClockMPEG::double2Timeval(double a,timeval_t* dest) { dest->tv_sec=(int)a; dest->tv_usec=(int)(1000000.0*(double)(a-(double)dest->tv_sec)); } int SyncClockMPEG::gowait(double ,double pts, TimeStamp* earlyTime,TimeStamp* ) { double window; double currentPTS=getPTSTime(&window); double diff_time; // in window we have the jitter // diff_time is positive when we are in the future diff_time=pts-(currentPTS+window); // tolerate one frame (for 25 frames/s) in the future // but wait a bit if (diff_time > 0.0) { diff_time=diff_time/4.0; double2Timeval(diff_time,earlyTime->getTime()); if (diff_time > 1) { // cannot be, we assume stange clock error earlyTime->set(1,0); } return true; } earlyTime->set(0,0); // one frame late, display it, without waiting // and hope we catch up if (diff_time > -0.04) { return true; } /* cout << "(video) pts stream:"<tv_sec << "time(usec)"<tv_usec<