Skip to content

Commit

Permalink
fix: add SetWriteDeadline() and prevent timeout when receiving via tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jan 21, 2020
1 parent d28a87c commit a9799c7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
5 changes: 0 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import (
"gortc.io/sdp"
)

const (
_READ_TIMEOUT = 5 * time.Second
_WRITE_TIMEOUT = 5 * time.Second
)

func interleavedChannelToTrack(channel int) (int, trackFlow) {
if (channel % 2) == 0 {
return (channel / 2), _TRACK_FLOW_RTP
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import (
"regexp"
"strings"
"sync"
"time"

"gopkg.in/alecthomas/kingpin.v2"
)

var Version string = "v0.0.0"

const (
_READ_TIMEOUT = 5 * time.Second
_WRITE_TIMEOUT = 5 * time.Second
)

type trackFlow int

const (
Expand Down Expand Up @@ -124,18 +130,21 @@ func (p *program) forwardTrack(path string, id int, flow trackFlow, frame []byte
if c.path == path && c.state == _CLIENT_STATE_PLAY {
if c.streamProtocol == _STREAM_PROTOCOL_UDP {
if flow == _TRACK_FLOW_RTP {
p.rtpl.nconn.SetWriteDeadline(time.Now().Add(_WRITE_TIMEOUT))
p.rtpl.nconn.WriteTo(frame, &net.UDPAddr{
IP: c.ip,
Port: c.streamTracks[id].rtpPort,
})
} else {
p.rtcpl.nconn.SetWriteDeadline(time.Now().Add(_WRITE_TIMEOUT))
p.rtcpl.nconn.WriteTo(frame, &net.UDPAddr{
IP: c.ip,
Port: c.streamTracks[id].rtcpPort,
})
}

} else {
c.conn.NetConn().SetWriteDeadline(time.Now().Add(_WRITE_TIMEOUT))
c.conn.WriteInterleavedFrame(trackToInterleavedChannel(id, flow), frame)
}
}
Expand Down
2 changes: 1 addition & 1 deletion server_udpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (l *serverUdpListener) run() {
l.p.mutex.RLock()
defer l.p.mutex.RUnlock()

// find path and track id
// find path and track id from ip and port
path, trackId := func() (string, int) {
for _, pub := range l.p.publishers {
for i, t := range pub.streamTracks {
Expand Down

0 comments on commit a9799c7

Please sign in to comment.