| 1 | /** |
|---|
| 2 | * Wrapper for playback of progressively downloaded video. |
|---|
| 3 | **/ |
|---|
| 4 | package com.longtailvideo.jwplayer.media { |
|---|
| 5 | import com.longtailvideo.jwplayer.events.MediaEvent; |
|---|
| 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.utils.NetClient; |
|---|
| 10 | |
|---|
| 11 | import flash.events.*; |
|---|
| 12 | import flash.media.*; |
|---|
| 13 | import flash.net.*; |
|---|
| 14 | import flash.utils.*; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | public class VideoMediaProvider extends MediaProvider { |
|---|
| 18 | /** Video object to be instantiated. **/ |
|---|
| 19 | protected var video:Video; |
|---|
| 20 | /** NetConnection object for setup of the video stream. **/ |
|---|
| 21 | protected var connection:NetConnection; |
|---|
| 22 | /** NetStream instance that handles the stream IO. **/ |
|---|
| 23 | protected var stream:NetStream; |
|---|
| 24 | /** Sound control object. **/ |
|---|
| 25 | protected var transformer:SoundTransform; |
|---|
| 26 | /** ID for the position interval. **/ |
|---|
| 27 | protected var interval:Number; |
|---|
| 28 | /** Interval ID for the loading. **/ |
|---|
| 29 | protected var loadinterval:Number; |
|---|
| 30 | /** Load offset for bandwidth checking. **/ |
|---|
| 31 | protected var loadtimer:Number; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /** Constructor; sets up the connection and display. **/ |
|---|
| 35 | public function VideoMediaProvider() { |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | public override function initializeMediaProvider(cfg:PlayerConfig):void { |
|---|
| 39 | super.initializeMediaProvider(cfg); |
|---|
| 40 | _provider = 'video'; |
|---|
| 41 | connection = new NetConnection(); |
|---|
| 42 | connection.connect(null); |
|---|
| 43 | stream = new NetStream(connection); |
|---|
| 44 | stream.addEventListener(NetStatusEvent.NET_STATUS, statusHandler); |
|---|
| 45 | stream.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); |
|---|
| 46 | stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler); |
|---|
| 47 | stream.bufferTime = _config.bufferlength; |
|---|
| 48 | stream.client = new NetClient(this); |
|---|
| 49 | video = new Video(320, 240); |
|---|
| 50 | video.smoothing = _config.smoothing; |
|---|
| 51 | video.attachNetStream(stream); |
|---|
| 52 | transformer = new SoundTransform(); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | /** Catch security errors. **/ |
|---|
| 57 | protected function errorHandler(evt:ErrorEvent):void { |
|---|
| 58 | stop(); |
|---|
| 59 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: evt.text}); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /** Load content. **/ |
|---|
| 64 | override public function load(itm:PlaylistItem):void { |
|---|
| 65 | _item = itm; |
|---|
| 66 | media = video; |
|---|
| 67 | stream.checkPolicyFile = true; |
|---|
| 68 | stream.play(item.file); |
|---|
| 69 | //stream.pause(); |
|---|
| 70 | interval = setInterval(positionInterval, 100); |
|---|
| 71 | loadinterval = setInterval(loadHandler, 200); |
|---|
| 72 | // TODO: Moved up load event |
|---|
| 73 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED); |
|---|
| 74 | _config.mute == true ? setVolume(0) : setVolume(_config.volume); |
|---|
| 75 | setState(PlayerState.BUFFERING); |
|---|
| 76 | sendBufferEvent(0); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | /** Interval for the loading progress **/ |
|---|
| 81 | protected function loadHandler():void { |
|---|
| 82 | var ldd:Number = stream.bytesLoaded; |
|---|
| 83 | var ttl:Number = stream.bytesTotal; |
|---|
| 84 | try { |
|---|
| 85 | sendBufferEvent(Math.round(ldd / ttl * 100)); |
|---|
| 86 | } catch (err:Error) { |
|---|
| 87 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: err.getStackTrace()}); |
|---|
| 88 | } |
|---|
| 89 | if (ldd == ttl && ldd > 0) { |
|---|
| 90 | clearInterval(loadinterval); |
|---|
| 91 | } |
|---|
| 92 | if (!loadtimer) { |
|---|
| 93 | loadtimer = setTimeout(loadTimeout, 3000); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /** timeout for checking the bitrate. **/ |
|---|
| 99 | protected function loadTimeout():void { |
|---|
| 100 | var obj:Object = new Object(); |
|---|
| 101 | obj.bandwidth = Math.round(stream.bytesLoaded / 1024 / 3 * 8); |
|---|
| 102 | if (item.duration) { |
|---|
| 103 | obj.bitrate = Math.round(stream.bytesTotal / 1024 * 8 / item.duration); |
|---|
| 104 | } |
|---|
| 105 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, {metadata: obj}); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | /** Get metadata information from netstream class. **/ |
|---|
| 110 | public function onData(dat:Object):void { |
|---|
| 111 | if (dat.width) { |
|---|
| 112 | video.width = dat.width; |
|---|
| 113 | video.height = dat.height; |
|---|
| 114 | } |
|---|
| 115 | if (dat.duration) { |
|---|
| 116 | _item.duration = dat.duration; |
|---|
| 117 | } |
|---|
| 118 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, {metadata: dat}); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /** Pause playback. **/ |
|---|
| 123 | override public function pause():void { |
|---|
| 124 | stream.pause(); |
|---|
| 125 | clearInterval(interval); |
|---|
| 126 | super.pause(); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | /** Resume playing. **/ |
|---|
| 131 | override public function play():void { |
|---|
| 132 | stream.resume(); |
|---|
| 133 | interval = setInterval(positionInterval, 100); |
|---|
| 134 | super.play(); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | /** Interval for the position progress **/ |
|---|
| 139 | protected function positionInterval():void { |
|---|
| 140 | _position = Math.round(stream.time * 10) / 10; |
|---|
| 141 | var bfr:Number = Math.round(stream.bufferLength / stream.bufferTime * 100); |
|---|
| 142 | if (bfr < 95 && position < Math.abs(item.duration - stream.bufferTime - 1)) { |
|---|
| 143 | if (state == PlayerState.PLAYING && bfr < 25) { |
|---|
| 144 | setState(PlayerState.BUFFERING); |
|---|
| 145 | } |
|---|
| 146 | sendBufferEvent(bfr); |
|---|
| 147 | } else if (bfr > 95 && state == PlayerState.BUFFERING) { |
|---|
| 148 | super.play(); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | if (position < item.duration) { |
|---|
| 152 | if (state == PlayerState.PLAYING && position >= 0) { |
|---|
| 153 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: position, duration: item.duration}); |
|---|
| 154 | } |
|---|
| 155 | } else if (item.duration > 0) { |
|---|
| 156 | stream.pause(); |
|---|
| 157 | clearInterval(interval); |
|---|
| 158 | setState(PlayerState.IDLE); |
|---|
| 159 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | protected function bufferUnderrun(bufferPercent:Number):Boolean { |
|---|
| 164 | return (state == PlayerState.PLAYING && bufferPercent < 25 && (bufferPercent < 95 && position < Math.abs(item.duration - stream.bufferTime - 1))); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | protected function bufferFull(bufferPercent:Number):Boolean { |
|---|
| 168 | return (bufferPercent > 95 && state == PlayerState.BUFFERING); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | /** Seek to a new position. **/ |
|---|
| 173 | override public function seek(pos:Number):void { |
|---|
| 174 | super.seek(pos); |
|---|
| 175 | clearInterval(interval); |
|---|
| 176 | stream.seek(position); |
|---|
| 177 | play(); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | /** Receive NetStream status updates. **/ |
|---|
| 182 | protected function statusHandler(evt:NetStatusEvent):void { |
|---|
| 183 | switch (evt.info.code) { |
|---|
| 184 | case "NetStream.Play.Stop": |
|---|
| 185 | if (position > 1) { |
|---|
| 186 | clearInterval(interval); |
|---|
| 187 | setState(PlayerState.IDLE); |
|---|
| 188 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE); |
|---|
| 189 | } |
|---|
| 190 | break; |
|---|
| 191 | case "NetStream.Play.StreamNotFound": |
|---|
| 192 | stop(); |
|---|
| 193 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: 'Video not found or access denied: ' + item.file}); |
|---|
| 194 | break; |
|---|
| 195 | } |
|---|
| 196 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, {metadata: {status: evt.info.code}}); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | /** Destroy the video. **/ |
|---|
| 201 | override public function stop():void { |
|---|
| 202 | if (stream.bytesLoaded < stream.bytesTotal) { |
|---|
| 203 | stream.close(); |
|---|
| 204 | } else { |
|---|
| 205 | stream.pause(); |
|---|
| 206 | } |
|---|
| 207 | loadtimer = undefined; |
|---|
| 208 | clearInterval(loadinterval); |
|---|
| 209 | clearInterval(interval); |
|---|
| 210 | super.stop(); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | /** Set the volume level. **/ |
|---|
| 215 | override public function setVolume(vol:Number):void { |
|---|
| 216 | transformer.volume = vol / 100; |
|---|
| 217 | stream.soundTransform = transformer; |
|---|
| 218 | super.setVolume(vol); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | } |
|---|