forked from zhangpeihao/gortmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunkstream.go
118 lines (105 loc) · 3.09 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2013, zhangpeihao All rights reserved.
package gortmp
import (
"github.com/zhangpeihao/log"
)
// Chunk stream
//
// A logical channel of communication that allows flow of chunks in a
// particular direction. The chunk stream can travel from the client
// to the server and reverse.
type OutboundChunkStream struct {
ID uint32
lastHeader *Header
lastOutAbsoluteTimestamp uint32
lastInAbsoluteTimestamp uint32
// Start at timestamp
startAt uint32
}
type InboundChunkStream struct {
ID uint32
lastHeader *Header
lastOutAbsoluteTimestamp uint32
lastInAbsoluteTimestamp uint32
// The unfinished incoming message
receivedMessage *Message
}
func NewOutboundChunkStream(id uint32) *OutboundChunkStream {
return &OutboundChunkStream{
ID: id,
}
}
func NewInboundChunkStream(id uint32) *InboundChunkStream {
return &InboundChunkStream{
ID: id,
}
}
func (chunkStream *OutboundChunkStream) NewOutboundHeader(message *Message) *Header {
header := &Header{
ChunkStreamID: chunkStream.ID,
MessageLength: uint32(message.Buf.Len()),
MessageTypeID: message.Type,
MessageStreamID: message.StreamID,
}
timestamp := message.Timestamp
if timestamp == AUTO_TIMESTAMP {
timestamp = chunkStream.GetTimestamp()
message.Timestamp = timestamp
message.AbsoluteTimestamp = timestamp
}
deltaTimestamp := uint32(0)
if chunkStream.lastOutAbsoluteTimestamp < message.Timestamp {
deltaTimestamp = message.Timestamp - chunkStream.lastOutAbsoluteTimestamp
}
if chunkStream.lastHeader == nil {
header.Fmt = HEADER_FMT_FULL
header.Timestamp = timestamp
} else {
if header.MessageStreamID == chunkStream.lastHeader.MessageStreamID {
if header.MessageTypeID == chunkStream.lastHeader.MessageTypeID &&
header.MessageLength == chunkStream.lastHeader.MessageLength {
switch chunkStream.lastHeader.Fmt {
case HEADER_FMT_FULL:
header.Fmt = HEADER_FMT_SAME_LENGTH_AND_STREAM
header.Timestamp = deltaTimestamp
case HEADER_FMT_SAME_STREAM:
fallthrough
case HEADER_FMT_SAME_LENGTH_AND_STREAM:
fallthrough
case HEADER_FMT_CONTINUATION:
if chunkStream.lastHeader.Timestamp == deltaTimestamp {
header.Fmt = HEADER_FMT_CONTINUATION
} else {
header.Fmt = HEADER_FMT_SAME_LENGTH_AND_STREAM
header.Timestamp = deltaTimestamp
}
}
} else {
header.Fmt = HEADER_FMT_SAME_STREAM
header.Timestamp = deltaTimestamp
}
} else {
header.Fmt = HEADER_FMT_FULL
header.Timestamp = timestamp
}
}
// Check extended timestamp
if header.Timestamp >= 0xffffff {
header.ExtendedTimestamp = message.Timestamp
header.Timestamp = 0xffffff
} else {
header.ExtendedTimestamp = 0
}
logger.ModulePrintf(logHandler, log.LOG_LEVEL_DEBUG,
"OutboundChunkStream::NewOutboundHeader() header: %+v\n", header)
chunkStream.lastHeader = header
chunkStream.lastOutAbsoluteTimestamp = timestamp
return header
}
func (chunkStream *OutboundChunkStream) GetTimestamp() uint32 {
if chunkStream.startAt == uint32(0) {
chunkStream.startAt = GetTimestamp()
return uint32(0)
}
return GetTimestamp() - chunkStream.startAt
}