i am capture and serve a (usb connected video camera) video streaming using gstreamer at port :3000
~$ gst-launch-0.10 v4l2src ! video/x-raw-yuv,width=320,height=240,framerate=\(fraction\)5/1 ! ffmpegcolorspace ! jpegenc ! multipartmux ! tcpserversink host=192.168.2.1 port=3000
here is my go http server will pull the binary content from port :3000 and stream to browser http client at port :8080
package main
import (
"fmt"
"html"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"time"
)
const (
RECV_BUF_LEN = 1024
layoutee = http.TimeFormat
)
func main() {
g := new(streamHandler)
http.Handle("/stream.webm", g)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
type streamHandler struct {
}
func (v *streamHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println("start streaming!!!")
g1, _ := httputil.DumpRequest(r, true)
fmt.Println(string(g1))
t := time.Now().UTC()
w.Header().Set("Date", t.Format(layoutee))
w.Header().Set("Connection", "Close")
w.Header().Set("Cache-Control", "private")
w.Header().Set("Content-Type", "video/webm")
w.Header().Set("Server", "CustomStreamer/0.0.1")
if err != nil {
fmt.Fprintf(w, "error listening: %q", html.EscapeString(err.Error()))
println("error listening:", err.Error())
w.(http.Flusher).Flush()
return
}
conn, err := listener.Accept()
if err != nil {
fmt.Fprintf(w, "Error accept: %q", html.EscapeString(err.Error()))
println("Error accept:", err.Error())
w.(http.Flusher).Flush()
return
}
c := make(chan []byte)
q := make(chan bool)
go streamer(conn, c, q)
for buf := range c {
writelen := len(buf)
println("got ", writelen, " bytes of data =")
_, err2 := w.Write(buf)
if err2 != nil {
println("Error 2 writing data:", err2.Error())
close(q)
listener.Close()
return
}
w.(http.Flusher).Flush()
}
close(q)
listener.Close()
w.(http.Flusher).Flush()
}
func streamer(conn net.Conn, c chan []byte, q chan bool) {
buf := make([]byte, RECV_BUF_LEN)
for {
fmt.Println("Inside")
n, err := conn.Read(buf)
if err != nil {
println("Error reading:", err.Error())
conn.Close()
close(c)
os.Exit(1)
return
}
//println("received ", n, " bytes of data =", string(buf))
select {
case c <- buf[0:n]:
println("received ", n, " bytes of data =")
case <-q:
println("closed normally")
conn.Close()
close(c)
os.Exit(0)
return
}
}
}