easyproctor-hml 2.5.28 → 2.5.30

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/esm/index.js CHANGED
@@ -12290,38 +12290,93 @@ function recorder(stream, buffer, videoOptions, onBufferSizeError = false, onBuf
12290
12290
  };
12291
12291
  }
12292
12292
  console.log("recorderOptions bitsPerSecond", recorderOptions.videoBitsPerSecond);
12293
- const mediaRecorder = new MediaRecorder(stream, recorderOptions);
12294
- mediaRecorder.ondataavailable = (e2) => {
12295
- if (mediaRecorder.state != "recording") return;
12296
- bufferSize = bufferSize + e2.data.size;
12297
- console.log("video tracks length > 0", stream.getVideoTracks().length > 0);
12298
- if (stream.getVideoTracks().length > 0) {
12299
- const videoTrack = stream.getVideoTracks()[0];
12300
- console.log(videoTrack.readyState);
12301
- console.log(videoTrack.enabled);
12302
- }
12303
- if (e2.data.size > 0) {
12304
- buffer.push(e2.data);
12305
- }
12306
- if (!stopped) {
12307
- if (lastEvent && e2.data.size === lastEvent.data.size || e2.data.size === 0) {
12308
- proctoringId && lastEvent && e2.data.size === lastEvent.data.size && trackers.registerOnBufferSizeError(
12309
- proctoringId,
12310
- `onBufferSizeError: Recorder size freezed: ${e2.data.size} Mb`
12311
- );
12312
- proctoringId && e2.data.size === 0 && trackers.registerOnBufferSizeError(
12313
- proctoringId,
12314
- `onBufferSizeError: Recorder size equal 0 Mb`
12315
- );
12316
- console.log("onbuffer size error" + e2.data.size);
12317
- onBufferSizeErrorCallback && onBufferSizeErrorCallback();
12293
+ function buildMediaRecorder(stream2, recorderOptions2) {
12294
+ const tracks = stream2.getTracks();
12295
+ if (tracks.length == 0) {
12296
+ throw new Error("No tracks found");
12297
+ }
12298
+ const invalidTracks = tracks.find((t2) => t2.readyState != "live");
12299
+ if (invalidTracks) {
12300
+ throw new Error("Track not live: " + invalidTracks.label);
12301
+ }
12302
+ console.log("buildMediaRecorder tracks OK");
12303
+ let mediaRecorder2 = new MediaRecorder(stream2, recorderOptions2);
12304
+ console.log("buildMediaRecorder mediaRecorder OK");
12305
+ mediaRecorder2.ondataavailable = (e2) => {
12306
+ bufferSize = bufferSize + e2.data.size;
12307
+ console.log("video tracks length > 0", stream2.getVideoTracks().length > 0);
12308
+ if (stream2.getVideoTracks().length > 0) {
12309
+ const videoTrack = stream2.getVideoTracks()[0];
12310
+ console.log(videoTrack.readyState);
12311
+ console.log(videoTrack.enabled);
12312
+ }
12313
+ if (e2.data.size > 0) {
12314
+ buffer.push(e2.data);
12315
+ }
12316
+ if (!stopped) {
12317
+ if (lastEvent && e2.data.size === lastEvent.data.size || e2.data.size === 0) {
12318
+ proctoringId && lastEvent && e2.data.size === lastEvent.data.size && trackers.registerOnBufferSizeError(
12319
+ proctoringId,
12320
+ `onBufferSizeError: Recorder size freezed: ${e2.data.size} Mb`
12321
+ );
12322
+ proctoringId && e2.data.size === 0 && trackers.registerOnBufferSizeError(
12323
+ proctoringId,
12324
+ `onBufferSizeError: Recorder size equal 0 Mb`
12325
+ );
12326
+ console.log("onbuffer size error" + e2.data.size);
12327
+ onBufferSizeErrorCallback && onBufferSizeErrorCallback();
12328
+ }
12329
+ lastEvent = e2;
12330
+ } else {
12331
+ resolvePromise && resolvePromise();
12318
12332
  }
12319
- lastEvent = e2;
12320
- } else {
12321
- resolvePromise && resolvePromise();
12333
+ };
12334
+ return mediaRecorder2;
12335
+ }
12336
+ let mediaRecorder = buildMediaRecorder(stream, recorderOptions);
12337
+ async function startRecorder(recorder2) {
12338
+ return new Promise((resolve, reject) => {
12339
+ let started = false;
12340
+ recorder2.onstart = () => {
12341
+ started = true;
12342
+ resolve();
12343
+ };
12344
+ recorder2.onerror = (e2) => {
12345
+ reject(e2);
12346
+ };
12347
+ try {
12348
+ recorder2.start(1e4);
12349
+ } catch (e2) {
12350
+ return reject(e2);
12351
+ }
12352
+ setTimeout(() => {
12353
+ if (!started) {
12354
+ reject(new Error("Timeout ao iniciar o recorder"));
12355
+ }
12356
+ }, 500);
12357
+ });
12358
+ }
12359
+ async function startWithRetry(recorder2) {
12360
+ for (let i2 = 0; i2 < 3; i2++) {
12361
+ try {
12362
+ console.log("startWithRetry try", i2);
12363
+ await startRecorder(recorder2);
12364
+ if (recorder2.state === "recording") {
12365
+ bufferSize = 0;
12366
+ stopped = false;
12367
+ return;
12368
+ }
12369
+ } catch (e2) {
12370
+ console.error("Erro ao iniciar o recorder:", e2);
12371
+ }
12372
+ await new Promise((r2) => setTimeout(r2, 300));
12373
+ recorder2 = buildMediaRecorder(stream, recorderOptions);
12322
12374
  }
12323
- };
12324
- function startRecording() {
12375
+ throw new Error("Falha ao iniciar o recorder");
12376
+ }
12377
+ async function startRecording() {
12378
+ await startWithRetry(mediaRecorder);
12379
+ return;
12325
12380
  return new Promise((resolve, reject) => {
12326
12381
  var timeSlice = 1e4;
12327
12382
  if ((videoOptions == null ? void 0 : videoOptions.timeSlice) != void 0) {
@@ -12348,9 +12403,10 @@ function recorder(stream, buffer, videoOptions, onBufferSizeError = false, onBuf
12348
12403
  bufferSize = 0;
12349
12404
  stopped = false;
12350
12405
  setTimeout(async () => {
12351
- console.log("onstart Timeout");
12406
+ if (mediaRecorder.state == "recording") return;
12407
+ +console.log("onstart Timeout");
12352
12408
  reject(new Error("onstart Timeout"));
12353
- }, 3e3);
12409
+ }, 2e3);
12354
12410
  });
12355
12411
  }
12356
12412
  function stopRecording() {
@@ -12393,6 +12449,7 @@ function recorder(stream, buffer, videoOptions, onBufferSizeError = false, onBuf
12393
12449
  }
12394
12450
  return {
12395
12451
  startRecording,
12452
+ startWithRetry,
12396
12453
  stopRecording,
12397
12454
  pauseRecording,
12398
12455
  resumeRecording,
@@ -12757,6 +12814,7 @@ var CameraRecorder = class {
12757
12814
  this.isCanvasLoopActive = false;
12758
12815
  this.hardwareStream = null;
12759
12816
  this.internalClonedStream = null;
12817
+ this.videoElement = null;
12760
12818
  this.currentRetries = 0;
12761
12819
  this.packageCount = 0;
12762
12820
  this.noiseWait = 20;
@@ -12887,8 +12945,131 @@ Setting: ${JSON.stringify(settings, null, 2)}`
12887
12945
  this.hardwareStream = null;
12888
12946
  }
12889
12947
  }
12948
+ async waitForVideoFlow() {
12949
+ return new Promise((resolve) => {
12950
+ const check = () => {
12951
+ var _a2;
12952
+ if (this.videoElement && ((_a2 = this.videoElement) == null ? void 0 : _a2.readyState) >= 3) return resolve();
12953
+ else requestAnimationFrame(check);
12954
+ };
12955
+ check();
12956
+ });
12957
+ }
12958
+ async attachAndWarmup(stream) {
12959
+ this.videoElement = document.createElement("video");
12960
+ this.videoElement.srcObject = stream;
12961
+ this.videoElement.muted = true;
12962
+ await this.videoElement.play().catch((e2) => {
12963
+ });
12964
+ console.log("cameraStream.getVideoTracks() length:", this.cameraStream.getVideoTracks().length);
12965
+ await new Promise((resolve) => {
12966
+ var _a2;
12967
+ if (this.videoElement && ((_a2 = this.videoElement) == null ? void 0 : _a2.readyState) >= 1) return resolve();
12968
+ if (this.videoElement)
12969
+ this.videoElement.onloadedmetadata = () => resolve();
12970
+ });
12971
+ console.log("metadata ok");
12972
+ await this.waitForVideoFlow();
12973
+ console.log("waitForVideoFlow ok");
12974
+ await new Promise((r2) => setTimeout(r2, 300));
12975
+ }
12890
12976
  async startRecording(options) {
12891
- var _a2, _b, _c2, _d, _e2, _f, _g, _h;
12977
+ var _a2, _b, _c2, _d, _e2, _f, _g;
12978
+ console.log("startRecording Camera Recorder");
12979
+ if ((((_a2 = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _a2.detectPerson) || ((_b = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _b.detectCellPhone) || ((_c2 = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _c2.detectFace)) && !(options == null ? void 0 : options.retry)) {
12980
+ console.log("startRecording Camera Recorder initializeDetectors");
12981
+ await this.initializeDetectors();
12982
+ }
12983
+ const { cameraId, microphoneId } = this.options;
12984
+ const constraints = {
12985
+ audio: { deviceId: microphoneId },
12986
+ video: {
12987
+ deviceId: cameraId,
12988
+ width: this.videoOptions.width,
12989
+ height: this.videoOptions.height,
12990
+ frameRate: 15
12991
+ }
12992
+ };
12993
+ try {
12994
+ this.hardwareStream = await navigator.mediaDevices.getUserMedia(
12995
+ constraints
12996
+ );
12997
+ this.cameraStream = this.hardwareStream;
12998
+ } catch (error) {
12999
+ console.log("startRecording Camera Recorder error", error);
13000
+ if (error.toString() == "NotReadableError: Could not start video source")
13001
+ throw "N\xE3o foi poss\xEDvel conectar a camera, ela pode estar sendo utilizada por outro programa";
13002
+ throw error;
13003
+ }
13004
+ await this.attachAndWarmup(this.cameraStream);
13005
+ const {
13006
+ startRecording,
13007
+ stopRecording,
13008
+ pauseRecording,
13009
+ resumeRecording,
13010
+ recorderOptions,
13011
+ getBufferSize
13012
+ } = recorder(
13013
+ this.cameraStream,
13014
+ // streamToRecord,
13015
+ this.blobs,
13016
+ this.videoOptions,
13017
+ this.options.onBufferSizeError,
13018
+ (e2) => this.bufferError(e2),
13019
+ false
13020
+ );
13021
+ this.recordingStart = startRecording;
13022
+ this.recordingStop = stopRecording;
13023
+ this.recordingPause = pauseRecording;
13024
+ this.recordingResume = resumeRecording;
13025
+ this.recorderOptions = recorderOptions;
13026
+ this.getBufferSize = getBufferSize;
13027
+ try {
13028
+ await new Promise((r2) => setTimeout(r2, 500));
13029
+ await this.recordingStart();
13030
+ } catch (error) {
13031
+ console.log("startRecording Camera Recorder error", error);
13032
+ throw error;
13033
+ }
13034
+ const track = this.cameraStream.getVideoTracks()[0];
13035
+ const settings = track.getSettings();
13036
+ let { width = 0, height = 0 } = settings;
13037
+ const isPortrait = (_d = screen.orientation) == null ? void 0 : _d.type.includes("portrait");
13038
+ if (isPortrait && isMobileDevice()) {
13039
+ if (this.videoOptions.width == height && this.videoOptions.height == width) {
13040
+ [width, height] = [height, width];
13041
+ }
13042
+ }
13043
+ if (this.videoOptions.minWidth > width || this.videoOptions.minHeight > height) {
13044
+ throw STREAM_UNDER_MINIMUM_PERMITTED;
13045
+ } else if (this.videoOptions.width !== width || this.videoOptions.height !== height) {
13046
+ trackers.registerAnotherStream(
13047
+ this.proctoringId,
13048
+ `Maybe have another stream active
13049
+ Video Options: ${JSON.stringify(
13050
+ this.videoOptions,
13051
+ null,
13052
+ 2
13053
+ )}
13054
+ Setting: ${JSON.stringify(settings, null, 2)}`
13055
+ );
13056
+ throw ANOTHER_STREAM_ACTIVE;
13057
+ }
13058
+ if ((_e2 = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _e2.detectFace) {
13059
+ await this.faceDetection.enableCam(this.cameraStream);
13060
+ }
13061
+ if (((_f = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _f.detectPerson) || ((_g = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _g.detectCellPhone)) {
13062
+ await this.objectDetection.enableCam(this.cameraStream);
13063
+ }
13064
+ this.filesToUpload = [];
13065
+ if (this.options.proctoringType == "REALTIME") {
13066
+ this.captureFrame();
13067
+ }
13068
+ this.packageCount = 0;
13069
+ console.log("startRecording Camera Recorder OK");
13070
+ }
13071
+ async startRecordingOld(options) {
13072
+ var _a2, _b, _c2, _d, _e2, _f, _g;
12892
13073
  console.log("startRecording Camera Recorder");
12893
13074
  if ((((_a2 = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _a2.detectPerson) || ((_b = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _b.detectCellPhone) || ((_c2 = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _c2.detectFace)) && !(options == null ? void 0 : options.retry)) {
12894
13075
  console.log("startRecording Camera Recorder initializeDetectors");
@@ -12918,6 +13099,17 @@ Setting: ${JSON.stringify(settings, null, 2)}`
12918
13099
  const track = this.cameraStream.getVideoTracks()[0];
12919
13100
  const settings = track.getSettings();
12920
13101
  let { width = 0, height = 0 } = settings;
13102
+ console.log("cameraStream.getVideoTracks() length:", this.cameraStream.getVideoTracks().length);
13103
+ await new Promise((resolve) => {
13104
+ const interval = setInterval(() => {
13105
+ console.log("cameraStream.getVideoTracks()[0].readyState", this.cameraStream.getVideoTracks()[0].readyState);
13106
+ if (this.cameraStream.getVideoTracks()[0].readyState == "live") {
13107
+ clearInterval(interval);
13108
+ resolve();
13109
+ }
13110
+ }, 100);
13111
+ });
13112
+ await new Promise((r2) => setTimeout(r2, 300));
12921
13113
  const {
12922
13114
  startRecording,
12923
13115
  stopRecording,
@@ -12942,6 +13134,7 @@ Setting: ${JSON.stringify(settings, null, 2)}`
12942
13134
  this.getBufferSize = getBufferSize;
12943
13135
  console.log("startRecording Camera Recorder recordingStart");
12944
13136
  try {
13137
+ await new Promise((r2) => setTimeout(r2, 500));
12945
13138
  await this.recordingStart();
12946
13139
  } catch (error) {
12947
13140
  console.log("startRecording Camera Recorder error", error);
@@ -12968,11 +13161,10 @@ Setting: ${JSON.stringify(settings, null, 2)}`
12968
13161
  );
12969
13162
  throw ANOTHER_STREAM_ACTIVE;
12970
13163
  }
12971
- ((_e2 = this.paramsConfig.imageBehaviourParameters) == null ? void 0 : _e2.useUploadImage) && this.options.proctoringType == "IMAGE" && this.photoShotsCycle();
12972
- if ((_f = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _f.detectFace) {
13164
+ if ((_e2 = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _e2.detectFace) {
12973
13165
  await this.faceDetection.enableCam(this.cameraStream);
12974
13166
  }
12975
- if (((_g = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _g.detectPerson) || ((_h = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _h.detectCellPhone)) {
13167
+ if (((_f = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _f.detectPerson) || ((_g = this.paramsConfig.videoBehaviourParameters) == null ? void 0 : _g.detectCellPhone)) {
12976
13168
  await this.objectDetection.enableCam(this.cameraStream);
12977
13169
  }
12978
13170
  this.filesToUpload = [];
@@ -12983,6 +13175,7 @@ Setting: ${JSON.stringify(settings, null, 2)}`
12983
13175
  console.log("startRecording Camera Recorder finished");
12984
13176
  }
12985
13177
  async stopRecording() {
13178
+ var _a2, _b, _c2;
12986
13179
  console.log("Stopping Camera Recorder...");
12987
13180
  this.isCanvasLoopActive = false;
12988
13181
  this.recordingStop && await this.recordingStop();
@@ -13002,8 +13195,18 @@ Setting: ${JSON.stringify(settings, null, 2)}`
13002
13195
  this.hardwareStream.getTracks().forEach((track) => track.stop());
13003
13196
  this.hardwareStream = null;
13004
13197
  }
13005
- } catch (e2) {
13006
- console.error("Erro ao parar os streams de m\xEDdia.");
13198
+ if (this.videoElement) {
13199
+ (_a2 = this.videoElement) == null ? void 0 : _a2.remove();
13200
+ this.videoElement = null;
13201
+ }
13202
+ if (this.video) {
13203
+ (_b = this.video) == null ? void 0 : _b.remove();
13204
+ }
13205
+ if (this.canvas) {
13206
+ (_c2 = this.canvas) == null ? void 0 : _c2.remove();
13207
+ }
13208
+ } catch (error) {
13209
+ console.error("Erro ao parar os streams de m\xEDdia.", error);
13007
13210
  }
13008
13211
  this.faceDetection && this.faceDetection.detecting && this.faceDetection.stopDetection();
13009
13212
  this.objectDetection && this.objectDetection.detecting && this.objectDetection.stopDetection();
@@ -13022,35 +13225,6 @@ Setting: ${JSON.stringify(settings, null, 2)}`
13022
13225
  async resumeRecording() {
13023
13226
  await this.recordingResume();
13024
13227
  }
13025
- photoShotsCycle() {
13026
- let imageFile;
13027
- this.configImageCapture();
13028
- this.imageInterval = setInterval(async () => {
13029
- this.canvas.getContext("2d").drawImage(
13030
- this.video,
13031
- 0,
13032
- 0,
13033
- this.videoOptions.width,
13034
- this.videoOptions.height
13035
- );
13036
- const image_data_url = this.canvas.toDataURL("image/jpeg");
13037
- imageFile = await this.getFile(
13038
- image_data_url,
13039
- `${this.proctoringId}_${this.imageCount + 1}.jpg`,
13040
- "image/jpeg"
13041
- );
13042
- if (imageFile && this.upload && this.backendToken) {
13043
- this.upload.upload(
13044
- {
13045
- file: imageFile
13046
- },
13047
- this.backendToken,
13048
- true
13049
- );
13050
- this.imageCount++;
13051
- }
13052
- }, this.paramsConfig.imageBehaviourParameters.uploadInterval * 1e3);
13053
- }
13054
13228
  async getCurrentImageBase64() {
13055
13229
  if (!this.video || !this.canvas) {
13056
13230
  this.configImageCapture();
@@ -18604,7 +18778,6 @@ var _ExternalCameraChecker = class _ExternalCameraChecker {
18604
18778
  this.connection = null;
18605
18779
  this.context = context;
18606
18780
  this.onRealtimeAlertsCallback = onRealtimeAlertsCallback;
18607
- console.log("context -> ", context);
18608
18781
  this.backend = new BackendService({
18609
18782
  type: (context == null ? void 0 : context.type) || "prod",
18610
18783
  token: context.token
@@ -19658,6 +19831,7 @@ var Proctoring = class {
19658
19831
  try {
19659
19832
  console.log("start Proctoring recorder.startAll");
19660
19833
  await this.recorder.startAll();
19834
+ console.log("start Proctoring recorder.startAll ok");
19661
19835
  } catch (error) {
19662
19836
  console.log("start Proctoring error", error);
19663
19837
  alert(error);