structure, whitespace fixes. Add not storing audio frames

This commit is contained in:
Isaac Connor 2016-03-31 15:33:27 -04:00
parent 742fab2ea4
commit 5c68e0c9cb
1 changed files with 61 additions and 63 deletions

View File

@ -123,8 +123,7 @@ int FfmpegCamera::PreCapture()
return( 0 );
}
int FfmpegCamera::Capture( Image &image )
{
int FfmpegCamera::Capture( Image &image ) {
if (!mCanCapture){
return -1;
}
@ -154,11 +153,9 @@ int FfmpegCamera::Capture( Image &image )
}
int frameComplete = false;
while ( !frameComplete )
{
while ( !frameComplete ) {
int avResult = av_read_frame( mFormatContext, &packet );
if ( avResult < 0 )
{
if ( avResult < 0 ) {
char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE);
if (
@ -166,8 +163,7 @@ int FfmpegCamera::Capture( Image &image )
(avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
// Check for Connection failure.
(avResult == -110)
)
{
) {
Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf );
ReopenFfmpeg();
}
@ -176,8 +172,8 @@ int FfmpegCamera::Capture( Image &image )
return( -1 );
}
Debug( 5, "Got packet from stream %d", packet.stream_index );
if ( packet.stream_index == mVideoStreamId )
{
// What about audio stream?
if ( packet.stream_index == mVideoStreamId ) {
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
#else
@ -187,8 +183,7 @@ int FfmpegCamera::Capture( Image &image )
Debug( 4, "Decoded video packet at frame %d", frameCount );
if ( frameComplete )
{
if ( frameComplete ) {
Debug( 3, "Got frame %d", frameCount );
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
@ -208,14 +203,16 @@ int FfmpegCamera::Capture( Image &image )
#endif // HAVE_LIBSWSCALE
frameCount++;
}
}
} // end if frameComplete
} else {
Debug( 4, "Different stream_index %d", packet.stream_index );
} // end if packet.stream_index == mVideoStreamId
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
av_packet_unref( &packet);
#else
av_free_packet( &packet );
#endif
}
} // end while ! frameComplete
return (0);
}
@ -303,8 +300,7 @@ int FfmpegCamera::OpenFfmpeg() {
// Find first video stream present
mVideoStreamId = -1;
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 ( mFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO )
#else
@ -518,11 +514,9 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
}
int frameComplete = false;
while ( !frameComplete )
{
while ( !frameComplete ) {
int avResult = av_read_frame( mFormatContext, &packet );
if ( avResult < 0 )
{
if ( avResult < 0 ) {
char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE);
if (
@ -530,8 +524,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
(avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
// Check for Connection failure.
(avResult == -110)
)
{
) {
Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf);
ReopenFfmpeg();
}
@ -540,8 +533,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
return( -1 );
}
Debug( 5, "Got packet from stream %d", packet.stream_index );
if ( packet.stream_index == mVideoStreamId )
{
if ( packet.stream_index == mVideoStreamId ) {
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
#else
@ -551,8 +543,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
Debug( 4, "Decoded video packet at frame %d", frameCount );
if ( frameComplete )
{
if ( frameComplete ) {
Debug( 3, "Got frame %d", frameCount );
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
@ -570,14 +561,16 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
wasRecording = true;
strcpy(oldDirectory, event_file);
}else if(!recording && wasRecording && videoStore){
} else if ( ( ! recording ) && wasRecording && videoStore ) {
Info("Deleting videoStore instance");
delete videoStore;
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
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");
if(videoStore){
delete videoStore;
@ -611,19 +604,24 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
#endif // HAVE_LIBSWSCALE
frameCount++;
}
} // end if frameComplete
} else if ( packet.stream_index == mAudioStreamId ) { //FIXME best way to copy all other streams
if ( videoStore && recording ) {
if ( record_audio ) {
Debug(4, "Recording audio packet" );
//Write the packet to our video store
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
av_free_packet( &packet );
return 0;
}
} else {
Debug(4, "Not recording audio packet" );
}
}
}
av_free_packet( &packet );
}
} // end while ! frameComplete
return (frameCount);
}