@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.
package/dist/index.cjs CHANGED
@@ -32,6 +32,7 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  CallAction: () => CallAction,
34
34
  CallStatus: () => CallStatus,
35
+ CallType: () => CallType,
35
36
  Channel: () => Channel,
36
37
  ChannelState: () => ChannelState,
37
38
  ClientState: () => ClientState,
@@ -3664,7 +3665,7 @@ var ErmisChat = class _ErmisChat {
3664
3665
  return pinExpires;
3665
3666
  }
3666
3667
  getUserAgent() {
3667
- return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.2"}`;
3668
+ return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.3"}`;
3668
3669
  }
3669
3670
  setUserAgent(userAgent) {
3670
3671
  this.userAgent = userAgent;
@@ -6374,21 +6375,38 @@ var ErmisCallNode = class {
6374
6375
  const mediaConstraints = await this.getMediaConstraints();
6375
6376
  try {
6376
6377
  const stream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
6377
- if (this.callStatus === "ended" /* ENDED */) {
6378
- stream.getTracks().forEach((track) => track.stop());
6379
- this.destroy();
6380
- return;
6378
+ return this.applyLocalStream(stream);
6379
+ } catch (error) {
6380
+ console.warn("Error getting user media:", error?.message);
6381
+ if (this.callType === "video" && mediaConstraints.video) {
6382
+ try {
6383
+ const audioOnlyStream = await navigator.mediaDevices.getUserMedia({
6384
+ audio: mediaConstraints.audio,
6385
+ video: false
6386
+ });
6387
+ this.setConnectionMessage("Camera not available, using audio only");
6388
+ return this.applyLocalStream(audioOnlyStream);
6389
+ } catch {
6390
+ }
6381
6391
  }
6382
- if (this.onLocalStream) {
6383
- this.onLocalStream(stream);
6392
+ if (typeof this.onError === "function") {
6393
+ this.onError("No microphone or camera found. Please check your device.");
6384
6394
  }
6385
- this.localStream = stream;
6386
- return stream;
6387
- } catch (error) {
6388
- console.error("Error getting user media:", error);
6389
6395
  return null;
6390
6396
  }
6391
6397
  }
6398
+ applyLocalStream(stream) {
6399
+ if (this.callStatus === "ended" /* ENDED */) {
6400
+ stream.getTracks().forEach((track) => track.stop());
6401
+ this.destroy();
6402
+ return;
6403
+ }
6404
+ if (this.onLocalStream) {
6405
+ this.onLocalStream(stream);
6406
+ }
6407
+ this.localStream = stream;
6408
+ return stream;
6409
+ }
6392
6410
  setConnectionMessage(message) {
6393
6411
  if (typeof this.onConnectionMessageChange === "function") {
6394
6412
  this.onConnectionMessageChange(message);
@@ -6433,20 +6451,10 @@ var ErmisCallNode = class {
6433
6451
  this.isDestroyed = false;
6434
6452
  this.callStatus = "";
6435
6453
  this.callType = is_video ? "video" : "audio";
6436
- await this.startLocalStream();
6437
- if (this.callStatus === "ended" /* ENDED */) return;
6438
6454
  this.setUserInfo(cid, eventUserId);
6439
6455
  this.setCallStatus("ringing" /* RINGING */);
6440
6456
  this.cid = cid || "";
6441
6457
  this.metadata = metadata || {};
6442
- console.log("----metadata---", metadata);
6443
- if (eventUserId !== this.userID) {
6444
- await this.initialize();
6445
- }
6446
- if (this.localStream && this.mediaSender && this.mediaReceiver) {
6447
- this.mediaSender?.initEncoders(this.localStream);
6448
- this.mediaReceiver?.initDecoders(this.callType);
6449
- }
6450
6458
  if (typeof this.onCallEvent === "function") {
6451
6459
  this.onCallEvent({
6452
6460
  type: eventUserId !== this.userID ? "incoming" : "outgoing",
@@ -6457,6 +6465,15 @@ var ErmisCallNode = class {
6457
6465
  metadata: this.metadata
6458
6466
  });
6459
6467
  }
6468
+ await this.startLocalStream();
6469
+ if (this.callStatus === "ended" /* ENDED */) return;
6470
+ if (eventUserId !== this.userID) {
6471
+ await this.initialize();
6472
+ }
6473
+ if (this.localStream && this.mediaSender && this.mediaReceiver) {
6474
+ this.mediaSender?.initEncoders(this.localStream);
6475
+ this.mediaReceiver?.initDecoders(this.callType);
6476
+ }
6460
6477
  if (eventUserId === this.userID) {
6461
6478
  if (this.missCallTimeout) clearTimeout(this.missCallTimeout);
6462
6479
  this.missCallTimeout = setTimeout(async () => {
@@ -6717,21 +6734,30 @@ var ErmisCallNode = class {
6717
6734
  }
6718
6735
  async stopScreenShare() {
6719
6736
  const mediaConstraints = await this.getMediaConstraints();
6720
- const cameraStream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
6721
- const cameraTrack = cameraStream.getVideoTracks()[0];
6722
- if (this.localStream) {
6723
- this.localStream.getVideoTracks().forEach((track) => track.stop());
6724
- this.localStream.removeTrack(this.localStream.getVideoTracks()[0]);
6725
- this.localStream.addTrack(cameraTrack);
6726
- } else {
6727
- this.localStream = cameraStream;
6728
- }
6729
- if (this.onLocalStream) {
6730
- this.onLocalStream(this.localStream);
6731
- this.mediaSender?.replaceVideoTrack(this.localStream.getVideoTracks()[0]);
6732
- }
6733
- if (typeof this.onScreenShareChange === "function") {
6734
- this.onScreenShareChange(false);
6737
+ try {
6738
+ const cameraStream = await navigator.mediaDevices.getUserMedia({
6739
+ video: mediaConstraints.video,
6740
+ audio: false
6741
+ });
6742
+ const cameraTrack = cameraStream.getVideoTracks()[0];
6743
+ if (this.localStream) {
6744
+ this.localStream.getVideoTracks().forEach((track) => {
6745
+ track.stop();
6746
+ this.localStream?.removeTrack(track);
6747
+ });
6748
+ this.localStream.addTrack(cameraTrack);
6749
+ } else {
6750
+ this.localStream = cameraStream;
6751
+ }
6752
+ if (this.onLocalStream) {
6753
+ this.onLocalStream(this.localStream);
6754
+ this.mediaSender?.replaceVideoTrack(this.localStream.getVideoTracks()[0]);
6755
+ }
6756
+ if (typeof this.onScreenShareChange === "function") {
6757
+ this.onScreenShareChange(false);
6758
+ }
6759
+ } catch (error) {
6760
+ console.error("Error stopping screen share and reverting to camera:", error);
6735
6761
  }
6736
6762
  }
6737
6763
  async toggleMic(enabled) {
@@ -6999,7 +7025,7 @@ var ErmisAuthProvider = class {
6999
7025
  return data;
7000
7026
  }
7001
7027
  getUserAgent() {
7002
- return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.2"}`;
7028
+ return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.3"}`;
7003
7029
  }
7004
7030
  setUserAgent(userAgent) {
7005
7031
  this.userAgent = userAgent;
@@ -7232,71 +7258,117 @@ function parseSystemMessage(value, userMap) {
7232
7258
  }
7233
7259
 
7234
7260
  // src/signal_message.ts
7235
- function formatDuration(durationSec) {
7236
- const sec = parseInt(durationSec, 10);
7237
- if (isNaN(sec) || sec < 0) return durationSec;
7238
- const minutes = Math.floor(sec / 60);
7239
- const seconds = sec % 60;
7240
- return `${minutes}:${seconds.toString().padStart(2, "0")}`;
7241
- }
7242
- function resolveUser2(userId, userMap) {
7243
- return userMap[userId] ?? userId;
7261
+ var CallType = {
7262
+ AUDIO: "audio",
7263
+ VIDEO: "video"
7264
+ };
7265
+ function formatDuration(durationMs) {
7266
+ if (!durationMs) return "";
7267
+ const ms = parseInt(durationMs, 10);
7268
+ if (isNaN(ms) || ms <= 0) return "";
7269
+ const totalSeconds = Math.floor(ms / 1e3);
7270
+ const minutes = Math.floor(totalSeconds / 60);
7271
+ const seconds = totalSeconds % 60;
7272
+ return `${minutes} min, ${seconds} sec`;
7244
7273
  }
7245
- function parseSignalMessage(value, userMap) {
7246
- if (!value || typeof value !== "string") return value ?? "";
7274
+ function parseSignalMessage(value, myUserId) {
7275
+ if (!value || typeof value !== "string") return null;
7247
7276
  const trimmed = value.trim();
7248
- if (!trimmed) return "";
7277
+ if (!trimmed) return null;
7249
7278
  const parts = trimmed.split(" ");
7250
- const formatId = parts[0];
7251
- const userId = parts[1] ?? "";
7252
- const userName = userId ? resolveUser2(userId, userMap) : "User";
7253
- switch (formatId) {
7254
- // 1: Audio call started
7255
- case "1":
7256
- return `\u{1F4DE} ${userName} started an audio call.`;
7257
- // 2: Audio call missed
7258
- case "2":
7259
- return `\u{1F4DE} Missed audio call from ${userName}.`;
7260
- // 3: Audio call ended (caller_id ender_id duration)
7261
- case "3": {
7262
- const enderId = parts[2] ?? "";
7263
- const duration = parts[3] ?? "0";
7264
- const enderName = enderId ? resolveUser2(enderId, userMap) : "User";
7265
- return `\u{1F4DE} Audio call by ${userName}, ended by ${enderName}. Duration: ${formatDuration(duration)}.`;
7266
- }
7267
- // 4: Video call started
7268
- case "4":
7269
- return `\u{1F4F9} ${userName} started a video call.`;
7270
- // 5: Video call missed
7271
- case "5":
7272
- return `\u{1F4F9} Missed video call from ${userName}.`;
7273
- // 6: Video call ended (caller_id ender_id duration)
7274
- case "6": {
7275
- const enderId = parts[2] ?? "";
7276
- const duration = parts[3] ?? "0";
7277
- const enderName = enderId ? resolveUser2(enderId, userMap) : "User";
7278
- return `\u{1F4F9} Video call by ${userName}, ended by ${enderName}. Duration: ${formatDuration(duration)}.`;
7279
- }
7280
- // 7: Audio call rejected
7281
- case "7":
7282
- return `\u{1F4DE} Audio call from ${userName} was rejected.`;
7283
- // 8: Video call rejected
7284
- case "8":
7285
- return `\u{1F4F9} Video call from ${userName} was rejected.`;
7286
- // 9: Audio call busy
7287
- case "9":
7288
- return `\u{1F4DE} Audio call from ${userName} \u2014 recipient was busy.`;
7289
- // 10: Video call busy
7290
- case "10":
7291
- return `\u{1F4F9} Video call from ${userName} \u2014 recipient was busy.`;
7279
+ const number = parseInt(parts[0], 10);
7280
+ const callerId = parts[1] ?? "";
7281
+ const isMe = myUserId === callerId;
7282
+ let enderId = "";
7283
+ let duration = "";
7284
+ let callType = "";
7285
+ let color = "";
7286
+ if (number === 3 || number === 6) {
7287
+ enderId = parts[2] ?? "";
7288
+ duration = parts[3] === "0" ? "" : parts[3] ?? "";
7289
+ }
7290
+ let text;
7291
+ switch (number) {
7292
+ case 1:
7293
+ text = isMe ? "Calling..." : "Incoming audio call...";
7294
+ callType = CallType.AUDIO;
7295
+ color = "#54D62C";
7296
+ break;
7297
+ case 2:
7298
+ text = isMe ? "Outgoing audio call" : "You missed audio call";
7299
+ callType = CallType.AUDIO;
7300
+ color = "#FF4842";
7301
+ break;
7302
+ case 3:
7303
+ if (duration) {
7304
+ text = isMe ? "Outgoing audio call" : "Incoming audio call";
7305
+ color = "#54D62C";
7306
+ } else {
7307
+ if (enderId === myUserId) {
7308
+ text = "You cancel audio call";
7309
+ } else {
7310
+ text = "You missed audio call";
7311
+ }
7312
+ color = "#FF4842";
7313
+ }
7314
+ callType = CallType.AUDIO;
7315
+ break;
7316
+ case 4:
7317
+ text = isMe ? "Calling..." : "Incoming video call...";
7318
+ callType = CallType.VIDEO;
7319
+ color = "#54D62C";
7320
+ break;
7321
+ case 5:
7322
+ text = isMe ? "Outgoing video call" : "You missed video call";
7323
+ callType = CallType.VIDEO;
7324
+ color = "#FF4842";
7325
+ break;
7326
+ case 6:
7327
+ if (duration) {
7328
+ text = isMe ? "Outgoing video call" : "Incoming video call";
7329
+ color = "#54D62C";
7330
+ } else {
7331
+ if (enderId === myUserId) {
7332
+ text = "You cancel video call";
7333
+ } else {
7334
+ text = "You missed video call";
7335
+ }
7336
+ color = "#FF4842";
7337
+ }
7338
+ callType = CallType.VIDEO;
7339
+ break;
7340
+ case 7:
7341
+ text = isMe ? "Recipient rejected audio call" : "You rejected audio call";
7342
+ callType = CallType.AUDIO;
7343
+ color = "#FF4842";
7344
+ break;
7345
+ case 8:
7346
+ text = isMe ? "Recipient rejected video call" : "You rejected video call";
7347
+ callType = CallType.VIDEO;
7348
+ color = "#FF4842";
7349
+ break;
7350
+ case 9:
7351
+ text = isMe ? "Recipient was busy" : "You missed audio call";
7352
+ callType = CallType.AUDIO;
7353
+ color = "#FF4842";
7354
+ break;
7355
+ case 10:
7356
+ text = isMe ? "Recipient was busy" : "You missed video call";
7357
+ callType = CallType.VIDEO;
7358
+ color = "#FF4842";
7359
+ break;
7292
7360
  default:
7293
- return trimmed;
7361
+ text = trimmed;
7362
+ callType = "";
7363
+ color = "";
7294
7364
  }
7365
+ return { text, duration: formatDuration(duration), callType, color };
7295
7366
  }
7296
7367
  // Annotate the CommonJS export names for ESM import in node:
7297
7368
  0 && (module.exports = {
7298
7369
  CallAction,
7299
7370
  CallStatus,
7371
+ CallType,
7300
7372
  Channel,
7301
7373
  ChannelState,
7302
7374
  ClientState,