Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various MP4Decrypt fixes #420

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Source/C++/Apps/Mp4Decrypt/Mp4Decrypt.cpp
Expand Up @@ -79,7 +79,8 @@ class ProgressListener : public AP4_Processor::ProgressListener
AP4_Result
ProgressListener::OnProgress(unsigned int step, unsigned int total)
{
printf("\r%d/%d", step, total);
fprintf(stdout, "\r%d/%d", step, total);
fflush(stdout);
return AP4_SUCCESS;
}

Expand Down
16 changes: 15 additions & 1 deletion Source/C++/Core/Ap4CommonEncryption.cpp
Expand Up @@ -2329,7 +2329,7 @@ AP4_CencFragmentDecrypter::ProcessSample(AP4_DataBuffer& data_in,
/*----------------------------------------------------------------------
| AP4_CencDecryptingProcessor::AP4_CencDecryptingProcessor
+---------------------------------------------------------------------*/
AP4_CencDecryptingProcessor::AP4_CencDecryptingProcessor(const AP4_ProtectionKeyMap* key_map,
AP4_CencDecryptingProcessor::AP4_CencDecryptingProcessor(AP4_ProtectionKeyMap* key_map,
AP4_BlockCipherFactory* block_cipher_factory) :
m_KeyMap(key_map)
{
Expand Down Expand Up @@ -2403,6 +2403,20 @@ AP4_CencDecryptingProcessor::CreateTrackHandler(AP4_TrakAtom* trak)
sample_descs.Append(protected_desc);
sample_entries.Append(sample_entry);
}

AP4_ContainerAtom* schi = protected_desc->GetSchemeInfo()->GetSchiAtom();
if (schi != NULL) {
AP4_CencTrackEncryption* tenc;
if (protected_desc->GetSchemeType() == AP4_PROTECTION_SCHEME_TYPE_PIFF) {
tenc = AP4_DYNAMIC_CAST(AP4_CencTrackEncryption, schi->GetChild(AP4_ATOM_TYPE_UUID));
} else {
tenc = AP4_DYNAMIC_CAST(AP4_CencTrackEncryption, schi->GetChild(AP4_ATOM_TYPE_TENC));
}
if (tenc != NULL) {
const AP4_DataBuffer* key = m_KeyMap->GetKeyByKid(tenc->GetDefaultKid());
if (key != NULL) m_KeyMap->SetKey(trak->GetId(), key->GetData(), 16);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing to the key map from AP4_CencDecryptingProcessor breaks the constness of that map. It doesn't seem right. Can you explain why you need to write a key here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mp4decrypt couldn't properly handle selecting the track based on key ID, it only seemed to work by track ID.

}
}
}
}
if (sample_entries.ItemCount() == 0) return NULL;
Expand Down
4 changes: 2 additions & 2 deletions Source/C++/Core/Ap4CommonEncryption.h
Expand Up @@ -653,7 +653,7 @@ class AP4_CencDecryptingProcessor : public AP4_Processor
{
public:
// constructor
AP4_CencDecryptingProcessor(const AP4_ProtectionKeyMap* key_map,
AP4_CencDecryptingProcessor(AP4_ProtectionKeyMap* key_map,
AP4_BlockCipherFactory* block_cipher_factory = NULL);

// AP4_Processor methods
Expand All @@ -670,7 +670,7 @@ class AP4_CencDecryptingProcessor : public AP4_Processor

// members
AP4_BlockCipherFactory* m_BlockCipherFactory;
const AP4_ProtectionKeyMap* m_KeyMap;
AP4_ProtectionKeyMap* m_KeyMap;
};

/*----------------------------------------------------------------------
Expand Down
12 changes: 9 additions & 3 deletions Source/C++/Core/Ap4Processor.cpp
Expand Up @@ -143,6 +143,7 @@ AP4_Processor::ProcessFragments(AP4_MoovAtom* moov,
AP4_ContainerAtom* mfra,
AP4_SidxAtom* sidx,
AP4_Position sidx_position,
ProgressListener* listener,
AP4_ByteStream& input,
AP4_ByteStream& output)
{
Expand Down Expand Up @@ -380,6 +381,11 @@ AP4_Processor::ProcessFragments(AP4_MoovAtom* moov,
for (unsigned int i=0; i<sample_tables.ItemCount(); i++) {
delete sample_tables[i];
}

// notify the progress listener
if (listener) {
listener->OnProgress(fragment_index+1, atoms.ItemCount());
}
}

// update the mfra if we have one
Expand Down Expand Up @@ -702,7 +708,7 @@ AP4_Processor::Process(AP4_ByteStream& input,
AP4_ASSERT(after-before == mdat_payload_size);
#endif
}

// find the position of the sidx atom
AP4_Position sidx_position = 0;
if (sidx) {
Expand All @@ -718,7 +724,7 @@ AP4_Processor::Process(AP4_ByteStream& input,
}

// process the fragments, if any
result = ProcessFragments(moov, frags, mfra, sidx, sidx_position, fragments?*fragments:input, output);
result = ProcessFragments(moov, frags, mfra, sidx, sidx_position, listener, fragments?*fragments:input, output);
if (AP4_FAILED(result)) return result;

// update and re-write the sidx if we have one
Expand Down Expand Up @@ -755,7 +761,7 @@ AP4_Processor::Process(AP4_ByteStream& input,
// so we need to delete it here
delete moov;
}

return AP4_SUCCESS;
}

Expand Down
1 change: 1 addition & 0 deletions Source/C++/Core/Ap4Processor.h
Expand Up @@ -263,6 +263,7 @@ class AP4_Processor {
AP4_ContainerAtom* mfra,
AP4_SidxAtom* sidx,
AP4_Position sidx_position,
ProgressListener* listener,
AP4_ByteStream& input,
AP4_ByteStream& output);

Expand Down