Skip to content

Commit

Permalink
chore: add error detail collection hooks to fairplaysession manager
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjl-mux committed Jul 24, 2024
1 parent 9e40aaa commit 73305bd
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
14 changes: 14 additions & 0 deletions Sources/MuxPlayerSwift/FairPlay/ErrorDispatcher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// ErrorDispatcher.swift
//
//

import Foundation

protocol ErrorDispatcher {
func dispatchError(
errorCode: String,
errorMessage: String,
playerObjectIdentifier: ObjectIdentifier
)
}
13 changes: 11 additions & 2 deletions Sources/MuxPlayerSwift/FairPlay/FairPlaySessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class DefaultFairPlayStreamingSessionManager<

var playbackOptionsByPlaybackID: [String: PlaybackOptions] = [:]
let contentKeySession: ContentKeySession

let errorDispatcher: (any ErrorDispatcher)

#if DEBUG
var logger: Logger = Logger(
OSLog(
Expand Down Expand Up @@ -276,6 +277,12 @@ class DefaultFairPlayStreamingSessionManager<
requestCompletion(Result.failure(
FairPlaySessionError.because(cause: error)
))
// // TODO: Confirm error code
// self.errorDispatcher.dispatchError(
// errorCode: "5001",
// errorMessage: error.localizedDescription,
// playerObjectIdentifier: <#T##ObjectIdentifier#>
// )
return
}

Expand Down Expand Up @@ -349,10 +356,12 @@ class DefaultFairPlayStreamingSessionManager<

init(
contentKeySession: ContentKeySession,
urlSession: URLSession
urlSession: URLSession,
errorDispatcher: any ErrorDispatcher
) {
self.contentKeySession = contentKeySession
self.urlSession = urlSession
self.errorDispatcher = errorDispatcher
}
}

Expand Down
19 changes: 13 additions & 6 deletions Sources/MuxPlayerSwift/GlobalLifecycle/PlayerSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,39 @@ class PlayerSDK {
let fairPlaySessionManager: FairPlayStreamingSessionManager

convenience init() {
let monitor = Monitor()

#if targetEnvironment(simulator)
self.init(
fairPlayStreamingSessionManager: DefaultFairPlayStreamingSessionManager(
contentKeySession: AVContentKeySession(keySystem: .clearKey),
urlSession: .shared
)
urlSession: .shared,
errorDispatcher: monitor
),
monitor: monitor
)
#else
let sessionManager = DefaultFairPlayStreamingSessionManager(
contentKeySession: AVContentKeySession(keySystem: .fairPlayStreaming),
urlSession: .shared
urlSession: .shared,
errorDispatcher: monitor
)
sessionManager.sessionDelegate = ContentKeySessionDelegate(
sessionManager: sessionManager
)
self.init(
fairPlayStreamingSessionManager: sessionManager
fairPlayStreamingSessionManager: sessionManager,
monitor: monitor
)
#endif
}

init(
fairPlayStreamingSessionManager: FairPlayStreamingSessionManager
fairPlayStreamingSessionManager: FairPlayStreamingSessionManager,
monitor: Monitor
) {
self.monitor = Monitor()
self.fairPlaySessionManager = fairPlayStreamingSessionManager
self.monitor = monitor

#if DEBUG
self.abrLogger = Logger(
Expand Down
22 changes: 21 additions & 1 deletion Sources/MuxPlayerSwift/Monitoring/Monitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import MuxCore
import MUXSDKStats

class Monitor {
class Monitor: ErrorDispatcher {

struct MonitoredPlayer {
var name: String
Expand Down Expand Up @@ -207,4 +207,24 @@ class Monitor {

bindings.removeValue(forKey: objectIdentifier)
}

// MARK: - Error Dispatch

func dispatchError(
errorCode: String,
errorMessage: String,
playerObjectIdentifier: ObjectIdentifier
) {
guard let monitoredPlayer = self.bindings[playerObjectIdentifier] else {
return
}

let playerName = monitoredPlayer.name

MUXSDKStats.dispatchError(
errorCode,
withMessage: errorMessage,
forPlayer: playerName
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class FairPlaySessionManagerTests : XCTestCase {
let defaultFairPlaySessionManager = DefaultFairPlayStreamingSessionManager(
// .clearKey is used because .fairPlay requires a physical device
contentKeySession: session,
urlSession: mockURLSession
urlSession: mockURLSession,
errorDispatcher: Monitor()
)
self.sessionManager = defaultFairPlaySessionManager
defaultFairPlaySessionManager.sessionDelegate = ContentKeySessionDelegate(
Expand Down Expand Up @@ -576,7 +577,8 @@ class FairPlaySessionManagerTests : XCTestCase {
)
let defaultFairPlaySessionManager = DefaultFairPlayStreamingSessionManager(
contentKeySession: session,
urlSession: mockURLSession
urlSession: mockURLSession,
errorDispatcher: Monitor()
)
self.sessionManager = defaultFairPlaySessionManager
let sessionDelegate = ContentKeySessionDelegate(
Expand Down Expand Up @@ -616,7 +618,8 @@ class FairPlaySessionManagerTests : XCTestCase {


PlayerSDK.shared = PlayerSDK(
fairPlayStreamingSessionManager: defaultFairPlaySessionManager
fairPlayStreamingSessionManager: defaultFairPlaySessionManager,
monitor: Monitor()
)

let _ = AVPlayerItem(
Expand Down
33 changes: 33 additions & 0 deletions Tests/MuxPlayerSwift/MonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class TestMonitor: Monitor {
options: MonitoringOptions,
usingDRM: Bool = false
) {
super.setupMonitoring(
playerViewController: playerViewController,
options: options,
usingDRM: usingDRM
)

monitoringRegistrations.append(
(options, usingDRM)
)
Expand All @@ -41,6 +47,12 @@ class TestMonitor: Monitor {
options: MonitoringOptions,
usingDRM: Bool = false
) {
super.setupMonitoring(
playerLayer: playerLayer,
options: options,
usingDRM: usingDRM
)

monitoringRegistrations.append(
(options, usingDRM)
)
Expand Down Expand Up @@ -137,6 +149,13 @@ class MonitorTests: XCTestCase {
)
)

let playerBinding = try XCTUnwrap(
testMonitor.bindings[ObjectIdentifier(playerViewController)]
)
XCTAssertNotNil(
playerBinding
)

let registration = try XCTUnwrap(
testMonitor.monitoringRegistrations.first
)
Expand All @@ -158,6 +177,13 @@ class MonitorTests: XCTestCase {
)
)

let playerBinding = try XCTUnwrap(
testMonitor.bindings[ObjectIdentifier(playerLayer)]
)
XCTAssertNotNil(
playerBinding
)

let registration = try XCTUnwrap(
testMonitor.monitoringRegistrations.first
)
Expand All @@ -184,6 +210,13 @@ class MonitorTests: XCTestCase {
)
)

let playerBinding = try XCTUnwrap(
testMonitor.bindings[ObjectIdentifier(preexistingPlayerLayer)]
)
XCTAssertNotNil(
playerBinding
)

let registration = try XCTUnwrap(
testMonitor.monitoringRegistrations.first
)
Expand Down

0 comments on commit 73305bd

Please sign in to comment.