-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplayer.cpp
628 lines (514 loc) · 18.1 KB
/
player.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include "precomp.h"
#include "player.h"
#include "chrono.h"
#include "profiler.h"
#include "logger.h"
#include "scopeguard.h"
#include <boost/bind.hpp>
namespace {
const int64_t queueFullSleepTimeMs = 100;
const int64_t pauseSleepTimeMs = 500;
const int64_t doneSleepTimeMs = 2000;
const int64_t sleepThresholdUs = 10000;
const int64_t sleepThresholdLogUs = 1000000;
const int64_t millisecondUs = 1000;
const int64_t logDeltaThresholdUs = 1000;
const double seekFrameSkipThresholdSec = 30.0;
player::SwapBufferCallback swapBufferCallback;
}
namespace {
bool WaitForPlayback(const char* name, uint64_t startTimeUs, uint64_t timeUs, int64_t waitThresholdUs)
{
int64_t waitTime = chrono::Wait(startTimeUs, timeUs);
if( waitTime <= 0 )
{
logger::Warn("WaitForPlayback. %s Late Playback. %f seconds", name, chrono::Seconds(waitTime));
return true;
}
else
{
if( waitTime >= sleepThresholdUs )
{
if(waitTime >= sleepThresholdLogUs)
{
logger::Warn("WaitForPlayback. %s Got frame to soon %f seconds. Sleeping.", name, chrono::Seconds(waitTime));
}
std::this_thread::sleep_for(std::chrono::microseconds(waitTime-millisecondUs));
waitTime = chrono::Wait(startTimeUs, timeUs);
if( waitTime <= 0 )
{
return true;
}
}
if( waitTime > 0 )
{
do
{
std::this_thread::yield();
waitTime = chrono::Wait(startTimeUs, timeUs);
} while( waitTime >= waitThresholdUs );
}
const uint64_t currentTimeUs = chrono::Current(startTimeUs);
const int64_t deltaUs = std::abs( static_cast<int64_t>(currentTimeUs-timeUs) );
if( deltaUs > logDeltaThresholdUs )
{
logger::Warn("WaitForPlayback %s play time %ld us decode %ld us diff %ld\n", name, currentTimeUs, timeUs, deltaUs );
}
}
return true;
}
void WaitSeekEnd(player::Player* player)
{
while( mediadecoder::IsSeeking(player->producer) )
{
std::this_thread::yield();
}
}
void AudioPlaybackThread(player::Player* player)
{
mediadecoder::AudioFrame* audioFrame = nullptr;
Result result;
uint32_t nbNoFrame = 0;
while(player->queueAudio)
{
if(!audioFrame)
{
mediadecoder::Consume(player->producer, audioFrame);
}
if( audioFrame )
{
nbNoFrame = 0;
int64_t waitTime
= chrono::Wait(player->playbackStartTimeUs, audioFrame->timeUs);
if( !player->buffering && waitTime >= audiodevice::ENQUEUE_SAMPLES_US)
{
logger::Debug("AudioPlayThread sleeping. Wait Time %f", chrono::Seconds(waitTime) );
std::this_thread::sleep_for(std::chrono::milliseconds(queueFullSleepTimeMs));
continue;
}
profiler::ScopeProfiler profiler(profiler::PROFILER_AUDIO_WRITE);
result = audiodevice::WriteInterleaved( player->audioDevice, audioFrame->samples, audioFrame->nbSamples, audioFrame->inUse );
if(!result)
{
logger::Error("AudioDeviceWriteInterleaved failed %s", result.getError().c_str());
}
mediadecoder::Release(player->producer, audioFrame);
audioFrame = nullptr;
}
else
{
nbNoFrame += 1;
logger::Trace("No audio frame count:%d", nbNoFrame);
}
}
}
void StartAudioThread(player::Player* player)
{
if(player->queueAudio == false)
{
player->queueAudio = true;
player->audioThread = std::thread(AudioPlaybackThread, player);
}
}
Result StartAudioPlayback(player::Player* player)
{
Result result;
if(!mediadecoder::GetHaveAudio(player->decoder))
{
return result;
}
StartAudioThread(player);
result = audiodevice::StartWhenReady(player->audioDevice);
if(!result)
{
return result;
}
return result;
}
void StopAudio(player::Player* player, bool drop)
{
if(!mediadecoder::GetHaveAudio(player->decoder))
{
return;
}
player->queueAudio = false;
if( player->audioThread.joinable() )
{
player->audioThread.join();
}
if( drop )
{
audiodevice::Flush(player->audioDevice);
}
}
void PauseAudio(player::Player* player)
{
if(mediadecoder::GetHaveAudio(player->decoder))
{
audiodevice::Pause(player->audioDevice);
}
}
void ResumeAudio(player::Player* player)
{
if(mediadecoder::GetHaveAudio(player->decoder))
{
audiodevice::Resume(player->audioDevice);
}
}
void DrawSubtitle(player::Player* player)
{
if(!player->subtitle)
{
mediadecoder::Consume(player->producer, player->subtitle);
}
if(!player->nextSubtitle)
{
mediadecoder::Consume(player->producer, player->nextSubtitle);
}
// subtitle
if(player->subtitle)
{
const int64_t subtitleDuration = player->subtitle->endTimeUs - player->subtitle->startTimeUs;
int64_t subtitleWait = chrono::Wait(player->playbackStartTimeUs, player->subtitle->startTimeUs);
const bool inSubtitleTime = (subtitleWait <= 0 && subtitleWait >= -subtitleDuration);
if(inSubtitleTime)
{
mediadecoder::Subtitle* sub = player->subtitle;
uint32_t width = 0;
uint32_t height = 0;
videodevice::GetWindowSize(player->videoDevice, width, height);
// ass header
subtitle::GetTextSizeCb textSizeCb = boost::bind(videodevice::GetTextSize, player->videoDevice, _1, _2, _3, _4, _5);
subtitle::SubLineList lines;
Result result = subtitle::GetDisplayInfo(sub->text, textSizeCb, width, height,
sub->header, sub->dialogue, sub->startTimeUs, sub->endTimeUs,
sub->fontName, sub->fontSize, sub->color, lines);
if(!result)
{
logger::Error("Error getting subtitle display info: %s", result.getError().c_str());
return;
}
for(auto it = lines.begin(); it != lines.end(); ++it)
{
videodevice::DrawText(player->videoDevice, (*it).text, sub->fontName, sub->fontSize, (*it).x, (*it).y, 1, sub->color);
}
}
if(subtitleWait < -subtitleDuration)
{
mediadecoder::Release(player->producer, player->subtitle);
// go to next subtitle
player->subtitle = player->nextSubtitle;
player->nextSubtitle = nullptr;
}
// duration is not always provided and a default duration is set to subtitle
// verify that the next subtitle should not be displaying
if(player->nextSubtitle)
{
const int64_t nextSubtitleDuration = player->nextSubtitle->endTimeUs - player->nextSubtitle->startTimeUs;
int64_t nextSubtitleWait = chrono::Wait(player->playbackStartTimeUs, player->nextSubtitle->startTimeUs);
const bool inNextSubtitleTime = (nextSubtitleWait <= 0 && nextSubtitleWait >= -nextSubtitleDuration);
if(inNextSubtitleTime)
{
if(player->subtitle)
{
mediadecoder::Release(player->producer, player->subtitle);
}
player->subtitle = player->nextSubtitle;
player->nextSubtitle = nullptr;
}
}
}
}
}
namespace player
{
Result Init(SwapBufferCallback cb)
{
Result result;
swapBufferCallback = cb;
return result;
}
Result Create(Player*& player)
{
Result result;
player = new Player();
result = audiodevice::Create(player->audioDevice);
if(!result)
{
result = Result(false, result.getError());
return result;
}
return result;
}
Result Open(Player* player, const std::string& filename)
{
Close(player);
Result result = mediadecoder::Create(player->decoder);
if(!result)
{
return result;
}
result = mediadecoder::Open(player->decoder, filename);
if(!result)
{
return result;
}
if(mediadecoder::GetHaveVideo(player->decoder))
{
result = videodevice::Create(player->videoDevice, mediadecoder::GetOutputFormat(player->decoder));
if(!result)
{
result = Result(false, result.getError());
return result;
}
}
player->path = filename;
result = mediadecoder::Create(player->producer, player->decoder);
if(!result)
{
return result;
}
if(mediadecoder::GetHaveVideo(player->decoder))
{
result = videodevice::SetTextureSize(player->videoDevice,
mediadecoder::GetVideoWidth(player->decoder),
mediadecoder::GetVideoHeight(player->decoder));
if(!result)
{
return result;
}
SetWindowSize(player,
mediadecoder::GetVideoWidth(player->decoder),
mediadecoder::GetVideoHeight(player->decoder));
}
if(mediadecoder::GetHaveAudio(player->decoder))
{
const uint32_t channels = mediadecoder::GetAudioNumChannels(player->decoder);
const uint32_t sampleRate = mediadecoder::GetAudioSampleRate(player->decoder);
const SampleFormat sampleFormat = mediadecoder::GetAudioSampleFormat(player->decoder);
result = audiodevice::SetInputFormat(player->audioDevice,channels,sampleRate,sampleFormat);
if(!result)
{
return result;
}
}
return result;
}
void SetWindowSize(Player* player, uint32_t w, uint32_t h)
{
assert(player);
if(player && player->videoDevice)
{
videodevice::SetWindowSize(player->videoDevice, w, h);
}
}
void ToggleSubtitleTrack(Player* player)
{
if(player && player->decoder)
{
mediadecoder::ToggleSubtitleTrack(player->decoder);
}
}
void AddSubtitleTrack(Player* player, std::shared_ptr<subtitle::SubRip> srt)
{
if(player && player->decoder)
{
mediadecoder::AddSubtitleTrack(player->decoder, srt);
}
}
void Play(Player* player)
{
logger::Info("Play");
if(player->playing)
{
logger::Warn("Player Play called while playing");
return;
}
if(!player->decoder || !player->producer)
{
logger::Warn("Player Play called without media");
return;
}
// start buffering
assert(!player->buffering);
scopeguard::SetValue bufferingGuard(player->buffering, true, false);
mediadecoder::WaitForPlayback(player->producer);
if(!player->pause)
{
Result result = StartAudioPlayback(player);
if(!result)
{
logger::Error("Cannot start audio %s", result.getError().c_str());
return;
}
}
else
{
ResumeAudio(player);
player->pause = false;
}
// start playback
player->playbackStartTimeUs = chrono::Now() - player->currentTimeUs;
player->playing = true;
}
void Seek(Player* player, uint64_t timeUs)
{
if( mediadecoder::IsSeeking(player->producer) )
{
logger::Warn("Player seek failed. Already seeking.");
return;
}
logger::Info("Seek start at %f ms", chrono::Milliseconds(timeUs));
mediadecoder::Seek(player->producer, timeUs);
// stop audio playback
StopAudio(player, true);
// wait for seek
WaitSeekEnd(player);
// start buffering
assert(!player->buffering);
scopeguard::SetValue bufferingGuard(player->buffering, true, false);
mediadecoder::Release(player->producer, player->videoFrame);
mediadecoder::WaitForPlayback(player->producer);
int64_t sleepTime = 0;
// skip old frames from decoder
do
{
if(!player->videoFrame)
{
mediadecoder::Consume(player->producer, player->videoFrame);
}
if(!player->videoFrame)
{
break;
}
int64_t deltaUs = player->videoFrame->timeUs - timeUs;
double deltaSec = chrono::Seconds(deltaUs);
if(deltaSec < seekFrameSkipThresholdSec && deltaSec > 0.0)
{
logger::Warn("Seek sleeping for %f", chrono::Seconds(deltaUs));
sleepTime = deltaUs;
break;
}
logger::Warn("Player: Seek skipping frame %f frame %ld time %ld", chrono::Seconds(deltaUs), player->videoFrame->timeUs, timeUs);
mediadecoder::Release(player->producer, player->videoFrame);
player->videoFrame = nullptr;
} while(true);
Result result = StartAudioPlayback(player);
if(!result)
{
logger::Error("Cannot start audio %s", result.getError().c_str());
return;
}
// reset playback start time
player->playbackStartTimeUs = static_cast<int64_t>(chrono::Now()) - static_cast<int64_t>(timeUs);
// we got video frame in the future, sleep until we can play them
if(sleepTime != 0)
{
std::this_thread::sleep_for(std::chrono::microseconds(sleepTime));
}
logger::Info("Seek end");
}
void Pause(Player* player)
{
logger::Info("Pause");
if(!player->playing)
{
return;
}
player->playing = false;
player->pause = true;
PauseAudio(player);
}
bool IsPlaying(Player* player)
{
return player->playing;
}
uint64_t GetDuration(Player* player)
{
if(!player || !player->decoder)
{
return 0;
}
return mediadecoder::GetDuration(player->decoder);
}
uint64_t GetCurrentTime(Player* player)
{
if(!player)
{
return 0;
}
return player->currentTimeUs;
}
const std::string& GetPath(Player* player)
{
assert(player);
return player->path;
}
void Present(Player* player)
{
if(!player->playing)
{
std::this_thread::sleep_for(std::chrono::milliseconds(pauseSleepTimeMs));
return;
}
if(!player->videoFrame)
{
mediadecoder::Consume(player->producer, player->videoFrame);
}
if( player->videoFrame )
{
player->currentTimeUs = player->videoFrame->timeUs;
const uint64_t waitThresholdUs = 25;
const bool drawFrame
= WaitForPlayback("video", player->playbackStartTimeUs, player->videoFrame->timeUs, waitThresholdUs);
if( drawFrame && player->videoFrame )
{
profiler::ScopeProfiler profiler(profiler::PROFILER_VIDEO_DRAW);
// create fb based on VideoFrame
videodevice::FrameBuffer fb;
fb.width = player->videoFrame->width;
fb.height = player->videoFrame->height;
for(uint32_t i = 0; i < videodevice::NUM_FRAME_DATA_POINTERS; i++)
{
fb.frameData[i] = player->videoFrame->buffers[i];
fb.lineSize[i] = player->videoFrame->lineSize[i];
}
// draw frame
videodevice::DrawFrame(player->videoDevice, &fb);
// draw subtitle
DrawSubtitle(player);
// swap buffer
swapBufferCallback();
mediadecoder::Release(player->producer, player->videoFrame);
player->videoFrame = nullptr;
}
else if(player->videoFrame)
{
mediadecoder::Release(player->producer, player->videoFrame);
player->videoFrame = nullptr;
}
}
else if(player->producer->done)
{
std::this_thread::sleep_for(std::chrono::milliseconds(doneSleepTimeMs));
}
}
void Close(Player* player)
{
StopAudio(player, true);
player->playbackStartTimeUs = 0;
player->currentTimeUs = 0;
player->playing = false;
player->pause = false;
mediadecoder::Destroy(player->producer);
mediadecoder::Destroy(player->decoder);
videodevice::Destroy(player->videoDevice);
}
void Destroy(Player*& player)
{
Close(player);
audiodevice::Destroy(player->audioDevice);
delete player;
player = nullptr;
}
}