srsRtc.js 5.2 KB

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