Git Product home page Git Product logo

Comments (1)

phsm avatar phsm commented on July 24, 2024

It seems to be possible to use ts.Demuxer with http server.
Here is a modified streaming server example (i didnt test it thoroughly so it is up to you)

package main

import (
    "sync"
    "io"
    "net/http"
    "github.com/nareix/joy4/format"
    "github.com/nareix/joy4/av/avutil"
    "github.com/nareix/joy4/av/pubsub"
    "github.com/nareix/joy4/format/rtmp"
    "github.com/nareix/joy4/format/flv"
    "github.com/nareix/joy4/format/ts"
)

func init() {
    format.RegisterAll()
}

func main() {
    server := &rtmp.Server{}

    l := &sync.RWMutex{}
    type Channel struct {
        que *pubsub.Queue
    }
    channels := map[string]*Channel{}

    server.HandlePlay = func(conn *rtmp.Conn) {
        l.RLock()
        ch := channels[conn.URL.Path]
        l.RUnlock()

        if ch != nil {
            cursor := ch.que.Latest()
            avutil.CopyFile(conn, cursor)
        }
    }

    server.HandlePublish = func(conn *rtmp.Conn) {
        streams, _ := conn.Streams()

        l.Lock()
        ch := channels[conn.URL.Path]
        if ch == nil {
            ch = &Channel{}
            ch.que = pubsub.NewQueue()
            ch.que.WriteHeader(streams)
            channels[conn.URL.Path] = ch
        } else {
            ch = nil
        }
        l.Unlock()
        if ch == nil {
            return
        }

        avutil.CopyPackets(ch.que, conn)

        l.Lock()
        delete(channels, conn.URL.Path)
        l.Unlock()
        ch.que.Close()
    }

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        l.RLock()
        ch := channels[r.URL.Path]
        l.RUnlock()

        if ch != nil {
            w.Header().Set("Content-Type", "application/octet-stream")
            w.Header().Set("Transfer-Encoding", "chunked")      
            w.WriteHeader(200)

            muxer := ts.NewMuxer(w)
            cursor := ch.que.Latest()

            avutil.CopyFile(muxer, cursor)
        } else {
            http.NotFound(w, r)
        }
    })

    go http.ListenAndServe(":8089", nil)

    server.ListenAndServe()

    // ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie
    // ffplay http://localhost:8089/movie <- that should be MPEG-TS over plain HTTP
}

from joy4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.