connectionmanager.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Manages DataConnections between its peer and one other peer.
  3. * Internally, manages PeerConnection.
  4. */
  5. function ConnectionManager(id, peer, socket, options) {
  6. if (!(this instanceof ConnectionManager)) return new ConnectionManager(id, peer, socket, options);
  7. // TODO: need eventemitter?
  8. EventEmitter.call(this);
  9. options = util.extend({
  10. config: { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] },
  11. reliable: false,
  12. serialization: 'binary'
  13. }, options);
  14. this._options = options;
  15. // PeerConnection is not yet open.
  16. this.open = false;
  17. this.id = id;
  18. this.peer = peer;
  19. this.pc = null;
  20. // Mapping labels to metadata and serialization.
  21. this.metadatas = {};
  22. this.serializations = {};
  23. this.reliables = {};
  24. // DataConnections on this PC.
  25. this.connections = {};
  26. this._socket = socket;
  27. if (!!this.id) {
  28. this.initialize();
  29. }
  30. };
  31. util.inherits(ConnectionManager, EventEmitter);
  32. ConnectionManager.prototype.initialize = function(id, socket) {
  33. if (!!id) {
  34. this.id = id;
  35. }
  36. if (!!socket) {
  37. this._socket = socket;
  38. }
  39. // Set up PeerConnection.
  40. this._startPeerConnection();
  41. // Listen for ICE candidates.
  42. this._setupIce();
  43. // Listen for negotiation needed.
  44. // Chrome only **
  45. this._setupNegotiationHandler();
  46. // Listen for data channel.
  47. this._setupDataChannel();
  48. this.initialize = function() { };
  49. };
  50. // Start a PC.
  51. ConnectionManager.prototype._startPeerConnection = function() {
  52. util.log('Creating RTCPeerConnection');
  53. this.pc = new RTCPeerConnection(this._options.config, { optional: [ { rtpDataChannels: true } ]});
  54. };
  55. // Set up ICE candidate handlers.
  56. ConnectionManager.prototype._setupIce = function() {
  57. util.log('Listening for ICE candidates.');
  58. var self = this;
  59. this.pc.onicecandidate = function(evt) {
  60. if (evt.candidate) {
  61. util.log('Received ICE candidates.');
  62. self._socket.send({
  63. type: 'CANDIDATE',
  64. payload: {
  65. candidate: evt.candidate
  66. },
  67. dst: self.peer
  68. });
  69. }
  70. };
  71. };
  72. // Set up onnegotiationneeded.
  73. ConnectionManager.prototype._setupNegotiationHandler = function() {
  74. var self = this;
  75. util.log('Listening for `negotiationneeded`');
  76. this.pc.onnegotiationneeded = function() {
  77. util.log('`negotiationneeded` triggered');
  78. self._makeOffer();
  79. };
  80. };
  81. // Set up Data Channel listener.
  82. ConnectionManager.prototype._setupDataChannel = function() {
  83. var self = this;
  84. this._pc.ondatachannel = function(evt) {
  85. util.log('Received data channel');
  86. // TODO: Create DataConnection object.
  87. };
  88. };
  89. // Send an offer.
  90. ConnectionManager.prototype._makeOffer = function() {
  91. var self = this;
  92. this.pc.createOffer(function(offer) {
  93. util.log('Created offer.');
  94. self.pc.setLocalDescription(offer, function() {
  95. util.log('Set localDescription to offer');
  96. self._socket.send({
  97. type: 'OFFER',
  98. payload: {
  99. sdp: offer,
  100. serialization: self.serializations,
  101. metadata: self.metadatas,
  102. reliable: self.reliables
  103. },
  104. dst: self.peer
  105. });
  106. }, function(err) {
  107. self.emit('error', err);
  108. util.log('Failed to setLocalDescription, ', err);
  109. });
  110. });
  111. };
  112. // Create an answer for PC.
  113. ConnectionManager.prototype._makeAnswer = function() {
  114. var self = this;
  115. this.pc.createAnswer(function(answer) {
  116. util.log('Created answer.');
  117. self.pc.setLocalDescription(answer, function() {
  118. util.log('Set localDescription to answer.');
  119. self._socket.send({
  120. type: 'ANSWER',
  121. payload: {
  122. sdp: answer
  123. },
  124. dst: self.peer
  125. });
  126. }, function(err) {
  127. self.emit('error', err);
  128. util.log('Failed to setLocalDescription, ', err);
  129. });
  130. }, function(err) {
  131. self.emit('error', err);
  132. util.log('Failed to create answer, ', err);
  133. });
  134. };
  135. // Clean up PC, close related DCs.
  136. ConnectionManager.prototype._cleanup = function() {
  137. }
  138. ConnectionManager.prototype.handleSDP = function(sdp) {
  139. };
  140. /** Closes manager and all related connections. */
  141. ConnectionManager.prototype.close = function() {
  142. this._cleanup();
  143. var self = this;
  144. if (this.open) {
  145. this._socket.send({
  146. type: 'LEAVE',
  147. dst: self.peer
  148. });
  149. }
  150. this.open = false;
  151. this.emit('close', this.peer);
  152. };
  153. /** Create and returns a DataConnection with the peer with the given label. */
  154. ConnectionManager.prototype.connect = function(label) {
  155. };