Skip to content

Commit

Permalink
Added options
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin committed Jun 28, 2020
1 parent f49435c commit 1b0a041
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
17 changes: 11 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ const (
fps = 30
)

func startWebserver() {
func main() {
options := stream.CameraOptions{
Width: width,
Height: height,
Fps: fps,
HorizontalFlip: true,
VerticalFlip: true,
Rotation: 0,
}

router := mux.NewRouter()

// Websocket
connectionNumber := make(chan int, 2)
wsh := NewWebSocketHandler(connectionNumber)
router.HandleFunc(videoWebsocketURL, wsh.Handler)
go stream.StreamVideo(width, height, fps, wsh, connectionNumber)
go stream.Video(options, wsh, connectionNumber)

// Static
fs := http.FileServer(http.Dir(staticDir))
router.PathPrefix(staticURL).Handler(handlers.CompressHandler(http.StripPrefix(staticURL, fs)))
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), router))
}

func main() {
startWebserver()
}
57 changes: 44 additions & 13 deletions stream/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package stream

import (
"bytes"
"fmt"
"io"
"log"
"os/exec"
Expand All @@ -21,8 +20,18 @@ type Sender interface {
Send([]byte) error
}

// StreamVideo streams the video for the Raspberry Pi camera to a websocket
func StreamVideo(width int, height int, fps int, sender Sender, connectionsChange chan int) {
// CameraOptions sets the options to send to raspivid
type CameraOptions struct {
Width int
Height int
Fps int
HorizontalFlip bool
VerticalFlip bool
Rotation int
}

// Video streams the video for the Raspberry Pi camera to a websocket
func Video(options CameraOptions, sender Sender, connectionsChange chan int) {
stopChan := make(chan bool)
cameraStarted := false

Expand All @@ -33,19 +42,41 @@ func StreamVideo(width int, height int, fps int, sender Sender, connectionsChang
stopChan <- true
cameraStarted = false
} else if !cameraStarted {
go startCamera(width, height, fps, sender, stopChan)
go startCamera(options, sender, stopChan)
cameraStarted = true
}
}
}

}

func startCamera(width int, height int, fps int, sender Sender, stop chan bool) {
cmd := exec.Command("raspivid", "-ih", "-t", "0", "-o", "-", "-w", strconv.Itoa(width), "-h", strconv.Itoa(height), "-fps", strconv.Itoa(fps), "-n", "-pf", "baseline")
fmt.Println("Started raspicam", cmd.Args)
//defer cmd.Wait()
//defer fmt.Println("Stopped raspicam")
func startCamera(options CameraOptions, sender Sender, stop chan bool) {
args := []string{
"-ih",
"-t", "0",
"-o", "-",
"-w", strconv.Itoa(options.Width),
"-h", strconv.Itoa(options.Height),
"-fps", strconv.Itoa(options.Fps),
"-n",
"-pf", "baseline",
}

if options.HorizontalFlip {
args = append(args, "--hflip")
}
if options.VerticalFlip {
args = append(args, "--vflip")
}
if options.Rotation != 0 {
args = append(args, "--rotation")
args = append(args, strconv.Itoa(options.Rotation))
}

cmd := exec.Command("raspivid", args...)
log.Println("Started raspicam", cmd.Args)
defer cmd.Wait()
defer log.Println("Stopped raspicam")

stdout, err := cmd.StdoutPipe()
if err != nil {
Expand All @@ -63,17 +94,17 @@ func startCamera(width int, height int, fps int, sender Sender, stop chan bool)
for {
select {
case <-stop:
fmt.Println("Stop requested")
log.Println("Stop requested")
return
default:
n, err := stdout.Read(p)
if err != nil {
if err == io.EOF {
//fmt.Println(string(p[:n])) //should handle any remainding bytes.
//return
log.Println("[Raspivid] EOF")
return
}
fmt.Println(err)
//os.Exit(1)
log.Println(err)
}

//fmt.Println("Received", p[:n])
Expand Down

0 comments on commit 1b0a041

Please sign in to comment.