Skip to content

Commit

Permalink
Merge 2f799f2 into 330385c
Browse files Browse the repository at this point in the history
  • Loading branch information
kontrollanten committed Sep 1, 2023
2 parents 330385c + 2f799f2 commit 5d5ce32
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ player.chromecast(); // initializes the Chromecast plugin
If nothing is returned or if this option is not defined, no custom data will be sent.
This option is intended to be used with a [custom receiver][custom-receiver] application
to extend its default capabilities.
* **`chromecast.modifyLoadRequestFn`** - a function that this plugin calls before doing
the request to [load media][chromecast-load-media]. The function gets called with the
[LoadRequest][chromecast-load-request] object as argument and expects it in return.

Here is an example configuration object that makes full use of all required and optional
configuration:
Expand Down Expand Up @@ -220,6 +223,11 @@ options = {
},
requestCustomDataFn: function(source) { // Not required
return customData[source.url];
},
modifyLoadRequestFn: function (loadRequest) { // HLS support
loadRequest.media.hlsSegmentFormat = 'fmp4';
loadRequest.media.hlsVideoSegmentFormat = 'fmp4';
return loadRequest;
}
},
plugins: {
Expand Down Expand Up @@ -342,3 +350,5 @@ details.
[player-source]: http://docs.videojs.com/Player.html#currentSource
[custom-receiver]: https://developers.google.com/cast/docs/custom_receiver
[contributing]: https://github.com/silvermine/silvermine-info#contributing
[chromecast-load-media]: https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastSession#loadMedia
[chromecast-load-request]: https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.LoadRequest
4 changes: 4 additions & 0 deletions src/js/tech/ChromecastTech.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ module.exports = function(videojs) {
this._requestTitle = options.requestTitleFn || function() { /* noop */ };
this._requestSubtitle = options.requestSubtitleFn || function() { /* noop */ };
this._requestCustomData = options.requestCustomDataFn || function() { /* noop */ };
this._modifyLoadRequestFn = options.modifyLoadRequestFn || function(request) {
return request;
};
// See `currentTime` function
this._initialStartTime = options.startTime || 0;

Expand Down Expand Up @@ -209,6 +212,7 @@ module.exports = function(videojs) {
request = new chrome.cast.media.LoadRequest(mediaInfo);
request.autoplay = true;
request.currentTime = startTime;
request = this._modifyLoadRequestFn(request);

this._isMediaLoading = true;
this._hasPlayedCurrentItem = false;
Expand Down
94 changes: 93 additions & 1 deletion tests/ChromcastTech.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
'use strict';

var expect = require('expect.js');
const expect = require('expect.js');

const sinon = require('sinon');

const chromecastTech = require('../src/js/tech/ChromecastTech');

class TechComponentStub {
constructor() {
this._ui = {
getPoster: () => {},
updatePoster: () => {},
updateSubtitle: () => {},
updateTitle: () => {},
};
this.trigger = () => {};
}
on() {}
ready() {}
}

describe('ChromecastTech', function() {
let originalCast,
originalChrome;

this.beforeEach(() => {
originalCast = global.cast;
global.cast = {
framework: {
RemotePlayerEventType: {},
},
};

originalChrome = global.chrome;
global.chrome = {
cast: {
media: {
GenericMediaMetadata: function() {},
LoadRequest: function() {},
MediaInfo: function() {},
MetadataType: {},
StreamType: {},
},
},
};
});

this.afterEach(() => {
global.cast = originalCast;
global.chrome = originalChrome;
});

it('should not call videojs.extend', function() {
const videoJsSpy = {
extend: function() {
Expand All @@ -23,4 +66,53 @@ describe('ChromecastTech', function() {

chromecastTech(videoJsSpy);
});

it('should call castSession.loadMedia with accurate req from chrome.cast.media.LoadRequest', function() {
let ChromecastTech;

const loadMediaSpy = sinon.stub().returns(Promise.resolve());

const videoJsSpy = () => {
return {
chromecastSessionManager: {
getCastContext: () => {
return {
getCurrentSession: () => {
return {
loadMedia: loadMediaSpy,
};
},
};
},
getRemotePlayer: () => {},
getRemotePlayerController: () => {
return {
addEventListener: () => {},
};
},
},
poster: () => {},
};
};

videoJsSpy.getComponent = () => {
return TechComponentStub;
};
videoJsSpy.registerTech = (_, component) => {
ChromecastTech = component;
};

const fakeRequest = {};

sinon.stub(global.chrome.cast.media, 'LoadRequest').returns(fakeRequest);

chromecastTech(videoJsSpy);

// eslint-disable-next-line no-new
new ChromecastTech({
source: 'source.url',
});

expect(loadMediaSpy.calledWith(fakeRequest)).to.be(true);
});
});

0 comments on commit 5d5ce32

Please sign in to comment.