-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
controller.go
277 lines (255 loc) · 7.7 KB
/
controller.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
package core
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/Roverr/rtsp-stream/core/auth"
"github.com/Roverr/rtsp-stream/core/config"
"github.com/Roverr/rtsp-stream/core/streaming"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
)
// ErrUnexpected describes an unexpected error
var ErrUnexpected = errors.New("Unexpected error")
// ErrDirectoryNotCreated is sent when the system cannot create the directory for the URI
var ErrDirectoryNotCreated = errors.New("Could not create directory for URI")
// ErrTimeout describes an error related to timing out
var ErrTimeout = errors.New("Timeout error")
// ErrDTO describes a DTO that has a message as an error
type ErrDTO struct {
Error string `json:"error"`
}
// StreamDto describes an uri where the client can access the stream
type StreamDto struct {
URI string `json:"uri"`
}
// SummariseDto describes each stream and their state of running
type SummariseDto struct {
Running bool `json:"running"`
URI string `json:"uri"`
}
// Controller holds all handler functions for the API
type Controller struct {
spec *config.Specification
streams map[string]*streaming.Stream
fileServer http.Handler
manager IManager
processor streaming.IProcessor
timeout time.Duration
jwt auth.JWT
}
// NewController creates a new instance of Controller
func NewController(spec *config.Specification, fileServer http.Handler) *Controller {
manager := NewManager(time.Second * 20)
provider, err := auth.NewJWTProvider(spec.Auth)
if err != nil {
logrus.Fatal("Could not create new JWT provider: ", err)
}
return &Controller{
spec,
map[string]*streaming.Stream{},
fileServer,
*manager,
streaming.NewProcessor(spec.StoreDir),
time.Second * 15,
provider,
}
}
// SendError sends an error to the client
func (c *Controller) SendError(w http.ResponseWriter, err error, status int) {
w.Header().Add("Content-Type", "application/json")
b, _ := json.Marshal(ErrDTO{Error: err.Error()})
w.WriteHeader(status)
w.Write(b)
}
// isAuthenticated is for checking if the user's request is valid or not
// from a given authentication strategy's perspective
func (c *Controller) isAuthenticated(r *http.Request) bool {
if c.spec.JWTEnabled {
return c.jwt.Validate(r.Header.Get("Authorization"))
}
return true
}
// ListStreamHandler is the HTTP handler of the /list call
func (c *Controller) ListStreamHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if !c.isAuthenticated(r) {
w.WriteHeader(http.StatusForbidden)
return
}
dto := []*SummariseDto{}
for key, stream := range c.streams {
dto = append(dto, &SummariseDto{URI: fmt.Sprintf("/stream/%s/index.m3u8", key), Running: stream.Streak.IsActive()})
}
b, err := json.Marshal(dto)
if err != nil {
c.SendError(w, ErrUnexpected, http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.Write(b)
}
// StartStreamHandler is an HTTP handler for the /start endpoint
func (c *Controller) StartStreamHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if !c.isAuthenticated(r) {
w.WriteHeader(http.StatusForbidden)
return
}
var dto StreamDto
if err := c.marshalValidatedURI(&dto, r.Body); err != nil {
logrus.Error(err)
c.SendError(w, err, http.StatusBadRequest)
return
}
// Calculate directory from URI
dir, err := streaming.GetURIDirectory(dto.URI)
if err != nil {
logrus.Error(err)
c.SendError(w, ErrUnexpected, http.StatusInternalServerError)
return
}
if stream, ok := c.streams[dir]; ok {
c.handleAlreadyKnownStream(w, stream, c.spec, dir)
return
}
streamResolved := c.startStream(dto.URI, dir, c.spec)
defer close(streamResolved)
select {
case <-time.After(c.timeout):
c.SendError(w, ErrTimeout, http.StatusRequestTimeout)
case success := <-streamResolved:
if !success {
c.SendError(w, ErrUnexpected, http.StatusInternalServerError)
return
}
s := c.streams[dir]
b, _ := json.Marshal(StreamDto{URI: s.Path})
w.Header().Add("Content-Type", "application/json")
w.Write(b)
}
}
// ExitHandler is a function that can recognise when the application is being closed
// and cleans up all background running processes
func (c *Controller) ExitHandler() chan bool {
done := make(chan bool)
ch := make(chan os.Signal, 3)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-ch
c.cleanUp()
done <- true
}()
return done
}
// handleAlreadyKnownStream is for dealing with stream starts that are already initiated before
func (c *Controller) handleAlreadyKnownStream(w http.ResponseWriter, strm *streaming.Stream, spec *config.Specification, dir string) {
// If transcoding is not running, spin it back up
if !strm.Streak.IsActive() {
err := c.processor.Restart(strm, dir)
if err != nil {
logrus.Error(err)
c.SendError(w, ErrUnexpected, http.StatusInternalServerError)
return
}
}
// If the stream is already running return its path
b, err := json.Marshal(StreamDto{URI: strm.Path})
if err != nil {
logrus.Error(err)
c.SendError(w, ErrUnexpected, http.StatusInternalServerError)
return
}
checkCh := c.manager.WaitForStream(fmt.Sprintf("%s/index.m3u8", strm.StorePath))
<-checkCh
w.Header().Add("Content-Type", "application/json")
w.Write(b)
}
// cleanUp stops all running processes
func (c *Controller) cleanUp() {
for uri, strm := range c.streams {
logrus.Debugf("Closing processing of %s", uri)
if err := strm.CleanProcess(); err != nil {
logrus.Debugf("Could not close %s", uri)
logrus.Error(err)
return
}
logrus.Debugf("Succesfully closed processing for %s", uri)
}
}
// cleanUnused is for stopping all transcoding for streams that are not watched anymore
func (c *Controller) cleanUnused() {
for name, data := range c.streams {
// If the streak is active, there is no need for stopping
if data.Streak.IsActive() {
logrus.Infof("%s is active, skipping cleaning process", name)
continue
}
logrus.Infof("%s is getting cleaned", name)
if err := data.CleanProcess(); err != nil {
if strings.Contains(err.Error(), "signal: killed") {
logrus.Infof("\n%s is cleaned", name)
continue
}
logrus.Error(err)
}
logrus.Infof("%s is cleaned", name)
}
}
// FileHandler is HTTP handler for direct file requests
func (c *Controller) FileHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
if !c.isAuthenticated(req) {
w.WriteHeader(http.StatusForbidden)
return
}
defer c.fileServer.ServeHTTP(w, req)
filepath := ps.ByName("filepath")
req.URL.Path = filepath
hostKey := determineHost(filepath)
s, ok := c.streams[hostKey]
if !ok {
return
}
if s.Streak.IsActive() {
s.Streak.Hit()
return
}
logrus.Debugf("%s is getting restarted", hostKey)
if err := c.processor.Restart(s, hostKey); err != nil {
logrus.Error(err)
return
}
checkCh := c.manager.WaitForStream(fmt.Sprintf("%s/index.m3u8", s.StorePath))
<-checkCh
s.Streak.Activate().Hit()
}
// startStream creates a new stream then starts processing it with a manager
func (c *Controller) startStream(uri, dir string, spec *config.Specification) chan bool {
logrus.Infof("%s started processing", dir)
stream, physicalPath := c.processor.NewStream(uri)
c.streams[dir] = stream
ch := c.manager.Start(stream.CMD, physicalPath)
return ch
}
// marshalValidateURI is for validiting that the URI is in a valid format
// and marshaling it into the dto pointer
func (c *Controller) marshalValidatedURI(dto *StreamDto, body io.Reader) error {
uri, err := ioutil.ReadAll(body)
if err != nil {
return err
}
if err = json.Unmarshal(uri, dto); err != nil {
return err
}
if _, err := url.Parse(dto.URI); err != nil {
return errors.New("Invalid URI")
}
return nil
}