jswebrtc.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*! jswebrtc v1.0 | (c) Derek Chan | MIT license */
  2. // This sets up the JSWebrtc "Namespace". The object is empty apart from the Now()
  3. // utility function and the automatic CreateVideoElements() after DOMReady.
  4. var JSWebrtc = {
  5. // The Player sets up the connections between source, demuxer, decoders,
  6. // renderer and audio output. It ties everything together, is responsible
  7. // of scheduling decoding and provides some convenience methods for
  8. // external users.
  9. Player: null,
  10. // A Video Element wraps the Player, shows HTML controls to start/pause
  11. // the video and handles Audio unlocking on iOS. VideoElements can be
  12. // created directly in HTML using the <div class="jswebrtc"/> tag.
  13. VideoElement: null,
  14. CreateVideoElements: function () {
  15. var elements = document.querySelectorAll('.jswebrtc');
  16. for (var i = 0; i < elements.length; i++) {
  17. new JSWebrtc.VideoElement(elements[i]);
  18. }
  19. },
  20. FillQuery: function (query_string, obj) {
  21. // pure user query object.
  22. obj.user_query = {};
  23. if (query_string.length == 0)
  24. return;
  25. // split again for angularjs.
  26. if (query_string.indexOf("?") >= 0)
  27. query_string = query_string.split("?")[1];
  28. var queries = query_string.split("&");
  29. for (var i = 0; i < queries.length; i++) {
  30. var query = queries[i].split("=");
  31. obj[query[0]] = query[1];
  32. obj.user_query[query[0]] = query[1];
  33. }
  34. // alias domain for vhost.
  35. if (obj.domain)
  36. obj.vhost = obj.domain;
  37. },
  38. ParseUrl: function (rtmp_url) {
  39. // @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
  40. var a = document.createElement("a");
  41. a.href = rtmp_url.replace("rtmp://", "http://")
  42. .replace("webrtc://", "http://")
  43. .replace("rtc://", "http://");
  44. var vhost = a.hostname;
  45. var app = a.pathname.substr(1, a.pathname.lastIndexOf("/") - 1);
  46. var stream = a.pathname.substr(a.pathname.lastIndexOf("/") + 1);
  47. // parse the vhost in the params of app, that srs supports.
  48. app = app.replace("...vhost...", "?vhost=");
  49. if (app.indexOf("?") >= 0) {
  50. var params = app.substr(app.indexOf("?"));
  51. app = app.substr(0, app.indexOf("?"));
  52. if (params.indexOf("vhost=") > 0) {
  53. vhost = params.substr(params.indexOf("vhost=") + "vhost=".length);
  54. if (vhost.indexOf("&") > 0) {
  55. vhost = vhost.substr(0, vhost.indexOf("&"));
  56. }
  57. }
  58. }
  59. // when vhost equals to server, and server is ip,
  60. // the vhost is __defaultVhost__
  61. if (a.hostname == vhost) {
  62. var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
  63. if (re.test(a.hostname))
  64. vhost = "__defaultVhost__";
  65. }
  66. // parse the schema
  67. var schema = "rtmp";
  68. if (rtmp_url.indexOf("://") > 0)
  69. schema = rtmp_url.substr(0, rtmp_url.indexOf("://"));
  70. var port = a.port;
  71. if (!port) {
  72. if (schema === 'http') {
  73. port = 80;
  74. } else if (schema === 'https') {
  75. port = 443;
  76. } else if (schema === 'rtmp') {
  77. port = 1935;
  78. } else if (schema === 'webrtc' || schema === 'rtc') {
  79. port = 1985;
  80. }
  81. }
  82. console.log("port--------->"+port)
  83. var ret = {
  84. url: rtmp_url,
  85. schema: schema,
  86. server: a.hostname, port: port,
  87. vhost: vhost, app: app, stream: stream
  88. };
  89. JSWebrtc.FillQuery(a.search, ret);
  90. return ret;
  91. },
  92. HttpPost: function (url, data) {
  93. return new Promise(function (resolve, reject) {
  94. var xhr = new XMLHttpRequest();
  95. xhr.onreadystatechange = function () {
  96. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  97. var respone = JSON.parse(xhr.responseText);
  98. xhr.onreadystatechange = new Function;
  99. xhr = null;
  100. resolve(respone);
  101. }
  102. };
  103. xhr.open("POST", url, true);
  104. // note: In Internet Explorer, the timeout property may be set only after calling the open()
  105. // method and before calling the send() method.
  106. xhr.timeout = 5000;// 5 seconds for timeout
  107. xhr.responseType = "text";
  108. xhr.setRequestHeader("Content-Type", "application/json");
  109. xhr.send(data);
  110. })
  111. }
  112. };
  113. // Automatically create players for all found <div class="jswebrtc"/> elements.
  114. if (document.readyState === 'complete') {
  115. JSWebrtc.CreateVideoElements();
  116. }
  117. else {
  118. document.addEventListener('DOMContentLoaded', JSWebrtc.CreateVideoElements);
  119. }