@ermis-network/ermis-chat-sdk 1.0.2 → 1.0.3

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.
@@ -3607,7 +3607,7 @@ var ErmisChat = class _ErmisChat {
3607
3607
  return pinExpires;
3608
3608
  }
3609
3609
  getUserAgent() {
3610
- return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.2"}`;
3610
+ return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.3"}`;
3611
3611
  }
3612
3612
  setUserAgent(userAgent) {
3613
3613
  this.userAgent = userAgent;
@@ -6316,21 +6316,38 @@ var ErmisCallNode = class {
6316
6316
  const mediaConstraints = await this.getMediaConstraints();
6317
6317
  try {
6318
6318
  const stream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
6319
- if (this.callStatus === "ended" /* ENDED */) {
6320
- stream.getTracks().forEach((track) => track.stop());
6321
- this.destroy();
6322
- return;
6319
+ return this.applyLocalStream(stream);
6320
+ } catch (error) {
6321
+ console.warn("Error getting user media:", error?.message);
6322
+ if (this.callType === "video" && mediaConstraints.video) {
6323
+ try {
6324
+ const audioOnlyStream = await navigator.mediaDevices.getUserMedia({
6325
+ audio: mediaConstraints.audio,
6326
+ video: false
6327
+ });
6328
+ this.setConnectionMessage("Camera not available, using audio only");
6329
+ return this.applyLocalStream(audioOnlyStream);
6330
+ } catch {
6331
+ }
6323
6332
  }
6324
- if (this.onLocalStream) {
6325
- this.onLocalStream(stream);
6333
+ if (typeof this.onError === "function") {
6334
+ this.onError("No microphone or camera found. Please check your device.");
6326
6335
  }
6327
- this.localStream = stream;
6328
- return stream;
6329
- } catch (error) {
6330
- console.error("Error getting user media:", error);
6331
6336
  return null;
6332
6337
  }
6333
6338
  }
6339
+ applyLocalStream(stream) {
6340
+ if (this.callStatus === "ended" /* ENDED */) {
6341
+ stream.getTracks().forEach((track) => track.stop());
6342
+ this.destroy();
6343
+ return;
6344
+ }
6345
+ if (this.onLocalStream) {
6346
+ this.onLocalStream(stream);
6347
+ }
6348
+ this.localStream = stream;
6349
+ return stream;
6350
+ }
6334
6351
  setConnectionMessage(message) {
6335
6352
  if (typeof this.onConnectionMessageChange === "function") {
6336
6353
  this.onConnectionMessageChange(message);
@@ -6375,20 +6392,10 @@ var ErmisCallNode = class {
6375
6392
  this.isDestroyed = false;
6376
6393
  this.callStatus = "";
6377
6394
  this.callType = is_video ? "video" : "audio";
6378
- await this.startLocalStream();
6379
- if (this.callStatus === "ended" /* ENDED */) return;
6380
6395
  this.setUserInfo(cid, eventUserId);
6381
6396
  this.setCallStatus("ringing" /* RINGING */);
6382
6397
  this.cid = cid || "";
6383
6398
  this.metadata = metadata || {};
6384
- console.log("----metadata---", metadata);
6385
- if (eventUserId !== this.userID) {
6386
- await this.initialize();
6387
- }
6388
- if (this.localStream && this.mediaSender && this.mediaReceiver) {
6389
- this.mediaSender?.initEncoders(this.localStream);
6390
- this.mediaReceiver?.initDecoders(this.callType);
6391
- }
6392
6399
  if (typeof this.onCallEvent === "function") {
6393
6400
  this.onCallEvent({
6394
6401
  type: eventUserId !== this.userID ? "incoming" : "outgoing",
@@ -6399,6 +6406,15 @@ var ErmisCallNode = class {
6399
6406
  metadata: this.metadata
6400
6407
  });
6401
6408
  }
6409
+ await this.startLocalStream();
6410
+ if (this.callStatus === "ended" /* ENDED */) return;
6411
+ if (eventUserId !== this.userID) {
6412
+ await this.initialize();
6413
+ }
6414
+ if (this.localStream && this.mediaSender && this.mediaReceiver) {
6415
+ this.mediaSender?.initEncoders(this.localStream);
6416
+ this.mediaReceiver?.initDecoders(this.callType);
6417
+ }
6402
6418
  if (eventUserId === this.userID) {
6403
6419
  if (this.missCallTimeout) clearTimeout(this.missCallTimeout);
6404
6420
  this.missCallTimeout = setTimeout(async () => {
@@ -6659,21 +6675,30 @@ var ErmisCallNode = class {
6659
6675
  }
6660
6676
  async stopScreenShare() {
6661
6677
  const mediaConstraints = await this.getMediaConstraints();
6662
- const cameraStream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
6663
- const cameraTrack = cameraStream.getVideoTracks()[0];
6664
- if (this.localStream) {
6665
- this.localStream.getVideoTracks().forEach((track) => track.stop());
6666
- this.localStream.removeTrack(this.localStream.getVideoTracks()[0]);
6667
- this.localStream.addTrack(cameraTrack);
6668
- } else {
6669
- this.localStream = cameraStream;
6670
- }
6671
- if (this.onLocalStream) {
6672
- this.onLocalStream(this.localStream);
6673
- this.mediaSender?.replaceVideoTrack(this.localStream.getVideoTracks()[0]);
6674
- }
6675
- if (typeof this.onScreenShareChange === "function") {
6676
- this.onScreenShareChange(false);
6678
+ try {
6679
+ const cameraStream = await navigator.mediaDevices.getUserMedia({
6680
+ video: mediaConstraints.video,
6681
+ audio: false
6682
+ });
6683
+ const cameraTrack = cameraStream.getVideoTracks()[0];
6684
+ if (this.localStream) {
6685
+ this.localStream.getVideoTracks().forEach((track) => {
6686
+ track.stop();
6687
+ this.localStream?.removeTrack(track);
6688
+ });
6689
+ this.localStream.addTrack(cameraTrack);
6690
+ } else {
6691
+ this.localStream = cameraStream;
6692
+ }
6693
+ if (this.onLocalStream) {
6694
+ this.onLocalStream(this.localStream);
6695
+ this.mediaSender?.replaceVideoTrack(this.localStream.getVideoTracks()[0]);
6696
+ }
6697
+ if (typeof this.onScreenShareChange === "function") {
6698
+ this.onScreenShareChange(false);
6699
+ }
6700
+ } catch (error) {
6701
+ console.error("Error stopping screen share and reverting to camera:", error);
6677
6702
  }
6678
6703
  }
6679
6704
  async toggleMic(enabled) {
@@ -6940,7 +6965,7 @@ var ErmisAuthProvider = class {
6940
6965
  return data;
6941
6966
  }
6942
6967
  getUserAgent() {
6943
- return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.2"}`;
6968
+ return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.3"}`;
6944
6969
  }
6945
6970
  setUserAgent(userAgent) {
6946
6971
  this.userAgent = userAgent;
@@ -7173,70 +7198,116 @@ function parseSystemMessage(value, userMap) {
7173
7198
  }
7174
7199
 
7175
7200
  // src/signal_message.ts
7176
- function formatDuration(durationSec) {
7177
- const sec = parseInt(durationSec, 10);
7178
- if (isNaN(sec) || sec < 0) return durationSec;
7179
- const minutes = Math.floor(sec / 60);
7180
- const seconds = sec % 60;
7181
- return `${minutes}:${seconds.toString().padStart(2, "0")}`;
7182
- }
7183
- function resolveUser2(userId, userMap) {
7184
- return userMap[userId] ?? userId;
7201
+ var CallType = {
7202
+ AUDIO: "audio",
7203
+ VIDEO: "video"
7204
+ };
7205
+ function formatDuration(durationMs) {
7206
+ if (!durationMs) return "";
7207
+ const ms = parseInt(durationMs, 10);
7208
+ if (isNaN(ms) || ms <= 0) return "";
7209
+ const totalSeconds = Math.floor(ms / 1e3);
7210
+ const minutes = Math.floor(totalSeconds / 60);
7211
+ const seconds = totalSeconds % 60;
7212
+ return `${minutes} min, ${seconds} sec`;
7185
7213
  }
7186
- function parseSignalMessage(value, userMap) {
7187
- if (!value || typeof value !== "string") return value ?? "";
7214
+ function parseSignalMessage(value, myUserId) {
7215
+ if (!value || typeof value !== "string") return null;
7188
7216
  const trimmed = value.trim();
7189
- if (!trimmed) return "";
7217
+ if (!trimmed) return null;
7190
7218
  const parts = trimmed.split(" ");
7191
- const formatId = parts[0];
7192
- const userId = parts[1] ?? "";
7193
- const userName = userId ? resolveUser2(userId, userMap) : "User";
7194
- switch (formatId) {
7195
- // 1: Audio call started
7196
- case "1":
7197
- return `\u{1F4DE} ${userName} started an audio call.`;
7198
- // 2: Audio call missed
7199
- case "2":
7200
- return `\u{1F4DE} Missed audio call from ${userName}.`;
7201
- // 3: Audio call ended (caller_id ender_id duration)
7202
- case "3": {
7203
- const enderId = parts[2] ?? "";
7204
- const duration = parts[3] ?? "0";
7205
- const enderName = enderId ? resolveUser2(enderId, userMap) : "User";
7206
- return `\u{1F4DE} Audio call by ${userName}, ended by ${enderName}. Duration: ${formatDuration(duration)}.`;
7207
- }
7208
- // 4: Video call started
7209
- case "4":
7210
- return `\u{1F4F9} ${userName} started a video call.`;
7211
- // 5: Video call missed
7212
- case "5":
7213
- return `\u{1F4F9} Missed video call from ${userName}.`;
7214
- // 6: Video call ended (caller_id ender_id duration)
7215
- case "6": {
7216
- const enderId = parts[2] ?? "";
7217
- const duration = parts[3] ?? "0";
7218
- const enderName = enderId ? resolveUser2(enderId, userMap) : "User";
7219
- return `\u{1F4F9} Video call by ${userName}, ended by ${enderName}. Duration: ${formatDuration(duration)}.`;
7220
- }
7221
- // 7: Audio call rejected
7222
- case "7":
7223
- return `\u{1F4DE} Audio call from ${userName} was rejected.`;
7224
- // 8: Video call rejected
7225
- case "8":
7226
- return `\u{1F4F9} Video call from ${userName} was rejected.`;
7227
- // 9: Audio call busy
7228
- case "9":
7229
- return `\u{1F4DE} Audio call from ${userName} \u2014 recipient was busy.`;
7230
- // 10: Video call busy
7231
- case "10":
7232
- return `\u{1F4F9} Video call from ${userName} \u2014 recipient was busy.`;
7219
+ const number = parseInt(parts[0], 10);
7220
+ const callerId = parts[1] ?? "";
7221
+ const isMe = myUserId === callerId;
7222
+ let enderId = "";
7223
+ let duration = "";
7224
+ let callType = "";
7225
+ let color = "";
7226
+ if (number === 3 || number === 6) {
7227
+ enderId = parts[2] ?? "";
7228
+ duration = parts[3] === "0" ? "" : parts[3] ?? "";
7229
+ }
7230
+ let text;
7231
+ switch (number) {
7232
+ case 1:
7233
+ text = isMe ? "Calling..." : "Incoming audio call...";
7234
+ callType = CallType.AUDIO;
7235
+ color = "#54D62C";
7236
+ break;
7237
+ case 2:
7238
+ text = isMe ? "Outgoing audio call" : "You missed audio call";
7239
+ callType = CallType.AUDIO;
7240
+ color = "#FF4842";
7241
+ break;
7242
+ case 3:
7243
+ if (duration) {
7244
+ text = isMe ? "Outgoing audio call" : "Incoming audio call";
7245
+ color = "#54D62C";
7246
+ } else {
7247
+ if (enderId === myUserId) {
7248
+ text = "You cancel audio call";
7249
+ } else {
7250
+ text = "You missed audio call";
7251
+ }
7252
+ color = "#FF4842";
7253
+ }
7254
+ callType = CallType.AUDIO;
7255
+ break;
7256
+ case 4:
7257
+ text = isMe ? "Calling..." : "Incoming video call...";
7258
+ callType = CallType.VIDEO;
7259
+ color = "#54D62C";
7260
+ break;
7261
+ case 5:
7262
+ text = isMe ? "Outgoing video call" : "You missed video call";
7263
+ callType = CallType.VIDEO;
7264
+ color = "#FF4842";
7265
+ break;
7266
+ case 6:
7267
+ if (duration) {
7268
+ text = isMe ? "Outgoing video call" : "Incoming video call";
7269
+ color = "#54D62C";
7270
+ } else {
7271
+ if (enderId === myUserId) {
7272
+ text = "You cancel video call";
7273
+ } else {
7274
+ text = "You missed video call";
7275
+ }
7276
+ color = "#FF4842";
7277
+ }
7278
+ callType = CallType.VIDEO;
7279
+ break;
7280
+ case 7:
7281
+ text = isMe ? "Recipient rejected audio call" : "You rejected audio call";
7282
+ callType = CallType.AUDIO;
7283
+ color = "#FF4842";
7284
+ break;
7285
+ case 8:
7286
+ text = isMe ? "Recipient rejected video call" : "You rejected video call";
7287
+ callType = CallType.VIDEO;
7288
+ color = "#FF4842";
7289
+ break;
7290
+ case 9:
7291
+ text = isMe ? "Recipient was busy" : "You missed audio call";
7292
+ callType = CallType.AUDIO;
7293
+ color = "#FF4842";
7294
+ break;
7295
+ case 10:
7296
+ text = isMe ? "Recipient was busy" : "You missed video call";
7297
+ callType = CallType.VIDEO;
7298
+ color = "#FF4842";
7299
+ break;
7233
7300
  default:
7234
- return trimmed;
7301
+ text = trimmed;
7302
+ callType = "";
7303
+ color = "";
7235
7304
  }
7305
+ return { text, duration: formatDuration(duration), callType, color };
7236
7306
  }
7237
7307
  export {
7238
7308
  CallAction,
7239
7309
  CallStatus,
7310
+ CallType,
7240
7311
  Channel,
7241
7312
  ChannelState,
7242
7313
  ClientState,