-
Notifications
You must be signed in to change notification settings - Fork 4
/
session.go
505 lines (436 loc) · 16.9 KB
/
session.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
package rtmp
import (
"fmt"
"github.com/torresjeff/rtmp/audio"
"github.com/torresjeff/rtmp/config"
"github.com/torresjeff/rtmp/rand"
"github.com/torresjeff/rtmp/video"
"go.uber.org/zap"
)
type AudioCallback func(format audio.Format, sampleRate audio.SampleRate, sampleSize audio.SampleSize, channels audio.Channel, payload []byte, timestamp uint32)
type VideoCallback func(frameType video.FrameType, codec video.Codec, payload []byte, timestamp uint32)
type MetadataCallback func(metadata map[string]interface{})
type surroundSound struct {
stereoSound bool
twoPointOneSound bool
threePointOneSound bool
fourPointZeroSound bool
fourPointOneSound bool
fivePointOneSound bool
sevenPointOneSound bool
}
type clientMetadata struct {
duration float64
fileSize float64
width float64
height float64
videoCodecID string
// number representation of videoCodecID (ffmpeg sends audioCodecID as a number rather than a string (like obs))
nVideoCodecID float64
videoDataRate float64
frameRate float64
audioCodecID string
// number representation of audioCodecID (ffmpeg sends audioCodecID as a number rather than a string (like obs))
nAudioCodecID float64
audioDataRate float64
audioSampleRate float64
audioSampleSize float64
audioChannels float64
sound surroundSound
encoder string
}
// Media Server interface defines the callbacks that are called when a message is received by the server
type MediaServer interface {
// Server callbacks
onSetChunkSize(size uint32)
onAbortMessage(chunkStreamId uint32)
onAck(sequenceNumber uint32)
onSetWindowAckSize(windowAckSize uint32)
onSetBandwidth(windowAckSize uint32, limitType uint8)
onConnect(csID uint32, transactionId float64, data map[string]interface{})
onReleaseStream(csID uint32, transactionId float64, args map[string]interface{}, streamKey string)
onFCPublish(csID uint32, transactionId float64, args map[string]interface{}, streamKey string)
onCreateStream(csID uint32, transactionId float64, data map[string]interface{})
onPublish(transactionId float64, args map[string]interface{}, streamKey string, publishingType string)
onFCUnpublish(args map[string]interface{}, streamKey string)
onDeleteStream(args map[string]interface{}, streamID float64)
onCloseStream(csID uint32, transactionId float64, args map[string]interface{})
onAudioMessage(format audio.Format, sampleRate audio.SampleRate, sampleSize audio.SampleSize, channels audio.Channel, payload []byte, timestamp uint32)
onVideoMessage(frameType video.FrameType, codec video.Codec, payload []byte, timestamp uint32)
onMetadata(metadata map[string]interface{})
onPlay(streamKey string, startTime float64)
// TODO: separate into two distinct interfaces: client, server (maybe 3 for common functions like onAck, onSetWindowAckSize, onSetChunkSize, etc.)
// Client callbacks
onResult(info map[string]interface{})
onStatus(info map[string]interface{})
onStreamBegin()
}
// Represents a connection made with the RTMP server where messages are exchanged between client/server.
type Session struct {
MediaServer
logger *zap.Logger
sessionID string
clientMetadata clientMetadata
broadcaster *Broadcaster // broadcasts audio/video messages to playback clients subscribed to a stream
active bool // true if the session is active
// Callbacks (for RTMP clients)
OnAudio AudioCallback
OnVideo VideoCallback
OnMetadata MetadataCallback
// Interprets messages, calling the appropriate callback on the session. Also in charge of sending messages.
messageManager *MessageManager
// app data
app string
flashVer string
swfUrl string
tcUrl string
typeOfStream string
streamKey string // used to identify user
publishingType string
isPublisher bool
isPlayer bool
isClient bool
serverAddress string
}
func NewSession(logger *zap.Logger, b *Broadcaster) *Session {
session := &Session{
logger: logger,
sessionID: rand.GenerateUuid(),
broadcaster: b,
active: true,
isClient: false,
}
return session
}
func NewClientSession(app string, tcUrl string, streamKey string, audioCallback AudioCallback, videoCallback VideoCallback, metadataCallback MetadataCallback) *Session {
session := &Session{
sessionID: rand.GenerateUuid(),
isClient: true,
app: app,
tcUrl: tcUrl,
streamKey: streamKey,
OnAudio: audioCallback,
OnVideo: videoCallback,
OnMetadata: metadataCallback,
active: true,
}
return session
}
// Start performs the initial handshake and starts receiving streams of data. This is used for servers only. For clients, use StartPlayback().
func (session *Session) Start() error {
// Perform handshake
err := session.messageManager.Initialize()
if err != nil {
return err
}
defer func() {
// Remove the session from the context
if session.isPlayer {
if config.Debug {
fmt.Println("session: destroying subscriber")
}
session.broadcaster.DestroySubscriber(session.streamKey, session.sessionID)
}
if session.isPublisher {
if config.Debug {
fmt.Println("session: destroying publisher")
}
// Broadcast end of stream
session.broadcaster.BroadcastEndOfStream(session.streamKey)
session.broadcaster.DestroyPublisher(session.streamKey)
}
}()
if config.Debug {
fmt.Println("Handshake completed successfully")
}
// After handshake, start reading chunks
for {
if session.active {
if err = session.messageManager.nextMessage(); err != nil {
return err
}
} else {
return nil
}
}
}
func (session *Session) StartPlayback() error {
err := session.messageManager.InitializeClient()
if err != nil {
return err
}
if config.Debug {
fmt.Println("client handshake completed successfully")
}
info := map[string]interface{}{
"app": session.app,
"flashVer": "LNX 9,0,124,2",
"tcUrl": session.tcUrl,
"fpad": false,
"capabilities": 15,
"audioCodecs": 4071,
"videoCodecs": 252,
"videoFunction": 1,
}
//n, err := session.socketw.Write([]byte("Hello, world!"))
//if err != nil {
// fmt.Println("error writing hello, world!", err)
//}
//err = session.socketw.Flush()
//if err != nil {
// fmt.Println("error flushing hello, world!", err)
//}
//fmt.Println("bytes written:", n)
fmt.Println("now requesting connect")
session.messageManager.sendSetChunkSize(config.DefaultChunkSize)
// After handshake, request connection to an application
err = session.messageManager.requestConnect(info)
if err != nil {
return err
}
// Start reading chunks
for {
if session.active {
if err = session.messageManager.nextMessage(); err != nil {
return err
}
} else {
return nil
}
}
}
func (session *Session) onResult(info map[string]interface{}) {
level, exists := info["level"]
if !exists {
fmt.Println("session: onResult: no 'level' in info object")
return
}
code, exists := info["code"]
if !exists {
fmt.Println("session: onResult: no 'code' in info object")
return
}
if level == "error" {
fmt.Printf("session: onResult error: %+v\n", info)
session.active = false
return
}
if level == "warning" {
fmt.Printf("session: onResult warning: %+v\n", info)
}
switch code {
case "NetConnection.Connect.Success":
session.messageManager.requestCreateStream(2)
}
}
func (session *Session) onStreamBegin() {
session.messageManager.requestPlay(session.streamKey)
}
func (session *Session) onStatus(info map[string]interface{}) {
level, exists := info["level"]
if !exists {
fmt.Println("session: onStatus: no 'level' in info object")
return
}
code, exists := info["code"]
if !exists {
fmt.Println("session: onStatus: no 'code' in info object")
return
}
if level == "error" {
fmt.Printf("session: onStatus error: %s, %+v\n", code, info)
session.active = false
return
}
if level == "warning" {
fmt.Printf("session: onStatus warning: %s, %+v\n", code, info)
}
switch code {
case "NetStream.Play.Start":
fmt.Println("received NetStream.Play.Start")
// TODO: set up transcoders
default:
fmt.Println("session: onStatus: received unknown code:", code)
}
}
func (session *Session) GetID() string {
return session.sessionID
}
func (session *Session) onWindowAckSizeReached(sequenceNumber uint32) {
}
func (session *Session) onConnect(csID uint32, transactionID float64, data map[string]interface{}) {
fmt.Println("onConnect called")
session.storeMetadata(data)
// If the app name to connect is App (whatever the user specifies in the config, ie. "app", "app/publish"),
if session.app == config.App {
// Initiate connect sequence
// As per the specification, after the connect command, the server sends the protocol message Window Acknowledgment Size
session.messageManager.sendWindowAckSize(config.DefaultClientWindowSize)
// After sending the window ack size message, the server sends the set peer bandwidth message
session.messageManager.sendSetPeerBandWidth(config.DefaultClientWindowSize, LimitDynamic)
// Send the User Control Message to begin stream with stream ID = DefaultPublishStream (which is 0)
// Subsequent messages sent by the client will have stream ID = DefaultPublishStream, until another sendBeginStream message is sent
session.messageManager.sendBeginStream(config.DefaultPublishStream)
// Send Set Chunk Size message
session.messageManager.sendSetChunkSize(config.DefaultChunkSize)
// Send Connect Success response
session.messageManager.sendConnectSuccess(csID)
} else {
fmt.Println("session: user trying to connect to app \"" + session.app + "\", but the app doesn't exist. Closing connection.")
session.active = false
}
}
func (session *Session) storeMetadata(metadata map[string]interface{}) {
fmt.Printf("storeMetadata: %+v\n", metadata)
// Playback clients send other properties in the command object, such as what audio/video codecs the client supports
session.app = metadata["app"].(string)
if _, exists := metadata["flashVer"]; exists {
session.flashVer = metadata["flashVer"].(string)
} else if _, exists := metadata["flashver"]; exists {
session.flashVer = metadata["flashver"].(string)
}
if _, exists := metadata["swfUrl"]; exists {
session.flashVer = metadata["swfUrl"].(string)
} else if _, exists := metadata["swfurl"]; exists {
session.flashVer = metadata["swfurl"].(string)
}
if _, exists := metadata["tcUrl"]; exists {
session.flashVer = metadata["tcUrl"].(string)
} else if _, exists := metadata["tcurl"]; exists {
session.flashVer = metadata["tcurl"].(string)
}
if _, exists := metadata["type"]; exists {
session.flashVer = metadata["type"].(string)
}
}
func (session *Session) onSetChunkSize(size uint32) {
session.messageManager.SetChunkSize(size)
}
func (session *Session) onAbortMessage(chunkStreamId uint32) {
}
func (session *Session) onAck(sequenceNumber uint32) {
}
func (session *Session) onSetWindowAckSize(windowAckSize uint32) {
session.messageManager.SetWindowAckSize(windowAckSize)
}
func (session *Session) onSetBandwidth(windowAckSize uint32, limitType uint8) {
session.messageManager.SetBandwidth(windowAckSize, limitType)
}
func (session *Session) onMetadata(metadata map[string]interface{}) {
// This is not for the RTMP server. RTMP servers don't have the option to specify callback. Only RTMP clients use this for now
if session.OnMetadata != nil {
session.OnMetadata(metadata)
return
}
// If this is a client session, no further processing should be done. i.e: no need to broadcast, since we're only receiving data.
if session.isClient {
return
}
// TODO: broadcast metadata to client
session.broadcaster.BroadcastMetadata(session.streamKey, metadata)
//if config.Debug {
// fmt.Printf("clientMetadata %+v", session.clientMetadata)
//}
}
func (session *Session) onReleaseStream(csID uint32, transactionID float64, args map[string]interface{}, streamKey string) {
}
func (session *Session) onFCPublish(csID uint32, transactionID float64, args map[string]interface{}, streamKey string) {
session.messageManager.sendOnFCPublish(csID, transactionID, streamKey)
}
func (session *Session) onCreateStream(csID uint32, transactionID float64, data map[string]interface{}) {
// data object could be nil
session.messageManager.sendCreateStreamResponse(csID, transactionID, data)
session.messageManager.sendBeginStream(uint32(config.DefaultStreamID))
}
func (session *Session) onPublish(transactionId float64, args map[string]interface{}, streamKey string, publishingType string) {
// TODO: Handle things like look up the user's stream key, check if it's valid.
// TODO: For example: twitch returns "Publishing live_user_<username>" in the description.
// TODO: Handle things like recording into a file if publishingType = "record" or "append". Or always record?
session.streamKey = streamKey
session.publishingType = publishingType
session.messageManager.sendStatusMessage("status", "NetStream.Publish.Start", "Publishing live_user_<x>")
session.isPublisher = true
session.broadcaster.RegisterPublisher(streamKey)
}
func (session *Session) onFCUnpublish(args map[string]interface{}, streamKey string) {
}
func (session *Session) onDeleteStream(args map[string]interface{}, streamID float64) {
}
func (session *Session) SendEndOfStream() {
session.messageManager.sendStatusMessage("status", "NetStream.Play.Stop", "Stopped playing stream.")
}
func (session *Session) onCloseStream(csID uint32, transactionId float64, args map[string]interface{}) {
}
// audioData is the full payload (it has the audio headers at the beginning of the payload), for easy forwarding
// If format == audio.AAC, audioData will contain AACPacketType at index 1
func (session *Session) onAudioMessage(format audio.Format, sampleRate audio.SampleRate, sampleSize audio.SampleSize, channels audio.Channel, payload []byte, timestamp uint32) {
// This is not for the RTMP server. RTMP servers don't have the option to specify callback. Only RTMP clients use this for now
if session.OnAudio != nil {
session.OnAudio(format, sampleRate, sampleSize, channels, payload, timestamp)
return
}
// If this is a client session, no further processing should be done. i.e: no need to broadcast, since we're only receiving data.
// TODO: maybe caching the AAC/ACV sequence headers will be necessary
if session.isClient {
return
}
// Cache aac sequence header to send to play back clients when they connect
if format == audio.AAC && audio.AACPacketType(payload[1]) == audio.AACSequenceHeader {
session.broadcaster.SetAacSequenceHeaderForPublisher(session.streamKey, payload)
}
session.broadcaster.BroadcastAudio(session.streamKey, payload, timestamp)
}
// videoData is the full payload (it has the video headers at the beginning of the payload), for easy forwarding
func (session *Session) onVideoMessage(frameType video.FrameType, codec video.Codec, payload []byte, timestamp uint32) {
// This is not for the RTMP server. RTMP servers don't have the option to specify callback. Only RTMP clients use this for now
if session.OnVideo != nil {
session.OnVideo(frameType, codec, payload, timestamp)
return
}
// If this is a client session, no further processing should be done. i.e: no need to broadcast, since we're only receiving data.
// TODO: maybe caching the AAC/ACV sequence headers will be necessary
if session.isClient {
return
}
// cache avc sequence header to send to playback clients when they connect
if codec == video.H264 && video.AVCPacketType(payload[1]) == video.AVCSequenceHeader {
session.broadcaster.SetAvcSequenceHeaderForPublisher(session.streamKey, payload)
}
session.broadcaster.BroadcastVideo(session.streamKey, payload, timestamp)
}
func (session *Session) onPlay(streamKey string, startTime float64) {
session.streamKey = streamKey
if !session.broadcaster.StreamExists(streamKey) {
session.messageManager.sendStatusMessage("error", "NetStream.Play.StreamNotFound", "not_found", streamKey)
return
}
session.messageManager.sendStatusMessage("status", "NetStream.Play.Start", "Playing stream for live_user_<x>")
avcSeqHeader := session.broadcaster.GetAvcSequenceHeaderForPublisher(streamKey)
if avcSeqHeader != nil {
if config.Debug {
fmt.Printf("sending video onPlay, sequence header with timestamp: 0, body size: %d\n", len(avcSeqHeader))
}
session.messageManager.sendVideo(avcSeqHeader, 0)
}
aacSeqHeader := session.broadcaster.GetAacSequenceHeaderForPublisher(streamKey)
if aacSeqHeader != nil {
if config.Debug {
fmt.Printf("sending audio onPlay, sequence header with timestamp: 0, body size: %d\n", len(aacSeqHeader))
}
session.messageManager.sendAudio(aacSeqHeader, 0)
}
session.isPlayer = true
err := session.broadcaster.RegisterSubscriber(streamKey, session)
if err != nil {
// TODO: send failure response to client
}
}
func (session *Session) SendAudio(audio []byte, timestamp uint32) {
session.messageManager.sendAudio(audio, timestamp)
}
func (session *Session) SendVideo(video []byte, timestamp uint32) {
session.messageManager.sendVideo(video, timestamp)
}
func (session *Session) SendMetadata(metadata map[string]interface{}) {
session.messageManager.sendMetadata(metadata)
}