forked from popcornmix/omxplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OMXClock.cpp
562 lines (439 loc) · 12.3 KB
/
OMXClock.cpp
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
* Copyright (C) 2005-2013 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#if (defined HAVE_CONFIG_H) && (!defined WIN32)
#include "config.h"
#elif defined(_WIN32)
#include "system.h"
#endif
#if defined(HAVE_OMXLIB)
//#include "video/VideoReferenceClock.h"
//#include "settings/Settings.h"
#include "OMXClock.h"
#define OMX_PRE_ROLL 200
#define TP(speed) ((speed) < 0 || (speed) > 4*DVD_PLAYSPEED_NORMAL)
OMXClock::OMXClock()
{
m_dllAvFormat.Load();
m_pause = false;
m_omx_speed = DVD_PLAYSPEED_NORMAL;
m_WaitMask = 0;
m_eState = OMX_TIME_ClockStateStopped;
m_eClock = OMX_TIME_RefClockNone;
pthread_mutex_init(&m_lock, NULL);
}
OMXClock::~OMXClock()
{
OMXDeinitialize();
m_dllAvFormat.Unload();
pthread_mutex_destroy(&m_lock);
}
void OMXClock::Lock()
{
pthread_mutex_lock(&m_lock);
}
void OMXClock::UnLock()
{
pthread_mutex_unlock(&m_lock);
}
void OMXClock::OMXSetClockPorts(OMX_TIME_CONFIG_CLOCKSTATETYPE *clock, bool has_video, bool has_audio)
{
if(m_omx_clock.GetComponent() == NULL)
return;
if(!clock)
return;
clock->nWaitMask = 0;
if(has_audio)
{
clock->nWaitMask |= OMX_CLOCKPORT0;
}
if(has_video)
{
clock->nWaitMask |= OMX_CLOCKPORT1;
}
}
bool OMXClock::OMXSetReferenceClock(bool has_audio, bool lock /* = true */)
{
if(lock)
Lock();
bool ret = true;
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refClock;
OMX_INIT_STRUCTURE(refClock);
if(has_audio)
refClock.eClock = OMX_TIME_RefClockAudio;
else
refClock.eClock = OMX_TIME_RefClockVideo;
if (refClock.eClock != m_eClock)
{
CLog::Log(LOGNOTICE, "OMXClock using %s as reference", refClock.eClock == OMX_TIME_RefClockVideo ? "video" : "audio");
omx_err = m_omx_clock.SetConfig(OMX_IndexConfigTimeActiveRefClock, &refClock);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::OMXSetReferenceClock error setting OMX_IndexConfigTimeActiveRefClock");
ret = false;
}
m_eClock = refClock.eClock;
}
if(lock)
UnLock();
return ret;
}
bool OMXClock::OMXInitialize()
{
std::string componentName = "";
m_pause = false;
componentName = "OMX.broadcom.clock";
if(!m_omx_clock.Initialize((const std::string)componentName, OMX_IndexParamOtherInit))
return false;
m_omx_clock.DisableAllPorts();
return true;
}
void OMXClock::OMXDeinitialize()
{
if(m_omx_clock.GetComponent() == NULL)
return;
m_omx_clock.Deinitialize();
m_omx_speed = DVD_PLAYSPEED_NORMAL;
}
bool OMXClock::OMXStateExecute(bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return false;
if(lock)
Lock();
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
if(m_omx_clock.GetState() != OMX_StateExecuting)
{
OMXStateIdle(false);
omx_err = m_omx_clock.SetStateForComponent(OMX_StateExecuting);
if (omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::StateExecute m_omx_clock.SetStateForComponent\n");
if(lock)
UnLock();
return false;
}
}
if(lock)
UnLock();
return true;
}
void OMXClock::OMXStateIdle(bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return;
if(lock)
Lock();
if(m_omx_clock.GetState() != OMX_StateIdle)
m_omx_clock.SetStateForComponent(OMX_StateIdle);
if(lock)
UnLock();
}
COMXCoreComponent *OMXClock::GetOMXClock()
{
return &m_omx_clock;
}
bool OMXClock::OMXStop(bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return false;
if(lock)
Lock();
CLog::Log(LOGDEBUG, "OMXClock::OMXStop\n");
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
OMX_TIME_CONFIG_CLOCKSTATETYPE clock;
OMX_INIT_STRUCTURE(clock);
clock.eState = OMX_TIME_ClockStateStopped;
clock.nOffset = ToOMXTime(-1000LL * OMX_PRE_ROLL);
omx_err = m_omx_clock.SetConfig(OMX_IndexConfigTimeClockState, &clock);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::Stop error setting OMX_IndexConfigTimeClockState\n");
if(lock)
UnLock();
return false;
}
m_eState = clock.eState;
if(lock)
UnLock();
return true;
}
bool OMXClock::OMXStep(int steps /* = 1 */, bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return false;
if(lock)
Lock();
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
OMX_PARAM_U32TYPE param;
OMX_INIT_STRUCTURE(param);
param.nPortIndex = OMX_ALL;
param.nU32 = steps;
omx_err = m_omx_clock.SetConfig(OMX_IndexConfigSingleStep, ¶m);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::Error setting OMX_IndexConfigSingleStep\n");
if(lock)
UnLock();
return false;
}
if(lock)
UnLock();
CLog::Log(LOGDEBUG, "OMXClock::Step (%d)", steps);
return true;
}
bool OMXClock::OMXReset(bool has_video, bool has_audio, bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return false;
if(lock)
Lock();
if(!OMXSetReferenceClock(has_audio, false))
{
if(lock)
UnLock();
return false;
}
if (m_eState == OMX_TIME_ClockStateStopped)
{
OMX_TIME_CONFIG_CLOCKSTATETYPE clock;
OMX_INIT_STRUCTURE(clock);
clock.eState = OMX_TIME_ClockStateWaitingForStartTime;
clock.nOffset = ToOMXTime(-1000LL * OMX_PRE_ROLL);
OMXSetClockPorts(&clock, has_video, has_audio);
if(clock.nWaitMask)
{
OMX_ERRORTYPE omx_err = m_omx_clock.SetConfig(OMX_IndexConfigTimeClockState, &clock);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::OMXReset error setting OMX_IndexConfigTimeClockState\n");
if(lock)
UnLock();
return false;
}
CLog::Log(LOGDEBUG, "OMXClock::OMXReset audio / video : %d / %d wait mask %d->%d state : %d->%d\n",
has_audio, has_video, m_WaitMask, clock.nWaitMask, m_eState, clock.eState);
if (m_eState != OMX_TIME_ClockStateStopped)
m_WaitMask = clock.nWaitMask;
m_eState = clock.eState;
}
}
if(lock)
UnLock();
return true;
}
double OMXClock::OMXMediaTime(bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return 0;
if(lock)
Lock();
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
double pts = 0;
OMX_TIME_CONFIG_TIMESTAMPTYPE timeStamp;
OMX_INIT_STRUCTURE(timeStamp);
timeStamp.nPortIndex = m_omx_clock.GetInputPort();
omx_err = m_omx_clock.GetConfig(OMX_IndexConfigTimeCurrentMediaTime, &timeStamp);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::MediaTime error getting OMX_IndexConfigTimeCurrentMediaTime\n");
if(lock)
UnLock();
return 0;
}
pts = FromOMXTime(timeStamp.nTimestamp);
if(lock)
UnLock();
return pts;
}
double OMXClock::OMXClockAdjustment(bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return 0;
if(lock)
Lock();
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
double pts = 0;
OMX_TIME_CONFIG_TIMESTAMPTYPE timeStamp;
OMX_INIT_STRUCTURE(timeStamp);
timeStamp.nPortIndex = m_omx_clock.GetInputPort();
omx_err = m_omx_clock.GetConfig(OMX_IndexConfigClockAdjustment, &timeStamp);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::MediaTime error getting OMX_IndexConfigClockAdjustment\n");
if(lock)
UnLock();
return 0;
}
pts = (double)FromOMXTime(timeStamp.nTimestamp);
//CLog::Log(LOGINFO, "OMXClock::ClockAdjustment %.0f %.0f\n", (double)FromOMXTime(timeStamp.nTimestamp), pts);
if(lock)
UnLock();
return pts;
}
// Set the media time, so calls to get media time use the updated value,
// useful after a seek so mediatime is updated immediately (rather than waiting for first decoded packet)
bool OMXClock::OMXMediaTime(double pts, bool lock /* = true*/)
{
if(m_omx_clock.GetComponent() == NULL)
return false;
if(lock)
Lock();
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
OMX_INDEXTYPE index;
OMX_TIME_CONFIG_TIMESTAMPTYPE timeStamp;
OMX_INIT_STRUCTURE(timeStamp);
timeStamp.nPortIndex = m_omx_clock.GetInputPort();
if(m_eClock == OMX_TIME_RefClockAudio)
index = OMX_IndexConfigTimeCurrentAudioReference;
else
index = OMX_IndexConfigTimeCurrentVideoReference;
timeStamp.nTimestamp = ToOMXTime(pts);
omx_err = m_omx_clock.SetConfig(index, &timeStamp);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::OMXMediaTime error setting %s", index == OMX_IndexConfigTimeCurrentAudioReference ?
"OMX_IndexConfigTimeCurrentAudioReference":"OMX_IndexConfigTimeCurrentVideoReference");
if(lock)
UnLock();
return false;
}
CLog::Log(LOGDEBUG, "OMXClock::OMXMediaTime set config %s = %.2f", index == OMX_IndexConfigTimeCurrentAudioReference ?
"OMX_IndexConfigTimeCurrentAudioReference":"OMX_IndexConfigTimeCurrentVideoReference", pts);
if(lock)
UnLock();
return true;
}
bool OMXClock::OMXPause(bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return false;
if(!m_pause)
{
if(lock)
Lock();
if (OMXSetSpeed(0, false, true))
m_pause = true;
if(lock)
UnLock();
}
return m_pause == true;
}
bool OMXClock::OMXResume(bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return false;
if(m_pause)
{
if(lock)
Lock();
if (OMXSetSpeed(m_omx_speed, false, true))
m_pause = false;
if(lock)
UnLock();
}
return m_pause == false;
}
bool OMXClock::OMXSetSpeed(int speed, bool lock /* = true */, bool pause_resume /* = false */)
{
if(m_omx_clock.GetComponent() == NULL)
return false;
if(lock)
Lock();
CLog::Log(LOGDEBUG, "OMXClock::OMXSetSpeed(%.2f) pause_resume:%d", (float)speed / (float)DVD_PLAYSPEED_NORMAL, pause_resume);
if (pause_resume)
{
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
OMX_TIME_CONFIG_SCALETYPE scaleType;
OMX_INIT_STRUCTURE(scaleType);
if (TP(speed))
scaleType.xScale = 0; // for trickplay we just pause, and single step
else
scaleType.xScale = (speed << 16) / DVD_PLAYSPEED_NORMAL;
omx_err = m_omx_clock.SetConfig(OMX_IndexConfigTimeScale, &scaleType);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::OMXSetSpeed error setting OMX_IndexConfigTimeClockState\n");
if(lock)
UnLock();
return false;
}
}
if (!pause_resume)
m_omx_speed = speed;
if(lock)
UnLock();
return true;
}
bool OMXClock::HDMIClockSync(bool lock /* = true */)
{
if(m_omx_clock.GetComponent() == NULL)
return false;
if(lock)
Lock();
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
OMX_CONFIG_LATENCYTARGETTYPE latencyTarget;
OMX_INIT_STRUCTURE(latencyTarget);
latencyTarget.nPortIndex = OMX_ALL;
latencyTarget.bEnabled = OMX_TRUE;
latencyTarget.nFilter = 10;
latencyTarget.nTarget = 0;
latencyTarget.nShift = 3;
latencyTarget.nSpeedFactor = -200;
latencyTarget.nInterFactor = 100;
latencyTarget.nAdjCap = 100;
omx_err = m_omx_clock.SetConfig(OMX_IndexConfigLatencyTarget, &latencyTarget);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "OMXClock::Speed error setting OMX_IndexConfigLatencyTarget\n");
if(lock)
UnLock();
return false;
}
if(lock)
UnLock();
return true;
}
void OMXClock::OMXSleep(unsigned int dwMilliSeconds)
{
struct timespec req;
req.tv_sec = dwMilliSeconds / 1000;
req.tv_nsec = (dwMilliSeconds % 1000) * 1000000;
while ( nanosleep(&req, &req) == -1 && errno == EINTR && (req.tv_nsec > 0 || req.tv_sec > 0));
}
static int64_t CurrentHostCounter(void)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return( ((int64_t)now.tv_sec * 1000000000L) + now.tv_nsec );
}
static int64_t CurrentHostFrequency(void)
{
return( (int64_t)1000000000L );
}
int64_t OMXClock::GetAbsoluteClock()
{
return CurrentHostCounter()/1000;
}
double OMXClock::GetClock(bool interpolated /*= true*/)
{
return GetAbsoluteClock();
}
#endif