目录

客户端开发

关键接口说明

WisePlay DRM客户端接口按照第三方框架API实现,接口定义请参见MediaDrm、MediaCodec。

MediaDrm的关键接口包括:

MediaCodec的关键接口包括:

获取终端DRM属性

通过MediaDrm的getPropertyString接口获取终端DRM的属性,相应的Key值及其说明如下表:

Key名称

说明

MediaDRM.PROPERTY_VENDOR

固定返回“Huawei”。

MediaDRM.PROPERTY_VERSION

WisePlay DRM客户端版本号。

  • 1.1
  • 1.0

MediaDRM.PROPERTY_DESCRIPTION

返回华为DRM对外的品牌:WisePlay DRM。

MediaDRM.PROPERTY_ALGORITHMS

根据DRM能力返回,包括国密和AES等算法,取值:

  • AES/CBC/NoPadding
  • AES/CTR/NoPadding
  • SM4/CBC/NoPadding

securityLevel

终端支持DRM的安全级别

  • SW:软件级
  • HW:硬件级
  • EH:增强硬件级

systemId

系统ID:

3d5e6d35-9b9a-41e8-b843-dd3c6e72c42c

hdcpState

当前的HDCP连接状态,取值:

  • Disconnected:无设备连接
  • Unprotected:HDCP协商失败
  • HDCP1.x:支持HDCP1.x
  • HDCP2.0:支持HDCP2.0
  • HDCP2.1:支持HDCP2.1
  • HDCP2.2:支持HDCP2.2

deviceHdcpLevel

终端可支持的HDCP最高安全级别,取值:

  • NULL:无HDCP能力
  • HDCP1.x:最高支持HDCP1.x
  • HDCP2.0:最高支持HDCP2.0
  • HDCP2.1:最高支持HDCP2.1
  • HDCP2.2:最高支持HDCP2.2

maxSessions

支持的最大会话数:64

currentSessions

返回当前的会话数:取值范围[0, 64]

创建MediaDrm对象

  1. 参见示例代码,判断是否支持WisePlay DRM的UUID。
  2. 创建MediaDrm对象,传入WisePlay DRM的UUID,UUID是每种DRM类型的全球唯一标识,通过此UUID,可唯一确认DRM的类型。

    对于WisePlay DRM,UUID为:3d5e6d35-9b9a-41e8-b843-dd3c6e72c42c。

  3. 调用openSession方法打开一个会话。

示例代码:

UUID WISEPLAY_DRM_UUID = new UUID(0x3d5e6d359b9a41e8L, 0xb843dd3c6e72c42cL);    
// 判断是否支持WisePlay DRM的UUID   
if(MediaDrm.isCryptoSchemeSupported(WISEPLAY_DRM_UUID)){  
    MediaDrm mediaDrm = null;    
    try {    
       mediaDrm = new MediaDrm(WISEPLAY_DRM_UUID);    
       mediaDrm.openSession();
       // 使用该session执行后续操作
    } catch (UnsupportedSchemeException e) {    
        e.printStackTrace();    
    } catch (ResourceBusyException e) {    
        e.printStackTrace();    
    } catch (NotProvisionedException e) {    
        e.printStackTrace();    
    }    
}

获取License

  1. 参见示例代码,创建MediaDrm对象并传入WisePlay DRM的UUID。
  2. 调用MediaDrm的openSession方法打开一个会话并获取到sessionId。
  3. 调用MeidaDrm的getKeyRequest方法,获取License请求消息体的KeyRequest对象,传入的参数的说明如下:

    • sessionId:openSession方法返回的sessionId。
    • initData:如果KeyType为MediaDrm.KEY_TYPE_STREAMING或MediaDrm.KEY_TYPE_OFFLINE,则传入影片PSSH Box的值,PSSH Box值可从媒体的索引文件中获取(Dash流从mpd文件中获取,HLS流从m3u8文件中获取);如果keyType是MediaDrm.KEY_TYPE_RELEASE,该值为空。
    • mimeType:视频或音频文件的媒体类型,例如"video/mp4"或"video/webm"等;删除License场景该值可以为空。
    • keyType:在线获取License的场景下,填写MediaDrm.KEY_TYPE_STREAMING;获取离线License场景下,填写MediaDrm.KEY_TYPE_OFFLINE;删除License场景,填写MediaDrm.KEY_TYPE_RELEASE。
    • optionalParameters:传空。

  4. 在线发送获取License请求,请求以POST方式发送给业务服务器,请求头的content-type头域设置为Application/json,请求的消息体为第三步获取的KeyRequest对象的getData方法获取到的数据。
  5. 得到服务端响应后,将服务端透传的WisePlay DRM Server响应的内容,通过provideKeyResponse方法,传递给底层Plugin,传入的参数为:从服务端获取到的License数据的byte数组。
示例代码:
/**
* Wiseplay DRM UUID
*/
private static final UUID WISEPLAY_DRM_UUID = new UUID(0x3d5e6d359b9a41e8L, 0xb843dd3c6e72c42cL);

/**
* Wiseplay log tag
*/
private static final String TAG = "WisePlay-GetLicense";

/**
* Wiseplay PSSH box
*/
private static final String PSSH_BOX = "AAAAY3Bzc2gBAAAAPV5tNZuaQei4Q908bnLELAAAAAO7vqaHr+24kInNiIm9y7x3iQi++lh914kkW+r9JHZ1u3iSe67fx4l6kSNlOHvv2soAAAAPc2Zhc1JBZXV5SEpGU0pB";

/**
* url of get license
*/
private String licenseUrl = "https://wiseplay.hwcloudtest.cn:9091/playdemo/chinaDrm/getLicense?contentId=sfasRAeuyHJFSJA&useDrmProxy=1";
HttpURLConnection connection = null;
MediaDrm mediaDrm = null;
byte[] sessionID = null;

try {
    // Value of pssh box obtained from the movie.
    byte[] initData = Base64.decode(PSSH_BOX, Base64.DEFAULT);

    // Obtain the value of pssh box of the movie whose UUID is wiseplay drm from the movie.
    String mimeType = "video/mp4";
    mediaDrm = new MediaDrm(WISEPLAY_DRM_UUID);
    sessionID = mediaDrm.openSession();
    MediaDrm.KeyRequest keyRequest =
                    mediaDrm.getKeyRequest(sessionID, initData, mimeType, MediaDrm.KEY_TYPE_STREAMING, null);
    byte[] requestData = keyRequest.getData();
    URL url = new URL(licenseUrl);
    // Get HttpURLConnection instance and set connection properties
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setConnectTimeout(10 * 1000);
    connection.setRequestProperty("content-type", "Application/json");
    connection.connect();
    String body = new String(requestData, UTF_8);
    Log.i(TAG, "getOnlineLicense body : " + body);
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), UTF_8));
    writer.write(body);
    writer.close();
    // Read HttpURLConnection response Code
    int responseCode = connection.getResponseCode();
    Log.i(TAG, "getOnlineLicense responseCode : " + responseCode);
    if (responseCode == HttpURLConnection.HTTP_OK) {
        InputStream inputStream = connection.getInputStream();
        byte[] response = toByteArray(inputStream);
        inputStream.close();
        Log.i(TAG, "getOnlineLicense response : " + new String(response, UTF_8));
        mediaDrm.provideKeyResponse(sessionID, response);
        //....
     }
} catch (NotProvisionedException | IOException | DeniedByServerException | ResourceBusyException | UnsupportedSchemeException | IllegalArgumentException e) {
    e.printStackTrace();
    Log.i(TAG, "get online license failed: " + e.getMessage());
} finally {
    // disConnection HttpURLCOnnection and release MediaDrm
    if (connection != null) {
        connection.disconnect();
    }
    if (mediaDrm != null) {
        if (sessionID != null) {
            mediaDrm.closeSession(sessionID);
        }
         mediaDrm.close();
    }
}

加密内容解码

  1. 创建MediaCrypto对象,传入WisePlay DRM的UUID和获取到的sessionId。

    //创建MediaCrypto对象
    new MediaCrypto(uuid, sessionId);

  2. 创建MediaCodec对象,调用configure方法配置MediaCodec,传入之前创建的MediaCrypto对象。

    //创建MediaCodec对象
    codec = MediaCodec.createByCodecName(codecName);
    ...
    //调用configure方法配置MediaCodec
    codec.configure(mediaFormat, surface, crypto, 0);

  3. 解析媒体数据,根据解析到的数据创建CryptoInfo对象(此信息封装了ISO/IEC (FDIS 23001-7:2016)“ISO基本媒体文件格式文件中的普通加密”中概述的元数据)。

    //创建CryptoInfo对象
    MediaCodec.CryptoInfo cryptoInfo = buffer.cryptoInfo.getFrameworkCryptoInfo();

  4. 调用MediaCodec的dequeueInputBuffer,从input缓冲区队列申请空buffer,把需要解码的每一帧数据拷贝到申请的空buffe中。

    //调用MediaCodec的dequeueInputBuffer
    inputIndex = codec.dequeueInputBuffer(0);

  5. 调用MediaCodec的queueSecureInputBuffer,将要解码的数据放入input缓冲区队列解码。

    //调用MediaCodec的queueSecureInputBuffer
    codec.queueSecureInputBuffer(inputIndex, 0, cryptoInfo, presentationTimeUs, 0);

  6. MediaCodec模块从input缓冲区队列取一帧数据进行解码处理,解码处理结束后,MediaCodec将原始数据buffer置为empty后放回input缓冲区队列。

    //处理原始数据buffer
    inputIndex = C.INDEX_UNSET;
    buffer.data = null;

  7. 调用MediaCodec的dequeueOutputBuffer,从output缓冲区队列获取解码后的数据buffer。

    //用MediaCodec的dequeueOutputBuffer
    outputIndex =codec.dequeueOutputBuffer(outputBufferInfo, getDequeueOutputBufferTimeoutUs());

  8. 调用MediaCodec的releaseOutputBuffer,对解码后的buffer进行渲染/播放。

    //调用MediaCodec的releaseOutputBuffer
    codec.releaseOutputBuffer(outputIndex, false);

该章节的示例代码来源于huawei-wiseplay-android-java-exoplayer-demo.zip,详情参见基于ExoPlayer的WisePlay DRM Demo。

离线License管理

  1. 参见示例代码,获取离线License信息。

    • 和获取License的流程一致,区分在于调用MeidaDrm的getKeyRequest接口的传参。
    • 对于离线License,在获取到License之后,调用provideKeyResponse方法,底层会返回该离线License对应的keySetId,即离线Licence的唯一ID,业务需要自己保存该ID。
    示例代码:
    /**
     * Wiseplay DRM UUID
     */
    private static final UUID WISEPLAY_DRM_UUID = new UUID(0x3d5e6d359b9a41e8L, 0xb843dd3c6e72c42cL);
    
    /**
     * Wiseplay log tag
     */
    private static final String TAG = "WisePlay-GetOfflineLicense";
    
    /**
     * Wiseplay PSSH box
     */
    private static final String PSSH_BOX = "AAAAY3Bzc2gBAAAAPV5tNZuaQei4Q908bnLELAAAAAO7vqaHr+24kInNiIm9y7x3iQi++lh914kkW+r9JHZ1u3iSe67fx4l6kSNlOHvv2soAAAAPc2Zhc1JBZXV5SEpGU0pB";
    
    /**
     * url of get license
     */
    private String licenseUrl = "https://wiseplay.hwcloudtest.cn:9091/playdemo/chinaDrm/getLicense?contentId=sfasRAeuyHJFSJA&useDrmProxy=1";
    
    HttpURLConnection connection = null;
    MediaDrm mediaDrm = null;
    byte[] sessionID = null;
    
    try {
        // Obtain the value of pssh box of the movie whose UUID is wiseplay drm from the movie.
        byte[] initData = Base64.decode(PSSH_BOX, Base64.DEFAULT);
        String mimeType = "video/mp4";
        mediaDrm = new MediaDrm(WISEPLAY_DRM_UUID);
        sessionID = mediaDrm.openSession();
        MediaDrm.KeyRequest keyRequest =
            mediaDrm.getKeyRequest(sessionID, initData, mimeType, MediaDrm.KEY_TYPE_OFFLINE, null);
        byte[] requestData = keyRequest.getData();
        URL url = new URL(licenseUrl);
        // Get HttpURLConnection instance and set connection properties
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestProperty("content-type", "Application/json");
        connection.connect();
    
        String body = new String(requestData, UTF_8);
        Log.i(TAG, "getOfflineLicense body : " + body);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), UTF_8));
        writer.write(body);
        writer.close();
    
        // Read HttpURLConnection response Code
        int responseCode = connection.getResponseCode();
        Log.i(TAG, "getOfflineLicense responseCode : " + responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = connection.getInputStream();
            byte[] buffer = new byte[1024 * 4];
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            byte[] response = outputStream.toByteArray();
            inputStream.close();
            Log.i(TAG, "getOfflineLicense response : " + new String(response, UTF_8));
            String keySetId = mediaDrm.provideKeyResponse(sessionID, response);
            // Save the keySetId of the offline license to the SharedPreferences.
            //....
            }
        } catch (NotProvisionedException | UnsupportedSchemeException | ResourceBusyException | IOException | DeniedByServerException | IllegalArgumentException e) {
            e.printStackTrace();
            Log.i(TAG, "get offline license failed: " + e.getMessage());
         } finally {
         // DisConnection HttpURLConnection and release MediaDrm
            if (connection != null) {
                  connection.disconnect();
             }
    
            if (mediaDrm != null) {
               if (sessionID != null) {
                   mediaDrm.closeSession(sessionID);
               }
             mediaDrm.close();
         }
    }

  2. 删除离线License。

    • 和获取License的流程一致,区分在于调用MeidaDrm的getKeyRequest接口的传参。

    示例代码:

    /**
     * Wiseplay DRM UUID
     */
    private static final UUID WISEPLAY_DRM_UUID = new UUID(0x3d5e6d359b9a41e8L, 0xb843dd3c6e72c42cL);
    
    /**
     * Wiseplay log tag
     */
    private static final String TAG = "WisePlay-DeleteLicense";
    
    /**
     * Wiseplay PSSH box
     */
    private static final String PSSH_BOX = "AAAAY3Bzc2gBAAAAPV5tNZuaQei4Q908bnLELAAAAAO7vqaHr+24kInNiIm9y7x3iQi++lh914kkW+r9JHZ1u3iSe67fx4l6kSNlOHvv2soAAAAPc2Zhc1JBZXV5SEpGU0pB";
    
    /**
     * url of get license
     */
    private String licenseUrl = "https://wiseplay.hwcloudtest.cn:9091/playdemo/chinaDrm/getLicense?contentId=sfasRAeuyHJFSJA&useDrmProxy=1";
    
    HttpURLConnection connection = null;
    MediaDrm mediaDrm = null;
    byte[] sessionID = null;
    
    try {
        mediaDrm = new MediaDrm(WISEPLAY_DRM_UUID);
        sessionID = mediaDrm.openSession();
    
        // Obtain the keySetId of the license from the SharedPreferences.
        byte[] keySetIdToDelete = keySetId;
        MediaDrm.KeyRequest keyRequest =
        mediaDrm.getKeyRequest(keySetIdToDelete, null, null, MediaDrm.KEY_TYPE_RELEASE, null);
    
        byte[] requestData = keyRequest.getData();
        URL url = new URL(licenseUrl);
        // Get HttpURLConnection instance and set connection properties
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestProperty("content-type", "Application/json");
        connection.connect();
    
        String body = new String(requestData, UTF_8);
        Log.i(TAG, "deleteLicense body : " + body);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
        writer.write(body);
        writer.close();
        // Read HttpURLConnection response Code
        int responseCode = connection.getResponseCode();
        Log.i(TAG, "deleteLicense responseCode : " + responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = connection.getInputStream();
            byte[] buffer = new byte[1024 * 4];
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            byte[] response = outputStream.toByteArray();
            inputStream.close();
            Log.i(TAG, "deleteLicense response : " + new String(response, UTF_8));
            mediaDrm.provideKeyResponse(keySetIdToDelete, response);
            //....
            // Delete the cached license relationship from the sharedPreferences.
        }
    } catch (NotProvisionedException | UnsupportedSchemeException | ResourceBusyException | IOException | DeniedByServerException | IllegalArgumentException e) {
        e.printStackTrace();
        Log.i(TAG, "delete license failed: " + e);
    } finally {
        // DisConnection HttpURLConnection and release MediaDRM
        if (connection != null) {
            connection.disconnect();
        }
        if (mediaDrm != null) {
            if (sessionID != null) {
                mediaDrm.closeSession(sessionID);
            }
            mediaDrm.close();
        }
    }

  3. 使用离线License播放。

    • 调用MediaDrm的restoreKeys,传入keySetId,后续即可使用该离线License。如果License已过期或keySetid不存在,则restoreKeys调用失败。
    • 如果restoreKeys调用失败,请根据业务实际情况判断重新申请或者报错返回。

    示例代码:

    UUID WISEPLAY_DRM_UUID = new UUID(0x3d5e6d359b9a41e8L, 0xb843dd3c6e72c42cL);
    // Obtain the keySetId of the license from the SharedPreferences.
    byte[] keySetIdToRestore = keySetId;
    try {
        MediaDrm mediaDrm = new MediaDrm(WISEPLAY_DRM_UUID);
        byte[] sessionID = mediaDrm.openSession();
        mediaDrm.restoreKeys(sessionID, keySetIdToRestore);
    
        // If resotreKeys succeed, you can play it with this license.
    } catch (NotProvisionedException | UnsupportedSchemeException | ResourceBusyException e) {
        e.printStackTrace();
    }

  4. 播放过程中监听License。

    • 如果listener的event为MediaDrm.EVENT_KEY_REQUIRED,表示License即将过期(License有效期还有30秒的时候触发)。请根据业务实际情况判断是否触发重新申请License流程。
    • 如果listener的event为MediaDrm.EVENT_KEY_EXPIRED,表示License已经过期。