-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes #287 Unable to stop the camera when route changed #357
base: master
Are you sure you want to change the base?
Conversation
Upon unmounting of the QrReader component, the video track will now be stopped.
@JodusNodus @JonatanSalas Please review and let me know if there are any issues! Thank you |
Stops the callback function passed inside parent function 'decodeFromConstraints'
Any chance this can get merged in? Deal breaker for my use case. |
if (stopRequested.current) { | ||
//Callback to parent function continuously runs after destruction of Video Element // Stream Video Tracks | ||
//Intentionally throwing exception to step out of parent function. | ||
throw new StopRequestedException(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have found that throwing an Exception here is unnecessary and undesirable as it leads to an uncaught runtime error. We can also stop the code reader using controls
:
if (isValidType(video, 'constraints', 'object')) {
codeReader
- .decodeFromConstraints({video}, videoId, (result, error) => {
+ .decodeFromConstraints({video}, videoId, (result, error, controls) => {
if (stopRequested.current) {
- //Callback to parent function continuously runs after destruction of Video Element // Stream Video Tracks
- //Intentionally throwing exception to step out of parent function.
- throw new StopRequestedException();
+ controls?.stop();
}
if (isValidType(onResult, 'onResult', 'function')) {
onResult?.(result, error, codeReader);
}
})
.then((controls: IScannerControls) => (controlsRef.current = controls))
.catch((error: Error) => {
if (isValidType(onResult, 'onResult', 'function')) {
onResult?.(null, error, codeReader);
}
});
}
export class StopRequestedException extends Exception { | ||
constructor() { | ||
super(); | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this class if we use controls
instead of throwing an exception
navigator.mediaDevices | ||
.getUserMedia({ | ||
video: true, | ||
audio: false, | ||
}) | ||
.then((mediaStream) => | ||
mediaStream.getTracks().forEach((track) => { | ||
track.stop(); | ||
mediaStream.removeTrack(track); | ||
}) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes an error when QrReader is conditionally rendered, because when QrReader is unmounted, no media can be found. We can handle it:
navigator.mediaDevices
.getUserMedia({
video: true,
audio: false,
})
.then((mediaStream) =>
mediaStream.getTracks().forEach((track) => {
track.stop();
mediaStream.removeTrack(track);
})
)
+ .catch((error) => {
+ console.warn('Error stopping video tracks:', error);
+ });
is this going to be merged? |
There is an issue in the following scenario:
Now, Upon unmounting of the QrReader component, the video track will be stopped and parent decode functions will be haulted.