2023-08-10 01:02:54 +05:00
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
"encoding/binary"
|
|
|
|
)
|
|
|
|
|
2023-08-10 13:58:57 +05:00
|
|
|
type ChunkStream struct {
|
2023-08-10 01:02:54 +05:00
|
|
|
timestamp uint32
|
|
|
|
last_msg_strm_id uint32
|
2023-08-10 13:09:26 +05:00
|
|
|
last_msg_len uint32
|
|
|
|
last_msg_type uint8
|
2023-08-10 01:02:54 +05:00
|
|
|
timedelta uint32
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:58:57 +05:00
|
|
|
type Message struct {
|
2023-08-10 01:02:54 +05:00
|
|
|
data []byte
|
|
|
|
curr_bytes_read uint32
|
|
|
|
timestamp uint32
|
|
|
|
msg_type uint8
|
|
|
|
msg_len uint32
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
type ChunkBuffers struct {
|
2023-08-10 12:49:26 +05:00
|
|
|
time []byte
|
|
|
|
msg_len []byte
|
|
|
|
msg_typeid []byte
|
|
|
|
msg_streamid []byte
|
|
|
|
fmt_csid_byte []byte
|
|
|
|
csid_true []byte
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_basic_header(conn net.Conn, chunk_bufs_ptr *ChunkBuffers) (format uint8, csid uint32, err error) {
|
2023-08-10 12:49:26 +05:00
|
|
|
if _, err = conn.Read(chunk_bufs_ptr.fmt_csid_byte); err != nil {
|
|
|
|
return
|
2023-08-10 01:02:54 +05:00
|
|
|
}
|
2023-08-10 12:49:26 +05:00
|
|
|
format = uint8(chunk_bufs_ptr.fmt_csid_byte[0] >> 6)
|
|
|
|
switch chunk_bufs_ptr.fmt_csid_byte[0] & 0x3f {
|
2023-08-10 01:02:54 +05:00
|
|
|
case 0:
|
2023-08-10 12:49:26 +05:00
|
|
|
if _, err = conn.Read(chunk_bufs_ptr.csid_true[1:]); err != nil {
|
|
|
|
return
|
2023-08-10 01:02:54 +05:00
|
|
|
}
|
2023-08-10 12:49:26 +05:00
|
|
|
csid = uint32(chunk_bufs_ptr.csid_true[1]) + 64
|
2023-08-10 01:02:54 +05:00
|
|
|
case 1:
|
2023-08-10 12:49:26 +05:00
|
|
|
if _, err = conn.Read(chunk_bufs_ptr.csid_true); err != nil {
|
|
|
|
return
|
2023-08-10 01:02:54 +05:00
|
|
|
}
|
2023-08-10 12:49:26 +05:00
|
|
|
csid = uint32(binary.LittleEndian.Uint16(chunk_bufs_ptr.csid_true)) + 64
|
2023-08-10 01:02:54 +05:00
|
|
|
default:
|
2023-08-10 12:49:26 +05:00
|
|
|
csid = uint32(chunk_bufs_ptr.fmt_csid_byte[0] & 0x3f)
|
2023-08-10 01:02:54 +05:00
|
|
|
}
|
2023-08-10 12:49:26 +05:00
|
|
|
return
|
2023-08-10 01:02:54 +05:00
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_time(conn net.Conn, chunk_bufs_ptr *ChunkBuffers) (time uint32, extended_time bool, err error) {
|
2023-08-10 12:49:26 +05:00
|
|
|
chunk_bufs_ptr.time[0] = 0
|
|
|
|
if _, err = conn.Read(chunk_bufs_ptr.time[1:]); err != nil {
|
|
|
|
return
|
2023-08-10 01:02:54 +05:00
|
|
|
}
|
2023-08-10 12:49:26 +05:00
|
|
|
time = binary.BigEndian.Uint32(chunk_bufs_ptr.time)
|
|
|
|
if time ^ 0xffffff == 0 {
|
2023-08-10 01:02:54 +05:00
|
|
|
extended_time = true
|
|
|
|
}
|
2023-08-10 12:49:26 +05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_msg_len(conn net.Conn, chunk_bufs_ptr *ChunkBuffers) (msg_len uint32, err error) {
|
2023-08-10 12:49:26 +05:00
|
|
|
if _, err = conn.Read(chunk_bufs_ptr.msg_len[1:]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg_len = binary.BigEndian.Uint32(chunk_bufs_ptr.msg_len)
|
|
|
|
return
|
|
|
|
}
|
2023-08-10 01:02:54 +05:00
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_msg_typeid(conn net.Conn, chunk_bufs_ptr *ChunkBuffers) (msg_type uint8, err error) {
|
2023-08-10 12:49:26 +05:00
|
|
|
if _, err = conn.Read(chunk_bufs_ptr.msg_typeid); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg_type = chunk_bufs_ptr.msg_typeid[0]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_msg_streamid(conn net.Conn, chunk_bufs_ptr *ChunkBuffers) (msg_streamid uint32, err error) {
|
2023-08-10 12:49:26 +05:00
|
|
|
if _, err = conn.Read(chunk_bufs_ptr.msg_streamid); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg_streamid = binary.BigEndian.Uint32(chunk_bufs_ptr.msg_streamid)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_time_extd(conn net.Conn, chunk_bufs_ptr *ChunkBuffers) (time uint32, err error) {
|
2023-08-10 12:49:26 +05:00
|
|
|
if _, err = conn.Read(chunk_bufs_ptr.time); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time = binary.BigEndian.Uint32(chunk_bufs_ptr.time)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_message_header_0(conn net.Conn, chnk_stream_ptr *ChunkStream, msg_ptr *Message, chunk_bufs_ptr *ChunkBuffers) (error) {
|
2023-08-10 12:49:26 +05:00
|
|
|
var extended_time bool
|
|
|
|
var err error
|
|
|
|
chnk_stream_ptr.timestamp, extended_time, err = read_time(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_ptr.msg_len, err = read_msg_len(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
2023-08-10 01:02:54 +05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg_ptr.data = make([]byte, msg_ptr.msg_len)
|
2023-08-10 13:09:26 +05:00
|
|
|
chnk_stream_ptr.last_msg_len = msg_ptr.msg_len
|
2023-08-10 01:02:54 +05:00
|
|
|
|
2023-08-10 12:49:26 +05:00
|
|
|
msg_ptr.msg_type, err = read_msg_typeid(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
2023-08-10 01:02:54 +05:00
|
|
|
return err
|
|
|
|
}
|
2023-08-10 13:09:26 +05:00
|
|
|
chnk_stream_ptr.last_msg_type = msg_ptr.msg_type
|
2023-08-10 01:02:54 +05:00
|
|
|
|
2023-08-10 12:49:26 +05:00
|
|
|
chnk_stream_ptr.last_msg_strm_id, err = read_msg_streamid(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
2023-08-10 01:02:54 +05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if extended_time {
|
2023-08-10 12:49:26 +05:00
|
|
|
chnk_stream_ptr.timestamp, err = read_time_extd(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
2023-08-10 01:02:54 +05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg_ptr.timestamp = chnk_stream_ptr.timestamp
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_message_header_1(conn net.Conn, chnk_stream_ptr *ChunkStream, msg_ptr *Message, chunk_bufs_ptr *ChunkBuffers) (error) {
|
2023-08-10 12:49:26 +05:00
|
|
|
var extended_time bool
|
|
|
|
var err error
|
|
|
|
|
|
|
|
chnk_stream_ptr.timedelta, extended_time, err = read_time(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg_ptr.msg_len, err = read_msg_len(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg_ptr.data = make([]byte, msg_ptr.msg_len)
|
2023-08-10 13:09:26 +05:00
|
|
|
chnk_stream_ptr.last_msg_len = msg_ptr.msg_len
|
2023-08-10 12:49:26 +05:00
|
|
|
|
|
|
|
msg_ptr.msg_type, err = read_msg_typeid(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-10 13:09:26 +05:00
|
|
|
chnk_stream_ptr.last_msg_type = msg_ptr.msg_type
|
2023-08-10 12:49:26 +05:00
|
|
|
|
|
|
|
if extended_time {
|
|
|
|
chnk_stream_ptr.timedelta, err = read_time_extd(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chnk_stream_ptr.timestamp += chnk_stream_ptr.timedelta
|
|
|
|
msg_ptr.timestamp = chnk_stream_ptr.timestamp
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_message_header_2(conn net.Conn, chnk_stream_ptr *ChunkStream, msg_ptr *Message, chunk_bufs_ptr *ChunkBuffers) (error) {
|
2023-08-10 13:09:26 +05:00
|
|
|
var extended_time bool
|
|
|
|
var err error
|
|
|
|
|
|
|
|
chnk_stream_ptr.timedelta, extended_time, err = read_time(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_ptr.msg_len = chnk_stream_ptr.last_msg_len
|
|
|
|
msg_ptr.data = make([]byte, msg_ptr.msg_len)
|
|
|
|
|
|
|
|
msg_ptr.msg_type = chnk_stream_ptr.last_msg_type
|
|
|
|
|
|
|
|
if extended_time {
|
|
|
|
chnk_stream_ptr.timedelta, err = read_time_extd(conn, chunk_bufs_ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chnk_stream_ptr.timestamp += chnk_stream_ptr.timedelta
|
|
|
|
msg_ptr.timestamp = chnk_stream_ptr.timestamp
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func read_chunk_data(conn net.Conn, msg_ptr *Message, chnk_size uint32) (uint32, error) {
|
2023-08-10 01:02:54 +05:00
|
|
|
bytes_left := msg_ptr.msg_len - msg_ptr.curr_bytes_read
|
|
|
|
var buffer_end uint32
|
|
|
|
if bytes_left < chnk_size {
|
2023-08-10 03:24:48 +05:00
|
|
|
buffer_end = bytes_left + msg_ptr.curr_bytes_read
|
2023-08-10 01:02:54 +05:00
|
|
|
} else {
|
2023-08-10 03:24:48 +05:00
|
|
|
buffer_end = chnk_size + msg_ptr.curr_bytes_read
|
2023-08-10 01:02:54 +05:00
|
|
|
}
|
|
|
|
n, err := conn.Read(msg_ptr.data[msg_ptr.curr_bytes_read:buffer_end])
|
|
|
|
if err != nil {
|
2023-08-10 15:28:43 +05:00
|
|
|
return 0, err
|
2023-08-10 01:02:54 +05:00
|
|
|
}
|
|
|
|
msg_ptr.curr_bytes_read += uint32(n)
|
2023-08-10 15:28:43 +05:00
|
|
|
return uint32(n), nil
|
2023-08-10 01:02:54 +05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
func ReadChunk(conn net.Conn, open_chnkstrms map[uint32]*ChunkStream, open_msgs map[uint32]*Message, chnk_size uint32, chunk_bufs_ptr *ChunkBuffers, n_read *uint32) (*Message, error){
|
2023-08-10 01:02:54 +05:00
|
|
|
conn.SetDeadline(time.Now().Add(10 * time.Second))
|
2023-08-10 15:28:43 +05:00
|
|
|
|
2023-08-10 13:58:57 +05:00
|
|
|
var chnkstream_ptr *ChunkStream
|
|
|
|
var msg_ptr *Message
|
2023-08-10 01:02:54 +05:00
|
|
|
|
2023-08-10 12:49:26 +05:00
|
|
|
format, csid, err := read_basic_header(conn, chunk_bufs_ptr)
|
2023-08-10 01:02:54 +05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-10 15:28:43 +05:00
|
|
|
if csid < 64 {
|
|
|
|
*n_read += 1
|
|
|
|
} else if csid < 320 {
|
|
|
|
*n_read += 2
|
|
|
|
} else {
|
|
|
|
*n_read += 3
|
|
|
|
}
|
2023-08-10 01:02:54 +05:00
|
|
|
|
|
|
|
switch format {
|
|
|
|
case 0:
|
2023-08-10 13:58:57 +05:00
|
|
|
chnkstream_ptr = new(ChunkStream)
|
2023-08-10 01:02:54 +05:00
|
|
|
open_chnkstrms[csid] = chnkstream_ptr
|
2023-08-10 13:58:57 +05:00
|
|
|
msg_ptr = new(Message)
|
2023-08-10 01:02:54 +05:00
|
|
|
|
2023-08-10 12:49:26 +05:00
|
|
|
if err := read_message_header_0(conn, chnkstream_ptr, msg_ptr, chunk_bufs_ptr); err != nil {
|
2023-08-10 01:02:54 +05:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-10 15:28:43 +05:00
|
|
|
*n_read += 11
|
|
|
|
if msg_ptr.timestamp > 0xffffff {
|
|
|
|
*n_read += 4
|
|
|
|
}
|
2023-08-10 12:49:26 +05:00
|
|
|
case 1:
|
|
|
|
chnkstream_ptr = open_chnkstrms[csid]
|
2023-08-10 13:58:57 +05:00
|
|
|
msg_ptr = new(Message)
|
2023-08-10 12:49:26 +05:00
|
|
|
|
|
|
|
if err := read_message_header_1(conn, chnkstream_ptr, msg_ptr, chunk_bufs_ptr); err != nil {
|
2023-08-10 01:02:54 +05:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-10 15:28:43 +05:00
|
|
|
*n_read += 7
|
|
|
|
if msg_ptr.timestamp > 0xffffff {
|
|
|
|
*n_read += 4
|
|
|
|
}
|
2023-08-10 13:09:26 +05:00
|
|
|
case 2:
|
|
|
|
chnkstream_ptr = open_chnkstrms[csid]
|
2023-08-10 13:58:57 +05:00
|
|
|
msg_ptr = new(Message)
|
2023-08-10 13:09:26 +05:00
|
|
|
|
|
|
|
if err := read_message_header_2(conn, chnkstream_ptr, msg_ptr, chunk_bufs_ptr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-10 15:28:43 +05:00
|
|
|
*n_read += 3
|
|
|
|
if msg_ptr.timestamp > 0xffffff {
|
|
|
|
*n_read += 4
|
|
|
|
}
|
2023-08-10 13:24:06 +05:00
|
|
|
case 3:
|
|
|
|
chnkstream_ptr = open_chnkstrms[csid]
|
2023-08-15 15:55:11 +05:00
|
|
|
msg_prev, ok := open_msgs[chnkstream_ptr.last_msg_strm_id]
|
|
|
|
if !ok {
|
|
|
|
msg_ptr = new(Message)
|
|
|
|
msg_ptr.msg_type = chnkstream_ptr.last_msg_type
|
|
|
|
msg_ptr.msg_len = chnkstream_ptr.last_msg_len
|
|
|
|
msg_ptr.data = make([]byte, msg_ptr.msg_len)
|
|
|
|
|
|
|
|
chnkstream_ptr.timestamp += chnkstream_ptr.timedelta
|
|
|
|
msg_ptr.timestamp = chnkstream_ptr.timestamp
|
|
|
|
} else {
|
|
|
|
msg_ptr = msg_prev
|
|
|
|
}
|
2023-08-10 12:49:26 +05:00
|
|
|
}
|
|
|
|
|
2023-08-10 15:28:43 +05:00
|
|
|
n, err := read_chunk_data(conn, msg_ptr, chnk_size)
|
|
|
|
if err != nil {
|
2023-08-10 12:49:26 +05:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-10 15:28:43 +05:00
|
|
|
*n_read += n
|
2023-08-10 12:49:26 +05:00
|
|
|
|
2023-08-10 13:24:06 +05:00
|
|
|
conn.SetDeadline(time.Time{})
|
|
|
|
|
2023-08-10 12:49:26 +05:00
|
|
|
if msg_ptr.curr_bytes_read < msg_ptr.msg_len {
|
|
|
|
open_msgs[chnkstream_ptr.last_msg_strm_id] = msg_ptr
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
delete(open_msgs, chnkstream_ptr.last_msg_strm_id)
|
|
|
|
return msg_ptr, nil
|
2023-08-10 01:02:54 +05:00
|
|
|
}
|
|
|
|
}
|