Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEM-2230: Instead of calling player.duration listen to durationChanged #21

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/KavaHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class KavaHelper {
case KavaPlugin.KavaEventType.flavorSwitched.rawValue:
request.setParam(key: "actualBitrate", value: String(describing: kibibits(bits: kavaData.indicatedBitrate)))
default:
PKLog.debug("KavaEventType accured: \(eventType)")
PKLog.trace("KavaEventType accured: \(eventType)")
}
}

Expand Down
29 changes: 16 additions & 13 deletions Sources/KavaPlugin+AnalyticsPluginProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extension KavaPlugin: AnalyticsPluginProtocol {
return [
PlayerEvent.stateChanged,
PlayerEvent.loadedMetadata,
PlayerEvent.durationChanged,
PlayerEvent.play,
PlayerEvent.pause,
PlayerEvent.playing,
Expand All @@ -52,6 +53,8 @@ extension KavaPlugin: AnalyticsPluginProtocol {
strongSelf.handleStateChanged(event: event)
case is PlayerEvent.LoadedMetadata:
strongSelf.handleLoadedMetadata()
case is PlayerEvent.DurationChanged:
strongSelf.duration = event.duration as! TimeInterval
case is PlayerEvent.Play:
strongSelf.handlePlay()
case is PlayerEvent.Pause:
Expand Down Expand Up @@ -93,11 +96,11 @@ extension KavaPlugin: AnalyticsPluginProtocol {
/************************************************************/

private func handleStateChanged(event: PKEvent) {
PKLog.debug("state changed event: \(event)")
PKLog.trace("state changed event: \(event)")

switch event.newState {
case .ready:
PKLog.info("media ready")
PKLog.trace("media ready")

if let _ = bufferingStartTime {
self.kavaData.totalBufferingInCurrentInterval += -bufferingStartTime!.timeIntervalSinceNow
Expand All @@ -113,12 +116,12 @@ extension KavaPlugin: AnalyticsPluginProtocol {
}

private func handleLoadedMetadata() {
PKLog.debug("loadedMetadata event")
PKLog.trace("loadedMetadata event")
self.sendMediaLoaded()
}

private func handlePlay() {
PKLog.debug("play event")
PKLog.trace("play event")

if self.isFirstPlay {
self.joinTimeStart = Date().timeIntervalSince1970
Expand All @@ -132,14 +135,14 @@ extension KavaPlugin: AnalyticsPluginProtocol {
}

private func handlePause() {
PKLog.debug("pause event")
PKLog.trace("pause event")
self.kavaData.isPaused = true
self.sendAnalyticsEvent(event: KavaEventType.pause)
self.stopViewTimer()
}

private func handlePlaying() {
PKLog.debug("playing event")
PKLog.trace("playing event")
self.kavaData.joinTime = nil
if self.isFirstPlay {
self.isFirstPlay = false
Expand All @@ -153,7 +156,7 @@ extension KavaPlugin: AnalyticsPluginProtocol {
}

private func handleSeeking(targetSeekPosition: NSNumber?) {
PKLog.debug("seeking event")
PKLog.trace("seeking event")

if let seekPosition = targetSeekPosition {
self.kavaData.targetSeekPosition = Double(truncating: seekPosition)
Expand All @@ -163,7 +166,7 @@ extension KavaPlugin: AnalyticsPluginProtocol {
}

private func handleSourceSelected(mediaSource: PKMediaSource?) {
PKLog.debug("sourceSelected event")
PKLog.trace("sourceSelected event")

if let source = mediaSource {
self.kavaData.selectedSource = source
Expand All @@ -172,13 +175,13 @@ extension KavaPlugin: AnalyticsPluginProtocol {
}

private func handleEnded() {
PKLog.debug("ended event")
PKLog.trace("ended event")
self.sendPercentageReachedEvent(percentage: 100)
self.stopViewTimer()
}

private func handleVideoTrackChanged(_ videoTrack: NSNumber?) {
PKLog.debug("videoTrackChanged event")
PKLog.trace("videoTrackChanged event")

if let bitrate = videoTrack {
self.kavaData.indicatedBitrate = bitrate.doubleValue
Expand All @@ -187,7 +190,7 @@ extension KavaPlugin: AnalyticsPluginProtocol {
}

private func handleAudioTrackChanged(_ audioTrack: Track?) {
PKLog.debug("audioTrackChanged event")
PKLog.trace("audioTrackChanged event")

if let track = audioTrack {
if (track.language != nil) {
Expand All @@ -198,7 +201,7 @@ extension KavaPlugin: AnalyticsPluginProtocol {
}

private func handleTextTrackChanged(_ textTrack: Track?) {
PKLog.debug("textTrackChanged event")
PKLog.trace("textTrackChanged event")

let textOffDisplay: String = "Off"

Expand All @@ -216,7 +219,7 @@ extension KavaPlugin: AnalyticsPluginProtocol {
}

private func handleError(error: NSError?) {
PKLog.debug("error event")
PKLog.trace("error event")

if let err = error {
self.kavaData.errorCode = err.code
Expand Down
18 changes: 10 additions & 8 deletions Sources/KavaPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ let playbackPoints: [KavaPlugin.KavaEventType] = [KavaPlugin.KavaEventType.playR
let timerInterval: TimeInterval = 1
let maxViewIdleInterval: TimeInterval = 30

var duration: TimeInterval = 0

/// Kava event types
enum KavaEventType : Int {
/// Media was loaded
Expand Down Expand Up @@ -102,7 +104,7 @@ let playbackPoints: [KavaPlugin.KavaEventType] = [KavaPlugin.KavaEventType.playR
}

public override func onUpdateMedia(mediaConfig: MediaConfig) {
PKLog.debug("onUpdateMedia: \(String(describing: mediaConfig))")
PKLog.trace("onUpdateMedia: \(String(describing: mediaConfig))")
super.onUpdateMedia(mediaConfig: mediaConfig)
self.resetPlayerFlags()
self.unregisterFromBoundaries()
Expand All @@ -118,7 +120,7 @@ let playbackPoints: [KavaPlugin.KavaEventType] = [KavaPlugin.KavaEventType.playR
return
}

PKLog.debug("new config::\(String(describing: config))")
PKLog.trace("new config::\(String(describing: config))")
self.config = config
}

Expand All @@ -139,7 +141,7 @@ let playbackPoints: [KavaPlugin.KavaEventType] = [KavaPlugin.KavaEventType.playR

func registerToBoundaries() {
if let player = player, boundaryObservationToken == nil, !player.isLive() {
let boundaryFactory = PKBoundaryFactory(duration: player.duration)
let boundaryFactory = PKBoundaryFactory(duration: self.duration)
let boundaries = playbackPoints.map({ boundaryFactory.percentageTimeBoundary(boundary: convertToPercentage(type: $0)) })
boundaryObservationToken = player.addBoundaryObserver(boundaries: boundaries, observeOn: nil) { [weak self] (time, percentage) in
self?.sendPercentageReachedEvent(percentage: Int(percentage * 100))
Expand Down Expand Up @@ -225,7 +227,7 @@ let playbackPoints: [KavaPlugin.KavaEventType] = [KavaPlugin.KavaEventType.playR
return
}

PKLog.debug("Action: \(event)")
PKLog.trace("Action: \(event)")

// Send event to messageBus
let eventType = KavaEvent.Report(message: "Kava event: \(event) (\(event.rawValue))")
Expand All @@ -251,12 +253,12 @@ let playbackPoints: [KavaPlugin.KavaEventType] = [KavaPlugin.KavaEventType.playR
self.kavaData.playTimeInCurrentInterval = 0
}

self.kavaData.mediaDuration = player.duration
self.kavaData.mediaDuration = self.duration

// Handle media current time. For live, send position against real time in "-" (minus values)
let mediaCurrentTime: TimeInterval
if player.isLive() {
let currentTime = player.currentTime - player.duration
let currentTime = player.currentTime - self.duration
mediaCurrentTime = currentTime <= 0 ? currentTime : 0
} else {
mediaCurrentTime = player.currentTime
Expand All @@ -275,7 +277,7 @@ let playbackPoints: [KavaPlugin.KavaEventType] = [KavaPlugin.KavaEventType.playR

builder.add(headerKey: "User-Agent", headerValue: KavaPlugin.userAgent)
builder.set { (response: Response) in
PKLog.debug("Response: \(String(describing: response))")
PKLog.trace("Response: \(String(describing: response))")

guard let data = response.data else { return }

Expand All @@ -302,7 +304,7 @@ let playbackPoints: [KavaPlugin.KavaEventType] = [KavaPlugin.KavaEventType.playR
}
}

print("Send Kava Event: \(builder.urlParams!)")
PKLog.trace("Send Kava Event: \(builder.urlParams!)")
USRExecutor.shared.send(request: builder.build())

self.eventIndex += 1
Expand Down
4 changes: 3 additions & 1 deletion Sources/KavaPluginConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ import PlayKit
if let sentReferrer = referrer, self.isValidReferrer(sentReferrer) {
self.referrer = sentReferrer
} else {
PKLog.warning("Invalid referrer argument. Should start with app:// or http:// or https://")
if referrer != nil {
PKLog.warning("Invalid referrer argument. Should start with app:// or http:// or https://")
}
if let appId = applicationId {
self.referrer = "app://" + appId
} else {
Expand Down