nativeVideo.js 7.5 KB

var client;    //客户端
var pubResultObj; // 存储底层给我的推流结果

var playResultObj;//存储底层给我的播流结果


var playVideoId; //播放的流的videoId
var pushVideoId; //推流的videoId
var playCallbackDate; //视频更新的callBack
var nativeVideo={};
window.nativeVideo=nativeVideo;

nativeVideo.nativeInit=function (_client){
    client=_client;
};



var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端


function mobileCallback(_param) {

    console.log(_param);

    if(isiOS){
        console.log(isiOS);
        _js2native(_param);

    }else if (isAndroid){
        console.log(isAndroid);
        window.xdyAndroid._js2native(_param);

    }
}






function teaTimeStamp() {
    var _teatimestamp= Date.parse(new Date());
    return _teatimestamp;
}



$(function () {

    //学生视频按钮点击
    $(".stuVideo-video").click(function (e) {
        console.log("stuVideo-video点击");
        console.log( $(".studentVideo").offset().left);

        $("#stuVideo-video").html(" ");
        $("#stuVideo-video").removeClass("stuVideo-video");
        $("#stuVideo-video").addClass("stuVideo-videoBg");

        $("#stuVideo-voice").html("");
        $("#stuVideo-voice").addClass("stuVideo-voice");
        $("#stuVideo-voice").removeClass("stuVideo-voiceBg");

        $(".studentVideo").css("background", "#CCCCCC");

        var pcVideoTop=$(".studentVideo").offset().top;
        var pcVideoLeft=$(".studentVideo").offset().left;
        var pcVideoW=$(".studentVideo").width();
        var pcVideoH=pcVideoW*3/4;

        //获取位置
        var scrollW=document.body.scrollWidth||document.documentElement.scrollWidth;
        var videoStuXFloat=pcVideoLeft/scrollW*100;
        var videoStuX=Math.round(videoStuXFloat*100)/100;

        var scrollH=document.body.scrollHeight||document.documentElement.scrollHeight;
        var videoStuYFloat=pcVideoTop/scrollH*100;
        var videoStuY=Math.round(videoStuYFloat*100)/100;


        //宽高
        var   videoStuWFloat=pcVideoW/scrollW*100;
        var  videoStuW=Math.round(videoStuWFloat*100)/100;

        var  videoStuHFloat=pcVideoH/scrollH*100;
        var  videoStuH=Math.round(videoStuHFloat*100)/100;

        console.log(videoStuX,videoStuY,videoStuW,videoStuH);


        //创建显示nativeVideo学生窗口,进行推流
        var creatTeaNativeVideoObj={
            "type":0,
            "videoId":teaTimeStamp(),
            "data":{
                "wintype":'rtmp',
                "x": videoStuX,
                "y": videoStuY,
                "width": videoStuW,
                "height": videoStuH
            }
        };

        console.log(creatTeaNativeVideoObj);
        var creatTeaNativeVideoJson=JSON.stringify(creatTeaNativeVideoObj);
        console.log(creatTeaNativeVideoJson);
        mobileCallback(creatTeaNativeVideoJson);

        //获取学生推流地址
        achieveStuPushStream();

    });


    //学生挂断
    $(".stuVideo-videoBg").click(function (e) {

    })

    //学生音频按钮点击
    $("#stuVideo-voice").click(function (e) {
        console.log("stuVideo-voice点击");
        $("#stuVideo-video").html("");
        $("#stuVideo-video").removeClass("stuVideo-videoBg");
        $("#stuVideo-video").addClass("stuVideo-video");

        $("#stuVideo-voice").html("");
        $("#stuVideo-voice").removeClass("stuVideo-voice");
        $("#stuVideo-voice").addClass("stuVideo-voiceBg");

    });

    //根据传进来的消息,创建老师NativeVideo窗口,进行播流
    client.on(MessageTypes.VIDEO_UPDATE,videoUpdate);





});

function videoUpdate(callbackDate){

    playCallbackDate=callbackDate;

    if ((callbackDate.status==1)&&(callbackDate.channelId!=='')){

        var scrollW=document.body.scrollWidth||document.documentElement.scrollWidth;
        var scrollH=document.body.scrollHeight||document.documentElement.scrollHeight;
        var videoTeaXFloat=(scrollW-246)/scrollW*100;
        var videoTeaX=Math.round(videoTeaXFloat*100)/100;

        var videoTeaYFloat=241/scrollH*100;
        var videoTeaY=Math.round(videoTeaYFloat*100)/100;

        var creatStuNativeVideoObj={
            "type":0,
            "videoId":teaTimeStamp(),
            "data":{
                "x": videoTeaX,
                "y": videoTeaY,
                "width": 30,
                "height": 30
            }
        };

        var creatStuNativeVideoJson=JSON.stringify(creatStuNativeVideoObj);
        mobileCallback(creatStuNativeVideoJson);


        //获取播流地址
        achievePlayStream();

    }

}


//从底层获取播流地址,然后传给nativa进行播放
function achievePlayStream(){

    var paramInfo={
        "type": "rtmp"
    };
    var playResult=client.getVideoPlayPath(paramInfo);

    console.log("执行getVideoPlayPath,返回的结果",playResult);

    playResultObj=playResult;
    if (playResult.code==0){

        var _playMsg={
            "type": 2,
            "videoId": teaTimeStamp(),
            "data":{
                "video": true, // true/false
                "audio": true, // true/false
                "url":playResult.publishUrl
            }
        };

        //播流的videoId
        playVideoId=_playMsg.videoId;

        //传给native
        mobileCallback(_playMsg);
    }


}

//从底层获取推流地址
function achievePushStream(){

    var paramInfo={
        "type": "live"
    };
    var pubResult=client.getVideoPublishPath(paramInfo);

    console.log("执行getVideoPublishPath,返回的结果",pubResult)

    pubResultObj=pubResult;


    //code为0,代表成功
    if (pubResult.code==0){

        var _publishMsg={
            "type": 10,
            "videoId": teaTimeStamp(),
            "data":{
                "video": true, // true/false
                "audio": true, // true/false
                "url":pubResult.publishUrl
            }
        };


        //推流时的VideoId
        var pushVideoId=_publishMsg.videoId;


        //传给native
        mobileCallback(_publishMsg);

    }

};



//native传给我
nativeVideo._native2js=function (_fromNative){

    var fromNative=JSON.parse(_fromNative);
    var type=fromNative.type;

    switch (type){
        case 0: SucessError(_fromNative);break;
        default:break;
    }
};

function SucessError(_fromNative) {

    var successErrorData=_fromNative.data;
    //0成功  1失败
    if (successErrorData.result==0){

        //将之前底层取的推流结果传递给底层
        var _successErrorResult= client.publishVideo(pubResultObj);

        if(_successErrorResult.code==0){
            console.log("底层广播推流成功");
        } else if(_successErrorResult.code==1){
            console.log("底层广播推流失败,关闭推流");

            //应用层关闭流
            var _closeStream={
                "type": 5,
                "videoId":pushVideoId
            }

            var paramInfo={
                "nodeId": 0,//用户的nodeId,默认0,就是自己的
            };
            client.stopPublishVideo(paramInfo);
        }




    }
    else if (successErrorData.result==1){
        console.log("native失败了,错误信息是",successErrorData.errmsg);

        var paramInfo={
            "nodeId": 0,//用户的nodeId,默认0,就是自己的
        };
        client.stopPublishVideo(paramInfo);

        //应用层出弹框提示
        $(".pop").trigger('click');


    }
}