index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. document.addEventListener('UniAppJSBridgeReady', function() {
  2. // var lats = getQueryVariable('lat') * 1;
  3. const peer=null;
  4. const localStream=null;
  5. /**
  6. * 初始化
  7. *
  8. */
  9. function initRtc() {
  10. //returngeocode('initRtc',1);
  11. setTimeout(() => {
  12. startCapture({ video:true });
  13. }, 1000)
  14. return;
  15. var peer = new RTCPeerConnection(
  16. {
  17. iceServers:[
  18. {
  19. urls:'stun:stun.l.google.com:19302',
  20. }
  21. ]
  22. }
  23. );
  24. peer.onicecandidate = function (event) {
  25. returngeocode('','onicecandidate');
  26. if (event.candidate){
  27. returngeocode('','candidate');
  28. }
  29. }
  30. returngeocode(JSON.stringify(peer),2);
  31. const constraints = { video: true, audio: true }
  32. var Stream = navigator.mediaDevices.getUserMedia(constraints);
  33. if(Stream){
  34. returngeocode(Stream,3);
  35. }
  36. else{
  37. returngeocode(Stream,333);
  38. }
  39. let video = document.getElementById("my_video");
  40. video.srcObject = Stream;
  41. video.setAttribute('autoplay',true);
  42. returngeocode(video,4);
  43. peer.addStream(Stream)
  44. returngeocode('addStream',5);
  45. const offer = peer.createOffer({
  46. offerToReceiveAudio:1,
  47. offerToReceiveVideo:1
  48. });
  49. returngeocode(offer,6);
  50. //发送offer
  51. }
  52. async function startCapture(displayMediaOptions) {
  53. let stream = null;
  54. try {
  55. stream = await navigator.mediaDevices.getUserMedia(displayMediaOptions);
  56. video = document.createElement("video");
  57. video.srcObject = stream;
  58. video.setAttribute('autoplay', true); /* THIS */
  59. document.body.appendChild(video);
  60. document.getElementById('note').innerHTML('startCapture');
  61. } catch(err) {
  62. console.error(err);
  63. }
  64. }
  65. function returngeocode(data,type){
  66. uni.postMessage({
  67. data: {
  68. data:data,
  69. type:type
  70. }
  71. });
  72. }
  73. window.onload=initRtc;
  74. });