bigbluebutton-Github/bigbluebutton-html5/imports/utils/ios-webview-audio-polyfills.js

101 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-12-22 01:14:05 +08:00
const iosWebviewAudioPolyfills = function () {
function shimRemoteStreamsAPI(window) {
if (!('getRemoteStreams' in window.RTCPeerConnection.prototype)) {
window.RTCPeerConnection.prototype.getRemoteStreams = function () {
return this._remoteStreams ? this._remoteStreams : [];
};
}
2018-12-22 01:14:05 +08:00
if (!('onaddstream' in window.RTCPeerConnection.prototype)) {
Object.defineProperty(window.RTCPeerConnection.prototype, 'onaddstream', {
get: function get() {
return this._onaddstream;
},
set: function set(f) {
const _this3 = this;
if (this._onaddstream) {
this.removeEventListener('addstream', this._onaddstream);
this.removeEventListener('track', this._onaddstreampoly);
}
this.addEventListener('addstream', this._onaddstream = f);
this.addEventListener('track', this._onaddstreampoly = function (e) {
e.streams.forEach((stream) => {
if (!_this3._remoteStreams) {
_this3._remoteStreams = [];
}
if (_this3._remoteStreams.includes(stream)) {
return;
}
_this3._remoteStreams.push(stream);
const event = new Event('addstream');
event.stream = stream;
_this3.dispatchEvent(event);
});
});
},
});
const origSetRemoteDescription = window.RTCPeerConnection.prototype.setRemoteDescription;
window.RTCPeerConnection.prototype.setRemoteDescription = function () {
const pc = this;
if (!this._onaddstreampoly) {
this.addEventListener('track', this._onaddstreampoly = function (e) {
e.streams.forEach((stream) => {
if (!pc._remoteStreams) {
pc._remoteStreams = [];
}
if (pc._remoteStreams.indexOf(stream) >= 0) {
return;
}
pc._remoteStreams.push(stream);
const event = new Event('addstream');
event.stream = stream;
pc.dispatchEvent(event);
});
});
}
return origSetRemoteDescription.apply(pc, arguments);
};
}
}
function shimCallbacksAPI(window) {
const prototype = window.RTCPeerConnection.prototype;
const createOffer = prototype.createOffer;
const setLocalDescription = prototype.setLocalDescription;
const setRemoteDescription = prototype.setRemoteDescription;
prototype.createOffer = function (successCallback, failureCallback) {
const options = arguments.length >= 2 ? arguments[2] : arguments[0];
const promise = createOffer.apply(this, [options]);
if (!failureCallback) {
return promise;
}
promise.then(successCallback, failureCallback);
return Promise.resolve();
};
prototype.setLocalDescription = function withCallback(description, successCallback, failureCallback) {
const promise = setLocalDescription.apply(this, [description]);
if (!failureCallback) {
return promise;
}
promise.then(successCallback, failureCallback);
return Promise.resolve();
};
prototype.setRemoteDescription = function withCallback(description, successCallback, failureCallback) {
const promise = setRemoteDescription.apply(this, [description]);
if (!failureCallback) {
return promise;
}
promise.then(successCallback, failureCallback);
return Promise.resolve();
};
}
shimCallbacksAPI(window);
shimRemoteStreamsAPI(window);
};
export default iosWebviewAudioPolyfills;