cas: explicitly define possible key id values

Remove cas header from media jni headers, and define flags
for PES header extraction.

bug: 73898003

test: CTS MediaCasTest, MediaDrmClearkeyTest

Change-Id: I2a512e2dbeb8be53a64bfa0b89254032c7341fa2
diff --git a/api/current.txt b/api/current.txt
index 6b829c7..51845f4 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -23344,6 +23344,11 @@
     method protected void finalize();
     method public boolean requiresSecureDecoderComponent(java.lang.String);
     method public void setMediaCasSession(android.media.MediaCas.Session);
+    field public static final byte SCRAMBLE_CONTROL_EVEN_KEY = 2; // 0x2
+    field public static final byte SCRAMBLE_CONTROL_ODD_KEY = 3; // 0x3
+    field public static final byte SCRAMBLE_CONTROL_RESERVED = 1; // 0x1
+    field public static final byte SCRAMBLE_CONTROL_UNSCRAMBLED = 0; // 0x0
+    field public static final byte SCRAMBLE_FLAG_PES_HEADER = 1; // 0x1
   }
 
   public class MediaDescription implements android.os.Parcelable {
diff --git a/media/java/android/media/MediaDescrambler.java b/media/java/android/media/MediaDescrambler.java
index 40c837b..99bd254 100644
--- a/media/java/android/media/MediaDescrambler.java
+++ b/media/java/android/media/MediaDescrambler.java
@@ -125,6 +125,38 @@
     }
 
     /**
+     * Scramble control value indicating that the samples are not scrambled.
+     * @see #descramble(ByteBuffer, ByteBuffer, android.media.MediaCodec.CryptoInfo)
+     */
+    public static final byte SCRAMBLE_CONTROL_UNSCRAMBLED = 0;
+
+    /**
+     * Scramble control value reserved and shouldn't be used currently.
+     * @see #descramble(ByteBuffer, ByteBuffer, android.media.MediaCodec.CryptoInfo)
+     */
+    public static final byte SCRAMBLE_CONTROL_RESERVED    = 1;
+
+    /**
+     * Scramble control value indicating that the even key is used.
+     * @see #descramble(ByteBuffer, ByteBuffer, android.media.MediaCodec.CryptoInfo)
+     */
+    public static final byte SCRAMBLE_CONTROL_EVEN_KEY     = 2;
+
+    /**
+     * Scramble control value indicating that the odd key is used.
+     * @see #descramble(ByteBuffer, ByteBuffer, android.media.MediaCodec.CryptoInfo)
+     */
+    public static final byte SCRAMBLE_CONTROL_ODD_KEY      = 3;
+
+    /**
+     * Scramble flag for a hint indicating that the descrambling request is for
+     * retrieving the PES header info only.
+     *
+     * @see #descramble(ByteBuffer, ByteBuffer, android.media.MediaCodec.CryptoInfo)
+     */
+    public static final byte SCRAMBLE_FLAG_PES_HEADER = (1 << 0);
+
+    /**
      * Descramble a ByteBuffer of data described by a
      * {@link android.media.MediaCodec.CryptoInfo} structure.
      *
@@ -133,7 +165,15 @@
      * @param dstBuf ByteBuffer to hold the descrambled data, which starts at
      * dstBuf.position().
      * @param cryptoInfo a {@link android.media.MediaCodec.CryptoInfo} structure
-     * describing the subsamples contained in src.
+     * describing the subsamples contained in srcBuf. The iv and mode fields in
+     * CryptoInfo are not used. key[0] contains the MPEG2TS scrambling control bits
+     * (as defined in ETSI TS 100 289 (2011): "Digital Video Broadcasting (DVB);
+     * Support for use of the DVB Scrambling Algorithm version 3 within digital
+     * broadcasting systems"), and the value must be one of {@link #SCRAMBLE_CONTROL_UNSCRAMBLED},
+     * {@link #SCRAMBLE_CONTROL_RESERVED}, {@link #SCRAMBLE_CONTROL_EVEN_KEY} or
+     * {@link #SCRAMBLE_CONTROL_ODD_KEY}. key[1] is a set of bit flags, with the
+     * only possible bit being {@link #SCRAMBLE_FLAG_PES_HEADER} currently.
+     * key[2~15] are not used.
      *
      * @return number of bytes that have been successfully descrambled, with negative
      * values indicating errors.
@@ -169,6 +209,7 @@
         try {
             return native_descramble(
                     cryptoInfo.key[0],
+                    cryptoInfo.key[1],
                     cryptoInfo.numSubSamples,
                     cryptoInfo.numBytesOfClearData,
                     cryptoInfo.numBytesOfEncryptedData,
@@ -204,7 +245,8 @@
     private native final void native_setup(@NonNull IHwBinder decramblerBinder);
     private native final void native_release();
     private native final int native_descramble(
-            byte key, int numSubSamples, int[] numBytesOfClearData, int[] numBytesOfEncryptedData,
+            byte key, byte flags, int numSubSamples,
+            int[] numBytesOfClearData, int[] numBytesOfEncryptedData,
             @NonNull ByteBuffer srcBuf, int srcOffset, int srcLimit,
             ByteBuffer dstBuf, int dstOffset, int dstLimit) throws RemoteException;
 
diff --git a/media/jni/android_media_MediaCodec.cpp b/media/jni/android_media_MediaCodec.cpp
index 022198b..1bb3dc1 100644
--- a/media/jni/android_media_MediaCodec.cpp
+++ b/media/jni/android_media_MediaCodec.cpp
@@ -1011,7 +1011,7 @@
 
     sp<IDescrambler> descrambler;
     if (descramblerBinderObj != NULL) {
-        descrambler = JDescrambler::GetDescrambler(env, descramblerBinderObj);
+        descrambler = GetDescrambler(env, descramblerBinderObj);
     }
 
     err = codec->configure(format, bufferProducer, crypto, descrambler, flags);
diff --git a/media/jni/android_media_MediaDescrambler.cpp b/media/jni/android_media_MediaDescrambler.cpp
index add47463..aa79ce0 100644
--- a/media/jni/android_media_MediaDescrambler.cpp
+++ b/media/jni/android_media_MediaDescrambler.cpp
@@ -25,18 +25,64 @@
 
 #include <android/hardware/cas/native/1.0/BpHwDescrambler.h>
 #include <android/hardware/cas/native/1.0/BnHwDescrambler.h>
+#include <android/hardware/cas/native/1.0/IDescrambler.h>
 #include <binder/MemoryDealer.h>
 #include <hidl/HidlSupport.h>
 #include <hidlmemory/FrameworkUtils.h>
 #include <media/stagefright/foundation/ADebug.h>
+#include <media/cas/DescramblerAPI.h>
 #include <nativehelper/ScopedLocalRef.h>
 
 namespace android {
+class IMemory;
+class MemoryDealer;
 
+namespace hardware {
+class HidlMemory;
+};
 using hardware::fromHeap;
+using hardware::HidlMemory;
+using hardware::hidl_string;
+using hardware::hidl_vec;
+using namespace hardware::cas::V1_0;
+using namespace hardware::cas::native::V1_0;
+
+struct JDescrambler : public RefBase {
+    JDescrambler(JNIEnv *env, jobject descramberBinderObj);
+
+    status_t descramble(
+            uint32_t key,
+            ssize_t totalLength,
+            const hidl_vec<SubSample>& subSamples,
+            const void *srcPtr,
+            jint srcOffset,
+            void *dstPtr,
+            jint dstOffset,
+            Status *status,
+            uint32_t *bytesWritten,
+            hidl_string *detailedError);
+
+
+protected:
+    virtual ~JDescrambler();
+
+private:
+    sp<IDescrambler> mDescrambler;
+    sp<IMemory> mMem;
+    sp<MemoryDealer> mDealer;
+    sp<HidlMemory> mHidlMemory;
+    SharedBuffer mDescramblerSrcBuffer;
+
+    Mutex mSharedMemLock;
+
+    bool ensureBufferCapacity(size_t neededSize);
+
+    DISALLOW_EVIL_CONSTRUCTORS(JDescrambler);
+};
 
 struct fields_t {
     jfieldID context;
+    jbyte flagPesHeader;
 };
 
 static fields_t gFields;
@@ -111,8 +157,7 @@
     mDealer.clear();
 }
 
-// static
-sp<IDescrambler> JDescrambler::GetDescrambler(JNIEnv *env, jobject obj) {
+sp<IDescrambler> GetDescrambler(JNIEnv *env, jobject obj) {
     if (obj != NULL) {
         sp<hardware::IBinder> hwBinder =
                 JHwRemoteBinder::GetNativeContext(env, obj)->getBinder();
@@ -155,7 +200,7 @@
 }
 
 status_t JDescrambler::descramble(
-        jbyte key,
+        uint32_t key,
         ssize_t totalLength,
         const hidl_vec<SubSample>& subSamples,
         const void *srcPtr,
@@ -228,6 +273,12 @@
 
     gFields.context = env->GetFieldID(clazz.get(), "mNativeContext", "J");
     CHECK(gFields.context != NULL);
+
+    jfieldID fieldPesHeader = env->GetStaticFieldID(
+            clazz.get(), "SCRAMBLE_FLAG_PES_HEADER", "B");
+    CHECK(fieldPesHeader != NULL);
+
+    gFields.flagPesHeader = env->GetStaticByteField(clazz.get(), fieldPesHeader);
 }
 
 static void android_media_MediaDescrambler_native_setup(
@@ -323,7 +374,7 @@
 }
 
 static jint android_media_MediaDescrambler_native_descramble(
-        JNIEnv *env, jobject thiz, jbyte key, jint numSubSamples,
+        JNIEnv *env, jobject thiz, jbyte key, jbyte flags, jint numSubSamples,
         jintArray numBytesOfClearDataObj, jintArray numBytesOfEncryptedDataObj,
         jobject srcBuf, jint srcOffset, jint srcLimit,
         jobject dstBuf, jint dstOffset, jint dstLimit) {
@@ -364,12 +415,18 @@
         return -1;
     }
 
+    uint32_t scramblingControl = (uint32_t)key;
+
+    if (flags & gFields.flagPesHeader) {
+        scramblingControl |= DescramblerPlugin::kScrambling_Flag_PesHeader;
+    }
+
     Status status;
     uint32_t bytesWritten;
     hidl_string detailedError;
 
     err = descrambler->descramble(
-            key, totalLength, subSamples,
+            scramblingControl, totalLength, subSamples,
             srcPtr, srcOffset, dstPtr, dstOffset,
             &status, &bytesWritten, &detailedError);
 
@@ -401,7 +458,7 @@
             (void *)android_media_MediaDescrambler_native_init },
     { "native_setup", "(Landroid/os/IHwBinder;)V",
             (void *)android_media_MediaDescrambler_native_setup },
-    { "native_descramble", "(BI[I[ILjava/nio/ByteBuffer;IILjava/nio/ByteBuffer;II)I",
+    { "native_descramble", "(BBI[I[ILjava/nio/ByteBuffer;IILjava/nio/ByteBuffer;II)I",
             (void *)android_media_MediaDescrambler_native_descramble },
 };
 
diff --git a/media/jni/android_media_MediaDescrambler.h b/media/jni/android_media_MediaDescrambler.h
index 2354dc2..0ae4187 100644
--- a/media/jni/android_media_MediaDescrambler.h
+++ b/media/jni/android_media_MediaDescrambler.h
@@ -19,57 +19,19 @@
 
 #include "jni.h"
 
-#include <android/hardware/cas/native/1.0/IDescrambler.h>
-
-#include <media/stagefright/foundation/ABase.h>
-#include <utils/Mutex.h>
+#include <utils/RefBase.h>
 
 namespace android {
-class IMemory;
-class MemoryDealer;
 
 namespace hardware {
-class HidlMemory;
-};
-using hardware::HidlMemory;
-using hardware::hidl_string;
-using hardware::hidl_vec;
-using namespace hardware::cas::V1_0;
-using namespace hardware::cas::native::V1_0;
+namespace cas {
+namespace native {
+namespace V1_0 {
+struct IDescrambler;
+}}}}
+using hardware::cas::native::V1_0::IDescrambler;
 
-struct JDescrambler : public RefBase {
-    JDescrambler(JNIEnv *env, jobject descramberBinderObj);
-
-    status_t descramble(
-            jbyte key,
-            ssize_t totalLength,
-            const hidl_vec<SubSample>& subSamples,
-            const void *srcPtr,
-            jint srcOffset,
-            void *dstPtr,
-            jint dstOffset,
-            Status *status,
-            uint32_t *bytesWritten,
-            hidl_string *detailedError);
-
-    static sp<IDescrambler> GetDescrambler(JNIEnv *env, jobject obj);
-
-protected:
-    virtual ~JDescrambler();
-
-private:
-    sp<IDescrambler> mDescrambler;
-    sp<IMemory> mMem;
-    sp<MemoryDealer> mDealer;
-    sp<HidlMemory> mHidlMemory;
-    SharedBuffer mDescramblerSrcBuffer;
-
-    Mutex mSharedMemLock;
-
-    bool ensureBufferCapacity(size_t neededSize);
-
-    DISALLOW_EVIL_CONSTRUCTORS(JDescrambler);
-};
+sp<IDescrambler> GetDescrambler(JNIEnv *env, jobject obj);
 
 }  // namespace android