forked from sipsorcery-org/sipsorcery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
179 lines (159 loc) · 7.14 KB
/
Program.cs
File metadata and controls
179 lines (159 loc) · 7.14 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using SIPSorceryMedia.Abstractions;
using SIPSorceryMedia.FFmpeg;
namespace FFmpegConsoleApp
{
class Program
{
// A valid to a video file - usefull if you want to test streaming of a video file
// It's alo possible to set a remote file
const String VIDEO_FILE_PATH = @"https://upload.wikimedia.org/wikipedia/commons/3/36/Cosmos_Laundromat_-_First_Cycle_-_Official_Blender_Foundation_release.webm";
//const String VIDEO_FILE_PATH = @"C:\media\big_buck_bunny.mp4";
//const String VIDEO_FILE_PATH = @"C:\media\Armello_Trailer.webm";
static private AsciiFrame? asciiFrame = null;
static void Main(string[] args)
{
VideoCodecsEnum VideoCodec = VideoCodecsEnum.H264;
IVideoSource? videoSource = null;
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.SetMinimumLevel(LogLevel.Debug)
.AddConsole();
});
ILogger logger = loggerFactory.CreateLogger<Program>();
// Initialise FFmpeg librairies
FFmpegInit.Initialise(FfmpegLogLevelEnum.AV_LOG_FATAL, null, logger);
// Get cameras and monitors
List<Camera>? cameras = FFmpegCameraManager.GetCameraDevices();
List<Monitor>? monitors = FFmpegMonitorManager.GetMonitorDevices();
char keyChar = ' ';
while (true)
{
Console.Clear();
if (!(cameras?.Count > 0))
Console.WriteLine("\nNo Camera found ...");
if (!(monitors?.Count > 0))
Console.WriteLine("\nNo Monitor found ...");
Console.WriteLine("\nWhat do you want to use ?");
if (cameras?.Count > 0)
Console.Write("\n [c] - Camera ");
if (monitors?.Count > 0)
Console.Write("\n [m] - Monitor ");
Console.Write($"\n [f] - File - Path:[{VIDEO_FILE_PATH}]");
Console.WriteLine("\n");
Console.Out.Flush();
var keyConsole = Console.ReadKey();
if ( ( (keyConsole.KeyChar == 'c') && (cameras?.Count > 0) )
|| ( (keyConsole.KeyChar == 'm') && (monitors?.Count > 0))
|| (keyConsole.KeyChar == 'f'))
{
keyChar = keyConsole.KeyChar;
break;
}
}
// Do we manage a camera ?
if (keyChar == 'c')
{
int cameraIndex = 0;
if (cameras?.Count > 1)
{
while (true)
{
Console.Clear();
Console.WriteLine("\nWhich camera do you want to use:");
int index = 0;
foreach (Camera camera in cameras)
{
Console.Write($"\n [{index}] - {camera.Name} ");
index++;
}
Console.WriteLine("\n");
Console.Out.Flush();
var keyConsole = Console.ReadKey();
if (int.TryParse($"{keyConsole.KeyChar}", out int keyValue) && keyValue < index && keyValue >= 0)
{
cameraIndex = keyValue;
break;
}
}
}
if (cameras != null)
{
var selectedCamera = cameras[cameraIndex];
SIPSorceryMedia.FFmpeg.FFmpegCameraSource cameraSource = new SIPSorceryMedia.FFmpeg.FFmpegCameraSource(selectedCamera.Path);
videoSource = cameraSource as IVideoSource;
}
}
// Do we manage a Monitor ?
else if (keyChar == 'm')
{
int monitorIndex = 0;
if (monitors?.Count > 1)
{
while (true)
{
Console.Clear();
Console.WriteLine("\nWhich Monitor do you want to use:");
int index = 0;
foreach (Monitor monitor in monitors)
{
Console.Write($"\n [{index}] - Monitor {monitor.Name} [{monitor.Rect.Width}x{monitor.Rect.Height}] {(monitor.Primary ? " PRIMARY" : "")}");
index++;
}
Console.WriteLine("\n");
Console.Out.Flush();
var keyConsole = Console.ReadKey();
if (int.TryParse($"{keyConsole.KeyChar}", out int keyValue) && keyValue < index && keyValue >= 0)
{
monitorIndex = keyValue;
break;
}
}
}
if (monitors != null)
{
var selectedMonitor = monitors[monitorIndex];
SIPSorceryMedia.FFmpeg.FFmpegScreenSource screenSource = new SIPSorceryMedia.FFmpeg.FFmpegScreenSource(selectedMonitor.Path, selectedMonitor.Rect, 20);
videoSource = screenSource as IVideoSource;
}
}
// Do we manage a File ?
else
{
SIPSorceryMedia.FFmpeg.FFmpegFileSource fileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(VIDEO_FILE_PATH, true, null, 960, true);
videoSource = fileSource as IVideoSource;
}
if(videoSource == null)
{
Console.WriteLine("No video source defined ...");
return;
}
// Create object used to display video in Ascii
asciiFrame = new AsciiFrame();
videoSource.RestrictFormats(x => x.Codec == VideoCodec);
videoSource.SetVideoSourceFormat(videoSource.GetVideoSourceFormats().Find(x => x.Codec == VideoCodec));
videoSource.OnVideoSourceRawSampleFaster+= FileSource_OnVideoSourceRawSampleFaster;
videoSource.StartVideo();
for (var loop = true; loop;)
{
var cki = Console.ReadKey(true);
switch (cki.Key)
{
case ConsoleKey.Q:
case ConsoleKey.Enter:
case ConsoleKey.Escape:
Console.CursorVisible = true;
loop = false;
break;
}
}
}
private static void FileSource_OnVideoSourceRawSampleFaster(uint durationMilliseconds, RawImage rawImage)
{
asciiFrame?.GotRawImage(ref rawImage);
}
}
}