Skip to content

Commit

Permalink
feat: judge box when we load src to prevent to many kernel switch
Browse files Browse the repository at this point in the history
  • Loading branch information
toxic-johann committed Jun 20, 2018
1 parent d995ca5 commit a1811df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
20 changes: 19 additions & 1 deletion __tests__/situation/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('load', () => {
expect(player.src).toBe('');
});

test('should not trigger switch kernel at the first auto load', () => {
test('should not block normal video event at the first auto load', () => {
const fn = jest.fn();
const plugin = {
name: 'test',
Expand All @@ -147,4 +147,22 @@ describe('load', () => {
expect(fn).toHaveBeenCalledTimes(1);
player.destroy();
});

test('should not switch kernel at the first auto load', () => {
let originalVideo;
const plugin = {
name: 'test',
create() {
originalVideo = this.$video;
},
};
Chimee.install(plugin);
const player = new Chimee({
wrapper: document.createElement('div'),
src: 'http://cdn.toxicjohann.com/lostStar.mp4',
plugins: [ plugin.name ],
});
expect(originalVideo === player.$video).toBe(true);
player.destroy();
});
});
5 changes: 4 additions & 1 deletion src/dispatcher/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import { isString, camelize, deepAssign, isObject, isEmpty, isArray, isFunction, transObjectAttrIntoArray, isPromise, Log, runRejectableQueue, addEvent, removeEvent, isError, deepClone } from 'chimee-helper';
import ChimeeKernel from './kernel';
import { getLegalBox } from './kernel';
import Plugin from './plugin';
import Dom from './dom';
import VideoConfig from 'config/video';
Expand Down Expand Up @@ -415,12 +416,14 @@ export default class Dispatcher {
const videoConfig = this.videoConfig;
const {
isLive = videoConfig.isLive,
box = videoConfig.box,
box = getLegalBox({ src, box: videoConfig.box }),
preset = videoConfig.preset,
kernels = videoConfig.kernels,
isFirst,
} = option;
delete option.isFirst;
if (box !== 'native' || box !== oldBox || !isEmpty(option)) {
option.isFirst = isFirst;
const video = document.createElement('video');
const config = { isLive, box, preset, src, kernels };
const kernel = this._createKernel(video, config);
Expand Down
28 changes: 14 additions & 14 deletions src/dispatcher/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@ const LOG_TAG = 'chimee';
const boxSuffixMap = {
flv: '.flv',
hls: '.m3u8',
mp4: '.mp4',
native: '.mp4',
};

// return the config box
// or choose the right one according to the src
export function getLegalBox({ src, box }: { src: string, box: string }): string {
if (isString(box) && box) return box;
src = src.toLowerCase();
for (const key in boxSuffixMap) {
const suffix = boxSuffixMap[key];
if (src.indexOf(suffix) > -1) return key;
}
return 'native';
}

export default class ChimeeKernel {
box: string;
boxConfig: Object;
Expand All @@ -34,7 +46,7 @@ export default class ChimeeKernel {

initVideoKernel() {
const config = this.config;
const box = this.chooseBox(config);
const box = getLegalBox(config);
this.box = box;
const VideoKernel = this.chooseVideoKernel(this.box, config.preset);

Expand All @@ -50,18 +62,6 @@ export default class ChimeeKernel {
this.videoKernel = new VideoKernel(this.videoElement, config, customConfig);
}

// return the config box
// or choose the right one according to the src
chooseBox({ src, box }: { src: string, box: string }): string {
if (isString(box) && box) return box;
src = src.toLowerCase();
for (const key in boxSuffixMap) {
const suffix = boxSuffixMap[key];
if (src.indexOf(suffix) > -1) return key;
}
return 'native';
}

// choose the right video kernel according to the box setting
chooseVideoKernel(box: string, preset: { [string]: Function }): VideoKernel {
switch (box) {
Expand Down

0 comments on commit a1811df

Please sign in to comment.