diff --git a/src/components/BootstrapBlazor.MediaMtx/skills/bb-mediamtx/SKILL.md b/src/components/BootstrapBlazor.MediaMtx/skills/bb-mediamtx/SKILL.md index 1031fa90..d7934581 100644 --- a/src/components/BootstrapBlazor.MediaMtx/skills/bb-mediamtx/SKILL.md +++ b/src/components/BootstrapBlazor.MediaMtx/skills/bb-mediamtx/SKILL.md @@ -14,7 +14,7 @@ builder.Services.AddMediaMtxService(); ``` - 主包提供服务端 `IMediaMtxService`,不支持独立 WebAssembly 宿主。DI 默认单例,从 `MediaMtxOptions` 节绑定;可用注册回调覆盖配置,修改后需重启应用。 -- 注册不启动网关。先通过 `bb-mediamtx --download --copy --output .\publish` 部署工具;跨平台加 `--rid`。CLI 不修改工程或创建构建规则,运行时不下载工具。 +- 注册不启动网关。先通过 `bb-mediamtx --download --copy --output .\publish` 部署工具;跨平台加 `--rid`,需要指定版本时用 `--download `(省略时使用默认版本)。CLI 不修改工程或创建构建规则,运行时不下载工具。 - `Gateway:MediaMtxPath` 默认指向应用目录的 `mediamtx-tools\\mediamtx.exe`(Windows)或 `mediamtx`;支持 Windows x64、Linux x64/ARM64/ARMv7、macOS x64/ARM64。CLI 需要 .NET 11。 - CLI 的 `--copy` 只使用缓存;`--force` 覆盖输出工具和许可证,不强制下载。失败以警告报告,不能仅凭退出码判断部署成功。 diff --git a/tools/BootstrapBlazor.MediaMtx.Cli/MediaMtxInstaller.cs b/tools/BootstrapBlazor.MediaMtx.Cli/MediaMtxInstaller.cs index 8b0bf0bd..94ec68d1 100644 --- a/tools/BootstrapBlazor.MediaMtx.Cli/MediaMtxInstaller.cs +++ b/tools/BootstrapBlazor.MediaMtx.Cli/MediaMtxInstaller.cs @@ -15,6 +15,8 @@ namespace BootstrapBlazor.MediaMtx.Cli; static partial class MediaMtxInstaller { + public const string DefaultVersion = "v1.22.0"; + private static (string Repository, Uri DownloadBase) GetSource(string source) => source switch { "github.com" => ("https://api.github.com/repos/bluenviron/mediamtx", @@ -40,13 +42,27 @@ static partial class MediaMtxInstaller ["osx-arm64"] = "darwin_arm64.tar.gz" }; - public static async Task DownloadAllAsync(string source, string? output, bool copy, bool force, CancellationToken cancellationToken) + public static string NormalizeVersion(string value) + { + var version = value.Trim(); + if (!version.StartsWith('v')) + { + version = "v" + version; + } + if (!VersionRegex().IsMatch(version)) + { + throw new ArgumentException($"Invalid version '{value}'. Expected a MediaMTX release tag such as v1.22.0."); + } + return version; + } + + public static async Task DownloadAllAsync(string source, string tag, string? output, bool copy, bool force, CancellationToken cancellationToken) { foreach (var rid in _platforms.Keys) { try { - await DeployAsync(source, rid, null, download: true, copy: false, force: false, skipExistingOutput: false, cancellationToken); + await DeployAsync(source, tag, rid, null, download: true, copy: false, force: false, skipExistingOutput: false, cancellationToken); } catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { @@ -60,11 +76,11 @@ public static async Task DownloadAllAsync(string source, string? output, bool co if (copy) { - await DeployAsync(source, null, output, download: false, copy: true, force, skipExistingOutput: false, cancellationToken); + await DeployAsync(source, tag, null, output, download: false, copy: true, force, skipExistingOutput: false, cancellationToken); } } - public static async Task DeployAsync(string source, string? rid, string? output, bool download, bool copy, + public static async Task DeployAsync(string source, string tag, string? rid, string? output, bool download, bool copy, bool force, bool skipExistingOutput, CancellationToken cancellationToken) { var (repository, downloadBase) = GetSource(source); @@ -84,8 +100,8 @@ public static async Task DeployAsync(string source, string? rid, string? output, return; } + tag = NormalizeVersion(tag); var cache = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "BootstrapBlazor", "mediamtx"); - var tag = "v1.22.0"; var toolRoot = Path.Combine(Path.GetFullPath(cache), tag); Directory.CreateDirectory(toolRoot); await using var cacheLock = await AcquireCacheLockAsync(Path.Combine(toolRoot, $"{rid}.lock"), cancellationToken); @@ -333,6 +349,9 @@ private static string ValidateHash(string value) [GeneratedRegex("^[0-9a-fA-F]{64}$", RegexOptions.CultureInvariant)] private static partial Regex Sha256Regex(); + [GeneratedRegex("^v\\d+\\.\\d+\\.\\d+(-[0-9A-Za-z.-]+)?$", RegexOptions.CultureInvariant)] + private static partial Regex VersionRegex(); + private static async Task GetHashAsync(string path, CancellationToken cancellationToken) { await using var stream = File.OpenRead(path); diff --git a/tools/BootstrapBlazor.MediaMtx.Cli/Program.cs b/tools/BootstrapBlazor.MediaMtx.Cli/Program.cs index 8c4311dc..e18d2b71 100644 --- a/tools/BootstrapBlazor.MediaMtx.Cli/Program.cs +++ b/tools/BootstrapBlazor.MediaMtx.Cli/Program.cs @@ -8,13 +8,16 @@ if (args.Contains("--help") || args.Contains("-h")) { await Console.Out.WriteLineAsync(""" - Usage: bb-mediamtx [--download] [--copy] [--source ] [--rid ] [--output ] [--force] + Usage: bb-mediamtx [--download [version]] [--copy] [--source ] [--rid ] [--output ] [--force] bb-mediamtx --help bb-mediamtx -h Without --download or --copy, copy missing files from the cache. - --download Verify the cache for the selected RID; download from the selected source if missing + --download [version] + Verify the cache for the selected RID; download from the selected source if missing or invalid. Shows the download URL and progress. Does not copy by itself. + Omitted or empty uses v1.22.0; the leading 'v' may be omitted. Each version + has its own cache directory. --copy Copy the executable and license from the verified cache without downloading. Can be combined with --download. --source Select github.com (default), gitcode.com or gitee.com for downloads. @@ -39,6 +42,7 @@ cannot be repeated. Examples: bb-mediamtx --download + bb-mediamtx --download v1.22.0 bb-mediamtx --download --source gitcode.com bb-mediamtx --download --source gitee.com bb-mediamtx --download --rid all @@ -65,6 +69,7 @@ cannot be repeated. var force = false; var download = false; var copy = false; + var version = MediaMtxInstaller.DefaultVersion; for (var index = 0; index < args.Length; index++) { switch (args[index]) @@ -74,6 +79,14 @@ cannot be repeated. break; case "--download" when !download: download = true; + if (index + 1 < args.Length && !args[index + 1].StartsWith("--", StringComparison.Ordinal)) + { + var versionArgument = args[++index].Trim(); + if (versionArgument.Length > 0) + { + version = MediaMtxInstaller.NormalizeVersion(versionArgument); + } + } break; case "--copy" when !copy: copy = true; @@ -108,7 +121,7 @@ cannot be repeated. output = args[++index]; break; default: - throw new ArgumentException("Usage: bb-mediamtx [--download] [--copy] [--source ] [--rid ] [--output ] [--force]."); + throw new ArgumentException("Usage: bb-mediamtx [--download [version]] [--copy] [--source ] [--rid ] [--output ] [--force]."); } } if (download && !copy && force) @@ -120,11 +133,11 @@ cannot be repeated. if (download && rid == "all") { cancellation.CancelAfter(TimeSpan.FromMinutes(30)); - await MediaMtxInstaller.DownloadAllAsync(source, output, copy, force, cancellation.Token); + await MediaMtxInstaller.DownloadAllAsync(source, version, output, copy, force, cancellation.Token); } else { - await MediaMtxInstaller.DeployAsync(source, rid, output, download, copy || defaultAction, force, + await MediaMtxInstaller.DeployAsync(source, version, rid, output, download, copy || defaultAction, force, skipExistingOutput: !download, cancellationToken: cancellation.Token); } } diff --git a/tools/BootstrapBlazor.MediaMtx.Cli/readme.md b/tools/BootstrapBlazor.MediaMtx.Cli/readme.md index 596ee3ef..fd1815cc 100644 --- a/tools/BootstrapBlazor.MediaMtx.Cli/readme.md +++ b/tools/BootstrapBlazor.MediaMtx.Cli/readme.md @@ -40,7 +40,7 @@ dotnet bb-mediamtx --help ## 参数 ```text -bb-mediamtx [--download] [--copy] [--source ] [--rid ] [--output ] [--force] +bb-mediamtx [--download [version]] [--copy] [--source ] [--rid ] [--output ] [--force] bb-mediamtx --help ``` @@ -48,7 +48,7 @@ bb-mediamtx --help | 参数 | 说明 | | --- | --- | -| `--download` | 检查目标 RID 的缓存,缺失或校验失败时从指定源下载并校验;不单独复制到输出目录。下载时显示资源地址和进度。 | +| `--download [version]` | 检查目标 RID 的缓存,缺失或校验失败时从指定源下载并校验;不单独复制到输出目录。下载时显示资源地址和进度。可选的 `version` 指定 MediaMTX 版本,省略或为空时使用默认值 `v1.22.0`;可省略前导 `v`,每个版本使用独立缓存目录。 | | `--copy` | 从已校验的缓存复制工具和许可证,不联网;缓存缺失或损坏时输出警告。可与 `--download` 组合。 | | `--source ` | 下载源,只接受 `github.com`(默认)、`gitcode.com` 或 `gitee.com`。三者共用缓存;设置此参数不自动下载、不强制刷新缓存,也不在源之间自动回退。 | | `--rid ` | 指定目标平台。省略或显式传入空字符串时使用当前操作系统和架构。`--download --rid all` 准备全部支持平台;与 `--copy` 组合时只复制本机平台。`all` 不能单独用于复制。 | @@ -71,6 +71,13 @@ bb-mediamtx --download bb-mediamtx --copy --output .\publish ``` +下载指定版本: + +```powershell +bb-mediamtx --download v1.22.0 +bb-mediamtx --download v1.22.0 --copy --rid linux-x64 --output .\publish +``` + 使用 GitCode 或 Gitee 下载并复制: ```powershell @@ -102,7 +109,7 @@ bb-mediamtx --copy --rid linux-arm64 --output .\publish --force ## 缓存和故障排查 -CLI 固定使用 MediaMTX `1.22.0`,缓存位于本机应用数据目录下的 `BootstrapBlazor\mediamtx\v1.22.0\`,不同下载源、构建和发布共用。下载通过所选源的发布 API 获取同名附件及 SHA-256,校验后只提取可执行文件和许可证,不使用压缩包中的 `mediamtx.yml`。有效缓存会校验后复用;下载进度在可获取文件大小时显示百分比,否则显示已下载字节数。 +CLI 默认使用 MediaMTX `v1.22.0`,可通过 `--download ` 指定其他版本;省略版本时使用该默认值。缓存位于本机应用数据目录下的 `BootstrapBlazor\mediamtx\\`,按版本区分,不同下载源、构建和发布共用。下载通过所选源的发布 API 获取同名附件及 SHA-256,校验后只提取可执行文件和许可证,不使用压缩包中的 `mediamtx.yml`。有效缓存会校验后复用;下载进度在可获取文件大小时显示百分比,否则显示已下载字节数。 GitHub 使用 `bluenviron/mediamtx`,GitCode 和 Gitee 使用 `LongbowEnterprise/mediamtx`。所选源必须提供目标平台附件和 SHA-256;缺失时输出警告,不自动切换到其他源。`--copy` 即使指定下载源也不会联网。 @@ -126,7 +133,7 @@ dotnet bb-mediamtx --copy --rid linux-x64 --output .\publish 从 MSBuild 调用时,若 `MediaMtxRidArgument` 只包含 RID 值,可使用 `--rid "$(MediaMtxRidArgument)"`;属性为空时使用本机 RID。Build 和 Publish 输出目录需通过 `--output` 显式指定,例如 `--output "$([MSBuild]::EnsureTrailingSlash('$(TargetDir)'))."` 或 `--output "$([MSBuild]::EnsureTrailingSlash('$(PublishDir)'))."`。末尾的 `.` 可避免 Windows 目录末尾反斜杠转义结束引号。宿主维护的构建规则应跳过 IDE 设计时构建。 -`dotnet publish --no-build` 仍可通过宿主的 Publish Target 调用 CLI;单文件发布时工具仍作为外部文件保留。升级 CLI 固定的 MediaMTX 版本后,已有输出文件不会自动覆盖,应使用 `--force`。 +`dotnet publish --no-build` 仍可通过宿主的 Publish Target 调用 CLI;单文件发布时工具仍作为外部文件保留。切换 MediaMTX 版本(升级 CLI 默认版本或使用 `--download `)后,已有输出文件不会自动覆盖,应使用 `--force`。 ## 源码开发