Skip to content

Instantly share code, notes, and snippets.

@wtrsltnk
Created May 1, 2024 17:57
Show Gist options
  • Save wtrsltnk/9a50d0824b5e5b82d96d718003e6fc47 to your computer and use it in GitHub Desktop.
Save wtrsltnk/9a50d0824b5e5b82d96d718003e6fc47 to your computer and use it in GitHub Desktop.
An example console app in C# looping a ogg file to a Shout cast server
using System.Net.Sockets;
using System.Text;
namespace ConsoleApp1
{
internal class Program
{
private const string clientId = "source";
private const string clientSecret = "hackme";
private const string clientName = "😂 stream";
private const string clientDescription = "This is just a simple 😀 stream";
private const string clientUrl = "http://example.org";
private const string clientGenre = "😎";
static void Main(string[] args)
{
using Stream s = File.OpenRead(args.First());
var client = new TcpClient();
client.Connect("localhost", 8000);
var authenticationString = $"{clientId}:{clientSecret}";
var base64EncodedAuthenticationString = Convert
.ToBase64String(ASCIIEncoding.ASCII.GetBytes(authenticationString));
var request = $@"PUT /stream.ogg HTTP/1.1
Host: localhost:8000
Authorization: Basic {base64EncodedAuthenticationString}
Accept: */*
Transfer-Encoding: chunked
Content-Type: audio/ogg
Ice-Public: 1
Ice-Name: {clientName}
Ice-Description: {clientDescription}
Ice-URL: {clientUrl}
Ice-Genre: {clientGenre}
Expect: 100-continue
";
using var stream = client.GetStream();
stream.Write(Encoding.UTF8.GetBytes(request));
byte[] buffer = new byte[4096];
var read = stream.Read(buffer);
var response = Encoding.UTF8.GetString(buffer);
Console.WriteLine(response.Trim());
if (!response.StartsWith("HTTP/1.1 100 Continue"))
{
Console.WriteLine("Unexpected response:");
Console.WriteLine(response);
return;
}
do
{
byte[] fileBuffer = new byte[4096];
var fileRead = s.Read(fileBuffer);
if (fileRead == 0)
{
Console.WriteLine("Restarting song");
s.Position = 0;
fileRead = s.Read(fileBuffer);
}
stream.Write(fileBuffer, 0, fileRead);
Thread.Sleep(50);
} while (true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment