source.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. function SourcePeer(options) {
  2. this._config = options.config || {};
  3. this._streams = options.streamType || 'd';
  4. this._name = options.name || 'StreamAPI';
  5. // PeerConnections open for this source. Client name => PC.
  6. this._pcs = {};
  7. this._id = null;
  8. // Same for DCs.
  9. this._dcs = {};
  10. this._socket = io.connect('http://localhost');
  11. this.socketInit();
  12. };
  13. SourcePeer.prototype.socketInit = function() {
  14. self = this;
  15. this._socket.emit('source', function(data) {
  16. self._id = data.id;
  17. self._socket.on('sink-connected', function(data) {
  18. // TODO: not just moz.
  19. target = data.sink;
  20. var pc = new mozRTCPeerConnection(self._config);
  21. self._pcs[target] = pc;
  22. // Setups will create streams--then the callback sets up the offers.
  23. self.handleStream(pc, target, function() {
  24. pc.createOffer(function(offer) {
  25. pc.setLocalDescription(offer);
  26. self._socket.emit('offer',
  27. { 'sdp': offer, 'sink': target, 'source': this._id });
  28. });
  29. });
  30. });
  31. self._socket.on('answer', function(data, fn) {
  32. self._pcs[data.sink].setRemoteDescription(data.sdp);
  33. fn();
  34. // Firefoxism
  35. self._pcs[data.sink].connectDataConnection(5000,5001);
  36. });
  37. });
  38. };
  39. // Based on stream type requested, sets up the stream for PC.
  40. SourcePeer.prototype.handleStream(pc, target, cb) {
  41. /*if (this._streams === 'v') {
  42. } else if (this._streams === 'a') {
  43. } else if (this._streams === 'av') {
  44. } else if (this._streams === 'd') {*/
  45. this.setupDataChannel(pc, target);
  46. /*} else if (this._streams === 'dav') {
  47. this.setupDataChannel(pc, target);
  48. } else if (this._streams === 'da') {
  49. this.setupDataChannel(pc, target);
  50. } else if (this._streams === 'dv') {
  51. this.setupDataChannel(pc, target);
  52. } else {
  53. //error
  54. }*/
  55. };
  56. SourcePeer.prototype.setupDataChannel = function(pc, target) {
  57. pc.onconnection = function() {
  58. var dc = pc.createDataChannel(this._name, {}, target);
  59. this._dc[target] = dc;
  60. dc.binaryType = 'blob';
  61. dc.onmessage = function(e) {
  62. this.handleDataMessage(pc, e);
  63. // process e.data
  64. };
  65. };
  66. pc.onclosedconnection = function() {
  67. // ??
  68. };
  69. };
  70. SourcePeer.prototype.on = function(code, cb) {
  71. // For enduser.
  72. };
  73. SourcePeer.prototype.gotDescription = function(desc) {
  74. this._pc
  75. };