PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/media-element/src/flash/htmlelements/HLSMediaElement.as

https://gitlab.com/Gashler/sg
ActionScript | 267 lines | 224 code | 34 blank | 9 comment | 19 complexity | be62fc5a7e17de9d14a04f9e4aa68fdf MD5 | raw file
  1. package htmlelements {
  2. import flash.display.Sprite;
  3. import flash.media.Video;
  4. import flash.media.SoundTransform;
  5. import org.mangui.hls.HLS;
  6. import org.mangui.hls.HLSSettings;
  7. import org.mangui.hls.event.HLSEvent;
  8. import org.mangui.hls.constant.HLSPlayStates;
  9. import org.mangui.hls.utils.Log;
  10. public class HLSMediaElement extends Sprite implements IMediaElement {
  11. private var _element:FlashMediaElement;
  12. private var _playqueued:Boolean = false;
  13. private var _autoplay:Boolean = true;
  14. private var _preload:String = "";
  15. private var _hls:HLS;
  16. private var _url:String;
  17. private var _video:Video;
  18. private var _hlsState:String = HLSPlayStates.IDLE;
  19. // event values
  20. private var _position:Number = 0;
  21. private var _duration:Number = 0;
  22. private var _framerate:Number;
  23. private var _isManifestLoaded:Boolean = false;
  24. private var _isPaused:Boolean = true;
  25. private var _isEnded:Boolean = false;
  26. private var _volume:Number = 1;
  27. private var _isMuted:Boolean = false;
  28. private var _bytesLoaded:Number = 0;
  29. private var _bytesTotal:Number = 0;
  30. private var _bufferedTime:Number = 0;
  31. private var _bufferEmpty:Boolean = false;
  32. private var _bufferingChanged:Boolean = false;
  33. private var _seekOffset:Number = 0;
  34. private var _videoWidth:Number = -1;
  35. private var _videoHeight:Number = -1;
  36. public function HLSMediaElement(element:FlashMediaElement, autoplay:Boolean, preload:String, timerRate:Number, startVolume:Number) {
  37. _element = element;
  38. _autoplay = autoplay;
  39. _volume = startVolume;
  40. _preload = preload;
  41. _video = new Video();
  42. addChild(_video);
  43. //HLSSettings.logDebug = true;
  44. _hls = new HLS();
  45. _hls.addEventListener(HLSEvent.PLAYBACK_COMPLETE,_completeHandler);
  46. _hls.addEventListener(HLSEvent.ERROR,_errorHandler);
  47. _hls.addEventListener(HLSEvent.MANIFEST_LOADED,_manifestHandler);
  48. _hls.addEventListener(HLSEvent.MEDIA_TIME,_mediaTimeHandler);
  49. _hls.addEventListener(HLSEvent.PLAYBACK_STATE,_stateHandler);
  50. _hls.addEventListener(HLSEvent.FRAGMENT_PLAYING,_fragmentHandler);
  51. _hls.stream.soundTransform = new SoundTransform(_volume);
  52. _video.attachNetStream(_hls.stream);
  53. }
  54. private function _completeHandler(event:HLSEvent):void {
  55. _isEnded = true;
  56. _isPaused = true;
  57. sendEvent(HtmlMediaEvent.PAUSE);
  58. sendEvent(HtmlMediaEvent.ENDED);
  59. };
  60. private function _errorHandler(event:HLSEvent):void {
  61. _element.logMessage(event.toString());
  62. };
  63. private function _fragmentHandler(event:HLSEvent):void {
  64. sendEvent(HtmlMediaEvent.FRAGMENT_PLAYING);
  65. };
  66. private function _manifestHandler(event:HLSEvent):void {
  67. _duration = event.levels[0].duration;
  68. _videoWidth = event.levels[0].width;
  69. _videoHeight = event.levels[0].height;
  70. _isManifestLoaded = true;
  71. _hls.stage = _video.stage;
  72. sendEvent(HtmlMediaEvent.LOADEDMETADATA);
  73. sendEvent(HtmlMediaEvent.CANPLAY);
  74. if(_autoplay || _playqueued) {
  75. _playqueued = false;
  76. _hls.stream.play();
  77. }
  78. };
  79. private function _mediaTimeHandler(event:HLSEvent):void {
  80. _position = event.mediatime.position;
  81. _duration = event.mediatime.duration;
  82. _bufferedTime = event.mediatime.buffer+event.mediatime.position;
  83. sendEvent(HtmlMediaEvent.PROGRESS);
  84. sendEvent(HtmlMediaEvent.TIMEUPDATE);
  85. };
  86. private function _stateHandler(event:HLSEvent):void {
  87. _hlsState = event.state;
  88. //Log.txt("state:"+ _hlsState);
  89. switch(event.state) {
  90. case HLSPlayStates.IDLE:
  91. break;
  92. case HLSPlayStates.PAUSED_BUFFERING:
  93. case HLSPlayStates.PLAYING_BUFFERING:
  94. _isPaused = true;
  95. sendEvent(HtmlMediaEvent.BUFFERING);
  96. break;
  97. case HLSPlayStates.PLAYING:
  98. _isPaused = false;
  99. _isEnded = false;
  100. _video.visible = true;
  101. sendEvent(HtmlMediaEvent.LOADEDDATA);
  102. sendEvent(HtmlMediaEvent.PLAY);
  103. sendEvent(HtmlMediaEvent.PLAYING);
  104. break;
  105. case HLSPlayStates.PAUSED:
  106. _isPaused = true;
  107. _isEnded = false;
  108. sendEvent(HtmlMediaEvent.PAUSE);
  109. sendEvent(HtmlMediaEvent.CANPLAY);
  110. break;
  111. }
  112. };
  113. public function get video():Video {
  114. return _video;
  115. }
  116. public function get videoHeight():Number {
  117. return _videoHeight;
  118. }
  119. public function get videoWidth():Number {
  120. return _videoWidth;
  121. }
  122. public function play():void {
  123. //Log.txt("HLSMediaElement:play");
  124. if(!_isManifestLoaded) {
  125. _playqueued = true;
  126. return;
  127. }
  128. if (_hlsState == HLSPlayStates.PAUSED || _hlsState == HLSPlayStates.PAUSED_BUFFERING) {
  129. _hls.stream.resume();
  130. } else {
  131. _hls.stream.play();
  132. }
  133. }
  134. public function pause():void {
  135. if(!_isManifestLoaded)
  136. return;
  137. //Log.txt("HLSMediaElement:pause");
  138. _hls.stream.pause();
  139. }
  140. public function load():void{
  141. //Log.txt("HLSMediaElement:load");
  142. if(_url) {
  143. sendEvent(HtmlMediaEvent.LOADSTART);
  144. _hls.load(_url);
  145. }
  146. }
  147. public function stop():void{
  148. _hls.stream.close();
  149. _video.clear();
  150. _isManifestLoaded = false;
  151. _duration = 0;
  152. _position = 0;
  153. _playqueued = false;
  154. sendEvent(HtmlMediaEvent.STOP);
  155. }
  156. public function setSrc(url:String):void{
  157. //Log.txt("HLSMediaElement:setSrc:"+url);
  158. stop();
  159. _url = url;
  160. _hls.load(_url);
  161. }
  162. public function setSize(width:Number, height:Number):void{
  163. _video.width = width;
  164. _video.height = height;
  165. }
  166. public function setCurrentTime(pos:Number):void{
  167. if(!_isManifestLoaded)
  168. return;
  169. sendEvent(HtmlMediaEvent.SEEKING);
  170. _hls.stream.seek(pos);
  171. }
  172. public function setVolume(vol:Number):void{
  173. _volume = vol;
  174. _isMuted = (_volume == 0);
  175. _hls.stream.soundTransform = new SoundTransform(vol);
  176. sendEvent(HtmlMediaEvent.VOLUMECHANGE);
  177. }
  178. public function getVolume():Number {
  179. if(_isMuted) {
  180. return 0;
  181. } else {
  182. return _volume;
  183. }
  184. }
  185. public function setMuted(muted:Boolean):void {
  186. // ignore if no change
  187. if (muted === _isMuted)
  188. return;
  189. _isMuted = muted;
  190. if (muted) {
  191. _hls.stream.soundTransform = new SoundTransform(0);
  192. sendEvent(HtmlMediaEvent.VOLUMECHANGE);
  193. } else {
  194. setVolume(_volume);
  195. }
  196. }
  197. public function duration():Number{
  198. return _duration;
  199. }
  200. public function currentTime():Number{
  201. return _position;
  202. }
  203. public function seekLimit():Number {
  204. return _duration;
  205. }
  206. public function currentProgress():Number {
  207. var progress:Number = 0;
  208. if (_duration != 0) {
  209. progress = Math.round( (_bufferedTime / _duration) * 100 );
  210. }
  211. return progress;
  212. }
  213. private function sendEvent(eventName:String):void {
  214. // build JSON
  215. var values:String =
  216. "duration:" + _duration +
  217. ",framerate:" + _hls.stream.currentFPS +
  218. ",currentTime:" + _position +
  219. ",muted:" + _isMuted +
  220. ",paused:" + _isPaused +
  221. ",ended:" + _isEnded +
  222. ",volume:" + _volume +
  223. ",src:\"" + _url + "\"" +
  224. ",bytesTotal:" + Math.round(1000*_duration) +
  225. ",bufferedBytes:" + Math.round(1000*(_position+_bufferedTime)) +
  226. ",bufferedTime:" + _bufferedTime +
  227. ",videoWidth:" + _videoWidth +
  228. ",videoHeight:" + _videoHeight +
  229. "";
  230. _element.sendEvent(eventName, values);
  231. }
  232. }
  233. }