diff --git a/src/zm_camera.cpp b/src/zm_camera.cpp index aa90a6be4..c56cc34c7 100644 --- a/src/zm_camera.cpp +++ b/src/zm_camera.cpp @@ -55,6 +55,8 @@ Camera::Camera( mAudioStream(nullptr), mFormatContext(nullptr), mSecondFormatContext(nullptr), + mFirstVideoPTS(0), + mFirstAudioPTS(0), mLastVideoPTS(0), mLastAudioPTS(0), bytes(0) diff --git a/src/zm_camera.h b/src/zm_camera.h index 5fd2a4bc9..fb117dcfb 100644 --- a/src/zm_camera.h +++ b/src/zm_camera.h @@ -58,6 +58,8 @@ protected: AVStream *mAudioStream; AVFormatContext *mFormatContext; // One for video, one for audio AVFormatContext *mSecondFormatContext; // One for video, one for audio + int64_t mFirstVideoPTS; + int64_t mFirstAudioPTS; int64_t mLastVideoPTS; int64_t mLastAudioPTS; unsigned int bytes; diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index 145c53221..85b711b1f 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -200,7 +200,10 @@ int FfmpegCamera::Capture(ZMPacket &zm_packet) { ) ) { // if audio stream is behind video stream, then read from audio, otherwise video mFormatContextPtr = mSecondFormatContext; - Debug(4, "Using audio input"); + Debug(4, "Using audio input because audio PTS %" PRId64 " < video PTS %" PRId64, + av_rescale_q(mLastAudioPTS, mAudioStream->time_base, AV_TIME_BASE_Q), + av_rescale_q(mLastVideoPTS, mVideoStream->time_base, AV_TIME_BASE_Q) + ); } else { mFormatContextPtr = mFormatContext; Debug(4, "Using video input because %" PRId64 " >= %" PRId64, @@ -240,9 +243,15 @@ int FfmpegCamera::Capture(ZMPacket &zm_packet) { zm_packet.pts = av_rescale_q(packet.pts, stream->time_base, AV_TIME_BASE_Q); if ( packet.pts != AV_NOPTS_VALUE ) { if ( stream == mVideoStream ) { - mLastVideoPTS = packet.pts; + if (mFirstVideoPTS == AV_NOPTS_VALUE) + mFirstVideoPTS = packet.pts; + + mLastVideoPTS = packet.pts - mFirstVideoPTS; } else { - mLastAudioPTS = packet.pts; + if (mFirstAudioPTS == AV_NOPTS_VALUE) + mFirstAudioPTS = packet.pts; + + mLastAudioPTS = packet.pts - mFirstAudioPTS; } } zm_av_packet_unref(&packet);