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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
|
/*
* responselive.c: VDR on Smart TV plugin
*
* Copyright (C) 2013 T. Lohmar
*
* 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
* Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
*/
/*
*
* TODO: Stop, when the channel is unavailable
*
*/
#include "responselive.h"
#include "httpresource.h"
#include "smarttvfactory.h"
#include "log.h"
#include <vdr/remux.h>
#include <vector>
#include <sys/stat.h>
#define DEBUGPREFIX mLog->getTimeString() << "mReqId= " << mRequest->mReqId << " fd= " << mRequest->mFd
#define OKAY 0
#define ERROR (-1)
#define DEBUG
cLiveRelay::cLiveRelay(cChannel* channel, cDevice* device, string chan_id, int req_id, cHttpResource* req) : cReceiver(channel, 0),
mChannelId(chan_id), mRingBuffer(), mReqId(req_id), mRequest(req), mNotifFd(0), mNotifRequired(true)
{
mLog = Log::getInstance();
pthread_mutex_init(&processedRingLock, NULL);
mNotifFd = mRequest->mFactory->openPipe();
if (mNotifFd == 0) {
*(mLog->log()) << "mReqId= " << mReqId
<< " cLiveRelay::cLiveRelay - Constructor ERROR, mNotifId is ZERO" << endl;
}
SetPids(channel);
// AddPid(channel->Tpid());
// AddPid(channel->Vpid());
// AddPid(channel->Apid(0));
device->SwitchChannel(channel, false);
device->AttachReceiver(this);
// mRingBuffer = new cRingBufferLinear(60000*188, MIN_TS_PACKETS_FOR_FRAME_DETECTOR * TS_SIZE, true, "responselive");
mRingBuffer = new cRingBufferLinear(60000*188);
mRingBuffer->SetTimeouts(0, 100);
#ifndef DEBUG
*(mLog->log()) << "mReqId= " << mReqId << " cLiveRelay::cLiveRelay created for chan_id= " << chan_id << endl;
#endif
};
cLiveRelay::~cLiveRelay() {
close(mNotifFd);
cReceiver::Detach();
delete mRingBuffer;
};
void cLiveRelay::Activate(bool On) {
// this method is called by vdr
if (On) {
// Start();
*(Log::getInstance()->log()) << DEBUGPREFIX
<< " cLiveRelay::Activate called by VDR for chan_id= " << mChannelId <<endl;
}
else {
// Cancel();
*(Log::getInstance()->log()) << DEBUGPREFIX
<< " cLiveRelay::Deactivate for chan_id= " << mChannelId <<endl;
}
}
void cLiveRelay::detachLiveRelay() {
Detach();
}
void cLiveRelay::Receive(uchar* data, int length) {
if (length != 188) {
*(Log::getInstance()->log()) << "ERROR: ******* ts packet unequal 188 Byte Length= " << length << endl;
}
if (data[0] != 0x47) {
*(Log::getInstance()->log()) << "ERROR: ******* Out of Sync " << endl;
}
pthread_mutex_lock(&processedRingLock);
int p = mRingBuffer->Put(data, length);
pthread_mutex_unlock(&processedRingLock);
if (p != length ) {
*(Log::getInstance()->log()) << "cLiveRelay::Receive: Overflow" << endl;
mRingBuffer->ReportOverflow(length - p);
}
if (mNotifRequired ) {
// *(Log::getInstance()->log()) << "cLiveRelay::Receive: Sending Notification" << endl;
mRequest->mFactory->setWriteFlag(mRequest->mFd);
mNotifRequired = false;
ssize_t res = write (mNotifFd, " ", 1);
if (res <= 0)
*(Log::getInstance()->log()) << "cLiveRelay::Receive: Notification NOT sent" << endl;
}
}
//----------------------------------------------
//----------------------------------------------
//----------------------------------------------
cResponseLive::cResponseLive(cHttpResource* req, string chan_id) : cResponseBase(req), mChannelId(chan_id),
mPatPmtGen(NULL), mInStartPhase(true), mFrameDetector(NULL), mVpid(0), mVtype(0), mPpid(0), mStartThreshold(75*188),
mCurOffset (0), mTuneInState(0), mFirstPcr (-1), mFirstPcrOffset(0), mCurPcr(-1), mCurPcrOffset(0), mNoFrames(0) ,
mIFrameOffset(-1), mLowerOffset(0) {
mStartThreshold= mRequest->mFactory->getConfig()->getBuiltInLivePktBuf4Sd() * 188;
*(mLog->log()) << DEBUGPREFIX << " cResponseLive - Constructor "
<< "mStartThreshold= " << mStartThreshold/188 << " Pkts"
<< " Mode= " << mRequest->mFactory->getConfig()->getBuiltInLiveStartMode()
<< endl;
if (InitRelay(chan_id)) {
mRelay->setNotifRequired();
mRequest->mFactory->clrWriteFlag(mRequest->mFd);
}
if (mRelay == NULL) {
*(mLog->log()) << DEBUGPREFIX << " cResponseLive::cResponseLive - Constructor - sending Error" << endl;
sendError(404, "Not Found.", NULL, "004 Resource Busy");
}
}
cResponseLive::~cResponseLive() {
if (mRelay != NULL)
delete mRelay;
if (mPatPmtGen != NULL)
delete mPatPmtGen;
if (mFrameDetector != NULL)
delete mFrameDetector;
}
bool cResponseLive::InitRelay(string channel_id) {
cChannel * channel = Channels.GetByChannelID(tChannelID::FromString(channel_id.c_str()));
int priority = 99;
if (!channel ){
*(mLog->log()) << DEBUGPREFIX
<< " " << channel_id << " No channel found! " << endl;
mRequest->mFactory->OsdStatusMessage("Channel not found!");
return false;
}
char buf [40];
snprintf(buf, 40, "%s", *(channel->GetChannelID()).ToString());
*(mLog->log()) << DEBUGPREFIX
<< " vpid= " << channel->Vpid()
<< " vtype= " << channel->Vtype()
<< " ppid= " << channel->Ppid()
<< endl;
if (channel->Vpid() == 0) {
mStartThreshold= 10 * 188;
*(mLog->log()) << DEBUGPREFIX << " cResponseLive::InitRelay Audio Only service detected. mStartThreshold is " << mStartThreshold
<< " (" << mStartThreshold/188 << " Pkts)" << endl;
mInStartPhase = true;
}
if (channel->Vpid() != 0) {
mVpid = channel->Vpid();
mVtype = channel->Vtype();
mPpid = channel->Ppid();
if (mVtype == 27) {
mStartThreshold= mRequest->mFactory->getConfig()->getBuiltInLivePktBuf4Hd() * 188;
*(mLog->log()) << DEBUGPREFIX << " cResponseLive::InitRelay H.264 detected. mStartThreshold is " << mStartThreshold
<< " (" << mStartThreshold/188 << " Pkts)" << endl;
}
mFrameDetector = new cFrameDetector(channel->Vpid(), channel->Vtype());
mInStartPhase = true;
}
else {
mInStartPhase = false;
}
*(mLog->log()) << DEBUGPREFIX << " cResponseLive::InitRelay channel " << buf << " found" << endl;
#ifndef DEBUG
*(mLog->log()) << DEBUGPREFIX << " vpid= " << channel->Vpid() << " vtype= " << channel->Vtype() << endl;
for (int n = 0; channel->Apid(n); n++) {
*(mLog->log()) << DEBUGPREFIX << " apid= " << channel->Apid(n) << " atype= " << channel->Atype(n)
<< " lang= " << channel->Alang(n) << endl;
}
for (int n = 0; channel->Dpid(n); n++) {
*(mLog->log()) << DEBUGPREFIX << " dpid= " << channel->Dpid(n) << " dtype= " << channel->Dtype(n) << endl;
}
for (int n = 0; channel->Spid(n); n++) {
*(mLog->log()) << DEBUGPREFIX << " spid= " << channel->Spid(n) << " stype= " << channel->SubtitlingType(n)
<< " lang= " << channel->Slang(n) << endl;
}
#endif
cDevice* device = cDevice::GetDevice(channel, priority, true); // last param is live-view
if (!device) {
*(mLog->log()) << DEBUGPREFIX << " cLiveRelay::create: No device found to receive this channel at this priority. chan_id= "
<< channel_id << endl;
return false;
}
mRelay = new cLiveRelay(channel, device, channel_id, mRequest->mReqId, mRequest);
mPatPmtGen = new cPatPmtGenerator(channel);
return true;
}
void cResponseLive::WritePatPmt() {
// mBlkPos = 0;
// mBlkLen = 0;
uchar* ptr = mPatPmtGen->GetPat();
memcpy(mBlkData + mBlkLen, ptr, 188);
mBlkLen += 188;
int idx = 0;
while ((ptr = mPatPmtGen->GetPmt(idx)) != NULL) {
memcpy(mBlkData+mBlkLen, ptr, 188);
mBlkLen += 188;
};
}
int cResponseLive::fillDataBlk() {
if (mError)
return ERROR;
switch (mRequest->mFactory->getConfig()->getBuiltInLiveStartMode()){
case 0:
return fillDataBlk_straight();
break;
case 3:
return fillDataBlk_duration();
break;
case 4:
default:
return fillDataBlk_iPlusDuration();
break;
};
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk() - ERROR: should not be here " << endl;
return ERROR;
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
int cResponseLive::fillDataBlk_straight() {
mBlkPos = 0;
mBlkLen = 0;
if (mError)
return ERROR;
int threshold = (mInStartPhase) ? mStartThreshold : (1*188);
if (mRelay->mRingBuffer->Available() >= threshold) {
pthread_mutex_lock(&(mRelay->processedRingLock));
int r;
char *b = (char*)mRelay->mRingBuffer->Get(r);
if (b != NULL) {
if (mInStartPhase)
sendHeaders(200, "OK", NULL, "video/mpeg", -1, -1);
mInStartPhase = false;
if ((r % 188) != 0) {
*(mLog->log()) << DEBUGPREFIX
<< " ERROR: r= " << r << " %188 = " << (r%188) << endl;
}
WritePatPmt();
int len = (r > (MAXLEN - mBlkLen)) ? (MAXLEN - mBlkLen): r;
if (r > (MAXLEN - mBlkLen)) {
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk() mInStartPhase = false - ((r - offset ) > (MAXLEN - mBlkLen)) len= " << len
<< " MAXLEN= " << MAXLEN
<< " mBlkLen= " << mBlkLen
<< endl;
}
memcpy(mBlkData + mBlkLen, b, len);
mBlkLen += len;
mRelay->mRingBuffer->Del(len);
} // if (b != NULL)
else {
*(mLog->log()) << DEBUGPREFIX << " WARNING - cResponseLive::fillDataBlk() - b== NULL" << endl;
}
pthread_mutex_unlock(&(mRelay->processedRingLock));
}
else {
mRequest->mFactory->clrWriteFlag(mRequest->mFd);
mRelay->setNotifRequired();
}
return OKAY;
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
int cResponseLive::fillDataBlk_duration() {
mBlkPos = 0;
mBlkLen = 0;
if (mError)
return ERROR;
if (mInStartPhase) {
pthread_mutex_lock(&(mRelay->processedRingLock));
int r;
char *b = (char*)mRelay->mRingBuffer->Get(r);
if (b != NULL) {
if ((r % 188) != 0) {
*(mLog->log()) << DEBUGPREFIX << " ERROR: r= " << r << " %188 = " << (r%188) << endl;
}
*(mLog->log()) << DEBUGPREFIX
<< " --------------- pkts= " << r / 188 << endl;
while ((mCurOffset +188) <= r) {
// while ((offset +188) <= r) {
if (TsPid((const uchar*) (b+mCurOffset)) == mPpid){
int64_t pcr = TsGetPcr((const uchar*) (b+mCurOffset));
if (pcr != -1) {
// pcr found
// *(mLog->log()) << DEBUGPREFIX << " pcr found offset= " << offset << " pcr= " <<pcr << " pkt= " << offset / 188<< endl;
if (mCurPcr == -1){
mCurPcr = pcr;
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk_duration() - first_pcr= " << mCurPcr
<< " pkt= " << mCurOffset / 188 << endl;
if (mCurOffset != 0) {
// deleting the packets before the pcr packet.
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk_duration() - deleting packets " << mCurOffset
<< " pkt= " << mCurOffset / 188 << endl;
mRelay->mRingBuffer->Del(mCurOffset);
pthread_mutex_unlock(&(mRelay->processedRingLock));
// sleep for a while
mRequest->mFactory->clrWriteFlag(mRequest->mFd);
mRelay->setNotifRequired();
return OKAY;
}
mCurOffset += 188;
continue; // process next packet
}
if (mCurPcr > pcr) {
mCurPcr = pcr;
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk_duration() - ERROR - wired. setting new first_pcr= " << mCurPcr
<< " pkt= " << mCurOffset / 188 << endl;
mCurOffset += 188;
continue; // process next packet
}
// if ((pcr - first_pcr) > (0.8 * 27000000.0)) {
if ((pcr - mCurPcr) > (mRequest->mFactory->getConfig()->getBuiltInLiveBufDur() * 27000000.0)) {
// ok start now
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk_duration() - starting "
<< "pcr= " << pcr
<< "(pcr - first_pcr) / 27000000.0 = "
<< (pcr - mCurPcr) / 27000000.0
<< " ts pkts= " << ((mCurOffset +188)/ 188)
<< endl;
mInStartPhase = false;
sendHeaders(200, "OK", NULL, "video/mpeg", -1, -1);
WritePatPmt();
int len = (r > (MAXLEN - mBlkLen)) ? (MAXLEN - mBlkLen): r;
if (r > (MAXLEN - mBlkLen)) {
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk_duration() mInStartPhase = false - ((r - offset ) > (MAXLEN - mBlkLen)) len= " << len
<< " MAXLEN= " << MAXLEN
<< " mBlkLen= " << mBlkLen
<< endl;
}
memcpy(mBlkData + mBlkLen, b, len);
mBlkLen += len;
mRelay->mRingBuffer->Del(len);
break;
} // if ((pcr - first_pcr) > (0.25 * 90000.0)) {
} // if (pcr != -1) {
} // if (TsPid((const uchar*) (b+offset)) == mPpid){
mCurOffset += 188;
} // while ((offset +188) <= r) {
} // if (b != NULL) {
else
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk_duration() - b is ZERO" << endl;
if (mInStartPhase) {
// still in start Phase, so wait
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk_duration() - still in start phase, so wait" << endl;
mRequest->mFactory->clrWriteFlag(mRequest->mFd);
mRelay->setNotifRequired();
}
pthread_mutex_unlock(&(mRelay->processedRingLock));
} // if (mInStartPhase) {
else {
if (mRelay->mRingBuffer->Available() < 188) {
// nothing to send, sleep
mRequest->mFactory->clrWriteFlag(mRequest->mFd);
mRelay->setNotifRequired();
return OKAY;
}
pthread_mutex_lock(&(mRelay->processedRingLock));
int r;
char *b = (char*)mRelay->mRingBuffer->Get(r);
if (b != NULL) {
if ((r % 188) != 0) {
*(mLog->log()) << DEBUGPREFIX << " ERROR: r= " << r << " %188 = " << (r%188) << endl;
}
WritePatPmt();
int len = (r > (MAXLEN - mBlkLen)) ? (MAXLEN - mBlkLen): r;
if (r > (MAXLEN - mBlkLen)) {
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk_duration() mInStartPhase = false - ((r - offset ) > (MAXLEN - mBlkLen)) len= " << len
<< " MAXLEN= " << MAXLEN
<< " mBlkLen= " << mBlkLen
<< endl;
}
memcpy(mBlkData + mBlkLen, b, len);
mBlkLen += len;
mRelay->mRingBuffer->Del(len);
}
else {
*(mLog->log()) << DEBUGPREFIX
<< " cResponseLive::fillDataBlk_duration() - b is ZERO" << endl;
}
pthread_mutex_unlock(&(mRelay->processedRingLock));
} // else
return OKAY;
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
bool cResponseLive::findIFrame(char *b, int r) {
// search from mCurOffset onwards for the iframe.
// return true, when found
cFrameDetector frame_detector(mVpid, mVtype);
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration findIFrame continue in state " << mTuneInState << " at ts " << mCurOffset/188
<< endl;
// run here only if mTuneInState is ZERO
if (mTuneInState != 0)
return true;
while ((mCurOffset +188) <= r) {
if ((b+mCurOffset)[0] != 0x47) {
*(mLog->log()) << " fillDataBlk_iPlusDuration - ERROR: ******* Out of Sync " << endl;
}
if (TsPid((const uchar*) (b+mCurOffset)) == mPpid){
int64_t pcr = TsGetPcr((const uchar*) (b+mCurOffset));
if (pcr != -1) {
if (mFirstPcr == -1) {
mFirstPcr = pcr;
mFirstPcrOffset = mCurOffset;
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - setting mFirstPcr " << mFirstPcr
<< endl;
}
mCurPcr = pcr;
mCurPcrOffset = mCurOffset;
}
} // if (TsPid((const uchar*) (b+offset)) == mPpid){
if (TsPid((const uchar*) (b+mCurOffset)) == mVpid){
//ok. point to frame start now. check whether i frame
if (TsIsScrambled((const uchar*) (b+mCurOffset)))
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - TsIsScrambled" << endl;
if (frame_detector.Analyze((const uchar*) (b+mCurOffset), (r - mCurOffset) ) == 0) {
// not sufficient data. delete and return
return false;
}
if (frame_detector.NewFrame()) {
mNoFrames ++;
}
else {
mCurOffset += 188;
continue;
}
if (frame_detector.IndependentFrame()) {
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration iframe found at " << mCurOffset/188
<< " pkts r= " << r/188 << " mCurPcr= " << mCurPcr
<< " at " << (mCurPcrOffset/188) << endl;
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration mFirstPcr= " << mFirstPcr
<< " (mCurPcr - mFirstPcr) / 27MHz= " << (mCurPcr - mFirstPcr) / 27000000.0
<< endl;
mIFrameOffset = mCurOffset;
mTuneInState = 1;
return true;
break;
}
} // if (TsPid((const uchar*) (b+offset)) == mVpid){
mCurOffset += 188;
} // while ((offset +188) <= r) {
// I should have now the offset of the iframe.
if (mIFrameOffset == -1){
return false;
}
return false;
}
bool cResponseLive::bufferData(char* b, int r) {
// buffer additional data, after the iframe.
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration bufferData continue in state " << mTuneInState << " at ts "<< mCurOffset/188
<< " with mCurPcr= " << mCurPcr
<< endl;
while ((mCurOffset +188) <= r) {
if (TsPid((const uchar*) (b+mCurOffset)) == mPpid){
int64_t pcr = TsGetPcr((const uchar*) (b+mCurOffset));
if (pcr != -1) {
if (mFirstPcr == -1) {
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - WARNING - re setting mFirstPcr " << mCurOffset/188
<< endl;
mFirstPcr = pcr;
mFirstPcrOffset = mCurOffset;
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - setting mFirstPcr " << mFirstPcr
<< endl;
}
if (mCurPcr == -1){
mCurPcr = pcr;
mCurPcrOffset = mCurOffset;
mCurOffset += 188;
continue; // process next packet
}
if (mCurPcr > pcr) {
mCurPcr = pcr;
mCurPcrOffset = mCurOffset;
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - ERROR - wired. setting new mCurPcr= " << mCurPcr
<< " pkt= " << mCurOffset / 188 << endl;
mCurOffset += 188;
continue; // process next packet
}
// if ((pcr - first_pcr) > (0.8 * 27000000.0)) {
if ((pcr - mCurPcr) > (mRequest->mFactory->getConfig()->getBuiltInLiveBufDur() * 27000000.0)) {
// OK, sufficient data to start
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - starting "
<< "pcr= " << pcr
<< " mCurPcr= " << mCurPcr
<< " (pcr - mCurPcr ) / 27MHz = "
<< (pcr - mCurPcr) / 27000000.0
<< endl ;
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration --- "
<< " pcr= " << pcr
<< " mFirstPcr= " << mFirstPcr
<< " (pcr - mFirstPcr) / 27MGz= " << (pcr - mFirstPcr ) / 27000000.0
<< endl;
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration mFirstPcr= " << mFirstPcr
<< " (mCurPcr - mFirstPcr) / 27MHz= " << (mCurPcr - mFirstPcr) / 27000000.0
<< endl;
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration "
<< " no_frames " << mNoFrames
<< " ts pkts= " << ((mCurOffset +188)/ 188)
<< " offset= " << mCurOffset
<< endl;
mInStartPhase = false;
mTuneInState = 2;
sendHeaders(200, "OK", NULL, "video/mpeg", -1, -1);
WritePatPmt();
return true;
break;
} // if ((pcr - first_pcr) > (0.8 * 27000000.0)) {
}
} // if (TsPid((const uchar*) (b+offset)) == mPpid){ // find pcr
mCurOffset += 188;
} //while ((offset +188) <= r) { // find pcr
return false;
}
bool cResponseLive::setLowerOffset(char *b, int r) {
mLowerOffset = 0;
if (mCurPcr != -1) {
int idx = 0;
while ((idx +188) <= mIFrameOffset) {
// I only need to run until the offset of the i frame.
if (TsPid((const uchar*) (b+idx)) == mPpid){
int64_t pcr = TsGetPcr((const uchar*) (b+idx));
if (pcr != -1) {
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration find lower_offset pkt= " << idx / 188
<< " pcr= " << pcr
<< " ((mCurPcr - pcr) / 27000000.0)= " << ((mCurPcr - pcr) / 27000000.0)
<< endl;
if (((mCurPcr - pcr) / 27000000.0) < 0.55) {
if (((mCurPcr - pcr) / 27000000.0) > 0.48) {
// from here.
mLowerOffset = idx;
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - lower_offset= " << mLowerOffset
<< " pcr= " << pcr
<< " ((pcr - mFirst_pcr) / 27MHz)= " << (( pcr - mFirstPcr) / 27000000.0)
<< endl;
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - lower_offset= "
<< " ((mCurPcr - pcr ) / 27MHz)= " << (( mCurPcr - pcr ) / 27000000.0)
<< endl;
}
break;
}
}
}
idx += 188;
} // while ((idx +188) <= offset)
}
return true;
}
void cResponseLive::justFillDataBlk(char *b, int r) {
int len = ((r - mLowerOffset) > (MAXLEN - mBlkLen)) ? (MAXLEN - mBlkLen): (r - mLowerOffset);
if (( r - mLowerOffset) > (MAXLEN - mBlkLen)) {
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - mInStartPhase = false - ((r - offset ) > (MAXLEN - mBlkLen)) len= " << len
<< " MAXLEN= " << MAXLEN
<< " mBlkLen= " << mBlkLen
<< endl;
}
memcpy(mBlkData + mBlkLen, (b + mLowerOffset), len);
mBlkLen += len;
mRelay->mRingBuffer->Del(mLowerOffset + len);
};
// new method: 200ms after the IFrame
int cResponseLive::fillDataBlk_iPlusDuration() {
//TODO: Prapably, I need to delete some data before the i_frame offset
mBlkPos = 0;
mBlkLen = 0;
if (mError)
return ERROR;
if (mInStartPhase) {
pthread_mutex_lock(&(mRelay->processedRingLock));
int r;
char *b = (char*)mRelay->mRingBuffer->Get(r);
if (b != NULL) {
if ((r % 188) != 0) {
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - ERROR: r= " << r << " %188 = " << (r%188) << endl;
}
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration --------------- pkts= " << r / 188 << endl;
if((r / 188) > 40000) {
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration Risk an overrun, so starting " << r / 188 << endl;
sendHeaders(200, "OK", NULL, "video/mpeg", -1, -1);
WritePatPmt();
justFillDataBlk(b,r);
mInStartPhase = false;
pthread_mutex_unlock(&(mRelay->processedRingLock));
return OKAY;
}
if (!findIFrame(b, r)) {
pthread_mutex_unlock(&(mRelay->processedRingLock));
// sleep for a while, not sufficient data
mRequest->mFactory->clrWriteFlag(mRequest->mFd);
mRelay->setNotifRequired();
return OKAY;
}
if (!bufferData(b, r)) {
// return;
pthread_mutex_unlock(&(mRelay->processedRingLock));
// sleep for a while, not sufficient data
mRequest->mFactory->clrWriteFlag(mRequest->mFd);
mRelay->setNotifRequired();
return OKAY;
}
else {
// have all data now!
// pthread_mutex_unlock(&(mRelay->processedRingLock));
// return OKAY;
}
// here I have all data.
setLowerOffset(b,r);
justFillDataBlk(b,r);
} // if (b != NULL) { in startPhase
else
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - b is ZERO" << endl;
pthread_mutex_unlock(&(mRelay->processedRingLock));
} // if (mInStartPhase) {
else {
// not in start phase
if (mRelay->mRingBuffer->Available() < 188) {
// nothing to send, sleep
mRequest->mFactory->clrWriteFlag(mRequest->mFd);
mRelay->setNotifRequired();
return OKAY;
}
pthread_mutex_lock(&(mRelay->processedRingLock));
int r;
char *b = (char*)mRelay->mRingBuffer->Get(r);
if (b != NULL) {
if ((r % 188) != 0) {
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - ERROR: r= " << r << " %188 = " << (r%188) << endl;
}
WritePatPmt();
int len = (r > (MAXLEN - mBlkLen)) ? (MAXLEN - mBlkLen): r;
if (r > (MAXLEN - mBlkLen)) {
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - mInStartPhase = false - ((r - offset ) > (MAXLEN - mBlkLen)) len= " << len
<< " MAXLEN= " << MAXLEN
<< " mBlkLen= " << mBlkLen
<< endl;
}
memcpy(mBlkData + mBlkLen, b, len);
mBlkLen += len;
mRelay->mRingBuffer->Del(len);
} // if (b != NULL) {
else {
*(mLog->log()) << DEBUGPREFIX
<< " fillDataBlk_iPlusDuration - b is ZERO" << endl;
}
pthread_mutex_unlock(&(mRelay->processedRingLock));
} // else
return OKAY;
}
|