Skip to content

Commit fa4df37

Browse files
Merge pull request #25 from matrix-org/SimonBrandner/task/lint
2 parents e97f0c2 + 3e1d8ba commit fa4df37

File tree

10 files changed

+205
-75
lines changed

10 files changed

+205
-75
lines changed

.github/workflows/lint.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Lint
2+
3+
on:
4+
pull_request: {}
5+
push:
6+
branches: [main]
7+
jobs:
8+
# Run golangci-lint
9+
lint:
10+
timeout-minutes: 5
11+
name: Linting
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Install Go
16+
uses: actions/setup-go@v3
17+
with:
18+
go-version: 1.18
19+
- name: golangci-lint
20+
uses: golangci/golangci-lint-action@v3

.golangci.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
linters-settings:
2+
lll:
3+
line-length: 120
4+
wsl:
5+
allow-cuddle-declarations: true
6+
7+
linters:
8+
enable-all: true
9+
disable:
10+
- nlreturn
11+
- exhaustivestruct
12+
- exhaustruct
13+
- cyclop
14+
- gochecknoglobals
15+
- gomoddirectives
16+
- exhaustive
17+
- funlen
18+
- gofumpt
19+
- godox # We ought to enable this at some point

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ require (
1010
maunium.net/go/mautrix v0.11.0
1111
)
1212

13-
replace maunium.net/go/mautrix v0.11.0 => ../mautrix-go
14-
1513
require (
1614
github.com/google/uuid v1.3.0 // indirect
1715
github.com/pion/datachannel v1.5.2 // indirect
@@ -38,3 +36,5 @@ require (
3836
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
3937
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
4038
)
39+
40+
replace maunium.net/go/mautrix => github.com/matrix-org/mautrix-go v0.0.0-20220817142816-160ea900a20b

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
2525
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
2626
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2727
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
28+
github.com/matrix-org/mautrix-go v0.0.0-20220817142816-160ea900a20b h1:qKvyphdDykNjyF1vJLaVuWCPfNJWNzP7wHvMV5mw+Ss=
29+
github.com/matrix-org/mautrix-go v0.0.0-20220817142816-160ea900a20b/go.mod h1:hHvNi5iKVAiI2MAdAeXHtP4g9BvNEX2rsQpSF/x6Kx4=
2830
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
2931
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
3032
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=

src/call.go

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import (
2121
"log"
2222
"time"
2323

24+
"github.com/pion/webrtc/v3"
2425
"maunium.net/go/mautrix"
2526
"maunium.net/go/mautrix/event"
2627
"maunium.net/go/mautrix/id"
27-
28-
"github.com/pion/webrtc/v3"
2928
)
3029

3130
type Call struct {
@@ -53,10 +52,12 @@ func (c *Call) onDCSelect(start []event.SFUTrackDescription) {
5352
StreamID: trackDesc.StreamID,
5453
TrackID: trackDesc.TrackID,
5554
})
55+
5656
if len(foundTracks) == 0 {
5757
log.Printf("%s | no track found StreamID %s TrackID %s", c.UserID, trackDesc.StreamID, trackDesc.TrackID)
5858
continue
5959
}
60+
6061
for _, track := range foundTracks {
6162
if _, err := c.PeerConnection.AddTrack(track); err == nil {
6263
log.Printf("%s | added %s StreamID %s TrackID %s", c.UserID, track.Kind(), track.StreamID(), track.ID())
@@ -84,6 +85,7 @@ func (c *Call) onDCPublish(sdp string) {
8485
log.Printf("%s | failed to create answer - ignoring: %s", c.UserID, err)
8586
return
8687
}
88+
8789
err = c.PeerConnection.SetLocalDescription(offer)
8890
if err != nil {
8991
log.Printf("%s | failed to set local description %+v - ignoring: %s", c.UserID, offer.SDP, err)
@@ -99,13 +101,13 @@ func (c *Call) onDCPublish(sdp string) {
99101
func (c *Call) onDCUnpublish(stop []event.SFUTrackDescription, sdp string) {
100102
for _, trackDesc := range stop {
101103
log.Printf("%s | unpublishing StreamID %s TrackID %s", c.UserID, trackDesc.StreamID, trackDesc.TrackID)
104+
102105
if removedTracksCount := c.Conf.RemoveTracksFromPeerConnectionsByInfo(LocalTrackInfo{
103106
StreamID: trackDesc.StreamID,
104107
TrackID: trackDesc.TrackID,
105108
}); removedTracksCount == 0 {
106109
log.Printf("%s | no tracks to remove for: %+v", c.UserID, stop)
107110
}
108-
109111
}
110112

111113
err := c.PeerConnection.SetRemoteDescription(webrtc.SessionDescription{
@@ -122,6 +124,7 @@ func (c *Call) onDCUnpublish(stop []event.SFUTrackDescription, sdp string) {
122124
log.Printf("%s | failed to create answer - ignoring: %s", c.UserID, err)
123125
return
124126
}
127+
125128
err = c.PeerConnection.SetLocalDescription(offer)
126129
if err != nil {
127130
log.Printf("%s | failed to set local description %+v - ignoring: %s", c.UserID, offer.SDP, err)
@@ -149,34 +152,33 @@ func (c *Call) onDCAnswer(sdp string) {
149152

150153
func (c *Call) onDCAlive() {
151154
c.lastKeepAliveTimestamp = time.Now()
152-
153155
}
154156

155-
func (c *Call) onDCMetadata(metadata event.CallSDPStreamMetadata) {
157+
func (c *Call) onDCMetadata() {
156158
log.Printf("%s | received DC metadata", c.UserID)
157159

158160
c.Conf.SendUpdatedMetadataFromCall(c.CallID)
159161
}
160162

161-
func (c *Call) dataChannelHandler(d *webrtc.DataChannel) {
162-
c.dataChannel = d
163+
func (c *Call) dataChannelHandler(channel *webrtc.DataChannel) {
164+
c.dataChannel = channel
163165

164-
d.OnOpen(func() {
166+
channel.OnOpen(func() {
165167
c.SendDataChannelMessage(event.SFUMessage{Op: event.SFUOperationMetadata})
166168
})
167169

168-
d.OnError(func(err error) {
170+
channel.OnError(func(err error) {
169171
log.Fatalf("%s | DC error: %s", c.CallID, err)
170172
})
171173

172-
d.OnMessage(func(m webrtc.DataChannelMessage) {
173-
if !m.IsString {
174-
log.Printf("%s | inbound message is not string - ignoring: %+v", c.UserID, m)
174+
channel.OnMessage(func(marshaledMsg webrtc.DataChannelMessage) {
175+
if !marshaledMsg.IsString {
176+
log.Printf("%s | inbound message is not string - ignoring: %+v", c.UserID, marshaledMsg)
175177
return
176178
}
177179

178180
msg := &event.SFUMessage{}
179-
if err := json.Unmarshal(m.Data, msg); err != nil {
181+
if err := json.Unmarshal(marshaledMsg.Data, msg); err != nil {
180182
log.Printf("%s | failed to unmarshal %+v - ignoring: %s", c.CallID, msg, err)
181183
return
182184
}
@@ -197,7 +199,7 @@ func (c *Call) dataChannelHandler(d *webrtc.DataChannel) {
197199
case event.SFUOperationAlive:
198200
c.onDCAlive()
199201
case event.SFUOperationMetadata:
200-
c.onDCMetadata(msg.Metadata)
202+
c.onDCMetadata()
201203

202204
default:
203205
log.Printf("Unknown operation - ignoring: %s", msg.Op)
@@ -217,7 +219,9 @@ func (c *Call) negotiationNeededHandler() {
217219
log.Printf("%s | failed to create offer - ignoring: %s", c.UserID, err)
218220
return
219221
}
222+
220223
err = c.PeerConnection.SetLocalDescription(offer)
224+
221225
if err != nil {
222226
log.Printf("%s | failed to set local description %+v - ignoring: %s", c.UserID, offer.SDP, err)
223227
return
@@ -257,12 +261,22 @@ func (c *Call) iceCandidateHandler(candidate *webrtc.ICECandidate) {
257261
c.sendToDevice(event.CallCandidates, candidateEvtContent)
258262
}
259263

260-
func (c *Call) trackHandler(trackRemote *webrtc.TrackRemote, rec *webrtc.RTPReceiver) {
264+
func (c *Call) trackHandler(trackRemote *webrtc.TrackRemote) {
261265
go WriteRTCP(trackRemote, c.PeerConnection)
262266

263-
trackLocal, err := webrtc.NewTrackLocalStaticRTP(trackRemote.Codec().RTPCodecCapability, trackRemote.ID(), trackRemote.StreamID())
267+
trackLocal, err := webrtc.NewTrackLocalStaticRTP(
268+
trackRemote.Codec().RTPCodecCapability,
269+
trackRemote.ID(),
270+
trackRemote.StreamID(),
271+
)
264272
if err != nil {
265-
log.Printf("%s | failed to create new track local static RTP %+v - ignoring: %s", c.UserID, trackRemote.Codec().RTPCodecCapability, err)
273+
log.Printf(
274+
"%s | failed to create new track local static RTP %+v - ignoring: %s",
275+
c.UserID,
276+
trackRemote.Codec().RTPCodecCapability,
277+
err,
278+
)
279+
266280
return
267281
}
268282

@@ -277,7 +291,13 @@ func (c *Call) trackHandler(trackRemote *webrtc.TrackRemote, rec *webrtc.RTPRece
277291
})
278292
c.Conf.Tracks.Mutex.Unlock()
279293

280-
log.Printf("%s | published %s StreamID %s TrackID %s", c.UserID, trackLocal.Kind(), trackLocal.StreamID(), trackLocal.ID())
294+
log.Printf(
295+
"%s | published %s StreamID %s TrackID %s",
296+
c.UserID,
297+
trackLocal.Kind(),
298+
trackLocal.StreamID(),
299+
trackLocal.ID(),
300+
)
281301

282302
go c.Conf.SendUpdatedMetadataFromCall(c.CallID)
283303
go CopyRemoteToLocal(trackRemote, trackLocal)
@@ -317,10 +337,11 @@ func (c *Call) OnInvite(content *event.CallInviteEventContent) {
317337
if err != nil {
318338
log.Panicf("%s | failed to create new peer connection: %s", c.UserID, err)
319339
}
340+
320341
c.PeerConnection = peerConnection
321342

322343
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
323-
c.trackHandler(track, receiver)
344+
c.trackHandler(track)
324345
})
325346
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
326347
c.dataChannelHandler(d)
@@ -352,10 +373,12 @@ func (c *Call) OnInvite(content *event.CallInviteEventContent) {
352373

353374
// TODO: trickle ICE for fast conn setup, rather than block here
354375
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
376+
355377
if err = peerConnection.SetLocalDescription(answer); err != nil {
356378
log.Printf("%s | failed to set local description %+v - ignoring: %s", c.UserID, offer.SDP, err)
357379
return
358380
}
381+
359382
<-gatherComplete
360383

361384
answerEvtContent := &event.Content{
@@ -387,7 +410,7 @@ func (c *Call) OnSelectAnswer(content *event.CallSelectAnswerEventContent) {
387410
}
388411
}
389412

390-
func (c *Call) OnHangup(content *event.CallHangupEventContent) {
413+
func (c *Call) OnHangup() {
391414
c.Terminate()
392415
}
393416

@@ -400,6 +423,7 @@ func (c *Call) OnCandidates(content *event.CallCandidatesEventContent) {
400423
SDPMLineIndex: &sdpMLineIndex,
401424
UsernameFragment: new(string),
402425
}
426+
403427
if err := c.PeerConnection.AddICECandidate(ice); err != nil {
404428
log.Printf("%s | failed to add ICE candidate %+v: %s", c.UserID, content, err)
405429
}
@@ -447,6 +471,7 @@ func (c *Call) sendToDevice(callType event.Type, content *event.Content) {
447471
if callType.Type != event.CallCandidates.Type {
448472
log.Printf("%s | sending to device %s", c.UserID, callType.Type)
449473
}
474+
450475
toDevice := &mautrix.ReqSendToDevice{
451476
Messages: map[id.UserID]map[id.DeviceID]*event.Content{
452477
c.UserID: {
@@ -457,7 +482,9 @@ func (c *Call) sendToDevice(callType event.Type, content *event.Content) {
457482

458483
// TODO: E2EE
459484
// TODO: to-device reliability
460-
c.Client.SendToDevice(callType, toDevice)
485+
if _, err := c.Client.SendToDevice(callType, toDevice); err != nil {
486+
log.Printf("%s | error sending to-device %s: %s", c.UserID, callType.Type, err)
487+
}
461488
}
462489

463490
func (c *Call) SendDataChannelMessage(msg event.SFUMessage) {
@@ -492,6 +519,7 @@ func (c *Call) CheckKeepAliveTimestamp() {
492519
log.Printf("%s | did not get keep-alive message in the last %s:", c.UserID, timeout)
493520
c.Hangup(event.CallHangupKeepAliveTimeout)
494521
}
522+
495523
break
496524
}
497525
}

0 commit comments

Comments
 (0)