connection.js 7.2 KB

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