-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVMXwebcam.go
81 lines (66 loc) · 2.1 KB
/
VMXwebcam.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
// Here is a GO VMX driver which wraps the VMX process with an HTTP handler
// This file is part of VMX
// Author: Tomasz Malisiewicz
// Copyright vision.ai, LLC 2014-2015
package main
import "os/exec"
import "fmt"
import "io/ioutil"
import "log"
import "net/http"
import "os"
import "net"
import "strconv"
import "path/filepath"
import "math/rand"
func main() {
if len(os.Args) != 3 {
fmt.Printf("Usage: ./VMXcamserver 0_or_movie :port\n")
fmt.Printf(" port can be something like :3001, :0, or localhost:3008, but :0 will take the first available port\n")
os.Exit(1)
}
cur_dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
my_channel := make(chan int, 1)
listener, err_listener := net.Listen("tcp4",os.Args[2]);
if err_listener != nil {
log.Fatal(err_listener)
}
addy := listener.Addr()
fmt.Fprintf(os.Stderr,"Welcome to VMXcamserver, to get an image visit the following address:\n")
fmt.Printf("%s\n",addy)
cmd2 := exec.Command(cur_dir+"/.VMXwebcam",os.Args[1])
stdin_pipe,_ := cmd2.StdinPipe()
stdout_pipe,_ := cmd2.StdoutPipe()
err2 := cmd2.Start()
counter := 0
if err2 != nil {
log.Panic(err2)
}
go func() {
eee := cmd2.Wait()
log.Printf("Process returned with error=%v",eee)
os.Exit(1)
}()
b1 := make([]byte, 5000)
_,_ = stdout_pipe.Read(b1)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf(r.RemoteAddr+" "+r.UserAgent()+"\n")
my_channel <- 1
result_file := "/tmp/VMXwebcam."+strconv.Itoa(rand.Intn(50000))+"_"+strconv.Itoa(counter)+".jpg"
_,_ = stdin_pipe.Write([]byte(result_file+"\n"))
_,_ = stdout_pipe.Read(b1)
dat, _ := ioutil.ReadFile(result_file)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Allow","GET")
w.Header().Set("Access-Control-Allow-Headers","Authorization,Content-Type")
w.Header().Set("Access-Control-Allow-Methods","GET")
fmt.Fprintf(w, "%s", dat)
counter = counter + 1
_ = os.Remove(result_file)
<-my_channel
})
log.Fatal(http.Serve(listener, nil))
}