diff --git a/packager/app/test/testdata/opus-vp9-mp4-with-encryption/bear-320x240-vp9-opus-audio.mp4 b/packager/app/test/testdata/opus-vp9-mp4-with-encryption/bear-320x240-vp9-opus-audio.mp4
index 646ae1c329..db1cc3a4ae 100644
Binary files a/packager/app/test/testdata/opus-vp9-mp4-with-encryption/bear-320x240-vp9-opus-audio.mp4 and b/packager/app/test/testdata/opus-vp9-mp4-with-encryption/bear-320x240-vp9-opus-audio.mp4 differ
diff --git a/packager/app/test/testdata/opus-vp9-mp4-with-encryption/decrypted-bear-320x240-vp9-opus-audio-0.mp4 b/packager/app/test/testdata/opus-vp9-mp4-with-encryption/decrypted-bear-320x240-vp9-opus-audio-0.mp4
index 54a091c00c..4332c1ac88 100644
Binary files a/packager/app/test/testdata/opus-vp9-mp4-with-encryption/decrypted-bear-320x240-vp9-opus-audio-0.mp4 and b/packager/app/test/testdata/opus-vp9-mp4-with-encryption/decrypted-bear-320x240-vp9-opus-audio-0.mp4 differ
diff --git a/packager/app/test/testdata/opus-vp9-mp4-with-encryption/output.mpd b/packager/app/test/testdata/opus-vp9-mp4-with-encryption/output.mpd
index 159d99300f..fcf3d57d14 100644
--- a/packager/app/test/testdata/opus-vp9-mp4-with-encryption/output.mpd
+++ b/packager/app/test/testdata/opus-vp9-mp4-with-encryption/output.mpd
@@ -10,8 +10,8 @@
bear-320x240-vp9-opus-audio.mp4
-
-
+
+
diff --git a/packager/media/formats/mp4/mp4_muxer.cc b/packager/media/formats/mp4/mp4_muxer.cc
index 319727c75d..3c4309e6c3 100644
--- a/packager/media/formats/mp4/mp4_muxer.cc
+++ b/packager/media/formats/mp4/mp4_muxer.cc
@@ -135,6 +135,16 @@ void GenerateSinf(FourCC old_type,
track_encryption.default_kid = encryption_config.key_id;
}
+// The roll distance is expressed in sample units and always takes negative
+// values.
+int16_t GetRollDistance(uint64_t seek_preroll_ns, uint32_t sampling_frequency) {
+ const double kNanosecondsPerSecond = 1000000000;
+ const double preroll_in_samples =
+ seek_preroll_ns / kNanosecondsPerSecond * sampling_frequency;
+ // Round to closest integer.
+ return -static_cast(preroll_in_samples + 0.5);
+}
+
} // namespace
MP4Muxer::MP4Muxer(const MuxerOptions& options) : Muxer(options) {}
@@ -497,34 +507,16 @@ bool MP4Muxer::GenerateAudioTrak(const AudioStreamInfo* audio_info,
entry.format = FOURCC_enca;
}
- // Opus requires at least one sample group description box and at least one
- // sample to group box with grouping type 'roll' within sample table box.
- if (audio_info->codec() == kCodecOpus) {
+ if (audio_info->seek_preroll_ns() > 0) {
sample_table.sample_group_descriptions.resize(1);
SampleGroupDescription& sample_group_description =
sample_table.sample_group_descriptions.back();
sample_group_description.grouping_type = FOURCC_roll;
sample_group_description.audio_roll_recovery_entries.resize(1);
- // The roll distance is expressed in sample units and always takes negative
- // values.
- const uint64_t kNanosecondsPerSecond = 1000000000ull;
sample_group_description.audio_roll_recovery_entries[0].roll_distance =
- (0 - (audio_info->seek_preroll_ns() * audio.samplerate +
- kNanosecondsPerSecond / 2)) /
- kNanosecondsPerSecond;
-
- sample_table.sample_to_groups.resize(1);
- SampleToGroup& sample_to_group = sample_table.sample_to_groups.back();
- sample_to_group.grouping_type = FOURCC_roll;
-
- sample_to_group.entries.resize(1);
- SampleToGroupEntry& sample_to_group_entry = sample_to_group.entries.back();
- // All samples are in track fragments.
- sample_to_group_entry.sample_count = 0;
- sample_to_group_entry.group_description_index =
- SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
- } else if (audio_info->seek_preroll_ns() != 0) {
- LOG(WARNING) << "Unexpected seek preroll for codec " << audio_info->codec();
+ GetRollDistance(audio_info->seek_preroll_ns(), audio.samplerate);
+ // sample to group box is not allowed in the init segment per CMAF
+ // specification. It is put in the fragment instead.
}
return true;
}