Skip to content

Commit bb4bcc6

Browse files
committed
chore: fix camera shot not freezing issue
1 parent 5e3f4ff commit bb4bcc6

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/blocks/CameraSource/CameraSource.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export class CameraSource extends LitUploaderBlock {
7272
private _audioDevices: AudioDeviceOption[] = [];
7373
private _permissionResponses: Partial<Record<(typeof DEFAULT_PERMISSIONS)[number], PermissionStatus>> = {};
7474
private _permissionCleanupFns: Array<() => void> = [];
75+
private _currentVideoSource: MediaStream | null = null;
7576

7677
private readonly _handlePreviewPlay = (): void => {
7778
this._startTimeline();
@@ -90,9 +91,6 @@ export class CameraSource extends LitUploaderBlock {
9091
private _startTime = 0;
9192
private _elapsedTime = 0;
9293

93-
@state()
94-
protected video: MediaStream | null = null;
95-
9694
@state()
9795
protected videoTransformCss: string | null = null;
9896

@@ -424,7 +422,7 @@ export class CameraSource extends LitUploaderBlock {
424422

425423
videoElement.muted = false;
426424
videoElement.volume = 1;
427-
this.video = null;
425+
this._setVideoSource(null);
428426
videoElement.src = videoURL;
429427

430428
this._attachPreviewListeners(videoElement);
@@ -445,12 +443,31 @@ export class CameraSource extends LitUploaderBlock {
445443
videoElement?.removeEventListener('pause', this._handlePreviewPause);
446444
}
447445

446+
private _setVideoSource(stream: MediaStream | null): void {
447+
if (this._currentVideoSource === stream) {
448+
return;
449+
}
450+
this._currentVideoSource = stream;
451+
this._applyVideoSource();
452+
}
453+
454+
private _applyVideoSource(): void {
455+
const videoElement = this.videoRef.value;
456+
if (!videoElement) {
457+
return;
458+
}
459+
const nextSource = this._currentVideoSource ?? null;
460+
if (videoElement.srcObject !== nextSource) {
461+
videoElement.srcObject = nextSource;
462+
}
463+
}
464+
448465
_retake = (): void => {
449466
this._setCameraState(CameraSourceEvents.RETAKE);
450467

451468
/** Reset video */
452469
if (this._activeTab === CameraSourceTypes.VIDEO) {
453-
this.video = this._stream;
470+
this._setVideoSource(this._stream);
454471
const videoElement = this.videoRef.value as HTMLVideoElement | undefined;
455472
if (videoElement) {
456473
videoElement.muted = true;
@@ -735,10 +752,10 @@ export class CameraSource extends LitUploaderBlock {
735752
if (this.videoRef.value) {
736753
this.videoRef.value.volume = 0;
737754
}
738-
const tracks = this.video?.getTracks?.();
755+
const tracks = this._currentVideoSource?.getTracks?.();
739756
tracks?.[0]?.stop();
740757
this._detachPreviewListeners(this.videoRef.value);
741-
this.video = null;
758+
this._setVideoSource(null);
742759

743760
this._makeStreamInactive();
744761
this._stopTimer();
@@ -782,7 +799,7 @@ export class CameraSource extends LitUploaderBlock {
782799
this._setPermissionsState('denied');
783800
});
784801

785-
this.video = this._stream;
802+
this._setVideoSource(this._stream);
786803
this._capturing = true;
787804
this._setPermissionsState('granted');
788805
} catch (error) {
@@ -942,10 +959,21 @@ export class CameraSource extends LitUploaderBlock {
942959
});
943960
}
944961

962+
protected override firstUpdated(_changedProperties: Map<PropertyKey, unknown>): void {
963+
super.firstUpdated(_changedProperties);
964+
this._applyVideoSource();
965+
}
966+
967+
protected override updated(_changedProperties: Map<PropertyKey, unknown>): void {
968+
super.updated(_changedProperties);
969+
this._applyVideoSource();
970+
}
971+
945972
_destroy(): void {
946973
this._teardownPermissionListeners();
947974
navigator.mediaDevices?.removeEventListener('devicechange', this._getDevices);
948975
this._detachPreviewListeners(this.videoRef.value);
976+
this._setVideoSource(null);
949977
}
950978

951979
override disconnectedCallback(): void {
@@ -991,7 +1019,6 @@ export class CameraSource extends LitUploaderBlock {
9911019
muted
9921020
autoplay
9931021
playsinline
994-
.srcObject=${this.video}
9951022
style=${styleMap({
9961023
transform: this.videoTransformCss,
9971024
})}

0 commit comments

Comments
 (0)