-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
http.go
139 lines (112 loc) · 3.15 KB
/
http.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
package api
import (
"io"
"net/http"
"os/exec"
"github.com/go-chi/chi"
"github.com/rs/zerolog/log"
"github.com/m1k1o/go-transcode/internal/utils"
)
func (a *ApiManagerCtx) Http(r chi.Router) {
r.Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "video/mp2t")
logger := log.With().
Str("path", r.URL.Path).
Str("module", "ffmpeg").
Logger()
// dummy input for testing purposes
file := a.config.AbsPath("profiles", "http-test.sh")
cmd := exec.Command(file)
logger.Info().Msg("command startred")
read, write := io.Pipe()
cmd.Stdout = write
cmd.Stderr = utils.LogWriter(logger)
defer func() {
logger.Info().Msg("command stopped")
read.Close()
write.Close()
}()
go func() {
_ = cmd.Run()
}()
_, _ = io.Copy(w, read)
})
r.Get("/{profile}/{input}", func(w http.ResponseWriter, r *http.Request) {
logger := log.With().
Str("path", r.URL.Path).
Str("module", "ffmpeg").
Logger()
profile := chi.URLParam(r, "profile")
input := chi.URLParam(r, "input")
// check if stream exists
_, ok := a.config.Streams[input]
if !ok {
http.Error(w, "404 stream not found", http.StatusNotFound)
return
}
// check if profile exists
profilePath, err := a.ProfilePath("hls", profile)
if err != nil {
logger.Warn().Err(err).Msg("profile path could not be found")
http.Error(w, "404 profile not found", http.StatusNotFound)
return
}
cmd, err := a.transcodeStart(profilePath, input)
if err != nil {
logger.Warn().Err(err).Msg("transcode could not be started")
http.Error(w, "500 not available", http.StatusInternalServerError)
return
}
logger.Info().Msg("command started")
w.Header().Set("Content-Type", "video/mp2t")
read, write := io.Pipe()
cmd.Stdout = write
cmd.Stderr = utils.LogWriter(logger)
defer func() {
logger.Info().Msg("command stopped")
read.Close()
write.Close()
}()
go func() {
_ = cmd.Run()
}()
_, _ = io.Copy(w, read)
})
// buffered http streaming (alternative to prervious type)
r.Get("/{profile}/{input}/buf", func(w http.ResponseWriter, r *http.Request) {
logger := log.With().
Str("path", r.URL.Path).
Str("module", "ffmpeg").
Logger()
profile := chi.URLParam(r, "profile")
input := chi.URLParam(r, "input")
// check if stream exists
_, ok := a.config.Streams[input]
if !ok {
http.Error(w, "404 stream not found", http.StatusNotFound)
return
}
// check if profile exists
profilePath, err := a.ProfilePath("hls", profile)
if err != nil {
logger.Warn().Err(err).Msg("profile path could not be found")
http.Error(w, "404 profile not found", http.StatusNotFound)
return
}
cmd, err := a.transcodeStart(profilePath, input)
if err != nil {
logger.Warn().Err(err).Msg("transcode could not be started")
http.Error(w, "500 not available", http.StatusInternalServerError)
return
}
logger.Info().Msg("command started")
w.Header().Set("Content-Type", "video/mp2t")
read, write := io.Pipe()
cmd.Stdout = write
cmd.Stderr = utils.LogWriter(logger)
go utils.IOPipeToHTTP(w, read)
_ = cmd.Run()
write.Close()
logger.Info().Msg("command stopped")
})
}