srsRtc.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. export class SrsRTC {
  2. constructor(url) {
  3. this.stream = new MediaStream();
  4. this.url = url;
  5. this.offer = null;
  6. const supportsInsertableStreams =
  7. !!RTCRtpSender.prototype.createEncodedStreams;
  8. console.log("supportsInsertableStreams", supportsInsertableStreams);
  9. // this.pc.ontrack = (event) => {
  10. // // https://webrtc.org/getting-started/remote-streams
  11. // console.warn('event',event.track)
  12. // if(this.stream){
  13. // this.stream.addTrack(event.track);
  14. // }
  15. // };
  16. this.pc = new RTCPeerConnection({
  17. encodedInsertableStreams: true,
  18. });
  19. this.pc.addEventListener("icecandidate", (e) =>
  20. this.onIceCandidate(this.pc, e)
  21. );
  22. this.pc.addEventListener("datachannel", this.ondatachannel);
  23. // var dc = this.pc.createDataChannel("datachannel");
  24. // dc.onmessage = function (event) {
  25. // console.log("received: " + event.data);
  26. // };
  27. // dc.onopen = function () {
  28. // console.log("datachannel open");
  29. // };
  30. // dc.onclose = function () {
  31. // console.log("datachannel close");
  32. // };
  33. // this.pc.ondatachannel = (event) => {
  34. // console.log("ondatachannel", event);
  35. // // this.inputChannel.onopen = () => {
  36. // // console.log("连接");
  37. // // };
  38. // // this.inputChannel.onmessage = (event) => {
  39. // // // mark00 rtcp接收
  40. // // console.log("连接", event);
  41. // // };
  42. // // this.inputChannel.close = (event) => {
  43. // // // mark00 rtcp接收
  44. // // console.log("close", event);
  45. // // };
  46. // };
  47. }
  48. onIceCandidate(event) {
  49. // console.warn("icecandidate", event);
  50. }
  51. ondatachannel(event) {
  52. console.warn("ondatachannel", ondatachannel);
  53. }
  54. close() {
  55. var remoteVideo = document.querySelector("#testVideoFeed");
  56. if (remoteVideo.srcObject) {
  57. remoteVideo.srcObject.getTracks().forEach((track) => track.stop());
  58. }
  59. this.pc && this.pc.close();
  60. this.pc = null;
  61. }
  62. async start(clipUrl) {
  63. this.pc.addTransceiver("video", { direction: "recvonly" });
  64. const serverApi = "https://demo-kms.4dage.com:443/rtc/v1/play/";
  65. const tid = Number(parseInt(new Date().getTime() * Math.random() * 100))
  66. .toString(16)
  67. .slice(0, 7);
  68. var offer = await this.pc.createOffer();
  69. await this.pc.setLocalDescription(offer);
  70. const session = await new Promise(function (resolve, reject) {
  71. // @see https://github.com/rtcdn/rtcdn-draft
  72. var data = {
  73. api: serverApi,
  74. tid: tid,
  75. streamurl: clipUrl,
  76. clientip: null,
  77. sdp: offer.sdp,
  78. };
  79. console.log("Generated offer: ", data);
  80. $.ajax({
  81. type: "POST",
  82. url: serverApi,
  83. data: JSON.stringify(data),
  84. contentType: "application/json",
  85. dataType: "json",
  86. })
  87. .done(function (data) {
  88. console.log("Got answer: ", data);
  89. if (data.code) {
  90. reject(data);
  91. return;
  92. }
  93. resolve(data);
  94. })
  95. .fail(function (reason) {
  96. reject(reason);
  97. });
  98. });
  99. await this.pc.setRemoteDescription(
  100. new RTCSessionDescription({ type: "answer", sdp: session.sdp })
  101. );
  102. }
  103. async getVideo(clipUrl) {
  104. if (!this.pc) {
  105. this.pc = new RTCPeerConnection(null);
  106. this.pc.ontrack = (event) => {
  107. // https://webrtc.org/getting-started/remote-streams
  108. console.log("event", event);
  109. if (this.stream) {
  110. // this.stream.removeTrack(event.track));
  111. const videoTrack = this.stream
  112. .getVideoTracks()
  113. .find((i) => i.kind === "video");
  114. console.log("旧videoTrack", videoTrack);
  115. if (videoTrack) {
  116. this.stream.removeTrack(videoTrack);
  117. }
  118. // if(this.stream.removeTrack())
  119. this.stream.addTrack(event.track);
  120. }
  121. // mediaRecorder.
  122. };
  123. this.pc.ondatachannel = (event) => {
  124. console.log("ondatachannel", ondatachannel);
  125. this.inputChannel.onopen = () => {
  126. console.log("连接");
  127. };
  128. this.inputChannel.onmessage = (event) => {
  129. // mark00 rtcp接收
  130. console.log("连接", event);
  131. };
  132. this.inputChannel.close = (event) => {
  133. // mark00 rtcp接收
  134. console.log("close", event);
  135. };
  136. };
  137. }
  138. this.pc.addTransceiver("video", { direction: "recvonly" });
  139. const serverApi = "https://demo-kms.4dage.com:443/rtc/v1/play/";
  140. const tid = Number(parseInt(new Date().getTime() * Math.random() * 100))
  141. .toString(16)
  142. .slice(0, 7);
  143. var offer = await this.pc.createOffer();
  144. this.offer = offer;
  145. await this.pc.setLocalDescription(offer);
  146. const session = await new Promise(function (resolve, reject) {
  147. // @see https://github.com/rtcdn/rtcdn-draft
  148. var data = {
  149. api: serverApi,
  150. tid: tid,
  151. streamurl: clipUrl,
  152. clientip: null,
  153. sdp: offer.sdp,
  154. };
  155. console.log("Generated offer: ", data);
  156. $.ajax({
  157. type: "POST",
  158. url: serverApi,
  159. data: JSON.stringify(data),
  160. contentType: "application/json",
  161. dataType: "json",
  162. })
  163. .done(function (data) {
  164. console.log("Got answer: ", data);
  165. if (data.code) {
  166. reject(data);
  167. return;
  168. }
  169. resolve(data);
  170. })
  171. .fail(function (reason) {
  172. reject(reason);
  173. });
  174. });
  175. await this.pc.setRemoteDescription(
  176. new RTCSessionDescription({ type: "answer", sdp: session.sdp })
  177. );
  178. return session;
  179. }
  180. }