1# DRM会话管理(C/C++) 2 3DRM会话管理(MediaKeySession)支持媒体密钥管理及媒体解密等,MediaKeySession实例由系统管理里的MediaKeySystem实例创建和销毁。 4 5## 开发步骤 6 7详细的API说明请参考[DRM API参考](../../reference/apis-drm-kit/_drm.md)。 8 91. 导入NDK接口,接口中提供了DRM相关的属性和方法,导入方法如下。 10 11 ```c++ 12 #include "multimedia/drm_framework/native_drm_common.h" 13 #include "multimedia/drm_framework/native_drm_err.h" 14 #include "multimedia/drm_framework/native_mediakeysession.h" 15 #include "multimedia/drm_framework/native_mediakeysystem.h" 16 ``` 17 182. 在CMake脚本中链接Drm NDK动态库。 19 20 ```txt 21 target_link_libraries(PUBLIC libnative_drm.so) 22 ``` 23 243. 声明MediaKeySystem监听回调。 25 26 ```c++ 27 // 适用于单个MediaKeySystem实例的场景。 28 static Drm_ErrCode SessoinEventCallBack(DRM_EventType eventType, uint8_t *info, int32_t infoLen, char *extra) 29 { 30 return DRM_ERR_OK; 31 } 32 33 static Drm_ErrCode SessoinKeyChangeCallBack(DRM_KeysInfo *keysInfo, bool newKeysAvailable) 34 { 35 return DRM_ERR_OK; 36 } 37 38 // 适用于多个MediaKeySystem实例的场景。 39 static Drm_ErrCode SessoinEventCallBackWithObj(MediaKeySession *mediaKeySessoin, DRM_EventType eventType, uint8_t *info, int32_t infoLen, char *extra) 40 { 41 return DRM_ERR_OK; 42 } 43 44 static Drm_ErrCode SessoinKeyChangeCallBackWithObj(MediaKeySession *mediaKeySessoin, DRM_KeysInfo *keysInfo, bool hasNewGoodKeys) 45 { 46 return DRM_ERR_OK; 47 } 48 ``` 49 504. 设置MediaKeySystem监听回调。 51 52 ```c++ 53 // 适用于单个MediaKeySystem实例的场景。 54 MediaKeySession_Callback sessionCallback = { SessoinEventCallBack, SessoinKeyChangeCallBack }; 55 Drm_ErrCode ret = OH_MediaKeySession_SetMediaKeySessionCallback(mediaKeySession, &sessionCallback); 56 if (ret != DRM_ERR_OK) { 57 printf("OH_MediaKeySession_SetMediaKeySessionCallback failed."); 58 } 59 60 // 适用于多个MediaKeySystem实例的场景。 61 OH_MediaKeySession_Callback sessionCallback = { SessoinEventCallBackWithObj, SessoinKeyChangeCallBackWithObj }; 62 ret = OH_MediaKeySession_SetCallback(mediaKeySession, &sessionCallback); 63 if (ret != DRM_ERR_OK) { 64 printf("OH_MediaKeySession_SetCallback failed."); 65 } 66 ``` 67 685. 生成媒体密钥请求,媒体密钥请求响应处理。 69 70 ```c++ 71 DRM_MediaKeyRequest mediaKeyRequest; 72 DRM_MediaKeyRequestInfo info; 73 // initData对应码流中的pssh数据,请按实际数据填入 74 unsigned char initData[128] = {0x00}; 75 memset_s(&info, sizeof(DRM_MediaKeyRequestInfo), 0, sizeof(DRM_MediaKeyRequestInfo)); 76 info.initDataLen = sizeof(initData); 77 info.type = MEDIA_KEY_TYPE_ONLINE; 78 memcpy_s(info.mimeType, sizeof("video/avc"), (char *)"video/avc", sizeof("video/avc")); 79 memcpy_s(info.initData, sizeof(initData), initData, sizeof(initData)); 80 memcpy_s(info.optionName[0], sizeof("optionalDataName"), (char *)"optionalDataName", sizeof("optionalDataName")); 81 memcpy_s(info.optionData[0], sizeof("optionalDataValue"), (char *)"optionalDataValue", sizeof("optionalDataValue")); 82 info.optionsCount = 1; 83 ret = OH_MediaKeySession_GenerateMediaKeyRequest(mediaKeySession, &info, &mediaKeyRequest); 84 if (ret != DRM_ERR_OK) { 85 printf("OH_MediaKeySession_GenerateMediaKeyRequest failed."); 86 } 87 /* 不同解决方案的离线媒体密钥标识组成不同,获取离线媒体密钥标识的方法有两种: 88 1、应用调用OH_MediaKeySystem_GetOfflineMediaKeyIds接口获取离线媒体密钥标识; 89 2、应用通过网络请求DRM服务,获取离线媒体密钥响应keySessionResponse,将响应传到OH_MediaKeySession_ProcessMediaKeyResponse解析 90 出离线媒体密钥标识mediaKeyId;媒体密钥标识最大长度为128,如下代码所示,请根据实际的媒体密钥数据和长度传入; 91 */ 92 unsigned char mediaKeyId[26] = {0x00}; 93 int32_t mediaKeyIdLen = 26; 94 // 媒体密钥请求响应长度最大为12288,请按实际数据输入 95 unsigned char mediaKeyResponse[12288] = {0x00}; 96 int32_t mediaKeyResponseLen = 12288; 97 ret = OH_MediaKeySession_ProcessMediaKeyResponse(mediaKeySession, mediaKeyResponse, 98 mediaKeyResponseLen, mediaKeyId, &mediaKeyIdLen); 99 if (ret != DRM_ERR_OK) { 100 printf("OH_MediaKeySession_ProcessMediaKeyResponse failed."); 101 } 102 ``` 103 1046. (可选)检查当前MediaKeySession会话的媒体密钥状态。 105 106 ```c++ 107 DRM_MediaKeyStatus mediaKeyStatus; 108 ret = OH_MediaKeySession_CheckMediaKeyStatus(mediaKeySession, &mediaKeyStatus); 109 if (ret != DRM_ERR_OK) { 110 printf("OH_MediaKeySession_CheckMediaKeyStatus failed."); 111 } 112 ``` 113 1147. (可选)清理当前会话下所有媒体密钥。 115 116 ```c++ 117 ret = OH_MediaKeySession_ClearMediaKeys(mediaKeySession); 118 if (ret != DRM_ERR_OK) { 119 printf("OH_MediaKeySession_ClearMediaKeys failed."); 120 } 121 ``` 122 1238. (可选)生成离线媒体密钥释放请求和处理离线媒体密钥释放响应。 124 125 ```c++ 126 uint8_t releaseRequest[MAX_MEDIA_KEY_REQUEST_DATA_LEN]; 127 int32_t releaseRequestLen = MAX_MEDIA_KEY_REQUEST_DATA_LEN; 128 ret = OH_MediaKeySession_GenerateOfflineReleaseRequest(mediaKeySession, 129 mediaKeyId, mediaKeyIdLen, releaseRequest, &releaseRequestLen); 130 if (ret != DRM_ERR_OK) { 131 printf("OH_MediaKeySession_GenerateOfflineReleaseRequest failed."); 132 } 133 // keyReleaseResponse是使用请求体releaseRequest通过网络请求从DRM服务获取的离线媒体密钥释放响应,请根据实际值传入 134 unsigned char keyReleaseResponse[12288] = {0x00}; 135 int32_t keyReleaseResponseLen = 12288; 136 ret = OH_MediaKeySession_ProcessOfflineReleaseResponse(mediaKeySession, mediaKeyId, mediaKeyIdLen, 137 keyReleaseResponse, keyReleaseResponseLen); 138 if (ret != DRM_ERR_OK) { 139 printf("OH_MediaKeySession_ProcessOfflineReleaseResponse failed."); 140 } 141 ``` 142 1439. (可选)恢复离线媒体密钥到当前会话。 144 145 ```c++ 146 // 将指定媒体密钥标识的媒体密钥加载到当前会话,被加载的媒体密钥可以是当前会话的也可以是其他会话的 147 ret = OH_MediaKeySession_RestoreOfflineMediaKeys(mediaKeySession, mediaKeyId, mediaKeyIdLen); 148 if (ret != DRM_ERR_OK) { 149 printf("OH_MediaKeySession_RestoreOfflineMediaKeys failed."); 150 } 151 ``` 152 15310. (可选)调用MediaKeySession类中的OH_MediaKeySession_GetContentProtectionLevel方法获取当前会话的内容保护级别。 154 155 ```c++ 156 DRM_ContentProtectionLevel sessionContentProtectionLevel; 157 ret = OH_MediaKeySession_GetContentProtectionLevel(mediaKeySession, 158 &sessionContentProtectionLevel); 159 if (ret != DRM_ERR_OK) { 160 printf("OH_MediaKeySession_GetContentProtectionLevel failed."); 161 } 162 ``` 163 16411. (可选)获取是否需要安全解码。 165 166 ```c++ 167 bool requireSecureDecoder; 168 ret = OH_MediaKeySession_RequireSecureDecoderModule(mediaKeySession, "video/avc", &requireSecureDecoder); 169 if (ret != DRM_ERR_OK) { 170 printf("OH_MediaKeySession_RequireSecureDecoderModule failed."); 171 } 172 ```