Created
October 31, 2018 23:04
-
-
Save index0h/01b5ff272f4061ce7e6658b1d124337a to your computer and use it in GitHub Desktop.
iframe.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/nareix/joy4/av" | |
"github.com/nareix/joy4/av/avutil" | |
"github.com/nareix/joy4/av/pubsub" | |
"github.com/nareix/joy4/format" | |
"github.com/nareix/joy4/format/flv" | |
"github.com/nareix/joy4/format/rtmp" | |
"io" | |
"net/http" | |
"sync" | |
) | |
var IFrame *av.Packet | |
func CopyPackets(dst av.PacketWriter, src av.PacketReader) (err error) { | |
for { | |
var pkt av.Packet | |
if pkt, err = src.ReadPacket(); err != nil { | |
if err == io.EOF { | |
break | |
} | |
return | |
} | |
if pkt.IsKeyFrame { | |
IFrame = &pkt | |
} | |
if err = dst.WritePacket(pkt); err != nil { | |
return | |
} | |
} | |
return | |
} | |
func init() { | |
format.RegisterAll() | |
} | |
type writeFlusher struct { | |
httpflusher http.Flusher | |
io.Writer | |
} | |
func (self writeFlusher) Flush() error { | |
self.httpflusher.Flush() | |
return nil | |
} | |
func main() { | |
server := &rtmp.Server{} | |
l := &sync.RWMutex{} | |
type Channel struct { | |
que *pubsub.Queue | |
} | |
channels := map[string]*Channel{} | |
server.HandlePlay = func(conn *rtmp.Conn) { | |
print("PLAY\n") | |
l.RLock() | |
ch := channels[conn.URL.Path] | |
l.RUnlock() | |
if ch != nil { | |
cursor := ch.que.Latest() | |
conn.WritePacket(*IFrame) | |
fmt.Printf("%+v\n", *IFrame) | |
avutil.CopyFile(conn, cursor) | |
} | |
} | |
server.HandlePublish = func(conn *rtmp.Conn) { | |
print("PUBLISH\n") | |
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 | |
} | |
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", "video/x-flv") | |
w.Header().Set("Transfer-Encoding", "chunked") | |
w.Header().Set("Access-Control-Allow-Origin", "*") | |
w.WriteHeader(200) | |
flusher := w.(http.Flusher) | |
flusher.Flush() | |
muxer := flv.NewMuxerWriteFlusher(writeFlusher{httpflusher: flusher, Writer: 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 | |
// ffmpeg -f avfoundation -i "0:0" .... -f flv rtmp://localhost/screen | |
// ffplay http://localhost:8089/movie | |
// ffplay http://localhost:8089/screen | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment