Slide 1

Slide 1 text

Video Streaming on Android wasabeef

Slide 2

Slide 2 text

About me 降矢 大地 (Daichi Furiya) Google Developer Expert AbemaTV, Inc. wasabeef @wasabeef_jp

Slide 3

Slide 3 text

Streaming

Slide 4

Slide 4 text

Streaming? 1:30 1:31 1:32 1:33

Slide 5

Slide 5 text

HLS MPEG-DASH Microsoft Smooth Streaming RTMP Streaming?

Slide 6

Slide 6 text

Video compression

Slide 7

Slide 7 text

Video compression 30fps 1sec 60KB × 30 = 1.8MB 1min 1.8MB × 60 = 108MB 1hour 108MB × 10 = 1.1GB 60KB 65KB 55KB 50KB

Slide 8

Slide 8 text

Video compression 60KB 5KB 2KB 7KB 30fps 1sec 60KB + (4KB × 30) = 180KB 1min 60KB + (4KB × 1800) = 7.2MB 1hour 7.2MB × 10 = 72MB

Slide 9

Slide 9 text

Video compression Be impossible ;(

Slide 10

Slide 10 text

Key Frame Be possible :)

Slide 11

Slide 11 text

Video structure Video Audio Meta Butterfly…? Discovered

Slide 12

Slide 12 text

Format

Slide 13

Slide 13 text

Container format

Slide 14

Slide 14 text

.avi .mp4 .mkv .ogg .flv etc.. Container format Video H.264 H.265 Divx VP9 Audio AAC WMA Voribis PCM Caption SAMI, SMIL Hi-Caption CMML, DXFP Meta Date Author Title

Slide 15

Slide 15 text

Video codec

Slide 16

Slide 16 text

H.264/MPEG-4 AVC H.265/HEVC VP8 VP9 Video codec

Slide 17

Slide 17 text

8K 300fps Half the size of AVC Android 5.+ H.265/HEVC

Slide 18

Slide 18 text

Audio codec

Slide 19

Slide 19 text

MP3 AAC PCM Vorbis Audio Codec

Slide 20

Slide 20 text

HLS

Slide 21

Slide 21 text

Apple Adaptive bitrate streaming MPEG2-TS H.264/AAC HLS

Slide 22

Slide 22 text

MPEG-4 Fragment Support In-playlist metadata Offline HLS playback NEW HLS

Slide 23

Slide 23 text

Draft - HTTP Live Streaming http://bit.ly/hls20 Document

Slide 24

Slide 24 text

playlist.m3u8

Slide 25

Slide 25 text

BigBuckBunny.mp4 (65MB) https://peach.blender.org/

Slide 26

Slide 26 text

720p

Slide 27

Slide 27 text

1280×720 16:9 (1.78:1) Progressive 720p

Slide 28

Slide 28 text

# ffmpeg -i BigBuckBunny.mp4 \ -vcodec libx264 \ -s 1280x720 \ -acodec aac -b:a 256k \ -flags +loop-global_header \ -bsf h264_mp4toannexb \ -f segment -segment_format mpegts \ -segment_time 10 \ -segment_list output.m3u8 output_%04d.ts Converting MP4 to HLS With FFmpeg

Slide 29

Slide 29 text

Converting MP4 to HLS With FFmpeg

Slide 30

Slide 30 text

ts?

Slide 31

Slide 31 text

Transport Stream (ts) Segment Container (2sec 〜 10sec)

Slide 32

Slide 32 text

# python -m SimpleHTTPServer Web Server

Slide 33

Slide 33 text

#EXTM3U #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:0 #EXTINF:10, no desc output0000.ts #EXTINF:10, no desc output0001.ts #EXTINF:10, no desc 〜 ... 〜 output180.ts #EXT-X-ENDLIST playlist.m3u8

Slide 34

Slide 34 text

playlist.m3u8 Segment1.ts Length: 10sec Segment2.ts Length: 10sec Segment3.ts Length: 10sec Segment4.ts Length: 10sec Header Footer

Slide 35

Slide 35 text

playlist.m3u8 Segment1.ts Length: 10sec Segment2.ts Length: 10sec Advertising.ts Length: 10sec Segment3.ts Length: 10sec Header Footer

Slide 36

Slide 36 text

#EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=200000 playlist1.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=311111 playlist2.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=484444 playlist3.m3u8 #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=737777 playlist4.m3u8 playlist.m3u8

Slide 37

Slide 37 text

playlist.m3u8 playlist1.m3u8 BANDWIDTH: 200000 Header playlist2.m3u8 BANDWIDTH: 311111 playlist3.m3u8 BANDWIDTH: 484444 playlist4.m3u8 BANDWIDTH: 737777

Slide 38

Slide 38 text

MPEG-DASH

Slide 39

Slide 39 text

Microsoft, Netflix, Google, Ericsson, Samsung, Adobe Adaptive bitrate streaming fMP4, WebM, MPEG2-TS MPEG-DASH

Slide 40

Slide 40 text

manifest.mdp

Slide 41

Slide 41 text

manifest.mdp <-- Created with Bento4 mp4-dash.py, VERSION=1.7.0-613 --> <-- Video --> <-- Audio -->

Slide 42

Slide 42 text

- Root manifest.mdp - A start time and duration. - Audio/Video content - Bitrate and Aspect

Slide 43

Slide 43 text

Android

Slide 44

Slide 44 text

Android Media APIs

Slide 45

Slide 45 text

AudioTrack MediaPlayer JetPlayer SoundPool Android Media APIs

Slide 46

Slide 46 text

API Level 1 Native (C/C++) Simple Not customizable MediaPlayer

Slide 47

Slide 47 text

MediaPlayer MediaExtractor MediaCodec AudioTrack MediaPlayer Network Buffering Extraction Decoding Rendering

Slide 48

Slide 48 text

MediaPlayer String url = “http://wasabeef.jp/sample.mp4”; MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(STREAM_MUSIC); mediaPlayer.setDataSource(url); mediaPlayer.prepare(); mediaPlayer.setSurface(surface); mediaPlayer.start(); // mediaPlayer.release();

Slide 49

Slide 49 text

ExoPlayer

Slide 50

Slide 50 text

API Level 16 Easy to extend and customize HLS, MPEG-DASH, SmoothStreaming DRM (WideVine, PlayReady) ExoPlayer

Slide 51

Slide 51 text

Supported Devices/Formats

Slide 52

Slide 52 text

Devices Use Case Version API Level Audio 4.1 16 Video 4.1 16 WideVine 4.3 18 PlayReady AndroidTV AndroidTV

Slide 53

Slide 53 text

Formats (Containers) Feature HLS MPEG-DASH fMP4 ✕ ◯ WebM ✕ ◯ Matroska ✕ ◯ MPEG-TS ◯ ✕ ADTS(AAC) ◯ ✕ MP3 ◯ ✕

Slide 54

Slide 54 text

Formats (Closed Caption) Feature HLS MPEG-DASH TTML ✕ ◯ WebVTT ◯ ◯ Tx3g ✕ ◯ SubRip ✕ ◯ EIA-608 ◯ ✕

Slide 55

Slide 55 text

Formats (Meta) Feature HLS MPEG-DASH ID3 Tags ◯ ✕

Slide 56

Slide 56 text

Formats (Content Protection) Feature HLS MPEG-DASH AES-128 ◯ ✕ WideVine ✕ ◯ PlayReady ◯ △ (AndroidTV)

Slide 57

Slide 57 text

2.x

Slide 58

Slide 58 text

A greatly simplified demo app. A VideoView equivalent in SimpleExoPlayerView. Improved Audio extension APIs with pre- built extensions for ffmpeg, opus, and flac. Playlist Support. New features

Slide 59

Slide 59 text

ExoPlayer MediaSource Parser Extractor DataSource MediaCodecRenderer MediaCodec AudioTrack ExoPlayer Network Buffering Extraction Decoding Rendering

Slide 60

Slide 60 text

MediaSource

Slide 61

Slide 61 text

HlsMediaSource DashMediaSource SsMediaSource ExtractorMediaSource MediaSource

Slide 62

Slide 62 text

MergingMediaSource LoopingMediaSource ConcatenatingMediaSource MediaSource

Slide 63

Slide 63 text

MediaSource // Measures bandwidth during playback. Can be null if not required. DefaultBandwidthMeter bandwidth = new DefaultBandwidthMeter(); // Produces DataSource instances through which media data is loaded. DataSource.Factory dataSource = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "wasabeef"), bandwidthMeter); // Produces Extractor instances for parsing the media data. ExtractorsFactory extractors = new DefaultExtractorsFactory(); // This is the MediaSource representing the media to be played. MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri, dataSource, extractors, null, null); // Prepare the player with the source. player.prepare(videoSource);

Slide 64

Slide 64 text

Parser

Slide 65

Slide 65 text

#EXTM3U #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:0 #EXTINF:10, no desc output0000.ts #EXTINF:10, no desc output0001.ts #EXTINF:10, no desc 〜 省略 〜 output180.ts #EXT-X-ENDLIST Parser

Slide 66

Slide 66 text

HlsPlaylistParser DashManifestParser SsManifestParser Parser

Slide 67

Slide 67 text

HlsPlaylistParser private static final String TAG_VERSION = "#EXT-X-VERSION"; private static final String TAG_STREAM_INF = "#EXT-X-STREAM-INF"; private static final String TAG_MEDIA = "#EXT-X-MEDIA"; private static final String TAG_DISCONTINUITY = "#EXT-X-DISCONTINUITY"; private static final String TAG_MEDIA_DURATION = “#EXTINF"; // ...

Slide 68

Slide 68 text

Extractor

Slide 69

Slide 69 text

TsExtractor FragmentedMp4Extractor Mp4Extractor FlvExtractor Extractor

Slide 70

Slide 70 text

DataSource

Slide 71

Slide 71 text

DataSource // Measures bandwidth during playback. Can be null if not required. DefaultBandwidthMeter bandwidth = new DefaultBandwidthMeter(); // Produces DataSource instances through which media data is loaded. DataSource.Factory dataSource = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "wasabeef"), bandwidthMeter); // Produces Extractor instances for parsing the media data. ExtractorsFactory extractors = new DefaultExtractorsFactory(); // This is the MediaSource representing the media to be played. MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri, dataSource, extractors, null, null); // Prepare the player with the source. player.prepare(videoSource);

Slide 72

Slide 72 text

HttpDataSource FileDataSource AssetDataSource ContentDataSource DataSource

Slide 73

Slide 73 text

with HLS

Slide 74

Slide 74 text

HLS Encryption

Slide 75

Slide 75 text

A Hollywood grade DRM is not always needed, sometimes it’s enough to just add another layer of security through AES encryption. HLS Encryption

Slide 76

Slide 76 text

#EXTM3U #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:0 HLS Encryption #EXT-X-KEY:METHOD=AES-128,URI="wasabeef:// yehMW2VNXmRAfYpUsYu2W5",IV=0xa0c54559151ced7 76a41893c737dea5f #EXTINF:5, output000.ts #EXT-X-ENDLIST

Slide 77

Slide 77 text

DataSource

Slide 78

Slide 78 text

HttpDataSource FileDataSource AssetDataSource ContentDataSource HLS Encryption - DataSource

Slide 79

Slide 79 text

HLS Encryption - DataSource @Override public long open(DataSpec dataSpec) throws IOException { Assertions.checkState(dataSource == null); // Choose the correct source for the scheme. String scheme = dataSpec.uri.getScheme(); if (Util.isLocalFileUri(dataSpec.uri)) { if (dataSpec.uri.getPath().startsWith("/android_asset/")) { dataSource = assetDataSource; } else { dataSource = fileDataSource; } } else if (SCHEME_ASSET.equals(scheme)) { dataSource = assetDataSource; } else if (SCHEME_CONTENT.equals(scheme)) { dataSource = contentDataSource; } else { dataSource = baseDataSource; } // Open the source and return. return dataSource.open(dataSpec); }

Slide 80

Slide 80 text

HLS Encryption - DataSource @Override public long open(DataSpec dataSpec) throws IOException { Assertions.checkState(dataSource == null); // Choose the correct source for the scheme. String scheme = dataSpec.uri.getScheme(); if (Util.isLocalFileUri(dataSpec.uri)) { if (dataSpec.uri.getPath().startsWith("/android_asset/")) { dataSource = assetDataSource; } else { dataSource = fileDataSource; } } else if (SCHEME_ASSET.equals(scheme)) { dataSource = assetDataSource; } else if (SCHEME_CONTENT.equals(scheme)) { dataSource = contentDataSource; } else if (SCHEME_WASABEEF.equals(scheme)) { dataSource = wasabeefDataSource; } else { dataSource = baseDataSource; } // Open the source and return. return dataSource.open(dataSpec); }

Slide 81

Slide 81 text

ID3v2 Tags

Slide 82

Slide 82 text

The timed metadata information can be used by clients as cuepoints, for information display, to invoke time- aligned actions, and so on. This information is available to the client on the playback timeline in the form of ID3 tags in HLS. ID3v2 Tags

Slide 83

Slide 83 text

MPEG-TS Support TXXX, PRIV, GEOB, APIC, Text Infomation ID3v2 Tags

Slide 84

Slide 84 text

SimpleExoPlayer.java @Override public void onMetadata(List id3Frames) { if (id3Output != null) { id3Output.onMetadata(id3Frames); } }

Slide 85

Slide 85 text

with MPEG-DASH

Slide 86

Slide 86 text

Content Protection Event Stream EMSG with MPEG-DASH

Slide 87

Slide 87 text

Content Protection

Slide 88

Slide 88 text

manifest.mdp <-- Created with Bento4 mp4-dash.py, VERSION=1.7.0-613 --> <-- Video --> <-- Audio -->

Slide 89

Slide 89 text

manifest.mdp

Slide 90

Slide 90 text

Content Protection - DataSource @Override public long open(DataSpec dataSpec) throws IOException { Assertions.checkState(dataSource == null); // Choose the correct source for the scheme. String scheme = dataSpec.uri.getScheme(); if (Util.isLocalFileUri(dataSpec.uri)) { if (dataSpec.uri.getPath().startsWith("/android_asset/")) { dataSource = assetDataSource; } else { dataSource = fileDataSource; } } else if (SCHEME_ASSET.equals(scheme)) { dataSource = assetDataSource; } else if (SCHEME_CONTENT.equals(scheme)) { dataSource = contentDataSource; } else if (SCHEME_WASABEEF.equals(scheme)) { dataSource = wasabeefDataSource; } else { dataSource = baseDataSource; } // Open the source and return. return dataSource.open(dataSpec); }

Slide 91

Slide 91 text

Event Stream

Slide 92

Slide 92 text

N/A... Event Stream

Slide 93

Slide 93 text

EMSG

Slide 94

Slide 94 text

N/A... Event Stream

Slide 95

Slide 95 text

Should I use SurfaceView or TextureView?

Slide 96

Slide 96 text

API Level 1 Hardware rendering Eco-friendly battery Can not overlay two SurfaceView

Slide 97

Slide 97 text

API Level 14 Software rendering Can animate, transform and scale TextureView

Slide 98

Slide 98 text

with DRM

Slide 99

Slide 99 text

PlayReady Widevine (Moduler/Classic) Primetime FairPlay DRM

Slide 100

Slide 100 text

DRM PlayReady Widevine(M) Widevine(C) FairPlay Android (4.3+) ✕ ◯ ◯ ✕ Android (3+) ✕ ✕ ◯ ✕ iOS (6+) ✕ ✕ ✕ ◯ Windows Phone ◯ ✕ ✕ ✕ $ISPNF ✕ ◯ ✕ ✕ 'JSFGPY ✕ ◯ ✕ ✕ *& ◯ ✕ ✕ ✕ Edge (Win10+) ◯ ✕ ✕ ✕ Safari(8+) ✕ ✕ ✕ ◯ Opera (31+) ✕ ◯ ✕ ✕ Chromecast ◯ ◯ ✕ ✕ Android TV ◯ ◯ ✕ ✕ Apple TV ✕ ✕ ✕ ✕ Amazno Fire TV ◯ ✕ ✕ ✕

Slide 101

Slide 101 text

Widevine + ExoPlayer

Slide 102

Slide 102 text

DASH SurfaceView EventStream is not supported. EMSG is not supported. Widevine

Slide 103

Slide 103 text

DRM HttpMediaDrmCallback callback = new HttpMediaDrmCallback(licenseUrl, dataSource, null); FrameworkMediaDrm mediaDrm = FrameworkMediaDrm.newInstance(uuid); DrmSessionManager dsm = new StreamingDrmSessionManager<> (uuid, mediaDrm, callback, null, handler, logger); DefaultLoadControl dlc = new DefaultLoadControl() ExoPlayer player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, dlc, dsm, false);

Slide 104

Slide 104 text

Manabi

Slide 105

Slide 105 text

Let’s buy a solution. Manabi

Slide 106

Slide 106 text

Brightcove INISOFT NEXTSCAPE Android

Slide 107

Slide 107 text

Brightcove Bitmovin THEOplayer Web

Slide 108

Slide 108 text

Conclusion

Slide 109

Slide 109 text

Thank you. twitter.com/wasabeef_jp wasabeef.jp github.com/wasabeef