WisePlay DRM客户端接口按照第三方框架API实现,接口定义请直接参考MediaDrm、MediaCodec。
MediaDrm的关键接口包括:
MediaCodec的关键接口包括:
判断是否支持WisePlay DRM的UUID;
创建MediaDrm对象,传入WisePlay DRM的UUID, UUID是每种DRM类型的全球唯一标识,通过此UUID,可唯一确认DRM的类型,对于WisePlay DRM,UUID为:3d5e6d35-9b9a-41e8-b843-dd3c6e72c42c;
调用openSession方法打开一个会话;
代码样例:
- UUID WISEPLAY_DRM_UUID = new UUID(0x3d5e6d359b9a41e8L, 0xb843dd3c6e72c42cL);
- if(MediaDrm.isCryptoSchemeSupported(WISEPLAY_DRM_UUID)){
- MediaDrm mediaDrm = null;
- try {
- mediaDrm = new MediaDrm(WISEPLAY_DRM_UUID);
- mediaDrm.openSession();
- } catch (UnsupportedSchemeException e) {
- e.printStackTrace();
- } catch (ResourceBusyException e) {
- e.printStackTrace();
- } catch (NotProvisionedException e) {
- e.printStackTrace();
- }
- }
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:媒体类型,如果Android API版本小于26且mimeType是视频或者音频的媒体类型,传入cenc,其它都默认返回传入的mimeType,keyType是KEY_TYPE_RELEASE时可为空;
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数组。
代码样例:
- UUID WISEPLAY_DRM_UUID = new UUID(0x3d5e6d359b9a41e8L, 0xb843dd3c6e72c42cL);
- MediaDrm mediaDrm = null;
- try {
- mediaDrm = new MediaDrm(WISEPLAY_DRM_UUID);
- byte[] sessionId = mediaDrm.openSession();
- String PSSH_BOX = "AAAAY3Bzc2gBAAAAPV5tNZuaQei4Q908bnLELAAAAAPNI3qKPeyxmFVnqmWqx5pmxpmL7+8Am67T7xI7qk7vVncfJnvmvqyBLvozK5iuE84AAAAPcXZhc2pGU2ZkZ2Zqc1ZD";
- byte[] initData = Base64.decode(PSSH_BOX, Base64.DEFAULT);
- String mimeType = "video/mp4";
- //... The initData needs to parse the information from the movie.
- MediaDrm.KeyRequest keyRequest = mediaDrm.getKeyRequest(sessionId, initData, mimeType, MediaDrm.KEY_TYPE_STREAMING, null);
- // Start the license request.
- // Set content-type to application/json
- byte [] data = keyRequest.getData();
- // Use license url and data to request the license.
- URL url = new URL(licenseServerUrl);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setDoOutput(true);
- connection.setDoInput(true);
- connection.setRequestProperty("content-type", "application/json");
- connection.connect();
- String body = "{\"payload\":\""+new String(data , UTF_8)+"\",\"psshBox\":\""+initData+"\",\"keyType\":\""+MediaDrm.KEY_TYPE_STREAMING+"\"}";
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
- writer.write(body);
- writer.close();
- int responseCode = connection.getResponseCode();
- if (responseCode == HttpURLConnection.HTTP_OK) {
- InputStream inputStream = connection.getInputStream();
- byte[] response = toByteArray(inputStream);
- inputStream.close();
- connection.disconnect();
- mediaDrm.provideKeyResponse(sessionId, response);
- }
- } catch (UnsupportedSchemeException | ResourceBusyException | NotProvisionedException | IOException | DeniedByServerException e) {
- e.printStackTrace();
- }
创建MediaCrypto对象,传入WisePlay DRM的UUID以及前边获取到的sessionId;
创建MediaCodec对象,调用configure方法配置MediaCodec,传入之前创建的MediaCrypto对象;
解析媒体数据,根据解析到的数据创建CryptoInfo对象(此信息封装了ISO/IEC (FDIS 23001-7:2016)“ISO基本媒体文件格式文件中的普通加密”中概述的元数据。);
调用MediaCodec的dequeueInputBuffer,从 input 缓冲区队列申请空buffer;
把需要解码的每一帧数据拷贝到前边申请的空buffer,然后调用MediaCodec的queueSecureInputBuffer,将要解码的数据放入 input 缓冲区队列解码;
MediaCodec 模块从 input 缓冲区队列取一帧数据进行解码处理,解码处理结束后,MediaCodec 将原始数据 buffer 置为 empty 后放回 input 缓冲区队列,将解码后的数据放入到 output 缓冲区队列;
调用MediaCodec的dequeueOutputBuffer,从 output 缓冲区队列获取解码后的数据buffer;
调用MediaCodec的releaseOutputBuffer,对编解码后的 buffer 进行渲染/播放,渲染/播放完成后,再将该 buffer 释放回 output 缓冲区队列。
对于离线License,在获取到License之后,调用provideKeyResponse方法,底层会返回这个离线License对应的keySetId,即离线licence的唯一id,业务需要自己保存这个id;
调用MediaDrm的restoreKeys,传入keySetId,后续即可使用该离线License。如果license已过期或keySetid不存在,则restoreKeys会失败,客户端需要重新到服务端申请License;
使用MediaDrm的queryKeyStatus方法可查询license的剩余可播放时长和剩余有效期时长。
代码样例:
- UUID WISEPLAY_DRM_UUID = new UUID(0x3d5e6d359b9a41e8L, 0xb843dd3c6e72c42cL);
- MediaDrm mediaDrm = null;
- try {
- mediaDrm = new MediaDrm(WISEPLAY_DRM_UUID);
- byte[] sessionId = mediaDrm.openSession();
- // The keySetId of the offline license has been downloaded.
- boolean restoerKeys = restoreKeys(sessionId, keySetId);
- if (restoerKeys){
- HashMap<String, String>licenseStatus = mediaDrm.queryKeyStatus(keySetId);
- // Remaining duration for playing a license.
- long playbackDuration = Long.parseLong(licenseStatus.get("PlaybackDurationRemaining"));
- // Remaining license duration
- long licenseDuration = Long.parseLong(licenseStatus.get("LicenseDurationRemaining"));
- } else{
- // Delete the cache of the license association relationship.
- }
- } catch (UnsupportedSchemeException e) {
- e.printStackTrace();
- } catch (ResourceBusyException e) {
- e.printStackTrace();
- } catch (NotProvisionedException e) {
- e.printStackTrace();
- }
- public boolean restoreKeys(byte[] sessionId, byte[] keySetId) {
- try {
- mediaDrm.restoreKeys(sessionId, keySetId);
- return true;
- } catch (Exception e){
- return false;
- }
- }
和获取license的流程一致,区分在于调用MeidaDrm的getKeyRequest接口的传参。
代码样例:
- UUID WISEPLAY_DRM_UUID = new UUID(0x3d5e6d359b9a41e8L, 0xb843dd3c6e72c42cL);
- MediaDrm mediaDrm = null;
- try {
- mediaDrm = new MediaDrm(WISEPLAY_DRM_UUID);
- byte[] sessionId = mediaDrm.openSession();
- //... The initData needs to parse the information from the movie.
- MediaDrm.KeyRequest keyRequest = mediaDrm.getKeyRequest(keySetId, null, null, MediaDrm.KEY_TYPE_RELEASE, null);
- // Start the license request.
- // Set content-type to application/json
- byte [] data = keyRequest.getData();
- // Use license url and data to request the license.
- URL url = new URL(licenseServerUrl);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setDoOutput(true);
- connection.setDoInput(true);
- connection.setRequestProperty("content-type", "application/json");
- connection.connect();
- String body = "{\"payload\":\""+new String(data , UTF_8)+"\",\"psshBox\":\""+initData+"\",\"keyType\":\""+MediaDrm.KEY_TYPE_RELEASE+"\"}";
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
- writer.write(body);
- writer.close();
- int responseCode = connection.getResponseCode();
- if (responseCode == HttpURLConnection.HTTP_OK) {
- InputStream inputStream = connection.getInputStream();
- byte[] response = toByteArray(inputStream);
- inputStream.close();
- connection.disconnect();
- mediaDrm.provideKeyResponse(sessionID, response);
- }
- } catch (UnsupportedSchemeException e) {
- e.printStackTrace();
- } catch (ResourceBusyException e) {
- e.printStackTrace();
- } catch (NotProvisionedException e) {
- e.printStackTrace();
- }
通过MediaDrm的restoreKeys方法传入sessionId和license的keySetId判断license是否可以恢复,可以的话进行下一步;否则删除本地缓存的license关联关系;
如果license已经过期不要初始化编解码器。
通过MeidaDrm的setOnEventListener方法设置listener监听;
如果listener的event等于MediaDrm.EVENT_KEY_REQUIRED,表示license即将过期(license有效期还有30秒的时候触发);
如果listener的event等于MediaDrm.EVENT_KEY_EXPIRED,表示license已经过期;
如果即将过期,业务可自行决定是否触发重新申请license流程。