/
httplivestream.cpp
990 lines (812 loc) · 28.2 KB
/
httplivestream.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
/* -*- Mode: c++ -*-
*
* Class HTTPLiveStream
*
* Copyright (C) Chris Pinkham 2011
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QIODevice>
#include <QRunnable>
#include <QUrl>
#include "mythcorecontext.h"
#include "mythdate.h"
#include "mythdirs.h"
#include "mythtimer.h"
#include "mthreadpool.h"
#include "mythsystem.h"
#include "exitcodes.h"
#include "mythlogging.h"
#include "storagegroup.h"
#include "httplivestream.h"
#define LOC QString("HLS(%1): ").arg(m_sourceFile)
#define LOC_ERR QString("HLS(%1) Error: ").arg(m_sourceFile)
#define SLOC QString("HLS(): ")
#define SLOC_ERR QString("HLS() Error: ")
/** \class HTTPLiveStreamThread
* \brief QRunnable class for running mythtranscode for HTTP Live Streams
*
* The HTTPLiveStreamThread class runs a mythtranscode command in
* non-blocking mode.
*/
class HTTPLiveStreamThread : public QRunnable
{
public:
/** \fn HTTPLiveStreamThread::HTTPLiveStreamThread(int)
* \brief Constructor for creating a SystemEventThread
* \param cmd Command line to run for this System Event
* \param eventName Optional System Event name for this command
*/
HTTPLiveStreamThread(int streamid)
: m_streamID(streamid) {}
/** \fn HTTPLiveStreamThread::run()
* \brief Runs mythtranscode for the given HTTP Live Stream ID
*
* Overrides QRunnable::run()
*/
void run(void)
{
uint flags = kMSDontBlockInputDevs;
QString command = GetInstallPrefix() +
QString("/bin/mythtranscode --hls --hlsstreamid %1")
.arg(m_streamID) + logPropagateArgs;
uint result = myth_system(command, flags);
if (result != GENERIC_EXIT_OK)
LOG(VB_GENERAL, LOG_WARNING, SLOC +
QString("Command '%1' returned %2")
.arg(command).arg(result));
}
private:
int m_streamID;
};
HTTPLiveStream::HTTPLiveStream(QString srcFile, uint16_t width, uint16_t height,
uint32_t bitrate, uint32_t abitrate,
uint16_t maxSegments, uint16_t segmentSize,
uint32_t aobitrate, int32_t srate)
: m_writing(false),
m_streamid(-1), m_sourceFile(srcFile),
m_sourceWidth(0), m_sourceHeight(0),
m_segmentSize(segmentSize), m_maxSegments(maxSegments),
m_segmentCount(0), m_startSegment(0),
m_curSegment(0),
m_height(height), m_width(width),
m_bitrate(bitrate),
m_audioBitrate(abitrate), m_audioOnlyBitrate(aobitrate),
m_sampleRate(srate),
m_created(MythDate::current()),
m_lastModified(MythDate::current()),
m_percentComplete(0),
m_status(kHLSStatusUndefined)
{
if ((m_width == 0) && (m_height == 0))
m_width = 640;
if (m_bitrate == 0)
m_bitrate = 800000;
if (m_audioBitrate == 0)
m_audioBitrate = 64000;
if (m_segmentSize == 0)
m_segmentSize = 10;
if (m_audioOnlyBitrate == 0)
m_audioOnlyBitrate = 32000;
m_sourceHost = gCoreContext->GetHostName();
QFileInfo finfo(m_sourceFile);
m_outBase = finfo.fileName() +
QString(".%1x%2_%3kV_%4kA").arg(m_width).arg(m_height)
.arg(m_bitrate/1000).arg(m_audioBitrate/1000);
SetOutputVars();
m_fullURL = m_httpPrefix + m_outBase + ".m3u8";
m_relativeURL = m_httpPrefixRel + m_outBase + ".m3u8";
StorageGroup sgroup("Streaming", gCoreContext->GetHostName());
m_outDir = sgroup.GetFirstDir();
QDir outDir(m_outDir);
if (!outDir.exists() && !outDir.mkdir(m_outDir))
{
LOG(VB_RECORD, LOG_ERR, "Unable to create HTTP Live Stream output "
"directory, Live Stream will not be created");
return;
}
AddStream();
}
HTTPLiveStream::HTTPLiveStream(int streamid)
: m_writing(false),
m_streamid(streamid)
{
LoadFromDB();
}
HTTPLiveStream::~HTTPLiveStream()
{
if (m_writing)
{
WritePlaylist(false, true);
if (m_audioOnlyBitrate)
WritePlaylist(true, true);
}
}
bool HTTPLiveStream::InitForWrite(void)
{
if ((m_streamid == -1) ||
(!WriteHTML()) ||
(!WriteMetaPlaylist()) ||
(!UpdateStatus(kHLSStatusStarting)) ||
(!UpdateStatusMessage("Transcode Starting")))
return false;
m_writing = true;
return true;
}
QString HTTPLiveStream::GetFilename(uint16_t segmentNumber, bool fileOnly,
bool audioOnly, bool encoded) const
{
QString filename;
if (encoded)
filename = audioOnly ? m_audioOutFileEncoded : m_outFileEncoded;
else
filename = audioOnly ? m_audioOutFile : m_outFile;
filename += ".%1.ts";
if (!fileOnly)
filename = m_outDir + "/" + filename;
if (segmentNumber)
return filename.arg(segmentNumber, 6, 10, QChar('0'));
return filename.arg(1, 6, 10, QChar('0'));
}
QString HTTPLiveStream::GetCurrentFilename(bool audioOnly, bool encoded) const
{
return GetFilename(m_curSegment, false, audioOnly, encoded);
}
int HTTPLiveStream::AddStream(void)
{
m_status = kHLSStatusQueued;
QString tmpBase = QString("");
QString tmpFullURL = QString("");
QString tmpRelURL = QString("");
if (m_width && m_height)
{
tmpBase = m_outBase;
tmpFullURL = m_fullURL;
tmpRelURL = m_relativeURL;
}
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"INSERT INTO livestream "
" ( width, height, bitrate, audiobitrate, segmentsize, "
" maxsegments, startsegment, currentsegment, segmentcount, "
" percentcomplete, created, lastmodified, relativeurl, "
" fullurl, status, statusmessage, sourcefile, sourcehost, "
" sourcewidth, sourceheight, outdir, outbase, "
" audioonlybitrate, samplerate ) "
"VALUES "
" ( :WIDTH, :HEIGHT, :BITRATE, :AUDIOBITRATE, :SEGMENTSIZE, "
" :MAXSEGMENTS, 0, 0, 0, "
" 0, :CREATED, :LASTMODIFIED, :RELATIVEURL, "
" :FULLURL, :STATUS, :STATUSMESSAGE, :SOURCEFILE, :SOURCEHOST, "
" :SOURCEWIDTH, :SOURCEHEIGHT, :OUTDIR, :OUTBASE, "
" :AUDIOONLYBITRATE, :SAMPLERATE ) ");
query.bindValue(":WIDTH", m_width);
query.bindValue(":HEIGHT", m_height);
query.bindValue(":BITRATE", m_bitrate);
query.bindValue(":AUDIOBITRATE", m_audioBitrate);
query.bindValue(":SEGMENTSIZE", m_segmentSize);
query.bindValue(":MAXSEGMENTS", m_maxSegments);
query.bindValue(":CREATED", m_created);
query.bindValue(":LASTMODIFIED", m_lastModified);
query.bindValue(":RELATIVEURL", tmpRelURL);
query.bindValue(":FULLURL", tmpFullURL);
query.bindValue(":STATUS", (int)m_status);
query.bindValue(":STATUSMESSAGE",
QString("Waiting for mythtranscode startup."));
query.bindValue(":SOURCEFILE", m_sourceFile);
query.bindValue(":SOURCEHOST", gCoreContext->GetHostName());
query.bindValue(":SOURCEWIDTH", 0);
query.bindValue(":SOURCEHEIGHT", 0);
query.bindValue(":OUTDIR", m_outDir);
query.bindValue(":OUTBASE", tmpBase);
query.bindValue(":AUDIOONLYBITRATE", m_audioOnlyBitrate);
query.bindValue(":SAMPLERATE", m_sampleRate);
if (!query.exec())
{
LOG(VB_GENERAL, LOG_ERR, LOC + "LiveStream insert failed.");
return -1;
}
if (!query.exec("SELECT LAST_INSERT_ID()") || !query.next())
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Unable to query LiveStream streamid.");
return -1;
}
m_streamid = query.value(0).toUInt();
return m_streamid;
}
bool HTTPLiveStream::AddSegment(void)
{
if (m_streamid == -1)
return false;
MSqlQuery query(MSqlQuery::InitCon());
++m_curSegment;
++m_segmentCount;
if (!m_startSegment)
m_startSegment = m_curSegment;
if ((m_maxSegments) &&
(m_segmentCount > (uint16_t)(m_maxSegments + 1)))
{
QString thisFile = GetFilename(m_startSegment);
if (!QFile::remove(thisFile))
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Unable to delete %1.").arg(thisFile));
++m_startSegment;
--m_segmentCount;
}
SaveSegmentInfo();
WritePlaylist(false);
if (m_audioOnlyBitrate)
WritePlaylist(true);
return true;
}
QString HTTPLiveStream::GetHTMLPageName(void) const
{
if (m_streamid == -1)
return QString();
QString outFile = m_outDir + "/" + m_outBase + ".html";
return outFile;
}
bool HTTPLiveStream::WriteHTML(void)
{
if (m_streamid == -1)
return false;
QString outFile = m_outDir + "/" + m_outBase + ".html";
QFile file(outFile);
if (!file.open(QIODevice::WriteOnly))
{
LOG(VB_RECORD, LOG_ERR, QString("Error opening %1").arg(outFile));
return false;
}
file.write(QString(
"<html>\n"
" <head>\n"
" <title>%1</title>\n"
" </head>\n"
" <body style='background-color:#FFFFFF;'>\n"
" <center>\n"
" <video controls>\n"
" <source src='%2.m3u8' />\n"
" </video>\n"
" </center>\n"
" </body>\n"
"</html>\n"
).arg(m_sourceFile).arg(m_outBaseEncoded)
.toAscii());
file.close();
return true;
}
QString HTTPLiveStream::GetMetaPlaylistName(void) const
{
if (m_streamid == -1)
return QString();
QString outFile = m_outDir + "/" + m_outBase + ".m3u8";
return outFile;
}
bool HTTPLiveStream::WriteMetaPlaylist(void)
{
if (m_streamid == -1)
return false;
QString outFile = GetMetaPlaylistName();
QFile file(outFile);
if (!file.open(QIODevice::WriteOnly))
{
LOG(VB_RECORD, LOG_ERR, QString("Error opening %1").arg(outFile));
return false;
}
file.write(QString(
"#EXTM3U\n"
"#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=%1\n"
"%2.m3u8\n"
).arg((int)((m_bitrate + m_audioBitrate) * 1.1))
.arg(m_outFileEncoded).toAscii());
if (m_audioOnlyBitrate)
{
file.write(QString(
"#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=%1\n"
"%2.m3u8\n"
).arg((int)((m_audioOnlyBitrate) * 1.1))
.arg(m_audioOutFileEncoded).toAscii());
}
file.close();
return true;
}
QString HTTPLiveStream::GetPlaylistName(bool audioOnly) const
{
if (m_streamid == -1)
return QString();
if (audioOnly && m_audioOutFile.isEmpty())
return QString();
QString base = audioOnly ? m_audioOutFile : m_outFile;
QString outFile = m_outDir + "/" + base + ".m3u8";
return outFile;
}
bool HTTPLiveStream::WritePlaylist(bool audioOnly, bool writeEndTag)
{
if (m_streamid == -1)
return false;
QString outFile = GetPlaylistName(audioOnly);
QString tmpFile = outFile + ".tmp";
QFile file(tmpFile);
if (!file.open(QIODevice::WriteOnly))
{
LOG(VB_RECORD, LOG_ERR, QString("Error opening %1").arg(tmpFile));
return false;
}
file.write(QString(
"#EXTM3U\n"
"#EXT-X-TARGETDURATION:%1\n"
"#EXT-X-MEDIA-SEQUENCE:%2\n"
).arg(m_segmentSize).arg(m_startSegment).toAscii());
if (writeEndTag)
file.write("#EXT-X-ENDLIST\n");
// Don't write out the current segment until the end
unsigned int tmpSegCount = m_segmentCount - 1;
unsigned int i = 0;
unsigned int segmentid = m_startSegment;
if (writeEndTag)
++tmpSegCount;
while (i < tmpSegCount)
{
file.write(QString(
"#EXTINF:%1,\n"
"%2\n"
).arg(m_segmentSize)
.arg(GetFilename(segmentid + i, true, audioOnly, true)).toAscii());
++i;
}
file.close();
rename(tmpFile.toAscii().constData(), outFile.toAscii().constData());
return true;
}
bool HTTPLiveStream::SaveSegmentInfo(void)
{
if (m_streamid == -1)
return false;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"UPDATE livestream "
"SET startsegment = :START, currentsegment = :CURRENT, "
" segmentcount = :COUNT "
"WHERE id = :STREAMID; ");
query.bindValue(":START", m_startSegment);
query.bindValue(":CURRENT", m_curSegment);
query.bindValue(":COUNT", m_segmentCount);
query.bindValue(":STREAMID", m_streamid);
if (query.exec())
return true;
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Unable to update segment info for streamid %1")
.arg(m_streamid));
return false;
}
bool HTTPLiveStream::UpdateSizeInfo(uint16_t width, uint16_t height,
uint16_t srcwidth, uint16_t srcheight)
{
if (m_streamid == -1)
return false;
QFileInfo finfo(m_sourceFile);
QString newOutBase = finfo.fileName() +
QString(".%1x%2_%3kV_%4kA").arg(width).arg(height)
.arg(m_bitrate/1000).arg(m_audioBitrate/1000);
QString newFullURL = m_httpPrefix + newOutBase + ".m3u8";
QString newRelativeURL = m_httpPrefixRel + newOutBase + ".m3u8";
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"UPDATE livestream "
"SET width = :WIDTH, height = :HEIGHT, "
" sourcewidth = :SRCWIDTH, sourceheight = :SRCHEIGHT, "
" fullurl = :FULLURL, relativeurl = :RELATIVEURL, "
" outbase = :OUTBASE "
"WHERE id = :STREAMID; ");
query.bindValue(":WIDTH", width);
query.bindValue(":HEIGHT", height);
query.bindValue(":SRCWIDTH", srcwidth);
query.bindValue(":SRCHEIGHT", srcheight);
query.bindValue(":FULLURL", newFullURL);
query.bindValue(":RELATIVEURL", newRelativeURL);
query.bindValue(":OUTBASE", newOutBase);
query.bindValue(":STREAMID", m_streamid);
if (!query.exec())
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Unable to update segment info for streamid %1")
.arg(m_streamid));
return false;
}
m_width = width;
m_height = height;
m_sourceWidth = srcwidth;
m_sourceHeight = srcheight;
m_outBase = newOutBase;
m_fullURL = newFullURL;
m_relativeURL = newRelativeURL;
SetOutputVars();
return true;
}
bool HTTPLiveStream::UpdateStatus(HTTPLiveStreamStatus status)
{
if (m_streamid == -1)
return false;
if ((m_status == kHLSStatusStopping) &&
(status == kHLSStatusRunning))
{
LOG(VB_RECORD, LOG_DEBUG, LOC + "Attempted to switch from "
"Stopping to Running State");
return false;
}
QString statusStr = StatusToString(status);
m_status = status;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"UPDATE livestream "
"SET status = :STATUS "
"WHERE id = :STREAMID; ");
query.bindValue(":STATUS", (int)status);
query.bindValue(":STREAMID", m_streamid);
if (query.exec())
return true;
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Unable to update status for streamid %1").arg(m_streamid));
return false;
}
bool HTTPLiveStream::UpdateStatusMessage(QString message)
{
if (m_streamid == -1)
return false;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"UPDATE livestream "
"SET statusmessage = :MESSAGE "
"WHERE id = :STREAMID; ");
query.bindValue(":MESSAGE", message);
query.bindValue(":STREAMID", m_streamid);
if (query.exec())
{
m_statusMessage = message;
return true;
}
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Unable to update status message for streamid %1")
.arg(m_streamid));
return false;
}
bool HTTPLiveStream::UpdatePercentComplete(int percent)
{
if (m_streamid == -1)
return false;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"UPDATE livestream "
"SET percentcomplete = :PERCENT "
"WHERE id = :STREAMID; ");
query.bindValue(":PERCENT", percent);
query.bindValue(":STREAMID", m_streamid);
if (query.exec())
{
m_percentComplete = percent;
return true;
}
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Unable to update percent complete for streamid %1")
.arg(m_streamid));
return false;
}
QString HTTPLiveStream::StatusToString(HTTPLiveStreamStatus status)
{
switch (m_status) {
case kHLSStatusUndefined : return QString("Undefined");
case kHLSStatusQueued : return QString("Queued");
case kHLSStatusStarting : return QString("Starting");
case kHLSStatusRunning : return QString("Running");
case kHLSStatusCompleted : return QString("Completed");
case kHLSStatusErrored : return QString("Errored");
case kHLSStatusStopping : return QString("Stopping");
case kHLSStatusStopped : return QString("Stopped");
};
return QString("Unknown status value");
}
bool HTTPLiveStream::LoadFromDB(void)
{
if (m_streamid == -1)
return false;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"SELECT width, height, bitrate, audiobitrate, segmentsize, "
" maxsegments, startsegment, currentsegment, segmentcount, "
" percentcomplete, created, lastmodified, relativeurl, "
" fullurl, status, statusmessage, sourcefile, sourcehost, "
" sourcewidth, sourceheight, outdir, outbase, audioonlybitrate, "
" samplerate "
"FROM livestream "
"WHERE id = :STREAMID; ");
query.bindValue(":STREAMID", m_streamid);
if (!query.exec() || !query.next())
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Unable to query DB info for stream %1")
.arg(m_streamid));
return false;
}
m_width = query.value(0).toUInt();
m_height = query.value(1).toUInt();
m_bitrate = query.value(2).toUInt();
m_audioBitrate = query.value(3).toUInt();
m_segmentSize = query.value(4).toUInt();
m_maxSegments = query.value(5).toUInt();
m_startSegment = query.value(6).toUInt();
m_curSegment = query.value(7).toUInt();
m_segmentCount = query.value(8).toUInt();
m_percentComplete = query.value(9).toUInt();
m_created = MythDate::as_utc(query.value(10).toDateTime());
m_lastModified = MythDate::as_utc(query.value(11).toDateTime());
m_relativeURL = query.value(12).toString();
m_fullURL = query.value(13).toString();
m_status = (HTTPLiveStreamStatus)(query.value(14).toInt());
m_statusMessage = query.value(15).toString();
m_sourceFile = query.value(16).toString();
m_sourceHost = query.value(17).toString();
m_sourceWidth = query.value(18).toUInt();
m_sourceHeight = query.value(19).toUInt();
m_outDir = query.value(20).toString();
m_outBase = query.value(21).toString();
m_audioOnlyBitrate = query.value(22).toUInt();
m_sampleRate = query.value(23).toUInt();
SetOutputVars();
return true;
}
void HTTPLiveStream::SetOutputVars(void)
{
m_outBaseEncoded = QString(QUrl::toPercentEncoding(m_outBase, "", " "));
m_outFile = m_outBase + ".av";
m_outFileEncoded = m_outBaseEncoded + ".av";
if (m_audioOnlyBitrate)
{
m_audioOutFile = m_outBase +
QString(".ao_%1kA").arg(m_audioOnlyBitrate/1000);
m_audioOutFileEncoded = m_outBaseEncoded +
QString(".ao_%1kA").arg(m_audioOnlyBitrate/1000);
}
m_httpPrefix = gCoreContext->GetSetting("HTTPLiveStreamPrefix", QString(
"http://%1:%2/StorageGroup/Streaming/")
.arg(gCoreContext->GetSetting("MasterServerIP"))
.arg(gCoreContext->GetSetting("BackendStatusPort")));
if (!m_httpPrefix.endsWith("/"))
m_httpPrefix.append("/");
if (!gCoreContext->GetSetting("HTTPLiveStreamPrefixRel").isEmpty())
{
m_httpPrefixRel = gCoreContext->GetSetting("HTTPLiveStreamPrefixRel");
if (!m_httpPrefix.endsWith("/"))
m_httpPrefix.append("/");
}
else if (m_httpPrefix.contains("/StorageGroup/Streaming/"))
m_httpPrefixRel = "/StorageGroup/Streaming/";
else
m_httpPrefixRel = "";
}
HTTPLiveStreamStatus HTTPLiveStream::GetDBStatus(void) const
{
if (m_streamid == -1)
return kHLSStatusUndefined;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"SELECT status FROM livestream "
"WHERE id = :STREAMID; ");
query.bindValue(":STREAMID", m_streamid);
if (!query.exec() || !query.next())
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Unable to check stop status for stream %1")
.arg(m_streamid));
return kHLSStatusUndefined;
}
return (HTTPLiveStreamStatus)query.value(0).toInt();
}
bool HTTPLiveStream::CheckStop(void)
{
if (m_streamid == -1)
return false;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"SELECT status FROM livestream "
"WHERE id = :STREAMID; ");
query.bindValue(":STREAMID", m_streamid);
if (!query.exec() || !query.next())
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Unable to check stop status for stream %1")
.arg(m_streamid));
return false;
}
if (query.value(0).toInt() == (int)kHLSStatusStopping)
return true;
return false;
}
DTC::LiveStreamInfo *HTTPLiveStream::StartStream(void)
{
HTTPLiveStreamThread *streamThread =
new HTTPLiveStreamThread(GetStreamID());
MThreadPool::globalInstance()->startReserved(streamThread,
"HTTPLiveStream");
MythTimer statusTimer;
int delay = 250000;
statusTimer.start();
HTTPLiveStreamStatus status = GetDBStatus();
while ((status == kHLSStatusQueued) &&
((statusTimer.elapsed() / 1000) < 30))
{
delay = (int)(delay * 1.5);
usleep(delay);
status = GetDBStatus();
}
return GetLiveStreamInfo();
}
bool HTTPLiveStream::RemoveStream(int id)
{
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"SELECT startSegment, segmentCount "
"FROM livestream "
"WHERE id = :STREAMID; ");
query.bindValue(":STREAMID", id);
if (!query.exec() || !query.next())
{
LOG(VB_RECORD, LOG_ERR, "Error selecting stream info in RemoveStream");
return false;
}
HTTPLiveStream *hls = new HTTPLiveStream(id);
if (hls->GetDBStatus() == kHLSStatusRunning) {
HTTPLiveStream::StopStream(id);
}
QString thisFile;
int startSegment = query.value(0).toInt();
int segmentCount = query.value(1).toInt();
for (int x = 0; x < segmentCount; ++x)
{
thisFile = hls->GetFilename(startSegment + x);
if (!thisFile.isEmpty() && !QFile::remove(thisFile))
LOG(VB_GENERAL, LOG_ERR, SLOC +
QString("Unable to delete %1.").arg(thisFile));
thisFile = hls->GetFilename(startSegment + x, false, true);
if (!thisFile.isEmpty() && !QFile::remove(thisFile))
LOG(VB_GENERAL, LOG_ERR, SLOC +
QString("Unable to delete %1.").arg(thisFile));
}
thisFile = hls->GetMetaPlaylistName();
if (!thisFile.isEmpty() && !QFile::remove(thisFile))
LOG(VB_GENERAL, LOG_ERR, SLOC +
QString("Unable to delete %1.").arg(thisFile));
thisFile = hls->GetPlaylistName();
if (!thisFile.isEmpty() && !QFile::remove(thisFile))
LOG(VB_GENERAL, LOG_ERR, SLOC +
QString("Unable to delete %1.").arg(thisFile));
thisFile = hls->GetPlaylistName(true);
if (!thisFile.isEmpty() && !QFile::remove(thisFile))
LOG(VB_GENERAL, LOG_ERR, SLOC +
QString("Unable to delete %1.").arg(thisFile));
thisFile = hls->GetHTMLPageName();
if (!thisFile.isEmpty() && !QFile::remove(thisFile))
LOG(VB_GENERAL, LOG_ERR, SLOC +
QString("Unable to delete %1.").arg(thisFile));
query.prepare(
"DELETE FROM livestream "
"WHERE id = :STREAMID; ");
query.bindValue(":STREAMID", id);
if (!query.exec())
LOG(VB_RECORD, LOG_ERR, "Error deleting stream info in RemoveStream");
delete hls;
return true;
}
DTC::LiveStreamInfo *HTTPLiveStream::StopStream(int id)
{
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"UPDATE livestream "
"SET status = :STATUS "
"WHERE id = :STREAMID; ");
query.bindValue(":STATUS", (int)kHLSStatusStopping);
query.bindValue(":STREAMID", id);
if (!query.exec())
{
LOG(VB_GENERAL, LOG_ERR, SLOC +
QString("Unable to remove mark stream stopped for stream %1.")
.arg(id));
return NULL;
}
HTTPLiveStream *hls = new HTTPLiveStream(id);
if (!hls)
return NULL;
MythTimer statusTimer;
int delay = 250000;
statusTimer.start();
HTTPLiveStreamStatus status = hls->GetDBStatus();
while ((status != kHLSStatusStopped) &&
(status != kHLSStatusCompleted) &&
(status != kHLSStatusErrored) &&
((statusTimer.elapsed() / 1000) < 30))
{
delay = (int)(delay * 1.5);
usleep(delay);
status = hls->GetDBStatus();
}
hls->LoadFromDB();
DTC::LiveStreamInfo *pLiveStreamInfo = hls->GetLiveStreamInfo();
delete hls;
return pLiveStreamInfo;
}
/////////////////////////////////////////////////////////////////////////////
// Content Service API helpers
/////////////////////////////////////////////////////////////////////////////
DTC::LiveStreamInfo *HTTPLiveStream::GetLiveStreamInfo(
DTC::LiveStreamInfo *info)
{
if (!info)
info = new DTC::LiveStreamInfo();
info->setId((int)m_streamid);
info->setWidth((int)m_width);
info->setHeight((int)m_height);
info->setBitrate((int)m_bitrate);
info->setAudioBitrate((int)m_audioBitrate);
info->setSegmentSize((int)m_segmentSize);
info->setMaxSegments((int)m_maxSegments);
info->setStartSegment((int)m_startSegment);
info->setCurrentSegment((int)m_curSegment);
info->setSegmentCount((int)m_segmentCount);
info->setPercentComplete((int)m_percentComplete);
info->setCreated(m_created);
info->setLastModified(m_lastModified);
info->setRelativeURL(m_relativeURL);
info->setFullURL(m_fullURL);
info->setStatusStr(StatusToString(m_status));
info->setStatusInt((int)m_status);
info->setStatusMessage(m_statusMessage);
info->setSourceFile(m_sourceFile);
info->setSourceHost(m_sourceHost);
info->setSourceWidth(m_sourceWidth);
info->setSourceHeight(m_sourceHeight);
info->setAudioOnlyBitrate((int)m_audioOnlyBitrate);
return info;
}
DTC::LiveStreamInfoList *HTTPLiveStream::GetLiveStreamInfoList(const QString &FileName)
{
DTC::LiveStreamInfoList *infoList = new DTC::LiveStreamInfoList();
QString sql = "SELECT id FROM livestream ";
if (!FileName.isEmpty())
sql += "WHERE sourcefile LIKE :FILENAME ";
sql += "ORDER BY lastmodified DESC;";
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(sql);
if (!FileName.isEmpty())
query.bindValue(":FILENAME", QString("%%1%").arg(FileName));
if (!query.exec())
{
LOG(VB_GENERAL, LOG_ERR, SLOC + "Unable to get list of Live Streams");
return infoList;
}
DTC::LiveStreamInfo *info = NULL;
HTTPLiveStream *hls = NULL;
while (query.next())
{
hls = new HTTPLiveStream(query.value(0).toUInt());
info = infoList->AddNewLiveStreamInfo();
hls->GetLiveStreamInfo(info);
delete hls;
}
return infoList;
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */