Last active
November 5, 2018 18:00
-
-
Save allanjos/97c4cc616fdbd5a569fd6bb57c6d2722 to your computer and use it in GitHub Desktop.
Transport stream MPEG2-TS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <cstring> | |
#include <cstdio> | |
#include <vector> | |
#include <cstdlib> | |
#include "ts.h" | |
#include "debug.h" | |
TransportStream::TransportStream() { | |
insideTsPacket = false; | |
parserByteCounter = 0; | |
tsFileReadIndex = 0; | |
fileSize = 0; | |
} | |
void TransportStream::setPath(char *path) { | |
if (path == NULL) { | |
return; | |
} | |
this->path = path; | |
} | |
int TransportStream::load() { | |
Debug::write("TransportStream::load()"); | |
if (path.length() <= 0) { | |
return -1; | |
} | |
FILE *tsFd = fopen(path.c_str(), "rb"); | |
if (!tsFd) { | |
std::cerr << "Could not open file " << path << std::endl; | |
return -1; | |
} | |
// Get file size | |
fseek(tsFd, 0, SEEK_END); | |
fileSize = ftell(tsFd); | |
std::cerr << "File size: " << fileSize << std::endl; | |
unsigned char byte; | |
// Seek to the begining of the file | |
rewind(tsFd); | |
tsFileReadIndex = 0; | |
packetCounter = 0; | |
std::cerr << "Reading..." << std::endl; | |
int bytes_counter = 0; | |
for (tsFileReadIndex = 0; tsFileReadIndex < fileSize; tsFileReadIndex++) { | |
bytes_counter++; | |
fread(&byte, 1, 1, tsFd); | |
if (byte == 0x47) { | |
//Debug::write("Bouquet name descriptor"); | |
++packetCounter; | |
printf("TS PACKET %d\n", packetCounter); | |
// Set state to inside the TS packet container | |
insideTsPacket = true; | |
// Packet size (188) - Sync byte size (8) = 180 | |
// Not a complete TS packet (8 bytes * 22 = 180 bytes) | |
if (tsFileReadIndex + 184 > fileSize) { | |
Debug::write("Incomplete TS Packet. Residual bytes: " + (fileSize - tsFileReadIndex), Debug::ERROR); | |
return -1; | |
} | |
unsigned char streamPacket[184]; | |
// Read packet content | |
int readBytes = fread(streamPacket, 1, 184, tsFd); | |
printf("read bytes: %d\n", readBytes); | |
for (int i = 0; i < readBytes; i++) { | |
printf("%02x ", streamPacket[i]); | |
} | |
printf("\n"); | |
tsFileReadIndex = tsFileReadIndex + 184; | |
// Error Indicator (1 byte) | |
short errorIndicator = streamPacket[0] >> 7; | |
printf("\t%d = errorIndicator\n", errorIndicator); | |
// Payload Unit Start Indicator (1 byte) | |
short unitStartIndicator = (streamPacket[0] >> 6) & 0x1; | |
printf("\t%d = unitStartIndicator\n", unitStartIndicator); | |
// Transport Priority (1 byte) | |
short transportPriority = (streamPacket[0] >> 5) & 0x1; | |
printf("\t%d = transportPriority\n", transportPriority); | |
// PID (13 bytes) | |
short pid = (streamPacket[0] << 11) | streamPacket[1]; | |
printf("\t%x (%d) = PID\n", pid, pid); | |
// Transport Scrambling Control (2 bytes) | |
short transportScramblingControl = (streamPacket[2] >> 6) & 0x3; | |
printf("\t%d = transportScramblingControl\n", transportScramblingControl); | |
// Transport Scrambling Control (2 bytes) | |
short adaptationFieldControl = (streamPacket[2] >> 4) & 0x3; | |
printf("\t%d = adaptationFieldControl\n", adaptationFieldControl); | |
// Continuity Counter | |
short continuityCounter = streamPacket[2] & 0xF; | |
printf("\t%x (%d) = continuityCounter\n", continuityCounter, continuityCounter); | |
} | |
} | |
Debug::write(""); | |
fclose(tsFd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment