浏览代码

fixed messaging, start new streaming stuff

Michelle Bu 12 年之前
父节点
当前提交
c7c48d6232
共有 5 个文件被更改,包括 51 次插入12 次删除
  1. 1 1
      public/js/binarypack
  2. 38 6
      public/js/sink.js
  3. 9 5
      public/js/source.js
  4. 1 0
      public/sink.html
  5. 2 0
      server.js

+ 1 - 1
public/js/binarypack

@@ -1 +1 @@
-Subproject commit 55aa8b3df2e7c38e0e7e028a566ef184c7b33ab1
+Subproject commit c708174c5baa7708409c9959803d2b3e35e07006

+ 38 - 6
public/js/sink.js

@@ -1,7 +1,9 @@
 function SinkPeer(options) {
   this._config = options.config || {};
   this._source = options.source || null;
-  this._stream = options.stream || 'd';
+  this._video = options.video;
+  this._data = options.data != undefined ? options.data : true;
+  this._audio = options.audio;
   this._pc = null;
   this._id = null;
   this._dc = null;
@@ -49,6 +51,8 @@ SinkPeer.prototype.socketInit = function() {
         console.log('SINK: data stream get');
       };
 
+      self.setupAudioVideo();
+
       self._socket.on('offer', function(offer) {
         self._pc.setRemoteDescription(offer.sdp, function() {
 
@@ -74,6 +78,7 @@ SinkPeer.prototype.socketInit = function() {
       self._socket.on('sink-connected', function(data) {
         target = data.sink;
         self._pc = new RTCPeerConnection(self._config);
+        self.setupAudioVideo();
         self.handleStream(true, target, function() {
           self.maybeBrowserisms(true, target);
         });
@@ -163,9 +168,32 @@ SinkPeer.prototype.makeOffer = function(target) {
 };
 
 
+SinkPeer.prototype.setupAudioVideo = function() {
+  this._pc.onaddstream = function(obj) {
+    if (!!self._handlers['remotestream']) {
+      self._handlers['remotestream'](obj.type, obj.stream);
+    }
+  };
+};
+
+
 SinkPeer.prototype.handleStream = function(originator, target, cb) {
-  this.setupDataChannel(originator, target, cb);
-}
+  if (this._data) {
+    this.setupDataChannel(originator, target, cb);
+  }
+  this.getAudioVideo(cb);
+};
+
+
+SinkPeer.prototype.getAudioVideo = function(cb) {
+  if (this._audio) {
+    getUserMedia({ video: true }, function(stream) {
+      self._pc.addStream(stream);
+      cb();
+    });
+  }
+
+};
 
 
 SinkPeer.prototype.setupDataChannel = function(originator, target, cb) {
@@ -225,11 +253,15 @@ SinkPeer.prototype.send = function(data) {
 // TODO: have these extend Peer, which will impl these generic handlers.
 SinkPeer.prototype.handleDataMessage = function(e) {
   var self = this;
-  BinaryPack.unpack(e.data, function(msg) {
+  var fr = new FileReader();
+  fr.onload = function(evt) {
+    var ab = evt.target.result;
+    var data = BinaryPack.unpack(ab);
     if (!!self._handlers['data']) {
-      self._handlers['data'](msg);
+      self._handlers['data'](data);
     }
-  });
+  };
+  fr.readAsArrayBuffer(e.data);
 }
 
 

+ 9 - 5
public/js/source.js

@@ -172,12 +172,16 @@ SourcePeer.prototype.send = function(data, sink) {
 
 // Handles a DataChannel message.
 SourcePeer.prototype.handleDataMessage = function(e) {
-  BinaryPack.unpack(e.data, function(msg) {
-    if (!!this._handlers['data']) {
-      this._handlers['data'](msg);
+  var self = this;
+  var fr = new FileReader();
+  fr.onload = function(evt) {
+    var ab = evt.target.result;
+    var data = BinaryPack.unpack(ab);
+    if (!!self._handlers['data']) {
+      self._handlers['data'](data);
     }
-  });
-
+  };
+  fr.readAsArrayBuffer(e.data);
 }
 
 

+ 1 - 0
public/sink.html

@@ -18,6 +18,7 @@ $(document).ready(function() {
     var source = $('#source').val();
     sink = new SinkPeer({ source: source });
     sink.on('data', function(data) {
+      console.log(data);
       sink.send('I am so scared.');
     });
   });

+ 2 - 0
server.js

@@ -30,6 +30,7 @@ io.sockets.on('connection', function(socket) {
     fn({ 'id': socket.id });
     connections[socket.id] = [];
   });
+
   // Sink connected.
   socket.on('sink', function(msg, fn) {
     var source_id = msg.source;
@@ -45,6 +46,7 @@ io.sockets.on('connection', function(socket) {
     sink = clients[msg.sink];
     sink.emit('offer', msg);
   });
+
   // Answer from dest to src.
   socket.on('answer', function (msg) {
     console.log('ANSWER MADE');