using Newtonsoft.Json; using Nostr.Client.Json; using Nostr.Client.Keys; using Nostr.Client.Messages; using NostrStreamer.Database; namespace NostrStreamer; public static class Extensions { public static NostrEvent? GetEvent(this UserStream us) { return JsonConvert.DeserializeObject(us.Event, NostrSerializer.Settings); } public static string GetPubKey(this Config cfg) { return NostrPrivateKey.FromBech32(cfg.PrivateKey).DerivePublicKey().Hex; } public static List GetVariants(this IngestEndpoint ep) { return ep.Capabilities .Where(a => a.StartsWith("variant")) .Select(Variant.FromString).ToList(); } } public class Variant { public int Width { get; init; } public int Height { get; init; } public int Bandwidth { get; init; } public string SourceName => Bandwidth == int.MaxValue ? "source" : $"{Height}h"; /// /// variant:{px}h:{bandwidth} /// /// /// /// public static Variant FromString(string str) { if (str.Equals("variant:source", StringComparison.InvariantCultureIgnoreCase)) { return new() { Width = 0, Height = 0, Bandwidth = int.MaxValue }; } var strSplit = str.Split(":"); if (strSplit.Length != 3 || !int.TryParse(strSplit[1][..^1], out var h) || !int.TryParse(strSplit[2], out var b)) { throw new FormatException(); } return new() { Height = h, Width = (int)Math.Ceiling(h / 9m * 16m), Bandwidth = b }; } public override string ToString() { if (Bandwidth == int.MaxValue) { return "variant:source"; } return $"variant:{SourceName}:{Bandwidth}"; } public string ToResolutionArg() { if (Bandwidth == int.MaxValue) { return string.Empty; } return $"RESOLUTION={Width}x{Height}"; } public string ToBandwidthArg() { if (Bandwidth == int.MaxValue) { return string.Empty; } return $"BANDWIDTH={Bandwidth * 1000}"; } }