FAQ
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 .



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ingo Oeser at Aug 13, 2014 at 9:12 am
    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?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Thaniyarasu Kannusamy at Aug 14, 2014 at 2:09 am
    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
    On Wednesday, 13 August 2014 14:42:44 UTC+5:30, Ingo Oeser wrote:

    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?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Thaniyarasu Kannusamy at Aug 14, 2014 at 2:44 am
    i tried

    listener, err := net.Listen("tcp", "192.168.2.1:3000
    <http://www.google.com/url?q=http%3A%2F%2F0.0.0.0%3A3000&sa=D&sntz=1&usg=AFQjCNH3MDp1gVdKx4LdHXZVhjWlIrcGIw>
    ")

    also
    but i got the same error


    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
    <http://www.google.com/url?q=http%3A%2F%2F0.0.0.0%3A3000&sa=D&sntz=1&usg=AFQjCNH3MDp1gVdKx4LdHXZVhjWlIrcGIw>
    ")
    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
    On Wednesday, 13 August 2014 14:42:44 UTC+5:30, Ingo Oeser wrote:

    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?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tamás Gulácsi at Aug 14, 2014 at 4:01 am
    Please, what is that error???

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Thaniyarasu Kannusamy at Aug 14, 2014 at 4:28 am
    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???
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ingo Oeser at Aug 14, 2014 at 3:41 pm
    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.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 13, '14 at 6:27a
activeAug 14, '14 at 3:41p
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase