Several minor bugfixes and improvements throughout.
This commit is contained in:
parent
cac5dcc9a1
commit
f3b0b36e2b
5 changed files with 125 additions and 54 deletions
129
lib/dtsc.cpp
129
lib/dtsc.cpp
|
@ -243,6 +243,12 @@ DTSC::Stream::~Stream(){
|
||||||
DTSC::File::File(std::string filename, bool create){
|
DTSC::File::File(std::string filename, bool create){
|
||||||
if (create){
|
if (create){
|
||||||
F = fopen(filename.c_str(), "w+b");
|
F = fopen(filename.c_str(), "w+b");
|
||||||
|
//write an empty header
|
||||||
|
fseek(F, 0, SEEK_SET);
|
||||||
|
fwrite(DTSC::Magic_Header, 4, 1, F);
|
||||||
|
memset(buffer, 0, 4);
|
||||||
|
fwrite(buffer, 4, 1, F);//write 4 zero-bytes
|
||||||
|
headerSize = 0;
|
||||||
}else{
|
}else{
|
||||||
F = fopen(filename.c_str(), "r+b");
|
F = fopen(filename.c_str(), "r+b");
|
||||||
}
|
}
|
||||||
|
@ -251,50 +257,26 @@ DTSC::File::File(std::string filename, bool create){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if first 4 bytes not available, assume empty file, write header
|
|
||||||
if (fread(buffer, 4, 1, F) != 1){
|
|
||||||
fseek(F, 0, SEEK_SET);
|
|
||||||
fwrite(DTSC::Magic_Header, 4, 1, F);
|
|
||||||
}else{
|
|
||||||
if (memcmp(buffer, DTSC::Magic_Header, 4) != 0){
|
|
||||||
fprintf(stderr, "Not a DTSC file - aborting: %s\n", filename.c_str());
|
|
||||||
fclose(F);
|
|
||||||
F = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//we now know the first 4 bytes are DTSC::Magic_Header and we have a valid file
|
//we now know the first 4 bytes are DTSC::Magic_Header and we have a valid file
|
||||||
fseek(F, 4, SEEK_SET);
|
fseek(F, 4, SEEK_SET);
|
||||||
if (fread(buffer, 4, 1, F) != 1){
|
if (fread(buffer, 4, 1, F) != 1){
|
||||||
fseek(F, 4, SEEK_SET);
|
fseek(F, 4, SEEK_SET);
|
||||||
memset(buffer, 0, 4);
|
memset(buffer, 0, 4);
|
||||||
fwrite(buffer, 4, 1, F);//write 4 zero-bytes
|
fwrite(buffer, 4, 1, F);//write 4 zero-bytes
|
||||||
headerSize = 0;
|
|
||||||
}else{
|
}else{
|
||||||
uint32_t * ubuffer = (uint32_t *)buffer;
|
uint32_t * ubuffer = (uint32_t *)buffer;
|
||||||
headerSize = ntohl(ubuffer[0]);
|
headerSize = ntohl(ubuffer[0]);
|
||||||
}
|
}
|
||||||
|
readHeader(0);
|
||||||
fseek(F, 8+headerSize, SEEK_SET);
|
fseek(F, 8+headerSize, SEEK_SET);
|
||||||
currframe = 1;
|
currframe = 1;
|
||||||
frames[1] = 8+headerSize;
|
frames[1] = 8+headerSize;
|
||||||
msframes[1] = 0;
|
msframes[1] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the header metadata for this file as a std::string.
|
/// Returns the header metadata for this file as JSON::Value.
|
||||||
/// Sets the file pointer to the first packet.
|
JSON::Value & DTSC::File::getMeta(){
|
||||||
std::string & DTSC::File::getHeader(){
|
return metadata;
|
||||||
if (fseek(F, 8, SEEK_SET) != 0){
|
|
||||||
strbuffer = "";
|
|
||||||
return strbuffer;
|
|
||||||
}
|
|
||||||
strbuffer.resize(headerSize);
|
|
||||||
if (fread((void*)strbuffer.c_str(), headerSize, 1, F) != 1){
|
|
||||||
strbuffer = "";
|
|
||||||
return strbuffer;
|
|
||||||
}
|
|
||||||
fseek(F, 8+headerSize, SEEK_SET);
|
|
||||||
currframe = 1;
|
|
||||||
return strbuffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// (Re)writes the given string to the header area if the size is the same as the existing header.
|
/// (Re)writes the given string to the header area if the size is the same as the existing header.
|
||||||
|
@ -311,10 +293,84 @@ bool DTSC::File::writeHeader(std::string & header, bool force){
|
||||||
return (ret == 1);
|
return (ret == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds the given string as a new header to the end of the file.
|
||||||
|
/// \returns The positon the header was written at, or 0 on failure.
|
||||||
|
long long int DTSC::File::addHeader(std::string & header){
|
||||||
|
fseek(F, 0, SEEK_END);
|
||||||
|
long long int writePos = ftell(F);
|
||||||
|
int hSize = htonl(header.size());
|
||||||
|
int ret = fwrite(DTSC::Magic_Header, 4, 1, F);//write header
|
||||||
|
if (ret != 1){return 0;}
|
||||||
|
ret = fwrite((void*)(&hSize), 4, 1, F);//write size
|
||||||
|
if (ret != 1){return 0;}
|
||||||
|
ret = fwrite(header.c_str(), header.size(), 1, F);//write contents
|
||||||
|
if (ret != 1){return 0;}
|
||||||
|
return writePos;//return position written at
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads the header at the given file position.
|
||||||
|
/// If the packet could not be read for any reason, the reason is printed to stderr.
|
||||||
|
/// Reading the header means the file position is moved to after the header.
|
||||||
|
void DTSC::File::readHeader(int pos){
|
||||||
|
fseek(F, pos, SEEK_SET);
|
||||||
|
if (fread(buffer, 4, 1, F) != 1){
|
||||||
|
if (feof(F)){
|
||||||
|
#if DEBUG >= 4
|
||||||
|
fprintf(stderr, "End of file reached (H%i)\n", pos);
|
||||||
|
#endif
|
||||||
|
}else{
|
||||||
|
fprintf(stderr, "Could not read header (H%i)\n", pos);
|
||||||
|
}
|
||||||
|
strbuffer = "";
|
||||||
|
metadata.null();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (memcmp(buffer, DTSC::Magic_Header, 4) != 0){
|
||||||
|
fprintf(stderr, "Invalid header - %.4s != %.4s (H%i)\n", buffer, DTSC::Magic_Header, pos);
|
||||||
|
strbuffer = "";
|
||||||
|
metadata.null();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (fread(buffer, 4, 1, F) != 1){
|
||||||
|
fprintf(stderr, "Could not read size (H%i)\n", pos);
|
||||||
|
strbuffer = "";
|
||||||
|
metadata.null();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uint32_t * ubuffer = (uint32_t *)buffer;
|
||||||
|
long packSize = ntohl(ubuffer[0]);
|
||||||
|
strbuffer.resize(packSize);
|
||||||
|
if (fread((void*)strbuffer.c_str(), packSize, 1, F) != 1){
|
||||||
|
fprintf(stderr, "Could not read packet (H%i)\n", pos);
|
||||||
|
strbuffer = "";
|
||||||
|
metadata.null();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
metadata = JSON::fromDTMI(strbuffer);
|
||||||
|
//if there is another header, read it and replace metadata with that one.
|
||||||
|
if (metadata.isMember("moreheader") && metadata["moreheader"].asInt() > 0){
|
||||||
|
readHeader(metadata["moreheader"].asInt());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (metadata.isMember("keytime")){
|
||||||
|
msframes.clear();
|
||||||
|
for (int i = 0; i < metadata["keytime"].size(); ++i){
|
||||||
|
msframes[i+1] = metadata["keytime"][i].asInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (metadata.isMember("keybpos")){
|
||||||
|
frames.clear();
|
||||||
|
for (int i = 0; i < metadata["keybpos"].size(); ++i){
|
||||||
|
frames[i+1] = metadata["keybpos"][i].asInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Reads the packet available at the current file position.
|
/// Reads the packet available at the current file position.
|
||||||
/// If the packet could not be read for any reason, the reason is printed to stderr.
|
/// If the packet could not be read for any reason, the reason is printed to stderr.
|
||||||
/// Reading the packet means the file position is increased to the next packet.
|
/// Reading the packet means the file position is increased to the next packet.
|
||||||
void DTSC::File::seekNext(){
|
void DTSC::File::seekNext(){
|
||||||
|
lastreadpos = ftell(F);
|
||||||
if (fread(buffer, 4, 1, F) != 1){
|
if (fread(buffer, 4, 1, F) != 1){
|
||||||
if (feof(F)){
|
if (feof(F)){
|
||||||
#if DEBUG >= 4
|
#if DEBUG >= 4
|
||||||
|
@ -350,23 +406,25 @@ void DTSC::File::seekNext(){
|
||||||
}
|
}
|
||||||
jsonbuffer = JSON::fromDTMI(strbuffer);
|
jsonbuffer = JSON::fromDTMI(strbuffer);
|
||||||
if (jsonbuffer.isMember("keyframe")){
|
if (jsonbuffer.isMember("keyframe")){
|
||||||
long pos = ftell(F) - (packSize + 8);
|
if (frames[currframe] != lastreadpos){
|
||||||
if (frames[currframe] != pos){
|
|
||||||
currframe++;
|
currframe++;
|
||||||
currtime = jsonbuffer["time"].asInt();
|
currtime = jsonbuffer["time"].asInt();
|
||||||
#if DEBUG >= 6
|
#if DEBUG >= 6
|
||||||
if (frames[currframe] != pos){
|
if (frames[currframe] != lastreadpos){
|
||||||
std::cerr << "Found a new frame " << currframe << " @ " << pos << "b/" << currtime << "ms" << std::endl;
|
std::cerr << "Found a new frame " << currframe << " @ " << lastreadpos << "b/" << currtime << "ms" << std::endl;
|
||||||
}else{
|
}else{
|
||||||
std::cerr << "Passing frame " << currframe << " @ " << pos << "b/" << currtime << "ms" << std::endl;
|
std::cerr << "Passing frame " << currframe << " @ " << lastreadpos << "b/" << currtime << "ms" << std::endl;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
frames[currframe] = pos;
|
frames[currframe] = lastreadpos;
|
||||||
msframes[currframe] = currtime;
|
msframes[currframe] = currtime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the byte positon of the start of the last packet that was read.
|
||||||
|
long long int DTSC::File::getLastReadPos(){return lastreadpos;}
|
||||||
|
|
||||||
/// Returns the internal buffer of the last read packet in raw binary format.
|
/// Returns the internal buffer of the last read packet in raw binary format.
|
||||||
std::string & DTSC::File::getPacket(){return strbuffer;}
|
std::string & DTSC::File::getPacket(){return strbuffer;}
|
||||||
|
|
||||||
|
@ -378,6 +436,9 @@ JSON::Value & DTSC::File::getJSON(){return jsonbuffer;}
|
||||||
bool DTSC::File::seek_frame(int frameno){
|
bool DTSC::File::seek_frame(int frameno){
|
||||||
if (frames.count(frameno) > 0){
|
if (frames.count(frameno) > 0){
|
||||||
if (fseek(F, frames[frameno], SEEK_SET) == 0){
|
if (fseek(F, frames[frameno], SEEK_SET) == 0){
|
||||||
|
#if DEBUG >= 4
|
||||||
|
std::cerr << "Seek direct from " << currframe << " @ " << frames[currframe] << " to " << frameno << " @ " << frames[frameno] << std::endl;
|
||||||
|
#endif
|
||||||
currframe = frameno;
|
currframe = frameno;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,19 +69,24 @@ namespace DTSC{
|
||||||
public:
|
public:
|
||||||
File(std::string filename, bool create = false);
|
File(std::string filename, bool create = false);
|
||||||
~File();
|
~File();
|
||||||
std::string & getHeader();
|
JSON::Value & getMeta();
|
||||||
|
long long int getLastReadPos();
|
||||||
bool writeHeader(std::string & header, bool force = false);
|
bool writeHeader(std::string & header, bool force = false);
|
||||||
|
long long int addHeader(std::string & header);
|
||||||
void seekNext();
|
void seekNext();
|
||||||
std::string & getPacket();
|
std::string & getPacket();
|
||||||
JSON::Value & getJSON();
|
JSON::Value & getJSON();
|
||||||
bool seek_frame(int frameno);
|
bool seek_frame(int frameno);
|
||||||
bool seek_time(int seconds);
|
bool seek_time(int seconds);
|
||||||
private:
|
private:
|
||||||
|
void readHeader(int pos);
|
||||||
std::string strbuffer;
|
std::string strbuffer;
|
||||||
JSON::Value jsonbuffer;
|
JSON::Value jsonbuffer;
|
||||||
|
JSON::Value metadata;
|
||||||
std::map<int, long> frames;
|
std::map<int, long> frames;
|
||||||
std::map<int, long> msframes;
|
std::map<int, long> msframes;
|
||||||
long long int currtime;
|
long long int currtime;
|
||||||
|
long long int lastreadpos;
|
||||||
int currframe;
|
int currframe;
|
||||||
FILE * F;
|
FILE * F;
|
||||||
unsigned long headerSize;
|
unsigned long headerSize;
|
||||||
|
|
|
@ -364,6 +364,10 @@ void FLV::Tag::setLen(){
|
||||||
/// Takes the DTSC Video init data and makes it into FLV.
|
/// Takes the DTSC Video init data and makes it into FLV.
|
||||||
/// Assumes init data is available - so check before calling!
|
/// Assumes init data is available - so check before calling!
|
||||||
bool FLV::Tag::DTSCVideoInit(DTSC::Stream & S){
|
bool FLV::Tag::DTSCVideoInit(DTSC::Stream & S){
|
||||||
|
//Unknown? Assume H264.
|
||||||
|
if (S.metadata["video"]["codec"].asString() == "?"){
|
||||||
|
S.metadata["video"]["codec"] = "H264";
|
||||||
|
}
|
||||||
if (S.metadata["video"]["codec"].asString() == "H264"){
|
if (S.metadata["video"]["codec"].asString() == "H264"){
|
||||||
len = S.metadata["video"]["init"].asString().length() + 20;
|
len = S.metadata["video"]["init"].asString().length() + 20;
|
||||||
}
|
}
|
||||||
|
@ -401,6 +405,10 @@ bool FLV::Tag::DTSCVideoInit(DTSC::Stream & S){
|
||||||
/// Assumes init data is available - so check before calling!
|
/// Assumes init data is available - so check before calling!
|
||||||
bool FLV::Tag::DTSCAudioInit(DTSC::Stream & S){
|
bool FLV::Tag::DTSCAudioInit(DTSC::Stream & S){
|
||||||
len = 0;
|
len = 0;
|
||||||
|
//Unknown? Assume AAC.
|
||||||
|
if (S.metadata["audio"]["codec"].asString() == "?"){
|
||||||
|
S.metadata["audio"]["codec"] = "AAC";
|
||||||
|
}
|
||||||
if (S.metadata["audio"]["codec"].asString() == "AAC"){
|
if (S.metadata["audio"]["codec"].asString() == "AAC"){
|
||||||
len = S.metadata["audio"]["init"].asString().length() + 17;
|
len = S.metadata["audio"]["init"].asString().length() + 17;
|
||||||
}
|
}
|
||||||
|
@ -446,6 +454,15 @@ bool FLV::Tag::DTSCAudioInit(DTSC::Stream & S){
|
||||||
/// Takes the DTSC metadata and makes it into FLV.
|
/// Takes the DTSC metadata and makes it into FLV.
|
||||||
/// Assumes metadata is available - so check before calling!
|
/// Assumes metadata is available - so check before calling!
|
||||||
bool FLV::Tag::DTSCMetaInit(DTSC::Stream & S){
|
bool FLV::Tag::DTSCMetaInit(DTSC::Stream & S){
|
||||||
|
//Unknown? Assume AAC.
|
||||||
|
if (S.metadata["audio"]["codec"].asString() == "?"){
|
||||||
|
S.metadata["audio"]["codec"] = "AAC";
|
||||||
|
}
|
||||||
|
//Unknown? Assume H264.
|
||||||
|
if (S.metadata["video"]["codec"].asString() == "?"){
|
||||||
|
S.metadata["video"]["codec"] = "H264";
|
||||||
|
}
|
||||||
|
|
||||||
AMF::Object amfdata("root", AMF::AMF0_DDV_CONTAINER);
|
AMF::Object amfdata("root", AMF::AMF0_DDV_CONTAINER);
|
||||||
|
|
||||||
amfdata.addContent(AMF::Object("", "onMetaData"));
|
amfdata.addContent(AMF::Object("", "onMetaData"));
|
||||||
|
@ -805,11 +822,6 @@ JSON::Value FLV::Tag::toJSON(JSON::Value & metadata){
|
||||||
if (!metadata["video"].isMember("bps")){metadata["video"]["bps"] = 0;}
|
if (!metadata["video"].isMember("bps")){metadata["video"]["bps"] = 0;}
|
||||||
if (!metadata["video"].isMember("keyms")){metadata["video"]["keyms"] = 0;}
|
if (!metadata["video"].isMember("keyms")){metadata["video"]["keyms"] = 0;}
|
||||||
if (!metadata["video"].isMember("keyvar")){metadata["video"]["keyvar"] = 0;}
|
if (!metadata["video"].isMember("keyvar")){metadata["video"]["keyvar"] = 0;}
|
||||||
if (!metadata["video"].isMember("keys")){
|
|
||||||
while (metadata["video"]["keys"].size() < 100){
|
|
||||||
metadata["video"]["keys"].append(JSON::Value((long long int)0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return pack_out;//empty
|
return pack_out;//empty
|
||||||
}
|
}
|
||||||
|
@ -829,7 +841,6 @@ JSON::Value FLV::Tag::toJSON(JSON::Value & metadata){
|
||||||
switch (audiodata & 0xF0){
|
switch (audiodata & 0xF0){
|
||||||
case 0x20: metadata["audio"]["codec"] = "MP3"; break;
|
case 0x20: metadata["audio"]["codec"] = "MP3"; break;
|
||||||
case 0xA0: metadata["audio"]["codec"] = "AAC"; break;
|
case 0xA0: metadata["audio"]["codec"] = "AAC"; break;
|
||||||
default: metadata["audio"]["codec"] = "?"; break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!metadata["audio"].isMember("rate") || metadata["audio"]["rate"].asInt() < 1){
|
if (!metadata["audio"].isMember("rate") || metadata["audio"]["rate"].asInt() < 1){
|
||||||
|
@ -878,7 +889,6 @@ JSON::Value FLV::Tag::toJSON(JSON::Value & metadata){
|
||||||
case 2: metadata["video"]["codec"] = "H263"; break;
|
case 2: metadata["video"]["codec"] = "H263"; break;
|
||||||
case 4: metadata["video"]["codec"] = "VP6"; break;
|
case 4: metadata["video"]["codec"] = "VP6"; break;
|
||||||
case 7: metadata["video"]["codec"] = "H264"; break;
|
case 7: metadata["video"]["codec"] = "H264"; break;
|
||||||
default: metadata["video"]["codec"] = "?"; break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pack_out["datatype"] = "video";
|
pack_out["datatype"] = "video";
|
||||||
|
|
13
lib/mp4.cpp
13
lib/mp4.cpp
|
@ -242,17 +242,8 @@ namespace MP4{
|
||||||
if (!reserve(index, 0, 8)){return;}
|
if (!reserve(index, 0, 8)){return;}
|
||||||
setInt64(0, index-payloadOffset);
|
setInt64(0, index-payloadOffset);
|
||||||
}
|
}
|
||||||
///\todo Fix 64 bit conversion
|
((int*)(data+index))[0] = htonl((int)(newData>>32));
|
||||||
// *((int*)(data[index])) = htonl((int)(newData>>32));
|
((int*)(data+index))[1] = htonl((int)(newData & 0xFFFFFFFF));
|
||||||
// *((int*)(data[index+4])) = htonl((int)newData);
|
|
||||||
data[index] = ( newData & 0xFF00000000000000 ) >> 56;
|
|
||||||
data[index+1] = ( newData & 0x00FF000000000000 ) >> 48;
|
|
||||||
data[index+2] = ( newData & 0x0000FF0000000000 ) >> 40;
|
|
||||||
data[index+3] = ( newData & 0x000000FF00000000 ) >> 32;
|
|
||||||
data[index+4] = ( newData & 0x00000000FF000000 ) >> 24;
|
|
||||||
data[index+5] = ( newData & 0x0000000000FF0000 ) >> 16;
|
|
||||||
data[index+6] = ( newData & 0x000000000000FF00 ) >> 8;
|
|
||||||
data[index+7] = ( newData & 0x00000000000000FF );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the 64 bits integer at the given index.
|
/// Gets the 64 bits integer at the given index.
|
||||||
|
|
|
@ -215,6 +215,8 @@ std::string Socket::Connection::getError(){return strerror(errno);}
|
||||||
/// \param address String containing the location of the Unix socket to connect to.
|
/// \param address String containing the location of the Unix socket to connect to.
|
||||||
/// \param nonblock Whether the socket should be nonblocking. False by default.
|
/// \param nonblock Whether the socket should be nonblocking. False by default.
|
||||||
Socket::Connection::Connection(std::string address, bool nonblock){
|
Socket::Connection::Connection(std::string address, bool nonblock){
|
||||||
|
pipes[0] = -1;
|
||||||
|
pipes[1] = -1;
|
||||||
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||||
if (sock < 0){
|
if (sock < 0){
|
||||||
#if DEBUG >= 1
|
#if DEBUG >= 1
|
||||||
|
@ -250,6 +252,8 @@ Socket::Connection::Connection(std::string address, bool nonblock){
|
||||||
/// \param port String containing the port to connect to.
|
/// \param port String containing the port to connect to.
|
||||||
/// \param nonblock Whether the socket should be nonblocking.
|
/// \param nonblock Whether the socket should be nonblocking.
|
||||||
Socket::Connection::Connection(std::string host, int port, bool nonblock){
|
Socket::Connection::Connection(std::string host, int port, bool nonblock){
|
||||||
|
pipes[0] = -1;
|
||||||
|
pipes[1] = -1;
|
||||||
struct addrinfo *result, *rp, hints;
|
struct addrinfo *result, *rp, hints;
|
||||||
Error = false;
|
Error = false;
|
||||||
Blocking = false;
|
Blocking = false;
|
||||||
|
|
Loading…
Add table
Reference in a new issue