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