Video Stream on Android (DevFest Tokyo 2016)

Video Stream on Android (DevFest Tokyo 2016)

Introduction to Video Stream
- Http Live Streaming (HLS)
- Dynamic Adaptive Streaming over HTTP (DASH)
- H.264
- H.265
- Fragmented MP4
- ExoPlayer
- DRM (Widevine, PlayReady)

6dd0483f1353a4a359e92633cfd65c64?s=128

Daichi Furiya (Wasabeef)

October 09, 2016
Tweet

Transcript

  1. Video Streaming on Android wasabeef

  2. About me 降矢 大地 (Daichi Furiya) Google Developer Expert AbemaTV,

    Inc. wasabeef @wasabeef_jp
  3. Streaming

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

  5. HLS MPEG-DASH Microsoft Smooth Streaming RTMP Streaming?

  6. Video compression

  7. Video compression 30fps 1sec 60KB × 30 = 1.8MB 1min

    1.8MB × 60 = 108MB 1hour 108MB × 10 = 1.1GB 60KB 65KB 55KB 50KB
  8. Video compression 60KB 5KB 2KB 7KB 30fps 1sec 60KB +

    (4KB × 30) = 180KB 1min 60KB + (4KB × 1800) = 7.2MB 1hour 7.2MB × 10 = 72MB
  9. Video compression Be impossible ;(

  10. Key Frame Be possible :)

  11. Video structure Video Audio Meta Butterfly…? Discovered

  12. Format

  13. Container format

  14. .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
  15. Video codec

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

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

  18. Audio codec

  19. MP3 AAC PCM Vorbis Audio Codec

  20. HLS

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

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

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

  24. playlist.m3u8

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

  26. 720p

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

  28. # 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
  29. Converting MP4 to HLS With FFmpeg

  30. ts?

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

  32. # python -m SimpleHTTPServer Web Server

  33. #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
  34. playlist.m3u8 Segment1.ts Length: 10sec Segment2.ts Length: 10sec Segment3.ts Length: 10sec

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

    Segment3.ts Length: 10sec Header Footer
  36. #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
  37. playlist.m3u8 playlist1.m3u8 BANDWIDTH: 200000 Header playlist2.m3u8 BANDWIDTH: 311111 playlist3.m3u8 BANDWIDTH:

    484444 playlist4.m3u8 BANDWIDTH: 737777
  38. MPEG-DASH

  39. Microsoft, Netflix, Google, Ericsson, Samsung, Adobe Adaptive bitrate streaming fMP4,

    WebM, MPEG2-TS MPEG-DASH
  40. manifest.mdp

  41. manifest.mdp <?xml version="1.0" ?> <MPD mediaPresentationDuration="PT9M56.458S" minBufferTime="PT20.00S" profiles="urn:mpeg:dash:profile:isoff-live:2011" type="static" xmlns="urn:mpeg:dash:schema:mpd:2011">

    <-- Created with Bento4 mp4-dash.py, VERSION=1.7.0-613 --> <Period> <-- Video --> <AdaptationSet maxHeight="180" maxWidth="320" mimeType="video/mp4" minHeight="180" minWidth="320" segmentAlignment="true" startWithSAP="1"> <ContentProtection schemeIdUri="urn:mpeg:dash:abmprotection:2016" value="0x55ea5d616e90eaca972bc31a9fb2c7f8,0xaccca4b41de3d9afb029070eb564be40"> </ContentProtection> <SegmentTemplate duration="20000" initialization="$/init.mp4" media="$/seg-$.m4s" startNumber="1" timescale="1000"/> <Representation bandwidth="702842" codecs="avc1.42C00D" frameRate="24" height="180" id="../video_enc/1" scanType="progressive" width="320"/> </AdaptationSet> <-- Audio --> <AdaptationSet mimeType="audio/mp4" segmentAlignment="true" startWithSAP="1"> <SegmentTemplate duration="20000" initialization="$/init.mp4" media="$/seg-$.m4s" startNumber="1" timescale="1000"/> <Representation audioSamplingRate="48000" bandwidth="163859" codecs="mp4a.40.2" id="../audio/und/mp4a"> <AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/> </Representation> </AdaptationSet> </Period> </MPD>
  42. <mdp> - Root manifest.mdp <period> - A start time and

    duration. <AdaptationSet> - Audio/Video content <Representation> - Bitrate and Aspect
  43. Android

  44. Android Media APIs

  45. AudioTrack MediaPlayer JetPlayer SoundPool Android Media APIs

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

  47. MediaPlayer MediaExtractor MediaCodec AudioTrack MediaPlayer Network Buffering Extraction Decoding Rendering

  48. 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();
  49. ExoPlayer

  50. API Level 16 Easy to extend and customize HLS, MPEG-DASH,

    SmoothStreaming DRM (WideVine, PlayReady) ExoPlayer
  51. Supported Devices/Formats

  52. Devices Use Case Version API Level Audio 4.1 16 Video

    4.1 16 WideVine 4.3 18 PlayReady AndroidTV AndroidTV
  53. Formats (Containers) Feature HLS MPEG-DASH fMP4 ✕ ◯ WebM ✕

    ◯ Matroska ✕ ◯ MPEG-TS ◯ ✕ ADTS(AAC) ◯ ✕ MP3 ◯ ✕
  54. Formats (Closed Caption) Feature HLS MPEG-DASH TTML ✕ ◯ WebVTT

    ◯ ◯ Tx3g ✕ ◯ SubRip ✕ ◯ EIA-608 ◯ ✕
  55. Formats (Meta) Feature HLS MPEG-DASH ID3 Tags ◯ ✕

  56. Formats (Content Protection) Feature HLS MPEG-DASH AES-128 ◯ ✕ WideVine

    ✕ ◯ PlayReady ◯ △ (AndroidTV)
  57. 2.x

  58. 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
  59. ExoPlayer MediaSource Parser Extractor DataSource MediaCodecRenderer MediaCodec AudioTrack ExoPlayer Network

    Buffering Extraction Decoding Rendering
  60. MediaSource

  61. HlsMediaSource DashMediaSource SsMediaSource ExtractorMediaSource MediaSource

  62. MergingMediaSource LoopingMediaSource ConcatenatingMediaSource MediaSource

  63. 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);
  64. Parser

  65. #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
  66. HlsPlaylistParser DashManifestParser SsManifestParser Parser

  67. 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"; // ...
  68. Extractor

  69. TsExtractor FragmentedMp4Extractor Mp4Extractor FlvExtractor Extractor

  70. DataSource

  71. 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);
  72. HttpDataSource FileDataSource AssetDataSource ContentDataSource DataSource

  73. with HLS

  74. HLS Encryption

  75. A Hollywood grade DRM is not always needed, sometimes it’s

    enough to just add another layer of security through AES encryption. HLS Encryption
  76. #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
  77. DataSource

  78. HttpDataSource FileDataSource AssetDataSource ContentDataSource HLS Encryption - DataSource

  79. 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); }
  80. 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); }
  81. ID3v2 Tags

  82. 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
  83. MPEG-TS Support TXXX, PRIV, GEOB, APIC, Text Infomation ID3v2 Tags

  84. SimpleExoPlayer.java @Override public void onMetadata(List<Id3Frame> id3Frames) { if (id3Output !=

    null) { id3Output.onMetadata(id3Frames); } }
  85. with MPEG-DASH

  86. Content Protection Event Stream EMSG with MPEG-DASH

  87. Content Protection

  88. manifest.mdp <?xml version="1.0" ?> <MPD mediaPresentationDuration="PT9M56.458S" minBufferTime="PT20.00S" profiles="urn:mpeg:dash:profile:isoff-live:2011" type="static" xmlns="urn:mpeg:dash:schema:mpd:2011">

    <-- Created with Bento4 mp4-dash.py, VERSION=1.7.0-613 --> <Period> <-- Video --> <AdaptationSet maxHeight="180" maxWidth="320" mimeType="video/mp4" minHeight="180" minWidth="320" segmentAlignment="true" startWithSAP="1"> <ContentProtection schemeIdUri="urn:mpeg:dash:abmprotection:2016" value="0x55ea5d616e90eaca972bc31a9fb2c7f8,0xaccca4b41de3d9afb029070eb564be40"> </ContentProtection> <SegmentTemplate duration="20000" initialization="$/init.mp4" media="$/seg-$.m4s" startNumber="1" timescale="1000"/> <Representation bandwidth="702842" codecs="avc1.42C00D" frameRate="24" height="180" id="../video_enc/1" scanType="progressive" width="320"/> </AdaptationSet> <-- Audio --> <AdaptationSet mimeType="audio/mp4" segmentAlignment="true" startWithSAP="1"> <SegmentTemplate duration="20000" initialization="$/init.mp4" media="$/seg-$.m4s" startNumber="1" timescale="1000"/> <Representation audioSamplingRate="48000" bandwidth="163859" codecs="mp4a.40.2" id="../audio/und/mp4a"> <AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/> </Representation> </AdaptationSet> </Period> </MPD>
  89. manifest.mdp <ContentProtection schemeIdUri=“urn:mpeg:dash:abmprotection:2016" value=“0x55ea5ak…, 0xaccca4…”> </ContentProtection>

  90. 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); }
  91. Event Stream

  92. N/A... Event Stream

  93. EMSG

  94. N/A... Event Stream

  95. Should I use SurfaceView or TextureView?

  96. API Level 1 Hardware rendering Eco-friendly battery Can not overlay

    two SurfaceView
  97. API Level 14 Software rendering Can animate, transform and scale

    TextureView
  98. with DRM

  99. PlayReady Widevine (Moduler/Classic) Primetime FairPlay DRM

  100. 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 ◯ ✕ ✕ ✕
  101. Widevine + ExoPlayer

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

    Widevine
  103. DRM HttpMediaDrmCallback callback = new HttpMediaDrmCallback(licenseUrl, dataSource, null); FrameworkMediaDrm mediaDrm

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

  105. Let’s buy a solution. Manabi

  106. Brightcove INISOFT NEXTSCAPE Android

  107. Brightcove Bitmovin THEOplayer Web

  108. Conclusion

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