From a34809bb9dde44a586d68e621b89f63f5690547f Mon Sep 17 00:00:00 2001 From: olly Date: Wed, 4 Aug 2021 14:36:13 +0100 Subject: [PATCH] Tweak use of TimestampAdjuster for seeking - Fix use of getTimestampOffsetUs in TsExtractor where getFirstSampleTimestampUs should have been used. - Don't reset TimestampAdjuster if it's in no-offset mode. - Improve comment clarity #minor-release PiperOrigin-RevId: 388682711 --- .../exoplayer2/extractor/ts/PsExtractor.java | 27 ++++++++------ .../exoplayer2/extractor/ts/TsExtractor.java | 27 ++++++++------ .../ts/sample_h262_mpeg_audio.ts.1.dump | 12 +++---- .../ts/sample_h262_mpeg_audio.ts.2.dump | 12 +++---- .../ts/sample_h262_mpeg_audio.ts.3.dump | 4 +-- .../extractordumps/ts/sample_h263.ts.1.dump | 36 +++++++++---------- .../ts/sample_h264_mpeg_audio.ts.1.dump | 12 +++---- .../ts/sample_h264_mpeg_audio.ts.2.dump | 12 +++---- ...e_h264_no_access_unit_delimiters.ts.1.dump | 8 ++--- ...e_h264_no_access_unit_delimiters.ts.2.dump | 8 ++--- .../extractordumps/ts/sample_scte35.ts.1.dump | 20 +++++------ .../extractordumps/ts/sample_scte35.ts.2.dump | 20 +++++------ .../extractordumps/ts/sample_scte35.ts.3.dump | 6 ++-- .../extractordumps/ts/sample_with_junk.1.dump | 12 +++---- .../extractordumps/ts/sample_with_junk.2.dump | 12 +++---- .../extractordumps/ts/sample_with_junk.3.dump | 4 +-- 16 files changed, 123 insertions(+), 109 deletions(-) diff --git a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ts/PsExtractor.java b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ts/PsExtractor.java index 29ef815b25..4611a25825 100644 --- a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ts/PsExtractor.java +++ b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ts/PsExtractor.java @@ -135,16 +135,23 @@ public final class PsExtractor implements Extractor { @Override public void seek(long position, long timeUs) { - boolean hasNotEncounteredFirstTimestamp = - timestampAdjuster.getTimestampOffsetUs() == C.TIME_UNSET; - if (hasNotEncounteredFirstTimestamp - || (timestampAdjuster.getFirstSampleTimestampUs() != 0 - && timestampAdjuster.getFirstSampleTimestampUs() != timeUs)) { - // - If the timestamp adjuster in the PS stream has not encountered any sample, it's going to - // treat the first timestamp encountered as sample time 0, which is incorrect. In this case, - // we have to set the first sample timestamp manually. - // - If the timestamp adjuster has its timestamp set manually before, and now we seek to a - // different position, we need to set the first sample timestamp manually again. + // If the timestamp adjuster has not yet established a timestamp offset, we need to reset its + // expected first sample timestamp to be the new seek position. Without this, the timestamp + // adjuster would incorrectly establish its timestamp offset assuming that the first sample + // after this seek corresponds to the start of the stream (or a previous seek position, if there + // was one). + boolean resetTimestampAdjuster = timestampAdjuster.getTimestampOffsetUs() == C.TIME_UNSET; + if (!resetTimestampAdjuster) { + long adjusterFirstSampleTimestampUs = timestampAdjuster.getFirstSampleTimestampUs(); + // Also reset the timestamp adjuster if its offset was calculated based on a non-zero position + // in the stream (other than the position being seeked to), since in this case the offset may + // not be accurate. + resetTimestampAdjuster = + adjusterFirstSampleTimestampUs != C.TIME_UNSET + && adjusterFirstSampleTimestampUs != 0 + && adjusterFirstSampleTimestampUs != timeUs; + } + if (resetTimestampAdjuster) { timestampAdjuster.reset(timeUs); } diff --git a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ts/TsExtractor.java b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ts/TsExtractor.java index 1411541e88..1ff9c06e79 100644 --- a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ts/TsExtractor.java +++ b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ts/TsExtractor.java @@ -252,16 +252,23 @@ public final class TsExtractor implements Extractor { int timestampAdjustersCount = timestampAdjusters.size(); for (int i = 0; i < timestampAdjustersCount; i++) { TimestampAdjuster timestampAdjuster = timestampAdjusters.get(i); - boolean hasNotEncounteredFirstTimestamp = - timestampAdjuster.getTimestampOffsetUs() == C.TIME_UNSET; - if (hasNotEncounteredFirstTimestamp - || (timestampAdjuster.getTimestampOffsetUs() != 0 - && timestampAdjuster.getFirstSampleTimestampUs() != timeUs)) { - // - If a track in the TS stream has not encountered any sample, it's going to treat the - // first sample encountered as timestamp 0, which is incorrect. So we have to set the first - // sample timestamp for that track manually. - // - If the timestamp adjuster has its timestamp set manually before, and now we seek to a - // different position, we need to set the first sample timestamp manually again. + // If the timestamp adjuster has not yet established a timestamp offset, we need to reset its + // expected first sample timestamp to be the new seek position. Without this, the timestamp + // adjuster would incorrectly establish its timestamp offset assuming that the first sample + // after this seek corresponds to the start of the stream (or a previous seek position, if + // there was one). + boolean resetTimestampAdjuster = timestampAdjuster.getTimestampOffsetUs() == C.TIME_UNSET; + if (!resetTimestampAdjuster) { + long adjusterFirstSampleTimestampUs = timestampAdjuster.getFirstSampleTimestampUs(); + // Also reset the timestamp adjuster if its offset was calculated based on a non-zero + // position in the stream (other than the position being seeked to), since in this case the + // offset may not be accurate. + resetTimestampAdjuster = + adjusterFirstSampleTimestampUs != C.TIME_UNSET + && adjusterFirstSampleTimestampUs != 0 + && adjusterFirstSampleTimestampUs != timeUs; + } + if (resetTimestampAdjuster) { timestampAdjuster.reset(timeUs); } } diff --git a/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.1.dump b/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.1.dump index 489763bbf8..8e996b6f31 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.1.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.1.dump @@ -17,11 +17,11 @@ track 256: initializationData: data = length 22, hash CE183139 sample 0: - time = 55610 + time = 33366 flags = 1 data = length 20711, hash 34341E8 sample 1: - time = 88977 + time = 66733 flags = 0 data = length 18112, hash EC44B35B track 257: @@ -35,19 +35,19 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 44699 + time = 22455 flags = 1 data = length 1253, hash 727FD1C6 sample 1: - time = 70821 + time = 48577 flags = 1 data = length 1254, hash 73FB07B8 sample 2: - time = 96944 + time = 74700 flags = 1 data = length 1254, hash 73FB07B8 sample 3: - time = 123066 + time = 100822 flags = 1 data = length 1254, hash 73FB07B8 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.2.dump b/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.2.dump index a7ef1ecf4b..8e996b6f31 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.2.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.2.dump @@ -17,11 +17,11 @@ track 256: initializationData: data = length 22, hash CE183139 sample 0: - time = 77854 + time = 33366 flags = 1 data = length 20711, hash 34341E8 sample 1: - time = 111221 + time = 66733 flags = 0 data = length 18112, hash EC44B35B track 257: @@ -35,19 +35,19 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 66943 + time = 22455 flags = 1 data = length 1253, hash 727FD1C6 sample 1: - time = 93065 + time = 48577 flags = 1 data = length 1254, hash 73FB07B8 sample 2: - time = 119188 + time = 74700 flags = 1 data = length 1254, hash 73FB07B8 sample 3: - time = 145310 + time = 100822 flags = 1 data = length 1254, hash 73FB07B8 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.3.dump b/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.3.dump index fffd040d68..f0fd81369f 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.3.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_h262_mpeg_audio.ts.3.dump @@ -27,11 +27,11 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 66733 + time = 74700 flags = 1 data = length 1254, hash 73FB07B8 sample 1: - time = 92855 + time = 100822 flags = 1 data = length 1254, hash 73FB07B8 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_h263.ts.1.dump b/testdata/src/test/assets/extractordumps/ts/sample_h263.ts.1.dump index a83a484f64..e67426081b 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_h263.ts.1.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_h263.ts.1.dump @@ -17,75 +17,75 @@ track 256: initializationData: data = length 47, hash 7696BF67 sample 0: - time = 320000 + time = 240000 flags = 0 data = length 212, hash 835B0727 sample 1: - time = 360000 + time = 280000 flags = 0 data = length 212, hash E9AF0AB7 sample 2: - time = 400000 + time = 320000 flags = 0 data = length 212, hash E9517D06 sample 3: - time = 440000 + time = 360000 flags = 0 data = length 212, hash 4FA58096 sample 4: - time = 480000 + time = 400000 flags = 0 data = length 212, hash 4F47F2E5 sample 5: - time = 520000 + time = 440000 flags = 0 data = length 212, hash B59BF675 sample 6: - time = 560000 + time = 480000 flags = 1 data = length 11769, hash 3ED9DF06 sample 7: - time = 600000 + time = 520000 flags = 0 data = length 230, hash 2AF3505D sample 8: - time = 640000 + time = 560000 flags = 0 data = length 222, hash F4E7436D sample 9: - time = 680000 + time = 600000 flags = 0 data = length 222, hash F0F812FD sample 10: - time = 720000 + time = 640000 flags = 0 data = length 222, hash 18472E8C sample 11: - time = 760000 + time = 680000 flags = 0 data = length 222, hash 1457FE1C sample 12: - time = 800000 + time = 720000 flags = 0 data = length 222, hash 3BA719AB sample 13: - time = 840000 + time = 760000 flags = 0 data = length 222, hash 37B7E93B sample 14: - time = 880000 + time = 800000 flags = 0 data = length 222, hash 5F0704CA sample 15: - time = 920000 + time = 840000 flags = 0 data = length 222, hash 5B17D45A sample 16: - time = 960000 + time = 880000 flags = 0 data = length 222, hash 8266EFE9 sample 17: - time = 1000000 + time = 920000 flags = 0 data = length 222, hash 7E77BF79 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_h264_mpeg_audio.ts.1.dump b/testdata/src/test/assets/extractordumps/ts/sample_h264_mpeg_audio.ts.1.dump index 295fa07e94..605daaf8dd 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_h264_mpeg_audio.ts.1.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_h264_mpeg_audio.ts.1.dump @@ -19,11 +19,11 @@ track 256: data = length 29, hash 4C2CAE9C data = length 9, hash D971CD89 sample 0: - time = 88977 + time = 66733 flags = 1 data = length 12394, hash A39F5311 sample 1: - time = 122344 + time = 100100 flags = 0 data = length 813, hash 99F7B4FA track 257: @@ -37,19 +37,19 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 88977 + time = 66733 flags = 1 data = length 1253, hash 727FD1C6 sample 1: - time = 115099 + time = 92855 flags = 1 data = length 1254, hash 73FB07B8 sample 2: - time = 141221 + time = 118977 flags = 1 data = length 1254, hash 73FB07B8 sample 3: - time = 167343 + time = 145099 flags = 1 data = length 1254, hash 73FB07B8 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_h264_mpeg_audio.ts.2.dump b/testdata/src/test/assets/extractordumps/ts/sample_h264_mpeg_audio.ts.2.dump index 8bf63a3901..605daaf8dd 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_h264_mpeg_audio.ts.2.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_h264_mpeg_audio.ts.2.dump @@ -19,11 +19,11 @@ track 256: data = length 29, hash 4C2CAE9C data = length 9, hash D971CD89 sample 0: - time = 111221 + time = 66733 flags = 1 data = length 12394, hash A39F5311 sample 1: - time = 144588 + time = 100100 flags = 0 data = length 813, hash 99F7B4FA track 257: @@ -37,19 +37,19 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 111221 + time = 66733 flags = 1 data = length 1253, hash 727FD1C6 sample 1: - time = 137343 + time = 92855 flags = 1 data = length 1254, hash 73FB07B8 sample 2: - time = 163465 + time = 118977 flags = 1 data = length 1254, hash 73FB07B8 sample 3: - time = 189587 + time = 145099 flags = 1 data = length 1254, hash 73FB07B8 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_h264_no_access_unit_delimiters.ts.1.dump b/testdata/src/test/assets/extractordumps/ts/sample_h264_no_access_unit_delimiters.ts.1.dump index b53c622489..dbc51ba77d 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_h264_no_access_unit_delimiters.ts.1.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_h264_no_access_unit_delimiters.ts.1.dump @@ -19,19 +19,19 @@ track 256: data = length 29, hash 4C2CAE9C data = length 9, hash D971CD89 sample 0: - time = 88977 + time = 66733 flags = 0 data = length 734, hash AF0D9485 sample 1: - time = 88977 + time = 66733 flags = 1 data = length 10938, hash 68420875 sample 2: - time = 155710 + time = 133466 flags = 0 data = length 6, hash 34E6CF79 sample 3: - time = 155710 + time = 133466 flags = 0 data = length 518, hash 546C177 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_h264_no_access_unit_delimiters.ts.2.dump b/testdata/src/test/assets/extractordumps/ts/sample_h264_no_access_unit_delimiters.ts.2.dump index 89899bb4e1..dbc51ba77d 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_h264_no_access_unit_delimiters.ts.2.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_h264_no_access_unit_delimiters.ts.2.dump @@ -19,19 +19,19 @@ track 256: data = length 29, hash 4C2CAE9C data = length 9, hash D971CD89 sample 0: - time = 111221 + time = 66733 flags = 0 data = length 734, hash AF0D9485 sample 1: - time = 111221 + time = 66733 flags = 1 data = length 10938, hash 68420875 sample 2: - time = 177954 + time = 133466 flags = 0 data = length 6, hash 34E6CF79 sample 3: - time = 177954 + time = 133466 flags = 0 data = length 518, hash 546C177 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.1.dump b/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.1.dump index 7e7eb9ad0c..b9564a3ffc 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.1.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.1.dump @@ -17,11 +17,11 @@ track 256: initializationData: data = length 22, hash CE183139 sample 0: - time = 55610 + time = 33366 flags = 1 data = length 20711, hash 34341E8 sample 1: - time = 88977 + time = 66733 flags = 0 data = length 18112, hash EC44B35B track 257: @@ -35,19 +35,19 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 44699 + time = 22455 flags = 1 data = length 1253, hash 727FD1C6 sample 1: - time = 70821 + time = 48577 flags = 1 data = length 1254, hash 73FB07B8 sample 2: - time = 96944 + time = 74700 flags = 1 data = length 1254, hash 73FB07B8 sample 3: - time = 123066 + time = 100822 flags = 1 data = length 1254, hash 73FB07B8 track 600: @@ -55,17 +55,17 @@ track 600: sample count = 3 format 0: sampleMimeType = application/x-scte35 - subsampleOffsetUs = -1377756 + subsampleOffsetUs = -1400000 sample 0: - time = 55610 + time = 33366 flags = 1 data = length 35, hash A892AAAF sample 1: - time = 55610 + time = 33366 flags = 1 data = length 35, hash A892AAAF sample 2: - time = 55610 + time = 33366 flags = 1 data = length 35, hash DFA3EF74 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.2.dump b/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.2.dump index 2db85e42e0..b9564a3ffc 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.2.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.2.dump @@ -17,11 +17,11 @@ track 256: initializationData: data = length 22, hash CE183139 sample 0: - time = 77854 + time = 33366 flags = 1 data = length 20711, hash 34341E8 sample 1: - time = 111221 + time = 66733 flags = 0 data = length 18112, hash EC44B35B track 257: @@ -35,19 +35,19 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 66943 + time = 22455 flags = 1 data = length 1253, hash 727FD1C6 sample 1: - time = 93065 + time = 48577 flags = 1 data = length 1254, hash 73FB07B8 sample 2: - time = 119188 + time = 74700 flags = 1 data = length 1254, hash 73FB07B8 sample 3: - time = 145310 + time = 100822 flags = 1 data = length 1254, hash 73FB07B8 track 600: @@ -55,17 +55,17 @@ track 600: sample count = 3 format 0: sampleMimeType = application/x-scte35 - subsampleOffsetUs = -1355512 + subsampleOffsetUs = -1400000 sample 0: - time = 77854 + time = 33366 flags = 1 data = length 35, hash A892AAAF sample 1: - time = 77854 + time = 33366 flags = 1 data = length 35, hash A892AAAF sample 2: - time = 77854 + time = 33366 flags = 1 data = length 35, hash DFA3EF74 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.3.dump b/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.3.dump index cc1c346b49..e96f346e29 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.3.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_scte35.ts.3.dump @@ -27,11 +27,11 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 66733 + time = 74700 flags = 1 data = length 1254, hash 73FB07B8 sample 1: - time = 92855 + time = 100822 flags = 1 data = length 1254, hash 73FB07B8 track 600: @@ -39,5 +39,5 @@ track 600: sample count = 0 format 0: sampleMimeType = application/x-scte35 - subsampleOffsetUs = -1355512 + subsampleOffsetUs = -1400000 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_with_junk.1.dump b/testdata/src/test/assets/extractordumps/ts/sample_with_junk.1.dump index c1b7f37bf3..f61ecb193a 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_with_junk.1.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_with_junk.1.dump @@ -17,11 +17,11 @@ track 256: initializationData: data = length 22, hash CE183139 sample 0: - time = 55610 + time = 33366 flags = 1 data = length 20711, hash 34341E8 sample 1: - time = 88977 + time = 66733 flags = 0 data = length 18112, hash EC44B35B track 257: @@ -35,19 +35,19 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 44699 + time = 22455 flags = 1 data = length 1253, hash 727FD1C6 sample 1: - time = 70821 + time = 48577 flags = 1 data = length 1254, hash 73FB07B8 sample 2: - time = 96944 + time = 74700 flags = 1 data = length 1254, hash 73FB07B8 sample 3: - time = 123066 + time = 100822 flags = 1 data = length 1254, hash 73FB07B8 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_with_junk.2.dump b/testdata/src/test/assets/extractordumps/ts/sample_with_junk.2.dump index 4c0bb6d4ab..f61ecb193a 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_with_junk.2.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_with_junk.2.dump @@ -17,11 +17,11 @@ track 256: initializationData: data = length 22, hash CE183139 sample 0: - time = 77854 + time = 33366 flags = 1 data = length 20711, hash 34341E8 sample 1: - time = 111221 + time = 66733 flags = 0 data = length 18112, hash EC44B35B track 257: @@ -35,19 +35,19 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 66943 + time = 22455 flags = 1 data = length 1253, hash 727FD1C6 sample 1: - time = 93065 + time = 48577 flags = 1 data = length 1254, hash 73FB07B8 sample 2: - time = 119188 + time = 74700 flags = 1 data = length 1254, hash 73FB07B8 sample 3: - time = 145310 + time = 100822 flags = 1 data = length 1254, hash 73FB07B8 tracksEnded = true diff --git a/testdata/src/test/assets/extractordumps/ts/sample_with_junk.3.dump b/testdata/src/test/assets/extractordumps/ts/sample_with_junk.3.dump index 3bdb0839ae..417481edc3 100644 --- a/testdata/src/test/assets/extractordumps/ts/sample_with_junk.3.dump +++ b/testdata/src/test/assets/extractordumps/ts/sample_with_junk.3.dump @@ -27,11 +27,11 @@ track 257: sampleRate = 44100 language = und sample 0: - time = 66733 + time = 74700 flags = 1 data = length 1254, hash 73FB07B8 sample 1: - time = 92855 + time = 100822 flags = 1 data = length 1254, hash 73FB07B8 tracksEnded = true