rework to dynamically allocate packet instead of putting it on the stack. Plus packet freeing cleanups
This commit is contained in:
parent
3a06ba7a65
commit
8148e35ec6
|
@ -222,11 +222,7 @@ int FfmpegCamera::Capture( Image &image )
|
||||||
} else {
|
} else {
|
||||||
Debug( 4, "Different stream_index %d", packet.stream_index );
|
Debug( 4, "Different stream_index %d", packet.stream_index );
|
||||||
} // end if packet.stream_index == mVideoStreamId
|
} // end if packet.stream_index == mVideoStreamId
|
||||||
#if LIBAVCODEC_VERSION_CHECK(57, 8, 0, 12, 100)
|
zm_av_unref_packet( &packet );
|
||||||
av_packet_unref( &packet);
|
|
||||||
#else
|
|
||||||
av_free_packet( &packet );
|
|
||||||
#endif
|
|
||||||
} // end while ! frameComplete
|
} // end while ! frameComplete
|
||||||
return (0);
|
return (0);
|
||||||
} // FfmpegCamera::Capture
|
} // FfmpegCamera::Capture
|
||||||
|
@ -550,8 +546,9 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
mReopenThread = 0;
|
mReopenThread = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
AVPacket packet;
|
// We are now allocating dynamically because we need to queue these and may go out of scope.
|
||||||
AVPacket queued_packet;
|
AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket));
|
||||||
|
av_init_packet( packet);
|
||||||
|
|
||||||
if (mVideoCodecContext->codec_id != AV_CODEC_ID_H264) {
|
if (mVideoCodecContext->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." );
|
||||||
|
@ -559,7 +556,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
|
|
||||||
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);
|
||||||
|
@ -573,10 +570,10 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
ReopenFfmpeg();
|
ReopenFfmpeg();
|
||||||
}
|
}
|
||||||
|
|
||||||
Error( "Unable to read packet from stream %d: error %d \"%s\".", packet.stream_index, avResult, errbuf );
|
Error( "Unable to read packet from stream %d: error %d \"%s\".", packet->stream_index, avResult, errbuf );
|
||||||
return( -1 );
|
return( -1 );
|
||||||
}
|
}
|
||||||
Debug( 5, "Got packet from stream %d", packet.stream_index );
|
Debug( 5, "Got packet from stream %d", packet->stream_index );
|
||||||
|
|
||||||
//Video recording
|
//Video recording
|
||||||
if ( recording ) {
|
if ( recording ) {
|
||||||
|
@ -584,7 +581,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
// The directory we are recording to is no longer tied to the current event.
|
// 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
|
// 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...
|
// 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 ) ) {
|
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?...
|
// 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 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");
|
||||||
|
@ -624,21 +621,22 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
|
|
||||||
// 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 ) ) {
|
AVPacket *queued_packet;
|
||||||
|
while ( ( queued_packet = packetqueue.popPacket() ) ) {
|
||||||
packet_count += 1;
|
packet_count += 1;
|
||||||
//Write the packet to our video store
|
//Write the packet to our video store
|
||||||
if ( queued_packet.stream_index == mVideoStreamId ) {
|
if ( queued_packet->stream_index == mVideoStreamId ) {
|
||||||
ret = videoStore->writeVideoFramePacket(&queued_packet, mFormatContext->streams[mVideoStreamId]);
|
ret = videoStore->writeVideoFramePacket( queued_packet, mFormatContext->streams[mVideoStreamId]);
|
||||||
} else if ( queued_packet.stream_index == mAudioStreamId ) {
|
} else if ( queued_packet->stream_index == mAudioStreamId ) {
|
||||||
//ret = videoStore->writeAudioFramePacket(&queued_packet, mFormatContext->streams[mAudioStreamId]);
|
//ret = videoStore->writeAudioFramePacket(&queued_packet, mFormatContext->streams[mAudioStreamId]);
|
||||||
} else {
|
} else {
|
||||||
Warning("Unknown stream id in queued packet (%d)", queued_packet.stream_index );
|
Warning("Unknown stream id in queued packet (%d)", queued_packet->stream_index );
|
||||||
ret = -1;
|
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 );
|
|
||||||
}
|
}
|
||||||
|
zm_av_unref_packet( queued_packet );
|
||||||
} // 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 );
|
||||||
} // end if ! wasRecording
|
} // end if ! wasRecording
|
||||||
|
@ -650,17 +648,17 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
}
|
}
|
||||||
|
|
||||||
//Buffer video packets
|
//Buffer video packets
|
||||||
if ( packet.flags & AV_PKT_FLAG_KEY ) {
|
if ( packet->flags & AV_PKT_FLAG_KEY ) {
|
||||||
packetqueue.clearQueue();
|
packetqueue.clearQueue();
|
||||||
}
|
}
|
||||||
packetqueue.queuePacket(&packet);
|
packetqueue.queuePacket(packet);
|
||||||
} // end if
|
} // end if
|
||||||
|
|
||||||
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(mVideoCodecContext, mRawFrame, &frameComplete, &packet) < 0)
|
if (avcodec_decode_video2(mVideoCodecContext, mRawFrame, &frameComplete, packet) < 0)
|
||||||
#else
|
#else
|
||||||
if (avcodec_decode_video(mVideoCodecContext, mRawFrame, &frameComplete, packet.data, packet.size) < 0)
|
if (avcodec_decode_video(mVideoCodecContext, mRawFrame, &frameComplete, packet->data, packet->size) < 0)
|
||||||
#endif
|
#endif
|
||||||
Fatal( "Unable to decode frame at frame %d", frameCount );
|
Fatal( "Unable to decode frame at frame %d", frameCount );
|
||||||
|
|
||||||
|
@ -675,16 +673,16 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
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.");
|
||||||
av_free_packet( &packet );
|
zm_av_unref_packet( packet );
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
avpicture_fill( (AVPicture *)mFrame, directbuffer, imagePixFormat, width, height);
|
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
|
||||||
int ret = videoStore->writeVideoFramePacket(&packet, mFormatContext->streams[mVideoStreamId]);//, &lastKeyframePkt);
|
int ret = videoStore->writeVideoFramePacket( packet, mFormatContext->streams[mVideoStreamId] );
|
||||||
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 );
|
zm_av_unref_packet( packet );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -713,16 +711,16 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
} else {
|
} else {
|
||||||
Debug( 3, "Not framecomplete after av_read_frame" );
|
Debug( 3, "Not framecomplete after av_read_frame" );
|
||||||
} // 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 ( videoStore ) {
|
if ( videoStore ) {
|
||||||
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
|
||||||
//FIXME no relevance of last key frame
|
//FIXME no relevance of last key frame
|
||||||
int ret = videoStore->writeAudioFramePacket( &packet, mFormatContext->streams[packet.stream_index] );
|
int ret = videoStore->writeAudioFramePacket( packet, mFormatContext->streams[packet->stream_index] );
|
||||||
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 );
|
zm_av_unref_packet( packet );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -731,12 +729,12 @@ int FfmpegCamera::CaptureAndRecord( Image &image, bool recording, char* event_fi
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
#if LIBAVUTIL_VERSION_CHECK(54, 23, 0, 23, 0)
|
#if LIBAVUTIL_VERSION_CHECK(54, 23, 0, 23, 0)
|
||||||
Debug( 3, "Some other stream index %d, %s", packet.stream_index, av_get_media_type_string( mFormatContext->streams[packet.stream_index]->codec->codec_type) );
|
Debug( 3, "Some other stream index %d, %s", packet->stream_index, av_get_media_type_string( mFormatContext->streams[packet->stream_index]->codec->codec_type) );
|
||||||
#else
|
#else
|
||||||
Debug( 3, "Some other stream index %d", packet.stream_index );
|
Debug( 3, "Some other stream index %d", packet->stream_index );
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
av_free_packet( &packet );
|
zm_av_unref_packet( packet );
|
||||||
} // end while ! frameComplete
|
} // end while ! frameComplete
|
||||||
return (frameCount);
|
return (frameCount);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue