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.

202 lines
8.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __VIDEOLIVE555AVLIBSOURCE_H_INCL__
  2. #define __VIDEOLIVE555AVLIBSOURCE_H_INCL__
  3. #include "../../../components/base/basicLIBAV.h"
  4. #include "../../../components/base/basicMediaSource.h"
  5. #include "../../../components/base/basicSettings.h"
  6. #include "../../../components/base/basicThread.h"
  7. #include "../../../components/base/basicLogCollection.h"
  8. #include "../../../alg/generic/md5.h"
  9. #include <liveMedia/liveMedia.hh>
  10. #include <BasicUsageEnvironment.hh>
  11. #define RTSP_CLIENT_VERBOSITY_LEVEL 1
  12. #define INBUF_SIZE 40960
  13. #define AUDIO_INBUF_SIZE 20480
  14. #define AUDIO_REFILL_THRESH 4096
  15. #define DUMMY_SINK_RECEIVE_BUFFER_SIZE 400000
  16. #define REQUEST_STREAMING_OVER_TCP True
  17. class videoLIVE555Source;
  18. class StreamClientState{
  19. public:
  20. StreamClientState();
  21. virtual ~StreamClientState();
  22. public:
  23. MediaSubsessionIterator * iter;
  24. MediaSession * session;
  25. MediaSubsession * subsession;
  26. TaskToken streamTimerTask;
  27. double duration;
  28. };
  29. class CustomRTSPClient: public RTSPClient {
  30. private:
  31. videoLIVE555Source * session;
  32. StreamClientState streamClientState;
  33. public:
  34. static CustomRTSPClient * createNew(
  35. videoLIVE555Source *session,
  36. UsageEnvironment& env,
  37. char const* rtspURL,
  38. int verbosityLevel = 0,
  39. char const* applicationName = NULL,
  40. portNumBits tunnelOverHTTPPortNum = 0);
  41. StreamClientState& getStreamClientState() { return this->streamClientState; }
  42. videoLIVE555Source * getSession() { return this->session; }
  43. protected:
  44. CustomRTSPClient( videoLIVE555Source *session,
  45. UsageEnvironment& env,
  46. char const* rtspURL,
  47. int verbosityLevel,
  48. char const* applicationName,
  49. portNumBits tunnelOverHTTPPortNum );
  50. virtual ~CustomRTSPClient();
  51. };
  52. class DummySink: public MediaSink {
  53. public:
  54. static DummySink* createNew( videoLIVE555Source *session,
  55. UsageEnvironment& env,
  56. MediaSubsession& subsession, // identifies the kind of data that's being received
  57. char const* streamId = NULL, int w = -1, int h = -1, int ww = -1, int hh = -1 ); // identifies the stream itself (optional)
  58. virtual ~DummySink();
  59. private:
  60. basicLogCollection * m_pLog;
  61. AVCodecID m_eCodecProbe;
  62. cv::Mat * m_pRGB;
  63. int m_nInWidth;
  64. int m_nInHeight;
  65. int m_nOutWidth;
  66. int m_nOutHeight;
  67. DummySink( videoLIVE555Source *session, UsageEnvironment& env, MediaSubsession& subsession, char const* streamId, int w, int h, int ww, int hh );
  68. // called only by "createNew()"
  69. static void afterGettingFrame(void* clientData, unsigned frameSize,
  70. unsigned numTruncatedBytes,
  71. struct timeval presentationTime,
  72. unsigned durationInMicroseconds);
  73. void afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
  74. struct timeval presentationTime, unsigned durationInMicroseconds);
  75. //void saveFrameToFile()
  76. private:
  77. // redefined virtual functions:
  78. virtual Boolean continuePlaying();
  79. public:
  80. void setSprop( u_int8_t const* prop, unsigned size );
  81. private:
  82. videoLIVE555Source * m_pSession;
  83. private:
  84. uint8_t * m_pReceiveBuffer;
  85. uint8_t * m_pReceiveBufferAV;
  86. MediaSubsession & m_sSubSession;
  87. char * m_pStreamId;
  88. H264or5VideoFileSink * qtOut;
  89. FILE * m_pOutFile[2];
  90. uint8_t m_nCurrentOutFile;
  91. private:
  92. AVCodec * m_pCodec;
  93. AVCodecContext * m_pCodecContext;
  94. AVCodec * m_pOutCodec;
  95. AVCodecContext * m_pOutCodecContext;
  96. AVFormatContext * m_pOutFormatCtx;
  97. AVDictionary * m_pOutFormatOpts;
  98. AVStream * m_pOutStream;
  99. uint8_t * m_pH264;
  100. uint64_t m_iFrameIndex;
  101. AVFrame * m_pFrame;
  102. AVFrame * m_pRGBFrame;
  103. SwsContext * m_pSwsContext;
  104. uint8_t m_aInBuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  105. //char buf[1024];
  106. AVPacket m_sAvPkt;
  107. };
  108. class videoLIVE555Source : public basicMediaSource, public basicThread {
  109. private:
  110. basicLogCollection * m_pLog;
  111. void * m_pOpaque;
  112. bool m_bIsParamsSet;
  113. int m_nInWidth;
  114. int m_nInHeight;
  115. int m_nOutWidth;
  116. int m_nOutHeight;
  117. MS_WORK_MODE m_eWM;
  118. StreamClientState streamClientState;
  119. virtual Boolean continuePlaying();
  120. uint8_t * fReceiveBuffer;
  121. TaskScheduler * m_pScheduler;
  122. UsageEnvironment * m_pUsageEnvironment;
  123. MediaSession * m_pMediaSession;
  124. char _eventLoopWatchVariable;
  125. CustomRTSPClient * m_pRtspClient;
  126. DummySink * m_pSink;
  127. MediaSubsession * fSubsession;
  128. char * fStreamId;
  129. static void continueAfterDESCRIBE( RTSPClient* rtspClient, int resultCode, char* resultString);
  130. static void continueAfterSETUP( RTSPClient* rtspClient, int resultCode, char* resultString);
  131. static void continueAfterPLAY( RTSPClient* rtspClient, int resultCode, char* resultString);
  132. // Other event handler functions:
  133. static void subsessionAfterPlaying( void* clientData );
  134. // called when a stream's subsession (e.g., audio or video substream) ends
  135. static void subsessionByeHandler( void* clientData );//, char const* reason );
  136. // called when a RTCP "BYE" is received for a subsession
  137. static void streamTimerHandler( void* clientData );
  138. // The main streaming routine (for each "rtsp://" URL):
  139. void openURL(UsageEnvironment& env, char const* progName, char const* rtspURL);
  140. // Used to iterate through each stream's 'subsessions', setting up each one:
  141. void setupNextSubsession( RTSPClient* rtspClient );
  142. // Used to shut down and close a stream (including its "RTSPClient" object):
  143. void shutdownStream( RTSPClient* rtspClient, int exitCode = 1);
  144. protected:
  145. void m_fThreadProc( void );
  146. public:
  147. static void sessionAfterPlaying( void* clientData );
  148. 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 );
  149. virtual bool _close( void );
  150. virtual bool _stop( void );
  151. bool isFinished( void );
  152. MS_WORK_MODE getWM( void ) const { return m_eWM; };
  153. void setWM( int M ){ m_eWM = (MS_WORK_MODE) M; };
  154. MediaStateStatus getStatus( void );
  155. explicit videoLIVE555Source( void *opaque=NULL );
  156. explicit videoLIVE555Source( CSTR URI, void *opaque=NULL, MS_WORK_MODE mode = MS_MODE_DECODE_FRAMES );
  157. virtual ~videoLIVE555Source();
  158. bool setSize( int Width,
  159. int Height );
  160. bool setProportionalByWidth( int Width );
  161. bool setProportionalByHeight( int Height );
  162. bool setFPS( int );
  163. bool setName( CSTR Name );
  164. bool setFrameAspectRatio( FrameAspectRatio AspectRatio );
  165. void consumeFrame( cv::Mat *frm );
  166. MediaSession * getMS( void ){ return m_pMediaSession; };
  167. };
  168. #endif