Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kaltura player): export setup method #2

Merged
merged 7 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,191 changes: 2,084 additions & 1,107 deletions dist/kaltura-player.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kaltura-player.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/kaltura-player.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kaltura-player.min.js.map

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
<div id="root"></div>
<script>
window.setupPlayer = function () {
let ovpProvider = new KalturaPlayer.OvpProvider(2196781);
ovpProvider.getConfig("1_afvj3z0u").then((data) => {
window.player = KalturaPlayer.Playkit.playkit(data);
window.uiManager = new KalturaPlayer.PlaykitUI(player, data);
let config = {
partnerId: 2196781, entryID: '1_h14v9eug'
};
KalturaPlayer.setup(config).then(player => {
window.player = player;
window.uiManager = new KalturaPlayer.PlaykitUI(player, player.config);
uiManager.buildDefaultUI();
player.play()
}).catch(error => {
console.error(error);
});
};
</script>
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import '../node_modules/playkit-js-ui/src/styles/style.css';
import 'playkit-js-hls';
import 'playkit-js-dash';

import setup from './setup'
// TODO: Import plugins

export {Playkit, OvpProvider, PlaykitUI};
export {Playkit, OvpProvider, PlaykitUI, setup};

63 changes: 63 additions & 0 deletions src/session-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//@flow
/**
* @return {string} - GUID
* @private
*/
function _generateGUID(): string {
let S4 = () => {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

/**
* @param {Object} selectedSource - selected source
* @param {Player} player - player
* @return {void}
* @private
*/
function _addSessionId(selectedSource: Object, player: Player): void {
let delimiter = selectedSource.url.indexOf('?') === -1 ? '?' : '&';
let primaryGUID = _generateGUID();
let secondGUID = _generateGUID();
let sessionId = primaryGUID + ':' + secondGUID;
selectedSource.url += delimiter + 'playSessionId=' + sessionId;
player.sessionId = sessionId;
}

/**
* @param {Object} selectedSource - selected source
* @param {string} sessionId - session id
* @param {Player} player - player
* @return {void}
* @private
*/
function _replaceSecondGUID(selectedSource: Object, sessionId: string, player: Player): void {
let secondGUIDRegex = /:((?:[a-z0-9]|-)*)/i;
let secondGUID = secondGUIDRegex.exec(sessionId);
if (secondGUID && secondGUID[1]) {
let newSessionId = sessionId.replace(secondGUID[1], _generateGUID());
selectedSource.url = selectedSource.url.replace(sessionId, newSessionId);
player.sessionId = newSessionId;
}
}

/**
* @param {Object} selectedSource - selected source
* @param {Player} player - player
* @return {void}
* @public
*/
function handleSessionId(selectedSource: Object = {}, player: Player): void {
if (typeof selectedSource.url === 'string' && selectedSource.url.toLowerCase().indexOf('playmanifest/') !== -1) {
let sessionIdRegex = /playSessionId=((?:[a-z0-9]|-|:)*)/i;
let sessionId = sessionIdRegex.exec(selectedSource.url);
if (sessionId && sessionId[1]) {
_replaceSecondGUID(selectedSource, sessionId[1], player);
} else {
_addSessionId(selectedSource, player);
}
}
}

export default handleSessionId
28 changes: 28 additions & 0 deletions src/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//@flow
import * as Playkit from 'playkit-js';
import OvpProvider from 'playkit-js-providers/dist/ovpProvider';
import handleSessionId from './session-id'

/**
*
* @param {Object} config - contains partnerId and entryID
* @return {Promise<*>} - player promise
*/
export default function setup(config: Object): Promise<*> {
let player = Playkit.playkit();
player.addEventListener(player.Event.SOURCE_SELECTED, (event) => {
handleSessionId(event.payload.selectedSource, player);
});
if (config) {
let provider = new OvpProvider(config.partnerId);
return provider.getConfig(config.entryID)
.then(data => {
player.configure(data);
return Promise.resolve(player);
}).catch(error => {
return Promise.reject(error);
});
} else {
return Promise.resolve(player);
}
}
53 changes: 53 additions & 0 deletions test/src/session-id.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import handleSessionId from '../../src/session-id'

describe('handleSessionId', function () {

it('should add session id for playManifest source', function () {
let selectedSource = {url: 'a/b/c/playmanifest/source'};
let player = {};
handleSessionId(selectedSource, player);
selectedSource.url.should.be.equal('a/b/c/playmanifest/source?playSessionId=' + player.sessionId);
});

it('should add session id for playManifest source with query param', function () {
let selectedSource = {url: 'a/b/c/playmanifest/source?a'};
let player = {};
handleSessionId(selectedSource, player);
selectedSource.url.should.be.equal('a/b/c/playmanifest/source?a&playSessionId=' + player.sessionId);
});

it('should add session id for PLAYMANIFEST source', function () {
let selectedSource = {url: 'a/b/c/PLAYMANIFEST/source'};
let player = {};
handleSessionId(selectedSource, player);
selectedSource.url.should.be.equal('a/b/c/PLAYMANIFEST/source?playSessionId=' + player.sessionId);
});

it('should add session id for PLAYMANIFEST source with query param', function () {
let selectedSource = {url: 'a/b/c/PLAYMANIFEST/source?a'};
let player = {};
handleSessionId(selectedSource, player);
selectedSource.url.should.be.equal('a/b/c/PLAYMANIFEST/source?a&playSessionId=' + player.sessionId);
});

it('should not add session id for no playManifest source', function () {
let selectedSource = {url: 'a/b/c'};
let player = {};
handleSessionId(selectedSource, player);
selectedSource.url.should.be.equal('a/b/c');
});

it('should replace the second GUID for existing session id as first query param', function () {
let selectedSource = {url: 'a/b/c/playmanifest/source?playSessionId=8a18888e-4110-d61b-5285-c601c51b70e3:b892a45b-23dc-7f3b-0ca1-5381a88e0c81&a'};
let player = {};
handleSessionId(selectedSource, player);
selectedSource.url.should.be.equal('a/b/c/playmanifest/source?playSessionId=8a18888e-4110-d61b-5285-c601c51b70e3' + player.sessionId.substr(player.sessionId.indexOf(':')) + '&a');
});

it('should replace the second GUID for existing session id as second query param', function () {
let selectedSource = {url: 'a/b/c/playmanifest/source?a&playSessionId=8a18888e-4110-d61b-5285-c601c51b70e3:b892a45b-23dc-7f3b-0ca1-5381a88e0c81'};
let player = {};
handleSessionId(selectedSource, player);
selectedSource.url.should.be.equal('a/b/c/playmanifest/source?a&playSessionId=8a18888e-4110-d61b-5285-c601c51b70e3' + player.sessionId.substr(player.sessionId.indexOf(':')));
});
});
46 changes: 46 additions & 0 deletions test/src/setup.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import setup from '../../src/setup'
import * as TestUtils from 'playkit-js/test/src/utils/test-utils'

describe('setup', function () {

let config, player;

beforeEach(function () {
config = {
partnerId: 2196781, entryID: '1_h14v9eug'
};
});

afterEach(function () {
player.destroy();
TestUtils.removeVideoElementsFromTestPage();
});

it('should create a full player', function (done) {
setup(config).then(p => {
player = p;
player.config.id.should.equal(config.entryID);
player.config.session.partnerID.should.equal(config.partnerId);
done();
});
});

it('should create an empty player', function (done) {
setup().then(p => {
player = p;
(!player.config.id).should.be.true;
done();
});
});

it('should decorate the selected source by session id', function (done) {
setup(config).then(p => {
player = p;
player.load();
let sessionIdRegex = /playSessionId=((?:[a-z0-9]|-|:)*)/i;
player.config.session.id.should.exist;
sessionIdRegex.exec(player.src)[1].should.equal(player.config.session.id);
done();
});
});
});