Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore Callbacks Support on registerGlobals getUserMedia|enumerateDevices RTCPeerConnection.prototype.createAnswer|createOffer|setRemoteDescription|setLocalDescription|addIceCandidate support for JsSIP, SIP.js and sipML5 #404

Merged
merged 2 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions dist/cordova-plugin-iosrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2513,8 +2513,57 @@ function refreshVideos() {
}
}

function callbackifyMethod(originalMethod) {
return function () {
var success, failure,
originalArgs = Array.prototype.slice.call(arguments);

var callbackArgs = [];
originalArgs.forEach(function (arg) {
if (typeof arg === 'function') {
if (!success) {
success = arg;
} else {
failure = arg;
}
} else {
callbackArgs.push(arg);
}
});

var promiseResult = originalMethod.apply(this, callbackArgs);

// Only apply then if callback success available
if (typeof success === 'function') {
promiseResult = promiseResult.then(success);
}

// Only apply catch if callback failure available
if (typeof failure === 'function') {
promiseResult = promiseResult.catch(failure);
}

function registerGlobals() {
return promiseResult;
};
}

function callbackifyPrototype(proto, method) {
var originalMethod = proto[method];
proto[method] = callbackifyMethod(originalMethod);
}

function restoreCallbacksSupport() {
debug('restoreCallbacksSupport()');
getUserMedia = callbackifyMethod(getUserMedia);
enumerateDevices = callbackifyMethod(enumerateDevices);
callbackifyPrototype(RTCPeerConnection.prototype, 'createAnswer');
callbackifyPrototype(RTCPeerConnection.prototype, 'createOffer');
callbackifyPrototype(RTCPeerConnection.prototype, 'setRemoteDescription');
callbackifyPrototype(RTCPeerConnection.prototype, 'setLocalDescription');
callbackifyPrototype(RTCPeerConnection.prototype, 'addIceCandidate');
}

function registerGlobals(doNotRestoreCallbacksSupport) {
debug('registerGlobals()');

if (!global.navigator) {
Expand All @@ -2529,6 +2578,12 @@ function registerGlobals() {
navigator.webkitGetUserMedia = getUserMedia;
navigator.mediaDevices.getUserMedia = getUserMedia;
navigator.mediaDevices.enumerateDevices = enumerateDevices;

// Restore Callback support
if (!doNotRestoreCallbacksSupport) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hthetiot should this be above navigator.webkitGetUserMedia = getUserMedia;?
I'm facing an issue where it still throws an error while using getUserMedia

restoreCallbacksSupport();
}

window.RTCPeerConnection = RTCPeerConnection;
window.webkitRTCPeerConnection = RTCPeerConnection;
window.RTCSessionDescription = RTCSessionDescription;
Expand All @@ -2538,7 +2593,6 @@ function registerGlobals() {
window.MediaStreamTrack = MediaStreamTrack;
}


function dump() {
exec(null, null, 'iosrtcPlugin', 'dump', []);
}
Expand Down
58 changes: 56 additions & 2 deletions js/iosrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,57 @@ function refreshVideos() {
}
}

function callbackifyMethod(originalMethod) {
return function () {
var success, failure,
originalArgs = Array.prototype.slice.call(arguments);

var callbackArgs = [];
originalArgs.forEach(function (arg) {
if (typeof arg === 'function') {
if (!success) {
success = arg;
} else {
failure = arg;
}
} else {
callbackArgs.push(arg);
}
});

var promiseResult = originalMethod.apply(this, callbackArgs);

// Only apply then if callback success available
if (typeof success === 'function') {
promiseResult = promiseResult.then(success);
}

// Only apply catch if callback failure available
if (typeof failure === 'function') {
promiseResult = promiseResult.catch(failure);
}

return promiseResult;
};
}

function callbackifyPrototype(proto, method) {
var originalMethod = proto[method];
proto[method] = callbackifyMethod(originalMethod);
}

function registerGlobals() {
function restoreCallbacksSupport() {
debug('restoreCallbacksSupport()');
getUserMedia = callbackifyMethod(getUserMedia);
enumerateDevices = callbackifyMethod(enumerateDevices);
callbackifyPrototype(RTCPeerConnection.prototype, 'createAnswer');
callbackifyPrototype(RTCPeerConnection.prototype, 'createOffer');
callbackifyPrototype(RTCPeerConnection.prototype, 'setRemoteDescription');
callbackifyPrototype(RTCPeerConnection.prototype, 'setLocalDescription');
callbackifyPrototype(RTCPeerConnection.prototype, 'addIceCandidate');
}

function registerGlobals(doNotRestoreCallbacksSupport) {
debug('registerGlobals()');

if (!global.navigator) {
Expand All @@ -97,6 +146,12 @@ function registerGlobals() {
navigator.webkitGetUserMedia = getUserMedia;
navigator.mediaDevices.getUserMedia = getUserMedia;
navigator.mediaDevices.enumerateDevices = enumerateDevices;

// Restore Callback support
if (!doNotRestoreCallbacksSupport) {
restoreCallbacksSupport();
}

window.RTCPeerConnection = RTCPeerConnection;
window.webkitRTCPeerConnection = RTCPeerConnection;
window.RTCSessionDescription = RTCSessionDescription;
Expand All @@ -106,7 +161,6 @@ function registerGlobals() {
window.MediaStreamTrack = MediaStreamTrack;
}


function dump() {
exec(null, null, 'iosrtcPlugin', 'dump', []);
}