re-organize to remove code duplication and make logic clearer
This commit is contained in:
parent
9153d2a986
commit
8bb956c3b1
|
@ -146,21 +146,11 @@ int FfmpegCamera::Capture( Image &image )
|
||||||
}
|
}
|
||||||
|
|
||||||
AVPacket packet;
|
AVPacket packet;
|
||||||
uint8_t* directbuffer;
|
|
||||||
|
|
||||||
/* Request a writeable buffer of the target image */
|
|
||||||
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
|
|
||||||
if(directbuffer == NULL) {
|
|
||||||
Error("Failed requesting writeable buffer for the captured image.");
|
|
||||||
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 (
|
||||||
|
@ -168,8 +158,7 @@ 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();
|
||||||
}
|
}
|
||||||
|
@ -179,8 +168,7 @@ int FfmpegCamera::Capture( Image &image )
|
||||||
}
|
}
|
||||||
Debug( 5, "Got packet from stream %d", packet.stream_index );
|
Debug( 5, "Got packet from stream %d", packet.stream_index );
|
||||||
// What about audio stream? Maybe someday we could do sound detection...
|
// What about audio stream? Maybe someday we could do sound detection...
|
||||||
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
|
||||||
|
@ -193,6 +181,15 @@ int FfmpegCamera::Capture( Image &image )
|
||||||
if ( frameComplete ) {
|
if ( frameComplete ) {
|
||||||
Debug( 4, "Got frame %d", frameCount );
|
Debug( 4, "Got frame %d", frameCount );
|
||||||
|
|
||||||
|
uint8_t* directbuffer;
|
||||||
|
|
||||||
|
/* Request a writeable buffer of the target image */
|
||||||
|
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
|
||||||
|
if(directbuffer == NULL) {
|
||||||
|
Error("Failed requesting writeable buffer for the captured image.");
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
|
#if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0)
|
||||||
av_image_fill_arrays(mFrame->data, mFrame->linesize,
|
av_image_fill_arrays(mFrame->data, mFrame->linesize,
|
||||||
directbuffer, imagePixFormat, width, height, 1);
|
directbuffer, imagePixFormat, width, height, 1);
|
||||||
|
@ -517,11 +514,11 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
if (!mCanCapture){
|
if (!mCanCapture){
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
int ret;
|
||||||
|
|
||||||
// If the reopen thread has a value, but mCanCapture != 0, then we have just reopened the connection to the ffmpeg device, and we can clean up the thread.
|
// If the reopen thread has a value, but mCanCapture != 0, then we have just reopened the connection to the ffmpeg device, and we can clean up the thread.
|
||||||
if (mReopenThread != 0) {
|
if (mReopenThread != 0) {
|
||||||
void *retval = 0;
|
void *retval = 0;
|
||||||
int ret;
|
|
||||||
|
|
||||||
ret = pthread_join(mReopenThread, &retval);
|
ret = pthread_join(mReopenThread, &retval);
|
||||||
if (ret != 0){
|
if (ret != 0){
|
||||||
|
@ -534,14 +531,6 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
|
|
||||||
AVPacket packet;
|
AVPacket packet;
|
||||||
AVPacket queued_packet;
|
AVPacket queued_packet;
|
||||||
uint8_t* directbuffer;
|
|
||||||
|
|
||||||
/* Request a writeable buffer of the target image */
|
|
||||||
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
|
|
||||||
if( directbuffer == NULL ) {
|
|
||||||
Error("Failed requesting writeable buffer for the captured image.");
|
|
||||||
return (-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( mCodecContext->codec_id != AV_CODEC_ID_H264 ) {
|
if ( mCodecContext->codec_id != AV_CODEC_ID_H264 ) {
|
||||||
Error( "Input stream is not h264. The stored event file may not be viewable in browser." );
|
Error( "Input stream is not h264. The stored event file may not be viewable in browser." );
|
||||||
|
@ -568,31 +557,21 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
}
|
}
|
||||||
Debug( 5, "Got packet from stream %d", packet.stream_index );
|
Debug( 5, "Got packet from stream %d", packet.stream_index );
|
||||||
|
|
||||||
//Buffer video packets
|
|
||||||
if ( ! recording ) {
|
|
||||||
if ( packet.flags & AV_PKT_FLAG_KEY ) {
|
|
||||||
packetqueue.clearQueue();
|
|
||||||
}
|
|
||||||
packetqueue.queuePacket(&packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( packet.stream_index == mVideoStreamId ) {
|
|
||||||
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
|
|
||||||
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
|
|
||||||
#else
|
|
||||||
if ( avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size ) < 0 )
|
|
||||||
#endif
|
|
||||||
Fatal( "Unable to decode frame at frame %d", frameCount );
|
|
||||||
|
|
||||||
Debug( 4, "Decoded video packet at frame %d", frameCount );
|
|
||||||
|
|
||||||
if ( frameComplete ) {
|
|
||||||
Debug( 4, "Got frame %d", frameCount );
|
|
||||||
|
|
||||||
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
|
|
||||||
|
|
||||||
//Video recording
|
//Video recording
|
||||||
if ( recording && !wasRecording ) {
|
if ( recording ) {
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// for efficiency's sake, we should test for keyframe before we test for directory change...
|
||||||
|
if ( videoStore && (packet.flags & AV_PKT_FLAG_KEY) && (strcmp(oldDirectory, event_file) != 0 ) ) {
|
||||||
|
// 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");
|
||||||
|
delete videoStore;
|
||||||
|
videoStore = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! videoStore ) {
|
||||||
//Instantiate the video storage module
|
//Instantiate the video storage module
|
||||||
|
|
||||||
if (record_audio) {
|
if (record_audio) {
|
||||||
|
@ -620,81 +599,66 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
startTime,
|
startTime,
|
||||||
this->getMonitor()->getOrientation() );
|
this->getMonitor()->getOrientation() );
|
||||||
}
|
}
|
||||||
wasRecording = true;
|
|
||||||
strcpy(oldDirectory, event_file);
|
strcpy(oldDirectory, event_file);
|
||||||
|
} // end if ! wasRecording
|
||||||
|
|
||||||
// Need to write out all the frames from the last keyframe?
|
// Need to write out all the frames from the last keyframe?
|
||||||
unsigned int packet_count = 0;
|
unsigned int packet_count = 0;
|
||||||
while ( packetqueue.popPacket( &queued_packet ) ) {
|
while ( packetqueue.popPacket( &queued_packet ) ) {
|
||||||
packet_count += 1;
|
packet_count += 1;
|
||||||
//Write the packet to our video store
|
//Write the packet to our video store
|
||||||
int ret = videoStore->writeVideoFramePacket(&queued_packet, mFormatContext->streams[mVideoStreamId]);//, &lastKeyframePkt);
|
if ( queued_packet.stream_index == mVideoStreamId ) {
|
||||||
|
ret = videoStore->writeVideoFramePacket(&queued_packet, mFormatContext->streams[mVideoStreamId]);
|
||||||
|
} else if ( queued_packet.stream_index == mAudioStreamId ) {
|
||||||
|
//ret = videoStore->writeAudioFramePacket(&queued_packet, mFormatContext->streams[mAudioStreamId]);
|
||||||
|
} else {
|
||||||
|
Warning("Unknown stream id in queued packet (%d)", queued_packet.stream_index );
|
||||||
|
ret = -1;
|
||||||
|
}
|
||||||
if ( ret < 0 ) {
|
if ( ret < 0 ) {
|
||||||
//Less than zero and we skipped a frame
|
//Less than zero and we skipped a frame
|
||||||
av_free_packet( &queued_packet );
|
//av_free_packet( &queued_packet );
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
} // end while packets in the packetqueue
|
} // end while packets in the packetqueue
|
||||||
Debug(2, "Wrote %d queued packets", packet_count );
|
Debug(2, "Wrote %d queued packets", packet_count );
|
||||||
|
} else {
|
||||||
} else if ( ( ! recording ) && wasRecording && videoStore ) {
|
if ( 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.
|
//Buffer video packets
|
||||||
// Need to re-init the videostore with the correct directory and start recording again
|
if ( packet.flags & AV_PKT_FLAG_KEY ) {
|
||||||
// for efficiency's sake, we should test for keyframe before we test for directory change...
|
packetqueue.clearQueue();
|
||||||
if ( recording && wasRecording && (packet.flags & AV_PKT_FLAG_KEY) && (strcmp(oldDirectory, event_file) != 0 ) ) {
|
|
||||||
// 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;
|
|
||||||
videoStore = NULL;
|
|
||||||
}
|
}
|
||||||
|
packetqueue.queuePacket(&packet);
|
||||||
|
} // end if
|
||||||
|
|
||||||
if (record_audio) {
|
|
||||||
if (mAudioStreamId == -1) {
|
|
||||||
Debug(3, "Record Audio on but no audio stream found");
|
|
||||||
videoStore = new VideoStore((const char *) event_file, "mp4",
|
|
||||||
mFormatContext->streams[mVideoStreamId],
|
|
||||||
NULL,
|
|
||||||
startTime,
|
|
||||||
this->getMonitor()->getOrientation() );
|
|
||||||
} else {
|
|
||||||
Debug(3, "Video module initiated with audio stream");
|
|
||||||
videoStore = new VideoStore((const char *) event_file, "mp4",
|
|
||||||
mFormatContext->streams[mVideoStreamId],
|
|
||||||
mFormatContext->streams[mAudioStreamId],
|
|
||||||
startTime,
|
|
||||||
this->getMonitor()->getOrientation() );
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Debug(3, "Record_audio is false so exclude audio stream");
|
|
||||||
videoStore = new VideoStore((const char *) event_file, "mp4",
|
|
||||||
mFormatContext->streams[mVideoStreamId],
|
|
||||||
NULL,
|
|
||||||
startTime,
|
|
||||||
this->getMonitor()->getOrientation() );
|
|
||||||
}
|
|
||||||
strcpy(oldDirectory, event_file);
|
|
||||||
|
|
||||||
// Need to write out all the frames from the last keyframe?
|
if ( packet.stream_index == mVideoStreamId ) {
|
||||||
unsigned int packet_count = 0;
|
#if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0)
|
||||||
while ( packetqueue.popPacket( &queued_packet ) ) {
|
if ( avcodec_decode_video2( mCodecContext, mRawFrame, &frameComplete, &packet ) < 0 )
|
||||||
packet_count += 1;
|
#else
|
||||||
//Write the packet to our video store
|
if ( avcodec_decode_video( mCodecContext, mRawFrame, &frameComplete, packet.data, packet.size ) < 0 )
|
||||||
int ret = videoStore->writeVideoFramePacket(&queued_packet, mFormatContext->streams[mVideoStreamId]);//, &lastKeyframePkt);
|
#endif
|
||||||
if ( ret < 0 ) {
|
Fatal( "Unable to decode frame at frame %d", frameCount );
|
||||||
//Less than zero and we skipped a frame
|
|
||||||
av_free_packet( &queued_packet );
|
Debug( 4, "Decoded video packet at frame %d", frameCount );
|
||||||
return 0;
|
|
||||||
}
|
if ( frameComplete ) {
|
||||||
} // end while packets in the packetqueue
|
Debug( 4, "Got frame %d", frameCount );
|
||||||
Debug(2, "Wrote %d queued packets", packet_count );
|
|
||||||
|
uint8_t* directbuffer;
|
||||||
|
|
||||||
|
/* Request a writeable buffer of the target image */
|
||||||
|
directbuffer = image.WriteBuffer(width, height, colours, subpixelorder);
|
||||||
|
if( directbuffer == NULL ) {
|
||||||
|
Error("Failed requesting writeable buffer for the captured image.");
|
||||||
|
av_free_packet( &packet );
|
||||||
|
return (-1);
|
||||||
}
|
}
|
||||||
|
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
|
||||||
|
|
||||||
if ( videoStore && recording ) {
|
if ( videoStore && recording ) {
|
||||||
//Write the packet to our video store
|
//Write the packet to our video store
|
||||||
|
@ -731,12 +695,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
} // end if frameComplete
|
} // 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
|
||||||
Debug( 4, "Audio stream index %d", packet.stream_index );
|
Debug( 4, "Audio stream index %d", packet.stream_index );
|
||||||
if ( frameComplete ) {
|
if ( videoStore ) {
|
||||||
Debug( 3, "Got audio frame with framecomplete %d", frameCount );
|
|
||||||
//} else {
|
|
||||||
//Debug( 3, "Got audio frame %d without frameComplete", frameCount );
|
|
||||||
}
|
|
||||||
if ( videoStore && recording ) {
|
|
||||||
if ( record_audio ) {
|
if ( record_audio ) {
|
||||||
Debug(3, "Recording audio packet streamindex(%d) packetstreamindex(%d)", mAudioStreamId, packet.stream_index );
|
Debug(3, "Recording audio packet streamindex(%d) packetstreamindex(%d)", mAudioStreamId, packet.stream_index );
|
||||||
//Write the packet to our video store
|
//Write the packet to our video store
|
||||||
|
|
Loading…
Reference in New Issue