You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
6.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
videoLIVE555Source: fix the crash in the destructor videoLIVE555Source: - _stop() tore the stream down on the caller's thread while the live555 thread was still inside doEventLoop(), and deleted the RTSP client, the environment and the scheduler under the running loop. Now it ends the loop, joins the thread, then closes what is left on its own thread. - shutdownStream() no longer deletes the scheduler/environment (a second shutdown, e.g. _close() then the destructor or a live555 callback then the destructor, used them after they were freed). Only the destructor frees them, once. A second shutdown is a no-op. - m_pRtspClient and the other pointers are initialised: a source that was never opened, or failed to open, shut down a random pointer. - The event-loop flag is reset in _open(), not at thread start, so a stop issued before the thread ran is not lost. - shutdownStream closes the subsession sinks and sends TEARDOWN, as live555 testRTSPClient does. The sinks were never closed and leaked. - continueAfterPLAY frees resultString. DummySink: - Every pointer is NULL-initialised before the constructor can return early. The destructor freed an uninitialised m_pFrame when the codec was not found or in stream-proxy mode. - Decode resources are created whenever DECODE_FRAMES is set, also together with STREAM_PROXY (deviceNodeCamera uses both). They were used uninitialised. Decoding is skipped if any of them is missing. - m_sAvPkt is zeroed (av_packet_unref() frees opaque_ref/side_data). - The destructor frees the frames, the RGB buffer, the scaler, the codec context, the output format context and dictionary, and m_pH264. The trailer is written only after a written header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
videoLIVE555Source: fix the crash in the destructor videoLIVE555Source: - _stop() tore the stream down on the caller's thread while the live555 thread was still inside doEventLoop(), and deleted the RTSP client, the environment and the scheduler under the running loop. Now it ends the loop, joins the thread, then closes what is left on its own thread. - shutdownStream() no longer deletes the scheduler/environment (a second shutdown, e.g. _close() then the destructor or a live555 callback then the destructor, used them after they were freed). Only the destructor frees them, once. A second shutdown is a no-op. - m_pRtspClient and the other pointers are initialised: a source that was never opened, or failed to open, shut down a random pointer. - The event-loop flag is reset in _open(), not at thread start, so a stop issued before the thread ran is not lost. - shutdownStream closes the subsession sinks and sends TEARDOWN, as live555 testRTSPClient does. The sinks were never closed and leaked. - continueAfterPLAY frees resultString. DummySink: - Every pointer is NULL-initialised before the constructor can return early. The destructor freed an uninitialised m_pFrame when the codec was not found or in stream-proxy mode. - Decode resources are created whenever DECODE_FRAMES is set, also together with STREAM_PROXY (deviceNodeCamera uses both). They were used uninitialised. Decoding is skipped if any of them is missing. - m_sAvPkt is zeroed (av_packet_unref() frees opaque_ref/side_data). - The destructor frees the frames, the RGB buffer, the scaler, the codec context, the output format context and dictionary, and m_pH264. The trailer is written only after a written header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __VIDEOLIVE555AVLIBSOURCE_H_INCL__
  2. #define __VIDEOLIVE555AVLIBSOURCE_H_INCL__
  3. #ifndef LRF_PROXY_ONLY
  4. #include "../../../alg/generic/md5.h"
  5. #include "../../../components/base/basicLIBAV.h"
  6. #include "../../../components/base/basicLogCollection.h"
  7. #include "../../../components/base/basicMediaSource.h"
  8. #include "../../../components/base/basicSettings.h"
  9. #include "../../../components/base/basicThread.h"
  10. #include <BasicUsageEnvironment.hh>
  11. #include <liveMedia/liveMedia.hh>
  12. #define RTSP_CLIENT_VERBOSITY_LEVEL 1
  13. #define INBUF_SIZE 40960
  14. #define AUDIO_INBUF_SIZE 20480
  15. #define AUDIO_REFILL_THRESH 4096
  16. #define DUMMY_SINK_RECEIVE_BUFFER_SIZE 400000
  17. #define REQUEST_STREAMING_OVER_TCP True
  18. class videoLIVE555Source;
  19. class StreamClientState
  20. {
  21. public:
  22. StreamClientState();
  23. virtual ~StreamClientState();
  24. public:
  25. MediaSubsessionIterator * iter;
  26. MediaSession * session;
  27. MediaSubsession * subsession;
  28. TaskToken streamTimerTask;
  29. double duration;
  30. };
  31. class CustomRTSPClient : public RTSPClient
  32. {
  33. private:
  34. videoLIVE555Source * session;
  35. StreamClientState streamClientState;
  36. public:
  37. static CustomRTSPClient * createNew(videoLIVE555Source * session, UsageEnvironment & env, const char * rtspURL, int verbosityLevel = 0, const char * applicationName = NULL, portNumBits tunnelOverHTTPPortNum = 0);
  38. StreamClientState & getStreamClientState()
  39. {
  40. return this->streamClientState;
  41. }
  42. videoLIVE555Source * getSession()
  43. {
  44. return this->session;
  45. }
  46. virtual ~CustomRTSPClient();
  47. private:
  48. CustomRTSPClient(videoLIVE555Source * session, UsageEnvironment & env, const char * rtspURL, int verbosityLevel, const char * applicationName, portNumBits tunnelOverHTTPPortNum);
  49. };
  50. class DummySink : public MediaSink
  51. {
  52. public:
  53. static DummySink * createNew(videoLIVE555Source * session, UsageEnvironment & env, MediaSubsession & subsession, // identifies the kind of data that's being received
  54. const char * streamId = NULL, int w = -1, int h = -1, int ww = -1, int hh = -1); // identifies the stream itself (optional)
  55. virtual ~DummySink();
  56. private:
  57. basicLogCollection * m_pLog;
  58. AVCodecID m_eCodecProbe;
  59. cv::Mat * m_pRGB;
  60. int m_nInWidth;
  61. int m_nInHeight;
  62. int m_nOutWidth;
  63. int m_nOutHeight;
  64. DummySink(videoLIVE555Source * session, UsageEnvironment & env, MediaSubsession & subsession, const char * streamId, int w, int h, int ww, int hh);
  65. // called only by "createNew()"
  66. static void afterGettingFrame(void * clientData, unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds);
  67. void afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds);
  68. // void saveFrameToFile()
  69. private:
  70. // redefined virtual functions:
  71. virtual Boolean continuePlaying();
  72. public:
  73. void setSprop(const u_int8_t * prop, unsigned size);
  74. private:
  75. videoLIVE555Source * m_pSession;
  76. uint8_t * m_pReceiveBuffer;
  77. uint8_t * m_pReceiveBufferAV;
  78. MediaSubsession & m_sSubSession;
  79. char * m_pStreamId;
  80. H264or5VideoFileSink * qtOut;
  81. FILE * m_pOutFile[2];
  82. uint8_t m_nCurrentOutFile;
  83. // const avlib v6
  84. const AVCodec * m_pCodec;
  85. AVCodecContext * m_pCodecContext;
  86. AVCodec * m_pOutCodec;
  87. AVCodecContext * m_pOutCodecContext;
  88. AVFormatContext * m_pOutFormatCtx;
  89. AVDictionary * m_pOutFormatOpts;
  90. AVStream * m_pOutStream;
  91. uint8_t * m_pH264;
  92. uint64_t m_iFrameIndex;
  93. AVFrame * m_pFrame;
  94. AVFrame * m_pRGBFrame;
  95. SwsContext * m_pSwsContext;
  96. bool m_bOutHeaderWritten;
  97. bool canDecode(void) const; // decoder, frames and scaler were all created
  98. uint8_t m_aInBuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  99. // char buf[1024];
  100. AVPacket m_sAvPkt;
  101. };
  102. class videoLIVE555Source : public basicMediaSource, public basicThread
  103. {
  104. private:
  105. basicLogCollection * m_pLog;
  106. void * m_pOpaque;
  107. bool m_bIsParamsSet;
  108. int m_nInWidth;
  109. int m_nInHeight;
  110. int m_nOutWidth;
  111. int m_nOutHeight;
  112. MS_WORK_MODE m_eWM;
  113. StreamClientState streamClientState;
  114. virtual Boolean continuePlaying();
  115. uint8_t * fReceiveBuffer;
  116. TaskScheduler * m_pScheduler;
  117. UsageEnvironment * m_pUsageEnvironment;
  118. MediaSession * m_pMediaSession;
  119. volatile char _eventLoopWatchVariable;
  120. CustomRTSPClient * m_pRtspClient;
  121. DummySink * m_pSink;
  122. MediaSubsession * fSubsession;
  123. char * fStreamId;
  124. static void continueAfterDESCRIBE(RTSPClient * rtspClient, int resultCode, char * resultString);
  125. static void continueAfterSETUP(RTSPClient * rtspClient, int resultCode, char * resultString);
  126. static void continueAfterPLAY(RTSPClient * rtspClient, int resultCode, char * resultString);
  127. // Other event handler functions:
  128. static void subsessionAfterPlaying(void * clientData);
  129. // called when a stream's subsession (e.g., audio or video substream) ends
  130. static void subsessionByeHandler(void * clientData); //, char const* reason );
  131. // called when a RTCP "BYE" is received for a subsession
  132. static void streamTimerHandler(void * clientData);
  133. // The main streaming routine (for each "rtsp://" URL):
  134. void openURL(UsageEnvironment & env, const char * progName, const char * rtspURL);
  135. // Used to iterate through each stream's 'subsessions', setting up each one:
  136. void setupNextSubsession(RTSPClient * rtspClient);
  137. // Used to shut down and close a stream (including its "RTSPClient" object):
  138. void shutdownStream(RTSPClient * rtspClient, int exitCode = 1);
  139. pthread_t m_tEventLoopThread;
  140. void initMembers(void);
  141. bool isEventLoopThread(void); // called from a live555 callback
  142. protected:
  143. void m_fThreadProc(void);
  144. public:
  145. static void sessionAfterPlaying(void * clientData);
  146. virtual bool _open(CSTR URI, bool ShowDeviceMenu = false, float framerate = -1.0f, int inputPin = -1, int width = -1, int height = -1, int s_width = -1, int s_height = -1);
  147. virtual bool _close(void);
  148. virtual bool _stop(void);
  149. bool isFinished(void);
  150. MS_WORK_MODE getWM(void) const
  151. {
  152. return m_eWM;
  153. }
  154. void setWM(int M)
  155. {
  156. m_eWM = (MS_WORK_MODE) M;
  157. }
  158. MediaStateStatus getStatus(void);
  159. explicit videoLIVE555Source(void * opaque = NULL);
  160. explicit videoLIVE555Source(CSTR URI, void * opaque = NULL, MS_WORK_MODE mode = MS_MODE_DECODE_FRAMES);
  161. virtual ~videoLIVE555Source();
  162. bool setSize(int Width, int Height);
  163. bool setProportionalByWidth(int Width);
  164. bool setProportionalByHeight(int Height);
  165. bool setFPS(int);
  166. bool setName(CSTR Name);
  167. bool setFrameAspectRatio(FrameAspectRatio AspectRatio);
  168. void consumeFrame(cv::Mat * frm);
  169. MediaSession * getMS(void)
  170. {
  171. return m_pMediaSession;
  172. }
  173. };
  174. #endif //!LRF_PROXY_ONLY
  175. #endif