-
Notifications
You must be signed in to change notification settings - Fork 52
/
msg.go
790 lines (687 loc) · 24 KB
/
msg.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
package rtmp
import (
"encoding/binary"
"errors"
"strings"
"go.uber.org/zap"
"m7s.live/engine/v4/util"
)
// https://zhuanlan.zhihu.com/p/196743129
const (
/* RTMP Message ID*/
// Protocal Control Messgae(1-7)
// Chunk
RTMP_MSG_CHUNK_SIZE = 1
RTMP_MSG_ABORT = 2
// RTMP
RTMP_MSG_ACK = 3
RTMP_MSG_USER_CONTROL = 4
RTMP_MSG_ACK_SIZE = 5
RTMP_MSG_BANDWIDTH = 6
RTMP_MSG_EDGE = 7
RTMP_MSG_AUDIO = 8
RTMP_MSG_VIDEO = 9
RTMP_MSG_AMF3_METADATA = 15
RTMP_MSG_AMF3_SHARED = 16
RTMP_MSG_AMF3_COMMAND = 17
RTMP_MSG_AMF0_METADATA = 18
RTMP_MSG_AMF0_SHARED = 19
RTMP_MSG_AMF0_COMMAND = 20
RTMP_MSG_AGGREGATE = 22
RTMP_DEFAULT_CHUNK_SIZE = 128
RTMP_MAX_CHUNK_SIZE = 65536
RTMP_MAX_CHUNK_HEADER = 18
// User Control Event
RTMP_USER_STREAM_BEGIN = 0
RTMP_USER_STREAM_EOF = 1
RTMP_USER_STREAM_DRY = 2
RTMP_USER_SET_BUFFLEN = 3
RTMP_USER_STREAM_IS_RECORDED = 4
RTMP_USER_PING_REQUEST = 6
RTMP_USER_PING_RESPONSE = 7
RTMP_USER_EMPTY = 31
// StreamID == (ChannelID-4)/5+1
// ChannelID == Chunk Stream ID
// StreamID == Message Stream ID
// Chunk Stream ID == 0, 第二个byte + 64
// Chunk Stream ID == 1, (第三个byte) * 256 + 第二个byte + 64
// Chunk Stream ID == 2.
// 2 < Chunk Stream ID < 64(2的6次方)
RTMP_CSID_CONTROL = 0x02
RTMP_CSID_COMMAND = 0x03
RTMP_CSID_AUDIO = 0x06
RTMP_CSID_DATA = 0x05
RTMP_CSID_VIDEO = 0x05
)
func newChunkHeader(messageType byte) *ChunkHeader {
head := new(ChunkHeader)
head.ChunkStreamID = RTMP_CSID_CONTROL
if messageType == RTMP_MSG_AMF0_COMMAND {
head.ChunkStreamID = RTMP_CSID_COMMAND
}
head.MessageTypeID = messageType
return head
}
func (h ChunkHeader) Clone() *ChunkHeader {
return &h
}
type RtmpMessage interface {
Encode(util.IAMF)
}
type HaveStreamID interface {
GetStreamID() uint32
}
func GetRtmpMessage(chunk *Chunk, body util.Buffer) error {
switch chunk.MessageTypeID {
case RTMP_MSG_CHUNK_SIZE, RTMP_MSG_ABORT, RTMP_MSG_ACK, RTMP_MSG_ACK_SIZE:
if body.Len() < 4 {
return errors.New("chunk.Body < 4")
}
chunk.MsgData = Uint32Message(body.ReadUint32())
case RTMP_MSG_USER_CONTROL: // RTMP消息类型ID=4, 用户控制消息.客户端或服务端发送本消息通知对方用户的控制事件.
{
if body.Len() < 2 {
return errors.New("UserControlMessage.Body < 2")
}
base := UserControlMessage{
EventType: body.ReadUint16(),
EventData: body,
}
switch base.EventType {
case RTMP_USER_STREAM_BEGIN: // 服务端向客户端发送本事件通知对方一个流开始起作用可以用于通讯.在默认情况下,服务端在成功地从客户端接收连接命令之后发送本事件,事件ID为0.事件数据是表示开始起作用的流的ID.
m := &StreamIDMessage{
UserControlMessage: base,
StreamID: 0,
}
if len(base.EventData) >= 4 {
//服务端在成功地从客户端接收连接命令之后发送本事件,事件ID为0.事件数据是表示开始起作用的流的ID.
m.StreamID = body.ReadUint32()
}
chunk.MsgData = m
case RTMP_USER_STREAM_EOF, RTMP_USER_STREAM_DRY, RTMP_USER_STREAM_IS_RECORDED: // 服务端向客户端发送本事件通知客户端,数据回放完成.果没有发行额外的命令,就不再发送数据.客户端丢弃从流中接收的消息.4字节的事件数据表示,回放结束的流的ID.
chunk.MsgData = &StreamIDMessage{
UserControlMessage: base,
StreamID: body.ReadUint32(),
}
case RTMP_USER_SET_BUFFLEN: // 客户端向服务端发送本事件,告知对方自己存储一个流的数据的缓存的长度(毫秒单位).当服务端开始处理一个流得时候发送本事件.事件数据的头四个字节表示流ID,后4个字节表示缓存长度(毫秒单位).
chunk.MsgData = &SetBufferMessage{
StreamIDMessage: StreamIDMessage{
UserControlMessage: base,
StreamID: body.ReadUint32(),
},
Millisecond: body.ReadUint32(),
}
case RTMP_USER_PING_REQUEST: // 服务端通过本事件测试客户端是否可达.事件数据是4个字节的事件戳.代表服务调用本命令的本地时间.客户端在接收到kMsgPingRequest之后返回kMsgPingResponse事件
chunk.MsgData = &PingRequestMessage{
UserControlMessage: base,
Timestamp: body.ReadUint32(),
}
case RTMP_USER_PING_RESPONSE, RTMP_USER_EMPTY: // 客户端向服务端发送本消息响应ping请求.事件数据是接kMsgPingRequest请求的时间.
chunk.MsgData = &base
default:
chunk.MsgData = &base
}
}
case RTMP_MSG_BANDWIDTH: // RTMP消息类型ID=6, 置对等端带宽.客户端或服务端发送本消息更新对等端的输出带宽.
if body.Len() < 4 {
return errors.New("chunk.Body < 4")
}
m := &SetPeerBandwidthMessage{
AcknowledgementWindowsize: body.ReadUint32(),
}
if body.Len() > 0 {
m.LimitType = body[0]
}
chunk.MsgData = m
case RTMP_MSG_EDGE: // RTMP消息类型ID=7, 用于边缘服务与源服务器.
case RTMP_MSG_AUDIO: // RTMP消息类型ID=8, 音频数据.客户端或服务端发送本消息用于发送音频数据.
case RTMP_MSG_VIDEO: // RTMP消息类型ID=9, 视频数据.客户端或服务端发送本消息用于发送视频数据.
case RTMP_MSG_AMF3_METADATA: // RTMP消息类型ID=15, 数据消息.用AMF3编码.
case RTMP_MSG_AMF3_SHARED: // RTMP消息类型ID=16, 共享对象消息.用AMF3编码.
case RTMP_MSG_AMF3_COMMAND: // RTMP消息类型ID=17, 命令消息.用AMF3编码.
decodeCommandAMF0(chunk, body[1:])
case RTMP_MSG_AMF0_METADATA: // RTMP消息类型ID=18, 数据消息.用AMF0编码.
case RTMP_MSG_AMF0_SHARED: // RTMP消息类型ID=19, 共享对象消息.用AMF0编码.
case RTMP_MSG_AMF0_COMMAND: // RTMP消息类型ID=20, 命令消息.用AMF0编码.
decodeCommandAMF0(chunk, body) // 解析具体的命令消息
case RTMP_MSG_AGGREGATE:
default:
}
return nil
}
// 03 00 00 00 00 01 02 14 00 00 00 00 02 00 07 63 6F 6E 6E 65 63 74 00 3F F0 00 00 00 00 00 00 08
//
// 这个函数解析的是从02(第13个字节)开始,前面12个字节是Header,后面的是Payload,即解析Payload.
//
// 解析用AMF0编码的命令消息.(Payload)
// 第一个字节(Byte)为此数据的类型.例如:string,int,bool...
// string就是字符类型,一个byte的amf类型,两个bytes的字符长度,和N个bytes的数据.
// 比如: 02 00 02 33 22,第一个byte为amf类型,其后两个bytes为长度,注意这里的00 02是大端模式,33 22是字符数据
// umber类型其实就是double,占8bytes.
// 比如: 00 00 00 00 00 00 00 00,第一个byte为amf类型,其后8bytes为double值0.0
// boolean就是布尔类型,占用1byte.
// 比如:01 00,第一个byte为amf类型,其后1byte是值,false.
// object类型要复杂点.
// 第一个byte是03表示object,其后跟的是N个(key+value).最后以00 00 09表示object结束
func decodeCommandAMF0(chunk *Chunk, body []byte) {
amf := util.AMF{body} // rtmp_amf.go, amf 是 bytes类型, 将rtmp body(payload)放到bytes.Buffer(amf)中去.
cmd := amf.ReadShortString() // rtmp_amf.go, 将payload的bytes类型转换成string类型.
cmdMsg := CommandMessage{
cmd,
uint64(amf.ReadNumber()),
}
switch cmd {
case "connect", "call":
chunk.MsgData = &CallMessage{
cmdMsg,
amf.ReadObject(),
amf.ReadObject(),
}
case "createStream":
amf.Unmarshal()
chunk.MsgData = &cmdMsg
case "play":
amf.Unmarshal()
m := &PlayMessage{
CURDStreamMessage{
cmdMsg,
chunk.MessageStreamID,
},
amf.ReadShortString(),
float64(-2),
float64(-1),
true,
}
for i := 0; i < 3; i++ {
if v, _ := amf.Unmarshal(); v != nil {
switch vv := v.(type) {
case float64:
if i == 0 {
m.Start = vv
} else {
m.Duration = vv
}
case bool:
m.Reset = vv
i = 2
}
} else {
break
}
}
chunk.MsgData = m
case "play2":
amf.Unmarshal()
chunk.MsgData = &Play2Message{
cmdMsg,
uint64(amf.ReadNumber()),
amf.ReadShortString(),
amf.ReadShortString(),
uint64(amf.ReadNumber()),
amf.ReadShortString(),
}
case "publish":
amf.Unmarshal()
chunk.MsgData = &PublishMessage{
CURDStreamMessage{
cmdMsg,
chunk.MessageStreamID,
},
amf.ReadShortString(),
amf.ReadShortString(),
}
case "pause":
amf.Unmarshal()
chunk.MsgData = &PauseMessage{
cmdMsg,
amf.ReadBool(),
uint64(amf.ReadNumber()),
}
case "seek":
amf.Unmarshal()
chunk.MsgData = &SeekMessage{
cmdMsg,
uint64(amf.ReadNumber()),
}
case "deleteStream", "closeStream":
amf.Unmarshal()
chunk.MsgData = &CURDStreamMessage{
cmdMsg,
uint32(amf.ReadNumber()),
}
case "releaseStream":
amf.Unmarshal()
chunk.MsgData = &ReleaseStreamMessage{
cmdMsg,
amf.ReadShortString(),
}
case "receiveAudio", "receiveVideo":
amf.Unmarshal()
chunk.MsgData = &ReceiveAVMessage{
cmdMsg,
amf.ReadBool(),
}
case Response_Result, Response_Error, Response_OnStatus:
if cmdMsg.TransactionId == 2 {
chunk.MsgData = &ResponseCreateStreamMessage{
cmdMsg, amf.ReadObject(), uint32(amf.ReadNumber()),
}
return
}
response := &ResponseMessage{
cmdMsg,
amf.ReadObject(),
amf.ReadObject(), "",
}
if response.Infomation == nil && response.Properties != nil {
response.Infomation = response.Properties
}
codef := zap.String("code", response.Infomation["code"].(string))
switch response.Infomation["level"] {
case Level_Status:
RTMPPlugin.Info("_result :", codef)
case Level_Warning:
RTMPPlugin.Warn("_result :", codef)
case Level_Error:
RTMPPlugin.Error("_result :", codef)
}
if strings.HasPrefix(response.Infomation["code"].(string), "NetStream.Publish") {
chunk.MsgData = &ResponsePublishMessage{
cmdMsg,
response.Properties,
response.Infomation,
chunk.MessageStreamID,
}
} else if strings.HasPrefix(response.Infomation["code"].(string), "NetStream.Play") {
chunk.MsgData = &ResponsePlayMessage{
cmdMsg,
response.Infomation,
chunk.MessageStreamID,
}
} else {
chunk.MsgData = response
}
case "FCPublish", "FCUnpublish":
fallthrough
default:
chunk.MsgData = &struct{ CommandMessage }{cmdMsg}
RTMPPlugin.Info("decode command amf0 ", zap.String("cmd", cmd))
}
}
/* Command Message */
type CommandMessage struct {
CommandName string // 命令名. 字符串. 命令名.设置为"connect"
TransactionId uint64 // 传输ID. 数字. 总是设为1
}
type Commander interface {
GetCommand() *CommandMessage
}
func (cmd *CommandMessage) GetCommand() *CommandMessage {
return cmd
}
func (msg *CommandMessage) Encode(buf util.IAMF) {
buf.Marshals(msg.CommandName, msg.TransactionId, nil)
}
// Protocol control message 1.
// Set Chunk Size, is used to notify the peer of a new maximum chunk size
// chunk size (31 bits): This field holds the new maximum chunk size,in bytes, which will be used for all of the sender’s subsequent chunks until further notice
type Uint32Message uint32
func (msg Uint32Message) Encode(buf util.IAMF) {
binary.BigEndian.PutUint32(buf.Malloc(4), uint32(msg))
}
// Protocol control message 4, User Control Messages.
// User Control messages SHOULD use message stream ID 0 (known as the control stream) and, when sent over RTMP Chunk Stream,
// be sent on chunk stream ID 2. User Control messages are effective at the point they are received in the stream; their timestamps are ignored.
// Event Type (16 bits) : The first 2 bytes of the message data are used to identify the Event type. Event type is followed by Event data.
// Event Data
type UserControlMessage struct {
EventType uint16
EventData []byte
}
// Protocol control message 6, Set Peer Bandwidth Message.
// The client or the server sends this message to limit the output bandwidth of its peer.
// AcknowledgementWindowsize (4 bytes)
// LimitType : The Limit Type is one of the following values: 0 - Hard, 1 - Soft, 2- Dynamic.
type SetPeerBandwidthMessage struct {
AcknowledgementWindowsize uint32 // 4 bytes
LimitType byte
}
func (msg *SetPeerBandwidthMessage) Encode(buf util.IAMF) {
buf.WriteUint32(msg.AcknowledgementWindowsize)
buf.WriteByte(msg.LimitType)
}
// Message 15, 18. Data Message. The client or the server sends this message to send Metadata or any
// user data to the peer. Metadata includes details about the data(audio, video etc.) like creation time, duration,
// theme and so on. These messages have been assigned message type value of 18 for AMF0 and message type value of 15 for AMF3
type MetadataMessage struct {
Proterties map[string]interface{} `json:",omitempty"`
}
// Object 可选值:
// App 客户端要连接到的服务应用名 Testapp
// Flashver Flash播放器版本.和应用文档中getversion()函数返回的字符串相同. FMSc/1.0
// SwfUrl 发起连接的swf文件的url file://C:/ FlvPlayer.swf
// TcUrl 服务url.有下列的格式.protocol://servername:port/appName/appInstance rtmp://localhost::1935/testapp/instance1
// fpad 是否使用代理 true or false
// audioCodecs 指示客户端支持的音频编解码器 SUPPORT_SND_MP3
// videoCodecs 指示支持的视频编解码器 SUPPORT_VID_SORENSON
// pageUrl SWF文件被加载的页面的Url http:// somehost/sample.html
// objectEncoding AMF编码方法 AMF编码方法 kAMF3
// Call Message.
// The call method of the NetConnection object runs remote procedure calls (RPC) at the receiving end.
// The called RPC name is passed as a parameter to the call command.
type CallMessage struct {
CommandMessage
Object map[string]any `json:",omitempty"`
Optional map[string]any `json:",omitempty"`
}
func (msg *CallMessage) Encode(buf util.IAMF) {
buf.Marshals(msg.CommandName, msg.TransactionId, msg.Object)
if msg.Optional != nil {
buf.Marshals(msg.Optional)
}
}
// func (msg *CallMessage) Encode3() []byte {
// var amf util.AMF
// amf.WriteByte(0)
// return amf.Marshals(msg.CommandName, msg.TransactionId, msg.Object, msg.Optional)
// }
// Create Stream Message.
// The client sends this command to the server to create a logical channel for message communication The publishing of audio,
// video, and metadata is carried out over stream channel created using the createStream command.
/*
func (msg *CreateStreamMessage) Encode3() {
msg.Encode0()
buf := new(bytes.Buffer)
buf.WriteByte(0)
buf.Write(msg.RtmpBody)
msg.RtmpBody = buf.Bytes()
}*/
// The following commands can be sent on the NetStream by the client to the server:
// Play
// Play2
// DeleteStream
// CloseStream
// ReceiveAudio
// ReceiveVideo
// Publish
// Seek
// Pause
// Release(37)
// FCPublish
// Play Message
// The client sends this command to the server to play a stream. A playlist can also be created using this command multiple times
type PlayMessage struct {
CURDStreamMessage
StreamName string
Start float64
Duration float64
Reset bool
}
// 命令名 -> 命令名,设置为”play”
// 传输ID -> 0
// 命令对象
// 流名字 -> 要播放流的名字
// start -> 可选的参数,以秒为单位定义开始时间.默认值为 -2,表示用户首先尝试播放流名字段中定义的直播流.
// Duration -> 可选的参数,以秒为单位定义了回放的持续时间.默认值为 -1.-1 值意味着一个直播流会一直播放直到它不再可用或者一个录制流一直播放直到结束
// Reset -> 可选的布尔值或者数字定义了是否对以前的播放列表进行 flush
func (msg *PlayMessage) Encode(buf util.IAMF) {
// if msg.Start > 0 {
// amf.writeNumber(msg.Start)
// }
// if msg.Duration > 0 {
// amf.writeNumber(msg.Duration)
// }
// amf.writeBool(msg.Reset)
buf.Marshals(msg.CommandName, msg.TransactionId, nil, msg.StreamName, -2000)
}
/*
func (msg *PlayMessage) Encode3() {
}*/
// Play2 Message
// Unlike the play command, play2 can switch to a different bit rate stream without changing the timeline of the content played. The
// server maintains multiple files for all supported bitrates that the client can request in play2.
type Play2Message struct {
CommandMessage
StartTime uint64
OldStreamName string
StreamName string
Duration uint64
Transition string
}
func (msg *Play2Message) Encode0() {
}
// Delete Stream Message
// NetStream sends the deleteStream command when the NetStream object is getting destroyed
type CURDStreamMessage struct {
CommandMessage
StreamId uint32
}
func (msg *CURDStreamMessage) GetStreamID() uint32 {
return msg.StreamId
}
func (msg *CURDStreamMessage) Encode0() {
}
type ReleaseStreamMessage struct {
CommandMessage
StreamName string
}
func (msg *ReleaseStreamMessage) Encode0() {
}
// Receive Audio Message
// NetStream sends the receiveAudio message to inform the server whether to send or not to send the audio to the client
type ReceiveAVMessage struct {
CommandMessage
BoolFlag bool
}
func (msg *ReceiveAVMessage) Encode0() {
}
// Publish Message
// The client sends the publish command to publish a named stream to the server. Using this name,
// any client can play this stream and receive the published audio, video, and data messages
type PublishMessage struct {
CURDStreamMessage
PublishingName string
PublishingType string
}
// 命令名 -> 命令名,设置为”publish”
// 传输ID -> 0
// 命令对象
// 发布名 -> 流发布的名字
// 发布类型 -> 设置为”live”,”record”或”append”.
// “record”:流被发布,并且数据被录制到一个新的文件,文件被存储到服务端的服务应用的目录的一个子目录下.如果文件已经存在则重写文件.
// “append”:流被发布并且附加到一个文件之后.如果没有发现文件则创建一个文件.
// “live”:发布直播数据而不录制到文件
func (msg *PublishMessage) Encode(buf util.IAMF) {
buf.Marshals(msg.CommandName, msg.TransactionId, nil, msg.PublishingName, msg.PublishingType)
}
// Seek Message
// The client sends the seek command to seek the offset (in milliseconds) within a media file or playlist.
type SeekMessage struct {
CommandMessage
Milliseconds uint64
}
func (msg *SeekMessage) Encode0() {
}
// Pause Message
// The client sends the pause command to tell the server to pause or start playing.
type PauseMessage struct {
CommandMessage
Pause bool
Milliseconds uint64
}
// 命令名 -> 命令名,设置为”pause”
// 传输ID -> 0
// 命令对象 -> null
// Pause/Unpause Flag -> true 或者 false,来指示暂停或者重新播放
// milliSeconds -> 流暂停或者重新开始所在的毫秒数.这个是客户端暂停的当前流时间.当回放已恢复时,服务器端值发送带有比这个值大的 timestamp 消息
func (msg *PauseMessage) Encode0() {
}
//
// Response Message. Server -> Response -> Client
//
// Response Connect Message
type ResponseConnectMessage struct {
CommandMessage
Properties map[string]any `json:",omitempty"`
Infomation map[string]any `json:",omitempty"`
}
func (msg *ResponseConnectMessage) Encode(buf util.IAMF) {
buf.Marshals(msg.CommandName, msg.TransactionId, msg.Properties, msg.Infomation)
}
/*
func (msg *ResponseConnectMessage) Encode3() {
}*/
// Response Call Message
type ResponseCallMessage struct {
CommandMessage
Object map[string]any
Response map[string]any
}
// func (msg *ResponseCallMessage) Encode0() []byte {
// return codec.MarshalAMFs(msg.CommandName, msg.TransactionId, msg.Object, msg.Response)
// }
// Response Create Stream Message
type ResponseCreateStreamMessage struct {
CommandMessage
Object any `json:",omitempty"`
StreamId uint32
}
func (msg *ResponseCreateStreamMessage) Encode(buf util.IAMF) {
buf.Marshals(msg.CommandName, msg.TransactionId, nil, msg.StreamId)
}
/*
func (msg *ResponseCreateStreamMessage) Encode3() {
}*/
// func (msg *ResponseCreateStreamMessage) Decode0(chunk *Chunk) {
// amf := util.AMF{chunk.Body}
// msg.CommandName = amf.ReadShortString()
// msg.TransactionId = uint64(amf.ReadNumber())
// amf.Unmarshal()
// msg.StreamId = uint32(amf.ReadNumber())
// }
// func (msg *ResponseCreateStreamMessage) Decode3(chunk *Chunk) {
// chunk.Body = chunk.Body[1:]
// msg.Decode0(chunk)
// }
// Response Play Message
type ResponsePlayMessage struct {
CommandMessage
Infomation map[string]any `json:",omitempty"`
StreamID uint32
}
func (msg *ResponsePlayMessage) GetStreamID() uint32 {
return msg.StreamID
}
func (msg *ResponsePlayMessage) Encode(buf util.IAMF) {
buf.Marshals(msg.CommandName, msg.TransactionId, nil, msg.Infomation)
}
/*
func (msg *ResponsePlayMessage) Encode3() {
}*/
// func (msg *ResponsePlayMessage) Decode0(chunk *Chunk) {
// amf := util.AMF{chunk.Body}
// msg.CommandName = amf.ReadShortString()
// msg.TransactionId = uint64(amf.ReadNumber())
// msg.Infomation = amf.ReadObject()
// }
// func (msg *ResponsePlayMessage) Decode3(chunk *Chunk) {
// chunk.Body = chunk.Body[1:]
// msg.Decode0(chunk)
// }
// Response Publish Message
type ResponsePublishMessage struct {
CommandMessage
Properties map[string]any `json:",omitempty"`
Infomation map[string]any `json:",omitempty"`
StreamID uint32
}
func (msg *ResponsePublishMessage) GetStreamID() uint32 {
return msg.StreamID
}
// 命令名 -> 命令名,设置为"OnStatus"
// 传输ID -> 0
// 属性 -> null
// 信息 -> level, code, description
func (msg *ResponsePublishMessage) Encode(buf util.IAMF) {
buf.Marshals(msg.CommandName, msg.TransactionId, msg.Properties, msg.Infomation)
}
/*
func (msg *ResponsePublishMessage) Encode3() {
}*/
// Response Seek Message
type ResponseSeekMessage struct {
CommandMessage
Description string
}
func (msg *ResponseSeekMessage) Encode0() {
}
//func (msg *ResponseSeekMessage) Encode3() {
//}
// Response Pause Message
type ResponsePauseMessage struct {
CommandMessage
Description string
}
// 命令名 -> 命令名,设置为"OnStatus"
// 传输ID -> 0
// 描述
func (msg *ResponsePauseMessage) Encode0() {
}
//func (msg *ResponsePauseMessage) Encode3() {
//}
// Response Message
type ResponseMessage struct {
CommandMessage
Properties map[string]any `json:",omitempty"`
Infomation map[string]any `json:",omitempty"`
Description string
}
// User Control Message 4.
// The client or the server sends this message to notify the peer about the user control events.
// For information about the message format, see Section 6.2.
// The following user control event types are supported:
// Stream Begin (=0)
// The server sends this event to notify the client that a stream has become functional and can be
// used for communication. By default, this event is sent on ID 0 after the application connect
// command is successfully received from the client. The event data is 4-byte and represents
// the stream ID of the stream that became functional.
type StreamIDMessage struct {
UserControlMessage
StreamID uint32
}
func (msg *StreamIDMessage) Encode(buffer util.IAMF) {
buffer.WriteUint16(msg.EventType)
msg.EventData = buffer.Malloc(4)
binary.BigEndian.PutUint32(msg.EventData, msg.StreamID)
}
// SetBuffer Length (=3)
// The client sends this event to inform the server of the buffer size (in milliseconds) that is
// used to buffer any data coming over a stream. This event is sent before the server starts |
// processing the stream. The first 4 bytes of the event data represent the stream ID and the next |
// 4 bytes represent the buffer length, in milliseconds.
type SetBufferMessage struct {
StreamIDMessage
Millisecond uint32
}
func (msg *SetBufferMessage) Encode(buf util.IAMF) {
buf.WriteUint16(msg.EventType)
msg.EventData = buf.Malloc(8)
binary.BigEndian.PutUint32(msg.EventData, msg.StreamID)
binary.BigEndian.PutUint32(msg.EventData[4:], msg.Millisecond)
}
// PingRequest (=6)
// The server sends this event to test whether the client is reachable. Event data is a 4-byte
// timestamp, representing the local server time when the server dispatched the command.
// The client responds with PingResponse on receiving MsgPingRequest.
type PingRequestMessage struct {
UserControlMessage
Timestamp uint32
}
func (msg *PingRequestMessage) Encode(buf util.IAMF) {
buf.WriteUint16(msg.EventType)
msg.EventData = buf.Malloc(4)
binary.BigEndian.PutUint32(msg.EventData, msg.Timestamp)
}
func (msg *UserControlMessage) Encode(buf util.IAMF) {
buf.WriteUint16(msg.EventType)
}