audiobridgetest.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var server = null;
  2. if(window.location.protocol === 'http:')
  3. server = "http://192.168.131.21:8088/janus";
  4. else
  5. server = "https://192.168.131.21:8089/janus";
  6. var janus = null;
  7. var mixertest = null;
  8. var opaqueId = "audiobridgetest-"+Janus.randomString(12);
  9. var spinner = null;
  10. var myroom = 1001; // Demo room
  11. var myid = null;
  12. var webrtcUp = false;
  13. var audioenabled = false;
  14. $(document).ready(function() {
  15. Janus.init({debug: "all", callback: function() {
  16. janus = new Janus(
  17. {
  18. server: server,
  19. success: function() {
  20. janus.attach(
  21. {
  22. plugin: "janus.plugin.audiobridge",
  23. opaqueId: opaqueId,
  24. success: function(pluginHandle) {
  25. mixertest = pluginHandle;
  26. registerUsername();
  27. $('#start').removeAttr('disabled').html("Stop")
  28. .click(function() {
  29. $(this).attr('disabled', true);
  30. janus.destroy();
  31. });
  32. },
  33. error: function(error) {
  34. Janus.error(" -- Error attaching plugin...", error);
  35. },
  36. consentDialog: function(on) {
  37. Janus.debug("Consent dialog should be " + (on ? "on" : "off") + " now");
  38. },
  39. iceState: function(state) {
  40. Janus.log("ICE state changed to " + state);
  41. },
  42. mediaState: function(medium, on) {
  43. Janus.log("Janus " + (on ? "started" : "stopped") + " receiving our " + medium);
  44. },
  45. webrtcState: function(on) {
  46. Janus.log("Janus says our WebRTC PeerConnection is " + (on ? "up" : "down") + " now");
  47. },
  48. onmessage: function(msg, jsep) {
  49. Janus.debug(" ::: Got a message :::", msg);
  50. var event = msg["audiobridge"];
  51. Janus.debug("Event: " + event);
  52. if(event) {
  53. if(event === "joined") {
  54. // Successfully joined, negotiate WebRTC now
  55. if(msg["id"]) {
  56. myid = msg["id"];
  57. Janus.log("Successfully joined room " + msg["room"] + " with ID " + myid);
  58. if(!webrtcUp) {
  59. webrtcUp = true;
  60. // Publish our stream
  61. mixertest.createOffer(
  62. {
  63. media: { video: false}, // This is an audio only room
  64. success: function(jsep) {
  65. Janus.debug("Got SDP!", jsep);
  66. var publish = { request: "configure", muted: false };
  67. mixertest.send({ message: publish, jsep: jsep });
  68. },
  69. error: function(error) {
  70. Janus.error("WebRTC error:", error);
  71. }
  72. });
  73. }
  74. }
  75. }
  76. }
  77. if(jsep) {
  78. Janus.debug("Handling SDP as well...", jsep);
  79. mixertest.handleRemoteJsep({ jsep: jsep });
  80. }
  81. },
  82. onlocalstream: function(stream) {
  83. Janus.debug(" ::: Got a local stream :::", stream);
  84. },
  85. onremotestream: function(stream) {
  86. if($('#roomaudio').length === 0) {
  87. $('#mixedaudio').append('<audio class="rounded centered" id="roomaudio" width="100%" height="100%" autoplay/>');
  88. }
  89. Janus.attachMediaStream($('#roomaudio').get(0), stream);
  90. },
  91. oncleanup: function() {
  92. Janus.log(" ::: Got a cleanup notification :::");
  93. }
  94. });
  95. },
  96. error: function(error) {
  97. Janus.error(error);
  98. },
  99. destroyed: function() {
  100. window.location.reload();
  101. }
  102. });
  103. //});
  104. }});
  105. });
  106. function registerUsername() {
  107. username = "car_1";
  108. var register = { request: "join", room: myroom, display: username };
  109. mixertest.send({ message: register});
  110. }