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.

227 lines
6.4 KiB

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