source: trunk/as3/com/jeroenwijering/models/HTTPModel.as @ 11

Revision 11, 8.3 KB checked in by jeroen, 5 years ago (diff)

added AbstractView and changes to view to ahndle plugin loading

  • Property svn:executable set to *
Line 
1/**
2* Wrapper for playback of progressively downloaded video.
3**/
4package com.jeroenwijering.models {
5
6
7import com.jeroenwijering.events.*;
8import com.jeroenwijering.models.ModelInterface;
9import com.jeroenwijering.player.Model;
10import flash.events.*;
11import flash.display.DisplayObject;
12import flash.media.SoundTransform;
13import flash.media.Video;
14import flash.net.*;
15import flash.utils.clearInterval;
16import flash.utils.setInterval;
17
18
19public class HTTPModel implements ModelInterface {
20
21
22        /** reference to the model. **/
23        private var model:Model;
24        /** Video object to be instantiated. **/
25        private var video:Video;
26        /** NetConnection object for setup of the video stream. **/
27        private var connection:NetConnection;
28        /** NetStream instance that handles the stream IO. **/
29        private var stream:NetStream;
30        /** Sound control object. **/
31        private var transform:SoundTransform;
32        /** Interval ID for the time. **/
33        private var timeinterval:Number;
34        /** Interval ID for the loading. **/
35        private var loadinterval:Number;
36        /** Object with keyframe times and positions. **/
37        private var keyframes:Object;
38        /** Offset byteposition to start streaming. **/
39        private var offset:Number;
40        /** Offset timeposition for lighttpd streaming. **/
41        private var timeoffset:Number;
42        /** switch for h264 streaming **/
43        private var h264:Boolean;
44        /** Byteposition to which the file has been loaded. **/
45        private var loaded:Number;
46
47
48        /** Constructor; sets up the connection and display. **/
49        public function HTTPModel(mod:Model) {
50                model = mod;
51                connection = new NetConnection();
52                connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
53                connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
54                connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR,errorHandler);
55                connection.connect(null);
56                stream = new NetStream(connection);
57                stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
58                stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
59                stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,errorHandler);
60                stream.bufferTime = model.config['bufferlength'];
61                stream.client = this;
62                video = new Video(320,240);
63                video.attachNetStream(stream);
64                transform = new SoundTransform();
65                stream.soundTransform = transform;
66                model.config['mute'] == true ? volume(0): volume(model.config['volume']);
67                quality(model.config['quality']);
68                offset = timeoffset = 0;
69        };
70
71
72        /** Catch security errors. **/
73        private function errorHandler(evt:ErrorEvent) {
74                model.sendEvent(ModelEvent.ERROR,{message:evt.text});
75        };
76
77
78        /** Return a keyframe byteoffset or timeoffset. **/
79        private function getOffset(pos:Number,tme:Boolean=false):Number {
80                var off = 0;
81                if(keyframes === null) {
82                        errorHandler(new ErrorEvent(ErrorEvent.ERROR,false,false,"This file has no seekpoints metadata."));
83                        return 0;
84                }
85                for (var i=0; i< keyframes.times.length; i++) {
86                        if((keyframes.times[i] <= pos || i ==0) && (keyframes.times[i+1] >= pos || !keyframes.times[i+1])) {
87                                if(tme == true) {
88                                        off = keyframes.times[i];
89                                } else {
90                                        off = keyframes.filepositions[i];
91                                }
92                                break;
93                        }
94                }
95                return off;
96        };
97
98
99        /** Load content. **/
100        public function load() {
101                stream.close();
102                var url = model.playlist[model.config['item']]['file'];
103                if(model.config["streamscript"] == "lighttpd") {
104                        if(h264) {
105                                url +='?start='+timeoffset;
106                        } else {
107                                url += '?start='+offset;
108                        }
109                } else {
110                        if(model.config["streamscript"].indexOf('?') > -1) {
111                                url = model.config["streamscript"]+"&file="+url+'&start='+offset;
112                        } else {
113                                url = model.config["streamscript"]+"?file="+url+'&start='+offset;
114                        }
115                }
116                url += '&width='+model.config['width'];
117                url += '&client='+encodeURI(model.config['client']);
118                url += '&version='+encodeURI(model.config['version']);
119                stream.play(url);
120                clearInterval(loadinterval);
121                clearInterval(timeinterval);
122                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING});
123                loadinterval = setInterval(loadHandler,100);
124                timeinterval = setInterval(timeHandler,100);
125        };
126
127
128        /** Interval for the loading progress **/
129        private function loadHandler() {
130                loaded = stream.bytesLoaded;
131                var ttl = stream.bytesTotal;
132                model.sendEvent(ModelEvent.LOADED,{loaded:loaded,total:ttl+offset,offset:offset});
133                if(loaded >= ttl && loaded > 0) {
134                        clearInterval(loadinterval);
135                }
136        };
137
138
139        /** Get textdata from netstream. **/
140        public function onImageData(info:Object) {
141                var dat = new Object();
142                for(var i in info) {
143                        dat[i] = info[i];
144                }
145                model.sendEvent(ModelEvent.META,dat);
146        };
147
148
149        /** Handler for onLastSecond call. **/
150        public function onLastSecond(info:Object) { };
151
152
153        /** Get metadata information from netstream class. **/
154        public function onMetaData(info:Object) {
155                if(h264) { return; }
156                if(info.width) {
157                        video.width = info.width;
158                        video.height = info.height;
159                        model.mediaHandler(video);
160                } else {
161                        model.mediaHandler();
162                }
163                if(info.seekpoints) {
164                        h264 = true;
165                        keyframes = new Object();
166                        keyframes.times = new Array();
167                        keyframes.filepositions = new Array();
168                        for (var j in info.seekpoints) {
169                                keyframes.times.push(Number(info.seekpoints[j]['time']));
170                                keyframes.filepositions.push(Number(info.seekpoints[j]['offset']));
171                        }
172                } else if(info.keyframes) {
173                        keyframes = info.keyframes;
174                }
175                var dat = new Object();
176                for(var i in info) {
177                        dat[i] = info[i];
178                }
179                delete dat.seekpoints;
180                dat.keyframes = '';
181                for(var k=0; k<keyframes.times.length; k++) {
182                        dat['keyframes'] += ','+keyframes.times[k]+':'+keyframes.filepositions[k];
183                }
184                model.sendEvent(ModelEvent.META,dat);
185                if(model.playlist[model.config['item']]['start'] > 0) {
186                        seek(model.playlist[model.config['item']]['start']);
187                }
188        };
189
190
191        /** Get textdata from netstream. **/
192        public function onTextData(info:Object) {
193                var dat = new Object();
194                for(var i in info) {
195                        dat[i] = info[i];
196                }
197                model.sendEvent(ModelEvent.META,dat);
198        };
199
200
201        /** Pause playback. **/
202        public function pause() {
203                clearInterval(timeinterval);
204                stream.pause();
205                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PAUSED});
206        };
207
208
209        /** Resume playing. **/
210        public function play() {
211                stream.resume();
212                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING});
213                timeinterval = setInterval(timeHandler,100);
214        };
215
216
217        /** Change the smoothing mode. **/
218        public function seek(pos:Number) {
219                clearInterval(timeinterval);
220                var off = getOffset(pos);
221                if(off < offset || off > offset+loaded) {
222                        offset = off;
223                        timeoffset = getOffset(pos,true);
224                        load();
225                } else {
226                        if(h264) {
227                                stream.seek(pos-timeoffset);
228                        } else {
229                                stream.seek(pos)
230                        }
231                        play();
232                }
233        };
234
235
236        /** Change the smoothing mode. **/
237        public function quality(qua:Boolean) {
238                if(qua == true) {
239                        video.smoothing = true;
240                        video.deblocking = 4;
241                } else {
242                        video.smoothing = false;
243                        video.deblocking = 1;
244                }
245        };
246
247
248        /** Receive NetStream status updates. **/
249        private function statusHandler(evt:NetStatusEvent) {
250                if(evt.info.code == "NetStream.Play.Stop") {
251                        if(model.config['state'] != ModelStates.COMPLETED) {
252                                clearInterval(timeinterval);
253                                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.COMPLETED});
254                        }
255                } else if(evt.info.code == "NetStream.Play.StreamNotFound") {
256                        stop();
257                        model.sendEvent(ModelEvent.ERROR,{message:"Video stream not found: " +
258                                model.playlist[model.config['item']]['file']});
259                } else {
260                        model.sendEvent(ModelEvent.META,{info:evt.info.code});
261                }
262        };
263
264
265        /** Destroy the HTTP stream. **/
266        public function stop() {
267                clearInterval(loadinterval);
268                clearInterval(timeinterval);
269                stream.close();
270                offset = timeoffset = 0;
271        };
272
273
274        /** Interval for the position progress **/
275        private function timeHandler() {
276                var bfr = Math.round(stream.bufferLength/stream.bufferTime*100);
277                var pos = Math.round(stream.time*10)/10;
278                if (h264) { pos += timeoffset; }
279                var dur = model.playlist[model.config['item']]['duration'];
280                if(bfr < 100 && pos < Math.abs(dur-stream.bufferTime-1)) {
281                        model.sendEvent(ModelEvent.BUFFER,{percentage:bfr});
282                        if(model.config['state'] != ModelStates.BUFFERING) {
283                                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING});
284                        }
285                } else if (model.config['state'] == ModelStates.BUFFERING) {
286                        model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING});
287                }
288                if(dur > 0) {
289                        model.sendEvent(ModelEvent.TIME,{position:pos,duration:dur});
290                }
291        };
292
293
294        /** Set the volume level. **/
295        public function volume(vol:Number) {
296                transform.volume = vol/100;
297                stream.soundTransform = transform;
298        };
299
300
301};
302
303
304}
Note: See TracBrowser for help on using the repository browser.