Skip to content

Commit

Permalink
show error on simulator when using DRM
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMoch committed Sep 18, 2024
1 parent c60756c commit 85a027c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
4 changes: 2 additions & 2 deletions ios/Video/DataStructures/VideoSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct VideoSource {
let cropEnd: Int64?
let customMetadata: CustomMetadata?
/* DRM */
let drm: DRMParams?
let drm: DRMParams
var textTracks: [TextTrack] = []

let json: NSDictionary?
Expand All @@ -28,7 +28,7 @@ struct VideoSource {
self.cropStart = nil
self.cropEnd = nil
self.customMetadata = nil
self.drm = nil
self.drm = DRMParams(nil)
return
}
self.json = json
Expand Down
27 changes: 20 additions & 7 deletions ios/Video/Features/DRMManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import AVFoundation

class DRMManager: NSObject {
static let queue = DispatchQueue(label: "RNVideoContentKeyDelegateQueue")
let contentKeySession: AVContentKeySession
let contentKeySession: AVContentKeySession?

var drmParams: DRMParams?
var reactTag: NSNumber?
Expand All @@ -20,10 +20,15 @@ class DRMManager: NSObject {
var pendingLicenses: [String: AVContentKeyRequest] = [:]

override init() {
contentKeySession = AVContentKeySession(keySystem: .fairPlayStreaming)
super.init()

contentKeySession.setDelegate(self, queue: DRMManager.queue)
#if targetEnvironment(simulator)
contentKeySession = nil
super.init()
#else
contentKeySession = AVContentKeySession(keySystem: .fairPlayStreaming)
super.init()

contentKeySession?.setDelegate(self, queue: DRMManager.queue)
#endif
}

func createContentKeyRequest(
Expand All @@ -46,7 +51,15 @@ class DRMManager: NSObject {
return
}

contentKeySession.addContentKeyRecipient(asset)
#if targetEnvironment(simulator)
DebugLog("Simulator is not supported for FairPlay DRM.")
self.onVideoError?([
"error": RCTVideoErrorHandler.createError(from: RCTVideoError.simulatorDRMNotSuported),
"target": self.reactTag as Any,
])
#endif

contentKeySession?.addContentKeyRecipient(asset)
}

// MARK: - Internal
Expand Down Expand Up @@ -106,7 +119,7 @@ class DRMManager: NSObject {
}

keyRequest.processContentKeyResponseError(error)
contentKeySession.expire()
contentKeySession?.expire()
}

// MARK: - Private
Expand Down
9 changes: 9 additions & 0 deletions ios/Video/Features/RCTVideoErrorHandling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum RCTVideoError: Error, Hashable {
case embeddedKeyExtractionFailed
case offlineDRMNotSuported
case unsupportedDRMType
case simulatorDRMNotSuported

var errorCode: Int {
switch self {
Expand Down Expand Up @@ -51,6 +52,8 @@ enum RCTVideoError: Error, Hashable {
return 1013
case .unsupportedDRMType:
return 1014
case .simulatorDRMNotSuported:
return 1015
}
}
}
Expand Down Expand Up @@ -90,6 +93,8 @@ extension RCTVideoError: LocalizedError {
return NSLocalizedString("Offline DRM is not supported, see https://github.com/TheWidlarzGroup/react-native-video/issues/3539", comment: "")
case .unsupportedDRMType:
return NSLocalizedString("Unsupporeted DRM type", comment: "")
case .simulatorDRMNotSuported:
return NSLocalizedString("DRM on simulators is not supported", comment: "")
}
}

Expand Down Expand Up @@ -125,6 +130,8 @@ extension RCTVideoError: LocalizedError {
return NSLocalizedString("You tried to use Offline DRM but it is not supported yet", comment: "")
case .unsupportedDRMType:
return NSLocalizedString("You tried to use unsupporeted DRM type", comment: "")
case .simulatorDRMNotSuported:
return NSLocalizedString("You tried to DRM on a simulator", comment: "")
}
}

Expand Down Expand Up @@ -160,6 +167,8 @@ extension RCTVideoError: LocalizedError {
return NSLocalizedString("Check if localSourceEncryptionKeyScheme is setted", comment: "")
case .unsupportedDRMType:
return NSLocalizedString("Verifiy that you are using fairplay (on Apple devices)", comment: "")
case .simulatorDRMNotSuported:
return NSLocalizedString("You need to test DRM conent on real device", comment: "")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
"type": _source?.type ?? NSNull(),
"isNetwork": NSNumber(value: _source?.isNetwork ?? false),
],
"drm": source.drm?.json ?? NSNull(),
"drm": source.drm.json ?? NSNull(),
"target": reactTag as Any,
])

Expand Down Expand Up @@ -457,7 +457,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
}
#endif

if source.drm?.type != nil {
if source.drm.json != nil {
if _drmManager == nil {
_drmManager = DRMManager()
}
Expand Down

0 comments on commit 85a027c

Please sign in to comment.