mediaconnection.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { util } from "./util";
  2. import logger from "./logger";
  3. import { Negotiator } from "./negotiator";
  4. import { ConnectionType, ServerMessageType } from "./enums";
  5. import { Peer } from "./peer";
  6. import { BaseConnection } from "./baseconnection";
  7. import { ServerMessage } from "./servermessage";
  8. import type { AnswerOption } from "./optionInterfaces";
  9. type MediaConnectionEvents = {
  10. /**
  11. * Emitted when a connection to the PeerServer is established.
  12. */
  13. stream: (stream: MediaStream) => void;
  14. };
  15. /**
  16. * Wraps the streaming interface between two Peers.
  17. */
  18. export class MediaConnection extends BaseConnection<MediaConnectionEvents> {
  19. private static readonly ID_PREFIX = "mc_";
  20. private _negotiator: Negotiator<MediaConnectionEvents, MediaConnection>;
  21. private _localStream: MediaStream;
  22. private _remoteStream: MediaStream;
  23. get type() {
  24. return ConnectionType.Media;
  25. }
  26. get localStream(): MediaStream {
  27. return this._localStream;
  28. }
  29. get remoteStream(): MediaStream {
  30. return this._remoteStream;
  31. }
  32. constructor(peerId: string, provider: Peer, options: any) {
  33. super(peerId, provider, options);
  34. this._localStream = this.options._stream;
  35. this.connectionId =
  36. this.options.connectionId ||
  37. MediaConnection.ID_PREFIX + util.randomToken();
  38. this._negotiator = new Negotiator(this);
  39. if (this._localStream) {
  40. this._negotiator.startConnection({
  41. _stream: this._localStream,
  42. originator: true,
  43. });
  44. }
  45. }
  46. addStream(remoteStream) {
  47. logger.log("Receiving stream", remoteStream);
  48. this._remoteStream = remoteStream;
  49. super.emit("stream", remoteStream); // Should we call this `open`?
  50. }
  51. handleMessage(message: ServerMessage): void {
  52. const type = message.type;
  53. const payload = message.payload;
  54. switch (message.type) {
  55. case ServerMessageType.Answer:
  56. // Forward to negotiator
  57. this._negotiator.handleSDP(type, payload.sdp);
  58. this._open = true;
  59. break;
  60. case ServerMessageType.Candidate:
  61. this._negotiator.handleCandidate(payload.candidate);
  62. break;
  63. default:
  64. logger.warn(`Unrecognized message type:${type} from peer:${this.peer}`);
  65. break;
  66. }
  67. }
  68. answer(stream?: MediaStream, options: AnswerOption = {}): void {
  69. if (this._localStream) {
  70. logger.warn(
  71. "Local stream already exists on this MediaConnection. Are you answering a call twice?",
  72. );
  73. return;
  74. }
  75. this._localStream = stream;
  76. if (options && options.sdpTransform) {
  77. this.options.sdpTransform = options.sdpTransform;
  78. }
  79. this._negotiator.startConnection({
  80. ...this.options._payload,
  81. _stream: stream,
  82. });
  83. // Retrieve lost messages stored because PeerConnection not set up.
  84. const messages = this.provider._getMessages(this.connectionId);
  85. for (let message of messages) {
  86. this.handleMessage(message);
  87. }
  88. this._open = true;
  89. }
  90. /**
  91. * Exposed functionality for users.
  92. */
  93. /** Allows user to close connection. */
  94. close(): void {
  95. if (this._negotiator) {
  96. this._negotiator.cleanup();
  97. this._negotiator = null;
  98. }
  99. this._localStream = null;
  100. this._remoteStream = null;
  101. if (this.provider) {
  102. this.provider._removeConnection(this);
  103. this.provider = null;
  104. }
  105. if (this.options && this.options._stream) {
  106. this.options._stream = null;
  107. }
  108. if (!this.open) {
  109. return;
  110. }
  111. this._open = false;
  112. super.emit("close");
  113. }
  114. }