1e41f4b71Sopenharmony_ci# DRM Media Key System Management (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciUsing the **MediaKeySystem** class of the DRM module, you can manage **MediaKeySystem** instances, generate media key system requests to obtain DRM certificates, process responses to these requests, manage media key sessions, manage offline media keys, and obtain DRM statistics and device configuration information. Before using DRM Kit, check whether the device supports the DRM capabilities of a specific DRM scheme. In DRM Kit, the DRM scheme exists as a plug-in.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci## How to Develop
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciRead [DRM](../../reference/apis-drm-kit/_drm.md) for the API reference.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci1. Import the NDK.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci   ```c++
12e41f4b71Sopenharmony_ci    #include "multimedia/drm_framework/native_drm_common.h"
13e41f4b71Sopenharmony_ci    #include "multimedia/drm_framework/native_drm_err.h"
14e41f4b71Sopenharmony_ci    #include "multimedia/drm_framework/native_mediakeysession.h"
15e41f4b71Sopenharmony_ci    #include "multimedia/drm_framework/native_mediakeysystem.h"
16e41f4b71Sopenharmony_ci   ```
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci2. Link the DRM NDK dynamic library in the CMake script.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci   ```txt
21e41f4b71Sopenharmony_ci    target_link_libraries(PUBLIC libnative_drm.so)
22e41f4b71Sopenharmony_ci   ```
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci3. Check whether the device supports the DRM scheme based on the specified name, MIME type, and content protection level.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci   ```c++
27e41f4b71Sopenharmony_ci    bool isSupported = OH_MediaKeySystem_IsSupported3("com.clearplay.drm", "video/avc", CONTENT_PROTECTION_LEVEL_SW_CRYPTO);
28e41f4b71Sopenharmony_ci    if (isSupported != true) {
29e41f4b71Sopenharmony_ci        printf("The device does not support the content protection level.");
30e41f4b71Sopenharmony_ci    }
31e41f4b71Sopenharmony_ci   ```
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci4. (Optional) Obtain the name and ID list of the DRM schemes supported by the device.
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci   ```c++
36e41f4b71Sopenharmony_ci    uint32_t count = 1; // count indicates the number of DRM plug-ins supported by the device. Pass in the actual number.
37e41f4b71Sopenharmony_ci    DRM_MediaKeySystemDescription descriptions[1];
38e41f4b71Sopenharmony_ci    memset(descriptions, 0, sizeof(descriptions));
39e41f4b71Sopenharmony_ci    Drm_ErrCode ret = OH_MediaKeySystem_GetMediaKeySystems(descriptions, &count);
40e41f4b71Sopenharmony_ci    if (ret != DRM_ERR_OK) {
41e41f4b71Sopenharmony_ci        printf("OH_MediaKeySystem_GetMediaKeySystems failed.");
42e41f4b71Sopenharmony_ci    }
43e41f4b71Sopenharmony_ci   ```
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci5. Create a **MediaKeySystem** instance.
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci   ```c++
48e41f4b71Sopenharmony_ci    MediaKeySystem *mediaKeySystem = NULL;
49e41f4b71Sopenharmony_ci    Drm_ErrCode ret = OH_MediaKeySystem_Create("com.clearplay.drm", &mediaKeySystem);
50e41f4b71Sopenharmony_ci    if (ret != DRM_ERR_OK) {
51e41f4b71Sopenharmony_ci        printf("OH_MediaKeySystem_Create failed.");
52e41f4b71Sopenharmony_ci    }
53e41f4b71Sopenharmony_ci   ```
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci6. (Optional) Declare the MediaKeySystem event listener callback.
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci   ```c++
58e41f4b71Sopenharmony_ci     // This callback applies to the scenario where there are multiple MediaKeySystem instances.
59e41f4b71Sopenharmony_ci    static Drm_ErrCode SystemCallBack(DRM_EventType eventType, uint8_t *info, int32_t infoLen, char *extra)
60e41f4b71Sopenharmony_ci    {
61e41f4b71Sopenharmony_ci        printf("SystemCallBack");
62e41f4b71Sopenharmony_ci    }
63e41f4b71Sopenharmony_ci    // This callback applies to the scenario where there is only one MediaKeySystem instance.
64e41f4b71Sopenharmony_ci    static Drm_ErrCode SystemCallBackWithObj(MediaKeySystem *mediaKeySystem, DRM_EventType eventType,
65e41f4b71Sopenharmony_ci    uint8_t *info, int32_t infoLen, char *extra)
66e41f4b71Sopenharmony_ci    {
67e41f4b71Sopenharmony_ci        printf("TestSystemCallBackWithObj");
68e41f4b71Sopenharmony_ci    }
69e41f4b71Sopenharmony_ci   ```
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci7. (Optional) Set the MediaKeySystem event listener callback.
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci   ```c++
74e41f4b71Sopenharmony_ci     // This callback applies to the scenario where there are multiple MediaKeySystem instances.
75e41f4b71Sopenharmony_ci    Drm_ErrCode ret = OH_MediaKeySystem_SetMediaKeySystemCallback(mediaKeySystem, SystemCallBack);
76e41f4b71Sopenharmony_ci    if (ret != DRM_ERR_OK) {
77e41f4b71Sopenharmony_ci        printf("OH_MediaKeySystem_SetMediaKeySystemCallback failed.");
78e41f4b71Sopenharmony_ci    }
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci    // This callback applies to the scenario where there is only one MediaKeySystem instance.
81e41f4b71Sopenharmony_ci    ret = OH_MediaKeySystem_SetCallback(mediaKeySystem, SystemCallBackWithObj);
82e41f4b71Sopenharmony_ci    if (ret != DRM_ERR_OK) {
83e41f4b71Sopenharmony_ci        printf("OH_MediaKeySystem_SetCallback failed.");
84e41f4b71Sopenharmony_ci    }
85e41f4b71Sopenharmony_ci   ```
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci8. Create a **MediaKeySession** instance.
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci   ```c++
90e41f4b71Sopenharmony_ci    MediaKeySession *mediaKeySession = nullptr;
91e41f4b71Sopenharmony_ci    DRM_ContentProtectionLevel contentProtectionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
92e41f4b71Sopenharmony_ci    ret = OH_MediaKeySystem_CreateMediaKeySession(mediaKeySystem, &contentProtectionLevel, &mediaKeySession);
93e41f4b71Sopenharmony_ci    if (ret != DRM_ERR_OK || mediaKeySession == nullptr) {
94e41f4b71Sopenharmony_ci        printf("OH_MediaKeySystem_CreateMediaKeySession failed.");
95e41f4b71Sopenharmony_ci    }
96e41f4b71Sopenharmony_ci   ```
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci9. Check the DRM certificate status of the device. If the device does not have a DRM certificate or the DRM certificate status is abnormal, generate a provision request to obtain a DRM certificate and process its response.
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci   ```c++
101e41f4b71Sopenharmony_ci    unsigned char request[12288] = { 0x00 };  // The maximum length of a provision request is 12288. Apply for memory based on the actual length.
102e41f4b71Sopenharmony_ci    int32_t requestLen = 12288;
103e41f4b71Sopenharmony_ci    // The maximum length of the DRM service URL is 2048.
104e41f4b71Sopenharmony_ci    char defaultUrl[2048] = { 0x00 };
105e41f4b71Sopenharmony_ci    int32_t defaultUrlLen = 2048;
106e41f4b71Sopenharmony_ci    DRM_CertificateStatus certStatus = CERT_STATUS_INVALID;
107e41f4b71Sopenharmony_ci    // Check the DRM certificate status of the device.
108e41f4b71Sopenharmony_ci    ret = OH_MediaKeySystem_GetCertificateStatus(mediaKeySystem, &certStatus);
109e41f4b71Sopenharmony_ci    if (ret == DRM_ERR_OK && certStatus == CERT_STATUS_NOT_PROVISIONED) {
110e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_GenerateKeySystemRequest(mediaKeySystem, request, &requestLen, defaultUrl,
111e41f4b71Sopenharmony_ci            defaultUrlLen);
112e41f4b71Sopenharmony_ci        /* 
113e41f4b71Sopenharmony_ci          The application sends a provision request to the DRM service through a network request, obtains a response,
114e41f4b71Sopenharmony_ci          and sets the response to the device. Pass in the actual value and length.
115e41f4b71Sopenharmony_ci        */
116e41f4b71Sopenharmony_ci        unsigned char KeySystemResponse[12288] = {0x00};
117e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_ProcessKeySystemResponse(mediaKeySystem, KeySystemResponse, sizeof(KeySystemResponse));
118e41f4b71Sopenharmony_ci        if (ret != DRM_ERR_OK) {
119e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_ProcessKeySystemResponse failed.");
120e41f4b71Sopenharmony_ci        }
121e41f4b71Sopenharmony_ci    }
122e41f4b71Sopenharmony_ci   ```
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci10. (Optional) Obtain the IDs of offline media keys, obtain their status, and clear the keys.
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci       ```c++
127e41f4b71Sopenharmony_ci        DRM_OfflineMediakeyIdArray offlineMediaKeyIds;
128e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_GetOfflineMediaKeyIds(mediaKeySystem, &offlineMediaKeyIds);
129e41f4b71Sopenharmony_ci        if (ret != DRM_ERR_OK) {
130e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_GetOfflineMediaKeyIds failed.");
131e41f4b71Sopenharmony_ci        }
132e41f4b71Sopenharmony_ci        DRM_OfflineMediaKeyStatus OfflineMediaKeyStatus = OFFLINE_MEDIA_KEY_STATUS_UNKNOWN;
133e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_GetOfflineMediaKeyStatus(mediaKeySystem, offlineMediaKeyIds.ids[0], offlineMediaKeyIds.idsLen[0], &OfflineMediaKeyStatus);
134e41f4b71Sopenharmony_ci        if (ret != DRM_ERR_OK) {
135e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_GetOfflineMediaKeyStatus failed.");
136e41f4b71Sopenharmony_ci        }
137e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_ClearOfflineMediaKeys(mediaKeySystem, offlineMediaKeyIds.ids[0], offlineMediaKeyIds.idsLen[0]);
138e41f4b71Sopenharmony_ci        if (ret != DRM_ERR_OK) {
139e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_ClearOfflineMediaKeys failed.");
140e41f4b71Sopenharmony_ci        }
141e41f4b71Sopenharmony_ci       ```
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci11. (Optional) Set and obtain the DRM configuration information.
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci    > **NOTE**
146e41f4b71Sopenharmony_ci    >
147e41f4b71Sopenharmony_ci    > The configuration information may vary according to the DRM scheme. The supported configuration item names are "vendor", "version", "description", "algorithms", "maxSessionNum", and "currentHDCPLevel." The DRM configuration information can be set only when the scheme supports the setting of configuration items.
148e41f4b71Sopenharmony_ci    
149e41f4b71Sopenharmony_ci       ```c++
150e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_SetConfigurationString(mediaKeySystem, "version", "2.0"); // Set the configuration information of the string type.
151e41f4b71Sopenharmony_ci        if (ret == DRM_ERR_OK) {
152e41f4b71Sopenharmony_ci            printf("MediaKeySystem_SetConfigurationString success");
153e41f4b71Sopenharmony_ci        } else {
154e41f4b71Sopenharmony_ci            printf("MediaKeySystem_SetConfigurationString failed. %d ", ret);
155e41f4b71Sopenharmony_ci        }
156e41f4b71Sopenharmony_ci        char value[32];
157e41f4b71Sopenharmony_ci        int32_t valueLen = 32;
158e41f4b71Sopenharmony_ci        // Obtain the value of a configuration item in the form of a string.
159e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_GetConfigurationString(mediaKeySystem, "version", value, valueLen);
160e41f4b71Sopenharmony_ci        if (ret == DRM_ERR_OK) {
161e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_GetConfigurationString success");
162e41f4b71Sopenharmony_ci        } else {
163e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_GetConfigurationString failed. %d ", ret);
164e41f4b71Sopenharmony_ci        }
165e41f4b71Sopenharmony_ci        // Set the configuration information of the character array type based on the actual data and length.
166e41f4b71Sopenharmony_ci        uint8_t description[4] = {0x00, 0x00, 0x00, 0x00};
167e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_SetConfigurationByteArray(mediaKeySystem, "description", description, sizeof(description)/sizeof(uint8_t));
168e41f4b71Sopenharmony_ci        if (ret == DRM_ERR_OK) {
169e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_SetConfigurationByteArray success ");
170e41f4b71Sopenharmony_ci        } else {
171e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_SetConfigurationByteArray failed. %d ", ret);
172e41f4b71Sopenharmony_ci        }
173e41f4b71Sopenharmony_ci         // Obtain the configuration information of the character array type. Pass in the actual data.
174e41f4b71Sopenharmony_ci        uint8_t descriptionValue[32];
175e41f4b71Sopenharmony_ci        int32_t descriptionValueLen = 32;
176e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_GetConfigurationByteArray(mediaKeySystem, "description", descriptionValue, &descriptionValueLen);
177e41f4b71Sopenharmony_ci        if (ret == DRM_ERR_OK) {
178e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_GetConfigurationByteArray success ");
179e41f4b71Sopenharmony_ci        } else {
180e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_GetConfigurationByteArray failed. %d ", ret);
181e41f4b71Sopenharmony_ci        }
182e41f4b71Sopenharmony_ci       ```
183e41f4b71Sopenharmony_ci    
184e41f4b71Sopenharmony_ci12. (Optional) Obtain the maximum content protection level supported by the device.
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci       ```c++
187e41f4b71Sopenharmony_ci        DRM_ContentProtectionLevel contentProtectionLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
188e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_GetMaxContentProtectionLevel(mediaKeySystem, &contentProtectionLevel);
189e41f4b71Sopenharmony_ci        if (ret != DRM_ERR_OK) {
190e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_GetMaxContentProtectionLevel failed.");
191e41f4b71Sopenharmony_ci        }
192e41f4b71Sopenharmony_ci       ```
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci13. Destroy the **MediaKeySession** instance.
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci       ```c++
197e41f4b71Sopenharmony_ci        ret = OH_MediaKeySession_Destroy(mediaKeySession);
198e41f4b71Sopenharmony_ci        if (ret != DRM_ERR_OK) {
199e41f4b71Sopenharmony_ci            printf("OH_MediaKeySession_Destroy failed.");
200e41f4b71Sopenharmony_ci        }
201e41f4b71Sopenharmony_ci       ```
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci14. Destroy the **MediaKeySystem** instance.
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci       ```c++
206e41f4b71Sopenharmony_ci        ret = OH_MediaKeySystem_Destroy(mediaKeySystem);
207e41f4b71Sopenharmony_ci        if (ret != DRM_ERR_OK) {
208e41f4b71Sopenharmony_ci            printf("OH_MediaKeySystem_Destroy failed.");
209e41f4b71Sopenharmony_ci        }
210e41f4b71Sopenharmony_ci       ```