summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/syncClockMPEG.cpp
blob: 53ae7e2dd7f0a43934525a3a1cb62e1462370c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
  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 <iostream>

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:"<<syncMode<<endl;
  }
  return true;
}


int SyncClockMPEG::syncVideo(double pts,double scr,
			     TimeStamp* earlyTime,
			     TimeStamp* waitTime) {
  int back;
  switch(syncMode) {
  case __SYNC_AUDIO:
    back=gowait(scr,pts,earlyTime,waitTime);
    return back;
    break;
  case __SYNC_NONE:  
    return true;
  default:
    cout << "syncMode not implemented"<<endl;
  } 
  return true;
}


void SyncClockMPEG::lockSyncClock() {
  abs_thread_mutex_lock(&changeMut);
  abs_thread_mutex_lock(&scrMut);
  abs_thread_mutex_unlock(&changeMut);
  
}


void SyncClockMPEG::unlockSyncClock() {
  abs_thread_mutex_unlock(&scrMut);
}


double SyncClockMPEG::getPTSTime(double* window) {
  lockSyncClock();
  double currentPTS;
  timeval_t current_time;
  timeval_t passed_time;
  TimeWrapper::gettimeofday(&current_time);

  a_Minus_b_Is_C(&current_time,&lastPTS_time,&passed_time);
  currentPTS=lastPTS+timeval2Double(&passed_time);
  *window=jitter+oldjitter;


  unlockSyncClock();
  return currentPTS;
}


void SyncClockMPEG::markLastPTSTime(double ,double pts) {
  double tmp;
  double expect_time=getPTSTime(&tmp);

  lockSyncClock();
  oldjitter=jitter;
  jitter=expect_time-pts;

  TimeWrapper::gettimeofday(&lastPTS_time);
  lastPTS=pts;


  unlockSyncClock();
  /*
  cout << "(audio) pts stream:"<<pts
       << " expect:"<<expect_time<<" jitter:"<<jitter<<endl;
  */

}


int SyncClockMPEG::a_Minus_b_Is_C(timeval_t* a,timeval_t* b,timeval_t* c){
  c->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:"<<pts
       << " pts expect:"<<currentPTS
       << " window:"<<window<<endl;
  */
  return false;
}
    

void SyncClockMPEG::printTime(timeval_t* a,char* text) {
  cout << text
       << "time(sec):"<<a->tv_sec 
       << "time(usec)"<<a->tv_usec<<endl;
}

void SyncClockMPEG::print(char* text) {
  cout << text
       << " lastPTS:"<<lastPTS
       << " lastSCR:"<<lastSCR
       << " jitter:"<<jitter;
  printTime(&lastPTS_time,(char*)"lastPTS_time");
  printTime(&lastSCR_time,(char*)"lastSCR_time");
}