mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 23:26:54 +08:00
add example client-publish-aac
This commit is contained in:
@@ -55,7 +55,8 @@ Features:
|
|||||||
* [client-read-pause](examples/client-read-pause/main.go)
|
* [client-read-pause](examples/client-read-pause/main.go)
|
||||||
* [client-read-h264](examples/client-read-h264/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-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-options](examples/client-publish-options/main.go)
|
||||||
* [client-publish-pause](examples/client-publish-pause/main.go)
|
* [client-publish-pause](examples/client-publish-pause/main.go)
|
||||||
* [server](examples/server/main.go)
|
* [server](examples/server/main.go)
|
||||||
|
64
examples/client-publish-aac/main.go
Normal file
64
examples/client-publish-aac/main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -9,21 +9,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// This example shows how to
|
// 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
|
// 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() {
|
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")
|
pc, err := net.ListenPacket("udp", "localhost:9000")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer pc.Close()
|
defer pc.Close()
|
||||||
|
|
||||||
fmt.Println("Waiting for a rtp/h264 stream on port 9000 - you can send one with gstreamer:\n" +
|
fmt.Println("Waiting for a RTP/h264 stream on UDP port 9000 - you can send one with Gstreamer:\n" +
|
||||||
"gst-launch-1.0 filesrc location=video.mp4 ! qtdemux ! video/x-h264" +
|
"gst-launch-1.0 videotestsrc ! video/x-raw,width=1920,height=1080" +
|
||||||
" ! h264parse config-interval=1 ! rtph264pay ! udpsink host=127.0.0.1 port=9000")
|
" ! x264enc speed-preset=veryfast tune=zerolatency bitrate=600000" +
|
||||||
|
" ! rtph264pay ! udpsink host=127.0.0.1 port=9000")
|
||||||
|
|
||||||
// get SPS and PPS
|
// get SPS and PPS
|
||||||
decoder := rtph264.NewDecoder()
|
decoder := rtph264.NewDecoder()
|
Reference in New Issue
Block a user