diff --git a/CHANGELOG.md b/CHANGELOG.md index f47e420..c4010c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `cmd/mp4ff-encrypt` did not parse command line - `SeigSampleGroupEntry` calculated skipBytes incorrectly - `cmd/mp4ff-pslister` did not parse annex B HEVC correctly +- error when decrypting and re-encrypting a segement (issue #378) ### Removed diff --git a/mp4/crypto.go b/mp4/crypto.go index 7a069c8..2e74d4e 100644 --- a/mp4/crypto.go +++ b/mp4/crypto.go @@ -596,6 +596,9 @@ func DecryptFragment(frag *Fragment, di DecryptInfo, key []byte) error { trun.DataOffset -= int32(nrBytesRemoved) } } + if frag.Mdat.StartPos > frag.Moof.StartPos { + frag.Mdat.StartPos -= nrBytesRemoved + } return nil } diff --git a/mp4/crypto_test.go b/mp4/crypto_test.go index 4dfdf92..b74433f 100644 --- a/mp4/crypto_test.go +++ b/mp4/crypto_test.go @@ -213,6 +213,22 @@ func TestEncryptDecrypt(t *testing.T) { if !bytes.Equal(rawSeg, decSegBuf.Bytes()) { t.Errorf("segment not equal after encryption+decryption") } + + // Make a new encryption to check that the decrypted segment is OK + // for re-encryption (Issue #378). + + pd2, err := InitProtect(encInit.Init, key, iv, c.scheme, kidUUID, nil) + if err != nil { + t.Error(err) + } + for _, s := range decode.Segments { + for _, f := range s.Fragments { + err := EncryptFragment(f, key, iv, pd2) + if err != nil { + t.Errorf("Error re-encrypting fragment: %v\n", err) + } + } + } }) } } @@ -238,3 +254,7 @@ func TestDecryptInit(t *testing.T) { } } } + +func TestDecryptEncrypt(t *testing.T) { + +}