void inputFLV::getNext(bool smart) { static JSON::Value thisPack; static AMF::Object amf_storage; thisPack.null(); long long int lastBytePos = ftell(inFile); FLV::Tag tmpTag; while (!feof(inFile) && !FLV::Parse_Error){ if (tmpTag.FileLoader(inFile)){ thisPack = tmpTag.toJSON(myMeta, amf_storage); thisPack["bpos"] = lastBytePos; if ( !selectedTracks.count(thisPack["trackid"].asInt())){ getNext(); } break; } } if (FLV::Parse_Error){ FAIL_MSG("FLV error: %s", FLV::Error_Str.c_str()); thisPack.null(); thisPacket.null(); return; } std::string tmpStr = thisPack.toNetPacked(); thisPacket.reInit(tmpStr.data(), tmpStr.size()); }
bool inputFLV::readHeader() { JSON::Value lastPack; if (!inFile) { return false; } //See whether a separate header file exists. DTSC::File tmp(config->getString("input") + ".dtsh"); if (tmp){ myMeta = tmp.getMeta(); return true; } //Create header file from FLV data fseek(inFile, 13, SEEK_SET); FLV::Tag tmpTag; long long int lastBytePos = 13; while (!feof(inFile) && !FLV::Parse_Error){ if (tmpTag.FileLoader(inFile)){ lastPack.null(); lastPack = tmpTag.toJSON(myMeta); lastPack["bpos"] = lastBytePos; myMeta.update(lastPack); lastBytePos = ftell(inFile); } } if (FLV::Parse_Error){ std::cerr << FLV::Error_Str << std::endl; return false; } std::ofstream oFile(std::string(config->getString("input") + ".dtsh").c_str()); oFile << myMeta.toJSON().toNetPacked(); oFile.close(); return true; }
void OutProgressiveFLV::sendNext(){ FLV::Tag tag; bool tmp = tag.DTSCLoader(currentPacket, myMeta.tracks[currentPacket.getTrackId()]); if (!tmp){ DEBUG_MSG(DLVL_DEVEL, "Invalid JSON"); } myConn.SendNow(tag.data, tag.len); }
void OutProgressiveFLV::sendHeader(){ HTTP::Parser HTTP_S; FLV::Tag tag; HTTP_S.SetHeader("Content-Type", "video/x-flv"); HTTP_S.protocol = "HTTP/1.0"; myConn.SendNow(HTTP_S.BuildResponse("200", "OK")); myConn.SendNow(FLV::Header, 13); tag.DTSCMetaInit(myMeta, selectedTracks); myConn.SendNow(tag.data, tag.len); for (std::set<long unsigned int>::iterator it = selectedTracks.begin(); it != selectedTracks.end(); it++){ if (myMeta.tracks[*it].type == "video"){ tag.DTSCVideoInit(myMeta.tracks[*it]); myConn.SendNow(tag.data, tag.len); } if (myMeta.tracks[*it].type == "audio"){ tag.DTSCAudioInit(myMeta.tracks[*it]); myConn.SendNow(tag.data, tag.len); } } sentHeader = true; }
void inputFLV::getNext(bool smart) { static JSON::Value thisPack; thisPack.null(); long long int lastBytePos = ftell(inFile); FLV::Tag tmpTag; while (!feof(inFile) && !FLV::Parse_Error){ if (tmpTag.FileLoader(inFile)){ thisPack = tmpTag.toJSON(myMeta); thisPack["bpos"] = lastBytePos; if ( !selectedTracks.count(thisPack["trackid"].asInt())){ getNext(); } break; } } if (FLV::Parse_Error){ std::cerr << FLV::Error_Str << std::endl; thisPack.null(); lastPack.null(); return; } std::string tmpStr = thisPack.toNetPacked(); lastPack.reInit(tmpStr.data(), tmpStr.size()); }
///\brief Debugging tool for RTMP data. /// ///Expects RTMP data of one side of the conversation through stdin, outputs human-readable information to stderr. /// ///Will output FLV file to stdout, if available. /// ///Automatically skips the handshake data. ///\param conf The configuration parsed from the commandline. ///\return The return code of the analyser. int analyseRTMP(Util::Config conf){ int Detail = conf.getInteger("detail"); if (Detail > 0){ fprintf(stderr, "Detail level set:\n"); if (Detail & DETAIL_RECONSTRUCT){ fprintf(stderr, " - Will reconstuct FLV file to stdout\n"); std::cout.write(FLV::Header, 13); } if (Detail & DETAIL_EXPLICIT){ fprintf(stderr, " - Will list explicit video/audio data information\n"); } if (Detail & DETAIL_VERBOSE){ fprintf(stderr, " - Will list verbose chunk information\n"); } } std::string inbuffer; inbuffer.reserve(3073); while (std::cin.good() && inbuffer.size() < 3073){ inbuffer += std::cin.get(); } //read all of std::cin to temp inbuffer.erase(0, 3073); //strip the handshake part RTMPStream::Chunk next; FLV::Tag F; //FLV holder AMF::Object amfdata("empty", AMF::AMF0_DDV_CONTAINER); AMF::Object3 amf3data("empty", AMF::AMF3_DDV_CONTAINER); while (std::cin.good() || inbuffer.size()){ if (next.Parse(inbuffer)){ if (Detail & DETAIL_VERBOSE){ fprintf(stderr, "Chunk info: [%#2X] CS ID %u, timestamp %u, len %u, type ID %u, Stream ID %u\n", next.headertype, next.cs_id, next.timestamp, next.len, next.msg_type_id, next.msg_stream_id); } switch (next.msg_type_id){ case 0: //does not exist fprintf(stderr, "Error chunk - %i, %i, %i, %i, %i\n", next.cs_id, next.timestamp, next.real_len, next.len_left, next.msg_stream_id); //return 0; break; //happens when connection breaks unexpectedly case 1: //set chunk size RTMPStream::chunk_rec_max = ntohl(*(int*)next.data.c_str()); fprintf(stderr, "CTRL: Set chunk size: %i\n", RTMPStream::chunk_rec_max); break; case 2: //abort message - we ignore this one fprintf(stderr, "CTRL: Abort message: %i\n", ntohl(*(int*)next.data.c_str())); //4 bytes of stream id to drop break; case 3: //ack RTMPStream::snd_window_at = ntohl(*(int*)next.data.c_str()); fprintf(stderr, "CTRL: Acknowledgement: %i\n", RTMPStream::snd_window_at); break; case 4: { short int ucmtype = ntohs(*(short int*)next.data.c_str()); switch (ucmtype){ case 0: fprintf(stderr, "CTRL: User control message: stream begin %u\n", ntohl(*(unsigned int*)(next.data.c_str()+2))); break; case 1: fprintf(stderr, "CTRL: User control message: stream EOF %u\n", ntohl(*(unsigned int*)(next.data.c_str()+2))); break; case 2: fprintf(stderr, "CTRL: User control message: stream dry %u\n", ntohl(*(unsigned int*)(next.data.c_str()+2))); break; case 3: fprintf(stderr, "CTRL: User control message: setbufferlen %u\n", ntohl(*(unsigned int*)(next.data.c_str()+2))); break; case 4: fprintf(stderr, "CTRL: User control message: streamisrecorded %u\n", ntohl(*(unsigned int*)(next.data.c_str()+2))); break; case 6: fprintf(stderr, "CTRL: User control message: pingrequest %u\n", ntohl(*(unsigned int*)(next.data.c_str()+2))); break; case 7: fprintf(stderr, "CTRL: User control message: pingresponse %u\n", ntohl(*(unsigned int*)(next.data.c_str()+2))); break; default: fprintf(stderr, "CTRL: User control message: UNKNOWN %hu - %u\n", ucmtype, ntohl(*(unsigned int*)(next.data.c_str()+2))); break; } } break; case 5: //window size of other end RTMPStream::rec_window_size = ntohl(*(int*)next.data.c_str()); RTMPStream::rec_window_at = RTMPStream::rec_cnt; fprintf(stderr, "CTRL: Window size: %i\n", RTMPStream::rec_window_size); break; case 6: RTMPStream::snd_window_size = ntohl(*(int*)next.data.c_str()); //4 bytes window size, 1 byte limit type (ignored) fprintf(stderr, "CTRL: Set peer bandwidth: %i\n", RTMPStream::snd_window_size); break; case 8: if (Detail & (DETAIL_EXPLICIT | DETAIL_RECONSTRUCT)){ F.ChunkLoader(next); if (Detail & DETAIL_EXPLICIT){ fprintf(stderr, "Received %i bytes audio data\n", next.len); std::cerr << "Got a " << F.len << " bytes " << F.tagType() << " FLV tag of time " << F.tagTime() << "." << std::endl; } if (Detail & DETAIL_RECONSTRUCT){ std::cout.write(F.data, F.len); } } break; case 9: if (Detail & (DETAIL_EXPLICIT | DETAIL_RECONSTRUCT)){ F.ChunkLoader(next); if (Detail & DETAIL_EXPLICIT){ fprintf(stderr, "Received %i bytes video data\n", next.len); std::cerr << "Got a " << F.len << " bytes " << F.tagType() << " FLV tag of time " << F.tagTime() << "." << std::endl; } if (Detail & DETAIL_RECONSTRUCT){ std::cout.write(F.data, F.len); } } break; case 15: fprintf(stderr, "Received AFM3 data message\n"); break; case 16: fprintf(stderr, "Received AFM3 shared object\n"); break; case 17: { fprintf(stderr, "Received AFM3 command message:\n"); char soort = next.data[0]; next.data = next.data.substr(1); if (soort == 0){ amfdata = AMF::parse(next.data); std::cerr << amfdata.Print() << std::endl; }else{ amf3data = AMF::parse3(next.data); amf3data.Print(); } } break; case 18: { fprintf(stderr, "Received AFM0 data message (metadata):\n"); amfdata = AMF::parse(next.data); amfdata.Print(); if (Detail & DETAIL_RECONSTRUCT){ F.ChunkLoader(next); std::cout.write(F.data, F.len); } } break; case 19: fprintf(stderr, "Received AFM0 shared object\n"); break; case 20: { //AMF0 command message fprintf(stderr, "Received AFM0 command message:\n"); amfdata = AMF::parse(next.data); std::cerr << amfdata.Print() << std::endl; } break; case 22: fprintf(stderr, "Received aggregate message\n"); break; default: fprintf(stderr, "Unknown chunk received! Probably protocol corruption, stopping parsing of incoming data.\n"); return 1; break; } //switch for type of chunk }else{ //if chunk parsed if (std::cin.good()){ inbuffer += std::cin.get(); }else{ inbuffer.clear(); } } }//while std::cin.good() fprintf(stderr, "No more readable data\n"); return 0; }
///\brief Main function for the HTTP Dynamic Connector ///\param conn A socket describing the connection the client. ///\return The exit code of the connector. int dynamicConnector(Socket::Connection conn){ std::deque<std::string> FlashBuf; int FlashBufSize = 0; long long int FlashBufTime = 0; FLV::Tag tmp; //temporary tag DTSC::Stream Strm; //Incoming stream buffer. HTTP::Parser HTTP_R, HTTP_S; //HTTP Receiver en HTTP Sender. Socket::Connection ss( -1); std::string streamname; std::string recBuffer = ""; std::string Quality; int Segment = -1; int ReqFragment = -1; unsigned int lastStats = 0; conn.setBlocking(false); //do not block on conn.spool() when no data is available while (conn.connected()){ if (conn.spool() || conn.Received().size()){ //make sure it ends in a \n if ( *(conn.Received().get().rbegin()) != '\n'){ std::string tmp = conn.Received().get(); conn.Received().get().clear(); if (conn.Received().size()){ conn.Received().get().insert(0, tmp); }else{ conn.Received().append(tmp); } } if (HTTP_R.Read(conn.Received().get())){ #if DEBUG >= 5 std::cout << "Received request: " << HTTP_R.getUrl() << std::endl; #endif conn.setHost(HTTP_R.GetHeader("X-Origin")); streamname = HTTP_R.GetHeader("X-Stream"); if ( !ss){ ss = Util::Stream::getStream(streamname); if ( !ss.connected()){ HTTP_S.Clean(); HTTP_S.SetBody("No such stream is available on the system. Please try again.\n"); conn.SendNow(HTTP_S.BuildResponse("404", "Not found")); continue; } ss.setBlocking(false); //make sure metadata is received while ( !Strm.metadata && ss.connected()){ if (ss.spool()){ while (Strm.parsePacket(ss.Received())){ //do nothing } } } } if (HTTP_R.url.find(".abst") != std::string::npos){ HTTP_S.Clean(); HTTP_S.SetBody(dynamicBootstrap(streamname, Strm.metadata)); HTTP_S.SetHeader("Content-Type", "binary/octet"); HTTP_S.SetHeader("Cache-Control", "no-cache"); conn.SendNow(HTTP_S.BuildResponse("200", "OK")); HTTP_R.Clean(); //clean for any possible next requests continue; } if (HTTP_R.url.find("f4m") == std::string::npos){ Quality = HTTP_R.url.substr(HTTP_R.url.find("/", 10) + 1); Quality = Quality.substr(0, Quality.find("Seg")); int temp; temp = HTTP_R.url.find("Seg") + 3; Segment = atoi(HTTP_R.url.substr(temp, HTTP_R.url.find("-", temp) - temp).c_str()); temp = HTTP_R.url.find("Frag") + 4; ReqFragment = atoi(HTTP_R.url.substr(temp).c_str()); #if DEBUG >= 5 printf("Quality: %s, Seg %d Frag %d\n", Quality.c_str(), Segment, ReqFragment); #endif if (Strm.metadata.isMember("live")){ int seekable = Strm.canSeekFrame(ReqFragment); if (seekable == 0){ // iff the fragment in question is available, check if the next is available too seekable = Strm.canSeekFrame(ReqFragment + 1); } if (seekable < 0){ HTTP_S.Clean(); HTTP_S.SetBody("The requested fragment is no longer kept in memory on the server and cannot be served.\n"); conn.SendNow(HTTP_S.BuildResponse("412", "Fragment out of range")); HTTP_R.Clean(); //clean for any possible next requests std::cout << "Fragment @ F" << ReqFragment << " too old (F" << Strm.metadata["keynum"][0u].asInt() << " - " << Strm.metadata["keynum"][Strm.metadata["keynum"].size() - 1].asInt() << ")" << std::endl; continue; } if (seekable > 0){ HTTP_S.Clean(); HTTP_S.SetBody("Proxy, re-request this in a second or two.\n"); conn.SendNow(HTTP_S.BuildResponse("208", "Ask again later")); HTTP_R.Clean(); //clean for any possible next requests std::cout << "Fragment @ F" << ReqFragment << " not available yet (F" << Strm.metadata["keynum"][0u].asInt() << " - " << Strm.metadata["keynum"][Strm.metadata["keynum"].size() - 1].asInt() << ")" << std::endl; continue; } } std::stringstream sstream; sstream << "f " << ReqFragment << "\no \n"; ss.SendNow(sstream.str().c_str()); }else{ HTTP_S.Clean(); HTTP_S.SetHeader("Content-Type", "text/xml"); HTTP_S.SetHeader("Cache-Control", "no-cache"); std::string manifest = dynamicIndex(streamname, Strm.metadata); HTTP_S.SetBody(manifest); conn.SendNow(HTTP_S.BuildResponse("200", "OK")); } HTTP_R.Clean(); //clean for any possible next requests } }else{ Util::sleep(1); } if (ss.connected()){ unsigned int now = Util::epoch(); if (now != lastStats){ lastStats = now; ss.SendNow(conn.getStats("HTTP_Dynamic").c_str()); } if (ss.spool()){ while (Strm.parsePacket(ss.Received())){ if (Strm.lastType() == DTSC::PAUSEMARK){ if (FlashBufSize){ HTTP_S.Clean(); HTTP_S.SetHeader("Content-Type", "video/mp4"); HTTP_S.SetBody(""); std::string new_strap = dynamicBootstrap(streamname, Strm.metadata, ReqFragment); HTTP_S.SetHeader("Content-Length", FlashBufSize + 8 + new_strap.size()); //32+33+btstrp.size()); conn.SendNow(HTTP_S.BuildResponse("200", "OK")); conn.SendNow(new_strap); unsigned long size = htonl(FlashBufSize+8); conn.SendNow((char*) &size, 4); conn.SendNow("mdat", 4); while (FlashBuf.size() > 0){ conn.SendNow(FlashBuf.front()); FlashBuf.pop_front(); } } FlashBuf.clear(); FlashBufSize = 0; } if (Strm.lastType() == DTSC::VIDEO || Strm.lastType() == DTSC::AUDIO){ if (FlashBufSize == 0){ //fill buffer with init data, if needed. if (Strm.metadata.isMember("audio") && Strm.metadata["audio"].isMember("init")){ tmp.DTSCAudioInit(Strm); tmp.tagTime(Strm.getPacket(0)["time"].asInt()); FlashBuf.push_back(std::string(tmp.data, tmp.len)); FlashBufSize += tmp.len; } if (Strm.metadata.isMember("video") && Strm.metadata["video"].isMember("init")){ tmp.DTSCVideoInit(Strm); tmp.tagTime(Strm.getPacket(0)["time"].asInt()); FlashBuf.push_back(std::string(tmp.data, tmp.len)); FlashBufSize += tmp.len; } FlashBufTime = Strm.getPacket(0)["time"].asInt(); } tmp.DTSCLoader(Strm); FlashBuf.push_back(std::string(tmp.data, tmp.len)); FlashBufSize += tmp.len; } } } if ( !ss.connected()){ break; } } } conn.close(); ss.SendNow(conn.getStats("HTTP_Dynamic").c_str()); ss.close(); return 0; } //Connector_HTTP_Dynamic main function
/// Tries to get and parse one RTMP chunk at a time. void Connector_RTMP::parseChunk(Socket::Buffer & inbuffer){ //for DTSC conversion static JSON::Value meta_out; static std::stringstream prebuffer; // Temporary buffer before sending real data static bool sending = false; static unsigned int counter = 0; //for chunk parsing static RTMPStream::Chunk next; FLV::Tag F; static AMF::Object amfdata("empty", AMF::AMF0_DDV_CONTAINER); static AMF::Object amfelem("empty", AMF::AMF0_DDV_CONTAINER); static AMF::Object3 amf3data("empty", AMF::AMF3_DDV_CONTAINER); static AMF::Object3 amf3elem("empty", AMF::AMF3_DDV_CONTAINER); while (next.Parse(inbuffer)){ //send ACK if we received a whole window if ((RTMPStream::rec_cnt - RTMPStream::rec_window_at > RTMPStream::rec_window_size)){ RTMPStream::rec_window_at = RTMPStream::rec_cnt; Socket.Send(RTMPStream::SendCTL(3, RTMPStream::rec_cnt));//send ack (msg 3) } switch (next.msg_type_id){ case 0://does not exist #if DEBUG >= 2 fprintf(stderr, "UNKN: Received a zero-type message. This is an error.\n"); #endif break;//happens when connection breaks unexpectedly case 1://set chunk size RTMPStream::chunk_rec_max = ntohl(*(int*)next.data.c_str()); #if DEBUG >= 4 fprintf(stderr, "CTRL: Set chunk size: %i\n", RTMPStream::chunk_rec_max); #endif break; case 2://abort message - we ignore this one #if DEBUG >= 4 fprintf(stderr, "CTRL: Abort message\n"); #endif //4 bytes of stream id to drop break; case 3://ack #if DEBUG >= 4 fprintf(stderr, "CTRL: Acknowledgement\n"); #endif RTMPStream::snd_window_at = ntohl(*(int*)next.data.c_str()); RTMPStream::snd_window_at = RTMPStream::snd_cnt; break; case 4:{ //2 bytes event type, rest = event data //types: //0 = stream begin, 4 bytes ID //1 = stream EOF, 4 bytes ID //2 = stream dry, 4 bytes ID //3 = setbufferlen, 4 bytes ID, 4 bytes length //4 = streamisrecorded, 4 bytes ID //6 = pingrequest, 4 bytes data //7 = pingresponse, 4 bytes data //we don't need to process this #if DEBUG >= 4 short int ucmtype = ntohs(*(short int*)next.data.c_str()); switch (ucmtype){ case 0: fprintf(stderr, "CTRL: UCM StreamBegin %i\n", ntohl(*((int*)(next.data.c_str()+2)))); break; case 1: fprintf(stderr, "CTRL: UCM StreamEOF %i\n", ntohl(*((int*)(next.data.c_str()+2)))); break; case 2: fprintf(stderr, "CTRL: UCM StreamDry %i\n", ntohl(*((int*)(next.data.c_str()+2)))); break; case 3: fprintf(stderr, "CTRL: UCM SetBufferLength %i %i\n", ntohl(*((int*)(next.data.c_str()+2))), ntohl(*((int*)(next.data.c_str()+6)))); break; case 4: fprintf(stderr, "CTRL: UCM StreamIsRecorded %i\n", ntohl(*((int*)(next.data.c_str()+2)))); break; case 6: fprintf(stderr, "CTRL: UCM PingRequest %i\n", ntohl(*((int*)(next.data.c_str()+2)))); break; case 7: fprintf(stderr, "CTRL: UCM PingResponse %i\n", ntohl(*((int*)(next.data.c_str()+2)))); break; default: fprintf(stderr, "CTRL: UCM Unknown (%hi)\n", ucmtype); break; } #endif } break; case 5://window size of other end #if DEBUG >= 4 fprintf(stderr, "CTRL: Window size\n"); #endif RTMPStream::rec_window_size = ntohl(*(int*)next.data.c_str()); RTMPStream::rec_window_at = RTMPStream::rec_cnt; Socket.Send(RTMPStream::SendCTL(3, RTMPStream::rec_cnt));//send ack (msg 3) break; case 6: #if DEBUG >= 4 fprintf(stderr, "CTRL: Set peer bandwidth\n"); #endif //4 bytes window size, 1 byte limit type (ignored) RTMPStream::snd_window_size = ntohl(*(int*)next.data.c_str()); Socket.Send(RTMPStream::SendCTL(5, RTMPStream::snd_window_size));//send window acknowledgement size (msg 5) break; case 8://audio data case 9://video data case 18://meta data if (SS.connected()){ F.ChunkLoader(next); JSON::Value pack_out = F.toJSON(meta_out); if (!pack_out.isNull()){ if (!sending){ counter++; if (counter > 8){ sending = true; SS.SendNow(meta_out.toNetPacked()); SS.SendNow(prebuffer.str().c_str());//write buffer prebuffer.str("");//clear buffer SS.Send(pack_out.toNetPacked()); }else{ prebuffer << pack_out.toNetPacked(); } }else{ SS.SendNow(pack_out.toNetPacked()); } } }else{ #if DEBUG >= 4 fprintf(stderr, "Received useless media data\n"); #endif Socket.close(); } break; case 15: #if DEBUG >= 4 fprintf(stderr, "Received AFM3 data message\n"); #endif break; case 16: #if DEBUG >= 4 fprintf(stderr, "Received AFM3 shared object\n"); #endif break; case 17:{ #if DEBUG >= 4 fprintf(stderr, "Received AFM3 command message\n"); #endif if (next.data[0] != 0){ next.data = next.data.substr(1); amf3data = AMF::parse3(next.data); #if DEBUG >= 4 amf3data.Print(); #endif }else{ #if DEBUG >= 4 fprintf(stderr, "Received AFM3-0 command message\n"); #endif next.data = next.data.substr(1); amfdata = AMF::parse(next.data); parseAMFCommand(amfdata, 17, next.msg_stream_id); }//parsing AMF0-style } break; case 19: #if DEBUG >= 4 fprintf(stderr, "Received AFM0 shared object\n"); #endif break; case 20:{//AMF0 command message amfdata = AMF::parse(next.data); parseAMFCommand(amfdata, 20, next.msg_stream_id); } break; case 22: #if DEBUG >= 4 fprintf(stderr, "Received aggregate message\n"); #endif break; default: #if DEBUG >= 1 fprintf(stderr, "Unknown chunk received! Probably protocol corruption, stopping parsing of incoming data.\n"); #endif Connector_RTMP::stopparsing = true; break; } } }//parseChunk