Skip to content

fix(Camera): add last device id support switch device#8262

Merged
ArgoZhang merged 3 commits into
dotnetcore:mainfrom
kkxkx:main
Jul 24, 2026
Merged

fix(Camera): add last device id support switch device#8262
ArgoZhang merged 3 commits into
dotnetcore:mainfrom
kkxkx:main

Conversation

@kkxkx

@kkxkx kkxkx commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

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

Added lastUsedDeviceId property to camera object for tracking the last used video device.

Signed-off-by: kkxkx <63846879+kkxkx@users.noreply.github.com>
@sourcery-ai

sourcery-ai Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds 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 tracking

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Track and store the last used video device ID on the camera object.
  • Initialize a lastUsedDeviceId property on the camera object when it is created.
  • Set lastUsedDeviceId when a new video stream is opened, handling both exact and non-exact deviceId formats.
src/BootstrapBlazor/Components/Camera/Camera.razor.js
Avoid reopening the camera when the selected device matches the last used device.
  • Read the currently selected deviceId from the camera element attribute on update.
  • Early-return from update when the selected deviceId equals lastUsedDeviceId to prevent redundant stop/open operations.
src/BootstrapBlazor/Components/Camera/Camera.razor.js

Possibly linked issues

  • #unknown: PR stores exact deviceId and prevents unnecessary switching, directly addressing the camera exact device-id bug.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b30c523) to head (b3e1636).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #8262   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          769       769           
  Lines        34436     34436           
=========================================
  Hits         34436     34436           
Flag Coverage Δ
BB 100.00% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ArgoZhang ArgoZhang changed the title Track last used video device in camera component fix(Camera): add last device id support switch device Jul 24, 2026
@ArgoZhang
ArgoZhang merged commit d4bca8a into dotnetcore:main Jul 24, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(Camera): add last device id support switch device Track last used video device in camera component

2 participants