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
/
VideoJS.as
443 lines (382 loc) · 16.9 KB
/
VideoJS.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
package{
import com.videojs.VideoJSApp;
import com.videojs.events.VideoJSEvent;
import com.videojs.structs.ExternalEventName;
import com.videojs.structs.ExternalErrorEventName;
import com.videojs.Base64;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.external.ExternalInterface;
import flash.geom.Rectangle;
import flash.system.Security;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.utils.ByteArray;
import flash.utils.Timer;
import flash.utils.setTimeout;
[SWF(backgroundColor="#000000", frameRate="60", width="480", height="270")]
public class VideoJS extends Sprite{
public const VERSION:String = CONFIG::version;
private var _app:VideoJSApp;
private var _stageSizeTimer:Timer;
public function VideoJS(){
_stageSizeTimer = new Timer(250);
_stageSizeTimer.addEventListener(TimerEvent.TIMER, onStageSizeTimerTick);
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function init():void{
// Allow JS calls from other domains
Security.allowDomain("*");
Security.allowInsecureDomain("*");
if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){
// we'll want to suppress ANY uncaught debug errors in production (for the sake of ux)
// IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", onUncaughtError);
}
if(ExternalInterface.available){
registerExternalMethods();
}
_app = new VideoJSApp();
addChild(_app);
_app.model.stageRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
// add content-menu version info
var _ctxVersion:ContextMenuItem = new ContextMenuItem("VideoJS Flash Component v" + VERSION, false, false);
var _ctxAbout:ContextMenuItem = new ContextMenuItem("Copyright © 2014 Brightcove, Inc.", false, false);
var _ctxMenu:ContextMenu = new ContextMenu();
_ctxMenu.hideBuiltInItems();
_ctxMenu.customItems.push(_ctxVersion, _ctxAbout);
this.contextMenu = _ctxMenu;
}
private function registerExternalMethods():void{
ExternalInterface.marshallExceptions = true;
try{
ExternalInterface.addCallback("vjs_appendBuffer", onAppendBufferCalled);
ExternalInterface.addCallback("vjs_appendChunkReady", onAppendChunkReadyCalled);
ExternalInterface.addCallback("vjs_echo", onEchoCalled);
ExternalInterface.addCallback("vjs_endOfStream", onEndOfStreamCalled);
ExternalInterface.addCallback("vjs_abort", onAbortCalled);
ExternalInterface.addCallback("vjs_discontinuity", onDiscontinuityCalled);
ExternalInterface.addCallback("vjs_getProperty", onGetPropertyCalled);
ExternalInterface.addCallback("vjs_setProperty", onSetPropertyCalled);
ExternalInterface.addCallback("vjs_autoplay", onAutoplayCalled);
ExternalInterface.addCallback("vjs_src", onSrcCalled);
ExternalInterface.addCallback("vjs_load", onLoadCalled);
ExternalInterface.addCallback("vjs_play", onPlayCalled);
ExternalInterface.addCallback("vjs_pause", onPauseCalled);
ExternalInterface.addCallback("vjs_resume", onResumeCalled);
ExternalInterface.addCallback("vjs_stop", onStopCalled);
// This callback should only be used when in data generation mode as it
// will adjust the notion of current time without notifiying the player
ExternalInterface.addCallback("vjs_adjustCurrentTime", onAdjustCurrentTimeCalled);
}
catch(e:SecurityError){
if (loaderInfo.parameters.debug != undefined && loaderInfo.parameters.debug == "true") {
throw new SecurityError(e.message);
}
}
catch(e:Error){
if (loaderInfo.parameters.debug != undefined && loaderInfo.parameters.debug == "true") {
throw new Error(e.message);
}
}
finally{}
setTimeout(finish, 50);
}
private function finish():void{
if(loaderInfo.parameters.mode != undefined){
_app.model.mode = loaderInfo.parameters.mode;
}
// Hard coding these in for now until we can come up with a better solution for 5.0 to avoid XSS.
_app.model.jsEventProxyName = 'videojs.Flash.onEvent';
_app.model.jsErrorEventProxyName = 'videojs.Flash.onError';
/*if(loaderInfo.parameters.eventProxyFunction != undefined){
_app.model.jsEventProxyName = loaderInfo.parameters.eventProxyFunction;
}
if(loaderInfo.parameters.errorEventProxyFunction != undefined){
_app.model.jsErrorEventProxyName = loaderInfo.parameters.errorEventProxyFunction;
}*/
if(loaderInfo.parameters.autoplay != undefined && loaderInfo.parameters.autoplay == "true"){
_app.model.autoplay = true;
}
if(loaderInfo.parameters.preload != undefined && loaderInfo.parameters.preload != ""){
_app.model.preload = String(loaderInfo.parameters.preload);
}
if(loaderInfo.parameters.muted != undefined && loaderInfo.parameters.muted == "true"){
_app.model.muted = true;
}
if(loaderInfo.parameters.loop != undefined && loaderInfo.parameters.loop == "true"){
_app.model.loop = true;
}
if(loaderInfo.parameters.src != undefined && loaderInfo.parameters.src != ""){
if (isExternalMSObjectURL(loaderInfo.parameters.src)) {
_app.model.srcFromFlashvars = null;
openExternalMSObject(loaderInfo.parameters.src);
} else {
_app.model.srcFromFlashvars = String(loaderInfo.parameters.src);
}
} else{
if(loaderInfo.parameters.rtmpConnection != undefined && loaderInfo.parameters.rtmpConnection != ""){
_app.model.rtmpConnectionURL = loaderInfo.parameters.rtmpConnection;
}
if(loaderInfo.parameters.rtmpStream != undefined && loaderInfo.parameters.rtmpStream != ""){
_app.model.rtmpStream = loaderInfo.parameters.rtmpStream;
}
}
// Hard coding this in for now until we can come up with a better solution for 5.0 to avoid XSS.
ExternalInterface.call('videojs.Flash.onReady', ExternalInterface.objectID);
/*if(loaderInfo.parameters.readyFunction != undefined){
try{
ExternalInterface.call(_app.model.cleanEIString(loaderInfo.parameters.readyFunction), ExternalInterface.objectID);
}
catch(e:Error){
if (loaderInfo.parameters.debug != undefined && loaderInfo.parameters.debug == "true") {
throw new Error(e.message);
}
}
}*/
}
private function onAddedToStage(e:Event):void{
stage.addEventListener(MouseEvent.CLICK, onStageClick);
stage.addEventListener(Event.RESIZE, onStageResize);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_stageSizeTimer.start();
}
private function onStageSizeTimerTick(e:TimerEvent):void{
if(stage.stageWidth > 0 && stage.stageHeight > 0){
_stageSizeTimer.stop();
_stageSizeTimer.removeEventListener(TimerEvent.TIMER, onStageSizeTimerTick);
init();
}
}
private function onStageResize(e:Event):void{
if(_app != null){
_app.model.stageRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
_app.model.broadcastEvent(new VideoJSEvent(VideoJSEvent.STAGE_RESIZE, {}));
}
}
private function onAppendBufferCalled(base64str:String):void{
var bytes:ByteArray = Base64.decode(base64str);
// write the bytes to the provider
_app.model.appendBuffer(bytes);
}
private function onAppendChunkReadyCalled(fnName:String):void{
var bytes:ByteArray = Base64.decode(ExternalInterface.call(fnName));
// write the bytes to the provider
_app.model.appendBuffer(bytes);
}
private function onAdjustCurrentTimeCalled(pValue:Number):void {
_app.model.adjustCurrentTime(pValue);
}
private function onEchoCalled(pResponse:* = null):*{
return pResponse;
}
private function onEndOfStreamCalled():*{
_app.model.endOfStream();
}
private function onAbortCalled():*{
_app.model.abort();
}
private function onDiscontinuityCalled():*{
_app.model.discontinuity();
}
private function onGetPropertyCalled(pPropertyName:String = ""):*{
switch(pPropertyName){
case "mode":
return _app.model.mode;
case "autoplay":
return _app.model.autoplay;
case "loop":
return _app.model.loop;
case "preload":
return _app.model.preload;
break;
case "metadata":
return _app.model.metadata;
break;
case "duration":
return _app.model.duration;
break;
case "eventProxyFunction":
return _app.model.jsEventProxyName;
break;
case "errorEventProxyFunction":
return _app.model.jsErrorEventProxyName;
break;
case "currentSrc":
return _app.model.src;
break;
case "currentTime":
return _app.model.time;
break;
case "time":
return _app.model.time;
break;
case "initialTime":
return 0;
break;
case "defaultPlaybackRate":
return 1;
break;
case "ended":
return _app.model.hasEnded;
break;
case "volume":
return _app.model.volume;
break;
case "muted":
return _app.model.muted;
break;
case "paused":
return _app.model.paused;
break;
case "seeking":
return _app.model.seeking;
break;
case "networkState":
return _app.model.networkState;
break;
case "readyState":
return _app.model.readyState;
break;
case "buffered":
return _app.model.buffered;
break;
case "bufferedBytesStart":
return 0;
break;
case "bufferedBytesEnd":
return _app.model.bufferedBytesEnd;
break;
case "bytesTotal":
return _app.model.bytesTotal;
break;
case "videoWidth":
return _app.model.videoWidth;
break;
case "videoHeight":
return _app.model.videoHeight;
break;
case "rtmpConnection":
return _app.model.rtmpConnectionURL;
break;
case "rtmpStream":
return _app.model.rtmpStream;
break;
case "getVideoPlaybackQuality":
return _app.model.videoPlaybackQuality;
break;
}
return null;
}
private function onSetPropertyCalled(pPropertyName:String = "", pValue:* = null):void{
switch(pPropertyName){
case "duration":
_app.model.duration = Number(pValue);
break;
case "mode":
_app.model.mode = String(pValue);
break;
case "loop":
_app.model.loop = _app.model.humanToBoolean(pValue);
break;
case "background":
_app.model.backgroundColor = _app.model.hexToNumber(String(pValue));
_app.model.backgroundAlpha = 1;
break;
case "eventProxyFunction":
_app.model.jsEventProxyName = String(pValue);
break;
case "errorEventProxyFunction":
_app.model.jsErrorEventProxyName = String(pValue);
break;
case "autoplay":
_app.model.autoplay = _app.model.humanToBoolean(pValue);
if (_app.model.autoplay) {
_app.model.preload = "auto";
}
break;
case "preload":
_app.model.preload = String(pValue);
break;
case "src":
// same as when vjs_src() is called directly
onSrcCalled(pValue);
break;
case "currentTime":
_app.model.seekBySeconds(Number(pValue));
break;
case "currentPercent":
_app.model.seekByPercent(Number(pValue));
break;
case "muted":
_app.model.muted = _app.model.humanToBoolean(pValue);
break;
case "volume":
_app.model.volume = Number(pValue);
break;
case "rtmpConnection":
_app.model.rtmpConnectionURL = String(pValue);
break;
case "rtmpStream":
_app.model.rtmpStream = String(pValue);
break;
default:
_app.model.broadcastErrorEventExternally(ExternalErrorEventName.PROPERTY_NOT_FOUND, pPropertyName);
break;
}
}
private function onAutoplayCalled(pAutoplay:* = false):void{
_app.model.autoplay = _app.model.humanToBoolean(pAutoplay);
}
private function isExternalMSObjectURL(pSrc:*):Boolean{
return pSrc.indexOf('blob:vjs-media-source/') === 0;
}
private function openExternalMSObject(pSrc:*):void{
var cleanSrc:String
if (/^blob:vjs-media-source\/\d+$/.test(pSrc)) {
cleanSrc = pSrc;
} else {
cleanSrc = _app.model.cleanEIString(pSrc);
}
ExternalInterface.call('videojs.MediaSource.open', cleanSrc, ExternalInterface.objectID);
}
private function onSrcCalled(pSrc:* = ""):void{
// check if an external media source object will provide the video data
if (isExternalMSObjectURL(pSrc)) {
// null is passed to the netstream which enables appendBytes mode
_app.model.src = null;
// open the media source object for creating a source buffer
// and provide a reference to this swf for passing data from the soure buffer
openExternalMSObject(pSrc);
// ExternalInterface.call('videojs.MediaSource.sourceBufferUrls["' + pSrc + '"]', ExternalInterface.objectID);
} else {
_app.model.src = String(pSrc);
}
}
private function onLoadCalled():void{
_app.model.load();
}
private function onPlayCalled():void{
_app.model.play();
}
private function onPauseCalled():void{
_app.model.pause();
}
private function onResumeCalled():void{
_app.model.resume();
}
private function onStopCalled():void{
_app.model.stop();
}
private function onUncaughtError(e:Event):void{
e.preventDefault();
}
private function onStageClick(e:MouseEvent):void{
_app.model.broadcastEventExternally(ExternalEventName.ON_STAGE_CLICK);
}
}
}