Skip to content

Commit

Permalink
Add API to get live stream position as Date.
Browse files Browse the repository at this point in the history
Closes #356

Change-Id: I5bea5906c4528313da96fd6577005b385b445a65
  • Loading branch information
ismena committed Oct 6, 2016
1 parent addece6 commit 91da191
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/media/presentation_timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() +
Expand Down
17 changes: 17 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
22 changes: 21 additions & 1 deletion test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('Player', function() {
};
}

video = createMockVideo();
video = createMockVideo(20);
player = new shaka.Player(video, dependencyInjector);

abrManager = new shaka.test.FakeAbrManager();
Expand Down Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions test/test/util/manifest_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 3 additions & 1 deletion test/test/util/simple_fakes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 91da191

Please sign in to comment.