connection.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. function DataConnection(options, socket, cb) {
  2. if (!(this instanceof DataConnection)) return new DataConnection(options);
  3. EventEmitter.call(this);
  4. // Is this the originator?
  5. this._originator = options.originator || false;
  6. this._cb = cb;
  7. this._peer = options.peer;
  8. this._id = options.id;
  9. var sdp = options.sdp;
  10. this.metadata = options.metadata;
  11. // Set up socket handlers.
  12. this._socket = socket;
  13. // Firefoxism: connectDataConnection ports.
  14. if (browserisms == 'Firefox') {
  15. if (!DataConnection.usedPorts) {
  16. DataConnection.usedPorts = [];
  17. }
  18. this.localPort = util.randomPort();
  19. while (DataConnection.usedPorts.indexOf(this.localPort) != -1) {
  20. this.localPort = util.randomPort();
  21. }
  22. this.remotePort = util.randomPort();
  23. while (this.remotePort == this.localPort ||
  24. DataConnection.usedPorts.indexOf(this.localPort) != -1) {
  25. this.remotePort = util.randomPort();
  26. }
  27. DataConnection.usedPorts.push(this.remotePort);
  28. DataConnection.usedPorts.push(this.localPort);
  29. }
  30. // Set up PeerConnection.
  31. this._startPeerConnection();
  32. var self = this;
  33. if (this._originator) {
  34. this._setupDataChannel();
  35. this._maybeBrowserisms();
  36. } else if (sdp) {
  37. try {
  38. sdp = new RTCSessionDescription(message.sdp);
  39. } catch(e) {
  40. util.log('Firefox');
  41. }
  42. this._pc.setRemoteDescription(sdp, function() {
  43. util.log('setRemoteDescription: offer');
  44. self._setupDataChannel();
  45. self._maybeBrowserisms();
  46. }, function(err) {
  47. util.log('failed to setRemoteDescription with offer, ', err);
  48. });
  49. }
  50. };
  51. util.inherits(DataConnection, EventEmitter);
  52. DataConnection.prototype.handleAnswer = function(message) {
  53. var sdp = message.sdp;
  54. try {
  55. sdp = new RTCSessionDescription(message.sdp);
  56. } catch(e) {
  57. util.log('Firefox');
  58. }
  59. var self = this;
  60. this._pc.setRemoteDescription(sdp, function() {
  61. util.log('setRemoteDescription: answer');
  62. // Firefoxism
  63. if (browserisms == 'Firefox') {
  64. self._pc.connectDataConnection(self.localPort, self.remotePort);
  65. self._socket.send(JSON.stringify({
  66. type: 'PORT',
  67. dst: self._peer,
  68. src: self._id,
  69. remote: self.localPort,
  70. local: self.remotePort
  71. }));
  72. }
  73. util.log('ORIGINATOR: PeerConnection success');
  74. }, function(err) {
  75. util.log('failed to setRemoteDescription, ', err);
  76. });
  77. };
  78. DataConnection.prototype.handleCandidate = function(message) {
  79. util.log(message.candidate);
  80. var candidate = new RTCIceCandidate(message.candidate);
  81. this._pc.addIceCandidate(candidate);
  82. };
  83. DataConnection.prototype.handleLeave = function(message) {
  84. util.log('counterpart disconnected');
  85. if (!!this._pc && this._pc.readyState != 'closed') {
  86. this._pc.close();
  87. this._pc = null;
  88. }
  89. if (!!this._dc && this._dc.readyState != 'closed') {
  90. this._dc.close();
  91. this._dc = null;
  92. }
  93. this.emit('close', this._peer);
  94. };
  95. DataConnection.prototype.handlePort = function(message) {
  96. if (!DataConnection.usedPorts) {
  97. DataConnection.usedPorts = [];
  98. }
  99. DataConnection.usedPorts.push(message.local);
  100. DataConnection.usedPorts.push(message.remote);
  101. this._pc.connectDataConnection(message.local, message.remote);
  102. };
  103. /** Starts a PeerConnection and sets up handlers. */
  104. DataConnection.prototype._startPeerConnection = function() {
  105. this._pc = new RTCPeerConnection(this._config, { optional:[ { RtpDataChannels: true } ]});
  106. this._setupIce();
  107. };
  108. /** Takes care of ice handlers. */
  109. DataConnection.prototype._setupIce = function() {
  110. var self = this;
  111. this._pc.onicecandidate = function(event) {
  112. util.log('candidates received');
  113. if (event.candidate) {
  114. self._socket.send(JSON.stringify({
  115. type: 'CANDIDATE',
  116. candidate: event.candidate,
  117. dst: self._peer,
  118. src: self._id
  119. }));
  120. } else {
  121. util.log("End of candidates.");
  122. }
  123. };
  124. };
  125. /** Sets up DataChannel handlers. */
  126. DataConnection.prototype._setupDataChannel = function() {
  127. var self = this;
  128. if (this._originator) {
  129. /** ORIGINATOR SETUP */
  130. if (browserisms == 'Webkit') {
  131. // TODO: figure out the right thing to do with this.
  132. this._pc.onstatechange = function() {
  133. util.log('State Change: ', self._pc.readyState);
  134. }
  135. } else {
  136. this._pc.onconnection = function() {
  137. util.log('ORIGINATOR: onconnection triggered');
  138. self._startDataChannel();
  139. };
  140. }
  141. } else {
  142. /** TARGET SETUP */
  143. this._pc.ondatachannel = function(dc) {
  144. util.log('SINK: ondatachannel triggered');
  145. self._dc = dc;
  146. self._dc.binaryType = 'blob';
  147. self._dc.onmessage = function(e) {
  148. self._handleDataMessage(e);
  149. };
  150. self._cb(null, self);
  151. };
  152. this._pc.onconnection = function() {
  153. util.log('SINK: onconnection triggered');
  154. };
  155. }
  156. this._pc.onclosedconnection = function() {
  157. // Remove socket handlers perhaps.
  158. self.emit('close', self._peer);
  159. };
  160. };
  161. DataConnection.prototype._startDataChannel = function() {
  162. var self = this;
  163. this._dc = this._pc.createDataChannel(this._peer, { reliable: false });
  164. this._dc.binaryType = 'blob';
  165. this._dc.onmessage = function(e) {
  166. self._handleDataMessage(e);
  167. };
  168. this._cb(null, self);
  169. };
  170. /** Decide whether to handle Firefoxisms. */
  171. DataConnection.prototype._maybeBrowserisms = function() {
  172. var self = this;
  173. if (browserisms == 'Firefox') {
  174. getUserMedia({ audio: true, fake: true }, function(s) {
  175. self._pc.addStream(s);
  176. if (self._originator) {
  177. self._makeOffer();
  178. } else {
  179. self._makeAnswer();
  180. }
  181. }, function(err) { util.log('crap'); });
  182. } else {
  183. if (self._originator) {
  184. this._makeOffer();
  185. } else {
  186. this._makeAnswer();
  187. }
  188. }
  189. }
  190. /** Create an answer for PC. */
  191. DataConnection.prototype._makeAnswer = function() {
  192. var self = this;
  193. this._pc.createAnswer(function(answer) {
  194. self._pc.setLocalDescription(answer, function() {
  195. util.log('setLocalDescription: answer');
  196. self._socket.send(JSON.stringify({
  197. type: 'ANSWER',
  198. src: self._id,
  199. sdp: answer,
  200. dst: self._peer
  201. }));
  202. }, function(err) {
  203. util.log('failed to setLocalDescription, ', err)
  204. });
  205. }, function(err) {
  206. util.log('failed to create answer, ', err)
  207. });
  208. };
  209. /** Create an offer for PC. */
  210. DataConnection.prototype._makeOffer = function() {
  211. var self = this;
  212. this._pc.createOffer(function(offer) {
  213. self._pc.setLocalDescription(offer, function() {
  214. util.log('setLocalDescription: offer');
  215. self._socket.send(JSON.stringify({
  216. type: 'OFFER',
  217. sdp: offer,
  218. dst: self._peer,
  219. src: self._id,
  220. metadata: self._metadata
  221. }));
  222. }, function(err) {
  223. util.log('failed to setLocalDescription, ', err);
  224. });
  225. });
  226. };
  227. /** Allows user to send data. */
  228. DataConnection.prototype.send = function(data) {
  229. var ab = BinaryPack.pack(data);
  230. this._dc.send(ab);
  231. };
  232. // Handles a DataChannel message.
  233. DataConnection.prototype._handleDataMessage = function(e) {
  234. var self = this;
  235. var fr = new FileReader();
  236. fr.onload = function(evt) {
  237. var ab = evt.target.result;
  238. var data = BinaryPack.unpack(ab);
  239. self.emit('data', data);
  240. };
  241. fr.readAsArrayBuffer(e.data);
  242. };
  243. exports.DataChannel = DataChannel;