source.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. var dc = pc.createDataChannel(this._name, {}, target);
  43. this._dc[target] = dc;
  44. dc.binaryType = 'blob';
  45. dc.onmessage = function(e) {
  46. // process e.data
  47. };
  48. } else if (this._streams === 'dav') {
  49. } else if (this._streams === 'da') {
  50. } else if (this._streams === 'dv') {
  51. } else {
  52. //error
  53. }
  54. }
  55. SourcePeer.prototype.setupDataChannel = function(pc, target) {
  56. });
  57. SourcePeer.prototype.gotDescription = function(desc) {
  58. this._pc
  59. };