-
Notifications
You must be signed in to change notification settings - Fork 161
/
outboundstream.go
349 lines (316 loc) · 8.79 KB
/
outboundstream.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
// Copyright 2013, zhangpeihao All rights reserved.
package gortmp
import (
"errors"
"github.com/zhangpeihao/goamf"
"github.com/zhangpeihao/log"
)
type OutboundStreamHandler interface {
OnPlayStart(stream OutboundStream)
OnPublishStart(stream OutboundStream)
}
// Message stream:
//
// A logical channel of communication that allows the flow of
// messages.
type outboundStream struct {
id uint32
conn OutboundConn
chunkStreamID uint32
handler OutboundStreamHandler
bufferLength uint32
}
// A RTMP logical stream on connection.
type OutboundStream interface {
OutboundPublishStream
OutboundPlayStream
// ID
ID() uint32
// Pause
Pause() error
// Resume
Resume() error
// Close
Close()
// Received messages
Received(message *Message) (handlered bool)
// Attach handler
Attach(handler OutboundStreamHandler)
// Publish audio data
PublishAudioData(data []byte, deltaTimestamp uint32) error
// Publish video data
PublishVideoData(data []byte, deltaTimestamp uint32) error
// Publish data
PublishData(dataType uint8, data []byte, deltaTimestamp uint32) error
// Call
Call(name string, customParameters ...interface{}) error
}
// A publish stream
type OutboundPublishStream interface {
// Publish
Publish(name, t string) (err error)
// Send audio data
SendAudioData(data []byte) error
// Send video data
SendVideoData(data []byte) error
}
// A play stream
type OutboundPlayStream interface {
// Play
Play(streamName string, start, duration *uint32, reset *bool) (err error)
// Seeks the kerframe closedst to the specified location.
Seek(offset uint32)
}
// ID
func (stream *outboundStream) ID() uint32 {
return stream.id
}
// Pause
func (stream *outboundStream) Pause() error {
return errors.New("Unimplemented")
}
// Resume
func (stream *outboundStream) Resume() error {
return errors.New("Unimplemented")
}
// Close
func (stream *outboundStream) Close() {
var err error
cmd := &Command{
IsFlex: true,
Name: "closeStream",
TransactionID: 0,
Objects: make([]interface{}, 1),
}
cmd.Objects[0] = nil
message := NewMessage(stream.chunkStreamID, COMMAND_AMF3, stream.id, AUTO_TIMESTAMP, nil)
if err = cmd.Write(message.Buf); err != nil {
return
}
message.Dump("closeStream")
conn := stream.conn.Conn()
conn.Send(message)
}
// Send audio data
func (stream *outboundStream) SendAudioData(data []byte) error {
return errors.New("Unimplemented")
}
// Send video data
func (stream *outboundStream) SendVideoData(data []byte) error {
return errors.New("Unimplemented")
}
// Seeks the kerframe closedst to the specified location.
func (stream *outboundStream) Seek(offset uint32) {}
func (stream *outboundStream) Publish(streamName, howToPublish string) (err error) {
conn := stream.conn.Conn()
// Create publish command
cmd := &Command{
IsFlex: true,
Name: "publish",
TransactionID: 0,
Objects: make([]interface{}, 3),
}
cmd.Objects[0] = nil
cmd.Objects[1] = streamName
if len(howToPublish) > 0 {
cmd.Objects[2] = howToPublish
} else {
cmd.Objects[2] = nil
}
// Construct message
message := NewMessage(stream.chunkStreamID, COMMAND_AMF3, stream.id, 0, nil)
if err = cmd.Write(message.Buf); err != nil {
return
}
message.Dump("publish")
return conn.Send(message)
}
func (stream *outboundStream) Play(streamName string, start, duration *uint32, reset *bool) (err error) {
conn := stream.conn.Conn()
// Keng-die: in stream transaction ID always been 0
// Create play command
cmd := &Command{
IsFlex: false,
Name: "play",
TransactionID: 0,
Objects: make([]interface{}, 2),
}
cmd.Objects[0] = nil
cmd.Objects[1] = streamName
if start != nil {
cmd.Objects = append(cmd.Objects, start)
}
zero := 0
if duration != nil {
if start == nil {
cmd.Objects = append(cmd.Objects, &zero)
}
cmd.Objects = append(cmd.Objects, duration)
}
if reset != nil {
if duration == nil {
if start == nil {
cmd.Objects = append(cmd.Objects, &zero)
}
cmd.Objects = append(cmd.Objects, &zero)
}
cmd.Objects = append(cmd.Objects, reset)
}
// Construct message
message := NewMessage(stream.chunkStreamID, COMMAND_AMF0, stream.id, 0, nil)
if err = cmd.Write(message.Buf); err != nil {
return
}
message.Dump("play")
err = conn.Send(message)
if err != nil {
return
}
// Set Buffer Length
// Buffer length
if stream.bufferLength < MIN_BUFFER_LENGTH {
stream.bufferLength = MIN_BUFFER_LENGTH
}
stream.conn.Conn().SetStreamBufferSize(stream.id, stream.bufferLength)
return nil
}
func (stream *outboundStream) Call(name string, customParameters ...interface{}) (err error) {
conn := stream.conn.Conn()
// Create play command
cmd := &Command{
IsFlex: false,
Name: name,
TransactionID: 0,
Objects: make([]interface{}, 1+len(customParameters)),
}
cmd.Objects[0] = nil
for index, param := range customParameters {
cmd.Objects[index+1] = param
}
// Construct message
message := NewMessage(stream.chunkStreamID, COMMAND_AMF0, stream.id, 0, nil)
if err = cmd.Write(message.Buf); err != nil {
return
}
message.Dump(name)
err = conn.Send(message)
if err != nil {
return
}
// Set Buffer Length
// Buffer length
if stream.bufferLength < MIN_BUFFER_LENGTH {
stream.bufferLength = MIN_BUFFER_LENGTH
}
stream.conn.Conn().SetStreamBufferSize(stream.id, stream.bufferLength)
return nil
}
func (stream *outboundStream) Received(message *Message) bool {
if message.Type == VIDEO_TYPE || message.Type == AUDIO_TYPE {
return false
}
var err error
if message.Type == COMMAND_AMF0 || message.Type == COMMAND_AMF3 {
cmd := &Command{}
if message.Type == COMMAND_AMF3 {
cmd.IsFlex = true
_, err = message.Buf.ReadByte()
if err != nil {
logger.ModulePrintln(logHandler, log.LOG_LEVEL_WARNING,
"outboundStream::Received() Read first in flex commad err:", err)
return true
}
}
cmd.Name, err = amf.ReadString(message.Buf)
if err != nil {
logger.ModulePrintln(logHandler, log.LOG_LEVEL_WARNING,
"outboundStream::Received() AMF0 Read name err:", err)
return true
}
var transactionID float64
transactionID, err = amf.ReadDouble(message.Buf)
if err != nil {
logger.ModulePrintln(logHandler, log.LOG_LEVEL_WARNING,
"outboundStream::Received() AMF0 Read transactionID err:", err)
return true
}
cmd.TransactionID = uint32(transactionID)
var object interface{}
for message.Buf.Len() > 0 {
object, err = amf.ReadValue(message.Buf)
if err != nil {
logger.ModulePrintln(logHandler, log.LOG_LEVEL_WARNING,
"outboundStream::Received() AMF0 Read object err:", err)
return true
}
cmd.Objects = append(cmd.Objects, object)
}
switch cmd.Name {
case "onStatus":
return stream.onStatus(cmd)
case "onMetaData":
return stream.onMetaData(cmd)
case "onTimeCoordInfo":
return stream.onTimeCoordInfo(cmd)
default:
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"outboundStream::Received() Unknown command: %s\n", cmd.Name)
}
}
return false
}
func (stream *outboundStream) onStatus(cmd *Command) bool {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_TRACE, "onStatus: %+v\n", cmd)
code := ""
if len(cmd.Objects) >= 2 {
obj, ok := cmd.Objects[1].(amf.Object)
if ok {
value, ok := obj["code"]
if ok {
code, _ = value.(string)
}
}
}
switch code {
case NETSTREAM_PLAY_START:
logger.ModulePrintln(logHandler, log.LOG_LEVEL_TRACE, "Play started")
// Set buffer size
//stream.conn.Conn().SetStreamBufferSize(stream.id, 1500)
if stream.handler != nil {
stream.handler.OnPlayStart(stream)
}
case NETSTREAM_PUBLISH_START:
logger.ModulePrintln(logHandler, log.LOG_LEVEL_TRACE, "Publish started")
if stream.handler != nil {
stream.handler.OnPublishStart(stream)
}
}
return false
}
func (stream *outboundStream) onMetaData(cmd *Command) bool {
return false
}
func (stream *outboundStream) onTimeCoordInfo(cmd *Command) bool {
return false
}
func (stream *outboundStream) Attach(handler OutboundStreamHandler) {
stream.handler = handler
}
// Publish audio data
func (stream *outboundStream) PublishAudioData(data []byte, deltaTimestamp uint32) (err error) {
message := NewMessage(stream.chunkStreamID, AUDIO_TYPE, stream.id, AUTO_TIMESTAMP, data)
message.Timestamp = deltaTimestamp
return stream.conn.Send(message)
}
// Publish video data
func (stream *outboundStream) PublishVideoData(data []byte, deltaTimestamp uint32) (err error) {
message := NewMessage(stream.chunkStreamID, VIDEO_TYPE, stream.id, AUTO_TIMESTAMP, data)
message.Timestamp = deltaTimestamp
return stream.conn.Send(message)
}
// Publish data
func (stream *outboundStream) PublishData(dataType uint8, data []byte, deltaTimestamp uint32) (err error) {
message := NewMessage(stream.chunkStreamID, dataType, stream.id, AUTO_TIMESTAMP, data)
message.Timestamp = deltaTimestamp
return stream.conn.Send(message)
}