Skip to content

Commit

Permalink
refactor: add ttplayervideo support dynamic use mse, and create stand…
Browse files Browse the repository at this point in the history
…ard mse library

add ttplayervideo support dynamic use mse, and create standard mse library, delete useless video
component and control component relative dependency, update relative rollup script code
  • Loading branch information
JohnApache committed May 11, 2020
1 parent 6e40a31 commit cd3ec19
Show file tree
Hide file tree
Showing 29 changed files with 365 additions and 554 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import TTPlayerMedia, {
TTPlayerMediaOptions,
TTPlayerMediaComponent,
} from './media/media';
import TTPlayerVideo, { TTPlayerVideoFactory, TTPlayerVideoOptions } from './media/video';
import TTPlayerVideo, {
TTPlayerVideoFactory,
TTPlayerVideoOptions,
VideoMSE,
} from './media/video';
import TTPlayerAudio, { TTPlayerAudioFactory, TTPlayerAudioOptions } from './media/audio';
import TTPlayerPlayButton from './components/play-button';
import TTPlayerTime from './components/time';
Expand All @@ -24,6 +28,7 @@ import {
TTPlayerQualityList,
TTPlayerBaseQuality,
} from './components/quality-switch';

import {
TTPlayerPlaySwitch,
TTPlayerPlayList,
Expand Down Expand Up @@ -61,6 +66,7 @@ export {
TTPlayerMediaOptions,
TTPlayerMediaComponent,

VideoMSE,
TTPlayerVideo,
TTPlayerVideoFactory,
TTPlayerVideoOptions,
Expand Down
51 changes: 49 additions & 2 deletions packages/core/src/media/video/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import TTPlayerCore from '../../core';
import TTPlayerMedia, { TMediaType } from '../media';
import TTPlayerMediaComponent from '../component';
import TTPlayerVideoOptions from './option';
import VideoMSE, { IMSECtor } from './mse';
import { DOMUtils } from '@dking/ttplayer-utils';


const VIDEO_MEDIA_TYPE: TMediaType = 'Video';

interface TTPlayerVideoComponentCtor {
Expand All @@ -16,7 +16,10 @@ class TTPlayerVideo extends TTPlayerMedia<'Video'> {
static className: string = 'ttplayer__video--component';
static mediaType = VIDEO_MEDIA_TYPE;
static videoComponentsCtor: TTPlayerVideoComponentCtor[] = [];
static msesCtor: IMSECtor[] = [];

public mses: VideoMSE[] = [];
public currentMSE: VideoMSE | null = null;
public video: DOMUtils<HTMLVideoElement>;
public options: TTPlayerVideoOptions;
public videoComponents: TTPlayerMediaComponent<'Video'>[] = []
Expand All @@ -30,13 +33,30 @@ class TTPlayerVideo extends TTPlayerMedia<'Video'> {
...player.options.video,
});
this.initVideoComponents();
this.initMSE();
}

static use (videoComponentCtor: TTPlayerVideoComponentCtor) {
this.videoComponentsCtor.push(videoComponentCtor);
return this;
}

static useMSE (ctor: IMSECtor) {
this.msesCtor.push(ctor);
return this;
}

get src (): string {
return this.mediaDom.src;
}

set src (src: string) {
this.logger.debug(`media set src ${ src }`);
this.mediaDom.src = src;
this.options.src = src;
this.createMSE(src);
}

get poster (): string {
return this.mediaDom.poster;
}
Expand Down Expand Up @@ -66,6 +86,7 @@ class TTPlayerVideo extends TTPlayerMedia<'Video'> {
componentWillUnmount () {
super.componentWillUnmount();
this.videoComponents.forEach(comp => comp.componentWillUnmount());
this.clearMSE();
}

beforeRender () {
Expand All @@ -78,12 +99,37 @@ class TTPlayerVideo extends TTPlayerMedia<'Video'> {
super.render();
}

private initMSE () {
/* eslint-disable */
const msesCtor = (this.constructor as typeof TTPlayerVideo).msesCtor || [];
/* eslint-enable */
msesCtor.forEach(ctor => {
this.mses.push(new ctor(this));
});
}

private createMSE (src: string, type: string = 'auto') {
const mses = this.mses;
for (let i = 0; i < mses.length; i++) {
const mse = mses[i];
if (!mse.checkType(src, type)) continue;
mse.initMSE(src);
this.currentMSE = mse;
break; // 初始化直接跳过后续检查
}
}

private clearMSE () {
if (!this.currentMSE) return;
this.currentMSE.clearMSE();
}

private initVideoComponents () {
/* eslint-disable */
(this.constructor as typeof TTPlayerVideo).videoComponentsCtor.forEach(ctor => {
this.videoComponents.push(new ctor(this));
});
/* eslint-enable */
/* eslint-enable */
}

private renderVideoComponents () {
Expand Down Expand Up @@ -114,6 +160,7 @@ export {
TTPlayerVideoComponentCtor,
TTPlayerVideoFactory,
TTPlayerVideoOptions,
VideoMSE,
};

export default TTPlayerVideo;
29 changes: 29 additions & 0 deletions packages/core/src/media/video/mse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import TTPlayerVideo from '.';

interface IMSECtor {
new (video: TTPlayerVideo): MSE;
}

class MSE {

public video: TTPlayerVideo;
constructor (video: TTPlayerVideo) {
this.video = video;
}

checkType (url: string, type: string): boolean {
console.log(url, type);
return false;
}

initMSE (url: string, options?: any) {
console.log(url, options);
}

clearMSE () {}

}

export { IMSECtor };

export default MSE;
11 changes: 11 additions & 0 deletions packages/mse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `mse`

> TODO: description
## Usage

```
const mse = require('mse');
// TODO: DEMONSTRATE API
```
15 changes: 6 additions & 9 deletions packages/video/package.json → packages/mse/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{
"name": "@dking/ttplayer-video",
"name": "@dking/ttplayer-mse",
"version": "0.0.3",
"description": "ccplayer-video",
"description": "> TODO: description",
"author": "cjw <[email protected]>",
"homepage": "https://github.com/JohnApache/TT-Player#readme",
"license": "MIT",
"main": "./lib/index.js",
"module": "./lib/index.esm.js",
"typings": "./lib/index.d.ts",
"publishConfig": {
"access": "public"
},
"directories": {
"lib": "lib",
"test": "__tests__"
Expand All @@ -22,13 +19,13 @@
"type": "git",
"url": "git+https://github.com/JohnApache/TT-Player.git"
},
"scripts": {},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
"url": "https://github.com/JohnApache/TT-Player/issues"
},
"dependencies": {
"@dking/ttplayer-core": "^0.0.3",
"@dking/ttplayer-utils": "^0.0.3",
"eventemitter3": "^4.0.0"
"@dking/ttplayer-core": "^0.0.3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -980,4 +980,5 @@ declare namespace dashjs {
export type MetricType = 'ManifestUpdate' | 'RequestsQueue';
export type TrackSwitchMode = 'alwaysReplace' | 'neverReplace';
export type TrackSelectionMode = 'highestBitrate' | 'widestRange';
}
}

53 changes: 53 additions & 0 deletions packages/mse/src/dash/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { MediaPlayerClass } from './dash';
import { VideoMSE, TTPlayerVideo } from '@dking/ttplayer-core';

declare global {
interface Window {
dashjs: MediaPlayerClass;
}
}

class DashMSE extends VideoMSE {

static MSE_TYPE: string = 'dash';
public MSE_TYPE: string;
public __dash__: MediaPlayerClass | null = null;

constructor (video: TTPlayerVideo) {
super(video);
this.handleMSEError = this.handleMSEError.bind(this);
/* eslint-disable */
this.MSE_TYPE = (this.constructor as typeof DashMSE).MSE_TYPE || 'auto';
/* eslint-enable */
}

checkType (url: string, type: string = 'auto'): boolean {
if (type === this.MSE_TYPE) return true;
return type === 'auto' && !!(/.mpd(#|\?|$)/i).exec(url);
}

initMSE (url: string, dashOption?: any) {
const dashjs = window.dashjs;
if (!dashjs) throw new Error(`can't find dashjs`);
const dash = dashjs.MediaPlayer().create();
dashOption && dash.updateSettings(dashOption);
dash.initialize(this.video.mediaDom, url, false);
dash.on('error', this.handleMSEError);
this.__dash__ = dash;
}

clearMSE () {
if (this.__dash__) {
this.__dash__.off('error', this.handleMSEError);
this.__dash__.reset();
}
this.__dash__ = null;
}

private handleMSEError (error: any) {
this.video.event.emit('error', error);
}

}

export default DashMSE;
File renamed without changes.
64 changes: 64 additions & 0 deletions packages/mse/src/flv/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import FlvJs from './flv';
import { VideoMSE, TTPlayerVideo } from '@dking/ttplayer-core';

declare global {
interface Window {
flvjs: typeof FlvJs;
}
}

class FlvMSE extends VideoMSE {

static MSE_TYPE: string = 'flv';
public MSE_TYPE: string;
public __flv__: FlvJs.Player | null = null;
constructor (video: TTPlayerVideo) {
super(video);
this.handleFlvError = this.handleFlvError.bind(this);
/* eslint-disable */
this.MSE_TYPE = (this.constructor as typeof FlvMSE).MSE_TYPE || 'auto';
/* eslint-enable */
}

checkType (url: string, type: string = 'auto'): boolean {
if (type === this.MSE_TYPE) return true;
return type === 'auto' && !!(/.flv(#|\?|$)/i).exec(url);
}

initMSE (url: string, flvjsOption?: any) {
const {
mediaDataSource = {},
config = {},
} = flvjsOption || {};
const flvjs = window.flvjs;
if (!flvjs) throw new Error(`can't find flvjs`);
if (!flvjs.isSupported()) throw new Error(`flv is not supported`);

const options = {
url : url,
type: 'flv',
...mediaDataSource,
};

const flv = flvjs.createPlayer(options, config);
flv.attachMediaElement(this.video.mediaDom);
flv.load();
flv.on('error', this.handleFlvError);
this.__flv__ = flv;
}

clearMSE () {
if (this.__flv__) {
this.__flv__.off('error', this.handleFlvError);
this.__flv__.destroy();
}
this.__flv__ = null;
}

private handleFlvError (error: any) {
this.video.event.emit('error', error);
}

}

export default FlvMSE;
File renamed without changes.
Loading

0 comments on commit cd3ec19

Please sign in to comment.