-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
188 lines (157 loc) · 4.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"sync/atomic"
"time"
"github.com/deepch/vdk/av"
"github.com/deepch/vdk/codec/h264parser"
"github.com/deepch/vdk/format/rtsp"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/shirou/gopsutil/cpu"
)
var (
outboundVideoTrack *webrtc.TrackLocalStaticSample
peerConnectionCount int64
)
// Generate CSV with columns of timestamp, peerConnectionCount, and cpuUsage
func reportBuilder() {
file, err := os.OpenFile("report.csv", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
if _, err := file.WriteString("timestamp, peerConnectionCount, cpuUsage\n"); err != nil {
panic(err)
}
for range time.NewTicker(3 * time.Second).C {
usage, err := cpu.Percent(0, false)
if err != nil {
panic(err)
} else if len(usage) != 1 {
panic(fmt.Sprintf("CPU Usage results should have 1 sample, have %d", len(usage)))
}
if _, err = file.WriteString(fmt.Sprintf("%s, %d, %f\n", time.Now().Format(time.RFC3339), atomic.LoadInt64(&peerConnectionCount), usage[0])); err != nil {
panic(err)
}
}
}
// HTTP Handler that accepts an Offer and returns an Answer
// adds outboundVideoTrack to PeerConnection
func doSignaling(w http.ResponseWriter, r *http.Request) {
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{})
if err != nil {
panic(err)
}
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
if connectionState == webrtc.ICEConnectionStateDisconnected {
atomic.AddInt64(&peerConnectionCount, -1)
if err := peerConnection.Close(); err != nil {
panic(err)
}
} else if connectionState == webrtc.ICEConnectionStateConnected {
atomic.AddInt64(&peerConnectionCount, 1)
}
})
if _, err = peerConnection.AddTrack(outboundVideoTrack); err != nil {
panic(err)
}
var offer webrtc.SessionDescription
if err = json.NewDecoder(r.Body).Decode(&offer); err != nil {
panic(err)
}
if err = peerConnection.SetRemoteDescription(offer); err != nil {
panic(err)
}
gatherCompletePromise := webrtc.GatheringCompletePromise(peerConnection)
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
} else if err = peerConnection.SetLocalDescription(answer); err != nil {
panic(err)
}
<-gatherCompletePromise
response, err := json.Marshal(*peerConnection.LocalDescription())
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(response); err != nil {
panic(err)
}
}
func main() {
var err error
outboundVideoTrack, err = webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{
MimeType: "video/h264",
}, "pion-rtsp", "pion-rtsp")
if err != nil {
panic(err)
}
go rtspConsumer()
go reportBuilder()
http.Handle("/", http.FileServer(http.Dir("./static")))
http.HandleFunc("/doSignaling", doSignaling)
fmt.Println("Open http://localhost:8080 to access this demo")
panic(http.ListenAndServe(":8080", nil))
}
// The RTSP URL that will be streamed
const rtspURL = "rtsp://170.93.143.139:1935/rtplive/0b01b57900060075004d823633235daa"
// Connect to an RTSP URL and pull media.
// Convert H264 to Annex-B, then write to outboundVideoTrack which sends to all PeerConnections
func rtspConsumer() {
annexbNALUStartCode := func() []byte { return []byte{0x00, 0x00, 0x00, 0x01} }
for {
session, err := rtsp.Dial(rtspURL)
if err != nil {
panic(err)
}
session.RtpKeepAliveTimeout = 10 * time.Second
codecs, err := session.Streams()
if err != nil {
panic(err)
}
for i, t := range codecs {
log.Println("Stream", i, "is of type", t.Type().String())
}
if codecs[0].Type() != av.H264 {
panic("RTSP feed must begin with a H264 codec")
}
if len(codecs) != 1 {
log.Println("Ignoring all but the first stream.")
}
var previousTime time.Duration
for {
pkt, err := session.ReadPacket()
if err != nil {
break
}
if pkt.Idx != 0 {
//audio or other stream, skip it
continue
}
pkt.Data = pkt.Data[4:]
// For every key-frame pre-pend the SPS and PPS
if pkt.IsKeyFrame {
pkt.Data = append(annexbNALUStartCode(), pkt.Data...)
pkt.Data = append(codecs[0].(h264parser.CodecData).PPS(), pkt.Data...)
pkt.Data = append(annexbNALUStartCode(), pkt.Data...)
pkt.Data = append(codecs[0].(h264parser.CodecData).SPS(), pkt.Data...)
pkt.Data = append(annexbNALUStartCode(), pkt.Data...)
}
bufferDuration := pkt.Time - previousTime
previousTime = pkt.Time
if err = outboundVideoTrack.WriteSample(media.Sample{Data: pkt.Data, Duration: bufferDuration}); err != nil && err != io.ErrClosedPipe {
panic(err)
}
}
if err = session.Close(); err != nil {
log.Println("session Close error", err)
}
time.Sleep(5 * time.Second)
}
}