From 206ac050939b9b79aebd20f3722e29db065e3e7c Mon Sep 17 00:00:00 2001 From: tianq Date: Thu, 20 Jun 2019 13:34:12 +0800 Subject: [PATCH] fix av sync --- src/remux/mp4-remuxer.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/remux/mp4-remuxer.js b/src/remux/mp4-remuxer.js index 3742e17b..91fc9941 100644 --- a/src/remux/mp4-remuxer.js +++ b/src/remux/mp4-remuxer.js @@ -65,6 +65,8 @@ class MP4Remuxer { this._mp3UseMpegAudio = !Browser.firefox; this._fillAudioTimestampGap = this._config.fixAudioTimestampGap; + + this._deltaAudioTimestapGap = 0; } destroy() { @@ -371,13 +373,15 @@ class MP4Remuxer { let needFillSilentFrames = false; let silentFrames = null; + this._deltaAudioTimestapGap += sampleDuration - refSampleDuration; // Silent frame generation, if large timestamp gap detected && config.fixAudioTimestampGap - if (sampleDuration > refSampleDuration * 1.5 && this._audioMeta.codec !== 'mp3' && this._fillAudioTimestampGap && !Browser.safari) { + if (this._deltaAudioTimestapGap > refSampleDuration && this._audioMeta.codec !== 'mp3' && this._fillAudioTimestampGap && !Browser.safari) { // We need to insert silent frames to fill timestamp gap needFillSilentFrames = true; let delta = Math.abs(sampleDuration - refSampleDuration); - let frameCount = Math.ceil(delta / refSampleDuration); + let frameCount = Math.floor(delta / refSampleDuration); let currentDts = dts + refSampleDuration; // Notice: in float + this._deltaAudioTimestapGap = this._deltaAudioTimestapGap % refSampleDuration; Log.w(this.TAG, 'Large audio timestamp gap detected, may cause AV sync to drift. ' + 'Silent frames will be generated to avoid unsync.\n' + @@ -740,4 +744,4 @@ class MP4Remuxer { } -export default MP4Remuxer; \ No newline at end of file +export default MP4Remuxer;