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 16, 2017
2 parents ff5f2fa + 6d50b18 commit 4856040
Show file tree
Hide file tree
Showing 20 changed files with 260 additions and 183 deletions.
2 changes: 1 addition & 1 deletion Classes/Network/KalturaMultiRequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class KalturaMultiRequestBuilder: KalturaRequestBuilder {
override public func build() -> Request {

let data = self.kalturaMultiRequestData()
let request = RequestElement(requestId: self.requestId, method: self.method, url: self.url, dataBody: data, headers: self.headers, timeout: self.timeout, configuration: self.configuration, completion: self.completion)
let request = RequestElement(requestId: self.requestId, method: self.method, url: self.url, dataBody: data, headers: self.headers, timeout: self.timeout, configuration: self.configuration, responseSerializer: self.responseSerializer, completion: self.completion)

return request
}
Expand Down
12 changes: 10 additions & 2 deletions Classes/Network/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public protocol Request {
var timeout: Double { get }
var configuration: RequestConfiguration? { get }
var completion: completionClosures? { get }
var responseSerializer: ResponseSerializer { get }
}

public struct RequestElement: Request {
Expand All @@ -45,6 +46,7 @@ public struct RequestElement: Request {
public var headers: [String:String]?
public var timeout: Double
public var configuration: RequestConfiguration?
public var responseSerializer: ResponseSerializer
public var completion: completionClosures?
}

Expand All @@ -62,7 +64,7 @@ public struct RequestElement: Request {
public var configuration: RequestConfiguration? = nil
public var completion: completionClosures? = nil
public var urlParams: [String: String]? = nil

public var responseSerializer : ResponseSerializer = JSONSerializer()
public init?(url: String){
if let path = URL(string: url) {
self.url = path
Expand Down Expand Up @@ -101,6 +103,12 @@ public struct RequestElement: Request {
return self
}

@discardableResult
public func set(responseSerializer: ResponseSerializer) -> Self{
self.responseSerializer = responseSerializer
return self
}

@discardableResult
public func set(completion:completionClosures?) -> Self{
self.completion = completion
Expand Down Expand Up @@ -170,7 +178,7 @@ public struct RequestElement: Request {

}

return RequestElement(requestId: self.requestId, method:self.method , url: self.url, dataBody: bodyData, headers: self.headers, timeout: self.timeout, configuration: self.configuration, completion: self.completion)
return RequestElement(requestId: self.requestId, method:self.method , url: self.url, dataBody: bodyData, headers: self.headers, timeout: self.timeout, configuration: self.configuration,responseSerializer: self.responseSerializer, completion: self.completion)
}
}

Expand Down
51 changes: 51 additions & 0 deletions Classes/Network/ResponseSerializer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// ResponseSerializer.swift
// Pods
//
// Created by Rivka Peleg on 14/03/2017.
//
//

import Foundation

enum SerializerError: Error {
case serializationError
}


public protocol ResponseSerializer {
/**
This fuction will serialize the response data of certin request to the expected type according to the serializer type
*/
func serialize(data: Data) throws -> Any
}


class JSONSerializer: ResponseSerializer {

func serialize(data: Data) throws -> Any {
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
return json
}
}


class IntSerializer: ResponseSerializer {

func serialize(data: Data) throws -> Any {
guard let int8 = [UInt8](data).last else {
throw SerializerError.serializationError
}

let int: Int = Int(int8)
return int
}
}

class StringSerializer: ResponseSerializer {

func serialize(data: Data) throws -> Any {
let string = String(data: data, encoding: .utf8)
return string
}
}
2 changes: 1 addition & 1 deletion Classes/Network/USRExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import UIKit

if let d = data {
do {
let json = try JSONSerialization.jsonObject(with: d, options: JSONSerialization.ReadingOptions())
let json = try r.responseSerializer.serialize(data: d)
let result = Response(data: json, error:nil)
completion(result)
} catch {
Expand Down
2 changes: 1 addition & 1 deletion Classes/Player/Player.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import AVKit

@objc public protocol Player: NSObjectProtocol {

@objc var delegate: PlayerDelegate? { get set }
@objc weak var delegate: PlayerDelegate? { get set }

/// The player's associated media entry.
weak var mediaEntry: MediaEntry? { get }
Expand Down
2 changes: 1 addition & 1 deletion Classes/Player/PlayerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PlayerController: NSObject, Player {

var onEventBlock: ((PKEvent)->Void)?

var delegate: PlayerDelegate?
weak var delegate: PlayerDelegate?

fileprivate var currentPlayer: AVPlayerEngine
fileprivate var assetBuilder: AssetBuilder?
Expand Down
4 changes: 2 additions & 2 deletions Classes/Plugins/BasePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import Foundation
fatalError("abstract property should be overriden in subclass")
}

@objc public unowned var player: Player
@objc public unowned var messageBus: MessageBus
@objc public weak var player: Player?
@objc public weak var messageBus: MessageBus?

@objc public required init(player: Player, pluginConfig: Any?, messageBus: MessageBus) throws {
PKLog.info("initializing plugin \(type(of:self))")
Expand Down
5 changes: 3 additions & 2 deletions Classes/Plugins/PKPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public protocol PKPlugin {
static var pluginName: String { get }

/// The player associated with the plugin
unowned var player: Player { get }
unowned var messageBus: MessageBus { get }
weak var player: Player? { get }
/// The messageBus associated with the plugin
weak var messageBus: MessageBus? { get }

init(player: Player, pluginConfig: Any?, messageBus: MessageBus) throws

Expand Down
6 changes: 3 additions & 3 deletions Classes/WidevineClassicHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ typealias LocalAssetStatusBlock = (Error?, TimeInterval, TimeInterval) -> Void
// MARK: - WidevineClassicError
/************************************************************/

enum WidevineClassicError: PKError {
enum WidevineClassicError: PKError {

case invalidDRMData
case missingWidevineFile

static let Domain = "com.kaltura.playkit.error.drm.widevine"
static let domain = "com.kaltura.playkit.error.drm.widevine"

var code: Int {
switch self {
Expand All @@ -48,7 +48,7 @@ typealias LocalAssetStatusBlock = (Error?, TimeInterval, TimeInterval) -> Void
}

extension PKErrorDomain {
@objc public static let Widevine = WidevineClassicError.Domain
@objc public static let Widevine = WidevineClassicError.domain
}

extension PKErrorCode {
Expand Down
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.23</string>
<string>0.1.25</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.23'
s.version = '0.1.25'
s.summary = 'PlayKit: Kaltura Mobile Player SDK - iOS'


Expand Down
2 changes: 0 additions & 2 deletions Plugins/AnalyticsCommon/AnalyticsPluginProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import Foundation

protocol AnalyticsPluginProtocol: PKPlugin {

unowned var player: Player { get set }
unowned var messageBus: MessageBus { get set }
var config: AnalyticsConfig? { get set }
var isFirstPlay: Bool { get set }
var playerEventsToRegister: [PlayerEvent.Type] { get }
Expand Down
2 changes: 1 addition & 1 deletion Plugins/AnalyticsCommon/BaseAnalyticsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ extension PKErrorCode {
}

public override func destroy() {
self.messageBus?.removeObserver(self, events: playerEventsToRegister)
super.destroy()
self.messageBus.removeObserver(self, events: playerEventsToRegister)
}

/************************************************************/
Expand Down
62 changes: 28 additions & 34 deletions Plugins/IMA/IMAPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ extension IMAAdsManager {
weak var dataSource: AdsPluginDataSource? {
didSet {
PKLog.debug("data source set")
self.setupMainView()
}
}
weak var delegate: AdsPluginDelegate?
weak var pipDelegate: AVPictureInPictureControllerDelegate?

private var contentPlayhead: IMAAVPlayerContentPlayhead?
private var adsManager: IMAAdsManager?
private var companionSlot: IMACompanionAdSlot?
private var renderingSettings: IMAAdsRenderingSettings! = IMAAdsRenderingSettings()
private static var loader: IMAAdsLoader!

Expand Down Expand Up @@ -94,9 +92,9 @@ extension IMAAdsManager {
throw PKPluginError.missingPluginConfig(pluginName: IMAPlugin.pluginName)
}

self.messageBus.addObserver(self, events: [PlayerEvent.ended], block: { (data: Any) -> Void in
self.contentComplete()
})
self.messageBus?.addObserver(self, events: [PlayerEvent.ended]) { [weak self] event in
self?.contentComplete()
}

self.timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(IMAPlugin.update), userInfo: nil, repeats: true)
}
Expand All @@ -120,11 +118,23 @@ extension IMAAdsManager {
/************************************************************/

func requestAds() {
guard let playerView = player?.view else { return }

if self.adTagUrl != nil && self.adTagUrl != "" {
self.startAdCalled = false

// setup ad display container and companion if exists, needs to create a new ad container for each request.
var companionAdSlot: IMACompanionAdSlot? = nil
let adDisplayContainer: IMAAdDisplayContainer
if let companionView = self.config?.companionView {
companionAdSlot = IMACompanionAdSlot(view: companionView, width: Int32(companionView.frame.size.width), height: Int32(companionView.frame.size.height))
adDisplayContainer = IMAAdDisplayContainer(adContainer: playerView, companionSlots: [companionAdSlot])
} else {
adDisplayContainer = IMAAdDisplayContainer(adContainer: playerView, companionSlots: [])
}

var request: IMAAdsRequest
request = IMAAdsRequest(adTagUrl: self.adTagUrl, adDisplayContainer: self.createAdDisplayContainer(), contentPlayhead: self, userContext: nil)
request = IMAAdsRequest(adTagUrl: self.adTagUrl, adDisplayContainer: adDisplayContainer, contentPlayhead: self, userContext: nil)

IMAPlugin.loader.requestAds(with: request)
PKLog.trace("request Ads")
Expand Down Expand Up @@ -176,16 +186,6 @@ extension IMAAdsManager {
imaSettings.autoPlayAdBreaks = config.autoPlayAdBreaks
IMAPlugin.loader = IMAAdsLoader(settings: imaSettings)
}

private func setupMainView() {
// if let _ = self.player.playerEngine {
// self.pictureInPictureProxy = IMAPictureInPictureProxy(avPictureInPictureControllerDelegate: self)
// }

if let companionView = self.config?.companionView {
self.companionSlot = IMACompanionAdSlot(view: companionView, width: Int32(companionView.frame.size.width), height: Int32(companionView.frame.size.height))
}
}

private func setupLoadingView() {
self.loadingView = UIView(frame: CGRect.zero)
Expand All @@ -202,16 +202,13 @@ extension IMAAdsManager {
self.loadingView!.addConstraint(NSLayoutConstraint(item: self.loadingView!, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: indicator, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
self.loadingView!.addConstraint(NSLayoutConstraint(item: self.loadingView!, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: indicator, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))

let videoView = self.player.view
videoView?.addSubview(self.loadingView!)
videoView?.addConstraint(NSLayoutConstraint(item: videoView!, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.loadingView!, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
videoView?.addConstraint(NSLayoutConstraint(item: videoView!, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self.loadingView!, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0))
videoView?.addConstraint(NSLayoutConstraint(item: videoView!, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.loadingView!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
videoView?.addConstraint(NSLayoutConstraint(item: videoView!, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: self.loadingView!, attribute: NSLayoutAttribute.right, multiplier: 1, constant: 0))
}

private func createAdDisplayContainer() -> IMAAdDisplayContainer {
return IMAAdDisplayContainer(adContainer: self.player.view, companionSlots: self.config?.companionView != nil ? [self.companionSlot!] : nil)
if let videoView = self.player?.view {
videoView.addSubview(self.loadingView!)
videoView.addConstraint(NSLayoutConstraint(item: videoView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.loadingView!, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
videoView.addConstraint(NSLayoutConstraint(item: videoView, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self.loadingView!, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0))
videoView.addConstraint(NSLayoutConstraint(item: videoView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.loadingView!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
videoView.addConstraint(NSLayoutConstraint(item: videoView, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: self.loadingView!, attribute: NSLayoutAttribute.right, multiplier: 1, constant: 0))
}
}

private func loadAdsIfNeeded() {
Expand Down Expand Up @@ -251,10 +248,7 @@ extension IMAAdsManager {

@objc private func update() {
if !self.isAdPlayback {
let currentTime = self.player.currentTime
if currentTime.isNaN {
return
}
guard let currentTime = self.player?.currentTime, !currentTime.isNaN else { return }
self.currentPlaybackTime = currentTime
self.loadAdsIfNeeded()
}
Expand All @@ -281,12 +275,12 @@ extension IMAAdsManager {
self.loadingView!.alpha = alpha
self.loadingView!.isHidden = !show

self.player.view?.bringSubview(toFront: self.loadingView!)
self.player?.view?.bringSubview(toFront: self.loadingView!)
}

private func notify(event: AdEvent) {
self.delegate?.adsPlugin(self, didReceive: event)
self.messageBus.post(event)
self.messageBus?.post(event)
}

private func notifyAdCuePoints(fromAdsManager adsManager: IMAAdsManager) {
Expand Down Expand Up @@ -324,7 +318,7 @@ extension IMAAdsManager {
self.loaderFailed = true
self.showLoadingView(false, alpha: 0)
PKLog.error(adErrorData.adError.message)
self.messageBus.post(AdEvent.Error(nsError: IMAPluginError(adError: adErrorData.adError).asNSError))
self.messageBus?.post(AdEvent.Error(nsError: IMAPluginError(adError: adErrorData.adError).asNSError))
self.delegate?.adsPlugin(self, loaderFailedWith: adErrorData.adError.message)
}

Expand Down Expand Up @@ -392,7 +386,7 @@ extension IMAAdsManager {
public func adsManager(_ adsManager: IMAAdsManager!, didReceive error: IMAAdError!) {
self.showLoadingView(false, alpha: 0)
PKLog.error(error.message)
self.messageBus.post(AdEvent.Error(nsError: IMAPluginError(adError: error).asNSError))
self.messageBus?.post(AdEvent.Error(nsError: IMAPluginError(adError: error).asNSError))
self.delegate?.adsPlugin(self, managerFailedWith: error.message)
}

Expand Down
Loading

0 comments on commit 4856040

Please sign in to comment.