From 8bdf015ce89da09be4ee416e3a3d8d0014890959 Mon Sep 17 00:00:00 2001 From: Moe Alam Date: Sun, 29 Nov 2020 18:08:27 -0800 Subject: [PATCH] onvifDeviceManager : add setVideoConfiguration --- libs/onvifDeviceManager.js | 131 +++++++++++++++++++++++++++++-------- 1 file changed, 105 insertions(+), 26 deletions(-) diff --git a/libs/onvifDeviceManager.js b/libs/onvifDeviceManager.js index 73346393..1f2ce258 100644 --- a/libs/onvifDeviceManager.js +++ b/libs/onvifDeviceManager.js @@ -1,3 +1,6 @@ +const { + mergeDeep +} = require('./common.js') const getDeviceInformation = async (onvifDevice,options) => { // const options = { // protocols: true, @@ -37,6 +40,9 @@ const getDeviceInformation = async (onvifDevice,options) => { if(options.videoEncoders){ response.videoEncoders = await onvifDevice.getVideoEncoderConfigurations().GetVideoEncoderConfigurationsResponse.Configurations } + if(options.videoEncoderOptions){ + response.videoEncoderOptions = await onvifDevice.getVideoEncoderConfigurationOptions().GetVideoEncoderConfigurationOptionsResponse.Options + } }catch(err){ response.ok = false response.error = err @@ -52,7 +58,7 @@ const setPotocols = async (onvifDevice,saveSet) => { ok: false } try{ - const protocols = await getDeviceInformation(onvifDevice,{protocols: true}) + const protocols = await getDeviceInformation(onvifDevice,{protocols: true}).protocols protocols.forEach((item) => { saveSet.forEach((saveItem) => { if(item.Name === saveItem.Name.toUpperCase()){ @@ -81,7 +87,7 @@ const setNetworkInterface = async (onvifDevice,options) => { ok: false } try{ - const networkInterfaces = await getDeviceInformation(onvifDevice,{networkInterface: true}) + const networkInterfaces = await getDeviceInformation(onvifDevice,{networkInterface: true}).networkInterface const onvifResponse = await onvifDevice.setNetworkInterfaces ({ SetNetworkInterfaces: { InterfaceToken: options.interface || networkInterfaces.$.token, @@ -113,7 +119,7 @@ const setGateway = async (onvifDevice,options) => { ok: false } try{ - const gatewayAddress = await getDeviceInformation(onvifDevice,{gateway: true}) + const gatewayAddress = await getDeviceInformation(onvifDevice,{gateway: true}).gateway const onvifResponse = await onvifDevice.setNetworkDefaultGateway({ 'NetworkGateway': [ {'IPv4Address': options.ipv4 || gatewayAddress} @@ -145,7 +151,7 @@ const setDNS = async (onvifDevice,options) => { IPv4Address: item, }) }) - const dnsInfo = await getDeviceInformation(onvifDevice,{dns: true}) + const dnsInfo = await getDeviceInformation(onvifDevice,{dns: true}).dns const onvifResponse = await onvifDevice.setDNS({ 'FromDHCP' : !options.dhcp ? false : true, 'SearchDomain': searchDomain, @@ -167,7 +173,7 @@ const setNTP = async (onvifDevice,options) => { ok: false } try{ - const ntpInfo = await getDeviceInformation(onvifDevice,{ntp: true}) + const ntpInfo = await getDeviceInformation(onvifDevice,{ntp: true}).ntp const ipv4 = options.ipv4 || ntpInfo.NTPManual.IPv4Address const onvifResponse = await onvifDevice.setNTP({ FromDHCP: !options.dhcp ? false : true, @@ -188,7 +194,7 @@ const setHostname = async (onvifDevice,options) => { ok: false } try{ - const hostname = options.name || await getDeviceInformation(onvifDevice,{hostname: true}) + const hostname = options.name || await getDeviceInformation(onvifDevice,{hostname: true}).hostname const onvifResponse = await onvifDevice.setHostname({ Name: hostname }) @@ -277,26 +283,98 @@ const deleteUser = async (onvifDevice,options) => { } return response } -// const setVideoConfiguration = async (onvifDevice,options) => { -// // const options = { -// // name: 'user1', -// // } -// const response = { -// ok: false -// } -// try{ -// const onvifResponse = await onvifDevice.deleteUsers({ -// 'User' : [ -// {'Username': options.name} -// ] -// }) -// response.ok = true -// response.onvifResponse = onvifResponse -// }catch(err){ -// response.error = err -// } -// return response -// } +const validateEncoderOptions = (chosenOptions, videoEncoderOptions) => { + const resolutions = [] + const minQuality = parseInt(videoEncoderOptions.QualityRange.Min) || 1 + const maxQuality = parseInt(videoEncoderOptions.QualityRange.Max) || 6 + const quality = !isNaN(chosenOptions.quality) ? parseInt(chosenOptions.quality) : 1 + const minGovLength = parseInt(videoEncoderOptions.H264.GovLengthRange.Min) || 0 + const maxGovLength = parseInt(videoEncoderOptions.H264.GovLengthRange.Max) || 100 + const govLength = !isNaN(chosenOptions.h264.govLength) ? parseInt(chosenOptions.h264.govLength) : 10 + const minFrameRateLimit = parseInt(videoEncoderOptions.H264.FrameRateRange.Min) || 1 + const maxFrameRateLimit = parseInt(videoEncoderOptions.H264.FrameRateRange.Max) || 30 + const frameRateLimit = !isNaN(chosenOptions.rateControl.frameRateLimit) ? parseInt(chosenOptions.rateControl.frameRateLimit) : 15 + const minEncodingInterval = parseInt(videoEncoderOptions.H264.EncodingIntervalRange.Min) || 1 + const maxEncodingInterval = parseInt(videoEncoderOptions.H264.EncodingIntervalRange.Max) || 30 + const encodingInterval = !isNaN(chosenOptions.rateControl.encodingInterval) ? parseInt(chosenOptions.rateControl.encodingInterval) : 1 + videoEncoderOptions.H264.ResolutionsAvailable.forEach((resolution) => { + resolutions.push(`${resolution.Width}x${resolution.Height}`) + }) + if(resolutions.indexOf(`${chosenOptions.resolution.width}x${chosenOptions.resolution.height}`) > -1){ + chosenOptions.resolution.width = resolution[0].Width + chosenOptions.resolution.height = resolution[0].Height + } + if(quality < minQuality || quality > maxQuality){ + chosenOptions.quality = 1 + } + if(govLength < minGovLength || govLength > maxGovLength){ + chosenOptions.h264.govLength = 10 + } + if(frameRateLimit < minFrameRateLimit || frameRateLimit > maxFrameRateLimit){ + chosenOptions.rateControl.frameRateLimit = 15 + } + if(encodingInterval < minEncodingInterval || encodingInterval > maxEncodingInterval){ + chosenOptions.rateControl.encodingInterval = 1 + } +} +const setVideoConfiguration = async (onvifDevice,options) => { + const response = { + ok: false + } + try{ + const { + videoEncoders, + videoEncoderOptions + } = await getDeviceInformation(onvifDevice,{ + videoEncoders: true, + videoEncoderOptions: true + }) + const videoEncoderIndex = {} + videoEncoders.forEach((encoder) => {videoEncoderIndex[encoder.$.token] = encoder}) + const chosenToken = `${options.token || videoEncoders[0].$.token}` + const videoEncoder = videoEncoderIndex[chosenToken] + const onvifParams = mergeDeep({ + name: videoEncoder.Name, + encoding: videoEncoder.Encoding, + resolution: { + width: videoEncoder.Resolution.Width, + height: videoEncoder.Resolution.Height, + }, + quality: videoEncoder.Quality, + rateControl: { + frameRateLimit: videoEncoder.RateControl.FrameRateLimit, + encodingInterval: videoEncoder.RateControl.EncodingInterval, + bitrateLimit: videoEncoder.RateControl.BitrateLimit, + }, + multicast: { + address: { + type: videoEncoder.Multicast.Address.Type, + ipv4Address: videoEncoder.Multicast.Address.IPv4Address, + }, + port: videoEncoder.Multicast.Port, + ttl: videoEncoder.Multicast.TTL, + autoStart: videoEncoder.Multicast.AutoStart, + }, + sessionTimeout: videoEncoder.SessionTimeout, + h264: { + govLength: videoEncoder.H264.GovLength, + h264Profile: videoEncoder.H264.H264Profile, + } + },options,{ + multicast: { + autoStart: !options.multicast.autoStart ? 'false' : 'true' + }, + token: undefined + }) + const validatedEncoderOptions = validateEncoderOptions(onvifParams, videoEncoderOptions) + const onvifResponse = await onvifDevice.setVideoEncoderConfiguration(chosenToken, onvifParams) + response.ok = true + response.onvifResponse = onvifResponse + }catch(err){ + response.error = err + } + return response +} module.exports = { getDeviceInformation: getDeviceInformation, setHostname: setHostname, @@ -308,4 +386,5 @@ module.exports = { setDateAndTime: setDateAndTime, createUser: createUser, deleteUser: deleteUser, + setVideoConfiguration: setVideoConfiguration, }