Skip to content

Commit

Permalink
Merge branch 'release/0.1.x' into release/widevine/0.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliza Sapir committed Mar 21, 2017
2 parents 4856040 + 2113080 commit eb0e564
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 144 deletions.
File renamed without changes.
13 changes: 11 additions & 2 deletions Classes/Player/AVPlayerEngine/AVPlayerEngine+Observation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ extension AVPlayerEngine {
#keyPath(currentItem),
#keyPath(currentItem.playbackLikelyToKeepUp),
#keyPath(currentItem.playbackBufferEmpty),
#keyPath(currentItem.duration)
#keyPath(currentItem.duration),
#keyPath(currentItem.timedMetadata)
]
}

Expand Down Expand Up @@ -128,6 +129,8 @@ extension AVPlayerEngine {
self.handleStatusChange()
case #keyPath(currentItem):
self.handleItemChange()
case #keyPath(currentItem.timedMetadata):
self.handleTimedMedia()
default:
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
Expand Down Expand Up @@ -194,6 +197,12 @@ extension AVPlayerEngine {
self.postStateChange(newState: newState, oldState: self.currentState)
self.currentState = newState
// in case item changed reset player reached end time indicator
isPlayedToEndTime = false
self.isPlayedToEndTime = false
}

private func handleTimedMedia() {
guard let currentItem = self.currentItem else { return }
guard let metadata = currentItem.timedMetadata else { return }
self.post(event: PlayerEvent.TimedMetadata(metadata: metadata))
}
}
7 changes: 7 additions & 0 deletions Classes/Player/PKEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import AVFoundation

/// PKEvent
@objc public class PKEvent: NSObject {
Expand All @@ -28,6 +29,7 @@ extension PKEvent {
static let OldState = "oldState"
static let NewState = "newState"
static let Error = "error"
static let Metadata = "metadata"
}

// MARK: Player Data Accessors
Expand Down Expand Up @@ -69,4 +71,9 @@ extension PKEvent {
@objc public var error: NSError? {
return self.data?[EventDataKeys.Error] as? NSError
}

/// Associated metadata from the event, PKEvent Data Accessor
@objc public var timedMetadata: [AVMetadataItem]? {
return self.data?[EventDataKeys.Metadata] as? [AVMetadataItem]
}
}
11 changes: 10 additions & 1 deletion Classes/PlayerEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//
import Foundation
import AVFoundation

/// An PlayerEvent is a class used to reflect player events.
@objc public class PlayerEvent: PKEvent {
Expand Down Expand Up @@ -43,6 +44,8 @@ import Foundation
@objc public static let playbackParamsUpdated: PlayerEvent.Type = PlaybackParamsUpdated.self
/// Sent when player state is changed.
@objc public static let stateChanged: PlayerEvent.Type = StateChanged.self
/// Sent when timed metadata is available.
@objc public static let timedMetadata: PlayerEvent.Type = TimedMetadata.self

/// Sent when an error occurs.
@objc public static let error: PlayerEvent.Type = Error.self
Expand Down Expand Up @@ -86,6 +89,12 @@ import Foundation
}
}

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

// MARK: - Player Tracks Events

class TracksAvailable: PlayerEvent {
Expand All @@ -105,7 +114,7 @@ import Foundation
class StateChanged: PlayerEvent {
convenience init(newState: PlayerState, oldState: PlayerState) {
self.init([EventDataKeys.NewState : newState as AnyObject,
EventDataKeys.OldState : oldState as AnyObject])
EventDataKeys.OldState : oldState as AnyObject])
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Classes/Plugins/Ads/PKAdCuePoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import Foundation
self.cuePoints = cuePoints.sorted() // makes sure array is sorted
}

@objc var count: Int {
@objc public var count: Int {
return self.cuePoints.count
}

@objc var hasPreRoll: Bool {
@objc public var hasPreRoll: Bool {
return self.cuePoints.filter { $0 == 0 }.count > 0 // pre-roll ads values = 0
}

@objc var hasMidRoll: Bool {
@objc public var hasMidRoll: Bool {
return self.cuePoints.filter { $0 > 0 }.count > 0
}

@objc var hasPostRoll: Bool {
@objc public var hasPostRoll: Bool {
return self.cuePoints.filter { $0 < 0 }.count > 0 // post-roll ads values = -1
}
}
10 changes: 0 additions & 10 deletions Example/PlayKit.xcworkspace/contents.xcworkspacedata

This file was deleted.

2 changes: 1 addition & 1 deletion Example/PlayKit/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.1.25</string>
<string>0.1.26</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion PlayKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PlayKit'
s.version = '0.1.25'
s.version = '0.1.26'
s.summary = 'PlayKit: Kaltura Mobile Player SDK - iOS'


Expand Down
60 changes: 34 additions & 26 deletions Plugins/KalturaLiveStats/KalturaLiveStatsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,32 @@
//
//

/// `KalturaStatsEvent` represents an event reporting from kaltura stats plugin.
@objc public class KalturaLiveStatsEvent: PKEvent {

static let bufferTimeKey = "bufferTime"

class Report: KalturaLiveStatsEvent {
convenience init(bufferTime: Int32) {
self.init([Report.bufferTimeKey: NSNumber(value: bufferTime)])
}
}

@objc public static let report: KalturaLiveStatsEvent.Type = Report.self
}

extension PKEvent {
/// bufferTime Value, PKEvent Data Accessor
@objc public var kalturaLiveStatsBufferTime: NSNumber? {
return self.data?[KalturaLiveStatsEvent.bufferTimeKey] as? NSNumber
}
}

public class KalturaLiveStatsPlugin: BaseAnalyticsPlugin {

enum KLiveStatsEventType : Int {
case LIVE = 1
case DVR = 2
case live = 1
case dvr = 2
}

public override class var pluginName: String {
Expand Down Expand Up @@ -91,10 +112,10 @@ public class KalturaLiveStatsPlugin: BaseAnalyticsPlugin {
if type(of: event) == PlayerEvent.stateChanged {
switch event.newState {
case .ready:
strongSelf.startTimer()
strongSelf.createTimer()
if strongSelf.isBuffering {
strongSelf.isBuffering = false
strongSelf.sendLiveEvent(theBufferTime: strongSelf.calculateBuffer(isBuffering: false))
strongSelf.sendLiveEvent(withBufferTime: strongSelf.calculateBuffer(isBuffering: false))
}
case .buffering:
strongSelf.isBuffering = true
Expand All @@ -114,17 +135,18 @@ public class KalturaLiveStatsPlugin: BaseAnalyticsPlugin {

private func startLiveEvents() {
if !self.isLive {
startTimer()
self.createTimer()
isLive = true
if isFirstPlay {
sendLiveEvent(theBufferTime: bufferTime);
sendLiveEvent(withBufferTime: bufferTime);
isFirstPlay = false
}
}
}

private func stopLiveEvents(){
self.isLive = false
self.timer?.invalidate()
}

private func createTimer() {
Expand All @@ -141,24 +163,7 @@ public class KalturaLiveStatsPlugin: BaseAnalyticsPlugin {
}

@objc private func timerHit() {
self.sendLiveEvent(theBufferTime: bufferTime);
}

private func startTimer() {
if let t = self.timer {
if t.isValid {
t.fire()
} else {
self.timer = Timer.scheduledTimer(timeInterval: TimeInterval(self.interval), target: self, selector: #selector(KalturaLiveStatsPlugin.timerHit), userInfo: nil, repeats: true)
self.timer!.fire()
}
}
}

private func pauseTimer() {
if let t = self.timer {
t.invalidate()
}
self.sendLiveEvent(withBufferTime: bufferTime);
}

private func calculateBuffer(isBuffering: Bool) -> Int32 {
Expand All @@ -176,8 +181,11 @@ public class KalturaLiveStatsPlugin: BaseAnalyticsPlugin {
return bufferTime
}

private func sendLiveEvent(theBufferTime: Int32) {
private func sendLiveEvent(withBufferTime bufferTime: Int32) {
PKLog.debug("sendLiveEvent - Buffer Time: \(bufferTime)")
// post event to message bus
let event = KalturaLiveStatsEvent.Report(bufferTime: bufferTime)
self.messageBus?.post(event)

guard let mediaEntry = self.player?.mediaEntry else { return }

Expand All @@ -201,7 +209,7 @@ public class KalturaLiveStatsPlugin: BaseAnalyticsPlugin {
partnerId: parterId,
eventType: self.isLive ? 1 : 0,
eventIndex: self.eventIdx,
bufferTime: theBufferTime,
bufferTime: bufferTime,
bitrate: self.lastReportedBitrate,
sessionId: sessionId,
startTime: self.lastReportedStartTime,
Expand Down
Loading

0 comments on commit eb0e564

Please sign in to comment.