mirror of
https://github.com/aler9/gortsplib
synced 2025-10-04 23:02:45 +08:00
simplify h264 publish examples
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/aler9/gortsplib"
|
||||
"github.com/aler9/gortsplib/pkg/rtph264"
|
||||
"github.com/pion/rtp/v2"
|
||||
)
|
||||
|
||||
@@ -27,17 +26,16 @@ func main() {
|
||||
" ! x264enc speed-preset=veryfast tune=zerolatency bitrate=600000" +
|
||||
" ! rtph264pay ! udpsink host=127.0.0.1 port=9000")
|
||||
|
||||
// get SPS and PPS
|
||||
decoder := &rtph264.Decoder{}
|
||||
decoder.Init()
|
||||
sps, pps, err := decoder.ReadSPSPPS(rtph264.PacketConnReader{pc})
|
||||
// wait for first packet
|
||||
buf := make([]byte, 2048)
|
||||
n, _, err := pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("stream connected")
|
||||
|
||||
// create an H264 track
|
||||
track, err := gortsplib.NewTrackH264(96, sps, pps, nil)
|
||||
track, err := gortsplib.NewTrackH264(96, nil, nil, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -51,25 +49,24 @@ func main() {
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
buf := make([]byte, 2048)
|
||||
var pkt rtp.Packet
|
||||
for {
|
||||
// read RTP packets from the source
|
||||
n, _, err := pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// parse RTP packets
|
||||
// parse RTP packet
|
||||
err = pkt.Unmarshal(buf[:n])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// route RTP packets to the server
|
||||
// route RTP packet to the server
|
||||
err = c.WritePacketRTP(0, &pkt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// read another RTP packet from source
|
||||
n, _, err = pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/aler9/gortsplib"
|
||||
"github.com/aler9/gortsplib/pkg/rtph264"
|
||||
"github.com/pion/rtp/v2"
|
||||
)
|
||||
|
||||
@@ -28,17 +27,16 @@ func main() {
|
||||
"gst-launch-1.0 filesrc location=video.mp4 ! qtdemux ! video/x-h264" +
|
||||
" ! h264parse config-interval=1 ! rtph264pay ! udpsink host=127.0.0.1 port=9000")
|
||||
|
||||
// get SPS and PPS
|
||||
decoder := &rtph264.Decoder{}
|
||||
decoder.Init()
|
||||
sps, pps, err := decoder.ReadSPSPPS(rtph264.PacketConnReader{pc})
|
||||
// wait for first packet
|
||||
buf := make([]byte, 2048)
|
||||
n, _, err := pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("stream connected")
|
||||
|
||||
// create an H264 track
|
||||
track, err := gortsplib.NewTrackH264(96, sps, pps, nil)
|
||||
track, err := gortsplib.NewTrackH264(96, nil, nil, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -61,25 +59,24 @@ func main() {
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
buf := make([]byte, 2048)
|
||||
var pkt rtp.Packet
|
||||
for {
|
||||
// read RTP packets from the source
|
||||
n, _, err := pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// parse RTP packets
|
||||
// parse RTP packet
|
||||
err = pkt.Unmarshal(buf[:n])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// route RTP packets to the server
|
||||
// route RTP packet to the server
|
||||
err = c.WritePacketRTP(0, &pkt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// read another RTP packet from source
|
||||
n, _, err = pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/aler9/gortsplib"
|
||||
"github.com/aler9/gortsplib/pkg/rtph264"
|
||||
"github.com/pion/rtp/v2"
|
||||
)
|
||||
|
||||
@@ -29,24 +28,22 @@ func main() {
|
||||
"gst-launch-1.0 filesrc location=video.mp4 ! qtdemux ! video/x-h264" +
|
||||
" ! h264parse config-interval=1 ! rtph264pay ! udpsink host=127.0.0.1 port=9000")
|
||||
|
||||
// get SPS and PPS
|
||||
decoder := &rtph264.Decoder{}
|
||||
decoder.Init()
|
||||
sps, pps, err := decoder.ReadSPSPPS(rtph264.PacketConnReader{pc})
|
||||
// wait for first packet
|
||||
buf := make([]byte, 2048)
|
||||
n, _, err := pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("stream connected")
|
||||
|
||||
// create an H264 track
|
||||
track, err := gortsplib.NewTrackH264(96, sps, pps, nil)
|
||||
track, err := gortsplib.NewTrackH264(96, nil, nil, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c := gortsplib.Client{}
|
||||
|
||||
// connect to the server and start publishing the track
|
||||
c := gortsplib.Client{}
|
||||
err = c.StartPublishing("rtsp://localhost:8554/mystream",
|
||||
gortsplib.Tracks{track})
|
||||
if err != nil {
|
||||
@@ -56,25 +53,21 @@ func main() {
|
||||
|
||||
for {
|
||||
go func() {
|
||||
buf := make([]byte, 2048)
|
||||
var pkt rtp.Packet
|
||||
for {
|
||||
// read RTP packets from the source
|
||||
n, _, err := pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
// parse RTP packets
|
||||
// parse RTP packet
|
||||
err = pkt.Unmarshal(buf[:n])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// route RTP packets to the server
|
||||
err = c.WritePacketRTP(0, &pkt)
|
||||
// route RTP packet to the server
|
||||
c.WritePacketRTP(0, &pkt)
|
||||
|
||||
// read another RTP packet from source
|
||||
n, _, err = pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
break
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
@@ -17,7 +17,7 @@ import (
|
||||
// This example shows how to
|
||||
// 1. connect to a RTSP server and read all tracks on a path
|
||||
// 2. check if there's a H264 track
|
||||
// 3. convert H264 into raw frames
|
||||
// 3. decode H264 into raw frames
|
||||
// 4. encode the frames into JPEG images and save them on disk
|
||||
|
||||
// This example requires the ffmpeg libraries, that can be installed in this way:
|
||||
|
@@ -10,7 +10,7 @@ import (
|
||||
// This example shows how to
|
||||
// 1. connect to a RTSP server and read all tracks on a path
|
||||
// 2. check if there's a H264 track
|
||||
// 3. save the content of the H264 track to a file in MPEG-TS format
|
||||
// 3. save the content of the H264 track into a file in MPEG-TS format
|
||||
|
||||
func main() {
|
||||
c := gortsplib.Client{}
|
||||
|
@@ -12,7 +12,7 @@ import (
|
||||
// This example shows how to
|
||||
// 1. connect to a RTSP server and read all tracks on a path
|
||||
// 2. check if there's an H264 track
|
||||
// 3. convert H264 into raw frames
|
||||
// 3. decode H264 into raw frames
|
||||
|
||||
// This example requires the ffmpeg libraries, that can be installed in this way:
|
||||
// apt install -y libavformat-dev libswscale-dev gcc pkg-config
|
||||
|
Reference in New Issue
Block a user