You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.4 KiB

  1. var DtlsSrtpKeyAgreement = {
  2. DtlsSrtpKeyAgreement: true
  3. };
  4. var optional = {
  5. optional: [DtlsSrtpKeyAgreement]
  6. };
  7. // Set up audio and video regardless of what devices are present.
  8. var sdpConstraints = {'mandatory': {
  9. 'OfferToReceiveAudio': true,
  10. 'OfferToReceiveVideo': true }};
  11. function onIceCandidate(event) {
  12. /*if (event.candidate) {
  13. sendMessage({type: 'candidate',
  14. label: event.candidate.sdpMLineIndex,
  15. id: event.candidate.sdpMid,
  16. candidate: event.candidate.candidate});
  17. noteIceCandidate("Local", iceCandidateType(event.candidate.candidate));
  18. } else {
  19. console.log('End of candidates.');
  20. }*/
  21. console.log('onIceCandidate');
  22. console.log(event);
  23. }
  24. function onIceConnectionStateChanged(event) {
  25. console.log('onIceConnectionStateChanged');
  26. console.log(event);
  27. }
  28. function onSignalingStateChanged(event) {
  29. console.log('onSignalingStateChanged');
  30. console.log(event);
  31. }
  32. function onError(err) {
  33. window.alert(err.message);
  34. }
  35. function onOfferCreated(description) {
  36. console.log(description);
  37. offer = description;
  38. pc.setLocalDescription(offer,onSetSessionDescriptionSuccess, onSetSessionDescriptionError);
  39. sendMessage(offer);
  40. }
  41. function sendMessage(offer) {
  42. var msgString = JSON.stringify(offer);
  43. console.log('C->S: ' + msgString);
  44. }
  45. function onSetSessionDescriptionSuccess() {
  46. console.log('Set session description success.');
  47. }
  48. function onSetSessionDescriptionError(error) {
  49. console.log('Failed to set session description: ' + error.toString());
  50. }
  51. function init() {
  52. var configuration = {"iceServers":[{"url": "stun:23.21.150.121:3478"}]};
  53. try {
  54. pc = new RTCPeerConnection(configuration, optional);
  55. pc.onicecandidate = onIceCandidate;
  56. pc.onsignalingstatechange = onSignalingStateChanged;
  57. pc.oniceconnectionstatechange = onIceConnectionStateChanged;
  58. } catch (e) {
  59. console.log('Failed to create PeerConnection, exception: ' + e.message);
  60. alert('Cannot create RTCPeerConnection object; \
  61. WebRTC is not supported by this browser.');
  62. return;
  63. }
  64. if(getUserMedia) {
  65. if (getUserMedia) {
  66. getUserMedia = getUserMedia.bind(navigator);
  67. }
  68. // Request the camera.
  69. getUserMedia(
  70. // Constraints
  71. {
  72. video: true, audio: true
  73. },
  74. // Success Callback
  75. function(localMediaStream) {
  76. // Get a reference to the video element on the page.
  77. var vid = document.getElementById('camera-stream');
  78. // Create an object URL for the video stream and use this
  79. // to set the video source.
  80. vid.src = window.URL.createObjectURL(localMediaStream);
  81. console.log(localMediaStream);
  82. pc.addStream(localMediaStream);
  83. channel = pc.createDataChannel("visio");
  84. pc.createOffer(onOfferCreated, onError);
  85. },
  86. // Error Callback
  87. function(err) {
  88. // Log the error to the console.
  89. console.log('The following error occurred when trying to use getUserMedia: ' + err);
  90. }
  91. );
  92. } else {
  93. alert('Sorry, your browser does not support getUserMedia');
  94. }
  95. //channel = pc.createDataChannel("visio");
  96. console.log(pc);
  97. //console.log(channel);
  98. }