yapi.js introduction (mopcon 2016 version)

67
ADAPTIVE MEDIA PLAYER BASED ON HTML5 陳建⾠ jessechen yapi . js

Upload: jesse-chien-chen-chen

Post on 20-Jan-2017

41 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: yapi.js introduction (mopcon 2016 version)

ADAPTIVE MEDIA PLAYER BASED ON HTML5

陳建⾠ jessechen

yapi.js

Page 2: yapi.js introduction (mopcon 2016 version)

surf, sb, football, draft beer, kktvjessechen陳建⾠

http://blog.hellojcc.tw

Page 3: yapi.js introduction (mopcon 2016 version)

OTT VOD SERVICE台⽇韓中超⼤⽚庫

⼤數據精準推薦

離線播放

投放⾄電視

Page 4: yapi.js introduction (mopcon 2016 version)
Page 5: yapi.js introduction (mopcon 2016 version)

PLAYBACK EXPERIENCEON WEB

WITH HTML5

Page 6: yapi.js introduction (mopcon 2016 version)

影⽚yapi .js

(台)

Page 7: yapi.js introduction (mopcon 2016 version)

WHAT IS BEING PLAYEDmedia

Page 8: yapi.js introduction (mopcon 2016 version)

analog-to-digital adapter

digital raw data

PREPARE MEDIA1. Make it digital

analog signal

Page 9: yapi.js introduction (mopcon 2016 version)

H.264 / H.265Encode

PREPARE MEDIA2. Encode video file

Raw data .mp4 / .ts file

Easier for storage and transference

Page 10: yapi.js introduction (mopcon 2016 version)

server holds media

VINTAGE WAY

Player get it and play

single file

super slow

Page 11: yapi.js introduction (mopcon 2016 version)

server holds media

EFFICIENT WAY

Player get part of video and

start play

streaming(progressive download)

Keep requesting rest part of video during

playback

inflexible

Page 12: yapi.js introduction (mopcon 2016 version)

PREPARE MEDIA3. Fit adaptive need

.mp4 / .ts file

more storage needed but much flexible

different bitrate

+

+

fragments

manifestfile

Page 13: yapi.js introduction (mopcon 2016 version)

MANIFEST TELLS- adaptions: video, audio or subtitle

- available bitrates

- how file is fragmented

- encryption

Page 14: yapi.js introduction (mopcon 2016 version)

ADAPTIVE STREAMING TYPES

SS Smooth Streaming

HLS HTTP Live Streaming

DASH Dynamic Adaptive

Streaming through HTTP

Page 15: yapi.js introduction (mopcon 2016 version)

.mp4 / .ts file different bitrate

+

+

fragments

manifestfileRaw data

imaging there are thousands of media needs to handle

Page 16: yapi.js introduction (mopcon 2016 version)

CONTENT OF KKTV IS PREPARED WITH

MASShttps://github.com/KKBOX/mass

https://goo.gl/6TCmWqintroduction by Jinkuen:

Page 17: yapi.js introduction (mopcon 2016 version)

download

adaptive streamingclient

yapi.js

media

manifestfile

adaptivealgorithm

fragments

Page 18: yapi.js introduction (mopcon 2016 version)

adaptive algorithm decides which quality to download

Page 19: yapi.js introduction (mopcon 2016 version)

• network status

• how long does buffered duration left

algorithm considering…

Page 20: yapi.js introduction (mopcon 2016 version)

adaptive

starting lag

buffering times / duration

overall bitrate

Page 21: yapi.js introduction (mopcon 2016 version)

a/v sync

decode

decrypt

‘playback’client

yapi.js

media

manifestfile

fragments

Page 22: yapi.js introduction (mopcon 2016 version)

decrypt

encryption + decryption

Page 23: yapi.js introduction (mopcon 2016 version)

H.264 / H.265Encode

ENCRYPTION

Raw data .mp4 / .ts file

encryption implemented

here

Page 24: yapi.js introduction (mopcon 2016 version)

CLIENT SIDE DECRYPTION

• Client get a license instead of a key

• A blackbox/sandbox get the key by processing license

• That blackbox/sandbox provide key for decryption and directly output to display

• This solution called DRM(Digital Right Management)

Page 25: yapi.js introduction (mopcon 2016 version)

DRM ON BROWSER

• 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 26: yapi.js introduction (mopcon 2016 version)

DRM ON BROWSER

Widevine FairplayPlayready PrimetimeWidevine

Page 27: yapi.js introduction (mopcon 2016 version)

INTRODUCE EME

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

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

Encrypted Media Extension

Page 28: yapi.js introduction (mopcon 2016 version)

YAPI’S PROTECTION LOGIC

• implement protection on client side through EME

• give what DRM server needs to retrieve license

• deal with different browsers (versions)

Page 29: yapi.js introduction (mopcon 2016 version)

EMECDM

attempting to play encrypted media

get ‘challenge’

DRM license server

request with challenge

get license

yapi

provide license for CDM to decrypt content

EMECDM

PROTECTION LOGIC FLOW

Page 30: yapi.js introduction (mopcon 2016 version)

a/v sync

decode

html5 media element + MSE

Page 31: yapi.js introduction (mopcon 2016 version)

MEDIA ELEMENT

var videoNode = document.createElement(‘video’);

videoNode.src = VIDEO_URL;

With html5 media element, you can play single video source easily

Page 32: yapi.js introduction (mopcon 2016 version)

single source multiple source

+

+

Page 33: yapi.js introduction (mopcon 2016 version)

MSE

“ MSE (Media Source Extension) extends HTMLMediaElement to allow JavaScript to generate media streams for playback.

Allowing JavaScript to generate streams facilitates a variety of use cases like adaptive streaming and time shifting live streams. “

media source extension

Page 34: yapi.js introduction (mopcon 2016 version)

var video = document.createElement(‘video’);

video.src = VIDEO_URL

MEDIASOURCE IS A ‘SOURCE’

set ‘src’ attribute of video element to an url pointed to media source

new window.MediaSource();window.URL.createObjectURL(ms);

Page 35: yapi.js introduction (mopcon 2016 version)

SOURCE BUFFER

sourceBufferVideo = ms.addSourceBuffer(VIDEO_CODEC); sourceBufferAudio = ms.addSourceBuffer(AUDIO_CODEC);

// get stream buffer via network

sourceBufferVideo.appendBuffer(buffer);

// sourcebuffer provides buffer info after append complete

Page 36: yapi.js introduction (mopcon 2016 version)

BUFFER INFO

var buffered = sourceBuffer.buffered; buffered.length; // how many discontinuous buffered time range

buffered.start(0); // first buffer start time

buffered.end(0); // first buffer end time

get buffer information from buffered attribute

Page 37: yapi.js introduction (mopcon 2016 version)

MSE EXTENDS MEDIA ELEMENT

• MSE focus on providing stream buffer to media element

• playback behavior still hold by media elemente.g play, pause, seek

Page 38: yapi.js introduction (mopcon 2016 version)

a/v sync

decode

‘playback’client

yapi.js

media

manifestfile

fragments

decrypt

Page 39: yapi.js introduction (mopcon 2016 version)

streaming

adaptive

protection / EME

mediaElement / MSE

client

yapi.js

Page 40: yapi.js introduction (mopcon 2016 version)

BETWEEN MODULES1. hold reference

problems

• dependency

streaming adaptive

Page 41: yapi.js introduction (mopcon 2016 version)

BETWEEN MODULES2. a parent module

problems

• defining ‘parent’

• multiple parent modules?

streaming

adaptive

parent

parent?

Page 42: yapi.js introduction (mopcon 2016 version)

BETWEEN MODULES3. event system

• modules are parallel

• semantic event name indicates current ‘state’ of player

streaming

adaptive

protection / EME

mediaElement / MSE

eventBus

Page 43: yapi.js introduction (mopcon 2016 version)

streaming

adaptive

MSECtrl

eventBus

eventBus

eventBus

eventBus

Page 44: yapi.js introduction (mopcon 2016 version)

function downloadFragment() {}streaming

adaptive

eventBus

eventBus

MSECtrl eventBus

function calculateQuality() {}

function appendToSourceBuffer() {}

function onDownloaded() { // notify FRAGMENT_DOWNLOADED }

function onAppended() { // notify FRAGMENT_APPENDED }

Page 45: yapi.js introduction (mopcon 2016 version)

register

eventBus.mapHandler(‘FRAGMENT_DOWNLOADED’, )adaptive. calculateQuality

eventBus.mapHandler(‘FRAGMENT_DOWNLOADED’, )MSECtrl. appendToSourceBuffer

eventBus.mapHandler(‘FRAGMENT_APPENDED’, )streaming. downloadFragment

Page 46: yapi.js introduction (mopcon 2016 version)

{ }

FRAGMENT_DOWNLOADED: [ , ],

adaptive. calculateQuality

MSECtrl. appendToSourceBuffer

FRAGMENT_APPENDED: [ ]streaming. downloadFragment

eventBus

Page 47: yapi.js introduction (mopcon 2016 version)

function downloadNextFragment() {}streaming

adaptive

eventBus

eventBus

MSECtrl eventBus

function calculateQuality() {}

function appendToSourceBuffer() {}

function onDownloaded() { eventBus.dispatch(‘FRAGMENT_DOWNLOADED’) }

function onAppended() { // notify FRAGMENT_APPENDED }

dispatch event

Page 48: yapi.js introduction (mopcon 2016 version)

eventBus

{ }

FRAGMENT_DOWNLOADED: [ , ],

adaptive. calculateQuality

MSECtrl. appendToSourceBuffer

FRAGMENT_APPENDED: [ ]streaming. downloadFragment

invoke callbacks

Page 49: yapi.js introduction (mopcon 2016 version)

function downloadNextFragment() {}streaming

adaptive

eventBus

eventBus

MSECtrl eventBus

function calculateQuality() {}

function appendToSourceBuffer() {}

function onDownloaded() { eventBus.dispatch(‘FRAGMENT_DOWNLOADED’) }

function onAppended() { eventBus.dispatch(‘FRAGMENT_APPENDED’) }

dispatch event

Page 50: yapi.js introduction (mopcon 2016 version)

eventBus

{ }

FRAGMENT_DOWNLOADED: [ , ],

adaptive. calculateQuality

MSECtrl. appendToSourceBuffer

FRAGMENT_APPENDED: [ ]streaming. downloadFragment

invoke callbacks

Page 51: yapi.js introduction (mopcon 2016 version)

works with app level

yapi.js

app

ui

client

api

Page 52: yapi.js introduction (mopcon 2016 version)
Page 53: yapi.js introduction (mopcon 2016 version)

works with app level

yapi.js

app

ui

client

events

api

Page 54: yapi.js introduction (mopcon 2016 version)

MEDIA EVENTS

http://www.w3.org/2010/05/video/mediaevents.html

Dispatched by HTML5 media element, notifying playback process

Page 55: yapi.js introduction (mopcon 2016 version)

YAPI PROXY/PATCH EVENTS

Media Element event event with same name

yapi.js

Page 56: yapi.js introduction (mopcon 2016 version)

USEFUL MEDIA EVENTS

loadstart: Indicate loading media begins, this fires when setting src attribute

loadedmetadata: while element has basic info of playback, e.g duration

timeupdate: while current time of playback is ticking

seeking/seeked: while conducting seek

ended: playback ends

play/playing: playback resume from other status to playing

Page 57: yapi.js introduction (mopcon 2016 version)

ADDITIONAL EVENTSloadedmanifest: after manifest is loaded/parsed

bitratechanged: when bitrate is changed

enableabr: when adaptive activation changed

buffering: when playback pending

cuechanged: webvtt subtitle cue changed (in and out)

Page 58: yapi.js introduction (mopcon 2016 version)

yapi.load(MANIFEST_URL); // exposed api

yapi.addEventListener(‘playing’, onPlaying);

function onPlaying() { // app logic // or ui reaction, e.g showing playing status ui }

// onPlaying invoked when playing

app yapi ui

Page 59: yapi.js introduction (mopcon 2016 version)

LESSONS

Page 60: yapi.js introduction (mopcon 2016 version)

1. test case helps

Page 61: yapi.js introduction (mopcon 2016 version)

2. handle errors

Page 62: yapi.js introduction (mopcon 2016 version)

3. boss wants ‘numbers’

Page 63: yapi.js introduction (mopcon 2016 version)

streaming

adaptive

protection / EME

mediaElement / MSE

client

yapi.js

statisticplaying smoothly is not

enough

you need to tell how good it is

Page 64: yapi.js introduction (mopcon 2016 version)
Page 65: yapi.js introduction (mopcon 2016 version)

LIVE DEMOhttps://app.kktv.me

Page 67: yapi.js introduction (mopcon 2016 version)

THANK YOUKKTV is hiring

web / android / iOS / backend developers

[email protected]