67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"sync"
|
|
"github.com/nareix/joy4/format"
|
|
"github.com/nareix/joy4/av/avutil"
|
|
"github.com/nareix/joy4/av/pubsub"
|
|
"github.com/nareix/joy4/format/rtmp"
|
|
)
|
|
|
|
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(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()
|
|
}
|
|
|
|
server.ListenAndServe()
|
|
|
|
// ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie
|
|
// ffmpeg -f avfoundation -i "0:0" .... -f flv rtmp://localhost/screen
|
|
// ffplay rtmp://localhost/movie
|
|
// ffplay rtmp://localhost/screen
|
|
}
|