@simplito/privmx-webendpoint
Version:
PrivMX Web Endpoint library
86 lines (83 loc) • 3.32 kB
JavaScript
;
/*!
PrivMX Endpoint.
Copyright © 2024 Simplito sp. z o.o.
This file is part of the PrivMX Platform (https://privmx.dev).
This software is Licensed under the PrivMX Free License.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebRtcInterfaceImpl = void 0;
class WebRtcInterfaceImpl {
webRtcClient;
constructor(webRtcClient) {
this.webRtcClient = webRtcClient;
}
methodsMap = {
createOfferAndSetLocalDescription: this.createOfferAndSetLocalDescription,
createAnswerAndSetDescriptions: this.createAnswerAndSetDescriptions,
setAnswerAndSetRemoteDescription: this.setAnswerAndSetRemoteDescription,
updateSessionId: this.updateSessionId,
close: this.close,
updateKeys: this.updateKeys,
};
isMainThread() {
return typeof window !== "undefined";
}
getClient() {
if (!this.webRtcClient) {
throw new Error("WebRtcClient not initialized. Aborting...");
}
return this.webRtcClient;
}
async methodCall(name, params) {
if (this.methodsMap[name]) {
const method = this.methodsMap[name];
if (typeof method === "function") {
return this.methodsMap[name].call(this, params);
}
}
throw new Error(`Method '${name}' is not implemented.`);
}
async createOfferAndSetLocalDescription(model) {
const peerConnection = this.getClient()
.getConnectionManager()
.getConnectionWithSession(model.roomId, "publisher").pc;
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
return offer.sdp; // sdp
}
async createAnswerAndSetDescriptions(model) {
const offer = { sdp: model.sdp, type: model.type };
await this.getClient().onSubscriptionUpdated(model.roomId, offer);
return this.webRtcClient.lastProcessedAnswer[model.roomId].sdp;
}
async setAnswerAndSetRemoteDescription(model) {
const janusSession = this.getClient()
.getConnectionManager()
.getConnectionWithSession(model.roomId, "publisher");
if (!("pc" in janusSession)) {
throw new Error("WebRtcInterfaceImpl: No peerConnection available on setAnswerAndSetRemoteDescription");
}
const peerConnection = janusSession.pc;
await peerConnection.setRemoteDescription(new RTCSessionDescription({ sdp: model.sdp, type: model.type }));
}
async close(roomId) {
this.getClient()
.getConnectionManager()
.closePeerConnectionBySessionIfExists(roomId, "subscriber");
this.getClient()
.getConnectionManager()
.closePeerConnectionBySessionIfExists(roomId, "publisher");
}
async updateKeys(model) {
return this.getClient().updateKeys(model.streamRoomId, model.keys);
}
async updateSessionId(streamRoomId, sessionId, connectionType) {
this.getClient()
.getConnectionManager()
.updateSessionForConnection(streamRoomId, connectionType, sessionId);
}
}
exports.WebRtcInterfaceImpl = WebRtcInterfaceImpl;