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.

601 lines
25 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "videoLive555AVLibSource.h"
  2. StreamClientState::StreamClientState()
  3. : iter(NULL), session(NULL), subsession(NULL), streamTimerTask(NULL), duration(0.0) {
  4. };
  5. StreamClientState::~StreamClientState() {
  6. delete iter;
  7. if( session != NULL ){
  8. // We also need to delete "session", and unschedule "streamTimerTask" (if set)
  9. UsageEnvironment& env = session->envir(); // alias
  10. env.taskScheduler().unscheduleDelayedTask(streamTimerTask);
  11. Medium::close(session);
  12. };
  13. };
  14. CustomRTSPClient* CustomRTSPClient::createNew(
  15. videoLIVE555Source *session,
  16. UsageEnvironment& env,
  17. char const* rtspURL,
  18. int verbosityLevel,
  19. char const* applicationName,
  20. portNumBits tunnelOverHTTPPortNum) {
  21. return new CustomRTSPClient(session, env, rtspURL, verbosityLevel, applicationName, tunnelOverHTTPPortNum);
  22. };
  23. CustomRTSPClient::CustomRTSPClient(
  24. videoLIVE555Source *session,
  25. UsageEnvironment& env,
  26. char const* rtspURL,
  27. int verbosityLevel,
  28. char const* applicationName,
  29. portNumBits tunnelOverHTTPPortNum)
  30. : RTSPClient(env,rtspURL, verbosityLevel, applicationName, tunnelOverHTTPPortNum, -1) ,
  31. session(session){
  32. };
  33. CustomRTSPClient::~CustomRTSPClient() {
  34. };
  35. #define LOG( X ) m_pLog->Log( "LIVE555_SIN", this, X )
  36. #define MLOG( X ) ME->m_pLog->Log( "LIVE555_SIN", ME, X )
  37. #ifdef DEBUG
  38. #define LOG_ERROR( X ) {\
  39. char _tBfrErrMSG2w34[256];\
  40. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  41. m_pLog->Log( "LIVE555_SIN", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  42. }
  43. #else
  44. #define LOG_ERROR( X ) m_pLog->Log( "LIVE555_SIN", this, _LOG_ERROR, X );
  45. #endif
  46. DummySink* DummySink::createNew( videoLIVE555Source* session, UsageEnvironment& env, MediaSubsession& subsession, char const* streamId, int w, int h, int ww, int hh ) {
  47. return new DummySink( session, env, subsession, streamId, w, h, ww, hh );
  48. }
  49. DummySink::DummySink(videoLIVE555Source* session, UsageEnvironment& env, MediaSubsession& subsession, char const* streamId, int w, int h, int ww, int hh )
  50. : MediaSink(env),
  51. session(session),
  52. frameIndex(0),
  53. fSubsession(subsession) {
  54. m_pLog = basicLogCollection::getInstance();
  55. fStreamId = strDup(streamId);
  56. m_nInWidth = w;
  57. m_nInHeight = h;
  58. m_nOutWidth = ww;
  59. m_nOutHeight = hh;
  60. fReceiveBuffer = new u_int8_t[DUMMY_SINK_RECEIVE_BUFFER_SIZE];
  61. fReceiveBufferAV = new u_int8_t[DUMMY_SINK_RECEIVE_BUFFER_SIZE + 4];
  62. fReceiveBufferAV[0] = 0;
  63. fReceiveBufferAV[1] = 0;
  64. fReceiveBufferAV[2] = 0;
  65. fReceiveBufferAV[3] = 1;
  66. av_init_packet(&avpkt);
  67. avpkt.flags |= AV_PKT_FLAG_KEY;
  68. avpkt.pts = avpkt.dts = 0;
  69. memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  70. LOG( subsession.codecName() );
  71. m_eCodecProbe = AV_CODEC_ID_NONE;
  72. if( strcmp(fSubsession.codecName(),"H264") == 0 ){
  73. m_eCodecProbe = AV_CODEC_ID_H264;
  74. };
  75. if( strcmp(fSubsession.codecName(),"JPEG") == 0 ){
  76. m_eCodecProbe = AV_CODEC_ID_MJPEG;
  77. };
  78. if( strcmp(fSubsession.codecName(),"H265") == 0 ){
  79. m_eCodecProbe = AV_CODEC_ID_HEVC;
  80. };
  81. codec = avcodec_find_decoder( m_eCodecProbe );
  82. if (!codec) {
  83. LOG_ERROR( "codec not found!" );
  84. };
  85. codecContext = avcodec_alloc_context3(codec);
  86. frame = av_frame_alloc();
  87. rgbFrame = av_frame_alloc();
  88. //avpicture_alloc( ( AVPicture *) rgbFrame, AV_PIX_FMT_RGB24, 1920, 1080);
  89. avpicture_alloc( ( AVPicture *) rgbFrame, AV_PIX_FMT_BGR24, 1280, 720 );
  90. if (codec->capabilities & AV_CODEC_CAP_TRUNCATED) {
  91. codecContext->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
  92. }
  93. if ( ( fSubsession.videoWidth() > 0 ) && ( fSubsession.videoHeight() > 0 ) ) {
  94. m_nInWidth = fSubsession.videoWidth();
  95. m_nInHeight = fSubsession.videoHeight();
  96. };
  97. codecContext->width = m_nInWidth;
  98. codecContext->height = m_nInHeight;
  99. codecContext->pix_fmt = AV_PIX_FMT_YUV420P;
  100. if (avcodec_open2(codecContext,codec,NULL) < 0) {
  101. LOG_ERROR( "could not open codec" );
  102. }
  103. //this->swsContext = sws_getContext( codecContext->width, codecContext->height, codecContext->pix_fmt, 1920, 1080, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
  104. this->swsContext = sws_getContext( codecContext->width, codecContext->height, codecContext->pix_fmt, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
  105. m_pRGB = new cv::Mat( m_nOutHeight, m_nOutWidth, CV_8UC3, cv::Scalar( 255, 0, 255 ) );
  106. char tB[128];
  107. sprintf( tB, "Created Sink [%dx%d]->[1280x720]", codecContext->width,codecContext->height );
  108. LOG( tB );
  109. };
  110. DummySink::~DummySink() {
  111. delete[] fReceiveBuffer;
  112. delete [] fReceiveBufferAV;
  113. delete[] fStreamId;
  114. }
  115. void DummySink::afterGettingFrame(void* clientData, unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds ) {
  116. DummySink* sink = (DummySink*)clientData;
  117. sink->afterGettingFrame(frameSize, numTruncatedBytes, presentationTime, durationInMicroseconds);
  118. };
  119. void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned /*durationInMicroseconds*/) {
  120. // We've just received a frame of data. (Optionally) print out information about it:
  121. //if (strcmp(fSubsession.codecName(),"H264") == 0) {
  122. if( m_eCodecProbe != AV_CODEC_ID_NONE ) {
  123. avpkt.data = fReceiveBufferAV;
  124. avpkt.size = frameSize + 4;
  125. memcpy (fReceiveBufferAV + 4, fReceiveBuffer, frameSize);
  126. avpkt.data = fReceiveBufferAV;
  127. len = avcodec_decode_video2 (codecContext, frame, &got_picture, &avpkt);
  128. if (len < 0) {
  129. //LOG_ERROR( "Error while decoding frame " );// << frameIndex << "\n";
  130. } else if (got_picture) {
  131. //envir() << "->Picture decoded :" << frameIndex << "\n";
  132. //LOG( "Picture decoded" );
  133. sws_scale( this->swsContext, this->frame->data,this->frame->linesize, 0, this->codecContext->height, this->rgbFrame->data, this->rgbFrame->linesize );
  134. memcpy( m_pRGB->data, this->rgbFrame->data[0], this->rgbFrame->linesize[0] * m_nOutHeight );
  135. this->session->consumeFrame( m_pRGB );
  136. frameIndex ++;
  137. };
  138. } else{
  139. LOG("Codec Not Handled!");
  140. LOG( fSubsession.codecName() );
  141. };
  142. // Then continue, to request the next frame of data:
  143. continuePlaying();
  144. }
  145. Boolean DummySink::continuePlaying() {
  146. if (fSource == NULL) return False; // sanity check (should not happen)
  147. // Request the next frame of data from our input source. "afterGettingFrame()" will get called later, when it arrives:
  148. fSource->getNextFrame(fReceiveBuffer, DUMMY_SINK_RECEIVE_BUFFER_SIZE,
  149. afterGettingFrame, this,
  150. onSourceClosure, this);
  151. return True;
  152. }
  153. void DummySink::setSprop(u_int8_t const* prop, unsigned size) {
  154. uint8_t *buf;
  155. uint8_t *buf_start;
  156. buf = (uint8_t *)malloc(1000);
  157. buf_start = buf + 4;
  158. avpkt.data = buf;
  159. avpkt.data[0] = 0;
  160. avpkt.data[1] = 0;
  161. avpkt.data[2] = 0;
  162. avpkt.data[3] = 1;
  163. memcpy (buf_start, prop, size);
  164. avpkt.size = size + 4;
  165. len = avcodec_decode_video2 (codecContext, frame, &got_picture, &avpkt);
  166. if (len < 0) {
  167. //envir() << "Error while decoding frame" << frame;
  168. LOG_ERROR( "Error while decoding frame" );
  169. // exit(6);
  170. };
  171. LOG( "after setSprop" );
  172. // exit (111);
  173. };
  174. #undef LOG_ERROR
  175. #undef MLOG
  176. #undef LOG
  177. #define LOG( X ) m_pLog->Log( "LIVE555_SRC", this, X )
  178. #define MLOG( X ) ME->m_pLog->Log( "LIVE555_SRC", ME, X )
  179. #ifdef DEBUG
  180. #define LOG_ERROR( X ) {\
  181. char _tBfrErrMSG2w34[256];\
  182. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  183. m_pLog->Log( "LIVE555_SRC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  184. }
  185. #else
  186. #define LOG_ERROR( X ) m_pLog->Log( "LIVE555_SRC", this, _LOG_ERROR, X );
  187. #endif
  188. videoLIVE555Source::videoLIVE555Source( void *opaque ):
  189. m_pScheduler(BasicTaskScheduler::createNew()),
  190. m_pUsageEnvironment(BasicUsageEnvironment::createNew(*m_pScheduler)),
  191. basicMediaSource(),
  192. //MediaSink(*m_pUsageEnvironment),
  193. basicThread(),
  194. m_pLog(basicLogCollection::getInstance()),
  195. m_pOpaque(opaque){
  196. m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = -1;
  197. if( !initLibAV() ) return;
  198. };
  199. videoLIVE555Source::videoLIVE555Source( CSTR URI, void *opaque ):
  200. m_pScheduler(BasicTaskScheduler::createNew()),
  201. m_pUsageEnvironment(BasicUsageEnvironment::createNew(*m_pScheduler)),
  202. basicMediaSource(),
  203. //MediaSink(*m_pUsageEnvironment),
  204. basicThread(),
  205. m_pLog(basicLogCollection::getInstance()),
  206. m_pOpaque(opaque){
  207. m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = -1;
  208. if( !initLibAV() ) return;
  209. _open( URI );
  210. };
  211. videoLIVE555Source::~videoLIVE555Source(){
  212. _stop();
  213. //if ( m_pSink ) delete( m_pSink );
  214. };
  215. void videoLIVE555Source::m_fThreadProc( void ){
  216. _eventLoopWatchVariable = 0;
  217. m_pScheduler->doEventLoop( &_eventLoopWatchVariable );
  218. LOG( "Rtsp event finished" );
  219. m_pUsageEnvironment->reclaim();
  220. m_pUsageEnvironment = NULL;
  221. delete m_pScheduler;
  222. m_pScheduler = NULL;
  223. };
  224. Boolean videoLIVE555Source::continuePlaying(){
  225. return False;
  226. };
  227. void videoLIVE555Source::continueAfterDESCRIBE( RTSPClient* rtspClient, int resultCode, char* resultString){
  228. CustomRTSPClient * MM = (CustomRTSPClient *) rtspClient;
  229. videoLIVE555Source * ME = (videoLIVE555Source *) MM->getSession();
  230. MLOG("After Describe EVT");
  231. do {
  232. UsageEnvironment& env = rtspClient->envir(); // alias
  233. StreamClientState& scs = MM->getStreamClientState(); // alias
  234. if (resultCode != 0) {
  235. MLOG( "Failed to get a SDP description: ");
  236. MLOG( resultString );
  237. delete[] resultString;
  238. break;
  239. };
  240. char* const sdpDescription = resultString;
  241. MLOG( "Got a SDP description:" );
  242. MLOG( sdpDescription );
  243. // Create a media session object from this SDP description:
  244. scs.session = MediaSession::createNew(env, sdpDescription);
  245. delete[] sdpDescription; // because we don't need it anymore
  246. if (scs.session == NULL) {
  247. MLOG( "Failed to create a MediaSession object from the SDP description:" );
  248. MLOG( env.getResultMsg() );
  249. break;
  250. } else if (!scs.session->hasSubsessions()) {
  251. MLOG( "This session has no media subsessions (i.e., no \"m=\" lines)" );
  252. break;
  253. };
  254. // Then, create and set up our data source objects for the session. We do this by iterating over the session's 'subsessions',
  255. // calling "MediaSubsession::initiate()", and then sending a RTSP "SETUP" command, on each one.
  256. // (Each 'subsession' will have its own data source.)
  257. scs.iter = new MediaSubsessionIterator(*scs.session);
  258. ME->setupNextSubsession(rtspClient);
  259. return;
  260. } while (0);
  261. // An unrecoverable error occurred with this stream.
  262. ME->shutdownStream( rtspClient );
  263. };
  264. void videoLIVE555Source::continueAfterSETUP( RTSPClient* rtspClient, int resultCode, char* resultString){
  265. CustomRTSPClient * MM = (CustomRTSPClient *) rtspClient;
  266. videoLIVE555Source * ME = (videoLIVE555Source *) MM->getSession();
  267. MLOG("After Setup EVT");
  268. do {
  269. UsageEnvironment& env = rtspClient->envir(); // alias
  270. StreamClientState& streamClientState = MM->getStreamClientState(); // alias
  271. if( resultCode != 0 ){
  272. MLOG( "Failed to set up the subsession:");
  273. MLOG( streamClientState.subsession->mediumName() );
  274. MLOG( streamClientState.subsession->codecName() );
  275. MLOG( resultString );
  276. break;
  277. };
  278. MLOG( "Set up the subsession" );
  279. MLOG( streamClientState.subsession->mediumName() );
  280. MLOG( streamClientState.subsession->codecName() );
  281. if (streamClientState.subsession->rtcpIsMuxed()) {
  282. char tB[128];
  283. sprintf( tB, "client port %d", streamClientState.subsession->clientPortNum() );
  284. MLOG( tB );
  285. } else {
  286. char tB[128];
  287. sprintf( tB, "client ports %d-%d", streamClientState.subsession->clientPortNum(), streamClientState.subsession->clientPortNum()+1 );
  288. MLOG( tB );
  289. };
  290. const char *sprop = streamClientState.subsession->fmtp_spropparametersets();
  291. uint8_t const* sps = NULL;
  292. unsigned spsSize = 0;
  293. uint8_t const* pps = NULL;
  294. unsigned ppsSize = 0;
  295. if (sprop != NULL) {
  296. unsigned numSPropRecords;
  297. SPropRecord* sPropRecords = parseSPropParameterSets(sprop, numSPropRecords);
  298. for (unsigned i = 0; i < numSPropRecords; ++i) {
  299. if (sPropRecords[i].sPropLength == 0) continue; // bad data
  300. u_int8_t nal_unit_type = (sPropRecords[i].sPropBytes[0])&0x1F;
  301. if (nal_unit_type == 7/*SPS*/) {
  302. sps = sPropRecords[i].sPropBytes;
  303. spsSize = sPropRecords[i].sPropLength;
  304. } else if (nal_unit_type == 8/*PPS*/) {
  305. pps = sPropRecords[i].sPropBytes;
  306. ppsSize = sPropRecords[i].sPropLength;
  307. }
  308. }
  309. }
  310. // Having successfully setup the subsession, create a data sink for it, and call "startPlaying()" on it.
  311. // (This will prepare the data sink to receive data; the actual flow of data from the client won't start happening until later,
  312. // after we've sent a RTSP "PLAY" command.)
  313. streamClientState.subsession->sink = DummySink::createNew(ME, env, *streamClientState.subsession, rtspClient->url(), ME->m_nInWidth, ME->m_nInHeight, ME->m_nOutWidth, ME->m_nOutHeight );
  314. // perhaps use your own custom "MediaSink" subclass instead
  315. if (streamClientState.subsession->sink == NULL) {
  316. MLOG( "Failed to create a data sink for the subsession" );
  317. MLOG( streamClientState.subsession->mediumName() );
  318. MLOG( streamClientState.subsession->codecName() );
  319. MLOG( env.getResultMsg() );
  320. break;
  321. };
  322. MLOG( "Created a data sink for the subsession" );
  323. MLOG( streamClientState.subsession->mediumName() );
  324. MLOG( streamClientState.subsession->codecName() );
  325. streamClientState.subsession->miscPtr = rtspClient; // a hack to let subsession handler functions get the "RTSPClient" from the subsession
  326. if (sps != NULL) {
  327. ((DummySink *)streamClientState.subsession->sink)->setSprop(sps, spsSize);
  328. }
  329. if (pps != NULL) {
  330. ((DummySink *)streamClientState.subsession->sink)->setSprop(pps, ppsSize);
  331. }
  332. streamClientState.subsession->sink->startPlaying(*(streamClientState.subsession->readSource()),
  333. subsessionAfterPlaying, streamClientState.subsession);
  334. // Also set a handler to be called if a RTCP "BYE" arrives for this subsession:
  335. if (streamClientState.subsession->rtcpInstance() != NULL) {
  336. streamClientState.subsession->rtcpInstance()->setByeHandler( subsessionByeHandler, streamClientState.subsession );
  337. }
  338. } while (0);
  339. delete[] resultString;
  340. // Set up the next subsession, if any:
  341. ME->setupNextSubsession(rtspClient);
  342. };
  343. void videoLIVE555Source::continueAfterPLAY( RTSPClient* rtspClient, int resultCode, char* resultString){
  344. Boolean success = False;
  345. CustomRTSPClient * MM = (CustomRTSPClient *) rtspClient;
  346. videoLIVE555Source * ME = (videoLIVE555Source *) MM->getSession();
  347. MLOG("After Play EVT");
  348. do {
  349. UsageEnvironment& env = rtspClient->envir(); // alias
  350. StreamClientState& scs = ((CustomRTSPClient*)rtspClient)->getStreamClientState(); // alias
  351. if (resultCode != 0) {
  352. MLOG( "Failed to start playing session: " );
  353. MLOG( resultString );
  354. break;
  355. };
  356. // Set a timer to be handled at the end of the stream's expected duration (if the stream does not already signal its end
  357. // using a RTCP "BYE"). This is optional. If, instead, you want to keep the stream active - e.g., so you can later
  358. // 'seek' back within it and do another RTSP "PLAY" - then you can omit this code.
  359. // (Alternatively, if you don't want to receive the entire stream, you could set this timer for some shorter value.)
  360. if (scs.duration > 0) {
  361. unsigned const delaySlop = 2; // number of seconds extra to delay, after the stream's expected duration. (This is optional.)
  362. scs.duration += delaySlop;
  363. unsigned uSecsToDelay = (unsigned)(scs.duration*1000000);
  364. scs.streamTimerTask = env.taskScheduler().scheduleDelayedTask(uSecsToDelay, (TaskFunc*)streamTimerHandler, rtspClient);
  365. }
  366. MLOG( "Started playing session" );
  367. //if (scs.duration > 0) {
  368. // env << " (for up to " << scs.duration << " seconds)";
  369. // }
  370. success = True;
  371. } while (0);
  372. delete[] resultString;
  373. if (!success) {
  374. // An unrecoverable error occurred with this stream.
  375. ME->shutdownStream(rtspClient);
  376. };
  377. };
  378. // Other event handler functions:
  379. // called when a stream's subsession (e.g., audio or video substream) ends
  380. void videoLIVE555Source::subsessionAfterPlaying( void* clientData ){
  381. MediaSubsession* subsession = (MediaSubsession*)clientData;
  382. RTSPClient* rtspClient = (RTSPClient*)(subsession->miscPtr);
  383. CustomRTSPClient * MM = (CustomRTSPClient *) rtspClient;
  384. videoLIVE555Source * ME = (videoLIVE555Source *) MM->getSession();
  385. // Begin by closing this subsession's stream:
  386. Medium::close(subsession->sink);
  387. subsession->sink = NULL;
  388. // Next, check whether *all* subsessions' streams have now been closed:
  389. MediaSession& session = subsession->parentSession();
  390. MediaSubsessionIterator iter(session);
  391. while ((subsession = iter.next()) != NULL) {
  392. if (subsession->sink != NULL) return; // this subsession is still active
  393. }
  394. // All subsessions' streams have now been closed, so shutdown the client:
  395. ME->shutdownStream(rtspClient);
  396. };
  397. // called when a RTCP "BYE" is received for a subsession
  398. void videoLIVE555Source::subsessionByeHandler( void* clientData ){//, char const* reason ){
  399. MediaSubsession* subsession = (MediaSubsession*)clientData;
  400. RTSPClient* rtspClient = (RTSPClient*)subsession->miscPtr;
  401. CustomRTSPClient * MM = (CustomRTSPClient *) rtspClient;
  402. videoLIVE555Source * ME = (videoLIVE555Source *) MM->getSession();
  403. UsageEnvironment& env = rtspClient->envir(); // alias
  404. MLOG( "Received RTCP \"BYE\" on subsession" );
  405. MLOG( subsession->mediumName() );
  406. MLOG( subsession->codecName() );
  407. // Now act as if the subsession had closed:
  408. subsessionAfterPlaying(subsession);
  409. };
  410. void videoLIVE555Source::streamTimerHandler( void* clientData ){
  411. CustomRTSPClient* rtspClient = (CustomRTSPClient*)clientData;
  412. StreamClientState& scs = rtspClient->getStreamClientState(); // alias
  413. videoLIVE555Source * ME = (videoLIVE555Source *) rtspClient->getSession();
  414. scs.streamTimerTask = NULL;
  415. // Shut down the stream:
  416. ME->shutdownStream(rtspClient);
  417. };
  418. // The main streaming routine (for each "rtsp://" URL):
  419. void videoLIVE555Source::openURL(UsageEnvironment& env, char const* progName, char const* rtspURL){
  420. LOG("openURL Called!");
  421. };
  422. // Used to iterate through each stream's 'subsessions', setting up each one:
  423. void videoLIVE555Source::setupNextSubsession(RTSPClient* rtspClient){
  424. CustomRTSPClient * MM = (CustomRTSPClient *) rtspClient;
  425. videoLIVE555Source * ME = (videoLIVE555Source *) MM->getSession();
  426. MLOG("Next Subsession EVT");
  427. UsageEnvironment& env = rtspClient->envir(); // alias
  428. StreamClientState& scs = MM->getStreamClientState(); // alias
  429. scs.subsession = scs.iter->next();
  430. if (scs.subsession != NULL) {
  431. if (!scs.subsession->initiate()) {
  432. MLOG( "Failed to initiate the subsession:" );
  433. MLOG( scs.subsession->mediumName() );
  434. MLOG( scs.subsession->codecName() );
  435. MLOG( env.getResultMsg() );
  436. setupNextSubsession(rtspClient); // give up on this subsession; go to the next one
  437. } else {
  438. MLOG( "Initiated the subsession");
  439. MLOG( scs.subsession->mediumName() );
  440. MLOG( scs.subsession->codecName() );
  441. if ( strstr( scs.subsession->mediumName(),"video") != NULL ) {
  442. if (scs.subsession->rtcpIsMuxed()) {
  443. char tB[64];
  444. sprintf( tB, "client port %d", scs.subsession->clientPortNum() );
  445. MLOG( tB );
  446. } else {
  447. char tB[64];
  448. sprintf( tB, "client ports %d-%d", scs.subsession->clientPortNum(), scs.subsession->clientPortNum()+1 );
  449. MLOG( tB );
  450. };
  451. // Continue setting up this subsession, by sending a RTSP "SETUP" command:
  452. rtspClient->sendSetupCommand(*scs.subsession, continueAfterSETUP, False, REQUEST_STREAMING_OVER_TCP);
  453. } else {
  454. //Skip all Not Video subsession go to the next one
  455. setupNextSubsession(rtspClient);
  456. };
  457. };
  458. return;
  459. };
  460. // We've finished setting up all of the subsessions. Now, send a RTSP "PLAY" command to start the streaming:
  461. if (scs.session->absStartTime() != NULL) {
  462. // Special case: The stream is indexed by 'absolute' time, so send an appropriate "PLAY" command:
  463. rtspClient->sendPlayCommand(*scs.session, continueAfterPLAY, scs.session->absStartTime(), scs.session->absEndTime());
  464. } else {
  465. scs.duration = scs.session->playEndTime() - scs.session->playStartTime();
  466. rtspClient->sendPlayCommand(*scs.session, continueAfterPLAY);
  467. };
  468. };
  469. // Used to shut down and close a stream (including its "RTSPClient" object):
  470. void videoLIVE555Source::shutdownStream(RTSPClient* rtspClient, int exitCode ){
  471. CustomRTSPClient * MM = (CustomRTSPClient *) rtspClient;
  472. videoLIVE555Source * ME = (videoLIVE555Source *) MM->getSession();
  473. MLOG("Shutdown EVT");
  474. };
  475. // https://chowdera.com/2021/01/20210102084512591N.html
  476. // https://github.com/drriguz/Kamera
  477. bool videoLIVE555Source::_open( CSTR URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, int s_width, int s_height ){
  478. m_nInHeight = height;
  479. m_nInWidth = width;
  480. m_nOutHeight = s_height;
  481. m_nOutWidth = s_width;
  482. m_pRtspClient = CustomRTSPClient::createNew( this, *m_pUsageEnvironment, URI, RTSP_CLIENT_VERBOSITY_LEVEL, "ARGUS CLIENT" );
  483. if( m_pRtspClient == NULL) {
  484. char tB[256];
  485. sprintf( tB, "Failed to create a RTSP client for URL \"%s\":%s", URI, m_pUsageEnvironment->getResultMsg() );
  486. LOG_ERROR( tB );
  487. return false;
  488. };
  489. m_pRtspClient->sendDescribeCommand(continueAfterDESCRIBE);
  490. return init();
  491. };
  492. void videoLIVE555Source::consumeFrame( cv::Mat *frm ){
  493. if ( m_pMediaDestination == NULL ) return;
  494. if(!m_pMediaDestination->pushFrame( *frm, m_pOpaque )){
  495. m_pMediaDestination = NULL;
  496. };
  497. };
  498. bool videoLIVE555Source::_close( void ){
  499. return _stop();
  500. };
  501. bool videoLIVE555Source::_stop( void ){
  502. shutdownStream( m_pRtspClient );
  503. _eventLoopWatchVariable = 1;
  504. basicThread::waitThreadEnd( true );
  505. return true;
  506. };
  507. bool videoLIVE555Source::isFinished( void ){ return false;};
  508. MediaStateStatus videoLIVE555Source::getStatus( void ){ return MS_UNDEFINED; };
  509. bool videoLIVE555Source::setSize( int Width, int Height ){ return false; };
  510. bool videoLIVE555Source::setProportionalByWidth( int Width ){ return false; };
  511. bool videoLIVE555Source::setProportionalByHeight( int Height ){ return false; };
  512. bool videoLIVE555Source::setFPS( int ){ return false; };
  513. bool videoLIVE555Source::setName( CSTR Name ){ return false; };
  514. bool videoLIVE555Source::setFrameAspectRatio( FrameAspectRatio AspectRatio ){ return false; };
  515. #undef MLOG
  516. #undef LOG
  517. #undef LOG_ERROR