tts-v4.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // We make use of this 'server' variable to provide the address of the
  2. // REST Janus API. By default, in this example we assume that Janus is
  3. // co-located with the web server hosting the HTML pages but listening
  4. // on a different port (8088, the default for HTTP in Janus), which is
  5. // why we make use of the 'window.location.hostname' base address. Since
  6. // Janus can also do HTTPS, and considering we don't really want to make
  7. // use of HTTP for Janus if your demos are served on HTTPS, we also rely
  8. // on the 'window.location.protocol' prefix to build the variable, in
  9. // particular to also change the port used to contact Janus (8088 for
  10. // HTTP and 8089 for HTTPS, if enabled).
  11. // In case you place Janus behind an Apache frontend (as we did on the
  12. // online demos at http://janus.conf.meetecho.com) you can just use a
  13. // relative path for the variable, e.g.:
  14. //
  15. // var server = "/janus";
  16. //
  17. // which will take care of this on its own.
  18. //
  19. //
  20. // If you want to use the WebSockets frontend to Janus, instead, you'll
  21. // have to pass a different kind of address, e.g.:
  22. //
  23. // var server = "ws://" + window.location.hostname + ":8188";
  24. //
  25. // Of course this assumes that support for WebSockets has been built in
  26. // when compiling the server. WebSockets support has not been tested
  27. // as much as the REST API, so handle with care!
  28. //
  29. //
  30. // If you have multiple options available, and want to let the library
  31. // autodetect the best way to contact your server (or pool of servers),
  32. // you can also pass an array of servers, e.g., to provide alternative
  33. // means of access (e.g., try WebSockets first and, if that fails, fall
  34. // back to plain HTTP) or just have failover servers:
  35. //
  36. // var server = [
  37. // "ws://" + window.location.hostname + ":8188",
  38. // "/janus"
  39. // ];
  40. //
  41. // This will tell the library to try connecting to each of the servers
  42. // in the presented order. The first working server will be used for
  43. // the whole session.
  44. //
  45. var server = null;
  46. if(window.location.protocol === 'http:')
  47. //server = "http://" + window.location.hostname + ":9020/janus";
  48. // yujianbo
  49. server = "https://" + "10.80.21.211" + ":9022/janus";
  50. else
  51. //server = "https://" + window.location.hostname + ":9022/janus";
  52. // -yujianbo
  53. server = "https://" + "10.80.21.211" + ":9022/janus";
  54. var janus = null;
  55. var tts = null;
  56. var opaqueId = "tts-"+Janus.randomString(12);
  57. var spinner = null;
  58. // Initialize the library (all console debuggers enabled)
  59. Janus.init({debug: "all", callback: function() {
  60. window.stopTalk = function (){
  61. janus.destroy();
  62. }
  63. // debugger;
  64. window.startTalk = function() {
  65. // Make sure the browser supports WebRTC
  66. if(!Janus.isWebrtcSupported()) {
  67. bootbox.alert("No WebRTC support... ");
  68. return;
  69. }
  70. // if($('#tts_url').val().length == 0){
  71. // bootbox.alert("Please input tts url... ");
  72. // return;
  73. // }
  74. // $(this).attr('disabled', true).unbind('click');
  75. // Create session
  76. janus = new Janus(
  77. {
  78. server: window.EZUIKit.opt.rtcUrl,
  79. // No "iceServers" is provided, meaning janus.js will use a default STUN server
  80. // Here are some examples of how an iceServers field may look like to support TURN
  81. // iceServers: [{urls: "turn:yourturnserver.com:3478", username: "janususer", credential: "januspwd"}],
  82. // iceServers: [{urls: "turn:yourturnserver.com:443?transport=tcp", username: "janususer", credential: "januspwd"}],
  83. // iceServers: [{urls: "turns:yourturnserver.com:443?transport=tcp", username: "janususer", credential: "januspwd"}],
  84. // Should the Janus API require authentication, you can specify either the API secret or user token here too
  85. // token: "mytoken",
  86. // or
  87. // apisecret: "serversecret",
  88. success: function() {
  89. // Attach to tts plugin
  90. janus.attach(
  91. {
  92. plugin: "rtcgw.plugin.tts",
  93. opaqueId: opaqueId,
  94. success: function(pluginHandle) {
  95. // $('#details').remove();
  96. tts = pluginHandle;
  97. Janus.log("Plugin attached! (" + tts.getPlugin() + ", id=" + tts.getId() + ")");
  98. // Negotiate WebRTC
  99. //var url = "tts://61.130.6.23:8664/talk://D13781761:0:1:cas.ys7.com:6500?97fbd2a75fa94b7682c994d3d1fac8ca:ut.5porslgu79e9r7ca48z32k8abgl3rp58-77bhb6i7xr-1kmumtg-jkhy7pvfr:0:3"
  100. //var url = "tts://10.86.15.209:8664/talk://D13781761:0:1:cas.ys7.com:6500?32db2578ba7c4a84be22ecc0bcd0f8db:ut.5lqpkhim5m7cdk2y5w60g7hm9vd7i3v0-3d2pwhxe2t-11wx2ge-sh4yazbll:0:3"
  101. //var url = "tts://10.86.15.209:8664/talk://D13781761:0:1:cas.ys7.com:6500"
  102. //test12.ys.com
  103. //var url = "tts://10.86.15.209:8664/talk://D08197169:0:1:cas.ys7.com:6500"
  104. //test10.ys.com
  105. //var url = "tts://10.86.29.210:8664/talk://D08197169:0:1:cas.ys7.com:6500"
  106. var url = window.EZUIKit.opt.talkLink;
  107. console.log("ttsUlr",url);
  108. var body = { "request": "start", "url": url, "codec": "opus", "dir": "sendrecv", "audio_debug": 1};
  109. //tts.send({"message": body});
  110. Janus.debug("Trying a createOffer too (audio/video sendrecv)");
  111. tts.createOffer(
  112. {
  113. // No media provided: by default, it's sendrecv for audio and video
  114. media: { audio: true, video: false, data: false }, // Audio only
  115. // If you want to test simulcasting (Chrome and Firefox only), then
  116. // pass a ?simulcast=true when opening this demo page: it will turn
  117. // the following 'simulcast' property to pass to janus.js to true
  118. simulcast: false,
  119. simulcast2: false,
  120. success: function(jsep) {
  121. Janus.debug("Got SDP!");
  122. Janus.debug(jsep);
  123. tts.send({"message": body, "jsep": jsep});
  124. if(typeof window.EZUIKit.handleTalkSuccess !== 'undefined'){
  125. window.EZUIKit.handleTalkSuccess();
  126. }
  127. },
  128. error: function(error) {
  129. Janus.error("WebRTC error:", error);
  130. // bootbox.alert("WebRTC error... " + JSON.stringify(error));
  131. if(typeof window.EZUIKit.handleTalkError !== 'undefined'){
  132. window.EZUIKit.handleTalkError(error);
  133. }
  134. }
  135. });
  136. // $('#start').removeAttr('disabled').html("Stop")
  137. // .click(function() {
  138. // $(this).attr('disabled', true);
  139. // janus.destroy();
  140. // });
  141. },
  142. error: function(error) {
  143. console.error(" -- Error attaching plugin...", error);
  144. bootbox.alert("Error attaching plugin... " + error);
  145. if(window.EZUIKit.handleTalkError !== 'undefined'){
  146. window.EZUIKit.handleTalkError(error);
  147. }
  148. },
  149. consentDialog: function(on) {
  150. Janus.debug("Consent dialog should be " + (on ? "on" : "off") + " now");
  151. if(on) {
  152. // Darken screen and show hint
  153. // $.blockUI({
  154. // message: '<div><img src="up_arrow.png"/></div>',
  155. // css: {
  156. // border: 'none',
  157. // padding: '15px',
  158. // backgroundColor: 'transparent',
  159. // color: '#aaa',
  160. // top: '10px',
  161. // left: (navigator.mozGetUserMedia ? '-100px' : '300px')
  162. // } });
  163. } else {
  164. // Restore screen
  165. // $.unblockUI();
  166. }
  167. },
  168. iceState: function(state) {
  169. Janus.log("ICE state changed to " + state);
  170. },
  171. mediaState: function(medium, on) {
  172. Janus.log("Janus " + (on ? "started" : "stopped") + " receiving our " + medium);
  173. },
  174. webrtcState: function(on) {
  175. Janus.log("Janus says our WebRTC PeerConnection is " + (on ? "up" : "down") + " now");
  176. // $("#audioleft").parent().unblock();
  177. },
  178. slowLink: function(uplink, lost) {
  179. Janus.warn("Janus reports problems " + (uplink ? "sending" : "receiving") +
  180. " packets on this PeerConnection (" + lost + " lost packets)");
  181. },
  182. onmessage: function(msg, jsep) {
  183. Janus.debug(" ::: Got a message :::");
  184. Janus.debug(msg);
  185. if(jsep !== undefined && jsep !== null) {
  186. Janus.debug("Handling SDP as well...");
  187. Janus.debug(jsep);
  188. tts.handleRemoteJsep({jsep: jsep});
  189. }
  190. var result = msg["result"];
  191. if(result !== null && result !== undefined) {
  192. if(result === "done") {
  193. // The plugin closed
  194. bootbox.alert("The TTS Test is over");
  195. if(spinner !== null && spinner !== undefined)
  196. spinner.stop();
  197. spinner = null;
  198. // $('#myaudio').remove();
  199. //$('#waitingvideo').remove();
  200. // $('#peeraudio').remove();
  201. return;
  202. }
  203. if(result === "msg"){
  204. if(typeof window.EZUIKit.handleTalkMessage !== 'undefined'){
  205. window.EZUIKit.handleTalkMessage(msg);
  206. }
  207. }
  208. // Any loss?
  209. var status = result["status"];
  210. if(status === "slow_link") {
  211. //~ var bitrate = result["bitrate"];
  212. //~ toastr.warning("The bitrate has been cut to " + (bitrate/1000) + "kbps", "Packet loss?", {timeOut: 2000});
  213. toastr.warning("Janus apparently missed many packets we sent, maybe we should reduce the bitrate", "Packet loss?", {timeOut: 2000});
  214. }
  215. }
  216. },
  217. onlocalstream: function(stream) {
  218. Janus.debug(" ::: Got a local stream :::");
  219. Janus.debug(stream);
  220. // if($('#myaudio').length === 0) {
  221. // $('#audios').removeClass('hide').show();
  222. // $('#audioleft').append('<audio id="myaudio" autoplay controls muted>Your browser does not support audio tag</audio>');
  223. // }
  224. Janus.attachMediaStream(document.getElementById("myaudio"), stream);
  225. //$("#myaudio").get(0).muted = "muted";
  226. if(tts.webrtcStuff.pc.iceConnectionState !== "completed" &&
  227. tts.webrtcStuff.pc.iceConnectionState !== "connected") {
  228. // $("#audioleft").parent().block({
  229. // message: '<b>Publishing...</b>',
  230. // css: {
  231. // border: 'none',
  232. // backgroundColor: 'transparent',
  233. // color: 'white'
  234. // }
  235. // });
  236. // No remote video yet
  237. //$('#audioright').append('<video class="rounded centered" id="waitingvideo" width=320 height=240 />');
  238. if(spinner == null) {
  239. var target = document.getElementById('audioright');
  240. //spinner = new Spinner({top:100}).spin(target);
  241. } else {
  242. spinner.spin();
  243. }
  244. }
  245. var audioTracks = stream.getAudioTracks();
  246. if(audioTracks === null || audioTracks === undefined || audioTracks.length === 0) {
  247. // $('#myaudio').hide();
  248. } else {
  249. // $('#myaudio').removeClass('hide').show();
  250. // document.getElementById('myaudio').play();
  251. }
  252. },
  253. onremotestream: function(stream) {
  254. Janus.debug(" ::: Got a remote stream :::");
  255. Janus.debug(stream);
  256. // if($('#peeraudio').length === 0) {
  257. // $('#audios').removeClass('hide').show();
  258. // // $('#audioright').append('<audio id="peeraudio" autoplay controls>Your browser does not support audio tag</audio>');
  259. // // Show the video, hide the spinner and show the resolution when we get a playing event
  260. // var audio = $('<audio id="peeraudio" autoplay controls playsinline preload="preload" loop="true"></audio>');
  261. // audio = audio.get(0);
  262. // audio.setAttribute("id", 'peeraudio');
  263. // audio.setAttribute("preload","preload");
  264. // // 自动播放解决苹果不兼容autoplay属性
  265. // audio.setAttribute("loop",true);
  266. // $('#audioright').append(audio);
  267. // $("#peeraudio").bind("playing", function () {
  268. // //$('#waitingvideo').remove();
  269. // $('#peeraudio').removeClass('hide').show();
  270. // if(spinner !== null && spinner !== undefined)
  271. // spinner.stop();
  272. // spinner = null;
  273. // });
  274. // }
  275. Janus.attachMediaStream(document.getElementById("peeraudio"), stream);
  276. var audioTracks = stream.getAudioTracks();
  277. if(audioTracks === null || audioTracks === undefined || audioTracks.length === 0) {
  278. // $('#peeraudio').hide();
  279. } else {
  280. // $('#peeraudio').removeClass('hide').show();
  281. document.getElementById('peeraudio').play();
  282. }
  283. },
  284. ondataopen: function(data) {
  285. Janus.log("The DataChannel is available!");
  286. },
  287. ondata: function(data) {
  288. Janus.debug("We got data from the DataChannel! " + data);
  289. },
  290. oncleanup: function() {
  291. Janus.log(" ::: Got a cleanup notification :::");
  292. if(spinner !== null && spinner !== undefined)
  293. spinner.stop();
  294. spinner = null;
  295. // $('#myaudio').remove();
  296. // //$('#waitingvideo').remove();
  297. // $("#audioleft").parent().unblock();
  298. // $('#peeraudio').remove();
  299. }
  300. });
  301. },
  302. error: function(error) {
  303. Janus.error(error);
  304. if(window.EZUIKit.handleTalkError !== 'undefined'){
  305. window.EZUIKit.handleTalkError(error);
  306. }
  307. },
  308. destroyed: function() {
  309. // window.location.reload();
  310. }
  311. });
  312. }
  313. }});
  314. function checkEnter(event) {
  315. var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  316. if(theCode == 13) {
  317. sendData();
  318. return false;
  319. } else {
  320. return true;
  321. }
  322. }
  323. // Helper to parse query string
  324. function getQueryStringValue(name) {
  325. name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  326. var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
  327. results = regex.exec(location.search);
  328. return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  329. }