| 1 | package com.longtailvideo.jwplayer.parsing { |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | import com.longtailvideo.jwplayer.parsing.*; |
|---|
| 5 | import com.longtailvideo.jwplayer.utils.*; |
|---|
| 6 | import flash.events.*; |
|---|
| 7 | import flash.net.*; |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * Parser for Apple HLS manifests. |
|---|
| 12 | * |
|---|
| 13 | * @todo: Support live streaming (reloading / discontinuities / dvrwindow) |
|---|
| 14 | **/ |
|---|
| 15 | public class Manifest extends EventDispatcher { |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | /** Header tag that must be at the first line. **/ |
|---|
| 19 | private const TAG_HEADER:String = '#EXTM3U'; |
|---|
| 20 | /** Starttag for a level. **/ |
|---|
| 21 | private const TAG_LEVEL:String = '#EXT-X-STREAM-INF:'; |
|---|
| 22 | /** Stream identifier bandwidth parameter. **/ |
|---|
| 23 | private const PARAM_BANDWIDTH:String = 'BANDWIDTH'; |
|---|
| 24 | /** Stream identifier bandwidth parameter. **/ |
|---|
| 25 | private const PARAM_RESOLUTION:String = 'RESOLUTION'; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | /** Array with levels. **/ |
|---|
| 29 | public var levels:Array; |
|---|
| 30 | /** Object that fetches the manifest. **/ |
|---|
| 31 | private var _loader:URLLoader; |
|---|
| 32 | /** Link to the M3U8 file. **/ |
|---|
| 33 | private var _url:String; |
|---|
| 34 | /** Amount of playlists still need loading. **/ |
|---|
| 35 | private var _toLoad:Number; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | /** Setup the loader. **/ |
|---|
| 39 | public function Manifest() { |
|---|
| 40 | _loader = new URLLoader(); |
|---|
| 41 | _loader.addEventListener(Event.COMPLETE,_loaderHandler); |
|---|
| 42 | _loader.addEventListener(IOErrorEvent.IO_ERROR,_errorHandler); |
|---|
| 43 | _loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,_errorHandler); |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /** Loading failed; return errors. **/ |
|---|
| 48 | private function _errorHandler(event:ErrorEvent):void { |
|---|
| 49 | dispatchEvent(new ErrorEvent( |
|---|
| 50 | ErrorEvent.ERROR, |
|---|
| 51 | false, |
|---|
| 52 | false, |
|---|
| 53 | event.text |
|---|
| 54 | )); |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | /** Load the manifest file. **/ |
|---|
| 59 | public function load(url:String):void { |
|---|
| 60 | _url = url; |
|---|
| 61 | _toLoad = 0; |
|---|
| 62 | levels = new Array(); |
|---|
| 63 | _loader.load(new URLRequest(_url)); |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | /** A level is loaded and parsed. **/ |
|---|
| 68 | private function _levelHandler(event:Event):void { |
|---|
| 69 | _toLoad--; |
|---|
| 70 | if(_toLoad == 0) { |
|---|
| 71 | dispatchEvent(new Event(Event.COMPLETE)); |
|---|
| 72 | } |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | /** The manifest is loaded. **/ |
|---|
| 77 | private function _loaderHandler(event:Event):void { |
|---|
| 78 | var lines:Array = String(event.target.data).split("\n"); |
|---|
| 79 | if(lines[0] != TAG_HEADER) { |
|---|
| 80 | dispatchEvent(new ErrorEvent( |
|---|
| 81 | ErrorEvent.ERROR, |
|---|
| 82 | false, |
|---|
| 83 | false, |
|---|
| 84 | "First line of the Manifest not equal to #EXTM3U: " + _url |
|---|
| 85 | )); |
|---|
| 86 | } |
|---|
| 87 | var i:Number = 0; |
|---|
| 88 | while (i<lines.length) { |
|---|
| 89 | if(lines[i].indexOf(TAG_LEVEL) == 0) { |
|---|
| 90 | _readLevel(lines,i); |
|---|
| 91 | } |
|---|
| 92 | i++; |
|---|
| 93 | } |
|---|
| 94 | if(levels.length == 0) { |
|---|
| 95 | dispatchEvent(new ErrorEvent( |
|---|
| 96 | ErrorEvent.ERROR, |
|---|
| 97 | false, |
|---|
| 98 | false, |
|---|
| 99 | "No Playlists found in Manifest: " + _url |
|---|
| 100 | )); |
|---|
| 101 | } |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /** Parse a manifest for levels. **/ |
|---|
| 106 | private function _readLevel(lines:Array,index:Number):void { |
|---|
| 107 | var level:Level = new Level(); |
|---|
| 108 | var params:Array = lines[index].substr(TAG_LEVEL.length).split(','); |
|---|
| 109 | for(var j:Number = 0; j<params.length; j++) { |
|---|
| 110 | if(params[j].indexOf(PARAM_BANDWIDTH) == 0) { |
|---|
| 111 | level.bitrate = params[j].split('=')[1]; |
|---|
| 112 | } else if (params[j].indexOf(PARAM_RESOLUTION) == 0) { |
|---|
| 113 | level.height = params[j].split('=')[1].split('x')[1]; |
|---|
| 114 | level.width = params[j].split('=')[1].split('x')[0]; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | levels.push(level); |
|---|
| 118 | var playlist:Playlist = new Playlist(level); |
|---|
| 119 | playlist.addEventListener(ErrorEvent.ERROR, _errorHandler); |
|---|
| 120 | playlist.addEventListener(Event.COMPLETE, _levelHandler); |
|---|
| 121 | var url:String = _gluePaths(lines[index+1],_url); |
|---|
| 122 | playlist.load(url); |
|---|
| 123 | _toLoad++; |
|---|
| 124 | }; |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | /** Glue the playlist path to the manifest path. **/ |
|---|
| 128 | private function _gluePaths(path:String,base:String):String { |
|---|
| 129 | if(path.indexOf('http') == 0) { |
|---|
| 130 | return path; |
|---|
| 131 | } else { |
|---|
| 132 | return base.substr(0,base.lastIndexOf('/') + 1) + path; |
|---|
| 133 | } |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | } |
|---|