-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtp-sequencer.js
More file actions
61 lines (53 loc) · 2.06 KB
/
rtp-sequencer.js
File metadata and controls
61 lines (53 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export default class RtpSequencer {
constructor() {
this.ignoreTones = false // Once speech starts, ignore future tone packets
this.highestLocalSeq = -1 // Tracks highest local sequence number so far
this.speechOffset = null // We set this when the first speech packet arrives
}
/**
* Process a single RTP packet and decide:
* - Should we drop it? (return false)
* - If not, update its sequenceNumber, return true to indicate it's ok to forward.
*
* @param {RtpPacket} rtp - RTP packet (with header.sequenceNumber, etc.)
* @param {boolean} isTone - Whether this packet is tone or speech
*
* @returns {boolean} - true if the packet should be forwarded, false if dropped
*/
process(rtp, isTone) {
// If we've already started speech, ignore new tone packets
if (isTone && this.ignoreTones) {
return false
}
// If this is the very first speech packet, switch to ignoring tones
// and compute the offset so speech won't go backward relative to tones
if (!isTone && !this.ignoreTones) {
this.ignoreTones = true
if (this.highestLocalSeq >= 0) {
console.log('RTP SEQUENCER: starting speech');
}
const originalSeq = rtp.header.sequenceNumber & 0xffff
// The first speech packet's local seq = highestLocalSeq + 1
this.speechOffset = ((this.highestLocalSeq + 1) - originalSeq) & 0xffff
}
// Map the sequence number
const originalSeq = rtp.header.sequenceNumber & 0xffff
let localSeq
if (isTone) {
// Tones use their native sequence
localSeq = originalSeq
} else {
// For speech, apply offset if set, else 0
const offset = this.speechOffset ?? 0
localSeq = (originalSeq + offset) & 0xffff
}
// Update highestLocalSeq if this is ahead
if (localSeq > this.highestLocalSeq) {
this.highestLocalSeq = localSeq
}
// Assign the mapped sequence
rtp.header.sequenceNumber = localSeq
// Return true => caller should forward the packet
return true
}
}