|
| 1 | +// |
| 2 | +// DRMManager+OnGetLicense.swift |
| 3 | +// react-native-video |
| 4 | +// |
| 5 | +// Created by Krzysztof Moch on 14/08/2024. |
| 6 | +// |
| 7 | + |
| 8 | +import AVFoundation |
| 9 | + |
| 10 | +extension DRMManager { |
| 11 | + func requestLicenseFromJS(spcData: Data, assetId: String, keyRequest: AVContentKeyRequest) async throws { |
| 12 | + guard let onGetLicense else { |
| 13 | + throw RCTVideoError.noDataFromLicenseRequest |
| 14 | + } |
| 15 | + |
| 16 | + guard let licenseServerUrl = drmParams?.licenseServer, !licenseServerUrl.isEmpty else { |
| 17 | + throw RCTVideoError.noLicenseServerURL |
| 18 | + } |
| 19 | + |
| 20 | + guard let loadedLicenseUrl = keyRequest.identifier as? String else { |
| 21 | + throw RCTVideoError.invalidContentId |
| 22 | + } |
| 23 | + |
| 24 | + pendingLicenses[loadedLicenseUrl] = keyRequest |
| 25 | + |
| 26 | + DispatchQueue.main.async { [weak self] in |
| 27 | + onGetLicense([ |
| 28 | + "licenseUrl": licenseServerUrl, |
| 29 | + "loadedLicenseUrl": loadedLicenseUrl, |
| 30 | + "contentId": assetId, |
| 31 | + "spcBase64": spcData.base64EncodedString(), |
| 32 | + "target": self?.reactTag as Any, |
| 33 | + ]) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + func setJSLicenseResult(license: String, licenseUrl: String) { |
| 38 | + guard let keyContentRequest = pendingLicenses[licenseUrl] else { |
| 39 | + setJSLicenseError(error: "Loading request for licenseUrl \(licenseUrl) not found", licenseUrl: licenseUrl) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + guard let responseData = Data(base64Encoded: license) else { |
| 44 | + setJSLicenseError(error: "Invalid license data", licenseUrl: licenseUrl) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + do { |
| 49 | + try finishProcessingContentKeyRequest(keyRequest: keyContentRequest, license: responseData) |
| 50 | + pendingLicenses.removeValue(forKey: licenseUrl) |
| 51 | + } catch { |
| 52 | + handleError(error, for: keyContentRequest) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + func setJSLicenseError(error: String, licenseUrl: String) { |
| 57 | + let rctError = RCTVideoError.fromJSPart(error) |
| 58 | + |
| 59 | + DispatchQueue.main.async { [weak self] in |
| 60 | + self?.onVideoError?([ |
| 61 | + "error": RCTVideoErrorHandler.createError(from: rctError), |
| 62 | + "target": self?.reactTag as Any, |
| 63 | + ]) |
| 64 | + } |
| 65 | + |
| 66 | + pendingLicenses.removeValue(forKey: licenseUrl) |
| 67 | + } |
| 68 | +} |
0 commit comments