-
-
Notifications
You must be signed in to change notification settings - Fork 363
feat(IVideoDevice): add IVideoDevice service #5940
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
939eeb5
refactor: 增加 IMediaDevices 服务
ArgoZhang 95fc5ed
doc: 更新示例
ArgoZhang 9350eec
feat: 增加 media 脚本
ArgoZhang ebcca89
feat: 增加 IMediaDeviceInfo 接口
ArgoZhang c42dab6
doc: 更新示例
ArgoZhang d31e559
refactor: 增加 getUserMedia 方法
ArgoZhang ca78fe9
feat: 增加视频功能
ArgoZhang 53d788a
doc: 增加示例
ArgoZhang 912503a
feat: 增加 IMeidaVideo 接口服务
ArgoZhang c9cf582
doc: 更新示例
ArgoZhang ac7faec
refactor: 优化 registerBootstrapBlazorModule 方法
ArgoZhang cc37a8a
feat: 增加 drawImage 方法
ArgoZhang 1cf7e5a
refactor: 移除 GetDisplayMedia 方法
ArgoZhang 63d1f6c
feat: 精简代码
ArgoZhang 890a515
doc: 更新示例
ArgoZhang 560379a
Merge branch 'main' into feat-media
ArgoZhang 21d216c
refactor: 重构 IMediaVideo 接口
ArgoZhang cb4f77e
refactor: 更新脚本
ArgoZhang 62ab88d
refactor: 重构代码
ArgoZhang 9553e17
doc: 更新示例
ArgoZhang 9555a5c
refactor: 更新接口
ArgoZhang 2299067
refactor: 增加镜头翻转功能
ArgoZhang 5e4ee67
refactor: 更改服务名称
ArgoZhang 5a8f7bd
feat: 更新 open close 方法实现逻辑
ArgoZhang 38f07b9
doc: 更新示例
ArgoZhang 3acbb27
feat: 实现 GetPreviewUrl 方法
ArgoZhang 3bddd3c
doc: 更新示例
ArgoZhang 4515544
refactor: 精简代码
ArgoZhang 7f0d7e9
refactor: 增加翻转功能
ArgoZhang 6f8e7f3
doc: 更新示例代码
ArgoZhang 8c1edfe
chore: 增加源码映射文件
ArgoZhang 9a9237f
doc: 增加菜单
ArgoZhang f7d7251
doc: 更改示例文档名称
ArgoZhang 885651e
doc: 更新文档
ArgoZhang ea65373
test: 增加单元测试
ArgoZhang 18d561a
chore: bump version 9.6.1-beta01
ArgoZhang d6d62b7
Merge branch 'chore-version' into feat-media
ArgoZhang ee34de8
refactor: 撤销 Flip 方法
ArgoZhang e549994
doc: 撤销更改
ArgoZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/BootstrapBlazor.Server/Components/Samples/MediaDevice.razor
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| @page "/media-device" | ||
| @inject IStringLocalizer<MediaDevice> Localizer | ||
|
|
||
| <h3>@Localizer["MediaDeviceTitle"]</h3> | ||
|
|
||
| <h4>@Localizer["MediaDeviceIntro"]</h4> | ||
|
|
||
| <p>@((MarkupString)Localizer["MediaDeviceDescription"].Value)</p> | ||
|
|
||
| <Pre>[Inject, NotNull] | ||
| private IBluetooth? BluetoothService { get; set; }</Pre> | ||
|
|
||
| <Tips> | ||
| <ul class="ul-demo"> | ||
| <li>@((MarkupString)Localizer["MediaDeviceTipsLi1"].Value)</li> | ||
| <li>@((MarkupString)Localizer["MediaDeviceTipsLi2"].Value)</li> | ||
| </ul> | ||
| <div>@((MarkupString)Localizer["MediaDeviceTipsTitle"].Value)</div> | ||
| </Tips> | ||
|
|
||
| <DemoBlock Title="@Localizer["BaseUsageTitle"]" | ||
| Introduction="@Localizer["BaseUsageIntro"]" | ||
| Name="Normal"> | ||
| <section ignore> | ||
| @Localizer["UsageDesc"] | ||
| </section> | ||
| <div class="row form-inline g-3"> | ||
| <div class="col-12"> | ||
| <Button Text="@Localizer["MediaDeviceRequestText"]" Icon="fa-solid fa-photo-film" OnClick="OnRequestDevice"></Button> | ||
| <Button Text="@Localizer["MediaDeviceOpenText"]" Icon="fa-solid fa-play" OnClick="OnOpenVideo"></Button> | ||
| <Button Text="@Localizer["MediaDeviceCloseText"]" Icon="fa-solid fa-stop" OnClick="OnCloseVideo"></Button> | ||
| <Button Text="@Localizer["MediaDeviceCaptureText"]" Icon="fa-solid fa-camera" OnClick="OnCapture"></Button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <Select Items="@_items" @bind-Value="_deviceId"></Select> | ||
|
|
||
| <video class="bb-video" muted playsinline autoplay style="height: auto; width: auto;"></video> | ||
|
|
||
| <canvas class="b-video-image"></canvas> | ||
| </DemoBlock> | ||
|
|
54 changes: 54 additions & 0 deletions
54
src/BootstrapBlazor.Server/Components/Samples/MediaDevice.razor.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Server.Components.Samples; | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public partial class MediaDevice | ||
| { | ||
| [Inject, NotNull] | ||
| private IMediaVideo? VideoDeviceService { get; set; } | ||
|
|
||
| private readonly List<IMediaDeviceInfo> _devices = []; | ||
|
|
||
| private List<SelectedItem> _items = []; | ||
|
|
||
| private string? _deviceId; | ||
|
|
||
| private async Task OnRequestDevice() | ||
| { | ||
| var devices = await VideoDeviceService.GetDevices(); | ||
| if (devices != null) | ||
| { | ||
| _devices.AddRange(devices); | ||
| _items = [.. _devices.Select(i => new SelectedItem(i.DeviceId, i.Label))]; | ||
| } | ||
| } | ||
|
|
||
| private async Task OnOpenVideo() | ||
| { | ||
| if (!string.IsNullOrEmpty(_deviceId)) | ||
| { | ||
| var constraints = new MediaTrackConstraints | ||
| { | ||
| DeviceId = _deviceId, | ||
| VideoSelector = ".bb-video" | ||
| }; | ||
| await VideoDeviceService.Open(constraints); | ||
| } | ||
| } | ||
|
|
||
| private async Task OnCloseVideo() | ||
| { | ||
| await VideoDeviceService.Close(".bb-video"); | ||
| } | ||
|
|
||
| private async Task OnCapture() | ||
| { | ||
| await VideoDeviceService.Capture(".bb-video"); | ||
| } | ||
| } |
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
43 changes: 43 additions & 0 deletions
43
src/BootstrapBlazor/Services/MediaDevices/DefaultMediaDevices.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| class DefaultMediaDevices(IJSRuntime jsRuntime) : IMediaDevices | ||
| { | ||
| private DotNetObjectReference<DefaultMediaDevices>? _interop = null; | ||
| private JSModule? _module = null; | ||
|
|
||
| private async Task<JSModule> LoadModule() | ||
| { | ||
| _interop ??= DotNetObjectReference.Create(this); | ||
| _module ??= await jsRuntime.LoadModuleByName("media"); | ||
| return _module; | ||
| } | ||
|
|
||
| public async Task<IEnumerable<IMediaDeviceInfo>?> EnumerateDevices() | ||
| { | ||
| var module = await LoadModule(); | ||
| return await module.InvokeAsync<List<MediaDeviceInfo>?>("enumerateDevices"); | ||
| } | ||
|
|
||
| public async Task Open(MediaTrackConstraints constraints) | ||
| { | ||
| var module = await LoadModule(); | ||
| await module.InvokeVoidAsync("open", constraints); | ||
| } | ||
|
|
||
| public async Task Close(string selector) | ||
| { | ||
| var module = await LoadModule(); | ||
| await module.InvokeVoidAsync("close", selector); | ||
| } | ||
|
|
||
| public async Task Capture(string selector) | ||
| { | ||
| var module = await LoadModule(); | ||
| await module.InvokeVoidAsync("capture", selector); | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
src/BootstrapBlazor/Services/MediaDevices/DefaultMediaVideo.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| class DefaultMediaVideo(IMediaDevices deviceService) : IMediaVideo | ||
| { | ||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public async Task<List<IMediaDeviceInfo>?> GetDevices() | ||
| { | ||
| var ret = new List<IMediaDeviceInfo>(); | ||
| var devices = await deviceService.EnumerateDevices(); | ||
| if (devices != null) | ||
| { | ||
| ret.AddRange(devices.Where(d => d.Kind == "videoinput")); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| public Task Open(MediaTrackConstraints constraints) | ||
| { | ||
| return deviceService.Open(constraints); | ||
| } | ||
|
|
||
| public Task Close(string selector) | ||
| { | ||
| return deviceService.Close(selector); | ||
| } | ||
|
|
||
| public Task Capture(string selector) | ||
| { | ||
| return deviceService.Capture(selector); | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/BootstrapBlazor/Services/MediaDevices/DisplayMediaOptions.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public class DisplayMediaOptions | ||
| { | ||
| } |
32 changes: 32 additions & 0 deletions
32
src/BootstrapBlazor/Services/MediaDevices/IMediaDeviceInfo.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// The MediaDeviceInfo interface of the Media Capture and Streams API contains information that describes a single media input or output device. | ||
| /// </summary> | ||
| public interface IMediaDeviceInfo | ||
| { | ||
| /// <summary> | ||
| /// The deviceId read-only property of the MediaDeviceInfo interface returns a string that is an identifier for the represented device and is persisted across sessions. | ||
| /// </summary> | ||
| public string DeviceId { get; } | ||
|
|
||
| /// <summary> | ||
| /// The groupId read-only property of the MediaDeviceInfo interface returns a string that is a group identifier. | ||
| /// </summary> | ||
| public string GroupId { get; } | ||
|
|
||
| /// <summary> | ||
| /// The kind read-only property of the MediaDeviceInfo interface returns an enumerated value, that is either "videoinput", "audioinput" or "audiooutput". | ||
| /// </summary> | ||
| public string Kind { get; } | ||
|
|
||
| /// <summary> | ||
| /// The label read-only property of the MediaDeviceInfo interface returns a string describing this device (for example "External USB Webcam"). | ||
| /// </summary> | ||
| public string Label { get; } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/BootstrapBlazor/Services/MediaDevices/IMediaDevices.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// The MediaDevices interface of the Media Capture and Streams API provides access to connected media input devices like cameras and microphones, as well as screen sharing. In essence, it lets you obtain access to any hardware source of media data. | ||
| /// </summary> | ||
| public interface IMediaDevices | ||
| { | ||
| /// <summary> | ||
| /// An array of MediaDeviceInfo objects. Each object in the array describes one of the available media input and output devices. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| Task<IEnumerable<IMediaDeviceInfo>?> EnumerateDevices(); | ||
|
|
||
| /// <summary> | ||
| /// The open() method of the MediaDevices interface creates a new MediaStream object and starts capturing media from the specified device. | ||
| /// </summary> | ||
| /// <param name="constraints"></param> | ||
| /// <returns></returns> | ||
| Task Open(MediaTrackConstraints constraints); | ||
|
|
||
| /// <summary> | ||
| /// The close() method of the MediaDevices interface stops capturing media from the specified device and closes the MediaStream object. | ||
| /// </summary> | ||
| /// <param name="selector"></param> | ||
| /// <returns></returns> | ||
| Task Close(string selector); | ||
|
|
||
| /// <summary> | ||
| /// The capture() method of the MediaDevices interface captures a still image from the specified video stream and saves it to the specified location. | ||
| /// </summary> | ||
| /// <param name="selector"></param> | ||
| /// <returns></returns> | ||
| Task Capture(string selector); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public interface IMediaVideo | ||
| { | ||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| Task<List<IMediaDeviceInfo>?> GetDevices(); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <param name="constraints"></param> | ||
| /// <returns></returns> | ||
| Task Open(MediaTrackConstraints constraints); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <param name="selector"></param> | ||
| /// <returns></returns> | ||
| Task Close(string selector); | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <param name="selector"></param> | ||
| /// <returns></returns> | ||
| Task Capture(string selector); | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/BootstrapBlazor/Services/MediaDevices/MediaDeviceInfo.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| class MediaDeviceInfo : IMediaDeviceInfo | ||
| { | ||
| public string DeviceId { get; set; } = ""; | ||
|
|
||
| public string GroupId { get; set; } = ""; | ||
|
|
||
| public string Kind { get; set; } = ""; | ||
|
|
||
| public string Label { get; set; } = ""; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// The MediaStream interface of the Media Capture and Streams API represents a stream of media content. A stream consists of several tracks, such as video or audio tracks. Each track is specified as an instance of MediaStreamTrack. | ||
| /// </summary> | ||
| public class MediaStream | ||
| { | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/BootstrapBlazor/Services/MediaDevices/MediaTrackConstraints.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// The MediaTrackConstraints interface of the Media Capture and Streams API is used to specify constraints on the media tracks that are requested from a media device. It is used in conjunction with the getUserMedia() method to specify the desired properties of the media tracks, such as resolution, frame rate, and aspect ratio. | ||
| /// </summary> | ||
| public class MediaTrackConstraints | ||
| { | ||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public string DeviceId { get; set; } = ""; | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public string? VideoSelector { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public int Width { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public int Height { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public string? FacingMode { get; set; } | ||
| } | ||
Oops, something went wrong.
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.
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.
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:
// ... existing code ...