nareix / joy4

Golang audio/video library and streaming server
MIT License
2.68k stars 500 forks source link

[Question] is it possible to convert RTMP stream (h264,mp3/aac) to Mpeg1,Mp2 format ? #42

Open c1ngular opened 7 years ago

c1ngular commented 7 years ago

i know mpeg1 and mp2 are outdated formats , but i have some ancient devices to support, any advice would be really appreciated !

phsm commented 6 years ago

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
}