From c2885affcab92cff2a6ee050a4b7df33524fe2f2 Mon Sep 17 00:00:00 2001 From: Noam Tamim Date: Wed, 10 Oct 2018 21:16:10 +0300 Subject: [PATCH 1/2] Instead of calling player.duration listen to event --- Sources/KavaPlugin+AnalyticsPluginProtocol.swift | 3 +++ Sources/KavaPlugin.swift | 8 +++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Sources/KavaPlugin+AnalyticsPluginProtocol.swift b/Sources/KavaPlugin+AnalyticsPluginProtocol.swift index 6409365..225b2e1 100644 --- a/Sources/KavaPlugin+AnalyticsPluginProtocol.swift +++ b/Sources/KavaPlugin+AnalyticsPluginProtocol.swift @@ -30,6 +30,7 @@ extension KavaPlugin: AnalyticsPluginProtocol { return [ PlayerEvent.stateChanged, PlayerEvent.loadedMetadata, + PlayerEvent.durationChanged, PlayerEvent.play, PlayerEvent.pause, PlayerEvent.playing, @@ -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: diff --git a/Sources/KavaPlugin.swift b/Sources/KavaPlugin.swift index 2297474..83c0fc3 100644 --- a/Sources/KavaPlugin.swift +++ b/Sources/KavaPlugin.swift @@ -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 @@ -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)) @@ -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 From a9a2561b4b369db25a952f63aa30636982540805 Mon Sep 17 00:00:00 2001 From: Noam Tamim Date: Thu, 11 Oct 2018 22:41:48 +0300 Subject: [PATCH 2/2] Reduce log noise (.debug -> .trace) --- Sources/KavaHelper.swift | 2 +- .../KavaPlugin+AnalyticsPluginProtocol.swift | 26 +++++++++---------- Sources/KavaPlugin.swift | 10 +++---- Sources/KavaPluginConfig.swift | 4 ++- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Sources/KavaHelper.swift b/Sources/KavaHelper.swift index f298c4e..73c356a 100644 --- a/Sources/KavaHelper.swift +++ b/Sources/KavaHelper.swift @@ -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)") } } diff --git a/Sources/KavaPlugin+AnalyticsPluginProtocol.swift b/Sources/KavaPlugin+AnalyticsPluginProtocol.swift index 225b2e1..11f3613 100644 --- a/Sources/KavaPlugin+AnalyticsPluginProtocol.swift +++ b/Sources/KavaPlugin+AnalyticsPluginProtocol.swift @@ -96,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 @@ -116,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 @@ -135,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 @@ -156,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) @@ -166,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 @@ -175,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 @@ -190,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) { @@ -201,7 +201,7 @@ extension KavaPlugin: AnalyticsPluginProtocol { } private func handleTextTrackChanged(_ textTrack: Track?) { - PKLog.debug("textTrackChanged event") + PKLog.trace("textTrackChanged event") let textOffDisplay: String = "Off" @@ -219,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 diff --git a/Sources/KavaPlugin.swift b/Sources/KavaPlugin.swift index 83c0fc3..03b284d 100644 --- a/Sources/KavaPlugin.swift +++ b/Sources/KavaPlugin.swift @@ -104,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() @@ -120,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 } @@ -227,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))") @@ -277,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 } @@ -304,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 diff --git a/Sources/KavaPluginConfig.swift b/Sources/KavaPluginConfig.swift index f53d41a..b601194 100644 --- a/Sources/KavaPluginConfig.swift +++ b/Sources/KavaPluginConfig.swift @@ -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 {