sink.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. function SinkPeer(options, readyfn) {
  2. if (!browserisms) {
  3. readyfn({ error: 'RTC-incompatible browser' });
  4. }
  5. this._config = options.config || {};
  6. this._source = options.source || 'StreamAPI';
  7. this._pc = null;
  8. this._id = null;
  9. this._dc = null;
  10. this._socket = io.connect('http://localhost');
  11. this.socketInit(readyfn);
  12. };
  13. SinkPeer.prototype.socketInit = function(cb) {
  14. self = this;
  15. this._socket.emit('sink', { source: this._source, isms: browserisms },
  16. function(data) {
  17. self._id = data.id;
  18. self._pc = new RTCPeerConnection(self._config);
  19. //FIREFOX
  20. self._pc.onaddstream = function(obj) {
  21. console.log('SINK: data stream get');
  22. };
  23. self.setupDataChannel();
  24. self._socket.on('offer', function(offer) {
  25. self._pc.setRemoteDescription(offer.sdp, function() {
  26. //Firefox
  27. getUserMedia({ audio: true, fake: true }, function(s) {
  28. self._pc.addStream(s);
  29. self._pc.createAnswer(function(answer) {
  30. self._pc.setLocalDescription(answer, function() {
  31. self._socket.emit('answer',
  32. { 'sink': self._id,
  33. 'sdp': answer,
  34. 'source': offer.source });
  35. // Firefoxism
  36. if (browserisms && browserisms == 'Firefox') {
  37. self._pc.connectDataConnection(5001, 5000);
  38. }
  39. if (cb) {
  40. cb({ response: 'done' });
  41. }
  42. }, function(err) {
  43. console.log('failed to setLocalDescription, ', err)
  44. });
  45. }, function(err) {
  46. console.log('failed to create answer, ', err)
  47. });
  48. }, function(err) { console.log('crap'); });
  49. }, function(err) {
  50. console.log('failed to setRemoteDescription with offer, ', err);
  51. });
  52. });
  53. });
  54. };
  55. SinkPeer.prototype.setupDataChannel = function() {
  56. self = this;
  57. if (browserisms != 'Webkit') {
  58. this._pc.ondatachannel = function(dc) {
  59. console.log('SINK: ondatachannel triggered');
  60. dc.binaryType = 'arraybuffer';
  61. dc.onmessage = function(e) {
  62. self.handleDataMessage(e);
  63. };
  64. self._dc = dc;
  65. };
  66. this._pc.onconnection = function() {
  67. console.log('SINK: onconnection triggered');
  68. }
  69. }
  70. this._pc.onclosedconnection = function() {
  71. // Remove socket handlers perhaps.
  72. };
  73. };
  74. SinkPeer.prototype.send = function(data) {
  75. var ab = MsgPack.encode(data);
  76. this._dc.send(ab);
  77. }
  78. // Handles a DataChannel message.
  79. // TODO: have these extend Peer, which will impl these generic handlers.
  80. SinkPeer.prototype.handleDataMessage = function(e) {
  81. data = MsgPack.decode(e.data);
  82. console.log(data);
  83. if (!!this._dataHandler) {
  84. this._dataHandler(data);
  85. }
  86. }
  87. SinkPeer.prototype.on = function(code, cb) {
  88. if (code === 'stream') {
  89. this._streamHandler = cb;
  90. } else if (code === 'disconnect') {
  91. this._disconnectHandler = cb;
  92. } else if (code === 'data') {
  93. this._dataHandler = cb;
  94. }
  95. }