| 1 | /** |
|---|
| 2 | * Wrapper for playback of progressively downloaded video. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.models { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.events.*; |
|---|
| 8 | import com.jeroenwijering.models.ModelInterface; |
|---|
| 9 | import com.jeroenwijering.player.Model; |
|---|
| 10 | import com.jeroenwijering.utils.NetClient; |
|---|
| 11 | import flash.events.*; |
|---|
| 12 | import flash.display.DisplayObject; |
|---|
| 13 | import flash.media.SoundTransform; |
|---|
| 14 | import flash.media.Video; |
|---|
| 15 | import flash.net.*; |
|---|
| 16 | import flash.utils.clearInterval; |
|---|
| 17 | import flash.utils.setInterval; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | public class VideoModel implements ModelInterface { |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** reference to the model. **/ |
|---|
| 24 | private var model:Model; |
|---|
| 25 | /** Video object to be instantiated. **/ |
|---|
| 26 | private var video:Video; |
|---|
| 27 | /** NetConnection object for setup of the video stream. **/ |
|---|
| 28 | private var connection:NetConnection; |
|---|
| 29 | /** NetStream instance that handles the stream IO. **/ |
|---|
| 30 | private var stream:NetStream; |
|---|
| 31 | /** Sound control object. **/ |
|---|
| 32 | private var transform:SoundTransform; |
|---|
| 33 | /** Interval ID for the time. **/ |
|---|
| 34 | private var timeinterval:Number; |
|---|
| 35 | /** Interval ID for the loading. **/ |
|---|
| 36 | private var loadinterval:Number; |
|---|
| 37 | /** Metadata received switch. **/ |
|---|
| 38 | private var metadata:Boolean; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /** Constructor; sets up the connection and display. **/ |
|---|
| 42 | public function VideoModel(mod:Model):void { |
|---|
| 43 | model = mod; |
|---|
| 44 | connection = new NetConnection(); |
|---|
| 45 | connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler); |
|---|
| 46 | connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler); |
|---|
| 47 | connection.objectEncoding = ObjectEncoding.AMF0; |
|---|
| 48 | connection.connect(null); |
|---|
| 49 | stream = new NetStream(connection); |
|---|
| 50 | stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler); |
|---|
| 51 | stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler); |
|---|
| 52 | stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler); |
|---|
| 53 | stream.bufferTime = model.config['bufferlength']; |
|---|
| 54 | stream.client = new NetClient(this); |
|---|
| 55 | video = new Video(320,240); |
|---|
| 56 | video.attachNetStream(stream); |
|---|
| 57 | transform = new SoundTransform(); |
|---|
| 58 | stream.soundTransform = transform; |
|---|
| 59 | quality(model.config['quality']); |
|---|
| 60 | model.config['mute'] == true ? volume(0): volume(model.config['volume']); |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /** Catch security errors. **/ |
|---|
| 65 | private function errorHandler(evt:ErrorEvent):void { |
|---|
| 66 | model.sendEvent(ModelEvent.ERROR,{message:evt.text}); |
|---|
| 67 | }; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | /** Load content. **/ |
|---|
| 71 | public function load():void { |
|---|
| 72 | video.clear(); |
|---|
| 73 | model.mediaHandler(video); |
|---|
| 74 | stream.play(model.playlist[model.config['item']]['file']); |
|---|
| 75 | loadinterval = setInterval(loadHandler,200); |
|---|
| 76 | timeinterval = setInterval(timeHandler,100); |
|---|
| 77 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING}); |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /** Interval for the loading progress **/ |
|---|
| 82 | private function loadHandler():void { |
|---|
| 83 | var ldd = stream.bytesLoaded; |
|---|
| 84 | var ttl = stream.bytesTotal; |
|---|
| 85 | model.sendEvent(ModelEvent.LOADED,{loaded:ldd,total:ttl}); |
|---|
| 86 | if(ldd == ttl && ldd > 0) { |
|---|
| 87 | clearInterval(loadinterval); |
|---|
| 88 | } |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | /** Catch noncritical errors. **/ |
|---|
| 93 | private function metaHandler(evt:ErrorEvent):void { |
|---|
| 94 | model.sendEvent(ModelEvent.META,{error:evt.text}); |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /** Get metadata information from netstream class. **/ |
|---|
| 99 | public function onData(dat:Object):void { |
|---|
| 100 | if(dat.type == 'metadata' && !metadata) { |
|---|
| 101 | metadata = true; |
|---|
| 102 | if(dat.width) { |
|---|
| 103 | video.width = dat.width; |
|---|
| 104 | video.height = dat.height; |
|---|
| 105 | } |
|---|
| 106 | if(model.playlist[model.config['item']]['start'] > 0) { |
|---|
| 107 | seek(model.playlist[model.config['item']]['start']); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | model.sendEvent(ModelEvent.META,dat); |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | /** Pause playback. **/ |
|---|
| 115 | public function pause():void { |
|---|
| 116 | clearInterval(timeinterval); |
|---|
| 117 | stream.pause(); |
|---|
| 118 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PAUSED}); |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /** Resume playing. **/ |
|---|
| 123 | public function play():void { |
|---|
| 124 | stream.resume(); |
|---|
| 125 | timeinterval = setInterval(timeHandler,100); |
|---|
| 126 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING}); |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | /** Change the smoothing mode. **/ |
|---|
| 131 | public function quality(qua:Boolean):void { |
|---|
| 132 | if(qua == true) { |
|---|
| 133 | video.smoothing = true; |
|---|
| 134 | video.deblocking = 3; |
|---|
| 135 | } else { |
|---|
| 136 | video.smoothing = false; |
|---|
| 137 | video.deblocking = 1; |
|---|
| 138 | } |
|---|
| 139 | }; |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | /** Change the smoothing mode. **/ |
|---|
| 143 | public function seek(pos:Number):void { |
|---|
| 144 | clearInterval(timeinterval); |
|---|
| 145 | stream.seek(pos); |
|---|
| 146 | play(); |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | /** Receive NetStream status updates. **/ |
|---|
| 151 | private function statusHandler(evt:NetStatusEvent):void { |
|---|
| 152 | if(evt.info.code == "NetStream.Play.Stop" && stream.bytesLoaded == stream.bytesTotal) { |
|---|
| 153 | clearInterval(timeinterval); |
|---|
| 154 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.COMPLETED}); |
|---|
| 155 | } else if (evt.info.code == "NetStream.Play.StreamNotFound") { |
|---|
| 156 | stop(); |
|---|
| 157 | model.sendEvent(ModelEvent.ERROR,{message:'Video not found: '+model.playlist[model.config['item']]['file']}); |
|---|
| 158 | } |
|---|
| 159 | model.sendEvent(ModelEvent.META,{info:evt.info.code}); |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | /** Destroy the video. **/ |
|---|
| 164 | public function stop():void { |
|---|
| 165 | stream.pause(); |
|---|
| 166 | if(stream.bytesLoaded != stream.bytesTotal) { |
|---|
| 167 | stream.close(); |
|---|
| 168 | } |
|---|
| 169 | metadata = false; |
|---|
| 170 | clearInterval(loadinterval); |
|---|
| 171 | clearInterval(timeinterval); |
|---|
| 172 | }; |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | /** Interval for the position progress **/ |
|---|
| 176 | private function timeHandler():void { |
|---|
| 177 | var bfr = Math.round(stream.bufferLength/stream.bufferTime*100); |
|---|
| 178 | var pos = Math.round(stream.time*10)/10; |
|---|
| 179 | var dur = model.playlist[model.config['item']]['duration']; |
|---|
| 180 | if(bfr < 95 && pos < Math.abs(dur-stream.bufferTime*2)) { |
|---|
| 181 | model.sendEvent(ModelEvent.BUFFER,{percentage:bfr}); |
|---|
| 182 | if(model.config['state'] != ModelStates.BUFFERING && bfr < 10) { |
|---|
| 183 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING}); |
|---|
| 184 | } |
|---|
| 185 | } else if (model.config['state'] == ModelStates.BUFFERING) { |
|---|
| 186 | model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING}); |
|---|
| 187 | } |
|---|
| 188 | model.sendEvent(ModelEvent.TIME,{position:pos}); |
|---|
| 189 | }; |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | /** Set the volume level. **/ |
|---|
| 193 | public function volume(vol:Number):void { |
|---|
| 194 | transform.volume = vol/100; |
|---|
| 195 | stream.soundTransform = transform; |
|---|
| 196 | }; |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | }; |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | } |
|---|