Skip to content

Commit

Permalink
Video: Fix settings not displaying properly
Browse files Browse the repository at this point in the history
  • Loading branch information
stephendade committed Feb 4, 2024
1 parent b17cc40 commit 3d0f5a0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 60 deletions.
76 changes: 30 additions & 46 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,43 +427,27 @@ app.get('/api/softwareinfo', (req, res) => {

app.get('/api/videodevices', (req, res) => {
vManager.populateAddresses()
vManager.getVideoDevices((err, devices, active, seldevice, selRes, selRot, selbitrate, selfps, SeluseUDP, SeluseUDPIP, SeluseUDPPort, timestamp) => {
vManager.getVideoDevices((err, devices, active, seldevice, selRes, selRot, selbitrate, selfps, SeluseUDP, SeluseUDPIP, SeluseUDPPort, timestamp, fps, FPSMax, vidres) => {
if (!err) {
res.setHeader('Content-Type', 'application/json')
if (!active) {
res.send(JSON.stringify({
dev: devices,
vidDeviceSelected: ((devices.length > 0) ? devices[0] : []),
vidres: ((devices.length > 0) ? devices[0].caps : []),
vidResSelected: ((devices.length > 0) ? devices[0].caps[0] : []),
// fpsSelected: ((devices.length > 0) ? devices[0].caps[0].fpsmax : 1),
fpsSelected: ((devices.length > 0) ? (devices[0].caps[0].fpsmax === 0 ? devices[0].caps[0].fps[0] : devices[0].caps[0].fpsmax) : 1),
streamingStatus: active,
streamAddresses: vManager.deviceAddresses,
UDPChecked: SeluseUDP,
useUDPIP: SeluseUDPIP,
useUDPPort: SeluseUDPPort,
timestamp,
error: null
}))
} else {
res.send(JSON.stringify({
dev: devices,
vidDeviceSelected: seldevice,
vidres: seldevice.caps,
vidResSelected: selRes,
streamingStatus: active,
streamAddresses: vManager.deviceAddresses,
rotSelected: selRot,
bitrate: selbitrate,
fpsSelected: selfps,
UDPChecked: SeluseUDP,
useUDPIP: SeluseUDPIP,
useUDPPort: SeluseUDPPort,
timestamp,
error: null
}))
}
res.send(JSON.stringify({
dev: devices,
vidDeviceSelected: seldevice,
vidres: vidres,
vidResSelected: selRes,
streamingStatus: active,
streamAddresses: vManager.deviceAddresses,
rotSelected: selRot,
bitrate: selbitrate,
fpsSelected: selfps,
UDPChecked: SeluseUDP,
useUDPIP: SeluseUDPIP,
useUDPPort: SeluseUDPPort,
timestamp,
error: null,
fps: fps,
FPSMax: FPSMax
}))
} else {
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify({ error: err }))
Expand Down Expand Up @@ -703,17 +687,17 @@ app.get('/api/networkconnections', (req, res) => {
})

app.post('/api/startstopvideo', [check('active').isBoolean(),
check('device').isLength({ min: 2 }),
check('height').isInt({ min: 1 }),
check('width').isInt({ min: 1 }),
check('useUDP').isBoolean(),
check('useTimestamp').isBoolean(),
check('useUDPPort').isPort(),
check('useUDPIP').isIP(),
check('bitrate').isInt({ min: 50, max: 50000 }),
check('format').isIn(['video/x-raw', 'video/x-h264', 'image/jpeg']),
check('fps').isInt({ min: -1, max: 100 }),
check('rotation').isInt().isIn([0, 90, 180, 270])], (req, res) => {
check('device').if(check('active').isIn([true])).isLength({ min: 2 }),
check('height').if(check('active').isIn([true])).isInt({ min: 1 }),
check('width').if(check('active').isIn([true])).isInt({ min: 1 }),
check('useUDP').if(check('active').isIn([true])).isBoolean(),
check('useTimestamp').if(check('active').isIn([true])).isBoolean(),
check('useUDPPort').if(check('active').isIn([true])).isPort(),
check('useUDPIP').if(check('active').isIn([true])).isIP(),
check('bitrate').if(check('active').isIn([true])).isInt({ min: 50, max: 50000 }),
check('format').if(check('active').isIn([true])).isIn(['video/x-raw', 'video/x-h264', 'image/jpeg']),
check('fps').if(check('active').isIn([true])).isInt({ min: -1, max: 100 }),
check('rotation').if(check('active').isIn([true])).isInt().isIn([0, 90, 180, 270])], (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
winston.error('Bad POST vars in /api/startstopvideo ', { message: errors.array() })
Expand Down
38 changes: 31 additions & 7 deletions server/videostream.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ class videoStream {
(err, status, addresses) => {
if (err) {
// failed setup, reset settings
console.log('Reset video4')
this.resetVideo()
}
})
} else {
// failed setup, reset settings
console.log('Reset video3')
this.resetVideo()
console.log(error)
}
Expand All @@ -55,6 +57,7 @@ class videoStream {
// video streaming
getVideoDevices (callback) {
// get all video device details
// callback is: err, devices, active, seldevice, selRes, selRot, selbitrate, selfps, SeluseUDP, SeluseUDPIP, SeluseUDPPort, timestamp, fps, FPSMax, vidres
exec('python3 ./python/gstcaps.py', (error, stdout, stderr) => {
const warnstrings = ['DeprecationWarning', 'gst_element_message_full_with_details', 'camera_manager.cpp', 'Unsupported V4L2 pixel format']
if (stderr && !warnstrings.some(wrn => stderr.includes(wrn))) {
Expand All @@ -67,28 +70,45 @@ class videoStream {
this.devices = JSON.parse(stdout)
console.log(this.devices)
this.winston.info(this.devices)
const fpsSelected = ((this.devices.length > 0) ? (this.devices[0].caps[0].fpsmax === 0 ? this.devices[0].caps[0].fps[0] : this.devices[0].caps[0].fpsmax) : 1)
// and return current settings
if (!this.active) {
return callback(null, this.devices, this.active, null, null, null, null, null, false, '127.0.0.1', 5400, false)
return callback(null, this.devices, this.active, this.devices[0], this.devices[0].caps[0],
{ label: '0°', value: 0 }, 1100, fpsSelected, false, '127.0.0.1', 5400, false,
this.devices[0].caps[0].fps, this.devices[0].caps[0].fpsmax, this.devices[0].caps)
} else {
// format saved settings
const seldevice = this.devices.filter(it => it.value === this.savedDevice.device)
if (seldevice.length !== 1) {
// bad settings
console.error('Bad video settings. Resetting')
console.error('Bad video settings1 Resetting')
this.winston.error('Bad video settings. Resetting ', { message: this.savedDevice })
this.resetVideo()
return callback(null, this.devices, this.active, null, null, null, null, null, false, '127.0.0.1', 5400, false)
return callback(null, this.devices, this.active, this.devices[0], this.devices[0].caps[0],
{ label: '0°', value: 0 }, 1100, fpsSelected, false, '127.0.0.1', 5400, false,
this.devices[0].caps[0].fps, this.devices[0].caps[0].fpsmax, this.devices[0].caps)
}
const selRes = seldevice[0].caps.filter(it => it.value === this.savedDevice.width.toString() + 'x' + this.savedDevice.height.toString() + 'x' + this.savedDevice.format.toString().split('/')[1])
let selFPS = this.savedDevice.fps
if (selRes[0].fpsmax === 0) {
selFPS = selRes[0].fps.filter(it => parseInt(it.value) === this.savedDevice.fps)[0]
}
const selRes = seldevice[0].caps.filter(it => it.value === this.savedDevice.width.toString() + 'x' + this.savedDevice.height.toString())
if (seldevice.length === 1 && selRes.length === 1) {
return callback(null, this.devices, this.active, seldevice[0], selRes[0], { label: this.savedDevice.rotation.toString() + '°', value: this.savedDevice.rotation }, this.savedDevice.bitrate, this.savedDevice.fps, this.savedDevice.useUDP, this.savedDevice.useUDPIP, this.savedDevice.useUDPPort, this.savedDevice.useTimestamp)
this.populateAddresses(seldevice[0].value.replace(/\W/g, ''))
console.log(seldevice[0])
return callback(null, this.devices, this.active, seldevice[0], selRes[0],
{ label: this.savedDevice.rotation.toString() + '°', value: this.savedDevice.rotation },
this.savedDevice.bitrate, selFPS, this.savedDevice.useUDP, this.savedDevice.useUDPIP,
this.savedDevice.useUDPPort, this.savedDevice.useTimestamp, selRes[0].fps,
selRes[0].fpsmax, seldevice[0].caps)
} else {
// bad settings
console.error('Bad video settings. Resetting')
console.error('Bad video settings. Resetting' + seldevice + ', ' + selRes)
this.winston.error('Bad video settings. Resetting ', { message: this.savedDevice })
this.resetVideo()
return callback(null, this.devices, this.active, null, null, null, null, null, false, '127.0.0.1', 5400, false)
return callback(null, this.devices, this.active, this.devices[0], this.devices[0].caps[0],
{ label: '0°', value: 0 }, 1100, fpsSelected, false, '127.0.0.1', 5400, false,
this.devices[0].caps[0].fps, this.devices[0].caps[0].fpsmax, this.devices[0].caps)
}
}
}
Expand Down Expand Up @@ -155,6 +175,9 @@ class videoStream {
this.winston.info('No video device: ' + device)
return callback(new Error('No video device: ' + device))
}
} else {
console.log('No video devices in list')
this.winston.info('No video devices in list')
}

this.active = true
Expand Down Expand Up @@ -235,6 +258,7 @@ class videoStream {

return callback(null, this.active, this.deviceAddresses)
} else {
// stop streaming
this.deviceStream.stdin.pause()
this.deviceStream.kill()
this.resetVideo()
Expand Down
15 changes: 9 additions & 6 deletions server/videostream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ describe('Video Functions', function () {
const vManager = new VideoStream(settings, winston)

vManager.populateAddresses()
vManager.getVideoDevices(function (err, devices, active, seldevice, selRes, selRot, selbitrate, selfps, SeluseUDP, SeluseUDPIP, SeluseUDPPort, timestamp) {
vManager.getVideoDevices(function (err, devices, active, seldevice, selRes, selRot, selbitrate, selfps, SeluseUDP, SeluseUDPIP, SeluseUDPPort, timestamp, fps, FPSMax, vidres) {
assert.equal(err, null)
assert.equal(active, false)
assert.equal(seldevice, null)
assert.equal(selRes, null)
assert.equal(selRot, null)
assert.equal(selbitrate, null)
assert.equal(selfps, null)
assert.notEqual(seldevice, null)
assert.notEqual(selRes, null)
assert.notEqual(selRot, null)
assert.notEqual(selbitrate, null)
assert.notEqual(selfps, null)
assert.equal(SeluseUDP, false)
assert.equal(SeluseUDPIP, '127.0.0.1')
assert.equal(SeluseUDPPort, 5400)
assert.equal(timestamp, false)
assert.notEqual(fps, null)
assert.notEqual(FPSMax, null)
assert.notEqual(vidres, null)
done()
})
}).timeout(5000)
Expand Down
2 changes: 1 addition & 1 deletion src/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class VideoPage extends basePage {
}

componentDidMount() {
fetch(`/api/videodevices`).then(response => response.json()).then(state => { this.setState(state); this.loadDone(); this.handleVideoChange(state.vidDeviceSelected, "") });
fetch(`/api/videodevices`).then(response => response.json()).then(state => { this.setState(state); this.loadDone() });
}

handleVideoChange = (value, action) => {
Expand Down

0 comments on commit 3d0f5a0

Please sign in to comment.