|
| 1 | +import type { |
| 2 | + CertificateRequest, |
| 3 | + CertificateResponse, |
| 4 | + ContentProtectionIntegration, |
| 5 | + LicenseRequest, |
| 6 | + LicenseResponse, |
| 7 | + MaybeAsync, |
| 8 | +} from 'react-native-theoplayer'; |
| 9 | +import { |
| 10 | + fromUint8ArrayToString, |
| 11 | + fromBase64StringToUint8Array, |
| 12 | + fromStringToUint8Array, |
| 13 | + fromUint8ArrayToBase64String, |
| 14 | + BufferSource, |
| 15 | +} from 'react-native-theoplayer'; |
| 16 | +import type { MediaKindDrmConfiguration } from './MediaKindDrmConfiguration'; |
| 17 | +import { getFairplayCertificateURL, getFairplayLicenseAcquisitionURL } from '../utils/drmUtils'; |
| 18 | + |
| 19 | +export class MediaKindDrmFairplayContentProtectionIntegration implements ContentProtectionIntegration { |
| 20 | + static readonly DEFAULT_CERTIFICATE_URL = 'insert default certificate url here'; |
| 21 | + static readonly DEFAULT_LICENSE_URL = 'insert default license url here'; |
| 22 | + |
| 23 | + private readonly contentProtectionConfiguration: MediaKindDrmConfiguration; |
| 24 | + private contentId: string | undefined = undefined; |
| 25 | + |
| 26 | + constructor(configuration: MediaKindDrmConfiguration) { |
| 27 | + this.contentProtectionConfiguration = configuration; |
| 28 | + } |
| 29 | + |
| 30 | + onCertificateRequest(request: CertificateRequest): MaybeAsync<Partial<CertificateRequest> | BufferSource> { |
| 31 | + request.url = getFairplayCertificateURL( |
| 32 | + this.contentProtectionConfiguration, |
| 33 | + MediaKindDrmFairplayContentProtectionIntegration.DEFAULT_CERTIFICATE_URL, |
| 34 | + ); |
| 35 | + return request; |
| 36 | + } |
| 37 | + |
| 38 | + onCertificateResponse(response: CertificateResponse): MaybeAsync<BufferSource> { |
| 39 | + let certificate = fromUint8ArrayToString(response.body); |
| 40 | + const match = certificate.match(/<cert>([\s\S]*?)<\/cert>/); |
| 41 | + if (match) { |
| 42 | + certificate = match[1].replace(/\s/g, ''); |
| 43 | + } |
| 44 | + return fromBase64StringToUint8Array(certificate); |
| 45 | + } |
| 46 | + |
| 47 | + onLicenseRequest(request: LicenseRequest): MaybeAsync<Partial<LicenseRequest> | BufferSource> { |
| 48 | + request.url = getFairplayLicenseAcquisitionURL( |
| 49 | + this.contentProtectionConfiguration, |
| 50 | + MediaKindDrmFairplayContentProtectionIntegration.DEFAULT_LICENSE_URL, |
| 51 | + ); |
| 52 | + if (request.body !== null) { |
| 53 | + let spc = fromUint8ArrayToBase64String(request.body); |
| 54 | + request.body = fromStringToUint8Array(spc); |
| 55 | + } |
| 56 | + return request; |
| 57 | + } |
| 58 | + |
| 59 | + onLicenseResponse(response: LicenseResponse): MaybeAsync<BufferSource> { |
| 60 | + let license = fromUint8ArrayToString(response.body); |
| 61 | + const match = license.match(/<ckc>([\s\S]*?)<\/ckc>/); |
| 62 | + if (match) { |
| 63 | + license = match[1].replace(/\s/g, ''); |
| 64 | + } |
| 65 | + return fromBase64StringToUint8Array(license); |
| 66 | + } |
| 67 | + |
| 68 | + extractFairplayContentId(skdUrl: string): string { |
| 69 | + // skdUrl without the "skd://" scheme |
| 70 | + this.contentId = skdUrl.substring(6); |
| 71 | + return this.contentId; |
| 72 | + } |
| 73 | +} |
0 commit comments