-
Notifications
You must be signed in to change notification settings - Fork 0
/
RtspProxyLib.dpr
201 lines (180 loc) · 5.4 KB
/
RtspProxyLib.dpr
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
library RtspProxyLib;
uses
Windows,
Classes,
SysUtils,
IniFiles,
Log in 'Log.pas',
Console in 'Console.pas',
RtspTypes in 'RtspTypes.pas',
RtspProxyTube in 'RtspProxyTube.pas',
RtspProxyServer in 'RtspProxyServer.pas';
{ Console
|
<------------- Log ------------->
| | |
|<--------- RtspTypes --------->|
| |
RtspProxyServer <------------- RtspProxyTube
}
var
Proxy: TProxyServer = nil;
ProxyPort: Integer = 554;
function GetLocalPath: String;
var
FileName: array[0..MAX_PATH] of Char;
begin
GetModuleFileName(hInstance, FileName, MAX_PATH);
Result := ExtractFilePath(FileName);
end;
function GetIniFileName: String;
begin
Result := GetLocalPath + 'RtspProxy.ini';
end;
procedure ReadSettings;
const
Section = 'RtspProxy';
DefLogFile = 'RtspProxy.log';
var
Ini: TMemIniFile;
begin
Ini := TMemIniFile.Create(GetIniFileName);
try
ProxyPort := Ini.ReadInteger(Section, 'ProxyPort', 554);
Log.LogToConsole := Ini.ReadBool(Section, 'LogToConsole', False);
Log.LogToFile := Ini.ReadBool(Section, 'LogToFile', False);
RtspProxyTube.LogRtpPackets := Ini.ReadBool(Section, 'LogRtpPackets', False);
RtspProxyTube.LogRtcpPackets := Ini.ReadBool(Section, 'LogRtcpPackets', False);
if Log.LogToFile then
begin
LogFileName := Trim(Ini.ReadString(Section, 'LogFileName', DefLogFile));
if LogFileName = '' then LogFileName := DefLogFile;
// Åñëè ïóòü íå çàäàí èëè çàäàí îòíîñèòåëüíî (e.g.: .\Logs\LogFile.log)
if (LogFileName[1] = '.') or (ExtractFilePath(LogFileName) = '') then
LogFileName := GetLocalPath + LogFileName;
end;
RtspProxyTube.MinClientPort := Ini.ReadInteger(Section, 'MinClientPort', 30000);
RtspProxyTube.MinServerPort := Ini.ReadInteger(Section, 'MinServerPort', 40000);
RtspProxyTube.MaxProxyCount := Ini.ReadInteger(Section, 'MaxProxyCount', 10);
RtspProxyServer.BadClientRequestAction := Ini.ReadInteger(Section, 'BadClientRequestAction', 0);
RtspProxyServer.BadServerResponceAction := Ini.ReadInteger(Section, 'BadServerResponceAction', 0);
RtspProxyServer.LocalHostForClient := Ini.ReadString(Section, 'LocalHostForClient', '0.0.0.0');
RtspProxyServer.LocalHostForServer := Ini.ReadString(Section, 'LocalHostForServer', '0.0.0.0');
finally
Ini.Free;
end;
end;
procedure PrintSettings;
function BadActionToStr(Action: Integer): String;
begin
Result := IntToStr(Action);
case Action of
BAD_ACTION_DISCARD: Result := Result + ' (DISCARD)';
BAD_ACTION_IGNORE: Result := Result + ' (IGNORE)';
else Result := Result + ' (STOP)';
end;
end;
var
Strs: TStrings;
begin
Strs := TStringList.Create;
try
Strs.Add('Settings:');
Strs.Add('SettingsFile = ' + GetIniFileName);
Strs.Add('LogToConsole = ' + BoolToStr(Log.LogToConsole, True));
Strs.Add('LogToFile = ' + BoolToStr(Log.LogToFile, True));
Strs.Add('LogRtpPackets = ' + BoolToStr(RtspProxyTube.LogRtpPackets, True));
Strs.Add('LogRtcpPackets = ' + BoolToStr(RtspProxyTube.LogRtcpPackets, True));
Strs.Add('ProxyPort = ' + IntToStr(ProxyPort));
Strs.Add('LogFileName = ' + Log.LogFileName);
Strs.Add('MinClientPort = ' + IntToStr(RtspProxyTube.MinClientPort));
Strs.Add('MinServerPort = ' + IntToStr(RtspProxyTube.MinServerPort));
Strs.Add('MaxProxyCount = ' + IntToStr(RtspProxyTube.MaxProxyCount));
Strs.Add('LocalHostForClient = ' + RtspProxyServer.LocalHostForClient);
Strs.Add('LocalHostForServer = ' + RtspProxyServer.LocalHostForServer);
Strs.Add('BadClientRequestAction = ' + BadActionToStr(RtspProxyServer.BadClientRequestAction));
Strs.Add('BadServerResponceAction = ' + BadActionToStr(RtspProxyServer.BadServerResponceAction));
WriteLine(Strs.Text);
finally
Strs.Free;
end;
end;
{$region 'Exports'}
procedure InitProxy; cdecl;
begin
try
ReadSettings;
PrintSettings;
except
on E: Exception do
WriteError('[InitProxy] %s: %s', [E.ClassName, E.Message]);
end;
end;
procedure StartProxyOnPort(LocalPort: Integer); cdecl;
begin
try
LockProxy;
try
if not Assigned(Proxy) then
begin
Proxy := TProxyServer.Create(IntToStr(LocalPort));
Proxy.Start;
end;
finally
UnlockProxy;
end;
except
on E: Exception do
WriteError('[StartProxyOnPort] %s: %s', [E.ClassName, E.Message]);
end;
end;
procedure StartProxy; cdecl;
begin
StartProxyOnPort(ProxyPort);
end;
procedure AppendResource(Name, RtspAddress: PChar); cdecl;
begin
try
Resources.AppendResource(Name, RtspAddress);
except
on E: Exception do
WriteError('[AppendResource] %s: %s', [E.ClassName, E.Message]);
end;
end;
procedure RemoveResource(Name: PChar); cdecl;
begin
try
Resources.RemoveResource(Name);
except
on E: Exception do
WriteError('[RemoveResource] %s: %s', [E.ClassName, E.Message]);
end;
end;
procedure StopProxy; cdecl;
begin
try
LockProxy;
try
if Assigned(Proxy) then
begin
Proxy.Stop;
FreeAndNil(Proxy);
end;
finally
UnlockProxy;
end;
except
on E: Exception do
WriteError('[StopProxy] %s: %s', [E.ClassName, E.Message]);
end;
end;
exports
InitProxy,
StartProxy,
StartProxyOnPort,
StopProxy,
AppendResource,
RemoveResource;
{$endregion}
begin
end.