srsRtc.js 5.3 KB

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