stream_broadcast_ome.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <div class="bx-stream-broadcast-wrapper">
  2. <video muted class="bx-stream-broadcast-preview" id="bx-stream-preview-__suffix__"></video>
  3. <div class="bx-stream-broadcast-controls bx-def-margin-sec-top">
  4. <button disabled id="bx-stream-start-__suffix__" class="bx-btn bx-btn-primary bx-btn-disabled"><bx_text:_bx_stream_start_streaming /></button>
  5. <button disabled id="bx-stream-stop-__suffix__" class="bx-btn bx-btn-primary bx-btn-disabled bx-def-margin-sec-left"><bx_text:_bx_stream_stop_streaming /></button>
  6. <button id="bx-stream-settings-__suffix__" class="bx-btn bx-def-margin-sec-left"><i class="sys-icon cog"></i></button>
  7. <button id="bx-stream-share-__suffix__" class="bx-btn bx-def-margin-sec-left"><i class="sys-icon share"></i></button>
  8. </div>
  9. </div>
  10. __popup_share__
  11. __popup_settings__
  12. <script src="https://cdn.jsdelivr.net/npm/ovenlivekit@latest/dist/OvenLiveKit.min.js"></script>
  13. <script>
  14. // setup share btn
  15. $('#bx-stream-share-__suffix__').click(function() {
  16. $('#bx-stream-popup-share').dolPopup();
  17. });
  18. // setup settings btn
  19. $('#bx-stream-settings-__suffix__').click(function() {
  20. $('#bx-stream-popup-settings').dolPopup();
  21. });
  22. // setup stop btn
  23. let fOnStreamingStop = function () {
  24. $('#bx-stream-preview-__suffix__').removeClass('bx-stream-broadcast-preview-live');
  25. $('#bx-stream-start-__suffix__').attr('disabled', false).removeClass('bx-btn-disabled');
  26. $('#bx-stream-stop-__suffix__').attr('disabled', true).addClass('bx-btn-disabled');
  27. $('#bx-stream-settings-__suffix__').attr('disabled', false).removeClass('bx-btn-disabled');
  28. };
  29. $('#bx-stream-stop-__suffix__').click(function() {
  30. if ('undefined' === typeof(oOvenLivekit__suffix__) || !oOvenLivekit__suffix__)
  31. return;
  32. fInit();
  33. });
  34. // setup start btn
  35. let fOnStreamingStart = function () {
  36. $('#bx-stream-preview-__suffix__').addClass('bx-stream-broadcast-preview-live');
  37. $('#bx-stream-start-__suffix__').attr('disabled', true).addClass('bx-btn-disabled');
  38. $('#bx-stream-stop-__suffix__').attr('disabled', false).removeClass('bx-btn-disabled');
  39. $('#bx-stream-settings-__suffix__').attr('disabled', true).addClass('bx-btn-disabled');
  40. };
  41. $('#bx-stream-start-__suffix__').click(function() {
  42. if ('undefined' === typeof(oOvenLivekit__suffix__) || !oOvenLivekit__suffix__)
  43. return;
  44. // Get media stream from user device
  45. oOvenLivekit__suffix__.getUserMedia(fGetUserConstraints()).then(function () {
  46. const oConnectionConfig = {
  47. iceServers : null ,
  48. iceTransportPolicy: null,
  49. maxBitrate: null // is Kbps
  50. }
  51. // Got device stream and start streaming to OvenMediaEngine
  52. oOvenLivekit__suffix__.startStreaming('__broadcast_url__', oConnectionConfig);
  53. fOnStreamingStart();
  54. }).catch(function (error) {
  55. // Failed to get device stream.
  56. console.log(error);
  57. });
  58. });
  59. // setup event for changing source devices
  60. $('select[name=audio],select[name=video]').change(function () {
  61. if ('undefined' === typeof(oOvenLivekit__suffix__) || !oOvenLivekit__suffix__)
  62. return;
  63. fInit();
  64. });
  65. // get currently selected devices
  66. let fGetUserConstraints = function() {
  67. let videoDeviceId = $('select[name=video]').val();
  68. let audioDeviceId = $('select[name=audio]').val();
  69. let newConstraint = {};
  70. if (videoDeviceId) {
  71. newConstraint.video = {
  72. deviceId: {
  73. exact: videoDeviceId
  74. }
  75. };
  76. if (__aspect_ratio__)
  77. newConstraint.video['aspectRatio'] = {ideal: __aspect_ratio__};
  78. }
  79. if (audioDeviceId) {
  80. newConstraint.audio = {
  81. deviceId: {
  82. exact: audioDeviceId
  83. }
  84. };
  85. }
  86. return newConstraint;
  87. }
  88. // fill list of source devices
  89. let fSetDevice = function (sType, eSelect, oDevices) {
  90. eSelect.html('');
  91. if (oDevices.length === 0) {
  92. eSelect.append('<option value="">No Source Available</option>'); // TODO: add to lang
  93. } else {
  94. $.each(oDevices, function () {
  95. let oDevice = this;
  96. let option = $('<option></option>');
  97. option.text(oDevice.label);
  98. option.val(oDevice.deviceId);
  99. eSelect.append(option);
  100. });
  101. }
  102. eSelect.find('option').eq(0).prop('selected', true);
  103. }
  104. // initialize
  105. let fInit = function () {
  106. if ('undefined' !== typeof(oOvenLivekit__suffix__) && oOvenLivekit__suffix__)
  107. oOvenLivekit__suffix__.remove();
  108. // Configuration
  109. var oConfig = {
  110. callbacks: {
  111. error: function (error) {
  112. let errorMessage = '';
  113. if (error.message)
  114. errorMessage = error.message;
  115. else if (error.name)
  116. errorMessage = error.name;
  117. else
  118. errorMessage = error.toString();
  119. if (errorMessage === 'OverconstrainedError')
  120. errorMessage = 'The input device does not support the specified resolution or frame rate.';
  121. console.error(errorMessage);
  122. },
  123. connected: function (event) {
  124. },
  125. connectionClosed: function (type, event) {
  126. if (type === 'websocket') {
  127. let reason;
  128. // See http://tools.ietf.org/html/rfc6455#section-7.4.1
  129. if (event.code === 1000)
  130. reason = "Normal closure, meaning that the purpose for which the connection was established has been fulfilled.";
  131. else if (event.code === 1001)
  132. reason = "An endpoint is \"going away\", such as a server going down or a browser having navigated away from a page.";
  133. else if (event.code === 1002)
  134. reason = "An endpoint is terminating the connection due to a protocol error";
  135. else if (event.code === 1003)
  136. reason = "An endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message).";
  137. else if (event.code === 1004)
  138. reason = "Reserved. The specific meaning might be defined in the future.";
  139. else if (event.code === 1005)
  140. reason = "No status code was actually present.";
  141. else if (event.code === 1006)
  142. reason = "The connection was closed abnormally, e.g., without sending or receiving a Close control frame";
  143. else if (event.code === 1007)
  144. reason = "An endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [http://tools.ietf.org/html/rfc3629] data within a text message).";
  145. else if (event.code === 1008)
  146. reason = "An endpoint is terminating the connection because it has received a message that \"violates its policy\". This reason is given either if there is no other sutible reason, or if there is a need to hide specific details about the policy.";
  147. else if (event.code === 1009)
  148. reason = "An endpoint is terminating the connection because it has received a message that is too big for it to process.";
  149. else if (event.code === 1010) // Note that this status code is not used by the server, because it can fail the WebSocket handshake instead.
  150. reason = "An endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. <br /> Specifically, the extensions that are needed are: " + event.reason;
  151. else if (event.code === 1011)
  152. reason = "A server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.";
  153. else if (event.code === 1015)
  154. reason = "The connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified).";
  155. else
  156. reason = "Unknown reason";
  157. console.log('Web Socket is closed. ' + reason);
  158. }
  159. if (type === 'ice') {
  160. console.log('Peer Connection is closed. State: ' + input.peerConnection.iceConnectionState);
  161. }
  162. },
  163. iceStateChange: function (state) {
  164. console.log("IceStateChange: " + state);
  165. }
  166. }
  167. }
  168. // Initialize OvenLiveKit
  169. oOvenLivekit__suffix__ = OvenLiveKit.create(oConfig);
  170. // Attaching video element for playing device stream
  171. oOvenLivekit__suffix__.attachMedia(document.getElementById('bx-stream-preview-__suffix__'));
  172. oOvenLivekit__suffix__.getUserMedia(fGetUserConstraints()).then(function () {
  173. fOnStreamingStop(); // instead of init
  174. }).catch(function (error) {
  175. // Failed to get device stream.
  176. console.log(error);
  177. });
  178. };
  179. let fPreInit = function () {
  180. fSetDevice('audio', $('select[name=audio]'), glAllDevices.audioinput);
  181. fSetDevice('video', $('select[name=video]'), glAllDevices.videoinput);
  182. fInit();
  183. };
  184. $(document).ready(function() {
  185. // get all devices at the first time
  186. OvenLiveKit.getDevices().then(function (devices) {
  187. glAllDevices = devices;
  188. fPreInit();
  189. }).catch(function (error) {
  190. let errorMessage = '';
  191. if (error.message) {
  192. errorMessage = error.message;
  193. } else if (error.name) {
  194. errorMessage = error.name;
  195. } else {
  196. errorMessage = error.toString();
  197. }
  198. console.error(errorMessage);
  199. });
  200. });
  201. </script>