@ -60,151 +60,540 @@ DummySink* DummySink::createNew( videoLIVE555Source* session, UsageEnvironment&
return new DummySink ( session , env , subsession , streamId , w , h , ww , hh ) ;
}
DummySink : : DummySink ( videoLIVE555Source * session , UsageEnvironment & env , MediaSubsession & subsession , char const * streamId , int w , int h , int ww , int hh )
void someStaticFunc ( void * data ) {
} ;
DummySink : : DummySink ( videoLIVE555Source * session , UsageEnvironment & env , MediaSubsession & subsession , char const * streamId , int w , int h , int ww , int hh )
: MediaSink ( env ) ,
session ( session ) ,
frameIndex ( 0 ) ,
fSubsession ( subsession ) {
m_pS ession( session ) ,
m_iF rameIndex( 0 ) ,
m_sSubS ession( subsession ) {
m_pLog = basicLogCollection : : getInstance ( ) ;
fStreamId = strDup ( streamId ) ;
m_pStreamId = strDup ( streamId ) ;
m_nCurrentOutFile = 0 ;
m_pOutFile [ 0 ] = m_pOutFile [ 1 ] = NULL ;
m_nInWidth = w ;
m_nInHeight = h ;
m_nOutWidth = ww ;
m_nOutHeight = hh ;
fReceiveBuffer = new u_int8_t [ DUMMY_SINK_RECEIVE_BUFFER_SIZE ] ;
fReceiveBufferAV = new u_int8_t [ DUMMY_SINK_RECEIVE_BUFFER_SIZE + 4 ] ;
m_pReceiveBuffer = new uint8_t [ DUMMY_SINK_RECEIVE_BUFFER_SIZE ] ;
m_pReceiveBufferAV = new uint8_t [ DUMMY_SINK_RECEIVE_BUFFER_SIZE + 4 ] ;
m_pReceiveBufferAV [ 0 ] = 0 ;
m_pReceiveBufferAV [ 1 ] = 0 ;
m_pReceiveBufferAV [ 2 ] = 0 ;
m_pReceiveBufferAV [ 3 ] = 1 ;
fReceiveBufferAV [ 0 ] = 0 ;
fReceiveBufferAV [ 1 ] = 0 ;
fReceiveBufferAV [ 2 ] = 0 ;
fReceiveBufferAV [ 3 ] = 1 ;
av_init_packet ( & m_sAvPkt ) ;
m_sAvPkt . flags | = AV_PKT_FLAG_KEY ;
m_sAvPkt . pts = m_sAvPkt . dts = 0 ;
av_init_packet ( & avpkt ) ;
avpkt . flags | = AV_PKT_FLAG_KEY ;
avpkt . pts = avpkt . dts = 0 ;
memset ( m_aInBuf + INBUF_SIZE , 0 , AV_INPUT_BUFFER_PADDING_SIZE ) ;
memset ( inbuf + INBUF_SIZE , 0 , AV_INPUT_BUFFER_PADDING_SIZE ) ;
LOG ( m_sSubSession . codecName ( ) ) ;
LOG ( subsession . codecName ( ) ) ;
m_eCodecProbe = AV_CODEC_ID_NONE ;
if ( strcmp ( fSubsession . codecName ( ) , " H264 " ) = = 0 ) {
if ( strcmp ( m_sSubSession . codecName ( ) , " H264 " ) = = 0 ) {
m_eCodecProbe = AV_CODEC_ID_H264 ;
} ;
if ( strcmp ( fSubs ession. codecName ( ) , " JPEG " ) = = 0 ) {
if ( strcmp ( m_sSubS ession . codecName ( ) , " JPEG " ) = = 0 ) {
m_eCodecProbe = AV_CODEC_ID_MJPEG ;
} ;
if ( strcmp ( fSubs ession. codecName ( ) , " H265 " ) = = 0 ) {
if ( strcmp ( m_sSubS ession . codecName ( ) , " H265 " ) = = 0 ) {
m_eCodecProbe = AV_CODEC_ID_HEVC ;
} ;
c odec = avcodec_find_decoder ( m_eCodecProbe ) ;
if ( ! codec ) {
m_pC odec = avcodec_find_decoder ( m_eCodecProbe ) ;
if ( m_pCodec = = NULL ) {
LOG_ERROR ( " codec not found! " ) ;
return ;
} ;
codecContext = avcodec_alloc_context3 ( codec ) ;
frame = av_frame_alloc ( ) ;
rgbFrame = av_frame_alloc ( ) ;
//avpicture_alloc( ( AVPicture *) rgbFrame, AV_PIX_FMT_RGB24, 1920, 1080);
avpicture_alloc ( ( AVPicture * ) rgbFrame , AV_PIX_FMT_BGR24 , 1280 , 720 ) ;
m_pCodecContext = avcodec_alloc_context3 ( m_pCodec ) ;
if ( c odec- > capabilities & AV_CODEC_CAP_TRUNCATED ) {
c odecContext- > flags | = AV_CODEC_FLAG_TRUNCATED ; // we do not send complete frames
}
if ( m_pCodec - > capabilities & AV_CODEC_CAP_TRUNCATED ) {
m_pCodecContext - > flags | = AV_CODEC_FLAG_TRUNCATED ; // we do not send complete frames
} ;
if ( ( fSubs ession. videoWidth ( ) > 0 ) & & ( fSubs ession. videoHeight ( ) > 0 ) ) {
m_nInWidth = fSubs ession. videoWidth ( ) ;
m_nInHeight = fSubs ession. videoHeight ( ) ;
if ( ( m_sSubS ession. videoWidth ( ) > 0 ) & & ( m_sSubS ession. videoHeight ( ) > 0 ) ) {
m_nInWidth = m_sSubS ession. videoWidth ( ) ;
m_nInHeight = m_sSubS ession. videoHeight ( ) ;
} ;
codecContext - > width = m_nInWidth ;
codecContext - > height = m_nInHeight ;
codecContext - > pix_fmt = AV_PIX_FMT_YUV420P ;
m_pCodecContext - > width = m_nInWidth ;
m_pCodecContext - > height = m_nInHeight ;
if ( avcodec_open2 ( codecContext , codec , NULL ) < 0 ) {
LOG_ERROR ( " could not open codec " ) ;
}
//Must be carefull! and get pix_fmt from stream
m_pCodecContext - > pix_fmt = AV_PIX_FMT_YUV420P ;
if ( avcodec_open2 ( m_pCodecContext , m_pCodec , NULL ) < 0 ) {
LOG_ERROR ( " could not open codec! " ) ;
return ;
} ;
if ( m_pSession - > getWM ( ) & MS_MODE_STREAM_PROXY ) {
m_pH264 = ( uint8_t * ) malloc ( 256 * 1024 ) ;
m_pOutFormatCtx = NULL ;
m_pOutFormatOpts = NULL ;
if ( avformat_alloc_output_context2 ( & m_pOutFormatCtx , NULL , NULL , " test.mp4 " ) < 0 ) {
LOG_ERROR ( " VideoFormat Context Memory Allocation Error " ) ;
//return false;
} else {
m_pOutFormatCtx - > oformat = av_guess_format ( " mp4 " , NULL , NULL ) ;
if ( m_pOutFormatCtx - > oformat ! = NULL ) {
av_dict_set ( & m_pOutFormatOpts , " brand " , " mp42 " , NULL ) ;
av_dict_set ( & m_pOutFormatOpts , " movflags " , " faststart " , NULL ) ;
m_pOutFormatCtx - > flags | = AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT ;
m_pOutStream = avformat_new_stream ( m_pOutFormatCtx , NULL ) ;
if ( m_pOutStream ! = NULL ) {
AVCodecContext * c = m_pOutStream - > codec ;
c - > codec_id = m_eCodecProbe ;
c - > codec_type = AVMEDIA_TYPE_VIDEO ;
/* put sample parameters */
c - > bit_rate = m_sSubSession . bandwidth ( ) ; // 400000;
/* resolution must be a multiple of two */
c - > width = m_sSubSession . videoWidth ( ) ;
c - > height = m_sSubSession . videoHeight ( ) ;
/* time base: this is the fundamental unit of time (in seconds) in terms
of which frame timestamps are represented . for fixed - fps content ,
timebase should be 1 / framerate and timestamp increments should be
identically 1. */
c - > time_base . den = m_sSubSession . videoFPS ( ) ;
c - > time_base . num = 1 ;
c - > gop_size = 12 ; /* emit one intra frame every twelve frames at most */
c - > pix_fmt = AV_PIX_FMT_YUV420P ;
if ( c - > codec_id = = AV_CODEC_ID_MPEG2VIDEO ) {
/* just for testing, we also add B frames */
c - > max_b_frames = 2 ;
}
if ( c - > codec_id = = AV_CODEC_ID_MPEG1VIDEO ) {
/* needed to avoid using macroblocks in which some coeffs overflow
this doesnt happen with normal video , it just happens here as the
motion of the chroma plane doesnt match the luma plane */
c - > mb_decision = 2 ;
}
// some formats want stream headers to be seperate
if ( ! strcmp ( m_pOutFormatCtx - > oformat - > name , " mp4 " ) | | ! strcmp ( m_pOutFormatCtx - > oformat - > name , " mov " ) | | ! strcmp ( m_pOutFormatCtx - > oformat - > name , " 3gp " ) ) {
c - > flags | = AV_CODEC_FLAG_GLOBAL_HEADER ;
LOG ( " Global Header Flag is Set! " ) ;
} ;
if ( avio_open ( & m_pOutFormatCtx - > pb , " test_99.mp4 " , AVIO_FLAG_WRITE ) < 0 ) {
LOG_ERROR ( " Could not open output file. " ) ;
//return false;
} ;
if ( avformat_write_header ( m_pOutFormatCtx , & m_pOutFormatOpts ) < 0 ) {
LOG_ERROR ( " Error occurred when writing header to output file. " ) ;
//return false;
} ;
}
} ;
} ;
/*
m_pOutCodec = = avcodec_find_encoder ( VIDEO_OUTPUT_CODEC ) ;
if ( m_pOutCodec = = NULL ) {
LOG_ERROR ( " output codec not found! " ) ;
//m_pSession->setWM( m_pSession->getWM() & ( ~ MS_MODE_STREAM_PROXY ) );
return ;
} ;
m_pOutCodecContext = avcodec_alloc_context3 ( m_pOutCodec ) ;
if ( m_pOutCodecContext = = NULL ) {
LOG_ERROR ( " VideoCodec Context Memory Allocation Error " ) ;
return ;
} else {
LOG ( " Codec Context3 Allocated " ) ;
} ;
if ( avformat_alloc_output_context2 ( & m_pOutFormatCtx , NULL , NULL , " test. " VIDEO_OUTPUT_CONTAINER ) < 0 ) {
LOG_ERROR ( " VideoFormat Context Memory Allocation Error " ) ;
return ;
} ;
m_pOutFormatCtx - > oformat = av_guess_format ( VIDEO_OUTPUT_CONTAINER , NULL , NULL ) ;
if ( m_pOutFormatCtx - > oformat = = NULL ) {
LOG_ERROR ( " VideoFormat Format Not Guessed! " ) ;
return ;
} else {
LOG ( " Video Container Format Guessed " ) ;
} ;
m_pOutVideoStr = avformat_new_stream ( m_pOutFormatCtx , m_pOutCodec ) ;
if ( m_pOutVideoStr = = NULL ) {
LOG_ERROR ( " VideoStream Not Created! " ) ;
} else {
LOG ( " VideoStream is Created " ) ;
} ;
m_pOutCodecContext - > codec_id = VIDEO_OUTPUT_CODEC ;
m_pOutCodecContext - > codec_type = AVMEDIA_TYPE_VIDEO ;
m_pOutCodecContext - > pix_fmt = VIDEO_OUTPUT_PIX_FMT ;
m_pOutCodecContext - > bit_rate = VIDEO_OUTPUT_BITRATE ;
//m_pOutCodecContext->bit_rate_tolerance = 1;
//m_pOutCodecContext->global_quality = 100;
//m_pOutCodecContext->compression_level = 2;
m_pOutCodecContext - > width = m_nWidth ;
m_pOutCodecContext - > height = m_nHeight ;
m_pOutCodecContext - > time_base . num = 1 ;
m_pOutCodecContext - > time_base . den = VIDEO_OUTPUT_FPS ;
m_pOutCodecContext - > gop_size = VIDEO_OUTPUT_GOP ; // emit one intra frame every ten frames
//m_pVideoState->m_pCodecCtx->qmin = 15;
//m_pVideoState->m_pCodecCtx->qmax = 30;
m_pOutCodecContext - > flags | = CODEC_FLAG_GLOBAL_HEADER ;
if ( m_pVideoState - > m_pCodecCtx - > codec_id = = AV_CODEC_ID_H264 ) {
//Reference for AvFormatContext options : https://ffmpeg.org/doxygen/2.8/movenc_8c_source.html
//Set format's privater options, to be passed to avformat_write_header()
LOG ( " Begin Options! " ) ;
int RetVal = 0 ;
//RetVal = av_dict_set(&m_pVideoState->m_pFormatOpts, "movflags", "faststart", 0);
//if( RetVal < 0 ){
// LOG_ERROR( "av_dict_set movflags!");
//};
//default brand is "isom", which fails on some devices
RetVal = av_dict_set ( & m_pVideoState - > m_pFormatOpts , " brand " , " mp42 " , 0 ) ;
if ( RetVal < 0 ) {
LOG_ERROR ( " av_dict_set brand! " ) ;
} ;
//m_pVideoState->m_pCodecCtx->max_b_frames = 1;
m_pVideoState - > m_pCodecCtx - > level = 31 ;
RetVal = av_opt_set ( m_pVideoState - > m_pCodecCtx - > priv_data , " crf " , " 12 " , 0 ) ;
if ( RetVal < 0 ) {
LOG_ERROR ( " av_opt_set crf " ) ;
} ;
RetVal = av_opt_set ( m_pVideoState - > m_pCodecCtx - > priv_data , " profile " , " main " , 0 ) ;
if ( RetVal < 0 ) {
LOG_ERROR ( " av_opt_set profile " ) ;
} ;
RetVal = av_opt_set ( m_pVideoState - > m_pCodecCtx - > priv_data , " preset " , " slow " , 0 ) ;
if ( RetVal < 0 ) {
LOG_ERROR ( " av_opt_set preset " ) ;
} ;
// Disable b-pyramid. CLI options for this is "-b-pyramid 0"
// Because Quicktime (ie. iOS) doesn't support this option
RetVal = av_opt_set ( m_pVideoState - > m_pCodecCtx - > priv_data , " b-pyramid " , " 0 " , 0 ) ;
if ( RetVal < 0 ) {
LOG_ERROR ( " av_opt_set b-pyramid " ) ;
} ;
LOG ( " End Options! " ) ;
} ;
//See up one page ->
avcodec_get_context_defaults3 ( m_pVideoState - > m_pVideoStr - > codec , m_pVideoState - > m_pVideoCodec ) ;
const AVRational dst_fps = { VIDEO_OUTPUT_FPS , 1 } ;
m_pVideoState - > m_pVideoStr - > codec - > width = m_nWidth ;
m_pVideoState - > m_pVideoStr - > codec - > height = m_nHeight ;
m_pVideoState - > m_pVideoStr - > codec - > pix_fmt = m_pVideoState - > m_pVideoCodec - > pix_fmts [ 0 ] ;
m_pVideoState - > m_pVideoStr - > codec - > time_base = m_pVideoState - > m_pVideoStr - > time_base = av_inv_q ( dst_fps ) ;
m_pVideoState - > m_pVideoStr - > r_frame_rate = m_pVideoState - > m_pVideoStr - > avg_frame_rate = dst_fps ;
if ( m_pVideoState - > m_pFormatCtx - > oformat - > flags & AVFMT_GLOBALHEADER ) m_pVideoState - > m_pVideoStr - > codec - > flags | = AV_CODEC_FLAG_GLOBAL_HEADER ;
//---->>
if ( avcodec_is_open ( m_pVideoState - > m_pCodecCtx ) = = 0 ) {
LOG ( " output codec context is closed, opening " ) ;
int ret = avcodec_open2 ( m_pVideoState - > m_pCodecCtx , m_pVideoState - > m_pVideoCodec , NULL ) ;
if ( ret < 0 ) {
LOG_ERROR ( " Error opening2 encoder " ) ;
char buffer [ 200 ] ;
sprintf ( buffer , " Error code: %i " , ret ) ;
LOG_ERROR ( buffer ) ;
return false ;
}
} else {
LOG ( " output codec context is opened " ) ;
} ;
*/
//https://github.com/rgaufman/live555/blob/master/testProgs/playCommon.cpp
//http://www.live555.com/liveMedia//doxygen/html/QuickTimeFileSink_8hh_source.html
//qtOut = QuickTimeFileSink::createNew(*env, *session, outFileName,
// fileSinkBufferSize,
// movieWidth, movieHeight,
// movieFPS,
// packetLossCompensate,
// syncStreams,
// generateHintTracks,
// generateMP4Format);
/* qtOut = QuickTimeFileSink::createNew( env, *session->getMS(), "TEST.mp4",
100000 ,
subsession . videoWidth ( ) , subsession . videoHeight ( ) ,
subsession . videoFPS ( ) ,
0 ,
0 ,
0 ,
1 ) ;
*/
qtOut = ( H264or5VideoFileSink * ) H264VideoFileSink : : createNew ( env , " TEST22.mp4 " ) ;
if ( qtOut ! = NULL ) {
LOG ( " Created QTFS ---> Test.mp4 " ) ;
//qtOut->startPlaying( NULL, NULL );
LOG ( " QTFS Started Play " ) ;
} ;
} else {
LOG ( " No Stream Proxy Mode! " ) ;
m_pOutCodec = NULL ;
} ;
if ( m_pSession - > getWM ( ) & MS_MODE_DECODE_FRAMES ) {
m_pFrame = av_frame_alloc ( ) ;
m_pRGBFrame = av_frame_alloc ( ) ;
//avpicture_alloc( ( AVPicture *) rgbFrame, AV_PIX_FMT_RGB24, 1920, 1080);
//avpicture_alloc( ( AVPicture *) rgbFrame, AV_PIX_FMT_BGR24, 1280, 720 );
avpicture_alloc ( ( AVPicture * ) m_pRGBFrame , AV_PIX_FMT_BGR24 , m_nOutWidth , m_nOutHeight ) ;
//this->swsContext = sws_getContext( codecContext->width, codecContext->height, codecContext->pix_fmt, 1920, 1080, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
m_pSwsContext = sws_getContext ( m_pCodecContext - > width , m_pCodecContext - > height , m_pCodecContext - > pix_fmt , m_nOutWidth , m_nOutHeight , AV_PIX_FMT_BGR24 , SWS_BICUBIC , NULL , NULL , NULL ) ;
m_pRGB = new cv : : Mat ( m_nOutHeight , m_nOutWidth , CV_8UC3 , cv : : Scalar ( 255 , 0 , 255 ) ) ;
} else {
m_pFrame = m_pRGBFrame = NULL ;
m_pSwsContext = NULL ;
m_pRGB = NULL ;
} ;
//this->swsContext = sws_getContext( codecContext->width, codecContext->height, codecContext->pix_fmt, 1920, 1080, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
this - > swsContext = sws_getContext ( codecContext - > width , codecContext - > height , codecContext - > pix_fmt , m_nOutWidth , m_nOutHeight , AV_PIX_FMT_BGR24 , SWS_BICUBIC , NULL , NULL , NULL ) ;
m_pRGB = new cv : : Mat ( m_nOutHeight , m_nOutWidth , CV_8UC3 , cv : : Scalar ( 255 , 0 , 255 ) ) ;
# ifdef DEBUG
char tB [ 128 ] ;
sprintf ( tB , " Created Sink [%dx%d]->[1280x720] " , codecContext - > width , codecContext - > height ) ;
sprintf ( tB , " Created Sink [%dx%d]->[1280x720] " , m_pCodecContext - > width , m_pCodecContext - > height ) ;
LOG ( tB ) ;
# endif
} ;
DummySink : : ~ DummySink ( ) {
delete [ ] fReceiveBuffer ;
delete [ ] fReceiveBufferAV ;
delete [ ] fStreamId ;
if ( m_pOutFormatCtx ! = NULL ) {
av_write_trailer ( m_pOutFormatCtx ) ;
avio_close ( m_pOutFormatCtx - > pb ) ;
} ;
if ( qtOut ! = NULL ) {
Medium : : close ( qtOut ) ;
qtOut = NULL ;
} ;
/*Medium::close(qtOut); qtOut = NULL;
Medium : : close ( aviOut ) ; aviOut = NULL ;
if ( session = = NULL ) return ;
MediaSubsessionIterator iter ( * session ) ;
MediaSubsession * subsession ;
while ( ( subsession = iter . next ( ) ) ! = NULL ) {
Medium : : close ( subsession - > sink ) ;
subsession - > sink = NULL ;
} */
if ( m_pOutFile [ 0 ] ! = NULL ) {
fflush ( m_pOutFile [ 0 ] ) ;
pclose ( m_pOutFile [ 0 ] ) ;
} ;
if ( m_pOutFile [ 1 ] ! = NULL ) {
fflush ( m_pOutFile [ 1 ] ) ;
pclose ( m_pOutFile [ 1 ] ) ;
} ;
if ( m_pFrame ) {
} ;
if ( m_pRGBFrame ) {
} ;
if ( m_pSwsContext ) {
} ;
if ( m_pCodecContext ) {
} ;
if ( m_pCodec ) {
} ;
delete [ ] m_pReceiveBuffer ;
delete [ ] m_pReceiveBufferAV ;
delete [ ] m_pStreamId ;
}
void DummySink : : afterGettingFrame ( void * clientData , unsigned frameSize , unsigned numTruncatedBytes , struct timeval presentationTime , unsigned durationInMicroseconds ) {
DummySink * sink = ( DummySink * ) clientData ;
sink - > afterGettingFrame ( frameSize , numTruncatedBytes , presentationTime , durationInMicroseconds ) ;
DummySink * sink = ( DummySink * ) clientData ;
sink - > afterGettingFrame ( frameSize , numTruncatedBytes , presentationTime , durationInMicroseconds ) ;
} ;
void DummySink : : afterGettingFrame ( unsigned frameSize , unsigned numTruncatedBytes , struct timeval presentationTime , unsigned /*durationInMicroseconds*/ ) {
void DummySink : : afterGettingFrame ( unsigned frameSize , unsigned numTruncatedBytes , struct timeval presentationTime , unsigned durationInMicroseconds ) {
// We've just received a frame of data. (Optionally) print out information about it:
//if (strcmp(fSubsession.codecName(),"H264") == 0) {
if ( m_eCodecProbe ! = AV_CODEC_ID_NONE ) {
avpkt . data = fReceiveBufferAV ;
avpkt . size = frameSize + 4 ;
memcpy ( fReceiveBufferAV + 4 , fReceiveBuffer , frameSize ) ;
avpkt . data = fReceiveBufferAV ;
len = avcodec_decode_video2 ( codecContext , frame , & got_picture , & avpkt ) ;
if ( len < 0 ) {
//LOG_ERROR( "Error while decoding frame " );// << frameIndex << "\n";
} else if ( got_picture ) {
//envir() << "->Picture decoded :" << frameIndex << "\n";
//LOG( "Picture decoded" );
sws_scale ( this - > swsContext , this - > frame - > data , this - > frame - > linesize , 0 , this - > codecContext - > height , this - > rgbFrame - > data , this - > rgbFrame - > linesize ) ;
memcpy ( m_pRGB - > data , this - > rgbFrame - > data [ 0 ] , this - > rgbFrame - > linesize [ 0 ] * m_nOutHeight ) ;
this - > session - > consumeFrame ( m_pRGB ) ;
frameIndex + + ;
int got_picture ;
memcpy ( m_pReceiveBufferAV + 4 , m_pReceiveBuffer , frameSize ) ;
av_init_packet ( & m_sAvPkt ) ;
m_sAvPkt . size = frameSize + 4 ;
m_sAvPkt . data = m_pReceiveBufferAV ;
if ( m_pSession - > getWM ( ) & MS_MODE_DECODE_FRAMES ) {
if ( avcodec_decode_video2 ( m_pCodecContext , m_pFrame , & got_picture , & m_sAvPkt ) < 0 ) {
//LOG_ERROR( "Error while decoding frame " );// << frameIndex << "\n";
} else if ( got_picture ) {
//envir() << "->Picture decoded :" << frameIndex << "\n";
//LOG( "Picture decoded" );
sws_scale ( m_pSwsContext , m_pFrame - > data , m_pFrame - > linesize , 0 , m_pCodecContext - > height , m_pRGBFrame - > data , m_pRGBFrame - > linesize ) ;
memcpy ( m_pRGB - > data , m_pRGBFrame - > data [ 0 ] , m_pRGBFrame - > linesize [ 0 ] * m_nOutHeight ) ;
m_pSession - > consumeFrame ( m_pRGB ) ;
m_iFrameIndex + + ;
} ;
} ;
if ( m_pSession - > getWM ( ) & MS_MODE_STREAM_PROXY ) {
if ( m_pSession - > getWM ( ) & MS_MODE_OVERLAYED_LOOPS ) {
// Sega - Mega - Drive
} ;
// write packet
# if(0)
// We've just received a frame of data. (Optionally) print out information about it:
# ifdef DEBUG_PRINT_EACH_RECEIVED_FRAME
if ( fStreamId ! = NULL ) envir ( ) < < " Stream \" " < < fStreamId < < " \" ; " ;
envir ( ) < < fSubsession . mediumName ( ) < < " / " < < fSubsession . codecName ( ) < < " : \t Received " < < frameSize < < " bytes " ;
if ( numTruncatedBytes > 0 ) envir ( ) < < " (with " < < numTruncatedBytes < < " bytes truncated) " ;
char uSecsStr [ 6 + 1 ] ; // used to output the 'microseconds' part of the presentation time
sprintf ( uSecsStr , " %06u " , ( unsigned ) presentationTime . tv_usec ) ;
envir ( ) < < " . \t Presentation time: " < < ( int ) presentationTime . tv_sec < < " . " < < uSecsStr ;
if ( fSubsession . rtpSource ( ) ! = NULL & & ! fSubsession . rtpSource ( ) - > hasBeenSynchronizedUsingRTCP ( ) ) {
envir ( ) < < " ! " ; // mark the debugging output to indicate that this presentation time is not RTCP-synchronized
}
# ifdef DEBUG_PRINT_NPT
envir ( ) < < " \t NPT: " < < fSubsession . getNormalPlayTime ( presentationTime ) ;
# endif
envir ( ) < < " \n " ;
# endif
# else
//*
// printf("__FUNCTION__ = %s\n", __FUNCTION__ );
// if(0 == strncmp(fSubsession.codecName(), "H264", 16) ){
unsigned char nalu_header [ 4 ] = { 0 , 0 , 0 , 1 } ;
unsigned char extraData [ 256 ] ;
unsigned int num = 0 ;
SPropRecord * pSPropRecord ;
pSPropRecord = parseSPropParameterSets ( m_sSubSession . fmtp_spropparametersets ( ) , num ) ;
unsigned int extraLen ;
extraLen = 0 ;
//p_record[0] is sps
//p+record[1] is pps
for ( unsigned int i = 0 ; i < num ; i + + ) {
memcpy ( & extraData [ extraLen ] , & nalu_header [ 0 ] , 4 ) ;
extraLen + = 4 ;
memcpy ( & extraData [ extraLen ] , pSPropRecord [ i ] . sPropBytes , pSPropRecord [ i ] . sPropLength ) ;
extraLen + = pSPropRecord [ i ] . sPropLength ;
} ;
memcpy ( & extraData [ extraLen ] , & nalu_header [ 0 ] , 4 ) ;
extraLen + = 4 ;
delete [ ] pSPropRecord ;
memcpy ( m_pH264 , & extraData [ 0 ] , extraLen ) ;
memcpy ( m_pH264 + extraLen , m_pReceiveBuffer , frameSize ) ;
int totalSize ;
totalSize = extraLen + frameSize ;
if ( m_pOutFile [ m_nCurrentOutFile ] = = NULL ) {
//m_pOutFile[m_nCurrentOutFile] = fopen("saved.h264", "wb" );
LOG ( " Opening " ) ;
//m_pOutFile[m_nCurrentOutFile] = popen("ffmpeg -y -hide_banner -i - -vcodec copy -movflags faststart -an ./ttt123.mp4", "w" );
m_pOutFile [ m_nCurrentOutFile ] = popen ( " ffmpeg -y -hide_banner -i - -vcodec copy -reserve_index_space 50k -an ./ttt123.mkv " , " w " ) ;
if ( m_pOutFile [ m_nCurrentOutFile ] = = NULL ) {
LOG ( " Error !! " ) ;
} ;
} ;
if ( m_pOutFile [ m_nCurrentOutFile ] ! = NULL ) {
fwrite ( m_pH264 , 1 , totalSize , m_pOutFile [ m_nCurrentOutFile ] ) ;
fflush ( m_pOutFile [ m_nCurrentOutFile ] ) ;
//fclose( fp );
} ;
//qtOut->addData( m_pH264, totalSize, presentationTime );
//printf("\tsaved %d bytes\n", totalSize );
// }/*if 0 == strncmp(fSubsession.codecName(), "H264", 16)*/
//if( got_picture ) {
if ( m_pOutFormatCtx ! = NULL ) {
m_sAvPkt . duration = durationInMicroseconds ;
if ( got_picture ) m_sAvPkt . flags | = AV_PKT_FLAG_KEY ;
m_sAvPkt . stream_index = m_pOutStream - > index ;
m_sAvPkt . size = totalSize ;
m_sAvPkt . data = m_pH264 ;
//m_sAvPkt.size = frameSize;
//m_sAvPkt.data = m_pReceiveBuffer;
//av_packet_rescale_ts( &avpkt, m_pOutCodec->time_base, time_base );
int r = av_write_frame ( m_pOutFormatCtx , & m_sAvPkt ) ;
if ( r < 0 ) {
char tB [ 256 ] ;
av_strerror ( r , tB , 256 ) ;
LOG_ERROR ( " Trouble with Video Output! " ) ;
LOG_ERROR ( tB ) ;
} ;
}
//}
# endif
} ;
} else {
# ifdef DEBUG
} else {
LOG ( " Codec Not Handled! " ) ;
LOG ( fSubsession . codecName ( ) ) ;
LOG ( m_sSubSession . codecName ( ) ) ;
# endif
} ;
// Then continue, to request the next frame of data:
continuePlaying ( ) ;
}
Boolean DummySink : : continuePlaying ( ) {
if ( fSource = = NULL ) return False ; // sanity check (should not happen)
if ( fSource = = NULL ) return False ; // sanity check (should not happen)
// Request the next frame of data from our input source. "afterGettingFrame()" will get called later, when it arrives:
fSource - > getNextFrame ( fReceiveBuffer , DUMMY_SINK_RECEIVE_BUFFER_SIZE ,
fSource - > getNextFrame ( m_p ReceiveBuffer , DUMMY_SINK_RECEIVE_BUFFER_SIZE ,
afterGettingFrame , this ,
onSourceClosure , this ) ;
return True ;
}
void DummySink : : setSprop ( u_int8_t const * prop , unsigned size ) {
uint8_t * buf ;
uint8_t * buf_start ;
buf = ( uint8_t * ) malloc ( 1000 ) ;
uint8_t * buf ;
uint8_t * buf_start ;
buf = ( uint8_t * ) malloc ( 1004 ) ;
buf_start = buf + 4 ;
avpkt . data = buf ;
avpkt . data [ 0 ] = 0 ;
avpkt . data [ 1 ] = 0 ;
avpkt . data [ 2 ] = 0 ;
avpkt . data [ 3 ] = 1 ;
memcpy ( buf_start , prop , size ) ;
avpkt . size = size + 4 ;
len = avcodec_decode_video2 ( codecContext , frame , & got_picture , & avpkt ) ;
if ( len < 0 ) {
//envir() << "Error while decoding frame" << frame;
m_sAvPkt . data = buf ;
m_sAvPkt . data [ 0 ] = 0 ;
m_sAvPkt . data [ 1 ] = 0 ;
m_sAvPkt . data [ 2 ] = 0 ;
m_sAvPkt . data [ 3 ] = 1 ;
if ( size > 1000 ) {
LOG_ERROR ( " Data Chunk too large!! Increase Mem! " ) ;
free ( buf ) ;
m_sAvPkt . data = NULL ;
m_sAvPkt . size = 0 ;
return ;
} ;
memcpy ( buf_start , prop , size ) ;
m_sAvPkt . size = size + 4 ;
int got_picture ;
if ( avcodec_decode_video2 ( m_pCodecContext , m_pFrame , & got_picture , & m_sAvPkt ) < 0 ) {
LOG_ERROR ( " Error while decoding frame " ) ;
// exit(6);
free ( buf ) ;
m_sAvPkt . data = NULL ;
m_sAvPkt . size = 0 ;
return ;
} ;
LOG ( " after setSprop " ) ;
// exit (111);
free ( buf ) ;
m_sAvPkt . data = NULL ;
m_sAvPkt . size = 0 ;
} ;
# undef LOG_ERROR
@ -230,11 +619,15 @@ videoLIVE555Source::videoLIVE555Source( void *opaque ):
basicThread ( ) ,
m_pLog ( basicLogCollection : : getInstance ( ) ) ,
m_pOpaque ( opaque ) {
m_bIsParamsSet = false ;
//Default -> only decode frames
m_eWM = MS_MODE_DECODE_FRAMES ;
m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = - 1 ;
if ( ! initLibAV ( ) ) return ;
} ;
videoLIVE555Source : : videoLIVE555Source ( CSTR URI , void * opaque ) :
videoLIVE555Source : : videoLIVE555Source ( CSTR URI , void * opaque , MS_WORK_MODE mode ) :
m_pScheduler ( BasicTaskScheduler : : createNew ( ) ) ,
m_pUsageEnvironment ( BasicUsageEnvironment : : createNew ( * m_pScheduler ) ) ,
basicMediaSource ( ) ,
@ -242,9 +635,11 @@ videoLIVE555Source::videoLIVE555Source( CSTR URI, void *opaque ):
basicThread ( ) ,
m_pLog ( basicLogCollection : : getInstance ( ) ) ,
m_pOpaque ( opaque ) {
m_bIsParamsSet = true ;
m_eWM = mode ;
m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = - 1 ;
if ( ! initLibAV ( ) ) return ;
_open ( URI ) ;
_open ( URI ) ;
} ;
videoLIVE555Source : : ~ videoLIVE555Source ( ) {
@ -298,11 +693,13 @@ void videoLIVE555Source::continueAfterDESCRIBE( RTSPClient* rtspClient, int resu
break ;
} ;
ME - > m_pMediaSession = scs . session ;
// Then, create and set up our data source objects for the session. We do this by iterating over the session's 'subsessions',
// calling "MediaSubsession::initiate()", and then sending a RTSP "SETUP" command, on each one.
// (Each 'subsession' will have its own data source.)
scs . iter = new MediaSubsessionIterator ( * scs . session ) ;
ME - > setupNextSubsession ( rtspClient ) ;
ME - > setupNextSubsession ( rtspClient ) ;
return ;
} while ( 0 ) ;
// An unrecoverable error occurred with this stream.
@ -400,6 +797,7 @@ void videoLIVE555Source::continueAfterSETUP( RTSPClient* rtspClient, int resultC
// Set up the next subsession, if any:
ME - > setupNextSubsession ( rtspClient ) ;
} ;
void videoLIVE555Source : : continueAfterPLAY ( RTSPClient * rtspClient , int resultCode , char * resultString ) {
Boolean success = False ;
CustomRTSPClient * MM = ( CustomRTSPClient * ) rtspClient ;
@ -432,10 +830,10 @@ void videoLIVE555Source::continueAfterPLAY( RTSPClient* rtspClient, int resultCo
// }
success = True ;
} while ( 0 ) ;
delete [ ] resultString ;
} while ( 0 ) ;
//delete[] resultString;
if ( ! success ) {
MLOG ( " Shutdown! " ) ;
// An unrecoverable error occurred with this stream.
ME - > shutdownStream ( rtspClient ) ;
} ;
@ -462,6 +860,25 @@ void videoLIVE555Source::subsessionAfterPlaying( void* clientData ){
ME - > shutdownStream ( rtspClient ) ;
} ;
void videoLIVE555Source : : sessionAfterPlaying ( void * clientData ) {
MediaSession * session = ( MediaSession * ) clientData ;
if ( session - > hasSubsessions ( ) ) {
MediaSubsessionIterator * itr = new MediaSubsessionIterator ( * session ) ;
if ( itr ! = NULL ) {
MediaSubsession * subsession = itr - > next ( ) ;
while ( subsession ! = NULL ) {
RTSPClient * rtspClient = ( RTSPClient * ) subsession - > miscPtr ;
CustomRTSPClient * MM = ( CustomRTSPClient * ) rtspClient ;
videoLIVE555Source * ME = ( videoLIVE555Source * ) MM - > getSession ( ) ;
UsageEnvironment & env = rtspClient - > envir ( ) ;
MLOG ( " sessionAfterPlaying! " ) ;
subsession = itr - > next ( ) ;
} ;
delete itr ;
} ;
} ;
} ;
// called when a RTCP "BYE" is received for a subsession
void videoLIVE555Source : : subsessionByeHandler ( void * clientData ) { //, char const* reason ){
MediaSubsession * subsession = ( MediaSubsession * ) clientData ;
@ -492,6 +909,7 @@ void videoLIVE555Source::streamTimerHandler( void* clientData ){
void videoLIVE555Source : : openURL ( UsageEnvironment & env , char const * progName , char const * rtspURL ) {
LOG ( " openURL Called! " ) ;
} ;
// Used to iterate through each stream's 'subsessions', setting up each one:
void videoLIVE555Source : : setupNextSubsession ( RTSPClient * rtspClient ) {
CustomRTSPClient * MM = ( CustomRTSPClient * ) rtspClient ;
@ -552,6 +970,15 @@ void videoLIVE555Source::shutdownStream(RTSPClient* rtspClient, int exitCode ){
// https://chowdera.com/2021/01/20210102084512591N.html
// https://github.com/drriguz/Kamera
bool videoLIVE555Source : : _open ( CSTR URI , bool ShowDeviceMenu , float framerate , int inputPin , int width , int height , int s_width , int s_height ) {
if ( URI = = NULL ) {
LOG_ERROR ( " Trying to open MediaSource without MRL! " ) ;
return false ;
} ;
//if( !m_bIsParamsSet ) {
// LOG_ERROR("Trying to open MediaSource without all needded parameters!");
// return false;
//};
m_nInHeight = height ;
m_nInWidth = width ;
@ -565,24 +992,29 @@ bool videoLIVE555Source::_open( CSTR URI, bool ShowDeviceMenu, float framerate,
LOG_ERROR ( tB ) ;
return false ;
} ;
m_pRtspClient - > sendDescribeCommand ( continueAfterDESCRIBE ) ;
m_pRtspClient - > sendDescribeCommand ( continueAfterDESCRIBE ) ;
return init ( ) ;
} ;
void videoLIVE555Source : : consumeFrame ( cv : : Mat * frm ) {
if ( m_pMediaDestination = = NULL ) return ;
if ( ! m_pMediaDestination - > pushFrame ( * frm , m_pOpaque ) ) {
if ( m_pMediaDestination = = NULL ) return ;
if ( ! m_pMediaDestination - > pushFrame ( * frm , m_pOpaque ) ) {
m_pMediaDestination = NULL ;
LOG_ERROR ( " Error Consume Frame by Media Destination! " ) ;
} ;
} ;
bool videoLIVE555Source : : _close ( void ) {
LOG ( " CLOSE! " ) ;
return _stop ( ) ;
} ;
bool videoLIVE555Source : : _stop ( void ) {
LOG ( " STOP! " ) ;
shutdownStream ( m_pRtspClient ) ;
LOG ( " SHUTDOWN! " ) ;
_eventLoopWatchVariable = 1 ;
basicThread : : waitThreadEnd ( true ) ;
LOG ( " TH ENDED! " ) ;
return true ;
} ;
bool videoLIVE555Source : : isFinished ( void ) { return false ; } ;