1# DRM Development (ArkTS) 2 3You can call the ArkTS APIs of DRM Kit to implement digital copyright protection features such as DRM certificate management, DRM license management, DRM content authorization, and DRM content decryption. 4 5DRM Kit provides MediaKeySystem to manage DRM certificates, DRM licenses, and MediaKeySession instances. MediaKeySession is responsible for authorizing DRM content and can work with Media Kit or AVCodec Kit to decrypt the DRM content, thereby enabling playback of DRM-protected content. 6 7## How to Develop 8 9Read [DRM](../../reference/apis-drm-kit/arkts-apis-drm.md) for the API reference. 10 111. Import the DRM Kit module. 12 13 ```ts 14 import { drm } from '@kit.DrmKit'; 15 ``` 16 172. Import the BusinessError module to capture error codes from the DRM Kit APIs. 18 19 ```ts 20 import { BusinessError } from '@kit.BasicServicesKit'; 21 ``` 22 233. (Optional) Obtain the name and ID list of the DRM solutions supported by the device. 24 25 ```ts 26 let description: drm.MediaKeySystemDescription[] = drm.getMediaKeySystems(); 27 ``` 28 29 If the returned array is empty, no DRM solution is supported by the device. 30 314. (Optional) Check whether the device supports the specified DRM solution based on the name, MIME type, and content protection level. 32 33 ```ts 34 let isSupported: boolean = drm.isMediaKeySystemSupported("com.clearplay.drm", "video/mp4", drm.ContentProtectionLevel.CONTENT_PROTECTION_LEVEL_SW_CRYPTO); 35 ``` 36 37 The value **false** means that the device does not support the specified DRM solution. 38 395. Create a MediaKeySystem instance. 40 41 ```ts 42 let mediaKeySystem: drm.MediaKeySystem = drm.createMediaKeySystem("com.clearplay.drm"); 43 ``` 44 45 If the creation fails, **undefined** is returned, indicating that the device does not support the DRM solution. 46 476. (Optional) Set a MediaKeySystem status listener. 48 49 Register the keySystemRequired callback to listen for DRM certificate request events. This event is triggered when the device needs a DRM certificate. You are advised to complete the certificate request and handling process at this point. 50 51 ```ts 52 mediaKeySystem.on('keySystemRequired', (eventInfo: drm.EventInfo) => { 53 console.info('keySystemRequired' + 'extra:' + eventInfo.extraInfo + ' data:' + eventInfo.info); 54 // Request and process the DRM certificate. 55 }); 56 ``` 57 587. (Optional) Obtain the status of the DRM certificate. 59 60 ```ts 61 let certificateStatus: drm.CertificateStatus = mediaKeySystem.getCertificateStatus(); 62 ``` 63 648. (Optional) Generate a DRM certificate request and process its response. 65 66 If the DRM certificate is not in the drm.CertificateStatus.CERT_STATUS_PROVISIONED state, generate a DRM certificate request and process its response. 67 68 ```ts 69 if(certificateStatus != drm.CertificateStatus.CERT_STATUS_PROVISIONED) { 70 mediaKeySystem.generateKeySystemRequest().then(async (drmRequest: drm.ProvisionRequest) => { 71 console.info("generateKeySystemRequest success", drmRequest.data, drmRequest.defaultURL); 72 }).catch((err:BusinessError) =>{ 73 console.error("generateKeySystemRequest err end", err.code); 74 }); 75 } else { 76 console.info("The certificate already exists."); 77 } 78 // Send drmRequest.data returned by the DRM certificate request to the DRM certificate service through a network request to obtain a response and process the response. 79 let provisionResponseByte = new Uint8Array([0x00, 0x00, 0x00, 0x00]); // Response to the DRM certificate request. 80 mediaKeySystem.processKeySystemResponse(provisionResponseByte).then(() => { 81 console.info("processKeySystemResponse success"); 82 }).catch((err:BusinessError) =>{ 83 console.error("processKeySystemResponse err end", err.code); 84 }); 85 ``` 86 879. Create a MediaKeySession instance. 88 89 Create a MediaKeySession instance with the default content protection level of the DRM solution. 90 91 ```ts 92 let mediaKeySession: drm.MediaKeySession = mediaKeySystem.createMediaKeySession(); 93 ``` 94 9510. (Optional) Set a MediaKeySession status listener. 96 97 Listen for events of the MediaKeySession instance, including media key request events, media key expiration events, media key validity period update events, and media key change events. 98 99 - Listen for media key request event. You are advised to complete the media key request and handling process at this point. 100 101 ```ts 102 mediaKeySession.on('keyRequired', (eventInfo: drm.EventInfo) => { 103 console.info('keyRequired' + 'info:' + eventInfo.info + ' extraInfo:' + eventInfo.extraInfo); 104 // Request and process the media key. 105 }); 106 ``` 107 108 - Listen for media key expiration events. 109 110 ```ts 111 mediaKeySession.on('keyExpired', (eventInfo: drm.EventInfo) => { 112 console.info('keyExpired' + 'info:' + eventInfo.info + ' extraInfo:' + eventInfo.extraInfo); 113 }); 114 ``` 115 116 - Listen for media key validity period update events. 117 118 ```ts 119 mediaKeySession.on('expirationUpdate', (eventInfo: drm.EventInfo) => { 120 console.info('expirationUpdate' + 'info:' + eventInfo.info + ' extraInfo:' + eventInfo.extraInfo); 121 }); 122 ``` 123 124 - Listen for media key change events. 125 126 ```ts 127 mediaKeySession.on('keysChange', (keyInfo : drm.KeysInfo[], newKeyAvailable:boolean) => { 128 for(let i = 0; i < keyInfo.length; i++){ 129 console.info('keysChange' + 'info:' + keyInfo[i].keyId + ' extraInfo:' + keyInfo[i].value); 130 } 131 }); 132 ``` 133 13411. (Optional) Check whether secure decoding is required. 135 136 ```ts 137 try { 138 let status: boolean = mediaKeySession.requireSecureDecoderModule("video/avc"); 139 } catch (err) { 140 let error = err as BusinessError; 141 console.error(`requireSecureDecoderModule ERROR: ${error}`); 142 } 143 ``` 144 14512. Generate a media key request and process its response. 146 147 After obtaining the DRM information of the DRM content, generate a media key request and process its response to request a license for DRM content authorization. 148 149 ```ts 150 // Generate initData based on PSSH in the DRM information as required by the DRM solution. 151 let initData = new Uint8Array([0x00, 0x00, 0x00, 0x00]); 152 // Set optional data based on the DRM solution. 153 let optionalData:drm.OptionsData[] = [{ 154 name: "optionalDataName", 155 value: "optionalDataValue" 156 }]; 157 // Request and response for an online media key. 158 mediaKeySession.generateMediaKeyRequest("video/mp4", initData, drm.MediaKeyType.MEDIA_KEY_TYPE_ONLINE, optionalData).then(async (licenseRequest: drm.MediaKeyRequest) => { 159 console.info("generateMediaKeyRequest success", licenseRequest.mediaKeyRequestType, licenseRequest.data, licenseRequest.defaultURL); 160 // Send licenseRequest.data returned by the media key request to the DRM service through a network request to obtain a media key response and process the response. 161 let licenseResponse = new Uint8Array([0x00, 0x00, 0x00, 0x00]); // Media key response. 162 mediaKeySession.processMediaKeyResponse(licenseResponse).then((mediaKeyId: Uint8Array) => { 163 console.info("processMediaKeyResponse success"); 164 }).catch((err:BusinessError) =>{ 165 console.error("processMediaKeyResponse err end", err.code); 166 }); 167 }).catch((err:BusinessError) =>{ 168 console.error("generateMediaKeyRequest err end", err.code); 169 }); 170 // Request and response for an offline media key. 171 let offlineMediaKeyId: Uint8Array; 172 mediaKeySession.generateMediaKeyRequest("video/mp4", initData, drm.MediaKeyType.MEDIA_KEY_TYPE_OFFLINE, optionalData).then((licenseRequest: drm.MediaKeyRequest) => { 173 console.info("generateMediaKeyRequest success", licenseRequest.mediaKeyRequestType, licenseRequest.data, licenseRequest.defaultURL); 174 // Send licenseRequest.data returned by the media key request to the DRM service through a network request to obtain a media key response and process the response. 175 let licenseResponse = new Uint8Array([0x00, 0x00, 0x00, 0x00]); // Media key response. 176 mediaKeySession.processMediaKeyResponse(licenseResponse).then((mediaKeyId: Uint8Array) => { 177 offlineMediaKeyId = new Uint8Array(mediaKeyId); 178 console.info("processMediaKeyResponse success"); 179 }).catch((err:BusinessError) =>{ 180 console.error("processMediaKeyResponse err end", err.code); 181 }); 182 }).catch((err:BusinessError) =>{ 183 console.error("generateMediaKeyRequest err end", err.code); 184 }); 185 ``` 186 18713. (Optional) Restore offline media keys. 188 189 ```ts 190 mediaKeySession.restoreOfflineMediaKeys(offlineMediaKeyId).then(() => { 191 console.info("restoreOfflineMediaKeys success."); 192 }).catch((err: BusinessError) => { 193 console.error(`restoreOfflineMediaKeys: ERROR: ${err}`); 194 }); 195 ``` 196 19714. (Optional) Check the status of media keys. 198 199 ```ts 200 let mediaKeyStatus: drm.MediaKeyStatus[] 201 try { 202 mediaKeyStatus = mediaKeySession.checkMediaKeyStatus() 203 } catch (err) { 204 let error = err as BusinessError; 205 console.error(`checkMediaKeyStatus: ERROR: ${error}`); 206 } 207 ``` 208 20915. (Optional) Obtain the IDs of offline media keys, obtain their status, and clear the keys. 210 211 The media key ID is used for offline media key management. 212 213 ```ts 214 let offlineMediaKeyIds: Uint8Array[] = mediaKeySystem.getOfflineMediaKeyIds(); 215 try { 216 let offlineMediaKeyStatus: drm.OfflineMediaKeyStatus = mediaKeySystem.getOfflineMediaKeyStatus(offlineMediaKeyIds[0]); 217 } catch (err) { 218 let error = err as BusinessError; 219 console.error(`getOfflineMediaKeyStatus ERROR: ${error}`); 220 } 221 try { 222 mediaKeySystem.clearOfflineMediaKeys(offlineMediaKeyIds[0]); 223 } catch (err) { 224 let error = err as BusinessError; 225 console.error(`clearOfflineMediaKeys ERROR: ${error}`); 226 } 227 ``` 228 22916. Destroy the MediaKeySession instance. 230 231 Destroy the MediaKeySession instance when the encrypted content is decrypted and the instance is no longer needed. 232 233 ```ts 234 // Release resources when the MediaKeySession instance is no longer needed. 235 mediaKeySession.destroy(); 236 ``` 237 23817. Destroy the MediaKeySystem instance. 239 240 Destroy the MediaKeySystem instance when it is no longer needed. 241 242 ```ts 243 // Release resources when the MediaKeySystem instance is no longer needed. 244 mediaKeySystem.destroy(); 245 ``` 246