-
Notifications
You must be signed in to change notification settings - Fork 192
/
main.go
122 lines (105 loc) · 2.74 KB
/
main.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"log"
"net"
"github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/base"
"github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/pion/rtp"
)
// This example shows how to
// 1. generate a G711 stream and RTP packets with GStreamer
// 2. connect to a RTSP server, find a back channel that supports G711
// 3. route the packets from GStreamer to the channel
func findPCMUBackChannel(desc *description.Session) *description.Media {
for _, media := range desc.Medias {
if media.IsBackChannel {
for _, forma := range media.Formats {
if g711, ok := forma.(*format.G711); ok {
if g711.MULaw {
return media
}
}
}
}
}
return nil
}
func main() {
// open a listener to receive RTP/G711 packets
pc, err := net.ListenPacket("udp", "localhost:9000")
if err != nil {
panic(err)
}
defer pc.Close()
log.Println("Waiting for a RTP/G711 stream on UDP port 9000 - you can generate one with GStreamer:\n\n" +
"* audio from a test sine:\n\n" +
"gst-launch-1.0 audiotestsrc freq=300 ! audioconvert ! audioresample ! audio/x-raw,rate=8000" +
" ! mulawenc ! rtppcmupay ! udpsink host=127.0.0.1 port=9000\n\n" +
"* audio from a file:\n\n" +
"gst-launch-1.0 filesrc location=my_file.mp4 ! decodebin ! audioconvert ! audioresample ! audio/x-raw,rate=8000" +
" ! mulawenc ! rtppcmupay ! udpsink host=127.0.0.1 port=9000\n\n" +
"* audio from a microphone:\n\n" +
"gst-launch-1.0 pulsesrc ! audioconvert ! audioresample ! audio/x-raw,rate=8000" +
" ! mulawenc ! rtppcmupay ! udpsink host=127.0.0.1 port=9000\n")
// wait for first packet
buf := make([]byte, 2048)
n, _, err := pc.ReadFrom(buf)
if err != nil {
panic(err)
}
log.Println("stream connected")
c := gortsplib.Client{
RequestBackChannels: true,
}
// parse URL
u, err := base.ParseURL("rtsp://localhost:8554/mystream")
if err != nil {
panic(err)
}
// connect to the server
err = c.Start(u.Scheme, u.Host)
if err != nil {
panic(err)
}
defer c.Close()
// find published medias
desc, _, err := c.Describe(u)
if err != nil {
panic(err)
}
// find the back channel
medi := findPCMUBackChannel(desc)
if medi == nil {
panic("media not found")
}
// setup a single media
_, err = c.Setup(desc.BaseURL, medi, 0, 0)
if err != nil {
panic(err)
}
// start playing
_, err = c.Play(nil)
if err != nil {
panic(err)
}
var pkt rtp.Packet
for {
// parse RTP packet
err = pkt.Unmarshal(buf[:n])
if err != nil {
panic(err)
}
// route RTP packet to the server
err = c.WritePacketRTP(medi, &pkt)
if err != nil {
panic(err)
}
// read another RTP packet from source
n, _, err = pc.ReadFrom(buf)
if err != nil {
panic(err)
}
}
}