From 67bf3b8320ce1ac3e68ba991f70f509e5da52efc Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 13 Sep 2011 00:32:35 +0200 Subject: [PATCH 1/7] First compiling version of DTSC-powered Buffer. Merged DTSC and DTMI into DTSC for ease of use. Todo: FLV2DTSC executable, DTSC support in all connectors... --- util/dtmi.cpp | 248 ------------------------------- util/dtmi.h | 58 -------- util/dtsc.cpp | 388 ++++++++++++++++++++++++++++++++++++++++++++++-- util/dtsc.h | 111 +++++++++++--- util/socket.cpp | 8 +- util/socket.h | 8 +- 6 files changed, 476 insertions(+), 345 deletions(-) delete mode 100644 util/dtmi.cpp delete mode 100644 util/dtmi.h diff --git a/util/dtmi.cpp b/util/dtmi.cpp deleted file mode 100644 index a9d38299..00000000 --- a/util/dtmi.cpp +++ /dev/null @@ -1,248 +0,0 @@ -/// \file dtmi.cpp -/// Holds all code for DDVTECH MediaInfo parsing/generation. - -#include "dtmi.h" -#include //needed for stderr only - -/// Returns the std::string Indice for the current object, if available. -/// Returns an empty string if no indice exists. -std::string DTSC::DTMI::Indice(){return myIndice;}; - -/// Returns the DTSC::DTMItype AMF0 object type for this object. -DTSC::DTMItype DTSC::DTMI::GetType(){return myType;}; - -/// Returns the numeric value of this object, if available. -/// If this object holds no numeric value, 0 is returned. -uint64_t DTSC::DTMI::NumValue(){return numval;}; - -/// Returns the std::string value of this object, if available. -/// If this object holds no string value, an empty string is returned. -std::string DTSC::DTMI::StrValue(){return strval;}; - -/// Returns the C-string value of this object, if available. -/// If this object holds no string value, an empty C-string is returned. -const char * DTSC::DTMI::Str(){return strval.c_str();}; - -/// Returns a count of the amount of objects this object currently holds. -/// If this object is not a container type, this function will always return 0. -int DTSC::DTMI::hasContent(){return contents.size();}; - -/// Adds an DTSC::DTMI to this object. Works for all types, but only makes sense for container types. -void DTSC::DTMI::addContent(DTSC::DTMI c){contents.push_back(c);}; - -/// Returns a pointer to the object held at indice i. -/// Returns AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice. -/// \param i The indice of the object in this container. -DTSC::DTMI* DTSC::DTMI::getContentP(int i){return &contents.at(i);}; - -/// Returns a copy of the object held at indice i. -/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice. -/// \param i The indice of the object in this container. -DTSC::DTMI DTSC::DTMI::getContent(int i){return contents.at(i);}; - -/// Returns a pointer to the object held at indice s. -/// Returns NULL if no object is held at this indice. -/// \param s The indice of the object in this container. -DTSC::DTMI* DTSC::DTMI::getContentP(std::string s){ - for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ - if (it->Indice() == s){return &(*it);} - } - return 0; -}; - -/// Returns a copy of the object held at indice s. -/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice. -/// \param s The indice of the object in this container. -DTSC::DTMI DTSC::DTMI::getContent(std::string s){ - for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ - if (it->Indice() == s){return *it;} - } - return DTSC::DTMI("error", DTMI::DTMI_ROOT); -}; - -/// Default constructor. -/// Simply fills the data with DTSC::DTMI("error", AMF0_DDV_CONTAINER) -DTSC::DTMI::DTMI(){ - *this = DTSC::DTMI("error", DTMI::DTMI_ROOT); -};//default constructor - -/// Constructor for numeric objects. -/// The object type is by default AMF::AMF0_NUMBER, but this can be forced to a different value. -/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic. -/// \param val The numeric value of this object. Numeric AMF0 objects only support double-type values. -/// \param setType The object type to force this object to. -DTSC::DTMI::DTMI(std::string indice, double val, DTSC::DTMItype setType){//num type initializer - myIndice = indice; - myType = setType; - strval = ""; - numval = val; -}; - -/// Constructor for string objects. -/// The object type is by default AMF::AMF0_STRING, but this can be forced to a different value. -/// There is no need to manually change the type to AMF::AMF0_LONGSTRING, this will be done automatically. -/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic. -/// \param val The string value of this object. -/// \param setType The object type to force this object to. -DTSC::DTMI::DTMI(std::string indice, std::string val, DTSC::DTMItype setType){//str type initializer - myIndice = indice; - myType = setType; - strval = val; - numval = 0; -}; - -/// Constructor for container objects. -/// The object type is by default AMF::AMF0_OBJECT, but this can be forced to a different value. -/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic. -/// \param setType The object type to force this object to. -DTSC::DTMI::DTMI(std::string indice, DTSC::DTMItype setType){//object type initializer - myIndice = indice; - myType = setType; - strval = ""; - numval = 0; -}; - -/// Prints the contents of this object to std::cerr. -/// If this object contains other objects, it will call itself recursively -/// and print all nested content in a nice human-readable format. -void DTSC::DTMI::Print(std::string indent){ - std::cerr << indent; - // print my type - switch (myType){ - case DTMItype::DTMI_INT: std::cerr << "Integer"; break; - case DTMItype::DTMI_STRING: std::cerr << "String"; break; - case DTMItype::DTMI_OBJECT: std::cerr << "Object"; break; - case DTMItype::DTMI_OBJ_END: std::cerr << "Object end"; break; - case DTMItype::DTMI_ROOT: std::cerr << "Root Node"; break; - } - // print my string indice, if available - std::cerr << " " << myIndice << " "; - // print my numeric or string contents - switch (myType){ - case DTMItype::DTMI_INT: std::cerr << numval; break; - case DTMItype::DTMI_STRING: std::cerr << strval; break; - default: break;//we don't care about the rest, and don't want a compiler warning... - } - std::cerr << std::endl; - // if I hold other objects, print those too, recursively. - if (contents.size() > 0){ - for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){it->Print(indent+" ");} - } -};//print - -/// Packs the AMF object to a std::string for transfer over the network. -/// If the object is a container type, this function will call itself recursively and contain all contents. -std::string DTSC::DTMI::Pack(){ - std::string r = ""; - //skip output of DDV container types, they do not exist. Only output their contents. - if (myType != DTMItype::DTMI_ROOT){r += myType;} - //output the properly formatted data stream for this object's contents. - switch (myType){ - case DTMItype::DTMI_INT: - r += *(((char*)&numval)+7); r += *(((char*)&numval)+6); - r += *(((char*)&numval)+5); r += *(((char*)&numval)+4); - r += *(((char*)&numval)+3); r += *(((char*)&numval)+2); - r += *(((char*)&numval)+1); r += *(((char*)&numval)); - break; - case DTMItype::DTMI_STRING: - r += strval.size() / (256*256*256); - r += strval.size() / (256*256); - r += strval.size() / 256; - r += strval.size() % 256; - r += strval; - break; - case DTMItype::DTMI_OBJECT: - if (contents.size() > 0){ - for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ - r += it->Indice().size() / 256; - r += it->Indice().size() % 256; - r += it->Indice(); - r += it->Pack(); - } - } - r += (char)0; r += (char)0; r += (char)9; - break; - case DTMItype::DTMI_ROOT://only send contents - if (contents.size() > 0){ - for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ - r += it->Pack(); - } - } - break; - } - return r; -};//pack - -/// Parses a single AMF0 type - used recursively by the AMF::parse() functions. -/// This function updates i every call with the new position in the data. -/// \param data The raw data to parse. -/// \param len The size of the raw data. -/// \param i Current parsing position in the raw data. -/// \param name Indice name for any new object created. -/// \returns A single DTSC::DTMI, parsed from the raw data. -DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name){ - std::string tmpstr; - unsigned int tmpi = 0; - unsigned char tmpdbl[8]; - #if DEBUG >= 10 - fprintf(stderr, "Note: AMF type %hhx found. %i bytes left\n", data[i], len-i); - #endif - switch (data[i]){ - case DTMI::DTMI_INT: - tmpdbl[7] = data[i+1]; - tmpdbl[6] = data[i+2]; - tmpdbl[5] = data[i+3]; - tmpdbl[4] = data[i+4]; - tmpdbl[3] = data[i+5]; - tmpdbl[2] = data[i+6]; - tmpdbl[1] = data[i+7]; - tmpdbl[0] = data[i+8]; - i+=9;//skip 8(a double)+1 forwards - return DTSC::DTMI(name, *(uint64_t*)tmpdbl, AMF::AMF0_NUMBER); - break; - case DTMI::DTMI_STRING: - tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];//set tmpi to UTF-8-long length - tmpstr.clear();//clean tmpstr, just to be sure - tmpstr.append((const char *)data+i+5, (size_t)tmpi);//add the string data - i += tmpi + 5;//skip length+size+1 forwards - return DTSC::DTMI(name, tmpstr, AMF::AMF0_LONGSTRING); - break; - case DTMI::DTMI_OBJECT:{ - ++i; - DTSC::DTMI ret(name, DTMI::DTMI_OBJECT); - while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x000009) - tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length - tmpstr.clear();//clean tmpstr, just to be sure - tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data - i += tmpi + 2;//skip length+size forwards - ret.addContent(AMF::parseOne(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr - } - i += 3;//skip 0x000009 - return ret; - } break; - } - #if DEBUG >= 2 - fprintf(stderr, "Error: Unimplemented DTMI type %hhx - returning.\n", data[i]); - #endif - return DTSC::DTMI("error", DTMI::DTMI_ROOT); -}//parseOne - -/// Parses a C-string to a valid DTSC::DTMI. -/// This function will find all AMF objects in the string and return -/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI. -DTSC::DTMI DTSC::parseDTMI(const unsigned char * data, unsigned int len){ - DTSC::DTMI ret("returned", DTMI::DTMI_ROOT);//container type - unsigned int i = 0, j = 0; - while (i < len){ - ret.addContent(AMF::parseOne(data, len, i, "")); - if (i > j){j = i;}else{return ret;} - } - return ret; -}//parse - -/// Parses a std::string to a valid DTSC::DTMI. -/// This function will find all AMF objects in the string and return -/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI. -DTSC::DTMI DTSC::parseDTMI(std::string data){ - return parse((const unsigned char*)data.c_str(), data.size()); -}//parse diff --git a/util/dtmi.h b/util/dtmi.h deleted file mode 100644 index 410df97d..00000000 --- a/util/dtmi.h +++ /dev/null @@ -1,58 +0,0 @@ -/// \file dtmi.h -/// Holds all headers for DDVTECH MediaInfo parsing/generation. - -#pragma once -#include -#include -//#include -#include - -/// Holds all DDVTECH Stream Container classes and parsers. -namespace DTSC{ - - /// Enumerates all possible DTMI types. - enum DTMItype { - DTMI_INT = 0x01, ///< Unsigned 64-bit integer. - DTMI_STRING = 0x02, ///< String, equivalent to the AMF longstring type. - DTMI_OBJECT = 0xE0, ///< Object, equivalent to the AMF object type. - DTMI_OBJ_END = 0xEE, ///< End of object marker. - DTMI_ROOT = 0xFF ///< Root node for all DTMI data. - }; - - /// Recursive class that holds DDVTECH MediaInfo. - class DTMI { - public: - std::string Indice(); - DTMItype GetType(); - uint64_t NumValue(); - std::string StrValue(); - const char * Str(); - int hasContent(); - void addContent(DTMI c); - DTMI* getContentP(int i); - DTMI getContent(int i); - DTMI* getContentP(std::string s); - DTMI getContent(std::string s); - DTMI(); - DTMI(std::string indice, double val, DTMItype setType = DTMI_INT); - DTMI(std::string indice, std::string val, DTMItype setType = DTMI_STRING); - DTMI(std::string indice, DTMItype setType = DTMI_OBJECT); - void Print(std::string indent = ""); - std::string Pack(); - protected: - std::string myIndice; ///< Holds this objects indice, if any. - DTMItype myType; ///< Holds this objects AMF0 type. - std::string strval; ///< Holds this objects string value, if any. - uint64_t numval; ///< Holds this objects numeric value, if any. - std::vector contents; ///< Holds this objects contents, if any (for container types). - };//AMFType - - /// Parses a C-string to a valid DTSC::DTMI. - DTMI parseDTMI(const unsigned char * data, unsigned int len); - /// Parses a std::string to a valid DTSC::DTMI. - DTMI parseDTMI(std::string data); - /// Parses a single DTMI type - used recursively by the DTSC::parseDTMI() functions. - DTMI parseOneDTMI(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name); - -};//DTSC namespace - diff --git a/util/dtsc.cpp b/util/dtsc.cpp index bc722ecf..9d046287 100644 --- a/util/dtsc.cpp +++ b/util/dtsc.cpp @@ -2,33 +2,50 @@ /// Holds all code for DDVTECH Stream Container parsing/generation. #include "dtsc.h" -#include "string.h" //for memcmp -#include "arpa/inet.h" //for htonl/ntohl +#include //for memcmp +#include //for htonl/ntohl +#include //for fprint, stderr -char * DTSC::Magic_Header = "DTSC"; -char * DTSC::Magic_Packet = "DTPD"; +char DTSC::Magic_Header[] = "DTSC"; +char DTSC::Magic_Packet[] = "DTPD"; +/// Initializes a DTSC::Stream with only one packet buffer. DTSC::Stream::Stream(){ datapointer = 0; + buffercount = 1; } +/// Initializes a DTSC::Stream with a minimum of rbuffers packet buffers. +/// The actual buffer count may not at all times be the requested amount. +DTSC::Stream::Stream(unsigned int rbuffers){ + datapointer = 0; + if (rbuffers < 1){rbuffers = 1;} + buffercount = rbuffers; +} + +/// Attempts to parse a packet from the given std::string buffer. +/// Returns true if successful, removing the parsed part from the buffer string. +/// Returns false if invalid or not enough data is in the buffer. +/// \arg buffer The std::string buffer to attempt to parse. bool DTSC::Stream::parsePacket(std::string & buffer){ uint32_t len; if (buffer.length() > 8){ if (memcmp(buffer.c_str(), DTSC::Magic_Header, 4) == 0){ len = ntohl(((uint32_t *)buffer.c_str())[1]); if (buffer.length() < len+8){return false;} - metadata = DTSC::parseDTMI(buffer.c_str() + 8, len); + metadata = DTSC::parseDTMI((unsigned char*)buffer.c_str() + 8, len); + buffer.erase(0, len+8); } if (memcmp(buffer.c_str(), DTSC::Magic_Packet, 4) == 0){ len = ntohl(((uint32_t *)buffer.c_str())[1]); if (buffer.length() < len+8){return false;} - lastPacket = DTSC::parseDTMI(buffer.c_str() + 8, len); + buffers.push_front(DTSC::DTMI("empty", DTMI_ROOT)); + buffers.front() = DTSC::parseDTMI((unsigned char*)buffer.c_str() + 8, len); datapointertype = INVALID; - if (lastPacket.getContentP("data")){ - datapointer = lastPacket.getContentP("data")->StrValue.c_str(); - if (lastPacket.getContentP("datatype")){ - std::string tmp = lastPacket.getContentP("datatype")->StrValue(); + if (buffers.front().getContentP("data")){ + datapointer = buffers.front().getContentP("data")->StrValue().c_str(); + if (buffers.front().getContentP("datatype")){ + std::string tmp = buffers.front().getContentP("datatype")->StrValue(); if (tmp == "video"){datapointertype = VIDEO;} if (tmp == "audio"){datapointertype = AUDIO;} if (tmp == "meta"){datapointertype = META;} @@ -36,23 +53,372 @@ bool DTSC::Stream::parsePacket(std::string & buffer){ }else{ datapointer = 0; } + buffer.erase(0, len+8); + while (buffers.size() > buffercount){buffers.pop_back();} + advanceRings(); } + #if DEBUG >= 2 + std::cerr << "Error: Invalid DTMI data! I *will* get stuck!" << std::endl; + #endif } return false; } -char * DTSC::Stream::lastData(){ +/// Returns a direct pointer to the data attribute of the last received packet, if available. +/// Returns NULL if no valid pointer or packet is available. +const char * DTSC::Stream::lastData(){ return datapointer; } +/// Returns the packed in this buffer number. +/// \arg num Buffer number. +DTSC::DTMI & DTSC::Stream::getPacket(unsigned int num){ + return buffers[num]; +} + +/// Returns the type of the last received packet. DTSC::datatype DTSC::Stream::lastType(){ return datapointertype; } +/// Returns true if the current stream contains at least one video track. bool DTSC::Stream::hasVideo(){ return (metadata.getContentP("video") != 0); } +/// Returns true if the current stream contains at least one audio track. bool DTSC::Stream::hasAudio(){ return (metadata.getContentP("audio") != 0); } + +/// Returns a packed DTSC packet, ready to sent over the network. +std::string DTSC::Stream::outPacket(unsigned int num){ + std::string tmp; + unsigned int size; + tmp = Magic_Packet; + size = htonl(buffers[num].Pack().length()); + tmp.append((char*)&size, 4); + tmp.append(buffers[num].Pack()); + return tmp; +} + +/// Returns a packed DTSC header, ready to sent over the network. +std::string DTSC::Stream::outHeader(){ + std::string tmp; + unsigned int size; + tmp = Magic_Header; + size = htonl(metadata.Pack().length()); + tmp.append((char*)&size, 4); + tmp.append(metadata.Pack()); + return tmp; +} + +/// advances all given out and internal Ring classes to point to the new buffer, after one has been added. +/// Also updates the internal keyframes ring, as well as marking rings as starved if they are. +/// Unsets waiting rings, updating them with their new buffer number. +void DTSC::Stream::advanceRings(){ + std::deque::iterator dit; + std::set::iterator sit; + for (sit = rings.begin(); sit != rings.end(); sit++){ + (*sit)->b++; + if ((*sit)->waiting){(*sit)->waiting = false; (*sit)->b = 0;} + if ((*sit)->b >= buffers.size()){(*sit)->starved = true;} + } + for (dit = keyframes.begin(); dit != keyframes.end(); dit++){ + dit->b++; + if (dit->b >= buffers.size()){keyframes.erase(dit); break;} + } + if ((lastType() == VIDEO) && (buffers.front().getContentP("keyframe"))){ + keyframes.push_front(DTSC::Ring(0)); + } + //increase buffer size if no keyframes available + if ((buffercount > 1) && (keyframes.size() < 1)){buffercount++;} +} + +/// Constructs a new Ring, at the given buffer position. +/// \arg v Position for buffer. +DTSC::Ring::Ring(unsigned int v){ + b = v; + waiting = false; + starved = false; +} + +/// Requests a new Ring, which will be created and added to the internal Ring list. +/// This Ring will be kept updated so it always points to valid data or has the starved boolean set. +/// Don't forget to call dropRing() for all requested Ring classes that are no longer neccessary! +DTSC::Ring * DTSC::Stream::getRing(){ + DTSC::Ring * tmp; + if (keyframes.size() == 0){ + tmp = new DTSC::Ring(0); + }else{ + tmp = new DTSC::Ring(keyframes[0].b); + } + rings.insert(tmp); + return tmp; +} + +/// Deletes a given out Ring class from memory and internal Ring list. +/// Checks for NULL pointers and invalid pointers, silently discarding them. +void DTSC::Stream::dropRing(DTSC::Ring * ptr){ + if (rings.find(ptr) != rings.end()){ + rings.erase(ptr); + delete ptr; + } +} + +/// Properly cleans up the object for erasing. +/// Drops all Ring classes that have been given out. +DTSC::Stream::~Stream(){ + std::set::iterator sit; + for (sit = rings.begin(); sit != rings.end(); sit++){delete (*sit);} +} + +/// Returns the std::string Indice for the current object, if available. +/// Returns an empty string if no indice exists. +std::string DTSC::DTMI::Indice(){return myIndice;}; + +/// Returns the DTSC::DTMItype AMF0 object type for this object. +DTSC::DTMItype DTSC::DTMI::GetType(){return myType;}; + +/// Returns the numeric value of this object, if available. +/// If this object holds no numeric value, 0 is returned. +uint64_t DTSC::DTMI::NumValue(){return numval;}; + +/// Returns the std::string value of this object, if available. +/// If this object holds no string value, an empty string is returned. +std::string DTSC::DTMI::StrValue(){return strval;}; + +/// Returns the C-string value of this object, if available. +/// If this object holds no string value, an empty C-string is returned. +const char * DTSC::DTMI::Str(){return strval.c_str();}; + +/// Returns a count of the amount of objects this object currently holds. +/// If this object is not a container type, this function will always return 0. +int DTSC::DTMI::hasContent(){return contents.size();}; + +/// Adds an DTSC::DTMI to this object. Works for all types, but only makes sense for container types. +/// This function resets DTMI::packed to an empty string, forcing a repack on the next call to DTMI::Pack. +void DTSC::DTMI::addContent(DTSC::DTMI c){contents.push_back(c); packed = "";}; + +/// Returns a pointer to the object held at indice i. +/// Returns AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice. +/// \param i The indice of the object in this container. +DTSC::DTMI* DTSC::DTMI::getContentP(int i){return &contents.at(i);}; + +/// Returns a copy of the object held at indice i. +/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice. +/// \param i The indice of the object in this container. +DTSC::DTMI DTSC::DTMI::getContent(int i){return contents.at(i);}; + +/// Returns a pointer to the object held at indice s. +/// Returns NULL if no object is held at this indice. +/// \param s The indice of the object in this container. +DTSC::DTMI* DTSC::DTMI::getContentP(std::string s){ + for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ + if (it->Indice() == s){return &(*it);} + } + return 0; +}; + +/// Returns a copy of the object held at indice s. +/// Returns a AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice. +/// \param s The indice of the object in this container. +DTSC::DTMI DTSC::DTMI::getContent(std::string s){ + for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ + if (it->Indice() == s){return *it;} + } + return DTSC::DTMI("error", DTMI_ROOT); +}; + +/// Default constructor. +/// Simply fills the data with DTSC::DTMI("error", AMF0_DDV_CONTAINER) +DTSC::DTMI::DTMI(){ + *this = DTSC::DTMI("error", DTMI_ROOT); +};//default constructor + +/// Constructor for numeric objects. +/// The object type is by default AMF::AMF0_NUMBER, but this can be forced to a different value. +/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic. +/// \param val The numeric value of this object. Numeric AMF0 objects only support double-type values. +/// \param setType The object type to force this object to. +DTSC::DTMI::DTMI(std::string indice, double val, DTSC::DTMItype setType){//num type initializer + myIndice = indice; + myType = setType; + strval = ""; + numval = val; +}; + +/// Constructor for string objects. +/// The object type is by default AMF::AMF0_STRING, but this can be forced to a different value. +/// There is no need to manually change the type to AMF::AMF0_LONGSTRING, this will be done automatically. +/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic. +/// \param val The string value of this object. +/// \param setType The object type to force this object to. +DTSC::DTMI::DTMI(std::string indice, std::string val, DTSC::DTMItype setType){//str type initializer + myIndice = indice; + myType = setType; + strval = val; + numval = 0; +}; + +/// Constructor for container objects. +/// The object type is by default AMF::AMF0_OBJECT, but this can be forced to a different value. +/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic. +/// \param setType The object type to force this object to. +DTSC::DTMI::DTMI(std::string indice, DTSC::DTMItype setType){//object type initializer + myIndice = indice; + myType = setType; + strval = ""; + numval = 0; +}; + +/// Prints the contents of this object to std::cerr. +/// If this object contains other objects, it will call itself recursively +/// and print all nested content in a nice human-readable format. +void DTSC::DTMI::Print(std::string indent){ + std::cerr << indent; + // print my type + switch (myType){ + case DTMI_INT: std::cerr << "Integer"; break; + case DTMI_STRING: std::cerr << "String"; break; + case DTMI_OBJECT: std::cerr << "Object"; break; + case DTMI_OBJ_END: std::cerr << "Object end"; break; + case DTMI_ROOT: std::cerr << "Root Node"; break; + } + // print my string indice, if available + std::cerr << " " << myIndice << " "; + // print my numeric or string contents + switch (myType){ + case DTMI_INT: std::cerr << numval; break; + case DTMI_STRING: std::cerr << strval; break; + default: break;//we don't care about the rest, and don't want a compiler warning... + } + std::cerr << std::endl; + // if I hold other objects, print those too, recursively. + if (contents.size() > 0){ + for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){it->Print(indent+" ");} + } +};//print + +/// Packs the DTMI to a std::string for transfer over the network. +/// If a packed version already exists, does not regenerate it. +/// If the object is a container type, this function will call itself recursively and contain all contents. +std::string DTSC::DTMI::Pack(){ + if (packed != ""){return packed;} + std::string r = ""; + //skip output of DDV container types, they do not exist. Only output their contents. + if (myType != DTMI_ROOT){r += myType;} + //output the properly formatted data stream for this object's contents. + switch (myType){ + case DTMI_INT: + r += *(((char*)&numval)+7); r += *(((char*)&numval)+6); + r += *(((char*)&numval)+5); r += *(((char*)&numval)+4); + r += *(((char*)&numval)+3); r += *(((char*)&numval)+2); + r += *(((char*)&numval)+1); r += *(((char*)&numval)); + break; + case DTMI_STRING: + r += strval.size() / (256*256*256); + r += strval.size() / (256*256); + r += strval.size() / 256; + r += strval.size() % 256; + r += strval; + break; + case DTMI_OBJECT: + if (contents.size() > 0){ + for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ + r += it->Indice().size() / 256; + r += it->Indice().size() % 256; + r += it->Indice(); + r += it->Pack(); + } + } + r += (char)0; r += (char)0; r += (char)9; + break; + case DTMI_ROOT://only send contents + if (contents.size() > 0){ + for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ + r += it->Pack(); + } + } + break; + case DTMI_OBJ_END: + break; + } + packed = r; + return r; +};//pack + +/// Parses a single AMF0 type - used recursively by the AMF::parse() functions. +/// This function updates i every call with the new position in the data. +/// \param data The raw data to parse. +/// \param len The size of the raw data. +/// \param i Current parsing position in the raw data. +/// \param name Indice name for any new object created. +/// \returns A single DTSC::DTMI, parsed from the raw data. +DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name){ + std::string tmpstr; + unsigned int tmpi = 0; + unsigned char tmpdbl[8]; + #if DEBUG >= 10 + fprintf(stderr, "Note: AMF type %hhx found. %i bytes left\n", data[i], len-i); + #endif + switch (data[i]){ + case DTMI_INT: + tmpdbl[7] = data[i+1]; + tmpdbl[6] = data[i+2]; + tmpdbl[5] = data[i+3]; + tmpdbl[4] = data[i+4]; + tmpdbl[3] = data[i+5]; + tmpdbl[2] = data[i+6]; + tmpdbl[1] = data[i+7]; + tmpdbl[0] = data[i+8]; + i+=9;//skip 8(a double)+1 forwards + return DTSC::DTMI(name, *(uint64_t*)tmpdbl, DTMI_INT); + break; + case DTMI_STRING: + tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];//set tmpi to UTF-8-long length + tmpstr.clear();//clean tmpstr, just to be sure + tmpstr.append((const char *)data+i+5, (size_t)tmpi);//add the string data + i += tmpi + 5;//skip length+size+1 forwards + return DTSC::DTMI(name, tmpstr, DTMI_STRING); + break; + case DTMI_OBJECT:{ + ++i; + DTSC::DTMI ret(name, DTMI_OBJECT); + while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x000009) + tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length + tmpstr.clear();//clean tmpstr, just to be sure + tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data + i += tmpi + 2;//skip length+size forwards + ret.addContent(parseOneDTMI(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr + } + i += 3;//skip 0x000009 + return ret; + } break; + } + #if DEBUG >= 2 + fprintf(stderr, "Error: Unimplemented DTMI type %hhx - returning.\n", data[i]); + #endif + return DTSC::DTMI("error", DTMI_ROOT); +}//parseOne + +/// Parses a C-string to a valid DTSC::DTMI. +/// This function will find all AMF objects in the string and return +/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI. +DTSC::DTMI DTSC::parseDTMI(const unsigned char * data, unsigned int len){ + DTSC::DTMI ret("returned", DTMI_ROOT);//container type + unsigned int i = 0, j = 0; + while (i < len){ + ret.addContent(parseOneDTMI(data, len, i, "")); + if (i > j){j = i;}else{return ret;} + } + ret.packed = std::string((char*)data, (size_t)len); + return ret; +}//parse + +/// Parses a std::string to a valid DTSC::DTMI. +/// This function will find all AMF objects in the string and return +/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI. +DTSC::DTMI DTSC::parseDTMI(std::string data){ + return parseDTMI((const unsigned char*)data.c_str(), data.size()); +}//parse diff --git a/util/dtsc.h b/util/dtsc.h index 6b918f30..3a025753 100644 --- a/util/dtsc.h +++ b/util/dtsc.h @@ -2,46 +2,117 @@ /// Holds all headers for DDVTECH Stream Container parsing/generation. #pragma once -#include "dtmi.h" +#include +#include +#include //for uint64_t +#include +#include +#include -// Video: -// Codec (string) +// video +// codec (string) -// Audio: -// Codec (string) -// Samping rate (int, Hz) -// Sample Size (int, bytesize) -// Channels (int, channelcount) +// audio +// codec (string) +// sampingrate (int, Hz) +// samplesize (int, bytesize) +// channels (int, channelcount) +/// Holds all DDVTECH Stream Container classes and parsers. namespace DTSC{ + /// Enumerates all possible DTMI types. + enum DTMItype { + DTMI_INT = 0x01, ///< Unsigned 64-bit integer. + DTMI_STRING = 0x02, ///< String, equivalent to the AMF longstring type. + DTMI_OBJECT = 0xE0, ///< Object, equivalent to the AMF object type. + DTMI_OBJ_END = 0xEE, ///< End of object marker. + DTMI_ROOT = 0xFF ///< Root node for all DTMI data. + }; + + /// Recursive class that holds DDVTECH MediaInfo. + class DTMI { + public: + std::string Indice(); + DTMItype GetType(); + uint64_t NumValue(); + std::string StrValue(); + const char * Str(); + int hasContent(); + void addContent(DTMI c); + DTMI* getContentP(int i); + DTMI getContent(int i); + DTMI* getContentP(std::string s); + DTMI getContent(std::string s); + DTMI(); + DTMI(std::string indice, double val, DTMItype setType = DTMI_INT); + DTMI(std::string indice, std::string val, DTMItype setType = DTMI_STRING); + DTMI(std::string indice, DTMItype setType = DTMI_OBJECT); + void Print(std::string indent = ""); + std::string Pack(); + std::string packed; + protected: + std::string myIndice; ///< Holds this objects indice, if any. + DTMItype myType; ///< Holds this objects AMF0 type. + std::string strval; ///< Holds this objects string value, if any. + uint64_t numval; ///< Holds this objects numeric value, if any. + std::vector contents; ///< Holds this objects contents, if any (for container types). + };//AMFType + + /// Parses a C-string to a valid DTSC::DTMI. + DTMI parseDTMI(const unsigned char * data, unsigned int len); + /// Parses a std::string to a valid DTSC::DTMI. + DTMI parseDTMI(std::string data); + /// Parses a single DTMI type - used recursively by the DTSC::parseDTMI() functions. + DTMI parseOneDTMI(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name); + /// This enum holds all possible datatypes for DTSC packets. enum datatype { AUDIO, ///< Stream Audio data VIDEO, ///< Stream Video data META, ///< Stream Metadata INVALID ///< Anything else or no data available. - } + }; - char * Magic_Header; ///< The magic bytes for a DTSC header - char * Magic_Packet; ///< The magic bytes for a DTSC packet + extern char Magic_Header[]; ///< The magic bytes for a DTSC header + extern char Magic_Packet[]; ///< The magic bytes for a DTSC packet - /// Holds temporary data for a DTSC stream and provides functions to access/store it. + /// A part from the DTSC::Stream ringbuffer. + /// Holds information about a buffer that will stay consistent + class Ring { + public: + Ring(unsigned int v); + unsigned int b; ///< Holds current number of buffer. May and is intended to change unexpectedly! + bool waiting; ///< If true, this Ring is currently waiting for a buffer fill. + bool starved; ///< If true, this Ring can no longer receive valid data. + }; + + /// Holds temporary data for a DTSC stream and provides functions to utilize it. + /// Optionally also acts as a ring buffer of a certain requested size. + /// If ring buffering mode is enabled, it will automatically grow in size to always contain at least one keyframe. class Stream { public: Stream(); + ~Stream(); + Stream(unsigned int buffers); DTSC::DTMI metadata; - DRSC::DTMI lastPacket; + DTSC::DTMI & getPacket(unsigned int num = 0); datatype lastType(); - char * lastData(); + const char * lastData(); bool hasVideo(); bool hasAudio(); bool parsePacket(std::string & buffer); - private: - char * datapointer; + std::string outPacket(unsigned int num); + std::string outHeader(); + Ring * getRing(); + void dropRing(Ring * ptr); + private: + std::deque buffers; + std::set rings; + std::deque keyframes; + void advanceRings(); + const char * datapointer; datatype datapointertype; - } - - - + unsigned int buffercount; + }; }; diff --git a/util/socket.cpp b/util/socket.cpp index 4fdaceec..ed669e75 100644 --- a/util/socket.cpp +++ b/util/socket.cpp @@ -270,7 +270,7 @@ bool Socket::Connection::write(const void * buffer, int len){ /// \param buffer Location of the buffer to read to. /// \param len Amount of bytes to read. /// \returns True if the whole read was succesfull, false otherwise. -bool Socket::Connection::read(void * buffer, int len){ +bool Socket::Connection::read(const void * buffer, int len){ int sofar = 0; if (sock < 0){return false;} while (sofar != len){ @@ -309,9 +309,9 @@ bool Socket::Connection::read(void * buffer, int len){ }//Socket::Connection::read /// Read call that is compatible with file access syntax. This function simply calls the other read function. -bool Socket::Connection::read(void * buffer, int width, int count){return read(buffer, width*count);} +bool Socket::Connection::read(const void * buffer, int width, int count){return read(buffer, width*count);} /// Write call that is compatible with file access syntax. This function simply calls the other write function. -bool Socket::Connection::write(void * buffer, int width, int count){return write(buffer, width*count);} +bool Socket::Connection::write(const void * buffer, int width, int count){return write(buffer, width*count);} /// Write call that is compatible with std::string. This function simply calls the other write function. bool Socket::Connection::write(const std::string data){return write(data.c_str(), data.size());} @@ -320,7 +320,7 @@ bool Socket::Connection::write(const std::string data){return write(data.c_str() /// \param buffer Location of the buffer to write from. /// \param len Amount of bytes to write. /// \returns The amount of bytes actually written. -int Socket::Connection::iwrite(void * buffer, int len){ +int Socket::Connection::iwrite(const void * buffer, int len){ if (sock < 0){return 0;} int r = send(sock, buffer, len, 0); if (r < 0){ diff --git a/util/socket.h b/util/socket.h index bfe824e5..747c861b 100644 --- a/util/socket.h +++ b/util/socket.h @@ -37,12 +37,12 @@ namespace Socket{ bool canWrite(); ///< Calls poll() on the socket, checking if data can be written. signed int ready(); ///< Returns the ready-state for this socket. bool connected(); ///< Returns the connected-state for this socket. - bool read(void * buffer, int len); ///< Reads data from socket. - bool read(void * buffer, int width, int count); ///< Read call that is compatible with file access syntax. + bool read(const void * buffer, int len); ///< Reads data from socket. + bool read(const void * buffer, int width, int count); ///< Read call that is compatible with file access syntax. bool write(const void * buffer, int len); ///< Writes data to socket. - bool write(void * buffer, int width, int count); ///< Write call that is compatible with file access syntax. + bool write(const void * buffer, int width, int count); ///< Write call that is compatible with file access syntax. bool write(const std::string data); ///< Write call that is compatible with std::string. - int iwrite(void * buffer, int len); ///< Incremental write call. + int iwrite(const void * buffer, int len); ///< Incremental write call. int iread(void * buffer, int len); ///< Incremental read call. bool read(std::string & buffer); ///< Read call that is compatible with std::string. bool swrite(std::string & buffer); ///< Read call that is compatible with std::string. From 553596c1e3a5027551bde2c1013e89b80ab8822a Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 13 Sep 2011 17:20:39 +0200 Subject: [PATCH 2/7] Finished FLV2DTSC code, fixed a lot of bugs in new code, added DTSC info tool. --- util/dtsc.cpp | 134 ++++++++++++++++++++++++++++------------------- util/dtsc.h | 9 ++-- util/flv_tag.cpp | 2 +- 3 files changed, 87 insertions(+), 58 deletions(-) diff --git a/util/dtsc.cpp b/util/dtsc.cpp index 9d046287..8301e5b3 100644 --- a/util/dtsc.cpp +++ b/util/dtsc.cpp @@ -35,6 +35,7 @@ bool DTSC::Stream::parsePacket(std::string & buffer){ if (buffer.length() < len+8){return false;} metadata = DTSC::parseDTMI((unsigned char*)buffer.c_str() + 8, len); buffer.erase(0, len+8); + return false; } if (memcmp(buffer.c_str(), DTSC::Magic_Packet, 4) == 0){ len = ntohl(((uint32_t *)buffer.c_str())[1]); @@ -56,6 +57,7 @@ bool DTSC::Stream::parsePacket(std::string & buffer){ buffer.erase(0, len+8); while (buffers.size() > buffercount){buffers.pop_back();} advanceRings(); + return true; } #if DEBUG >= 2 std::cerr << "Error: Invalid DTMI data! I *will* get stuck!" << std::endl; @@ -92,25 +94,18 @@ bool DTSC::Stream::hasAudio(){ } /// Returns a packed DTSC packet, ready to sent over the network. -std::string DTSC::Stream::outPacket(unsigned int num){ - std::string tmp; - unsigned int size; - tmp = Magic_Packet; - size = htonl(buffers[num].Pack().length()); - tmp.append((char*)&size, 4); - tmp.append(buffers[num].Pack()); - return tmp; +std::string & DTSC::Stream::outPacket(unsigned int num){ + buffers[num].Pack(true); + return buffers[num].packed; } /// Returns a packed DTSC header, ready to sent over the network. -std::string DTSC::Stream::outHeader(){ - std::string tmp; - unsigned int size; - tmp = Magic_Header; - size = htonl(metadata.Pack().length()); - tmp.append((char*)&size, 4); - tmp.append(metadata.Pack()); - return tmp; +std::string & DTSC::Stream::outHeader(){ + if ((metadata.packed.length() < 4) || !metadata.netpacked){ + metadata.Pack(true); + metadata.packed.replace(0, 4, Magic_Header); + } + return metadata.packed; } /// advances all given out and internal Ring classes to point to the new buffer, after one has been added. @@ -198,7 +193,17 @@ int DTSC::DTMI::hasContent(){return contents.size();}; /// Adds an DTSC::DTMI to this object. Works for all types, but only makes sense for container types. /// This function resets DTMI::packed to an empty string, forcing a repack on the next call to DTMI::Pack. -void DTSC::DTMI::addContent(DTSC::DTMI c){contents.push_back(c); packed = "";}; +/// If the indice name already exists, replaces the indice. +void DTSC::DTMI::addContent(DTSC::DTMI c){ + std::vector::iterator it; + for (it = contents.begin(); it != contents.end(); it++){ + if (it->Indice() == c.Indice()){ + contents.erase(it); + break; + } + } + contents.push_back(c); packed = ""; +}; /// Returns a pointer to the object held at indice i. /// Returns AMF::AMF0_DDV_CONTAINER of indice "error" if no object is held at this indice. @@ -237,11 +242,11 @@ DTSC::DTMI::DTMI(){ };//default constructor /// Constructor for numeric objects. -/// The object type is by default AMF::AMF0_NUMBER, but this can be forced to a different value. +/// The object type is by default DTMItype::DTMI_INT, but this can be forced to a different value. /// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic. -/// \param val The numeric value of this object. Numeric AMF0 objects only support double-type values. +/// \param val The numeric value of this object. Numeric objects only support uint64_t values. /// \param setType The object type to force this object to. -DTSC::DTMI::DTMI(std::string indice, double val, DTSC::DTMItype setType){//num type initializer +DTSC::DTMI::DTMI(std::string indice, uint64_t val, DTSC::DTMItype setType){//num type initializer myIndice = indice; myType = setType; strval = ""; @@ -249,8 +254,6 @@ DTSC::DTMI::DTMI(std::string indice, double val, DTSC::DTMItype setType){//num t }; /// Constructor for string objects. -/// The object type is by default AMF::AMF0_STRING, but this can be forced to a different value. -/// There is no need to manually change the type to AMF::AMF0_LONGSTRING, this will be done automatically. /// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic. /// \param val The string value of this object. /// \param setType The object type to force this object to. @@ -262,8 +265,7 @@ DTSC::DTMI::DTMI(std::string indice, std::string val, DTSC::DTMItype setType){// }; /// Constructor for container objects. -/// The object type is by default AMF::AMF0_OBJECT, but this can be forced to a different value. -/// \param indice The string indice of this object in its container, or empty string if none. Numeric indices are automatic. +/// \param indice The string indice of this object in its container, or empty string if none. /// \param setType The object type to force this object to. DTSC::DTMI::DTMI(std::string indice, DTSC::DTMItype setType){//object type initializer myIndice = indice; @@ -290,7 +292,13 @@ void DTSC::DTMI::Print(std::string indent){ // print my numeric or string contents switch (myType){ case DTMI_INT: std::cerr << numval; break; - case DTMI_STRING: std::cerr << strval; break; + case DTMI_STRING: + if (strval.length() > 200 || ((strval.length() > 1) && ( (strval[0] < 'A') || (strval[0] > 'z') ) )){ + std::cerr << strval.length() << " bytes of data"; + }else{ + std::cerr << strval; + } + break; default: break;//we don't care about the rest, and don't want a compiler warning... } std::cerr << std::endl; @@ -303,11 +311,22 @@ void DTSC::DTMI::Print(std::string indent){ /// Packs the DTMI to a std::string for transfer over the network. /// If a packed version already exists, does not regenerate it. /// If the object is a container type, this function will call itself recursively and contain all contents. -std::string DTSC::DTMI::Pack(){ - if (packed != ""){return packed;} +/// \arg netpack If true, will pack as a full DTMI packet, if false only as the contents without header. +std::string DTSC::DTMI::Pack(bool netpack){ + if (packed != ""){ + if (netpacked == netpack){return packed;} + if (netpacked){ + packed.erase(0, 8); + }else{ + unsigned int size = htonl(packed.length()); + packed.insert(0, (char*)&size, 4); + packed.insert(0, Magic_Packet); + } + netpacked = !netpacked; + return packed; + } std::string r = ""; - //skip output of DDV container types, they do not exist. Only output their contents. - if (myType != DTMI_ROOT){r += myType;} + r += myType; //output the properly formatted data stream for this object's contents. switch (myType){ case DTMI_INT: @@ -324,6 +343,7 @@ std::string DTSC::DTMI::Pack(){ r += strval; break; case DTMI_OBJECT: + case DTMI_ROOT: if (contents.size() > 0){ for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ r += it->Indice().size() / 256; @@ -332,20 +352,19 @@ std::string DTSC::DTMI::Pack(){ r += it->Pack(); } } - r += (char)0; r += (char)0; r += (char)9; - break; - case DTMI_ROOT://only send contents - if (contents.size() > 0){ - for (std::vector::iterator it = contents.begin(); it != contents.end(); it++){ - r += it->Pack(); - } - } + r += (char)0x0; r += (char)0x0; r += (char)0xEE; break; case DTMI_OBJ_END: break; } packed = r; - return r; + netpacked = netpack; + if (netpacked){ + unsigned int size = htonl(packed.length()); + packed.insert(0, (char*)&size, 4); + packed.insert(0, Magic_Packet); + } + return packed; };//pack /// Parses a single AMF0 type - used recursively by the AMF::parse() functions. @@ -372,7 +391,7 @@ DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, un tmpdbl[2] = data[i+6]; tmpdbl[1] = data[i+7]; tmpdbl[0] = data[i+8]; - i+=9;//skip 8(a double)+1 forwards + i+=9;//skip 8(an uint64_t)+1 forwards return DTSC::DTMI(name, *(uint64_t*)tmpdbl, DTMI_INT); break; case DTMI_STRING: @@ -382,17 +401,30 @@ DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, un i += tmpi + 5;//skip length+size+1 forwards return DTSC::DTMI(name, tmpstr, DTMI_STRING); break; - case DTMI_OBJECT:{ + case DTMI_ROOT:{ ++i; - DTSC::DTMI ret(name, DTMI_OBJECT); - while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x000009) + DTSC::DTMI ret(name, DTMI_ROOT); + while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x0000EE) tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length tmpstr.clear();//clean tmpstr, just to be sure tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data i += tmpi + 2;//skip length+size forwards ret.addContent(parseOneDTMI(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr } - i += 3;//skip 0x000009 + i += 3;//skip 0x0000EE + return ret; + } break; + case DTMI_OBJECT:{ + ++i; + DTSC::DTMI ret(name, DTMI_OBJECT); + while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x0000EE) + tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length + tmpstr.clear();//clean tmpstr, just to be sure + tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data + i += tmpi + 2;//skip length+size forwards + ret.addContent(parseOneDTMI(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr + } + i += 3;//skip 0x0000EE return ret; } break; } @@ -403,22 +435,18 @@ DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, un }//parseOne /// Parses a C-string to a valid DTSC::DTMI. -/// This function will find all AMF objects in the string and return -/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI. +/// This function will find one DTMI object in the string and return it. DTSC::DTMI DTSC::parseDTMI(const unsigned char * data, unsigned int len){ - DTSC::DTMI ret("returned", DTMI_ROOT);//container type - unsigned int i = 0, j = 0; - while (i < len){ - ret.addContent(parseOneDTMI(data, len, i, "")); - if (i > j){j = i;}else{return ret;} - } + DTSC::DTMI ret;//container type + unsigned int i = 0; + ret = parseOneDTMI(data, len, i, ""); ret.packed = std::string((char*)data, (size_t)len); + ret.netpacked = false; return ret; }//parse /// Parses a std::string to a valid DTSC::DTMI. -/// This function will find all AMF objects in the string and return -/// them all packed in a single AMF::AMF0_DDV_CONTAINER DTSC::DTMI. +/// This function will find one DTMI object in the string and return it. DTSC::DTMI DTSC::parseDTMI(std::string data){ return parseDTMI((const unsigned char*)data.c_str(), data.size()); }//parse diff --git a/util/dtsc.h b/util/dtsc.h index 3a025753..7d5827df 100644 --- a/util/dtsc.h +++ b/util/dtsc.h @@ -45,11 +45,12 @@ namespace DTSC{ DTMI* getContentP(std::string s); DTMI getContent(std::string s); DTMI(); - DTMI(std::string indice, double val, DTMItype setType = DTMI_INT); + DTMI(std::string indice, uint64_t val, DTMItype setType = DTMI_INT); DTMI(std::string indice, std::string val, DTMItype setType = DTMI_STRING); DTMI(std::string indice, DTMItype setType = DTMI_OBJECT); void Print(std::string indent = ""); - std::string Pack(); + std::string Pack(bool netpack = false); + bool netpacked; std::string packed; protected: std::string myIndice; ///< Holds this objects indice, if any. @@ -102,8 +103,8 @@ namespace DTSC{ bool hasVideo(); bool hasAudio(); bool parsePacket(std::string & buffer); - std::string outPacket(unsigned int num); - std::string outHeader(); + std::string & outPacket(unsigned int num); + std::string & outHeader(); Ring * getRing(); void dropRing(Ring * ptr); private: diff --git a/util/flv_tag.cpp b/util/flv_tag.cpp index 526de7d0..2a7b671c 100644 --- a/util/flv_tag.cpp +++ b/util/flv_tag.cpp @@ -109,7 +109,7 @@ std::string FLV::Tag::tagType(){ case 4: R += "VP6"; break; case 5: R += "VP6Alpha"; break; case 6: R += "ScreenVideo2"; break; - case 7: R += "AVC"; break; + case 7: R += "H264"; break; default: R += "unknown"; break; } R += " video "; From ae48440f4b366c9687b84efcfc7622463d1cd5a7 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 13 Sep 2011 20:37:16 +0200 Subject: [PATCH 3/7] Buffer stabilized --- util/dtsc.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/util/dtsc.cpp b/util/dtsc.cpp index 8301e5b3..9e2a5ba4 100644 --- a/util/dtsc.cpp +++ b/util/dtsc.cpp @@ -117,7 +117,7 @@ void DTSC::Stream::advanceRings(){ for (sit = rings.begin(); sit != rings.end(); sit++){ (*sit)->b++; if ((*sit)->waiting){(*sit)->waiting = false; (*sit)->b = 0;} - if ((*sit)->b >= buffers.size()){(*sit)->starved = true;} + if ((*sit)->starved || ((*sit)->b >= buffers.size())){(*sit)->starved = true; (*sit)->b = 0;} } for (dit = keyframes.begin(); dit != keyframes.end(); dit++){ dit->b++; @@ -375,7 +375,6 @@ std::string DTSC::DTMI::Pack(bool netpack){ /// \param name Indice name for any new object created. /// \returns A single DTSC::DTMI, parsed from the raw data. DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name){ - std::string tmpstr; unsigned int tmpi = 0; unsigned char tmpdbl[8]; #if DEBUG >= 10 @@ -394,20 +393,18 @@ DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, un i+=9;//skip 8(an uint64_t)+1 forwards return DTSC::DTMI(name, *(uint64_t*)tmpdbl, DTMI_INT); break; - case DTMI_STRING: + case DTMI_STRING:{ tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];//set tmpi to UTF-8-long length - tmpstr.clear();//clean tmpstr, just to be sure - tmpstr.append((const char *)data+i+5, (size_t)tmpi);//add the string data + std::string tmpstr = std::string((const char *)data+i+5, (size_t)tmpi);//set the string data i += tmpi + 5;//skip length+size+1 forwards return DTSC::DTMI(name, tmpstr, DTMI_STRING); - break; + } break; case DTMI_ROOT:{ ++i; DTSC::DTMI ret(name, DTMI_ROOT); while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x0000EE) tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length - tmpstr.clear();//clean tmpstr, just to be sure - tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data + std::string tmpstr = std::string((const char *)data+i+2, (size_t)tmpi);//set the string data i += tmpi + 2;//skip length+size forwards ret.addContent(parseOneDTMI(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr } @@ -419,8 +416,7 @@ DTSC::DTMI DTSC::parseOneDTMI(const unsigned char *& data, unsigned int &len, un DTSC::DTMI ret(name, DTMI_OBJECT); while (data[i] + data[i+1] != 0){//while not encountering 0x0000 (we assume 0x0000EE) tmpi = data[i]*256+data[i+1];//set tmpi to the UTF-8 length - tmpstr.clear();//clean tmpstr, just to be sure - tmpstr.append((const char*)data+i+2, (size_t)tmpi);//add the string data + std::string tmpstr = std::string((const char *)data+i+2, (size_t)tmpi);//set the string data i += tmpi + 2;//skip length+size forwards ret.addContent(parseOneDTMI(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr } From 63bf65952bc3dace8ed43ce4a56970bf5b3a6c25 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 14 Sep 2011 00:05:54 +0200 Subject: [PATCH 4/7] Almost working HTTP connector - mid-rewrite, running into some issues, sleepy, going to bed... --- util/dtsc.cpp | 10 +- util/dtsc.h | 45 ++++++--- util/flv_tag.cpp | 251 +++++++++++++++++++++++++++++++++++++++++++++++ util/flv_tag.h | 5 + util/socket.h | 2 +- 5 files changed, 296 insertions(+), 17 deletions(-) diff --git a/util/dtsc.cpp b/util/dtsc.cpp index 9e2a5ba4..85e1ce47 100644 --- a/util/dtsc.cpp +++ b/util/dtsc.cpp @@ -44,7 +44,7 @@ bool DTSC::Stream::parsePacket(std::string & buffer){ buffers.front() = DTSC::parseDTMI((unsigned char*)buffer.c_str() + 8, len); datapointertype = INVALID; if (buffers.front().getContentP("data")){ - datapointer = buffers.front().getContentP("data")->StrValue().c_str(); + datapointer = &(buffers.front().getContentP("data")->StrValue()); if (buffers.front().getContentP("datatype")){ std::string tmp = buffers.front().getContentP("datatype")->StrValue(); if (tmp == "video"){datapointertype = VIDEO;} @@ -68,8 +68,8 @@ bool DTSC::Stream::parsePacket(std::string & buffer){ /// Returns a direct pointer to the data attribute of the last received packet, if available. /// Returns NULL if no valid pointer or packet is available. -const char * DTSC::Stream::lastData(){ - return datapointer; +std::string & DTSC::Stream::lastData(){ + return *datapointer; } /// Returns the packed in this buffer number. @@ -177,11 +177,11 @@ DTSC::DTMItype DTSC::DTMI::GetType(){return myType;}; /// Returns the numeric value of this object, if available. /// If this object holds no numeric value, 0 is returned. -uint64_t DTSC::DTMI::NumValue(){return numval;}; +uint64_t & DTSC::DTMI::NumValue(){return numval;}; /// Returns the std::string value of this object, if available. /// If this object holds no string value, an empty string is returned. -std::string DTSC::DTMI::StrValue(){return strval;}; +std::string & DTSC::DTMI::StrValue(){return strval;}; /// Returns the C-string value of this object, if available. /// If this object holds no string value, an empty C-string is returned. diff --git a/util/dtsc.h b/util/dtsc.h index 7d5827df..30bbc092 100644 --- a/util/dtsc.h +++ b/util/dtsc.h @@ -9,16 +9,39 @@ #include #include -// video -// codec (string) -// audio -// codec (string) -// sampingrate (int, Hz) -// samplesize (int, bytesize) -// channels (int, channelcount) /// Holds all DDVTECH Stream Container classes and parsers. +///Video: +/// - codec (string: AAC, MP3) +/// - width (int, pixels) +/// - height (int, pixels) +/// - fpks (int, frames per kilosecond (FPS * 1000)) +/// - bps (int, bytes per second) +/// - init (string, init data) +/// +///Audio: +/// - codec (string: H264, H263, VP6) +/// - rate (int, Hz) +/// - size (int, bitsize) +/// - bps (int, bytes per second) +/// - channels (int, channelcount) +/// - init (string, init data) +/// +///All packets: +/// - datatype (string: audio, video, meta (unused)) +/// - data (string: data) +/// - time (int: ms into video) +/// +///Video packets: +/// - keyframe (int, if set, is a seekable keyframe) +/// - interframe (int, if set, is a non-seekable interframe) +/// - disposableframe (int, if set, is a disposable interframe) +/// +///H264 video packets: +/// - nalu (int, if set, is a nalu) +/// - nalu_end (int, if set, is a end-of-sequence) +/// - offset (int, unsigned version of signed int! Holds the ms offset between timestamp and proper display time for B-frames) namespace DTSC{ /// Enumerates all possible DTMI types. @@ -35,8 +58,8 @@ namespace DTSC{ public: std::string Indice(); DTMItype GetType(); - uint64_t NumValue(); - std::string StrValue(); + uint64_t & NumValue(); + std::string & StrValue(); const char * Str(); int hasContent(); void addContent(DTMI c); @@ -99,7 +122,7 @@ namespace DTSC{ DTSC::DTMI metadata; DTSC::DTMI & getPacket(unsigned int num = 0); datatype lastType(); - const char * lastData(); + std::string & lastData(); bool hasVideo(); bool hasAudio(); bool parsePacket(std::string & buffer); @@ -112,7 +135,7 @@ namespace DTSC{ std::set rings; std::deque keyframes; void advanceRings(); - const char * datapointer; + std::string * datapointer; datatype datapointertype; unsigned int buffercount; }; diff --git a/util/flv_tag.cpp b/util/flv_tag.cpp index 2a7b671c..3af96eea 100644 --- a/util/flv_tag.cpp +++ b/util/flv_tag.cpp @@ -2,6 +2,7 @@ /// Holds all code for the FLV namespace. #include "flv_tag.h" +#include "amf.h" #include "rtmpchunks.h" #include //for Tag::FileLoader #include //for Tag::FileLoader @@ -245,6 +246,256 @@ FLV::Tag & FLV::Tag::operator= (const FLV::Tag& O){ return *this; }//assignment operator +/// FLV loader function from DTSC. +/// Takes the DTSC data and makes it into FLV. +bool FLV::Tag::DTSCLoader(DTSC::Stream & S){ + switch (S.lastType()){ + case DTSC::VIDEO: + len = S.lastData().length() + 16; + if (S.metadata.getContentP("video") && S.metadata.getContentP("video")->getContentP("codec")){ + if (S.metadata.getContentP("video")->getContentP("codec")->StrValue() == "H264"){len += 4;} + } + break; + case DTSC::AUDIO: + len = S.lastData().length() + 16; + if (S.metadata.getContentP("audio") && S.metadata.getContentP("audio")->getContentP("codec")){ + if (S.metadata.getContentP("audio")->getContentP("codec")->StrValue() == "AAC"){len += 1;} + } + break; + case DTSC::META: + len = S.lastData().length() + 15; + break; + default://ignore all other types (there are currently no other types...) + break; + } + if (len > 0){ + if (!data){ + data = (char*)malloc(len); + buf = len; + }else{ + if (buf < len){ + data = (char*)realloc(data, len); + buf = len; + } + } + switch (S.lastType()){ + case DTSC::VIDEO: + if ((unsigned int)len == S.lastData().length() + 16){ + memcpy(data+12, S.lastData().c_str(), S.lastData().length()); + }else{ + memcpy(data+16, S.lastData().c_str(), S.lastData().length()); + if (S.getPacket().getContentP("nalu")){data[12] = 1;}else{data[12] = 2;} + int offset = S.getPacket().getContentP("offset")->NumValue(); + data[13] = (offset >> 16) & 0xFF; + data[14] = (offset >> 8) & 0XFF; + data[15] = offset & 0xFF; + } + data[11] = 0; + if (S.metadata.getContentP("video")->getContentP("codec")->StrValue() == "H264"){data[11] += 7;} + if (S.metadata.getContentP("video")->getContentP("codec")->StrValue() == "H263"){data[11] += 2;} + if (S.getPacket().getContentP("keyframe")){data[11] += 0x10;} + if (S.getPacket().getContentP("interframe")){data[11] += 0x20;} + if (S.getPacket().getContentP("disposableframe")){data[11] += 0x30;} + break; + case DTSC::AUDIO: + if ((unsigned int)len == S.lastData().length() + 16){ + memcpy(data+12, S.lastData().c_str(), S.lastData().length()); + }else{ + memcpy(data+13, S.lastData().c_str(), S.lastData().length()); + data[12] = 1;//raw AAC data, not sequence header + } + data[11] = 0; + if (S.metadata.getContentP("audio")->getContentP("codec")->StrValue() == "AAC"){data[11] += 0xA0;} + if (S.metadata.getContentP("audio")->getContentP("codec")->StrValue() == "MP3"){data[11] += 0x20;} + if (S.metadata.getContentP("audio")->getContentP("rate")->NumValue() == 11025){data[11] += 0x04;} + if (S.metadata.getContentP("audio")->getContentP("rate")->NumValue() == 22050){data[11] += 0x08;} + if (S.metadata.getContentP("audio")->getContentP("rate")->NumValue() == 44100){data[11] += 0x0C;} + if (S.metadata.getContentP("audio")->getContentP("size")->NumValue() == 16){data[11] += 0x02;} + if (S.metadata.getContentP("audio")->getContentP("channels")->NumValue() > 1){data[11] += 0x01;} + break; + case DTSC::META: + memcpy(data+11, S.lastData().c_str(), S.lastData().length()); + break; + default: break; + } + } + ((unsigned int *)(data+len-4))[0] = len-15; + switch (S.lastType()){ + case DTSC::VIDEO: data[0] = 0x09; break; + case DTSC::AUDIO: data[0] = 0x08; break; + case DTSC::META: data[0] = 0x12; break; + default: break; + } + data[1] = ((len-15) >> 16) & 0xFF; + data[2] = ((len-15) >> 8) & 0xFF; + data[3] = (len-15) & 0xFF; + tagTime(S.getPacket().getContentP("time")->NumValue()); + return true; +} + +/// FLV Video init data loader function from DTSC. +/// Takes the DTSC Video init data and makes it into FLV. +/// Assumes init data is available - so check before calling! +bool FLV::Tag::DTSCVideoInit(DTSC::Stream & S){ + if (S.metadata.getContentP("video")->getContentP("codec")->StrValue() == "H264"){ + len = S.metadata.getContentP("video")->getContentP("init")->StrValue().length() + 20; + } + if (len > 0){ + if (!data){ + data = (char*)malloc(len); + buf = len; + }else{ + if (buf < len){ + data = (char*)realloc(data, len); + buf = len; + } + } + memcpy(data+16, S.metadata.getContentP("video")->getContentP("init")->StrValue().c_str(), len-20); + data[12] = 0;//H264 sequence header + data[13] = 0; + data[14] = 0; + data[15] = 0; + data[11] = 0x57;//H264 init data (0x07 & 0x50) + } + ((unsigned int *)(data+len-4))[0] = len-15; + switch (S.lastType()){ + case DTSC::VIDEO: data[0] = 0x09; break; + case DTSC::AUDIO: data[0] = 0x08; break; + case DTSC::META: data[0] = 0x12; break; + default: break; + } + data[1] = ((len-15) >> 16) & 0xFF; + data[2] = ((len-15) >> 8) & 0xFF; + data[3] = (len-15) & 0xFF; + tagTime(0); + return true; +} + +/// FLV Audio init data loader function from DTSC. +/// Takes the DTSC Audio init data and makes it into FLV. +/// Assumes init data is available - so check before calling! +bool FLV::Tag::DTSCAudioInit(DTSC::Stream & S){ + len = 0; + if (S.metadata.getContentP("audio")->getContentP("codec")->StrValue() == "AAC"){ + len = S.metadata.getContentP("audio")->getContentP("init")->StrValue().length() + 17; + } + if (len > 0){ + if (!data){ + data = (char*)malloc(len); + buf = len; + }else{ + if (buf < len){ + data = (char*)realloc(data, len); + buf = len; + } + } + memcpy(data+13, S.metadata.getContentP("audio")->getContentP("init")->StrValue().c_str(), len-17); + data[12] = 0;//AAC sequence header + data[11] = 0; + if (S.metadata.getContentP("audio")->getContentP("codec")->StrValue() == "AAC"){data[11] += 0xA0;} + if (S.metadata.getContentP("audio")->getContentP("codec")->StrValue() == "MP3"){data[11] += 0x20;} + if (S.metadata.getContentP("audio")->getContentP("rate")->NumValue() == 11000){data[11] += 0x04;} + if (S.metadata.getContentP("audio")->getContentP("rate")->NumValue() == 22000){data[11] += 0x08;} + if (S.metadata.getContentP("audio")->getContentP("rate")->NumValue() == 44000){data[11] += 0x0C;} + if (S.metadata.getContentP("audio")->getContentP("size")->NumValue() == 16){data[11] += 0x02;} + if (S.metadata.getContentP("audio")->getContentP("channels")->NumValue() > 1){data[11] += 0x01;} + } + ((unsigned int *)(data+len-4))[0] = len-15; + switch (S.lastType()){ + case DTSC::VIDEO: data[0] = 0x09; break; + case DTSC::AUDIO: data[0] = 0x08; break; + case DTSC::META: data[0] = 0x12; break; + default: break; + } + data[1] = ((len-15) >> 16) & 0xFF; + data[2] = ((len-15) >> 8) & 0xFF; + data[3] = (len-15) & 0xFF; + tagTime(0); + return true; +} + +/// FLV metadata loader function from DTSC. +/// Takes the DTSC metadata and makes it into FLV. +/// Assumes metadata is available - so check before calling! +bool FLV::Tag::DTSCMetaInit(DTSC::Stream & S){ + AMF::Object amfdata("root", AMF::AMF0_DDV_CONTAINER); + + amfdata.addContent(AMF::Object("", "onMetaData")); + amfdata.addContent(AMF::Object("", AMF::AMF0_ECMA_ARRAY)); + if (S.metadata.getContentP("video")){ + amfdata.getContentP(1)->addContent(AMF::Object("hasVideo", 1, AMF::AMF0_BOOL)); + if (S.metadata.getContentP("video")->getContentP("codec")->StrValue() == "H264"){ + amfdata.getContentP(1)->addContent(AMF::Object("videocodecid", 7, AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("video")->getContentP("codec")->StrValue() == "VP6"){ + amfdata.getContentP(1)->addContent(AMF::Object("videocodecid", 4, AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("video")->getContentP("codec")->StrValue() == "H263"){ + amfdata.getContentP(1)->addContent(AMF::Object("videocodecid", 2, AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("video")->getContentP("width")){ + amfdata.getContentP(1)->addContent(AMF::Object("width", S.metadata.getContentP("video")->getContentP("width")->NumValue(), AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("video")->getContentP("height")){ + amfdata.getContentP(1)->addContent(AMF::Object("height", S.metadata.getContentP("video")->getContentP("height")->NumValue(), AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("video")->getContentP("fpks")){ + amfdata.getContentP(1)->addContent(AMF::Object("framerate", (double)S.metadata.getContentP("video")->getContentP("fpks")->NumValue() / 1000.0, AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("video")->getContentP("bps")){ + amfdata.getContentP(1)->addContent(AMF::Object("videodatarate", ((double)S.metadata.getContentP("video")->getContentP("bps")->NumValue() * 8.0) / 1024.0, AMF::AMF0_NUMBER)); + } + } + if (S.metadata.getContentP("audio")){ + amfdata.getContentP(1)->addContent(AMF::Object("hasAudio", 1, AMF::AMF0_BOOL)); + amfdata.getContentP(1)->addContent(AMF::Object("audiodelay", 0, AMF::AMF0_NUMBER)); + if (S.metadata.getContentP("audio")->getContentP("codec")->StrValue() == "AAC"){ + amfdata.getContentP(1)->addContent(AMF::Object("audiocodecid", 10, AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("audio")->getContentP("codec")->StrValue() == "MP3"){ + amfdata.getContentP(1)->addContent(AMF::Object("audiocodecid", 2, AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("audio")->getContentP("channels")){ + if (S.metadata.getContentP("audio")->getContentP("channels")->NumValue() > 1){ + amfdata.getContentP(1)->addContent(AMF::Object("stereo", 1, AMF::AMF0_BOOL)); + }else{ + amfdata.getContentP(1)->addContent(AMF::Object("stereo", 0, AMF::AMF0_BOOL)); + } + } + if (S.metadata.getContentP("audio")->getContentP("rate")){ + amfdata.getContentP(1)->addContent(AMF::Object("audiosamplerate", S.metadata.getContentP("audio")->getContentP("rate")->NumValue(), AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("audio")->getContentP("size")){ + amfdata.getContentP(1)->addContent(AMF::Object("audiosamplesize", S.metadata.getContentP("audio")->getContentP("size")->NumValue(), AMF::AMF0_NUMBER)); + } + if (S.metadata.getContentP("audio")->getContentP("bps")){ + amfdata.getContentP(1)->addContent(AMF::Object("audiodatarate", ((double)S.metadata.getContentP("audio")->getContentP("bps")->NumValue() * 8.0) / 1024.0, AMF::AMF0_NUMBER)); + } + } + + std::string tmp = amfdata.Pack(); + len = tmp.length() + 15; + if (len > 0){ + if (!data){ + data = (char*)malloc(len); + buf = len; + }else{ + if (buf < len){ + data = (char*)realloc(data, len); + buf = len; + } + } + memcpy(data+11, tmp.c_str(), len-15); + } + ((unsigned int *)(data+len-4))[0] = len-15; + data[0] = 0x12; + data[1] = ((len-15) >> 16) & 0xFF; + data[2] = ((len-15) >> 8) & 0xFF; + data[3] = (len-15) & 0xFF; + tagTime(0); + return true; +} + /// FLV loader function from chunk. /// Copies the contents and wraps it in a FLV header. bool FLV::Tag::ChunkLoader(const RTMPStream::Chunk& O){ diff --git a/util/flv_tag.h b/util/flv_tag.h index 1350c870..d349d533 100644 --- a/util/flv_tag.h +++ b/util/flv_tag.h @@ -3,6 +3,7 @@ #pragma once #include "socket.h" +#include "dtsc.h" #include //forward declaration of RTMPStream::Chunk to avoid circular dependencies. @@ -38,6 +39,10 @@ namespace FLV { Tag(const RTMPStream::Chunk& O); /// Date: Thu, 15 Sep 2011 18:48:57 +0200 Subject: [PATCH 5/7] Attempt to fix FLV --- util/flv_tag.cpp | 33 +++++++++++++++++++++------------ util/flv_tag.h | 1 + 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/util/flv_tag.cpp b/util/flv_tag.cpp index 3af96eea..f0fb956f 100644 --- a/util/flv_tag.cpp +++ b/util/flv_tag.cpp @@ -319,7 +319,7 @@ bool FLV::Tag::DTSCLoader(DTSC::Stream & S){ default: break; } } - ((unsigned int *)(data+len-4))[0] = len-15; + setLen(); switch (S.lastType()){ case DTSC::VIDEO: data[0] = 0x09; break; case DTSC::AUDIO: data[0] = 0x08; break; @@ -333,6 +333,19 @@ bool FLV::Tag::DTSCLoader(DTSC::Stream & S){ return true; } +/// Helper function that properly sets the tag length from the internal len variable. +void FLV::Tag::setLen(){ + int len4 = len - 4; + int i = data+len-1; + data[--i] = (len4) & 0xFF; + len4 >>= 8; + data[--i] = (len4) & 0xFF; + len4 >>= 8; + data[--i] = (len4) & 0xFF; + len4 >>= 8; + data[--i] = (len4) & 0xFF; +} + /// FLV Video init data loader function from DTSC. /// Takes the DTSC Video init data and makes it into FLV. /// Assumes init data is available - so check before calling! @@ -355,15 +368,10 @@ bool FLV::Tag::DTSCVideoInit(DTSC::Stream & S){ data[13] = 0; data[14] = 0; data[15] = 0; - data[11] = 0x57;//H264 init data (0x07 & 0x50) - } - ((unsigned int *)(data+len-4))[0] = len-15; - switch (S.lastType()){ - case DTSC::VIDEO: data[0] = 0x09; break; - case DTSC::AUDIO: data[0] = 0x08; break; - case DTSC::META: data[0] = 0x12; break; - default: break; + data[11] = 0x17;//H264 keyframe (0x07 & 0x10) } + setLen(); + data[0] = 0x09; data[1] = ((len-15) >> 16) & 0xFF; data[2] = ((len-15) >> 8) & 0xFF; data[3] = (len-15) & 0xFF; @@ -400,13 +408,14 @@ bool FLV::Tag::DTSCAudioInit(DTSC::Stream & S){ if (S.metadata.getContentP("audio")->getContentP("size")->NumValue() == 16){data[11] += 0x02;} if (S.metadata.getContentP("audio")->getContentP("channels")->NumValue() > 1){data[11] += 0x01;} } - ((unsigned int *)(data+len-4))[0] = len-15; + setLen(); switch (S.lastType()){ case DTSC::VIDEO: data[0] = 0x09; break; case DTSC::AUDIO: data[0] = 0x08; break; case DTSC::META: data[0] = 0x12; break; default: break; } + data[0] = 0x08; data[1] = ((len-15) >> 16) & 0xFF; data[2] = ((len-15) >> 8) & 0xFF; data[3] = (len-15) & 0xFF; @@ -487,7 +496,7 @@ bool FLV::Tag::DTSCMetaInit(DTSC::Stream & S){ } memcpy(data+11, tmp.c_str(), len-15); } - ((unsigned int *)(data+len-4))[0] = len-15; + setLen(); data[0] = 0x12; data[1] = ((len-15) >> 16) & 0xFF; data[2] = ((len-15) >> 8) & 0xFF; @@ -512,7 +521,7 @@ bool FLV::Tag::ChunkLoader(const RTMPStream::Chunk& O){ } memcpy(data+11, &(O.data[0]), O.len); } - ((unsigned int *)(data+len-4))[0] = O.len; + setLen(); data[0] = O.msg_type_id; data[3] = O.len & 0xFF; data[2] = (O.len >> 8) & 0xFF; diff --git a/util/flv_tag.h b/util/flv_tag.h index d349d533..6f1f7da7 100644 --- a/util/flv_tag.h +++ b/util/flv_tag.h @@ -51,6 +51,7 @@ namespace FLV { int buf; ///< Maximum length of buffer space. bool done; ///< Body reading done? unsigned int sofar; ///< How many bytes are read sofar? + void setLen(); //loader helper functions bool MemReadUntil(char * buffer, unsigned int count, unsigned int & sofar, char * D, unsigned int S, unsigned int & P); bool SockReadUntil(char * buffer, unsigned int count, unsigned int & sofar, Socket::Connection & sock); From 53c6d582ba12e6db6cd5cfc15573d68cb59ecd77 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 5 Dec 2011 15:35:10 +0100 Subject: [PATCH 6/7] Fix DTSC tools compiling --- util/flv_tag.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/flv_tag.cpp b/util/flv_tag.cpp index f0fb956f..c5b4b964 100644 --- a/util/flv_tag.cpp +++ b/util/flv_tag.cpp @@ -336,7 +336,7 @@ bool FLV::Tag::DTSCLoader(DTSC::Stream & S){ /// Helper function that properly sets the tag length from the internal len variable. void FLV::Tag::setLen(){ int len4 = len - 4; - int i = data+len-1; + int i = len-1; data[--i] = (len4) & 0xFF; len4 >>= 8; data[--i] = (len4) & 0xFF; From 4c3ce2d837865ffd6b45c7c60fdee9379c70c73d Mon Sep 17 00:00:00 2001 From: Thulinma Date: Fri, 2 Mar 2012 17:48:20 +0100 Subject: [PATCH 7/7] Fixed DTSC info comment. --- util/dtsc.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util/dtsc.h b/util/dtsc.h index 30bbc092..3d690d53 100644 --- a/util/dtsc.h +++ b/util/dtsc.h @@ -13,7 +13,7 @@ /// Holds all DDVTECH Stream Container classes and parsers. ///Video: -/// - codec (string: AAC, MP3) +/// - codec (string: H264, H263, VP6) /// - width (int, pixels) /// - height (int, pixels) /// - fpks (int, frames per kilosecond (FPS * 1000)) @@ -21,7 +21,7 @@ /// - init (string, init data) /// ///Audio: -/// - codec (string: H264, H263, VP6) +/// - codec (string: AAC, MP3) /// - rate (int, Hz) /// - size (int, bitsize) /// - bps (int, bytes per second)