From d140448625c476ca58334df228a5be7152607b0c Mon Sep 17 00:00:00 2001 From: hyc Date: Thu, 12 Nov 2009 12:37:37 +0000 Subject: [PATCH] Minor cleanup, experimental Reconnect approach. Seems to only be relevant for FMS 3.0; pause/resume works for FMS 3.5 git-svn-id: svn://svn.mplayerhq.hu/rtmpdump@48 400ebc74-4327-4243-bc38-086b20814532 --- rtmp.cpp | 10 ++++++++++ rtmp.h | 1 + rtmpdump.cpp | 34 ++++++++++------------------------ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/rtmp.cpp b/rtmp.cpp index 903ff72..9de5fcb 100644 --- a/rtmp.cpp +++ b/rtmp.cpp @@ -122,6 +122,12 @@ double CRTMP::GetDuration() { return m_fDuration; } bool CRTMP::IsConnected() { return m_socket != 0; } bool CRTMP::IsTimedout() { return m_bTimedout; } +void CRTMP::GetPauseStamps(uint32_t *pause, uint32_t *resume) +{ + *pause = m_pauseStamp; + *resume = m_mediaStamp; +} + void CRTMP::SetBufferMS(int size) { m_nBufferMS = size; @@ -440,6 +446,10 @@ int CRTMP::GetNextMediaPacket(RTMPPacket &packet) if (bHasMediaPacket) m_bPlaying = true; + else if (m_bTimedout) { + m_pauseStamp = m_channelTimestamp[m_mediaChannel]; + m_bPausing = 3; + } return bHasMediaPacket; } diff --git a/rtmp.h b/rtmp.h index 65558fe..c568cab 100644 --- a/rtmp.h +++ b/rtmp.h @@ -141,6 +141,7 @@ class CRTMP bool IsConnected(); bool IsTimedout(); double GetDuration(); + void GetPauseStamps(uint32_t *pause, uint32_t *resume); bool ConnectStream(double seekTime=-10.0, uint32_t dLength=0); bool ReconnectStream(int bufferTime, double seekTime=-10.0, uint32_t dLength=0); diff --git a/rtmpdump.cpp b/rtmpdump.cpp index 9d1e4ea..edc7f78 100644 --- a/rtmpdump.cpp +++ b/rtmpdump.cpp @@ -40,6 +40,7 @@ #define SET_BINMODE(f) #endif +#define _DEBUG 1 #include "rtmp.h" #include "log.h" @@ -750,7 +751,7 @@ int Download(CRTMP *rtmp, // connected CRTMP object // Workaround to exit with 0 if the file is fully (> 99.9%) downloaded if( duration > 0 ) { if ((double)timestamp >= (double)duration*999.0 ) { - LogPrintf("Already Completed at: %.3f sec Duration=%.3f sec\n", (double)timestamp/1000.0, (double)timestamp/1000.0); + LogPrintf("Already Completed at: %.3f sec Duration=%.3f sec\n", (double)timestamp/1000.0, (double)duration/1000.0); return RD_SUCCESS; } else { *percent = ((double)timestamp) / (duration*1000.0)*100.0; @@ -788,7 +789,7 @@ int Download(CRTMP *rtmp, // connected CRTMP object lastUpdate = now-1000; do { - nRead = WriteStream(rtmp, &buffer, bufferSize, ×tamp, bResume, bLiveStream, dSeek, metaHeader, nMetaHeaderSize, initialFrame, initialFrameType, nInitialFrameSize, &dataType); + nRead = WriteStream(rtmp, &buffer, bufferSize, ×tamp, bResume && nInitialFrameSize > 0, bLiveStream, dSeek, metaHeader, nMetaHeaderSize, initialFrame, initialFrameType, nInitialFrameSize, &dataType); //LogPrintf("nRead: %d\n", nRead); if(nRead > 0) { @@ -1254,7 +1255,6 @@ int main(int argc, char **argv) dSeek, 0, bLiveStream, timeout); off_t size = 0; - uint32_t timestamp = 0; // ok, we have to get the timestamp of the last keyframe (only keyframes are seekable) / last audio frame (audio only streams) if(bResume) { @@ -1316,11 +1316,6 @@ int main(int argc, char **argv) Log(LOGINFO, "Connected..."); - timestamp = dSeek; - if(dSeek != 0) { - Log(LOGDEBUG, "Continuing at TS: %d ms\n", timestamp); - } - // User defined seek offset if (dStartOffset > 0) { // Don't need the start offset if resuming an existing file @@ -1328,8 +1323,7 @@ int main(int argc, char **argv) Log(LOGWARNING, "Can't seek a resumed stream, ignoring --start option"); dStartOffset = 0; } else { - // ??? we want to add not equals ??? - dSeek += dStartOffset; + dSeek = dStartOffset; } } @@ -1351,21 +1345,14 @@ int main(int argc, char **argv) } } else { nInitialFrameSize = 0; + uint32_t lastOff; Log(LOGINFO, "Connection timed out, trying to reconnect.\n\n"); - - nStatus = GetLastKeyframe(file, nSkipKeyFrames, - &dSeek, &initialFrame, - &initialFrameType, - &nInitialFrameSize); - if (nStatus != RD_SUCCESS) { - Log(LOGDEBUG, "Failed to get last keyframe."); - break; - } + rtmp->GetPauseStamps(&lastOff, &dSeek); // Calculate the length of the stream to still play if (dStopOffset > 0) { - dLength = dStopOffset - dSeek; + dLength = dStopOffset - lastOff; // Quit if start seek is past required stop offset if(dLength <= 0) { @@ -1375,7 +1362,7 @@ int main(int argc, char **argv) } } - if (!rtmp->ReconnectStream(bufferTime, dSeek, dLength)) { + if (!rtmp->ReconnectStream(bufferTime, lastOff, dLength)) { Log(LOGERROR, "Failed to resume the stream\n\n"); if (!rtmp->IsTimedout()) nStatus = RD_FAILED; @@ -1394,10 +1381,9 @@ int main(int argc, char **argv) free(initialFrame); initialFrame = NULL; - /* If we succeeded, we're done. If writing to stdout - * we can't seek and retry. + /* If we succeeded, we're done. */ - if (nStatus != RD_INCOMPLETE || bStdoutMode || !rtmp->IsTimedout()) + if (nStatus != RD_INCOMPLETE || !rtmp->IsTimedout()) break; } -- 2.40.0