Skip to content

Commit 944d693

Browse files
committed
fix: rtp DTMF reader was broken #31
1 parent 55bda19 commit 944d693

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

media/rtp_dtmf_reader.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ func (w *RTPDtmfReader) processDTMFEvent(ev DTMFEvent) {
6060
// Expensive call on logger
6161
log.Debug().Interface("ev", ev).Msg("Processing DTMF event")
6262
}
63-
if ev.EndOfEvent && w.lastEv.Duration > 0 {
63+
if ev.EndOfEvent {
64+
if w.lastEv.Duration == 0 {
65+
return
66+
}
6467
// Does this match to our last ev
6568
// Consider Event can be 0, that is why we check is also lastEv.Duration set
6669
if w.lastEv.Event != ev.Event {
@@ -78,7 +81,7 @@ func (w *RTPDtmfReader) processDTMFEvent(ev DTMFEvent) {
7881
w.lastEv = DTMFEvent{}
7982
return
8083
}
81-
if w.lastEv.Event == ev.Event {
84+
if w.lastEv.Duration > 0 && w.lastEv.Event == ev.Event {
8285
return
8386
}
8487
w.lastEv = ev

media/rtp_dtmf_reader_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,43 @@ func TestDTMFReader(t *testing.T) {
4646

4747
assert.Equal(t, "109", detected.String())
4848
}
49+
50+
func TestDTMFReaderRepeated(t *testing.T) {
51+
r := RTPDtmfReader{}
52+
53+
// DTMF 109
54+
sequence := []DTMFEvent{
55+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 160},
56+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 320},
57+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 480},
58+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 640},
59+
{Event: 1, EndOfEvent: true, Volume: 10, Duration: 800},
60+
{Event: 1, EndOfEvent: true, Volume: 10, Duration: 800},
61+
{Event: 1, EndOfEvent: true, Volume: 10, Duration: 800},
62+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 160},
63+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 320},
64+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 480},
65+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 640},
66+
{Event: 1, EndOfEvent: true, Volume: 10, Duration: 800},
67+
{Event: 1, EndOfEvent: true, Volume: 10, Duration: 800},
68+
{Event: 1, EndOfEvent: true, Volume: 10, Duration: 800},
69+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 160},
70+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 320},
71+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 480},
72+
{Event: 1, EndOfEvent: false, Volume: 10, Duration: 640},
73+
{Event: 1, EndOfEvent: true, Volume: 10, Duration: 800},
74+
{Event: 1, EndOfEvent: true, Volume: 10, Duration: 800},
75+
{Event: 1, EndOfEvent: true, Volume: 10, Duration: 800},
76+
}
77+
78+
detected := strings.Builder{}
79+
for _, ev := range sequence {
80+
r.processDTMFEvent(ev)
81+
dtmf, set := r.ReadDTMF()
82+
if set {
83+
detected.WriteRune(dtmf)
84+
}
85+
}
86+
87+
assert.Equal(t, "111", detected.String())
88+
}

0 commit comments

Comments
 (0)