diff --git a/Connector_RTMP/amf.cpp b/Connector_RTMP/amf.cpp index 794ae577..7feed259 100644 --- a/Connector_RTMP/amf.cpp +++ b/Connector_RTMP/amf.cpp @@ -2,6 +2,26 @@ #include #include +#define AMF0_NUMBER 0x00 +#define AMF0_BOOL 0x01 +#define AMF0_STRING 0x02 +#define AMF0_OBJECT 0x03 +#define AMF0_MOVIECLIP 0x04 +#define AMF0_NULL 0x05 +#define AMF0_UNDEFINED 0x06 +#define AMF0_REFERENCE 0x07 +#define AMF0_ECMA_ARRAY 0x08 +#define AMF0_OBJ_END 0x09 +#define AMF0_STRICT_ARRAY 0x0A +#define AMF0_DATE 0x0B +#define AMF0_LONGSTRING 0x0C +#define AMF0_UNSUPPORTED 0x0D +#define AMF0_RECORDSET 0x0E +#define AMF0_XMLDOC 0x0F +#define AMF0_TYPED_OBJ 0x10 +#define AMF0_UPGRADE 0x11 +#define AMF0_DDV_CONTAINER 0xFF + class AMFType { public: std::string Indice(){return myIndice;}; @@ -36,21 +56,21 @@ class AMFType { } return AMFType("error"); }; - AMFType(std::string indice, double val, unsigned char setType = 0x00){//num type initializer + AMFType(std::string indice, double val, unsigned char setType = AMF0_NUMBER){//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 + AMFType(std::string indice, std::string val, unsigned char setType = AMF0_STRING){//str type initializer myIndice = indice; myType = setType; strval = val; numval = 0; contents = 0; }; - AMFType(std::string indice, unsigned char setType = 0x03){//object type initializer + AMFType(std::string indice, unsigned char setType = AMF0_OBJECT){//object type initializer myIndice = indice; myType = setType; strval = ""; @@ -100,21 +120,30 @@ class AMFType { 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 0x08: std::cerr << "ECMA Array"; 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; + case AMF0_NUMBER: std::cerr << "Number"; break; + case AMF0_BOOL: std::cerr << "Bool"; break; + case AMF0_STRING://short string + case AMF0_LONGSTRING: std::cerr << "String"; break; + case AMF0_OBJECT: std::cerr << "Object"; break; + case AMF0_MOVIECLIP: std::cerr << "MovieClip"; break; + case AMF0_NULL: std::cerr << "Null"; break; + case AMF0_UNDEFINED: std::cerr << "Undefined"; break; + case AMF0_REFERENCE: std::cerr << "Reference"; break; + case AMF0_ECMA_ARRAY: std::cerr << "ECMA Array"; break; + case AMF0_OBJ_END: std::cerr << "Object end"; break; + case AMF0_STRICT_ARRAY: std::cerr << "Strict Array"; break; + case AMF0_DATE: std::cerr << "Date"; break; + case AMF0_UNSUPPORTED: std::cerr << "Unsupported"; break; + case AMF0_RECORDSET: std::cerr << "Recordset"; break; + case AMF0_XMLDOC: std::cerr << "XML Document"; break; + case AMF0_TYPED_OBJ: std::cerr << "Typed Object"; break; + case AMF0_UPGRADE: std::cerr << "Upgrade to AMF3"; break; + case AMF0_DDV_CONTAINER: std::cerr << "DDVTech Container"; break; } std::cerr << " " << myIndice << " "; switch (myType){ - case 0x00: case 0x01: std::cerr << numval; break; - case 0x02: case 0x0C: std::cerr << strval; break; + case AMF0_NUMBER: case AMF0_BOOL: case AMF0_REFERENCE: case AMF0_DATE: std::cerr << numval; break; + case AMF0_STRING: case AMF0_LONGSTRING: case AMF0_XMLDOC: case AMF0_TYPED_OBJ: std::cerr << strval; break; } std::cerr << std::endl; if (contents){ @@ -123,31 +152,45 @@ class AMFType { };//print std::string Pack(){ std::string r = ""; - if ((myType == 0x02) && (strval.size() > 0xFFFF)){myType = 0x0C;} - if (myType != 0xFF){r += myType;} + if ((myType == AMF0_STRING) && (strval.size() > 0xFFFF)){myType = AMF0_LONGSTRING;} + if (myType != AMF0_DDV_CONTAINER){r += myType;} switch (myType){ - case 0x00://number + case AMF0_NUMBER: 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 0x01://bool + case AMF0_DATE: + 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)); + r += (char)0;//timezone always 0 + r += (char)0;//timezone always 0 + break; + case AMF0_BOOL: r += (char)numval; break; - case 0x02://short string + case AMF0_STRING: r += strval.size() / 256; r += strval.size() % 256; r += strval; break; - case 0x0C://long string + case AMF0_LONGSTRING: + case AMF0_XMLDOC://is always a longstring r += strval.size() / (256*256*256); r += strval.size() / (256*256); r += strval.size() / 256; r += strval.size() % 256; r += strval; break; - case 0x03://object + case AMF0_TYPED_OBJ: + r += Indice().size() / 256; + r += Indice().size() % 256; + r += Indice(); + //is an object, with the classname first + case AMF0_OBJECT: if (contents){ for (std::vector::iterator it = contents->begin(); it != contents->end(); it++){ r += it->Indice().size() / 256; @@ -158,7 +201,18 @@ class AMFType { } r += (char)0; r += (char)0; r += (char)9; break; - case 0x08:{//array + case AMF0_MOVIECLIP: + case AMF0_NULL: + case AMF0_UNDEFINED: + case AMF0_RECORDSET: + case AMF0_UNSUPPORTED: + //no data to add + break; + case AMF0_REFERENCE: + r += (char)((int)numval / 256); + r += (char)((int)numval % 256); + break; + case AMF0_ECMA_ARRAY:{ int arrlen = 0; if (contents){ arrlen = getContentP("length")->NumValue(); @@ -174,7 +228,19 @@ class AMFType { } r += (char)0; r += (char)0; r += (char)9; } break; - case 0xFF://container - our own type - do not send, only send contents + case AMF0_STRICT_ARRAY:{ + int arrlen = 0; + if (contents){ + arrlen = getContentP("length")->NumValue(); + r += arrlen / (256*256*256); r += arrlen / (256*256); r += arrlen / 256; r += arrlen % 256; + for (std::vector::iterator it = contents->begin(); it != contents->end(); it++){ + r += it->Pack(); + } + }else{ + r += (char)0; r += (char)0; r += (char)0; r += (char)0; + } + } break; + case AMF0_DDV_CONTAINER://only send contents if (contents){ for (std::vector::iterator it = contents->begin(); it != contents->end(); it++){ r += it->Pack(); @@ -193,12 +259,11 @@ class AMFType { };//AMFType AMFType parseOneAMF(const unsigned char *& data, unsigned int &len, unsigned int &i, std::string name){ - char * helperchar = 0; std::string tmpstr; unsigned int tmpi = 0; unsigned char tmpdbl[8]; switch (data[i]){ - case 0x00://number + case AMF0_NUMBER: tmpdbl[7] = data[i+1]; tmpdbl[6] = data[i+2]; tmpdbl[5] = data[i+3]; @@ -207,66 +272,112 @@ AMFType parseOneAMF(const unsigned char *& data, unsigned int &len, unsigned int tmpdbl[2] = data[i+6]; tmpdbl[1] = data[i+7]; tmpdbl[0] = data[i+8]; - i+=9; - return AMFType(name, *(double*)tmpdbl, 0x00); + i+=9;//skip 8(a double)+1 forwards + return AMFType(name, *(double*)tmpdbl, AMF0_NUMBER); break; - case 0x01://bool - i+=2; + case AMF0_DATE: + 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+=11;//skip 8(a double)+1+timezone(2) forwards + return AMFType(name, *(double*)tmpdbl, AMF0_DATE); + break; + case AMF0_BOOL: + i+=2;//skip bool+1 forwards if (data[i-1] == 0){ - return AMFType(name, (double)0, 0x01); + return AMFType(name, (double)0, AMF0_BOOL); }else{ - return AMFType(name, (double)1, 0x01); + return AMFType(name, (double)1, AMF0_BOOL); } break; - case 0x0C://long string - tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4]; - helperchar = (char*)malloc(tmpi+1); - memcpy(helperchar, data+i+5, tmpi); - helperchar[tmpi] = 0; - tmpstr = helperchar; - free(helperchar); - i += tmpi + 5; - return AMFType(name, tmpstr, 0x0C); + case AMF0_REFERENCE: + tmpi = data[i+1]*256+data[i+2];//get the ref number value as a double + i+=3;//skip ref+1 forwards + return AMFType(name, (double)tmpi, AMF0_REFERENCE); break; - case 0x02://string - tmpi = data[i+1]*256+data[i+2]; - helperchar = (char*)malloc(tmpi+1); - memcpy(helperchar, data+i+3, tmpi); - helperchar[tmpi] = 0; - tmpstr = helperchar; - free(helperchar); - i += tmpi + 3; - return AMFType(name, tmpstr, 0x02); + case AMF0_XMLDOC: + 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 AMFType(name, tmpstr, AMF0_XMLDOC); break; - case 0x05://null - case 0x06://undefined - case 0x0D://unsupported + case AMF0_LONGSTRING: + 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 AMFType(name, tmpstr, AMF0_LONGSTRING); + break; + case AMF0_STRING: + tmpi = data[i+1]*256+data[i+2];//set tmpi to UTF-8 length + tmpstr.clear();//clean tmpstr, just to be sure + tmpstr.append((const char *)data+i+3, (size_t)tmpi);//add the string data + i += tmpi + 3;//skip length+size+1 forwards + return AMFType(name, tmpstr, AMF0_STRING); + break; + case AMF0_NULL: + case AMF0_UNDEFINED: + case AMF0_UNSUPPORTED: ++i; return AMFType(name, (double)0, data[i-1]); break; - case 0x03:{//object + case AMF0_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; - ret.addContent(parseOneAMF(data, len, i, tmpstr)); + AMFType ret = AMFType(name, (unsigned char)AMF0_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(parseOneAMF(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr } - i += 3; + i += 3;//skip 0x000009 return ret; } break; - case 0x08:{//ECMA array + case AMF0_TYPED_OBJ:{ ++i; - AMFType ret = AMFType(name, data[i-1]); - i += 4; - while (data[i] + data[i+1] != 0){ - tmpi = data[i]*256+data[i+1]; - tmpstr = (char*)(data+i+2); - i += tmpi + 2; - ret.addContent(parseOneAMF(data, len, i, tmpstr)); + 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 + AMFType ret = AMFType(tmpstr, (unsigned char)AMF0_TYPED_OBJ);//the object is not named "name" but tmpstr + 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(parseOneAMF(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr + } + i += 3;//skip 0x000009 + return ret; + } break; + case AMF0_ECMA_ARRAY:{ + ++i; + AMFType ret = AMFType(name, (unsigned char)AMF0_ECMA_ARRAY); + i += 4;//ignore the array length, we re-calculate it + 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(parseOneAMF(data, len, i, tmpstr));//add content, recursively parsed, updating i, setting indice to tmpstr + } + i += 3;//skip 0x000009 + return ret; + } break; + case AMF0_STRICT_ARRAY:{ + AMFType ret = AMFType(name, (unsigned char)AMF0_STRICT_ARRAY); + tmpi = data[i+1]*256*256*256+data[i+2]*256*256+data[i+3]*256+data[i+4];//set tmpi to array length + i += 5;//skip size+1 forwards + while (tmpi > 0){//while not done parsing array + ret.addContent(parseOneAMF(data, len, i, "arrVal"));//add content, recursively parsed, updating i + --tmpi; } - i += 3; return ret; } break; } diff --git a/Connector_RTMP/parsechunks.cpp b/Connector_RTMP/parsechunks.cpp index 61a3612b..6b5fae0f 100644 --- a/Connector_RTMP/parsechunks.cpp +++ b/Connector_RTMP/parsechunks.cpp @@ -5,8 +5,8 @@ std::string streamname = "/tmp/shared_socket"; //gets and parses one chunk void parseChunk(){ static chunkpack next; - static AMFType amfdata("empty", (unsigned char)0xFF); - static AMFType amfelem("empty", (unsigned char)0xFF); + static AMFType amfdata("empty", (unsigned char)AMF0_DDV_CONTAINER); + static AMFType amfelem("empty", (unsigned char)AMF0_DDV_CONTAINER); next = getWholeChunk(); switch (next.msg_type_id){ case 0://does not exist @@ -117,7 +117,7 @@ void parseChunk(){ SendCTL(5, snd_window_size);//send window acknowledgement size (msg 5) SendUSR(0, 1);//send UCM StreamBegin (0), stream 1 //send a _result reply - AMFType amfreply("container", (unsigned char)0xFF); + AMFType amfreply("container", (unsigned char)AMF0_DDV_CONTAINER); amfreply.addContent(AMFType("", "_result"));//result success amfreply.addContent(amfdata.getContent(1));//same transaction ID // amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info @@ -144,10 +144,10 @@ void parseChunk(){ }//connect if (amfdata.getContentP(0)->StrValue() == "createStream"){ //send a _result reply - AMFType amfreply("container", (unsigned char)0xFF); + AMFType amfreply("container", (unsigned char)AMF0_DDV_CONTAINER); amfreply.addContent(AMFType("", "_result"));//result success amfreply.addContent(amfdata.getContent(1));//same transaction ID - amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info + amfreply.addContent(AMFType("", (double)0, AMF0_NULL));//null - command info amfreply.addContent(AMFType("", (double)1));//stream ID - we use 1 #if DEBUG >= 4 amfreply.Print(); @@ -158,10 +158,10 @@ void parseChunk(){ }//createStream if ((amfdata.getContentP(0)->StrValue() == "getStreamLength") || (amfdata.getContentP(0)->StrValue() == "getMovLen")){ //send a _result reply - AMFType amfreply("container", (unsigned char)0xFF); + AMFType amfreply("container", (unsigned char)AMF0_DDV_CONTAINER); amfreply.addContent(AMFType("", "_result"));//result success amfreply.addContent(amfdata.getContent(1));//same transaction ID - amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info + amfreply.addContent(AMFType("", (double)0, AMF0_NULL));//null - command info amfreply.addContent(AMFType("", (double)0));//zero length #if DEBUG >= 4 amfreply.Print(); @@ -171,11 +171,11 @@ void parseChunk(){ }//getStreamLength if (amfdata.getContentP(0)->StrValue() == "checkBandwidth"){ //send a _result reply - AMFType amfreply("container", (unsigned char)0xFF); + AMFType amfreply("container", (unsigned char)AMF0_DDV_CONTAINER); amfreply.addContent(AMFType("", "_result"));//result success amfreply.addContent(amfdata.getContent(1));//same transaction ID - amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info - amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info + amfreply.addContent(AMFType("", (double)0, AMF0_NULL));//null - command info + amfreply.addContent(AMFType("", (double)0, AMF0_NULL));//null - command info #if DEBUG >= 4 amfreply.Print(); #endif @@ -191,10 +191,10 @@ void parseChunk(){ streamname = "/tmp/shared_socket_" + streamname; SendUSR(0, 1);//send UCM StreamBegin (0), stream 1 //send a status reply - AMFType amfreply("container", (unsigned char)0xFF); + AMFType amfreply("container", (unsigned char)AMF0_DDV_CONTAINER); amfreply.addContent(AMFType("", "onStatus"));//status reply amfreply.addContent(amfdata.getContent(1));//same transaction ID - amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info + amfreply.addContent(AMFType("", (double)0, AMF0_NULL));//null - command info amfreply.addContent(AMFType(""));//info amfreply.getContentP(3)->addContent(AMFType("level", "status")); amfreply.getContentP(3)->addContent(AMFType("code", "NetStream.Play.Reset")); @@ -205,10 +205,10 @@ void parseChunk(){ amfreply.Print(); #endif SendChunk(4, 20, next.msg_stream_id, amfreply.Pack()); - amfreply = AMFType("container", (unsigned char)0xFF); + amfreply = AMFType("container", (unsigned char)AMF0_DDV_CONTAINER); amfreply.addContent(AMFType("", "onStatus"));//status reply amfreply.addContent(amfdata.getContent(1));//same transaction ID - amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info + amfreply.addContent(AMFType("", (double)0, AMF0_NULL));//null - command info amfreply.addContent(AMFType(""));//info amfreply.getContentP(3)->addContent(AMFType("level", "status")); amfreply.getContentP(3)->addContent(AMFType("code", "NetStream.Play.Start"));