Skip to content

Commit 2d12bcd

Browse files
committed
Add --timeout
1 parent 6eae428 commit 2d12bcd

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Usage of webrtc-cli:
7979
--answer enable answer mode
8080
--source string pulseaudio source or input wav file
8181
--sink string pulseaudio sink
82+
--timeout duration exit if can't connect during timeout
8283
--stun string STUN server URL (default "stun:stun.l.google.com:19302")
8384
--ports string use specific UDP port range (e.g. "3100:3200")
8485
--override-ip string override IP address in SDP offer/answer

main.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func mainWithCode() int {
3737
source := fset.String("source", "", "pulseaudio source or input wav file")
3838
sink := fset.String("sink", "", "pulseaudio sink")
3939

40+
timeout := fset.Duration("timeout", 0, "exit if can't connect during timeout")
41+
4042
stun := fset.String("stun", "stun:stun.l.google.com:19302", "STUN server URL")
4143
ports := fset.String("ports", "", "use specific UDP port range (e.g. \"3100:3200\")")
4244
overrideIP := fset.String("override-ip", "", "override IP address in SDP offer/answer")
@@ -217,17 +219,39 @@ func mainWithCode() int {
217219
}
218220
}
219221

220-
if err := snd.SetPulseBufferSize(*pulseBuf); err != nil {
221-
printErr(err)
222-
return 1
223-
}
224-
225222
sigCh := make(chan os.Signal, 2)
226223
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
227224

228-
errCh := make(chan error, 4)
225+
errCh := make(chan error, 32)
229226
eofCh := make(chan struct{})
230227

228+
go func() {
229+
var state rtc.State
230+
var timeoutCh <-chan time.Time
231+
232+
for {
233+
if state.IsConnected() {
234+
timeoutCh = nil
235+
} else if *timeout > 0 && timeoutCh == nil {
236+
timeoutCh = time.After(*timeout)
237+
}
238+
239+
select {
240+
case state = <-peer.State():
241+
printMsg("ICE connection state changed to " + state.String())
242+
243+
case <-timeoutCh:
244+
errCh <- fmt.Errorf("can't connect to remote peer during %s", *timeout)
245+
return
246+
}
247+
}
248+
}()
249+
250+
if err := snd.SetPulseBufferSize(*pulseBuf); err != nil {
251+
printErr(err)
252+
return 1
253+
}
254+
231255
if *source != "" {
232256
printMsg("Starting recording...")
233257

@@ -430,7 +454,7 @@ func printErr(err error) {
430454
}
431455

432456
func printErrMsg(s string) {
433-
printMsg("Error " + s)
457+
printMsg("Error: " + s)
434458
}
435459

436460
func printMsg(s string) {
File renamed without changes.

src/rtc/peer.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import (
1313
"gopkg.in/gavv/opus.v2"
1414
)
1515

16+
type State string
17+
18+
func (s State) String() string {
19+
return string(s)
20+
}
21+
22+
func (s State) IsConnected() bool {
23+
return s == State(webrtc.ICEConnectionStateConnected.String())
24+
}
25+
1626
type Mode int
1727

1828
const (
@@ -62,6 +72,7 @@ type Peer struct {
6272
simulateLossPerc int
6373

6474
remoteTrackCh chan struct{}
75+
connCh chan State
6576
closingCh chan struct{}
6677
closedCh chan struct{}
6778
}
@@ -71,6 +82,7 @@ func NewPeer(params Params) (*Peer, error) {
7182
channels: params.Channels,
7283
simulateLossPerc: params.SimulateLossPercent,
7384
remoteTrackCh: make(chan struct{}),
85+
connCh: make(chan State, 128),
7486
closingCh: make(chan struct{}),
7587
closedCh: make(chan struct{}),
7688
}
@@ -173,10 +185,17 @@ func NewPeer(params Params) (*Peer, error) {
173185

174186
p.conn.OnICEConnectionStateChange(
175187
func(connState webrtc.ICEConnectionState) {
176-
fmt.Fprintf(os.Stderr, "ICE connection state changed to %s\n",
177-
connState.String())
188+
select {
189+
case p.connCh <- State(connState.String()):
190+
default:
191+
select {
192+
case p.connCh <- State(connState.String()):
193+
case <-p.closingCh:
194+
}
195+
}
178196

179197
if connState == webrtc.ICEConnectionStateClosed {
198+
close(p.connCh)
180199
close(p.closedCh)
181200
}
182201
})
@@ -222,13 +241,19 @@ func NewPeer(params Params) (*Peer, error) {
222241

223242
func (p *Peer) Close() error {
224243
close(p.closingCh)
244+
225245
if err := p.conn.Close(); err != nil {
226246
return err
227247
}
248+
228249
<-p.closedCh
229250
return nil
230251
}
231252

253+
func (p *Peer) State() <-chan State {
254+
return p.connCh
255+
}
256+
232257
func (p *Peer) GetOffer() string {
233258
if p.offer == nil {
234259
return ""

0 commit comments

Comments
 (0)