sink.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. function SinkPeer(options) {
  2. this._config = options.config || {};
  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. //FIREFOX
  44. self._pc.onaddstream = function(obj) {
  45. console.log('SINK: data stream get');
  46. };
  47. self.setupAudioVideo();
  48. self._socket.on('offer', function(offer) {
  49. self._pc.setRemoteDescription(offer.sdp, function() {
  50. // If we also have to set up a stream on the sink end, do so.
  51. self.handleStream(false, offer.source, function() {
  52. self.maybeBrowserisms(false, offer.source);
  53. });
  54. }, function(err) {
  55. console.log('failed to setRemoteDescription with offer, ', err);
  56. });
  57. });
  58. });
  59. } else {
  60. // Otherwise, this sink is the originator to another sink and should wait
  61. // for an alert.
  62. this._socket.emit('source', function(data) {
  63. self._id = data.id;
  64. if (!!self._handlers['ready']) {
  65. self._handlers['ready'](self._id);
  66. }
  67. self._socket.on('sink-connected', function(data) {
  68. target = data.sink;
  69. self._pc = new RTCPeerConnection(self._config);
  70. self.setupAudioVideo();
  71. self.handleStream(true, target, function() {
  72. self.maybeBrowserisms(true, target);
  73. });
  74. });
  75. self._socket.on('answer', function(data) {
  76. self._pc.setRemoteDescription(data.sdp, function() {
  77. // Firefoxism
  78. if (browserisms == 'Firefox') {
  79. self._pc.connectDataConnection(self.localPort, self.remotePort);
  80. //self._pc.connectDataConnection(5000, 5001);
  81. self._socket.emit('port', { sink: data.sink, remote: self.localPort, local: self.remotePort });
  82. }
  83. console.log('ORIGINATOR: PeerConnection success');
  84. }, function(err) {
  85. console.log('failed to setRemoteDescription, ', err);
  86. });
  87. });
  88. });
  89. }
  90. };
  91. SinkPeer.prototype.maybeBrowserisms = function(originator, target) {
  92. var self = this;
  93. if (browserisms == 'Firefox') {
  94. getUserMedia({ audio: true, fake: true }, function(s) {
  95. self._pc.addStream(s);
  96. if (originator) {
  97. self.makeOffer(target);
  98. } else {
  99. self.makeAnswer(target);
  100. }
  101. }, function(err) { console.log('crap'); });
  102. } else {
  103. if (originator) {
  104. this.makeOffer(target);
  105. } else {
  106. this.makeAnswer(target);
  107. }
  108. }
  109. }
  110. SinkPeer.prototype.makeAnswer = function(target) {
  111. var self = this;
  112. this._pc.createAnswer(function(answer) {
  113. self._pc.setLocalDescription(answer, function() {
  114. if (browserisms && browserisms == 'Firefox') {
  115. self._socket.on('port', function(data) {
  116. self._pc.connectDataConnection(data.local, data.remote);
  117. });
  118. }
  119. self._socket.emit('answer',
  120. { 'sink': self._id,
  121. 'sdp': answer,
  122. 'source': target });
  123. // Firefoxism
  124. //if (browserisms && browserisms == 'Firefox') {
  125. //self._pc.connectDataConnection(5001, 5000);
  126. //}
  127. }, function(err) {
  128. console.log('failed to setLocalDescription, ', err)
  129. });
  130. }, function(err) {
  131. console.log('failed to create answer, ', err)
  132. });
  133. };
  134. SinkPeer.prototype.makeOffer = function(target) {
  135. var self = this;
  136. this._pc.createOffer(function(offer) {
  137. self._pc.setLocalDescription(offer, function() {
  138. self._socket.emit('offer',
  139. { 'sdp': offer,
  140. 'sink': target,
  141. 'source': self._id });
  142. }, function(err) {
  143. console.log('failed to setLocalDescription, ', err);
  144. });
  145. });
  146. };
  147. SinkPeer.prototype.setupAudioVideo = function() {
  148. this._pc.onaddstream = function(obj) {
  149. if (!!self._handlers['remotestream']) {
  150. self._handlers['remotestream'](obj.type, obj.stream);
  151. }
  152. };
  153. };
  154. SinkPeer.prototype.handleStream = function(originator, target, cb) {
  155. if (this._data) {
  156. this.setupDataChannel(originator, target, cb);
  157. }
  158. this.getAudioVideo(cb);
  159. };
  160. SinkPeer.prototype.getAudioVideo = function(cb) {
  161. if (this._audio) {
  162. getUserMedia({ video: true }, function(stream) {
  163. self._pc.addStream(stream);
  164. cb();
  165. });
  166. }
  167. };
  168. SinkPeer.prototype.setupDataChannel = function(originator, target, cb) {
  169. var self = this;
  170. if (browserisms != 'Webkit') {
  171. if (originator) {
  172. /** ORIGINATOR SETUP */
  173. this._pc.onconnection = function() {
  174. console.log('ORIGINATOR: onconnection triggered');
  175. self._dc = self._pc.createDataChannel('StreamAPI', {}, target);
  176. self._dc.binaryType = 'blob';
  177. if (!!self._handlers['connection']) {
  178. self._handlers['connection'](target);
  179. }
  180. self._dc.onmessage = function(e) {
  181. self.handleDataMessage(e);
  182. };
  183. };
  184. } else {
  185. /** TARGET SETUP */
  186. this._pc.ondatachannel = function(dc) {
  187. console.log('SINK: ondatachannel triggered');
  188. self._dc = dc;
  189. self._dc.binaryType = 'blob';
  190. if (!!self._handlers['connection']) {
  191. self._handlers['connection'](target);
  192. }
  193. self._dc.onmessage = function(e) {
  194. self.handleDataMessage(e);
  195. };
  196. };
  197. this._pc.onconnection = function() {
  198. console.log('SINK: onconnection triggered');
  199. };
  200. }
  201. }
  202. this._pc.onclosedconnection = function() {
  203. // Remove socket handlers perhaps.
  204. };
  205. cb();
  206. };
  207. SinkPeer.prototype.send = function(data) {
  208. var ab = BinaryPack.pack(data);
  209. this._dc.send(ab);
  210. }
  211. // Handles a DataChannel message.
  212. // TODO: have these extend Peer, which will impl these generic handlers.
  213. SinkPeer.prototype.handleDataMessage = function(e) {
  214. var self = this;
  215. var fr = new FileReader();
  216. fr.onload = function(evt) {
  217. var ab = evt.target.result;
  218. var data = BinaryPack.unpack(ab);
  219. if (!!self._handlers['data']) {
  220. self._handlers['data'](data);
  221. }
  222. };
  223. fr.readAsArrayBuffer(e.data);
  224. }
  225. SinkPeer.prototype.on = function(code, cb) {
  226. this._handlers[code] = cb;
  227. }