| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <!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>{{title}}</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" preload autoplay onclick="Bvideoselc()"></video> -->
- <!-- <video class="Svideo" id="sm_video" preload muted autoplay onclick="Svideoselc()"></video> -->
- <audio id="my_audio" autoplay></audio>
- <div class="contentColumnC" style="width: 100%;margin-top: 60px;"">
- <div>{{ICnote}}</div>
- <img class="iconImg" style="margin-top: 5px;" src="./img/logo.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 style="width: 100%;margin-top: 100px;">
- <div class="contentColumnC">
- <img id='dianhua_Img' class="guadanImg" style="margin-top: 5px;" src="./img/jietong.png" alt="" onclick="guaduan()"/>
- </div>
- </div>
- </body>
- <script type="text/javascript" src="./js/lib/uniwebviewsdk.js"></script>
- <script>
- let language = getQueryVariable('language');
- let title,state1,state2,state3,state4,state5,ICnote;
- let localStream = null;
- let localCall = 0;
- let localCandidata=null;
- let peer = null;
- let video=null;
- let jietong=false;
- let payload=null;
- // const Big_video = document.getElementById('Big_video');
- // const sm_video = document.getElementById('sm_video');
- // const my_audio = document.getElementById('my_audio');
- const dianhua_Img = document.getElementById('dianhua_Img');
-
- 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(){
- if(jietong){
- if(peer){
- peer.close();
- }
- uni.postMessage({
- data: {
- data:'', // 回传
- type:88
- },
- });
- }
- else{
- jietong=true;
- markNote.innerHTML=state2;
- RCstartWebRct(payload);
- dianhua_Img.src = "./img/guaduandd.png"
- }
-
- }
- //====================================================
- function appAct (obj) {
- if(obj.type==1){//主动发起im通话
- localCall=1;
- //startWebRct(obj.type);
- markNote.innerHTML=state1;
-
- }
- else if(obj.type==2){//收到推送消息
- if(localCall==1){
- markNote.innerHTML=state2;
- RCremoteData(obj.payload);
- }
- else{
- markNote.innerHTML=state1;
- payload=obj.payload;
- //RCstartWebRct(obj.payload);
- }
- }
- else if(obj.type==3){//收到candidate
- markNote.innerHTML=state2;
- 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);
- // Big_video.srcObject =localStream;
- // Big_video.setAttribute('autoplay', true); /* THIS */
-
- 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:0
- });
- //记录本地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=state5;
- }
- if(peer.connectionState=="connected"){
- markNote.innerHTML=state3;
- }
- if(peer.connectionState=="connecting"){
- markNote.innerHTML=state2;
- }
- if(peer.connectionState=="disconnected"){
- markNote.innerHTML=state4;
- }
- };
- 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];
- }
- // sm_video.srcObject =localStream;
- // sm_video.setAttribute('autoplay', true); /* THIS */
- }
- }
- 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() {
- if(language=='yuenan'){//越南语
- ICnote='美食達 Rider yêu cầu IM Voice Call';
- title='IM Cuộc gọi thoại';
- state1='Đang gọi';
- state2='Đang kết nối';
- state3='Đã kết nối';
- state4='Bị treo';
- state5='Kết thúc cuộc gọi';
- }
- if(language=='zh-Hans'){//简体中文
- ICnote='美食達骑手请求IM语音通话'
- title='IM 语音通话';
- state1='正在呼叫';
- state2='正在接通';
- state3='已接通';
- state4='已挂断';
- state5='结束通话';
- }
- if(language=='zh-Hant'){//繁体中文
- ICnote='美食達騎手請求IM語音通話'
- title='IM 語音通話';
- state1='正在呼叫';
- state2='正在接通';
- state3='已接通';
- state4='已掛斷';
- state5='結束通話';
- }
- markNote.innerHTML=title;
- 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>
|