sink.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. function SinkPeer(options) {
  2. var options = options || {};
  3. debug = options.debug;
  4. this._config = options.config || { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] };
  5. this._id = null;
  6. // User handlers.
  7. this._handlers = {};
  8. // Source to connect to; null if waiting for a connection.
  9. this._peer = options.source || null;
  10. // Booleans to determine what streams to allow.
  11. this._video = options.video;
  12. this._data = options.data != undefined ? options.data : true;
  13. this._audio = options.audio;
  14. // Connections
  15. this._pc = null;
  16. this._dc = null;
  17. this._socket = new WebSocket(options.ws || 'ws://localhost');
  18. // Local streams for multiple use.
  19. this._localVideo = options.localVideo || null;
  20. this._localAudio = options.localAudio || null;
  21. // Init socket msg handlers
  22. var self = this;
  23. this._socket.onopen = function() {
  24. self.socketInit();
  25. };
  26. // Firefoxism: connectDataConnection ports.
  27. if (browserisms == 'Firefox' && !options.source) {
  28. if (!SinkPeer.usedPorts) {
  29. SinkPeer.usedPorts = [];
  30. }
  31. this.localPort = randomPort();
  32. while (SinkPeer.usedPorts.indexOf(this.localPort) != -1) {
  33. this.localPort = randomPort();
  34. }
  35. this.remotePort = randomPort();
  36. while (this.remotePort == this.localPort ||
  37. SinkPeer.usedPorts.indexOf(this.localPort) != -1) {
  38. this.remotePort = randomPort();
  39. }
  40. SinkPeer.usedPorts.push(this.remotePort);
  41. SinkPeer.usedPorts.push(this.localPort);
  42. }
  43. };
  44. /** Generates random port number. */
  45. function randomPort() {
  46. return Math.round(Math.random() * 60535) + 5000;
  47. };
  48. /** Start up websocket communications. */
  49. SinkPeer.prototype.socketInit = function() {
  50. var self = this;
  51. if (!!this._peer) {
  52. // Announce as a sink if initiated with a source.
  53. this._socket.send(JSON.stringify({
  54. type: 'SINK',
  55. source: this._peer,
  56. isms: browserisms
  57. }));
  58. this._socket.onmessage = function(event) {
  59. var message = JSON.parse(event.data);
  60. switch (message.type) {
  61. case 'SINK-ID':
  62. self._id = message.id;
  63. if (!!self._handlers['ready']) {
  64. self._handlers['ready'](self._id);
  65. }
  66. self.startPeerConnection();
  67. break;
  68. case 'OFFER':
  69. var sdp = message.sdp;
  70. try {
  71. sdp = new RTCSessionDescription(message.sdp);
  72. } catch(e) {
  73. log('Firefox');
  74. }
  75. self._pc.setRemoteDescription(sdp, function() {
  76. log('setRemoteDescription: offer');
  77. // If we also have to set up a stream on the sink end, do so.
  78. self.handleStream(false, function() {
  79. self.maybeBrowserisms(false);
  80. });
  81. }, function(err) {
  82. log('failed to setRemoteDescription with offer, ', err);
  83. });
  84. break;
  85. case 'CANDIDATE':
  86. log(message.candidate);
  87. var candidate = new RTCIceCandidate(message.candidate);
  88. self._pc.addIceCandidate(candidate);
  89. break;
  90. case 'LEAVE':
  91. log('counterpart disconnected');
  92. if (!!self._pc && self._pc.readyState != 'closed') {
  93. self._pc.close();
  94. self._pc = null;
  95. self._peer = null;
  96. }
  97. if (!!self._dc && self._dc.readyState != 'closed') {
  98. self._dc.close();
  99. self._dc = null;
  100. }
  101. break;
  102. case 'PORT':
  103. if (browserisms && browserisms == 'Firefox') {
  104. if (!SinkPeer.usedPorts) {
  105. SinkPeer.usedPorts = [];
  106. }
  107. SinkPeer.usedPorts.push(message.local);
  108. SinkPeer.usedPorts.push(message.remote);
  109. self._pc.connectDataConnection(message.local, message.remote);
  110. break;
  111. }
  112. case 'DEFAULT':
  113. log('SINK: unrecognized message ', message.type);
  114. break;
  115. }
  116. };
  117. } else {
  118. // Otherwise, this sink is the originator to another sink and should wait
  119. // for an alert to begin the PC process.
  120. this._socket.send(JSON.stringify({
  121. type: 'SOURCE',
  122. isms: browserisms
  123. }));
  124. this._socket.onmessage = function(event) {
  125. var message = JSON.parse(event.data);
  126. switch (message.type) {
  127. case 'SOURCE-ID':
  128. self._id = message.id;
  129. if (!!self._handlers['ready']) {
  130. self._handlers['ready'](self._id);
  131. }
  132. break;
  133. case 'SINK-CONNECTED':
  134. self._peer = message.sink;
  135. self.startPeerConnection();
  136. self.handleStream(true, function() {
  137. self.maybeBrowserisms(true);
  138. });
  139. break;
  140. case 'ANSWER':
  141. var sdp = message.sdp;
  142. try {
  143. sdp = new RTCSessionDescription(message.sdp);
  144. } catch(e) {
  145. log('Firefox');
  146. }
  147. self._pc.setRemoteDescription(sdp, function() {
  148. log('setRemoteDescription: answer');
  149. // Firefoxism
  150. if (browserisms == 'Firefox') {
  151. self._pc.connectDataConnection(self.localPort, self.remotePort);
  152. self._socket.send(JSON.stringify({
  153. type: 'PORT',
  154. dst: self._peer,
  155. remote: self.localPort,
  156. local: self.remotePort
  157. }));
  158. }
  159. log('ORIGINATOR: PeerConnection success');
  160. }, function(err) {
  161. log('failed to setRemoteDescription, ', err);
  162. });
  163. break;
  164. case 'CANDIDATE':
  165. log(message.candidate);
  166. var candidate = new RTCIceCandidate(message.candidate);
  167. self._pc.addIceCandidate(candidate);
  168. break;
  169. case 'LEAVE':
  170. log('counterpart disconnected');
  171. if (!!self._pc && self._pc.readyState != 'closed') {
  172. self._pc.close();
  173. self._pc = null;
  174. self._peer = null;
  175. }
  176. if (!!self._dc && self._dc.readyState != 'closed') {
  177. self._dc.close();
  178. self._dc = null;
  179. }
  180. break;
  181. case 'DEFAULT':
  182. log('ORIGINATOR: message not recognized ', message.type);
  183. }
  184. };
  185. }
  186. // Makes sure things clean up neatly when window is closed.
  187. window.onbeforeunload = function() {
  188. if (!!self._pc && self._pc.readyState != 'closed') {
  189. self._pc.close();
  190. }
  191. if (!!self._socket && !!self._peer) {
  192. self._socket.send(JSON.stringify({ type: 'LEAVE', dst: self._peer }));
  193. if (!!self._dc && self._dc.readyState != 'closed') {
  194. self._dc.close();
  195. }
  196. }
  197. }
  198. };
  199. /** Takes care of ice handlers. */
  200. SinkPeer.prototype.setupIce = function() {
  201. var self = this;
  202. this._pc.onicecandidate = function(event) {
  203. log('candidates received');
  204. if (event.candidate) {
  205. self._socket.send(JSON.stringify({
  206. type: 'CANDIDATE',
  207. candidate: event.candidate,
  208. dst: self._peer
  209. }));
  210. } else {
  211. log("End of candidates.");
  212. }
  213. };
  214. };
  215. /** Starts a PeerConnection and sets up handlers. */
  216. SinkPeer.prototype.startPeerConnection = function() {
  217. this._pc = new RTCPeerConnection(this._config, { optional:[ { RtpDataChannels: true } ]});
  218. this.setupIce();
  219. this.setupAudioVideo();
  220. };
  221. /** Decide whether to handle Firefoxisms. */
  222. SinkPeer.prototype.maybeBrowserisms = function(originator) {
  223. var self = this;
  224. if (browserisms == 'Firefox' && !this._video && !this._audio/* && !this._stream*/) {
  225. getUserMedia({ audio: true, fake: true }, function(s) {
  226. self._pc.addStream(s);
  227. if (originator) {
  228. self.makeOffer();
  229. } else {
  230. self.makeAnswer();
  231. }
  232. }, function(err) { log('crap'); });
  233. } else {
  234. if (originator) {
  235. this.makeOffer();
  236. } else {
  237. this.makeAnswer();
  238. }
  239. }
  240. }
  241. /** Create an answer for PC. */
  242. SinkPeer.prototype.makeAnswer = function() {
  243. var self = this;
  244. this._pc.createAnswer(function(answer) {
  245. log('createAnswer');
  246. self._pc.setLocalDescription(answer, function() {
  247. log('setLocalDescription: answer');
  248. self._socket.send(JSON.stringify({
  249. type: 'ANSWER',
  250. src: self._id,
  251. sdp: answer,
  252. dst: self._peer
  253. }));
  254. }, function(err) {
  255. log('failed to setLocalDescription, ', err)
  256. });
  257. }, function(err) {
  258. log('failed to create answer, ', err)
  259. });
  260. };
  261. /** Create an offer for PC. */
  262. SinkPeer.prototype.makeOffer = function() {
  263. var self = this;
  264. this._pc.createOffer(function(offer) {
  265. log('createOffer')
  266. self._pc.setLocalDescription(offer, function() {
  267. log('setLocalDescription: offer');
  268. self._socket.send(JSON.stringify({
  269. type: 'OFFER',
  270. sdp: offer,
  271. dst: self._peer,
  272. src: self._id
  273. }));
  274. }, function(err) {
  275. log('failed to setLocalDescription, ', err);
  276. });
  277. });
  278. };
  279. /** Sets up A/V stream handler. */
  280. SinkPeer.prototype.setupAudioVideo = function() {
  281. var self = this;
  282. log('onaddstream handler added');
  283. this._pc.onaddstream = function(obj) {
  284. log('Remote stream added');
  285. //this._stream = true;
  286. if (!!self._handlers['remotestream']) {
  287. self._handlers['remotestream'](obj.type, obj.stream);
  288. }
  289. };
  290. };
  291. /** Handle the different types of streams requested by user. */
  292. SinkPeer.prototype.handleStream = function(originator, cb) {
  293. if (this._data) {
  294. this.setupDataChannel(originator);
  295. }
  296. this.getAudioVideo(originator, cb);
  297. };
  298. /** Get A/V streams. */
  299. SinkPeer.prototype.getAudioVideo = function(originator, cb) {
  300. var self = this;
  301. if (this._video && !this._localVideo) {
  302. getUserMedia({ video: true }, function(vstream) {
  303. self._pc.addStream(vstream);
  304. self._localVideo = vstream;
  305. log('Local video stream added');
  306. if (!!self._handlers['localstream']) {
  307. self._handlers['localstream']('video', vstream);
  308. }
  309. if (self._audio && !self._localAudio) {
  310. getUserMedia({ audio: true }, function(astream) {
  311. self._pc.addStream(astream);
  312. self._localAudio = astream;
  313. log('Local audio stream added');
  314. if (!!self._handlers['localstream']) {
  315. self._handlers['localstream']('audio', astream);
  316. }
  317. cb();
  318. }, function(err) { log('Audio cannot start'); cb(); });
  319. } else {
  320. if (self._audio) {
  321. self._pc.addStream(self._localAudio);
  322. }
  323. cb();
  324. }
  325. }, function(err) { log('Video cannot start', err); cb(); });
  326. } else if (this._audio && !this._localAudio) {
  327. getUserMedia({ audio: true }, function(astream) {
  328. self._pc.addStream(astream);
  329. self._localAudio = astream;
  330. log('Local audio stream added');
  331. if (!!self._handlers['localstream']) {
  332. self._handlers['localstream']('audio', astream);
  333. }
  334. cb();
  335. }, function(err) { log('Audio cannot start'); cb(); });
  336. } else {
  337. if (this._audio) {
  338. this._pc.addStream(this._localAudio);
  339. }
  340. if (this._video) {
  341. this._pc.addStream(this._localVideo);
  342. }
  343. log('no audio/video streams initiated');
  344. cb();
  345. }
  346. };
  347. /** Sets up DataChannel handlers. */
  348. SinkPeer.prototype.setupDataChannel = function(originator, cb) {
  349. var self = this;
  350. if (originator) {
  351. /** ORIGINATOR SETUP */
  352. if (browserisms == 'Webkit') {
  353. this._pc.onstatechange = function() {
  354. log('State Change: ', self._pc.readyState);
  355. /*if (self._pc.readyState == 'active') {
  356. log('ORIGINATOR: active state detected');
  357. self._dc = self._pc.createDataChannel('StreamAPI', { reliable: false });
  358. self._dc.binaryType = 'blob';
  359. if (!!self._handlers['connection']) {
  360. self._handlers['connection'](self._peer);
  361. }
  362. self._dc.onmessage = function(e) {
  363. self.handleDataMessage(e);
  364. };
  365. }*/
  366. }
  367. } else {
  368. this._pc.onconnection = function() {
  369. log('ORIGINATOR: onconnection triggered');
  370. self.startDataChannel();
  371. };
  372. }
  373. } else {
  374. /** TARGET SETUP */
  375. this._pc.ondatachannel = function(dc) {
  376. log('SINK: ondatachannel triggered');
  377. self._dc = dc;
  378. self._dc.binaryType = 'blob';
  379. if (!!self._handlers['connection']) {
  380. self._handlers['connection'](self._peer);
  381. }
  382. self._dc.onmessage = function(e) {
  383. self.handleDataMessage(e);
  384. };
  385. };
  386. this._pc.onconnection = function() {
  387. log('SINK: onconnection triggered');
  388. };
  389. }
  390. this._pc.onclosedconnection = function() {
  391. // Remove socket handlers perhaps.
  392. };
  393. };
  394. SinkPeer.prototype.startDataChannel = function() {
  395. var self = this;
  396. this._dc = this._pc.createDataChannel(this._peer, { reliable: false });
  397. this._dc.binaryType = 'blob';
  398. if (!!this._handlers['connection']) {
  399. this._handlers['connection'](this._peer);
  400. }
  401. this._dc.onmessage = function(e) {
  402. self.handleDataMessage(e);
  403. };
  404. };
  405. /** Allows user to send data. */
  406. SinkPeer.prototype.send = function(data) {
  407. var ab = BinaryPack.pack(data);
  408. this._dc.send(ab);
  409. }
  410. // Handles a DataChannel message.
  411. // TODO: have these extend Peer, which will impl these generic handlers.
  412. SinkPeer.prototype.handleDataMessage = function(e) {
  413. var self = this;
  414. var fr = new FileReader();
  415. fr.onload = function(evt) {
  416. var ab = evt.target.result;
  417. var data = BinaryPack.unpack(ab);
  418. if (!!self._handlers['data']) {
  419. self._handlers['data'](data);
  420. }
  421. };
  422. fr.readAsArrayBuffer(e.data);
  423. }
  424. SinkPeer.prototype.on = function(code, cb) {
  425. this._handlers[code] = cb;
  426. }
  427. function log() {
  428. if (debug) {
  429. for (var i = 0; i < arguments.length; i++) {
  430. console.log('*', i, '-- ', arguments[i]);
  431. }
  432. }
  433. }
  434. exports.Peer = SinkPeer;