using System; using System.Diagnostics; using System.Text; using System.Threading; using System.Threading.Tasks; if (!OperatingSystem.IsWindows()) throw new PlatformNotSupportedException("This example requires Windows."); if (args.Length != 1) throw new ArgumentException("Usage: StarBondingLauncher ACCOUNT_ID"); string executable = @"C:\Program Files\StarBonding\starbonding-cli.exe"; string account = args[0]; string password = ReadPassword(); string stopName = @"Local\StarBonding.OEM." + Guid.NewGuid().ToString("N"); using EventWaitHandle stopEvent = new(false, EventResetMode.ManualReset, stopName); var start = new ProcessStartInfo { FileName = executable, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }; start.ArgumentList.Add("--username"); start.ArgumentList.Add(account); start.ArgumentList.Add("--password-stdin"); start.ArgumentList.Add("--stop-event"); start.ArgumentList.Add(stopName); using var process = Process.Start(start) ?? throw new InvalidOperationException("Unable to start StarBonding CLI."); await process.StandardInput.WriteLineAsync(password); process.StandardInput.Close(); password = string.Empty; Console.CancelKeyPress += (_, eventArgs) => { eventArgs.Cancel = true; stopEvent.Set(); }; bool publisherStarted = false; Task stdoutTask = Task.Run(async () => { while (await process.StandardOutput.ReadLineAsync() is { } line) { Console.WriteLine(line); if (!publisherStarted && line.StartsWith("EVENT READY ", StringComparison.Ordinal)) { publisherStarted = true; Console.WriteLine("StarBonding is ready. Start the RTMP publisher."); // Start your publisher once here. Publish to: // rtmp://127.0.0.1:1935/live/your-stream-key } } }); Task stderrTask = Task.Run(async () => { while (await process.StandardError.ReadLineAsync() is { } line) Console.Error.WriteLine(line); }); await process.WaitForExitAsync(); await Task.WhenAll(stdoutTask, stderrTask); if (process.ExitCode != 0) throw new InvalidOperationException( $"StarBonding exited with code {process.ExitCode}."); static string ReadPassword() { var password = new StringBuilder(); for (;;) { ConsoleKeyInfo key = Console.ReadKey(intercept: true); if (key.Key == ConsoleKey.Enter) { Console.WriteLine(); return password.ToString(); } if (key.Key == ConsoleKey.Backspace) { if (password.Length != 0) password.Length--; continue; } if (!char.IsControl(key.KeyChar)) password.Append(key.KeyChar); } }