source.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. function SourcePeer(options) {
  2. // TODO: Allow passing in own ID.
  3. this._config = options.config || {};
  4. this._streams = options.streamType || 'd';
  5. this._name = options.name || 'StreamAPI';
  6. // PeerConnections open for this source. Client name => PC.
  7. this._pcs = {};
  8. this._id = null;
  9. // Same for DCs.
  10. this._dcs = {};
  11. this._socket = io.connect('http://localhost');
  12. this.socketInit();
  13. };
  14. SourcePeer.prototype.socketInit = function() {
  15. self = this;
  16. this._socket.emit('source', function(data) {
  17. self._id = data.id;
  18. self._socket.on('sink-connected', function(data) {
  19. console.log('SINK CONNECTED');
  20. // TODO: not just moz.
  21. target = data.sink;
  22. var pc = new window.mozRTCPeerConnection(self._config);
  23. self._pcs[target] = pc;
  24. // Setups will create streams--then the callback sets up the offers.
  25. self.handleStream(pc, target, function() {
  26. // Firefoxisms, or just dumb?
  27. navigator.mozGetUserMedia({ audio: true, fake: true }, function(s) {
  28. pc.addStream(s);
  29. pc.createOffer(function(offer) {
  30. pc.setLocalDescription(offer, function() {
  31. self._socket.emit('offer',
  32. { 'sdp': offer,
  33. 'sink': target,
  34. 'source': self._id });
  35. }, function(err) {
  36. console.log('failed to setLocalDescription, ', err);
  37. });
  38. });
  39. }, function(err) { console.log('crap'); });
  40. });
  41. });
  42. self._socket.on('answer', function(data) {
  43. self._pcs[data.sink].setRemoteDescription(data.sdp, function() {
  44. // Firefoxism
  45. console.log('FIREFOX', new Date());
  46. self._pcs[data.sink].connectDataConnection(5000, 5001);
  47. console.log('FIREFOX-2');
  48. console.log('SOURCE: PeerConnection success');
  49. }, function(err) {
  50. console.log('failed to setRemoteDescription, ', err)
  51. });
  52. });
  53. });
  54. };
  55. // Based on stream type requested, sets up the stream for PC.
  56. SourcePeer.prototype.handleStream = function(pc, target, cb) {
  57. pc.onaddstream = function(obj) {
  58. console.log('SOURCE: data stream get');
  59. };
  60. /*if (this._streams === 'v') {
  61. } else if (this._streams === 'a') {
  62. } else if (this._streams === 'av') {
  63. } else if (this._streams === 'd') {*/
  64. this.setupDataChannel(pc, target, cb);
  65. /*} else if (this._streams === 'dav') {
  66. this.setupDataChannel(pc, target);
  67. } else if (this._streams === 'da') {
  68. this.setupDataChannel(pc, target);
  69. } else if (this._streams === 'dv') {
  70. this.setupDataChannel(pc, target);
  71. } else {
  72. //error
  73. }*/
  74. };
  75. SourcePeer.prototype.setupDataChannel = function(pc, target, cb) {
  76. self = this;
  77. pc.onconnection = function() {
  78. console.log('SOURCE: onconnection triggered.');
  79. var dc = pc.createDataChannel(self._name, {}, target);
  80. self._dc[target] = dc;
  81. dc.binaryType = 'blob';
  82. dc.onmessage = function(e) {
  83. self.handleDataMessage(pc, e);
  84. // process e.data
  85. };
  86. };
  87. pc.ondatachannel = function() {
  88. console.log('SOURCE: data channeled');
  89. };
  90. pc.onclosedconnection = function() {
  91. // ??
  92. };
  93. cb();
  94. };
  95. SourcePeer.prototype.on = function(code, cb) {
  96. // For enduser.
  97. };