• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using AVPlayer to Play DRM Content (ArkTS)
2
3You can call the ArkTS APIs of DRM Kit and Media Kit to implement the playback of DRM-protected content using the AVPlayer.
4
5## How to Develop
6
71. Import the DRM Kit and Media Kit modules.
8
9   ```ts
10   import { drm } from '@kit.DrmKit'
11   import { media } from '@kit.MediaKit'
12   ```
13
142. Import the BusinessError module to capture error codes from the DRM Kit APIs.
15
16   ```ts
17   import { BusinessError } from '@kit.BasicServicesKit'
18   ```
19
203. Create an AVPlayer instance and set a DRM information listener.
21
22   ```ts
23   let playerHandle: media.AVPlayer = await media.createAVPlayer()
24   playerHandle.on('mediaKeySystemInfoUpdate', async (mediaKeySystemInfo: drm.MediaKeySystemInfo[]) => {
25     console.info('player has received drmInfo signal: ' + JSON.stringify(mediaKeySystemInfo))
26     // Process DRM information.
27     // Set a decryption session.
28   })
29   ```
30
314. Create MediaKeySystem and MediaKeySession instances based on the UUID in the DRM information.
32
33   ```ts
34   let mediaKeySystem: drm.MediaKeySystem
35   let mediaKeySession: drm.MediaKeySession
36   let drmInfoArr: drm.MediaKeySystemInfo[] = mediaKeySystemInfo
37   for (let i = 0; i < drmInfoArr.length; i++) {
38     console.info('drmInfoArr - uuid: ' + drmInfoArr[i].uuid)
39     console.info('drmInfoArr - pssh: ' + drmInfoArr[i].pssh)
40     let description: drm.MediaKeySystemDescription[] = drm.getMediaKeySystems();
41     let solutionName: string = "com.clearplay.drm"
42     for (let item of description) {
43       if (drmInfoArr[i].uuid == item.uuid) {
44         solutionName = item.name
45         }
46       }
47       let isSupported: boolean = drm.isMediaKeySystemSupported(solutionName, "video/mp4");
48       if (isSupported) {
49         mediaKeySystem = drm.createMediaKeySystem(solutionName);
50         mediaKeySession = mediaKeySystem.createMediaKeySession();
51     }
52     // Request and process the media key.
53   }
54   ```
55
565. Generate a media key request based on the PSSH information in the DRM information and process its response.
57
58   ```ts
59   let initData: Uint8Array = new Uint8Array(drmInfoArr[i].pssh);
60   const optionsData: drm.OptionsData[] = [{
61     name: "optionalDataName",
62     value: "optionalDataValue"
63   }]
64   mediaKeySession.generateMediaKeyRequest("video/mp4", initData, drm.MediaKeyType.MEDIA_KEY_TYPE_ONLINE, optionsData).then(async (licenseRequest) => {
65     console.info("generateMediaKeyRequest success", licenseRequest.mediaKeyRequestType, licenseRequest.data, licenseRequest.defaultURL);
66     // 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.
67     let licenseResponse = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
68     mediaKeySession.processMediaKeyResponse(licenseResponse).then((mediaKeyId: Uint8Array) => {
69       console.info("processMediaKeyResponse success");
70     }).catch((err:BusinessError) =>{
71       console.error("processMediaKeyResponse err end", err.code);
72     });
73   }).catch((err:BusinessError) =>{
74     console.error("generateMediaKeyRequest err end", err.code);
75   });
76   ```
77
786. Set the decryption session after the media key response is successfully processed.
79
80   ```ts
81   let svp: boolean = mediaKeySession.requireSecureDecoderModule('video/avc');
82   playerHandle.setDecryptionConfig(mediaKeySession, svp)
83   ```
84
857. Destroy the AVPlayer instance and destroy the MediaKeySession and MediaKeySystem instances based on the released event.
86
87   ```ts
88   playerHandle.on('stateChange', async (state: string, reason: media.StateChangeReason) => {
89     if (state == 'released') {
90       mediaKeySession.destroy();
91       mediaKeySystem.destroy();
92     }
93   }
94   await this.playerHandle.release()
95   ```
96