Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Dec 24, 2025

Link issues

fixes #860

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 sound control capabilities to the HikVision web plugin, including opening, closing, and adjusting volume during real-time playback.

New Features:

  • Expose OpenSound, CloseSound, and SetVolume methods on the HikVision web plugin component for controlling audio during live preview.

Enhancements:

  • Extend the HikVision JavaScript bridge and Razor JS module to support audio control operations when a real-time stream is active.

Copilot AI review requested due to automatic review settings December 24, 2025 02:56
@bb-auto bb-auto bot added the enhancement New feature or request label Dec 24, 2025
@bb-auto bb-auto bot added this to the v9.2.0 milestone Dec 24, 2025
@sourcery-ai
Copy link

sourcery-ai bot commented Dec 24, 2025

Reviewer's Guide

Adds sound control support to the HikVision web plugin by wiring new JavaScript sound APIs (open, close, set volume) through to the Blazor component and its JS interop wrapper, with basic state checks and error-code handling.

Sequence diagram for HikVision sound control via Blazor and JS

sequenceDiagram
    actor User
    participant HikVisionWebPlugin as HikVisionWebPlugin_cs
    participant HikVisionWebPlugin_js as HikVisionWebPlugin_js
    participant Hikvision_js as hikvision_js
    participant Data
    participant WebVideoCtrl

    User->>HikVisionWebPlugin: OpenSound()
    HikVisionWebPlugin->>HikVisionWebPlugin: check IsLogin && IsRealPlaying
    alt not logged in or not playing
        HikVisionWebPlugin-->>User: false
    else logged in and playing
        HikVisionWebPlugin->>HikVisionWebPlugin_js: InvokeAsync openSound(Id)
        HikVisionWebPlugin_js->>Hikvision_js: openSound(Id)
        Hikvision_js->>Data: get(Id)
        Data-->>Hikvision_js: vision with iWndIndex and realPlaying
        Hikvision_js->>Hikvision_js: check realPlaying === true
        alt not real playing
            Hikvision_js-->>HikVisionWebPlugin_js: 101
        else real playing
            Hikvision_js->>WebVideoCtrl: I_OpenSound(iWndIndex)
            alt WebVideoCtrl success
                WebVideoCtrl-->>Hikvision_js: success
                Hikvision_js-->>HikVisionWebPlugin_js: 0
            else WebVideoCtrl throws
                WebVideoCtrl-->>Hikvision_js: exception
                Hikvision_js->>Hikvision_js: set code = 102
                Hikvision_js-->>HikVisionWebPlugin_js: 102
            end
        end
        HikVisionWebPlugin_js-->>HikVisionWebPlugin: code
        HikVisionWebPlugin->>HikVisionWebPlugin: ret = (code == 0)
        HikVisionWebPlugin-->>User: ret
    end

    User->>HikVisionWebPlugin: SetVolume(value)
    HikVisionWebPlugin->>HikVisionWebPlugin: check IsLogin && IsRealPlaying
    alt not logged in or not playing
        HikVisionWebPlugin-->>User: false
    else logged in and playing
        HikVisionWebPlugin->>HikVisionWebPlugin_js: InvokeAsync setVolume(Id, clamped value)
        HikVisionWebPlugin_js->>Hikvision_js: setVolume(Id, value)
        Hikvision_js->>Data: get(Id)
        Data-->>Hikvision_js: vision with iWndIndex and realPlaying
        Hikvision_js->>Hikvision_js: check realPlaying === true
        alt not real playing
            Hikvision_js-->>HikVisionWebPlugin_js: 101
        else real playing
            Hikvision_js->>Hikvision_js: parse and clamp volume 0-100
            Hikvision_js->>WebVideoCtrl: I_SetVolume(volume)
            alt WebVideoCtrl success
                WebVideoCtrl-->>Hikvision_js: success
                Hikvision_js-->>HikVisionWebPlugin_js: 0
            else WebVideoCtrl throws
                WebVideoCtrl-->>Hikvision_js: exception
                Hikvision_js->>Hikvision_js: set code = 101
                Hikvision_js-->>HikVisionWebPlugin_js: 101
            end
        end
        HikVisionWebPlugin_js-->>HikVisionWebPlugin: code
        HikVisionWebPlugin->>HikVisionWebPlugin: ret = (code == 0)
        HikVisionWebPlugin-->>User: ret
    end
Loading

Class diagram for updated HikVisionWebPlugin sound methods

classDiagram
    class HikVisionWebPlugin {
        string Id
        bool IsLogin
        bool IsRealPlaying

        Task TriggerGetChannelList(HikVisionChannel channel)
        Task bool OpenSound()
        Task bool CloseSound()
        Task bool SetVolume(int value)
    }

    class HikVisionChannel {
    }

    HikVisionWebPlugin --> HikVisionChannel : uses
Loading

File-Level Changes

Change Details Files
Introduce sound control functions in the HikVision JavaScript SDK wrapper with basic playback-state validation and error codes.
  • Add async openSound(id) that validates realPlaying, invokes WebVideoCtrl.I_OpenSound with the current window index, and returns 0/101/102 status codes.
  • Add async closeSound(id) mirroring openSound but calling WebVideoCtrl.I_CloseSound.
  • Add async setVolume(id, value) that validates realPlaying, normalizes the volume input to 0–100, calls WebVideoCtrl.I_SetVolume, and returns status codes with basic error handling.
src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js
Expose the new sound control APIs to Blazor via the HikVisionWebPlugin component using JS interop.
  • Add OpenSound, CloseSound, and SetVolume instance methods on HikVisionWebPlugin that guard on IsLogin and IsRealPlaying, call the corresponding JS functions via InvokeAsync, and return success as a bool.
  • Clamp the volume value between 0 and 100 before passing it to JS in SetVolume.
src/components/BootstrapBlazor.HikVision/Components/HikVisionWebPlugin.razor.cs
Wire the new sound functions through the HikVisionWebPlugin JavaScript bridge module so they can be invoked from .NET.
  • Import openSound, closeSound, and setVolume from hikvision.js into HikVisionWebPlugin.razor.js.
  • Re-export openSound, closeSound, and setVolume from the JS bridge alongside the existing logout/startRealPlay/stopRealPlay exports.
src/components/BootstrapBlazor.HikVision/Components/HikVisionWebPlugin.razor.js

Assessment against linked issues

Issue Objective Addressed Explanation
#860 Add OpenSound and CloseSound methods to the HikVision component so that sound can be opened/closed during real play (including wiring through JS and .NET layers).

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

Copy link

@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 - I've found 2 issues, and left some high level feedback:

  • In setVolume the catch block sets code = 101 while the other methods use 102 for exceptions and 101 for precondition failures, which makes error semantics inconsistent and harder to interpret; consider standardizing the error codes across openSound, closeSound, and setVolume.
  • Volume normalization is currently done both in C# (Math.Max(0, Math.Min(100, value))) and again in JS (parseInt + clamp + default 50), which is redundant and can diverge over time; consider centralizing the range/default handling in a single layer.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `setVolume` the catch block sets `code = 101` while the other methods use `102` for exceptions and `101` for precondition failures, which makes error semantics inconsistent and harder to interpret; consider standardizing the error codes across `openSound`, `closeSound`, and `setVolume`.
- Volume normalization is currently done both in C# (`Math.Max(0, Math.Min(100, value))`) and again in JS (`parseInt` + clamp + default 50), which is redundant and can diverge over time; consider centralizing the range/default handling in a single layer.

## Individual Comments

### Comment 1
<location> `src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js:351-352` </location>
<code_context>
 }

+export async function openSound(id) {
+    const vision = Data.get(id);
+    const { iWndIndex, realPlaying } = vision;
+
+    if (realPlaying !== true) {
</code_context>

<issue_to_address>
**issue (bug_risk):** Handle the case where `Data.get(id)` returns `undefined` to avoid a runtime error during destructuring.

If `Data.get(id)` can return `undefined` (e.g., after `dispose` or for an invalid `id`), the destructuring will throw before you can return an error. Add a null/undefined check on `vision` (e.g., `if (!vision) return 101;`) before destructuring in `openSound`, `closeSound`, and `setVolume`.
</issue_to_address>

### Comment 2
<location> `src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js:401-408` </location>
<code_context>
+    try {
+        await WebVideoCtrl.I_OpenSound(iWndIndex);
+    }
+    catch (ex) {
+        code = 102;
+        console.log(ex);
+    }
+    return code;
+}
+
+export async function closeSound(id) {
+    const vision = Data.get(id);
+    const { iWndIndex, realPlaying } = vision;
+
+    if (realPlaying !== true) {
+        return 101;
+    }
+
+    let code = 0;
+    try {
+        await WebVideoCtrl.I_CloseSound(iWndIndex);
+    }
+    catch (ex) {
+        code = 102;
+        console.log(ex);
+    }
+    return code;
+}
+
+export async function setVolume(id, value) {
+    const vision = Data.get(id);
+    const { iWndIndex, realPlaying } = vision;
+
+    if (realPlaying !== true) {
+        return 101;
+    }
+
+    let v = parseInt(value);
+    if (isNaN(v)) {
+        v = 50;
+    }
+
+    let code = 0;
+    try {
+        await WebVideoCtrl.I_SetVolume(Math.min(100, Math.max(0, v)));
+    }
+    catch (ex) {
+        code = 101;
+        console.log(ex);
+    }
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Use a distinct error code in `setVolume`'s catch block to keep semantics consistent with `openSound`/`closeSound`.

`openSound`/`closeSound` use `101` for "not playing" and `102` for exceptions, but `setVolume` uses `101` for both. This makes it impossible for callers to distinguish an invalid state from a runtime failure. Consider using `102` (or another distinct code) in the `catch` block for consistency and clearer error handling.

```suggestion
    let code = 0;
    try {
        await WebVideoCtrl.I_SetVolume(Math.min(100, Math.max(0, v)));
    }
    catch (ex) {
        code = 102;
        console.log(ex);
    }
```
</issue_to_address>

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.

@ArgoZhang ArgoZhang merged commit 3275974 into master Dec 24, 2025
1 of 2 checks passed
@ArgoZhang ArgoZhang deleted the feat-hik branch December 24, 2025 03:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds sound control functionality to the HikVision Web Plugin component by implementing three new methods: OpenSound, CloseSound, and SetVolume. The version number is incremented from 10.0.4 to 10.0.5.

  • Adds JavaScript functions for sound control operations with error handling
  • Exposes sound control methods in the C# API layer with proper validation
  • Updates module exports to include the new functionality

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
wwwroot/hikvision.js Adds three new exported functions (openSound, closeSound, setVolume) with error handling and validation
Components/HikVisionWebPlugin.razor.js Updates imports and exports to include the new sound control functions
Components/HikVisionWebPlugin.razor.cs Adds public OpenSound, CloseSound, and SetVolume methods with proper state checks and JavaScript interop
BootstrapBlazor.HikVision.csproj Increments package version to 10.0.5

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

await WebVideoCtrl.I_SetVolume(Math.min(100, Math.max(0, v)));
}
catch (ex) {
code = 102;
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

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

Inconsistent error code in the setVolume function. The catch block uses error code 101, while the openSound and closeSound functions use 102 for operation failures. For consistency with the other sound control functions, this should be 102.

Copilot uses AI. Check for mistakes.
Comment on lines +350 to +410
export async function openSound(id) {
const vision = Data.get(id);
const { iWndIndex, realPlaying } = vision;

if (realPlaying !== true) {
return 101;
}

let code = 0;
try {
await WebVideoCtrl.I_OpenSound(iWndIndex);
}
catch (ex) {
code = 102;
console.log(ex);
}
return code;
}

export async function closeSound(id) {
const vision = Data.get(id);
const { iWndIndex, realPlaying } = vision;

if (realPlaying !== true) {
return 101;
}

let code = 0;
try {
await WebVideoCtrl.I_CloseSound(iWndIndex);
}
catch (ex) {
code = 102;
console.log(ex);
}
return code;
}

export async function setVolume(id, value) {
const vision = Data.get(id);
const { iWndIndex, realPlaying } = vision;

if (realPlaying !== true) {
return 101;
}

let v = parseInt(value);
if (isNaN(v)) {
v = 50;
}

let code = 0;
try {
await WebVideoCtrl.I_SetVolume(Math.min(100, Math.max(0, v)));
}
catch (ex) {
code = 102;
console.log(ex);
}
return code;
}
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

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

The three new sound control functions (openSound, closeSound, setVolume) have duplicated validation and error handling logic. Consider extracting this into a shared helper function that takes the operation function as a parameter to improve maintainability and reduce code duplication.

Copilot uses AI. Check for mistakes.

export async function setVolume(id, value) {
const vision = Data.get(id);
const { iWndIndex, realPlaying } = vision;
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

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

Unused variable iWndIndex.

Suggested change
const { iWndIndex, realPlaying } = vision;
const { realPlaying } = vision;

Copilot uses AI. Check for mistakes.
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(HikVision): add OpenSound/CloseSound method

2 participants