sink.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. function SinkPeer(options, readyfn) {
  2. this._config = options.config || {};
  3. this._source = options.source || 'StreamAPI';
  4. this._pc = null;
  5. this._id = null;
  6. this._dc = null;
  7. this._socket = io.connect('http://localhost');
  8. this.socketInit(readyfn);
  9. };
  10. SinkPeer.prototype.socketInit = function(cb) {
  11. self = this;
  12. this._socket.emit('sink', { source: this._source }, function(data) {
  13. self._id = data.id;
  14. self._pc = new window.mozRTCPeerConnection(self._config);
  15. //FIREFOX
  16. self._pc.onaddstream = function(obj) {
  17. console.log('SINK: data stream get');
  18. };
  19. self.setupDataChannel();
  20. self._socket.on('offer', function(offer) {
  21. self._pc.setRemoteDescription(offer.sdp, function() {
  22. //Firefox
  23. navigator.mozGetUserMedia({ audio: true, fake: true }, function(s) {
  24. self._pc.addStream(s);
  25. self._pc.createAnswer(function(answer) {
  26. self._pc.setLocalDescription(answer, function() {
  27. self._socket.emit('answer',
  28. { 'sink': self._id,
  29. 'sdp': answer,
  30. 'source': offer.source });
  31. // Firefoxism
  32. console.log('FIREFOX');
  33. self._pc.connectDataConnection(5001, 5000);
  34. console.log('FIREFOX-2');
  35. }, function(err) {
  36. console.log('failed to setLocalDescription, ', err)
  37. });
  38. }, function(err) {
  39. console.log('failed to create answer, ', err)
  40. });
  41. }, function(err) { console.log('crap'); });
  42. }, function(err) {
  43. console.log('failed to setRemoteDescription with offer, ', err);
  44. });
  45. });
  46. });
  47. };
  48. SinkPeer.prototype.setupDataChannel = function() {
  49. this._pc.ondatachannel = function(dc) {
  50. console.log('SINK: ondatachannel triggered');
  51. dc.binaryType = "blob";
  52. dc.onmessage = function(e) {
  53. };
  54. self._dc = dc;
  55. };
  56. this._pc.onconnection = function() {
  57. console.log('SINK: onconnection triggered');
  58. }
  59. this._pc.onclosedconnection = function() {
  60. // ??
  61. };
  62. };