To play DRM-protected content using AVCodec, you can use the native APIs provided by DRM Kit.
Currently, the following decryption capabilities are supported:
| Audio Container | Audio Decryption |
|---|---|
| mp4 | AAC |
| Video Container | Video Decryption |
|---|---|
| ts | AVC(H.264) |
| mp4 | AVC(H.264) |
| mp4 | HEVC(H.265) |
Usage Scenario
Before setting up DRM, obtain the DRM information. For details, see step 4 in Media Data Demultiplexing.
For detailed API descriptions, refer to DRM.
Follow the sample code below to implement the entire DRM workflow, including obtaining a name and ID list of the DRM solutions supported by the device, creating a MediaKeySystem, creating a MediaKeySession, generating a media key request, processing a media key response, checking for secure video decoding requirements, and cleaning up resources.
When developing your application, follow the specified order of API calls to avoid exceptions or undefined behaviors. The development steps and explanations below outline the correct sequence.
target_link_libraries(sample PUBLIC libnative_drm.so)
Replace 'sample' with your actual project name as needed.
Import the DRM Kit module.
// ... #include <cstring> #include <multimedia/drm_framework/native_drm_common.h> #include <multimedia/drm_framework/native_drm_err.h> #include <multimedia/drm_framework/native_mediakeysession.h> #include <multimedia/drm_framework/native_mediakeysystem.h>
Obtain the name and ID list of the DRM solutions supported by the device.
uint32_t count = 3; // count specifies the number of DRM plugins supported by the device. Pass in the actual number. DRM_MediaKeySystemDescription descriptions[3]; memset(descriptions, 0, sizeof(descriptions)); Drm_ErrCode ret = OH_MediaKeySystem_GetMediaKeySystems(descriptions, &count); // ...
After obtaining the list of names and unique identifiers of the DRM solutions supported by the device, match them with the DRM information and create the corresponding DRM solution. The DRM information can be obtained through step 4 of the development procedure in Media Data Demultiplexing.
Alternatively, directly parse the media protocol or media data to extract the unique identifier of the DRM solution and the PSSH data to generate the DRM information.
Create a MediaKeySystem instance.
Drm_ErrCode ret = OH_MediaKeySystem_Create(name, &mediaKeySystem);
if (ret != DRM_ERR_OK || mediaKeySystem == nullptr) {
// ...
return log;
}
// ... Create a MediaKeySession instance.
DRM_ContentProtectionLevel securityLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO; ret = OH_MediaKeySystem_CreateMediaKeySession(mediaKeySystem, &securityLevel, &mediaKeySession); // ...
Check whether secure decoding is required.
bool requireSecureDecoder = false; ret = OH_MediaKeySession_RequireSecureDecoderModule(mediaKeySession, mimeType, &requireSecureDecoder); // ...
Generate a media key request and process its response.
DRM_MediaKeyRequest mediaKeyRequest;
DRM_MediaKeyRequestInfo info;
// Set initData to the actual PSSH data in the media stream.
unsigned char initData[512] = {0x00};
memset(&info, 0, sizeof(DRM_MediaKeyRequestInfo));
info.initDataLen = sizeof(initData);
info.type = MEDIA_KEY_TYPE_ONLINE; // MEDIA_KEY_TYPE_ONLINE: online media key request; MEDIA_KEY_TYPE_OFFLINE: offline media key request.
memcpy(info.mimeType, (char *)"video/mp4", sizeof("video/mp4"));
memcpy(info.initData, initData, sizeof(initData));
memcpy(info.optionName[0], (char *)"optionalDataName", sizeof("optionalDataName"));
memcpy(info.optionData[0], (char *)"optionalDataValue", sizeof("optionalDataValue"));
info.optionsCount = 1;
ret = OH_MediaKeySession_GenerateMediaKeyRequest(mediaKeySession, &info, &mediaKeyRequest);
// ... // Obtain mediaKeyResponse from the DRM service over the network and pass in the actual response data.
unsigned char mediaKeyId[128] = {0x00};
int32_t mediaKeyIdLen = 128;
unsigned char mediaKeyResponse[MAX_DRM_MEDIA_KEY_RESPONSE_BUF_SIZE] = {0x00};
int32_t mediaKeyResponseLen = MAX_DRM_MEDIA_KEY_RESPONSE_BUF_SIZE;
ret = OH_MediaKeySession_ProcessMediaKeyResponse(mediaKeySession, mediaKeyResponse,
mediaKeyResponseLen, mediaKeyId, &mediaKeyIdLen);
// ... If required, set the audio decryption configuration by following step 4 in Audio Decoding, and set the video decryption configuration by following step 4 in Surface Mode in Video Decoding or step 4 in Buffer Mode in Video Decoding.
Destroy the MediaKeySession instance.
ret = OH_MediaKeySession_Destroy(mediaKeySession); // ...
Destroy the MediaKeySystem instance.
ret = OH_MediaKeySystem_Destroy(mediaKeySystem); // ...