Skip to content

Commit aaa4713

Browse files
committed
onebinary: refactor TSOutput and TSOutputHTTP
1 parent 7128551 commit aaa4713

File tree

8 files changed

+61
-43
lines changed

8 files changed

+61
-43
lines changed

meson.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ onebinary_stuff += 'MistOutFLAC'
235235
onebinary_stuff += 'MistOutFLV'
236236
onebinary_stuff += 'MistOutH264'
237237
onebinary_stuff += 'MistOutHDS'
238-
# onebinary_stuff += 'MistOutHLS'
238+
onebinary_stuff += 'MistOutHLS'
239239
onebinary_stuff += 'MistOutHTTP'
240240
onebinary_stuff += 'MistOutHTTPMinimalServer'
241241
onebinary_stuff += 'MistOutHTTPS'
242-
# onebinary_stuff += 'MistOutHTTPTS'
242+
onebinary_stuff += 'MistOutHTTPTS'
243243
onebinary_stuff += 'MistOutJSON'
244244
onebinary_stuff += 'MistOutJSONLine'
245245
onebinary_stuff += 'MistOutMP3'

src/output/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ foreach output : outputs
7878
endif
7979
if extra.contains('ts')
8080
sources += files('output_ts_base.cpp')
81+
sources += files('output_http.cpp')
8182
endif
8283
if extra.contains('with_rist')
8384
deps += librist

src/output/output_hls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ namespace Mist{
445445
return;
446446
}
447447
// Invoke the generic TS output sendNext handler
448-
TSOutput::sendNext();
448+
TSOutputHTTP::sendNext();
449449
}
450450

451451
void OutHLS::sendTS(const char *tsData, size_t len){H.Chunkify(tsData, len, myConn);}

src/output/output_hls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "output_ts_base.h"
33

44
namespace Mist{
5-
class OutHLS : public TSOutput{
5+
class OutHLS : public TSOutputHTTP{
66
public:
77
OutHLS(Socket::Connection &conn);
88
~OutHLS();

src/output/output_httpts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <unistd.h>
1212

1313
namespace Mist{
14-
OutHTTPTS::OutHTTPTS(Socket::Connection &conn) : TSOutput(conn){
14+
OutHTTPTS::OutHTTPTS(Socket::Connection &conn) : TSOutputHTTP(conn){
1515
sendRepeatingHeaders = 500; // PAT/PMT every 500ms (DVB spec)
1616
HTTP::URL target(config->getString("target"));
1717
if (target.protocol == "srt"){

src/output/output_httpts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "output_ts_base.h"
33

44
namespace Mist{
5-
class OutHTTPTS : public TSOutput{
5+
class OutHTTPTS : public TSOutputHTTP{
66
public:
77
OutHTTPTS(Socket::Connection &conn);
88
~OutHTTPTS();

src/output/output_ts_base.cpp

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@
22
#include <mist/bitfields.h>
33

44
namespace Mist{
5-
TSOutput::TSOutput(Socket::Connection &conn) : TS_BASECLASS(conn){
5+
template<class T>
6+
TSOutputTmpl<T>::TSOutputTmpl(Socket::Connection &conn) : T(conn){
67
packCounter = 0;
78
ts_from = 0;
8-
setBlocking(true);
9+
this->setBlocking(true);
910
sendRepeatingHeaders = 0;
1011
lastHeaderTime = 0;
1112
}
1213

13-
void TSOutput::fillPacket(char const *data, size_t dataLen, bool &firstPack, bool video,
14+
template<class T>
15+
void TSOutputTmpl<T>::fillPacket(char const *data, size_t dataLen, bool &firstPack, bool video,
1416
bool keyframe, size_t pkgPid, uint16_t &contPkg){
1517
do{
1618
if (!packData.getBytesFree()){
17-
if ((sendRepeatingHeaders && thisPacket.getTime() - lastHeaderTime > sendRepeatingHeaders) || !packCounter){
19+
if ((sendRepeatingHeaders && this->thisPacket.getTime() - lastHeaderTime > sendRepeatingHeaders) || !packCounter){
1820

1921
std::set<size_t> selectedTracks;
20-
for (std::map<size_t, Comms::Users>::iterator it = userSelect.begin(); it != userSelect.end(); it++){
22+
for (std::map<size_t, Comms::Users>::iterator it = this->userSelect.begin(); it != this->userSelect.end(); it++){
2123
selectedTracks.insert(it->first);
2224
}
2325

24-
lastHeaderTime = thisPacket.getTime();
26+
lastHeaderTime = this->thisPacket.getTime();
2527
TS::Packet tmpPack;
2628
tmpPack.FromPointer(TS::PAT);
2729
tmpPack.setContinuityCounter(++contPAT);
2830
sendTS(tmpPack.checkAndGetBuffer());
29-
sendTS(TS::createPMT(selectedTracks, M, ++contPMT));
30-
sendTS(TS::createSDT(streamName, ++contSDT));
31+
sendTS(TS::createPMT(selectedTracks, this->M, ++contPMT));
32+
sendTS(TS::createSDT(this->streamName, ++contSDT));
3133
packCounter += 3;
3234
}
3335
sendTS(packData.checkAndGetBuffer());
@@ -48,7 +50,7 @@ namespace Mist{
4850
packData.setRandomAccess(true);
4951
packData.setESPriority(true);
5052
}
51-
packData.setPCR(thisPacket.getTime() * 27000);
53+
packData.setPCR(this->thisPacket.getTime() * 27000);
5254
}
5355
firstPack = false;
5456
}
@@ -60,29 +62,30 @@ namespace Mist{
6062
}while (dataLen);
6163
}
6264

63-
void TSOutput::sendNext(){
65+
template<class T>
66+
void TSOutputTmpl<T>::sendNext(){
6467
static uint64_t lastMeta = 0;
6568
if (Util::epoch() > lastMeta + 5){
6669
lastMeta = Util::epoch();
67-
if (selectDefaultTracks()){
70+
if (this->selectDefaultTracks()){
6871
INFO_MSG("Track selection changed - resending headers and continuing");
6972
packCounter = 0;
7073
return;
7174
}
7275
}
7376
// Get ready some data to speed up accesses
74-
std::string type = M.getType(thisIdx);
75-
std::string codec = M.getCodec(thisIdx);
77+
std::string type = this->M.getType(this->thisIdx);
78+
std::string codec = this->M.getCodec(this->thisIdx);
7679
bool video = (type == "video");
77-
size_t pkgPid = TS::getUniqTrackID(M, thisIdx);
78-
bool &firstPack = first[thisIdx];
80+
size_t pkgPid = TS::getUniqTrackID(this->M, this->thisIdx);
81+
bool &firstPack = first[this->thisIdx];
7982
uint16_t &contPkg = contCounters[pkgPid];
80-
uint64_t packTime = thisPacket.getTime();
81-
bool keyframe = thisPacket.getInt("keyframe");
83+
uint64_t packTime = this->thisPacket.getTime();
84+
bool keyframe = this->thisPacket.getInt("keyframe");
8285
firstPack = true;
8386
char *dataPointer = 0;
8487
size_t dataLen = 0;
85-
thisPacket.getString("data", dataPointer, dataLen); // data
88+
this->thisPacket.getString("data", dataPointer, dataLen); // data
8689

8790
if (codec == "rawts"){
8891
for (size_t i = 0; i+188 <= dataLen; i+=188){sendTS(dataPointer+i, 188);}
@@ -120,13 +123,13 @@ namespace Mist{
120123
if (addInit){
121124
if (codec == "H264"){
122125
MP4::AVCC avccbox;
123-
avccbox.setPayload(M.getInit(thisIdx));
126+
avccbox.setPayload(this->M.getInit(this->thisIdx));
124127
bs = avccbox.asAnnexB();
125128
extraSize += bs.size();
126129
}
127130
if (codec == "HEVC"){
128131
MP4::HVCC hvccbox;
129-
hvccbox.setPayload(M.getInit(thisIdx));
132+
hvccbox.setPayload(this->M.getInit(this->thisIdx));
130133
bs = hvccbox.asAnnexB();
131134
extraSize += bs.size();
132135
}
@@ -135,12 +138,12 @@ namespace Mist{
135138
const uint32_t MAX_PES_SIZE = 65490 - 13;
136139
uint32_t ThisNaluSize = 0;
137140
uint32_t i = 0;
138-
uint64_t offset = thisPacket.getInt("offset") * 90;
141+
uint64_t offset = this->thisPacket.getInt("offset") * 90;
139142

140143
bs.clear();
141144
TS::Packet::getPESVideoLeadIn(bs,
142145
(((dataLen + extraSize) > MAX_PES_SIZE) ? 0 : dataLen + extraSize),
143-
packTime, offset, true, M.getBps(thisIdx));
146+
packTime, offset, true, this->M.getBps(this->thisIdx));
144147
fillPacket(bs.data(), bs.size(), firstPack, video, keyframe, pkgPid, contPkg);
145148

146149
// End of previous nal unit, if not already present
@@ -151,21 +154,21 @@ namespace Mist{
151154
if (addInit){
152155
if (codec == "H264"){
153156
MP4::AVCC avccbox;
154-
avccbox.setPayload(M.getInit(thisIdx));
157+
avccbox.setPayload(this->M.getInit(this->thisIdx));
155158
bs = avccbox.asAnnexB();
156159
fillPacket(bs.data(), bs.size(), firstPack, video, keyframe, pkgPid, contPkg);
157160
}
158161
/*LTS-START*/
159162
if (codec == "HEVC"){
160163
MP4::HVCC hvccbox;
161-
hvccbox.setPayload(M.getInit(thisIdx));
164+
hvccbox.setPayload(this->M.getInit(this->thisIdx));
162165
bs = hvccbox.asAnnexB();
163166
fillPacket(bs.data(), bs.size(), firstPack, video, keyframe, pkgPid, contPkg);
164167
}
165168
/*LTS-END*/
166169
}
167170
size_t lenSize = 4;
168-
if (codec == "H264"){lenSize = (M.getInit(thisIdx)[4] & 3) + 1;}
171+
if (codec == "H264"){lenSize = (this->M.getInit(this->thisIdx)[4] & 3) + 1;}
169172
while (i + lenSize < (unsigned int)dataLen){
170173
if (lenSize == 4){
171174
ThisNaluSize = Bit::btohl(dataPointer + i);
@@ -184,9 +187,9 @@ namespace Mist{
184187
i += ThisNaluSize + lenSize;
185188
}
186189
}else{
187-
uint64_t offset = thisPacket.getInt("offset") * 90;
190+
uint64_t offset = this->thisPacket.getInt("offset") * 90;
188191
bs.clear();
189-
TS::Packet::getPESVideoLeadIn(bs, 0, packTime, offset, true, M.getBps(thisIdx));
192+
TS::Packet::getPESVideoLeadIn(bs, 0, packTime, offset, true, this->M.getBps(this->thisIdx));
190193
fillPacket(bs.data(), bs.size(), firstPack, video, keyframe, pkgPid, contPkg);
191194

192195
fillPacket(dataPointer, dataLen, firstPack, video, keyframe, pkgPid, contPkg);
@@ -196,7 +199,7 @@ namespace Mist{
196199
if (codec == "AAC"){
197200
tempLen += 7;
198201
// Make sure TS timestamp is sample-aligned, if possible
199-
uint32_t freq = M.getRate(thisIdx);
202+
uint32_t freq = this->M.getRate(this->thisIdx);
200203
if (freq){
201204
uint64_t aacSamples = packTime * freq / 90000;
202205
//round to nearest packet, assuming all 1024 samples (probably wrong, but meh)
@@ -208,26 +211,26 @@ namespace Mist{
208211
}
209212
if (codec == "opus"){
210213
tempLen += 3 + (dataLen/255);
211-
bs = TS::Packet::getPESPS1LeadIn(tempLen, packTime, M.getBps(thisIdx));
214+
bs = TS::Packet::getPESPS1LeadIn(tempLen, packTime, this->M.getBps(this->thisIdx));
212215
fillPacket(bs.data(), bs.size(), firstPack, video, keyframe, pkgPid, contPkg);
213216
bs = "\177\340";
214217
bs.append(dataLen/255, (char)255);
215218
bs.append(1, (char)(dataLen-255*(dataLen/255)));
216219
fillPacket(bs.data(), bs.size(), firstPack, video, keyframe, pkgPid, contPkg);
217220
}else{
218221
bs.clear();
219-
TS::Packet::getPESAudioLeadIn(bs, tempLen, packTime, M.getBps(thisIdx));
222+
TS::Packet::getPESAudioLeadIn(bs, tempLen, packTime, this->M.getBps(this->thisIdx));
220223
fillPacket(bs.data(), bs.size(), firstPack, video, keyframe, pkgPid, contPkg);
221224
if (codec == "AAC"){
222-
bs = TS::getAudioHeader(dataLen, M.getInit(thisIdx));
225+
bs = TS::getAudioHeader(dataLen, this->M.getInit(this->thisIdx));
223226
fillPacket(bs.data(), bs.size(), firstPack, video, keyframe, pkgPid, contPkg);
224227
}
225228
}
226229
fillPacket(dataPointer, dataLen, firstPack, video, keyframe, pkgPid, contPkg);
227230
}else if (type == "meta"){
228231
long unsigned int tempLen = dataLen;
229232
if (codec == "JSON"){tempLen += 2;}
230-
bs = TS::Packet::getPESMetaLeadIn(tempLen, packTime, M.getBps(thisIdx));
233+
bs = TS::Packet::getPESMetaLeadIn(tempLen, packTime, this->M.getBps(this->thisIdx));
231234
fillPacket(bs.data(), bs.size(), firstPack, video, keyframe, pkgPid, contPkg);
232235
if (codec == "JSON"){
233236
char dLen[2];
@@ -241,4 +244,7 @@ namespace Mist{
241244
fillPacket(0, 0, firstPack, video, keyframe, pkgPid, contPkg);
242245
}
243246
}
247+
248+
TSOutput::TSOutput(Socket::Connection &conn) : TSOutputTmpl<Output>(conn){}
249+
TSOutputHTTP::TSOutputHTTP(Socket::Connection &conn) : TSOutputTmpl<HTTPOutput>(conn){}
244250
}// namespace Mist

src/output/output_ts_base.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma once
12
#include "output.h"
23
#include "output_http.h"
34
#include <mist/defines.h>
@@ -10,17 +11,18 @@
1011

1112
namespace Mist{
1213

13-
class TSOutput : public TS_BASECLASS{
14+
template<class T>
15+
class TSOutputTmpl : public T{
1416
public:
15-
TSOutput(Socket::Connection &conn);
16-
virtual ~TSOutput(){};
17+
TSOutputTmpl(Socket::Connection &conn);
18+
virtual ~TSOutputTmpl(){};
1719
virtual void sendNext();
1820
virtual void sendTS(const char *tsData, size_t len = 188){};
1921
void fillPacket(char const *data, size_t dataLen, bool &firstPack, bool video, bool keyframe,
2022
size_t pkgPid, uint16_t &contPkg);
2123
virtual void sendHeader(){
22-
sentHeader = true;
23-
packCounter = 0;
24+
this->sentHeader = true;
25+
this->packCounter = 0;
2426
}
2527

2628
protected:
@@ -36,4 +38,13 @@ namespace Mist{
3638
uint64_t lastHeaderTime; ///< Timestamp last PAT/PMT were sent.
3739
uint64_t ts_from; ///< Starting time to subtract from timestamps
3840
};
41+
42+
class TSOutput : public TSOutputTmpl<Output>{
43+
public:
44+
TSOutput(Socket::Connection &conn);
45+
};
46+
class TSOutputHTTP : public TSOutputTmpl<HTTPOutput>{
47+
public:
48+
TSOutputHTTP(Socket::Connection &conn);
49+
};
3950
}// namespace Mist

0 commit comments

Comments
 (0)