Blame view

trunk/research/players/srs_player/src/srs_player.as 19.8 KB
1 2 3
package
{
    import flash.display.Sprite;
winlin authored
4
    import flash.display.StageAlign;
5
    import flash.display.StageDisplayState;
winlin authored
6 7
    import flash.display.StageScaleMode;
    import flash.events.Event;
8 9
    import flash.events.FullScreenEvent;
    import flash.events.MouseEvent;
winlin authored
10
    import flash.events.NetStatusEvent;
11
    import flash.events.TimerEvent;
winlin authored
12
    import flash.external.ExternalInterface;
13
    import flash.media.SoundTransform;
winlin authored
14 15 16
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
17
    import flash.system.Security;
winlin authored
18 19
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
20
    import flash.utils.Timer;
winlin authored
21
    import flash.utils.setTimeout;
22
    
23 24
    import flashx.textLayout.formats.Float;
    
25 26
    public class srs_player extends Sprite
    {
27
        // user set id.
winlin authored
28
        private var js_id:String = null;
29
        // user set callback
winlin authored
30 31 32
        private var js_on_player_ready:String = null;
        private var js_on_player_metadata:String = null;
        private var js_on_player_timer:String = null;
winlin authored
33
        
34
        // play param url.
winlin authored
35
        private var user_url:String = null;
36
        // play param, user set width and height
winlin authored
37 38
        private var user_w:int = 0;
        private var user_h:int = 0;
39
        // user set dar den:num
winlin authored
40
        private var user_dar_den:int = 0;
41
        private var user_dar_num:int = 0;
42
        // user set fs(fullscreen) refer and percent.
winlin authored
43 44
        private var user_fs_refer:String = null;
        private var user_fs_percent:int = 0;
winlin authored
45
        
winlin authored
46 47 48 49 50 51
        // media specified.
        private var media_conn:NetConnection = null;
        private var media_stream:NetStream = null;
        private var media_video:Video = null;
        private var media_metadata:Object = {};
        private var media_timer:Timer = new Timer(300);
winlin authored
52
        
winlin authored
53
        // controls.
54 55
        // flash donot allow js to set to fullscreen,
        // only allow user click to enter fullscreen.
winlin authored
56
        private var control_fs_mask:Sprite = new Sprite();
57
        
58 59
        public function srs_player()
        {
winlin authored
60
            if (!this.stage) {
winlin authored
61
                this.addEventListener(Event.ADDED_TO_STAGE, this.system_on_add_to_stage);
winlin authored
62
            } else {
winlin authored
63
                this.system_on_add_to_stage(null);
winlin authored
64 65 66
            }
        }
        
winlin authored
67 68 69 70 71 72 73
        /**
        * system event callback, when this control added to stage.
        * the main function.
        */
        private function system_on_add_to_stage(evt:Event):void {
            this.removeEventListener(Event.ADDED_TO_STAGE, this.system_on_add_to_stage);
            
winlin authored
74 75 76
            this.stage.align = StageAlign.TOP_LEFT;
            this.stage.scaleMode = StageScaleMode.NO_SCALE;
            
winlin authored
77
            this.stage.addEventListener(FullScreenEvent.FULL_SCREEN, this.user_on_stage_fullscreen);
78
            
winlin authored
79 80 81
            this.addChild(this.control_fs_mask);
            this.control_fs_mask.buttonMode = true;
            this.control_fs_mask.addEventListener(MouseEvent.CLICK, user_on_click_video);
82
            
winlin authored
83 84 85 86 87 88 89 90 91
            this.contextMenu = new ContextMenu();
            this.contextMenu.hideBuiltInItems();
            
            var flashvars:Object = this.root.loaderInfo.parameters;
            
            if (!flashvars.hasOwnProperty("id")) {
                throw new Error("must specifies the id");
            }
            
winlin authored
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
            this.js_id = flashvars.id;
            this.js_on_player_ready = flashvars.on_player_ready;
            this.js_on_player_metadata = flashvars.on_player_metadata;
            this.js_on_player_timer = flashvars.on_player_timer;
            
            this.media_timer.addEventListener(TimerEvent.TIMER, this.system_on_timer);
            this.media_timer.start();
            
            flash.utils.setTimeout(this.system_on_js_ready, 0);
        }
        
        /**
         * system callack event, when js ready, register callback for js.
         * the actual main function.
         */
        private function system_on_js_ready():void {
            if (!flash.external.ExternalInterface.available) {
                trace("js not ready, try later.");
                flash.utils.setTimeout(this.system_on_js_ready, 100);
                return;
            }
113
            
winlin authored
114 115 116 117
            flash.external.ExternalInterface.addCallback("__play", this.js_call_play);
            flash.external.ExternalInterface.addCallback("__stop", this.js_call_stop);
            flash.external.ExternalInterface.addCallback("__pause", this.js_call_pause);
            flash.external.ExternalInterface.addCallback("__resume", this.js_call_resume);
winlin authored
118
            flash.external.ExternalInterface.addCallback("__set_dar", this.js_call_set_dar);
winlin authored
119 120
            flash.external.ExternalInterface.addCallback("__set_fs", this.js_call_set_fs_size);
            flash.external.ExternalInterface.addCallback("__set_bt", this.js_call_set_bt);
winlin authored
121
            
winlin authored
122
            flash.external.ExternalInterface.call(this.js_on_player_ready, this.js_id);
winlin authored
123 124
        }
        
winlin authored
125 126 127 128 129
        /**
        * system callack event, timer to do some regular tasks.
        */
        private function system_on_timer(evt:TimerEvent):void {
            if (!this.media_stream) {
130 131 132 133 134 135
                trace("stream is null, ignore timer event.");
                return;
            }
            
            trace("notify js the timer event.");
            flash.external.ExternalInterface.call(
winlin authored
136 137 138 139 140 141 142 143 144 145 146
                this.js_on_player_timer, this.js_id, this.media_stream.time, this.media_stream.bufferLength);
        }
        
        /**
         * system callack event, when got metadata from stream.
         * or got video dimension change event(the DAR notification), to update the metadata manually.
         */
        private function system_on_metadata(metadata:Object):void {
            this.media_metadata = metadata;
            
            if (metadata.hasOwnProperty("server")) {
147 148 149 150 151 152 153 154 155
                // for context menu
                var customItems:Array = [new ContextMenuItem("SrsPlayer")];
                if (metadata.hasOwnProperty("server")) {
                    customItems.push(new ContextMenuItem("Server: " + metadata.server));
                }
                if (metadata.hasOwnProperty("contributor")) {
                    customItems.push(new ContextMenuItem("Contributor: " + metadata.contributor));
                }
                contextMenu.customItems = customItems;
winlin authored
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            }
            
            // for js.
            var obj:Object = __get_video_size_object();
            
            obj.server = 'srs';
            obj.contributor = 'winlin';
            
            if (metadata.hasOwnProperty("server")) {
                obj.server = metadata.server;
            }
            if (metadata.hasOwnProperty("contributor")) {
                obj.contributor = metadata.contributor;
            }
            
            var code:int = flash.external.ExternalInterface.call(js_on_player_metadata, js_id, obj);
            if (code != 0) {
                throw new Error("callback on_player_metadata failed. code=" + code);
            }
175 176
        }
        
winlin authored
177 178 179 180
        /**
         * player callack event, when user click video to enter or leave fullscreen.
         */
        private function user_on_stage_fullscreen(evt:FullScreenEvent):void {
181
            if (!evt.fullScreen) {
winlin authored
182
                __execute_user_set_dar();
183
            } else {
winlin authored
184
                __execute_user_enter_fullscreen();
185 186 187
            }
        }
        
winlin authored
188 189 190 191 192 193
        /**
         * user event callback, js cannot enter the fullscreen mode, user must click to.
         */
        private function user_on_click_video(evt:MouseEvent):void {
            if (!this.stage.allowsFullScreen) {
                trace("donot allow fullscreen.");
winlin authored
194 195 196
                return;
            }
            
winlin authored
197 198 199 200 201 202
            // enter fullscreen to get the fullscreen size correctly.
            if (this.stage.displayState == StageDisplayState.FULL_SCREEN) {
                this.stage.displayState = StageDisplayState.NORMAL;
            } else {
                this.stage.displayState = StageDisplayState.FULL_SCREEN;
            }
winlin authored
203 204
        }
        
winlin authored
205 206 207
        /**
         * function for js to call: to pause the stream. ignore if not play.
         */
208
        private function js_call_pause():void {
winlin authored
209 210
            if (this.media_stream) {
                this.media_stream.pause();
winlin authored
211 212 213
            }
        }
        
winlin authored
214 215 216
        /**
         * function for js to call: to resume the stream. ignore if not play.
         */
217
        private function js_call_resume():void {
winlin authored
218 219
            if (this.media_stream) {
                this.media_stream.resume();
winlin authored
220 221 222
            }
        }
        
223
        /**
224 225 226 227 228 229 230
        * to set the DAR, for example, DAR=16:9 where num=16,den=9.
        * @param num, for example, 16. 
        *       use metadata width if 0.
        *       use user specified width if -1.
        * @param den, for example, 9. 
        *       use metadata height if 0.
        *       use user specified height if -1.
231
         */
winlin authored
232
        private function js_call_set_dar(num:int, den:int):void {
winlin authored
233 234
            user_dar_num = num;
            user_dar_den = den;
235
            
winlin authored
236
            flash.utils.setTimeout(__execute_user_set_dar, 0);
237 238 239 240 241 242 243 244 245 246
        }
        
        /**
         * set the fullscreen size data.
         * @refer the refer fullscreen mode. it can be:
         *       video: use video orignal size.
         *       screen: use screen size to rescale video.
         * @param percent, the rescale percent, where
         *       100 means 100%.
         */
247
        private function js_call_set_fs_size(refer:String, percent:int):void {
winlin authored
248 249
            user_fs_refer = refer;
            user_fs_percent = percent;
250 251
        }
        
252 253 254 255 256
        /**
         * set the stream buffer time in seconds.
         * @buffer_time the buffer time in seconds.
         */
        private function js_call_set_bt(buffer_time:Number):void {
winlin authored
257 258
            if (this.media_stream) {
                this.media_stream.bufferTime = buffer_time;
259 260 261
            }
        }
        
winlin authored
262 263 264
        /**
         * function for js to call: to stop the stream. ignore if not play.
         */
265
        private function js_call_stop():void {
winlin authored
266 267 268 269
            if (this.media_video) {
                this.removeChild(this.media_video);
                this.media_video = null;
            }
winlin authored
270 271 272
            if (this.media_stream) {
                this.media_stream.close();
                this.media_stream = null;
winlin authored
273
            }
winlin authored
274 275 276
            if (this.media_conn) {
                this.media_conn.close();
                this.media_conn = null;
winlin authored
277 278 279
            }
        }
        
winlin authored
280 281 282 283 284 285
        /**
         * function for js to call: to play the stream. stop then play.
         * @param url, the rtmp/http url to play.
         * @param _width, the player width.
         * @param _height, the player height.
         * @param buffer_time, the buffer time in seconds. recommend to >=0.5
286
         * @param volume, the volume, 0 is mute, 1 is 100%, 2 is 200%.
winlin authored
287
         */
288
        private function js_call_play(url:String, _width:int, _height:int, buffer_time:Number, volume:Number):void {
winlin authored
289 290 291 292
            this.user_url = url;
            this.user_w = _width;
            this.user_h = _height;
            trace("start to play url: " + this.user_url + ", w=" + this.user_w + ", h=" + this.user_h);
293
            
winlin authored
294 295
            js_call_stop();
            
winlin authored
296 297 298 299
            this.media_conn = new NetConnection();
            this.media_conn.client = {};
            this.media_conn.client.onBWDone = function():void {};
            this.media_conn.addEventListener(NetStatusEvent.NET_STATUS, function(evt:NetStatusEvent):void {
winlin authored
300
                trace ("NetConnection: code=" + evt.info.code);
301
                
302 303 304 305 306 307
                if (evt.info.hasOwnProperty("data") && evt.info.data) {
                    // for context menu
                    var customItems:Array = [new ContextMenuItem("SrsPlayer")];
                    if (evt.info.data.hasOwnProperty("srs_server")) {
                        customItems.push(new ContextMenuItem("Server: " + evt.info.data.srs_server));
                    }
winlin authored
308 309
                    if (evt.info.data.hasOwnProperty("srs_primary_authors")) {
                        customItems.push(new ContextMenuItem("PrimaryAuthors: " + evt.info.data.srs_primary_authors));
310 311 312 313
                    }
                    contextMenu.customItems = customItems;
                }
                
314
                // TODO: FIXME: failed event.
winlin authored
315 316 317 318
                if (evt.info.code != "NetConnection.Connect.Success") {
                    return;
                }
                
winlin authored
319
                media_stream = new NetStream(media_conn);
320
                media_stream.soundTransform = new SoundTransform(volume);
winlin authored
321 322 323 324
                media_stream.bufferTime = buffer_time;
                media_stream.client = {};
                media_stream.client.onMetaData = system_on_metadata;
                media_stream.addEventListener(NetStatusEvent.NET_STATUS, function(evt:NetStatusEvent):void {
winlin authored
325
                    trace ("NetStream: code=" + evt.info.code);
326 327
                    
                    if (evt.info.code == "NetStream.Video.DimensionChange") {
winlin authored
328
                        system_on_metadata(media_metadata);
329
                    }
330 331
                    
                    // TODO: FIXME: failed event.
winlin authored
332
                });
333
                
334
                if (url.indexOf("http") == 0) {
winlin authored
335
                    media_stream.play(url);
336
                } else {
337
                    var streamName:String = url.substr(url.lastIndexOf("/") + 1);
winlin authored
338
                    media_stream.play(streamName);
339
                }
winlin authored
340
                
winlin authored
341 342 343 344 345 346
                media_video = new Video();
                media_video.width = _width;
                media_video.height = _height;
                media_video.attachNetStream(media_stream);
                media_video.smoothing = true;
                addChild(media_video);
347
                
winlin authored
348
                __draw_black_background(_width, _height);
349 350
                
                // lowest layer, for mask to cover it.
winlin authored
351
                setChildIndex(media_video, 0);
winlin authored
352 353
            });
            
354
            if (url.indexOf("http") == 0) {
winlin authored
355
                this.media_conn.connect(null);
356
            } else {
winlin authored
357 358
                var tcUrl:String = this.user_url.substr(0, this.user_url.lastIndexOf("/"));
                this.media_conn.connect(tcUrl);
359 360 361 362 363 364 365 366 367
            }
        }
        
        /**
        * get the "right" size of video,
        * 1. initialize with the original video object size.
        * 2. override with metadata size if specified.
        * 3. override with codec size if specified.
        */
winlin authored
368
        private function __get_video_size_object():Object {
369
            var obj:Object = {
winlin authored
370 371
                width: media_video.width,
                height: media_video.height
372 373
            };
            
374
            // override with metadata size.
winlin authored
375 376
            if (this.media_metadata.hasOwnProperty("width")) {
                obj.width = this.media_metadata.width;
377
            }
winlin authored
378 379
            if (this.media_metadata.hasOwnProperty("height")) {
                obj.height = this.media_metadata.height;
380 381
            }
            
382
            // override with codec size.
winlin authored
383 384
            if (media_video.videoWidth > 0) {
                obj.width = media_video.videoWidth;
385
            }
winlin authored
386 387
            if (media_video.videoHeight > 0) {
                obj.height = media_video.videoHeight;
388 389
            }
            
390 391 392 393 394 395
            return obj;
        }
        
        /**
        * execute the enter fullscreen action.
        */
winlin authored
396 397
        private function __execute_user_enter_fullscreen():void {
            if (!user_fs_refer || user_fs_percent <= 0) {
398
                return;
399
            }
400 401
            
            // change to video size if refer to video.
winlin authored
402
            var obj:Object = __get_video_size_object();
403 404
            
            // get the DAR
winlin authored
405
            var den:int = user_dar_den;
406
            var num:int = user_dar_num;
407
            
408 409
            if (den == 0) {
                den = obj.height;
410
            }
411 412
            if (den == -1) {
                den = this.stage.fullScreenHeight;
413 414
            }
            
415 416
            if (num == 0) {
                num = obj.width;
417
            }
418 419
            if (num == -1) {
                num = this.stage.fullScreenWidth;
420 421 422
            }
                
            // for refer is screen.
winlin authored
423
            if (user_fs_refer == "screen") {
424 425 426 427
                obj = {
                    width: this.stage.fullScreenWidth,
                    height: this.stage.fullScreenHeight
                };
428
            }
429 430
            
            // rescale to fs
winlin authored
431
            __update_video_size(num, den, obj.width * user_fs_percent / 100, obj.height * user_fs_percent / 100, this.stage.fullScreenWidth, this.stage.fullScreenHeight);
432 433 434 435 436
        }
        
        /**
         * for user set dar, or leave fullscreen to recover the dar.
         */
winlin authored
437
        private function __execute_user_set_dar():void {
438
            // get the DAR
winlin authored
439
            var den:int = user_dar_den;
440
            var num:int = user_dar_num;
441
            
winlin authored
442
            var obj:Object = __get_video_size_object();
443
            
444 445
            if (den == 0) {
                den = obj.height;
446
            }
447 448
            if (den == -1) {
                den = this.user_h;
449 450
            }
            
451 452
            if (num == 0) {
                num = obj.width;
453
            }
454 455
            if (num == -1) {
                num = this.user_w;
456 457
            }
            
winlin authored
458
            __update_video_size(num, den, this.user_w, this.user_h, this.user_w, this.user_h);
459 460 461 462 463 464 465 466 467 468 469
        }
        
        /**
        * update the video width and height, 
        * according to the specifies DAR(den:num) and max size(w:h).
        * set the position of video(x,y) specifies by size(sw:sh),
        * and update the bg to size(sw:sh).
        * @param _num/_den the DAR. use to rescale the player together with paper size.
        * @param _w/_h the video draw paper size. used to rescale the player together with DAR.
        * @param _sw/_wh the stage size, >= paper size. used to center the player.
        */
winlin authored
470
        private function __update_video_size(_num:int, _den:int, _w:int, _h:int, _sw:int, _sh:int):void {
471
            if (!this.media_video || _den <= 0 || _num <= 0) {
472 473 474 475 476
                return;
            }
            
            // set DAR.
            // calc the height by DAR
477
            var _height:int = _w * _den / _num;
478
            if (_height <= _h) {
winlin authored
479 480
                this.media_video.width = _w;
                this.media_video.height = _height;
481 482
            } else {
                // height overflow, calc the width by DAR
483
                var _width:int = _h * _num / _den;
484
                
winlin authored
485 486
                this.media_video.width = _width;
                this.media_video.height = _h;
487 488 489
            }
            
            // align center.
winlin authored
490 491
            this.media_video.x = (_sw - this.media_video.width) / 2;
            this.media_video.y = (_sh - this.media_video.height) / 2;
492
            
winlin authored
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
            __draw_black_background(_sw, _sh);
        }
        
        /**
        * draw black background and draw the fullscreen mask.
        */
        private function __draw_black_background(_width:int, _height:int):void {
            // draw black bg.
            this.graphics.beginFill(0x00, 1.0);
            this.graphics.drawRect(0, 0, _width, _height);
            this.graphics.endFill();
            
            // draw the fs mask.
            this.control_fs_mask.graphics.beginFill(0xff0000, 0);
            this.control_fs_mask.graphics.drawRect(0, 0, _width, _height);
            this.control_fs_mask.graphics.endFill();
509
        }
510 511
    }
}