-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (64 loc) · 1.72 KB
/
main.go
File metadata and controls
77 lines (64 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Package main contains an example.
package main
import (
"log"
"github.com/bluenviron/gortsplib/v5"
"github.com/bluenviron/gortsplib/v5/pkg/base"
"github.com/bluenviron/gortsplib/v5/pkg/description"
"github.com/bluenviron/gortsplib/v5/pkg/format"
"github.com/pion/rtp"
)
// This example shows how to:
// 1. connect to a RTSP server.
// 2. read all medias on a path.
// 3. re-publish all medias on another path.
func main() {
// parse source URL
sourceURL, err := base.ParseURL("rtsp://myuser:mypass@localhost:8554/mystream")
if err != nil {
panic(err)
}
reader := gortsplib.Client{
Scheme: sourceURL.Scheme,
Host: sourceURL.Host,
}
// connect to the server
err = reader.Start()
if err != nil {
panic(err)
}
defer reader.Close()
// find available medias
desc, _, err := reader.Describe(sourceURL)
if err != nil {
panic(err)
}
log.Printf("republishing %d medias", len(desc.Medias))
// setup all medias
// this must be called before StartRecording(), since it overrides the control attribute.
err = reader.SetupAll(desc.BaseURL, desc.Medias)
if err != nil {
panic(err)
}
// connect to the server and start recording the same medias
publisher := gortsplib.Client{}
err = publisher.StartRecording("rtsp://myuser:mypass@localhost:8554/mystream2", desc)
if err != nil {
panic(err)
}
defer publisher.Close()
// read RTP packets from the reader and route them to the publisher
reader.OnPacketRTPAny(func(media *description.Media, _ format.Format, pkt *rtp.Packet) {
err2 := publisher.WritePacketRTP(media, pkt)
if err2 != nil {
log.Printf("ERR: %v", err2)
}
})
// start playing
_, err = reader.Play(nil)
if err != nil {
panic(err)
}
// wait until a fatal error
panic(reader.Wait())
}