This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 313
/
HTTPVideoProvider.as
643 lines (560 loc) · 23.1 KB
/
HTTPVideoProvider.as
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
35
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
247
248
249
250
251
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package com.videojs.providers{
import com.videojs.VideoJSModel;
import com.videojs.events.VideoPlaybackEvent;
import com.videojs.structs.ExternalErrorEventName;
import com.videojs.structs.ExternalEventName;
import flash.events.EventDispatcher;
import flash.events.NetStatusEvent;
import flash.events.TimerEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetStreamAppendBytesAction;
import flash.utils.ByteArray;
import flash.utils.Timer;
import flash.utils.getTimer;
public class HTTPVideoProvider extends EventDispatcher implements IProvider{
private var _nc:NetConnection;
private var _ns:NetStream;
private var _throughputTimer:Timer;
private var _currentThroughput:int = 0; // in B/sec
private var _loadStartTimestamp:int;
private var _loadStarted:Boolean = false;
private var _loadCompleted:Boolean = false;
private var _loadErrored:Boolean = false;
private var _pauseOnStart:Boolean = false;
private var _pausePending:Boolean = false;
/**
* The number of seconds between the logical start of the stream and the current zero
* playhead position of the NetStream. During normal, file-based playback this value should
* always be zero. When the NetStream is in data generation mode, seeking during playback
* resets the zero point of the stream to the seek target. To recover the playhead position
* in the logical stream, this value can be added to the NetStream reported time.
*
* @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetStream.html#play()
*/
private var _startOffset:Number = 0;
/**
* If true, an empty NetStream buffer should be interpreted as the end of the video. This
* is probably the case because the video data is being fed to the NetStream dynamically
* through appendBuffer, not for traditional file download video.
*/
private var _ending:Boolean = false;
private var _videoReference:Video;
/**
* When the player is paused, and a seek is executed, the NetStream.time property will NOT update until the decoder encounters a new time tag,
* which won't happen until playback is resumed. This wrecks havoc with external scrubber logic, so when the player is paused and a seek is requested,
* we cache the intended time, and use it IN PLACE OF NetStream's time when the time accessor is hit.
*/
private var _pausedSeekValue:Number = -1;
private var _src:Object;
private var _metadata:Object;
private var _isPlaying:Boolean = false;
private var _isPaused:Boolean = true;
private var _isBuffering:Boolean = false;
private var _isSeeking:Boolean = false;
private var _isLive:Boolean = false;
private var _canSeekAhead:Boolean = false;
private var _hasEnded:Boolean = false;
private var _canPlayThrough:Boolean = false;
private var _loop:Boolean = false;
private var _durationOverride:Number;
private var _model:VideoJSModel;
public function HTTPVideoProvider(){
_model = VideoJSModel.getInstance();
_metadata = {};
_throughputTimer = new Timer(250, 0);
_throughputTimer.addEventListener(TimerEvent.TIMER, onThroughputTimerTick);
}
public function get loop():Boolean{
return _loop;
}
public function set loop(pLoop:Boolean):void{
_loop = pLoop;
}
public function get time():Number{
if(_ns != null){
if(_pausedSeekValue != -1){
return _pausedSeekValue;
}
else{
return _startOffset + _ns.time;
}
}
else{
return 0;
}
}
public function get duration():Number{
if(_metadata != null && _metadata.duration != undefined){
return Number(_metadata.duration);
} else if( _durationOverride && _durationOverride > 0 ) {
return _durationOverride;
}
else{
return 0;
}
}
public function set duration(value:Number):void {
_durationOverride = value;
}
public function get readyState():int{
// if we have metadata and a known duration
if(_metadata != null && _metadata.duration != undefined){
// if playback has begun
if(_isPlaying){
// if the asset can play through without rebuffering
if(_canPlayThrough){
return 4;
}
// if we don't know if the asset can play through without buffering
else{
// if the buffer is full, we assume we can seek a head at least a keyframe
if(_ns.bufferLength >= _ns.bufferTime){
return 3;
}
// otherwise, we can't be certain that seeking ahead will work
else{
return 2;
}
}
}
// if playback has not begun
else{
return 1;
}
}
// if we have no metadata
else{
return 0;
}
}
public function get networkState():int{
if(!_loadStarted){
return 0;
}
else{
if(_loadCompleted){
return 1;
}
else if(_loadErrored){
return 3;
}
else{
return 2;
}
}
}
public function appendBytesAction(action:String):void {
if(_ns) {
_ns.appendBytesAction(action);
}
}
public function appendBuffer(bytes:ByteArray):void{
_ns.appendBytes(bytes);
}
public function endOfStream():void{
_ending = true;
}
public function abort():void{
// flush the netstream buffers
_ns.seek(time);
}
public function get buffered():Number{
// _src.path == null when in data generation mode
if(_ns && _src.path == null)
{
return _startOffset + _ns.bufferLength + _ns.time;
} else if(duration > 0){
return (_ns.bytesLoaded / _ns.bytesTotal) * duration;
} else {
return 0;
}
}
public function get bufferedBytesEnd():int{
if(_loadStarted){
return _ns.bytesLoaded;
}
else{
return 0;
}
}
public function get bytesLoaded():int{
return 0;
}
public function get bytesTotal():int{
return 0;
}
public function get playing():Boolean{
return _isPlaying;
}
public function get paused():Boolean{
return _isPaused;
}
public function get ended():Boolean{
return _hasEnded;
}
public function get seeking():Boolean{
return _isSeeking;
}
public function get usesNetStream():Boolean{
return true;
}
public function get metadata():Object{
return _metadata;
}
public function set src(pSrc:Object):void{
init(pSrc, false);
}
public function get srcAsString():String{
if(_src != null){
return _src.url;
}
return "";
}
public function init(pSrc:Object, pAutoplay:Boolean):void{
_src = pSrc;
_loadErrored = false;
_loadStarted = false;
_loadCompleted = false;
if(pAutoplay){
initNetConnection();
}
}
public function load():void{
_pauseOnStart = true;
_isPlaying = false;
_isPaused = true;
initNetConnection();
}
public function play():void{
// if this is a fresh playback request
if(!_loadStarted){
_pauseOnStart = false;
_isPlaying = false;
_isPaused = false;
_metadata = {};
initNetConnection();
}
// if the asset is already loading
else{
if (_hasEnded) {
_hasEnded = false;
_ns.seek(0);
}
_pausePending = false;
_ns.resume();
_isPaused = false;
_model.broadcastEventExternally(ExternalEventName.ON_RESUME);
if (!_isBuffering) {
_model.broadcastEventExternally(ExternalEventName.ON_START);
}
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_STREAM_START, {}));
}
}
public function pause():void{
_ns.pause();
if(_isPlaying && !_isPaused){
_isPaused = true;
_model.broadcastEventExternally(ExternalEventName.ON_PAUSE);
if(_isBuffering){
_pausePending = true;
}
} else if (_hasEnded) {
_hasEnded = false;
_ns.seek(0);
}
}
public function resume():void{
if(_isPlaying && _isPaused){
_ns.resume();
_isPaused = false;
_model.broadcastEventExternally(ExternalEventName.ON_RESUME);
if (!_isBuffering) {
_model.broadcastEventExternally(ExternalEventName.ON_START);
}
}
}
public function seekBySeconds(pTime:Number):void{
if(_isPlaying)
{
_isSeeking = true;
_throughputTimer.stop();
if(_isPaused)
{
_pausedSeekValue = pTime;
}
}
else if(_hasEnded)
{
_isPlaying = true;
_hasEnded = false;
}
_isBuffering = true;
if(_src.path === null)
{
_startOffset = pTime;
return;
}
_ns.seek(pTime);
}
public function seekByPercent(pPercent:Number):void{
if(_isPlaying && _metadata.duration != undefined){
_isSeeking = true;
if(pPercent < 0){
_ns.seek(0);
}
else if(pPercent > 1){
_throughputTimer.stop();
_ns.seek((pPercent / 100) * _metadata.duration);
}
else{
_throughputTimer.stop();
_ns.seek(pPercent * _metadata.duration);
}
}
}
public function stop():void{
if(_isPlaying){
_ns.close();
_isPlaying = false;
_hasEnded = true;
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_STREAM_CLOSE, {}));
_throughputTimer.stop();
_throughputTimer.reset();
}
}
public function attachVideo(pVideo:Video):void{
_videoReference = pVideo;
}
public function die():void{
if(_videoReference)
{
_videoReference.attachNetStream(null);
}
if( _ns )
{
try {
_ns.close();
_ns = null;
} catch( err: Error ) {
}
}
if( _nc )
{
try {
_nc.close();
_nc = null;
} catch( err: Error ) {
}
}
if(_throughputTimer)
{
try {
_throughputTimer.stop();
_throughputTimer = null;
} catch( err: Error ) {
}
}
}
private function initNetConnection():void{
// the video element triggers loadstart as soon as the resource selection algorithm selects a source
// this is somewhat later than that moment but relatively close
_model.broadcastEventExternally(ExternalEventName.ON_LOAD_START);
if(_nc != null) {
try {
_nc.close();
} catch( err: Error ) {
}
_nc.removeEventListener(NetStatusEvent.NET_STATUS, onNetConnectionStatus);
_nc = null;
}
_nc = new NetConnection();
_nc.client = this;
_nc.addEventListener(NetStatusEvent.NET_STATUS, onNetConnectionStatus);
_nc.connect(null);
}
private function initNetStream():void{
if(_ns != null){
_ns.close();
_ns.removeEventListener(NetStatusEvent.NET_STATUS, onNetStreamStatus);
_ns = null;
}
_ns = new NetStream(_nc);
_ns.inBufferSeek = true;
_ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStreamStatus);
_ns.client = this;
_ns.bufferTime = .5;
_ns.play(_src.path);
_videoReference.attachNetStream(_ns);
if (_src.path === null) {
_pausePending = true;
_loadStarted = true;
}
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_STREAM_READY, {ns:_ns}));
}
private function calculateThroughput():void{
// if it's finished loading, we can kill the calculations and assume it can play through
if(_ns.bytesLoaded == _ns.bytesTotal){
_canPlayThrough = true;
_loadCompleted = true;
_throughputTimer.stop();
_throughputTimer.reset();
_model.broadcastEventExternally(ExternalEventName.ON_CAN_PLAY_THROUGH);
}
// if it's still loading, but we know its duration, we can check to see if the current transfer rate
// will sustain uninterrupted playback - this requires the duration to be known, which is currently
// only accessible via metadata, which isn't parsed until the Flash Player encounters the metadata atom
// in the file itself, which means that this logic will only work if the asset is playing - preload
// won't ever cause this logic to run :(
else if(_ns.bytesTotal > 0 && _metadata != null && _metadata.duration != undefined){
_currentThroughput = _ns.bytesLoaded / ((getTimer() - _loadStartTimestamp) / 1000);
var __estimatedTimeToLoad:Number = (_ns.bytesTotal - _ns.bytesLoaded) * _currentThroughput;
if(__estimatedTimeToLoad <= _metadata.duration){
_throughputTimer.stop();
_throughputTimer.reset();
_canPlayThrough = true;
_model.broadcastEventExternally(ExternalEventName.ON_CAN_PLAY_THROUGH);
}
}
}
private function onNetConnectionStatus(e:NetStatusEvent):void{
switch(e.info.code){
case "NetConnection.Connect.Success":
initNetStream();
break;
case "NetConnection.Connect.Failed":
break;
}
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_NETCONNECTION_STATUS, {info:e.info}));
}
private function onNetStreamStatus(e:NetStatusEvent):void{
switch(e.info.code){
case "NetStream.Play.Start":
_pausedSeekValue = -1;
_metadata = null;
_canPlayThrough = false;
_hasEnded = false;
_isBuffering = true;
_currentThroughput = 0;
_loadStartTimestamp = getTimer();
_throughputTimer.reset();
_throughputTimer.start();
if(_pauseOnStart && _loadStarted == false){
_ns.pause();
_isPaused = true;
}
else{
_model.broadcastEventExternally(ExternalEventName.ON_RESUME);
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_STREAM_START, {info:e.info}));
}
_loadStarted = true;
break;
case "NetStream.SeekStart.Notify":
if(_src.path === null) {
appendBytesAction(NetStreamAppendBytesAction.RESET_SEEK);
}
_model.broadcastEventExternally(ExternalEventName.ON_SEEK_START);
break;
case "NetStream.Buffer.Full":
_model.broadcastEventExternally(ExternalEventName.ON_BUFFER_FULL);
_model.broadcastEventExternally(ExternalEventName.ON_CAN_PLAY);
_pausedSeekValue = -1;
_isPlaying = true;
if(_pausePending){
_pausePending = false;
_ns.pause();
_isPaused = true;
} else if (_isBuffering) {
_model.broadcastEventExternally(ExternalEventName.ON_START);
}
_isBuffering = false;
break;
case "NetStream.Buffer.Empty":
// should not fire if ended/paused. issue #38
if(!_isPlaying){ return; }
// reaching the end of the buffer after endOfStream has been called means we've
// hit the end of the video
if (_ending) {
_ending = false;
_isPlaying = false;
_isPaused = true;
_hasEnded = true;
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_STREAM_CLOSE, {info:e.info}));
_model.broadcastEventExternally(ExternalEventName.ON_PAUSE);
_model.broadcastEventExternally(ExternalEventName.ON_PLAYBACK_COMPLETE);
_startOffset = 0;
_pausedSeekValue = 0;
break;
}
_isBuffering = true;
_model.broadcastEventExternally(ExternalEventName.ON_BUFFER_EMPTY);
break;
case "NetStream.Play.Stop":
if(!_loop){
_isPlaying = false;
_isPaused = true;
_hasEnded = true;
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_STREAM_CLOSE, {info:e.info}));
_model.broadcastEventExternally(ExternalEventName.ON_PAUSE);
_model.broadcastEventExternally(ExternalEventName.ON_PLAYBACK_COMPLETE);
}
else{
_ns.seek(0);
}
_throughputTimer.stop();
_throughputTimer.reset();
break;
case "NetStream.Seek.Notify":
_isPlaying = true;
_isSeeking = false;
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_STREAM_SEEK_COMPLETE, {info:e.info}));
_model.broadcastEventExternally(ExternalEventName.ON_SEEK_COMPLETE);
_model.broadcastEventExternally(ExternalEventName.ON_BUFFER_EMPTY);
_currentThroughput = 0;
_loadStartTimestamp = getTimer();
_throughputTimer.reset();
_throughputTimer.start();
break;
case "NetStream.Play.StreamNotFound":
_loadErrored = true;
_model.broadcastErrorEventExternally(ExternalErrorEventName.SRC_404);
break;
case "NetStream.Video.DimensionChange":
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_VIDEO_DIMENSION_UPDATE, {videoWidth: _videoReference.videoWidth, videoHeight: _videoReference.videoHeight}));
if(_model.metadata && _videoReference)
{
_model.metadata.width = _videoReference.videoWidth;
_model.metadata.height = _videoReference.videoHeight;
}
break;
}
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_NETSTREAM_STATUS, {info:e.info}));
}
private function onThroughputTimerTick(e:TimerEvent):void{
calculateThroughput();
}
public function onMetaData(pMetaData:Object):void{
_metadata = pMetaData;
if(pMetaData.duration != undefined){
_isLive = false;
_canSeekAhead = true;
_model.broadcastEventExternally(ExternalEventName.ON_DURATION_CHANGE, _metadata.duration);
}
else{
_isLive = true;
_canSeekAhead = false;
}
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_META_DATA, {metadata:_metadata}));
_model.broadcastEventExternally(ExternalEventName.ON_METADATA, _metadata);
}
public function onCuePoint(pInfo:Object):void{
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_CUE_POINT, {cuepoint:pInfo}));
}
public function onXMPData(pInfo:Object):void{
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_XMP_DATA, {cuepoint:pInfo}));
}
public function onPlayStatus(e:Object):void{
}
}
}