22#include < mist/bitfields.h>
33
44namespace 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
0 commit comments