From 19a60b01da16e23fa87500a693f8cf6655099c77 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 20 Sep 2016 16:59:43 -0400 Subject: [PATCH] maybe fixes, maybe big memleaks --- src/zm_ffmpeg_camera.cpp | 9 +++++---- src/zm_packetqueue.cpp | 14 +++++++++++--- src/zm_packetqueue.h | 1 + src/zm_videostore.cpp | 20 +++++++++++++++++--- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index d04aca2db..bd72ae3a9 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -167,7 +167,7 @@ int FfmpegCamera::Capture( Image &image ) Error( "Unable to read packet from stream %d: error %d \"%s\".", packet.stream_index, avResult, errbuf ); return( -1 ); } - Debug( 5, "Got packet from stream %d", packet.stream_index ); + Debug( 5, "Got packet from stream %d dts (%d) pts(%d)", packet.stream_index, packet.pts, packet.dts ); // What about audio stream? Maybe someday we could do sound detection... if ( packet.stream_index == mVideoStreamId ) { #if LIBAVCODEC_VERSION_CHECK(52, 23, 0, 23, 0) @@ -290,7 +290,6 @@ int FfmpegCamera::OpenFfmpeg() { Debug ( 1, "Opened input" ); Info( "Stream open %s", mPath.c_str() ); - startTime=av_gettime();//FIXME here or after find_Stream_info //FIXME can speed up initial analysis but need sensible parameters... //mFormatContext->probesize = 32; @@ -305,6 +304,7 @@ int FfmpegCamera::OpenFfmpeg() { #endif Fatal( "Unable to find stream info from %s due to: %s", mPath.c_str(), strerror(errno) ); + startTime=av_gettime();//FIXME here or after find_Stream_info Debug ( 1, "Got stream info" ); // Find first video stream present @@ -584,7 +584,7 @@ Debug(5, "After av_read_frame (%d)", ret ); Error( "Unable to read packet from stream %d: error %d \"%s\".", packet->stream_index, ret, errbuf ); return( -1 ); } - Debug( 5, "Got packet from stream %d", packet->stream_index ); + Debug( 4, "Got packet from stream %d dts (%d) pts(%d)", packet->stream_index, packet->dts, packet->pts ); //Video recording if ( recording ) { @@ -636,7 +636,7 @@ Debug(5, "After av_read_frame (%d)", ret ); while ( ( queued_packet = packetqueue.popPacket() ) ) { packet_count += 1; //Write the packet to our video store - Debug(2, "Writing queued packet stream: %d KEY %d", queued_packet->stream_index, packet->flags & AV_PKT_FLAG_KEY ); + Debug(2, "Writing queued packet stream: %d KEY %d, remaining (%d)", queued_packet->stream_index, packet->flags & AV_PKT_FLAG_KEY, packetqueue.size() ); if ( queued_packet->stream_index == mVideoStreamId ) { ret = videoStore->writeVideoFramePacket( queued_packet, mFormatContext->streams[mVideoStreamId]); } else if ( queued_packet->stream_index == mAudioStreamId ) { @@ -748,6 +748,7 @@ Debug(5, "After av_read_frame (%d)", ret ); Debug( 3, "Some other stream index %d", packet->stream_index ); #endif } + if ( videoStore ) zm_av_unref_packet( packet ); } // end while ! frameComplete return (frameCount); diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index 42a74af98..5758e8871 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -37,10 +37,14 @@ bool zm_packetqueue::queuePacket( AVPacket* packet ) { //AVPacket *input_ref = (AVPacket *)av_malloc(sizeof(AVPacket)); //if ( av_packet_ref( input_ref, packet ) < 0 ) { - //free(input_ref); - //return false; - //} + if ( av_packet_ref( packet, packet ) < 0 ) { +Error("error refing packet"); + //av_free_packet(input_ref); + return false; + } + pktQueue.push( packet ); + //pktQueue.push( input_ref ); return true; } @@ -66,3 +70,7 @@ void zm_packetqueue::clearQueue() { zm_av_unref_packet( packet ); } } + +unsigned int zm_packetqueue::size() { + return pktQueue.size(); +} diff --git a/src/zm_packetqueue.h b/src/zm_packetqueue.h index f79502996..60a9620a4 100644 --- a/src/zm_packetqueue.h +++ b/src/zm_packetqueue.h @@ -38,6 +38,7 @@ public: bool popVideoPacket(AVPacket* packet); bool popAudioPacket(AVPacket* packet); void clearQueue( ); + unsigned int size(); private: std::queue pktQueue; diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index 311318a90..95dc9dd7f 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -274,6 +274,7 @@ Debug(2, "Have audio_output_context"); startDts = 0; filter_in_rescale_delta_last = AV_NOPTS_VALUE; + // now - when streaming started startTime=av_gettime()-nStartTime;//oc->start_time; Info("VideoStore startTime=%d\n",startTime); } // VideoStore::VideoStore @@ -342,26 +343,39 @@ int VideoStore::writeVideoFramePacket(AVPacket *ipkt, AVStream *input_video_stre Debug(2, "writeVideoFrame init_packet"); av_init_packet(&opkt); +if ( 1 ) { + if ( ! startPts ) { + //never gets set, so the first packet can set it. + startPts = ipkt->pts; + } //Scale the PTS of the outgoing packet to be the correct time base if (ipkt->pts != AV_NOPTS_VALUE) { - opkt.pts = av_rescale_q(ipkt->pts-startPts, input_video_stream->time_base, video_stream->time_base) - ost_tb_start_time; + opkt.pts = av_rescale_q(ipkt->pts-startPts, input_video_stream->time_base, video_stream->time_base); + //- ost_tb_start_time; Debug(3, "opkt.pts = %d from ipkt->pts(%d) - startPts(%d), input->time_base(%d) video_stream->time-base(%d)", opkt.pts, ipkt->pts, startPts, input_video_stream->time_base, video_stream->time_base ); } else { + Debug(3, "opkt.pts = undef"); opkt.pts = AV_NOPTS_VALUE; } + if ( ! startDts ) { + startDts = input_video_stream->cur_dts; +} //Scale the DTS of the outgoing packet to be the correct time base if(ipkt->dts == AV_NOPTS_VALUE) { opkt.dts = av_rescale_q(input_video_stream->cur_dts-startDts, AV_TIME_BASE_Q, video_stream->time_base); - Debug(3, "opkt.dts = %d from input_video_stream->cur_dts(%d) - startDts(%d), video_stream->time-base(%d)", opkt.dts, input_video_stream->cur_dts, startDts, video_stream->time_base ); + Debug(3, "opkt.dts = %d from input_video_stream->cur_dts(%d) - startDts(%d), video_stream->time-base(%d)", opkt.dts, input_video_stream->cur_dts, startDts, video_stream->time_base ); } else { opkt.dts = av_rescale_q(ipkt->dts-startDts, input_video_stream->time_base, video_stream->time_base); Debug(3, "opkt.dts = %d from ipkt->dts(%d) - startDts(%d), video_stream->time-base(%d)", opkt.dts, ipkt->dts, startDts, video_stream->time_base ); } - opkt.dts -= ost_tb_start_time; + //opkt.dts -= ost_tb_start_time; opkt.duration = av_rescale_q(ipkt->duration, input_video_stream->time_base, video_stream->time_base); +} else { + av_packet_rescale_ts( &opkt, input_video_stream->time_base, video_stream->time_base ); +} opkt.flags = ipkt->flags; opkt.pos=-1;