diff --git a/packager/app/test/testdata/live-profile-and-encryption-and-mult-files/output.mpd b/packager/app/test/testdata/live-profile-and-encryption-and-mult-files/output.mpd
index 5f6ea3d243..0feb548297 100644
--- a/packager/app/test/testdata/live-profile-and-encryption-and-mult-files/output.mpd
+++ b/packager/app/test/testdata/live-profile-and-encryption-and-mult-files/output.mpd
@@ -7,8 +7,8 @@
AAAANHBzc2gBAAAAEHfv7MCyTQKs4zweUuL7SwAAAAExMjM0NTY3ODkwMTIzNDU2AAAAAA==
-
-
+
+
@@ -23,8 +23,8 @@
-
-
+
+
@@ -37,17 +37,7 @@
AAAANHBzc2gBAAAAEHfv7MCyTQKs4zweUuL7SwAAAAExMjM0NTY3ODkwMTIzNDU2AAAAAA==
-
-
-
-
-
-
-
-
-
-
-
+
@@ -57,6 +47,16 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/packager/app/test/testdata/live-profile-and-key-rotation-and-non-dash-if-iop/bear-640x360-audio-1.m4s b/packager/app/test/testdata/live-profile-and-key-rotation-and-non-dash-if-iop/bear-640x360-audio-1.m4s
index 9996ce2afd..f88b004a5e 100644
Binary files a/packager/app/test/testdata/live-profile-and-key-rotation-and-non-dash-if-iop/bear-640x360-audio-1.m4s and b/packager/app/test/testdata/live-profile-and-key-rotation-and-non-dash-if-iop/bear-640x360-audio-1.m4s differ
diff --git a/packager/app/test/testdata/live-profile-and-key-rotation-and-non-dash-if-iop/bear-640x360-video-1.m4s b/packager/app/test/testdata/live-profile-and-key-rotation-and-non-dash-if-iop/bear-640x360-video-1.m4s
index b27575a8b2..5c18184d26 100644
Binary files a/packager/app/test/testdata/live-profile-and-key-rotation-and-non-dash-if-iop/bear-640x360-video-1.m4s and b/packager/app/test/testdata/live-profile-and-key-rotation-and-non-dash-if-iop/bear-640x360-video-1.m4s differ
diff --git a/packager/app/test/testdata/live-profile-and-key-rotation/bear-640x360-audio-1.m4s b/packager/app/test/testdata/live-profile-and-key-rotation/bear-640x360-audio-1.m4s
index 9996ce2afd..f88b004a5e 100644
Binary files a/packager/app/test/testdata/live-profile-and-key-rotation/bear-640x360-audio-1.m4s and b/packager/app/test/testdata/live-profile-and-key-rotation/bear-640x360-audio-1.m4s differ
diff --git a/packager/app/test/testdata/live-profile-and-key-rotation/bear-640x360-video-1.m4s b/packager/app/test/testdata/live-profile-and-key-rotation/bear-640x360-video-1.m4s
index b27575a8b2..5c18184d26 100644
Binary files a/packager/app/test/testdata/live-profile-and-key-rotation/bear-640x360-video-1.m4s and b/packager/app/test/testdata/live-profile-and-key-rotation/bear-640x360-video-1.m4s differ
diff --git a/packager/media/base/widevine_key_source.cc b/packager/media/base/widevine_key_source.cc
index 2df58243b7..c914d1926d 100644
--- a/packager/media/base/widevine_key_source.cc
+++ b/packager/media/base/widevine_key_source.cc
@@ -221,8 +221,9 @@ Status WidevineKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index,
first_crypto_period_index_ =
crypto_period_index ? crypto_period_index - 1 : 0;
DCHECK(!key_pool_);
- key_pool_.reset(new EncryptionKeyQueue(crypto_period_count_,
- first_crypto_period_index_));
+ const size_t queue_size = crypto_period_count_ * 10;
+ key_pool_.reset(
+ new EncryptionKeyQueue(queue_size, first_crypto_period_index_));
start_key_production_.Signal();
key_production_started_ = true;
}
diff --git a/packager/media/base/widevine_key_source_unittest.cc b/packager/media/base/widevine_key_source_unittest.cc
index 5a3a4f4eec..a0f11f7639 100644
--- a/packager/media/base/widevine_key_source_unittest.cc
+++ b/packager/media/base/widevine_key_source_unittest.cc
@@ -564,9 +564,9 @@ TEST_P(WidevineKeySourceParameterizedTest, KeyRotationTest) {
const uint32_t kCryptoPeriodCount = 10;
// Array of indexes to be checked.
const uint32_t kCryptoPeriodIndexes[] = {
- kFirstCryptoPeriodIndex, 17, 37, 38, 36, 39};
- // Derived from kCryptoPeriodIndexes: ceiling((39 - 8 ) / 10).
- const uint32_t kCryptoIterations = 4;
+ kFirstCryptoPeriodIndex, 17, 37, 38, 36, 89};
+ // Derived from kCryptoPeriodIndexes: ceiling((89 - 8 ) / 10).
+ const uint32_t kCryptoIterations = 9;
// Generate expectations in sequence.
InSequence dummy;
@@ -597,6 +597,9 @@ TEST_P(WidevineKeySourceParameterizedTest, KeyRotationTest) {
EXPECT_CALL(*mock_key_fetcher_, FetchKeys(_, _, _))
.WillOnce(DoAll(SetArgPointee<2>(mock_response), Return(Status::OK)));
}
+ // Fail future requests.
+ EXPECT_CALL(*mock_request_signer_, GenerateSignature(_, _))
+ .WillRepeatedly(Return(false));
CreateWidevineKeySource();
widevine_key_source_->set_signer(std::move(mock_request_signer_));
diff --git a/packager/media/crypto/encryption_handler.cc b/packager/media/crypto/encryption_handler.cc
index 86f67ec22d..3d23acf952 100644
--- a/packager/media/crypto/encryption_handler.cc
+++ b/packager/media/crypto/encryption_handler.cc
@@ -227,8 +227,10 @@ Status EncryptionHandler::ProcessMediaSample(
// need to be encrypted, so we can signal encryption metadata earlier to
// allows clients to prefetch the keys.
if (check_new_crypto_period_) {
- const int64_t current_crypto_period_index =
- clear_sample->dts() / crypto_period_duration_;
+ // |dts| can be negative, e.g. after EditList adjustments. Normalized to 0
+ // in that case.
+ const int64_t dts = std::max(clear_sample->dts(), static_cast(0));
+ const int64_t current_crypto_period_index = dts / crypto_period_duration_;
if (current_crypto_period_index != prev_crypto_period_index_) {
EncryptionKey encryption_key;
Status status = key_source_->GetCryptoPeriodKey(