sink.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. function SinkPeer(options) {
  2. this._config = options.config || {"iceServers": [{"url": "stun:stun.l.google.com:19302"}]};
  3. this._source = options.source || null;
  4. this._video = options.video;
  5. this._data = options.data != undefined ? options.data : true;
  6. this._audio = options.audio;
  7. this._pc = null;
  8. this._id = null;
  9. this._dc = null;
  10. this._socket = io.connect('http://localhost');
  11. this.socketInit();
  12. this._handlers = {};
  13. // Testing firefox.
  14. // MULTICONNECTION doesn't work still.
  15. if (browserisms == 'Firefox' && !options.source) {
  16. if (!SinkPeer.usedPorts) {
  17. SinkPeer.usedPorts = [];
  18. }
  19. this.localPort = randomPort();
  20. while (SinkPeer.usedPorts.indexOf(this.localPort) != -1) {
  21. this.localPort = randomPort();
  22. }
  23. this.remotePort = randomPort();
  24. while (this.remotePort == this.localPort ||
  25. SinkPeer.usedPorts.indexOf(this.localPort) != -1) {
  26. this.remotePort = randomPort();
  27. }
  28. SinkPeer.usedPorts.push(this.remotePort);
  29. SinkPeer.usedPorts.push(this.localPort);
  30. }
  31. };
  32. function randomPort() {
  33. return Math.round(Math.random() * 60535) + 5000;
  34. };
  35. SinkPeer.prototype.socketInit = function() {
  36. var self = this;
  37. // Multiple sinks to one source.
  38. if (!!this._source) {
  39. this._socket.emit('sink', { source: this._source, isms: browserisms },
  40. function(data) {
  41. self._id = data.id;
  42. self._pc = new RTCPeerConnection(self._config);
  43. self.setupAudioVideo();
  44. self._socket.on('offer', function(offer) {
  45. self._pc.setRemoteDescription(new RTCSessionDescription(offer.sdp),
  46. function() {
  47. // If we also have to set up a stream on the sink end, do so.
  48. self.handleStream(false, offer.source, function() {
  49. self.maybeBrowserisms(false, offer.source);
  50. });
  51. }, function(err) {
  52. console.log('failed to setRemoteDescription with offer, ', err);
  53. });
  54. });
  55. });
  56. } else {
  57. // Otherwise, this sink is the originator to another sink and should wait
  58. // for an alert.
  59. this._socket.emit('source', function(data) {
  60. self._id = data.id;
  61. if (!!self._handlers['ready']) {
  62. self._handlers['ready'](self._id);
  63. }
  64. self._socket.on('sink-connected', function(data) {
  65. target = data.sink;
  66. self._pc = new RTCPeerConnection(self._config);
  67. self.setupAudioVideo();
  68. self.handleStream(true, target, function() {
  69. self.maybeBrowserisms(true, target);
  70. });
  71. });
  72. self._socket.on('answer', function(data) {
  73. self._pc.setRemoteDescription(new RTCSessionDescription(data.sdp),
  74. function() {
  75. // Firefoxism
  76. if (browserisms == 'Firefox') {
  77. self._pc.connectDataConnection(self.localPort, self.remotePort);
  78. //self._pc.connectDataConnection(5000, 5001);
  79. self._socket.emit('port', { sink: data.sink, remote: self.localPort, local: self.remotePort });
  80. }
  81. console.log('ORIGINATOR: PeerConnection success');
  82. }, function(err) {
  83. console.log('failed to setRemoteDescription, ', err);
  84. });
  85. });
  86. });
  87. }
  88. };
  89. SinkPeer.prototype.maybeBrowserisms = function(originator, target) {
  90. console.log('maybeBrowserisms');
  91. var self = this;
  92. if (browserisms == 'Firefox' && !this._video && !this._audio && !this._stream) {
  93. getUserMedia({ audio: true, fake: true }, function(s) {
  94. self._pc.addStream(s);
  95. if (originator) {
  96. self.makeOffer(target);
  97. } else {
  98. self.makeAnswer(target);
  99. }
  100. }, function(err) { console.log('crap'); });
  101. } else {
  102. if (originator) {
  103. this.makeOffer(target);
  104. } else {
  105. this.makeAnswer(target);
  106. }
  107. }
  108. }
  109. SinkPeer.prototype.makeAnswer = function(target) {
  110. var self = this;
  111. this._pc.createAnswer(function(answer) {
  112. self._pc.setLocalDescription(answer, function() {
  113. if (browserisms && browserisms == 'Firefox') {
  114. self._socket.on('port', function(data) {
  115. self._pc.connectDataConnection(data.local, data.remote);
  116. });
  117. }
  118. self._socket.emit('answer',
  119. { 'sink': self._id,
  120. 'sdp': answer,
  121. 'source': target });
  122. // Firefoxism
  123. //if (browserisms && browserisms == 'Firefox') {
  124. //self._pc.connectDataConnection(5001, 5000);
  125. //}
  126. }, function(err) {
  127. console.log('failed to setLocalDescription, ', err)
  128. });
  129. }, function(err) {
  130. console.log('failed to create answer, ', err)
  131. });
  132. };
  133. SinkPeer.prototype.makeOffer = function(target) {
  134. var self = this;
  135. this._pc.createOffer(function(offer) {
  136. self._pc.setLocalDescription(offer, function() {
  137. self._socket.emit('offer',
  138. { 'sdp': offer,
  139. 'sink': target,
  140. 'source': self._id });
  141. }, function(err) {
  142. console.log('failed to setLocalDescription, ', err);
  143. });
  144. });
  145. };
  146. SinkPeer.prototype.setupAudioVideo = function() {
  147. console.log('setupAudioVideo');
  148. var self = this;
  149. this._pc.onaddstream = function(obj) {
  150. console.log('onaddstream');
  151. this._stream = true;
  152. if (!!self._handlers['remotestream']) {
  153. self._handlers['remotestream'](obj.type, obj.stream);
  154. }
  155. };
  156. };
  157. SinkPeer.prototype.handleStream = function(originator, target, cb) {
  158. if (this._data) {
  159. this.setupDataChannel(originator, target);
  160. } else {
  161. cb();
  162. }
  163. this.getAudioVideo(originator, cb);
  164. };
  165. SinkPeer.prototype.getAudioVideo = function(originator, cb) {
  166. var self = this;
  167. if (this._video) {
  168. getUserMedia({ video: true }, function(vstream) {
  169. self._pc.addStream(vstream);
  170. if (originator && !!self._handlers['localstream']) {
  171. self._handlers['localstream']('video', vstream);
  172. } else if (!originator && !self.handlers['remotestream']) {
  173. self.handlers['remotestream']('video', vstream);
  174. }
  175. if (self._audio) {
  176. getUserMedia({ audio: true }, function(astream) {
  177. self._pc.addStream(astream);
  178. if (originator && !!self._handlers['localstream']) {
  179. self._handlers['localstream']('audio', astream);
  180. } else if (!originator && !self.handlers['remotestream']) {
  181. self.handlers['remotestream']('audio', astream);
  182. }
  183. cb();
  184. }, function(err) { console.log('Audio cannot start'); });
  185. } else {
  186. cb();
  187. }
  188. }, function(err) { console.log('Video cannot start'); });
  189. } else if (this._audio) {
  190. getUserMedia({ audio: true }, function(astream) {
  191. self._pc.addStream(astream);
  192. if (!!self._handlers['localstream']) {
  193. self._handlers['localstream']('audio', astream);
  194. } else if (!originator && !self.handlers['remotestream']) {
  195. self.handlers['remotestream']('audio', astream);
  196. }
  197. cb();
  198. }, function(err) { console.log('Audio cannot start'); });
  199. } else {
  200. cb();
  201. }
  202. };
  203. SinkPeer.prototype.setupDataChannel = function(originator, target, cb) {
  204. var self = this;
  205. if (browserisms != 'Webkit') {
  206. if (originator) {
  207. /** ORIGINATOR SETUP */
  208. this._pc.onconnection = function() {
  209. console.log('ORIGINATOR: onconnection triggered');
  210. self._dc = self._pc.createDataChannel('StreamAPI', {}, target);
  211. self._dc.binaryType = 'blob';
  212. if (!!self._handlers['connection']) {
  213. self._handlers['connection'](target);
  214. }
  215. self._dc.onmessage = function(e) {
  216. self.handleDataMessage(e);
  217. };
  218. };
  219. } else {
  220. /** TARGET SETUP */
  221. this._pc.ondatachannel = function(dc) {
  222. console.log('SINK: ondatachannel triggered');
  223. self._dc = dc;
  224. self._dc.binaryType = 'blob';
  225. if (!!self._handlers['connection']) {
  226. self._handlers['connection'](target);
  227. }
  228. self._dc.onmessage = function(e) {
  229. self.handleDataMessage(e);
  230. };
  231. };
  232. this._pc.onconnection = function() {
  233. console.log('SINK: onconnection triggered');
  234. };
  235. }
  236. }
  237. this._pc.onclosedconnection = function() {
  238. // Remove socket handlers perhaps.
  239. };
  240. };
  241. SinkPeer.prototype.send = function(data) {
  242. var ab = BinaryPack.pack(data);
  243. this._dc.send(ab);
  244. }
  245. // Handles a DataChannel message.
  246. // TODO: have these extend Peer, which will impl these generic handlers.
  247. SinkPeer.prototype.handleDataMessage = function(e) {
  248. var self = this;
  249. var fr = new FileReader();
  250. fr.onload = function(evt) {
  251. var ab = evt.target.result;
  252. var data = BinaryPack.unpack(ab);
  253. if (!!self._handlers['data']) {
  254. self._handlers['data'](data);
  255. }
  256. };
  257. fr.readAsArrayBuffer(e.data);
  258. }
  259. SinkPeer.prototype.on = function(code, cb) {
  260. this._handlers[code] = cb;
  261. }