Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented May 2, 2025

Link issues

fixes #5939

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Add media device support to the BootstrapBlazor library, introducing services and components for interacting with media devices like cameras

New Features:

  • Implement IMediaDevices and IMediaVideo services for accessing and managing media devices
  • Add JavaScript module for media device interactions
  • Create MediaDevice sample component to demonstrate media device functionality

Enhancements:

  • Extend service collection to support media device services
  • Provide interfaces for device enumeration, opening, closing, and capturing media

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented May 2, 2025

Reviewer's Guide

This pull request introduces services (IMediaDevices, IMediaVideo) for interacting with browser media devices (like cameras) by defining C# interfaces and implementations that use JavaScript interop (media.js) to call the navigator.mediaDevices browser API. Services are registered for dependency injection, and a sample component demonstrates usage.

File-Level Changes

Change Details Files
Added core services and models for media device interaction.
  • Defined IMediaDevices and IMediaVideo interfaces.
  • Implemented DefaultMediaDevices using JS interop.
  • Implemented DefaultMediaVideo wrapping IMediaDevices and filtering for video inputs.
  • Added data models for device info (IMediaDeviceInfo, MediaDeviceInfo) and track constraints (MediaTrackConstraints).
src/BootstrapBlazor/Services/MediaDevices/IMediaDevices.cs
src/BootstrapBlazor/Services/MediaDevices/DefaultMediaDevices.cs
src/BootstrapBlazor/Services/MediaDevices/IMediaVideo.cs
src/BootstrapBlazor/Services/MediaDevices/DefaultMediaVideo.cs
src/BootstrapBlazor/Services/MediaDevices/IMediaDeviceInfo.cs
src/BootstrapBlazor/Services/MediaDevices/MediaDeviceInfo.cs
src/BootstrapBlazor/Services/MediaDevices/MediaTrackConstraints.cs
src/BootstrapBlazor/Services/MediaDevices/DisplayMediaOptions.cs
src/BootstrapBlazor/Services/MediaDevices/MediaStream.cs
Implemented JavaScript interop for browser media APIs.
  • Created enumerateDevices function to list media devices.
  • Created open function to access and display video stream.
  • Created close function to stop video stream.
  • Created capture function to take photos from video stream.
src/BootstrapBlazor/wwwroot/modules/media.js
Registered new services for dependency injection.
  • Registered IMediaDevices and DefaultMediaDevices.
  • Registered IMediaVideo and DefaultMediaVideo.
src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs
Added a sample component demonstrating media device usage.
  • Created MediaDevice.razor markup with buttons, device selector, video element, and canvas.
  • Implemented MediaDevice.razor.cs code-behind to handle user interactions (request devices, open/close video, capture image) using IMediaVideo service.
src/BootstrapBlazor.Server/Components/Samples/MediaDevice.razor.cs
src/BootstrapBlazor.Server/Components/Samples/MediaDevice.razor

Assessment against linked issues

Issue Objective Addressed Explanation
#5939 Add IMediaDevice service to the application.

Possibly linked issues


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

@bb-auto bb-auto bot added the enhancement New feature or request label May 2, 2025
@bb-auto bb-auto bot added this to the v9.6.0 milestone May 2, 2025
sourcery-ai[bot]
sourcery-ai bot previously approved these changes May 2, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ArgoZhang - I've reviewed your changes - here's some feedback:

  • Consider separating media device enumeration logic from media stream control logic (open/close/capture) into distinct services.
  • Passing DOM selectors from C# services to JavaScript tightly couples the service logic to specific UI implementations.
  • Remove the empty DisplayMediaOptions and MediaStream classes if they are placeholders for future work.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

Comment on lines 30 to 31
const stream = await navigator.mediaDevices.getUserMedia(constrains);
video.srcObject = stream;
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Consider handling errors in open() function.

Wrap the navigator.mediaDevices.getUserMedia call in a try-catch to handle permission denials and other errors gracefully.

Suggested change
const stream = await navigator.mediaDevices.getUserMedia(constrains);
video.srcObject = stream;
try {
const stream = await navigator.mediaDevices.getUserMedia(constrains);
video.srcObject = stream;
} catch (error) {
console.error("Error accessing media devices:", error);
}

}
}

export async function capture(videoSelector) {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Check for existence of target element in capture().

Ensure document.querySelector('.b-video-image') returns a non-null element before use to avoid runtime errors.

Suggested implementation:

export async function capture(videoSelector) {
    const video = document.querySelector(videoSelector);
    if (video) {
        const bVideoImage = document.querySelector('.b-video-image');
        if (!bVideoImage) {
            console.error("The element with class 'b-video-image' was not found.");
            return;
        }

If the capture() function uses the bVideoImage element later to display a captured image, ensure that further logic (e.g., drawing from a canvas and setting bVideoImage.src) is placed after this check.


class DefaultMediaDevices(IJSRuntime jsRuntime) : IMediaDevices
{
private DotNetObjectReference<DefaultMediaDevices>? _interop = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Dispose DotNetObjectReference to prevent memory leaks.

Implement IAsyncDisposable (or manually dispose) to release the _interop DotNetObjectReference created in LoadModule when the service is disposed.

Suggested implementation:

class DefaultMediaDevices(IJSRuntime jsRuntime) : IMediaDevices, IAsyncDisposable
    private DotNetObjectReference<DefaultMediaDevices>? _interop = null;
    // ... existing code ...
    private async Task<JSModule> LoadModule()
    {
        _interop ??= DotNetObjectReference.Create(this);
    // Other existing methods...

    public async ValueTask DisposeAsync()
    {
        if (_module is not null)
        {
            await _module.DisposeAsync();
            _module = null;
        }
        if (_interop is not null)
        {
            _interop.Dispose();
            _interop = null;
        }
    }

@codecov
Copy link

codecov bot commented May 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (0e5d5d4) to head (e549994).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #5940   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          665       669    +4     
  Lines        30485     30551   +66     
  Branches      4345      4347    +2     
=========================================
+ Hits         30485     30551   +66     

☔ View full report in Codecov by Sentry.
📢 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.

@ArgoZhang ArgoZhang changed the title feat(IMediaDevice): add IMediaDevice service feat(IVideoDevice): add IVideoDevice service May 2, 2025
@ArgoZhang ArgoZhang merged commit fde404c into main May 2, 2025
5 checks passed
@ArgoZhang ArgoZhang deleted the feat-media branch May 2, 2025 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(IVideoDevice): add IVideoDevice service

2 participants