Skip to content

Commit

Permalink
GetMaskedCredentialUrl
Browse files Browse the repository at this point in the history
Signed-off-by: Johnson Shih <[email protected]>
  • Loading branch information
johnsonshih committed Sep 2, 2023
1 parent 23939c1 commit 3e1a6c4
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions samples/brokers/onvif-video-broker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class CameraService : Camera.Camera.CameraBase
public override Task<NotifyResponse> GetFrame(
NotifyRequest request, ServerCallContext context)
{
// Mask credential information in Program.RtspUrl to prevent the credential info shown in console output
var rtspUrlWithMaskedCredential = RtspUrlHelper.GetMaskedCredentialUrl(Program.RtspUrl);
byte[] frame = null;
lock (Program.Frames)
{
Expand All @@ -32,17 +34,17 @@ public override Task<NotifyResponse> GetFrame(

if (frame == null)
{
Console.WriteLine("No frame available for {0}", Program.RtspUrl.MaskedUrl);
Console.WriteLine("No frame available for {0}", rtspUrlWithMaskedCredential);
}
else
{
Console.WriteLine("Sending frame for {0}, Q size: {1}", Program.RtspUrl.MaskedUrl, Program.Frames.Count);
Console.WriteLine("Sending frame for {0}, Q size: {1}", rtspUrlWithMaskedCredential, Program.Frames.Count);
}
}

return Task.FromResult(new NotifyResponse
{
Camera = Program.RtspUrl.Url,
Camera = Program.RtspUrl,
Frame = (frame == null ? Google.Protobuf.ByteString.Empty : Google.Protobuf.ByteString.CopyFrom(frame))
});
}
Expand Down Expand Up @@ -73,19 +75,8 @@ public T Pop()
}
}

public class RtspUrlContent {
private readonly string rtspUrl;
private readonly string maskedRtspUrl;

public RtspUrlContent(string rtspUrl) {
this.rtspUrl = rtspUrl;
this.maskedRtspUrl = GetMaskedUrl(rtspUrl);
}

public string Url { get { return rtspUrl; } }
public string MaskedUrl { get { return maskedRtspUrl; } }

private string GetMaskedUrl(string rtspUrl) {
public static class RtspUrlHelper {
public static string GetMaskedCredentialUrl(string rtspUrl) {
const string rtspPrefix = "rtsp://";
var maskedRtspUrl = rtspUrl;
if (rtspUrl.StartsWith(rtspPrefix)) {
Expand All @@ -102,7 +93,7 @@ private string GetMaskedUrl(string rtspUrl) {
class Program
{
public static Task FrameTask;
public static RtspUrlContent RtspUrl;
public static string RtspUrl;
public static LimitedSizeStack<byte[]> Frames;

static void Main(string[] args)
Expand All @@ -115,15 +106,15 @@ static void Main(string[] args)
throw new ArgumentNullException("Unable to create Frames");
}

var rtspUrl = Environment.GetEnvironmentVariable("RTSP_URL");
if (string.IsNullOrEmpty(rtspUrl)) {
rtspUrl = Akri.Akri.GetRtspUrl();
RtspUrl = Environment.GetEnvironmentVariable("RTSP_URL");
if (string.IsNullOrEmpty(RtspUrl)) {
RtspUrl = Akri.Akri.GetRtspUrl();
}
if (string.IsNullOrEmpty(rtspUrl))
if (string.IsNullOrEmpty(RtspUrl))
{
throw new ArgumentNullException("Unable to find RTSP URL");
}
RtspUrl = new RtspUrlContent(rtspUrl);

CamerasCounter.Inc();

FrameTask = Task.Run(() => Process(RtspUrl));
Expand Down Expand Up @@ -153,13 +144,15 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
"camera_disconnects",
"Number of times camera connection had to be restablished.");

static void Process(RtspUrlContent videoPath)
static void Process(string videoPath)
{
Console.WriteLine($"[VideoProcessor] Processing RTSP stream: {videoPath.MaskedUrl}");
// Mask credential information in videoPath to prevent the credential info shown in console output
var videoPathWithMaskedCredential = RtspUrlHelper.GetMaskedCredentialUrl(videoPath);
Console.WriteLine($"[VideoProcessor] Processing RTSP stream: {videoPathWithMaskedCredential}");

while (true)
{
var capture = new VideoCapture(videoPath.Url);
var capture = new VideoCapture(videoPath);
Console.WriteLine("Ready " + capture.IsOpened());

using (var image = new Mat()) // Frame image buffer
Expand All @@ -172,7 +165,7 @@ static void Process(RtspUrlContent videoPath)
var imageBytes = image.ToBytes();
Frames.Push(imageBytes);
JobsInQueue.Set(Frames.Count);
Console.WriteLine("Adding frame from {0}, Q size: {1}, frame size: {2}", videoPath.MaskedUrl, Program.Frames.Count, imageBytes.Length);
Console.WriteLine("Adding frame from {0}, Q size: {1}, frame size: {2}", videoPathWithMaskedCredential, Program.Frames.Count, imageBytes.Length);
}
}
}
Expand Down

0 comments on commit 3e1a6c4

Please sign in to comment.