Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <version>`(省略时使用默认版本)。CLI 不修改工程或创建构建规则,运行时不下载工具。
- `Gateway:MediaMtxPath` 默认指向应用目录的 `mediamtx-tools\<RID>\mediamtx.exe`(Windows)或 `mediamtx`;支持 Windows x64、Linux x64/ARM64/ARMv7、macOS x64/ARM64。CLI 需要 .NET 11。
- CLI 的 `--copy` 只使用缓存;`--force` 覆盖输出工具和许可证,不强制下载。失败以警告报告,不能仅凭退出码判断部署成功。

Expand Down
29 changes: 24 additions & 5 deletions tools/BootstrapBlazor.MediaMtx.Cli/MediaMtxInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
{
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Malformed tags reach release lookup

When the supplied version has an invalid prerelease suffix, such as a trailing hyphen, NormalizeVersion accepts malformed prerelease tags such as v1.22.0- because the optional suffix permits a lone hyphen; DeployAsync then queries the release API and reports a download failure instead of rejecting the version during validation.

Validate prerelease identifiers so the suffix cannot be empty or contain empty dot-separated identifiers.

private static partial Regex VersionRegex();

private static async Task<string> GetHashAsync(string path, CancellationToken cancellationToken)
{
await using var stream = File.OpenRead(path);
Expand Down
23 changes: 18 additions & 5 deletions tools/BootstrapBlazor.MediaMtx.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
if (args.Contains("--help") || args.Contains("-h"))
{
await Console.Out.WriteLineAsync("""
Usage: bb-mediamtx [--download] [--copy] [--source <source>] [--rid <rid>] [--output <directory>] [--force]
Usage: bb-mediamtx [--download [version]] [--copy] [--source <source>] [--rid <rid>] [--output <directory>] [--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 <source> Select github.com (default), gitcode.com or gitee.com for downloads.
Expand All @@ -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
Expand 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])
Expand All @@ -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);
Comment on lines +82 to +87

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Version selection cannot be published

When the CLI is packaged or published without overriding its project version, the CLI project still declares package version 11.0.4, and dotnet-tools.json pins that same version, so publishing this change under the current version cannot deliver the new option to users; the repository README also says same-version packages are immutable.

Increment the CLI package version and update the pinned version in dotnet-tools.json.

}
}
break;
case "--copy" when !copy:
copy = true;
Expand Down Expand Up @@ -108,7 +121,7 @@ cannot be repeated.
output = args[++index];
break;
default:
throw new ArgumentException("Usage: bb-mediamtx [--download] [--copy] [--source <source>] [--rid <rid>] [--output <directory>] [--force].");
throw new ArgumentException("Usage: bb-mediamtx [--download [version]] [--copy] [--source <source>] [--rid <rid>] [--output <directory>] [--force].");
}
}
if (download && !copy && force)
Expand All @@ -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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Deployment files come from different versions

When two CLI processes copy different selected versions to the same output directory, or a copy stops after replacing only one file, CopyToolsAsync moves the executable and license separately without an output-level lock or pair-level commit, so the output directory ends up with files from different versions and a later build sees an inconsistent deployment.

Protect both file replacements with a shared output-level lock and commit them as a pair.

Also at tools/BootstrapBlazor.MediaMtx.Cli/MediaMtxInstaller.cs:103.

skipExistingOutput: !download, cancellationToken: cancellation.Token);
}
}
Expand Down
15 changes: 11 additions & 4 deletions tools/BootstrapBlazor.MediaMtx.Cli/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ dotnet bb-mediamtx --help
## 参数

```text
bb-mediamtx [--download] [--copy] [--source <source>] [--rid <rid>] [--output <directory>] [--force]
bb-mediamtx [--download [version]] [--copy] [--source <source>] [--rid <rid>] [--output <directory>] [--force]
bb-mediamtx --help
```

参数顺序不限;参数名、下载源和 RID 区分大小写,动作及其选项不能重复。包含 `--help` 或 `-h` 时只显示帮助。

| 参数 | 说明 |
| --- | --- |
| `--download` | 检查目标 RID 的缓存,缺失或校验失败时从指定源下载并校验;不单独复制到输出目录。下载时显示资源地址和进度。 |
| `--download [version]` | 检查目标 RID 的缓存,缺失或校验失败时从指定源下载并校验;不单独复制到输出目录。下载时显示资源地址和进度。可选的 `version` 指定 MediaMTX 版本,省略或为空时使用默认值 `v1.22.0`;可省略前导 `v`,每个版本使用独立缓存目录。 |
| `--copy` | 从已校验的缓存复制工具和许可证,不联网;缓存缺失或损坏时输出警告。可与 `--download` 组合。 |
| `--source <source>` | 下载源,只接受 `github.com`(默认)、`gitcode.com` 或 `gitee.com`。三者共用缓存;设置此参数不自动下载、不强制刷新缓存,也不在源之间自动回退。 |
| `--rid <rid>` | 指定目标平台。省略或显式传入空字符串时使用当前操作系统和架构。`--download --rid all` 准备全部支持平台;与 `--copy` 组合时只复制本机平台。`all` 不能单独用于复制。 |
Expand 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
Expand Down Expand Up @@ -102,7 +109,7 @@ bb-mediamtx --copy --rid linux-arm64 --output .\publish --force

## 缓存和故障排查

CLI 固定使用 MediaMTX `1.22.0`,缓存位于本机应用数据目录下的 `BootstrapBlazor\mediamtx\v1.22.0\<RID>`,不同下载源、构建和发布共用。下载通过所选源的发布 API 获取同名附件及 SHA-256,校验后只提取可执行文件和许可证,不使用压缩包中的 `mediamtx.yml`。有效缓存会校验后复用;下载进度在可获取文件大小时显示百分比,否则显示已下载字节数。
CLI 默认使用 MediaMTX `v1.22.0`,可通过 `--download <version>` 指定其他版本;省略版本时使用该默认值。缓存位于本机应用数据目录下的 `BootstrapBlazor\mediamtx\<version>\<RID>`,按版本区分,不同下载源、构建和发布共用。下载通过所选源的发布 API 获取同名附件及 SHA-256,校验后只提取可执行文件和许可证,不使用压缩包中的 `mediamtx.yml`。有效缓存会校验后复用;下载进度在可获取文件大小时显示百分比,否则显示已下载字节数。

GitHub 使用 `bluenviron/mediamtx`,GitCode 和 Gitee 使用 `LongbowEnterprise/mediamtx`。所选源必须提供目标平台附件和 SHA-256;缺失时输出警告,不自动切换到其他源。`--copy` 即使指定下载源也不会联网。

Expand All @@ -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 <version>`)后,已有输出文件不会自动覆盖,应使用 `--force`。

## 源码开发

Expand Down
Loading