-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmjpeg-proxy.go
429 lines (347 loc) · 8.97 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
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
/*
* mjpeg-proxy -- Republish a MJPEG HTTP image stream using a server in Go
*
* Copyright (C) 2015, 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 (
"bufio"
"errors"
"flag"
"fmt"
"io"
"net/http"
"strconv"
"strings"
)
/* Sample source stream starts like this:
HTTP/1.1 200 OK
Content-Type: multipart/x-mixed-replace;boundary=myboundary
Cache-Control: no-cache
Pragma: no-cache
--myboundary
Content-Type: image/jpeg
Content-Length: 36291
JPEG data...
*/
func chunker(body io.ReadCloser, pubChan chan []byte, stopChan chan bool) {
fmt.Println("chunker: starting")
reader := bufio.NewReader(body)
defer func() {
err := body.Close()
if err != nil {
fmt.Println("chunker: body close failed:", err)
}
}()
defer close(pubChan)
defer close(stopChan)
var failure error
ChunkLoop:
for {
head, size, err := readChunkHeader(reader)
if err != nil {
failure = err
break ChunkLoop
}
data, err := readChunkData(reader, size)
if err != nil {
failure = err
break ChunkLoop
}
select {
case <-stopChan:
break ChunkLoop
case pubChan <- append(head, data...):
}
if size == 0 {
failure = errors.New("received final chunk")
break ChunkLoop
}
}
if failure != nil {
fmt.Println("chunker:", failure)
}
fmt.Println("chunker: stopping")
}
func readChunkHeader(reader *bufio.Reader) (head []byte, size int, err error) {
head = make([]byte, 0)
size = -1
err = nil
// read boundary
var line []byte
line, err = reader.ReadSlice('\n')
if err != nil {
return
}
/* don't check for valid boundary in this function; a lot of webcams
(such as those by AXIS) seem to provide improper boundaries. */
head = append(head, line...)
// read header
for {
line, err = reader.ReadSlice('\n')
if err != nil {
return
}
head = append(head, line...)
// empty line marks end of header
lineStr := strings.TrimRight(string(line), "\r\n")
if len(lineStr) == 0 {
break
}
// find data size
parts := strings.SplitN(lineStr, ": ", 2)
if strings.EqualFold(parts[0], "Content-Length") {
var n int
n, err = strconv.Atoi(parts[1])
if err != nil {
return
}
size = n
}
}
if size == -1 {
err = errors.New("Content-Length chunk header not found")
return
}
return
}
func readChunkData(reader *bufio.Reader, size int) ([]byte, error) {
buf := make([]byte, size)
for pos := 0; pos < size; {
n, err := reader.Read(buf[pos:])
if err != nil {
return nil, err
}
pos += n
}
return buf, nil
}
func getBoundary(resp http.Response) (string, error) {
ct := strings.Split(resp.Header.Get("Content-Type"), ";")
fixedCt := ""
fixedPrefix := "multipart/x-mixed-replace;boundary="
if len(ct) < 2 || !strings.HasPrefix(ct[0], "multipart/x-mixed-replace") || !strings.HasPrefix(strings.TrimPrefix(ct[1], " "), "boundary=") {
errStr := fmt.Sprintf("Content-Type is invalid (%s)", strings.Join(ct, ";"))
return "", errors.New(errStr)
}
// Build normalized Content-Type string
builder := strings.Builder{}
builder.WriteString(ct[0])
builder.WriteString(";")
builder.WriteString(strings.TrimPrefix(ct[1], " "))
fixedCt = builder.String()
boundary := "--" + strings.TrimPrefix(fixedCt, fixedPrefix)
return boundary, nil
}
func connectChunker(url, username, password string) (*http.Response, string, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, "", err
}
if username != "" && password != "" {
req.SetBasicAuth(username, password)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, "", err
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
errStr := fmt.Sprintf("Request failed (%s)", resp.Status)
return nil, "", errors.New(errStr)
}
boundary, err := getBoundary(*resp)
if err != nil {
resp.Body.Close()
return nil, "", err
}
return resp, boundary, nil
}
type PubSub struct {
url string
username string
password string
pubChan chan []byte
stopChan chan bool
subChan chan *Subscriber
unsubChan chan *Subscriber
subscribers map[*Subscriber]bool
header http.Header
}
func NewPubSub(url, username, password string) *PubSub {
pubsub := new(PubSub)
pubsub.url = url
pubsub.username = username
pubsub.password = password
pubsub.subChan = make(chan *Subscriber)
pubsub.unsubChan = make(chan *Subscriber)
pubsub.subscribers = make(map[*Subscriber]bool)
return pubsub
}
func (pubsub *PubSub) GetHeader() http.Header {
return pubsub.header
}
func (pubsub *PubSub) Start() {
go pubsub.loop()
}
func (pubsub *PubSub) Subscribe(s *Subscriber) {
pubsub.subChan <- s
}
func (pubsub *PubSub) Unsubscribe(s *Subscriber) {
pubsub.unsubChan <- s
}
func (pubsub *PubSub) loop() {
for {
select {
case data, ok := <-pubsub.pubChan:
if ok {
pubsub.doPublish(data)
} else {
pubsub.stopChan = nil
pubsub.stopPublisher()
pubsub.stopSubscribers()
}
case sub := <-pubsub.subChan:
pubsub.doSubscribe(sub)
case sub := <-pubsub.unsubChan:
pubsub.doUnsubscribe(sub)
}
}
}
func (pubsub *PubSub) doPublish(data []byte) {
subs := pubsub.subscribers
for s := range subs {
select {
case s.ChunkChannel <- data: // try to send
default: // or skip this frame
}
}
}
func (pubsub *PubSub) doSubscribe(s *Subscriber) {
pubsub.subscribers[s] = true
fmt.Printf("pubsub: added subscriber %s (total=%d)\n",
s.RemoteAddr, len(pubsub.subscribers))
if len(pubsub.subscribers) == 1 {
if err := pubsub.startPublisher(); err != nil {
fmt.Println("pubsub: failed to start publisher:", err)
pubsub.stopSubscribers()
}
}
}
func (pubsub *PubSub) stopSubscribers() {
for s := range pubsub.subscribers {
close(s.ChunkChannel)
}
}
func (pubsub *PubSub) doUnsubscribe(s *Subscriber) {
delete(pubsub.subscribers, s)
fmt.Printf("pubsub: removed subscriber %s (total=%d)\n",
s.RemoteAddr, len(pubsub.subscribers))
if len(pubsub.subscribers) == 0 {
pubsub.stopPublisher()
}
}
func (pubsub *PubSub) startPublisher() error {
fmt.Println("pubsub: starting publisher for", pubsub.url)
resp, _, err := connectChunker(pubsub.url, pubsub.username, pubsub.password)
if err != nil {
return err
}
pubsub.header = resp.Header
pubsub.pubChan = make(chan []byte)
pubsub.stopChan = make(chan bool)
go chunker(resp.Body, pubsub.pubChan, pubsub.stopChan)
return nil
}
func (pubsub *PubSub) stopPublisher() {
if pubsub.stopChan != nil {
fmt.Println("pubsub: stopping publisher")
pubsub.stopChan <- true
}
pubsub.stopChan = nil
pubsub.pubChan = nil
}
type Subscriber struct {
RemoteAddr string
ChunkChannel chan []byte
}
func NewSubscriber(client string) *Subscriber {
sub := new(Subscriber)
sub.RemoteAddr = client
sub.ChunkChannel = make(chan []byte)
return sub
}
func makeHandler(pubsub *PubSub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("server: client %s connected\n", r.RemoteAddr)
// prepare response for flushing
flusher, ok := w.(http.Flusher)
if !ok {
fmt.Printf("server: client %s could not be flushed",
r.RemoteAddr)
return
}
// subscribe to new chunks
sub := NewSubscriber(r.RemoteAddr)
pubsub.Subscribe(sub)
defer pubsub.Unsubscribe(sub)
headerSet := false
for {
// wait for next chunk
data, ok := <-sub.ChunkChannel
if !ok {
break
}
// set header before first chunk sent
if !headerSet {
header := w.Header()
for k, v := range pubsub.GetHeader() {
header[k] = v
}
headerSet = true
}
// send chunk to client
_, err := w.Write(data)
flusher.Flush()
// check for client close
if err != nil {
fmt.Printf("server: client %s failed: %s\n",
r.RemoteAddr, err)
break
}
}
}
}
func main() {
// check parameters
source := flag.String("source", "http://example.com/img.mjpg", "source mjpg url")
username := flag.String("username", "", "source mjpg username")
password := flag.String("password", "", "source mjpg password")
bind := flag.String("bind", ":8080", "proxy bind address")
url := flag.String("url", "/", "proxy serve url")
flag.Parse()
// start pubsub client connector
pubsub := NewPubSub(*source, *username, *password)
pubsub.Start()
// start web server
fmt.Printf("server: starting on address %s with url %s\n", *bind, *url)
http.HandleFunc(*url, makeHandler(pubsub))
err := http.ListenAndServe(*bind, nil)
if err != nil {
fmt.Println("server: failed to start:", err)
}
}