forked from zhangpeihao/gortmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
55 lines (49 loc) · 1.52 KB
/
message.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
// Copyright 2013, zhangpeihao All rights reserved.
package gortmp
import (
"bytes"
"github.com/zhangpeihao/log"
)
// Message
//
// The different types of messages that are exchanged between the server
// and the client include audio messages for sending the audio data,
// video messages for sending video data, data messages for sending any
// user data, shared object messages, and command messages.
type Message struct {
ChunkStreamID uint32
Timestamp uint32
Size uint32
Type uint8
StreamID uint32
Buf *bytes.Buffer
IsInbound bool
AbsoluteTimestamp uint32
}
func NewMessage(csi uint32, t uint8, sid uint32, ts uint32, data []byte) *Message {
message := &Message{
ChunkStreamID: csi,
Type: t,
StreamID: sid,
Timestamp: ts,
AbsoluteTimestamp: ts,
Buf: new(bytes.Buffer),
}
if data != nil {
message.Buf.Write(data)
message.Size = uint32(len(data))
}
return message
}
func (message *Message) Dump(name string) {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_DEBUG,
"Message(%s){CID: %d, Type: %d, Timestamp: %d, Size: %d, StreamID: %d, IsInbound: %t, AbsoluteTimestamp: %d}\n", name,
message.ChunkStreamID, message.Type, message.Timestamp, message.Size, message.StreamID, message.IsInbound, message.AbsoluteTimestamp)
}
// The length of remain data to read
func (message *Message) Remain() uint32 {
if message.Buf == nil {
return message.Size
}
return message.Size - uint32(message.Buf.Len())
}