rename 'read' into 'play', 'publish' into 'record' (#463)

This commit is contained in:
Alessandro Ros
2023-11-11 20:36:26 +01:00
committed by GitHub
parent ccecef1ed7
commit aaef8c29a7
42 changed files with 64 additions and 64 deletions

View File

@@ -0,0 +1,101 @@
package main
import (
"log"
"github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/base"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtph265"
"github.com/pion/rtp"
)
// This example shows how to
// 1. connect to a RTSP server
// 2. check if there's a H265 media
// 3. save the content of the media into a file in MPEG-TS format
func main() {
c := gortsplib.Client{}
// parse URL
u, err := base.ParseURL("rtsp://localhost:8554/mystream")
if err != nil {
panic(err)
}
// connect to the server
err = c.Start(u.Scheme, u.Host)
if err != nil {
panic(err)
}
defer c.Close()
// find available medias
desc, _, err := c.Describe(u)
if err != nil {
panic(err)
}
// find the H265 media and format
var forma *format.H265
medi := desc.FindFormat(&forma)
if medi == nil {
panic("media not found")
}
// setup RTP/H265 -> H265 decoder
rtpDec, err := forma.CreateDecoder()
if err != nil {
panic(err)
}
// setup H265 -> MPEG-TS muxer
mpegtsMuxer, err := newMPEGTSMuxer(forma.VPS, forma.SPS, forma.PPS)
if err != nil {
panic(err)
}
// setup a single media
_, err = c.Setup(desc.BaseURL, medi, 0, 0)
if err != nil {
panic(err)
}
// called when a RTP packet arrives
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
// decode timestamp
pts, ok := c.PacketPTS(medi, pkt)
if !ok {
log.Printf("waiting for timestamp")
return
}
// extract access unit from RTP packets
au, err := rtpDec.Decode(pkt)
if err != nil {
if err != rtph265.ErrNonStartingPacketAndNoPrevious && err != rtph265.ErrMorePacketsNeeded {
log.Printf("ERR: %v", err)
}
return
}
// encode the access unit into MPEG-TS
err = mpegtsMuxer.encode(au, pts)
if err != nil {
log.Printf("ERR: %v", err)
return
}
log.Printf("saved TS packet")
})
// start playing
_, err = c.Play(nil)
if err != nil {
panic(err)
}
// wait until a fatal error
panic(c.Wait())
}

View File

@@ -0,0 +1,123 @@
package main
import (
"bufio"
"os"
"time"
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
)
func durationGoToMPEGTS(v time.Duration) int64 {
return int64(v.Seconds() * 90000)
}
// mpegtsMuxer allows to save a H265 stream into a MPEG-TS file.
type mpegtsMuxer struct {
vps []byte
sps []byte
pps []byte
f *os.File
b *bufio.Writer
w *mpegts.Writer
track *mpegts.Track
dtsExtractor *h265.DTSExtractor
}
// newMPEGTSMuxer allocates a mpegtsMuxer.
func newMPEGTSMuxer(vps []byte, sps []byte, pps []byte) (*mpegtsMuxer, error) {
f, err := os.Create("mystream.ts")
if err != nil {
return nil, err
}
b := bufio.NewWriter(f)
track := &mpegts.Track{
Codec: &mpegts.CodecH265{},
}
w := mpegts.NewWriter(b, []*mpegts.Track{track})
return &mpegtsMuxer{
vps: vps,
sps: sps,
pps: pps,
f: f,
b: b,
w: w,
track: track,
}, nil
}
// close closes all the mpegtsMuxer resources.
func (e *mpegtsMuxer) close() {
e.b.Flush()
e.f.Close()
}
// encode encodes a H265 access unit into MPEG-TS.
func (e *mpegtsMuxer) encode(au [][]byte, pts time.Duration) error {
// prepend an AUD. This is required by some players
filteredAU := [][]byte{
{byte(h265.NALUType_AUD_NUT) << 1, 1, 0x50},
}
isRandomAccess := false
for _, nalu := range au {
typ := h265.NALUType((nalu[0] >> 1) & 0b111111)
switch typ {
case h265.NALUType_VPS_NUT:
e.vps = nalu
continue
case h265.NALUType_SPS_NUT:
e.sps = nalu
continue
case h265.NALUType_PPS_NUT:
e.pps = nalu
continue
case h265.NALUType_AUD_NUT:
continue
case h265.NALUType_IDR_W_RADL, h265.NALUType_IDR_N_LP, h265.NALUType_CRA_NUT:
isRandomAccess = true
}
filteredAU = append(filteredAU, nalu)
}
au = filteredAU
if len(au) <= 1 {
return nil
}
// add VPS, SPS and PPS before random access access unit
if isRandomAccess {
au = append([][]byte{e.vps, e.sps, e.pps}, au...)
}
var dts time.Duration
if e.dtsExtractor == nil {
// skip samples silently until we find one with a IDR
if !isRandomAccess {
return nil
}
e.dtsExtractor = h265.NewDTSExtractor()
}
var err error
dts, err = e.dtsExtractor.Extract(au, pts)
if err != nil {
return err
}
// encode into MPEG-TS
return e.w.WriteH26x(e.track, durationGoToMPEGTS(pts), durationGoToMPEGTS(dts), isRandomAccess, au)
}