sink.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. self._pc.createAnswer(function(answer) {
  23. self._pc.setLocalDescription(answer, function() {
  24. self._socket.emit('answer',
  25. { 'sink': self._id,
  26. 'sdp': answer,
  27. 'source': offer.source });
  28. // Firefoxism
  29. console.log('FIREFOX');
  30. self._pc.connectDataConnection(5001, 5000);
  31. console.log('FIREFOX-2');
  32. }, function(err) {
  33. console.log('failed to setLocalDescription, ', err)
  34. });
  35. }, function(err) {
  36. console.log('failed to create answer, ', err)
  37. });
  38. }, function(err) {
  39. console.log('failed to setRemoteDescription with offer, ', err);
  40. });
  41. });
  42. });
  43. };
  44. SinkPeer.prototype.setupDataChannel = function() {
  45. this._pc.ondatachannel = function(dc) {
  46. console.log('SINK: ondatachannel triggered');
  47. dc.binaryType = "blob";
  48. dc.onmessage = function(e) {
  49. };
  50. self._dc = dc;
  51. };
  52. this._pc.onconnection = function() {
  53. console.log('SINK: onconnection triggered');
  54. }
  55. this._pc.onclosedconnection = function() {
  56. // ??
  57. };
  58. };