source.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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', { 'sdp': offer, 'sink': target });
  27. });
  28. });
  29. });
  30. self._socket.on('answer', function(data) {
  31. self._pcs[data.sink].setRemoteDescription(data.sdp);
  32. });
  33. });
  34. };
  35. // Based on stream type requested, sets up the stream for PC.
  36. SourcePeer.prototype.handleStream(pc, target, cb) {
  37. /*if (this._streams === 'v') {
  38. } else if (this._streams === 'a') {
  39. } else if (this._streams === 'av') {
  40. } else if (this._streams === 'd') {*/
  41. this.setupDataChannel(pc, target);
  42. /*} else if (this._streams === 'dav') {
  43. this.setupDataChannel(pc, target);
  44. } else if (this._streams === 'da') {
  45. this.setupDataChannel(pc, target);
  46. } else if (this._streams === 'dv') {
  47. this.setupDataChannel(pc, target);
  48. } else {
  49. //error
  50. }*/
  51. };
  52. SourcePeer.prototype.setupDataChannel = function(pc, target) {
  53. pc.onconnection = function() {
  54. var dc = pc.createDataChannel(this._name, {}, target);
  55. this._dc[target] = dc;
  56. dc.binaryType = 'blob';
  57. dc.onmessage = function(e) {
  58. this.handleDataMessage(pc, e);
  59. // process e.data
  60. };
  61. };
  62. pc.onclosedconnection = function() {
  63. // ??
  64. };
  65. };
  66. SourcePeer.prototype.on = function(code, cb) {
  67. // For enduser.
  68. };
  69. SourcePeer.prototype.gotDescription = function(desc) {
  70. this._pc
  71. };