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 5 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
495 changes: 336 additions & 159 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.

30 changes: 30 additions & 0 deletions src/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//@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
*/
function create(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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently don't pass the config data we pass to create method to the playkit.configure, which can contain player config as well as config to the provider.
Need to handle the entire config object that we defined for playkit

return Promise.resolve(player);
}).catch(error => {
return Promise.reject(error);
});
} else {
return Promise.resolve(player);
}
}

export default create;
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.create(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 create from './create'
// TODO: Import plugins

export {Playkit, OvpProvider, PlaykitUI};
export {Playkit, OvpProvider, PlaykitUI, create};
Copy link
Contributor

@dan-ziv dan-ziv Jun 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OrenMe @yairans
Should we really need to export anything except create now?


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
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(':')));
});
});