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.

194 lines
8.1 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. virtual ~CustomRTSPClient();
  44. private:
  45. CustomRTSPClient( videoLIVE555Source *session,
  46. UsageEnvironment& env,
  47. char const* rtspURL,
  48. int verbosityLevel,
  49. char const* applicationName,
  50. portNumBits tunnelOverHTTPPortNum );
  51. };
  52. class DummySink: public MediaSink {
  53. public:
  54. static DummySink* createNew( videoLIVE555Source *session, UsageEnvironment& env, MediaSubsession& subsession, // identifies the kind of data that's being received
  55. char const* streamId = NULL, int w = -1, int h = -1, int ww = -1, int hh = -1 ); // identifies the stream itself (optional)
  56. virtual ~DummySink();
  57. private:
  58. basicLogCollection * m_pLog;
  59. AVCodecID m_eCodecProbe;
  60. cv::Mat * m_pRGB;
  61. int m_nInWidth;
  62. int m_nInHeight;
  63. int m_nOutWidth;
  64. int m_nOutHeight;
  65. DummySink( videoLIVE555Source *session, UsageEnvironment& env, MediaSubsession& subsession, char const* streamId, int w, int h, int ww, int hh );
  66. // called only by "createNew()"
  67. static void afterGettingFrame( void* clientData, unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds );
  68. void afterGettingFrame( unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds );
  69. //void saveFrameToFile()
  70. private:
  71. // redefined virtual functions:
  72. virtual Boolean continuePlaying();
  73. public:
  74. void setSprop( u_int8_t const* prop, unsigned size );
  75. private:
  76. videoLIVE555Source * m_pSession;
  77. uint8_t * m_pReceiveBuffer;
  78. uint8_t * m_pReceiveBufferAV;
  79. MediaSubsession & m_sSubSession;
  80. char * m_pStreamId;
  81. H264or5VideoFileSink * qtOut;
  82. FILE * m_pOutFile[2];
  83. uint8_t m_nCurrentOutFile;
  84. 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. 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, char const* progName, char const* 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 { return m_eWM; };
  145. void setWM( int M ){ m_eWM = (MS_WORK_MODE) M; };
  146. MediaStateStatus getStatus( void );
  147. explicit videoLIVE555Source( void *opaque=NULL );
  148. explicit videoLIVE555Source( CSTR URI, void *opaque=NULL, MS_WORK_MODE mode = MS_MODE_DECODE_FRAMES );
  149. virtual ~videoLIVE555Source();
  150. bool setSize( int Width,
  151. int Height );
  152. bool setProportionalByWidth( int Width );
  153. bool setProportionalByHeight( int Height );
  154. bool setFPS( int );
  155. bool setName( CSTR Name );
  156. bool setFrameAspectRatio( FrameAspectRatio AspectRatio );
  157. void consumeFrame( cv::Mat *frm );
  158. MediaSession * getMS( void ){ return m_pMediaSession; };
  159. };
  160. #endif