-
Notifications
You must be signed in to change notification settings - Fork 15
/
mjpeg-proxy.go
177 lines (154 loc) · 4.44 KB
/
mjpeg-proxy.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
/*
* mjpeg-proxy -- Republish a MJPEG HTTP image stream using a server in Go
*
* Copyright (C) 2015-2020, Valentin Vidic
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net"
"net/http"
"os"
"runtime"
"strings"
"time"
)
var (
clientHeader string
frameTimeout time.Duration
stopDelay time.Duration
tcpSendBuffer int
)
type configSource struct {
Source string
Username string
Password string
Digest bool
Path string
Rate float64
}
func startSource(source, username, password, proxyUrl string, digest bool, rate float64) error {
chunker, err := NewChunker(proxyUrl, source, username, password, digest, rate)
if err != nil {
return fmt.Errorf("chunker[%s]: create failed: %s", proxyUrl, err)
}
pubSub := NewPubSub(proxyUrl, chunker)
pubSub.Start()
fmt.Printf("chunker[%s]: serving from %s\n", proxyUrl, source)
http.Handle(proxyUrl, pubSub)
return nil
}
func loadConfig(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer func() {
err := file.Close()
if err != nil {
fmt.Printf("config: file close failed for %s: %s\n", file.Name(), err)
}
}()
sources := make([]configSource, 0)
dec := json.NewDecoder(file)
err = dec.Decode(&sources)
if err != nil && err != io.EOF {
return err
}
exists := make(map[string]bool)
for _, conf := range sources {
if exists[conf.Path] {
return fmt.Errorf("duplicate proxy path: %s", conf.Path)
}
err = startSource(conf.Source, conf.Username, conf.Password, conf.Path, conf.Digest, conf.Rate)
if err != nil {
return err
}
exists[conf.Path] = true
}
return nil
}
func connStateEvent(conn net.Conn, event http.ConnState) {
if event == http.StateActive && tcpSendBuffer > 0 {
switch c := conn.(type) {
case *net.TCPConn:
c.SetWriteBuffer(tcpSendBuffer)
case *net.UnixConn:
c.SetWriteBuffer(tcpSendBuffer)
}
}
}
func unixListen(path string) (net.Listener, error) {
fi, err := os.Stat(path)
if !os.IsNotExist(err) && fi.Mode()&os.ModeSocket != 0 {
os.Remove(path)
}
return net.Listen("unix", path)
}
func listenAndServe(addr string) error {
var listener net.Listener
var err error
if strings.HasPrefix(addr, "unix:") {
listener, err = unixListen(strings.TrimPrefix(addr, "unix:"))
} else {
listener, err = net.Listen("tcp", addr)
}
if err != nil {
return err
}
fmt.Printf("server: starting on address %s\n", addr)
server := &http.Server{
ConnState: connStateEvent,
}
return server.Serve(listener)
}
func main() {
source := flag.String("source", "http://example.com/img.mjpg", "source uri")
username := flag.String("username", "", "source uri username")
password := flag.String("password", "", "source uri password")
digest := flag.Bool("digest", false, "source uri uses digest authentication")
sources := flag.String("sources", "", "JSON configuration file to load sources from")
bind := flag.String("bind", ":8080", "proxy bind address")
path := flag.String("path", "/", "proxy serving path")
rate := flag.Float64("rate", 0, "limit output frame rate")
maxprocs := flag.Int("maxprocs", 0, "limit number of CPUs used")
flag.DurationVar(&frameTimeout, "frametimeout", 60*time.Second, "limit waiting for next frame")
flag.DurationVar(&stopDelay, "stopduration", 60*time.Second, "follow source after last client")
flag.IntVar(&tcpSendBuffer, "sendbuffer", 4096, "limit buffering of frames")
flag.StringVar(&clientHeader, "clientheader", "", "request header with client address")
flag.Parse()
if *maxprocs > 0 {
runtime.GOMAXPROCS(*maxprocs)
}
var err error
if *sources != "" {
err = loadConfig(*sources)
} else {
err = startSource(*source, *username, *password, *path, *digest, *rate)
}
if err != nil {
fmt.Println("config:", err)
os.Exit(1)
}
err = listenAndServe(*bind)
if err != nil {
fmt.Println("server:", err)
os.Exit(1)
}
}