-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathONVIF.pas
1450 lines (1263 loc) · 57.9 KB
/
ONVIF.pas
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//*************************************************************
// ONVIF_WDSL *
// *
// Freeware Library *
// For Delphi 10.4 *
// by *
// Alessandro Mancini *
// *
//*************************************************************
{LICENSE:
THIS SOFTWARE IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED INCLUDING BUT NOT LIMITED TO THE APPLIED
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
YOU ASSUME THE ENTIRE RISK AS TO THE ACCURACY AND THE USE OF THE SOFTWARE
AND ALL OTHER RISK ARISING OUT OF THE USE OR PERFORMANCE OF THIS SOFTWARE
AND DOCUMENTATION. PRODUCTIONS DOES NOT WARRANT THAT THE SOFTWARE IS ERROR-FREE
OR WILL OPERATE WITHOUT INTERRUPTION. THE SOFTWARE IS NOT DESIGNED, INTENDED
OR LICENSED FOR USE IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE CONTROLS,
INCLUDING WITHOUT LIMITATION, THE DESIGN, CONSTRUCTION, MAINTENANCE OR
OPERATION OF NUCLEAR FACILITIES, AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS,
AIR TRAFFIC CONTROL, AND LIFE SUPPORT OR WEAPONS SYSTEMS. PRODUCTIONS SPECIFICALLY
DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR SUCH PURPOSE.
You may use/change/modify the component under 1 conditions:
1. In your application, add credits to "ONVIF WDSL"
{*******************************************************************************}
unit ONVIF;
interface
uses
System.Classes, System.SysUtils, System.SyncObjs, System.Messaging,
System.IOUtils, IdUDPServer, IdGlobal, Soap.XSBuiltIns, IdSocketHandle,
IdAuthenticationDigest, Winsock, XmlDoc, XmlIntf, XMLDom,
ONVIF.Structure.Device, ONVIF.Structure.Profile,
ONVIF.Structure.Capabilities;
CONST ONVIF_ERROR_URL_EMPTY = -1000;
ONVIF_ERROR_SOAP_INVALID = -1001;
ONVIF_ERROR_SOAP_NOBODY = -1002;
ONVIF_ERROR_SOAP_FAULTCODE_NOT_FOUND = -1003;
Type
{ https://www.onvif.org/specs/core/ONVIF-Core-Specification.pdf
https://www.onvif.org/specs/srv/ptz/ONVIF-PTZ-Service-Spec-v221.pdf
https://www.onvif.org/specs/srv/img/ONVIF-Imaging-Service-Spec.pdf?441d4a&441d4a
TODO
Preset
Imaging like IR CuT, IRIS Focus
Home
Move AbsoluteMove,RelativeMove
PRofile parser in record
AudioEncoderConfiguration : TAudioEncoderConfiguration;
VideoAnalyticsConfiguration: TVideoAnalyticsConfiguration;
Extension : TExtension;
Need example
SystemdateTime
}
/// <summary>
/// Specifies the type of ONVIF address, including device service, media, and PTZ.
/// </summary>
TONVIFAddrType = (atDeviceService, atMedia, atPtz,atDevice);
/// <summary>
/// Specifies the type of ONVIF PTZ (Pan-Tilt-Zoom) command, including left, top, right,
/// bottom, top-right, top-left, bottom-left, and bottom-right.
/// </summary>
TONVIF_PTZ_CommandType = (opcNone,opcLeft, opcTop, opcRight, opcBotton, opcTopRight, opcTopLeft, opcBottonLeft, opcBottonRight);
/// <summary>
/// Enumerates the types of PTZ movement supported by ONVIF.
/// </summary>
/// <remarks>
/// Possible values are: Continuous Move, Relative Move, and Absolute Move.
/// </remarks>
TONVIF_PTZ_MoveType = (opmvContinuousMove,opmvtRelativeMove,opmvtAbsoluteMove);
/// <summary>
/// Enumerates the logging levels for ONVIF events.
/// </summary>
/// <remarks>
/// Possible values are: Information, Error, Warning, and Exception.
/// </remarks>
TPONVIFLivLog = (tpLivInfo,tpLivError,tpLivWarning,tpLiveException);
/// <summary>
/// Defines a procedure type for writing logs with specific parameters.
/// </summary>
/// <param name="Funzione">
/// Name of the function or operation.
/// </param>
/// <param name="Descrizione">
/// Description of the log entry.
/// </param>
/// <param name="Livello">
/// Logging level (Information, Error, Warning, or Exception).
/// </param>
/// <param name="IsVerboseLog">
/// Indicates whether the log entry is verbose. Default is False.
/// </param>
/// <remarks>
/// This procedure is used to write logs with detailed information based on the specified parameters.
/// </remarks>
TEventWriteLog = procedure (Const Funzione,Descrizione:String;Livello : TPONVIFLivLog;IsVerboseLog:boolean=False) of object;
/// <summary>
/// Represents a manager class for handling ONVIF-related functionalities.
/// </summary>
TONVIFManager = class
private
FUrl : String;
FLogin : String;
FPassword : String;
FToken : String;
FLastResponse : String;
FLastStatusCode : Integer;
FSaveResponseOnDisk : Boolean;
FSpeed : Byte;
FDevice : TDeviceInformation;
FOnWriteLog : TEventWriteLog;
FProfiles : TProfiles;
FCapabilities : TCapabilitiesONVIF;
/// <summary>
/// Calculates the password digest based on the provided parameters.
/// </summary>
/// <param name="aPasswordDigest">
/// Variable to store the calculated password digest.
/// </param>
/// <param name="aNonce">
/// String containing the nonce value.
/// </param>
/// <param name="aCreated">
/// String containing the created timestamp.
/// </param>
/// <remarks>
/// Ensure that the input parameters are valid and properly formatted.
/// The calculated password digest will be stored in the provided variable.
/// </remarks>
procedure GetPasswordDigest( Var aPasswordDigest, aNonce, aCreated: String);
/// <summary>
/// Generates a URL based on the specified ONVIF address type.
/// </summary>
/// <param name="aUrlType">
/// The type of ONVIF address for which the URL needs to be generated.
/// </param>
/// <returns>
/// The generated URL as a string.
/// </returns>
/// <remarks>
/// The function takes an ONVIF address type as input and returns
/// the corresponding URL. Make sure to handle unexpected address types
/// appropriately.
/// </remarks>
function GetUrlByType(const aUrlType: TONVIFAddrType): string;
/// <summary>
/// Calculates the SHA-1 hash of the provided byte array.
/// </summary>
/// <param name="aData">
/// The input data for which the SHA-1 hash will be calculated.
/// </param>
/// <returns>
/// The calculated SHA-1 hash as a byte array.
/// </returns>
/// <remarks>
/// This function uses the SHA-1 hashing algorithm to generate a hash
/// for the provided input data. Ensure that the input data is valid
/// and properly formatted.
/// </remarks>
function SHA1(const aData: TBytes): TBytes;
/// <summary>
/// Returns an XML string representing a SOAP connection.
/// </summary>
/// <returns>
/// A string containing the details of the SOAP connection in XML format.
/// </returns>
function GetSoapXMLConnection:String;
/// <summary>
/// Prepares an XML string representing a request to retrieve profiles.
/// </summary>
/// <returns>
/// The XML string representing the GetProfiles request.
/// </returns>
/// <remarks>
/// This function generates an XML string that can be used as a request
/// to retrieve profiles. The format and structure of the XML may depend
/// on the specific requirements of the system or API you are working with.
/// </remarks>
function PrepareGetProfilesRequest: String;
/// <summary>
/// Prepares and returns an XML string representing a request for device information.
/// </summary>
/// <returns>
/// A string containing the XML-formatted request for device information.
/// </returns>
function PrepareGetDeviceInformationRequest: String;
/// <summary>
/// Retrieves device information and returns a Boolean indicating the success of the operation.
/// </summary>
/// <returns>
/// True if the device information is successfully retrieved; False otherwise, compile TDeviceInformation record.
/// </returns>
function GetDeviceInformation: Boolean;
/// <summary>
/// Resets the state or configuration of internal record like TDeviceInformation.
/// </summary>
procedure Reset;
{PTZ}
/// <summary>
/// Prepares an ONVIF PTZ (Pan-Tilt) start move request based on the specified command.
/// </summary>
/// <param name="aCommand">
/// The command to be included in the PTZ start move request.
/// </param>
/// <returns>
/// An XML-formatted string representing the PTZ start move request.
/// </returns>
function PreparePTZ_StartMoveRequest(const aCommand: String): String;
/// <summary>
/// Prepares an ONVIF PTZ stop move request.
/// </summary>
/// <returns>
/// An XML-formatted string representing the PTZ stop move request.
/// </returns>
function PreparePTZ_StopMoveRequest: String;
/// <summary>
/// Prepares an ONVIF PTZ start zoom request based on the specified command.
/// </summary>
/// <param name="aCommand">
/// The command to be included in the PTZ start zoom request.
/// </param>
/// <returns>
/// An XML-formatted string representing the PTZ start zoom request.
/// </returns>
function PreparePTZ_StartZoomRequest(const aCommand: String): String;
/// <summary>
/// Executes an ONVIF request using the provided address, input stream, and output stream.
/// </summary>
/// <param name="Addr">
/// The address for the ONVIF request.
/// </param>
/// <param name="InStream">
/// The input stream containing the ONVIF request data.
/// </param>
/// <param name="OutStream">
/// The output stream to store the ONVIF response data.
/// </param>
/// <returns>
/// True if the request is executed successfully; False otherwise.
/// </returns>
function ExecuteRequest(const Addr: String; const InStream, OutStream: TStringStream): Boolean; overload;
/// <summary>
/// Executes an ONVIF request using the provided address and request string.
/// </summary>
/// <param name="Addr">
/// The address for the ONVIF request.
/// </param>
/// <param name="Request">
/// The ONVIF request string.
/// </param>
/// <param name="Answer">
/// Returns the ONVIF response string after executing the request.
/// </param>
/// <returns>
/// True if the request is executed successfully; False otherwise.
/// </returns>
function ExecuteRequest(const Addr, Request: String; var Answer: String): Boolean; overload;
/// <summary>
/// Checks and handles an auxiliary command.
/// </summary>
procedure CheckAuxiliaryCommand;
/// <summary>
/// Sets the URL used for ONVIF communication.
/// </summary>
/// <param name="aValue">
/// The URL to be set.
/// </param>
procedure SetUrl(const aValue: String);
/// <summary>
/// Prepares a GetCapabilities request for ONVIF communication.
/// </summary>
/// <returns>
/// The prepared GetCapabilities request string.
/// </returns>
function PrepareGetCapabilitiesRequest: String;
/// <summary>
/// Retrieves and processes capabilities for the current ONVIF device.
/// </summary>
/// <returns>
/// Returns True if the capabilities are successfully retrieved and processed; otherwise, returns False.
/// </returns>
function GetCapabilities: Boolean;
/// <summary>
/// Writes a log entry with specified parameters.
/// </summary>
/// <param name="aFunction">
/// Name of the function or operation.
/// </param>
/// <param name="aDescription">
/// Description of the log entry.
/// </param>
/// <param name="aLivel">
/// Logging level (Information, Error, Warning, or Exception).
/// </param>
/// <param name="aIsVerboseLog">
/// Indicates whether the log entry is verbose. Default is False.
/// </param>
procedure DoWriteLog(const aFunction, aDescription: String; aLivel: TPONVIFLivLog; aIsVerboseLog: boolean = False);
/// <summary>
/// Checks if the set URL is valid for ONVIF communication.
/// </summary>
/// <returns>
/// True if the URL is valid; otherwise, False.
/// </returns>
function UrlIsValid: Boolean;
/// <summary>
/// Converts an internal error code to a human-readable string.
/// </summary>
/// <param name="aError">
/// The internal error code to be converted.
/// </param>
/// <returns>
/// The human-readable string representation of the internal error.
/// </returns>
function InternalErrorToString(const aError: Integer): String;
/// <summary>
/// Checks whether the given XML node is a valid SOAP XML.
/// </summary>
/// <param name="aRootNode">The root node of the XML document.</param>
/// <returns>True if the XML is a valid SOAP XML; otherwise, false.</returns>
function IsValidSoapXML(const aRootNode: IXMLNode): Boolean;
/// <summary>
/// Retrieves the SOAP body node from the given SOAP XML document.
/// </summary>
/// <param name="aRootNode">The root node of the SOAP XML document.</param>
/// <returns>The SOAP body node.</returns>
function GetSoapBody(const aRootNode: IXMLNode): IXMLNode;
/// <summary>
/// Recursively searches for an XML node with the specified name within the given XML node.
/// </summary>
/// <param name="ANode">The XML node to start the search from.</param>
/// <param name="aSearchNodeName">The name of the XML node to search for.</param>
/// <returns>The found XML node or nil if not found.</returns>
function RecursiveFindNode(ANode: IXMLNode; const aSearchNodeName: string;const aScanAllNode: Boolean=False): IXMLNode;
/// <summary>
/// Resets the ONVIF capabilities of the device to default values.
/// </summary>
procedure ResetCapabilities;
/// <summary>
/// Resets the ONVIF device information to default values.
/// </summary>
procedure ResetDevice;
/// <summary>
/// Resets the ONVIF profiles information to default values.
/// </summary>
procedure ResetProfiles;
public
/// <summary>
/// Initializes a new instance of the TONVIFManager class with the specified ONVIF service details.
/// </summary>
/// <param name="aUrl">
/// The URL of the ONVIF service.
/// </param>
/// <param name="aLogin">
/// The login credentials for the ONVIF service.
/// </param>
/// <param name="aPassword">
/// The password credentials for the ONVIF service.
/// </param>
constructor Create(const aUrl, aLogin, aPassword:String);overload;
/// <summary>
/// Initializes a new instance of the TONVIFManager class with the specified ONVIF service details.
/// </summary>
/// <param name="aUrl">
/// The URL of the ONVIF service.
/// </param>
/// <param name="aLogin">
/// The login credentials for the ONVIF service.
/// </param>
/// <param name="aPassword">
/// The password credentials for the ONVIF service.
/// </param>
/// <param name="aToken">
/// The security token for the ONVIF service.
/// </param>
constructor Create(const aUrl, aLogin, aPassword, aToken: String);overload;
/// <summary>
/// ONVIF PTZ Zoom start operation.
/// </summary>
/// <param name="aInZoom">
/// Indicates whether it is an "in" zoom operation (True) or "out" zoom operation (False).
/// </param>
/// <param name="aResultStr">
/// Returns the result string after executing the PTZ start zoom operation.
/// <returns>
/// True if the PTZ start zoom operation is executed successfully; False otherwise.
/// </returns>
function PTZ_StartZoom(aMoveType:TONVIF_PTZ_MoveType;aInZoom: Boolean): Boolean;
/// <summary>
/// Initiates an ONVIF PTZ (Pan-Tilt) move operation based on the specified command.
/// </summary>
/// <param name="acommand">
/// The type of PTZ move command to be executed.
/// </param>
/// <param name="aResultStr">
/// Returns the result string after executing the PTZ move operation.
/// </param>
/// <returns>
/// True if the PTZ move operation is executed successfully; False otherwise.
/// </returns>
function PTZ_StartMove(aMoveType:TONVIF_PTZ_MoveType;const acommand: TONVIF_PTZ_CommandType): Boolean;
/// <summary>
/// Stops an ongoing ONVIF PTZ (Pan-Tilt-Zoom) move operation.
/// </summary>
/// <param name="aResultStr">
/// Returns the result string after stopping the PTZ move operation.
/// </param>
/// <returns>
/// True if the PTZ move operation is successfully stopped; False otherwise.
/// </returns>
function PTZ_StopMove: Boolean;
/// <summary>
/// Retrieves the profiles associated with the ONVIF device.
/// </summary>
/// <param name="aResultStr">
/// Returns the result string containing the profiles after executing the operation.
/// </param>
/// <returns>
/// True if the operation is executed successfully; False otherwise.
/// </returns>
function GetProfiles: Boolean;
/// <summary>
/// Gets or sets the speed parameter for PTZ operations.
/// </summary>
property Speed : Byte read FSpeed write FSpeed;
/// <summary>
/// Gets or sets the URL of the ONVIF service.
/// </summary>
property Url : String read Furl write SetUrl;
/// <summary>
/// Gets or sets the token of the ONVIF camera.
/// </summary>
property Token : String read FToken write FToken;
/// <summary>
/// Event handler for writing logs with specific parameters.
/// </summary>
/// <remarks>
/// Use this event to handle log writing with detailed information based on the specified parameters.
/// </remarks>
property OnWriteLog : TEventWriteLog read FOnWriteLog write FOnWriteLog;
/// <summary>
/// Gets or sets whether to save the last HTTP response on disk.
/// </summary>
/// <remarks>
/// Set this property to True if you want to save the last HTTP response on disk.
/// </remarks>
property SaveResponseOnDisk : Boolean read FSaveResponseOnDisk write FSaveResponseOnDisk;
/// <summary>
/// Gets the last HTTP status code received.
/// </summary>
/// <remarks>
/// Use this property to retrieve the last HTTP status code received during communication.
/// </remarks>
property LastStatusCode : Integer read FLastStatusCode;
/// <summary>
/// Gets the last HTTP response received.
/// </summary>
/// <remarks>
/// Use this property to retrieve the last HTTP response received during communication.
/// </remarks>
property LastResponse : String read FLastResponse;
/// <summary>
/// Gets information about the ONVIF device.
/// </summary>
property Device : TDeviceInformation read FDevice;
/// <summary>
/// Gets the profiles associated with the ONVIF communication.
/// </summary>
/// <remarks>
/// Use this property to retrieve the profiles associated with the ONVIF communication.
/// </remarks>
property Profiles : TProfiles read FProfiles;
/// <summary>
/// Represents the ONVIF capabilities of the device.
/// </summary>
/// <remarks>
/// The ONVIF capabilities, including device, events, PTZ, and extension capabilities.
/// </remarks>
property Capabilities : TCapabilitiesONVIF read FCapabilities;
end;
implementation
Uses System.NetEncoding, IdHashSHA, IdHTTP, IdURI;
constructor TONVIFManager.Create(const aUrl, aLogin, aPassword:String);
begin
Create(aUrl, aLogin, aPassword,String.Empty);
end;
constructor TONVIFManager.Create(const aUrl,aLogin,aPassword,aToken:String);
begin
FLogin := aLogin;
FSaveResponseOnDisk := False;
FPassword := aPassword;
FToken := aToken;
Url := aUrl; // execute setUrl;
FSpeed := 6;
end;
procedure TONVIFManager.DoWriteLog(const aFunction, aDescription: String;aLivel: TPONVIFLivLog; aIsVerboseLog: boolean=false);
begin
if Assigned(FOnWriteLog) then
FOnWriteLog(aFunction,aDescription,aLivel,aIsVerboseLog)
end;
procedure TONVIFManager.CheckAuxiliaryCommand;
begin
end;
procedure TONVIFManager.GetPasswordDigest(Var aPasswordDigest, aNonce, aCreated: String);
Var i : Integer;
LRaw_nonce : TBytes;
LBnonce : TBytes;
LDigest : TBytes;
Lraw_digest: TBytes;
begin
SetLength(LRaw_nonce, 20);
for i := 0 to High(LRaw_nonce) do
LRaw_nonce[i]:= Random(256);
LBnonce := TNetEncoding.Base64.Encode(LRaw_nonce);
aNonce := TEncoding.ANSI.GetString(LBnonce);
aCreated := DateTimeToXMLTime(Now,False);
Lraw_digest := SHA1(LRaw_nonce + TEncoding.ANSI.GetBytes(aCreated) + TEncoding.ANSI.GetBytes(FPassword));
LDigest := TNetEncoding.Base64.Encode(Lraw_digest);
aPasswordDigest := TEncoding.ANSI.GetString(LDigest);
end;
procedure TONVIFManager.SetUrl(const aValue: String);
begin
if Furl <> aValue then
begin
if aValue.Trim.IsEmpty then
Reset
else
begin
Furl := aValue;
{TODO get system date time to be use for password? }
{TODO GetCapabilities}
GetCapabilities;
GetDeviceInformation;
GetProfiles;
if not Token.Trim.IsEmpty then
begin
{TODO Get preset}
CheckAuxiliaryCommand;
end;
end;
end;
end;
function TONVIFManager.SHA1(const aData: TBytes): TBytes;
Var LIdHashSHA1: TIdHashSHA1;
i, j: TIdBytes;
begin
LIdHashSHA1 := TIdHashSHA1.Create;
try
SetLength(i, Length(aData));
Move(aData[0], i[0], Length(aData));
j := LIdHashSHA1.HashBytes(i);
SetLength(Result, Length(j));
Move(j[0], Result[0], Length(j));
finally
LIdHashSHA1.Free;
end;
end;
function TONVIFManager.UrlIsValid: Boolean;
begin
Result := not FUrl.Trim.IsEmpty;
if not result then
begin
FLastStatusCode := ONVIF_ERROR_URL_EMPTY;
FLastResponse := InternalErrorToString(ONVIF_ERROR_URL_EMPTY);
end;
end;
function TONVIFManager.InternalErrorToString(const aError :Integer):String;
begin
case aError of
ONVIF_ERROR_URL_EMPTY : Result := 'Url is empty';
ONVIF_ERROR_SOAP_INVALID : result := 'Root node is not SOAP envelope';
ONVIF_ERROR_SOAP_NOBODY : result := 'Body SOAP node not found';
ONVIF_ERROR_SOAP_FAULTCODE_NOT_FOUND : Result := 'SOAP Fault code not found';
else
result := 'Unknow error'
end;
end;
function TONVIFManager.GetUrlByType(const aUrlType: TONVIFAddrType): string;
CONST
URL_DEVICE_SERVICE = 'device_service';
URL_PTZ_SERVICE = 'ptz_service';
URL_MEDIA = 'media';
URL_DEVICE = 'device';
Var LUri: TIdURI;
begin
Result := String.Empty;
if not UrlIsValid then Exit;
LUri := TIdURI.Create(FUrl);
try
case aUrlType of
atDeviceService: LUri.Document := URL_DEVICE_SERVICE;
atMedia : LUri.Document := URL_MEDIA;
atPtz : LUri.Document := URL_PTZ_SERVICE;
atDevice : LUri.Document := URL_DEVICE;
end;
Result := LUri.Uri;
finally
FreeAndNil(LUri);
end;
end;
function TONVIFManager.GetSoapXMLConnection:String;
CONST XML_SOAP_CONNECTION: String =
'<?xml version="1.0"?> ' +
'<soap:Envelope ' +
'xmlns:soap="http://www.w3.org/2003/05/soap-envelope" ' +
'xmlns:wsdl="http://www.onvif.org/ver10/media/wsdl">' +
'<soap:Header>' +
'<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" s:mustUnderstand="1"> ' +
'<UsernameToken> ' +
'<Username>%s</Username> ' +
'<Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">%s</Password> ' +
'<Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">%s</Nonce> ' +
'<Created xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">%s</Created> ' +
'</UsernameToken> ' +
'</Security> ' +
'</soap:Header>' +
'<soap:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> ';
var LPasswordDigest : String;
LNonce : String;
LCreated : String;
begin
GetPasswordDigest(LPasswordDigest, LNonce, LCreated);
Result := Format(XML_SOAP_CONNECTION, [FLogin, LPasswordDigest, LNonce, LCreated]);
end;
function TONVIFManager.PrepareGetCapabilitiesRequest: String;
const GET_CAPABILITIES = '<GetCapabilities'+
' xmlns="http://www.onvif.org/ver10/device/wsdl">'+
'<Category>All</Category>'+
'</GetCapabilities>'+
'</soap:Body>'+
'</soap:Envelope>';
begin
Result := GetSoapXMLConnection+ GET_CAPABILITIES;
end;
function TONVIFManager.GetCapabilities: Boolean;
var LResultStr : String;
LXMLDoc : IXMLDocument;
LSoapBodyNode : IXMLNode;
LCapabilitieNode : IXMLNode;
LNodeTmp1 : IXMLNode;
LNodeTmp2 : IXMLNode;
function GetChildNodeValue(const ParentNode: IXMLNode; const ChildNodeName: string): string;
begin
Result := '';
if Assigned(ParentNode) then
begin
// Check if the child node exists before accessing its value
if ParentNode.ChildNodes.IndexOf(ChildNodeName) > -1 then
Result := ParentNode.ChildNodes[ChildNodeName].Text;
end;
end;
begin
Result := false;
ResetCapabilities;
if not UrlIsValid then Exit;
Result := ExecuteRequest(GetUrlByType(atDevice), PrepareGetCapabilitiesRequest, LResultStr);
if Result then
begin
{$REGION 'Log'}
{TSI:IGNORE ON}
DoWriteLog('TONVIFManager.GetCapabilities',Format(' XML response [%s]',[LResultStr]),tpLivInfo,true);
{TSI:IGNORE OFF}
{$ENDREGION}
LXMLDoc := TXMLDocument.Create(nil);
LXMLDoc.LoadFromXML(LResultStr);
if not IsValidSoapXML(LXMLDoc.DocumentElement) then exit;
LSoapBodyNode := GetSoapBody(LXMLDoc.DocumentElement);
LCapabilitieNode := RecursiveFindNode(LSoapBodyNode,'Device');
if Assigned(LCapabilitieNode) then
begin
FCapabilities.Device.XAddr := GetChildNodeValue(LCapabilitieNode,'XAddr');
LNodeTmp1 := RecursiveFindNode(LCapabilitieNode,'Network');
if Assigned(LNodeTmp1) then
begin
FCapabilities.Device.Network.IPFilter := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'IPFilter'),False);
FCapabilities.Device.Network.ZeroConfiguration := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'IPFilter'),False);
FCapabilities.Device.Network.IPVersion6 := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'IPVersion6'),False);
FCapabilities.Device.Network.DynDNS := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'DynDNS'),False);
LNodeTmp2 := RecursiveFindNode(LNodeTmp1,'Extension');
if Assigned(LNodeTmp2) then
FCapabilities.Device.Network.Extension.Dot11Configuration := StrToBoolDef(GetChildNodeValue(LNodeTmp2,'Dot11Configuration'),False);
end;
LNodeTmp1 := RecursiveFindNode(LCapabilitieNode,'System');
if Assigned(LNodeTmp1) then
begin
FCapabilities.Device.System.DiscoveryResolve := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'DiscoveryResolve'),False);
FCapabilities.Device.System.DiscoveryBye := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'DiscoveryBye'),False);
FCapabilities.Device.System.RemoteDiscovery := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'RemoteDiscovery'),False);
FCapabilities.Device.System.SystemBackup := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'SystemBackup'),False);
FCapabilities.Device.System.SystemLogging := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'SystemLogging'),False);
FCapabilities.Device.System.FirmwareUpgrade := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'FirmwareUpgrade'),False);
LNodeTmp2 := RecursiveFindNode(LNodeTmp1,'SupportedVersions');
if Assigned(LNodeTmp2) then
begin
FCapabilities.Device.System.SupportedVersions.Major := StrToIntDef(GetChildNodeValue(LNodeTmp2,'Major'),-1);
FCapabilities.Device.System.SupportedVersions.Minor := StrToIntDef(GetChildNodeValue(LNodeTmp2,'Minor'),-1);
end;
end;
{event}
LCapabilitieNode := RecursiveFindNode(LSoapBodyNode,'Events');
if Assigned(LCapabilitieNode) then
begin
FCapabilities.Events.XAddr := GetChildNodeValue(LCapabilitieNode,'XAddr');
FCapabilities.Events.WSSubscriptionPolicySupport := StrToBoolDef(GetChildNodeValue(LCapabilitieNode,'WSSubscriptionPolicySupport'),False);
FCapabilities.Events.WSPullPointSupport := StrToBoolDef(GetChildNodeValue(LCapabilitieNode,'WSPullPointSupport'),False);
FCapabilities.Events.WSPausableSubscriptionManagerInterfaceSupport := StrToBoolDef(GetChildNodeValue(LCapabilitieNode,'WSPausableSubscriptionManagerInterfaceSupport'),False);
end;
{Media}
LCapabilitieNode := RecursiveFindNode(LSoapBodyNode,'Media');
if Assigned(LCapabilitieNode) then
begin
FCapabilities.Media.XAddr := GetChildNodeValue(LCapabilitieNode,'XAddr');
LNodeTmp1 := RecursiveFindNode(LCapabilitieNode,'StreamingCapabilities');
if Assigned(LNodeTmp1) then
begin
FCapabilities.Media.StreamingCapabilities.RTPMulticast := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'RTPMulticast'),False);
FCapabilities.Media.StreamingCapabilities.RTP_TCP := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'RTP_TCP'),False);
FCapabilities.Media.StreamingCapabilities.RTP_RTSP_TCP := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'RTP_RTSP_TCP'),False);
end;
end;
{PTZ}
LCapabilitieNode := RecursiveFindNode(LSoapBodyNode,'PTZ');
if Assigned(LCapabilitieNode) then
FCapabilities.PTZ.XAddr := GetChildNodeValue(LCapabilitieNode,'XAddr');
{Extension}
LCapabilitieNode := RecursiveFindNode(LSoapBodyNode,'Extension',True);
if Assigned(LCapabilitieNode) then
begin
LNodeTmp1 := RecursiveFindNode(LCapabilitieNode,'Search');
if Assigned(LNodeTmp1) then
begin
FCapabilities.Extension.Search.XAddr := GetChildNodeValue(LNodeTmp1,'XAddr');
FCapabilities.Extension.Search.MetadataSearch := StrToBoolDef(GetChildNodeValue(LNodeTmp1,'MetadataSearch'),False);
end;
LNodeTmp1 := RecursiveFindNode(LCapabilitieNode,'Replay');
if Assigned(LNodeTmp1) then
FCapabilities.Extension.Replay.XAddr := GetChildNodeValue(LNodeTmp1,'XAddr');
end;
end;
end
else
{$REGION 'Log'}
{TSI:IGNORE ON}
DoWriteLog('TONVIFManager.GetCapabilities',Format(' Error [%d] response [%s]',[FLastStatusCode,LResultStr]),tpLivError);
{TSI:IGNORE OFF}
{$ENDREGION}
end;
function TONVIFManager.PrepareGetProfilesRequest: String;
const GET_PROFILES = '<GetProfiles xmlns="http://www.onvif.org/ver10/media/wsdl" /> ' +
'</soap:Body> ' +
'</soap:Envelope>';
begin
Result := GetSoapXMLConnection + GET_PROFILES
end;
function TONVIFManager.GetProfiles: Boolean;
var LResultStr : String;
LXMLDoc : IXMLDocument;
LSoapBodyNode : IXMLNode;
LProfilesNode : IXMLNode;
LTokenNode : IXMLNode;
LChildNodeRoot : IXMLNode;
LChildNodeNode : IXMLNode;
LChildNodeNode2 : IXMLNode;
LChildNodeNode3 : IXMLNode;
I : Integer;
LProfile : TProfile;
LCurrentIndex : integer;
function GetAttribute(const Node: IXMLNode; const AttributeName: string): string;
begin
Result := '';
if Assigned(Node) and Node.HasAttribute(AttributeName) then
Result := Node.Attributes[AttributeName];
end;
function GetChildNodeValue(const ParentNode: IXMLNode; const ChildNodeName: string): string;
begin
Result := '';
if Assigned(ParentNode) then
begin
// Check if the child node exists before accessing its value
if ParentNode.ChildNodes.IndexOf(ChildNodeName) > -1 then
Result := ParentNode.ChildNodes[ChildNodeName].Text;
end;
end;
begin
Result := False;
ResetProfiles;
if not UrlIsValid then Exit;
Result := ExecuteRequest(GetUrlByType(atDeviceService), PrepareGetProfilesRequest, LResultStr);
if Result then
begin
Result := False;
{$REGION 'Log'}
{TSI:IGNORE ON}
DoWriteLog('TONVIFManager.GetProfiles',Format(' XML response [%s]',[LResultStr]),tpLivInfo,true);
{TSI:IGNORE OFF}
{$ENDREGION}
LXMLDoc := TXMLDocument.Create(nil);
LXMLDoc.LoadFromXML(LResultStr);
if not IsValidSoapXML(LXMLDoc.DocumentElement) then exit;
LSoapBodyNode := GetSoapBody(LXMLDoc.DocumentElement);
LProfilesNode := RecursiveFindNode(LSoapBodyNode,'GetProfilesResponse');
if Assigned(LProfilesNode) then
begin
Result := LProfilesNode.ChildNodes.Count > 0;
if FToken.Trim.IsEmpty then
SetLength(FProfiles,LProfilesNode.ChildNodes.Count)
else
SetLength(FProfiles,1);
LCurrentIndex := 0;
for I := 0 to LProfilesNode.ChildNodes.Count -1 do
begin
LProfile.token := String(LProfilesNode.ChildNodes[I].Attributes['token']);
if not FToken.Trim.IsEmpty then
begin
if LProfile.token.ToLower.Trim <> FToken.Trim.ToLower then
continue;
end;
LProfile.fixed := Boolean(StrToBoolDef(GetAttribute(LProfilesNode.ChildNodes[I],'fixed'), False));
LProfile.name := GetChildNodeValue(LProfilesNode.ChildNodes[I],'Name');
// Continue parsing TVideoSourceConfiguration
LChildNodeRoot := RecursiveFindNode(LProfilesNode.ChildNodes[I],'VideoSourceConfiguration');
if Assigned(LChildNodeRoot) then
begin
LProfile.VideoSourceConfiguration.token := GetAttribute(LChildNodeRoot,'token');
LProfile.VideoSourceConfiguration.name := GetChildNodeValue(LChildNodeRoot, 'Name');
LProfile.VideoSourceConfiguration.UseCount := StrToIntDef(GetChildNodeValue(LChildNodeRoot, 'UseCount'), 0);
LProfile.VideoSourceConfiguration.SourceToken := GetChildNodeValue(LChildNodeRoot, 'SourceToken');
LChildNodeNode := RecursiveFindNode(LChildNodeRoot,'Bounds');
if Assigned(LChildNodeNode) then
begin
LProfile.VideoSourceConfiguration.Bounds.x := StrToIntDef(GetAttribute(LChildNodeNode,'x'), 0);
LProfile.VideoSourceConfiguration.Bounds.y := StrToIntDef(GetAttribute(LChildNodeNode,'y'), 0);
LProfile.VideoSourceConfiguration.Bounds.width := StrToIntDef(GetAttribute(LChildNodeNode,'width'), 0);
LProfile.VideoSourceConfiguration.Bounds.height := StrToIntDef(GetAttribute(LChildNodeNode,'height'), 0);
end;
end;
// Continue parsing TVideoEncoderConfiguration
LChildNodeRoot := RecursiveFindNode(LProfilesNode.ChildNodes[I],'VideoEncoderConfiguration');
if Assigned(LChildNodeRoot) then
begin
LProfile.VideoEncoderConfiguration.token := GetAttribute(LChildNodeRoot,'token');
LProfile.VideoEncoderConfiguration.name := GetChildNodeValue(LChildNodeRoot, 'Name');
LProfile.VideoEncoderConfiguration.UseCount := StrToIntDef(GetChildNodeValue(LChildNodeRoot, 'UseCount'), 0);
LProfile.VideoEncoderConfiguration.Encoding := GetChildNodeValue(LChildNodeRoot, 'Encoding');
LProfile.VideoEncoderConfiguration.Quality := StrToFloatDef(GetChildNodeValue(LChildNodeRoot, 'Quality'),0);
LProfile.VideoEncoderConfiguration.SessionTimeout := GetChildNodeValue(LChildNodeRoot, 'SessionTimeout');
LChildNodeNode := RecursiveFindNode(LChildNodeRoot,'Resolution');
if Assigned(LChildNodeNode) then
begin
LProfile.VideoEncoderConfiguration.Resolution.width := StrToIntDef(GetChildNodeValue(LChildNodeNode,'Width'), 0);
LProfile.VideoEncoderConfiguration.Resolution.height := StrToIntDef(GetChildNodeValue(LChildNodeNode,'Height'), 0);
end;
LChildNodeNode := RecursiveFindNode(LChildNodeRoot,'RateControl');
if Assigned(LChildNodeNode) then
begin
LProfile.VideoEncoderConfiguration.RateControl.FrameRateLimit := StrToIntDef(GetChildNodeValue(LChildNodeNode,'FrameRateLimit'), 0);
LProfile.VideoEncoderConfiguration.RateControl.EncodingInterval := StrToIntDef(GetChildNodeValue(LChildNodeNode,'EncodingInterval'), 0);
LProfile.VideoEncoderConfiguration.RateControl.BitrateLimit := StrToIntDef(GetChildNodeValue(LChildNodeNode,'BitrateLimit'), 0);
end;
LChildNodeNode := RecursiveFindNode(LChildNodeRoot,'H264');
if Assigned(LChildNodeNode) then
begin
LProfile.VideoEncoderConfiguration.H264.GovLength := StrToIntDef(GetChildNodeValue(LChildNodeNode,'GovLength'), 0);
LProfile.VideoEncoderConfiguration.H264.H264Profile := GetChildNodeValue(LChildNodeNode,'H264Profile')
end;
LChildNodeNode := RecursiveFindNode(LChildNodeRoot,'Multicast');
if Assigned(LChildNodeNode) then
begin
LProfile.VideoEncoderConfiguration.Multicast.Port := StrToIntDef(GetChildNodeValue(LChildNodeNode,'Port'), 0);
LProfile.VideoEncoderConfiguration.Multicast.TTL := StrToIntDef(GetChildNodeValue(LChildNodeNode,'TTL'), 0);
LProfile.VideoEncoderConfiguration.Multicast.AutoStart := StrToBoolDef(GetChildNodeValue(LChildNodeNode,'AutoStart'),false);
LChildNodeNode := RecursiveFindNode(LChildNodeNode,'Address');
if Assigned(LChildNodeNode) then
LProfile.VideoEncoderConfiguration.Multicast.Address.TypeAddr := GetChildNodeValue(LChildNodeNode,'Type')
end;
end;
// Continue parsing TPTZConfiguration