negotiator.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import logger from "./logger";
  2. import type { MediaConnection } from "./mediaconnection";
  3. import type { DataConnection } from "./dataconnection/DataConnection";
  4. import { ConnectionType, PeerErrorType, ServerMessageType } from "./enums";
  5. import type { BaseConnection, BaseConnectionEvents } from "./baseconnection";
  6. import type { ValidEventTypes } from "eventemitter3";
  7. /**
  8. * Manages all negotiations between Peers.
  9. */
  10. export class Negotiator<
  11. A extends ValidEventTypes,
  12. T extends BaseConnection<A | BaseConnectionEvents>,
  13. > {
  14. constructor(readonly connection: T) {}
  15. /** Returns a PeerConnection object set up correctly (for data, media). */
  16. startConnection(options: any) {
  17. const peerConnection = this._startPeerConnection();
  18. // Set the connection's PC.
  19. this.connection.peerConnection = peerConnection;
  20. if (this.connection.type === ConnectionType.Media && options._stream) {
  21. this._addTracksToConnection(options._stream, peerConnection);
  22. }
  23. // What do we need to do now?
  24. if (options.originator) {
  25. const dataConnection = this.connection;
  26. const config: RTCDataChannelInit = { ordered: !!options.reliable };
  27. const dataChannel = peerConnection.createDataChannel(
  28. dataConnection.label,
  29. config,
  30. );
  31. dataConnection._initializeDataChannel(dataChannel);
  32. void this._makeOffer();
  33. } else {
  34. void this.handleSDP("OFFER", options.sdp);
  35. }
  36. }
  37. /** Start a PC. */
  38. private _startPeerConnection(): RTCPeerConnection {
  39. logger.log("Creating RTCPeerConnection.");
  40. const peerConnection = new RTCPeerConnection(
  41. this.connection.provider.options.config,
  42. );
  43. this._setupListeners(peerConnection);
  44. return peerConnection;
  45. }
  46. /** Set up various WebRTC listeners. */
  47. private _setupListeners(peerConnection: RTCPeerConnection) {
  48. const peerId = this.connection.peer;
  49. const connectionId = this.connection.connectionId;
  50. const connectionType = this.connection.type;
  51. const provider = this.connection.provider;
  52. // ICE CANDIDATES.
  53. logger.log("Listening for ICE candidates.");
  54. peerConnection.onicecandidate = (evt) => {
  55. if (!evt.candidate || !evt.candidate.candidate) return;
  56. logger.log(`Received ICE candidates for ${peerId}:`, evt.candidate);
  57. provider.socket.send({
  58. type: ServerMessageType.Candidate,
  59. payload: {
  60. candidate: evt.candidate,
  61. type: connectionType,
  62. connectionId: connectionId,
  63. },
  64. dst: peerId,
  65. });
  66. };
  67. peerConnection.oniceconnectionstatechange = () => {
  68. switch (peerConnection.iceConnectionState) {
  69. case "failed":
  70. logger.log(
  71. "iceConnectionState is failed, closing connections to " + peerId,
  72. );
  73. this.connection.emit(
  74. "error",
  75. new Error("Negotiation of connection to " + peerId + " failed."),
  76. );
  77. this.connection.close();
  78. break;
  79. case "closed":
  80. logger.log(
  81. "iceConnectionState is closed, closing connections to " + peerId,
  82. );
  83. this.connection.emit(
  84. "error",
  85. new Error("Connection to " + peerId + " closed."),
  86. );
  87. this.connection.close();
  88. break;
  89. case "disconnected":
  90. logger.log(
  91. "iceConnectionState changed to disconnected on the connection with " +
  92. peerId,
  93. );
  94. break;
  95. case "completed":
  96. peerConnection.onicecandidate = () => {};
  97. break;
  98. }
  99. this.connection.emit(
  100. "iceStateChanged",
  101. peerConnection.iceConnectionState,
  102. );
  103. };
  104. // DATACONNECTION.
  105. logger.log("Listening for data channel");
  106. // Fired between offer and answer, so options should already be saved
  107. // in the options hash.
  108. peerConnection.ondatachannel = (evt) => {
  109. logger.log("Received data channel");
  110. const dataChannel = evt.channel;
  111. const connection = <DataConnection>(
  112. provider.getConnection(peerId, connectionId)
  113. );
  114. connection._initializeDataChannel(dataChannel);
  115. };
  116. // MEDIACONNECTION.
  117. logger.log("Listening for remote stream");
  118. peerConnection.ontrack = (evt) => {
  119. logger.log("Received remote stream");
  120. const stream = evt.streams[0];
  121. const connection = provider.getConnection(peerId, connectionId);
  122. if (connection.type === ConnectionType.Media) {
  123. const mediaConnection = <MediaConnection>connection;
  124. this._addStreamToMediaConnection(stream, mediaConnection);
  125. }
  126. };
  127. }
  128. cleanup(): void {
  129. logger.log("Cleaning up PeerConnection to " + this.connection.peer);
  130. const peerConnection = this.connection.peerConnection;
  131. if (!peerConnection) {
  132. return;
  133. }
  134. this.connection.peerConnection = null;
  135. //unsubscribe from all PeerConnection's events
  136. peerConnection.onicecandidate =
  137. peerConnection.oniceconnectionstatechange =
  138. peerConnection.ondatachannel =
  139. peerConnection.ontrack =
  140. () => {};
  141. const peerConnectionNotClosed = peerConnection.signalingState !== "closed";
  142. let dataChannelNotClosed = false;
  143. const dataChannel = this.connection.dataChannel;
  144. if (dataChannel) {
  145. dataChannelNotClosed =
  146. !!dataChannel.readyState && dataChannel.readyState !== "closed";
  147. }
  148. if (peerConnectionNotClosed || dataChannelNotClosed) {
  149. peerConnection.close();
  150. }
  151. }
  152. private async _makeOffer(): Promise<void> {
  153. const peerConnection = this.connection.peerConnection;
  154. const provider = this.connection.provider;
  155. try {
  156. const offer = await peerConnection.createOffer(
  157. this.connection.options.constraints,
  158. );
  159. logger.log("Created offer.");
  160. if (
  161. this.connection.options.sdpTransform &&
  162. typeof this.connection.options.sdpTransform === "function"
  163. ) {
  164. offer.sdp =
  165. this.connection.options.sdpTransform(offer.sdp) || offer.sdp;
  166. }
  167. try {
  168. await peerConnection.setLocalDescription(offer);
  169. logger.log(
  170. "Set localDescription:",
  171. offer,
  172. `for:${this.connection.peer}`,
  173. );
  174. let payload: any = {
  175. sdp: offer,
  176. type: this.connection.type,
  177. connectionId: this.connection.connectionId,
  178. metadata: this.connection.metadata,
  179. };
  180. if (this.connection.type === ConnectionType.Data) {
  181. const dataConnection = <DataConnection>(<unknown>this.connection);
  182. payload = {
  183. ...payload,
  184. label: dataConnection.label,
  185. reliable: dataConnection.reliable,
  186. serialization: dataConnection.serialization,
  187. };
  188. }
  189. provider.socket.send({
  190. type: ServerMessageType.Offer,
  191. payload,
  192. dst: this.connection.peer,
  193. });
  194. } catch (err) {
  195. // TODO: investigate why _makeOffer is being called from the answer
  196. if (
  197. err !=
  198. "OperationError: Failed to set local offer sdp: Called in wrong state: kHaveRemoteOffer"
  199. ) {
  200. provider.emitError(PeerErrorType.WebRTC, err);
  201. logger.log("Failed to setLocalDescription, ", err);
  202. }
  203. }
  204. } catch (err_1) {
  205. provider.emitError(PeerErrorType.WebRTC, err_1);
  206. logger.log("Failed to createOffer, ", err_1);
  207. }
  208. }
  209. private async _makeAnswer(): Promise<void> {
  210. const peerConnection = this.connection.peerConnection;
  211. const provider = this.connection.provider;
  212. try {
  213. const answer = await peerConnection.createAnswer();
  214. logger.log("Created answer.");
  215. if (
  216. this.connection.options.sdpTransform &&
  217. typeof this.connection.options.sdpTransform === "function"
  218. ) {
  219. answer.sdp =
  220. this.connection.options.sdpTransform(answer.sdp) || answer.sdp;
  221. }
  222. try {
  223. await peerConnection.setLocalDescription(answer);
  224. logger.log(
  225. `Set localDescription:`,
  226. answer,
  227. `for:${this.connection.peer}`,
  228. );
  229. provider.socket.send({
  230. type: ServerMessageType.Answer,
  231. payload: {
  232. sdp: answer,
  233. type: this.connection.type,
  234. connectionId: this.connection.connectionId,
  235. },
  236. dst: this.connection.peer,
  237. });
  238. } catch (err) {
  239. provider.emitError(PeerErrorType.WebRTC, err);
  240. logger.log("Failed to setLocalDescription, ", err);
  241. }
  242. } catch (err_1) {
  243. provider.emitError(PeerErrorType.WebRTC, err_1);
  244. logger.log("Failed to create answer, ", err_1);
  245. }
  246. }
  247. /** Handle an SDP. */
  248. async handleSDP(type: string, sdp: any): Promise<void> {
  249. sdp = new RTCSessionDescription(sdp);
  250. const peerConnection = this.connection.peerConnection;
  251. const provider = this.connection.provider;
  252. logger.log("Setting remote description", sdp);
  253. const self = this;
  254. try {
  255. await peerConnection.setRemoteDescription(sdp);
  256. logger.log(`Set remoteDescription:${type} for:${this.connection.peer}`);
  257. if (type === "OFFER") {
  258. await self._makeAnswer();
  259. }
  260. } catch (err) {
  261. provider.emitError(PeerErrorType.WebRTC, err);
  262. logger.log("Failed to setRemoteDescription, ", err);
  263. }
  264. }
  265. /** Handle a candidate. */
  266. async handleCandidate(ice: RTCIceCandidate) {
  267. logger.log(`handleCandidate:`, ice);
  268. try {
  269. await this.connection.peerConnection.addIceCandidate(ice);
  270. logger.log(`Added ICE candidate for:${this.connection.peer}`);
  271. } catch (err) {
  272. this.connection.provider.emitError(PeerErrorType.WebRTC, err);
  273. logger.log("Failed to handleCandidate, ", err);
  274. }
  275. }
  276. private _addTracksToConnection(
  277. stream: MediaStream,
  278. peerConnection: RTCPeerConnection,
  279. ): void {
  280. logger.log(`add tracks from stream ${stream.id} to peer connection`);
  281. if (!peerConnection.addTrack) {
  282. return logger.error(
  283. `Your browser does't support RTCPeerConnection#addTrack. Ignored.`,
  284. );
  285. }
  286. stream.getTracks().forEach((track) => {
  287. peerConnection.addTrack(track, stream);
  288. });
  289. }
  290. private _addStreamToMediaConnection(
  291. stream: MediaStream,
  292. mediaConnection: MediaConnection,
  293. ): void {
  294. logger.log(
  295. `add stream ${stream.id} to media connection ${mediaConnection.connectionId}`,
  296. );
  297. mediaConnection.addStream(stream);
  298. }
  299. }