From fee658cf1d5e388ee8d9f12e173978120dd27410 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 12 Sep 2011 16:48:18 +0200 Subject: [PATCH] First version DTSC support (still untested), fixed /TODO to /todo in RTMP main.cpp --- util/dtmi.cpp | 248 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/dtmi.h | 58 ++++++++++++ util/dtsc.cpp | 58 ++++++++++++ util/dtsc.h | 47 ++++++++++ 4 files changed, 411 insertions(+) create mode 100644 util/dtmi.cpp create mode 100644 util/dtmi.h create mode 100644 util/dtsc.cpp create mode 100644 util/dtsc.h diff --git a/util/dtmi.cpp b/util/dtmi.cpp new file mode 100644 index 00000000..a9d38299 --- /dev/null +++ b/util/dtmi.cpp @@ -0,0 +1,248 @@ +/// \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 new file mode 100644 index 00000000..410df97d --- /dev/null +++ b/util/dtmi.h @@ -0,0 +1,58 @@ +/// \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 new file mode 100644 index 00000000..bc722ecf --- /dev/null +++ b/util/dtsc.cpp @@ -0,0 +1,58 @@ +/// \file dtsc.cpp +/// Holds all code for DDVTECH Stream Container parsing/generation. + +#include "dtsc.h" +#include "string.h" //for memcmp +#include "arpa/inet.h" //for htonl/ntohl + +char * DTSC::Magic_Header = "DTSC"; +char * DTSC::Magic_Packet = "DTPD"; + +DTSC::Stream::Stream(){ + datapointer = 0; +} + +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); + } + 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); + 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 (tmp == "video"){datapointertype = VIDEO;} + if (tmp == "audio"){datapointertype = AUDIO;} + if (tmp == "meta"){datapointertype = META;} + } + }else{ + datapointer = 0; + } + } + } + return false; +} + +char * DTSC::Stream::lastData(){ + return datapointer; +} + +DTSC::datatype DTSC::Stream::lastType(){ + return datapointertype; +} + +bool DTSC::Stream::hasVideo(){ + return (metadata.getContentP("video") != 0); +} + +bool DTSC::Stream::hasAudio(){ + return (metadata.getContentP("audio") != 0); +} diff --git a/util/dtsc.h b/util/dtsc.h new file mode 100644 index 00000000..6b918f30 --- /dev/null +++ b/util/dtsc.h @@ -0,0 +1,47 @@ +/// \file dtsc.h +/// Holds all headers for DDVTECH Stream Container parsing/generation. + +#pragma once +#include "dtmi.h" + +// Video: +// Codec (string) + +// Audio: +// Codec (string) +// Samping rate (int, Hz) +// Sample Size (int, bytesize) +// Channels (int, channelcount) + +namespace DTSC{ + + /// 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 + + /// Holds temporary data for a DTSC stream and provides functions to access/store it. + class Stream { + public: + Stream(); + DTSC::DTMI metadata; + DRSC::DTMI lastPacket; + datatype lastType(); + char * lastData(); + bool hasVideo(); + bool hasAudio(); + bool parsePacket(std::string & buffer); + private: + char * datapointer; + datatype datapointertype; + } + + + +};