add example client-publish-aac

This commit is contained in:
aler9
2021-10-23 13:18:43 +02:00
parent e8e94c2b14
commit ca13db6b5b
3 changed files with 73 additions and 7 deletions

View File

@@ -55,7 +55,8 @@ Features:
* [client-read-pause](examples/client-read-pause/main.go)
* [client-read-h264](examples/client-read-h264/main.go)
* [client-read-save-to-disk](examples/client-read-save-to-disk/main.go)
* [client-publish](examples/client-publish/main.go)
* [client-publish-h264](examples/client-publish-h264/main.go)
* [client-publish-aac](examples/client-publish-aac/main.go)
* [client-publish-options](examples/client-publish-options/main.go)
* [client-publish-pause](examples/client-publish-pause/main.go)
* [server](examples/server/main.go)

View File

@@ -0,0 +1,64 @@
package main
import (
"fmt"
"net"
"github.com/aler9/gortsplib"
)
// This example shows how to
// 1. generate RTP/AAC packets with Gstreamer
// 2. connect to a RTSP server, announce an AAC track
// 3. route the packets from Gstreamer to the server
func main() {
// open a listener to receive RTP/AAC packets
pc, err := net.ListenPacket("udp", "localhost:9000")
if err != nil {
panic(err)
}
defer pc.Close()
fmt.Println("Waiting for a RTP/AAC stream on UDP port 9000 - you can send one with Gstreamer:\n" +
"gst-launch-1.0 audiotestsrc freq=300 ! audioconvert ! audioresample ! audio/x-raw,rate=48000" +
" ! avenc_aac bitrate=128000" +
" ! rtpmp4gpay ! udpsink host=127.0.0.1 port=9000")
// wait for first packet
buf := make([]byte, 2048)
_, _, err = pc.ReadFrom(buf)
if err != nil {
panic(err)
}
fmt.Println("stream connected")
// create an AAC track
track, err := gortsplib.NewTrackAAC(96, &gortsplib.TrackConfigAAC{Type: 2, SampleRate: 48000, ChannelCount: 2})
if err != nil {
panic(err)
}
// connect to the server and start publishing the track
conn, err := gortsplib.DialPublish("rtsp://localhost:8554/mystream",
gortsplib.Tracks{track})
if err != nil {
panic(err)
}
defer conn.Close()
buf = make([]byte, 2048)
for {
// read RTP packets from the source
n, _, err := pc.ReadFrom(buf)
if err != nil {
panic(err)
}
// write RTP packets
err = conn.WriteFrame(0, gortsplib.StreamTypeRTP, buf[:n])
if err != nil {
panic(err)
}
}
}

View File

@@ -9,21 +9,22 @@ import (
)
// This example shows how to
// 1. generate RTP/H264 frames from a file with Gstreamer
// 1. generate RTP/H264 packets with Gstreamer
// 2. connect to a RTSP server, announce an H264 track
// 3. write the frames to the server
// 3. route the packets from Gstreamer to the server
func main() {
// open a listener to receive RTP/H264 frames
// open a listener to receive RTP/H264 packets
pc, err := net.ListenPacket("udp", "localhost:9000")
if err != nil {
panic(err)
}
defer pc.Close()
fmt.Println("Waiting for a rtp/h264 stream on port 9000 - you can send one with gstreamer:\n" +
"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")
fmt.Println("Waiting for a RTP/h264 stream on UDP port 9000 - you can send one with Gstreamer:\n" +
"gst-launch-1.0 videotestsrc ! video/x-raw,width=1920,height=1080" +
" ! x264enc speed-preset=veryfast tune=zerolatency bitrate=600000" +
" ! rtph264pay ! udpsink host=127.0.0.1 port=9000")
// get SPS and PPS
decoder := rtph264.NewDecoder()