-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathomxdevice.c
826 lines (694 loc) · 19.3 KB
/
omxdevice.c
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/*
* rpihddevice - VDR HD output device for Raspberry Pi
* Copyright (C) 2014, 2015, 2016 Thomas Reufer
*
* 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 of the License, 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "omxdevice.h"
#include "omx.h"
#include "audio.h"
#include "display.h"
#include "setup.h"
#include "tools.h"
#include <vdr/thread.h>
#include <vdr/remux.h>
#include <vdr/tools.h>
#include <vdr/skins.h>
#include <string.h>
#define S(x) ((int)(floor(x * pow(2, 16))))
#define PTS_START_OFFSET (32 * (MAX33BIT + 1))
#define PRE_ROLL_LIVE 250
#define PRE_ROLL_PLAYBACK 0
// trick speeds as defined in vdr/dvbplayer.c
const int cOmxDevice::s_playbackSpeeds[eNumDirections][eNumPlaybackSpeeds] = {
{ S(0.0f), S( 0.125f), S( 0.25f), S( 0.5f), S( 1.0f), S( 2.0f), S( 4.0f), S( 12.0f) },
{ S(0.0f), S(-0.125f), S(-0.25f), S(-0.5f), S(-1.0f), S(-2.0f), S(-4.0f), S(-12.0f) }
};
// speed correction factors for live mode
// HDMI specification allows a tolerance of 1000ppm, however on the Raspberry Pi
// it's limited to 175ppm to avoid audio drops one some A/V receivers
const int cOmxDevice::s_liveSpeeds[eNumLiveSpeeds] = {
S(0.999f), S(0.99985f), S(1.000f), S(1.00015), S(1.001)
};
const uchar cOmxDevice::s_pesVideoHeader[14] = {
0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x80, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00
};
const uchar cOmxDevice::s_mpeg2EndOfSequence[4] = { 0x00, 0x00, 0x01, 0xb7 };
const uchar cOmxDevice::s_h264EndOfSequence[8] = { 0x00, 0x00, 0x01, 0x0a, 0x00, 0x00, 0x01, 0x0b };
cOmxDevice::cOmxDevice(void (*onPrimaryDevice)(void), int display, int layer) :
cDevice(),
m_onPrimaryDevice(onPrimaryDevice),
m_omx(),
m_audio(&m_omx),
m_mutex(),
m_videoCodec(cVideoCodec::eInvalid),
m_playMode(pmNone),
m_liveSpeed(eNoCorrection),
m_playbackSpeed(eNormal),
m_direction(eForward),
m_hasVideo(false),
m_hasAudio(false),
m_skipAudio(false),
m_playDirection(0),
m_trickRequest(0),
m_audioPts(0),
m_videoPts(0),
m_lastStc(0),
m_display(display),
m_layer(layer)
{
}
cOmxDevice::~cOmxDevice()
{
DeInit();
}
int cOmxDevice::Init(void)
{
m_omx.SetBufferStallCallback(&OnBufferStall, this);
m_omx.SetEndOfStreamCallback(&OnEndOfStream, this);
m_omx.SetStreamStartCallback(&OnStreamStart, this);
if (m_omx.Init(m_display, m_layer) < 0)
{
ELOG("failed to initialize OMX!");
return -1;
}
if (m_audio.Init() < 0)
{
ELOG("failed to initialize audio!");
return -1;
}
cRpiSetup::SetVideoSetupChangedCallback(&OnVideoSetupChanged, this);
return 0;
}
void cOmxDevice::DeInit(void)
{
SetPlayMode(pmNone);
cRpiSetup::SetVideoSetupChangedCallback(0);
}
bool cOmxDevice::Start(void)
{
HandleVideoSetupChanged();
return true;
}
void cOmxDevice::GetOsdSize(int &Width, int &Height, double &PixelAspect)
{
cRpiDisplay::GetSize(Width, Height, PixelAspect);
}
void cOmxDevice::GetVideoSize(int &Width, int &Height, double &VideoAspect)
{
Height = m_omx.GetVideoFrameFormat()->height;
Width = m_omx.GetVideoFrameFormat()->width;
if (Height)
VideoAspect = (double)Width / Height;
else
VideoAspect = 1.0;
}
void cOmxDevice::ScaleVideo(const cRect &Rect)
{
DBG("ScaleVideo(%d, %d, %d, %d)",
Rect.X(), Rect.Y(), Rect.Width(), Rect.Height());
m_omx.SetDisplayRegion(Rect.X(), Rect.Y(), Rect.Width(), Rect.Height());
}
bool cOmxDevice::SetPlayMode(ePlayMode PlayMode)
{
m_mutex.Lock();
DBG("SetPlayMode(%s)",
PlayMode == pmNone ? "none" :
PlayMode == pmAudioVideo ? "Audio/Video" :
PlayMode == pmAudioOnly ? "Audio only" :
PlayMode == pmAudioOnlyBlack ? "Audio only, black" :
PlayMode == pmVideoOnly ? "Video only" :
"unsupported");
// Stop audio / video if play mode is set to pmNone. Start
// is triggered once a packet is going to be played, since
// we don't know what kind of stream we'll get (audio-only,
// video-only or both) after SetPlayMode() - VDR will always
// pass pmAudioVideo as argument.
switch (PlayMode)
{
case pmNone:
if (m_playMode == pmNone) break;
FlushStreams(true);
m_omx.StopVideo();
m_hasAudio = false;
m_hasVideo = false;
m_videoCodec = cVideoCodec::eInvalid;
m_audio.DeInit();
m_omx.DeInit();
m_playMode = pmNone;
break;
case pmAudioVideo:
case pmAudioOnly:
case pmAudioOnlyBlack:
case pmVideoOnly:
if (m_playMode == pmNone)
{
m_omx.Init(m_display, m_layer);
m_audio.Init();
SetVolumeDevice(IsMute() ? 0 : CurrentVolume());
}
m_playbackSpeed = eNormal;
m_direction = eForward;
break;
default:
break;
}
m_mutex.Unlock();
return true;
}
void cOmxDevice::StillPicture(const uchar *Data, int Length)
{
if (Data[0] == 0x47)
cDevice::StillPicture(Data, Length);
else
{
DBG("StillPicture()");
int pesLength = 0;
uchar *pesPacket = 0;
cVideoCodec::eCodec codec = ParseVideoCodec(Data, Length);
if (codec != cVideoCodec::eInvalid)
{
// some plugins deliver raw MPEG data, but PlayVideo() needs a
// complete PES packet with valid header
pesLength = Length + sizeof(s_pesVideoHeader);
pesPacket = MALLOC(uchar, pesLength);
if (!pesPacket)
return;
memcpy(pesPacket, s_pesVideoHeader, sizeof(s_pesVideoHeader));
memcpy(pesPacket + sizeof(s_pesVideoHeader), Data, Length);
}
else
codec = ParseVideoCodec(Data + PesPayloadOffset(Data),
Length - PesPayloadOffset(Data));
if (codec == cVideoCodec::eInvalid)
return;
m_mutex.Lock();
m_playbackSpeed = eNormal;
m_direction = eForward;
m_hasVideo = false;
m_omx.StopClock();
// since the stream might be interlaced, we send each frame twice, so
// the advanced deinterlacer is able to render an output picture
int repeat = 2;
while (repeat--)
{
int length = pesPacket ? pesLength : Length;
const uchar *data = pesPacket ? pesPacket : Data;
// play every single PES packet, rise ENDOFFRAME flag on last
while (PesLongEnough(length))
{
int pktLen = PesHasLength(data) ? PesLength(data) : length;
// skip non-video packets as they may occur in PES recordings
if ((data[3] & 0xf0) == 0xe0)
PlayVideo(data, pktLen, pktLen == length);
data += pktLen;
length -= pktLen;
}
}
if (pesPacket)
free(pesPacket);
SubmitEOS();
m_mutex.Unlock();
}
}
int cOmxDevice::PlayAudio(const uchar *Data, int Length, uchar Id)
{
// ignore audio packets during fast trick speeds for non-radio recordings
if (m_playbackSpeed > eNormal && m_playMode != pmAudioOnly)
{
DLOG("audio packet ignored!");
return Length;
}
m_mutex.Lock();
int ret = Length;
int64_t pts = PesHasPts(Data) ? PesGetPts(Data) : OMX_INVALID_PTS;
if (pts != OMX_INVALID_PTS)
{
if (!m_hasAudio)
{
m_hasAudio = true;
m_omx.SetClockReference(cOmx::eClockRefAudio);
if (!m_hasVideo)
{
DBG("audio first");
m_omx.SetClockScale(
s_playbackSpeeds[m_direction][m_playbackSpeed]);
m_omx.StartClock(m_hasVideo, m_hasAudio,
Transferring() ? PRE_ROLL_LIVE : PRE_ROLL_PLAYBACK);
m_audioPts = PTS_START_OFFSET + pts;
m_playMode = pmAudioOnly;
}
else
{
m_audioPts = m_videoPts + PtsDiff(m_videoPts & MAX33BIT, pts);
m_playMode = pmAudioVideo;
}
}
int64_t ptsDiff = PtsDiff(m_audioPts & MAX33BIT, pts);
if ((m_audioPts & ~MAX33BIT) != (m_audioPts + ptsDiff & ~MAX33BIT))
DBG("audio PTS wrap around");
m_audioPts += ptsDiff;
// keep track of direction in case of trick speed
if (m_trickRequest && ptsDiff)
PtsTracker(ptsDiff);
}
int length = Length - PesPayloadOffset(Data);
// ignore packets with invalid payload offset
if (length > 0)
{
const uchar *data = Data + PesPayloadOffset(Data);
// remove audio substream header as seen in PES recordings with AC3
// audio track (0x80: AC3, 0x88: DTS, 0xA0: LPCM)
if ((data[0] == 0x80 || data[0] == 0x88 || data[0] == 0xa0)
&& data[0] == Id)
{
data += 4;
length -= 4;
}
if (!m_audio.WriteData(data, length,
pts != OMX_INVALID_PTS ? m_audioPts : OMX_INVALID_PTS))
ret = 0;
}
if (Transferring() && !ret)
DBG("failed to write %d bytes of audio packet!", Length);
if (ret && Transferring())
AdjustLiveSpeed();
m_mutex.Unlock();
return ret;
}
int cOmxDevice::PlayVideo(const uchar *Data, int Length, bool EndOfFrame)
{
// prevent writing incomplete frames
if (m_hasVideo && !m_omx.PollVideo())
return 0;
m_mutex.Lock();
int ret = Length;
cVideoCodec::eCodec codec = ParseVideoCodec(Data + PesPayloadOffset(Data),
Length - PesPayloadOffset(Data));
int64_t pts = PesHasPts(Data) && codec != cVideoCodec::eInvalid ?
PesGetPts(Data) : OMX_INVALID_PTS;
if (!m_hasVideo && pts != OMX_INVALID_PTS &&
m_videoCodec == cVideoCodec::eInvalid)
{
if (codec != cVideoCodec::eInvalid)
{
m_videoCodec = codec;
if (cRpiSetup::IsVideoCodecSupported(m_videoCodec))
{
m_omx.SetVideoCodec(m_videoCodec);
DLOG("set video codec to %s", cVideoCodec::Str(m_videoCodec));
}
else
Skins.QueueMessage(mtError, tr("video format not supported!"));
}
}
if (!m_hasVideo && pts != OMX_INVALID_PTS &&
cRpiSetup::IsVideoCodecSupported(m_videoCodec))
{
m_hasVideo = true;
if (!m_hasAudio)
{
DBG("video first");
m_omx.SetClockReference(cOmx::eClockRefVideo);
m_omx.SetClockScale(s_playbackSpeeds[m_direction][m_playbackSpeed]);
m_omx.StartClock(m_hasVideo, m_hasAudio,
Transferring() ? PRE_ROLL_LIVE : PRE_ROLL_PLAYBACK);
m_videoPts = PTS_START_OFFSET + pts;
m_playMode = pmVideoOnly;
}
else
{
m_videoPts = m_audioPts + PtsDiff(m_audioPts & MAX33BIT, pts);
m_playMode = pmAudioVideo;
}
}
if (m_hasVideo)
{
if (pts != OMX_INVALID_PTS)
{
int64_t ptsDiff = PtsDiff(m_videoPts & MAX33BIT, pts);
m_videoPts += ptsDiff;
// keep track of direction in case of trick speed
if (m_trickRequest && ptsDiff)
PtsTracker(ptsDiff);
}
// skip PES header, proceed with payload towards OMX
Length -= PesPayloadOffset(Data);
Data += PesPayloadOffset(Data);
while (Length > 0)
{
if (OMX_BUFFERHEADERTYPE *buf = m_omx.GetVideoBuffer(
pts != OMX_INVALID_PTS ? m_videoPts : OMX_INVALID_PTS))
{
buf->nFilledLen = buf->nAllocLen < (unsigned)Length ?
buf->nAllocLen : Length;
memcpy(buf->pBuffer, Data, buf->nFilledLen);
Length -= buf->nFilledLen;
Data += buf->nFilledLen;
if (EndOfFrame && !Length)
buf->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
if (!m_omx.EmptyVideoBuffer(buf))
{
ret = 0;
ELOG("failed to pass buffer to video decoder!");
break;
}
}
else
{
ret = 0;
break;
}
pts = OMX_INVALID_PTS;
}
}
if (Transferring() && !ret)
DBG("failed to write %d bytes of video packet!", Length);
if (ret && Transferring())
AdjustLiveSpeed();
m_mutex.Unlock();
return ret;
}
bool cOmxDevice::SubmitEOS(void)
{
DBG("SubmitEOS()");
OMX_BUFFERHEADERTYPE *buf = m_omx.GetVideoBuffer(0);
if (buf)
{
buf->nFlags = OMX_BUFFERFLAG_EOS | OMX_BUFFERFLAG_ENDOFFRAME;
buf->nFilledLen = m_videoCodec == cVideoCodec::eMPEG2 ?
sizeof(s_mpeg2EndOfSequence) : sizeof(s_h264EndOfSequence);
memcpy(buf->pBuffer, m_videoCodec == cVideoCodec::eMPEG2 ?
s_mpeg2EndOfSequence : s_h264EndOfSequence, buf->nFilledLen);
}
return m_omx.EmptyVideoBuffer(buf);
}
int64_t cOmxDevice::GetSTC(void)
{
int64_t stc = m_omx.GetSTC();
if (stc != OMX_INVALID_PTS)
m_lastStc = stc;
return m_lastStc & MAX33BIT;
}
uchar *cOmxDevice::GrabImage(int &Size, bool Jpeg, int Quality,
int SizeX, int SizeY)
{
DBG("GrabImage(%s, %dx%d)", Jpeg ? "JPEG" : "PNM", SizeX, SizeY);
uint8_t* ret = NULL;
int width, height;
cRpiDisplay::GetSize(width, height);
SizeX = (SizeX > 0) ? SizeX : width;
SizeY = (SizeY > 0) ? SizeY : height;
Quality = (Quality >= 0) ? Quality : 100;
// bigger than needed, but uint32_t ensures proper alignment
uint8_t* frame = (uint8_t*)(MALLOC(uint32_t, SizeX * SizeY));
if (!frame)
{
ELOG("failed to allocate image buffer!");
return ret;
}
if (cRpiDisplay::Snapshot(frame, SizeX, SizeY))
{
ELOG("failed to grab image!");
free(frame);
return ret;
}
if (Jpeg)
ret = RgbToJpeg(frame, SizeX, SizeY, Size, Quality);
else
{
char buf[32];
snprintf(buf, sizeof(buf), "P6\n%d\n%d\n255\n", SizeX, SizeY);
int l = strlen(buf);
Size = l + SizeX * SizeY * 3;
ret = MALLOC(uint8_t, Size);
if (ret)
{
memcpy(ret, buf, l);
memcpy(ret + l, frame, SizeX * SizeY * 3);
}
}
free(frame);
return ret;
}
void cOmxDevice::Clear(void)
{
DBG("Clear()");
m_mutex.Lock();
FlushStreams();
m_hasAudio = false;
m_hasVideo = false;
m_mutex.Unlock();
cDevice::Clear();
}
void cOmxDevice::Play(void)
{
DBG("Play()");
m_mutex.Lock();
m_playbackSpeed = eNormal;
m_direction = eForward;
m_omx.SetClockScale(s_playbackSpeeds[m_direction][m_playbackSpeed]);
m_mutex.Unlock();
cDevice::Play();
}
void cOmxDevice::Freeze(void)
{
DBG("Freeze()");
m_mutex.Lock();
m_omx.SetClockScale(s_playbackSpeeds[eForward][ePause]);
m_mutex.Unlock();
cDevice::Freeze();
}
#if APIVERSNUM >= 20103
void cOmxDevice::TrickSpeed(int Speed, bool Forward)
{
m_mutex.Lock();
ApplyTrickSpeed(Speed, Forward);
m_mutex.Unlock();
}
#else
void cOmxDevice::TrickSpeed(int Speed)
{
m_mutex.Lock();
m_audioPts = 0;
m_videoPts = 0;
m_playDirection = 0;
// play direction is ambiguous for fast modes, start PTS tracking
if (Speed == 1 || Speed == 3 || Speed == 6)
m_trickRequest = Speed;
else
ApplyTrickSpeed(Speed, (Speed == 8 || Speed == 4 || Speed == 2));
m_mutex.Unlock();
}
#endif
void cOmxDevice::ApplyTrickSpeed(int trickSpeed, bool forward)
{
m_direction = forward ? eForward : eBackward;
m_playbackSpeed =
// slow forward
trickSpeed == 8 ? eSlowest :
trickSpeed == 4 ? eSlower :
trickSpeed == 2 ? eSlow :
// fast for-/backward
trickSpeed == 6 ? eFast :
trickSpeed == 3 ? eFaster :
trickSpeed == 1 ? eFastest :
// slow backward
trickSpeed == 63 ? eSlowest :
trickSpeed == 48 ? eSlower :
trickSpeed == 24 ? eSlow : eNormal;
m_omx.SetClockScale(s_playbackSpeeds[m_direction][m_playbackSpeed]);
DBG("ApplyTrickSpeed(%s, %s)",
PlaybackSpeedStr(m_playbackSpeed), DirectionStr(m_direction));
return;
}
void cOmxDevice::PtsTracker(int64_t ptsDiff)
{
DBG("PtsTracker(%lld)", ptsDiff);
if (ptsDiff < 0)
--m_playDirection;
else if (ptsDiff > 0)
m_playDirection += 2;
if (m_playDirection < -2 || m_playDirection > 3)
{
ApplyTrickSpeed(m_trickRequest, m_playDirection > 0);
m_trickRequest = 0;
}
}
bool cOmxDevice::HasIBPTrickSpeed(void)
{
return !m_hasVideo;
}
void cOmxDevice::AdjustLiveSpeed(void)
{
if (m_timer.TimedOut())
{
int usedBuffers, usedAudioBuffers, usedVideoBuffers;
m_omx.GetBufferUsage(usedAudioBuffers, usedVideoBuffers);
usedBuffers = m_hasAudio ? usedAudioBuffers : usedVideoBuffers;
if (usedBuffers < 5)
m_liveSpeed = eNegCorrection;
else if (usedBuffers > 15)
m_liveSpeed = ePosCorrection;
else if ((usedBuffers > 10 && m_liveSpeed == eNegCorrection) ||
(usedBuffers < 10 && m_liveSpeed == ePosCorrection))
m_liveSpeed = eNoCorrection;
#ifdef DEBUG_BUFFERSTAT
DLOG("buffer usage: A=%3d%%, V=%3d%%, Corr=%d",
usedAudioBuffers, usedVideoBuffers,
m_liveSpeed == eNegMaxCorrection ? -2 :
m_liveSpeed == eNegCorrection ? -1 :
m_liveSpeed == eNoCorrection ? 0 :
m_liveSpeed == ePosCorrection ? 1 :
m_liveSpeed == ePosMaxCorrection ? 2 : 0);
#endif
m_omx.SetClockScale(s_liveSpeeds[m_liveSpeed]);
m_timer.Set(1000);
}
}
void cOmxDevice::HandleBufferStall()
{
ELOG("buffer stall!");
m_mutex.Lock();
FlushStreams(true);
m_omx.StopVideo();
m_hasAudio = false;
m_hasVideo = false;
m_videoCodec = cVideoCodec::eInvalid;
m_mutex.Unlock();
}
void cOmxDevice::HandleEndOfStream()
{
DBG("HandleEndOfStream()");
m_mutex.Lock();
// flush pipes and restart clock after still image
FlushStreams();
m_omx.SetClockScale(s_playbackSpeeds[m_direction][m_playbackSpeed]);
m_omx.StartClock(m_hasVideo, m_hasAudio,
Transferring() ? PRE_ROLL_LIVE : PRE_ROLL_PLAYBACK);
m_mutex.Unlock();
}
void cOmxDevice::HandleStreamStart()
{
DBG("HandleStreamStart()");
const cVideoFrameFormat *format = m_omx.GetVideoFrameFormat();
DLOG("video stream started %dx%d@%d%s, PAR=%d/%d",
format->width, format->height, format->frameRate,
format->Interlaced() ? "i" : "p",
format->pixelWidth, format->pixelHeight);
HandleVideoSetupChanged();
}
void cOmxDevice::HandleVideoSetupChanged()
{
// apply framing parameters
switch (cRpiSetup::GetVideoFraming())
{
default:
case cVideoFraming::eFrame:
m_omx.SetDisplayMode(false, false);
break;
case cVideoFraming::eCut:
m_omx.SetDisplayMode(true, false);
break;
case cVideoFraming::eStretch:
m_omx.SetDisplayMode(true, true);
break;
}
const cVideoFrameFormat *format = m_omx.GetVideoFrameFormat();
double videoPAR = format->pixelHeight ?
(double)format->pixelWidth / format->pixelHeight : 1.0f;
// update display format according current video stream
cRpiDisplay::SetVideoFormat(format);
// get updated display format ...
int width, height;
double displayPAR;
cRpiDisplay::GetSize(width, height, displayPAR);
// ... and set video render format accordingly
cRational renderPAR = cRational(videoPAR / displayPAR);
renderPAR.Reduce(100);
m_omx.SetPixelAspectRatio(renderPAR.num, renderPAR.den);
DLOG("display PAR=%0.3f, setting video render PAR=%d/%d",
displayPAR, renderPAR.num, renderPAR.den);
}
void cOmxDevice::FlushStreams(bool flushVideoRender)
{
DBG("FlushStreams(%s)", flushVideoRender ? "flushVideoRender" : "");
m_omx.StopClock();
if (m_hasVideo)
m_omx.FlushVideo(flushVideoRender);
if (m_hasAudio)
m_audio.Reset();
m_omx.ResetClock();
}
void cOmxDevice::SetVolumeDevice(int Volume)
{
DBG("SetVolume(%d)", Volume);
if (Volume)
{
m_omx.SetVolume(Volume);
m_omx.SetMute(false);
}
else
m_omx.SetMute(true);
}
bool cOmxDevice::Poll(cPoller &Poller, int TimeoutMs)
{
cTimeMs timer(TimeoutMs);
while (!m_omx.PollVideo() || !m_audio.Poll())
{
if (timer.TimedOut())
return false;
cCondWait::SleepMs(5);
}
return true;
}
void cOmxDevice::MakePrimaryDevice(bool On)
{
if (On && m_onPrimaryDevice)
m_onPrimaryDevice();
cDevice::MakePrimaryDevice(On);
}
cVideoCodec::eCodec cOmxDevice::ParseVideoCodec(const uchar *data, int length)
{
const uchar *p = data;
for (int i = 0; (i < 5) && (i + 4 < length); i++)
{
// find start code prefix - should be right at the beginning of payload
if ((!p[i] && !p[i + 1] && p[i + 2] == 0x01))
{
if (p[i + 3] == 0xb3) // sequence header
return cVideoCodec::eMPEG2;
//p[i + 3] = 0xf0
else if (p[i + 3] == 0x09) // slice
{
// quick hack for converted mkvs
if (p[i + 4] == 0xf0)
return cVideoCodec::eH264;
switch (p[i + 4] >> 5)
{
case 0: case 3: case 5: // I frame
return cVideoCodec::eH264;
case 2: case 7: // B frame
case 1: case 4: case 6: // P frame
default:
// return cVideoCodec::eInvalid;
return cVideoCodec::eH264;
}
}
return cVideoCodec::eInvalid;
}
}
return cVideoCodec::eInvalid;
}