Introduce native html5 streaming player

75
HOW DID I CREATE A HTML5 VIDEO STREAMING PLAYER and what I’ve learned KKBOXVideo 陳建辰 jessechen

Transcript of Introduce native html5 streaming player

Page 1: Introduce native html5 streaming player

HOW DID I CREATE A HTML5 VIDEO STREAMING PLAYER

and what I’ve learned

KKBOXVideo陳建辰 jessechen

Page 2: Introduce native html5 streaming player

AGENDA• Some beforehand knowhow

• Why developing this player

• HTML5 extensions

• Introduce our player

• Future of this player

Page 3: Introduce native html5 streaming player

SOME BEFOREHAND KNOWHOW

Page 4: Introduce native html5 streaming player

analog-to-digital adapter

digital raw data

PROCESS OF PLAYING VIDEO1. Make it digital

analog signal

Page 5: Introduce native html5 streaming player

H.264 / H.265Encode

PROCESS OF PLAYING VIDEO2. Encode video file

Raw data .mp4 / .ts file

Easier for storage and transference

Page 6: Introduce native html5 streaming player

PROCESS OF PLAYING VIDEO3. Play it !

.mp4 / .ts filea/v sync

codec decode and

display

extract contentfrom container

Page 7: Introduce native html5 streaming player

WAIT, WHERE IS PLAYER?

Page 8: Introduce native html5 streaming player

WHAT PLAYER DOES

.mp4 / .ts filea/v sync

codec decode and

display

HERE

extract contentfrom container

Page 9: Introduce native html5 streaming player

THEY ARE ALREADY HANDLED

.mp4 / .ts file

HTML5

a/v sync

codec decode and

display

extract contentfrom container

Page 10: Introduce native html5 streaming player

SO WHY DEVELOPING THIS PLAYER?

Page 11: Introduce native html5 streaming player

OUR PLAYER FOCUS ON (1)

extract content and a/v info

from container

.mp4 / .ts file

get filesin smart way

a/v sync

codec decode and

display

Page 12: Introduce native html5 streaming player

HOW PLAYER GET FILE ?

Page 13: Introduce native html5 streaming player

server holds .mp4

content

VINTAGE WAY

Player get it and play

single file

slow and inflexible

Page 14: Introduce native html5 streaming player

server holds .mp4

content

EFFICIENT WAY

Player get part of video and

start play

progressive download

Keep requesting rest part of video during

playback

still inflexible

Page 15: Introduce native html5 streaming player

prepare different qualities

of contenton server

MODERN WAYadaptive streaming

Player playsbest quality depends on

network status

Keep requesting rest part of video during

playback

Page 16: Introduce native html5 streaming player

HOW ABOUT WE SPLIT FILE• split file into segments, described by manifest

• manifest tells

- available bitrates

- how file is fragmented

- other information, e.g encryption

+

+

Page 17: Introduce native html5 streaming player

knows how toget fragments

ADAPTIVE STREAMING IN SHORT

fragments

manifestfile

Page 18: Introduce native html5 streaming player

ADAPTIVE STREAMING TYPES

SSSmooth Streaming

HLSHTTP Live Streaming

HDS

Page 19: Introduce native html5 streaming player

INTRODUCE DASHDynamic Adaptive Streaming through HTTP

Page 20: Introduce native html5 streaming player

PROS OF DASH

• open source, with industry standard, which means more universal usage

• support 2 types of containers, the MPEG-4 file format or the MPEG-2 Transport Stream

Page 21: Introduce native html5 streaming player

GET FILES IN SMART WAY

• manifest parser - able to read different type of manifest

• adaptive bitrate - able to decide which quality to load

Page 22: Introduce native html5 streaming player

OUR PLAYER FOCUS ON (2)

.mp4 / .ts file

get filesin smart way

protectionlogic

extract contentfrom container

a/v sync

codec decode and

display

Page 23: Introduce native html5 streaming player

DRM ?(DIGITAL RIGHTS MANAGEMENT)

Content provider / DRM provider likes it

For end user

Page 24: Introduce native html5 streaming player

CLIENT SIDE PROTECTION• Client get a license instead of a key from DRM

service

• A blackbox or sandbox get the key by processing license

• That blackbox / sandbox decrypt content and directly output it to display

Page 25: Introduce native html5 streaming player

H.264 / H.265Encode

HOW TO PROTECT CONTENT

Raw data .mp4 / .ts file

DRM implemented

here

Page 26: Introduce native html5 streaming player

PROTECTION LOGIC

• give what DRM server needs and retrieve license

• negotiate with browser in order to implement protection on client side

• deal with different browsers

knows how to

Page 27: Introduce native html5 streaming player

NOW, LET’S START TALK ABOUT BROWSER

<video>, MSE and EME

Page 28: Introduce native html5 streaming player

HTML5 VIDEO ELEMENT• <video></video> support progressive download

• For adaptive streaming, MSE is required

Page 29: Introduce native html5 streaming player

extract content from container

WHAT ROLE MSE PLAYS

.mp4 / .ts file

MSE handles

a/v sync

codec decode and

display

MSE handles

Page 30: Introduce native html5 streaming player

INTRODUCE MSE

• Given different bitrate segments and it helpshtml5 video element to play smoothly

• SourceBuffer provide video and audio buffer condition

Media Source Extension

Page 31: Introduce native html5 streaming player

EME IS ABOUT PROTECTION

Page 32: Introduce native html5 streaming player

HOW BROWSER ADOPT DRM

• For browser, that blackbox called CDM (Content Decrypt Module)

• Each browser support different DRM

context - “a blackbox or sandbox get the key by processing license”

Page 33: Introduce native html5 streaming player

DRM ON BROWSER

Widevine FairplayPlayready Primetime

Page 34: Introduce native html5 streaming player

INTRODUCE EME

• Even though browser support its own DRM, W3C defines a EME spec, in order to expose same api for client

• prefixed api was implemented on earlier version of chrome(smart tv)

Encrypted Media Extension

Page 35: Introduce native html5 streaming player

EMECDM

provide context from encrypted content

get ‘challenge’

DRM license server

request with challenge

get license

provide license for CDM to decrypt content

player

EMECDM

PROTECTION LOGIC FLOW

Page 36: Introduce native html5 streaming player

WHAT ROLE EME PLAYS

extract contentfrom container

.mp4 / .ts file

a/v synccodec decode

anddisplay

decrypt content in blackbox

Page 37: Introduce native html5 streaming player

INTRODUCE OUR PLAYER

Page 38: Introduce native html5 streaming player

OUR GOAL

• play not single file but sequence of segments, with different bitrate, a.k.a adaptive streaming

• play protected stuffs, for content providers’ goods

a player able to

Page 39: Introduce native html5 streaming player

影片ya pi

yapi.js

Page 40: Introduce native html5 streaming player

DEVELOP PROCESS

make it work stable refactor

Page 41: Introduce native html5 streaming player

MAKE IT WORK FIRST

only 2 files in the very beginning

Page 42: Introduce native html5 streaming player

BE STABLE

• well structured

• modularized

• dependency management (dijon.js)

• consistent code style

Page 43: Introduce native html5 streaming player

MODULARIZED

api

adaptive

extension

fragment

manifest

protection

stats

stream

utils

Page 44: Introduce native html5 streaming player

UTILS• Capabilities

• Debug

• ErrorHandler

• EventBus

• IntervalBus

• UrlModel

api

adaptive

extension

fragment

manifest

protection

stats

stream

utils

Page 45: Introduce native html5 streaming player

EXTENSION

• MediaSourceExtension

• SourceBufferExtension

• VideoModel

api

adaptive

extension

fragment

manifest

protection

stats

stream

utils

Page 46: Introduce native html5 streaming player

MANIFEST

• ManifestLoader

• ManifestExtension

• ManifestModel

api

adaptive

extension

fragment

manifest

protection

stats

stream

utils

Page 47: Introduce native html5 streaming player

STREAM

• StreamCtrl

• BufferCtrlapi

adaptive

extension

fragment

manifest

protection

stats

stream

utils

Page 48: Introduce native html5 streaming player

FRAGMENT

• FragmentCtrl

• FragmentLoader

• SegmentTemplate

api

adaptive

extension

fragment

manifest

protection

stats

stream

utils

Page 49: Introduce native html5 streaming player

ADAPTIVE

• AbrCtrl

• BandwidthRecorderapi

adaptive

extension

fragment

manifest

protection

stats

stream

utils

Page 50: Introduce native html5 streaming player

PROTECTION

• ProtectionCtrl

• ProtectionModel

• ProtectionRequest

• Playready / Widevine

api

adaptive

extension

fragment

manifest

protection

stats

stream

utils

Page 51: Introduce native html5 streaming player

STATS

• MetricsCtrl

• Metrics

• StatsCtrl

• BitrateHistory

api

adaptive

extension

fragment

manifest

protection

stats

stream

utils

Page 52: Introduce native html5 streaming player

REFACTOR

• review flow

• redefine api

Page 53: Introduce native html5 streaming player

APPLICATION FLOW

Page 54: Introduce native html5 streaming player
Page 55: Introduce native html5 streaming player

REDEFINE API

• use jsdoc to generate spec document

• define api, event and vo(value object)

• spec

• result

Page 56: Introduce native html5 streaming player

DEMOSample player

Page 57: Introduce native html5 streaming player

HOW TO MANAGE THOSE MODULES

Page 58: Introduce native html5 streaming player

DEPENDENCY INJECTION

• not necessary in the beginning, but became very important after it went much more complex

• use dijon.js as di framework

Page 59: Introduce native html5 streaming player

INTRODUCE DIJON.JS

• a system holds all dependencies, after mapping modules to it

• an object get dependencies after injection

Page 60: Introduce native html5 streaming player

DEFINE DEPENDENCIES

// A depfunction A() {}

// B requires Afunction B() { this.a = undefined; // how dijon knows where to inject}

// instantiate dijonvar system = new dijon.System();

Page 61: Introduce native html5 streaming player

3 KINDS OF MAPPING

var a = new A();system.mapValue(‘aValue’, a);// system.getObject(‘a’) would return an ‘a’ instance

system.mapSingleton(aSingleton, A);// system.getObject(‘aSingleton’) would return a singleton a

system.mapClass(bClass, B);// system.getObject(‘bClass’) would return a new inited B

Page 62: Introduce native html5 streaming player

OUTLET MAPPING

// map outlet to make dependency work// @param sourceKey {String} - the key mapped to system would be injected// @param targetKey {String|'global'} - the key outlet is assigned to// @param outletName {String|sourceKey} - the name of property used as an outletsystem.mapOutlet('aSingleton', 'bClass', ‘a');

var b = system.getObject(‘bClass’);// b.a is aSingleton

Page 63: Introduce native html5 streaming player

DIRECTLY INJECTION

var b = new B();system.injectInto(b);

// b WOULDN’T have dependency singleton A// b/c we only mapOutlet to key bClass of system

// map outlet to globalsystem.mapOutlet(‘aSingleton’, ‘global’, ‘a’); system.injectInto(b); // this would work

Page 64: Introduce native html5 streaming player

AUTO MAP OUTLET

function B(){this.aSingleton = undefined;this.c = undefined;

}function C(){}

// auto mapped outlet would assign to ‘global’// and outlet name is the same as mapped key (before mapping)system.autoMapOutlets = true;

system.mapClass(‘bClass’, B); // map again b/c B is changedsystem.mapClass(‘c’, C);// system.getObject(‘b’) would have c dep

Page 65: Introduce native html5 streaming player

COMMUNICATE IN BETWEEN

Now B has dependencies of A and C

How would you do in this scenario:

A is doing something when it’s done, invoke a method of B

–this is a very common situation in player

Page 66: Introduce native html5 streaming player

MAP HANDLER AND NOTIFYfunction A() {

this.say = function() {system.notify(‘aDoneSaying’);

};}

function B() {this.a = undefined;this.afterAsays = function(){

// do something};

}

system.mapSingleton(‘a’, A); system.mapSingleton(‘b’, B);

system.mapHandler(‘aDoneSaying’, ’b’, ‘afterAsays’);// system.getObject(‘b’).a.say() would invoke b.afterAsays

Page 67: Introduce native html5 streaming player

NOTIFYING CLASSsystem.mapSingleton(‘a’, A); system.mapClass(‘b’, B); // map class here

var b1 = system.getObject(‘b’);// b1.say() would invoke a newly instantiated b.afterAsays// instead of b1.afterAsays

system.mapValue(‘b1’, b1);system.unmapHandler(‘aDoneSaying’, ‘b’, ‘afterAsays’) system.mapHandler(‘aDoneSaying’, ‘b1’, ‘afterAsays’)// b1.say() would invoke b1.afterAsays

Page 68: Introduce native html5 streaming player

function E() { this.a = undefined; this.setup = function () {};}

var e = new E();

system.injectInto(e);// e.setup invoked

CONVENTIONsetup method of module would be invoked

after getObject or injectInto

Page 69: Introduce native html5 streaming player

INJECT SYSTEM ITSELF

system.mapValue('system', system);

function D() { this.system = undefined;}

system.mapSingleton('d', D);

// system.getObject(‘d’) has system as dependency

Page 70: Introduce native html5 streaming player

function A() {}; function B() {}

function Dep() { return { system: undefined, setup: function () { this.system.autoMapOutlets = true; // map dep here this.system.mapSingleton('a', A); this.system.mapClass('b', B); } };}

function App() { var b; var system = new dijon.System(); system.mapValue('system', system); system.mapOutlet('system'); var dep = new Dep(); system.injectInto(dep); // inject system to dep and invoke setup function, which map all deps

return { system: undefined, a: undefined, setup: function () { b = system.getObject('b'); },

init: function () { system.injectInto(this); // after init, app.a exists } };}// after new App().init(), app get all dep setup in Dep

Page 71: Introduce native html5 streaming player

SUMMARY OF DIJONJS• 3kb

• very little convention

• focus on di and notify / handler

Page 72: Introduce native html5 streaming player

BUILD PROJECT

• list all source files in index.html of testing page

• grunt as task manager

• concat and minify scripts with grunt-usemin

• export to dist/player.min.js

Page 73: Introduce native html5 streaming player

ONE LAST NOTICE

• ui should be totally separated

• ui <-> player -> videoElement

• ui order player by api, respond to player behavior by event

playervideoElement

ui

Page 74: Introduce native html5 streaming player

FUTURE OF YAPI.JS

• firefox / safari drm support

• live

• ads

• smart tv / chromecast / nexus player

Page 75: Introduce native html5 streaming player

THANK YOUquestions ?