RTCPeerConnection.js [plain text]
function initializeRTCPeerConnection(configuration)
{
"use strict";
if (arguments.length < 1)
throw new @TypeError("Not enough arguments");
if (!@isObject(configuration))
throw new @TypeError("RTCPeerConnection argument must be a valid Dictionary");
try {
this.@initializeWith(configuration);
} catch (e) {
const message = e.name === "TypeMismatchError" ? "Invalid RTCPeerConnection constructor arguments"
: "Error creating RTCPeerConnection";
throw new @TypeError(message);
}
this.@operations = [];
this.@localStreams = [];
return this;
}
function getLocalStreams()
{
"use strict";
if (!@isRTCPeerConnection(this))
throw new @TypeError("Function should be called on an RTCPeerConnection");
return this.@localStreams.slice();
}
function getStreamById(streamIdArg)
{
"use strict";
if (!@isRTCPeerConnection(this))
throw new @TypeError("Function should be called on an RTCPeerConnection");
if (arguments.length < 1)
throw new @TypeError("Not enough arguments");
const streamId = @String(streamIdArg);
return this.@localStreams.find(stream => stream.id === streamId)
|| this.@getRemoteStreams().find(stream => stream.id === streamId)
|| null;
}
function addStream(stream)
{
"use strict";
if (!@isRTCPeerConnection(this))
throw new @TypeError("Function should be called on an RTCPeerConnection");
if (arguments.length < 1)
throw new @TypeError("Not enough arguments");
if (!(stream instanceof @MediaStream))
throw new @TypeError("Argument 1 ('stream') to RTCPeerConnection.addStream must be an instance of MediaStream");
if (this.@localStreams.find(localStream => localStream.id === stream.id))
return;
this.@localStreams.@push(stream);
stream.@getTracks().forEach(track => this.@addTrack(track, stream));
}
function removeStream(stream)
{
"use strict";
if (!@isRTCPeerConnection(this))
throw new @TypeError("Function should be called on an RTCPeerConnection");
if (arguments.length < 1)
throw new @TypeError("Not enough arguments");
if (!(stream instanceof @MediaStream))
throw new @TypeError("Argument 1 ('stream') to RTCPeerConnection.removeStream must be an instance of MediaStream");
const indexOfStreamToRemove = this.@localStreams.findIndex(localStream => localStream.id === stream.id);
if (indexOfStreamToRemove === -1)
return;
const senders = this.@getSenders();
this.@localStreams[indexOfStreamToRemove].@getTracks().forEach(track => {
const senderForTrack = senders.find(sender => sender.track && sender.track.id === track.id);
if (senderForTrack)
this.@removeTrack(senderForTrack);
});
this.@localStreams.splice(indexOfStreamToRemove, 1);
}
function createOffer()
{
"use strict";
if (!@isRTCPeerConnection(this))
return @Promise.@reject(new @TypeError("Function should be called on an RTCPeerConnection"));
const peerConnection = this;
return @callbacksAndDictionaryOverload(arguments, "createOffer", function (options) {
return @enqueueOperation(peerConnection, function () {
return peerConnection.@queuedCreateOffer(options);
});
}, function (successCallback, errorCallback, options) {
@enqueueOperation(peerConnection, function () {
return peerConnection.@queuedCreateOffer(options).@then(successCallback, errorCallback);
});
return @Promise.@resolve(@undefined);
});
}
function createAnswer()
{
"use strict";
if (!@isRTCPeerConnection(this))
return @Promise.@reject(new @TypeError("Function should be called on an RTCPeerConnection"));
const peerConnection = this;
return @callbacksAndDictionaryOverload(arguments, "createAnswer", function (options) {
return @enqueueOperation(peerConnection, function () {
return peerConnection.@queuedCreateAnswer(options);
});
}, function (successCallback, errorCallback, options) {
@enqueueOperation(peerConnection, function () {
return peerConnection.@queuedCreateAnswer(options).@then(successCallback, errorCallback);
});
return @Promise.@resolve(@undefined);
});
}
function setLocalDescription()
{
"use strict";
if (!@isRTCPeerConnection(this))
return @Promise.@reject(new @TypeError("Function should be called on an RTCPeerConnection"));
const peerConnection = this;
const objectInfo = {
"constructor": @RTCSessionDescription,
"argName": "description",
"argType": "RTCSessionDescription"
};
return @objectAndCallbacksOverload(arguments, "setLocalDescription", objectInfo, function (description) {
return @enqueueOperation(peerConnection, function () {
return peerConnection.@queuedSetLocalDescription(description);
});
}, function (description, successCallback, errorCallback) {
@enqueueOperation(peerConnection, function () {
return peerConnection.@queuedSetLocalDescription(description).@then(successCallback, errorCallback);
});
return @Promise.@resolve(@undefined);
});
}
function setRemoteDescription()
{
"use strict";
if (!@isRTCPeerConnection(this))
return @Promise.@reject(new @TypeError("Function should be called on an RTCPeerConnection"));
const peerConnection = this;
const objectInfo = {
"constructor": @RTCSessionDescription,
"argName": "description",
"argType": "RTCSessionDescription"
};
return @objectAndCallbacksOverload(arguments, "setRemoteDescription", objectInfo, function (description) {
return @enqueueOperation(peerConnection, function () {
return peerConnection.@queuedSetRemoteDescription(description);
});
}, function (description, successCallback, errorCallback) {
@enqueueOperation(peerConnection, function () {
return peerConnection.@queuedSetRemoteDescription(description).@then(successCallback, errorCallback);
});
return @Promise.@resolve(@undefined);
});
}
function addIceCandidate()
{
"use strict";
if (!@isRTCPeerConnection(this))
return @Promise.@reject(new @TypeError("Function should be called on an RTCPeerConnection"));
const peerConnection = this;
const objectInfo = {
"constructor": @RTCIceCandidate,
"argName": "candidate",
"argType": "RTCIceCandidate"
};
return @objectAndCallbacksOverload(arguments, "addIceCandidate", objectInfo, function (candidate) {
return @enqueueOperation(peerConnection, function () {
return peerConnection.@queuedAddIceCandidate(candidate);
});
}, function (candidate, successCallback, errorCallback) {
@enqueueOperation(peerConnection, function () {
return peerConnection.@queuedAddIceCandidate(candidate).@then(successCallback, errorCallback);
});
return @Promise.@resolve(@undefined);
});
}
function getStats()
{
"use strict";
if (!@isRTCPeerConnection(this))
return @Promise.@reject(new @TypeError("Function should be called on an RTCPeerConnection"));
const peerConnection = this;
const objectInfo = {
"constructor": @MediaStreamTrack,
"argName": "selector",
"argType": "MediaStreamTrack",
"defaultsToNull": true
};
return @objectAndCallbacksOverload(arguments, "getStats", objectInfo, function (selector) {
return peerConnection.@privateGetStats(selector);
}, function (selector, successCallback, errorCallback) {
peerConnection.@privateGetStats(selector).@then(successCallback, errorCallback);
return @Promise.@resolve(@undefined);
});
}