Skip to content

Instantly share code, notes, and snippets.

@EatonZ
Created November 11, 2023 16:25
Show Gist options
  • Save EatonZ/ecc3aad552950194eeae92b903d19743 to your computer and use it in GitHub Desktop.
Save EatonZ/ecc3aad552950194eeae92b903d19743 to your computer and use it in GitHub Desktop.
Camera image stream parsing code.
//https://eaton-works.com/2023/11/14/telecom-camera-hack/
using ServiceStack.Text;
namespace CameraStreamApp;
public sealed partial class MainForm : Form
{
private readonly Dictionary<string, PictureBox> cameras = new();
public MainForm() => InitializeComponent();
private sealed class CameraUpdate
{
public string ip { get; set; }
public string image_name { get; set; }
public string raw_image { get; set; }
public string camera_no { get; set; }
}
private async void MainForm_Shown(object sender, EventArgs e)
{
var client = new HttpClient();
using (var streamReader = new StreamReader(await client.GetStreamAsync("https://a-telecom-company.com/api/events/incident-events")))
{
while (true)
{
//Each stream line contains a new live image.
var message = await streamReader.ReadLineAsync();
if (string.IsNullOrEmpty(message)) continue;
//Clean up the JSON a bit and then deserialize.
var cu = JsonSerializer.DeserializeFromString<CameraUpdate>(message[7..^1].Replace("\\\"", "\"").Replace("\\n", string.Empty));
if (cameras.TryGetValue(cu.camera_no, out var camera))
{
//An existing camera has been found. Update the picture box in the UI with the latest live image.
if (string.IsNullOrEmpty(cu.raw_image)) continue;
using (var ms = new MemoryStream(Convert.FromBase64String(cu.raw_image)))
{
camera.Image = Image.FromStream(ms);
}
}
else
{
//A new camera has been found. Add a picture box in the UI for it.
var pb = new PictureBox { Width = 400, Height = 300, Padding = new Padding(10), Dock = DockStyle.Top };
cameras.Add(cu.camera_no, pb);
CamerasPanel.Controls.Add(pb);
Text = $"A Telecom Company Video ({cameras.Count} Cameras)";
if (string.IsNullOrEmpty(cu.raw_image)) continue;
using (var ms = new MemoryStream(Convert.FromBase64String(cu.raw_image)))
{
pb.Image = Image.FromStream(ms);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment