new recursive AMF parser, new and improved AMF class

This commit is contained in:
Thulinma 2010-07-29 17:29:53 +02:00
parent 3a3bd060be
commit 4f0f71716f
3 changed files with 202 additions and 141 deletions

View file

@ -19,6 +19,6 @@ clean:
run-test: $(OUT) run-test: $(OUT)
rm -rf ./meh rm -rf ./meh
mkfifo ./meh mkfifo ./meh
nc -o test -l -p 1935 -e './Connector_RTMP 2>./meh' nc -l -p 1935 -e './Connector_RTMP 2>./meh'
run-cat: run-cat:
cat < ./meh cat < ./meh

View file

@ -4,148 +4,209 @@
class AMFType { class AMFType {
public: public:
std::string Indice(){return myIndice;};
unsigned char GetType(){return myType;};
double NumValue(){return numval;}; double NumValue(){return numval;};
std::string StrValue(){return strval;}; std::string StrValue(){return strval;};
AMFType(double val){strval = ""; numval = val;}; const char * Str(){return strval.c_str();};
AMFType(std::string val){strval = val; numval = 0;}; int hasContent(){
private: if (!contents){return 0;}
return contents->size();
};
void addContent(AMFType c){if (contents != 0){contents->push_back(c);}};
AMFType* getContentP(int i){if (contents != 0){return &contents->at(i);}else{return 0;}};
AMFType getContent(int i){if (contents != 0){return contents->at(i);}else{return AMFType("error");}};
AMFType* getContentP(std::string s){
if (contents != 0){
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){
if (it->Indice() == s){
return &(*it);
}
}
}
return this;
};
AMFType getContent(std::string s){
if (contents != 0){
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){
if (it->Indice() == s){
return *it;
}
}
}
return AMFType("error");
};
AMFType(std::string indice, double val, unsigned char setType = 0x00){//num type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = val;
contents = 0;
};
AMFType(std::string indice, std::string val, unsigned char setType = 0x02){//str type initializer
myIndice = indice;
myType = setType;
strval = val;
numval = 0;
contents = 0;
};
AMFType(std::string indice, unsigned char setType = 0x03){//object type initializer
myIndice = indice;
myType = setType;
strval = "";
numval = 0;
contents = new std::vector<AMFType>;
};
~AMFType(){if (contents != 0){delete contents;contents=0;}};
AMFType& operator=(const AMFType &a) {
myIndice = a.myIndice;
myType = a.myType;
strval = a.strval;
numval = a.numval;
if (contents){
if (a.contents != contents){
delete contents;
if (a.contents){
contents = new std::vector<AMFType>;
for (std::vector<AMFType>::iterator it = a.contents->begin(); it < a.contents->end(); it++){
contents->push_back(*it);
}
}else{
contents = 0;
}
}
}else{
if (a.contents){
contents = new std::vector<AMFType>;
for (std::vector<AMFType>::iterator it = a.contents->begin(); it < a.contents->end(); it++){
contents->push_back(*it);
}
}
}
return *this;
};
AMFType(const AMFType &a){
myIndice = a.myIndice;
myType = a.myType;
strval = a.strval;
numval = a.numval;
if (a.contents){
contents = new std::vector<AMFType>;
for (std::vector<AMFType>::iterator it = a.contents->begin(); it < a.contents->end(); it++){
contents->push_back(*it);
}
}else{contents = 0;}
};
void Print(std::string indent = ""){
std::cerr << indent;
switch (myType){
case 0x00: std::cerr << "Number"; break;
case 0x01: std::cerr << "Bool"; break;
case 0x02://short string
case 0x0C: std::cerr << "String"; break;
case 0x03: std::cerr << "Object"; break;
case 0x05: std::cerr << "Null"; break;
case 0x06: std::cerr << "Undefined"; break;
case 0x0D: std::cerr << "Unsupported"; break;
case 0xFF: std::cerr << "Container"; break;
}
std::cerr << " " << myIndice << " ";
switch (myType){
case 0x00: case 0x01: std::cerr << numval; break;
case 0x02: case 0x0C: std::cerr << strval; break;
}
std::cerr << std::endl;
if (contents){
for (std::vector<AMFType>::iterator it = contents->begin(); it != contents->end(); it++){it->Print(indent+" ");}
}
};
protected:
std::string myIndice;
unsigned char myType;
std::string strval; std::string strval;
double numval; double numval;
std::vector<AMFType> * contents;
};//AMFType };//AMFType
//scans the vector for the indice, returns the next AMFType from it or null AMFType parseOneAMF(unsigned char *& data, unsigned int &len, unsigned int &i, std::string name){
AMFType * getAMF(std::vector<AMFType> * vect, std::string indice){
std::vector<AMFType>::iterator it;
for (it=vect->begin(); it < vect->end(); it++){
if ((*it).StrValue() == indice){it++; return &(*it);}
}
return 0;
}//getAMF
std::vector<AMFType> * parseAMF(unsigned char * data, unsigned int len){
std::vector<AMFType> * ret = new std::vector<AMFType>;
unsigned int i = 0;
std::string tmpstr; std::string tmpstr;
unsigned int tmpi = 0; unsigned int tmpi = 0;
unsigned char tmpdbl[8]; unsigned char tmpdbl[8];
while (i < len){ switch (data[i]){
switch (data[i]){ case 0x00://number
case 0x00://number tmpdbl[7] = data[i+1];
tmpdbl[7] = data[i+1]; tmpdbl[6] = data[i+2];
tmpdbl[6] = data[i+2]; tmpdbl[5] = data[i+3];
tmpdbl[5] = data[i+3]; tmpdbl[4] = data[i+4];
tmpdbl[4] = data[i+4]; tmpdbl[3] = data[i+5];
tmpdbl[3] = data[i+5]; tmpdbl[2] = data[i+6];
tmpdbl[2] = data[i+6]; tmpdbl[1] = data[i+7];
tmpdbl[1] = data[i+7]; tmpdbl[0] = data[i+8];
tmpdbl[0] = data[i+8]; i+=9;
ret->push_back(*(double*)tmpdbl); fprintf(stderr, "AMF: Number %f\n", *(double*)tmpdbl);
fprintf(stderr, "AMF: Number %f\n", *(double*)tmpdbl); return AMFType(name, *(double*)tmpdbl, 0x00);
i += 8; break;
break; case 0x01://bool
case 0x01://bool i+=2;
if (data[i+1] == 0){ if (data[i-1] == 0){
ret->push_back((double)0); fprintf(stderr, "AMF: Bool false\n");
fprintf(stderr, "AMF: Bool false\n"); return AMFType(name, (double)0, 0x01);
}else{ }else{
ret->push_back((double)1); fprintf(stderr, "AMF: Bool true\n");
fprintf(stderr, "AMF: Bool true\n"); return AMFType(name, (double)1, 0x01);
} }
++i; break;
break; case 0x0C://long string
case 0x0C://long string tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];
tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4]; tmpstr = (char*)(data+i+5);
tmpstr = (char*)(data+i+5); i += tmpi + 5;
ret->push_back(tmpstr); fprintf(stderr, "AMF: String %s\n", tmpstr.c_str());
i += tmpi + 4; return AMFType(name, tmpstr, 0x0C);
fprintf(stderr, "AMF: String %s\n", tmpstr.c_str()); break;
break; case 0x02://string
case 0x02://string tmpi = data[i+1]*256+data[i+2];
tmpi = data[i+1]*256+data[i+2]; tmpstr = (char*)(data+i+3);
tmpstr = (char*)(data+i+3); i += tmpi + 3;
ret->push_back(tmpstr); fprintf(stderr, "AMF: String %s\n", tmpstr.c_str());
return AMFType(name, tmpstr, 0x02);
break;
case 0x05://null
case 0x06://undefined
case 0x0D://unsupported
fprintf(stderr, "AMF: Null\n");
++i;
return AMFType(name, (double)0, data[i-1]);
break;
case 0x03:{//object
++i;
AMFType ret = AMFType(name, data[i-1]);
while (data[i] + data[i+1] != 0){
tmpi = data[i]*256+data[i+1];
tmpstr = (char*)(data+i+2);
i += tmpi + 2; i += tmpi + 2;
fprintf(stderr, "AMF: String %s\n", tmpstr.c_str()); fprintf(stderr, "AMF: Indice %s\n", tmpstr.c_str());
break; ret.addContent(parseOneAMF(data, len, i, tmpstr));
case 0x05://null }
case 0x06://undefined i += 3;
case 0x0D://unsupported return ret;
fprintf(stderr, "AMF: Null\n"); } break;
ret->push_back((double)0); case 0x07://reference
break; case 0x08://array
case 0x03://object case 0x0A://strict array
++i; case 0x0B://date
while (data[i] + data[i+1] != 0){ case 0x0F://XML
tmpi = data[i]*256+data[i+1]; case 0x10://typed object
tmpstr = (char*)(data+i+2); case 0x11://AMF+
ret->push_back(tmpstr); default:
i += tmpi + 2; fprintf(stderr, "Error: Unimplemented AMF type %hhx - returning.\n", data[i]);
fprintf(stderr, "AMF: Indice %s\n", tmpstr.c_str()); return AMFType("error", (unsigned char)0xFF);
switch (data[i]){ break;
case 0x00://number
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];
ret->push_back(*(double*)tmpdbl);
fprintf(stderr, "AMF: Value Number %f\n", *(double*)tmpdbl);
i += 8;
break;
case 0x01://bool
if (data[i+1] == 0){
ret->push_back((double)0);
fprintf(stderr, "AMF: Value Bool false\n");
}else{
ret->push_back((double)1);
fprintf(stderr, "AMF: Value Bool true\n");
}
++i;
break;
case 0x0C://long string
tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];
tmpstr = (char*)(data+i+5);
ret->push_back(tmpstr);
i += tmpi + 4;
fprintf(stderr, "AMF: Value String %s\n", tmpstr.c_str());
break;
case 0x02://string
tmpi = data[i+1]*256+data[i+2];
tmpstr = (char*)(data+i+3);
ret->push_back(tmpstr);
i += tmpi + 2;
fprintf(stderr, "AMF: Value String %s\n", tmpstr.c_str());
break;
case 0x05://null
case 0x06://undefined
case 0x0D://unsupported
fprintf(stderr, "AMF: Value Null\n");
ret->push_back((double)0);
break;
default:
fprintf(stderr, "Error: Unknown AMF object contents type %hhx - returning.\n", data[i]);
break;
}
++i;
}
i += 2;
break;
case 0x07://reference
case 0x08://array
case 0x0A://strict array
case 0x0B://date
case 0x0F://XML
case 0x10://typed object
case 0x11://AMF+
default:
fprintf(stderr, "Error: Unknown AMF type %hhx - returning.\n", data[i]);
return ret;
break;
}
++i;
} }
}//parseOneAMF
AMFType parseAMF(unsigned char * data, unsigned int len){
AMFType ret("returned", (unsigned char)0xFF);//container type
unsigned int i = 0;
while (i < len){ret.addContent(parseOneAMF(data, len, i, ""));}
return ret; return ret;
}//parseAMF }//parseAMF

View file

@ -5,8 +5,8 @@
//gets and parses one chunk //gets and parses one chunk
void parseChunk(){ void parseChunk(){
static chunkpack next; static chunkpack next;
static std::vector<AMFType> * amfdata = 0; static AMFType amfdata("empty", (unsigned char)0xFF);
static AMFType * amfelem = 0; static AMFType amfelem("empty", (unsigned char)0xFF);
static int tmpint; static int tmpint;
next = getWholeChunk(); next = getWholeChunk();
if (next.cs_id == 2 && next.msg_stream_id == 0){ if (next.cs_id == 2 && next.msg_stream_id == 0){
@ -67,14 +67,14 @@ void parseChunk(){
fprintf(stderr, "Received AFM0 shared object\n"); fprintf(stderr, "Received AFM0 shared object\n");
break; break;
case 20: case 20:
if (amfdata != 0){delete amfdata;}
amfdata = parseAMF(next.data, next.real_len); amfdata = parseAMF(next.data, next.real_len);
fprintf(stderr, "Received AFM0 command message: %s\n", (*amfdata)[0].StrValue().c_str()); amfdata.Print();
if ((*amfdata)[0].StrValue() == "connect"){ fprintf(stderr, "Received AFM0 command message: %s\n", amfdata.getContentP(0)->Str());
tmpint = getAMF(amfdata, "videoCodecs")->NumValue(); if (amfdata.getContentP(0)->StrValue() == "connect"){
tmpint = amfdata.getContentP(2)->getContentP("videoCodecs")->NumValue();
if (tmpint & 0x04){fprintf(stderr, "Sorensen video support detected\n");} if (tmpint & 0x04){fprintf(stderr, "Sorensen video support detected\n");}
if (tmpint & 0x80){fprintf(stderr, "H264 video support detected\n");} if (tmpint & 0x80){fprintf(stderr, "H264 video support detected\n");}
tmpint = getAMF(amfdata, "audioCodecs")->NumValue(); tmpint = amfdata.getContentP(2)->getContentP("audioCodecs")->NumValue();
if (tmpint & 0x04){fprintf(stderr, "MP3 audio support detected\n");} if (tmpint & 0x04){fprintf(stderr, "MP3 audio support detected\n");}
if (tmpint & 0x400){fprintf(stderr, "AAC video support detected\n");} if (tmpint & 0x400){fprintf(stderr, "AAC video support detected\n");}
SendCTL(5, snd_window_size);//send window acknowledgement size (msg 5) SendCTL(5, snd_window_size);//send window acknowledgement size (msg 5)
@ -84,7 +84,7 @@ void parseChunk(){
}else{ }else{
//call, close, createStream //call, close, createStream
//TODO: play (&& play2?) //TODO: play (&& play2?)
fprintf(stderr, "Ignored AFM0 command.\n"); //fprintf(stderr, "Ignored AFM0 command.\n");
} }
break; break;
case 22: case 22: