connection.js 7.4 KB

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