fix(Camera): add last device id support switch device#8262
Merged
Conversation
Added lastUsedDeviceId property to camera object for tracking the last used video device. Signed-off-by: kkxkx <63846879+kkxkx@users.noreply.github.com>
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds tracking of the last used video device in the camera component and skips unnecessary device reinitialization when the same device is selected. Sequence diagram for camera update flow with lastUsedDeviceId trackingsequenceDiagram
actor User
participant CameraComponent as CameraElement
participant CameraJs as CameraModule
participant MediaDevices
User->>CameraComponent: change data-device-id
CameraComponent->>CameraJs: update(id)
CameraJs->>CameraJs: [read data-device-id]
CameraJs->>CameraJs: [camera.lastUsedDeviceId === deviceId]
alt same deviceId
CameraJs->>CameraJs: return
else different deviceId
CameraJs->>CameraJs: stopDevice(camera)
CameraJs->>CameraJs: openDevice(camera)
CameraJs->>MediaDevices: navigator.mediaDevices.getUserMedia(option)
MediaDevices-->>CameraJs: stream
CameraJs->>CameraJs: play(camera, option)
CameraJs->>CameraJs: camera.lastUsedDeviceId = option.video.deviceId
end
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
play, accessingoption.video.deviceId.exactassumesoption.videoandoption.video.deviceIdare always present and either a constraint object or string; consider guarding or normalizing this value to avoid runtime errors whenplayis called without an explicit device. - The type of
lastUsedDeviceIdmay differ from thedata-device-idattribute (e.g., constraint object vs. string), so it would be safer to normalize both before comparing inupdateto ensure the equality check behaves as intended. - The early return
if (camera.lastUsedDeviceId === deviceId) return;inupdatecan block switching whencamera.video.deviceIdhas changed; consider basing the decision solely oncamera.video.deviceIdor ensuringlastUsedDeviceIdis always kept in sync with the active track.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `play`, accessing `option.video.deviceId.exact` assumes `option.video` and `option.video.deviceId` are always present and either a constraint object or string; consider guarding or normalizing this value to avoid runtime errors when `play` is called without an explicit device.
- The type of `lastUsedDeviceId` may differ from the `data-device-id` attribute (e.g., constraint object vs. string), so it would be safer to normalize both before comparing in `update` to ensure the equality check behaves as intended.
- The early return `if (camera.lastUsedDeviceId === deviceId) return;` in `update` can block switching when `camera.video.deviceId` has changed; consider basing the decision solely on `camera.video.deviceId` or ensuring `lastUsedDeviceId` is always kept in sync with the active track.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8262 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 769 769
Lines 34436 34436
=========================================
Hits 34436 34436
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ArgoZhang
approved these changes
Jul 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link issues
fixes #8263
fixes #8264
Is there an existing issue for this?
I have searched the existing issues
Describe the bug
Camera Devices Added lastUsedDeviceId property to camera object for tracking the last used video device.
Camera组件添加最终使用的设备ID属性,解决因UI刷新导致的视频流画面闪烁问题。
Expected Behavior
Camera Devices Added lastUsedDeviceId property to camera object for tracking the last used video device.
Camera组件添加最终使用的设备ID属性,解决因UI刷新导致的视频流画面闪烁问题。
Interactive render mode
Interactive Server (Interactive server-side rendering (interactive SSR) using Blazor Server)
Steps To Reproduce
none
Exceptions (if any)
No response
.NET Version
NET10.0
Anything else?
No response