-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (115 loc) · 4.92 KB
/
Copy pathProgram.cs
File metadata and controls
137 lines (115 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Diagnostics;
using RtspGStreamerLib;
namespace RtspGStreamerExample;
/// <summary>
/// Exemplo de uso com salvamento de imagens usando SkiaSharp
/// Performance otimizada para Khadas VIM3 (ARM64), Windows e Linux
/// </summary>
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== RTSP Frame Capture + Image Processing (SkiaSharp) ===\n");
if (args.Length == 0)
{
Console.WriteLine("Uso:");
Console.WriteLine(" dotnet run <rtsp-url> [hw-accel] [save-interval]");
Console.WriteLine();
Console.WriteLine("Parâmetros:");
Console.WriteLine(" rtsp-url - URL do stream RTSP");
Console.WriteLine(" hw-accel - 1 para hardware accel (ARM64), 0 para software (padrão)");
Console.WriteLine(" save-interval - Salvar 1 frame a cada N segundos (padrão: 5)");
Console.WriteLine();
Console.WriteLine("Exemplos:");
Console.WriteLine(" dotnet run rtsp://admin:senha@192.168.0.124:554/live/0/main");
Console.WriteLine(" dotnet run rtsp://admin:senha@192.168.0.124:554/live/0/main 1 2");
return;
}
string rtspUrl = args[0];
bool useHwAccel = args.Length > 1 && args[1] == "1";
int saveIntervalSeconds = args.Length > 2 ? int.Parse(args[2]) : 5;
Console.WriteLine($"URL: {rtspUrl}");
Console.WriteLine($"Hardware Acceleration: {(useHwAccel ? "Sim (v4l2h265dec)" : "Não (avdec_h265)")}");
Console.WriteLine($"Save Interval: A cada {saveIntervalSeconds} segundos\n");
// Criar diretório para frames
string outputDir = "frames";
Directory.CreateDirectory(outputDir);
Console.WriteLine($"Frames serão salvos em: {Path.GetFullPath(outputDir)}/\n");
// Inicializar GStreamer
Console.WriteLine("Inicializando GStreamer...");
RtspFrameCapture.Initialize();
Console.WriteLine("[OK] GStreamer inicializado\n");
// Criar captura
using var capture = new RtspFrameCapture();
// Estatísticas
int frameCount = 0;
int savedFrames = 0;
var sw = Stopwatch.StartNew();
var lastSaveTime = sw.Elapsed;
var lastFpsUpdate = sw.Elapsed;
// Callback de frame recebido
capture.OnFrameReceived += (frame) =>
{
frameCount++;
// Salvar frame periodicamente
if ((sw.Elapsed - lastSaveTime).TotalSeconds >= saveIntervalSeconds)
{
savedFrames++;
string fileName = $"frame_{savedFrames:D5}_{DateTime.Now:yyyyMMdd_HHmmss}.jpg";
string filePath = Path.Combine(outputDir, fileName);
try
{
// MÉTODO 1: Salvar JPEG simples (mais rápido)
ImageHelper.SaveAsJpeg(frame, filePath, quality: 85);
Console.WriteLine($"💾 Frame salvo: {fileName} ({frame.Width}x{frame.Height})");
}
catch (Exception ex)
{
Console.WriteLine($"❌ Erro ao salvar frame: {ex.Message}");
}
lastSaveTime = sw.Elapsed;
}
// Mostrar FPS a cada segundo
if ((sw.Elapsed - lastFpsUpdate).TotalSeconds >= 1.0)
{
double fps = frameCount / sw.Elapsed.TotalSeconds;
Console.WriteLine($"📊 Frame #{frameCount} | FPS: {fps:F1} | Salvos: {savedFrames} | " +
$"PTS: {TimeSpan.FromTicks(frame.StreamTimestampTicks).TotalSeconds:F3}s");
lastFpsUpdate = sw.Elapsed;
}
};
// Callback de erro
capture.OnError += (error) =>
{
Console.WriteLine($"[ERRO] {error}");
};
// Iniciar captura
Console.WriteLine("Iniciando captura...\n");
if (!capture.Start(rtspUrl, useHwAccel))
{
Console.WriteLine("Falha ao iniciar captura!");
return;
}
Console.WriteLine("✅ Captura iniciada! Pressione Ctrl+C para parar\n");
// Aguardar Ctrl+C
var exitEvent = new ManualResetEvent(false);
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
Console.WriteLine("\n\n⏹️ Parando captura...");
exitEvent.Set();
};
exitEvent.WaitOne();
// Parar captura
capture.Stop();
// Estatísticas finais
sw.Stop();
double avgFps = frameCount / sw.Elapsed.TotalSeconds;
Console.WriteLine();
Console.WriteLine("=== Estatísticas Finais ===");
Console.WriteLine($"Total de frames recebidos: {frameCount}");
Console.WriteLine($"Total de frames salvos: {savedFrames}");
Console.WriteLine($"Tempo total: {sw.Elapsed:mm\\:ss}");
Console.WriteLine($"FPS médio: {avgFps:F2}");
}
}