00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "MPEG2TransportStreamFramer.hh"
00024 #include <GroupsockHelper.hh>
00025
00026 #define TRANSPORT_PACKET_SIZE 188
00027
00029
00030 #if !defined(NEW_DURATION_WEIGHT)
00031 #define NEW_DURATION_WEIGHT 0.5
00032
00033 #endif
00034
00035 #if !defined(TIME_ADJUSTMENT_FACTOR)
00036 #define TIME_ADJUSTMENT_FACTOR 0.8
00037
00038
00039
00040
00041 #endif
00042
00043 #if !defined(MAX_PLAYOUT_BUFFER_DURATION)
00044 #define MAX_PLAYOUT_BUFFER_DURATION 0.1 // (seconds)
00045 #endif
00046
00047 #if !defined(PCR_PERIOD_VARIATION_RATIO)
00048 #define PCR_PERIOD_VARIATION_RATIO 0.5
00049 #endif
00050
00052
00053 class PIDStatus {
00054 public:
00055 PIDStatus(double _firstClock, double _firstRealTime)
00056 : firstClock(_firstClock), lastClock(_firstClock),
00057 firstRealTime(_firstRealTime), lastRealTime(_firstRealTime),
00058 lastPacketNum(0) {
00059 }
00060
00061 double firstClock, lastClock, firstRealTime, lastRealTime;
00062 u_int64_t lastPacketNum;
00063 };
00064
00065
00067
00068 MPEG2TransportStreamFramer* MPEG2TransportStreamFramer
00069 ::createNew(UsageEnvironment& env, FramedSource* inputSource) {
00070 return new MPEG2TransportStreamFramer(env, inputSource);
00071 }
00072
00073 MPEG2TransportStreamFramer
00074 ::MPEG2TransportStreamFramer(UsageEnvironment& env, FramedSource* inputSource)
00075 : FramedFilter(env, inputSource),
00076 fTSPacketCount(0), fTSPacketDurationEstimate(0.0), fTSPCRCount(0),
00077 fLimitNumTSPacketsToStream(False), fNumTSPacketsToStream(0) {
00078 fPIDStatusTable = HashTable::create(ONE_WORD_HASH_KEYS);
00079 }
00080
00081 MPEG2TransportStreamFramer::~MPEG2TransportStreamFramer() {
00082 clearPIDStatusTable();
00083 delete fPIDStatusTable;
00084 }
00085
00086 void MPEG2TransportStreamFramer::clearPIDStatusTable() {
00087 PIDStatus* pidStatus;
00088 while ((pidStatus = (PIDStatus*)fPIDStatusTable->RemoveNext()) != NULL) {
00089 delete pidStatus;
00090 }
00091 }
00092
00093 void MPEG2TransportStreamFramer::setNumTSPacketsToStream(unsigned long numTSRecordsToStream) {
00094 fNumTSPacketsToStream = numTSRecordsToStream;
00095 fLimitNumTSPacketsToStream = numTSRecordsToStream > 0;
00096 }
00097
00098 void MPEG2TransportStreamFramer::doGetNextFrame() {
00099 if (fLimitNumTSPacketsToStream) {
00100 if (fNumTSPacketsToStream == 0) {
00101 handleClosure(this);
00102 return;
00103 }
00104 if (fNumTSPacketsToStream*TRANSPORT_PACKET_SIZE < fMaxSize) {
00105 fMaxSize = fNumTSPacketsToStream*TRANSPORT_PACKET_SIZE;
00106 }
00107 }
00108
00109
00110 fFrameSize = 0;
00111 fInputSource->getNextFrame(fTo, fMaxSize,
00112 afterGettingFrame, this,
00113 FramedSource::handleClosure, this);
00114 }
00115
00116 void MPEG2TransportStreamFramer::doStopGettingFrames() {
00117 FramedFilter::doStopGettingFrames();
00118 fTSPacketCount = 0;
00119 fTSPCRCount = 0;
00120
00121 clearPIDStatusTable();
00122 }
00123
00124 void MPEG2TransportStreamFramer
00125 ::afterGettingFrame(void* clientData, unsigned frameSize,
00126 unsigned ,
00127 struct timeval presentationTime,
00128 unsigned ) {
00129 MPEG2TransportStreamFramer* framer = (MPEG2TransportStreamFramer*)clientData;
00130 framer->afterGettingFrame1(frameSize, presentationTime);
00131 }
00132
00133 #define TRANSPORT_SYNC_BYTE 0x47
00134
00135 void MPEG2TransportStreamFramer::afterGettingFrame1(unsigned frameSize,
00136 struct timeval presentationTime) {
00137 fFrameSize += frameSize;
00138 unsigned const numTSPackets = fFrameSize/TRANSPORT_PACKET_SIZE;
00139 fNumTSPacketsToStream -= numTSPackets;
00140 fFrameSize = numTSPackets*TRANSPORT_PACKET_SIZE;
00141 if (fFrameSize == 0) {
00142
00143 handleClosure(this);
00144 return;
00145 }
00146
00147
00148 unsigned syncBytePosition;
00149 for (syncBytePosition = 0; syncBytePosition < fFrameSize; ++syncBytePosition) {
00150 if (fTo[syncBytePosition] == TRANSPORT_SYNC_BYTE) break;
00151 }
00152 if (syncBytePosition == fFrameSize) {
00153 envir() << "No Transport Stream sync byte in data.";
00154 handleClosure(this);
00155 return;
00156 } else if (syncBytePosition > 0) {
00157
00158
00159 memmove(fTo, &fTo[syncBytePosition], fFrameSize - syncBytePosition);
00160 fFrameSize -= syncBytePosition;
00161 fInputSource->getNextFrame(&fTo[fFrameSize], syncBytePosition,
00162 afterGettingFrame, this,
00163 FramedSource::handleClosure, this);
00164 return;
00165 }
00166
00167 fPresentationTime = presentationTime;
00168
00169
00170
00171 struct timeval tvNow;
00172 gettimeofday(&tvNow, NULL);
00173 double timeNow = tvNow.tv_sec + tvNow.tv_usec/1000000.0;
00174 for (unsigned i = 0; i < numTSPackets; ++i) {
00175 updateTSPacketDurationEstimate(&fTo[i*TRANSPORT_PACKET_SIZE], timeNow);
00176 }
00177
00178 fDurationInMicroseconds
00179 = numTSPackets * (unsigned)(fTSPacketDurationEstimate*1000000);
00180
00181
00182 afterGetting(this);
00183 }
00184
00185 void MPEG2TransportStreamFramer
00186 ::updateTSPacketDurationEstimate(unsigned char* pkt, double timeNow) {
00187
00188 if (pkt[0] != TRANSPORT_SYNC_BYTE) {
00189 envir() << "Missing sync byte!\n";
00190 return;
00191 }
00192
00193 ++fTSPacketCount;
00194
00195
00196 u_int8_t const adaptation_field_control = (pkt[3]&0x30)>>4;
00197 if (adaptation_field_control != 2 && adaptation_field_control != 3) return;
00198
00199
00200 u_int8_t const adaptation_field_length = pkt[4];
00201 if (adaptation_field_length == 0) return;
00202
00203 u_int8_t const discontinuity_indicator = pkt[5]&0x80;
00204 u_int8_t const pcrFlag = pkt[5]&0x10;
00205 if (pcrFlag == 0) return;
00206
00207
00208 ++fTSPCRCount;
00209 u_int32_t pcrBaseHigh = (pkt[6]<<24)|(pkt[7]<<16)|(pkt[8]<<8)|pkt[9];
00210 double clock = pcrBaseHigh/45000.0;
00211 if ((pkt[10]&0x80) != 0) clock += 1/90000.0;
00212 unsigned short pcrExt = ((pkt[10]&0x01)<<8) | pkt[11];
00213 clock += pcrExt/27000000.0;
00214
00215 unsigned pid = ((pkt[1]&0x1F)<<8) | pkt[2];
00216
00217
00218 PIDStatus* pidStatus = (PIDStatus*)(fPIDStatusTable->Lookup((char*)pid));
00219
00220 if (pidStatus == NULL) {
00221
00222 pidStatus = new PIDStatus(clock, timeNow);
00223 fPIDStatusTable->Add((char*)pid, pidStatus);
00224 #ifdef DEBUG_PCR
00225 fprintf(stderr, "PID 0x%x, FIRST PCR 0x%08x+%d:%03x == %f @ %f, pkt #%lu\n", pid, pcrBaseHigh, pkt[10]>>7, pcrExt, clock, timeNow, fTSPacketCount);
00226 #endif
00227 } else {
00228
00229 int64_t packetsSinceLast = (int64_t)(fTSPacketCount - pidStatus->lastPacketNum);
00230
00231 double durationPerPacket = (clock - pidStatus->lastClock)/packetsSinceLast;
00232
00233
00234
00235 double meanPCRPeriod = 0.0;
00236 if (fTSPCRCount > 0) {
00237 double tsPacketCount = (double)(int64_t)fTSPacketCount;
00238 double tsPCRCount = (double)(int64_t)fTSPCRCount;
00239 meanPCRPeriod = tsPacketCount/tsPCRCount;
00240 if (packetsSinceLast < meanPCRPeriod*PCR_PERIOD_VARIATION_RATIO) return;
00241 }
00242
00243 if (fTSPacketDurationEstimate == 0.0) {
00244 fTSPacketDurationEstimate = durationPerPacket;
00245 } else if (discontinuity_indicator == 0 && durationPerPacket >= 0.0) {
00246 fTSPacketDurationEstimate
00247 = durationPerPacket*NEW_DURATION_WEIGHT
00248 + fTSPacketDurationEstimate*(1-NEW_DURATION_WEIGHT);
00249
00250
00251
00252 double transmitDuration = timeNow - pidStatus->firstRealTime;
00253 double playoutDuration = clock - pidStatus->firstClock;
00254 if (transmitDuration > playoutDuration) {
00255 fTSPacketDurationEstimate *= TIME_ADJUSTMENT_FACTOR;
00256 } else if (transmitDuration + MAX_PLAYOUT_BUFFER_DURATION < playoutDuration) {
00257 fTSPacketDurationEstimate /= TIME_ADJUSTMENT_FACTOR;
00258 }
00259 } else {
00260
00261
00262 pidStatus->firstClock = clock;
00263 pidStatus->firstRealTime = timeNow;
00264 }
00265 #ifdef DEBUG_PCR
00266 fprintf(stderr, "PID 0x%x, PCR 0x%08x+%d:%03x == %f @ %f (diffs %f @ %f), pkt #%lu, discon %d => this duration %f, new estimate %f, mean PCR period=%f\n", pid, pcrBaseHigh, pkt[10]>>7, pcrExt, clock, timeNow, clock - pidStatus->firstClock, timeNow - pidStatus->firstRealTime, fTSPacketCount, discontinuity_indicator != 0, durationPerPacket, fTSPacketDurationEstimate, meanPCRPeriod );
00267 #endif
00268 }
00269
00270 pidStatus->lastClock = clock;
00271 pidStatus->lastRealTime = timeNow;
00272 pidStatus->lastPacketNum = fTSPacketCount;
00273 }