-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
110 lines (91 loc) · 1.91 KB
/
stream.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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
)
func getStreamArgs(input string, start string) []string {
args := []string{
"-hide_banner",
"-v", "error",
}
if start != "" {
args = append(args, "-ss", start)
}
args = append(args,
"-i", input,
)
args = append(args,
"-c:v", "libvpx-vp9",
)
args = append(args, "-deadline", "realtime",
"-cpu-used", "5",
"-row-mt", "1",
"-crf", "30",
"-b:v", "0")
args = append(args,
// this is needed for 5-channel ac3 files
"-ac", "2",
"-f", "webm",
"pipe:",
)
return args
}
func stream(c *config, input string, start string) (*Stream, error) {
ffmpegPath := c.FFmpegPath
args := getStreamArgs(input, start)
cmd := exec.Command(ffmpegPath, args...)
setSysProcAttr(cmd)
if c.LogDebug {
fmt.Printf("Streaming via: %s\n", strings.Join(cmd.Args, " "))
}
stdout, err := cmd.StdoutPipe()
if nil != err {
log.Println("FFMPEG stdout not available: " + err.Error())
return nil, err
}
stderr, err := cmd.StderrPipe()
if nil != err {
log.Println("FFMPEG stderr not available: " + err.Error())
return nil, err
}
if err = cmd.Start(); err != nil {
return nil, err
}
// stderr must be consumed or the process deadlocks
go func() {
stderrData, _ := ioutil.ReadAll(stderr)
stderrString := string(stderrData)
if len(stderrString) > 0 {
log.Printf("[stream] ffmpeg stderr: %s\n", stderrString)
}
}()
ret := &Stream{
Stdout: stdout,
Process: cmd.Process,
}
return ret, nil
}
type Stream struct {
Stdout io.ReadCloser
Process *os.Process
}
func (s *Stream) Serve(w http.ResponseWriter, r *http.Request) {
const mimeType = "video/webm"
w.Header().Set("Content-Type", mimeType)
w.WriteHeader(http.StatusOK)
// handle if client closes the connection
notify := r.Context().Done()
go func() {
<-notify
if s.Process != nil {
s.Process.Kill()
}
}()
io.Copy(w, s.Stdout)
}