‘liusuyi’
2023-11-24 bf75d92d753bcd3cf871d6d1201f8dd359b11bc6
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
<html>
<head>
    <title>WebRTC Streamer Info</title>
    <link rel="icon" type="image/png" href="info.png" />
    <script src="libs/ag-grid.min.js"></script>
    <script src="webrtcconfig.js" ></script>
    <script src="webrtcstreamer.js" ></script>
    <style type="text/css">
    .cell {
    display: flex;
    align-items: center;
    justify-content: center;
    }
    footer {
    text-align: center; 
    }
    </style> 
</head>
<body> 
    <div>
        log <select id="log" onchange="fetch('api/log?level='+this.value)" >
            <option>0</option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
        </select>    
    </div>
    <div id="myGrid" style="height: 80%;" class="ag-fresh"></div>
    <footer id="footer"></footer>
</body>
<script>    
    // columns
    var columnDefs = [
        { headerName: "PeerId" , field: "peerid",  cellClass: "cell", headerClass: "cell" },
        { headerName: "StreamId", field: "streamid",  cellClass: "cell", headerClass: "cell" },
        { headerName: "Size", field: "size",  cellClass: "cell", headerClass: "cell" },
        { headerName: "Ice State", field: "ice_state",  cellClass: "cell", headerClass: "cell" },
        { headerName: "Nb Candidates", field: "nbCandidates",  cellClass: "cell", headerClass: "cell" },        
        { headerName: "PC State", field: "pc_state",  cellClass: "cell", headerClass: "cell" },
        { headerName: "Action" , field: "action", cellClass: "cell", headerClass: "cell", 
          cellRenderer: function(params) { 
                const eDiv = document.createElement('div');
                const eButton = document.createElement('button');
                eButton.innerText = params.value;
                eDiv.appendChild(eButton);
                eButton.addEventListener('click', () => fetch( webrtcConfig.url + "/api/hangup?peerid=" + params.data.peerid ));
 
                return eDiv;
            } 
        }
    ];
 
    // options
    var gridOptions = {
        enableFilter: true,
        enableSorting: true,
        enableColResize: true,
        columnDefs: columnDefs,
        onGridReady: function(event) { event.api.sizeColumnsToFit(); }
    };
    
    // fill grid
    new agGrid.Grid(document.querySelector('#myGrid'), gridOptions);    
    
    // ------------------------------------------
    // init device list 
    // ------------------------------------------    
    function onGetPeerConnectionList(peerConnectionList) {
    
        var rowData = [];
        peerConnectionList.forEach(function(item) {
            const peerConnection = Object.values(item)[0];
            
            let streamid = "";
            let frameHeight = 0;
            let frameWidth = 0;
            if (peerConnection && peerConnection.hasOwnProperty("streams")){
                streamid = Object.keys(peerConnection.streams)[0];
 
                var stream = Object.values(peerConnection.streams)[0];
                Object.entries(stream).forEach( ([key, value]) => {
                    if (value.hasOwnProperty("width")) {
                        frameHeight = value.width;
                    }
                    if (value.hasOwnProperty("height")) {
                        frameWidth = value.height;
                    }
                });
            }
            
            rowData.push({peerid: Object.keys(item)[0] , streamid, size:frameHeight+"x"+frameWidth, action:"hangup", nbCandidates: Object.values(item)[0].candidateList.length, ... Object.values(item)[0] })
        });
        gridOptions.api.setRowData(rowData);        
    }
    
    // ------------------------------------------
    // Fill version
    // ------------------------------------------    
    function onVersion(version) {
        document.getElementById("footer").innerHTML = "<p><a href='https://github.com/mpromonet/webrtc-streamer'>WebRTC-Streamer</a>"
                            + " " + version.split(" ")[0] + "</p>";
    }
    
    function getPeerConnectionList() {
        fetch(webrtcConfig.url + "/api/getPeerConnectionList")
            .then( (response) => response.json() ) 
            .then( (response) => onGetPeerConnectionList( response ))
    }
 
    // load
    window.onload         = function() { 
        getPeerConnectionList();
        window.setInterval(()=> { getPeerConnectionList(); }, 2000);
        fetch(webrtcConfig.url + "/api/version")
            .then( (response) => response.text() ) 
            .then( (response) => onVersion( response ))
        fetch('api/log')
            .then( (response) => response.text() ) 
            .then( (response)=> document.getElementById("log").value = response )
    } 
    
    // unload
    window.onbeforeunload = function() { 
    }
</script>
</html>