var bit = [64, 128, 256, 512, 1024, 1536, 2048, 3072, 4096, 5120, 6144, 8192, 10240, 12288];
var bit1 = [128, 256, 512, 1024, 1536, 2048];
var frame = [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];
var vbr = [0, 1, 2, 3, 4, 5];
var propMainBitRate1 = 102.4;
var propMainBitRate2 = 81.92;
var propMainBitRate3 = 81.92;
var propSubBitRate1 = 20.48;
var propSubBitRate2 = 20.48;
var propSubBitRate3 = 16;
var typeSet = false;
var subResolutions = "";
var deviceCap = {},currentchannel = 0;
var user = config.user,
    pwd = config.password;
var gs_arrResolution = config.gs_arrResolution;
var gs_stream_type_map_table = [{
        strStreamTypeID: "1bar",
        strResolutionID: "mResolutionWH",
        strFrameRateID: "mFrameRate",
        strXMLStreamName: "MainStreamCapability"
    },
    {
        strStreamTypeID: "2bar",
        strResolutionID: "sResolutionWH",
        strFrameRateID: "sFrameRate",
        strXMLStreamName: "SubStreamCapability"
    },

];
var gs_stream_cap_xml = "";
var gs_main_stream_xml = "";
var gs_first_stream_xml = "";
var gs_third_stream_xml = "";
$(document).ready(function() {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 220
        });
    }
    deviceCap = config.isSupportDeviceCapV2 ? config.deviceCapV2[currentchannel] : config.deviceCapV1[currentchannel];
    inInterface();
    $("#NVR_CHNBMchn").empty(); //Channel
    $("#sNVR_CHNBMchn").empty(); //Channel
    for (var i = 1; i <= config.deviceInfo.VideoInputChannels; i++) {
        $("#NVR_CHNBMchn").append('<option class="option" value="' + i + '">' + i + '</option>');
        $("#sNVR_CHNBMchn").append('<option class="option" value="' + i + '">' + i + '</option>');
        if (i != 1) {
            $("#coppyInds").append('<option class="option" value="' + i + '">' + i + '</option>');
        }
    }
    // 默认第一个通道
    $("#NVR_CHNBMchn").val("1").trigger('change.select2')
    $("#sNVR_CHNBMchn").val("1").trigger('change.select2')
    getStreamCap(1);
    getMainstream(1);
    getSubstream(1);
    $('#myTab a').click(function(e) {
        e.preventDefault()
        $(this).tab('show')
        if ($(this).attr("aria-controls") == "mianstream") {
            $("#mBitrate").empty();
            for (var i = 0; i < bit.length; i++) {
                $("#mBitrate").append('<option class="option" value="' + bit[i] + '">' + bit[i] + '</option>');
            }
            getMainstream($("#NVR_CHNBMchn").val() - 0);
        } else if ($(this).attr("aria-controls") == "substream") {
            $("#sBitrate").empty();
            for (var i = 0; i < bit1.length; i++) {
                $("#sBitrate").append('<option class="option" value="' + bit1[i] + '">' + bit1[i] + '</option>');
            }
            $(".isShowRateSub").show()
            getSubstream($("#sNVR_CHNBMchn").val() - 0);
        }
    });
    // 复制到
    $("#coppyto").click(function() {
            if ($("#coppyInds").val() == "00") {
                typeSet = true
                for (var i = 1; i <= config.deviceInfo.VideoInputChannels; i++) {
                    setMainstream(i);
                    setSubstream(i);
                }
            } else {
                typeSet = false
                setMainstream($("#coppyInds").val() - 0);
                setSubstream($("#coppyInds").val() - 0);
            }
        })
        //主码流通道改变
    $("#NVR_CHNBMchn").change(function() {
        var chid = $("#NVR_CHNBMchn").val() * 1;
        currentchannel = chid - 1;
        if (!config.isSupportDeviceCapV2&&config.deviceCapV1[currentchannel] == null) {
            getDeviceCap(chid);
        }
        deviceCap = config.isSupportDeviceCapV2 ? config.deviceCapV2[currentchannel] : config.deviceCapV1[currentchannel];
        $("#coppyInds").empty()
        $("#coppyInds").append('<option class="option" value="00">' + $.i18n.prop("i18n-video-all") + '</option>')
        for (var i = 1; i <= config.deviceInfo.VideoInputChannels; i++) {
            if (i != chid) {
                $("#coppyInds").append('<option class="option" value="' + i + '">' + i + '</option>');
            }
        }
        getStreamCap(chid);
        getMainstream(chid);
        getSubstream(chid);
        InitMainStreamResolutionData();
        RefreshMainStreamMaxFrameDataByMainStreamResolution();
    });
    // 子码流通道改变
    $("#sNVR_CHNBMchn").change(function() {
        var chid = $("#sNVR_CHNBMchn").val() * 1;
        getStreamCap(chid);
        getSubstream(chid);
    });
    // 主码流分辨率改变
    $("#mResolutionWH").change(function() {
        //根据选择的主码流的分辨率初始化帧率数据
        RefreshMainStreamMaxFrameDataByMainStreamResolution();
        //根据主码流的分辨率来插入子码流的分辨率（包括了所有的子码流）
        // RefreshSubStreamResolutionDataByMainStreamResulution();
        //根据主码流的分辨率来插入子码流的最大帧率（包括了对所有子码流的操作）
        // RefreshSubStreamMaxFrameByMainAndSubStreamResulution();
        //刷新数据
        RefreshMainStreamDataFromNetData(0);
        // RefreshSubStreamDataFromNetData();
        // RefreshMoblieStreamDataFromNetData();
        // RefreshForthStreamDataFromNetData();
        // var mainResolution = $(this).select2("val");
        // switch (mainResolution) {
        //     case "1920*1080":
        //         FrameRate(propMainBitRate1, $("#mFrameRate").val(), "#main_bitrate_tip");
        //         break;
        //     case "1280*960":
        //         FrameRate(propMainBitRate2, $("#mFrameRate").val(), "#main_bitrate_tip");
        //         break;
        //     case "1280*720":
        //         FrameRate(propMainBitRate3, $("#mFrameRate").val(), "#main_bitrate_tip");
        //         break;
        //     default:
        //         FrameRate(propMainBitRate1, $("#mFrameRate").val(), "#main_bitrate_tip");
        //         break;
        // }
    });
    //子码分辩率选择事件
    $("#sResolutionWH").change(function() {
        subResolutions = $(this).select2("val");
        RefreshSubStreamMaxFrameByMainAndSubStreamResulution();
        RefreshSubStreamDataFromNetData();
    });
    $("#bitrateType").change(function() {
        changeVCbr();
    });
    $("#sbitrateType").change(function() {
        changesVCbr();
    });
    $("#mBitPU").change(function() {
        changeBitPU("m");
    });
    $("#sBitPU").change(function() {
        changeBitPU("s");
    });
    // $("#mainSave").click(function() {
    //     setMainstream();
    // });

    $("#subSave").click(function() {
        typeSet = false
        setMainstream();
        setSubstream();
    });
});

function inInterface() {
    //添加预设值
    for (var i = 0; i < bit.length; i++) {
        if (bit[i] >= 256) {
            $("<option value='" + bit[i] + "'>" + bit[i] + "</option>").appendTo("#mBitrate"); //主码位
        }
        if (3072 > bit[i]) {
            $("<option value='" + bit[i] + "'>" + bit[i] + "</option>").appendTo("#sBitrate"); //福码流一 sBitrate
        }
    }
}

function getStreamCap(ch) {
    $.ajax({
        url: config.baseUrl + "/Streams/" + ch + "/CapabilityV2",
        type: "GET",
        async: false,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            gs_stream_cap_xml = xmlDoc;
            InitUIByStreamCap();
        },
        error: function(xhr, textStatus, errorThrown) {}
    });

}

function InitUIByStreamCap() {
    //刷新编码能力
    RefreshALLStreamCodec();

    //根据能力来插入主码流的分辨率
    InitMainStreamResolutionData();

    //根据选择的主码流的分辨率初始化帧率数据
    // RefreshMainStreamMaxFrameDataByMainStreamResolution();

    //根据主码流的分辨率来插入子码流的分辨率和最大帧率（包括了所有的子码流）
    // RefreshSubStreamResolutionDataByMainStreamResulution();

    //选择当前子码流的分辨率
    //$("#sResolutionWH").val();

    //根据主码流和子码流的分辨率来插入子码流的最大帧率（包括了对所有子码流的操作）
    // RefreshSubStreamMaxFrameByMainAndSubStreamResulution();

}


function RefreshForthStreamMaxFrameByMainAndSubStreamResulution() {
    var subStreamTypeCount = deviceCap.StreamCount - 1
        //获取当前界面选择的主码流的分辨率
    var selectedMainResolution = $("#mResolutionWH").select2("val");

    //根据选择的主码流的分辨率获取主码流在xml的位置
    var index = getMainStreamPosByMainStreamResolutin(selectedMainResolution);
    for (var i = 0; i < subStreamTypeCount; i++) {
        //获取当前选择的主码流对应的支持的子码流的个数
        var subCount = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find(gs_stream_type_map_table[i + 1].strXMLStreamName + "List").find('StreamCapability').length;
        var maxFrame = 25;
        for (var j = 0; j < subCount; j++) {
            //获取当前界面选择的子码流的分辨率
            var selectedSubResolution = $("#forthResolutionWH").val();
            var subResolutionW = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find(gs_stream_type_map_table[i + 1].strXMLStreamName + "List").find('StreamCapability').eq(j).find('ResolutionWidth').eq(0).text();
            var subResolutionH = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find(gs_stream_type_map_table[i + 1].strXMLStreamName + "List").find('StreamCapability').eq(j).find('ResolutionHeigth').eq(0).text();
            if (selectedSubResolution == subResolutionW + "*" + subResolutionH) {
                maxFrame = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find(gs_stream_type_map_table[i + 1].strXMLStreamName + "List").find('StreamCapability').eq(j).find('MaxFrameRate').eq(0).text();
                break;
            }
        }
        //插入帧率数据
        $("#forthFrameRate").empty();
        for (var k = 0; k < maxFrame; k++) {
            var nID = k + 1;
            $("<option value='" + nID + "'>" + frame[k] + "</option>").appendTo("#forthFrameRate");
        }
    }
}

function getMainStreamTypeCount() {
    return $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').length;
}
// 主码流分辨率
function InitMainStreamResolutionData() {
    $("#mResolutionWH").empty();
    var ResolutionH = "";
    var ResolutionW = "";
    var MainStreamTypeCount = getMainStreamTypeCount();
    for (var i = 0; i < MainStreamTypeCount; i++) {
        ResolutionH = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(i).find('ResolutionHeigth').eq(0).text();
        ResolutionW = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(i).find('ResolutionWidth').eq(0).text();
        for (var j = 0; j < gs_arrResolution.length; j++) {
            if (gs_arrResolution[j].Resolution == ResolutionW + "*" + ResolutionH) {
                $("<option value='" + gs_arrResolution[j].Resolution + "'>" + gs_arrResolution[j].ResolutionVal + "</option>").appendTo("#mResolutionWH");
                break;
            }
        }
    }
}

function RefreshALLStreamCodec() {
    $("#mCodec").empty();
    $("#sCodec").empty();
    var codecSize = $(gs_stream_cap_xml).find('StreamCapability').find('CodecCapabilityList').find('streamCodecCapability').length;
    for (var i = 0; i < codecSize; i++) {
        var codecVal = $(gs_stream_cap_xml).find('StreamCapability').find('CodecCapabilityList').find('streamCodecCapability').eq(i).find('Codec').text();
        $("<option value='" + codecVal + "'>" + codecVal + "</option>").appendTo("#mCodec");
        $("<option value='" + codecVal + "'>" + codecVal + "</option>").appendTo("#sCodec");
    }
}
/** 
 *@function 获取主码流配置信息
 */
function getMainstream(ch) {
    $.ajax({
        url: config.baseUrl + "/Streams/" + ch + "/1",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            gs_main_stream_xml = xmlDoc;
            var codecVal = $(gs_main_stream_xml).find('StreamConfig').find('VideoCodecType').text();
            $("#mCodec").select2("val", codecVal); //视频编码
            var rlWidth = $(gs_main_stream_xml).find('StreamConfig').eq(0).find('ResolutionWidth').eq(0).text();
            var rlHeight = $(gs_main_stream_xml).find('StreamConfig').eq(0).find('ResolutionHeigth').eq(0).text();
            $("#mResolutionWH").select2("val", rlWidth + '*' + rlHeight); //主码流分辨率
            $("#mIFrameInterval").val($(gs_main_stream_xml).find('StreamConfig').eq(0).find('keyFrameInterval').eq(0).text());
            $("#streamType").select2("val", $(gs_main_stream_xml).find('StreamConfig').eq(0).find('StreamType').eq(0).text()); //主码流码流类型
            $("#bitrateType").select2("val", $(gs_main_stream_xml).find('StreamConfig').eq(0).find('BitrateType').eq(0).text()); //码率控制
            $("#fixedQuality").select2("val", $(gs_main_stream_xml).find('StreamConfig').eq(0).find('FixedQuality').eq(0).text()); //图像质量
            var maxFrameRate = getMainStreamMaxFrameDataByMainStreamResolution();
            $("#mFrameRate").empty();
            for (var i = 1; i <= maxFrameRate; i++) {
                $("<option value='" + i + "'>" + i + "</option>").appendTo("#mFrameRate"); //帧率选项
            };
            changeVCbr()
                //是预设值还是自定义
            var flg = '0';
            for (var i = 0; i < bit.length; i++) {
                if ($(gs_main_stream_xml).find('StreamConfig').eq(0).find('Bitrate').eq(0).text() == bit[i]) {
                    flg = '1';
                    break;
                }
            }
            $("#mBitPU").select2("val", flg);
            $("select#mBitrate").select2("val", $(gs_main_stream_xml).find('StreamConfig').eq(0).find('Bitrate').eq(0).text());
            $("input.mBitrate").val($(gs_main_stream_xml).find('StreamConfig').eq(0).find('Bitrate').eq(0).text());
            RefreshMainStreamDataFromNetData(1);
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

function changeVCbr() {
    if ("CBR" == $("#bitrateType").val()) {
        $(".isShowQualit").hide();
        $(".isShowRate").show();
    } else {
        $(".isShowQualit").show();
        $(".isShowRate").hide();
    }

}

function getSubstream(ch) {
    $.ajax({
        url: config.baseUrl + "/Streams/" + ch + "/2",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            gs_first_stream_xml = xmlDoc;
            var rlWidth = $(gs_first_stream_xml).find('StreamConfig').eq(0).find('ResolutionWidth').eq(0).text();
            var rlHeight = $(gs_first_stream_xml).find('StreamConfig').eq(0).find('ResolutionHeigth').eq(0).text();
            subResolutions = rlWidth + '*' + rlHeight
            RefreshSubStreamMaxFrameByMainAndSubStreamResulution();
            $("#sResolutionWH").select2("val", rlWidth + '*' + rlHeight);
            $("#sIFrameInterval").val($(gs_first_stream_xml).find('StreamConfig').eq(0).find('keyFrameInterval').eq(0).text());
            $("#sCodec").select2("val", $(gs_first_stream_xml).find('StreamConfig').eq(0).find('VideoCodecType').eq(0).text());
            $("#sstreamType").select2("val", $(gs_first_stream_xml).find('StreamConfig').eq(0).find('StreamType').eq(0).text());
            $("#sbitrateType").select2("val", $(gs_first_stream_xml).find('StreamConfig').eq(0).find('BitrateType').eq(0).text());
            $("#sfixedQuality").select2("val", $(gs_first_stream_xml).find('StreamConfig').eq(0).find('FixedQuality').eq(0).text()); //改动
            var flg = '0';
            for (var i = 0; i < bit.length; i++) {
                if ($(gs_first_stream_xml).find('StreamConfig').eq(0).find('Bitrate').eq(0).text() == bit[i]) {
                    flg = '1';
                    break;
                }
            }
            $("#sBitPU").select2("val", flg);
            // changesBitPU();
            $("select.sBitrate").select2("val", $(gs_first_stream_xml).find('StreamConfig').eq(0).find('Bitrate').eq(0).text());
            $("input.sBitrate").val($(gs_first_stream_xml).find('StreamConfig').eq(0).find('Bitrate').eq(0).text());
            RefreshSubStreamDataFromNetData(1);
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

function setMainstream(num) {
    if ((!(/(^[1-9]\d*$)/.test($("#mIFrameInterval").val()))) || ($("#mIFrameInterval").val() < 1) || ($("#mIFrameInterval").val() > 200)) {
        $.optionBootoast($.i18n.prop('i18n-code-gop-tip'), "danger");
        return;
    }
    var rlWidth = $("#mResolutionWH").select2("val").split('*')[0];
    var rlHeight = $("#mResolutionWH").select2("val").split('*')[1];
    var MainFrame = $("#mFrameRate").select2("val") * 100;
    if ($("#mBitPU").select2("val") == "1") { //预设值
        var mbitrate = $("select.mBitrate").select2("val");
    } else {
        var mbitrate = $("input.mBitrate").val();
    }
    if ((!(/(^[1-9]\d*$)/.test(mbitrate))) || (mbitrate < 256) || (mbitrate > 12288)) {
        mbitrate = '256';
    }
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><StreamConfig Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><ID>1</ID>" +
        "<StreamType>" + $("#streamType").select2("val") + "</StreamType>" +
        "<VideoCodecType>" + $("#mCodec").select2("val") + "</VideoCodecType>" +
        "<ResolutionWidth>" + rlWidth + "</ResolutionWidth>" +
        "<ResolutionHeigth>" + rlHeight + "</ResolutionHeigth>" +
        "<FrameRate>" + MainFrame + "</FrameRate>" +
        "<BitrateType>" + $("#bitrateType").select2("val") + "</BitrateType>" +
        "<Bitrate>" + mbitrate + "</Bitrate>" +
        "<FixedQuality>" + $("#fixedQuality").select2("val") + "</FixedQuality>" +
        "<keyFrameInterval>" + $("#mIFrameInterval").val() + "</keyFrameInterval>" +
        "<Transport Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><RTSPURI></RTSPURI></Transport></StreamConfig>";
    if (num) {
        var cyURL = config.baseUrl + "/Streams/" + num + "/1";
    } else {
        var cyURL = config.baseUrl + "/Streams/" + $("#NVR_CHNBMchn").val() + "/1";
    }
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            if (typeSet == true && num == config.deviceInfo.VideoInputChannels) {
                SaveState(xhr);
                getMainstream($("#NVR_CHNBMchn").val());
            } else if (typeSet == false) {
                SaveState(xhr);
                getMainstream($("#NVR_CHNBMchn").val());

            }

        }
    });
}

function setSubstream(num) {
    var rlWidth = subResolutions.split('*')[0];
    var rlHeight = subResolutions.split('*')[1];
    var NetSubFrame = $("#sFrameRate").select2("val") * 100
        //限制码流的大小
    if ($("#sBitPU").select2("val") == "1") { //预设值
        var sbitrate = $("select.sBitrate").select2("val");
    } else {
        var sbitrate = $("input.sBitrate").val();
    }
    if ((!(/(^[1-9]\d*$)/.test(sbitrate))) || (sbitrate < 64) || (sbitrate > 2048)) {
        sbitrate = '64';
    }
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><StreamConfig Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><ID>2</ID>" +
        "<StreamType>" + $("#sstreamType").select2("val") + "</StreamType>" +
        "<VideoCodecType>" + $("#sCodec").select2("val") + "</VideoCodecType>" +
        "<ResolutionWidth>" + rlWidth + "</ResolutionWidth>" +
        "<ResolutionHeigth>" + rlHeight + "</ResolutionHeigth>" +
        "<FrameRate>" + NetSubFrame + "</FrameRate>" +
        "<BitrateType>" + $("#sbitrateType").select2("val") + "</BitrateType>" +
        "<Bitrate>" + sbitrate + "</Bitrate>" +
        "<FixedQuality>" + $("#sfixedQuality").select2("val") + "</FixedQuality>" +
        "<keyFrameInterval></keyFrameInterval><Transport Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><RTSPURI></RTSPURI></Transport></StreamConfig>";
    if (num) {
        var cyURL = config.baseUrl + "/Streams/" + num + "/2";
    } else {
        var cyURL = config.baseUrl + "/Streams/" + $("#NVR_CHNBMchn").val() + "/2";
    }
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            if (typeSet == true && num == config.deviceInfo.VideoInputChannels) {
                // SaveState(xhr);
                getSubstream($("#NVR_CHNBMchn").val() - 0);
            } else if (typeSet == false) {
                // SaveState(xhr);
                getSubstream($("#NVR_CHNBMchn").val() - 0);

            }

        }
    });
}

function RefreshSubStreamDataFromNetData(n) {
    var subMaxFrame = getSubStreamMaxFrameDataByMainStreamResolution(gs_stream_type_map_table[1].strStreamTypeID);
    var netFrameRate = $(gs_first_stream_xml).find('StreamConfig').eq(0).find('FrameRate').eq(0).text();
    var UIFrameRate = getUIFrameRateFromNetFrameRate(subMaxFrame, netFrameRate);
    if (n == 1) {
        $("#sFrameRate").select2("val", UIFrameRate);
    }
    changeBitPU("s");
    changesVCbr();
}
/** 
 *@function 根据当前选择的主码流和子码流的分辨率，来确定子码流的最大帧率
 */
function RefreshSubStreamMaxFrameByMainAndSubStreamResulution() {
    $("#sResolutionWH").empty();
    var subStreamTypeCount = parseInt(deviceCap.StreamCount) - 1
        //获取当前界面选择的主码流的分辨率
    var selectedMainResolution = $("#mResolutionWH").select2("val");

    //根据选择的主码流的分辨率获取主码流在xml的位置
    var index = getMainStreamPosByMainStreamResolutin(selectedMainResolution);
    for (var i = 0; i < subStreamTypeCount; i++) {
        //获取当前选择的主码流对应的支持的子码流的个数
        var subCount = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find('SubStreamCapabilityList').find('StreamCapability').length;
        // var maxFrame = 25;
        var maxFrame = 0;
        for (var j = 0; j < subCount; j++) {
            //获取当前界面选择的子码流的分辨率
            var selectedSubResolution = subResolutions;
            var subResolutionW = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find('SubStreamCapabilityList').find('StreamCapability').eq(j).find('ResolutionWidth').eq(0).text();
            var subResolutionH = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find('SubStreamCapabilityList').find('StreamCapability').eq(j).find('ResolutionHeigth').eq(0).text();
            for (var k = 0; k < gs_arrResolution.length; k++) {
                if (gs_arrResolution[k].Resolution == subResolutionW + "*" + subResolutionH) {
                    $("<option value='" + gs_arrResolution[k].Resolution + "'>" + gs_arrResolution[k].ResolutionVal + "</option>").appendTo("#sResolutionWH");
                }
            }

            if (selectedSubResolution == subResolutionW + "*" + subResolutionH) {
                maxFrame = parseInt($(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find('SubStreamCapabilityList').find('StreamCapability').eq(j).find('MaxFrameRate').eq(0).text());
            }
        }
        var cur = parseInt($("#sFrameRate").select2("val"))
            //插入帧率数据
        $("#sFrameRate").empty();
        for (var k = 1; k <= maxFrame; k++) {
            $("<option value='" + k + "'>" + k + "</option>").appendTo("#sFrameRate");
        };
        if (cur > maxFrame) {
            $("#sFrameRate").select2("val", maxFrame);
        } else {
            $("#sFrameRate").select2("val", cur);
        }

    }
}
/** 
 *@function 根据当前选择的主码流的分辨率，来确定1.主码流的最大帧率，2.子码流的分辨率和最大帧率
 */
function RefreshMainStreamMaxFrameDataByMainStreamResolution() {
    //获取当前界面选择的主码流的分辨率
    var selectedMainResolution = $("#mResolutionWH").select2("val");
    //根据选择的主码流的分辨率在xml的位置
    var index = getMainStreamPosByMainStreamResolutin(selectedMainResolution);
    //找到能力对应主码流的最大帧率
    var mainMaxFrame = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find('MaxFrameRate').eq(0).text();
    //比较当前页面的帧率是不是大于当前主码流的最大帧率
    var curfps = $("#mFrameRate").select2("val");
    $("#mFrameRate").empty(); //清空贞
    for (var i = 0; i < mainMaxFrame; i++) {
        var nID = i + 1;
        $("<option value='" + nID + "'>" + frame[i] + "</option>").appendTo("#mFrameRate");
    }
    if ($("#mResolutionWH").select2("val")) {
        var res = $("#mResolutionWH").select2("val").split('*')[0];
        if (curfps > mainMaxFrame) //如果当前的值大于
        {
            $("#mFrameRate").select2("val", mainMaxFrame);
        } else if (curfps == 20 && res != '2304') {
            $("#mFrameRate").select2("val", mainMaxFrame)
        } else {
            $("#mFrameRate").select2("val", curfps)
        }
    }

}

function RefreshMainStreamDataFromNetData(n) {
    var maxFrameRate = getMainStreamMaxFrameDataByMainStreamResolution(); //当前主码流分辨率获得最大帧率
    var getFrameRate = $(gs_main_stream_xml).find('StreamConfig').eq(0).find('FrameRate').eq(0).text(); //get配置获取的帧率
    getFrameRate = getUIFrameRateFromNetFrameRate(maxFrameRate, getFrameRate);
    if (n == 1) {
        $("#mFrameRate").select2("val", getFrameRate);
    }
    changeVCbr();
    changeBitPU("m");
}

function FrameRate(res, arg, p) {
    if (arg >= 25) {
        arg = 25;
    } else {
        arg = arg;
    }
    $(p).text(parseInt(res * arg));
}
/** 
 *@function 根据从网络获取的帧率（存在全帧率的概念）转换为界面上帧率
 *@param {能力获取最大帧率,get获取的当前帧率}
 */
function getUIFrameRateFromNetFrameRate(maxFrameRate, getFrameRate) {
    if (getFrameRate / 100 > maxFrameRate) {
        return maxFrameRate;
    } else {
        return getFrameRate / 100;
    }
}

function changeBitPU(type) {
    //预设值与自定义的显示隐藏
    if ("1" == $("#" + type + "BitPU").val()) {
        $("#" + type + "BitrateShowIn").hide();
        $("#" + type + "BitrateShowSe").show();
    } else {
        $("#" + type + "BitrateShowIn").show();
        $("#" + type + "BitrateShowSe").hide();
    }
}
/** 
 *@function 根据当前选择的主码流的分辨率，返回主码流的最大帧率
 */
function getMainStreamMaxFrameDataByMainStreamResolution() {
    //获取当前界面选择的主码流的分辨率
    var selectedMainResolution = $("#mResolutionWH").select2("val");
    //根据选择的主码流的分辨率获取主码流在xml的位置 从而获取最大帧率
    var index = getMainStreamPosByMainStreamResolutin(selectedMainResolution);
    var mainMaxFrame = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find('MaxFrameRate').eq(0).text();
    return mainMaxFrame;
}
/** 
 *@function 根据选择的主码流的分辨率获取主码流cap能力的位置
 */
function getMainStreamPosByMainStreamResolutin(MainStreamResolution) {
    var index = 0;
    var MainStreamCount = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').length;
    var ResolutionH = "";
    var ResolutionW = "";
    for (var i = 0; i < MainStreamCount; i++) {
        ResolutionH = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(i).find('ResolutionHeigth').eq(0).text();
        ResolutionW = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(i).find('ResolutionWidth').eq(0).text();
        if (MainStreamResolution == ResolutionW + "*" + ResolutionH) {
            index = i;
            break;
        }
    }
    return index;
}

function changesBitPU() {
    if ("1" == $("#sBitPU").select2("val")) {
        $("#isBitrate").hide();
        $("#ssBitrate").show();
    } else {
        $("#isBitrate").show();
        $("#ssBitrate").hide();
    }

}
//根据当前选择的主码流的分辨率和子码流的分辨率（多个子码流），返回子码流的最大帧率
function getSubStreamMaxFrameDataByMainStreamResolution(nSubStreamTypeID) {
    var subStreamTypeCount = deviceCap.StreamCount - 1
        //获取当前界面选择的主码流的分辨率
    var selectedMainResolution = $("#mResolutionWH").select2("val");

    var maxFrame = 25;
    //根据选择的主码流的分辨率获取主码流在xml的位置
    var index = getMainStreamPosByMainStreamResolutin(selectedMainResolution);
    for (var i = 0; i < subStreamTypeCount; i++) {
        if (nSubStreamTypeID == gs_stream_type_map_table[i + 1].strStreamTypeID) {
            //获取当前选择的主码流对应的支持的子码流的个数
            var subCount = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find(gs_stream_type_map_table[i + 1].strXMLStreamName + "List").find('StreamCapability').length;
            for (var j = 0; j < subCount; j++) {
                //获取当前界面选择的子码流的分辨率
                var selectedSubResolution = $("#" + gs_stream_type_map_table[i + 1].strResolutionID).val();
                var subResolutionW = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find(gs_stream_type_map_table[i + 1].strXMLStreamName + "List").find('StreamCapability').eq(j).find('ResolutionWidth').eq(0).text();
                var subResolutionH = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find(gs_stream_type_map_table[i + 1].strXMLStreamName + "List").find('StreamCapability').eq(j).find('ResolutionHeigth').eq(0).text();
                if (selectedSubResolution == subResolutionW + "*" + subResolutionH) {
                    maxFrame = $(gs_stream_cap_xml).find('StreamCapability').find('MainStreamCapabilityList').find('MainStreamCapability').eq(index).find(gs_stream_type_map_table[i + 1].strXMLStreamName + "List").find('StreamCapability').eq(j).find('MaxFrameRate').eq(0).text();
                    break;
                }
            }
            break;
        }
    }

    return maxFrame;
}

function RefreshForthStreamDataFromNetData() {
    var subMaxFrame = getSubStreamMaxFrameDataByMainStreamResolution(gs_stream_type_map_table[2].strStreamTypeID);
    var netFrameRate = $(gs_third_stream_xml).find('StreamConfig').eq(0).find('FrameRate').eq(0).text();
    var UIFrameRate = getUIFrameRateFromNetFrameRate(subMaxFrame, netFrameRate);
    $("#forthFrameRate").val(UIFrameRate);
    $("#forthfixedQuality").val($(gs_third_stream_xml).find('StreamConfig').eq(0).find('FixedQuality').eq(0).text());
}

function changeVCbr() { //码率切换
    if ("CBR" == $("#bitrateType").val()) {
        $(".isShowQualit").hide();
        $(".isShowRate").show();
        changeBitPU("m"); //预设值与自定义的显示隐藏
        var mainResolution = $("#mResolutionWH").select2("val");
        switch (mainResolution) {
            case "1920*1080":
                FrameRate(propMainBitRate1, $("#mFrameRate").val(), "#main_bitrate_tip");
                break;
            case "1280*960":
                FrameRate(propMainBitRate2, $("#mFrameRate").val(), "#main_bitrate_tip");
                break;
            case "1280*720":
                FrameRate(propMainBitRate3, $("#mFrameRate").val(), "#main_bitrate_tip");
                break;
            default:
                FrameRate(propMainBitRate1, $("#mFrameRate").val(), "#main_bitrate_tip");
                break;
        }
    } else { //可变码率 显示画质
        $(".isShowQualit").show();
        $(".isShowRate").hide();
    }

}

function changesVCbr() {
    if ("CBR" == $("#sbitrateType").val()) { //恒定码率
        $(".isShowQualitSub").hide();
        $(".isShowRateSub").show();
        changeBitPU("s"); //预设值与自定义的显示隐藏
        var subResolution = $("#sResolutionWH").val();
        // switch (subResolution) {
        //     case "704*576":
        //         FrameRate(propSubBitRate1, $("#sFrameRate").val(), "#sub_bitrate_tip");
        //         break;
        //     case "640*360":
        //         FrameRate(propSubBitRate2, $("#sFrameRate").val(), "#sub_bitrate_tip");
        //         break;
        //     case "352*288":
        //         FrameRate(propSubBitRate3, $("#sFrameRate").val(), "#sub_bitrate_tip");
        //         break;
        //     case "320*240":
        //         FrameRate(propSubBitRate3, $("#sFrameRate").val(), "#sub_bitrate_tip");
        //         break;
        //     case "800*448":
        //         FrameRate(propSubBitRate1, $("#sFrameRate").val(), "#sub_bitrate_tip");
        //         break;
        //     default:
        //         FrameRate(propSubBitRate1, $("#sFrameRate").val(), "#sub_bitrate_tip");
        //         break;
        // }
    } else {
        $(".isShowQualitSub").show();
        $(".isShowRateSub").hide();
    }

}