sabha-meetings-react 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +29 -3
- package/dist/index.d.ts +29 -3
- package/dist/index.js +256 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +256 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -679,6 +679,11 @@ var DEFAULT_EVENTS = {
|
|
|
679
679
|
error: "meeting:error",
|
|
680
680
|
mediaState: "meeting:media-state"
|
|
681
681
|
};
|
|
682
|
+
var SIGNAL_EVENT = "meeting:signal";
|
|
683
|
+
var ICE_SERVERS = [
|
|
684
|
+
{ urls: "stun:stun.l.google.com:19302" },
|
|
685
|
+
{ urls: "stun:stun1.l.google.com:19302" }
|
|
686
|
+
];
|
|
682
687
|
var MeetingConnection = class {
|
|
683
688
|
constructor(options) {
|
|
684
689
|
this.options = options;
|
|
@@ -687,20 +692,37 @@ var MeetingConnection = class {
|
|
|
687
692
|
this.listeners = /* @__PURE__ */ new Map();
|
|
688
693
|
this.mediaUnsub = null;
|
|
689
694
|
this.captureUnsub = null;
|
|
695
|
+
this.peers = /* @__PURE__ */ new Map();
|
|
696
|
+
this.remoteStream = null;
|
|
690
697
|
this.events = { ...DEFAULT_EVENTS, ...options.events };
|
|
691
|
-
this.
|
|
698
|
+
this.role = options.role ?? "host";
|
|
699
|
+
this.selfId = options.selfId || `anon-${Math.random().toString(36).slice(2, 10)}`;
|
|
700
|
+
this.mediaPolicy = this.role === "participant" ? {
|
|
701
|
+
...DEFAULT_MEDIA_POLICY,
|
|
702
|
+
...options.mediaPolicy,
|
|
703
|
+
audio: false,
|
|
704
|
+
video: false,
|
|
705
|
+
allowScreenShare: false,
|
|
706
|
+
screenOff: true
|
|
707
|
+
} : { ...DEFAULT_MEDIA_POLICY, ...options.mediaPolicy };
|
|
692
708
|
this.media = new MediaController(this.mediaPolicy);
|
|
693
709
|
this.protection = new ContentProtection(this.mediaPolicy);
|
|
694
710
|
}
|
|
695
711
|
get meetingId() {
|
|
696
712
|
return this.options.meetingId;
|
|
697
713
|
}
|
|
714
|
+
get meetingRole() {
|
|
715
|
+
return this.role;
|
|
716
|
+
}
|
|
698
717
|
get connectionState() {
|
|
699
718
|
return this.state;
|
|
700
719
|
}
|
|
701
720
|
getMediaState() {
|
|
702
721
|
return this.media.getMediaState();
|
|
703
722
|
}
|
|
723
|
+
getRemoteStream() {
|
|
724
|
+
return this.remoteStream;
|
|
725
|
+
}
|
|
704
726
|
on(event, listener) {
|
|
705
727
|
const set = this.listeners.get(event) ?? /* @__PURE__ */ new Set();
|
|
706
728
|
set.add(listener);
|
|
@@ -732,7 +754,9 @@ var MeetingConnection = class {
|
|
|
732
754
|
withCredentials: this.options.withCredentials !== false,
|
|
733
755
|
auth,
|
|
734
756
|
extraHeaders,
|
|
735
|
-
autoConnect: true
|
|
757
|
+
autoConnect: true,
|
|
758
|
+
// Leave must stay left — do not let socket.io reconnect the room.
|
|
759
|
+
reconnection: false
|
|
736
760
|
});
|
|
737
761
|
this.socket = socket;
|
|
738
762
|
const onConnect = () => {
|
|
@@ -747,11 +771,22 @@ var MeetingConnection = class {
|
|
|
747
771
|
meetingId: this.options.meetingId,
|
|
748
772
|
...mediaState
|
|
749
773
|
});
|
|
774
|
+
if (this.role === "host") {
|
|
775
|
+
void this.refreshHostTracks();
|
|
776
|
+
}
|
|
750
777
|
});
|
|
751
778
|
this.captureUnsub = this.protection.onCaptureAttempt((event) => {
|
|
752
779
|
this.emit("captureAttempt", event);
|
|
753
780
|
});
|
|
754
|
-
|
|
781
|
+
if (this.role === "host") {
|
|
782
|
+
await this.media.start(this.mediaPolicy);
|
|
783
|
+
} else {
|
|
784
|
+
this.sendSignal({
|
|
785
|
+
type: "request-offer",
|
|
786
|
+
from: this.selfId,
|
|
787
|
+
role: "participant"
|
|
788
|
+
});
|
|
789
|
+
}
|
|
755
790
|
resolve();
|
|
756
791
|
}).catch(reject);
|
|
757
792
|
};
|
|
@@ -775,24 +810,56 @@ var MeetingConnection = class {
|
|
|
775
810
|
});
|
|
776
811
|
}
|
|
777
812
|
async setAudioEnabled(enabled) {
|
|
813
|
+
if (this.role !== "host") {
|
|
814
|
+
throw new SabhaError("Only the host can control the microphone", {
|
|
815
|
+
code: "HOST_ONLY"
|
|
816
|
+
});
|
|
817
|
+
}
|
|
778
818
|
return this.media.setAudioEnabled(enabled);
|
|
779
819
|
}
|
|
780
820
|
async setVideoEnabled(enabled) {
|
|
821
|
+
if (this.role !== "host") {
|
|
822
|
+
throw new SabhaError("Only the host can control the camera", {
|
|
823
|
+
code: "HOST_ONLY"
|
|
824
|
+
});
|
|
825
|
+
}
|
|
781
826
|
return this.media.setVideoEnabled(enabled);
|
|
782
827
|
}
|
|
783
828
|
async setScreenOff(off) {
|
|
829
|
+
if (this.role !== "host") {
|
|
830
|
+
throw new SabhaError("Only the host can change screen settings", {
|
|
831
|
+
code: "HOST_ONLY"
|
|
832
|
+
});
|
|
833
|
+
}
|
|
784
834
|
return this.media.setScreenOff(off);
|
|
785
835
|
}
|
|
786
836
|
async startScreenShare() {
|
|
837
|
+
if (this.role !== "host") {
|
|
838
|
+
throw new SabhaError("Only the host can share screen", {
|
|
839
|
+
code: "HOST_ONLY"
|
|
840
|
+
});
|
|
841
|
+
}
|
|
787
842
|
return this.media.startScreenShare();
|
|
788
843
|
}
|
|
789
844
|
async stopScreenShare() {
|
|
845
|
+
if (this.role !== "host") {
|
|
846
|
+
throw new SabhaError("Only the host can stop screen share", {
|
|
847
|
+
code: "HOST_ONLY"
|
|
848
|
+
});
|
|
849
|
+
}
|
|
790
850
|
return this.media.stopScreenShare();
|
|
791
851
|
}
|
|
792
852
|
async leave() {
|
|
793
853
|
const socket = this.socket;
|
|
794
854
|
this.teardownMedia();
|
|
795
|
-
if (!socket)
|
|
855
|
+
if (!socket) {
|
|
856
|
+
this.setState("disconnected");
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
try {
|
|
860
|
+
socket.io.reconnection(false);
|
|
861
|
+
} catch {
|
|
862
|
+
}
|
|
796
863
|
await new Promise((resolve) => {
|
|
797
864
|
socket.timeout(5e3).emit(
|
|
798
865
|
this.events.leave,
|
|
@@ -801,13 +868,22 @@ var MeetingConnection = class {
|
|
|
801
868
|
);
|
|
802
869
|
setTimeout(() => resolve(), 500);
|
|
803
870
|
});
|
|
871
|
+
socket.removeAllListeners();
|
|
804
872
|
socket.disconnect();
|
|
805
873
|
this.socket = null;
|
|
806
874
|
this.setState("disconnected");
|
|
807
875
|
}
|
|
808
876
|
disconnect() {
|
|
809
877
|
this.teardownMedia();
|
|
810
|
-
this.socket
|
|
878
|
+
const socket = this.socket;
|
|
879
|
+
if (socket) {
|
|
880
|
+
try {
|
|
881
|
+
socket.io.reconnection(false);
|
|
882
|
+
} catch {
|
|
883
|
+
}
|
|
884
|
+
socket.removeAllListeners();
|
|
885
|
+
socket.disconnect();
|
|
886
|
+
}
|
|
811
887
|
this.socket = null;
|
|
812
888
|
this.setState("disconnected");
|
|
813
889
|
}
|
|
@@ -824,6 +900,10 @@ var MeetingConnection = class {
|
|
|
824
900
|
this.captureUnsub?.();
|
|
825
901
|
this.mediaUnsub = null;
|
|
826
902
|
this.captureUnsub = null;
|
|
903
|
+
for (const peer of this.peers.values()) peer.close();
|
|
904
|
+
this.peers.clear();
|
|
905
|
+
this.remoteStream = null;
|
|
906
|
+
this.emit("remoteStream", null);
|
|
827
907
|
this.media.stopAll();
|
|
828
908
|
this.protection.disable();
|
|
829
909
|
}
|
|
@@ -844,7 +924,11 @@ var MeetingConnection = class {
|
|
|
844
924
|
};
|
|
845
925
|
socket.timeout(5e3).emit(
|
|
846
926
|
this.events.join,
|
|
847
|
-
{
|
|
927
|
+
{
|
|
928
|
+
meetingId: this.options.meetingId,
|
|
929
|
+
role: this.role,
|
|
930
|
+
peerId: this.selfId
|
|
931
|
+
},
|
|
848
932
|
() => done()
|
|
849
933
|
);
|
|
850
934
|
setTimeout(done, 600);
|
|
@@ -854,10 +938,19 @@ var MeetingConnection = class {
|
|
|
854
938
|
socket.on(this.events.participantJoined, (payload) => {
|
|
855
939
|
this.emit("participantJoined", payload);
|
|
856
940
|
this.emit("raw", { event: this.events.participantJoined, payload });
|
|
941
|
+
if (this.role === "host") {
|
|
942
|
+
const peerId = payload.id;
|
|
943
|
+
if (peerId && peerId !== this.selfId) {
|
|
944
|
+
void this.createHostOffer(peerId).catch((error) => {
|
|
945
|
+
this.emit("error", toError(error, "Failed to create host offer"));
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
}
|
|
857
949
|
});
|
|
858
950
|
socket.on(this.events.participantLeft, (payload) => {
|
|
859
951
|
this.emit("participantLeft", payload);
|
|
860
952
|
this.emit("raw", { event: this.events.participantLeft, payload });
|
|
953
|
+
this.closePeer(payload.id);
|
|
861
954
|
});
|
|
862
955
|
socket.on(
|
|
863
956
|
this.events.meetingEnded,
|
|
@@ -879,10 +972,135 @@ var MeetingConnection = class {
|
|
|
879
972
|
this.emit("error", error);
|
|
880
973
|
this.emit("raw", { event: this.events.error, payload });
|
|
881
974
|
});
|
|
975
|
+
socket.on(SIGNAL_EVENT, (payload) => {
|
|
976
|
+
void this.handleSignal(payload).catch((error) => {
|
|
977
|
+
this.emit("error", toError(error, "WebRTC signal failed"));
|
|
978
|
+
});
|
|
979
|
+
});
|
|
882
980
|
socket.on("disconnect", () => {
|
|
883
981
|
this.setState("disconnected");
|
|
884
982
|
});
|
|
885
983
|
}
|
|
984
|
+
sendSignal(partial) {
|
|
985
|
+
if (!this.socket?.connected) return;
|
|
986
|
+
this.socket.emit(SIGNAL_EVENT, {
|
|
987
|
+
meetingId: this.options.meetingId,
|
|
988
|
+
...partial
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
async handleSignal(payload) {
|
|
992
|
+
if (!payload || payload.meetingId !== this.options.meetingId) return;
|
|
993
|
+
if (payload.from === this.selfId) return;
|
|
994
|
+
if (payload.to && payload.to !== this.selfId) return;
|
|
995
|
+
if (payload.type === "request-offer" && this.role === "host") {
|
|
996
|
+
await this.createHostOffer(payload.from);
|
|
997
|
+
return;
|
|
998
|
+
}
|
|
999
|
+
if (payload.type === "offer" && this.role === "participant") {
|
|
1000
|
+
const peer = this.getOrCreatePeer(payload.from, false);
|
|
1001
|
+
await peer.setRemoteDescription(payload.sdp);
|
|
1002
|
+
const answer = await peer.createAnswer();
|
|
1003
|
+
await peer.setLocalDescription(answer);
|
|
1004
|
+
this.sendSignal({
|
|
1005
|
+
type: "answer",
|
|
1006
|
+
from: this.selfId,
|
|
1007
|
+
to: payload.from,
|
|
1008
|
+
sdp: answer,
|
|
1009
|
+
role: "participant"
|
|
1010
|
+
});
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
1013
|
+
if (payload.type === "answer" && this.role === "host") {
|
|
1014
|
+
const peer = this.peers.get(payload.from);
|
|
1015
|
+
if (!peer) return;
|
|
1016
|
+
await peer.setRemoteDescription(payload.sdp);
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
if (payload.type === "ice") {
|
|
1020
|
+
const peer = this.peers.get(payload.from);
|
|
1021
|
+
if (!peer || !payload.candidate) return;
|
|
1022
|
+
await peer.addIceCandidate(payload.candidate);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
async createHostOffer(peerId) {
|
|
1026
|
+
const peer = this.getOrCreatePeer(peerId, true);
|
|
1027
|
+
await this.attachLocalTracks(peer);
|
|
1028
|
+
const offer = await peer.createOffer({
|
|
1029
|
+
offerToReceiveAudio: false,
|
|
1030
|
+
offerToReceiveVideo: false
|
|
1031
|
+
});
|
|
1032
|
+
await peer.setLocalDescription(offer);
|
|
1033
|
+
this.sendSignal({
|
|
1034
|
+
type: "offer",
|
|
1035
|
+
from: this.selfId,
|
|
1036
|
+
to: peerId,
|
|
1037
|
+
sdp: offer,
|
|
1038
|
+
role: "host"
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
getOrCreatePeer(peerId, isHost) {
|
|
1042
|
+
const existing = this.peers.get(peerId);
|
|
1043
|
+
if (existing) return existing;
|
|
1044
|
+
const peer = new RTCPeerConnection({ iceServers: ICE_SERVERS });
|
|
1045
|
+
this.peers.set(peerId, peer);
|
|
1046
|
+
peer.onicecandidate = (event) => {
|
|
1047
|
+
this.sendSignal({
|
|
1048
|
+
type: "ice",
|
|
1049
|
+
from: this.selfId,
|
|
1050
|
+
to: peerId,
|
|
1051
|
+
candidate: event.candidate ? event.candidate.toJSON() : null,
|
|
1052
|
+
role: this.role
|
|
1053
|
+
});
|
|
1054
|
+
};
|
|
1055
|
+
peer.onconnectionstatechange = () => {
|
|
1056
|
+
if (peer.connectionState === "failed" || peer.connectionState === "closed" || peer.connectionState === "disconnected") ;
|
|
1057
|
+
};
|
|
1058
|
+
if (!isHost) {
|
|
1059
|
+
peer.ontrack = (event) => {
|
|
1060
|
+
const stream = event.streams[0] ?? new MediaStream([event.track]);
|
|
1061
|
+
this.remoteStream = stream;
|
|
1062
|
+
this.emit("remoteStream", stream);
|
|
1063
|
+
};
|
|
1064
|
+
}
|
|
1065
|
+
return peer;
|
|
1066
|
+
}
|
|
1067
|
+
async attachLocalTracks(peer) {
|
|
1068
|
+
const local = new MediaStream();
|
|
1069
|
+
const cam = this.media.getCameraStream();
|
|
1070
|
+
const mic = this.media.getMicrophoneStream();
|
|
1071
|
+
const screen = this.media.getScreenStream();
|
|
1072
|
+
mic?.getAudioTracks().forEach((track) => local.addTrack(track));
|
|
1073
|
+
const videoSource = screen ?? cam;
|
|
1074
|
+
videoSource?.getVideoTracks().forEach((track) => local.addTrack(track));
|
|
1075
|
+
const senders = peer.getSenders();
|
|
1076
|
+
for (const track of local.getTracks()) {
|
|
1077
|
+
const already = senders.some((s) => s.track?.id === track.id);
|
|
1078
|
+
if (!already) peer.addTrack(track, local);
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
async refreshHostTracks() {
|
|
1082
|
+
for (const [peerId, peer] of this.peers) {
|
|
1083
|
+
try {
|
|
1084
|
+
await this.attachLocalTracks(peer);
|
|
1085
|
+
const offer = await peer.createOffer();
|
|
1086
|
+
await peer.setLocalDescription(offer);
|
|
1087
|
+
this.sendSignal({
|
|
1088
|
+
type: "offer",
|
|
1089
|
+
from: this.selfId,
|
|
1090
|
+
to: peerId,
|
|
1091
|
+
sdp: offer,
|
|
1092
|
+
role: "host"
|
|
1093
|
+
});
|
|
1094
|
+
} catch {
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
closePeer(peerId) {
|
|
1099
|
+
const peer = this.peers.get(peerId);
|
|
1100
|
+
if (!peer) return;
|
|
1101
|
+
peer.close();
|
|
1102
|
+
this.peers.delete(peerId);
|
|
1103
|
+
}
|
|
886
1104
|
setState(state) {
|
|
887
1105
|
this.state = state;
|
|
888
1106
|
this.emit("state", state);
|
|
@@ -893,6 +1111,10 @@ var MeetingConnection = class {
|
|
|
893
1111
|
for (const listener of set) listener(payload);
|
|
894
1112
|
}
|
|
895
1113
|
};
|
|
1114
|
+
function toError(error, fallback) {
|
|
1115
|
+
if (error instanceof Error) return error;
|
|
1116
|
+
return new Error(fallback);
|
|
1117
|
+
}
|
|
896
1118
|
|
|
897
1119
|
// src/SabhaClient.ts
|
|
898
1120
|
var SabhaClient = class {
|
|
@@ -1108,15 +1330,16 @@ var SabhaClient = class {
|
|
|
1108
1330
|
groupId: input?.groupId
|
|
1109
1331
|
});
|
|
1110
1332
|
}
|
|
1111
|
-
const connection = await this.connect(meeting.id);
|
|
1333
|
+
const connection = await this.connect(meeting.id, { role: "host" });
|
|
1112
1334
|
return { meeting, connection };
|
|
1113
1335
|
}
|
|
1114
|
-
async connect(meetingId) {
|
|
1336
|
+
async connect(meetingId, options) {
|
|
1115
1337
|
this.ensureConfigured();
|
|
1116
1338
|
if (this.activeConnection) {
|
|
1117
1339
|
this.activeConnection.disconnect();
|
|
1118
1340
|
this.activeConnection = null;
|
|
1119
1341
|
}
|
|
1342
|
+
const role = options?.role ?? "host";
|
|
1120
1343
|
const connection = new MeetingConnection({
|
|
1121
1344
|
baseUrl: this.config.baseUrl ?? DEFAULT_BASE_URL,
|
|
1122
1345
|
meetingId,
|
|
@@ -1125,7 +1348,10 @@ var SabhaClient = class {
|
|
|
1125
1348
|
withCredentials: this.config.withCredentials !== false,
|
|
1126
1349
|
socketPath: this.config.socketPath,
|
|
1127
1350
|
events: this.config.socketEvents,
|
|
1128
|
-
mediaPolicy: this.mediaPolicy
|
|
1351
|
+
mediaPolicy: this.mediaPolicy,
|
|
1352
|
+
role,
|
|
1353
|
+
// Per-browser id so same Sabha account can host on one device and view on another.
|
|
1354
|
+
selfId: getDevicePeerId(this.user?.userId)
|
|
1129
1355
|
});
|
|
1130
1356
|
try {
|
|
1131
1357
|
await connection.connect();
|
|
@@ -1220,6 +1446,18 @@ function createSabhaClient(config) {
|
|
|
1220
1446
|
client.configure(config ?? { baseUrl: DEFAULT_BASE_URL });
|
|
1221
1447
|
return client;
|
|
1222
1448
|
}
|
|
1449
|
+
function getDevicePeerId(userId) {
|
|
1450
|
+
const key = "sabha_device_peer_id";
|
|
1451
|
+
try {
|
|
1452
|
+
const existing = typeof sessionStorage !== "undefined" ? sessionStorage.getItem(key) : null;
|
|
1453
|
+
if (existing) return existing;
|
|
1454
|
+
const next = `${userId || "user"}-${Math.random().toString(36).slice(2, 10)}`;
|
|
1455
|
+
if (typeof sessionStorage !== "undefined") sessionStorage.setItem(key, next);
|
|
1456
|
+
return next;
|
|
1457
|
+
} catch {
|
|
1458
|
+
return `${userId || "user"}-${Math.random().toString(36).slice(2, 10)}`;
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1223
1461
|
var SabhaContext = createContext(null);
|
|
1224
1462
|
var idleMedia = {
|
|
1225
1463
|
audioEnabled: false,
|
|
@@ -1282,7 +1520,13 @@ function SabhaProvider({
|
|
|
1282
1520
|
}
|
|
1283
1521
|
setConnectionState(connection.connectionState);
|
|
1284
1522
|
setMediaState(connection.getMediaState());
|
|
1285
|
-
const offState = connection.on("state",
|
|
1523
|
+
const offState = connection.on("state", (state) => {
|
|
1524
|
+
setConnectionState(state);
|
|
1525
|
+
if (state === "disconnected" || state === "error") {
|
|
1526
|
+
setConnection(null);
|
|
1527
|
+
setMediaState(idleMedia);
|
|
1528
|
+
}
|
|
1529
|
+
});
|
|
1286
1530
|
const offMedia = connection.on("mediaState", setMediaState);
|
|
1287
1531
|
return () => {
|
|
1288
1532
|
offState();
|
|
@@ -1376,10 +1620,10 @@ function SabhaProvider({
|
|
|
1376
1620
|
[client, connection]
|
|
1377
1621
|
);
|
|
1378
1622
|
const connect = useCallback(
|
|
1379
|
-
async (meetingId) => {
|
|
1623
|
+
async (meetingId, options) => {
|
|
1380
1624
|
setError(null);
|
|
1381
1625
|
try {
|
|
1382
|
-
const next = await client.connect(meetingId);
|
|
1626
|
+
const next = await client.connect(meetingId, options);
|
|
1383
1627
|
setConnection(next);
|
|
1384
1628
|
return next;
|
|
1385
1629
|
} catch (err) {
|