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.

180 lines
7.2 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* session;
  83. private:
  84. u_int8_t* fReceiveBuffer;
  85. u_int8_t* fReceiveBufferAV;
  86. MediaSubsession& fSubsession;
  87. char* fStreamId;
  88. private:
  89. AVCodec *codec;
  90. AVCodecContext *codecContext;
  91. int frameIndex;
  92. int got_picture;
  93. int len;
  94. AVFrame *frame;
  95. AVFrame *rgbFrame;
  96. SwsContext* swsContext;
  97. uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  98. char buf[1024];
  99. AVPacket avpkt;
  100. };
  101. class videoLIVE555Source : public basicMediaSource, public basicThread {
  102. private:
  103. basicLogCollection * m_pLog;
  104. void * m_pOpaque;
  105. int m_nInWidth;
  106. int m_nInHeight;
  107. int m_nOutWidth;
  108. int m_nOutHeight;
  109. StreamClientState streamClientState;
  110. virtual Boolean continuePlaying();
  111. uint8_t * fReceiveBuffer;
  112. TaskScheduler * m_pScheduler;
  113. UsageEnvironment * m_pUsageEnvironment;
  114. char _eventLoopWatchVariable;
  115. CustomRTSPClient * m_pRtspClient;
  116. DummySink * m_pSink;
  117. MediaSubsession * fSubsession;
  118. char * fStreamId;
  119. static void continueAfterDESCRIBE( RTSPClient* rtspClient, int resultCode, char* resultString);
  120. static void continueAfterSETUP( RTSPClient* rtspClient, int resultCode, char* resultString);
  121. static void continueAfterPLAY( RTSPClient* rtspClient, int resultCode, char* resultString);
  122. // Other event handler functions:
  123. static void subsessionAfterPlaying( void* clientData );
  124. // called when a stream's subsession (e.g., audio or video substream) ends
  125. static void subsessionByeHandler( void* clientData );//, char const* reason );
  126. // called when a RTCP "BYE" is received for a subsession
  127. static void streamTimerHandler( void* clientData );
  128. // The main streaming routine (for each "rtsp://" URL):
  129. void openURL(UsageEnvironment& env, char const* progName, char const* rtspURL);
  130. // Used to iterate through each stream's 'subsessions', setting up each one:
  131. void setupNextSubsession( RTSPClient* rtspClient );
  132. // Used to shut down and close a stream (including its "RTSPClient" object):
  133. void shutdownStream( RTSPClient* rtspClient, int exitCode = 1);
  134. protected:
  135. void m_fThreadProc( void );
  136. public:
  137. 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 );
  138. virtual bool _close( void );
  139. virtual bool _stop( void );
  140. bool isFinished( void );
  141. MediaStateStatus getStatus( void );
  142. videoLIVE555Source( void *opaque=NULL );
  143. videoLIVE555Source( CSTR URI, void *opaque=NULL );
  144. virtual ~videoLIVE555Source();
  145. bool setSize( int Width,
  146. int Height );
  147. bool setProportionalByWidth( int Width );
  148. bool setProportionalByHeight( int Height );
  149. bool setFPS( int );
  150. bool setName( CSTR Name );
  151. bool setFrameAspectRatio( FrameAspectRatio AspectRatio );
  152. void consumeFrame( cv::Mat *frm );
  153. };
  154. #endif