-
Notifications
You must be signed in to change notification settings - Fork 164
/
broadcast.go
606 lines (520 loc) · 18.3 KB
/
broadcast.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
package server
import (
"errors"
"fmt"
"math"
"math/big"
"net/url"
"strings"
"sync"
"github.com/golang/glog"
"github.com/ethereum/go-ethereum/crypto"
"github.com/livepeer/go-livepeer/common"
"github.com/livepeer/go-livepeer/core"
"github.com/livepeer/go-livepeer/drivers"
"github.com/livepeer/go-livepeer/monitor"
"github.com/livepeer/go-livepeer/net"
"github.com/livepeer/go-livepeer/verification"
"github.com/livepeer/lpms/stream"
)
var Policy *verification.Policy
var BroadcastCfg = &BroadcastConfig{}
type BroadcastConfig struct {
maxPrice *big.Rat
mu sync.RWMutex
}
func (cfg *BroadcastConfig) MaxPrice() *big.Rat {
cfg.mu.RLock()
defer cfg.mu.RUnlock()
return cfg.maxPrice
}
func (cfg *BroadcastConfig) SetMaxPrice(price *big.Rat) {
cfg.mu.RLock()
defer cfg.mu.RUnlock()
cfg.maxPrice = price
}
type BroadcastSessionsManager struct {
// Accessing or changing any of the below requires ownership of this mutex
sessLock *sync.Mutex
mid core.ManifestID
sel BroadcastSessionsSelector
sessMap map[string]*BroadcastSession
numOrchs int // how many orchs to request at once
refreshing bool // only allow one refresh in-flight
finished bool // set at stream end
createSessions func() ([]*BroadcastSession, error)
}
func (bsm *BroadcastSessionsManager) selectSession() *BroadcastSession {
bsm.sessLock.Lock()
defer bsm.sessLock.Unlock()
checkSessions := func(m *BroadcastSessionsManager) bool {
numSess := m.sel.Size()
if numSess < int(math.Ceil(float64(m.numOrchs)/2.0)) {
go m.refreshSessions()
}
return numSess > 0
}
for checkSessions(bsm) {
sess := bsm.sel.Select()
// Handle the case where there is an error during selection and Select() returns nil when session list length > 0
if sess == nil {
return nil
}
if _, ok := bsm.sessMap[sess.OrchestratorInfo.Transcoder]; ok {
return sess
}
/*
Don't select sessions no longer in the map.
Retry if the first selected session has been removed from the map.
This may occur if the session is removed while still in the list.
To avoid a runtime search of the session list under lock, simply
fixup the session list at selection time by retrying the selection.
*/
}
return nil
}
func (bsm *BroadcastSessionsManager) removeSession(session *BroadcastSession) {
bsm.sessLock.Lock()
defer bsm.sessLock.Unlock()
delete(bsm.sessMap, session.OrchestratorInfo.Transcoder)
}
func (bsm *BroadcastSessionsManager) completeSession(sess *BroadcastSession) {
bsm.sessLock.Lock()
defer bsm.sessLock.Unlock()
if existingSess, ok := bsm.sessMap[sess.OrchestratorInfo.Transcoder]; ok {
// If the new session and the existing session share the same key in sessMap replace
// the existing session with the new session
if existingSess != sess {
bsm.sessMap[sess.OrchestratorInfo.Transcoder] = sess
}
bsm.sel.Complete(sess)
}
}
func (bsm *BroadcastSessionsManager) refreshSessions() {
glog.V(common.DEBUG).Info("Starting session refresh manifestID=", bsm.mid)
defer glog.V(common.DEBUG).Info("Ending session refresh manifestID=", bsm.mid)
bsm.sessLock.Lock()
if bsm.finished || bsm.refreshing {
bsm.sessLock.Unlock()
return
}
bsm.refreshing = true
bsm.sessLock.Unlock()
newBroadcastSessions, err := bsm.createSessions()
if err != nil {
bsm.sessLock.Lock()
bsm.refreshing = false
bsm.sessLock.Unlock()
return
}
// if newBroadcastSessions is empty, exit without refreshing list
if len(newBroadcastSessions) <= 0 {
bsm.sessLock.Lock()
bsm.refreshing = false
bsm.sessLock.Unlock()
return
}
uniqueSessions := make([]*BroadcastSession, 0, len(newBroadcastSessions))
bsm.sessLock.Lock()
defer bsm.sessLock.Unlock()
bsm.refreshing = false
if bsm.finished {
return
}
for _, sess := range newBroadcastSessions {
if _, ok := bsm.sessMap[sess.OrchestratorInfo.Transcoder]; ok {
continue
}
uniqueSessions = append(uniqueSessions, sess)
bsm.sessMap[sess.OrchestratorInfo.Transcoder] = sess
}
bsm.sel.Add(uniqueSessions)
}
func (bsm *BroadcastSessionsManager) cleanup() {
bsm.sessLock.Lock()
defer bsm.sessLock.Unlock()
bsm.finished = true
bsm.sel.Clear()
bsm.sessMap = make(map[string]*BroadcastSession) // prevent segfaults
}
func NewSessionManager(node *core.LivepeerNode, params *streamParameters, pl core.PlaylistManager, sel BroadcastSessionsSelector) *BroadcastSessionsManager {
var poolSize float64
if node.OrchestratorPool != nil {
poolSize = float64(node.OrchestratorPool.Size())
}
maxInflight := common.HTTPTimeout.Seconds() / SegLen.Seconds()
numOrchs := int(math.Min(poolSize, maxInflight*2))
bsm := &BroadcastSessionsManager{
mid: params.mid,
sel: sel,
sessMap: make(map[string]*BroadcastSession),
createSessions: func() ([]*BroadcastSession, error) { return selectOrchestrator(node, params, pl, numOrchs) },
sessLock: &sync.Mutex{},
numOrchs: numOrchs,
}
bsm.refreshSessions()
return bsm
}
func selectOrchestrator(n *core.LivepeerNode, params *streamParameters, cpl core.PlaylistManager, count int) ([]*BroadcastSession, error) {
if n.OrchestratorPool == nil {
glog.Info("No orchestrators specified; not transcoding")
return nil, errDiscovery
}
tinfos, err := n.OrchestratorPool.GetOrchestrators(count)
if len(tinfos) <= 0 {
glog.Info("No orchestrators found; not transcoding. Error: ", err)
return nil, errNoOrchs
}
if err != nil {
return nil, err
}
var sessions []*BroadcastSession
for _, tinfo := range tinfos {
var sessionID string
var balance Balance
ticketParams := pmTicketParams(tinfo.TicketParams)
if n.Sender != nil {
sessionID = n.Sender.StartSession(*ticketParams)
}
if n.Balances != nil {
balance = core.NewBalance(ticketParams.Recipient, params.mid, n.Balances)
}
var orchOS drivers.OSSession
if len(tinfo.Storage) > 0 {
orchOS = drivers.NewSession(tinfo.Storage[0])
}
bcastOS := cpl.GetOSSession()
if bcastOS.IsExternal() {
// Give each O its own OS session to prevent front running uploads
pfx := fmt.Sprintf("%v/%v", cpl.ManifestID(), core.RandomManifestID())
bcastOS = drivers.NodeStorage.NewSession(pfx)
}
session := &BroadcastSession{
Broadcaster: core.NewBroadcaster(n),
ManifestID: params.mid,
Profiles: params.profiles,
OrchestratorInfo: tinfo,
OrchestratorOS: orchOS,
BroadcasterOS: bcastOS,
Sender: n.Sender,
PMSessionID: sessionID,
Balance: balance,
}
sessions = append(sessions, session)
}
return sessions, nil
}
func processSegment(cxn *rtmpConnection, seg *stream.HLSSegment) ([]string, error) {
rtmpStrm := cxn.stream
nonce := cxn.nonce
cpl := cxn.pl
mid := cxn.mid
vProfile := cxn.profile
glog.V(common.DEBUG).Infof("Processing segment nonce=%d manifestID=%s seqNo=%d dur=%v", nonce, mid, seg.SeqNo, seg.Duration)
if monitor.Enabled {
monitor.SegmentEmerged(nonce, seg.SeqNo, len(BroadcastJobVideoProfiles))
}
seg.Name = "" // hijack seg.Name to convey the uploaded URI
name := fmt.Sprintf("%s/%d.ts", vProfile.Name, seg.SeqNo)
uri, err := cpl.GetOSSession().SaveData(name, seg.Data)
if err != nil {
glog.Errorf("Error saving segment nonce=%d seqNo=%d: %v", nonce, seg.SeqNo, err)
if monitor.Enabled {
monitor.SegmentUploadFailed(nonce, seg.SeqNo, monitor.SegmentUploadErrorUnknown, err.Error(), true)
}
return nil, err
}
if cpl.GetOSSession().IsExternal() {
seg.Name = uri // hijack seg.Name to convey the uploaded URI
}
err = cpl.InsertHLSSegment(vProfile, seg.SeqNo, uri, seg.Duration)
if monitor.Enabled {
monitor.SourceSegmentAppeared(nonce, seg.SeqNo, string(mid), vProfile.Name)
}
if err != nil {
glog.Errorf("Error inserting segment nonce=%d seqNo=%d: %v", nonce, seg.SeqNo, err)
if monitor.Enabled {
monitor.SegmentUploadFailed(nonce, seg.SeqNo, monitor.SegmentUploadErrorUnknown, err.Error(), true)
}
}
var sv *verification.SegmentVerifier
if Policy != nil {
sv = verification.NewSegmentVerifier(Policy)
}
for {
// if fails, retry; rudimentary
if urls, err := transcodeSegment(cxn, seg, name, sv); err == nil {
return urls, nil
}
if shouldStopStream(err) {
glog.Warningf("Stopping current stream due to: %v", err)
rtmpStrm.Close()
return nil, err
}
// recoverable error, retry
}
}
func transcodeSegment(cxn *rtmpConnection, seg *stream.HLSSegment, name string,
verifier *verification.SegmentVerifier) ([]string, error) {
nonce := cxn.nonce
cpl := cxn.pl
sess := cxn.sessManager.selectSession()
// Return early under a few circumstances:
// View-only (non-transcoded) streams or no sessions available
if sess == nil {
if monitor.Enabled {
monitor.SegmentTranscodeFailed(monitor.SegmentTranscodeErrorNoOrchestrators, nonce, seg.SeqNo, errNoOrchs, true)
}
glog.Infof("No sessions available for segment nonce=%d manifestID=%s seqNo=%d", nonce, cxn.mid, seg.SeqNo)
// We may want to introduce a "non-retryable" error type here
// would help error propagation for live ingest.
// similar to the orchestrator's RemoteTranscoderFatalError
return nil, nil
}
glog.Infof("Trying to transcode segment nonce=%d seqNo=%d", nonce, seg.SeqNo)
if monitor.Enabled {
monitor.TranscodeTry(nonce, seg.SeqNo)
}
// storage the orchestrator prefers
if ios := sess.OrchestratorOS; ios != nil {
// XXX handle case when orch expects direct upload
uri, err := ios.SaveData(name, seg.Data)
if err != nil {
glog.Errorf("Error saving segment to OS nonce=%d seqNo=%d: %v", nonce, seg.SeqNo, err)
if monitor.Enabled {
monitor.SegmentUploadFailed(nonce, seg.SeqNo, monitor.SegmentUploadErrorOS, err.Error(), false)
}
cxn.sessManager.removeSession(sess)
return nil, err
}
seg.Name = uri // hijack seg.Name to convey the uploaded URI
}
// send segment to the orchestrator
glog.V(common.DEBUG).Infof("Submitting segment nonce=%d manifestID=%s seqNo=%d orch=%s", nonce, cxn.mid, seg.SeqNo, sess.OrchestratorInfo.Transcoder)
res, err := SubmitSegment(sess, seg, nonce)
if err != nil || res == nil {
cxn.sessManager.removeSession(sess)
if res == nil && err == nil {
return nil, errors.New("Empty response")
}
return nil, err
}
cxn.sessManager.completeSession(updateSession(sess, res))
// download transcoded segments from the transcoder
gotErr := false // only send one error msg per segment list
var errCode monitor.SegmentTranscodeError
errFunc := func(subType monitor.SegmentTranscodeError, url string, err error) {
glog.Errorf("%v error with segment nonce=%d seqNo=%d: %v (URL: %v)", subType, nonce, seg.SeqNo, err, url)
if monitor.Enabled && !gotErr {
monitor.SegmentTranscodeFailed(subType, nonce, seg.SeqNo, err, false)
gotErr = true
errCode = subType
}
}
var dlErr error
segHashes := make([][]byte, len(res.Segments))
n := len(res.Segments)
segURLs := make([]string, len(res.Segments))
segHashLock := &sync.Mutex{}
cond := sync.NewCond(segHashLock)
dlFunc := func(url string, pixels int64, i int) {
defer func() {
cond.L.Lock()
n--
if n == 0 {
cond.Signal()
}
cond.L.Unlock()
}()
if bos := sess.BroadcasterOS; bos != nil && !drivers.IsOwnExternal(url) {
data, err := drivers.GetSegmentData(url)
if err != nil {
errFunc(monitor.SegmentTranscodeErrorDownload, url, err)
segHashLock.Lock()
dlErr = err
segHashLock.Unlock()
cxn.sessManager.removeSession(sess)
return
}
name := fmt.Sprintf("%s/%d.ts", sess.Profiles[i].Name, seg.SeqNo)
newURL, err := bos.SaveData(name, data)
if err != nil {
segHashLock.Lock()
segHashLock.Unlock()
switch err.Error() {
case "Session ended":
errFunc(monitor.SegmentTranscodeErrorSessionEnded, url, err)
default:
errFunc(monitor.SegmentTranscodeErrorSaveData, url, err)
}
return
}
url = newURL
hash := crypto.Keccak256(data)
segHashLock.Lock()
segHashes[i] = hash
segHashLock.Unlock()
}
// Store URLs for the verifier. Be aware that the segment is
// already within object storage at this point, whether local or
// external. If a client were to ignore the playlist and
// preemptively fetch segments, they could be reading tampered
// data. Not an issue if the delivery protocol is being obeyed.
segHashLock.Lock()
segURLs[i] = url
segHashLock.Unlock()
if monitor.Enabled {
monitor.TranscodedSegmentAppeared(nonce, seg.SeqNo, sess.Profiles[i].Name)
}
}
for i, v := range res.Segments {
go dlFunc(v.Url, v.Pixels, i)
}
cond.L.Lock()
for n != 0 {
cond.Wait()
}
cond.L.Unlock()
if dlErr != nil {
return nil, dlErr
}
if verifier != nil {
// verify potentially can change content of segURLs
err := verify(verifier, cxn, sess, seg, res.TranscodeData, segURLs)
if err != nil {
glog.Errorf("Error verifying nonce=%d manifestID=%s seqNo=%d err=%s", nonce, cxn.mid, seg.SeqNo, err)
return nil, err
}
}
for i, url := range segURLs {
err = cpl.InsertHLSSegment(&sess.Profiles[i], seg.SeqNo, url, seg.Duration)
if err != nil {
// InsertHLSSegment only returns ErrSegmentAlreadyExists error
// Right now InsertHLSSegment call is atomic regarding transcoded segments - we either inserting
// all the transcoded segments or none, so we shouldn't hit this error
// But report in case that InsertHLSSegment changed or something wrong is going on in other parts of workflow
glog.Errorf("Playlist insertion error nonce=%d manifestID=%s seqNo=%d err=%s", nonce, cxn.mid, seg.SeqNo, err)
if monitor.Enabled {
monitor.SegmentTranscodeFailed(monitor.SegmentTranscodeErrorPlaylist, nonce, seg.SeqNo, err, false)
}
}
}
if monitor.Enabled {
monitor.SegmentFullyTranscoded(nonce, seg.SeqNo, common.ProfilesNames(sess.Profiles), errCode)
}
glog.V(common.DEBUG).Infof("Successfully validated segment nonce=%d seqNo=%d", nonce, seg.SeqNo)
return segURLs, nil
}
var sessionErrStrings = []string{"dial tcp", "unexpected EOF", core.ErrOrchBusy.Error(), core.ErrOrchCap.Error()}
var sessionErrRegex = common.GenErrRegex(sessionErrStrings)
func shouldStopSession(err error) bool {
return sessionErrRegex.MatchString(err.Error())
}
func verify(verifier *verification.SegmentVerifier, cxn *rtmpConnection,
sess *BroadcastSession, source *stream.HLSSegment,
res *net.TranscodeData, URIs []string) error {
// Cache segment contents if necessary.
// If we need to retry transcoding because verification fails,
// the the segments' OS location will be overwritten.
// Cache the segments so we can restore them in OS if necessary.
renditionData := make([][]byte, len(URIs))
for i, fname := range URIs {
if sess.BroadcasterOS.IsExternal() && drivers.IsOwnExternal(fname) {
// If broadcaster is using external storage and segments are there
// Then have the verifier use that external storage.
continue
}
// Sanity check for absolute URIs to ensure we're actually local.
uri, err := url.ParseRequestURI(fname)
if err != nil {
return err // Implies this is retryable?
}
memOS, ok := sess.BroadcasterOS.(*drivers.MemorySession)
if !uri.IsAbs() && ok {
data := memOS.GetData(fname)
if data == nil {
return errors.New("Missing Local Data")
}
renditionData[i] = data
} else {
return errors.New("Expected local storage but did not have it")
}
}
params := &verification.Params{
ManifestID: sess.ManifestID,
Source: source,
Profiles: sess.Profiles,
Orchestrator: sess.OrchestratorInfo,
Results: res,
URIs: URIs,
Renditions: renditionData,
}
// The return value from the verifier, if any, are the *accepted* params.
// The accepted params are not necessarily the same as `params` sent here.
// The accepted params may be from an earlier iteration if max retries hit.
accepted, err := verifier.Verify(params)
if verification.IsRetryable(err) {
// If retryable, means tampering was detected from this O
// Remove the O from the working set for now
// Error falls through towards end if necessary
cxn.sessManager.removeSession(sess)
}
if accepted != nil {
// The returned set of results has been accepted by the verifier
// Check if an earlier verification attempt was the one accepted.
// If so, reset the local OS if we're using that since it's been
// overwritten with this rendition.
for i, data := range accepted.Renditions {
if accepted != params && !sess.BroadcasterOS.IsExternal() {
// Sanity check that we actually have the rendition data?
if len(data) <= 0 {
return errors.New("MissingLocalData")
}
// SaveData only takes the /<rendition>/<seqNo> part of the URI
// However, it returns /stream/<manifestID>/<rendition>/<seqNo>
// The incoming URI is likely to be in the longer format.
// Hence, trim the /stream/<manifestID> prefix if it exists.
pfx := fmt.Sprintf("/stream/%s/", sess.ManifestID)
uri := strings.TrimPrefix(accepted.URIs[i], pfx)
_, err := sess.BroadcasterOS.SaveData(uri, data)
if err != nil {
return err
}
} else {
// Normally we don't need to reset the URI here, but we do
// if an external OS is used and an earlier attempt is accepted
// (Recall that each O uploads segments to a different location
// if a B-supplied external OS is used)
URIs[i] = accepted.URIs[i]
}
}
// Ignore any errors from the Verify call; don't need to retry anymore
return nil
}
return err // possibly nil
}
// Return an updated copy of the given session using the received transcode result
func updateSession(sess *BroadcastSession, res *ReceivedTranscodeResult) *BroadcastSession {
// Instead of mutating the existing session we copy it and return an updated copy
newSess := &BroadcastSession{}
*newSess = *sess
newSess.LatencyScore = res.LatencyScore
if res.Info == nil {
// Return newSess early if we do not need to update OrchestratorInfo
return newSess
}
oInfo := res.Info
newSess.OrchestratorInfo = oInfo
if len(oInfo.Storage) > 0 {
newSess.OrchestratorOS = drivers.NewSession(oInfo.Storage[0])
}
if newSess.Sender != nil && oInfo.TicketParams != nil {
// Note: We do not validate the ticket params included in the OrchestratorInfo
// message here. Instead, we store the ticket params with the current BroadcastSession
// and the next time this BroadcastSession is used, the ticket params will be validated
// during ticket creation in genPayment(). If ticket params validation during ticket
// creation fails, then this BroadcastSession will be removed
newSess.PMSessionID = newSess.Sender.StartSession(*pmTicketParams(oInfo.TicketParams))
}
return newSess
}