2828#include < media/stagefright/foundation/AMessage.h>
2929#include < media/stagefright/DataSource.h>
3030#include < media/stagefright/MediaCodec.h>
31+ #include < media/stagefright/MediaCodecList.h>
3132#include < media/stagefright/MediaDefs.h>
3233#include < media/stagefright/NuMediaExtractor.h>
3334#include < gui/SurfaceComposerClient.h>
@@ -36,7 +37,9 @@ static void usage(const char *me) {
3637 fprintf (stderr, " usage: %s [-a] use audio\n "
3738 " \t\t [-v] use video\n "
3839 " \t\t [-p] playback\n "
39- " \t\t [-S] allocate buffers from a surface\n " , me);
40+ " \t\t [-S] allocate buffers from a surface\n "
41+ " \t\t [-D] decrypt input buffers\n " ,
42+ me);
4043
4144 exit (1 );
4245}
@@ -63,7 +66,8 @@ static int decode(
6366 const char *path,
6467 bool useAudio,
6568 bool useVideo,
66- const android::sp<android::Surface> &surface) {
69+ const android::sp<android::Surface> &surface,
70+ bool decryptInputBuffers) {
6771 using namespace android ;
6872
6973 static int64_t kTimeout = 500ll ;
@@ -109,13 +113,31 @@ static int decode(
109113 state->mNumBuffersDecoded = 0 ;
110114 state->mIsAudio = isAudio;
111115
112- state->mCodec = MediaCodec::CreateByType (
113- looper, mime.c_str (), false /* encoder */ );
116+ if (decryptInputBuffers && !isAudio) {
117+ static const MediaCodecList *list = MediaCodecList::getInstance ();
118+
119+ ssize_t index =
120+ list->findCodecByType (mime.c_str (), false /* encoder */ );
121+
122+ CHECK_GE (index, 0 );
123+
124+ const char *componentName = list->getCodecName (index);
125+
126+ AString fullName = componentName;
127+ fullName.append (" .secure" );
128+
129+ state->mCodec = MediaCodec::CreateByComponentName (
130+ looper, fullName.c_str ());
131+ } else {
132+ state->mCodec = MediaCodec::CreateByType (
133+ looper, mime.c_str (), false /* encoder */ );
134+ }
114135
115136 CHECK (state->mCodec != NULL );
116137
117138 err = state->mCodec ->configure (
118- format, isVideo ? surface : NULL , 0 /* flags */ );
139+ format, isVideo ? surface : NULL ,
140+ decryptInputBuffers ? MediaCodec::CONFIGURE_FLAG_SECURE : 0 );
119141
120142 CHECK_EQ (err, (status_t )OK);
121143
@@ -202,12 +224,24 @@ static int decode(
202224 err = extractor->getSampleTime (&timeUs);
203225 CHECK_EQ (err, (status_t )OK);
204226
227+ uint32_t bufferFlags = 0 ;
228+
229+ uint32_t sampleFlags;
230+ err = extractor->getSampleFlags (&sampleFlags);
231+ CHECK_EQ (err, (status_t )OK);
232+
233+ if (sampleFlags & NuMediaExtractor::SAMPLE_FLAG_ENCRYPTED) {
234+ CHECK (decryptInputBuffers);
235+
236+ bufferFlags |= MediaCodec::BUFFER_FLAG_ENCRYPTED;
237+ }
238+
205239 err = state->mCodec ->queueInputBuffer (
206240 index,
207241 0 /* offset */ ,
208242 buffer->size (),
209243 timeUs,
210- 0 /* flags */ );
244+ bufferFlags );
211245
212246 CHECK_EQ (err, (status_t )OK);
213247
@@ -341,9 +375,10 @@ int main(int argc, char **argv) {
341375 bool useVideo = false ;
342376 bool playback = false ;
343377 bool useSurface = false ;
378+ bool decryptInputBuffers = false ;
344379
345380 int res;
346- while ((res = getopt (argc, argv, " havpS " )) >= 0 ) {
381+ while ((res = getopt (argc, argv, " havpSD " )) >= 0 ) {
347382 switch (res) {
348383 case ' a' :
349384 {
@@ -369,6 +404,12 @@ int main(int argc, char **argv) {
369404 break ;
370405 }
371406
407+ case ' D' :
408+ {
409+ decryptInputBuffers = true ;
410+ break ;
411+ }
412+
372413 case ' ?' :
373414 case ' h' :
374415 default :
@@ -440,7 +481,8 @@ int main(int argc, char **argv) {
440481 player->stop ();
441482 player->reset ();
442483 } else {
443- decode (looper, argv[0 ], useAudio, useVideo, surface);
484+ decode (looper, argv[0 ],
485+ useAudio, useVideo, surface, decryptInputBuffers);
444486 }
445487
446488 if (playback || (useSurface && useVideo)) {
0 commit comments