mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 23:26:54 +08:00
merge examples
This commit is contained in:
@@ -20,11 +20,9 @@ Features:
|
|||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
* [client-query](examples/client-query.go)
|
* [client-query](examples/client-query.go)
|
||||||
* [client-read-udp](examples/client-read-udp.go)
|
* [client-read](examples/client-read.go)
|
||||||
* [client-read-tcp](examples/client-read-tcp.go)
|
|
||||||
* [client-read-pause](examples/client-read-pause.go)
|
* [client-read-pause](examples/client-read-pause.go)
|
||||||
* [client-publish-udp](examples/client-publish-udp.go)
|
* [client-publish](examples/client-publish.go)
|
||||||
* [client-publish-tcp](examples/client-publish-tcp.go)
|
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
@@ -1,69 +0,0 @@
|
|||||||
// +build ignore
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/aler9/gortsplib"
|
|
||||||
"github.com/aler9/gortsplib/pkg/rtph264"
|
|
||||||
)
|
|
||||||
|
|
||||||
// This example shows how to
|
|
||||||
// * generate RTP/H264 frames from a file with Gstreamer
|
|
||||||
// * connect to a RTSP server, announce a H264 track
|
|
||||||
// * write the frames with the TCP protocol
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// open a listener to receive RTP/H264 frames
|
|
||||||
pc, err := net.ListenPacket("udp4", "127.0.0.1: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")
|
|
||||||
|
|
||||||
// wait for RTP/H264 frames
|
|
||||||
decoder := rtph264.NewDecoderFromPacketConn(pc)
|
|
||||||
sps, pps, err := decoder.ReadSPSPPS()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
fmt.Println("stream connected")
|
|
||||||
|
|
||||||
// create a H264 track
|
|
||||||
track, err := gortsplib.NewTrackH264(0, sps, pps)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// connect to the server and start publishing the track
|
|
||||||
dialer := gortsplib.Dialer{
|
|
||||||
StreamProtocol: gortsplib.StreamProtocolTCP,
|
|
||||||
}
|
|
||||||
conn, err := dialer.DialPublish("rtsp://localhost:8554/mystream", gortsplib.Tracks{track})
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
buf := make([]byte, 2048)
|
|
||||||
for {
|
|
||||||
// read frames from the source
|
|
||||||
n, _, err := pc.ReadFrom(buf)
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// write frames to the server
|
|
||||||
err = conn.WriteFrame(track.Id, gortsplib.StreamTypeRtp, buf[:n])
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("connection is closed (%s)\n", err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,32 +0,0 @@
|
|||||||
// +build ignore
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/aler9/gortsplib"
|
|
||||||
)
|
|
||||||
|
|
||||||
// This example shows how to
|
|
||||||
// * connect to a RTSP server
|
|
||||||
// * read all tracks with the TCP protocol
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// connect to the server and start reading all tracks
|
|
||||||
dialer := gortsplib.Dialer{
|
|
||||||
StreamProtocol: gortsplib.StreamProtocolTCP,
|
|
||||||
}
|
|
||||||
conn, err := dialer.DialRead("rtsp://localhost:8554/mystream")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// read frames from the server
|
|
||||||
done := conn.OnFrame(func(id int, typ gortsplib.StreamType, buf []byte) {
|
|
||||||
fmt.Printf("frame from track %d, type %v: %v\n", id, typ, buf)
|
|
||||||
})
|
|
||||||
|
|
||||||
<-done
|
|
||||||
}
|
|
Reference in New Issue
Block a user