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.

228 lines
6.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months 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. uint8_t m_aInBuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  97. // char buf[1024];
  98. AVPacket m_sAvPkt;
  99. };
  100. class videoLIVE555Source : public basicMediaSource, public basicThread
  101. {
  102. private:
  103. basicLogCollection * m_pLog;
  104. void * m_pOpaque;
  105. bool m_bIsParamsSet;
  106. int m_nInWidth;
  107. int m_nInHeight;
  108. int m_nOutWidth;
  109. int m_nOutHeight;
  110. MS_WORK_MODE m_eWM;
  111. StreamClientState streamClientState;
  112. virtual Boolean continuePlaying();
  113. uint8_t * fReceiveBuffer;
  114. TaskScheduler * m_pScheduler;
  115. UsageEnvironment * m_pUsageEnvironment;
  116. MediaSession * m_pMediaSession;
  117. volatile char _eventLoopWatchVariable;
  118. CustomRTSPClient * m_pRtspClient;
  119. DummySink * m_pSink;
  120. MediaSubsession * fSubsession;
  121. char * fStreamId;
  122. static void continueAfterDESCRIBE(RTSPClient * rtspClient, int resultCode, char * resultString);
  123. static void continueAfterSETUP(RTSPClient * rtspClient, int resultCode, char * resultString);
  124. static void continueAfterPLAY(RTSPClient * rtspClient, int resultCode, char * resultString);
  125. // Other event handler functions:
  126. static void subsessionAfterPlaying(void * clientData);
  127. // called when a stream's subsession (e.g., audio or video substream) ends
  128. static void subsessionByeHandler(void * clientData); //, char const* reason );
  129. // called when a RTCP "BYE" is received for a subsession
  130. static void streamTimerHandler(void * clientData);
  131. // The main streaming routine (for each "rtsp://" URL):
  132. void openURL(UsageEnvironment & env, const char * progName, const char * rtspURL);
  133. // Used to iterate through each stream's 'subsessions', setting up each one:
  134. void setupNextSubsession(RTSPClient * rtspClient);
  135. // Used to shut down and close a stream (including its "RTSPClient" object):
  136. void shutdownStream(RTSPClient * rtspClient, int exitCode = 1);
  137. protected:
  138. void m_fThreadProc(void);
  139. public:
  140. static void sessionAfterPlaying(void * clientData);
  141. 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);
  142. virtual bool _close(void);
  143. virtual bool _stop(void);
  144. bool isFinished(void);
  145. MS_WORK_MODE getWM(void) const
  146. {
  147. return m_eWM;
  148. }
  149. void setWM(int M)
  150. {
  151. m_eWM = (MS_WORK_MODE) M;
  152. }
  153. MediaStateStatus getStatus(void);
  154. explicit videoLIVE555Source(void * opaque = NULL);
  155. explicit videoLIVE555Source(CSTR URI, void * opaque = NULL, MS_WORK_MODE mode = MS_MODE_DECODE_FRAMES);
  156. virtual ~videoLIVE555Source();
  157. bool setSize(int Width, int Height);
  158. bool setProportionalByWidth(int Width);
  159. bool setProportionalByHeight(int Height);
  160. bool setFPS(int);
  161. bool setName(CSTR Name);
  162. bool setFrameAspectRatio(FrameAspectRatio AspectRatio);
  163. void consumeFrame(cv::Mat * frm);
  164. MediaSession * getMS(void)
  165. {
  166. return m_pMediaSession;
  167. }
  168. };
  169. #endif //!LRF_PROXY_ONLY
  170. #endif