From 91da191a02abf42ba0e23f6065b118c762f20b91 Mon Sep 17 00:00:00 2001 From: Sandra Lokshina Date: Wed, 5 Oct 2016 16:48:49 -0700 Subject: [PATCH] Add API to get live stream position as Date. Closes #356 Change-Id: I5bea5906c4528313da96fd6577005b385b445a65 --- lib/media/presentation_timeline.js | 10 ++++++++++ lib/player.js | 17 +++++++++++++++++ test/player_unit.js | 22 +++++++++++++++++++++- test/test/util/manifest_generator.js | 12 ++++++++++++ test/test/util/simple_fakes.js | 4 +++- 5 files changed, 63 insertions(+), 2 deletions(-) diff --git a/lib/media/presentation_timeline.js b/lib/media/presentation_timeline.js index 1a87b0e00a..cc3cae9c57 100644 --- a/lib/media/presentation_timeline.js +++ b/lib/media/presentation_timeline.js @@ -88,6 +88,16 @@ shaka.media.PresentationTimeline.prototype.setDuration = function(duration) { }; +/** + * @return {?number} The presentation's start time in seconds. + * @export + */ +shaka.media.PresentationTimeline.prototype.getPresentationStartTime = + function() { + return this.presentationStartTime_; +}; + + /** * Sets the clock offset, which is the the difference between the client's clock * and the server's clock, in milliseconds (i.e., serverTime = Date.now() + diff --git a/lib/player.js b/lib/player.js index b0565f095d..6c7df4a8d9 100644 --- a/lib/player.js +++ b/lib/player.js @@ -924,6 +924,23 @@ shaka.Player.prototype.setTextTrackVisibility = function(on) { }; +/** + * Returns current playhead time as a Date. + * + * @return {Date} + * @export + */ +shaka.Player.prototype.getPlayheadTimeAsDate = function() { + goog.asserts.assert(this.isLive(), + 'getPlayheadTimeInUTC should be called on a live stream!'); + var time = + this.manifest_.presentationTimeline.getPresentationStartTime() * 1000 + + this.video_.currentTime * 1000; + + return new Date(time); +}; + + /** * Return playback and adaptation stats. * diff --git a/test/player_unit.js b/test/player_unit.js index 0123bc9ca9..86990218b3 100644 --- a/test/player_unit.js +++ b/test/player_unit.js @@ -79,7 +79,7 @@ describe('Player', function() { }; } - video = createMockVideo(); + video = createMockVideo(20); player = new shaka.Player(video, dependencyInjector); abrManager = new shaka.test.FakeAbrManager(); @@ -1318,6 +1318,26 @@ describe('Player', function() { } }); + describe('getPlayheadTimeAsDate()', function() { + beforeEach(function(done) { + var timeline = new shaka.media.PresentationTimeline(300, 0); + timeline.setStatic(false); + manifest = new shaka.test.ManifestGenerator() + .setTimeline(timeline) + .addPeriod(0) + .build(); + goog.asserts.assert(manifest, 'manifest must be non-null'); + var parser = new shaka.test.FakeManifestParser(manifest); + var factory = function() { return parser; }; + player.load('', 0, factory).catch(fail).then(done); + }); + + it('gets current wall clock time in UTC', function() { + var liveTimeUtc = player.getPlayheadTimeAsDate(); + expect(liveTimeUtc).toEqual(new Date(320000)); + }); + }); + /** * Choose streams for the given period. * diff --git a/test/test/util/manifest_generator.js b/test/test/util/manifest_generator.js index 0a7a05d5d9..af044e97e9 100644 --- a/test/test/util/manifest_generator.js +++ b/test/test/util/manifest_generator.js @@ -56,6 +56,18 @@ shaka.test.ManifestGenerator.prototype.build = function() { }; +/** + * Sets a specified presentation timeline. + * + * @param {!shaka.media.PresentationTimeline} timeline + * @return {!shaka.test.ManifestGenerator} + */ +shaka.test.ManifestGenerator.prototype.setTimeline = function(timeline) { + this.manifest_.presentationTimeline = timeline; + return this; +}; + + /** * Sets the duration of the presentation timeline. * diff --git a/test/test/util/simple_fakes.js b/test/test/util/simple_fakes.js index c9ad1dd537..7ca61b0b84 100644 --- a/test/test/util/simple_fakes.js +++ b/test/test/util/simple_fakes.js @@ -213,13 +213,15 @@ shaka.test.FakeManifestParser.prototype.configure = function() {}; /** * Creates a fake video element. + * @param {number=} opt_currentTime * @return {!HTMLVideoElement} * @suppress {invalidCasts} */ -function createMockVideo() { +function createMockVideo(opt_currentTime) { var video = { src: '', textTracks: [], + currentTime: opt_currentTime || 0, addTextTrack: jasmine.createSpy('addTextTrack'), addEventListener: jasmine.createSpy('addEventListener'), removeEventListener: jasmine.createSpy('removeEventListener'),