Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* #FEM-1297

* Separated player error log events from regular errors.
Now use errorLog type to observe error log events. Could be helpful for debug and analytics.

* Changed player error log code

* fixed typo and static analysis issue.
  • Loading branch information
gal-orlanczyk authored Apr 2, 2017
1 parent efedddb commit 341cea7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
36 changes: 26 additions & 10 deletions Classes/PKError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ enum PlayerError: PKError {
case failedToLoadAssetFromKeys(rootError: NSError?)
case assetNotPlayable
case failedToPlayToEndTime(rootError: NSError)
case playerItemErrorLogEvent(errorLogEvent: AVPlayerItemErrorLogEvent)

static let domain = "com.kaltura.playkit.error.player"

Expand All @@ -28,7 +27,6 @@ enum PlayerError: PKError {
case .failedToLoadAssetFromKeys: return PKErrorCode.failedToLoadAssetFromKeys
case .assetNotPlayable: return PKErrorCode.assetNotPlayable
case .failedToPlayToEndTime: return PKErrorCode.failedToPlayToEndTime
case .playerItemErrorLogEvent: return PKErrorCode.playerItemErrorLogEvent
}
}

Expand All @@ -37,7 +35,6 @@ enum PlayerError: PKError {
case .failedToLoadAssetFromKeys: return "Can't use this AVAsset because one of it's keys failed to load"
case .assetNotPlayable: return "Can't use this AVAsset because it isn't playable"
case .failedToPlayToEndTime: return "Item failed to play to its end time"
case .playerItemErrorLogEvent(let errorLogEvent): return errorLogEvent.errorComment ?? ""
}
}

Expand All @@ -50,15 +47,33 @@ enum PlayerError: PKError {
return [:]
case .assetNotPlayable: return [:]
case .failedToPlayToEndTime(let rootError): return [PKErrorKeys.RootErrorKey: rootError]
case .playerItemErrorLogEvent(let errorLogEvent):
return [
PKErrorKeys.RootCodeKey: errorLogEvent.errorStatusCode,
PKErrorKeys.RootDomainKey: errorLogEvent.errorDomain
]
}
}
}

/// `PlayerErrorLog` represents an error log emitted from AVPlayer (usually non-fatal).
struct PlayerErrorLog: PKError {

static var domain = PlayerError.domain

let errorLogEvent: AVPlayerItemErrorLogEvent

var code: Int {
return PKErrorCode.playerItemErrorLogEvent
}

var errorDescription: String {
return errorLogEvent.errorComment ?? ""
}

var userInfo: [String: Any] {
return [
PKErrorKeys.RootCodeKey: errorLogEvent.errorStatusCode,
PKErrorKeys.RootDomainKey: errorLogEvent.errorDomain
]
}
}

/************************************************************/
// MARK: - PKPluginError
/************************************************************/
Expand Down Expand Up @@ -111,7 +126,7 @@ protocol PKError: Error, CustomStringConvertible {

/**
The error code.
use `switch self` to retrive the value in **enums**.
use `switch self` to retrieve the value in **enums**.
````
var code: Int {
Expand Down Expand Up @@ -222,7 +237,8 @@ struct PKErrorKeys {
@objc(FailedToLoadAssetFromKeys) public static let failedToLoadAssetFromKeys = 7000
@objc(AssetNotPlayable) public static let assetNotPlayable = 7001
@objc(FailedToPlayToEndTime) public static let failedToPlayToEndTime = 7002
@objc(PlayerItemErrorLogEvent) public static let playerItemErrorLogEvent = 7003
// PlayerErrorLog
@objc(PlayerItemErrorLogEvent) public static let playerItemErrorLogEvent = 7100
// PKPluginError
@objc(FailedToCreatePlugin) public static let failedToCreatePlugin = 2000
@objc(MissingPluginConfig) public static let missingPluginConfig = 2001
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ extension AVPlayerEngine {

func onErrorLogEntryNotification(notification: Notification) {
guard let playerItem = notification.object as? AVPlayerItem, let errorLog = playerItem.errorLog(), let lastEvent = errorLog.events.last else { return }
PKLog.error("error description: \(lastEvent.errorComment), error domain: \(lastEvent.errorDomain), error code: \(lastEvent.errorStatusCode)")
self.post(event: PlayerEvent.Error(error: PlayerError.playerItemErrorLogEvent(errorLogEvent: lastEvent)))
PKLog.warning("error description: \(String(describing: lastEvent.errorComment)), error domain: \(lastEvent.errorDomain), error code: \(lastEvent.errorStatusCode)")
self.post(event: PlayerEvent.ErrorLog(error: PlayerErrorLog(errorLogEvent: lastEvent)))
}

public func playerFailed(notification: NSNotification) {
Expand Down
12 changes: 12 additions & 0 deletions Classes/PlayerEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import AVFoundation
@objc public static let error: PlayerEvent.Type = Error.self
/// Sent when an plugin error occurs.
@objc public static let pluginError: PlayerEvent.Type = PluginError.self
/// Sent when an error log event received from player.
@objc public static let errorLog: PlayerEvent.Type = ErrorLog.self

// MARK: - Player Basic Events

Expand Down Expand Up @@ -89,6 +91,16 @@ import AVFoundation
}
}

class ErrorLog: PlayerEvent {
convenience init(nsError: NSError) {
self.init([EventDataKeys.Error: nsError])
}

convenience init(error: PKError) {
self.init([EventDataKeys.Error: error.asNSError])
}
}

class TimedMetadata: PlayerEvent {
convenience init(metadata: [AVMetadataItem]) {
self.init([EventDataKeys.Metadata: metadata])
Expand Down

0 comments on commit 341cea7

Please sign in to comment.