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 ); return( 0 );
} }
int FfmpegCamera::Capture( Image &image ) int FfmpegCamera::Capture( Image &image ) {
{
if (!mCanCapture){ if (!mCanCapture){
return -1; return -1;
} }
@ -154,11 +153,9 @@ int FfmpegCamera::Capture( Image &image )
} }
int frameComplete = false; int frameComplete = false;
while ( !frameComplete ) while ( !frameComplete ) {
{
int avResult = av_read_frame( mFormatContext, &packet ); int avResult = av_read_frame( mFormatContext, &packet );
if ( avResult < 0 ) if ( avResult < 0 ) {
{
char errbuf[AV_ERROR_MAX_STRING_SIZE]; char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE); av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE);
if ( if (
@ -166,9 +163,8 @@ int FfmpegCamera::Capture( Image &image )
(avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) || (avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
// Check for Connection failure. // Check for Connection failure.
(avResult == -110) (avResult == -110)
) ) {
{ Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf );
Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf);
ReopenFfmpeg(); ReopenFfmpeg();
} }
@ -176,8 +172,8 @@ int FfmpegCamera::Capture( Image &image )
return( -1 ); return( -1 );
} }
Debug( 5, "Got packet from stream %d", packet.stream_index ); 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 LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 ) if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
#else #else
@ -187,35 +183,36 @@ int FfmpegCamera::Capture( Image &image )
Debug( 4, "Decoded video packet at frame %d", frameCount ); Debug( 4, "Decoded video packet at frame %d", frameCount );
if ( frameComplete ) if ( frameComplete ) {
{
Debug( 3, "Got frame %d", frameCount ); Debug( 3, "Got frame %d", frameCount );
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height); avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
#if HAVE_LIBSWSCALE #if HAVE_LIBSWSCALE
if(mConvertContext == NULL) { if(mConvertContext == NULL) {
mConvertContext = sws_getContext( mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt, width, height, imagePixFormat, SWS_BICUBIC, NULL, NULL, NULL ); mConvertContext = sws_getContext( mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt, width, height, imagePixFormat, SWS_BICUBIC, NULL, NULL, NULL );
if(mConvertContext == NULL) if(mConvertContext == NULL)
Fatal( "Unable to create conversion context for %s", mPath.c_str() ); Fatal( "Unable to create conversion context for %s", mPath.c_str() );
} }
if ( sws_scale( mConvertContext, mRawFrame->data, mRawFrame->linesize, 0, mCodecContext->height, mFrame->data, mFrame->linesize ) < 0 ) if ( sws_scale( mConvertContext, mRawFrame->data, mRawFrame->linesize, 0, mCodecContext->height, mFrame->data, mFrame->linesize ) < 0 )
Fatal( "Unable to convert raw format %u to target format %u at frame %d", mCodecContext->pix_fmt, imagePixFormat, frameCount ); Fatal( "Unable to convert raw format %u to target format %u at frame %d", mCodecContext->pix_fmt, imagePixFormat, frameCount );
#else // HAVE_LIBSWSCALE #else // HAVE_LIBSWSCALE
Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" ); Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" );
#endif // HAVE_LIBSWSCALE #endif // HAVE_LIBSWSCALE
frameCount++; 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) #if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
av_packet_unref( &packet); av_packet_unref( &packet);
#else #else
av_free_packet( &packet ); av_free_packet( &packet );
#endif #endif
} } // end while ! frameComplete
return (0); return (0);
} }
@ -303,8 +300,7 @@ int FfmpegCamera::OpenFfmpeg() {
// Find first video stream present // Find first video stream present
mVideoStreamId = -1; 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 (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 )
#else #else
@ -512,17 +508,15 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
/* Request a writeable buffer of the target image */ /* Request a writeable buffer of the target image */
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder); directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
if(directbuffer == NULL) { if( directbuffer == NULL ) {
Error("Failed requesting writeable buffer for the captured image."); Error("Failed requesting writeable buffer for the captured image.");
return (-1); return (-1);
} }
int frameComplete = false; int frameComplete = false;
while ( !frameComplete ) while ( !frameComplete ) {
{
int avResult = av_read_frame( mFormatContext, &packet ); int avResult = av_read_frame( mFormatContext, &packet );
if ( avResult < 0 ) if ( avResult < 0 ) {
{
char errbuf[AV_ERROR_MAX_STRING_SIZE]; char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE); av_strerror(avResult, errbuf, AV_ERROR_MAX_STRING_SIZE);
if ( if (
@ -530,8 +524,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
(avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) || (avResult == AVERROR_EOF || (mFormatContext->pb && mFormatContext->pb->eof_reached)) ||
// Check for Connection failure. // Check for Connection failure.
(avResult == -110) (avResult == -110)
) ) {
{
Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf); Info( "av_read_frame returned \"%s\". Reopening stream.", errbuf);
ReopenFfmpeg(); ReopenFfmpeg();
} }
@ -540,8 +533,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
return( -1 ); return( -1 );
} }
Debug( 5, "Got packet from stream %d", packet.stream_index ); 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 LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 ) if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
#else #else
@ -551,8 +543,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
Debug( 4, "Decoded video packet at frame %d", frameCount ); Debug( 4, "Decoded video packet at frame %d", frameCount );
if ( frameComplete ) if ( frameComplete ) {
{
Debug( 3, "Got frame %d", frameCount ); Debug( 3, "Got frame %d", frameCount );
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height); avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
@ -563,21 +554,23 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
//TODO I think we need to store the key frame location for seeking as part of the event //TODO I think we need to store the key frame location for seeking as part of the event
//Video recording //Video recording
if(recording && !wasRecording){ if ( recording && !wasRecording ) {
//Instantiate the video storage module //Instantiate the video storage module
videoStore = new VideoStore((const char *)event_file, "mp4", mFormatContext->streams[mVideoStreamId],mAudioStreamId==-1?NULL:mFormatContext->streams[mAudioStreamId],startTime); videoStore = new VideoStore((const char *)event_file, "mp4", mFormatContext->streams[mVideoStreamId],mAudioStreamId==-1?NULL:mFormatContext->streams[mAudioStreamId],startTime);
wasRecording = true; wasRecording = true;
strcpy(oldDirectory, event_file); strcpy(oldDirectory, event_file);
}else if(!recording && wasRecording && videoStore){ } else if ( ( ! recording ) && wasRecording && videoStore ) {
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;
@ -588,7 +581,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
strcpy(oldDirectory, event_file); strcpy(oldDirectory, event_file);
} }
if(videoStore && recording){ if ( videoStore && recording ) {
//Write the packet to our video store //Write the packet to our video store
int ret = videoStore->writeVideoFramePacket(&packet, mFormatContext->streams[mVideoStreamId]);//, &lastKeyframePkt); int ret = videoStore->writeVideoFramePacket(&packet, mFormatContext->streams[mVideoStreamId]);//, &lastKeyframePkt);
if(ret<0){//Less than zero and we skipped a frame if(ret<0){//Less than zero and we skipped a frame
@ -598,32 +591,37 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
} }
#if HAVE_LIBSWSCALE #if HAVE_LIBSWSCALE
if(mConvertContext == NULL) { if ( mConvertContext == NULL ) {
mConvertContext = sws_getContext( mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt, width, height, imagePixFormat, SWS_BICUBIC, NULL, NULL, NULL ); mConvertContext = sws_getContext( mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt, width, height, imagePixFormat, SWS_BICUBIC, NULL, NULL, NULL );
if(mConvertContext == NULL) if ( mConvertContext == NULL )
Fatal( "Unable to create conversion context for %s", mPath.c_str() ); Fatal( "Unable to create conversion context for %s", mPath.c_str() );
} }
if ( sws_scale( mConvertContext, mRawFrame->data, mRawFrame->linesize, 0, mCodecContext->height, mFrame->data, mFrame->linesize ) < 0 ) if ( sws_scale( mConvertContext, mRawFrame->data, mRawFrame->linesize, 0, mCodecContext->height, mFrame->data, mFrame->linesize ) < 0 )
Fatal( "Unable to convert raw format %u to target format %u at frame %d", mCodecContext->pix_fmt, imagePixFormat, frameCount ); Fatal( "Unable to convert raw format %u to target format %u at frame %d", mCodecContext->pix_fmt, imagePixFormat, frameCount );
#else // HAVE_LIBSWSCALE #else // HAVE_LIBSWSCALE
Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" ); Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" );
#endif // HAVE_LIBSWSCALE #endif // HAVE_LIBSWSCALE
frameCount++; frameCount++;
} } // end if frameComplete
}else if(packet.stream_index == mAudioStreamId){//FIXME best way to copy all other streams } else if ( packet.stream_index == mAudioStreamId ) { //FIXME best way to copy all other streams
if(videoStore && recording){ if ( videoStore && recording ) {
//Write the packet to our video store if ( record_audio ) {
int ret = videoStore->writeAudioFramePacket(&packet, mFormatContext->streams[packet.stream_index]); //FIXME no relevance of last key frame Debug(4, "Recording audio packet" );
if(ret<0){//Less than zero and we skipped a frame //Write the packet to our video store
av_free_packet( &packet ); int ret = videoStore->writeAudioFramePacket(&packet, mFormatContext->streams[packet.stream_index]); //FIXME no relevance of last key frame
return 0; 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 ); av_free_packet( &packet );
} } // end while ! frameComplete
return (frameCount); return (frameCount);
} }