From b5312edb90d58d61166887e2e0a80d99ce2afaf1 Mon Sep 17 00:00:00 2001 From: Andrew King Date: Tue, 13 Oct 2020 04:34:26 +0700 Subject: [PATCH 1/3] Dissector update and some notes about BC message (#80) Co-authored-by: twistedddx --- dissector/baichuan.lua | 379 +++++-- dissector/mediapacket.md | 111 ++ dissector/messages.md | 2083 ++++++++++++++++++++++++++++++++++++++ dissector/protocol.md | 235 +++++ 4 files changed, 2729 insertions(+), 79 deletions(-) create mode 100644 dissector/mediapacket.md create mode 100644 dissector/messages.md create mode 100644 dissector/protocol.md diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index 6657992..aab5d6f 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -7,8 +7,13 @@ bc_protocol = Proto("Baichuan", "Baichuan/Reolink IP Camera Protocol") magic_bytes = ProtoField.int32("baichuan.magic", "magic", base.DEC) message_id = ProtoField.int32("baichuan.msg_id", "messageId", base.DEC) message_len = ProtoField.int32("baichuan.msg_len", "messageLen", base.DEC) -xml_enc_offset = ProtoField.int32("baichuan.xml_encryption_offset", "xmlEncryptionOffset", base.DEC) -xml_enc_used = ProtoField.bool("baichuan.xml_encryption_used", "encrypted", base.NONE) +xml_enc_offset = ProtoField.int8("baichuan.xml_encryption_offset", "xmlEncryptionOffset", base.DEC) +encrypt_xml = ProtoField.bool("baichuan.encrypt_xml", "encrypt_xml", base.NONE) +channel_id = ProtoField.int8("baichuan.channel_id", "channel_id", base.DEC) +stream_id = ProtoField.int8("baichuan.stream_id", "streamID", base.DEC) +unknown = ProtoField.int8("baichuan.unknown", "unknown", base.DEC) +msg_handle = ProtoField.int8("baichuan.message_handle", "messageHandle", base.DEC) +status_code = ProtoField.int16("baichuan.status_code", "status_code", base.DEC) message_class = ProtoField.int32("baichuan.msg_class", "messageClass", base.DEC) f_bin_offset = ProtoField.int32("baichuan.bin_offset", "binOffset", base.DEC) username = ProtoField.string("baichuan.username", "username", base.ASCII) @@ -19,7 +24,12 @@ bc_protocol.fields = { message_id, message_len, xml_enc_offset, - xml_enc_used, + channel_id, + stream_id, + unknown, + msg_handle, + encrypt_xml, + status_code, message_class, f_bin_offset, username, @@ -27,16 +37,99 @@ bc_protocol.fields = { } message_types = { - [1]="login", + [1]="login", -- / / + [2]="logout", [3]=" (video)", - [58]="", - [78]="", - [79]="", + [4]=" (stop)", + [5]=" (replay)", + [7]=" (stop)", + [8]=" (DL Video)", + [10]="", + [13]=" (download)", + [14]="", + [15]="", + [16]="", + [18]="", + [25]=" (write)", + [26]="", -- + [31]="Start Motion Alarm", + [33]="", + [36]=" (write)", + [37]="", -- //// + [38]="", + [39]=" (write)", + [40]="", + [41]=" (write)", + [42]="", + [43]=" (write)", + [44]="", -- + [45]=" (write)", + [46]="", + [47]=" (write)", + [50]="", + [51]=" (write)", + [52]=" (priv mask)", + [53]=" (write)", + [54]="", + [55]=" (write)", + [56]="", + [57]=" (write)", + [58]="", -- + [59]=" (write)", + [65]=" (Export)", + [66]=" (Import)", + [68]="", + [69]=" (write)", + [70]="", + [71]=" (write)", + [76]="", -- // + [77]=" (write)", + [78]=" (IPC desc)", + [79]=" (ptz)", [80]="", + [81]=" (schedule)", + [82]=" (write)", + [83]="", + [84]=" (write)", + [91]="", + [92]=" (write)", + [93]="", + [97]="", + [98]=" (write)", + [100]=" (write)", + [101]="", + [102]="", + [104]="", + [105]=" (write)", + [106]="", + [107]=" (write)", + [108]=" (log)", [114]="", + [115]="", + [120]="", + [122]="", + [123]="", + [132]="", -- + [133]="", + [141]=" (test)", + [142]="", + [145]="", [146]="", [151]="", + [190]="PTZ Preset", + [194]=" (test)", + [199]="", + [208]="", + [209]=" (write)", + [210]="", + [211]=" (write)", + [219]="", + [228]="", + [229]=" (write)", [230]="", + [272]="", + [273]="", + [274]="", } message_classes = { @@ -54,7 +147,7 @@ header_lengths = { } function xml_encrypt(ba, offset) - local key = "\031\045\060\075\090\105\120\255" + local key = "\031\045\060\075\090\105\120\255" -- 1f, 2d, 3c, 4b, 5a, 69, 78 ,ff local e = ByteArray.new() e:set_size(ba:len()) for i=0,ba:len() - 1 do @@ -63,103 +156,231 @@ function xml_encrypt(ba, offset) return e end -function bc_protocol.dissector(buffer, pinfo, tree) - length = buffer:len() - if length == 0 then return end - - if buffer:len() < 20 then - -- Need more bytes but we don't have a header to learn how many bytes - pinfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT - pinfo.desegment_offset = 0 - return - end - - pinfo.cols.protocol = bc_protocol.name - +function get_header_len(buffer) local magic = buffer(0, 4):le_uint() if magic ~= 0x0abcdef0 then - -- Case 3, capture started in middle of packet, - -- from https://wiki.wireshark.org/Lua/Dissectors#TCP_reassembly - -- The camera always seems to emit a new TCP packet for a new message - return 0 + return -1 -- No header found end - - local msg_type = buffer(4, 4):le_uint() - local msg_type_str = message_types[msg_type] or "unknown" - local msg_len = buffer(8, 4):le_uint() - local enc_offset = buffer(12, 4):le_uint() - local msg_cls = buffer(18, 2):le_uint() - local encrypted = (msg_cls == 0x6414 or buffer(16, 1):le_uint() ~= 0) - local class = message_classes[buffer(18, 2):le_uint()] local header_len = header_lengths[buffer(18, 2):le_uint()] + return header_len +end - if buffer:len() ~= msg_len + header_len then - -- Case 1, need more bytes, - -- from https://wiki.wireshark.org/Lua/Dissectors#TCP_reassembly - pinfo.desegment_len = msg_len + header_len - buffer:len() - pinfo.desegment_offset = 0 - return buffer:len() - end - +function get_header(buffer) -- bin_offset is either nil (no binary data) or nonzero -- TODO: bin_offset is actually stateful! local bin_offset = nil + local status_code = nil + local encrypt_xml = nil + local header_len = header_lengths[buffer(18, 2):le_uint()] + local msg_type = buffer(4, 4):le_uint() if header_len == 24 then - bin_offset = buffer(20, 4):le_uint() - if bin_offset == 0 then bin_offset = nil end + bin_offset = buffer(20, 4):le_uint() -- if NHD-805/806 legacy protocol 30 30 30 30 aka "0000" + status_code = buffer(16, 2):le_uint() + else + encrypt_xml = buffer(16, 1):le_uint() end + local msg_type = buffer(4, 4):le_uint() + local stream_text = "HD (Clear)" + if buffer(13, 1):le_uint() == 1 then + stream_text = "SD (Fluent)" + end + return { + magic = buffer(0, 4):le_uint(), + msg_type = buffer(4, 4):le_uint(), + msg_type_str = message_types[msg_type] or "unknown", + msg_len = buffer(8, 4):le_uint(), + encrypt_xml = encrypt_xml, + channel_id = buffer(12, 1):le_uint(), + enc_offset = buffer(12, 1):le_uint(), + stream_type = stream_text, + unknown = buffer(14, 1):le_uint(), + msg_handle = buffer(15, 1):le_uint(), + msg_cls = buffer(18, 2):le_uint(), + status_code = status_code, + class = message_classes[buffer(18, 2):le_uint()], + header_len = header_lengths[buffer(18, 2):le_uint()], + bin_offset = bin_offset, + } +end - local bc_subtree = tree:add(bc_protocol, buffer(), - "Baichuan IP Camera Protocol, " .. msg_type_str .. " message") - local header = bc_subtree:add(bc_protocol, buffer(0, header_len), - "Baichuan Message Header, length: " .. header_len) - - pinfo.cols['info'] = msg_type_str .. ", type " .. msg_type .. ", " .. msg_len .. " bytes" - +function process_header(buffer, headers_tree) + local header_data = get_header(buffer) + local header = headers_tree:add(bc_protocol, buffer(0, header_len), + "Baichuan Message Header, length: " .. header_data.header_len .. ", type " .. header_data.msg_type) + local stream_text = " HD (Clear)" + if buffer(13, 1):le_uint() == 1 then + stream_text = " SD (Fluent)" + end header:add_le(magic_bytes, buffer(0, 4)) header:add_le(message_id, buffer(4, 4)) - :append_text(" (" .. msg_type_str .. ")") + :append_text(" (" .. header_data.msg_type_str .. ")") header:add_le(message_len, buffer(8, 4)) - header:add_le(xml_enc_offset, buffer(12, 4)) - :append_text(" (& 0xF == " .. bit32.band(enc_offset, 0xF) .. ")") - header:add_le(xml_enc_used, buffer(16, 1)) - header:add_le(message_class, buffer(18, 2)) - :append_text(" (" .. class .. ")") - header:add_le(f_bin_offset, buffer(20, 4)) - if msg_len == 0 then + header:add_le(xml_enc_offset, buffer(12, 1)) + :append_text(" (& 0xF == " .. bit32.band(header_data.enc_offset, 0xF) .. ")") + + header:add_le(channel_id, buffer(12, 1)) + header:add_le(stream_id, buffer(13, 1)) + :append_text(stream_text) + header:add_le(unknown, buffer(14, 1)) + header:add_le(msg_handle, buffer(15, 1)) + + header:add_le(message_class, buffer(18, 2)):append_text(" (" .. header_data.class .. ")") + + if header_data.header_len == 24 then + header:add_le(status_code, buffer(16, 2)) + header:add_le(f_bin_offset, buffer(20, 4)) + else + header:add_le(encrypt_xml, buffer(16, 1)) + end + return header_len +end + +function process_body(header, body_buffer, bc_subtree, pinfo) + if header.msg_len == 0 then return end - local body = bc_subtree:add(bc_protocol, buffer(header_len, nil), - "Baichuan Message Body, " .. class .. ", length: " .. msg_len .. ", encrypted: " .. tostring(encrypted)) + local body = bc_subtree:add(bc_protocol, body_buffer(0,header.msg_len), + "Baichuan Message Body, " .. header.class .. ", length: " .. header.msg_len .. ", type " .. header.msg_type) - if class == "legacy" then - if msg_type == 1 then - body:add_le(username, buffer(header_len, 32)) - body:add_le(password, buffer(header_len + 32, 32)) + if header.class == "legacy" then + if header.msg_type == 1 then + body:add_le(username, body_buffer(0, 32)) + body:add_le(password, body_buffer(0 + 32, 32)) end else - local body_tvb = buffer(header_len, bin_offset):tvb() - body:add(body_tvb(), "XML Payload") - - if encrypted then - local ba = buffer(header_len, bin_offset):bytes() - local decrypted = xml_encrypt(ba, enc_offset) - body_tvb = decrypted:tvb("Decrypted XML") - -- Create a tree item that, when clicked, automatically shows the tab we just created - body:add(body_tvb(), "Decrypted XML") + local xml_len = header.bin_offset + if xml_len == nil then + xml_len = header.msg_len + end + local xml_buffer = body_buffer(0, xml_len) + if xml_len > 0 then + local body_tvb = xml_buffer:tvb("Meta Payload") + body:add(body_tvb(), "Meta Payload") + if xml_len >= 4 then + if xml_encrypt(xml_buffer(0,5):bytes(), header.enc_offset):raw() == " 0 then + local binary_buffer = body_buffer(header.bin_offset, bin_len) -- Don't extend beyond msg size + body_tvb = binary_buffer:tvb("Main Payload"); + body:add(body_tvb(), "Main Payload") + if bin_len > 4 then + if xml_encrypt(binary_buffer(0,5):bytes(), header.enc_offset):raw() == "= 0 then + pinfo.cols['info'] = "Valid header" + -- Valid magic and header found + + -- Ensure min bytes for full header + if sub_buffer:len() < header_len then + pinfo.cols['info'] = "Need even more header" + -- Need more bytes + pinfo.desegment_len = header_len - sub_buffer:len() + pinfo.desegment_offset = 0 + return buffer:len() + end + + + local header = get_header(sub_buffer(0, nil)) + table.insert(table_msg_type_str, header.msg_type_str) + table.insert(table_msg_type, header.msg_type) + + -- Get full header and body + local full_body_len = header.msg_len + header.header_len + if sub_buffer:len() < full_body_len then + -- need more bytes, + -- from https://wiki.wireshark.org/Lua/Dissectors#TCP_reassembly + pinfo.cols['info'] = "Need more body: " .. sub_buffer:len() .. " expected " .. full_body_len + pinfo.desegment_len = full_body_len - sub_buffer:len() + pinfo.desegment_offset = 0 + return buffer:len() + end + + local remaining = sub_buffer:len() - header.header_len + + local bc_subtree = tree:add(bc_protocol, sub_buffer(0, header.msg_len), + "Baichuan IP Camera Protocol, " .. header.msg_type_str .. ":" .. header.msg_type .. " message") + process_header(sub_buffer, bc_subtree) + if header.header_len < sub_buffer:len() then + local body_buffer = sub_buffer(header.header_len,nil) + process_body(header, body_buffer, bc_subtree, pinfo) + + remaining = body_buffer:len() - header.msg_len + end + + -- Remaning bytes? + if remaining == 0 then + continue_loop = false + else + pinfo.cols['info'] = "Another packet" + sub_buffer = sub_buffer(full_body_len, nil) + end + else + pinfo.cols['info'] = "Invalid magic" + return -- Not a valid header + end + end + + local msg_type_strs = table.concat(table_msg_type_str, ",") + local msg_types = table.concat(table_msg_type, ",") + pinfo.cols['info'] = msg_type_strs .. ", type " .. msg_types +end + DissectorTable.get("tcp.port"):add(9000, bc_protocol) +DissectorTable.get("tcp.port"):add(52941, bc_protocol) -- change to your own custom port diff --git a/dissector/mediapacket.md b/dissector/mediapacket.md new file mode 100644 index 0000000..26cf0b5 --- /dev/null +++ b/dissector/mediapacket.md @@ -0,0 +1,111 @@ +# Media Packets + +This file attempts to document the encapsulated stream that makes up the binary +data of message ID 3. This stream represents the video and audio data. + +The are six types of media packets + +- Info V1 + +- Info V2 + +- I Frame + +- P Frame + +- AAC + +- ADPCM + +The I and P frames come in two formats H264 and H265. + +The ADPCM is DVI-4 encoded with an extra header that represents the block size. + +## Magic + +Each packet can be distinguished with the following magic bytes + +- Info V1: 0x31, 0x30, 0x30, 0x31 +- Info V2: 0x31, 0x30, 0x30, 0x32 +- I Frame: 0x30, 0x30, 0x64, 0x63 +- P Frame: 0x30, 0x31, 0x64, 0x63 +- AAC: 0x30, 0x35, 0x77, 0x62 +- ADPCM: 0x30, 0x31, 0x77, 0x62 + + +## Headers + +The full headers for each of these are as follows: + +Most of this information comes from the work of @twisteddx at his +[site](https://www.wasteofcash.com/BCConvert/BC_fileformat.txt) + +- Info V1/V2: + + - 4 bytes magic + - 4 bytes data size of header itself + - 4 bytes video width + - 4 bytes video height + - 1 byte unknown. Known values 00/01 + - 1 byte Frames per second (Reolink=FPS, Swann=Appears to be the index value of the FPS setting) + - 1 byte Start UTC year since 1900 + - 1 byte Start UTC month + - 1 byte Start UTC date + - 1 byte Start UTC hour + - 1 byte Start UTC minute + - 1 byte Start UTC seconds + - 1 byte End UTC year since 1900 + - 1 byte End UTC month + - 1 byte End UTC date + - 1 byte End UTC hour + - 1 byte End UTC minute + - 1 byte End UTC seconds + - 2 bytes reserved + +- I Frame + + - 4 bytes magic + - 4 bytes video type (ASCII text of either H264 or H265) + - 4 bytes data size of payload after header + - 4 bytes unknown. NVR channel count? Known values 1-00/08 2-00 3-00 4-00 + - 4 bytes Microseconds + - 4 bytes unknown. Known values 1-00/23/5A 2-00 3-00 4-00 + - 4 bytes POSIX time_t 32bit UTC time (seconds since 00:00:00 Jan 1 1970) + - 4 bytes unknown. Known values 1-00/06/29 2-00/01 3-00/C3 4-00 + +- P Frame + - 4 bytes magic + - 4 bytes video type (eg H264 or H265) + - 4 bytes data size of payload after header + - 4 bytes unknown. Known values 1-00 2-00 3-00 4-00 + - 4 bytes Microseconds + - 4 bytes unknown. Known values 1-00/5A 2-00 3-00 4-00 + +- AAC + + - 4 bytes magic + - 2 bytes data size of payload after header + - 2 bytes data size of payload after header (Same as previous) + +- ADPCM + + - 4 bytes magic + - 2 bytes data size of payload after header + - 2 bytes data size of payload after header + +### ADPCM + +After the ADPCM header is another header of the form + +- 2 bytes Magic either 0x00 0x01 or 0x00 0x7a +- 2 Bytes DVI-4 Block size + +After this header the adpcm DVI-4 data follows. + +## Processing + +The data in ID 3 messages represents an encapsulated stream. BC messages may +terminate mid media packet messages. Clients should create a buffer of the +BC ID 3 messages then read the media packet magic and expected length. Once +length is known they should wait for more BC message ID 3 packets until a +complete media packet is received before processing it. diff --git a/dissector/messages.md b/dissector/messages.md new file mode 100644 index 0000000..b1f851c --- /dev/null +++ b/dissector/messages.md @@ -0,0 +1,2083 @@ +# BC Messages +--- + +This is an attempt to document the BC messages. It is subject to change +and some aspects of it may not be correct. Please feel free to submit +a PR to improve it. + +- 1: Login Legacy + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------| + | f0 de bc 0a | 01 00 00 00 | 2c 07 00 00 | 00 00 00 01 | 01 | dc | 14 65 | + + - Body + + Body is hash of user 32 bytes and password 32 bytes and then a lot of zero pads + + ```hex + MD5USERNAME0MD5PASSWORD00000000000000000000000000000000000000000 + 0000000000000000000000000000000000000000000000000000000000000000 + ....... + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------| + | f0 de bc 0a | 01 00 00 00 | 91 00 00 00 | 00 00 00 01 | 01 | dd | 14 66 | + + - Body + + ```xml + + + + md5 + 13BCECE33DA453DB + + + ``` + + - **Notes:** Sends back a NONCE used for the modern login message. This is + effectively an upgrade request to use the modern xml style over legacy. + A legacy camera likely replies differently but I don't have one to test on. + +- 1: Login Modern + + - Client + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | f0 de bc 0a | 01 00 00 00 | 28 01 00 00 | 00 00 00 01 | 00 | 00 | 14 64 | 00 00 00 00 | + + - Binary + + ```xml + + + + ... + ... + 1 + + + LAN + 0 + + + ``` + + - Camera + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | f0 de bc 0a | 01 00 00 00 | 2e 06 00 00 | 00 00 00 01 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 00000000000000 + 0 + 0 + 0 + wifi_solo_ipc + 1 + 1 + 0 + 1 + + 2304*1296 + 2304 + 1296 + + English + 1 + pt + IPC + 33555019 + 0 + 0 + 0 + 0 + 0 + 0 + NTSC + DMY + 0 + 0 + 1 + + + + 1 + + mainStream + + 2304 + 1296 + + 15 + 2560 + 15,12,10,8,6,4,2 + 1024,1536,2048,2560,3072 + + + subStream + + 896 + 512 + + 15 + 512 + 15,12,10,8,6,4,2 + 128,256,384,512,768,1024 + + + + + ``` + +- 2: logout + + - Client + + - Binary + + ```xml + + + + PlainTextUsername + PlainTextPASSWORD + 1 + + + ``` + +- 3: Stream + + - Client + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | f0 de bc 0a | 03 00 00 00 | aa 00 00 00 | 00 00 00 09 | 00 | 00 | 14 64 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 0 + mainStream + + + ``` + + - **Notes:** This requests the camera to send this stream + + - Camera + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | f0 de bc 0a | 03 00 00 00 | 8a 00 00 00 | 00 00 00 09 | c8 | 00 | 00 00 | 6a 00 00 00 | + + - Body + + ```xml + + + 1 + + ``` + + - Binary + + ```hex + 31303032200000000009000010050000000F780A06122422780A061224220000 + ``` + + - **Notes:** Camera then send the stream as a binary payload in all + following messages of id 3 + + - Camera Stream Binary + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | f0 de bc 0a | 03 00 00 00 | e8 5e 00 00 | 00 00 00 09 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Body + + Body is binary. This binary represents an embedded stream which should + be detailed elsewhere. + +- 4: `` (stop) + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 04 | 00 00 00 86 | 2b 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 0 + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 04 | 00 00 00 00 | 2b 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +- 10: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 0a | 00 00 00 68 | 0b 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 0a | 00 00 01 f7 | 0b 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + + FDX + + + followVideoStream + + + + 0 + adpcm + 16000 + 16 + 1024 + mono + + + + + ``` + +- 18: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 12 | 00 00 00 a4 | 1e 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 32 + right + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 12 | 00 00 00 00 | 1e 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +- 25: `` (write) + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 19 | 00 00 05 c2 | 64 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Binary + + ```xml + + + + 0 + 128 + 128 + 128 + 128 + 166 + + + 0 + 1 + + 50hz + 0 + + + auto + + 1 + 100 + 1 + 62 + + + 0 + 125 + 0 + 125 + + 1/30 + 50 + + + auto + + 0 + 255 + 128 + + + 0 + 255 + 128 + + + + auto + ir + medium + + + 0 + backLight + + 0 + 255 + 128 + + + 0 + 255 + 128 + + + 0 + 0 + + 0 + success + 0 + + + high + 1 + + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 19 | 00 00 00 00 | 64 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + +- 26: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 1a | 00 00 00 68 | 2d 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 1a | 00 00 05 7c | 2d 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 128 + 128 + 128 + 128 + 128 + + + 0 + 1 + + 50hz + 0 + + + auto + + 1 + 100 + 1 + 62 + + + 0 + 125 + 0 + 125 + + 1/30 + 50 + + + auto + auto, manual + + 0 + 255 + 128 + + + 0 + 255 + 128 + + + + auto + ir + medium + + + 0 + backLight + + 0 + 255 + 128 + + + 0 + 255 + 128 + + + 0 + 0 + + 0 + success + 0 + + + high + 1 + + + + ``` + +- 31: Start Motion Alarm + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 1f | 00 00 00 00 | 05 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 1f | 00 00 00 00 | 05 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - **Notes:** The camera will not send message 33 to the client until + after this msg has been recieved + +- 33: `` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 21 | 00 00 00 f0 | 05 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + + 0 + MD + 0 + 0 + + + + ``` + +- 44: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 2c | 00 00 00 68 | 30 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 2c | 00 00 01 df | 30 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + Cammy02 + 1 + 65536 + 65536 + 0 + 0 + + + 0 + 1 + 65537 + 1 + 0 + 0 + Chinese + + + ``` + +- 45: `` (write) + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 2d | 00 00 02 23 | 32 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Binary + + ```xml + + + + 0 + Cammy02 + 0 + 65536 + 65536 + 0 + 0 + + + 0 + 1 + 65537 + 1 + Chinese + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 2d | 00 00 00 00 | 32 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +- 52: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 34 | 00 00 00 68 | 36 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 34 | 00 00 00 96 | 36 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 0 + + + + ``` + +- 53: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 35 | 00 00 01 d7 | 38 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Binary + + ```xml + + + + 0 + 1 + + + 0 + 0 + + + 1 + 0 + + + 2 + 0 + + + 3 + 0 + + + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 35 | 00 00 00 00 | 38 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +- 54: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 36 | 00 00 00 68 | 14 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 36 | 00 00 00 ed | 14 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 1 + 15 + 10 + 5 + + + ``` + +- 55: `` (write) + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 37 | 00 00 01 3b | 16 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Binary + + ```xml + + + + 0 + 15 + 1 + 5 + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 37 | 00 00 00 00 | 16 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +- 56: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 38 | 00 00 00 68 | 1d 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 38 | 00 00 03 61 | 1d 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 1 + + + 2304*1296 + 2304 + 1296 + cbr + 15 + 2560 + high + + + + 896*512 + 896 + 512 + cbr + 15 + 512 + high + + + + + 0 + 0 + vbr + 0 + 0 + default + + + + ``` + +- 57: `` (write) + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 39 | 00 00 02 bc | 1f 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Binary + + ```xml + + + + 0 + + + 2304*1296 + 2304 + 1296 + vbr + 15 + 2560 + high + + + + 896*512 + 896 + 512 + vbr + 15 + 512 + high + + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 39 | 00 00 00 00 | 1f 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +- 58: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 3a | 00 00 00 6b | 03 00 00 00 | 00 | 00 | 64 14 | 00 00 00 6b | + + - Body + + ```xml + + + PlainTextUsername + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 3a | 00 00 03 a4 | 03 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + + 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 + + + 1 + 1 + 1 + + + + 0 + PlainTextUsername + PlainTextPASSWORD + 1 + 0 + none + + + 0 + PlainTextUsername + PlainTextPASSWORD]VX + 0 + 0 + none + + + 0 + PlainTextUsername + PlainTextPASSWORD + 1 + 1 + none + + + + ``` + - **Notes:** The passwords are not sent in some models of cameras namely + RLC-410 4mp, RLC-410 5mp, RLC-520 (fw 200710) in these cases the passwords + are blank. In some older cameras that do not use encryption at all these + passwords are completely visible to any network sniffers. Even the "encrypted" + cameras only have weak encryption that is easily broken since the + decryption key is fixed and well-known. + +- 76: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 4c | 00 00 00 00 | 22 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 4c | 00 00 01 69 | 22 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 1 + + + 1 + + + 192.168.1.101 + 255.255.255.0 + 94:E0:D6:E9:89:86 + 192.168.1.1 + + + 1.1.1.1 + 8.8.8.8 + + + ``` + +- 77: `` (write) + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 4d | 00 00 01 5b | 25 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Binary + + ```xml + + + + 1 + + + 0 + + + 192.168.1.101 + 255.255.255.0 + 94:E0:D6:E9:89:86 + 192.168.1.1 + + + 1.1.1.1 + 8.8.8.8 + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 4d | 00 00 00 00 | 14 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +- 78: `` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 4e | 00 00 00 d3 | 1b 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 128 + 128 + 128 + 128 + + + ``` + +- 79: `` (ptz) + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 4f | 00 00 01 3b | 1b 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 9600 + CS8 + 1 + none + none + PELCO_D + 1 + + + ``` + +- 80: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 50 | 00 00 00 00 | 08 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 50 | 00 00 01 f0 | 08 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + Cammy02 + E1 + 00000000000000 + build 19110800 + IPC_517SD5 + v2.0.0.0 + v2.0.0.587_19110800 + IPC_51716M110000000100000 + IEClient + pak + blackPointsLevel=0 + + + ``` + +- 81: `` (schedule) + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 51 | 00 00 00 68 | 19 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 51 | 00 00 04 30 | 19 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 1 + + + MD + + + 1 + Sunday + 0 + 23 + + + 1 + Monday + 0 + 23 + + + 1 + Tuesday + 0 + 23 + + + 1 + Wednesday + 0 + 23 + + + 1 + Thursday + 0 + 23 + + + 1 + Friday + 0 + 23 + + + 1 + Saturday + 0 + 23 + + + + + + + ``` + +- 82: `` (write) + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 52 | 00 00 05 da | 1a 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Binary + + ```xml + + + + 0 + 1 + + + MD + + + 1 + Sunday + 0 + 23 + + + 1 + Monday + 0 + 23 + + + 1 + Tuesday + 0 + 12 + + + 1 + Tuesday + 14 + 23 + + + 1 + Wednesday + 0 + 23 + + + 1 + Thursday + 0 + 23 + + + 1 + Friday + 0 + 23 + + + 1 + Saturday + 0 + 23 + + + + + none + + + 1 + Tuesday + 13 + 13 + + + + + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 52 | 00 00 00 00 | 1a 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +- 93: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 5d | 00 00 00 00 | 17 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + +- 102: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 66 | 00 00 00 00 | 07 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 66 | 00 00 00 55 | 07 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + + ``` + +- 104: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 68 | 00 00 00 00 | 0a 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 68 | 00 00 01 a5 | 0a 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + -25200 + DMY + 2020 + 10 + 6 + 18 + 36 + 34 + 0 + 0 + English + Cammy02 + + + NTSC + + + ``` + +- 115: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 73 | 00 00 00 00 | 0c 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 73 | 00 00 00 75 | 0c 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + -40 + + + ``` + +- 132: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 84 | 00 00 00 68 | 65 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 84 | 00 00 05 7c | 65 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 128 + 128 + 128 + 128 + 128 + + + 0 + 1 + + 50hz + 0 + + + auto + + 1 + 100 + 1 + 62 + + + 0 + 125 + 0 + 125 + + 1/30 + 50 + + + auto + auto, manual + + 0 + 255 + 128 + + + 0 + 255 + 128 + + + + auto + ir + medium + + + 0 + backLight + + 0 + 255 + 128 + + + 0 + 255 + 128 + + + 0 + 0 + + 0 + success + 0 + + + high + 1 + + + + ``` + +- 133: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 85 | 00 00 00 00 | 06 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 85 | 00 00 00 7f | 06 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 1 + none + + + ``` + +- 146: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 92 | 00 00 00 00 | 04 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 92 | 00 00 02 fc | 04 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + + 1 + + mainStream + + 2304 + 1296 + + 15 + 2560 + 15,12,10,8,6,4,2 + 1024,1536,2048,2560,3072 + + + subStream + + 896 + 512 + + 15 + 512 + 15,12,10,8,6,4,2 + 128,256,384,512,768,1024 + + + + + ``` + +- 151: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 97 | 00 00 00 a7 | 02 00 00 00 | 00 | 00 | 64 14 | 00 00 00 a7 | + + - Body + + ```xml + + + PlainTextUsername + system, network, alarm, record, video, image + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 97 | 00 00 03 ac | 02 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + PlainTextUsername + + + general_rw, norm_rw, version_ro, uid_ro, autoReboot_rw, restore_rw, reboot_rw, shutdown_rw, dst_rw, log_ro, performance_ro, upgrade_rw, export_rw, import_rw, bootPwd_rw + + + + + port_rw, dns_rw, email_rw, ipFilter_rw, localLink_rw, pppoe_rw, upnp_rw, wifi_rw, ntp_rw, netStatus_rw, ptop_rw, autontp_rw + + + + + 0 + motion_rw + + + + + 0 + ispBasic_rw, ispAdvance_rw, ledState_rw + + + + + + ``` + +- 190: PTZ Preset + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 be | 00 00 00 68 | 0d 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 be | 00 00 00 86 | 0d 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + + + + ``` + +- 192: + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 c0 | 00 00 00 00 | 05 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 c0 | 00 00 00 00 | 05 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +- 199: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 c7 | 00 00 00 00 | 02 00 00 00 | 00 | 00 | 64 14 | 00 00 00 00 | + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 c7 | 00 00 05 f6 | 02 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 0 + 0 + 1 + 1 + pt + 0 + 0 + 0 + 1 + 0 + 1 + 1 + 5 + 0 + 1 + 0 + 0 + 1 + 0 + 0 + 1 + 1 + 1 + 3 + 1 + 1 + 1 + 0 + 30 + 1 + 0 + 1 + 1 + 0 + + 1 + + googleHome + 1 + + + amazonAlexa + 1 + + + + 0 + 3 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 15 + 1 + 1 + + + + ``` + +- 208: `` + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 d0 | 00 00 00 68 | 2e 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 d0 | 00 00 00 c2 | 2e 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | + + - Binary + + ```xml + + + + 0 + 2 + auto + open + + + ``` + +- 209: `` (write) + + - Client + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 d1 | 00 00 01 10 | 85 00 00 00 | 00 | 00 | 64 14 | 00 00 00 68 | + + - Body + + ```xml + + + 0 + + ``` + + - Binary + + ```xml + + + + 0 + close + open + + + ``` + + - Camera + + - Header + + | magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | Binary Offset | + |--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| + | 0a bc de f0 | 00 00 00 d1 | 00 00 00 00 | 85 00 00 00 | c8 | 00 | 00 00 | 00 00 00 00 | diff --git a/dissector/protocol.md b/dissector/protocol.md new file mode 100644 index 0000000..fd81dce --- /dev/null +++ b/dissector/protocol.md @@ -0,0 +1,235 @@ +# BC Protocol + +This is an attempt to document the BC protocol. It is far from complete +but should serve as a good basis for those wishing to develop apps for +BC cameras. + +## Messages + +Each message has the general format: + +- Header: 20-24 bytes + +- Message Body + +### Header + +The header has the format: + +| magic | message id | message length | encryption offset | Encryption flag | Unknown | message class | +|--------------|--------------|----------------|-------------------|-----------------|---------|---------------| +| f0 de bc 0a | 01 00 00 00 | 2c 07 00 00 | 00 00 00 01 | 01 | dc | 14 65 | + +Or + +| Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset | +|--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| +| f0 de bc 0a | 01 00 00 00 | 28 01 00 00 | 00 00 00 01 | 00 | 00 | 14 64 | 00 00 00 00 | + + +- Magic 4 bytes +- ID 4 bytes +- Message length 4 bytes +- Encryption offset 4 bytes +- Encryption flag (not always honoured by clients) 1 byte +- Unknown 1 byte (always zero) +- Binary offset 4 bytes (Presence depend on message class) + +#### Magic + +The magic bytes for BC messages is always `f0 de bc 0a`. When receiving packet +this should be used to quickly discard invalid packets. + +#### Message ID + +Each function in BC has its own message ID. For example login is 1, video data +is 3, motion detection is 33. + +For a more complete list please see the [messages doc](dissector/messages.md) + +#### Message length + +The message length contains the full length of the data to follow the header +this includes both the XML and binary parts. + +#### Encryption offset + +The encryption offset is used as part of the decoding process. It is combined +with the key to decrypt the data. + +Here is an example decrypter in rust + +```rust +const XML_KEY: [u8; 8] = [0x1F, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0xFF]; + +pub fn crypt(offset: u32, buf: &[u8]) -> Vec { + let key_iter = XML_KEY.iter().cycle().skip(offset as usize % 8); + key_iter + .zip(buf) + .map(|(key, i)| *i ^ key ^ (offset as u8)) + .collect() +} +``` + +In short the key is offset by the encryption offset in the header. Then each +encrypted byte is paired with the offseted keys bytes (looping the offseted +key as necessary). Then each byte is XORed with the paired key byte and the +offset. + +The key is the same for all cameras. + +Older cameras do not use encryption and all messages are sent as plain text. + +#### Encryption Flag + +This flag is 0 for unencrypted or non zero for encrypted. This flag seems +only to be valid during the login step, and seems to be arbitrary otherwise. + +Clients should decide to use encryption or not based on this byte that the +camera send during login. Then should use that same setting for all further +messages with the camera. + +#### Unknown + +The purpose of this byte is unknown but is always zero. If you figure out +what it does please submit a PR. + +#### Message class + +The message class determines the length of the header. The following classes +and header lengths are known. + +- 0x6514: "legacy" 20 bytes + +- 0x6614: "modern" 20 bytes + +- 0x6414: "modern" 24 bytes + +- 0x0000: "modern" 24 bytes + +#### Binary offset + +For messages that contain the binary offset field this represents where to start +the binary part of the message. The total length of XML + binary is equal +to the message length in the header so Binary Offset is +total_length - this_offset. Where as this field also represents the end of the +XML part of the message. + +Some messages seems to not set this field correctly. For example message ID 1 +sets this to zero meaning that all data in the message is binary, however +it contains XML. This best way to tell is this is binary or XML is to check the +message ID and determine from there. Alternatively you could look for ` + + +0 +0 +mainStream + + +``` + +streamType can be either + +- `mainStream` in which case it will be HD +- `subStream` in which case it will be SD + + +channelId is part of the NVR when multiple cameras use the same IP. In this +case each camera has its own channelId. + +Not sure what handle does. + + +The reply is first a message with the following xml + +```xml + + +1 + +``` + +After which all message bodies of type id 3 are binary. + +The binary represents a stream of data. It can be interrupted by packet +boundaries. Clients should create a buffer and pop bytes for processing when +complete media packets are received. Media packets descriptions can be found in +the [docs](dissector/mediapacket.md) + +# Other Function + +Other data can be received from the camera by sending the appropriate header to +the camera. For example sending the header for ID 78 + +| Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset | +|--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| +| f0 de bc 0a | 4e 00 00 00 | d3 00 00 00 | 08 db 9c 00 | c8 | 00 | 00 00 | 00 00 00 00 | + +The camera will reply with an xml with brightness and contrast + +```xml + + + +0 +128 +128 +128 +128 + + +``` + +Some message IDs also require input along with the request header. For example + +ID 151 which is the users ability info requires the header + +| Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset | +|--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------| +| f0 de bc 0a | 97 00 00 00 | a7 00 00 00 | 00 00 00 02 | 00 | 00 | 14 64 | a7 00 00 00 | + +and the body of + +```xml + + +... +system, network, alarm, record, video, image + +``` + +Which contains the plain text of the username of interest and the tokens for +abilities you want to know about. + +Details of expected formats should be found from the +[docs](dissector/messages.md) From f7f12c76a1ac59ed7b9919769f6333917aee94aa Mon Sep 17 00:00:00 2001 From: Andrew King Date: Fri, 13 Nov 2020 21:45:40 +0700 Subject: [PATCH 2/3] Audio and Format auto detect (#68) * Add queues to the gstreamer launch * Add audio to the stream * Removing format option in prep for auto detect * Adding format detect from headers * Set format in gstreamer launch depending on video magic bytes * Rename MaybeAppSrcs to reflect new role of managing gst outputs * Auto detect format * Remove all setting of stream format * Re-add missing media_format caused by rebase * Add audio format auto detect too * Use more appropiate adpcm in gst * Combined gst and media_packet format enums into a single enum StreamFormat * Reduce adpcm header size * Disable adpcm output * Add oki adpcm native rust support * Maintain state between packets for adpcm * Use bytes in adpcm stream to determine adpcm decoding state * Clean up adpcm code * Use raw rtp payload for the adpcm stream * Bug fix to treat adpcm and aac seperatly in the match * Remove redundent second clamp of the step index in adpcm. * ADPCM format changes. This is a squashed commit of changes made during live testing of ADPCM format. Summary: Using DVI4 for the ADPCM, with our own decoder written in rust. * Changes to comments and IMA max range in the adpcm decoder * ADPCM magic contains block size use it for chunking the adpcm data * Use error logs rather than asserts in adpcm decode * Rustfmt and clippy styling * Use static slice for adpcm step and changes arrays * Bounds and error checks in ADPCM * Use i/u32 in adpcm * Rust fmted --- src/bc_protocol.rs | 29 +++- src/bc_protocol/adpcm.rs | 244 ++++++++++++++++++++++++++++++++ src/bc_protocol/media_packet.rs | 103 ++++++++++---- src/config.rs | 14 +- src/gst.rs | 125 ++++++++++++---- src/main.rs | 42 +++--- 6 files changed, 462 insertions(+), 95 deletions(-) create mode 100644 src/bc_protocol/adpcm.rs diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 640f24b..9e0bad2 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -2,6 +2,8 @@ use self::connection::BcConnection; use self::media_packet::{MediaDataKind, MediaDataSubscriber}; use crate::bc; use crate::bc::{model::*, xml::*}; +use crate::gst::GstOutputs; +use adpcm::adpcm_to_pcm; use err_derive::Error; use log::*; use std::io::Write; @@ -10,6 +12,7 @@ use std::time::Duration; use Md5Trunc::*; +mod adpcm; mod connection; mod media_packet; mod time; @@ -49,12 +52,12 @@ pub enum Error { #[error(display = "Timeout")] Timeout(#[error(source)] std::sync::mpsc::RecvTimeoutError), - #[error(display = "Media")] - MediaPacket(#[error(source)] self::media_packet::Error), - #[error(display = "Credential error")] AuthFailed, + #[error(display = "ADPCM Decoding Error")] + AdpcmDecodingError(&'static str), + #[error(display = "Other error")] Other(&'static str), } @@ -245,7 +248,7 @@ impl BcCamera { pub fn start_video( &self, - data_out: &mut dyn Write, + data_outs: &mut GstOutputs, stream_name: &str, channel_id: u32, ) -> Result { @@ -278,12 +281,26 @@ impl BcCamera { let mut media_sub = MediaDataSubscriber::from_bc_sub(&sub_video); loop { - let binary_data = media_sub.next_media_packet(RX_TIMEOUT)?; + let binary_data = media_sub.next_media_packet()?; // We now have a complete interesting packet. Send it to gst. // Process the packet match binary_data.kind() { MediaDataKind::VideoDataIframe | MediaDataKind::VideoDataPframe => { - data_out.write_all(binary_data.body())?; + let media_format = binary_data.media_format(); + data_outs.set_format(media_format); + data_outs.vidsrc.write_all(binary_data.body())?; + } + MediaDataKind::AudioDataAac => { + let media_format = binary_data.media_format(); + data_outs.set_format(media_format); + data_outs.audsrc.write_all(binary_data.body())?; + } + MediaDataKind::AudioDataAdpcm => { + let media_format = binary_data.media_format(); + data_outs.set_format(media_format); + let adpcm = binary_data.body(); + let pcm = adpcm_to_pcm(adpcm)?; + data_outs.audsrc.write_all(&pcm)?; } _ => {} }; diff --git a/src/bc_protocol/adpcm.rs b/src/bc_protocol/adpcm.rs new file mode 100644 index 0000000..e8b8793 --- /dev/null +++ b/src/bc_protocol/adpcm.rs @@ -0,0 +1,244 @@ +/* + This is a rust implementation of OKI and DVI/IMA ADPCM. +*/ +use crate::Error; +use log::error; +use std::convert::TryInto; + +struct AdpcmSetup { + max_step_index: u32, + steps: &'static [u32], + max_sample_size: i32, + changes: &'static [i32], +} + +impl AdpcmSetup { + // Unused, originally we thought BC might be using OKI but it is actually DVI4 + fn new_oki() -> Self { + Self { + max_step_index: 48, + steps: &[ + 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, + 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, + 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, + ], + changes: &[-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8], + max_sample_size: 2048, + } + } + + // This is IMA format, but it is the same as DVI4 format except in the block header + fn new_ima() -> Self { + Self { + max_step_index: 88, + steps: &[ + 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, + 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, + 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, + 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, + 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, + 18500, 20350, 22385, 24623, 27086, 29794, 32767, + ], + changes: &[-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8], + max_sample_size: 32768, + } + } +} + +struct Nibble { + // A nibble is a 4bit int + data: u8, // This is the raw data for the nibble +} + +impl Nibble { + // Use u/i32 throughout to ensure that we always have enough + // Headroom to do the math without needing `as` casting everywhere + fn unsigned(&self) -> u32 { + (self.data & 0b00001111) as u32 // Mask first 4 bits it just to be sure its in nibble range + } + + fn signed_magnitude(&self) -> u32 { + (self.data & 0b00000111) as u32 // Mask of first 3 bits which are the magnitiude bits in signed int + } + + fn signed(&self) -> i32 { + match self.data & 0b00001000 { + // Sign bit is at the 4th bit + 0b00001000 => -(self.signed_magnitude() as i32), + _ => self.signed_magnitude() as i32, + } + } + + fn from_byte(byte: &u8) -> [Self; 2] { + // Two nibbles per byte + [ + Self { + data: (byte & 0b11110000) >> 4, + }, + Self { + data: byte & 0b00001111, + }, + ] + } +} + +pub fn adpcm_to_pcm(bytes: &[u8]) -> Result, Error> { + let context = AdpcmSetup::new_ima(); + + let mut result: Vec = vec![]; // Stores the PCM byte array + + // ADPCM is not really a streamable format + // Each audio sample requires information on the previous sample + // To solve this reolink caches the intermediate variables (step_index and last_output) + // Into the block header in the stream. When ever an adpcm packet arrives it starts with + // 0x0001 which is the frame time from HISilicon documentation (I think) following by + // 0xWW which is half the block size + // 0xYY which is the last output + // 0xZZ which is the step index of the last output + // We must initialise our decoder with this data + + if bytes.len() < 4 { + error!("ADPCM data is too short for even the magic."); + return Err(Error::AdpcmDecodingError( + "ADPCM data is too short for even the magic.", + )); + } + + // Check for valid number of frame type + let frame_type_bytes = &bytes[0..2]; + const FRAME_TYPE_HISILICON: &[u8] = &[0x00, 0x01]; + if frame_type_bytes != FRAME_TYPE_HISILICON { + error!("Unexpected ADPCM frame type: {:x?}", frame_type_bytes); + return Err(Error::AdpcmDecodingError("Unexpected ADPCM frame type")); + } + + // Check for valid block size + let block_size_bytes = &bytes[2..4]; + let block_size = (u16::from_le_bytes( + block_size_bytes + .try_into() + .expect("slice with incorrect length"), + ) as u32) + * 2; // Block size is stored as 1/2 (don't know why) + let full_block_size = block_size + 4; // block_size + magic (2 bytes) + size (2 bytes) + if !bytes.len() % full_block_size as usize == 0 { + error!("ADPCM Data is not a multiple of the block size"); + return Err(Error::AdpcmDecodingError( + "ADPCM block size does not match data length.", + )); + } + + // Chunk on block size + for bytes in bytes.chunks(full_block_size as usize) { + // Get predictor state from block header using DVI 4 format. + if bytes.len() < 8 { + error!("ADPCM Block size is not long enough for header"); + return Err(Error::AdpcmDecodingError( + "ADPCM has insufficent block size", + )); + } + let step_output_bytes = &bytes[4..6]; + let mut last_output = i16::from_le_bytes( + step_output_bytes + .try_into() + .expect("slice with incorrect length"), + ) as i32; + let step_index_bytes = &bytes[6..8]; + let mut step_index = u16::from_le_bytes( + step_index_bytes + .try_into() + .expect("slice with incorrect length"), + ) as i32; + + // To avoid casting to u8 <-> u16 <-> u32 and back all the time I just do all maths in u/i32 + // This gives enough headroom to do all calculations without overflow because adpcm puts artifical + // limits on the sample sizes + let mut step: u32; + + // The rest is all data to be decoded + let data = &bytes[8..]; + + for byte in data { + let nibbles: [Nibble; 2] = Nibble::from_byte(byte); + for nibble in &nibbles { + let unibble = nibble.unsigned(); + + // Specifications say: Clamp it in max index range 0..context.max_step_index + step_index = match step_index { + n if n < 0 => 0, + n if n > context.max_step_index as i32 => context.max_step_index as i32, + n => n, + }; + + // This is just Eulers approximation with a variable step size + // **Adaptive** Differential PCM + // Adaptive: because the step size is variable + step = context.steps[step_index as usize]; + + let raw_sample; + /* == Non approxiate version === + // This is the full maths version + // We don't use this one as we need to match the way the encoder + // works if we want to use the state stored in the header. + // I have Left it here as it is easier to understand then the bit shift version below + let inibble = nibble.signed(); + + // Calculate the delta (which is really what adpcm is all about) + // Adaptive **Differential** PCM + // Differential: Becuase its all about the difference (gradient) + let diff = (step as i32) * (inibble) / 2 + (step as i32) / 8; + + // Eulers approxiation + // Sample = Previous_Sample + difference*step_size + raw_sample = last_output + diff; + */ + + // === Approximate version == + // Approximate form uses bit shift operators. + // This is a legacy of the days when mult/divides were expensive + // It is also the format used on low end CPUs like cameras + let mut diff = step >> 3; + if (unibble & 0b0100) == 0b0100 { + diff += step; + } + if (unibble & 0b0010) == 0b0010 { + diff += step >> 1; + } + if (unibble & 0b0001) == 0b0001 { + diff += step >> 2; + } + // Sign test + if (unibble & 0b1000) == 0b1000 { + raw_sample = last_output - (diff as i32); + } else { + raw_sample = last_output + (diff as i32); + } + + // Specifications say: Clamp it in max sample range -context.max_sample_size..context.max_sample_size + let sample = match raw_sample { + value if value > context.max_sample_size - 1 => context.max_sample_size - 1, + value if value < -context.max_sample_size => -context.max_sample_size, + value => value, + }; + + // PCM is really in i16 range + // Some formats e.g. OKI are not in the full PCM range of values + // To convert we must scale it to the i16 range + // We also cast to i16 at this point ready for the conversion to u8 bytes of the output + let scaled_sample = (sample as i32 * (i16::MAX as i32) + / (context.max_sample_size - 1) as i32) + as i16; + + // Get the results in bytes + result.extend(scaled_sample.to_le_bytes().iter()); + + // Increment the step index + step_index = step_index as i32 + context.changes[unibble as usize]; + + // cache the last_output ready for next run + last_output = sample; + } + } + } + Ok(result) +} diff --git a/src/bc_protocol/media_packet.rs b/src/bc_protocol/media_packet.rs index 3d4689b..b69a1d8 100644 --- a/src/bc_protocol/media_packet.rs +++ b/src/bc_protocol/media_packet.rs @@ -1,11 +1,11 @@ +use super::{Error, Result, RX_TIMEOUT}; use crate::bc::model::*; use crate::bc_protocol::connection::BcSubscription; -use err_derive::Error; +use crate::gst::StreamFormat; use log::trace; use log::*; use std::collections::VecDeque; use std::convert::TryInto; -use std::time::Duration; const INVALID_MEDIA_PACKETS: &[MediaDataKind] = &[MediaDataKind::Unknown]; @@ -15,14 +15,6 @@ const MAGIC_SIZE: usize = 4; // PAD_SIZE: Media packets use 8 byte padding const PAD_SIZE: usize = 8; -type Result = std::result::Result; - -#[derive(Debug, Error)] -pub enum Error { - #[error(display = "Timeout")] - Timeout(#[error(source)] std::sync::mpsc::RecvTimeoutError), -} - #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] pub enum MediaDataKind { VideoDataIframe, @@ -50,7 +42,7 @@ impl MediaData { MediaDataKind::VideoDataIframe => 32, MediaDataKind::VideoDataPframe => 24, MediaDataKind::AudioDataAac => 8, - MediaDataKind::AudioDataAdpcm => 16, + MediaDataKind::AudioDataAdpcm => 8, MediaDataKind::InfoData => 32, MediaDataKind::Unknown => 0, } @@ -61,6 +53,39 @@ impl MediaData { MediaData::header_size_from_kind(kind) } + pub fn header(&self) -> &[u8] { + &self.data[0..self.header_size()] + } + + pub fn header_dump(&self) { + info!("{:?}-hex: {:02?}", self.kind(), self.header()); + let mut result = vec![]; + for four in self.header().chunks(4) { + result.push(u32::from_le_bytes(four.try_into().unwrap())); + } + info!("{:?}-32: {:?}", self.kind(), result); + let mut result = vec![]; + for two in self.header().chunks(2) { + result.push(u16::from_le_bytes(two.try_into().unwrap())); + } + info!("{:?}-16: {:?}", self.kind(), result); + let mut result = vec![]; + for one in self.header().chunks(1) { + result.push(u8::from_le_bytes(one.try_into().unwrap())); + } + info!("{:?}-8: {:?}", self.kind(), result); + let mut result = vec![]; + for four in self.header().chunks(4) { + result.push(f32::from_le_bytes(four.try_into().unwrap())); + } + info!("{:?}-f32: {:?}", self.kind(), result); + let mut result = vec![]; + for four in self.header().chunks(4) { + result.push(String::from_utf8_lossy(four)); + } + info!("{:?}-utf8: {:?}", self.kind(), result); + } + fn header_size(&self) -> usize { MediaData::header_size_from_raw(&self.data) } @@ -129,7 +154,7 @@ impl MediaData { MAGIC_IFRAME => MediaDataKind::VideoDataIframe, MAGIC_PFRAME => MediaDataKind::VideoDataPframe, _ => { - trace!("Unknown magic kind: {:x?}", &magic); + //trace!("Unknown magic kind: {:x?}", &magic); MediaDataKind::Unknown } } @@ -138,6 +163,39 @@ impl MediaData { pub fn kind(&self) -> MediaDataKind { MediaData::kind_from_raw(&self.data) } + + pub fn media_format(&self) -> Option { + let kind = self.kind(); + match kind { + MediaDataKind::VideoDataIframe | MediaDataKind::VideoDataPframe => { + let stream_type = &self.data[4..8]; + const H264_STR_UPPER: &[u8] = &[0x48, 0x32, 0x36, 0x34]; + const H264_STR_LOWER: &[u8] = &[0x68, 0x32, 0x36, 0x34]; + const H265_STR_UPPER: &[u8] = &[0x48, 0x32, 0x36, 0x35]; + const H265_STR_LOWER: &[u8] = &[0x68, 0x32, 0x36, 0x35]; + match stream_type { + H264_STR_UPPER | H264_STR_LOWER => Some(StreamFormat::H264), // Offically it should be "H264" not "h264" but covering all cases + H265_STR_UPPER | H265_STR_LOWER => Some(StreamFormat::H265), + _ => None, + } + } + MediaDataKind::AudioDataAac => Some(StreamFormat::AAC), + MediaDataKind::AudioDataAdpcm => Some(StreamFormat::ADPCM), + _ => None, + } + } + + pub fn timestamp(&self) -> Option { + let kind = self.kind(); + match kind { + MediaDataKind::VideoDataIframe | MediaDataKind::VideoDataPframe => Some( + Self::bytes_to_size(&self.data[16..20]) + .try_into() + .expect("usize wont fit into u64"), + ), + _ => None, + } + } } pub struct MediaDataSubscriber<'a> { @@ -153,10 +211,10 @@ impl<'a> MediaDataSubscriber<'a> { } } - fn fill_binary_buffer(&mut self, rx_timeout: Duration) -> Result<()> { + fn fill_binary_buffer(&mut self) -> Result<()> { // Loop messages until we get binary add that data and return loop { - let msg = self.bc_sub.rx.recv_timeout(rx_timeout)?; + let msg = self.bc_sub.rx.recv_timeout(RX_TIMEOUT)?; if let BcBody::ModernMsg(ModernMsg { binary: Some(binary), .. @@ -170,11 +228,11 @@ impl<'a> MediaDataSubscriber<'a> { Ok(()) } - fn advance_to_media_packet(&mut self, rx_timeout: Duration) -> Result<()> { + fn advance_to_media_packet(&mut self) -> Result<()> { // In the event we get an unknown packet we advance by brute force // reading of bytes to the next valid magic while self.binary_buffer.len() < MAGIC_SIZE { - self.fill_binary_buffer(rx_timeout)?; + self.fill_binary_buffer()?; } // Check the kind, if its invalid use pop a byte and try again @@ -186,7 +244,7 @@ impl<'a> MediaDataSubscriber<'a> { while INVALID_MEDIA_PACKETS.contains(&MediaData::kind_from_raw(&magic)) { self.binary_buffer.pop_front(); while self.binary_buffer.len() < MAGIC_SIZE { - self.fill_binary_buffer(rx_timeout)?; + self.fill_binary_buffer()?; } magic = MediaDataSubscriber::get_first_n_deque(&self.binary_buffer, MAGIC_SIZE); } @@ -213,12 +271,9 @@ impl<'a> MediaDataSubscriber<'a> { } } - pub fn next_media_packet( - &mut self, - rx_timeout: Duration, - ) -> std::result::Result { + pub fn next_media_packet(&mut self) -> std::result::Result { // Find the first packet (does nothing if already at one) - self.advance_to_media_packet(rx_timeout)?; + self.advance_to_media_packet()?; // Get the magic bytes (guaranteed by advance_to_media_packet) let magic = MediaDataSubscriber::get_first_n_deque(&self.binary_buffer, MAGIC_SIZE); @@ -226,7 +281,7 @@ impl<'a> MediaDataSubscriber<'a> { // Get enough for the full header let header_size = MediaData::header_size_from_raw(&magic); while self.binary_buffer.len() < header_size { - self.fill_binary_buffer(rx_timeout)?; + self.fill_binary_buffer()?; } // Get enough for the full data + 8 byte buffer @@ -235,7 +290,7 @@ impl<'a> MediaDataSubscriber<'a> { let pad_size = MediaData::pad_size_from_raw(&header); let full_size = header_size + data_size + pad_size; while self.binary_buffer.len() < full_size { - self.fill_binary_buffer(rx_timeout)?; + self.fill_binary_buffer()?; } // Pop the full binary buffer diff --git a/src/config.rs b/src/config.rs index 765649a..79b4af5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,6 @@ use validator::{Validate, ValidationError}; use validator_derive::Validate; lazy_static! { - static ref RE_STREAM_FORM: Regex = Regex::new(r"^([hH]26[45]|[ \t]*[!].*)$").unwrap(); static ref RE_STREAM_SRC: Regex = Regex::new(r"^(mainStream|subStream|both)$").unwrap(); static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap(); } @@ -54,13 +53,8 @@ pub struct CameraConfig { // no longer used, but still here so we can warn users: pub timeout: Option, - #[validate(regex( - path = "RE_STREAM_FORM", - message = "Incorrect stream format", - code = "format" - ))] - #[serde(default = "default_format")] - pub format: String, + // no longer used, but still here so we can warn users: + pub format: Option, #[validate(regex( path = "RE_STREAM_SRC", @@ -95,10 +89,6 @@ fn default_bind_port() -> u16 { 8554 } -fn default_format() -> String { - "h265".to_string() -} - fn default_stream() -> String { "both".to_string() } diff --git a/src/gst.rs b/src/gst.rs index 6cec61e..d826c8c 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -1,7 +1,6 @@ //! This module provides an "RtspServer" abstraction that allows consumers of its API to feed it //! data using an ordinary std::io::Write interface. pub use self::maybe_app_src::MaybeAppSrc; - use gstreamer::prelude::Cast; use gstreamer::{Bin, Structure}; use gstreamer_app::AppSrc; @@ -27,10 +26,82 @@ pub struct RtspServer { server: GstRTSPServer, } +#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] pub enum StreamFormat { H264, H265, - Custom(String), + AAC, + ADPCM, +} + +pub struct GstOutputs { + pub audsrc: MaybeAppSrc, + pub vidsrc: MaybeAppSrc, + video_format: Option, + audio_format: Option, + factory: RTSPMediaFactory, +} + +impl GstOutputs { + pub fn from_appsrcs(vidsrc: MaybeAppSrc, audsrc: MaybeAppSrc) -> GstOutputs { + let result = GstOutputs { + vidsrc, + audsrc, + video_format: None, + audio_format: None, + factory: RTSPMediaFactory::new(), + }; + result.apply_format(); + result + } + + pub fn set_format(&mut self, format: Option) { + match format { + Some(StreamFormat::H264) | Some(StreamFormat::H265) => { + if format != self.video_format { + self.video_format = format; + self.apply_format(); + } + } + Some(StreamFormat::AAC) | Some(StreamFormat::ADPCM) => { + if format != self.audio_format { + self.audio_format = format; + self.apply_format(); + } + } + _ => {} + } + } + + fn apply_format(&self) { + let launch_vid = match self.video_format { + Some(StreamFormat::H264) => { + "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h264parse ! rtph264pay name=pay0" + } + Some(StreamFormat::H265) => { + "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h265parse ! rtph265pay name=pay0" + } + _ => "! fakesink", + }; + + let launch_aud = match self.audio_format { + Some(StreamFormat::ADPCM) => "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! rawaudioparse format=pcm pcm-format=s16le sample-rate=8000 num-channels=1 interleaved=true ! audioconvert ! rtpL16pay name=pay1", // DVI4 is converted to pcm in the appsrc + Some(StreamFormat::AAC) => "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aacparse ! rtpmp4apay name=pay1", + _ => "! fakesink", + }; + + self.factory.set_launch( + &vec![ + "( ", + "appsrc name=vidsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME", // 50MB max size so that it won't grow to infinite if the queue blocks + launch_vid, + "appsrc name=audsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME", // 50MB max size so that it won't grow to infinite if the queue blocks + launch_aud, + ")" + ] + .join(" "), + ); + } } impl Default for RtspServer { @@ -50,21 +121,23 @@ impl RtspServer { pub fn add_stream( &self, paths: &[&str], - stream_format: StreamFormat, permitted_users: &HashSet<&str>, - ) -> Result { + ) -> Result { let mounts = self .server .get_mount_points() .expect("The server should have mountpoints"); - let launch_str = match stream_format { - StreamFormat::H264 => "! h264parse ! rtph264pay name=pay0", - StreamFormat::H265 => "! h265parse ! rtph265pay name=pay0", - StreamFormat::Custom(ref custom_format) => custom_format, - }; + // Create a MaybeAppSrc: Write which we will give the caller. When the backing AppSrc is + // created by the factory, fish it out and give it to the waiting MaybeAppSrc via the + // channel it provided. This callback may be called more than once by Gstreamer if it is + // unhappy with the pipeline, so keep updating the MaybeAppSrc. + let (maybe_app_src, tx) = MaybeAppSrc::new_with_tx(); + let (maybe_app_src_aud, tx_aud) = MaybeAppSrc::new_with_tx(); - let factory = RTSPMediaFactory::new(); + let outputs = GstOutputs::from_appsrcs(maybe_app_src, maybe_app_src_aud); + + let factory = &outputs.factory; debug!( "Permitting {} to access {}", @@ -76,25 +149,10 @@ impl RtspServer { .collect::(), paths.join(", ") ); - self.add_permitted_roles(&factory, permitted_users); + self.add_permitted_roles(factory, permitted_users); - //factory.set_protocols(RTSPLowerTrans::TCP); - factory.set_launch(&format!("{}{}{}{}", - "( ", - "appsrc name=writesrc is-live=true block=true emit-signals=false max-bytes=0 do-timestamp=true ", - launch_str, - " )" - )); factory.set_shared(true); - // TODO maybe set video format via - // https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/blob/master/examples/src/bin/appsrc.rs#L66 - - // Create a MaybeAppSrc: Write which we will give the caller. When the backing AppSrc is - // created by the factory, fish it out and give it to the waiting MaybeAppSrc via the - // channel it provided. This callback may be called more than once by Gstreamer if it is - // unhappy with the pipeline, so keep updating the MaybeAppSrc. - let (maybe_app_src, tx) = MaybeAppSrc::new_with_tx(); factory.connect_media_configure(move |_factory, media| { debug!("RTSP: media was configured"); let bin = media @@ -103,18 +161,25 @@ impl RtspServer { .dynamic_cast::() .expect("Media source's element should be a bin"); let app_src = bin - .get_by_name_recurse_up("writesrc") + .get_by_name_recurse_up("vidsrc") .expect("write_src must be present in created bin") .dynamic_cast::() .expect("Source element is expected to be an appsrc!"); let _ = tx.send(app_src); // Receiver may be dropped, don't panic if so + + let app_src_aud = bin + .get_by_name_recurse_up("audsrc") + .expect("write_src must be present in created bin") + .dynamic_cast::() + .expect("Source element is expected to be an appsrc!"); + let _ = tx_aud.send(app_src_aud); // Receiver may be dropped, don't panic if so }); for path in paths { - mounts.add_factory(path, &factory); + mounts.add_factory(path, factory); } - Ok(maybe_app_src) + Ok(outputs) } pub fn add_permitted_roles(&self, factory: &RTSPMediaFactory, permitted_roles: &HashSet<&str>) { @@ -241,12 +306,14 @@ mod maybe_app_src { Some(src) => src, None => return Ok(buf.len()), }; + let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap(); { let gst_buf_mut = gst_buf.get_mut().unwrap(); let mut gst_buf_data = gst_buf_mut.map_writable().unwrap(); gst_buf_data.copy_from_slice(buf); } + let res = app_src.push_buffer(gst_buf); //.map_err(|e| io::Error::new(io::ErrorKind::Other, Box::new(e)))?; if res.is_err() { self.app_src = None; diff --git a/src/main.rs b/src/main.rs index 0660aa3..ffba065 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,10 @@ use err_derive::Error; use gio::TlsAuthenticationMode; use log::*; use neolink::bc_protocol::BcCamera; -use neolink::gst::{MaybeAppSrc, RtspServer, StreamFormat}; +use neolink::gst::{GstOutputs, RtspServer}; use neolink::Never; use std::collections::HashSet; use std::fs; -use std::io::Write; use std::sync::Arc; use std::time::Duration; use structopt::StructOpt; @@ -29,6 +28,8 @@ pub enum Error { IoError(#[error(source)] std::io::Error), #[error(display = "Validation error")] ValidationError(#[error(source)] validator::ValidationErrors), + #[error(display = "ADPCM Decoding Error")] + AdpcmDecodingError(&'static str), } fn main() -> Result<(), Error> { @@ -59,22 +60,14 @@ fn main() -> Result<(), Error> { crossbeam::scope(|s| { for camera in config.cameras { - let stream_format = match &*camera.format { - "h264" | "H264" => StreamFormat::H264, - "h265" | "H265" => StreamFormat::H265, - custom_format => StreamFormat::Custom(custom_format.to_string()), - }; - + if camera.format.is_some() { + warn!("The format config option of the camera has been removed in favour of auto detection.") + } // Let subthreads share the camera object; in principle I think they could share // the object as it sits in the config.cameras block, but I have not figured out the // syntax for that. let arc_cam = Arc::new(camera); - // The substream always seems to be H264, even on B800 cameras - let substream_format = match &*arc_cam.format { - "h264" | "H264" | "h265" | "H265" => StreamFormat::H264, - custom_format => StreamFormat::Custom(custom_format.to_string()), - }; let permitted_users = get_permitted_users(config.users.as_slice(), &arc_cam.permitted_users); @@ -84,20 +77,20 @@ fn main() -> Result<(), Error> { &*format!("/{}", arc_cam.name), &*format!("/{}/mainStream", arc_cam.name), ]; - let mut output = rtsp - .add_stream(paths, stream_format, &permitted_users) + let mut outputs = rtsp + .add_stream(paths, &permitted_users) .unwrap(); let main_camera = arc_cam.clone(); - s.spawn(move |_| camera_loop(&*main_camera, "mainStream", &mut output, true)); + s.spawn(move |_| camera_loop(&*main_camera, "mainStream", &mut outputs, true)); } if ["both", "subStream"].iter().any(|&e| e == arc_cam.stream) { let paths = &[&*format!("/{}/subStream", arc_cam.name)]; - let mut output = rtsp - .add_stream(paths, substream_format, &permitted_users) + let mut outputs = rtsp + .add_stream(paths, &permitted_users) .unwrap(); let sub_camera = arc_cam.clone(); let manage = arc_cam.stream == "subStream"; - s.spawn(move |_| camera_loop(&*sub_camera, "subStream", &mut output, manage)); + s.spawn(move |_| camera_loop(&*sub_camera, "subStream", &mut outputs, manage)); } } @@ -111,7 +104,7 @@ fn main() -> Result<(), Error> { fn camera_loop( camera_config: &CameraConfig, stream_name: &str, - output: &mut MaybeAppSrc, + outputs: &mut GstOutputs, manage: bool, ) -> Result { let min_backoff = Duration::from_secs(1); @@ -119,8 +112,9 @@ fn camera_loop( let mut current_backoff = min_backoff; loop { - let cam_err = camera_main(camera_config, stream_name, output, manage).unwrap_err(); - output.on_stream_error(); + let cam_err = camera_main(camera_config, stream_name, outputs, manage).unwrap_err(); + outputs.vidsrc.on_stream_error(); + outputs.audsrc.on_stream_error(); // Authentication failures are permanent; we retry everything else if cam_err.connected { current_backoff = min_backoff; @@ -199,7 +193,7 @@ fn get_permitted_users<'a>( fn camera_main( camera_config: &CameraConfig, stream_name: &str, - output: &mut dyn Write, + outputs: &mut GstOutputs, manage: bool, ) -> Result { let mut connected = false; @@ -251,7 +245,7 @@ fn camera_main( "{}: Starting video stream {}", camera_config.name, stream_name ); - camera.start_video(output, stream_name, camera_config.channel_id) + camera.start_video(outputs, stream_name, camera_config.channel_id) })() .map_err(|err| CameraErr { connected, err }) } From 428a6f767a195a6f064142257fe9c94e32a10cf9 Mon Sep 17 00:00:00 2001 From: twistedddx Date: Wed, 21 Oct 2020 19:33:44 +1030 Subject: [PATCH 3/3] Dissector - more message_types --- dissector/baichuan.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index aab5d6f..c91b8e2 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -50,6 +50,7 @@ message_types = { [15]="", [16]="", [18]="", + [23]="Reboot", [25]=" (write)", [26]="", -- [31]="Start Motion Alarm", @@ -78,6 +79,7 @@ message_types = { [59]=" (write)", [65]=" (Export)", [66]=" (Import)", + [67]=" (FW Upgrade)", [68]="", [69]=" (write)", [70]="", @@ -96,9 +98,11 @@ message_types = { [93]="", [97]="", [98]=" (write)", + [99]=" (factory default)", [100]=" (write)", [101]="", [102]="", + [103]=" (format)", [104]="", [105]=" (write)", [106]="", @@ -123,6 +127,9 @@ message_types = { [209]=" (write)", [210]="", [211]=" (write)", + [216]=" (write)", + [217]="", + [218]=" (write)", [219]="", [228]="", [229]=" (write)",