| 1 | package com.longtailvideo.jwplayer.media { |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | import com.longtailvideo.jwplayer.events.MediaEvent; |
|---|
| 5 | import com.longtailvideo.jwplayer.media.MediaProvider; |
|---|
| 6 | import com.longtailvideo.jwplayer.model.PlayerConfig; |
|---|
| 7 | import com.longtailvideo.jwplayer.model.PlaylistItem; |
|---|
| 8 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 9 | import com.longtailvideo.jwplayer.muxing.*; |
|---|
| 10 | import com.longtailvideo.jwplayer.utils.*; |
|---|
| 11 | |
|---|
| 12 | import flash.events.*; |
|---|
| 13 | import flash.media.*; |
|---|
| 14 | import flash.net.*; |
|---|
| 15 | import flash.system.*; |
|---|
| 16 | import flash.utils.*; |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | /** JW Player provider for playback of adaptive streams. **/ |
|---|
| 20 | public class AdaptiveProvider extends MediaProvider { |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** Video object to be instantiated. **/ |
|---|
| 24 | private var _video:Video; |
|---|
| 25 | /** NetConnection object for setup of the video _stream. **/ |
|---|
| 26 | private var _connection:NetConnection; |
|---|
| 27 | /** NetStream instance that handles the stream IO. **/ |
|---|
| 28 | private var _stream:NetStream; |
|---|
| 29 | /** Sound control object. **/ |
|---|
| 30 | private var _transformer:SoundTransform; |
|---|
| 31 | /** ID for the position interval. **/ |
|---|
| 32 | private var _positionInterval:Number; |
|---|
| 33 | /** Whether the buffer has filled **/ |
|---|
| 34 | private var _bufferFull:Boolean; |
|---|
| 35 | /** Save the buffer percentage. **/ |
|---|
| 36 | private var _bufferPercent:Number; |
|---|
| 37 | /** URLStream that loads the file. **/ |
|---|
| 38 | private var _urlStream:URLStream; |
|---|
| 39 | /** URLRequest for loading the stream. **/ |
|---|
| 40 | private var _urlRequest:URLRequest; |
|---|
| 41 | /** Reference to the ts. **/ |
|---|
| 42 | private var _ts:TS; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** Constructor; sets up the connection and display. **/ |
|---|
| 46 | public function AdaptiveProvider() { |
|---|
| 47 | super('adaptive'); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | public override function initializeMediaProvider(cfg:PlayerConfig):void { |
|---|
| 52 | super.initializeMediaProvider(cfg); |
|---|
| 53 | _connection = new NetConnection(); |
|---|
| 54 | _connection.connect(null); |
|---|
| 55 | _stream = new NetStream(_connection); |
|---|
| 56 | _stream.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); |
|---|
| 57 | _stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler); |
|---|
| 58 | _stream.bufferTime = config.bufferlength; |
|---|
| 59 | _stream.client = new Object(); |
|---|
| 60 | _transformer = new SoundTransform(); |
|---|
| 61 | _urlStream = new URLStream(); |
|---|
| 62 | _urlStream.addEventListener(Event.COMPLETE, fileLoaded); |
|---|
| 63 | _video = new Video(320,240); |
|---|
| 64 | _video.smoothing = config.smoothing; |
|---|
| 65 | _video.attachNetStream(_stream); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | /** Catch security errors. **/ |
|---|
| 70 | protected function errorHandler(evt:ErrorEvent):void { |
|---|
| 71 | Logger.log(evt.text,provider); |
|---|
| 72 | error(evt.text); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | /** Load content. **/ |
|---|
| 77 | override public function load(itm:PlaylistItem):void { |
|---|
| 78 | media = _video; |
|---|
| 79 | _bufferFull = false; |
|---|
| 80 | _stream.play(null); |
|---|
| 81 | _urlStream.load(new URLRequest(itm.file)); |
|---|
| 82 | _item = itm; |
|---|
| 83 | super.load(itm); |
|---|
| 84 | setState(PlayerState.BUFFERING); |
|---|
| 85 | sendBufferEvent(0); |
|---|
| 86 | streamVolume(config.mute ? 0 : config.volume); |
|---|
| 87 | clearInterval(_positionInterval); |
|---|
| 88 | _positionInterval = setInterval(positionHandler, 200); |
|---|
| 89 | resize(_width, _height); |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | private function fileLoaded(e:Event):void { |
|---|
| 94 | var input:ByteArray = new ByteArray(); |
|---|
| 95 | _urlStream.readBytes(input); |
|---|
| 96 | _stream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN); |
|---|
| 97 | if(item.file.substr(-3) != 'flv') { |
|---|
| 98 | _ts = new TS(input); |
|---|
| 99 | _stream.appendBytes(_ts.data); |
|---|
| 100 | } else { |
|---|
| 101 | _stream.appendBytes(input); |
|---|
| 102 | } |
|---|
| 103 | _bufferPercent = 100; |
|---|
| 104 | sendBufferEvent(100, 0, {}); |
|---|
| 105 | _bufferFull = true; |
|---|
| 106 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL); |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** Pause playback. **/ |
|---|
| 111 | override public function pause():void { |
|---|
| 112 | _stream.pause(); |
|---|
| 113 | super.pause(); |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /** Resume playing. **/ |
|---|
| 118 | override public function play():void { |
|---|
| 119 | if (!_positionInterval) { |
|---|
| 120 | _positionInterval = setInterval(positionHandler, 100); |
|---|
| 121 | } |
|---|
| 122 | if (_bufferFull) { |
|---|
| 123 | _stream.resume(); |
|---|
| 124 | super.play(); |
|---|
| 125 | } else { |
|---|
| 126 | setState(PlayerState.BUFFERING); |
|---|
| 127 | } |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | /** Interval for the position progress **/ |
|---|
| 132 | protected function positionHandler():void { |
|---|
| 133 | var pos:Number = Math.round(_stream.time * 10) / 10; |
|---|
| 134 | if (state == PlayerState.PLAYING && pos > 0) { |
|---|
| 135 | _position = pos; |
|---|
| 136 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: position, duration: item.duration}); |
|---|
| 137 | if (item.duration > 0 && _position >= item.duration - 0.2) { |
|---|
| 138 | stop(); |
|---|
| 139 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | }; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | /** Seek to a new position. **/ |
|---|
| 146 | override public function seek(pos:Number):void { |
|---|
| 147 | /* Small files; seeking not very useful */ |
|---|
| 148 | }; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | /** Destroy the video. **/ |
|---|
| 152 | override public function stop():void { |
|---|
| 153 | _stream.seek(0); |
|---|
| 154 | _stream.pause(); |
|---|
| 155 | _bufferFull = false; |
|---|
| 156 | _bufferPercent = 0; |
|---|
| 157 | clearInterval(_positionInterval); |
|---|
| 158 | super.stop(); |
|---|
| 159 | }; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | /** Set the volume level. **/ |
|---|
| 163 | override public function setVolume(vol:Number):void { |
|---|
| 164 | streamVolume(vol); |
|---|
| 165 | super.setVolume(vol); |
|---|
| 166 | var fr:FileReference = new FileReference( ); |
|---|
| 167 | fr.save(_ts.data,'export.flv'); |
|---|
| 168 | }; |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | /** Set the stream's volume, without sending a volume event **/ |
|---|
| 172 | protected function streamVolume(level:Number):void { |
|---|
| 173 | _transformer.volume = level / 100; |
|---|
| 174 | if (_stream) { |
|---|
| 175 | _stream.soundTransform = _transformer; |
|---|
| 176 | } |
|---|
| 177 | }; |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | } |
|---|
| 181 | } |
|---|