Skip to content

Instantly share code, notes, and snippets.

@MHolmes91
Created February 14, 2019 18:02
Show Gist options
  • Save MHolmes91/7fa3dcee37182d4fc84a57eed61cfbb8 to your computer and use it in GitHub Desktop.
Save MHolmes91/7fa3dcee37182d4fc84a57eed61cfbb8 to your computer and use it in GitHub Desktop.
//Use for SimplePeers wrtc property.
//Optionally, register your onStream handler here if needed. It won't return the stream, but it is when the audio/video starts playing.
//Needed for https://github.com/Temasys/AdapterJS/issues/59
(onStream) => ({
RTCPeerConnection: (() => {
//From https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection
const underlyingProperties = ["addIceCandidate", "addStream", "addTrack", "addTransceiver", "close", "createAnswer", "createDTMFSender", "createDataChannel", "createOffer", "currentLocalDescription", "currentRemoteDescription", "getConfiguration", "getLocalStreams", "getReceivers", "getRemoteStreams", "getSenders", "getStats", "getTransceivers", "iceConnectionState", "iceGatheringState", "localDescription", "onaddstream", "ondatachannel", "onicecandidate", "oniceconnectionstatechange", "onicegatheringstatechange", "onnegotiationneeded", "onremovestream", "onsignalingstatechange", "ontrack", "pendingLocalDescription", "pendingRemoteDescription", "remoteDescription", "removeStream", "removeTrack", "setConfiguration", "setLocalDescription", "setRemoteDescription", "signalingState"]
return function(args){
const _this = this;
this.underlying = new window.RTCPeerConnection(...args);
underlyingProperties.forEach(prop => Object.defineProperty(this, prop, {
set: val => {
//Shiv for Temasys not running the onstream event and starting audio when the connection is made
switch(prop){
case 'oniceconnectionstatechange':
case 'onicegatheringstatechange':
val = (newFn => event => {
if(_this.underlying.iceConnectionState === 'connected' && onStream){
onStream();
}
return newFn(event);
})(val);
}
_this.underlying[prop] = val;
},
get: () => {
const _this = this;
switch(prop){
case 'createOffer':
return constraints => new Promise((resolve, reject) => {
_this.underlying.createOffer.call(this, resolve, reject, constraints);
});
case 'createAnswer':
return constraints => new Promise((resolve, reject) => {
_this.underlying.createAnswer.call(this, resolve, reject, constraints);
});
case 'setLocalDescription':
return description => new Promise((resolve, reject) => {
_this.underlying.setLocalDescription.call(this, description, resolve, reject);
});
case 'setRemoteDescription':
return description => new Promise((resolve, reject) => {
_this.underlying.setRemoteDescription.call(this, description, resolve, reject);
});
case 'ontrack':
//This doesn't get called by Temasys, somehow the audio just magically plays
writeDebug('get ontrack');
}
return this.underlying[prop];
}
}));
};
})(),
RTCSessionDescription: window.RTCSessionDescription,
RTCIceCandidate: window.RTCIceCandidate
})
@MHolmes91
Copy link
Author

This was written to shim Temasys RTC support in IE11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment