root/trunk/as3/com/jeroenwijering/models/HTTPModel.as @ 1

Revision 1, 7.9 kB (checked in by jeroen, 18 months ago)

initial commit of old repository into public one

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