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

Revision 6, 8.2 KB checked in by jeroen, 5 years ago (diff)

added stacking and first skin (inverted)

  • 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 && keyframes.times[i+1] >= pos) {
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        /** Get metadata information from netstream class. **/
150        public function onMetaData(info:Object) {
151                if(h264) { return; }
152                video.width = info.width;
153                video.height = info.height;
154                model.mediaHandler(video);
155                if(info.seekpoints) {
156                        h264 = true;
157                        keyframes = new Object();
158                        keyframes.times = new Array();
159                        keyframes.filepositions = new Array();
160                        for (var j in info.seekpoints) {
161                                keyframes.times.push(Number(info.seekpoints[j]['time']));
162                                keyframes.filepositions.push(Number(info.seekpoints[j]['offset']));
163                        }
164                } else if(info.keyframes) {
165                        keyframes = info.keyframes;
166                }
167                var dat = new Object();
168                for(var i in info) {
169                        dat[i] = info[i];
170                }
171                delete dat.seekpoints;
172                dat.keyframes = '';
173                for(var k=0; k<keyframes.times.length; k++) {
174                        dat['keyframes'] += ','+keyframes.times[k]+':'+keyframes.filepositions[k];
175                }
176                model.sendEvent(ModelEvent.META,dat);
177                if(model.playlist[model.config['item']]['start'] > 0) {
178                        seek(model.playlist[model.config['item']]['start']);
179                }
180        };
181
182
183        /** Get textdata from netstream. **/
184        public function onTextData(info:Object) {
185                var dat = new Object();
186                for(var i in info) {
187                        dat[i] = info[i];
188                }
189                model.sendEvent(ModelEvent.META,dat);
190        };
191
192
193        /** Pause playback. **/
194        public function pause() {
195                clearInterval(timeinterval);
196                stream.pause();
197                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PAUSED});
198        };
199
200
201        /** Resume playing. **/
202        public function play() {
203                stream.resume();
204                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING});
205                timeinterval = setInterval(timeHandler,100);
206        };
207
208
209        /** Change the smoothing mode. **/
210        public function seek(pos:Number) {
211                clearInterval(timeinterval);
212                var off = getOffset(pos);
213                if(off < offset || off > offset+loaded) {
214                        offset = off;
215                        timeoffset = getOffset(pos,true);
216                        load();
217                } else {
218                        if(h264) {
219                                stream.seek(pos-timeoffset);
220                        } else {
221                                stream.seek(pos)
222                        }
223                        play();
224                }
225        };
226
227
228        /** Change the smoothing mode. **/
229        public function quality(qua:Boolean) {
230                if(qua == true) {
231                        video.smoothing = true;
232                        video.deblocking = 4;
233                } else {
234                        video.smoothing = false;
235                        video.deblocking = 1;
236                }
237        };
238
239
240        /** Receive NetStream status updates. **/
241        private function statusHandler(evt:NetStatusEvent) {
242                if(evt.info.code == "NetStream.Play.Stop") {
243                        if(model.config['state'] == ModelStates.COMPLETED) {
244                                stream.close();
245                        } else {
246                                clearInterval(timeinterval);
247                                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.COMPLETED});
248                        }
249                } else if(evt.info.code == "NetStream.Play.StreamNotFound") {
250                        stop();
251                        model.sendEvent(ModelEvent.ERROR,{message:"Video stream not found: " +
252                                model.playlist[model.config['item']]['file']});
253                }
254                model.sendEvent(ModelEvent.META,{info:evt.info.code});
255        };
256
257
258        /** Destroy the HTTP stream. **/
259        public function stop() {
260                clearInterval(loadinterval);
261                clearInterval(timeinterval);
262                stream.close();
263                offset = timeoffset = 0;
264        };
265
266
267        /** Interval for the position progress **/
268        private function timeHandler() {
269                var bfr = Math.round(stream.bufferLength/stream.bufferTime*100);
270                var pos = Math.round(stream.time*10)/10;
271                if (h264) { pos += timeoffset; }
272                var dur = model.playlist[model.config['item']]['duration'];
273                if(bfr < 100 && pos < Math.abs(dur-stream.bufferTime-1)) {
274                        model.sendEvent(ModelEvent.BUFFER,{percentage:bfr});
275                        if(model.config['state'] != ModelStates.BUFFERING) {
276                                model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING});
277                        }
278                } else if (model.config['state'] == ModelStates.BUFFERING) {
279                        model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING});
280                }
281                if(dur > 0) {
282                        model.sendEvent(ModelEvent.TIME,{position:pos,duration:dur});
283                }
284        };
285
286
287        /** Set the volume level. **/
288        public function volume(vol:Number) {
289                transform.volume = vol/100;
290                stream.soundTransform = transform;
291        };
292
293
294};
295
296
297}
Note: See TracBrowser for help on using the repository browser.