is there any audio/video ( dynamically generated video content ) streaming http library in golang ?

889 views
Skip to first unread message

Thaniyarasu Kannusamy

unread,
Aug 13, 2014, 2:27:30 AM8/13/14
to golan...@googlegroups.com
i use net/http library to send dynamic content.
where i use http.ServeFile(w,r,filename.webm) to serve static file .
what if i have to do , to serve a dynamically generated webm file , or dynamically generated byte stream . 

 

Ingo Oeser

unread,
Aug 13, 2014, 5:12:44 AM8/13/14
to golan...@googlegroups.com
If you can move freely in the generated stream, just use http.ServeContent.

If you have a byte slice of the content,wrap it in a bytes.Reader and pass it to http.ServeContent.

If you have a real stream, which generated partially and is expensive to implement seeking for, just use do io.Copy to the passed http.ResposeWriter in your handler.

If this doesn't answer your question, please provide a bit sample code describing how you generate the stream, how this stream generation relates to the request. E.g. can it be reused to respond to a different request? How big is it?

Thaniyarasu Kannusamy

unread,
Aug 13, 2014, 10:09:09 PM8/13/14
to golan...@googlegroups.com
thanks to your replies .here is what i am doing.
i am capture and serve a (usb connected video camera) video streaming using gstreamer at port :3000
here is the gstreamer command i issue

~$ 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
/////////code////////////////
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")

listener, err := net.Listen("tcp", "0.0.0.0:3000")
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
}
}
}
///////////////code end///////////

when i start browser at "http://192.168.2.1:8080/stream.webm" i am not able to receive any video output.

i am sure that gstreamer capturing my camera stream and serve streaming at :3000
~$ vlc tcp://192.168.2.1:3000
because vlc is pulling the stream content and can show on vlc player window.   

Thanks
Thani

Thaniyarasu Kannusamy

unread,
Aug 13, 2014, 10:44:00 PM8/13/14
to golan...@googlegroups.com
i tried 

listener, err := net.Listen("tcp", "192.168.2.1:3000")

also
but i got the same error

Tamás Gulácsi

unread,
Aug 14, 2014, 12:01:37 AM8/14/14
to golan...@googlegroups.com
Please, what is that error???

Thaniyarasu Kannusamy

unread,
Aug 14, 2014, 12:28:29 AM8/14/14
to golan...@googlegroups.com
no output .
i mean browser is simply buffering without any output.


On Thursday, 14 August 2014 09:31:37 UTC+5:30, Tamás Gulácsi wrote:
Please, what is that error???

Ingo Oeser

unread,
Aug 14, 2014, 11:41:42 AM8/14/14
to golan...@googlegroups.com
I think you want to use net.DialTCP() instead of net.Listen/net.Accept.

Then you can simply io.Copy(w, connection) the resulting connection (which satisfies the io.Reader interface) from of net.DialTcp. That's all.

Reply all
Reply to author
Forward
0 new messages