From d16d14210e46756b82fe0e0014ea98eeb4e4893d Mon Sep 17 00:00:00 2001 From: nareix Date: Sun, 17 Jul 2016 08:32:03 +0800 Subject: [PATCH] examples: move gop cache into channels --- examples/rtmp_server_channels.go | 37 ++++++++++++++++++++- examples/rtmp_server_gop_cache.go | 54 ------------------------------- 2 files changed, 36 insertions(+), 55 deletions(-) delete mode 100644 examples/rtmp_server_gop_cache.go diff --git a/examples/rtmp_server_channels.go b/examples/rtmp_server_channels.go index dc3be4d..12b165c 100644 --- a/examples/rtmp_server_channels.go +++ b/examples/rtmp_server_channels.go @@ -2,9 +2,12 @@ package main import ( "sync" + "fmt" + "time" "github.com/nareix/joy4/format" "github.com/nareix/joy4/av/avutil" "github.com/nareix/joy4/av/pubsub" + "github.com/nareix/joy4/av/pktque" "github.com/nareix/joy4/format/rtmp" ) @@ -28,7 +31,25 @@ func main() { if ch != nil { cursor := ch.que.Latest() - avutil.CopyFile(conn, cursor) + query := conn.URL.Query() + if q := query.Get("delaygop"); q != "" { + n := 0 + fmt.Sscanf(q, "%d", &n) + cursor = ch.que.DelayedGopCount(n) + } else if q := query.Get("delaytime"); q != "" { + dur, _ := time.ParseDuration(q) + cursor = ch.que.DelayedTime(dur) + } + filters := pktque.Filters{} + if q := query.Get("waitkey"); q != "" { + filters = append(filters, &pktque.WaitKeyFrame{}) + } + filters = append(filters, &pktque.FixTime{StartFromZero: true}) + demuxer := &pktque.FilterDemuxer{ + Filter: filters, + Demuxer: cursor, + } + avutil.CopyFile(conn, demuxer) } } @@ -40,6 +61,11 @@ func main() { if ch == nil { ch = &Channel{} ch.que = pubsub.NewQueue(streams) + query := conn.URL.Query() + if q := query.Get("cachetime"); q != "" { + dur, _ := time.ParseDuration(q) + ch.que.SetMaxDuration(dur) + } channels[conn.URL.Path] = ch } else { ch = nil @@ -61,6 +87,15 @@ func main() { // ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie // ffmpeg -f avfoundation -i "0:0" .... -f flv rtmp://localhost/screen + + // with cache size options + // ffplay rtmp://localhost/movie // ffplay rtmp://localhost/screen + // ffplay rtmp://localhost/movie?delaytime=5s + // ffplay rtmp://localhost/movie?delaytime=10s&waitkey=true + // ffplay rtmp://localhost/movie?delaytime=20s + + // ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie?cachetime=30s + // ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie?cachetime=1m } diff --git a/examples/rtmp_server_gop_cache.go b/examples/rtmp_server_gop_cache.go deleted file mode 100644 index a0143d5..0000000 --- a/examples/rtmp_server_gop_cache.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "fmt" - "time" - "github.com/nareix/joy4/format" - "github.com/nareix/joy4/av/avutil" - "github.com/nareix/joy4/av/pktque" - "github.com/nareix/joy4/av/pubsub" - "github.com/nareix/joy4/format/rtmp" -) - -func init() { - format.RegisterAll() -} - -func main() { - server := &rtmp.Server{} - - var que *pubsub.Queue - - go func() { - file, _ := avutil.Open("projectindex.flv") - streams, _ := file.Streams() - que = pubsub.NewQueue(streams) - demuxer := &pktque.FilterDemuxer{Demuxer: file, Filter: &pktque.Walltime{}} - avutil.CopyPackets(que, demuxer) - file.Close() - que.Close() - }() - - server.HandlePlay = func(conn *rtmp.Conn) { - cursor := que.Latest() - query := conn.URL.Query() - if q := query.Get("delaygop"); q != "" { - n := 0 - fmt.Sscanf(q, "%d", &n) - cursor = que.DelayedGopCount(n) - } else if q := query.Get("delaytime"); q != "" { - dur, _ := time.ParseDuration(q) - cursor = que.DelayedTime(dur) - } - demuxer := &pktque.FilterDemuxer{Demuxer: cursor, Filter: &pktque.WaitKeyFrame{}} - avutil.CopyFile(conn, demuxer) - } - - server.ListenAndServe() - - // ffplay rtmp://localhost/test.flv - // ffplay rtmp://localhost/test.flv?delaygop=2 - // ffplay rtmp://localhost/test.flv?delaytime=3s - // ffplay rtmp://localhost/test.flv?delaytime=10s -} -