48. PromiseベースのAPI
// let the "negotiationneeded" event trigger offer generation
pc.onnegotiationneeded = function () {
pc.createOffer().then(function (offer) {
return pc.setLocalDescription(offer);
})
.then(function () {
// send the offer to the other peer
signalingChannel.send(JSON.stringify({ "desc":
pc.localDescription }));
})
.catch(logError);
};
49. pc.addTrack
// get a local stream, show it in a self-view and add it to be sent
navigator.mediaDevices.getUserMedia({ "audio": true, "video": true },
function (stream) {
selfView.srcObject = stream;
if (stream.getAudioTracks().length > 0)
pc.addTrack(stream.getAudioTracks()[0], stream);
if (stream.getVideoTracks().length > 0)
pc.addTrack(stream.getVideoTracks()[0], stream);
}, logError);