HttpStreamingProvider.as 11 KB
Newer Older
Anssi Piirainen's avatar
Anssi Piirainen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * This file is part of Flowplayer, http://flowplayer.org
 *
 * By: Daniel Rossi <electroteque@gmail.com>, Anssi Piirainen <api@iki.fi> Flowplayer Oy
 * Copyright (c) 2009, 2010 Electroteque Multimedia, Flowplayer Oy
 *
 * Released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

package org.flowplayer.httpstreaming {


    import flash.events.NetStatusEvent;
    import flash.net.NetStream;
    import flash.net.NetConnection;
    import flash.utils.setTimeout;

    import org.flowplayer.model.Clip;
    import org.flowplayer.model.ClipEvent;
    import org.flowplayer.model.ClipEventType;
    import org.flowplayer.controller.NetStreamControllingStreamProvider;
    import org.flowplayer.controller.NetStreamClient;
    import org.flowplayer.model.ClipType;
    import org.flowplayer.model.Plugin;
    import org.flowplayer.model.PluginModel;
    import org.flowplayer.util.NumberUtil;
    import org.flowplayer.util.PropertyBinder;
    import org.flowplayer.view.Flowplayer;

    import org.osmf.logging.Log;
    import org.osmf.net.httpstreaming.HTTPNetStream;
    import org.osmf.net.httpstreaming.f4f.HTTPStreamingF4FFactory;
    import org.osmf.net.httpstreaming.dvr.DVRInfo;
35
    import org.osmf.net.StreamingURLResource;
Anssi Piirainen's avatar
Anssi Piirainen committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    import org.osmf.metadata.Metadata;
    import org.osmf.metadata.MetadataNamespaces;
    import org.osmf.media.URLResource;
    import org.osmf.events.DVRStreamInfoEvent;


    import org.flowplayer.bwcheck.net.OsmfLoggerFactory;
    import org.flowplayer.httpstreaming.Config;

    public class HttpStreamingProvider extends NetStreamControllingStreamProvider implements Plugin {
        private var _bufferStart:Number;
        private var _config:Config;
        private var _startSeekDone:Boolean;
        private var _model:PluginModel;
        private var _previousClip:Clip;
        private var _player:Flowplayer;
        private var netResource:URLResource;
        private var _dvrInfo:DVRInfo;
        private var _dvrIsRecording:Boolean;
        private var _initialSeekTime:Number;
        
        override public function onConfig(model:PluginModel):void {
            _model = model;
            _config = new PropertyBinder(new Config(), null).copyProperties(model.config) as Config;

            CONFIG::LOGGING {
                Log.loggerFactory = new OsmfLoggerFactory();
            }
        }
    
        override public function onLoad(player:Flowplayer):void {
            log.info("onLoad()");
            _player = player;
            
            _player.playlist.current.onSeek(function(event:ClipEvent):void {

                //if we have dvr info determine if its a dvr or live position.
                if (_dvrInfo) {
                    if (event.info <= dvrSeekOffset) {
                        dispatchDVREvent(_dvrInfo);
                    } else {
                        dispatchLiveEvent(Number(event.info));
                    }
                }
            });



            _model.dispatchOnLoad();
        }
    
        override protected function getClipUrl(clip:Clip):String {
            return clip.completeUrl;
        }
    
        override protected function doLoad(event:ClipEvent, netStream:NetStream, clip:Clip):void {
            if (!netResource) return;

            clip.onPlayStatus(onPlayStatus);
            _bufferStart = clip.currentTime;
            _startSeekDone = false;
            //reset dvr info
            _dvrInfo = null;
            _dvrIsRecording = false;

101
            //#70 fixes for live streams.
102
            netStream.play(clip.url, clip.live && !isDvr ? -1 : clip.start);
Anssi Piirainen's avatar
Anssi Piirainen committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
        }

        private function onPlayStatus(event:ClipEvent) : void {
            log.debug("onPlayStatus() -- " + event.info.code);
            if (event.info.code == "NetStream.Play.TransitionComplete"){
                dispatchEvent(new ClipEvent(ClipEventType.SWITCH_COMPLETE));
            }
            return;
        }

        override protected function onNetStatus(event:NetStatusEvent) : void {
            log.debug("onNetStatus(), code: " + event.info.code + ", paused? " + paused + ", seeking? " + seeking);
            switch(event.info.code){
                case "NetStream.Play.Transition":
                    log.debug("Stream Transition -- " + event.info.details);
                    dispatchEvent(new ClipEvent(ClipEventType.SWITCH, event.info.details));
                    break;
                case "NetStream.Play.UnpublishNotify":
                case "NetStream.Play.Stop":
                    //#550 for live streams once unpublished,  stop the player to prevent streamnotfound errors reconnecting.
123
124
                   //#27 regression caused by #550, only stop the player for live streams. caused issues when stopping between playlist items.
                   if (clip.live && !_player.playlist.hasNext()) _player.stop();
Anssi Piirainen's avatar
Anssi Piirainen committed
125
                    break;
126
127
128
129
130
131
132
133
134
                case "NetStream.Buffer.Empty":
                    //#70 implement playback optimisations from strobe media playback.
                    if (netStream.bufferTime >= 2.0) {
                        netStream.bufferTime += 1.0;
                    }
                    else {
                        netStream.bufferTime = 2.0;
                    }
                    break;
Anssi Piirainen's avatar
Anssi Piirainen committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
            }
            return;
        }

        override protected function doSeek(event : ClipEvent, netStream : NetStream, seconds : Number) : void {
            var seekTime:int = int(seconds);
            _bufferStart = seekTime;
            log.debug("calling netStream.seek(" + seekTime + ")");
            seeking = true;

            //#515 when seeking on startup set a delay or else the initial time is treated as the clip start time.
            if (time <= 0 ) {
                setTimeout(function():void {
                    netStream.seek(seconds);
                }, 250);
                return;
            }
            netStream.seek(seekTime);
        }

        override protected function doSwitchStream(event:ClipEvent, netStream:NetStream, clip:Clip, netStreamPlayOptions:Object = null):void {      
            log.debug("doSwitchStream()");

			_previousClip = clip;

            if (netStream.hasOwnProperty("play2") && netStreamPlayOptions) {
                import flash.net.NetStreamPlayOptions;
                if (netStreamPlayOptions is NetStreamPlayOptions) {
					log.debug("doSwitchStream() calling play2()")
					netStream.play2(netStreamPlayOptions as NetStreamPlayOptions);
				}
			} else {
                //fix for #338, don't set the currentTime when dynamic stream switching
                _bufferStart = clip.currentTime;
                clip.currentTime = Math.floor(_previousClip.currentTime + netStream.time);
				load(event, clip);
                dispatchEvent(event);
			}
        }

175
        /*override protected function onMetaData(event:ClipEvent):void {
Anssi Piirainen's avatar
Anssi Piirainen committed
176
177
            log.debug("in NetStreamControllingStremProvider.onMetaData: " + event.target);

178
            //#70 remove clip duration for live streams and when not dvr recording
179
180
181
182
183
184
185
            if (clip.live && !isDvr) {
                clip.metaData.duration = 0;
                clip.duration = 0;
                clip.durationFromMetadata = 0;
            }


186

187
            if (! clip.startDispatched) {
Anssi Piirainen's avatar
Anssi Piirainen committed
188
189
190
191
192
193
194
195
                clip.dispatch(ClipEventType.START, pauseAfterStart);
                clip.startDispatched = true;
            }

            if (pauseAfterStart) {
                pauseToFrame();
            }
            switching = false;
196
        } */
Anssi Piirainen's avatar
Anssi Piirainen committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

        override public function get allowRandomSeek():Boolean {
           return true;
        }
    
        override protected function canDispatchBegin():Boolean {
            return true;
        }

        override protected function canDispatchStreamNotFound():Boolean {
            return false;
        }
    
        public function getDefaultConfig():Object {
            return null;
        }
        
        override public function get type():String {
            return "httpstreaming";    
        }

        private function onDVRStreamInfo(event:DVRStreamInfoEvent):void
        {
            //this.netStream.removeEventListener(DVRStreamInfoEvent.DVRSTREAMINFO, onDVRStreamInfo);

            _dvrInfo = event.info as DVRInfo;

            _dvrIsRecording = _dvrInfo.isRecording;

            //don't update unless we are dvr recording
            if (!_dvrIsRecording) return;

            clip.duration = _dvrInfo.curLength;
            clip.setCustomProperty("dvrInfo", _dvrInfo);

            //start at dvr not live position
            //if clip start has not been dispatch seek to the live position and dispatch start
            if (!_config.startLivePosition || clip.startDispatched) return;

            //dispatch the dvr event
            dispatchDVREvent(_dvrInfo);

            //seek to the closest offset to the live position determined by the current dvr duration, buffertime and live snap offset.
            var livePosition:Number = Math.max(0, dvrSeekOffset);
            this.netStream.seek(livePosition);

            dispatchLiveEvent(livePosition);

        }

247
248
249
250
251
        private function get isDvr():Boolean
        {
            return StreamingURLResource(netResource).streamType == "dvr";
        }

Anssi Piirainen's avatar
Anssi Piirainen committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
        private function dispatchDVREvent(dvrInfo:DVRInfo):void
        {
            log.debug("Seeking to DVR position");
            clip.dispatch(ClipEventType.PLAY_STATUS, {
                type: "dvr",
                info: dvrInfo
            });
        }

        private function dispatchLiveEvent(livePosition:Number):void
        {
            log.debug("Seeking to Live position");
            clip.dispatch(ClipEventType.PLAY_STATUS, {
                type: "live",
                info: livePosition
            });
        }

        private function get dvrSeekOffset():Number
        {
            return clip.duration - netStream.bufferTime - _config.dvrSnapToLiveClockOffset;
        }

        override protected function createNetStream(connection:NetConnection):NetStream {


            if (!clip.getCustomProperty("urlResource")) return super.createNetStream(connection);

            clip.type = ClipType.VIDEO;

            netResource = clip.getCustomProperty("urlResource") as URLResource;

            var httpNetStream:HTTPNetStream = new HTTPNetStream(connection, new HTTPStreamingF4FFactory(), netResource);

            //#452 dvr integration
            var metadata:Metadata = netResource.getMetadataValue(MetadataNamespaces.DVR_METADATA) as Metadata;
            if (metadata) {
                clip.setCustomProperty("dvr", true);
                httpNetStream.addEventListener(DVRStreamInfoEvent.DVRSTREAMINFO, onDVRStreamInfo);
            }

            return httpNetStream;
        }

296
        //#70 fixes for buffer start value
297
298
299
300
        override public function get bufferStart():Number
        {
            if (! clip) return 0;
            return _bufferStart - clip.start;
Anssi Piirainen's avatar
Anssi Piirainen committed
301
302
303
304
305
306
307
308
309
        }

        override public function get bufferEnd() : Number {
            if (!clip) return 0;
            if (!netStream) return 0;
            return getCurrentPlayheadTime(netStream) + netStream.bufferLength;
        }
    }
}