sink.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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._socket.emit('port', { sink: data.sink, remote: self.localPort, local: self.remotePort });
  79. }
  80. console.log('ORIGINATOR: PeerConnection success');
  81. }, function(err) {
  82. console.log('failed to setRemoteDescription, ', err);
  83. });
  84. });
  85. });
  86. }
  87. };
  88. SinkPeer.prototype.maybeBrowserisms = function(originator, target) {
  89. console.log('maybeBrowserisms');
  90. var self = this;
  91. if (browserisms == 'Firefox' && !this._video && !this._audio && !this._stream) {
  92. getUserMedia({ audio: true, fake: true }, function(s) {
  93. self._pc.addStream(s);
  94. if (originator) {
  95. self.makeOffer(target);
  96. } else {
  97. self.makeAnswer(target);
  98. }
  99. }, function(err) { console.log('crap'); });
  100. } else {
  101. if (originator) {
  102. this.makeOffer(target);
  103. } else {
  104. this.makeAnswer(target);
  105. }
  106. }
  107. }
  108. SinkPeer.prototype.makeAnswer = function(target) {
  109. var self = this;
  110. this._pc.createAnswer(function(answer) {
  111. self._pc.setLocalDescription(answer, function() {
  112. if (browserisms && browserisms == 'Firefox') {
  113. self._socket.on('port', function(data) {
  114. self._pc.connectDataConnection(data.local, data.remote);
  115. });
  116. }
  117. self._socket.emit('answer',
  118. { 'sink': self._id,
  119. 'sdp': answer,
  120. 'source': target });
  121. }, function(err) {
  122. console.log('failed to setLocalDescription, ', err)
  123. });
  124. }, function(err) {
  125. console.log('failed to create answer, ', err)
  126. });
  127. };
  128. SinkPeer.prototype.makeOffer = function(target) {
  129. var self = this;
  130. this._pc.createOffer(function(offer) {
  131. self._pc.setLocalDescription(offer, function() {
  132. self._socket.emit('offer',
  133. { 'sdp': offer,
  134. 'sink': target,
  135. 'source': self._id });
  136. }, function(err) {
  137. console.log('failed to setLocalDescription, ', err);
  138. });
  139. });
  140. };
  141. SinkPeer.prototype.setupAudioVideo = function() {
  142. var self = this;
  143. this._pc.onaddstream = function(obj) {
  144. this._stream = true;
  145. if (!!self._handlers['remotestream']) {
  146. self._handlers['remotestream'](obj.type, obj.stream);
  147. }
  148. };
  149. };
  150. SinkPeer.prototype.handleStream = function(originator, target, cb) {
  151. if (this._data) {
  152. this.setupDataChannel(originator, target);
  153. } else {
  154. cb();
  155. }
  156. this.getAudioVideo(originator, cb);
  157. };
  158. SinkPeer.prototype.getAudioVideo = function(originator, cb) {
  159. var self = this;
  160. if (this._video) {
  161. getUserMedia({ video: true }, function(vstream) {
  162. self._pc.addStream(vstream);
  163. if (originator && !!self._handlers['localstream']) {
  164. self._handlers['localstream']('video', vstream);
  165. } else if (!originator && !self.handlers['remotestream']) {
  166. self.handlers['remotestream']('video', vstream);
  167. }
  168. if (self._audio) {
  169. getUserMedia({ audio: true }, function(astream) {
  170. self._pc.addStream(astream);
  171. if (originator && !!self._handlers['localstream']) {
  172. self._handlers['localstream']('audio', astream);
  173. } else if (!originator && !self.handlers['remotestream']) {
  174. self.handlers['remotestream']('audio', astream);
  175. }
  176. cb();
  177. }, function(err) { console.log('Audio cannot start'); });
  178. } else {
  179. cb();
  180. }
  181. }, function(err) { console.log('Video cannot start'); });
  182. } else if (this._audio) {
  183. getUserMedia({ audio: true }, function(astream) {
  184. self._pc.addStream(astream);
  185. if (!!self._handlers['localstream']) {
  186. self._handlers['localstream']('audio', astream);
  187. } else if (!originator && !self.handlers['remotestream']) {
  188. self.handlers['remotestream']('audio', astream);
  189. }
  190. cb();
  191. }, function(err) { console.log('Audio cannot start'); });
  192. } else {
  193. cb();
  194. }
  195. };
  196. SinkPeer.prototype.setupDataChannel = function(originator, target, cb) {
  197. var self = this;
  198. if (browserisms != 'Webkit') {
  199. if (originator) {
  200. /** ORIGINATOR SETUP */
  201. this._pc.onconnection = function() {
  202. console.log('ORIGINATOR: onconnection triggered');
  203. self._dc = self._pc.createDataChannel('StreamAPI', {}, target);
  204. self._dc.binaryType = 'blob';
  205. if (!!self._handlers['connection']) {
  206. self._handlers['connection'](target);
  207. }
  208. self._dc.onmessage = function(e) {
  209. self.handleDataMessage(e);
  210. };
  211. };
  212. } else {
  213. /** TARGET SETUP */
  214. this._pc.ondatachannel = function(dc) {
  215. console.log('SINK: ondatachannel triggered');
  216. self._dc = dc;
  217. self._dc.binaryType = 'blob';
  218. if (!!self._handlers['connection']) {
  219. self._handlers['connection'](target);
  220. }
  221. self._dc.onmessage = function(e) {
  222. self.handleDataMessage(e);
  223. };
  224. };
  225. this._pc.onconnection = function() {
  226. console.log('SINK: onconnection triggered');
  227. };
  228. }
  229. }
  230. this._pc.onclosedconnection = function() {
  231. // Remove socket handlers perhaps.
  232. };
  233. };
  234. SinkPeer.prototype.send = function(data) {
  235. var ab = BinaryPack.pack(data);
  236. this._dc.send(ab);
  237. }
  238. // Handles a DataChannel message.
  239. // TODO: have these extend Peer, which will impl these generic handlers.
  240. SinkPeer.prototype.handleDataMessage = function(e) {
  241. var self = this;
  242. var fr = new FileReader();
  243. fr.onload = function(evt) {
  244. var ab = evt.target.result;
  245. var data = BinaryPack.unpack(ab);
  246. if (!!self._handlers['data']) {
  247. self._handlers['data'](data);
  248. }
  249. };
  250. fr.readAsArrayBuffer(e.data);
  251. }
  252. SinkPeer.prototype.on = function(code, cb) {
  253. this._handlers[code] = cb;
  254. }