Skip to content

Commit

Permalink
AP_Scripting: set-video-stream-information gets params
Browse files Browse the repository at this point in the history
  • Loading branch information
rmackay9 committed Nov 18, 2024
1 parent 2edbff1 commit 9fb244c
Showing 1 changed file with 194 additions and 15 deletions.
209 changes: 194 additions & 15 deletions libraries/AP_Scripting/examples/set_VIDEO_STREAM_INFORMATION.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,195 @@
--[[
Populate the fields of the VIDEO_STREAM_INFORMATION message sent by the selected camera instance.
Populate the fields of the VIDEO_INFORMATION message sent by the selected camera instance.
--]]
function set_video_stream_information()

local PARAM_TABLE_KEY = 87
local PARAM_TABLE_PREFIX = "VID1_"

local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7}
local VID_TYPE_ENUM = {RTSP=0, RTPUDP=1, TCP_MPEG=2, MPEG_TS=3}
local CAMMODEL_ENUM = {UNKNOWN=0, SIYI_A8=1}

-- add a parameter and bind it to a variable
function bind_add_param(name, idx, default_value)
assert(param:add_param(PARAM_TABLE_KEY, idx, name, default_value), string.format('could not add param %s', name))
return Parameter(PARAM_TABLE_PREFIX .. name)
end

-- setup script specific parameters
assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 15), 'could not add param table')

--[[
// @Param: VID1_CAMMODEL
// @DisplayName: Camera1 Video Stream Camera Model
// @Description: Video stream camera model
// @Values: 0:Unknown, 1:SiyiA8
// @User: Standard
--]]
-- values should match CAMMODEL_ENUM
local VID1_CAMMODEL = bind_add_param('CAMMODEL', 1, 0)

--[[
// @Param: VID1_ID
// @DisplayName: Camera1 Video Stream Id
// @Description: Video stream id
// @Range: 0 50
// @User: Standard
--]]
local VID1_ID = bind_add_param('ID', 2, 1)

--[[
// @Param: VID1_TYPE
// @DisplayName: Camera1 Video Stream Type
// @Description: Video stream type
// @Values: 0:RTSP, 1:RTPUDP, 2:TCP_MPEG, 3:MPEG_TS
// @User: Standard
--]]
-- values should match VID_TYPE_ENUM
local VID1_TYPE = bind_add_param('TYPE', 3, 0)

--[[
// @Param: VID1_FLAG
// @DisplayName: Camera1 Video Stream Flags
// @Description: Video stream flags
// @Bitmask: 0:Running,1:Thermal,2:Thermal Range Enabled
// @User: Standard
--]]
local VID1_FLAG = bind_add_param('FLAG', 4, 1)

--[[
// @Param: VID1_FRAME_RATE
// @DisplayName: Camera1 Video Stream Frame Rate
// @Description: Video stream frame rate
// @Range: 0 50
// @User: Standard
--]]
local VID1_FR = bind_add_param('FRAME_RATE', 5, 30)

--[[
// @Param: VID1_HRES
// @DisplayName: Camera1 Video Stream Horizontal Resolution
// @Description: Video stream horizontal resolution
// @Range: 0 4096
// @User: Standard
--]]
local VID1_HRES = bind_add_param('HRES', 6, 1920)

--[[
// @Param: VID1_VRES
// @DisplayName: Camera1 Video Stream Vertical Resolution
// @Description: Video stream vertical resolution
// @Range: 0 4096
// @User: Standard
--]]
local VID1_VRES = bind_add_param('VRES', 7, 1080)

--[[
// @Param: VID1_BITRATE
// @DisplayName: Camera1 Video Stream Bitrate
// @Description: Video stream bitrate
// @Range: 0 10000
// @User: Standard
--]]
local VID1_BITR = bind_add_param('BITRATE', 8, 1500)

--[[
// @Param: VID1_HFOV
// @DisplayName: Camera1 Video Stream Horizontal FOV
// @Description: Video stream horizontal FOV
// @Range: 0 360
// @User: Standard
--]]
local VID1_HFOV = bind_add_param('HFOV', 9, 50)

--[[
// @Param: VID1_ENCODING
// @DisplayName: Camera1 Video Stream Encoding
// @Description: Video stream encoding
// @Values: 0:Unknown, 1:H264, 2:H265
// @User: Standard
--]]
local VID1_ENC = bind_add_param('ENCODING', 10, 1)

--[[
// @Param: VID1_IPADDR0
// @DisplayName: Camera1 Video Stream IP Address 0
// @Description: Video stream IP Address first octet
// @Range: 0 255
// @User: Standard
--]]
local VID1_IPADDR0 = bind_add_param('IPADDR0', 11, 127)

--[[
// @Param: VID1_IPADDR1
// @DisplayName: Camera1 Video Stream IP Address 1
// @Description: Video stream IP Address second octet
// @Range: 0 255
// @User: Standard
--]]
local VID1_IPADDR1 = bind_add_param('IPADDR1', 12, 0)

--[[
// @Param: VID1_IPADDR2
// @DisplayName: Camera1 Video Stream IP Address 2
// @Description: Video stream IP Address third octet
// @Range: 0 255
// @User: Standard
--]]
local VID1_IPADDR2 = bind_add_param('IPADDR2', 13, 0)

--[[
// @Param: VID1_IPADDR3
// @DisplayName: Camera1 Video Stream IP Address 3
// @Description: Video stream IP Address fourth octet
// @Range: 0 255
// @User: Standard
--]]
local VID1_IPADDR3 = bind_add_param('IPADDR3', 14, 1)

--[[
// @Param: VID1_IPPORT
// @DisplayName: Camera1 Video Stream IP Address Port
// @Description: Video stream IP Address Port
// @Range: 0 65535
// @User: Standard
--]]
local VID1_IPPORT = bind_add_param('IPPORT', 15, 5600)

function set_video_stream_information()
-- set the Video Stream Information data
local stream_info = mavlink_video_stream_information_t()

local INSTANCE = 0
local name = 'Video'
local uri = '127.0.0.1:5600'

stream_info:framerate(30)
stream_info:bitrate(1500)
stream_info:flags(1) -- VIDEO_STREAM_STATUS_FLAGS_RUNNING
stream_info:resolution_h(1920)
stream_info:resolution_v(1080)
stream_info:rotation(0)
stream_info:hfov(50)
stream_info:stream_id(1)
stream_info:count(1)
stream_info:type(1) -- VIDEO_STREAM_TYPE_RTPUDP

-- calculate uri
local uri_prefix = ''
if VID1_TYPE:get() == VID_TYPE_ENUM.RTSP then
uri_prefix = 'rtsp://'
end
local uri_ip = math.floor(VID1_IPADDR0:get()) .. '.' ..
math.floor(VID1_IPADDR1:get()) .. '.' ..
math.floor(VID1_IPADDR2:get()) .. '.' ..
math.floor(VID1_IPADDR3:get()) .. ':' ..
math.floor(VID1_IPPORT:get())
local uri_suffix = ''
if VID1_CAMMODEL:get() == 1 then
uri_suffix = '/main.264'
end
local uri = uri_prefix .. uri_ip .. uri_suffix

stream_info:stream_id(VID1_ID:get())
stream_info:count(1) -- hard coded to just a single stream
stream_info:type(VID1_TYPE:get())
stream_info:flags(VID1_FLAG:get())
stream_info:framerate(VID1_FR:get())
stream_info:resolution_h(VID1_HRES:get())
stream_info:resolution_v(VID1_VRES:get())
stream_info:bitrate(VID1_BITR:get())
stream_info:rotation(0) -- video image rotation clockwise, hardcoded to zero
stream_info:hfov(VID1_HFOV:get())
stream_info:encoding(VID1_ENC:get())

for i = 0, #name do
stream_info:name(i, name:byte(i+1))
end
Expand All @@ -27,7 +198,15 @@
end

camera:set_stream_information(INSTANCE, stream_info)
end

-- print welcome message
gcs:send_text(MAV_SEVERITY.INFO, "Loaded set_VIDEO_INFORMATION.lua")

-- update function runs every 3 secs
function update()
set_video_stream_information()
return update, 3000
end

return set_video_stream_information()
return update()

0 comments on commit 9fb244c

Please sign in to comment.