Rtcp.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import Workers from "./Workers.js"
  2. import Heartbeat from "./Heartbeat.js"
  3. import Logger from "./Logger.js"
  4. const logger = new Logger('rtcp')
  5. export default class Rtcp extends EventEmitter {
  6. constructor(e) {
  7. super();
  8. E(this, "connection", null);
  9. E(this, "inputChannel", null);
  10. E(this, "mediaStream");
  11. E(this, "socket");
  12. E(this, "connected", !1);
  13. E(this, "candidates", []);
  14. E(this, "isAnswered", !1);
  15. E(this, "isFlushing", !1);
  16. E(this, "inputReady", !1);
  17. E(this, "workers");
  18. E(this, "actived", !0);
  19. E(this, "heartbeat");
  20. /*
  21. E(this, "onIcecandidate", e=>{
  22. if (e.candidate != null) {
  23. const t = JSON.stringify(e.candidate);
  24. logger.debug(`Got ice candidate: ${t}`),
  25. this.network.socket.send({
  26. id: "ice_candidate",
  27. data: btoa(t)
  28. })
  29. }
  30. }
  31. );
  32. E(this, "onIcecandidateerror", e=>{
  33. logger.error("onicecandidateerror", e.errorCode, e.errorText, e)
  34. }
  35. );
  36. E(this, "onIceStateChange", e=>{
  37. switch (e.target.iceGatheringState) {
  38. case "gathering":
  39. logger.info("ice gathering");
  40. break;
  41. case "complete":
  42. logger.info("Ice gathering completed")
  43. }
  44. }
  45. );
  46. E(this, "onIceConnectionStateChange", ()=>{
  47. if (!!this.connection)
  48. switch (logger.info(`iceConnectionState: ${this.connection.iceConnectionState}`),
  49. this.connection.iceConnectionState) {
  50. case "connected":
  51. {
  52. this.connected = !0;
  53. break
  54. }
  55. case "disconnected":
  56. {
  57. this.connected = !1,
  58. this.emit("rtcDisconnected");
  59. break
  60. }
  61. case "failed":
  62. {
  63. this.emit("rtcDisconnected"),
  64. this.connected = !1;
  65. break
  66. }
  67. }
  68. }
  69. );
  70. E(this, "setRemoteDescription", async(e,t)=>{
  71. var a, s, l;
  72. if (!this.connection)
  73. {
  74. return;
  75. }
  76. const r = JSON.parse(atob(e))
  77. , n = new RTCSessionDescription(r);
  78. await this.connection.setRemoteDescription(n);
  79. const o = await this.connection.createAnswer();
  80. // if (o.sdp = (a = o.sdp) == null ? void 0 : a.replace(/(a=fmtp:111 .*)/g, "$1;stereo=1;sprop-stereo=1"),
  81. // ((l = (s = o.sdp) == null ? void 0 : s.match(/a=mid:1/g)) == null ? void 0 : l.length) == 2) {
  82. // const u = o.sdp.lastIndexOf("a=mid:1");
  83. // o.sdp = o.sdp.slice(0, u) + "a=mid:2" + o.sdp.slice(u + 7)
  84. // }
  85. try {
  86. await this.connection.setLocalDescription(o)
  87. } catch (u) {
  88. logger.error("error", u)
  89. }
  90. this.isAnswered = !0;
  91. this.network.rtcp.flushCandidate();
  92. this.network.socket.send({
  93. id: "answer",
  94. data: btoa(JSON.stringify(o))
  95. });
  96. // t.srcObject = this.mediaStream
  97. }
  98. );
  99. E(this, "flushCandidate", ()=>{
  100. if(!this.isFlushing && this.isAnswered){
  101. this.isFlushing = !0;
  102. this.candidates.forEach(e=>{
  103. const t = atob(e)
  104. , r = JSON.parse(t);
  105. if (/172\./.test(r.candidate))
  106. return;
  107. const n = new RTCIceCandidate(r);
  108. this.connection && this.connection.addIceCandidate(n).then(()=>{}
  109. , o=>{
  110. logger.info("add candidate failed", o)
  111. }
  112. )
  113. });
  114. this.isFlushing = !1;
  115. }
  116. }
  117. );
  118. E(this, "input", e=>{
  119. // var t = this.inputChannel
  120. // if(!this.actived || !this.inputChannel || this.inputChannel.readyState === "open"){
  121. // if(t!=null){
  122. // //发送webrtc数据
  123. // console.log('发送webrtc数据:'+e)
  124. // t.send(e)
  125. // }
  126. // }
  127. //!this.actived || !this.inputChannel || this.inputChannel.readyState === "open" && (t == null || t.send(e))
  128. }
  129. );*/
  130. this.network = e;
  131. this.workers = new Workers(this,logger);
  132. // this.workers.registerLogger(logger),
  133. // this.workers.registerFunction("data", t=>{
  134. // this.emit("data", t)
  135. // });
  136. this.heartbeat = new Heartbeat({
  137. ping: t=>{
  138. e.room.actionsHandler.echo(t)
  139. }
  140. ,
  141. pong(t, r) {
  142. var n;
  143. r && t > 500 && logger.warn(`high hb value ${t}, traceId:` + r),
  144. (n = e.room.stats) == null || n.assign({
  145. hb: t
  146. })
  147. }
  148. })
  149. }
  150. start() {
  151. /*
  152. this.connection = new RTCPeerConnection;
  153. const e = Date.now();
  154. this.connection.ondatachannel = t=>{
  155. logger.info(`ondatachannel: ${t.channel.label}`);
  156. this.inputChannel = t.channel;
  157. //this.inputChannel.onopen = ()=>{
  158. // var r;
  159. // logger.info("The input channel has opened, id:", (r = this.inputChannel) == null ? void 0 : r.id),
  160. // this.inputReady = !0;
  161. // this.emit("rtcConnected");
  162. // this.network.room.currentNetworkOptions.reconnect || (logger.infoAndReportMeasurement({
  163. // metric: "datachannelOpenedAt",
  164. // startTime: this.network.room._startTime,
  165. // group: "joinRoom"
  166. // }),
  167. // logger.infoAndReportMeasurement({
  168. // metric: "datachannelOpenedCost",
  169. // startTime: e,
  170. // group: "joinRoom"
  171. // }))
  172. // console.log('this.inputChannel',this.inputChannel)
  173. //}
  174. //,
  175. //this.inputChannel.onclose = ()=>{
  176. // var r;
  177. // return logger.info("The input channel has closed, id:", (r = this.inputChannel) == null ? void 0 : r.id)
  178. //},
  179. this.inputChannel.onmessage = r=>{
  180. // console.log('this.workers',this.workers)
  181. // console.log('inputChannel',r.data)
  182. // this.workers.dataHandle(r.data)
  183. }
  184. };
  185. this.connection.oniceconnectionstatechange = this.onIceConnectionStateChange,
  186. this.connection.onicegatheringstatechange = this.onIceStateChange,
  187. this.connection.onicecandidate = this.onIcecandidate,
  188. this.connection.onicecandidateerror = this.onIcecandidateerror;
  189. //服务器收到这个请求后,会发送webrtc的相关信息
  190. // this.network.socket.send({
  191. // id: "init_webrtc",
  192. // data: JSON.stringify({
  193. // is_mobile: !0
  194. // })
  195. // })
  196. */
  197. }
  198. // addCandidate(e) {
  199. // e === "" ? this.network.rtcp.flushCandidate() : this.candidates.push(e)
  200. // }
  201. // disconnect() {
  202. // var e, t, r;
  203. // this.heartbeat.stop(),
  204. // logger.info("ready to close datachannel, id", (e = this.inputChannel) == null ? void 0 : e.id),
  205. // (t = this.inputChannel) == null || t.close(),
  206. // (r = this.connection) == null || r.close(),
  207. // this.connection = null,
  208. // this.inputChannel = null
  209. // }
  210. // sendStringData(e) {
  211. // console.log('e',e)
  212. // this.input(e)
  213. // }
  214. // sendData(e) {
  215. // let t = "";
  216. // try {
  217. // t = JSON.stringify(e)
  218. // } catch (r) {
  219. // logger.error(r);
  220. // return
  221. // }
  222. // this.input(t)
  223. // }
  224. }