From e7d04b55b3ba48d43bda9f012bd9524b46fcd359 Mon Sep 17 00:00:00 2001 From: Michal Buzik Date: Wed, 22 Jul 2026 10:02:22 +0200 Subject: [PATCH] Live Stream Operations: Add StartLiveStream to CJMS11 camera --- TBF/Rig/Network/Camera/CJMS11/Camera.cs | 52 +++++++++++++-- TBF/Rig/Network/Camera/CJMS11/LiveStreamOp.cs | 66 +++++++++++-------- 2 files changed, 85 insertions(+), 33 deletions(-) diff --git a/TBF/Rig/Network/Camera/CJMS11/Camera.cs b/TBF/Rig/Network/Camera/CJMS11/Camera.cs index 8afcb4958..8aec7e28c 100644 --- a/TBF/Rig/Network/Camera/CJMS11/Camera.cs +++ b/TBF/Rig/Network/Camera/CJMS11/Camera.cs @@ -2337,6 +2337,49 @@ namespace TBF.Rig.Network.Camera.CJMS11 } } + /// + /// pouziva LiveStreamOp - pre nahlad + /// + /// + public void StartLiveStream(bool hiResolution) + { + if (CameraCfg.DebugLevel == DebugMode.Simulate) + return; + + StartLiveStreamListener(); + } + + /// + /// pouziva LiveStreamOp - pre ukoncenie nahladu + /// + public void StopLiveStream() + { + if (CameraCfg.DebugLevel == DebugMode.Simulate) + return; + + try + { + liveCts?.Cancel(); + + if (liveStreamTask != null) + { + try + { + liveStreamTask.Wait(1000); + } + catch (AggregateException) + { + // expected after cancellation + } + } + } + catch (Exception ex) + { + log.Error($"{Name}: StopLiveStream failed.", ex); + } + } + + /// /// listener for multiple - repetetive image receiving /// @@ -2345,12 +2388,7 @@ namespace TBF.Rig.Network.Camera.CJMS11 /// /// /// - - // private void StartLiveStreamListener(IPAddress localIP, int localPort) - // { - // liveStreamListenerThread = new Thread(new ThreadStart(LiveStreamListener)); - // liveStreamListenerThread.Start(); - // } + private CancellationTokenSource liveCts; private Task liveStreamTask; @@ -2662,7 +2700,7 @@ namespace TBF.Rig.Network.Camera.CJMS11 //string command = string.Format("~/userland/build/bin/raspivid -t 0 -cd MJPEG -w {0} -h {1} -fps 30 -b 8000000 -o - | gst-launch-1.0 fdsrc ! \"image/jpeg,framerate=30/1\" ! jpegparse ! rtpjpegpay ! udpsink host={2} port={3}", // wid, hgh, NetAdapter.IPAddress, rtpUdpLocalPort); - return new LiveStreamOp(this, command); + return new LiveStreamOp(this, hiRes); } diff --git a/TBF/Rig/Network/Camera/CJMS11/LiveStreamOp.cs b/TBF/Rig/Network/Camera/CJMS11/LiveStreamOp.cs index b150e46a2..fa7aec2cb 100644 --- a/TBF/Rig/Network/Camera/CJMS11/LiveStreamOp.cs +++ b/TBF/Rig/Network/Camera/CJMS11/LiveStreamOp.cs @@ -10,58 +10,72 @@ namespace TBF.Rig.Network.Camera.CJMS11 { private static readonly ILog log = LogManager.GetLogger(typeof(LiveStreamOp)); - readonly Camera camera; - readonly Telnet.TelnetClient telnet; - readonly string command; + private readonly Camera camera; + private readonly bool hiResolution; + + private bool started; + private Exception startException; + - bool liveStreamCommandSent; /// /// Events: Event.None or Event.Error /// /// CLP1611.Camera reference - public LiveStreamOp(Camera camera, string command) + public LiveStreamOp(Camera camera, bool hiResolution) { this.camera = camera; - this.command = command; + this.hiResolution = hiResolution; } public void Start() { - liveStreamCommandSent = false; + started = false; + startException = null; + + try + { + camera.StartLiveStream(hiResolution); + started = true; + + log.InfoFormat("{0} Live stream started", + camera.Name); + } + catch (Exception ex) + { + startException = ex; + log.Error("Unable to start live stream.", ex); + } } public Event Run() { - if (camera.CameraCfg.DebugLevel == DebugMode.Simulate) return Event.None; + if (camera.CameraCfg.DebugLevel == DebugMode.Simulate) + return Event.None; - log.DebugFormat("Run(): telnet.State = {0}, liveStreamCommandSent = {1}", telnet.State, liveStreamCommandSent); - - if (!liveStreamCommandSent) - { - if (camera.Running && (telnet.State == TelnetClient.TelnetState.Inactive)) - { - telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.SEND_STRING, command)); - liveStreamCommandSent = true; - log.WarnFormat("{0} IP={1} command={2}", camera.Name, camera.IPAddress, command); - } - } + if (startException != null) + return Event.Error; return Event.None; } public void Stop() { - if (camera.CameraCfg.DebugLevel == DebugMode.Simulate) return; + if (camera.CameraCfg.DebugLevel == DebugMode.Simulate) + return; - log.DebugFormat("Stop(): telnet.State = {0}, liveStreamCommandSent = {1}", telnet.State, liveStreamCommandSent); - - if (liveStreamCommandSent) + try { - telnet.Enqueue(new Telnet.Command(Telnet.CmdAction.SEND_COMMAND, Telnet.TelnetClient.CtrlCCommand)); - log.WarnFormat("{0} IP={1} command=CTRL-C", camera.Name, camera.IPAddress); - } + camera.StopLiveStream(); + + log.InfoFormat("{0} Live stream stopped", + camera.Name); + } + catch (Exception ex) + { + log.Error("Unable to stop live stream.", ex); + } } } }