Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #358 from clappr/feature/current_live_date
Browse files Browse the repository at this point in the history
Add the current live date property to playback
  • Loading branch information
caiobzen authored Nov 12, 2019
2 parents b810c90 + 554e968 commit 4c9f2fd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/Clappr/Classes/Base/Playback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ extension Playback {
@objc open var currentDate: Date? {
return nil
}

@objc open var currentLiveDate: Date? {
return nil
}

@objc open var seekableTimeRanges: [NSValue] {
return []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ extension AVFoundationPlayback {
open override var currentDate: Date? {
return player?.currentItem?.currentDate()
}

open override var currentLiveDate: Date? {
guard let currentDate = currentDate, playbackType == .live else { return nil }
let liveDate = currentDate.timeIntervalSince1970 + (duration - TimeInterval(position))

return Date(timeIntervalSince1970: liveDate)
}

open override var seekableTimeRanges: [NSValue] {
guard let ranges = player?.currentItem?.seekableTimeRanges else { return [] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,46 @@ class AVFoundationPlaybackTests: QuickSpec {
expect(playback.currentDate).to(equal(date))
}
}

describe("#currentLiveDate") {
context("when a video is not live") {
it("returns nil") {
let playback = StubbedLiveDatePlayback(options: [:])
playback.player = player
playback._playbackType = .vod

expect(playback.currentLiveDate).to(beNil())
}
}

context("when there's a currentDate") {
it("returns the currentLiveDate of the video") {
let playback = StubbedLiveDatePlayback(options: [:])
playback.player = player
let currentDate = Date()

item.set(currentDate: currentDate)
playback._position = 0
playback._duration = 0

expect(playback.currentLiveDate?.timeIntervalSince1970).to(equal(currentDate.timeIntervalSince1970))
}
}

context("when the video is not at the live position") {
it("returns the currentLiveDate of the video") {
let playback = StubbedLiveDatePlayback(options: [:])
playback.player = player
let currentDate = Date()

item.set(currentDate: currentDate)
playback._position = 30
playback._duration = 1000

expect(currentDate.timeIntervalSince1970).to(beLessThan(playback.currentLiveDate?.timeIntervalSince1970))
}
}
}
}

describe("#duration") {
Expand Down Expand Up @@ -1677,3 +1717,14 @@ private class MockAccessLogEvent: AVPlayerItemAccessLogEvent {
return 5
}
}

private class StubbedLiveDatePlayback: AVFoundationPlayback {
var _position: Double = .zero
override var position: Double { _position }

var _duration: Double = .zero
override var duration: Double { _duration }

var _playbackType: PlaybackType = .live
override var playbackType: PlaybackType { _playbackType }
}

0 comments on commit 4c9f2fd

Please sign in to comment.