spacing, code structure and code documentation. Also, add in a flag to say whether to store the audio packets
This commit is contained in:
parent
fa08646243
commit
92b98e1953
|
@ -116,6 +116,8 @@ void RemoteCameraRtsp::Initialise()
|
||||||
|
|
||||||
int max_size = width*height*colours;
|
int max_size = width*height*colours;
|
||||||
|
|
||||||
|
// This allocates a buffer able to hold a raw fframe, which is a little artbitrary. Might be nice to get some
|
||||||
|
// decent data on how large a buffer is really needed.
|
||||||
buffer.size( max_size );
|
buffer.size( max_size );
|
||||||
|
|
||||||
if ( logDebugging() )
|
if ( logDebugging() )
|
||||||
|
@ -171,6 +173,7 @@ int RemoteCameraRtsp::PrimeCapture()
|
||||||
// Find first video stream present
|
// Find first video stream present
|
||||||
mVideoStreamId = -1;
|
mVideoStreamId = -1;
|
||||||
|
|
||||||
|
// Find the first video stream.
|
||||||
for ( unsigned int i = 0; i < mFormatContext->nb_streams; i++ )
|
for ( unsigned int i = 0; i < mFormatContext->nb_streams; i++ )
|
||||||
#if (LIBAVCODEC_VERSION_CHECK(52, 64, 0, 64, 0) || LIBAVUTIL_VERSION_CHECK(50, 14, 0, 14, 0))
|
#if (LIBAVCODEC_VERSION_CHECK(52, 64, 0, 64, 0) || LIBAVUTIL_VERSION_CHECK(50, 14, 0, 14, 0))
|
||||||
if ( mFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO )
|
if ( mFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO )
|
||||||
|
@ -239,8 +242,7 @@ int RemoteCameraRtsp::PrimeCapture()
|
||||||
return( 0 );
|
return( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
int RemoteCameraRtsp::PreCapture()
|
int RemoteCameraRtsp::PreCapture() {
|
||||||
{
|
|
||||||
if ( !rtspThread->isRunning() )
|
if ( !rtspThread->isRunning() )
|
||||||
return( -1 );
|
return( -1 );
|
||||||
if ( !rtspThread->hasSources() )
|
if ( !rtspThread->hasSources() )
|
||||||
|
@ -251,8 +253,7 @@ int RemoteCameraRtsp::PreCapture()
|
||||||
return( 0 );
|
return( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
int RemoteCameraRtsp::Capture( Image &image )
|
int RemoteCameraRtsp::Capture( Image &image ) {
|
||||||
{
|
|
||||||
AVPacket packet;
|
AVPacket packet;
|
||||||
uint8_t* directbuffer;
|
uint8_t* directbuffer;
|
||||||
int frameComplete = false;
|
int frameComplete = false;
|
||||||
|
@ -264,14 +265,12 @@ int RemoteCameraRtsp::Capture( Image &image )
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ( true )
|
while ( true ) {
|
||||||
{
|
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
if ( !rtspThread->isRunning() )
|
if ( !rtspThread->isRunning() )
|
||||||
return (-1);
|
return (-1);
|
||||||
|
|
||||||
if ( rtspThread->getFrame( buffer ) )
|
if ( rtspThread->getFrame( buffer ) ) {
|
||||||
{
|
|
||||||
Debug( 3, "Read frame %d bytes", buffer.size() );
|
Debug( 3, "Read frame %d bytes", buffer.size() );
|
||||||
Debug( 4, "Address %p", buffer.head() );
|
Debug( 4, "Address %p", buffer.head() );
|
||||||
Hexdump( 4, buffer.head(), 16 );
|
Hexdump( 4, buffer.head(), 16 );
|
||||||
|
@ -279,18 +278,17 @@ int RemoteCameraRtsp::Capture( Image &image )
|
||||||
if ( !buffer.size() )
|
if ( !buffer.size() )
|
||||||
return( -1 );
|
return( -1 );
|
||||||
|
|
||||||
if(mCodecContext->codec_id == AV_CODEC_ID_H264)
|
if(mCodecContext->codec_id == AV_CODEC_ID_H264) {
|
||||||
{
|
|
||||||
// SPS and PPS frames should be saved and appended to IDR frames
|
// SPS and PPS frames should be saved and appended to IDR frames
|
||||||
int nalType = (buffer.head()[3] & 0x1f);
|
int nalType = (buffer.head()[3] & 0x1f);
|
||||||
|
|
||||||
// SPS
|
// SPS The SPS NAL unit contains parameters that apply to a series of consecutive coded video pictures
|
||||||
if(nalType == 7)
|
if(nalType == 7)
|
||||||
{
|
{
|
||||||
lastSps = buffer;
|
lastSps = buffer;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// PPS
|
// PPS The PPS NAL unit contains parameters that apply to the decoding of one or more individual pictures inside a coded video sequence
|
||||||
else if(nalType == 8)
|
else if(nalType == 8)
|
||||||
{
|
{
|
||||||
lastPps = buffer;
|
lastPps = buffer;
|
||||||
|
@ -306,17 +304,17 @@ int RemoteCameraRtsp::Capture( Image &image )
|
||||||
|
|
||||||
av_init_packet( &packet );
|
av_init_packet( &packet );
|
||||||
|
|
||||||
while ( !frameComplete && buffer.size() > 0 )
|
while ( !frameComplete && buffer.size() > 0 ) {
|
||||||
{
|
|
||||||
packet.data = buffer.head();
|
packet.data = buffer.head();
|
||||||
packet.size = buffer.size();
|
packet.size = buffer.size();
|
||||||
|
|
||||||
|
// So I think this is the magic decode step. Result is a raw image?
|
||||||
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
|
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
|
||||||
int len = avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet );
|
int len = avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet );
|
||||||
#else
|
#else
|
||||||
int len = avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size );
|
int len = avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size );
|
||||||
#endif
|
#endif
|
||||||
if ( len < 0 )
|
if ( len < 0 ) {
|
||||||
{
|
|
||||||
Error( "Error while decoding frame %d", frameCount );
|
Error( "Error while decoding frame %d", frameCount );
|
||||||
Hexdump( Logger::ERROR, buffer.head(), buffer.size()>256?256:buffer.size() );
|
Hexdump( Logger::ERROR, buffer.head(), buffer.size()>256?256:buffer.size() );
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
|
@ -327,8 +325,8 @@ int RemoteCameraRtsp::Capture( Image &image )
|
||||||
//Hexdump( 0, buffer.head(), buffer.size() );
|
//Hexdump( 0, buffer.head(), buffer.size() );
|
||||||
|
|
||||||
buffer -= len;
|
buffer -= len;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// At this point, we either have a frame or ran out of buffer. What happens if we run out of buffer?
|
||||||
if ( frameComplete ) {
|
if ( frameComplete ) {
|
||||||
|
|
||||||
Debug( 3, "Got frame %d", frameCount );
|
Debug( 3, "Got frame %d", frameCount );
|
||||||
|
@ -363,13 +361,14 @@ int RemoteCameraRtsp::Capture( Image &image )
|
||||||
if(frameComplete)
|
if(frameComplete)
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
}
|
} // end while true
|
||||||
|
|
||||||
|
// can never get here.
|
||||||
return (0) ;
|
return (0) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Function to handle capture and store
|
//Function to handle capture and store
|
||||||
int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* event_file )
|
int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* event_file ) {
|
||||||
{
|
|
||||||
AVPacket packet;
|
AVPacket packet;
|
||||||
uint8_t* directbuffer;
|
uint8_t* directbuffer;
|
||||||
int frameComplete = false;
|
int frameComplete = false;
|
||||||
|
@ -381,14 +380,12 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ( true )
|
while ( true ) {
|
||||||
{
|
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
if ( !rtspThread->isRunning() )
|
if ( !rtspThread->isRunning() )
|
||||||
return (-1);
|
return (-1);
|
||||||
|
|
||||||
if ( rtspThread->getFrame( buffer ) )
|
if ( rtspThread->getFrame( buffer ) ) {
|
||||||
{
|
|
||||||
Debug( 3, "Read frame %d bytes", buffer.size() );
|
Debug( 3, "Read frame %d bytes", buffer.size() );
|
||||||
Debug( 4, "Address %p", buffer.head() );
|
Debug( 4, "Address %p", buffer.head() );
|
||||||
Hexdump( 4, buffer.head(), 16 );
|
Hexdump( 4, buffer.head(), 16 );
|
||||||
|
@ -396,47 +393,43 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
|
||||||
if ( !buffer.size() )
|
if ( !buffer.size() )
|
||||||
return( -1 );
|
return( -1 );
|
||||||
|
|
||||||
if(mCodecContext->codec_id == AV_CODEC_ID_H264)
|
if(mCodecContext->codec_id == AV_CODEC_ID_H264) {
|
||||||
{
|
|
||||||
// SPS and PPS frames should be saved and appended to IDR frames
|
// SPS and PPS frames should be saved and appended to IDR frames
|
||||||
int nalType = (buffer.head()[3] & 0x1f);
|
int nalType = (buffer.head()[3] & 0x1f);
|
||||||
|
|
||||||
// SPS
|
// SPS
|
||||||
if(nalType == 7)
|
if(nalType == 7) {
|
||||||
{
|
|
||||||
lastSps = buffer;
|
lastSps = buffer;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// PPS
|
// PPS
|
||||||
else if(nalType == 8)
|
else if(nalType == 8) {
|
||||||
{
|
|
||||||
lastPps = buffer;
|
lastPps = buffer;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// IDR
|
// IDR
|
||||||
else if(nalType == 5)
|
else if(nalType == 5) {
|
||||||
{
|
|
||||||
buffer += lastSps;
|
buffer += lastSps;
|
||||||
buffer += lastPps;
|
buffer += lastPps;
|
||||||
}
|
}
|
||||||
}
|
} // end if H264, what about other codecs?
|
||||||
|
|
||||||
av_init_packet( &packet );
|
av_init_packet( &packet );
|
||||||
|
|
||||||
if ( packet.stream_index == mVideoStreamId )
|
// Why are we checking for it being the video stream
|
||||||
{
|
if ( packet.stream_index == mVideoStreamId ) {
|
||||||
|
|
||||||
while ( !frameComplete && buffer.size() > 0 )
|
while ( !frameComplete && buffer.size() > 0 ) {
|
||||||
{
|
|
||||||
packet.data = buffer.head();
|
packet.data = buffer.head();
|
||||||
packet.size = buffer.size();
|
packet.size = buffer.size();
|
||||||
|
|
||||||
|
// So this does the decode
|
||||||
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
|
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
|
||||||
int len = avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet );
|
int len = avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet );
|
||||||
#else
|
#else
|
||||||
int len = avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size );
|
int len = avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size );
|
||||||
#endif
|
#endif
|
||||||
if ( len < 0 )
|
if ( len < 0 ) {
|
||||||
{
|
|
||||||
Error( "Error while decoding frame %d", frameCount );
|
Error( "Error while decoding frame %d", frameCount );
|
||||||
Hexdump( Logger::ERROR, buffer.head(), buffer.size()>256?256:buffer.size() );
|
Hexdump( Logger::ERROR, buffer.head(), buffer.size()>256?256:buffer.size() );
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
|
@ -447,8 +440,8 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
|
||||||
//Hexdump( 0, buffer.head(), buffer.size() );
|
//Hexdump( 0, buffer.head(), buffer.size() );
|
||||||
|
|
||||||
buffer -= len;
|
buffer -= len;
|
||||||
|
} // end while get & decode a frame
|
||||||
|
|
||||||
}
|
|
||||||
if ( frameComplete ) {
|
if ( frameComplete ) {
|
||||||
|
|
||||||
Debug( 3, "Got frame %d", frameCount );
|
Debug( 3, "Got frame %d", frameCount );
|
||||||
|
@ -464,13 +457,15 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
|
||||||
strcpy(oldDirectory, event_file);
|
strcpy(oldDirectory, event_file);
|
||||||
|
|
||||||
} else if ( !recording && wasRecording && videoStore ) {
|
} else if ( !recording && wasRecording && videoStore ) {
|
||||||
|
// Why are we deleting the videostore? Becase for soem reason we are no longer recording? How does that happen?
|
||||||
Info("Deleting videoStore instance");
|
Info("Deleting videoStore instance");
|
||||||
delete videoStore;
|
delete videoStore;
|
||||||
videoStore = NULL;
|
videoStore = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//The directory we are recording to is no longer tied to the current event. Need to re-init the videostore with the correct directory and start recording again
|
//The directory we are recording to is no longer tied to the current event. Need to re-init the videostore with the correct directory and start recording again
|
||||||
if(recording && wasRecording && (strcmp(oldDirectory, event_file)!=0) && (packet.flags & AV_PKT_FLAG_KEY) ){ //don't open new videostore until we're on a key frame..would this require an offset adjustment for the event as a result?...if we store our key frame location with the event will that be enough?
|
if ( recording && wasRecording && (strcmp(oldDirectory, event_file)!=0) && (packet.flags & AV_PKT_FLAG_KEY) ) {
|
||||||
|
//don't open new videostore until we're on a key frame..would this require an offset adjustment for the event as a result?...if we store our key frame location with the event will that be enough?
|
||||||
Info("Re-starting video storage module");
|
Info("Re-starting video storage module");
|
||||||
if ( videoStore ) {
|
if ( videoStore ) {
|
||||||
delete videoStore;
|
delete videoStore;
|
||||||
|
@ -507,20 +502,22 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
|
||||||
frameCount++;
|
frameCount++;
|
||||||
|
|
||||||
} /* frame complete */
|
} /* frame complete */
|
||||||
}
|
} else if ( packet.stream_index == mAudioStreamId ) {
|
||||||
else if(packet.stream_index == mAudioStreamId)
|
Debug( 4, "Got audio packet" );
|
||||||
{
|
if ( videoStore && recording ) {
|
||||||
if(videoStore && recording)
|
if ( recordAudio ) {
|
||||||
{
|
Debug( 4, "Storing Audio packet" );
|
||||||
//Write the packet to our video store
|
//Write the packet to our video store
|
||||||
int ret = videoStore->writeAudioFramePacket(&packet, mFormatContext->streams[packet.stream_index]); //FIXME no relevance of last key frame
|
int ret = videoStore->writeAudioFramePacket(&packet, mFormatContext->streams[packet.stream_index]); //FIXME no relevance of last key frame
|
||||||
if(ret<0) //Less than zero and we skipped a frame
|
if ( ret < 0 ) { //Less than zero and we skipped a frame
|
||||||
{
|
|
||||||
av_free_packet( &packet );
|
av_free_packet( &packet );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Debug( 4, "Not storing audio" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} // end if video or audio packet
|
||||||
|
|
||||||
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
|
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
|
||||||
av_packet_unref( &packet);
|
av_packet_unref( &packet);
|
||||||
|
@ -531,10 +528,9 @@ int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* even
|
||||||
|
|
||||||
if(frameComplete)
|
if(frameComplete)
|
||||||
return (0);
|
return (0);
|
||||||
|
} // end while true
|
||||||
}
|
|
||||||
return (0) ;
|
return (0) ;
|
||||||
}
|
} // int RemoteCameraRtsp::CaptureAndRecord( Image &image, bool recording, char* event_file )
|
||||||
|
|
||||||
int RemoteCameraRtsp::PostCapture()
|
int RemoteCameraRtsp::PostCapture()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue