/
httplivestreambuffer.cpp
2969 lines (2704 loc) · 83.1 KB
/
httplivestreambuffer.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
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* httplivestreambuffer.cpp
* MythTV
*
* Created by Jean-Yves Avenard on 6/05/12.
* Copyright (c) 2012 Bubblestuff Pty Ltd. All rights reserved.
*
* Based on httplive.c by Jean-Paul Saman <jpsaman _AT_ videolan _DOT_ 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 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.
*****************************************************************************/
// QT
#include <QObject>
#include <QString>
#include <QStringList>
#include <QtAlgorithms>
#include <QUrl>
// C++
#include <algorithm> // for min/max
using std::max;
using std::min;
// Posix
#include <sys/time.h> // for gettimeofday
// libmythbase
#include "mthread.h"
#include "mythdownloadmanager.h"
#include "mythlogging.h"
// libmythtv
#include "httplivestreambuffer.h"
#ifdef USING_LIBCRYPTO
// encryption related stuff
#include <openssl/aes.h>
#define AES_BLOCK_SIZE 16 // HLS only support AES-128
#endif
#define LOC QString("HLSBuffer: ")
// Constants
#define PLAYBACK_MINBUFFER 2 // number of segments to prefetch before playback starts
#define PLAYBACK_READAHEAD 6 // number of segments download queue ahead of playback
#define PLAYLIST_FAILURE 6 // number of consecutive failures after which
// playback will abort
enum
{
RET_ERROR = -1,
RET_OK = 0,
};
/* utility methods */
static QString decoded_URI(const QString &uri)
{
QByteArray ba = uri.toLatin1();
QUrl url = QUrl::fromEncoded(ba);
return url.toString();
}
static QString relative_URI(const QString &surl, const QString &spath)
{
QUrl url = QUrl(surl);
QUrl path = QUrl(spath);
if (!path.isRelative())
{
return spath;
}
return url.resolved(path).toString();
}
static uint64_t mdate(void)
{
timeval t {};
gettimeofday(&t, nullptr);
return t.tv_sec * 1000000ULL + t.tv_usec;
}
static bool downloadURL(const QString &url, QByteArray *buffer)
{
MythDownloadManager *mdm = GetMythDownloadManager();
return mdm->download(url, buffer);
}
static void cancelURL(const QString &url)
{
MythDownloadManager *mdm = GetMythDownloadManager();
mdm->cancelDownload(url);
}
static void cancelURL(const QStringList &urls)
{
MythDownloadManager *mdm = GetMythDownloadManager();
mdm->cancelDownload(urls);
}
/* segment container */
class HLSSegment
{
public:
HLSSegment(const int mduration, const int id, const QString &title,
const QString &uri, const QString ¤t_key_path)
{
m_duration = mduration; /* seconds */
m_id = id;
m_url = uri;
m_title = title;
#ifdef USING_LIBCRYPTO
m_psz_key_path = current_key_path;
#else
Q_UNUSED(current_key_path);
#endif
}
~HLSSegment() = default;
HLSSegment(const HLSSegment &rhs)
{
*this = rhs;
}
HLSSegment &operator=(const HLSSegment &rhs)
{
if (this == &rhs)
return *this;
m_id = rhs.m_id;
m_duration = rhs.m_duration;
m_bitrate = rhs.m_bitrate;
m_url = rhs.m_url;
// keep the old data downloaded
// m_data = m_data;
// m_played = m_played;
m_title = rhs.m_title;
#ifdef USING_LIBCRYPTO
m_psz_key_path = rhs.m_psz_key_path;
memcpy(&m_aeskey, &(rhs.m_aeskey), sizeof(m_aeskey));
m_keyloaded = rhs.m_keyloaded;
#endif
m_downloading = rhs.m_downloading;
return *this;
}
int Duration(void) const
{
return m_duration;
}
int Id(void) const
{
return m_id;
}
void Lock(void)
{
m_lock.lock();
}
void Unlock(void)
{
m_lock.unlock();
}
bool IsEmpty(void) const
{
return m_data.isEmpty();
}
int32_t Size(void) const
{
return m_data.size();
}
int Download(void)
{
// must own lock
m_downloading = true;
bool ret = downloadURL(m_url, &m_data);
m_downloading = false;
// didn't succeed, clear buffer
if (!ret)
{
m_data.clear();
return RET_ERROR;
}
return RET_OK;
}
void CancelDownload(void)
{
if (m_downloading)
{
cancelURL(m_url);
QMutexLocker lock(&m_lock);
m_downloading = false;
}
}
QString Url(void) const
{
return m_url;
}
int32_t SizePlayed(void) const
{
return m_played;
}
uint32_t Read(uint8_t *buffer, int32_t length, FILE *fd = nullptr)
{
int32_t left = m_data.size() - m_played;
if (length > left)
{
length = left;
}
if (buffer != nullptr)
{
memcpy(buffer, m_data.constData() + m_played, length);
// write data to disk if required
if (fd)
{
fwrite(m_data.constData() + m_played, length, 1, fd);
}
}
m_played += length;
return length;
}
void Reset(void)
{
m_played = 0;
}
void Clear(void)
{
m_played = 0;
m_data.clear();
}
QString Title(void) const
{
return m_title;
}
void SetTitle(const QString &x)
{
m_title = x;
}
/**
* provides pointer to raw segment data
*/
const char *Data(void) const
{
return m_data.constData();
}
#ifdef USING_LIBCRYPTO
int DownloadKey(void)
{
// must own lock
if (m_keyloaded)
return RET_OK;
QByteArray key;
bool ret = downloadURL(m_psz_key_path, &key);
if (!ret || key.size() != AES_BLOCK_SIZE)
{
if (ret)
{
LOG(VB_PLAYBACK, LOG_ERR, LOC +
QString("The AES key loaded doesn't have the right size (%1)")
.arg(key.size()));
}
else
{
LOG(VB_PLAYBACK, LOG_ERR, LOC + "Failed to download AES key");
}
return RET_ERROR;
}
AES_set_decrypt_key((const unsigned char*)key.constData(), 128, &m_aeskey);
m_keyloaded = true;
return RET_OK;
}
int DecodeData(const uint8_t *IV)
{
/* Decrypt data using AES-128 */
int aeslen = m_data.size() & ~0xf;
unsigned char iv[AES_BLOCK_SIZE];
char *decrypted_data = new char[m_data.size()];
if (IV == nullptr)
{
/*
* If the EXT-X-KEY tag does not have the IV attribute, implementations
* MUST use the sequence number of the media file as the IV when
* encrypting or decrypting that media file. The big-endian binary
* representation of the sequence number SHALL be placed in a 16-octet
* buffer and padded (on the left) with zeros.
*/
memset(iv, 0, AES_BLOCK_SIZE);
iv[15] = m_id & 0xff;
iv[14] = (m_id >> 8) & 0xff;
iv[13] = (m_id >> 16) & 0xff;
iv[12] = (m_id >> 24) & 0xff;
}
else
{
memcpy(iv, IV, sizeof(iv));
}
AES_cbc_encrypt((unsigned char*)m_data.constData(),
(unsigned char*)decrypted_data, aeslen,
&m_aeskey, iv, AES_DECRYPT);
memcpy(decrypted_data + aeslen, m_data.constData() + aeslen,
m_data.size() - aeslen);
// remove the PKCS#7 padding from the buffer
int pad = decrypted_data[m_data.size()-1];
if (pad <= 0 || pad > AES_BLOCK_SIZE)
{
LOG(VB_PLAYBACK, LOG_ERR, LOC +
QString("bad padding character (0x%1)").arg(pad, 0, 16, QLatin1Char('0')));
delete[] decrypted_data;
return RET_ERROR;
}
aeslen = m_data.size() - pad;
m_data = QByteArray(decrypted_data, aeslen);
delete[] decrypted_data;
return RET_OK;
}
bool HasKeyPath(void) const
{
return !m_psz_key_path.isEmpty();
}
bool KeyLoaded(void) const
{
return m_keyloaded;
}
QString KeyPath(void) const
{
return m_psz_key_path;
}
void SetKeyPath(const QString &path)
{
m_psz_key_path = path;
}
void CopyAESKey(const HLSSegment &segment)
{
memcpy(&m_aeskey, &(segment.m_aeskey), sizeof(m_aeskey));
m_keyloaded = segment.m_keyloaded;
}
private:
AES_KEY m_aeskey {}; // AES-128 key
bool m_keyloaded {false};
QString m_psz_key_path; // URL key path
#endif
private:
int m_id; // unique sequence number
int m_duration {0}; // segment duration (seconds)
uint64_t m_bitrate {0}; // bitrate of segment's content (bits per second)
QString m_title; // human-readable informative title of the media segment
QString m_url;
QByteArray m_data; // raw data
int32_t m_played {0}; // bytes counter of data already read from segment
QMutex m_lock;
bool m_downloading {false};
};
/* stream class */
class HLSStream
{
public:
HLSStream(const int mid, const uint64_t bitrate, const QString &uri)
{
m_id = mid;
m_bitrate = bitrate;
m_url = uri;
#ifdef USING_LIBCRYPTO
memset(m_AESIV, 0, sizeof(m_AESIV));
#endif
}
HLSStream(const HLSStream &rhs, bool copy = true)
{
(*this) = rhs;
if (!copy)
return;
// copy all the segments across
QList<HLSSegment*>::iterator it = m_segments.begin();
for (; it != m_segments.end(); ++it)
{
const HLSSegment *old = *it;
HLSSegment *segment = new HLSSegment(*old);
AppendSegment(segment);
}
}
~HLSStream()
{
QList<HLSSegment*>::iterator it = m_segments.begin();
for (; it != m_segments.end(); ++it)
{
delete *it;
}
}
HLSStream &operator=(const HLSStream &rhs)
{
if (this == &rhs)
return *this;
// do not copy segments
m_id = rhs.m_id;
m_version = rhs.m_version;
m_startsequence = rhs.m_startsequence;
m_targetduration= rhs.m_targetduration;
m_bitrate = rhs.m_bitrate;
m_size = rhs.m_size;
m_duration = rhs.m_duration;
m_live = rhs.m_live;
m_url = rhs.m_url;
m_cache = rhs.m_cache;
#ifdef USING_LIBCRYPTO
m_keypath = rhs.m_keypath;
m_ivloaded = rhs.m_ivloaded;
memcpy(m_AESIV, rhs.m_AESIV, sizeof(m_AESIV));
#endif
return *this;
}
static bool IsGreater(const HLSStream *s1, const HLSStream *s2)
{
return s1->Bitrate() > s2->Bitrate();
}
bool operator<(const HLSStream &b) const
{
return this->Bitrate() < b.Bitrate();
}
bool operator>(const HLSStream &b) const
{
return this->Bitrate() > b.Bitrate();
}
/**
* Return the estimated size of the stream in bytes
* if a segment hasn't been downloaded, its size is estimated according
* to the bandwidth of the stream
*/
uint64_t Size(bool force = false)
{
if (m_size > 0 && !force)
return m_size;
QMutexLocker lock(&m_lock);
int64_t size = 0;
int count = NumSegments();
for (int i = 0; i < count; i++)
{
HLSSegment *segment = GetSegment(i);
segment->Lock();
if (segment->Size() > 0)
{
size += (int64_t)segment->Size();
}
else
{
size += segment->Duration() * Bitrate() / 8;
}
segment->Unlock();
}
m_size = size;
return m_size;
}
int64_t Duration(void)
{
QMutexLocker lock(&m_lock);
return m_duration;
}
void Clear(void)
{
m_segments.clear();
}
int NumSegments(void) const
{
return m_segments.size();
}
void AppendSegment(HLSSegment *segment)
{
// must own lock
m_segments.append(segment);
}
HLSSegment *GetSegment(const int wanted) const
{
int count = NumSegments();
if (count <= 0)
return nullptr;
if ((wanted < 0) || (wanted >= count))
return nullptr;
return m_segments[wanted];
}
HLSSegment *FindSegment(const int id, int *segnum = nullptr) const
{
int count = NumSegments();
if (count <= 0)
return nullptr;
for (int n = 0; n < count; n++)
{
HLSSegment *segment = GetSegment(n);
if (segment == nullptr)
break;
if (segment->Id() == id)
{
if (segnum != nullptr)
{
*segnum = n;
}
return segment;
}
}
return nullptr;
}
void AddSegment(const int duration, const QString &title, const QString &uri)
{
QMutexLocker lock(&m_lock);
QString psz_uri = relative_URI(m_url, uri);
int id = NumSegments() + m_startsequence;
#ifndef USING_LIBCRYPTO
QString m_keypath;
#endif
HLSSegment *segment = new HLSSegment(duration, id, title, psz_uri,
m_keypath);
AppendSegment(segment);
m_duration += duration;
}
void RemoveSegment(HLSSegment *segment, bool willdelete = true)
{
QMutexLocker lock(&m_lock);
m_duration -= segment->Duration();
if (willdelete)
{
delete segment;
}
int count = NumSegments();
if (count <= 0)
return;
for (int n = 0; n < count; n++)
{
HLSSegment *old = GetSegment(n);
if (old == segment)
{
m_segments.removeAt(n);
break;
}
}
}
void RemoveSegment(int segnum, bool willdelete = true)
{
QMutexLocker lock(&m_lock);
HLSSegment *segment = GetSegment(segnum);
m_duration -= segment->Duration();
if (willdelete)
{
delete segment;
}
m_segments.removeAt(segnum);
}
void RemoveListSegments(QMap<HLSSegment*,bool> &table)
{
QMap<HLSSegment*,bool>::iterator it;
for (it = table.begin(); it != table.end(); ++it)
{
bool todelete = *it;
HLSSegment *p = it.key();
RemoveSegment(p, todelete);
}
}
int DownloadSegmentData(int segnum, uint64_t &bandwidth, int stream)
{
HLSSegment *segment = GetSegment(segnum);
if (segment == nullptr)
return RET_ERROR;
segment->Lock();
if (!segment->IsEmpty())
{
/* Segment already downloaded */
segment->Unlock();
return RET_OK;
}
LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
QString("started download of segment %1 [%2/%3] using stream %4")
.arg(segnum).arg(segment->Id()).arg(NumSegments()+m_startsequence)
.arg(stream));
/* sanity check - can we download this segment on time? */
if ((bandwidth > 0) && (m_bitrate > 0))
{
uint64_t size = (segment->Duration() * m_bitrate); /* bits */
int estimated = (int)(size / bandwidth);
if (estimated > segment->Duration())
{
LOG(VB_PLAYBACK, LOG_INFO, LOC +
QString("downloading of segment %1 [id:%2] will take %3s, "
"which is longer than its playback (%4s) at %5bit/s")
.arg(segnum)
.arg(segment->Id())
.arg(estimated)
.arg(segment->Duration())
.arg(bandwidth));
}
}
uint64_t start = mdate();
if (segment->Download() != RET_OK)
{
LOG(VB_PLAYBACK, LOG_ERR, LOC +
QString("downloaded segment %1 [id:%2] from stream %3 failed")
.arg(segnum).arg(segment->Id()).arg(m_id));
segment->Unlock();
return RET_ERROR;
}
uint64_t downloadduration = mdate() - start;
if (m_bitrate == 0 && segment->Duration() > 0)
{
/* Try to estimate the bandwidth for this stream */
m_bitrate = (uint64_t)(((double)segment->Size() * 8) /
((double)segment->Duration()));
}
#ifdef USING_LIBCRYPTO
/* If the segment is encrypted, decode it */
if (segment->HasKeyPath())
{
/* Do we have loaded the key ? */
if (!segment->KeyLoaded())
{
if (ManageSegmentKeys() != RET_OK)
{
LOG(VB_PLAYBACK, LOG_ERR, LOC +
"couldn't retrieve segment AES-128 key");
segment->Unlock();
return RET_OK;
}
}
if (segment->DecodeData(m_ivloaded ? m_AESIV : nullptr) != RET_OK)
{
segment->Unlock();
return RET_ERROR;
}
}
#endif
segment->Unlock();
downloadduration = downloadduration < 1 ? 1 : downloadduration;
bandwidth = segment->Size() * 8 * 1000000ULL / downloadduration; /* bits / s */
LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
QString("downloaded segment %1 [id:%2] took %3ms for %4 bytes: bandwidth:%5kiB/s")
.arg(segnum)
.arg(segment->Id())
.arg(downloadduration / 1000)
.arg(segment->Size())
.arg(bandwidth / 8192.0));
return RET_OK;
}
int Id(void) const
{
return m_id;
}
int Version(void) const
{
return m_version;
}
void SetVersion(int x)
{
m_version = x;
}
int StartSequence(void) const
{
return m_startsequence;
}
void SetStartSequence(int x)
{
m_startsequence = x;
}
int TargetDuration(void) const
{
return m_targetduration;
}
void SetTargetDuration(int x)
{
m_targetduration = x;
}
uint64_t Bitrate(void) const
{
return m_bitrate;
}
bool Cache(void) const
{
return m_cache;
}
void SetCache(bool x)
{
m_cache = x;
}
bool Live(void) const
{
return m_live;
}
void SetLive(bool x)
{
m_live = x;
}
void Lock(void)
{
m_lock.lock();
}
void Unlock(void)
{
m_lock.unlock();
}
QString Url(void) const
{
return m_url;
}
void UpdateWith(const HLSStream &upd)
{
QMutexLocker lock(&m_lock);
m_targetduration = upd.m_targetduration < 0 ?
m_targetduration : upd.m_targetduration;
m_cache = upd.m_cache;
}
void Cancel(void)
{
QMutexLocker lock(&m_lock);
QList<HLSSegment*>::iterator it = m_segments.begin();
for (; it != m_segments.end(); ++it)
{
if (*it)
{
(*it)->CancelDownload();
}
}
}
#ifdef USING_LIBCRYPTO
/**
* Will download all required segment AES-128 keys
* Will try to re-use already downloaded keys if possible
*/
int ManageSegmentKeys()
{
HLSSegment *seg = nullptr;
HLSSegment *prev_seg = nullptr;
int count = NumSegments();
for (int i = 0; i < count; i++)
{
prev_seg = seg;
seg = GetSegment(i);
if (seg == nullptr )
continue;
if (!seg->HasKeyPath())
continue; /* No key to load ? continue */
if (seg->KeyLoaded())
continue; /* The key is already loaded */
/* if the key has not changed, and already available from previous segment,
* try to copy it, and don't load the key */
if (prev_seg && prev_seg->KeyLoaded() &&
(seg->KeyPath() == prev_seg->KeyPath()))
{
seg->CopyAESKey(*prev_seg);
continue;
}
if (seg->DownloadKey() != RET_OK)
return RET_ERROR;
}
return RET_OK;
}
bool SetAESIV(QString line)
{
/*
* If the EXT-X-KEY tag has the IV attribute, implementations MUST use
* the attribute value as the IV when encrypting or decrypting with that
* key. The value MUST be interpreted as a 128-bit hexadecimal number
* and MUST be prefixed with 0x or 0X.
*/
if (!line.startsWith(QLatin1String("0x"), Qt::CaseInsensitive))
return false;
if (line.size() % 2)
{
// not even size, pad with front 0
line.insert(2, QLatin1String("0"));
}
int padding = max(0, AES_BLOCK_SIZE - (line.size() - 2));
QByteArray ba = QByteArray(padding, 0x0);
ba.append(QByteArray::fromHex(QByteArray(line.toLatin1().constData() + 2)));
memcpy(m_AESIV, ba.constData(), ba.size());
m_ivloaded = true;
return true;
}
uint8_t *AESIV(void)
{
return m_AESIV;
}
void SetKeyPath(const QString &x)
{
m_keypath = x;
}
private:
QString m_keypath; // URL path of the encrypted key
bool m_ivloaded {false};
uint8_t m_AESIV[AES_BLOCK_SIZE]{0};// IV used when decypher the block
#endif
private:
int m_id; // program id
int m_version {1}; // protocol version should be 1
int m_startsequence {0}; // media starting sequence number
int m_targetduration {-1}; // maximum duration per segment (s)
uint64_t m_bitrate; // bitrate of stream content (bits per second)
uint64_t m_size {0LL}; // stream length is calculated by taking the sum
// foreach segment of (segment->duration * hls->bitrate/8)
int64_t m_duration {0LL}; // duration of the stream in seconds
bool m_live {true};
QList<HLSSegment*> m_segments; // list of segments
QString m_url; // uri to m3u8
QMutex m_lock;
bool m_cache {true};// allow caching
};
// Playback Stream Information
class HLSPlayback
{
public:
HLSPlayback(void) = default;
/* offset is only used from main thread, no need for locking */
uint64_t Offset(void) const
{
return m_offset;
}
void SetOffset(uint64_t val)
{
m_offset = val;
}
void AddOffset(uint64_t val)
{
m_offset += val;
}
int Stream(void)
{
QMutexLocker lock(&m_lock);
return m_stream;
}
void SetStream(int val)
{
QMutexLocker lock(&m_lock);
m_stream = val;
}
int Segment(void)
{
QMutexLocker lock(&m_lock);
return m_segment;
}
void SetSegment(int val)
{
QMutexLocker lock(&m_lock);
m_segment = val;
}
int IncrSegment(void)
{
QMutexLocker lock(&m_lock);
return ++m_segment;
}
private:
uint64_t m_offset {0}; // current offset in media
int m_stream {0}; // current HLSStream
int m_segment {0}; // current segment for playback
QMutex m_lock;
};
// Stream Download Thread
class StreamWorker : public MThread
{
public:
StreamWorker(HLSRingBuffer *parent, int startup, int buffer) : MThread("HLSStream"),
m_parent(parent), m_segment(startup), m_buffer(buffer)
{
}
void Cancel(void)
{
m_interrupted = true;
Wakeup();
m_lock.lock();
// Interrupt on-going downloads of all segments
int streams = m_parent->NumStreams();
for (int i = 0; i < streams; i++)
{
HLSStream *hls = m_parent->GetStream(i);
if (hls)
{
hls->Cancel();
}
}
m_lock.unlock();
wait();
}
int CurrentStream(void)
{
QMutexLocker lock(&m_lock);
return m_stream;
}
int Segment(void)
{
QMutexLocker lock(&m_lock);
return m_segment;
}
void Seek(int val)
{
m_lock.lock();
m_segment = val;
m_lock.unlock();
Wakeup();
}
bool IsAtEnd(bool lock = false)
{
if (lock)
{
m_lock.lock();
}
int count = m_parent->NumSegments();
bool ret = m_segment >= count;
if (lock)
{
m_lock.unlock();
}
return ret;
}
/**
* check that we have at least [count] segments buffered from position [from]
*/
bool GotBufferedSegments(int from, int count) const
{
if (from + count > m_parent->NumSegments())
return false;
for (int i = from; i < from + count; i++)
{
if (StreamForSegment(i, false) < 0)
{
return false;
}
}
return true;
}