sink.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. function SinkPeer(options) {
  2. this._config = options.config || { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }]};
  3. this._peer = 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://192.168.0.14:8000');
  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. // Makes sure things clean up neatly.
  32. window.onbeforeunload = function() {
  33. if (!!this._socket) {
  34. this._socket.emit('leave');
  35. }
  36. }
  37. };
  38. function randomPort() {
  39. return Math.round(Math.random() * 60535) + 5000;
  40. };
  41. /** Start up websocket communications. */
  42. SinkPeer.prototype.socketInit = function() {
  43. var self = this;
  44. // Multiple sinks to one source.
  45. if (!!this._peer) {
  46. this._socket.emit('sink', { source: this._peer, isms: browserisms },
  47. function(data) {
  48. self._id = data.id;
  49. self.startPeerConnection();
  50. self._socket.on('offer', function(offer) {
  51. var sdp = offer.sdp;
  52. try {
  53. sdp = new RTCSessionDescription(offer.sdp);
  54. } catch(e) {
  55. console.log('Firefox');
  56. }
  57. self._pc.setRemoteDescription(sdp, function() {
  58. // If we also have to set up a stream on the sink end, do so.
  59. self.handleStream(false, function() {
  60. self.maybeBrowserisms(false);
  61. });
  62. }, function(err) {
  63. console.log('failed to setRemoteDescription with offer, ', err);
  64. });
  65. });
  66. /*self._socket.on('candidate', function(data) {
  67. console.log('SINK: Ice Candidate');
  68. var candidate = new RTCIceCandidate({ sdpMLineIndex: data.label,
  69. candidate: data.candidate });
  70. self._pc.addIceCandidate(candidate);
  71. });*/
  72. });
  73. } else {
  74. // Otherwise, this sink is the originator to another sink and should wait
  75. // for an alert.
  76. this._socket.emit('source', function(data) {
  77. self._id = data.id;
  78. if (!!self._handlers['ready']) {
  79. self._handlers['ready'](self._id);
  80. }
  81. self._socket.on('sink-connected', function(data) {
  82. self._peer = data.sink;
  83. self.startPeerConnection();
  84. self.handleStream(true, function() {
  85. self.maybeBrowserisms(true);
  86. });
  87. });
  88. self._socket.on('answer', function(data) {
  89. var sdp = data.sdp;
  90. try {
  91. sdp = new RTCSessionDescription(data.sdp);
  92. } catch(e) {
  93. console.log('Firefox');
  94. }
  95. self._pc.setRemoteDescription(sdp, function() {
  96. // Firefoxism
  97. if (browserisms == 'Firefox') {
  98. self._pc.connectDataConnection(self.localPort, self.remotePort);
  99. self._socket.emit('port', { sink: data.sink, remote: self.localPort, local: self.remotePort });
  100. }
  101. console.log('ORIGINATOR: PeerConnection success');
  102. }, function(err) {
  103. console.log('failed to setRemoteDescription, ', err);
  104. });
  105. });
  106. self._socket.on('candidate', function(data) {
  107. console.log('ORIGINATOR: Ice Candidate');
  108. var candidate = new RTCIceCandidate({ sdpMLineIndex: data.label,
  109. candidate: data.candidate });
  110. self._pc.addIceCandidate(candidate);
  111. });
  112. });
  113. }
  114. };
  115. /** Takes care of ice handlers. */
  116. SinkPeer.prototype.setupIce = function() {
  117. this._pc.onicecandidate = function(event) {
  118. if (event.candidate) {
  119. this._socket.emit('candidate', { label: event.candidate.sdpMLineIndex,
  120. id: event.candidate.sdpMid,
  121. candidate: event.candidate.candidate });
  122. } else {
  123. console.log("End of candidates.");
  124. }
  125. };
  126. };
  127. /** Starts a PeerConnection and sets up handlers. */
  128. SinkPeer.prototype.startPeerConnection = function() {
  129. this._pc = new RTCPeerConnection(this._config);
  130. //this.setupIce();
  131. this.setupAudioVideo();
  132. };
  133. /** Decide whether to handle Firefoxisms. */
  134. SinkPeer.prototype.maybeBrowserisms = function(originator) {
  135. var self = this;
  136. if (browserisms == 'Firefox' && !this._video && !this._audio && !this._stream) {
  137. getUserMedia({ audio: true, fake: true }, function(s) {
  138. self._pc.addStream(s);
  139. if (originator) {
  140. self.makeOffer();
  141. } else {
  142. self.makeAnswer();
  143. }
  144. }, function(err) { console.log('crap'); });
  145. } else {
  146. if (originator) {
  147. this.makeOffer();
  148. } else {
  149. this.makeAnswer();
  150. }
  151. }
  152. }
  153. /** Create an answer for PC. */
  154. SinkPeer.prototype.makeAnswer = function() {
  155. var self = this;
  156. this._pc.createAnswer(function(answer) {
  157. self._pc.setLocalDescription(answer, function() {
  158. if (browserisms && browserisms == 'Firefox') {
  159. self._socket.on('port', function(data) {
  160. self._pc.connectDataConnection(data.local, data.remote);
  161. });
  162. }
  163. self._socket.emit('answer',
  164. { 'sink': self._id,
  165. 'sdp': answer,
  166. 'source': self._peer });
  167. }, function(err) {
  168. console.log('failed to setLocalDescription, ', err)
  169. });
  170. }, function(err) {
  171. console.log('failed to create answer, ', err)
  172. });
  173. };
  174. /** Create an offer for PC. */
  175. SinkPeer.prototype.makeOffer = function() {
  176. var self = this;
  177. this._pc.createOffer(function(offer) {
  178. self._pc.setLocalDescription(offer, function() {
  179. self._socket.emit('offer',
  180. { 'sdp': offer,
  181. 'sink': self._peer,
  182. 'source': self._id });
  183. }, function(err) {
  184. console.log('failed to setLocalDescription, ', err);
  185. });
  186. });
  187. };
  188. /** Sets up A/V stream handler. */
  189. SinkPeer.prototype.setupAudioVideo = function() {
  190. var self = this;
  191. this._pc.onaddstream = function(obj) {
  192. this._stream = true;
  193. if (!!self._handlers['remotestream']) {
  194. self._handlers['remotestream'](obj.type, obj.stream);
  195. }
  196. };
  197. };
  198. /** Handle the different types of streams requested by user. */
  199. SinkPeer.prototype.handleStream = function(originator, cb) {
  200. if (this._data) {
  201. this.setupDataChannel(originator);
  202. } else {
  203. cb();
  204. }
  205. this.getAudioVideo(originator, cb);
  206. };
  207. /** Get A/V streams. */
  208. SinkPeer.prototype.getAudioVideo = function(originator, cb) {
  209. var self = this;
  210. if (this._video) {
  211. getUserMedia({ video: true }, function(vstream) {
  212. self._pc.addStream(vstream);
  213. if (originator && !!self._handlers['localstream']) {
  214. self._handlers['localstream']('video', vstream);
  215. } else if (!originator && !self._handlers['remotestream']) {
  216. self._handlers['remotestream']('video', vstream);
  217. }
  218. if (self._audio) {
  219. getUserMedia({ audio: true }, function(astream) {
  220. self._pc.addStream(astream);
  221. if (originator && !!self._handlers['localstream']) {
  222. self._handlers['localstream']('audio', astream);
  223. } else if (!originator && !self._handlers['remotestream']) {
  224. self._handlers['remotestream']('audio', astream);
  225. }
  226. cb();
  227. }, function(err) { console.log('Audio cannot start'); });
  228. } else {
  229. cb();
  230. }
  231. }, function(err) { console.log('Video cannot start'); });
  232. } else if (this._audio) {
  233. getUserMedia({ audio: true }, function(astream) {
  234. self._pc.addStream(astream);
  235. if (!!self._handlers['localstream']) {
  236. self._handlers['localstream']('audio', astream);
  237. } else if (!originator && !self.handlers['remotestream']) {
  238. self.handlers['remotestream']('audio', astream);
  239. }
  240. cb();
  241. }, function(err) { console.log('Audio cannot start'); });
  242. } else {
  243. cb();
  244. }
  245. };
  246. /** Sets up DataChannel handlers. */
  247. SinkPeer.prototype.setupDataChannel = function(originator, cb) {
  248. var self = this;
  249. if (browserisms != 'Webkit') {
  250. if (originator) {
  251. /** ORIGINATOR SETUP */
  252. this._pc.onconnection = function() {
  253. console.log('ORIGINATOR: onconnection triggered');
  254. self._dc = self._pc.createDataChannel('StreamAPI', {}, self._peer);
  255. self._dc.binaryType = 'blob';
  256. if (!!self._handlers['connection']) {
  257. self._handlers['connection'](self._peer);
  258. }
  259. self._dc.onmessage = function(e) {
  260. self.handleDataMessage(e);
  261. };
  262. };
  263. } else {
  264. /** TARGET SETUP */
  265. this._pc.ondatachannel = function(dc) {
  266. console.log('SINK: ondatachannel triggered');
  267. self._dc = dc;
  268. self._dc.binaryType = 'blob';
  269. if (!!self._handlers['connection']) {
  270. self._handlers['connection'](self._peer);
  271. }
  272. self._dc.onmessage = function(e) {
  273. self.handleDataMessage(e);
  274. };
  275. };
  276. this._pc.onconnection = function() {
  277. console.log('SINK: onconnection triggered');
  278. };
  279. }
  280. }
  281. this._pc.onclosedconnection = function() {
  282. // Remove socket handlers perhaps.
  283. };
  284. };
  285. /** Allows user to send data. */
  286. SinkPeer.prototype.send = function(data) {
  287. var ab = BinaryPack.pack(data);
  288. this._dc.send(ab);
  289. }
  290. // Handles a DataChannel message.
  291. // TODO: have these extend Peer, which will impl these generic handlers.
  292. SinkPeer.prototype.handleDataMessage = function(e) {
  293. var self = this;
  294. var fr = new FileReader();
  295. fr.onload = function(evt) {
  296. var ab = evt.target.result;
  297. var data = BinaryPack.unpack(ab);
  298. if (!!self._handlers['data']) {
  299. self._handlers['data'](data);
  300. }
  301. };
  302. fr.readAsArrayBuffer(e.data);
  303. }
  304. SinkPeer.prototype.on = function(code, cb) {
  305. this._handlers[code] = cb;
  306. }