sink.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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(JSON.parse(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': JSON.stringify(answer),
  27. 'source': offer.source });
  28. // Firefoxism
  29. console.log('FIREFOX', new Date());
  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. cb(self._id);
  43. });
  44. };
  45. SinkPeer.prototype.setupDataChannel = function() {
  46. this._pc.ondatachannel = function(dc) {
  47. console.log('SINK: ondatachannel triggered');
  48. dc.binaryType = "blob";
  49. dc.onmessage = function(e) {
  50. };
  51. self._dc = dc;
  52. };
  53. this._pc.onconnection = function() {
  54. console.log('SINK: onconnection triggered');
  55. }
  56. this._pc.onclosedconnection = function() {
  57. // ??
  58. };
  59. };