Skip to content

Fix DTMF deduplication to use (timestamp, event code) composite key - #796

Open
cuihang wants to merge 1 commit into
livekit:mainfrom
cuihang:fix/dtmf-dedup-event-code
Open

Fix DTMF deduplication to use (timestamp, event code) composite key#796
cuihang wants to merge 1 commit into
livekit:mainfrom
cuihang:fix/dtmf-dedup-event-code

Conversation

@cuihang

@cuihang cuihang commented Aug 16, 2026

Copy link
Copy Markdown

Problem

The DTMF handler in dtmfHandler (media_port.go) deduplicated incoming DTMF packets using only the RTP timestamp:

if h.Timestamp == p.lastDTMFTimestamp.Load() {
    return nil
}

RFC 4733 requires all packets of a given digit to share identical timestamps, so this correctly filters redundant packets within one digit. However, some SIP devices or carriers reuse the timestamp of the previous digit when sending the next digit. When this happens, the next digit is incorrectly filtered out and never reported.

Fix

Replace the timestamp-only dedup with a (timestamp, event code) composite key:

ev, err := dtmf.Decode(payload)
if err != nil {
    return nil
}
eventID := uint64(h.Timestamp)<<8 | uint64(ev.Code)
if eventID == p.lastDTMFEvent.Load() {
    return nil
}
p.lastDTMFEvent.Store(eventID)
fnc(ev)

Behavior

Scenario Before After
Same digit, redundant packets (same ts + same code) Deduped ✓ Deduped ✓
Different digits sharing timestamp (same ts + different code, e.g. *, 0, 1) Incorrectly dropped Each reported ✓
No dependency on RTP marker bit

Known limitation

If an upstream sends two identical digits (e.g. two 0s) with the exact same timestamp and event code, the (timestamp, event code) key alone cannot distinguish them. Resolving that case requires full DTMF lifecycle state tracking (marker bit, End bit, duration reset, sequence number gaps). That is a more complex enhancement beyond this fix; in the worst case (all fields identical) the receiver cannot distinguish the digits at all.

Changes

  • pkg/sip/media_port.go: Replace lastDTMFTimestamp atomic.Uint32 with lastDTMFEvent atomic.Uint64; update dtmfHandler to decode first, then dedup on composite key.
  • pkg/sip/media_port_test.go: Update field reference; add TestMediaPortDTMFSameTimestamp verifying two different digits (0 and 1) sharing the same RTP timestamp are both reported.

Test plan

  • TestMediaPortDTMF — all 12 existing subtests pass (digits 1/12/123 × loss none/first/last/middle)
  • TestMediaPortDTMFSameTimestamp — new test passes: digits 0 and 1 with same timestamp → both reported as 01

@cuihang
cuihang requested a review from a team as a code owner August 16, 2026 18:12
@CLAassistant

CLAassistant commented Aug 16, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

@codecov

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.69%. Comparing base (0460b40) to head (135a5fc).
⚠️ Report is 348 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #796      +/-   ##
==========================================
+ Coverage   65.25%   66.69%   +1.44%     
==========================================
  Files          51       41      -10     
  Lines        6588     8096    +1508     
==========================================
+ Hits         4299     5400    +1101     
- Misses       1915     2209     +294     
- Partials      374      487     +113     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The DTMF handler previously deduplicated packets using only the RTP
timestamp. Per RFC 4733, all packets of a given digit share the same
timestamp, so this correctly filters redundant packets within one digit.
However, some SIP devices or carriers reuse the previous digit's
timestamp for the next digit, causing legitimate digits to be silently
dropped.

Replace the timestamp-only dedup with a (timestamp, event code) composite
key so that:
- Same timestamp + same event code: redundant packets still deduped
- Same timestamp + different event code (e.g. *, 0, 1): each digit
  reported individually
- No dependency on the RTP marker bit, avoiding missed digits on loss

Add TestMediaPortDTMFSameTimestamp covering the scenario where two
different digits share the same RTP timestamp.
@cuihang
cuihang force-pushed the fix/dtmf-dedup-event-code branch from 94cae6a to 135a5fc Compare August 16, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants