examples: move gop cache into channels
This commit is contained in:
parent
9c6c79df1f
commit
d16d14210e
@ -2,9 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
"github.com/nareix/joy4/format"
|
"github.com/nareix/joy4/format"
|
||||||
"github.com/nareix/joy4/av/avutil"
|
"github.com/nareix/joy4/av/avutil"
|
||||||
"github.com/nareix/joy4/av/pubsub"
|
"github.com/nareix/joy4/av/pubsub"
|
||||||
|
"github.com/nareix/joy4/av/pktque"
|
||||||
"github.com/nareix/joy4/format/rtmp"
|
"github.com/nareix/joy4/format/rtmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,7 +31,25 @@ func main() {
|
|||||||
|
|
||||||
if ch != nil {
|
if ch != nil {
|
||||||
cursor := ch.que.Latest()
|
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 {
|
if ch == nil {
|
||||||
ch = &Channel{}
|
ch = &Channel{}
|
||||||
ch.que = pubsub.NewQueue(streams)
|
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
|
channels[conn.URL.Path] = ch
|
||||||
} else {
|
} else {
|
||||||
ch = nil
|
ch = nil
|
||||||
@ -61,6 +87,15 @@ func main() {
|
|||||||
|
|
||||||
// ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie
|
// ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie
|
||||||
// ffmpeg -f avfoundation -i "0:0" .... -f flv rtmp://localhost/screen
|
// ffmpeg -f avfoundation -i "0:0" .... -f flv rtmp://localhost/screen
|
||||||
|
|
||||||
|
// with cache size options
|
||||||
|
|
||||||
// ffplay rtmp://localhost/movie
|
// ffplay rtmp://localhost/movie
|
||||||
// ffplay rtmp://localhost/screen
|
// 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
|
||||||
}
|
}
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user