1# DRM Media Key Session Management (C/C++) 2 3The **MediaKeySystem** class of the DRM module supports media key management and media decryption. **MediaKeySession** instances are created and destroyed by **MediaKeySystem** instances. 4 5## How to Develop 6 7Read [DRM](../../reference/apis-drm-kit/_drm.md) for the API reference. 8 91. Import the NDK, which provides DRM-related attributes and methods. 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. Link the DRM NDK dynamic library in the CMake script. 19 20 ```txt 21 target_link_libraries(PUBLIC libnative_drm.so) 22 ``` 23 243. Declare the MediaKeySystem event listener callback. 25 26 ```c++ 27 // This callback applies to the scenario where there is only one MediaKeySystem instance. 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 // This callback applies to the scenario where there are multiple MediaKeySystem instances. 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. Set the MediaKeySystem event listener callback. 51 52 ```c++ 53 // This callback applies to the scenario where there is only one MediaKeySystem instance. 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 // This callback applies to the scenario where there are multiple MediaKeySystem instances. 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. Generate a media key request and process its response. 69 70 ```c++ 71 DRM_MediaKeyRequest mediaKeyRequest; 72 DRM_MediaKeyRequestInfo info; 73 // initData corresponds to the PSSH data in the stream. Pass in the actual data. 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 /* The composition of an offline media key ID varies according to the DRM scheme. You can obtain an ID in either of the following ways: 88 1. The application calls OH_MediaKeySystem_GetOfflineMediaKeyIds to obtain the ID. 89 2. The application requests the DRM service through the network, obtains a keySessionResponse, and sends the response to OH_MediaKeySession_ProcessMediaKeyResponse for parsing. 90 An offline media key ID is obtained. The maximum length of a media key ID is 128. The following code is an example. Set the media key ID based on the actual media key data and length. 91 */ 92 unsigned char mediaKeyId[26] = {0x00}; 93 int32_t mediaKeyIdLen = 26; 94 // The maximum length of a media key response is 12288. Enter the actual length. 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. (Optional) Check the media key status of the media key session. 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. (Optional) Clear all media keys in the media key session. 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. (Optional) Generate an offline media key release request and process its response. 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 is obtained from the DRM service through the network request using the request body releaseRequest. Pass in the actual data obtained. 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. (Optional) Restore offline media keys. 144 145 ```c++ 146 // Load the media key with the specified media key ID to the current session. The loaded media key can belong to the current session or another session. 147 ret = OH_MediaKeySession_RestoreOfflineMediaKeys(mediaKeySession, mediaKeyId, mediaKeyIdLen); 148 if (ret != DRM_ERR_OK) { 149 printf("OH_MediaKeySession_RestoreOfflineMediaKeys failed."); 150 } 151 ``` 152 15310. (Optional) Call **OH_MediaKeySession_GetContentProtectionLevel** in the **MediaKeySession** class to obtain the content protection level of the current session. 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. (Optional) Check whether secure decoding is required. 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 ``` 173