-
Notifications
You must be signed in to change notification settings - Fork 230
/
media.js
1111 lines (1058 loc) · 46.6 KB
/
media.js
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
/**
* @namespace cam
* @description Media section for Cam class
* @author Andrew D.Laptev <a.d.laptev@gmail.com>
* @licence MIT
*/
module.exports = function(Cam) {
const linerase = require('./utils').linerase;
/**
* @typedef {object} Cam~VideoSource
* @property {string} $.token Video source token
* @property {number} framerate
* @property {number} resolution.width
* @property {number} resolution.height
*/
/**
* @callback Cam~GetVideoSourcesCallback
* @property {?Error} error
* @property {Cam~VideoSource|Array.<Cam~VideoSource>} videoSources
* @property {string} xml Raw SOAP response
*/
/**
* Receive video sources
* @param {Cam~GetVideoSourcesCallback} [callback]
*/
Cam.prototype.getVideoSources = function(callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetVideoSources xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
/**
* Video sources
* @name Cam#videoSources
* @type {Cam~VideoSource|Array.<Cam~VideoSource>}
*/
this.videoSources = linerase(data).getVideoSourcesResponse.videoSources;
}
if (callback) {
callback.call(this, err, this.videoSources, xml);
}
}.bind(this));
};
/**
* @typedef {object} Cam~VideoSourceConfiguration
* @property {string} token Token that uniquely refernces this configuration
* @property {string} sourceToken Reference to the physical input
* @property {string} name User readable name
* @property {number} useCount Number of internal references currently using this configuration
* @property {object} bounds
* @property {number} bounds.height
* @property {number} bounds.width
* @property {number} bounds.x
* @property {number} bounds.y
*/
/**
* @callback Cam~GetVideoSourceConfigurationsCallback
* @property {?Error} error
* @property {Array.<Cam~VideoSourceConfiguration>} videoSourceConfigurations
* @property {string} xml Raw SOAP response
*/
/**
* Receive video sources
* @param {Cam~GetVideoSourceConfigurationsCallback} [callback]
*/
Cam.prototype.getVideoSourceConfigurations = function(callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetVideoSourceConfigurations xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
this.videoSourceConfigurations = data[0].getVideoSourceConfigurationsResponse[0].configurations.map(function(data) {
var obj = linerase(data);
return {
token: obj.$.token
, name: obj.name
, useCount: obj.useCount
, sourceToken: obj.sourceToken
, bounds: {
height: obj.bounds.$.height
, width: obj.bounds.$.width
, x: obj.bounds.$.x
, y: obj.bounds.$.y
}
};
});
}
if (callback) {
callback.call(this, err, this.videoSourceConfigurations, xml);
}
}.bind(this));
};
/**
* @typedef {object} Cam~VideoEncoderConfiguration
* @property {string} $.token Token that uniquely refernces this configuration
* @property {string} name User readable name.
* @property {string} useCount Number of internal references currently using this configuration
* @property {string} encoding Used video codec ('JPEG' | 'MPEG4' | 'H264' )
* @property {object} resolution Configured video resolution
* @property {number} resolution.width
* @property {number} resolution.height
* @property {number} quality Relative value for the video quantizers and the quality of the video. A high value within supported quality range means higher quality
* @property {object} [rateControl] Optional element to configure rate control related parameters
* @property {number} rateControl.frameRateLimit
* @property {number} rateControl.encodingInterval
* @property {number} rateControl.bitrateLimit
* @property {object} [H264] Optional element to configure H.264 related parameters
* @property {number} H264.govLength Group of Video frames length
* @property {string} H264.H264profile the H.264 profile
* @property {object} [MPEG4] Optional element to configure Mpeg4 related parameters
* @property {number} MPEG4.govLength Determines the interval in which the I-Frames will be coded.
* @property {string} MPEG4.MPEG4profile the Mpeg4 profile
* @property {object} multicast
* @property {string} multicast.address.type
* @property {string} [multicast.address.IPv4Address]
* @property {string} [multicast.address.IPv6Address]
* @property {number} multicast.port
* @property {number} multicast.TTL
* @property {boolean} multicast.autoStart
* @property {string} sessionTimeout The rtsp session timeout for the related video stream
*/
/**
* @callback Cam~VideoEncoderConfigurationCallback
* @property {?Error} error
* @property {Cam~VideoEncoderConfiguration} videoEncoderConfiguration
* @property {string} xml Raw SOAP response
*/
/**
* @callback Cam~VideoEncoderConfigurationsCallback
* @property {?Error} error
* @property {Array.<Cam~VideoEncoderConfiguration>} videoEncoderConfigurations
* @property {string} xml Raw SOAP response
*/
/**
* Get existing video encoder configuration by its token
* If token is omitted tries first from #videoEncoderConfigurations array
* @param {string} [token] Token of the requested video encoder configuration
* @param {Cam~VideoEncoderConfigurationCallback} callback
*/
Cam.prototype.getVideoEncoderConfiguration = function(token, callback) {
if (callback === undefined) {
callback = token;
if (this.videoEncoderConfigurations && this.videoEncoderConfigurations[0]) {
token = this.videoEncoderConfigurations[0].$.token;
} else {
return callback(new Error('No video encoder configuration token is present!'));
}
}
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetVideoEncoderConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ConfigurationToken>' + token + '</ConfigurationToken>' +
'</GetVideoEncoderConfiguration>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, err ? null : linerase(data[0].getVideoEncoderConfigurationResponse[0].configuration), xml);
}
}.bind(this));
};
/**
* @typedef {object} Cam~VideoEncoderConfigurationOptions
* @property {object} qualityRange Range of the quality values. A high value means higher quality
* @property {number} qualityRange.min
* @property {number} qualityRange.max
* @property {object} [JPEG] Optional JPEG encoder settings ranges
* @property {object} JPEG.resolutionsAvailable List of supported resolutions
* @property {number} JPEG.resolutionsAvailable.width
* @property {number} JPEG.resolutionsAvailable.height
* @property {object} JPEG.frameRateRange Range of frame rate settings
* @property {number} JPEG.frameRateRange.min
* @property {number} JPEG.frameRateRange.max
* @property {object} JPEG.encodingIntervalRange Range of encoding interval settings
* @property {number} JPEG.encodingInterval.min
* @property {number} JPEG.encodingInterval.max
* @property {object} [MPEG4] Optional MPEG4 encoder settings ranges
* @property {object} MPEG4.resolutionsAvailable List of supported resolutions
* @property {number} MPEG4.resolutionsAvailable.width
* @property {number} MPEG4.resolutionsAvailable.height
* @property {object} MPEG4.resolutionsAvailable List of supported resolutions
* @property {object} MPEG4.frameRateRange Range of frame rate settings
* @property {number} MPEG4.frameRateRange.min
* @property {number} MPEG4.frameRateRange.max
* @property {object} MPEG4.encodingIntervalRange Range of encoding interval settings
* @property {number} MPEG4.encodingInterval.min
* @property {number} MPEG4.encodingInterval.max
* @property {object} MPEG4.govLengthRange Range of group of video frames length settings
* @property {number} MPEG4.govLengthRange.min
* @property {number} MPEG4.govLengthRange.max
* @property {object} MPEG4.MPEG4ProfilesSupported List of supported MPEG4 profiles enum {'SP', 'ASP'}
* @property {object} [H264] Optional H.264 encoder settings ranges
* @property {object} H264.resolutionsAvailable List of supported resolutions
* @property {number} H264.resolutionsAvailable.width
* @property {number} H264.resolutionsAvailable.height
* @property {object} H264.frameRateRange Range of frame rate settings
* @property {number} H264.frameRateRange.min
* @property {number} H264.frameRateRange.max
* @property {object} H264.encodingIntervalRange Range of encoding interval settings
* @property {number} H264.encodingInterval.min
* @property {number} H264.encodingInterval.max
* @property {object} H264.govLengthRange Range of group of video frames length settings
* @property {number} H264.govLengthRange.min
* @property {number} H264.govLengthRange.max
* @property {object} H264.H264ProfilesSupported List of supported H.264 profiles enum {'Baseline', 'Main', 'Extended', 'High'}
* @property {object} [extension] Optional encoder extensions
* @property {object} [extension.JPEG] Optional JPEG encoder settings ranges
* @property {object} extension.JPEG.resolutionsAvailable List of supported resolutions
* @property {number} extension.JPEG.resolutionsAvailable.width
* @property {number} extension.JPEG.resolutionsAvailable.height
* @property {object} extension.JPEG.frameRateRange Range of frame rate settings
* @property {number} extension.JPEG.frameRateRange.min
* @property {number} extension.JPEG.frameRateRange.max
* @property {object} extension.JPEG.encodingIntervalRange Range of encoding interval settings
* @property {number} extension.JPEG.encodingInterval.min
* @property {number} extension.JPEG.encodingInterval.max
* @property {object} extension.JPEG.bitrateRange Range of bitrate settings
* @property {number} extension.JPEG.bitrateRange.min
* @property {number} extension.JPEG.bitrateRange.max
* @property {object} [extension.MPEG4] Optional MPEG4 encoder settings ranges
* @property {object} extension.MPEG4.resolutionsAvailable List of supported resolutions
* @property {number} extension.MPEG4.resolutionsAvailable.width
* @property {number} extension.MPEG4.resolutionsAvailable.height
* @property {object} extension.MPEG4.resolutionsAvailable List of supported resolutions
* @property {object} extension.MPEG4.frameRateRange Range of frame rate settings
* @property {number} extension.MPEG4.frameRateRange.min
* @property {number} extension.MPEG4.frameRateRange.max
* @property {object} extension.MPEG4.encodingIntervalRange Range of encoding interval settings
* @property {number} extension.MPEG4.encodingInterval.min
* @property {number} extension.MPEG4.encodingInterval.max
* @property {object} extension.MPEG4.govLengthRange Range of group of video frames length settings
* @property {number} extension.MPEG4.govLengthRange.min
* @property {number} extension.MPEG4.govLengthRange.max
* @property {object} extension.MPEG4.MPEG4ProfilesSupported List of supported MPEG4 profiles enum {'SP', 'ASP'}
* @property {object} extension.MPEG4.bitrateRange Range of bitrate settings
* @property {number} extension.MPEG4.bitrateRange.min
* @property {number} extension.MPEG4.bitrateRange.max
* @property {object} [extension.H264] Optional H.264 encoder settings ranges
* @property {object} extension.H264.resolutionsAvailable List of supported resolutions
* @property {number} extension.H264.resolutionsAvailable.width
* @property {number} extension.H264.resolutionsAvailable.height
* @property {object} extension.H264.frameRateRange Range of frame rate settings
* @property {number} extension.H264.frameRateRange.min
* @property {number} extension.H264.frameRateRange.max
* @property {object} extension.H264.encodingIntervalRange Range of encoding interval settings
* @property {number} extension.H264.encodingInterval.min
* @property {number} extension.H264.encodingInterval.max
* @property {object} extension.H264.govLengthRange Range of group of video frames length settings
* @property {number} extension.H264.govLengthRange.min
* @property {number} extension.H264.govLengthRange.max
* @property {object} extension.H264.H264ProfilesSupported List of supported H.264 profiles enum {'Baseline', 'Main', 'Extended', 'High'}
* @property {object} extension.H264.bitrateRange Range of bitrate settings
* @property {number} extension.H264.bitrateRange.min
* @property {number} extension.H264.bitrateRange.max
* @property {object} [extension.extension] Even more optional extensions
*/
/**
* @callback Cam~VideoEncoderConfigurationOptionsCallback
* @property {?Error} error
* @property {Cam~VideoEncoderConfigurationOptions} videoEncoderConfigurationOptions
* @property {string} xml Raw SOAP response
*/
/**
* Get video encoder configuration options by video encoder configuration token or media profile token
* If options is omitted, returns camera generic video encoder configuration options
* If both token are set, returns video encoder configuration options compatible with both
* If options is a string it is considered as configurationToken (for backward compatibility)
* @param {object|string} [options]
* @param {string} [options.configurationToken] The video encoder configuration token
* @param {string} [options.profileToken] The media profile token
* @param {Cam~VideoEncoderConfigurationOptionsCallback} callback
*/
Cam.prototype.getVideoEncoderConfigurationOptions = function(options, callback) {
if (callback === undefined) {
callback = options;
if (this.videoEncoderConfigurations && this.videoEncoderConfigurations[0]) {
options = { configurationToken: this.videoEncoderConfigurations[0].$.token };
} else {
return callback(new Error('No video encoder configuration token is present!'));
}
} else if (typeof options == "string") { // For backward compatibility
options = { configurationToken: options };
} else if (!(options && (options.configurationToken || options.profileToken))) {
return callback(new Error("'options' must have one or both 'configurationToken' or 'profileToken'"));
}
this._request({
service: 'media'
, body: this._envelopeHeader() +
(
(options) ?
(
'<GetVideoEncoderConfigurationOptions xmlns="http://www.onvif.org/ver10/media/wsdl">' +
(
(options.configurationToken)
? '<ConfigurationToken>' + options.configurationToken + '</ConfigurationToken>'
: ""
) +
(
(options.profileToken)
? '<ProfileToken>' + options.profileToken + '</ProfileToken>'
: ""
) +
'</GetVideoEncoderConfigurationOptions>'
)
: '<GetVideoEncoderConfigurationOptions xmlns="http://www.onvif.org/ver10/media/wsdl" />'
) +
this._envelopeFooter()
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, err ? null : linerase(data[0].getVideoEncoderConfigurationOptionsResponse[0].options), xml);
}
}.bind(this));
};
/**
* Get all existing video encoder configurations of a device
* @param {Cam~VideoEncoderConfigurationsCallback} callback
*/
Cam.prototype.getVideoEncoderConfigurations = function(callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetVideoEncoderConfigurations xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
this.videoEncoderConfigurations = data[0].getVideoEncoderConfigurationsResponse[0].configurations.map(function(config) {
return linerase(config);
});
}
if (callback) {
callback.call(this, err, this.videoEncoderConfigurations, xml);
}
}.bind(this));
};
/**
* Set the device video encoder configuration
* @param {object} options
* @param {string} [options.token] Token that uniquely references this configuration.
* @param {string} [options.$.token] Token that uniquely references this configuration.
* @param {string} [options.name] User readable name.
* @param {number} [options.useCount] Number of internal references (read-only)
* @param {string} [options.encoding] ( JPEG | H264 | MPEG4 )
* @param {object} [options.resolution] Configured video resolution
* @param {number} options.resolution.width Number of the columns of the Video image
* @param {number} options.resolution.height Number of the lines of the Video image
* @param {number} options.quality Relative value for the video quantizers and the quality of the video
* @param {object} [options.rateControl] Optional element to configure rate control related parameters
* @param {number} options.rateControl.frameRateLimit Maximum output framerate in fps
* @param {number} options.rateControl.encodingInterval Interval at which images are encoded and transmitted (A value of 1 means that every frame is encoded, a value of 2 means that every 2nd frame is encoded ...)
* @param {number} options.rateControl.bitrateLimit the maximum output bitrate in kbps
* @param {object} [options.MPEG4]
* @param {number} options.MPEG4.govLength Determines the interval in which the I-Frames will be coded
* @param {string} options.MPEG4.profile the Mpeg4 profile ( SP | ASP )
* @param {object} [options.H264]
* @param {number} options.H264.govLength Group of Video frames length
* @param {string} options.H264.profile the H.264 profile ( Baseline | Main | Extended | High )
* @param {object} [options.multicast]
* @param {object|number} [options.multicast.address] The multicast address (if this address is set to 0 no multicast streaming is enaled)
* @param {string} options.multicast.address.type Indicates if the address is an IPv4 or IPv6 address ( IPv4 | IPv6)
* @param {string} [options.multicast.address.IPv4Address]
* @param {string} [options.multicast.address.IPv6Address]
* @param {number} [options.multicast.port] The RTP mutlicast destination port
* @param {number} [options.multicast.TTL]
* @param {boolean} [options.multicast.autoStart]
* @param {string} options.sessionTimeout
* @param {Cam~VideoEncoderConfigurationCallback} callback
*/
Cam.prototype.setVideoEncoderConfiguration = function(options, callback) {
if (!options.token && !(options.$ && options.$.token)) {
return callback(new Error('No video encoder configuration token is present!'));
}
this._request({
service: 'media',
body: this._envelopeHeader() +
'<SetVideoEncoderConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<Configuration token = "' + (options.token || options.$.token) + '">' +
( options.name ? '<Name xmlns="http://www.onvif.org/ver10/schema">' + options.name + '</Name>' : '' ) +
( options.useCount ? '<UseCount xmlns="http://www.onvif.org/ver10/schema">' + options.useCount + '</UseCount>' : '' ) +
( options.encoding ? '<Encoding xmlns="http://www.onvif.org/ver10/schema">' + options.encoding + '</Encoding>' : '' ) +
( options.resolution ?
'<Resolution xmlns="http://www.onvif.org/ver10/schema">' +
( options.resolution.width ? '<Width>' + options.resolution.width + '</Width>' : '') +
( options.resolution.height ? '<Height>' + options.resolution.height + '</Height>' : '') +
'</Resolution>' : '') +
( options.quality ? '<Quality xmlns="http://www.onvif.org/ver10/schema">' + options.quality + '</Quality>' : '' ) +
( options.rateControl ?
'<RateControl xmlns="http://www.onvif.org/ver10/schema">' +
( options.rateControl.frameRateLimit ? '<FrameRateLimit>' + options.rateControl.frameRateLimit + '</FrameRateLimit>' : '' ) +
( options.rateControl.encodingInterval ? '<EncodingInterval>' + options.rateControl.encodingInterval + '</EncodingInterval>' : '' ) +
( options.rateControl.bitrateLimit ? '<BitrateLimit>' + options.rateControl.bitrateLimit + '</BitrateLimit>' : '' ) +
'</RateControl>' : '' ) +
( options.MPEG4 ?
'<MPEG4 xmlns="http://www.onvif.org/ver10/schema">' +
( options.MPEG4.govLength ? '<GovLength>' + options.MPEG4.govLength + '</GovLength>' : '' ) +
( options.MPEG4.profile ? '<MPEG4Profile>' + options.MPEG4.profile + '</MPEG4Profile>' : '') +
'</MPEG4>' : '') +
( options.H264 ? '<H264 xmlns="http://www.onvif.org/ver10/schema">' +
( options.H264.govLength ? '<GovLength>' + options.H264.govLength + '</GovLength>' : '' ) +
( options.H264.profile ? '<H264Profile>' + options.H264.profile + '</H264Profile>' : '' ) +
'</H264>' : '') +
( options.multicast ?
'<Multicast xmlns="http://www.onvif.org/ver10/schema">' +
( options.multicast.address ?
'<Address>' +
( options.multicast.address === 0 ? '0' :
( options.multicast.address.type ? '<Type>' + options.multicast.address.type + '</Type>' : '' ) +
( options.multicast.address.IPv4Address ? '<IPv4Address>' + options.multicast.address.IPv4Address + '</IPv4Address>' : '') +
( options.multicast.address.IPv6Address ? '<IPv6Address>' + options.multicast.address.IPv6Address + '</IPv6Address>' : '')
) +
'</Address>' : '') +
( options.multicast.port !== undefined ? '<Port>' + options.multicast.port + '</Port>' : '' ) +
( options.multicast.TTL !== undefined ? '<TTL>' + options.multicast.TTL + '</TTL>' : '') +
( options.multicast.autoStart !== undefined ? '<AutoStart>' + options.multicast.autoStart + '</AutoStart>' : '') +
'</Multicast>' : '' ) +
( options.sessionTimeout ?
'<SessionTimeout xmlns="http://www.onvif.org/ver10/schema">' +
options.sessionTimeout +
'</SessionTimeout>' : '' ) +
'</Configuration>' +
'<ForcePersistence>true</ForcePersistence>' +
'</SetVideoEncoderConfiguration>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (err || linerase(data).setVideoEncoderConfigurationResponse !== '') {
return callback.call(this, linerase(data).setVideoEncoderConfigurationResponse !== ''
? new Error('Wrong `SetVideoEncoderConfiguration` response')
: err, data, xml);
}
//get new encoding settings from device
this.getVideoEncoderConfiguration(options.token || options.$.token, callback);
}.bind(this));
};
/**
* Get all available physical audio iutputs of a device
* @param callback
*/
Cam.prototype.getAudioSources = function(callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetAudioSources xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
this.audioSources = data[0].getAudioSourcesResponse[0].audioSources.map(function(config) {
return linerase(config);
});
}
if (callback) {
callback.call(this, err, this.audioSources, xml);
}
}.bind(this));
};
/**
* Get all available audio encoder configurations of a device
* @param callback
*/
Cam.prototype.getAudioEncoderConfigurations = function(callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetAudioEncoderConfigurations xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
this.audioEncoderConfigurations = data[0].getAudioEncoderConfigurationsResponse[0].configurations.map(function(config) {
return linerase(config);
});
}
if (callback) {
callback.call(this, err, this.audioEncoderConfigurations, xml);
}
}.bind(this));
};
/**
* Get existing audio encoder configuration by its token
* If token is omitted tries first from #audioEncoderConfigurations array
* @param {string} [token] Token of the requested audio encoder configuration
* @param {Cam~AudioEncoderConfigurationCallback} callback
*/
Cam.prototype.getAudioEncoderConfiguration = function(token, callback) {
if (callback === undefined) {
callback = token;
if (this.audioEncoderConfigurations && this.audioEncoderConfigurations[0]) {
token = this.audioEncoderConfigurations[0].$.token;
} else {
return callback(new Error('No audio encoder configuration token is present!'));
}
}
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetAudioEncoderConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ConfigurationToken>' + token + '</ConfigurationToken>' +
'</GetAudioEncoderConfiguration>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, err ? null : linerase(data[0].getAudioEncoderConfigurationResponse[0].configuration), xml);
}
}.bind(this));
};
/**
* Get existing audio encoder configuration options by its token
* If token is omitted tries first from #audioEncoderConfigurations array
* @param {string} [token] Token of the requested audio encoder configuration
* @param {Cam~AudioEncoderConfigurationOptionsCallback} callback
*/
Cam.prototype.getAudioEncoderConfigurationOptions = function(token, callback) {
if (callback === undefined) {
callback = token;
if (this.audioEncoderConfigurations && this.audioEncoderConfigurations[0]) {
token = this.audioEncoderConfigurations[0].$.token;
} else {
return callback(new Error('No audio encoder configuration token is present!'));
}
}
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetAudioEncoderConfigurationOptions xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ConfigurationToken>' + token + '</ConfigurationToken>' +
'</GetAudioEncoderConfigurationOptions>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, err ? null : linerase(data[0].getAudioEncoderConfigurationOptionsResponse[0].options), xml);
}
}.bind(this));
};
/**
* Set the device audio encoder configuration
* @param {object} options
* @param {string} [options.token] Token that uniquely references this configuration.
* @param {string} [options.$.token] Token that uniquely references this configuration.
* @param {string} [options.name] User readable name.
* @param {number} [options.useCount] Number of internal references (read-only)
* @param {string} [options.encoding] { 'G711', 'G726', 'AAC' }
* @param {number} [options.bitrate] The output bitrate in kbps.
* @param {number} [options.sampleRate] The output sample rate in kHz.
* @param {object} [options.multicast]
* @param {object|number} [options.multicast.address] The multicast address (if this address is set to 0 no multicast streaming is enaled)
* @param {string} options.multicast.address.type Indicates if the address is an IPv4 or IPv6 address ( IPv4 | IPv6)
* @param {string} [options.multicast.address.IPv4Address]
* @param {string} [options.multicast.address.IPv6Address]
* @param {number} [options.multicast.port] The RTP mutlicast destination port
* @param {number} [options.multicast.TTL]
* @param {boolean} [options.multicast.autoStart]
* @param {string} options.sessionTimeout
* @param {Cam~AudioEncoderConfigurationCallback} callback
*/
Cam.prototype.setAudioEncoderConfiguration = function(options, callback) {
if (!options.token && !(options.$ && options.$.token)) {
return callback(new Error('No audio encoder configuration token is present!'));
}
this._request({
service: 'media',
body: this._envelopeHeader() +
'<SetAudioEncoderConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<Configuration token = "' + (options.token || options.$.token) + '">' +
( options.name ? '<Name xmlns="http://www.onvif.org/ver10/schema">' + options.name + '</Name>' : '' ) +
( options.useCount ? '<UseCount xmlns="http://www.onvif.org/ver10/schema">' + options.useCount + '</UseCount>' : '' ) +
( options.encoding ? '<Encoding xmlns="http://www.onvif.org/ver10/schema">' + options.encoding + '</Encoding>' : '' ) +
( options.bitrate ? '<Bitrate xmlns="http://www.onvif.org/ver10/schema">' + options.bitrate + '</Bitrate>' : '' ) +
( options.sampleRate ? '<SampleRate xmlns="http://www.onvif.org/ver10/schema">' + options.sampleRate + '</SampleRate>' : '' ) +
( options.multicast ?
'<Multicast xmlns="http://www.onvif.org/ver10/schema">' +
( options.multicast.address ?
'<Address>' +
( options.multicast.address === 0 ? '0' :
( options.multicast.address.type ? '<Type>' + options.multicast.address.type + '</Type>' : '' ) +
( options.multicast.address.IPv4Address ? '<IPv4Address>' + options.multicast.address.IPv4Address + '</IPv4Address>' : '') +
( options.multicast.address.IPv6Address ? '<IPv6Address>' + options.multicast.address.IPv6Address + '</IPv6Address>' : '')
) +
'</Address>' : '') +
( options.multicast.port !== undefined ? '<Port>' + options.multicast.port + '</Port>' : '' ) +
( options.multicast.TTL !== undefined ? '<TTL>' + options.multicast.TTL + '</TTL>' : '') +
( options.multicast.autoStart !== undefined ? '<AutoStart>' + options.multicast.autoStart + '</AutoStart>' : '') +
'</Multicast>' : '' ) +
( options.sessionTimeout ?
'<SessionTimeout xmlns="http://www.onvif.org/ver10/schema">' +
options.sessionTimeout +
'</SessionTimeout>' : '' ) +
'</Configuration>' +
'<ForcePersistence>true</ForcePersistence>' +
'</SetAudioEncoderConfiguration>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (err || linerase(data).setAudioEncoderConfigurationResponse !== '') {
return callback.call(this, linerase(data).setAudioEncoderConfigurationResponse !== ''
? new Error('Wrong `SetAudioEncoderConfiguration` response')
: err, data, xml);
}
this.getAudioEncoderConfiguration(options.token || options.$.token, callback);
}.bind(this));
};
/**
* Get all existing audio source configurations of a device
* @param callback
*/
Cam.prototype.getAudioSourceConfigurations = function(callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetAudioSourceConfigurations xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
this.audioSourceConfigurations = data[0].getAudioSourceConfigurationsResponse[0].configurations.map(function(config) {
return linerase(config);
});
}
if (callback) {
callback.call(this, err, this.audioSourceConfigurations, xml);
}
}.bind(this));
};
/**
* Get all available audio outputs of a device
* @param callback
*/
Cam.prototype.getAudioOutputs = function(callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetAudioOutputs xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
this.audioOutputs = data[0].getAudioOutputsResponse[0].audioOutputs.map(function(config) {
return linerase(config);
});
}
if (callback) {
callback.call(this, err, this.audioOutputs, xml);
}
}.bind(this));
};
/**
* Get all existing audio output configurations of a device
* @param callback
*/
Cam.prototype.getAudioOutputConfigurations = function(callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetAudioOutputConfigurations xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
this.audioOutputConfigurations = data[0].getAudioOutputConfigurationsResponse[0].configurations.map(function(config) {
return linerase(config);
});
}
if (callback) {
callback.call(this, err, this.audioOutputConfigurations, xml);
}
}.bind(this));
};
/**
* @callback Cam~AddAudioEncoderConfigurationCallback
* @property {?Error} error
* @property {null}
* @property {string} xml Raw XML response
*/
/**
* This operation adds an AudioEncoderConfiguration to an existing media profile. If a configuration exists in the media profile, it will be replaced. The change shall be persistent. A device shall support adding a compatible AudioEncoderConfiguration to a profile containing an AudioSourceConfiguration and shall support streaming audio data of such a profile.
* @param {object} options
* @param {string} options.profileToken Reference to the profile where the configuration should be added
* @param {string} options.configurationToken Contains a reference to the AudioSourceConfiguration to add
* @param {Cam~AddAudioEncoderConfigurationCallback} callback
*/
Cam.prototype.addAudioEncoderConfiguration = function(options,callback) {
let body = this._envelopeHeader() +
'<AddAudioEncoderConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ProfileToken>' + options.profileToken + '</ProfileToken>' +
'<ConfigurationToken>' + options.configurationToken + '</ConfigurationToken>' +
'</AddAudioEncoderConfiguration>' +
this._envelopeFooter();
this._request({
service: 'media',
body: body,
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, null, xml);
}
}.bind(this));
};
/**
* @callback Cam~AddAudioSourceConfigurationCallback
* @property {?Error} error
* @property {null}
* @property {string} xml Raw XML response
*/
/**
* This operation adds an AudioSourceConfiguration to an existing media profile. If a configuration exists in the media profile, it will be replaced. The change shall be persistent.
* @param {object} options
* @param {string} options.profileToken Reference to the profile where the configuration should be added
* @param {string} options.configurationToken Contains a reference to the AudioSourceConfiguration to add
* @param {Cam~AddAudioSourceConfigurationCallback} callback
*/
Cam.prototype.addAudioSourceConfiguration = function(options,callback) {
let body = this._envelopeHeader() +
'<AddAudioSourceConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ProfileToken>' + options.profileToken + '</ProfileToken>' +
'<ConfigurationToken>' + options.configurationToken + '</ConfigurationToken>' +
'</AddAudioSourceConfiguration>' +
this._envelopeFooter();
this._request({
service: 'media',
body: body,
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, null, xml);
}
}.bind(this));
};
/**
* @callback Cam~AddVideoEncoderConfigurationCallback
* @property {?Error} error
* @property {null}
* @property {string} xml Raw XML response
*/
/**
* This operation adds a VideoEncoderConfiguration to an existing media profile. If a configuration exists in the media profile, it will be replaced. The change shall be persistent. A device shall support adding a compatible VideoEncoderConfiguration to a Profile containing a VideoSourceConfiguration and shall support streaming video data of such a profile.
* @param {object} options
* @param {string} options.profileToken Reference to the profile where the configuration should be added
* @param {string} options.configurationToken Contains a reference to the VideoEncoderConfiguration to add
* @param {Cam~AddVideoEncoderConfigurationCallback} callback
*/
Cam.prototype.addVideoEncoderConfiguration = function(options,callback) {
let body = this._envelopeHeader() +
'<AddVideoEncoderConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ProfileToken>' + options.profileToken + '</ProfileToken>' +
'<ConfigurationToken>' + options.configurationToken + '</ConfigurationToken>' +
'</AddVideoEncoderConfiguration>' +
this._envelopeFooter();
this._request({
service: 'media',
body: body,
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, null, xml);
}
}.bind(this));
};
/**
* @callback Cam~AddVideoSourceConfigurationCallback
* @property {?Error} error
* @property {null}
* @property {string} xml Raw XML response
*/
/**
* This operation adds a VideoSourceConfiguration to an existing media profile. If such a configuration exists in the media profile, it will be replaced. The change shall be persistent.
* @param {object} options
* @param {string} options.profileToken Reference to the profile where the configuration should be added
* @param {string} options.configurationToken Contains a reference to the VideoSourceConfiguration to add
* @param {Cam~AddVideoSourceConfigurationCallback} callback
*/
Cam.prototype.addVideoSourceConfiguration = function(options,callback) {
let body = this._envelopeHeader() +
'<AddVideoSourceConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ProfileToken>' + options.profileToken + '</ProfileToken>' +
'<ConfigurationToken>' + options.configurationToken + '</ConfigurationToken>' +
'</AddVideoSourceConfiguration>' +
this._envelopeFooter();
this._request({
service: 'media',
body: body,
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, null, xml);
}
}.bind(this));
};
/**
* @callback Cam~RemoveAudioEncoderConfigurationCallback
* @property {?Error} error
* @property {null}
* @property {string} xml Raw XML response
*/
/**
* This operation removes an AudioEncoderConfiguration from an existing media profile. If the media profile does not contain an AudioEncoderConfiguration, the operation has no effect. The removal shall be persistent.
* @param {string} profileToken Contains a reference to the media profile from which the AudioEncoderConfiguration shall be removed.
* @param {Cam~RemoveAudioEncoderConfigurationCallback} callback
*/
Cam.prototype.removeAudioEncoderConfiguration = function(profileToken,callback) {
let body = this._envelopeHeader() +
'<RemoveAudioEncoderConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ProfileToken>' + profileToken + '</ProfileToken>' +
'</RemoveAudioEncoderConfiguration>' +
this._envelopeFooter();
this._request({
service: 'media',
body: body,
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, null, xml);
}
}.bind(this));
};
/**
* @callback Cam~RemoveAudioSourceConfigurationCallback
* @property {?Error} error
* @property {null}
* @property {string} xml Raw XML response
*/
/**
* This operation removes an AudioSourceConfiguration from an existing media profile. If the media profile does not contain an AudioSourceConfiguration, the operation has no effect. The removal shall be persistent. Audio source configurations should only be removed after removing an AudioEncoderConfiguration from the media profile.
* @param {string} profileToken Contains a reference to the media profile from which the AudioOutputConfiguration shall be removed.
* @param {Cam~RemoveAudioSourceConfigurationCallback} callback
*/
Cam.prototype.removeAudioSourceConfiguration = function(profileToken,callback) {
let body = this._envelopeHeader() +
'<RemoveAudioSourceConfiguration xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ProfileToken>' + profileToken + '</ProfileToken>' +
'</RemoveAudioSourceConfiguration>' +
this._envelopeFooter();
this._request({
service: 'media',
body: body,
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, null, xml);
}
}.bind(this));
};
/*
Cam.prototype.getVideoEncoderConfigurationOptions = function(options, callback) {
if (callback === undefined) {callback = options; options = {};}
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetVideoEncoderConfigurationOptions xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
}
if (callback) {
callback.call(this, err, this.videoEncoderConfigurations, xml);
}
}.bind(this));
};
*/
/**
* @typedef {object} Cam~Profile
* @property {object} $
* @property {string} $.token profile token
* @property {boolean} $.fixed is this a system or a user profile
* @property {object} videoSourceConfiguration
* @property {string} videoSourceConfiguration.$.token video source token
* @property {object} videoEncoderConfiguration
* @property {object} PTZConfiguration
* @property {string} PTZConfiguration.$.token PTZ token
* @property {string} PTZConfiguration.name PTZ configuration name
*/
/**
* @callback Cam~GetProfilesCallback
* @property {?Error} error
* @property {Array.<Cam~Profile>} profiles Array of device's profiles
* @property {string} xml Raw XML response
*/
/**
* /Media/ Receive profiles
* @param {Cam~GetProfilesCallback} [callback]
*/
Cam.prototype.getProfiles = function(callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetProfiles xmlns="http://www.onvif.org/ver10/media/wsdl"/>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (!err) {
/**
* Array of all device profiles
* @name Cam#profiles
* @type {Array<Cam~Profile>}
*/
this.profiles = data[0]['getProfilesResponse'][0]['profiles'].map(function(profile) {
return linerase(profile);
});
}
if (callback) {
callback.call(this, err, this.profiles, xml);
}
}.bind(this));
};
/**
* Create an empty new deletable media profile
* @param options
* @param {string} options.name Profile name
* @param {string} [options.token] Profile token
* @param {Cam~MessageCallback} callback
*/
Cam.prototype.createProfile = function(options, callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<CreateProfile xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<Name>' + options.name + '</Name>' +
( options.token ? '<Token>' + options.token + '</Token>' : '' ) +
'</CreateProfile>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, err ? null : linerase(data).createProfileResponse.profile, xml);
}
}.bind(this));
};
/**
* Delete a profile
* @param {string} token
* @param {Cam~MessageCallback} callback
*/
Cam.prototype.deleteProfile = function(token, callback) {
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<DeleteProfile xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<ProfileToken>' + token + '</ProfileToken>' +
'</DeleteProfile>' +
this._envelopeFooter()
}, function(err, data, xml) {
if (callback) {
callback.call(this, err, err ? null : linerase(data).deleteProfileResponse, xml);
}
}.bind(this));
};
/**
* @callback Cam~ResponseUriCallback
* @property {string} uri
*/
/**
* Receive stream URI
* @param {Object} [options]
* @param {string} [options.stream]
* @param {string} [options.protocol]
* @param {string} [options.profileToken]
* @param {Cam~ResponseUriCallback} [callback]
*/
Cam.prototype.getStreamUri = function(options, callback) {
if (callback === undefined) { callback = options; options = {}; }
this._request({
service: 'media'
, body: this._envelopeHeader() +
'<GetStreamUri xmlns="http://www.onvif.org/ver10/media/wsdl">' +
'<StreamSetup>' +
'<Stream xmlns="http://www.onvif.org/ver10/schema">' + (options.stream || 'RTP-Unicast') + '</Stream>' +
'<Transport xmlns="http://www.onvif.org/ver10/schema">' +
'<Protocol>' + (options.protocol || 'RTSP') + '</Protocol>' +