From a8c7bc5d27c1294529434432f1716ce830922c5f Mon Sep 17 00:00:00 2001 From: Thulinma Date: Fri, 6 Dec 2019 13:29:26 +0100 Subject: [PATCH] Improved RTMP cross-server compatibility --- lib/rtmpchunks.cpp | 12 ++++++++---- src/output/output_rtmp.cpp | 7 +++++-- src/output/output_rtmp.h | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/rtmpchunks.cpp b/lib/rtmpchunks.cpp index c4370b46..a06a435b 100644 --- a/lib/rtmpchunks.cpp +++ b/lib/rtmpchunks.cpp @@ -250,7 +250,7 @@ std::string &RTMPStream::SendMedia(FLV::Tag &tag){ std::string &RTMPStream::SendCTL(unsigned char type, unsigned int data){ static RTMPStream::Chunk ch; ch.cs_id = 2; - ch.timestamp = Util::getMS(); + ch.timestamp = 0; ch.msg_type_id = type; ch.msg_stream_id = 0; ch.len_left = 0; @@ -265,6 +265,7 @@ std::string &RTMPStream::SendCTL(unsigned char type, unsigned int data){ ch.data.resize(4); } *(int *)((char *)ch.data.data()) = htonl(data); + lastsend.erase(2u); return ch.Pack(); }// SendCTL @@ -272,7 +273,7 @@ std::string &RTMPStream::SendCTL(unsigned char type, unsigned int data){ std::string &RTMPStream::SendCTL(unsigned char type, unsigned int data, unsigned char data2){ static RTMPStream::Chunk ch; ch.cs_id = 2; - ch.timestamp = Util::getMS(); + ch.timestamp = 0; ch.len = 5; ch.real_len = 5; ch.len_left = 0; @@ -281,6 +282,7 @@ std::string &RTMPStream::SendCTL(unsigned char type, unsigned int data, unsigned ch.data.resize(5); *(unsigned int *)((char *)ch.data.c_str()) = htonl(data); ch.data[4] = data2; + lastsend.erase(2u); return ch.Pack(); }// SendCTL @@ -288,7 +290,7 @@ std::string &RTMPStream::SendCTL(unsigned char type, unsigned int data, unsigned std::string &RTMPStream::SendUSR(unsigned char type, unsigned int data){ static RTMPStream::Chunk ch; ch.cs_id = 2; - ch.timestamp = Util::getMS(); + ch.timestamp = 0; ch.len = 6; ch.real_len = 6; ch.len_left = 0; @@ -298,6 +300,7 @@ std::string &RTMPStream::SendUSR(unsigned char type, unsigned int data){ *(unsigned int *)(((char *)ch.data.c_str()) + 2) = htonl(data); ch.data[0] = 0; ch.data[1] = type; + lastsend.erase(2u); return ch.Pack(); }// SendUSR @@ -305,7 +308,7 @@ std::string &RTMPStream::SendUSR(unsigned char type, unsigned int data){ std::string &RTMPStream::SendUSR(unsigned char type, unsigned int data, unsigned int data2){ static RTMPStream::Chunk ch; ch.cs_id = 2; - ch.timestamp = Util::getMS(); + ch.timestamp = 0; ch.len = 10; ch.real_len = 10; ch.len_left = 0; @@ -316,6 +319,7 @@ std::string &RTMPStream::SendUSR(unsigned char type, unsigned int data, unsigned *(unsigned int *)(((char *)ch.data.c_str()) + 6) = htonl(data2); ch.data[0] = 0; ch.data[1] = type; + lastsend.erase(2u); return ch.Pack(); }// SendUSR diff --git a/src/output/output_rtmp.cpp b/src/output/output_rtmp.cpp index b38cecf9..d5979c27 100644 --- a/src/output/output_rtmp.cpp +++ b/src/output/output_rtmp.cpp @@ -13,6 +13,7 @@ namespace Mist{ OutRTMP::OutRTMP(Socket::Connection &conn) : Output(conn){ + lastAck = Util::bootSecs(); lastOutTime = 0; setRtmpOffset = false; rtmpOffset = 0; @@ -1151,7 +1152,8 @@ namespace Mist{ while (next.Parse(inputBuffer)){ // send ACK if we received a whole window - if ((RTMPStream::rec_cnt - RTMPStream::rec_window_at > RTMPStream::rec_window_size)){ + if ((RTMPStream::rec_cnt - RTMPStream::rec_window_at > RTMPStream::rec_window_size) || Util::bootSecs() > lastAck+15){ + lastAck = Util::bootSecs(); RTMPStream::rec_window_at = RTMPStream::rec_cnt; myConn.SendNow(RTMPStream::SendCTL(3, RTMPStream::rec_cnt)); // send ack (msg 3) } @@ -1203,7 +1205,7 @@ namespace Mist{ break; case 6: MEDIUM_MSG("CTRL: UCM PingRequest %" PRIu32, Bit::btohl(next.data.data() + 2)); - myConn.SendNow(RTMPStream::SendUSR(7, 1)); // send UCM PingResponse (7) + myConn.SendNow(RTMPStream::SendUSR(7, Bit::btohl(next.data.data() + 2))); // send UCM PingResponse (7) break; case 7: MEDIUM_MSG("CTRL: UCM PingResponse %" PRIu32, Bit::btohl(next.data.data() + 2)); @@ -1216,6 +1218,7 @@ namespace Mist{ RTMPStream::rec_window_size = Bit::btohl(next.data.data()); RTMPStream::rec_window_at = RTMPStream::rec_cnt; myConn.SendNow(RTMPStream::SendCTL(3, RTMPStream::rec_cnt)); // send ack (msg 3) + lastAck = Util::bootSecs(); break; case 6: MEDIUM_MSG("CTRL: Set peer bandwidth"); diff --git a/src/output/output_rtmp.h b/src/output/output_rtmp.h index fe19174b..cdc72b0e 100644 --- a/src/output/output_rtmp.h +++ b/src/output/output_rtmp.h @@ -29,6 +29,7 @@ namespace Mist{ void parseAMFCommand(AMF::Object &amfData, int messageType, int streamId); void sendCommand(AMF::Object &amfReply, int messageType, int streamId); void startPushOut(const char *args); + uint64_t lastAck; HTTP::URL pushApp, pushUrl; uint8_t authAttempts; };