forked from zhangpeihao/gortmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
117 lines (106 loc) · 2.71 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
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
// Copyright 2013, zhangpeihao All rights reserved.
package gortmp
import (
"bytes"
"log"
"fmt"
)
// 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 {
Timestamp uint32
ChunkStreamID uint32
Size uint32
Type uint8
MessageStreamID uint32
Buf *bytes.Buffer
IsInbound bool
AbsoluteTimestamp uint32
}
func NewMessage(csid uint32, typ uint8, msid uint32, ts uint32, data []byte) *Message {
message := &Message{
Timestamp: ts,
ChunkStreamID: csid,
Type: typ,
MessageStreamID: msid,
AbsoluteTimestamp: ts,
Buf: new(bytes.Buffer),
}
if data != nil {
message.Buf.Write(data)
message.Size = uint32(len(data))
}
return message
}
func CopyToStream(stream ServerStream, messageIn *Message) *Message {
byteCopy := make([]byte, messageIn.Buf.Len())
copy(byteCopy, messageIn.Buf.Bytes())
newBuffer := bytes.NewBuffer(byteCopy)
msgOut := Message{
Timestamp: messageIn.Timestamp,
ChunkStreamID: stream.ChunkStreamID(),
Size: messageIn.Size,
Type: messageIn.Type,
MessageStreamID: stream.ID(),
Buf: newBuffer,
IsInbound: false,
AbsoluteTimestamp: messageIn.AbsoluteTimestamp,
}
return &msgOut
}
func (message *Message) Dump(name string) string {
direction := "outbound"
if message.IsInbound {
direction = "inbound"
}
return fmt.Sprintf(
"%s[cs %d] AMF3 Message: timestamp: %d, ms id: %d, cs id: %d, type: %d (%s), size: %d, %s, AbsoluteTimestamp: %d",
name, message.ChunkStreamID, message.Timestamp, message.MessageStreamID, message.ChunkStreamID,
message.Type, message.TypeDisplay(), message.Size, direction, message.AbsoluteTimestamp)
}
func (message *Message) LogDump(name string) {
log.Println(message.Dump(name))
}
// 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())
}
func (message *Message) TypeDisplay() string {
switch (message.Type) {
default:
return "unknown"
case 1:
return "set-chunk-size"
case 2:
return "abort"
case 3:
return "acknowledgment"
case 4:
return "user-control"
case 5:
return "window-size-ack"
case 6:
return "set-peer-bandwidth"
case 20:
return "command-amf0"
case 17:
return "command-amf3"
case 18:
return "data-amf0"
case 15:
return "data-amf3"
case 8:
return "audio"
case 9:
return "video"
case 22:
return "aggregate"
}
}