summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/output/avSyncer.cpp
blob: ca12a21e05b52aecc61302a46d0b343b3420f197 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
  encapsulates the syncing methods, to use it in other classes
  Copyright (C) 1999  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 "avSyncer.h"

#include "audioDataArray.h"
#include "performance.h"

#include <iostream>

using namespace std;

AVSyncer::AVSyncer(int bufferSize) {
  oneFrameTime=0;
  onePicFrameInAudioBytes=8192;
  this->bufferSize=bufferSize;

  abs_thread_mutex_init(&writeInMut);
  abs_thread_mutex_init(&changeMut);

  
  audioDataInsert=new AudioData();
  audioDataArray=new AudioDataArray(400);
  audioDataCurrent=audioDataArray->readAudioData();
  
  startAudio=new TimeStamp();
  endAudio=new TimeStamp();
  audioTime=new AudioTime();
  lAudioRunning=false;
  performance=new Performance();

  waitTime=new TimeStamp();
  diffTime=new TimeStamp();
  videoTimeStamp=new TimeStamp();

  lPerformance=false;
  lavSync=true;


}


AVSyncer::~AVSyncer() {
  delete audioDataArray;
  delete audioDataInsert;
  delete audioTime;
  delete startAudio;
  delete endAudio;
  abs_thread_mutex_destroy(&writeInMut);
  abs_thread_mutex_destroy(&changeMut);
  delete waitTime;
  delete diffTime;
  delete performance;
  delete videoTimeStamp;
}



int AVSyncer::audioSetup(int frequency,int stereo,int sign,
			 int big,int sixteen) {
  audioTime->setFormat(stereo,sixteen,frequency,sign,big);
  setAudioRunning(true);
  return true;
}


int AVSyncer::audioPlay(TimeStamp* startStamp,
			TimeStamp* endStamp,char* , int size) {




  audioDataInsert->setStart(startStamp);
  audioDataInsert->setEnd(endStamp);
  audioDataInsert->setAudioTime(audioTime);
  audioDataInsert->setPCMLen(size);

  setAudioSync(audioDataInsert);
  return size;
}



void AVSyncer::audioClose(void) {
  lockSyncData();
  setAudioRunning(false);
  audioDataArray->clear();
  unlockSyncData();
}





int AVSyncer::getPreferredDeliverSize() {
  return onePicFrameInAudioBytes;
}



int AVSyncer::getFrameusec() {
  lockSyncData();
  int back=oneFrameTime;
  unlockSyncData();
  return back; 
}

void AVSyncer::setAudioBufferSize(int size) {
  bufferSize=size;
}


void AVSyncer::config(const char* key,const char* value,void*) {
  if (strcmp(key,"-s")==0) {
    if (strcmp(value,"on")==0) {
      lavSync=true;
      cout << "******** lavSync on"<<endl;
    } else {
      lavSync=false;
      cout << "******** lavSync off"<<endl;
    }
  }
  if (strcmp(key,"-p")==0) {
    cout << "setting perfomance test true"<<endl;
    lPerformance=true;
  }  

}



void AVSyncer::setAudioSync(AudioData* audioData) {

  lockSyncData();

  if (onePicFrameInAudioBytes <= 0) {
    // no video present, we cannot calculate audio
    // pcm length
    //cout << "no video present, we cannot calculate audio pcm length"<<endl;
    unlockSyncData();    
    return;
  }
  // buffersize is the simulated size of /dev/dsp



  // we implement a fifo hopefully
  // we check the right audio pts time out
  // when /dev/dsp acutally plays it.
  audioDataArray->insertAudioData(audioData);
  int pcmSum=audioDataArray->getPCMSum();
  if (pcmSum >= bufferSize) {
    audioDataCurrent=audioDataArray->readAudioData();
    setAudioRunning(true);
    audioDataArray->forward();
  }
  TimeStamp* startAudio=audioDataCurrent->getStart();
  int lpts=startAudio->getPTSFlag();

  if (lpts==true) {
    SyncClock* syncClock=startAudio->getSyncClock();
    if (syncClock != NULL) {
      double pts=startAudio->getPTSTimeStamp();
      double scr=startAudio->getSCRTimeStamp();

      syncClock->syncAudio(pts,scr);

    } else {
      cout <<"syncClock == NULL (audio)"<<endl;
    }
  } else {
    //cout << "lpts is false"<<endl;
  }
  unlockSyncData();    
  
}



int AVSyncer::syncPicture(YUVPicture* syncPic) {
  if (syncPic == NULL) {
    cout << "syncPic == NULL"<<endl;
    return false;
  }

  float picPerSec=syncPic->getPicturePerSecond();
  int oneFrameTime=0;

  if (picPerSec > 0.0) {
    oneFrameTime=(int) (1000000.0/picPerSec);
  } else {
    syncPic->print("picPersec is 0");
    return true;
  }

  if (lPerformance==true) {
    waitTime->set(0,0);
    syncPic->setWaitTime(waitTime);
    performance->incPictureCount();
    return true;
  }

  int lpacketSync=true;

  videoTimeStamp->gettimeofday();
  diffTime->minus(videoTimeStamp,videoTimeStamp);


  if (lavSync==false) {
    if (videoTimeStamp->isNegative()) {
      diffTime->gettimeofday();
      diffTime->addOffset(0,oneFrameTime);
      cout << "skip time based"<<endl;
      return false;
    }
  }



  videoTimeStamp->copyTo(waitTime);

  TimeStamp* earlyTime=syncPic->getEarlyTime();
  earlyTime->set(0,0);


  if (lavSync) {
    
    lpacketSync=avSync(syncPic->getStartTimeStamp(),
		       waitTime,
		       earlyTime,
		       syncPic->getPicturePerSecond());

  } 




  if (lpacketSync == false) {
    //cout << "skip"<<endl;
    diffTime->gettimeofday();
    diffTime->addOffset(0,oneFrameTime);
    return false;
  }

  syncPic->setWaitTime(waitTime);

  if (lavSync) {
     waitTime->minus(videoTimeStamp,waitTime);
     if (waitTime->isPositive()) {
       diffTime->addOffset(waitTime);
     }
  }
  diffTime->addOffset(0,oneFrameTime);
  return true;


}



/**
   Heart of the sync routine is here!

   Currently its more in a state of "try/test"

   
*/
int AVSyncer::avSync(TimeStamp* startVideo,
		     TimeStamp* waitTime,
		     TimeStamp* earlyTime,
		     float picPerSec ) {


  double videoStartPTSTime=startVideo->getPTSTimeStamp();
  double videoStartSCRTime=startVideo->getSCRTimeStamp();
  int videoFrameCounter=startVideo->getVideoFrameCounter();
  double frameTime=0.0;

 

  lockSyncData();
  if (picPerSec > 0) {
    oneFrameTime=(long)(1000000.0/picPerSec);
    frameTime=1.0/picPerSec;
    onePicFrameInAudioBytes=audioTime->calculateBytes(1.0/picPerSec);

  }
  if (lAudioRunning==false) {
    waitTime->set(0,oneFrameTime);

    unlockSyncData();
    return true;
  }
   

  /*
    startAudio->print("audio");
    startVideo->print("video");
  */




		       

  /*
  cout << "audioStartAudioPacketNrMinor:"<<audioStartAudioPacketNrMinor<<endl;
  cout << "audioStartPTSTime:"<<audioStartPTSTime<<endl;
  cout << "audioStartEndPTSTime:"<<audioStartEndPTSTime<<endl;
  cout << "videoStartPTSTime:"<<videoStartPTSTime<<endl;
  */
  
    
  /**
     Here we make sure that we sync over an audio packet only one
     time.
  */

  waitTime->set(0,0);
  SyncClock* syncClock=startVideo->getSyncClock();
  int back=false;
  double addPts=videoFrameCounter*frameTime;
  double pts=videoStartPTSTime+addPts;

  if (syncClock != NULL) {

    back=syncClock->syncVideo(pts,
			      videoStartSCRTime,earlyTime,waitTime);
  } else {
    cout << "syncClock == NULL (video)"<<endl;
  }
  unlockSyncData();

  if (back==true) {
    //earlyTime->print("earlyTime");
    earlyTime->waitForIt();
    /*
    double tmp;
    double time=syncClock->getPTSTime(&tmp);
    cout << "time after wait:"<<time<<endl;
    */
  }
  /*
  if (back == false) {
    cout <<"real pts:"<<videoStartPTSTime
         <<" calc pts"<<pts
	 <<" frameNo:"<<videoFrameCounter
         <<" frameTime:"<<frameTime<<endl;
  }
  */
          


  return back;

}
  
int AVSyncer::getAudioRunning() {
  return lAudioRunning;
}


void AVSyncer::setAudioRunning(int lAudioRunning) {
  this->lAudioRunning=lAudioRunning;
}
 

void AVSyncer::lockSyncData() {
  abs_thread_mutex_lock(&changeMut);
  abs_thread_mutex_lock(&writeInMut);
  abs_thread_mutex_unlock(&changeMut);
}

void AVSyncer::unlockSyncData() {
  abs_thread_mutex_unlock(&writeInMut); 
}