-
Notifications
You must be signed in to change notification settings - Fork 3
/
chunkstream.go
100 lines (87 loc) · 2.35 KB
/
chunkstream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package rtmp
type OutboundChunkStream struct {
Id uint32
lastHeader *Header
lastOutAbsoluteTimestamp uint32
lastInAbsoluteTimestamp uint32
startAtTimestamp uint32
}
type InboundChunkStream struct {
Id uint32
lastHeader *Header
lastOutAbsoluteTimestamp uint32
lastInAbsoluteTimestamp uint32
currentMessage *Message
}
func NewOutboundChunkStream(id uint32) *OutboundChunkStream {
return &OutboundChunkStream{
Id: id,
}
}
func NewInboundChunkStream(id uint32) *InboundChunkStream {
return &InboundChunkStream{
Id: id,
}
}
func (cs *OutboundChunkStream) NewOutboundHeader(m *Message) *Header {
h := &Header{
ChunkStreamId: cs.Id,
MessageLength: uint32(m.Buffer.Len()),
MessageTypeId: m.Type,
MessageStreamId: m.StreamId,
}
ts := m.Timestamp
if ts == TIMESTAMP_AUTO {
ts = cs.GetTimestamp()
m.Timestamp = ts
m.AbsoluteTimestamp = ts
}
deltaTimestamp := uint32(0)
if cs.lastOutAbsoluteTimestamp < m.Timestamp {
deltaTimestamp = m.Timestamp - cs.lastOutAbsoluteTimestamp
}
if cs.lastHeader == nil {
h.Format = HEADER_FORMAT_FULL
h.Timestamp = ts
} else {
if h.MessageStreamId == cs.lastHeader.MessageStreamId {
if h.MessageTypeId == cs.lastHeader.MessageTypeId && h.MessageLength == cs.lastHeader.MessageLength {
switch cs.lastHeader.Format {
case HEADER_FORMAT_FULL:
h.Format = HEADER_FORMAT_SAME_LENGTH_AND_STREAM
h.Timestamp = deltaTimestamp
case HEADER_FORMAT_SAME_STREAM:
fallthrough
case HEADER_FORMAT_SAME_LENGTH_AND_STREAM:
fallthrough
case HEADER_FORMAT_CONTINUATION:
if cs.lastHeader.Timestamp == deltaTimestamp {
h.Format = HEADER_FORMAT_CONTINUATION
} else {
h.Format = HEADER_FORMAT_SAME_LENGTH_AND_STREAM
h.Timestamp = deltaTimestamp
}
}
} else {
h.Format = HEADER_FORMAT_SAME_STREAM
h.Timestamp = ts
}
}
}
if h.Timestamp >= TIMESTAMP_EXTENDED {
h.ExtendedTimestamp = m.Timestamp
h.Timestamp = TIMESTAMP_EXTENDED
} else {
h.ExtendedTimestamp = 0
}
cs.lastHeader = h
cs.lastOutAbsoluteTimestamp = ts
return h
}
func (cs *OutboundChunkStream) GetTimestamp() uint32 {
if cs.startAtTimestamp == uint32(0) {
cs.startAtTimestamp = GetCurrentTimestamp()
return uint32(0)
}
return GetCurrentTimestamp() - cs.startAtTimestamp
}