| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" type="text/css" href="./css/index.css" />
- <title>IM通话</title>
- <style>
- html, body {
- height: 100%;
- }
- body {
- margin: 0;
- }
- video {
- width: 100%;
- height: 100%;
- position: absolute;
- left: 0;
- top: 0;
- z-index: -1;
- }
-
- </style>
- </head>
- <body>
- <video class="Bvideo" id="Big_video" autoplay></video>
- <!-- <video class="Svideo" id="sm_video" preload muted autoplay onclick="Svideoselc()"></video> -->
- <!-- <audio id="my_audio" autoplay></audio> -->
- <div class="bottomView" style="width: 100%;">
- <div class="contentColumnC">
- <img class="guadanImg" style="margin-top: 5px;" src="./img/guaduandd.png" alt="" onclick="guaduan()"/>
- <div class="contentInRowC" style="width: 100%;margin-top: 10px;">
- <div id="markNote"></div>
- <img class="calling" src="./img/loading2.gif" alt="" />
- </div>
- </div>
- </div>
- </body>
- <script type="text/javascript" src="./js/lib/uniwebviewsdk.js"></script>
- <script>
- let localStream = null;
- let localCall = 0;
- let localCandidata=null;
- let peer = null;
- let video=null;
- //const Big_video = document.getElementById('Big_video');
- // const sm_video = document.getElementById('sm_video');
- // const my_audio = document.getElementById('my_audio');
- const markNote = document.getElementById('markNote');
- //====================================================
- // function Bvideoselc(){
- // if(localStream!=null){
- // //Big_video.srcObject =localStream;
- // Big_video.setAttribute('autoplay', true); /* THIS */
- // }
- // }
- function Svideoselc(){
- if(localStream!=null){
- //sm_video.srcObject =localStream;
- sm_video.setAttribute('autoplay', true); /* THIS */
- }
-
- }
- function guaduan(){
- uni.postMessage({
- data: {
- data:'', // 回传
- type:88
- },
- });
- }
- //====================================================
- function appAct (obj) {
- if(obj.type==1){//主动发起im通话
- localCall=1;
- startWebRct(obj.type);
- markNote.innerHTML='拨打中'
- }
- else if(obj.type==2){//收到推送消息
- if(localCall==1){
- markNote.innerHTML='接通中'
- RCremoteData(obj.payload);
- }
- else{
- RCstartWebRct(obj.payload);
- }
- }
- else if(obj.type==3){//收到candidate
- markNote.innerHTML='接通中'
- for(var i=0;i<obj.payload.length;i++){
- peer.addIceCandidate(obj.payload[i]);
- }
- if(localCall!=1){
- uni.postMessage({
- data: {
- data:localCandidata, //应答方回应candidate
- type:3
- },
- });
- }
-
- }
- }
-
- async function startCapture(displayMediaOptions) {
- try {
- localStream = await navigator.mediaDevices.getUserMedia(displayMediaOptions);
- // var otherVideos = document.querySelector('#Big_video');
- // otherVideos.srcObject = localStream;
- uni.postMessage({
- data: {
- data:'', // 回传
- type:99
- },
- });
- } catch(err) {
- console.error(err);
- }
- }
- async function startWebRct(type){
- peer = new RTCPeerConnection(
- {
- iceServers:[
- {
- urls:'stun:stun.l.google.com:19302',
- }
- ]
- }
- );
- //添加本地音视频
- localStream.getTracks().forEach((track) => {
- peer.addTrack(track, localStream)
- });
- //创建本地offer
- const offer = peer.createOffer({
- offerToReceiveAudio:1,
- offerToReceiveVideo:1
- });
- //记录本地offer
- offer.then(value => {
- peer.setLocalDescription(value);
- uni.postMessage({
- data: {
- data:value, // 回传并推送offer
- type:2
- },
- });
- }).catch(error => {
- });
- peer.oniceconnectionstatechange = function (ev) {
- if(peer.connectionState=="closed"){
- markNote.innerHTML='结束通话'
- }
- if(peer.connectionState=="connected"){
- markNote.innerHTML='已接通'
- }
- if(peer.connectionState=="connecting"){
- markNote.innerHTML='连接中'
- }
- if(peer.connectionState=="disconnected"){
- markNote.innerHTML='对方挂断'
- }
- };
- localCandidata=[]
- peer.onicecandidate = function (event) {
- if (event.candidate){
- localCandidata.push(event.candidate);
- }
- }
- startCommunicate();
- }
-
- async function RCremoteData(rcData){//收到remoteAnswer
- //记录远端offer
- await peer.setRemoteDescription(rcData)
-
- uni.postMessage({
- data: {
- data:localCandidata, // 发起方先发candidate
- type:3
- },
- });
- }
- function startCommunicate(){
- peer.ontrack = (e) => {
- if(e.streams[0]){
- //var otherVideos = document.querySelector('#my_audio');
- var otherVideos = document.querySelector('#Big_video');
- otherVideos.srcObject = e.streams[0];
- //markNote.innerHTML=e.streams[0];
- //Big_video.srcObject=e.streams[0];
- //Big_video.setAttribute('autoplay', true); /* THIS */
- }
-
- // sm_video.srcObject =localStream;
- // sm_video.setAttribute('autoplay', true); /* THIS */
-
-
- // if(video!=null){
- // video.srcObject = e.streams[0];
- // video.setAttribute('autoplay', true); /* THIS */
- // }
- // else{
- // video = document.createElement("video");
- // video.srcObject = e.streams[0];
- // video.setAttribute('autoplay', true); /* THIS */
- // document.body.appendChild(video);
- // }
- }
- }
- async function RCstartWebRct(payload){
- peer = new RTCPeerConnection(
- {
- iceServers:[
- {
- urls:'stun:stun.l.google.com:19302',
- }
- ]
- }
- );
- //添加本地音视频
- localStream.getTracks().forEach((track) => {
- peer.addTrack(track, localStream)
- })
- localCandidata=[];
- peer.onicecandidate = function (event) {
- if (event.candidate){
- //let strcandidate=JSON.stringify(event.candidate);
- localCandidata.push(event.candidate);
- }
- }
- startCommunicate();
- //记录远端offer
- await peer.setRemoteDescription(payload)
-
- const remoteAnswer = await peer.createAnswer();
- //记录本地offer
- await peer.setLocalDescription(remoteAnswer);
- //推送remoteAnswer
- uni.postMessage({
- data: {
- data:remoteAnswer, // 回传并推送offer
- type:2
- },
- });
-
- }
- function initRtc() {
- markNote.innerHTML='IM通话'
- setTimeout(() => {
- startCapture({ video: true, audio: true});
- //startCapture({audio: true});
- }, 1000)
- return;
- }
- function releaseTack(){
- if(peer){
- peer.close();
- }
- }
- window.onload=initRtc;
- window.onunload=releaseTack;
- </script>
-
- </html>
|