Skip to content

Instantly share code, notes, and snippets.

@the-eater

the-eater/0.js Secret

Created February 28, 2019 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-eater/6d5a3625fa0ffe7e66ce90011f72794d to your computer and use it in GitHub Desktop.
Save the-eater/6d5a3625fa0ffe7e66ce90011f72794d to your computer and use it in GitHub Desktop.
`IS_CRHOME` WebRTC hacks
async renegotiate(msg) {
if (IS_CHROME) {
// While Firefox gladly reuses open media channels. Chrome doesn't want to.
// So server counts amount of tracks are added,
// and client creates that amount of new transceivers
for (let i = 0; i < msg.newTracks; i++) {
this.peer.addTransceiver('audio', {
direction: "recvonly"
});
}
}
let size = msg.size + 1;
if (this.peer.getTransceivers().length < size) {
for (let i = 0; i < (size - this.peer.getTransceivers().length); i++) {
this.peer.addTransceiver('audio', {
direction: "recvonly"
});
}
}
await this.sendOffer();
}
async handleNewTrack(e) {
if (IS_CHROME) {
// If the stream is not added to an <audio> element
// Chrome will not write any audio to the AudioContext.
// So add the stream to a muted (and paused?) <audio> element
const audio = new Audio();
audio.muted = true;
audio.srcObject = e.streams[0];
this.audioElements.set(e.track.id, audio);
}
let node = new MediaStreamAudioSourceNode(this.audioContext, {mediaStream: e.streams[0]});
node.connect(this.audioContext.destination);
this.audioStreams.set(e.track.id, node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment