-
Notifications
You must be signed in to change notification settings - Fork 176
/
mediaserver.go
874 lines (786 loc) · 24.1 KB
/
mediaserver.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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/*
Package server is the place we integrate the Livepeer node with the LPMS media server.
*/
package server
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/livepeer/go-livepeer/drivers"
"github.com/livepeer/go-livepeer/monitor"
"github.com/livepeer/go-livepeer/net"
"github.com/livepeer/go-livepeer/pm"
"github.com/golang/glog"
"github.com/livepeer/go-livepeer/common"
"github.com/livepeer/go-livepeer/core"
lpmscore "github.com/livepeer/lpms/core"
ffmpeg "github.com/livepeer/lpms/ffmpeg"
"github.com/livepeer/lpms/segmenter"
"github.com/livepeer/lpms/stream"
"github.com/livepeer/lpms/vidplayer"
"github.com/livepeer/m3u8"
)
var errAlreadyExists = errors.New("StreamAlreadyExists")
var errBroadcast = errors.New("ErrBroadcast")
var errLowDeposit = errors.New("ErrLowDeposit")
var errStorage = errors.New("ErrStorage")
var errDiscovery = errors.New("ErrDiscovery")
var errNoOrchs = errors.New("ErrNoOrchs")
var errUnknownStream = errors.New("ErrUnknownStream")
var errMismatchedParams = errors.New("Mismatched type for stream params")
const HLSWaitInterval = time.Second
const HLSBufferCap = uint(43200) //12 hrs assuming 1s segment
const HLSBufferWindow = uint(5)
const StreamKeyBytes = 6
const SegLen = 2 * time.Second
const BroadcastRetry = 15 * time.Second
var BroadcastJobVideoProfiles = []ffmpeg.VideoProfile{ffmpeg.P240p30fps4x3, ffmpeg.P360p30fps16x9}
var AuthWebhookURL string
// For HTTP push watchdog
var httpPushTimeout = 1 * time.Minute
var httpPushResetTimer = func() (context.Context, context.CancelFunc) {
sleepDur := time.Duration(int64(float64(httpPushTimeout) * 0.9))
return context.WithTimeout(context.Background(), sleepDur)
}
type streamParameters struct {
mid core.ManifestID
rtmpKey string
profiles []ffmpeg.VideoProfile
resolution string
format ffmpeg.Format
}
func (s *streamParameters) StreamID() string {
return string(s.mid) + "/" + s.rtmpKey
}
type rtmpConnection struct {
mid core.ManifestID
nonce uint64
stream stream.RTMPVideoStream
pl core.PlaylistManager
profile *ffmpeg.VideoProfile
params *streamParameters
sessManager *BroadcastSessionsManager
lastUsed time.Time
}
type LivepeerServer struct {
RTMPSegmenter lpmscore.RTMPSegmenter
LPMS *lpmscore.LPMS
LivepeerNode *core.LivepeerNode
HTTPMux *http.ServeMux
ExposeCurrentManifest bool
// Thread sensitive fields. All accesses to the
// following fields should be protected by `connectionLock`
rtmpConnections map[core.ManifestID]*rtmpConnection
lastHLSStreamID core.StreamID
lastManifestID core.ManifestID
connectionLock *sync.RWMutex
}
type authWebhookResponse struct {
ManifestID string `json:"manifestID"`
StreamKey string `json:"streamKey"`
Presets []string `json:"presets"`
Profiles []struct {
Name string `json:"name"`
Width int `json:"width"`
Height int `json:"height"`
Bitrate int `json:"bitrate"`
FPS uint `json:"fps"`
} `json:"profiles"`
}
func NewLivepeerServer(rtmpAddr string, lpNode *core.LivepeerNode, httpIngest bool) *LivepeerServer {
opts := lpmscore.LPMSOpts{
RtmpAddr: rtmpAddr,
RtmpDisabled: true,
WorkDir: lpNode.WorkDir,
HttpMux: http.NewServeMux(),
}
switch lpNode.NodeType {
case core.BroadcasterNode:
opts.RtmpDisabled = false
}
server := lpmscore.New(&opts)
ls := &LivepeerServer{RTMPSegmenter: server, LPMS: server, LivepeerNode: lpNode, HTTPMux: opts.HttpMux, connectionLock: &sync.RWMutex{},
rtmpConnections: make(map[core.ManifestID]*rtmpConnection),
}
if lpNode.NodeType == core.BroadcasterNode && httpIngest {
opts.HttpMux.HandleFunc("/live/", ls.HandlePush)
}
return ls
}
//StartMediaServer starts the LPMS server
func (s *LivepeerServer) StartMediaServer(ctx context.Context, transcodingOptions string, httpAddr string) error {
if transcodingOptions != "" { // mostly to mitigate race conditions in tests
BroadcastJobVideoProfiles = parsePresets(strings.Split(transcodingOptions, ","))
}
glog.V(common.SHORT).Infof("Transcode Job Type: %v", BroadcastJobVideoProfiles)
//LPMS handlers for handling RTMP video
s.LPMS.HandleRTMPPublish(createRTMPStreamIDHandler(s), gotRTMPStreamHandler(s), endRTMPStreamHandler(s))
s.LPMS.HandleRTMPPlay(getRTMPStreamHandler(s))
//LPMS hanlder for handling HLS video play
s.LPMS.HandleHLSPlay(getHLSMasterPlaylistHandler(s), getHLSMediaPlaylistHandler(s), getHLSSegmentHandler(s))
//Start the LPMS server
lpmsCtx, cancel := context.WithCancel(ctx)
ec := make(chan error, 2)
go func() {
if err := s.LPMS.Start(lpmsCtx); err != nil {
// typically triggered if there's an error with broadcaster LPMS
// transcoder LPMS should return without an error
ec <- s.LPMS.Start(lpmsCtx)
}
}()
if s.LivepeerNode.NodeType == core.BroadcasterNode {
go func() {
glog.V(4).Infof("HTTP Server listening on http://%v", httpAddr)
ec <- http.ListenAndServe(httpAddr, s.HTTPMux)
}()
}
select {
case err := <-ec:
glog.Infof("LPMS Server Error: %v. Quitting...", err)
cancel()
return err
case <-ctx.Done():
cancel()
return ctx.Err()
}
}
//RTMP Publish Handlers
func createRTMPStreamIDHandler(s *LivepeerServer) func(url *url.URL) (strmID stream.AppData) {
return func(url *url.URL) (strmID stream.AppData) {
//Check webhook for ManifestID
//If ManifestID is returned from webhook, use it
//Else check URL for ManifestID
//If ManifestID is passed in URL, use that one
//Else create one
var resp *authWebhookResponse
var mid core.ManifestID
var err error
var key string
profiles := []ffmpeg.VideoProfile{}
if resp, err = authenticateStream(url.String()); err != nil {
glog.Error("Authentication denied for ", err)
return nil
}
if resp != nil {
mid, key = parseManifestID(resp.ManifestID), resp.StreamKey
// Process transcoding options presets
if len(resp.Presets) > 0 {
profiles = parsePresets(resp.Presets)
}
for _, profile := range resp.Profiles {
name := profile.Name
if name == "" {
name = "webhook_" + common.DefaultProfileName(
profile.Width,
profile.Height,
profile.Bitrate)
}
prof := ffmpeg.VideoProfile{
Name: name,
Bitrate: fmt.Sprint(profile.Bitrate),
Framerate: profile.FPS,
Resolution: fmt.Sprintf("%dx%d", profile.Width, profile.Height),
}
profiles = append(profiles, prof)
}
// Only set defaults if user did not specify a preset/profile
if len(resp.Profiles) <= 0 && len(resp.Presets) <= 0 {
profiles = BroadcastJobVideoProfiles
}
} else {
profiles = BroadcastJobVideoProfiles
}
if mid == "" {
sid := parseStreamID(url.Path)
mid, key = sid.ManifestID, sid.Rendition
}
if mid == "" {
mid = core.RandomManifestID()
}
// Ensure there's no concurrent StreamID with the same name
s.connectionLock.RLock()
defer s.connectionLock.RUnlock()
if core.MaxSessions > 0 && len(s.rtmpConnections) >= core.MaxSessions {
glog.Error("Too many connections")
return nil
}
if _, exists := s.rtmpConnections[mid]; exists {
glog.Error("Manifest already exists ", mid)
return nil
}
// Generate RTMP part of StreamID
if key == "" {
key = common.RandomIDGenerator(StreamKeyBytes)
}
return &streamParameters{
mid: mid,
rtmpKey: key,
// HTTP push mutates `profiles` so make a copy of it
profiles: append([]ffmpeg.VideoProfile(nil), profiles...),
}
}
}
func authenticateStream(url string) (*authWebhookResponse, error) {
if AuthWebhookURL == "" {
return nil, nil
}
started := time.Now()
values := map[string]string{"url": url}
jsonValue, err := json.Marshal(values)
if err != nil {
return nil, err
}
resp, err := http.Post(AuthWebhookURL, "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
rbody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}
if len(rbody) == 0 {
return nil, nil
}
var authResp authWebhookResponse
err = json.Unmarshal(rbody, &authResp)
if err != nil {
return nil, err
}
if authResp.ManifestID == "" {
return nil, errors.New("Empty manifest id not allowed")
}
took := time.Since(started)
glog.Infof("Stream authentication for url=%s dur=%s", url, took)
if monitor.Enabled {
monitor.AuthWebhookFinished(took)
}
return &authResp, nil
}
func streamParams(rtmpStrm stream.RTMPVideoStream) *streamParameters {
d := rtmpStrm.AppData()
p, ok := d.(*streamParameters)
if !ok {
glog.Error("Mismatched type for RTMP app data")
return nil
}
return p
}
func gotRTMPStreamHandler(s *LivepeerServer) func(url *url.URL, rtmpStrm stream.RTMPVideoStream) (err error) {
return func(url *url.URL, rtmpStrm stream.RTMPVideoStream) (err error) {
cxn, err := s.registerConnection(rtmpStrm)
if err != nil {
return err
}
mid := cxn.mid
nonce := cxn.nonce
startSeq := 0
streamStarted := false
//Segment the stream, insert the segments into the broadcaster
go func(rtmpStrm stream.RTMPVideoStream) {
hid := string(core.RandomManifestID()) // ffmpeg m3u8 output name
hlsStrm := stream.NewBasicHLSVideoStream(hid, stream.DefaultHLSStreamWin)
hlsStrm.SetSubscriber(func(seg *stream.HLSSegment, eof bool) {
if eof {
// XXX update HLS manifest
return
}
if streamStarted == false {
streamStarted = true
if monitor.Enabled {
monitor.StreamStarted(nonce)
}
}
go processSegment(cxn, seg)
})
segOptions := segmenter.SegmenterOptions{
StartSeq: startSeq,
SegLength: SegLen,
}
err := s.RTMPSegmenter.SegmentRTMPToHLS(context.Background(), rtmpStrm, hlsStrm, segOptions)
if err != nil {
// Stop the incoming RTMP connection.
// TODO retry segmentation if err != SegmenterTimeout; may be recoverable
rtmpStrm.Close()
}
}(rtmpStrm)
if monitor.Enabled {
monitor.StreamCreated(string(mid), nonce)
}
glog.Infof("\n\nVideo Created With ManifestID: %v\n\n", mid)
return nil
}
}
func endRTMPStreamHandler(s *LivepeerServer) func(url *url.URL, rtmpStrm stream.RTMPVideoStream) error {
return func(url *url.URL, rtmpStrm stream.RTMPVideoStream) error {
params := streamParams(rtmpStrm)
if params == nil {
return errMismatchedParams
}
//Remove RTMP stream
err := removeRTMPStream(s, params.mid)
if err != nil {
return err
}
return nil
}
}
func (s *LivepeerServer) registerConnection(rtmpStrm stream.RTMPVideoStream) (*rtmpConnection, error) {
nonce := rand.Uint64()
// Set up the connection tracking
params := streamParams(rtmpStrm)
if params == nil {
return nil, errMismatchedParams
}
mid := params.mid
if drivers.NodeStorage == nil {
glog.Error("Missing node storage")
return nil, errStorage
}
storage := drivers.NodeStorage.NewSession(string(mid))
// Build the source video profile from the RTMP stream.
if params.resolution == "" {
params.resolution = fmt.Sprintf("%vx%v", rtmpStrm.Width(), rtmpStrm.Height())
}
vProfile := ffmpeg.VideoProfile{
Name: "source",
Resolution: params.resolution,
Bitrate: "4000k", // Fix this
Format: params.format,
}
hlsStrmID := core.MakeStreamID(mid, &vProfile)
s.connectionLock.RLock()
// Fast path - check early if session exists - creating new session can take time
_, exists := s.rtmpConnections[mid]
s.connectionLock.RUnlock()
if exists {
// We can only have one concurrent stream per ManifestID
return nil, errAlreadyExists
}
playlist := core.NewBasicPlaylistManager(mid, storage)
var stakeRdr stakeReader
if s.LivepeerNode.Eth != nil {
stakeRdr = &storeStakeReader{store: s.LivepeerNode.Database}
}
cxn := &rtmpConnection{
mid: mid,
nonce: nonce,
stream: rtmpStrm,
pl: playlist,
profile: &vProfile,
params: params,
sessManager: NewSessionManager(s.LivepeerNode, params, playlist, NewMinLSSelector(stakeRdr, 1.0)),
lastUsed: time.Now(),
}
s.connectionLock.Lock()
_, exists = s.rtmpConnections[mid]
// Check if session exist again - potentially two sessions can be created simultaneously,
// so we don't want to overwrite one that was already created
if exists {
// We can only have one concurrent stream per ManifestID
s.connectionLock.Unlock()
return nil, errAlreadyExists
}
s.rtmpConnections[mid] = cxn
s.lastManifestID = mid
s.lastHLSStreamID = hlsStrmID
sessionsNumber := len(s.rtmpConnections)
s.connectionLock.Unlock()
if monitor.Enabled {
monitor.CurrentSessions(sessionsNumber)
}
return cxn, nil
}
func removeRTMPStream(s *LivepeerServer, mid core.ManifestID) error {
s.connectionLock.Lock()
defer s.connectionLock.Unlock()
cxn, ok := s.rtmpConnections[mid]
if !ok || cxn.pl == nil {
glog.Error("Attempted to end unknown stream with manifest ID ", mid)
return errUnknownStream
}
cxn.stream.Close()
cxn.sessManager.cleanup()
cxn.pl.Cleanup()
glog.Infof("Ended stream with id=%s", mid)
delete(s.rtmpConnections, mid)
if monitor.Enabled {
monitor.StreamEnded(cxn.nonce)
monitor.CurrentSessions(len(s.rtmpConnections))
}
return nil
}
//End RTMP Publish Handlers
//HLS Play Handlers
func getHLSMasterPlaylistHandler(s *LivepeerServer) func(url *url.URL) (*m3u8.MasterPlaylist, error) {
return func(url *url.URL) (*m3u8.MasterPlaylist, error) {
var manifestID core.ManifestID
if s.ExposeCurrentManifest && "/stream/current.m3u8" == strings.ToLower(url.Path) {
manifestID = s.LastManifestID()
} else {
sid := parseStreamID(url.Path)
if sid.Rendition != "" {
// requesting a media PL, not master PL
return nil, vidplayer.ErrNotFound
}
manifestID = sid.ManifestID
}
s.connectionLock.RLock()
defer s.connectionLock.RUnlock()
cxn, ok := s.rtmpConnections[manifestID]
if !ok || cxn.pl == nil {
return nil, vidplayer.ErrNotFound
}
cpl := cxn.pl
if cpl.ManifestID() != manifestID {
return nil, vidplayer.ErrNotFound
}
return cpl.GetHLSMasterPlaylist(), nil
}
}
func getHLSMediaPlaylistHandler(s *LivepeerServer) func(url *url.URL) (*m3u8.MediaPlaylist, error) {
return func(url *url.URL) (*m3u8.MediaPlaylist, error) {
strmID := parseStreamID(url.Path)
mid := strmID.ManifestID
s.connectionLock.RLock()
defer s.connectionLock.RUnlock()
cxn, ok := s.rtmpConnections[mid]
if !ok || cxn.pl == nil {
return nil, vidplayer.ErrNotFound
}
//Get the hls playlist
pl := cxn.pl.GetHLSMediaPlaylist(strmID.Rendition)
if pl == nil {
return nil, vidplayer.ErrNotFound
}
return pl, nil
}
}
func getHLSSegmentHandler(s *LivepeerServer) func(url *url.URL) ([]byte, error) {
return func(url *url.URL) ([]byte, error) {
// Strip the /stream/ prefix
segName := cleanStreamPrefix(url.Path)
if segName == "" || drivers.NodeStorage == nil {
glog.Error("SegName not found or storage nil")
return nil, vidplayer.ErrNotFound
}
parts := strings.SplitN(segName, "/", 2)
if len(parts) <= 0 {
glog.Error("Unexpected path structure")
return nil, vidplayer.ErrNotFound
}
memoryOS, ok := drivers.NodeStorage.(*drivers.MemoryOS)
if !ok {
return nil, vidplayer.ErrNotFound
}
// We index the session by the first entry of the path, eg
// <session>/<more-path>/<data>
os := memoryOS.GetSession(parts[0])
if os == nil {
return nil, vidplayer.ErrNotFound
}
data := os.GetData(segName)
if len(data) > 0 {
return data, nil
}
return nil, vidplayer.ErrNotFound
}
}
//End HLS Play Handlers
//Start RTMP Play Handlers
func getRTMPStreamHandler(s *LivepeerServer) func(url *url.URL) (stream.RTMPVideoStream, error) {
return func(url *url.URL) (stream.RTMPVideoStream, error) {
mid := parseManifestID(url.Path)
s.connectionLock.RLock()
cxn, ok := s.rtmpConnections[mid]
defer s.connectionLock.RUnlock()
if !ok {
glog.Error("Cannot find RTMP stream for ManifestID ", mid)
return nil, vidplayer.ErrNotFound
}
//Could use a subscriber, but not going to here because the RTMP stream doesn't need to be available for consumption by multiple views. It's only for the segmenter.
return cxn.stream, nil
}
}
//End RTMP Handlers
// HandlePush processes request for HTTP ingest
func (s *LivepeerServer) HandlePush(w http.ResponseWriter, r *http.Request) {
// we read this unconditionally, mostly for ffmpeg
body, err := ioutil.ReadAll(r.Body)
if err != nil {
httpErr := fmt.Sprintf(`Error reading http request body: %s`, err.Error())
glog.Error(httpErr)
http.Error(w, httpErr, http.StatusInternalServerError)
return
}
r.Body.Close()
r.URL = &url.URL{Scheme: "http", Host: r.Host, Path: r.URL.Path}
// Determine the input format the request is claiming to have
ext := path.Ext(r.URL.Path)
format := common.ProfileExtensionFormat(ext)
if ffmpeg.FormatNone == format {
// ffmpeg sends us a m3u8 as well, so ignore
// Alternatively, reject m3u8s explicitly and take any other type
// TODO also look at use content-type
http.Error(w, fmt.Sprintf(`ignoring file extension: %s`, ext), http.StatusBadRequest)
return
}
glog.Infof("Got push request at url=%s ua=%s addr=%s len=%d", r.URL.String(), r.UserAgent(), r.RemoteAddr, len(body))
now := time.Now()
mid := parseManifestID(r.URL.Path)
if mid == "" {
http.Error(w, `Bad URL`, http.StatusBadRequest)
return
}
s.connectionLock.Lock()
cxn, exists := s.rtmpConnections[mid]
if exists && cxn != nil {
cxn.lastUsed = now
}
s.connectionLock.Unlock()
// Check for presence and register if a fresh cxn
if !exists {
appData := (createRTMPStreamIDHandler(s))(r.URL)
if appData == nil {
http.Error(w, "Could not create stream ID: ", http.StatusInternalServerError)
return
}
st := stream.NewBasicRTMPVideoStream(appData)
params := streamParams(st)
params.resolution = r.Header.Get("Content-Resolution")
params.format = format
// Set output formats if not explicitly specified
for i, v := range params.profiles {
if ffmpeg.FormatNone == v.Format {
params.profiles[i].Format = format
}
}
cxn, err = s.registerConnection(st)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Start a watchdog to remove session after a period of inactivity
ticker := time.NewTicker(httpPushTimeout)
go func(s *LivepeerServer, mid core.ManifestID) {
defer ticker.Stop()
for range ticker.C {
var lastUsed time.Time
s.connectionLock.RLock()
if cxn, exists := s.rtmpConnections[mid]; exists {
lastUsed = cxn.lastUsed
}
s.connectionLock.RUnlock()
if time.Since(lastUsed) > httpPushTimeout {
_ = removeRTMPStream(s, mid)
return
}
}
}(s, mid)
}
fname := path.Base(r.URL.Path)
seq, err := strconv.ParseUint(strings.TrimSuffix(fname, ext), 10, 64)
if err != nil {
seq = 0
}
duration, err := strconv.Atoi(r.Header.Get("Content-Duration"))
if err != nil {
duration = 2000
glog.Info("Missing duration; filling in a default of 2000ms")
}
seg := &stream.HLSSegment{
Data: body,
Name: fname,
SeqNo: seq,
Duration: float64(duration) / 1000.0,
}
// Kick watchdog periodically so session doesn't time out during long transcodes
requestEnded := make(chan struct{}, 1)
defer func() { requestEnded <- struct{}{} }()
go func() {
for {
tick, cancel := httpPushResetTimer()
select {
case <-requestEnded:
cancel()
return
case <-tick.Done():
glog.V(common.VERBOSE).Infof("watchdog reset mid=%s seq=%d dur=%v started=%v", mid, seq, duration, now)
s.connectionLock.Lock()
if cxn, exists := s.rtmpConnections[mid]; exists {
cxn.lastUsed = time.Now()
}
s.connectionLock.Unlock()
}
}
}()
// Do the transcoding!
urls, err := processSegment(cxn, seg)
if err != nil {
// TODO distinguish between user errors (400) and server errors (500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(urls) == 0 {
http.Error(w, "No sessions available", http.StatusServiceUnavailable)
return
}
renditionData := make([][]byte, len(urls))
// find data in local storage
memOS, ok := cxn.pl.GetOSSession().(*drivers.MemorySession)
if ok {
for i, fname := range urls {
data := memOS.GetData(fname)
if data != nil {
renditionData[i] = data
}
}
}
boundary := common.RandName()
accept := r.Header.Get("Accept")
if accept == "multipart/mixed" {
contentType := "multipart/mixed; boundary=" + boundary
w.Header().Set("Content-Type", contentType)
}
w.WriteHeader(http.StatusOK)
w.(http.Flusher).Flush()
if accept != "multipart/mixed" {
return
}
mw := multipart.NewWriter(w)
for i, url := range urls {
mw.SetBoundary(boundary)
var typ, ext string
length := len(renditionData[i])
if length == 0 {
typ, ext, length = "application/vnd+livepeer.uri", ".txt", len(url)
} else {
format := cxn.params.profiles[i].Format
ext, err = common.ProfileFormatExtension(format)
if err != nil {
glog.Error("Unknown extension for format: ", err)
break
}
typ, err = common.ProfileFormatMimeType(format)
if err != nil {
glog.Error("Unknown mime type for format: ", err)
}
}
profile := cxn.params.profiles[i].Name
fname := fmt.Sprintf(`"%s_%d%s"`, profile, seq, ext)
hdrs := textproto.MIMEHeader{
"Content-Type": {typ + "; name=" + fname},
"Content-Length": {strconv.Itoa(length)},
"Content-Disposition": {"attachment; filename=" + fname},
"Rendition-Name": {profile},
}
fw, err := mw.CreatePart(hdrs)
if err != nil {
glog.Error("Could not create multipart part ", err)
break
}
if len(renditionData[i]) > 0 {
io.Copy(fw, bytes.NewBuffer(renditionData[i]))
} else {
fw.Write([]byte(url))
}
}
mw.Close()
}
//Helper Methods Begin
// StreamPrefix match all leading spaces, slashes and optionally `stream/`
var StreamPrefix = regexp.MustCompile(`^[ /]*(stream/)?|(live/)?`) // test carefully!
func cleanStreamPrefix(reqPath string) string {
return StreamPrefix.ReplaceAllString(reqPath, "")
}
func parseStreamID(reqPath string) core.StreamID {
// remove extension and create streamid
p := strings.TrimSuffix(reqPath, path.Ext(reqPath))
return core.SplitStreamIDString(cleanStreamPrefix(p))
}
func parseManifestID(reqPath string) core.ManifestID {
return parseStreamID(reqPath).ManifestID
}
func parsePresets(presets []string) []ffmpeg.VideoProfile {
profs := make([]ffmpeg.VideoProfile, 0)
for _, v := range presets {
if p, ok := ffmpeg.VideoProfileLookup[strings.TrimSpace(v)]; ok {
profs = append(profs, p)
}
}
return profs
}
func (s *LivepeerServer) LastManifestID() core.ManifestID {
s.connectionLock.RLock()
defer s.connectionLock.RUnlock()
return s.lastManifestID
}
func (s *LivepeerServer) LastHLSStreamID() core.StreamID {
s.connectionLock.RLock()
defer s.connectionLock.RUnlock()
return s.lastHLSStreamID
}
func (s *LivepeerServer) GetNodeStatus() *net.NodeStatus {
// not threadsafe; need to deep copy the playlist
m := make(map[string]*m3u8.MasterPlaylist, 0)
s.connectionLock.RLock()
defer s.connectionLock.RUnlock()
for _, cxn := range s.rtmpConnections {
if cxn.pl == nil {
continue
}
cpl := cxn.pl
m[string(cpl.ManifestID())] = cpl.GetHLSMasterPlaylist()
}
res := &net.NodeStatus{
Manifests: m,
Version: core.LivepeerVersion,
GolangRuntimeVersion: runtime.Version(),
GOArch: runtime.GOARCH,
GOOS: runtime.GOOS,
OrchestratorPool: []string{},
RegisteredTranscoders: []net.RemoteTranscoderInfo{},
LocalTranscoding: s.LivepeerNode.TranscoderManager == nil,
}
if s.LivepeerNode.TranscoderManager != nil {
res.RegisteredTranscodersNumber = s.LivepeerNode.TranscoderManager.RegisteredTranscodersCount()
res.RegisteredTranscoders = s.LivepeerNode.TranscoderManager.RegisteredTranscodersInfo()
}
if s.LivepeerNode.OrchestratorPool != nil {
urls := s.LivepeerNode.OrchestratorPool.GetURLs()
for _, url := range urls {
res.OrchestratorPool = append(res.OrchestratorPool, url.String())
}
}
return res
}
// Debug helpers
func (s *LivepeerServer) LatestPlaylist() core.PlaylistManager {
s.connectionLock.RLock()
defer s.connectionLock.RUnlock()
cxn, ok := s.rtmpConnections[s.lastManifestID]
if !ok || cxn.pl == nil {
return nil
}
return cxn.pl
}
func shouldStopStream(err error) bool {
_, ok := err.(pm.ErrSenderValidation)
return ok
}