2022-05-19 07:29:25 -06:00
|
|
|
import AVFoundation
|
|
|
|
import Promises
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
enum RCTVideoDRM {
|
2022-05-19 07:29:25 -06:00
|
|
|
static func fetchLicense(
|
|
|
|
licenseServer: String,
|
|
|
|
spcData: Data?,
|
|
|
|
contentId: String,
|
2023-12-07 00:47:40 -07:00
|
|
|
headers: [String: Any]?
|
2022-05-19 07:29:25 -06:00
|
|
|
) -> Promise<Data> {
|
2023-12-07 00:47:40 -07:00
|
|
|
let request = createLicenseRequest(licenseServer: licenseServer, spcData: spcData, contentId: contentId, headers: headers)
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
return Promise<Data>(on: .global()) { fulfill, reject in
|
2023-12-07 00:47:40 -07:00
|
|
|
let postDataTask = URLSession.shared.dataTask(
|
|
|
|
with: request as URLRequest,
|
|
|
|
completionHandler: { (data: Data!, response: URLResponse!, error: Error!) in
|
|
|
|
let httpResponse: HTTPURLResponse! = (response as! HTTPURLResponse)
|
|
|
|
|
|
|
|
guard error == nil else {
|
|
|
|
print("Error getting license from \(licenseServer), HTTP status code \(httpResponse.statusCode)")
|
|
|
|
reject(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard httpResponse.statusCode == 200 else {
|
|
|
|
print("Error getting license from \(licenseServer), HTTP status code \(httpResponse.statusCode)")
|
|
|
|
reject(RCTVideoErrorHandler.licenseRequestNotOk(httpResponse.statusCode))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard data != nil, let decodedData = Data(base64Encoded: data, options: []) else {
|
|
|
|
reject(RCTVideoErrorHandler.noDataFromLicenseRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fulfill(decodedData)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
)
|
2022-05-19 07:29:25 -06:00
|
|
|
postDataTask.resume()
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
static func createLicenseRequest(
|
|
|
|
licenseServer: String,
|
|
|
|
spcData: Data?,
|
|
|
|
contentId: String,
|
2023-12-07 00:47:40 -07:00
|
|
|
headers: [String: Any]?
|
2022-05-19 07:29:25 -06:00
|
|
|
) -> URLRequest {
|
|
|
|
var request = URLRequest(url: URL(string: licenseServer)!)
|
|
|
|
request.httpMethod = "POST"
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-01-04 12:16:23 -07:00
|
|
|
if let headers {
|
2022-05-19 07:29:25 -06:00
|
|
|
for item in headers {
|
|
|
|
guard let key = item.key as? String, let value = item.value as? String else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
request.setValue(value, forHTTPHeaderField: key)
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
let spcEncoded = spcData?.base64EncodedString(options: [])
|
2023-12-07 00:47:40 -07:00
|
|
|
let spcUrlEncoded = CFURLCreateStringByAddingPercentEscapes(
|
|
|
|
kCFAllocatorDefault,
|
|
|
|
spcEncoded as? CFString? as! CFString,
|
|
|
|
nil,
|
|
|
|
"?=&+" as CFString,
|
|
|
|
CFStringBuiltInEncodings.UTF8.rawValue
|
|
|
|
) as? String
|
|
|
|
let post = String(format: "spc=%@&%@", spcUrlEncoded as! CVarArg, contentId)
|
|
|
|
let postData = post.data(using: String.Encoding.utf8, allowLossyConversion: true)
|
2022-05-19 07:29:25 -06:00
|
|
|
request.httpBody = postData
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
return request
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
static func fetchSpcData(
|
|
|
|
loadingRequest: AVAssetResourceLoadingRequest,
|
|
|
|
certificateData: Data,
|
|
|
|
contentIdData: Data
|
|
|
|
) -> Promise<Data> {
|
|
|
|
return Promise<Data>(on: .global()) { fulfill, reject in
|
2023-12-07 00:47:40 -07:00
|
|
|
var spcError: NSError!
|
2022-05-19 07:29:25 -06:00
|
|
|
var spcData: Data?
|
|
|
|
do {
|
|
|
|
spcData = try loadingRequest.streamingContentKeyRequestData(forApp: certificateData, contentIdentifier: contentIdData as Data, options: nil)
|
|
|
|
} catch _ {
|
|
|
|
print("SPC error")
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
if spcError != nil {
|
|
|
|
reject(spcError)
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let spcData else {
|
2022-05-19 07:29:25 -06:00
|
|
|
reject(RCTVideoErrorHandler.noSPC)
|
|
|
|
return
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
fulfill(spcData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
static func createCertificateData(certificateStringUrl: String?, base64Certificate: Bool?) -> Promise<Data> {
|
|
|
|
return Promise<Data>(on: .global()) { fulfill, reject in
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let certificateStringUrl,
|
2022-05-19 07:29:25 -06:00
|
|
|
let certificateURL = URL(string: certificateStringUrl.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) ?? "") else {
|
2023-12-07 00:47:40 -07:00
|
|
|
reject(RCTVideoErrorHandler.noCertificateURL)
|
2022-05-19 07:29:25 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
var certificateData: Data?
|
2022-05-19 07:29:25 -06:00
|
|
|
do {
|
2023-12-07 00:47:40 -07:00
|
|
|
certificateData = try Data(contentsOf: certificateURL)
|
|
|
|
if base64Certificate != nil {
|
2022-05-19 07:29:25 -06:00
|
|
|
certificateData = Data(base64Encoded: certificateData! as Data, options: .ignoreUnknownCharacters)
|
|
|
|
}
|
|
|
|
} catch {}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let certificateData else {
|
2022-05-19 07:29:25 -06:00
|
|
|
reject(RCTVideoErrorHandler.noCertificateData)
|
|
|
|
return
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
fulfill(certificateData)
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
static func handleWithOnGetLicense(loadingRequest: AVAssetResourceLoadingRequest, contentId: String?, certificateUrl: String?,
|
|
|
|
base64Certificate: Bool?) -> Promise<Data> {
|
2022-05-19 07:29:25 -06:00
|
|
|
let contentIdData = contentId?.data(using: .utf8)
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
return RCTVideoDRM.createCertificateData(certificateStringUrl: certificateUrl, base64Certificate: base64Certificate)
|
|
|
|
.then { certificateData -> Promise<Data> in
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let contentIdData else {
|
2022-05-19 07:29:25 -06:00
|
|
|
throw RCTVideoError.invalidContentId as! Error
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
return RCTVideoDRM.fetchSpcData(
|
2023-12-07 00:47:40 -07:00
|
|
|
loadingRequest: loadingRequest,
|
|
|
|
certificateData: certificateData,
|
|
|
|
contentIdData: contentIdData
|
2022-05-19 07:29:25 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
static func handleInternalGetLicense(
|
|
|
|
loadingRequest: AVAssetResourceLoadingRequest,
|
|
|
|
contentId: String?,
|
|
|
|
licenseServer: String?,
|
|
|
|
certificateUrl: String?,
|
|
|
|
base64Certificate: Bool?,
|
|
|
|
headers: [String: Any]?
|
|
|
|
) -> Promise<Data> {
|
2022-05-19 07:29:25 -06:00
|
|
|
let url = loadingRequest.request.url
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
guard let contentId = contentId ?? url?.absoluteString.replacingOccurrences(of: "skd://", with: "") else {
|
2022-05-19 07:29:25 -06:00
|
|
|
return Promise(RCTVideoError.invalidContentId as! Error)
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
let contentIdData = NSData(bytes: contentId.cString(using: String.Encoding.utf8), length: contentId.lengthOfBytes(using: String.Encoding.utf8)) as Data
|
|
|
|
|
|
|
|
return RCTVideoDRM.createCertificateData(certificateStringUrl: certificateUrl, base64Certificate: base64Certificate)
|
|
|
|
.then { certificateData in
|
2022-05-19 07:29:25 -06:00
|
|
|
return RCTVideoDRM.fetchSpcData(
|
2023-12-07 00:47:40 -07:00
|
|
|
loadingRequest: loadingRequest,
|
|
|
|
certificateData: certificateData,
|
|
|
|
contentIdData: contentIdData
|
2022-05-19 07:29:25 -06:00
|
|
|
)
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
.then { spcData -> Promise<Data> in
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let licenseServer else {
|
2022-05-19 07:29:25 -06:00
|
|
|
throw RCTVideoError.noLicenseServerURL as! Error
|
|
|
|
}
|
|
|
|
return RCTVideoDRM.fetchLicense(
|
|
|
|
licenseServer: licenseServer,
|
|
|
|
spcData: spcData,
|
|
|
|
contentId: contentId,
|
|
|
|
headers: headers
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|