ScreenShareApe.js 17.1 KB
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
//*
// 屏幕共享模块
// */

import Loger from "../Loger";
import Ape from "./Ape";
import xdysdk from "libs/xdysdk";
import $ from "jquery";
import ClassDataProxy from "proxy/ClassDataProxy";
import FlashToolComponet from "libs/FlashToolComponet";
import HLSPlayer from 'tinyhls';
import UIControlApe from './UIControlApe';
import ErrorApe from './ErrorApe';
let loger = Loger.getLoger('PC-ScreenShareApe');

const FLASH_PATH = './FlashVideoPlayer.swf';//flash路径
class ScreenShareApe extends Ape {
    constructor() {
        super();
        this._isEndShare = false;//判断是否点击结束屏幕共享
        this.isLoading = false;//当前是否正在下载插件
        this.isOpenScreenShare=false;//是否已经开启屏幕共享
        this.screenFlashPlayer;
        this.h5ShareView;
        this.addEvent();
        this.isOpenScreenShare=false;
        UIControlApe.enabledLeftBar();
        UIControlApe.enabledBottomBar();
        UIControlApe.showBottomDocControlBar();
    }

    init() {
        //默认隐藏
        //屏幕共享事件
        $("#screenSharingOut").on("click", this._openScreenSharingHandler.bind(this));//开启屏幕共享
        $("#stopShare").on("click", this._closeScreenSharingHandler.bind(this));//终止共享
        //点击开启之后,为下载插件时的取消操作
        $('.downloadCancel').on("click", this._closeScreenSharingHandler.bind(this));
        //下载
        $('#download').on("click", this._downloading.bind(this));

        /*

        //点击终止共享后弹出界面->弹出界面的按钮
        $('.endShareA').on('click',this._hidePopBox.bind(this));//关闭弹出框
        $('.endShareCancel').on('click',this._hidePopBox.bind(this));//弹出框界面点击取消
        $('.endShareEnsure').on('click',this._closeScreenSharingHandler.bind(this));//弹出框界面点击确定->停止共享
        */

    }


    addEvent() {
        xdysdk.on("class_get_info_success", this._classGetInfoSuccessHandler.bind(this));//课堂数据获取成功之后就可以创建屏幕共享的flash或H5播放器
        xdysdk.on("media_stop_publish", this._mediaStopPublishHandler.bind(this));

        //屏幕共享相关
        xdysdk.on("screen_share_play", this._screenSharePlayHandler.bind(this));//收到屏幕共享播放
        xdysdk.on("screen_share_stop", this._screenShareStopHandler.bind(this));//收到屏幕共享停止
        xdysdk.on("publish_screen_share_success", this._publishScreenSuccessHandler.bind(this));//屏幕共享流发布成
        xdysdk.on("publish_screen_share_faile", this._publishScreenFailedHandler.bind(this));//屏幕共享流发布失败
        xdysdk.on("publish_screen_share_disconnect", this._screenShareConnectedHandler.bind(this));//屏幕共享服务器没有连接
        xdysdk.on("publish_screen_share_connected", this._screenShareDisconnectedHandler.bind(this));//获取屏幕共享服务器已经连接
    }
    //鼠标点击事件-------------------
    //屏幕共享
    _openScreenSharingHandler(){
        this._showScreenShareBox()
        UIControlApe.disEnabledLeftBar();
        UIControlApe.disEnabledBottomBar();
        UIControlApe.hideBottomDocControlBar();
        //调用sdk打开屏幕共享
        this._startOpenScreenShare();
        this._emit(ScreenShareApe.SCENE_SCREEN_SHARE_CHANGE,ClassDataProxy.SCENE_SCREEN_SHARE);
    }
    _onClickStopShareHandler(){
        loger.log('点击终止共享')
        //$('.screenSharingGroup').hide();
        //$('.sharingMsgBox').hide();
        this.isLoading = false
        this._showPopBox();
    }
    //在下载界面点击取消
    _closeScreenSharingHandler(){
        this.isLoading = false
        this._hidePopBox();
       // $('.screenSharingGroup').hide();
        this._hideScreenShareBox();
        this._stopScreenShare();
        this.isOpenScreenShare=false;
        UIControlApe.enabledLeftBar();
        UIControlApe.enabledBottomBar();
        UIControlApe.showBottomDocControlBar();

    }
    //在已经共享的过程中点击停止共享
    _closeSharingHandler(){
        console.log('在已经共享的过程中点击停止共享')
        this._showPopBox();
    }
    //下载插件
    _downloading(){
        this.isLoading = true;
        this._showLoadingShareTool();
        window.location.href =ClassDataProxy.ssTunnelAppURL;// 'http://baseh5.dev.3mang.com:90/Setup/0503/Xuedianyun.exe';
    }


    //事件监听-----------
    //停止当前的所有推流
    _mediaStopPublishHandler(){
        this.isLoading = false
        if(ClassDataProxy.userRole==ClassDataProxy.USER_HOST){
            loger.log('停止当前的所有推流->');
            this._stopScreenShare();
            this.isOpenScreenShare=false;
            UIControlApe.enabledLeftBar();
            UIControlApe.enabledBottomBar();
            UIControlApe.showBottomDocControlBar();
        }
        this._hideScreenShareBox();
    }

    _classGetInfoSuccessHandler(_data) {
        this.init();
        console.log('_classGetInfoSuccessHandler');
        if(_data&&_data.userRole!=ClassDataProxy.USER_HOST){
            $('.sharingMsgBox').hide();
            if (ClassDataProxy.isRecordPlayBack) {
                loger.log('屏幕共享录制回放->创建h5播放器');
                //创建h5播放器
                $('.h5ScreenShareBox').show();
                this.h5ShareView= new HLSPlayer(document.getElementById('h5ScreenShare'));
                this.h5ShareView.divName='h5ScreenShare';
                $("#h5ScreenShare")[0].poster = "";//不需要背景图片
            }else {
                //创建flash播放器
                loger.log('屏幕共享->创建flash播放器');
                $('.screenMediaShareBox').show();
                this.creatFlashPlayer();
            }
        }else {
            loger.log('屏幕共享不需要创建播放器->');
        }
    }

    //创建flash 播放器
    creatFlashPlayer() {
     /*   this.screenFlashPlayer= new FlashToolComponet();
        this.screenFlashPlayer.init({
            swfUrl: './FlashVideoPlayer.swf',
            divName: 'screenMediaShare',
            mediaId: 0,
            type: FlashToolComponet.VIDEO,//audio 或video
            callBack: this._mediaPlayerCallback.bind(this),
            videoImage: '',
            audioImage:''
        });*/

        this.screenFlashPlayer = new FlashToolComponet();
        this.screenFlashPlayer.leftGap =0;
        this.screenFlashPlayer.topGap =0;

        this.screenFlashPlayer.init({
            swfUrl: FLASH_PATH,
            divName: "screenMediaShare",
            mediaId: 0,
            layoutType: FlashToolComponet.LAYOUT_V,
            type: FlashToolComponet.SCREEN_SHARE,
            callBack: this._mediaPlayerCallback.bind(this),
            playersDataList: [
                {
                    mediaType: FlashToolComponet.SCREEN_SHARE,
                    autoScale:true,
                    width: 320,
                    height:240,
                    showFullBtn: true,
                    showPublishBtn: false,
                    showUserName:false,
                    level: 1
                }
            ],
            videoImage: '',
            audioImage: '',
            audioBtnImage: '',
            videoBtnImage: '',
            stopPublishImage: '',
            backgroundColor: 0x353536
        });
    }
    _mediaPlayerCallback(_data){
        loger.log("屏幕共享播放器监听返回->",_data);
    }
    _screenSharePlayHandler(_data){
        if(ClassDataProxy.userRole==ClassDataProxy.USER_HOST){
            loger.log('收到屏幕共享播放->老师不需要播放');
            return;
        }

        this._showScreenShareBox();
        UIControlApe.disEnabledLeftBar();
        UIControlApe.disEnabledBottomBar();
        UIControlApe.hideBottomDocControlBar();
        if (ClassDataProxy.isRecordPlayBack) {
            let seek = parseInt(_data.seek);
            if (seek < 1) {
                seek = 1;
            }

            if( this.h5ShareView){
                loger.log('收到屏幕共享播放->seek->'+seek);
                this.h5ShareView.rtmpURL=_data.replay;
                this.h5ShareView.nodeId=_data.nodeId;
                this.h5ShareView.mediaId=_data.mediaId;
                this.h5ShareView.IS_VIDEO=false;
                this.h5ShareView.play(_data.replay, HLSPlayer.MODE_VOD,seek);
            }else {
                loger.log('收到屏幕共享播放->HLS播放器不存在->');
            }

        } else {
           /* if( this.screenFlashPlayer){
                loger.log('flash->收到屏幕共享播放->',_data);
                this.screenFlashPlayer.playMedia({
                    rtmpUrl:_data.rtmpUrl,
                    mediaId:_data.mediaId,
                    mediaType:FlashToolComponet.VIDEO,
                    nodeId:_data.fromNodeId,
                    userRole:_data.userRole,
                    screenWidth:_data.screenWidth,
                    screenHeight:_data.screenHeight,
                    showFullBtn:true
                });
            }else {
                loger.log('收到屏幕共享播放->flash播放器不存在');
            }
          */
          this.playMedia({
            rtmpUrl: _data.rtmpUrl,
            mediaId: _data.mediaId,
            mediaType: FlashToolComponet.SCREEN_SHARE,
            nodeId: _data.fromNodeId,
            userRole: _data.userRole,
            userName: _data.userName,
            showFullBtn: true,
            screenWidth:_data.screenWidth||0,
            screenHeight:_data.screenHeight||0
          });
        }

    }
  //播放音视频
  playMedia(_data) {
    if (!_data) {
      loger.warn('playMedia->参数错误--->', _data);
      return;
    }
    if (this.screenFlashPlayer) {
      this.screenFlashPlayer.playMedia(_data);
    } else {
      loger.log('playMedia->没有找到对应的播放器');
    }
  }

  //停止音视频
  stopMedia(_data) {
    if (!_data) {
      loger.warn('stopMedia->参数错误', _data);
      return;
    }
    if (this.screenFlashPlayer) {
      this.screenFlashPlayer.stopFlashPlay(_data);
    } else {
      loger.log('stopMedia->没有找到对应的播放器');
    }
  }

    _screenShareStopHandler(_data){
        this.isLoading = false;
        if(ClassDataProxy.userRole==ClassDataProxy.USER_HOST){
            loger.log('收到屏幕共享停止->老师不需停止');
            return;
        }
        loger.log('收到屏幕共享停止');
        if (ClassDataProxy.isRecordPlayBack) {
            if( this.h5ShareView){
                this.h5ShareView.rtmpURL='';
                this.h5ShareView.nodeId=0;
                this.h5ShareView.mediaId=0;
                this.h5ShareView.stop();
            }

        } else {
           /* if(this.screenFlashPlayer){
                this.screenFlashPlayer.stopMedia({
                    mediaId:_data.mediaId,
                    mediaType:FlashToolComponet.SCREEN_SHARE,
                    nodeId:_data.fromNodeId,
                    userRole:_data.userRole
                });
            }*/
          this.stopMedia({
            mediaId: _data.mediaId,
            mediaType: FlashToolComponet.SCREEN_SHARE,
            nodeId: _data.fromNodeId,
            userRole: _data.userRole
          });
        }
        this._hideScreenShareBox();
        UIControlApe.enabledLeftBar();
        UIControlApe.enabledBottomBar();
        UIControlApe.showBottomDocControlBar();
    }

    _publishScreenSuccessHandler(_dara){
        if(ClassDataProxy.userRole!=ClassDataProxy.USER_HOST){
            loger.log('屏幕共享流发布成->学生不需操作');
            return;
        }
        loger.log('屏幕共享流发布成');
        this.isOpenScreenShare=true;
        UIControlApe.disEnabledLeftBar();
        UIControlApe.disEnabledBottomBar();
        UIControlApe.hideBottomDocControlBar();
        this._showStopShareBtn();
        this._emit(ScreenShareApe.SCENE_SCREEN_SHARE_CHANGE,ClassDataProxy.SCENE_SCREEN_SHARE);
    }
    _publishScreenFailedHandler(_dara){
        if(ClassDataProxy.userRole!=ClassDataProxy.USER_HOST){
            loger.log('屏幕共享流发布失败->学生不需操作');
            return;
        }
        loger.log('屏幕共享流发布失败->关闭共享界面');
        this.isOpenScreenShare=false;
        this.isLoading = false;
        this._hideScreenShareBox();
        UIControlApe.enabledLeftBar();
        UIControlApe.enabledBottomBar();
        UIControlApe.showBottomDocControlBar();
    }
    _screenShareConnectedHandler(_dara){
        if(ClassDataProxy.userRole!=ClassDataProxy.USER_HOST){
            loger.log('屏幕共享服务器没有连接->学生不需操作');
            return;
        }
        loger.log('屏幕共享服务器没有连接');
        //UIControlApe.enabledLeftBar();
        //UIControlApe.enabledBottomBar();
        //UIControlApe.showBottomDocControlBar();
        if(!this.isLoading){
            this._showLoadShareTool();
        }
        if(this.isOpenScreenShare){
            this._stopScreenShare();
        }
        this.isOpenScreenShare=false;
    }
    _screenShareDisconnectedHandler(_dara){
        if(ClassDataProxy.userRole!=ClassDataProxy.USER_HOST){
            loger.log('获取屏幕共享服务器已经连接->学生不需操作');
            return;
        }
        loger.log('获取屏幕共享服务器已经连接');
        this._showLoadingShareTool();
    }

    //----------方法调用---------

    //调用SDK打开共享
    _startOpenScreenShare(){
        loger.log('调用SDK打开共享');
        let result = xdysdk.api("publishScreenShare", {"type": "flash"});
    }
    //调用SDK停止共享
    _stopScreenShare(){
        loger.log('调用SDK停止共享');
        if(ClassDataProxy.userRole!=ClassDataProxy.USER_HOST){
            loger.log('调用SDK停止共享->学生不需操作');
            return;
        }
        if(this.isOpenScreenShare){
            this.isOpenScreenShare=false;
            let result = xdysdk.api("stopPublishScreenShare");
        }
        this.isOpenScreenShare=false;
    }

    _showScreenShareBox(){
        $(".screenSharingGroup").show();
        $(".screenSharingGroup").css("z-index",501);
        $("#screenSharingOut").addClass('active')
    }
    _hideScreenShareBox(){
        $(".screenSharingGroup").css("z-index",-6);
        $("#screenSharingOut").removeClass('active')
    }
    //点击停止共享时的弹出确认框
    _showPopBox() {
        loger.log('点击停止共享时的弹出确认框')
        $('.endShareBox').show();
        $(".masker").show();
    }
    //隐藏弹出确认框
    _hidePopBox() {
        $('.endShareBox').hide();
        $(".masker").hide();
    }

    //显示插件下载界面的1
    _showLoadShareTool(){
        $('#download').show();
        $('.downloadCancel').show();
        $('#opening').hide();
        $('#downloading').hide();
        $('#stopShare').hide();
        $('.sharingMsgBox').addClass('sharingMsgBox1');
        $('.shareBackdrop').hide();
    }
    //正在下载插件中2
    _showLoadingShareTool(){
        $('#download').hide();
        $('#opening').hide();
        $('#downloading').show();
        $('.downloadCancel').show();
        $('#stopShare').hide();
        $('.sharingMsgBox').addClass('sharingMsgBox1');
        $('.shareBackdrop').hide();
    }

    //显示屏幕共享过程中的界面->终止共享的按钮2
    _showStopShareBtn(){
        $('#download').hide();
        $('#opening').hide();
        $('#downloading').hide();
        $('#stopShare').show();
        $('.downloadCancel').hide();
        $('.sharingMsgBox').removeClass('sharingMsgBox1');
        $('.shareBackdrop').show();
    }
    //停止并且隐藏
    stopAndHide(){
        //与点击取消做同样操作
        this._closeScreenSharingHandler()
    }
    clear() {
        loger.log("clear");
        try {
            if(this.h5ShareView){
                this.h5ShareView.rtmpURL='';
                this.h5ShareView.mediaId=0;
                this.h5ShareView.nodeId=0;
                this.h5ShareView.stop()
            }
            this._hideScreenShareBox();
        } catch (err) {
            loger.warn("clear", err.message);
        }
    }

    play() {
        loger.log("play");
        try {
            if(this.h5ShareView){
                this.h5ShareView.resume()
            }
        } catch (err) {
            loger.warn("clear", err.message);
        }

    }

    pause() {
        loger.log("pause");
        try {
            if(this.h5ShareView){
                this.h5ShareView.pause()
            }
        } catch (err) {
            loger.warn("clear", err.message);
        }

    }

    stop() {
        loger.log("stop");
        try {
            if(this.h5ShareView){
                this.h5ShareView.rtmpURL='';
                this.h5ShareView.mediaId=0;
                this.h5ShareView.nodeId=0;
                this.h5ShareView.stop()
            }
            this._hideScreenShareBox();
        } catch (err) {
            loger.warn("clear", err.message);
        }

    }
}
ScreenShareApe.prototype.SCENE_SCREEN_SHARE_CHANGE = ScreenShareApe.SCENE_SCREEN_SHARE_CHANGE = "scene_screen_share_change";
export default ScreenShareApe;