-
Notifications
You must be signed in to change notification settings - Fork 19
feat(opus): support DTX, FEC and PLC concealment #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package opus | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/livekit/media-sdk" | ||
| "github.com/livekit/media-sdk/jitter" | ||
| "github.com/livekit/media-sdk/rtp" | ||
| "github.com/livekit/protocol/logger" | ||
| ) | ||
|
|
||
| const ( | ||
| opusJitterMaxLatency = 60 * time.Millisecond | ||
| opusDTXFrameLength = 1 | ||
| ) | ||
|
|
||
| func HandleOpusJitter(h rtp.Handler, pcmWriter media.PCM16Writer, targetChannels int) rtp.Handler { | ||
| handler := &opusJitterHandler{ | ||
| h: h, | ||
| err: make(chan error, 1), | ||
| logger: logger.GetLogger(), | ||
| } | ||
|
|
||
| dec, err := Decode(pcmWriter, targetChannels, handler.logger) | ||
| if err != nil { | ||
| handler.err <- err | ||
| return handler | ||
| } | ||
| handler.decoder = dec.(*decoder) | ||
|
|
||
| handler.buf = jitter.NewBuffer( | ||
| rtp.AudioDepacketizer{}, | ||
| opusJitterMaxLatency, | ||
| func(packets []*rtp.Packet) { | ||
| for _, p := range packets { | ||
| handler.handleRTP(p) | ||
| } | ||
| }, | ||
| jitter.WithPacketLossHandler(func() { | ||
| handler.pendingLoss = true | ||
| }), | ||
| ) | ||
|
|
||
| return handler | ||
| } | ||
|
|
||
| type opusJitterHandler struct { | ||
| h rtp.Handler | ||
| buf *jitter.Buffer | ||
| decoder *decoder | ||
| err chan error | ||
| logger logger.Logger | ||
| nextPacket *rtp.Packet | ||
| lastPacket *rtp.Packet | ||
| pendingLoss bool | ||
| } | ||
|
|
||
| func (r *opusJitterHandler) String() string { | ||
| return "OpusJitter -> " + r.h.String() | ||
| } | ||
|
|
||
| func (r *opusJitterHandler) HandleRTP(h *rtp.Header, payload []byte) error { | ||
| r.buf.Push(&rtp.Packet{Header: *h, Payload: payload}) | ||
| select { | ||
| case err := <-r.err: | ||
| return err | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func (r *opusJitterHandler) handleRTP(p *rtp.Packet) { | ||
| isDtx := len(p.Payload) == opusDTXFrameLength | ||
|
|
||
| // Not sure what to do if we have a pending loss and the packet is DTX. | ||
| if r.pendingLoss && !isDtx { | ||
| // Store the next packet for FEC | ||
| r.nextPacket = p | ||
| r.handlePacketLoss() | ||
| r.pendingLoss = false | ||
| } | ||
|
|
||
| if r.lastPacket != nil && (isDtx || len(r.lastPacket.Payload) == opusDTXFrameLength) { | ||
| silenceSamples := int(p.Timestamp - r.lastPacket.Timestamp) | ||
| if silenceSamples > 0 { | ||
| silenceBuf := make([]int16, silenceSamples*r.decoder.targetChannels) | ||
| if err := r.decoder.w.WriteSample(silenceBuf); err != nil { | ||
| r.logger.Warnw("failed to write silence", err) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the decoder need to be filled with silence samples. That does not sound good. The decoder should have updated with PLC/FEC samples and should not ideally need filling with silence.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is directly writing to the PCMWriter that the decoder writes to after decoding |
||
| } | ||
|
|
||
| if isDtx { | ||
| r.lastPacket = p | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if err := r.decoder.WriteSample(p.Payload); err != nil { | ||
| r.logger.Warnw("failed to decode packet", err) | ||
| } | ||
|
|
||
| r.lastPacket = p | ||
| } | ||
|
|
||
| func (r *opusJitterHandler) handlePacketLoss() { | ||
| if r.decoder == nil || r.nextPacket == nil { | ||
| return | ||
| } | ||
|
|
||
| lostPackets := int(r.nextPacket.SequenceNumber - r.buf.LastSequenceNumber() - 1) | ||
| if lostPackets <= 0 { | ||
| return | ||
| } | ||
|
|
||
| lastTs := r.buf.LastTimestamp() | ||
| nextTs := r.nextPacket.Timestamp | ||
|
|
||
| totalSamples := int(nextTs - lastTs) | ||
| if totalSamples <= 0 { | ||
| return | ||
| } | ||
|
|
||
| samplesPerPacket := totalSamples / lostPackets | ||
|
|
||
| if lostPackets > 1 { | ||
| // For mono audio, if we call DecodePLC right after a | ||
| // SFU generated mono silence, the concealment might not be proper. | ||
| // But, we need to pass the buffer for the exact duration of the lost audio. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not able to understand this comment. Ideally, there should be no dependence on what SFU does. Ideally, this should not even know that it is interacting with SFU. It is just getting Opus packets and needs to deal with it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically:
Actually worth checking how libwebrtc handles this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is okay, if the lost packet is before the mono silence, it should not be concealed post that. Concealment should happen when it is detected. Again, pull model will make this simpler. |
||
| plcSamples := samplesPerPacket * (lostPackets - 1) * r.decoder.lastChannels | ||
| buf := make([]int16, plcSamples) | ||
|
|
||
| err := r.decoder.DecodePLC(buf) | ||
| if err != nil { | ||
| r.logger.Warnw("failed to recover lost packets with PLC", err) | ||
| return | ||
| } | ||
| _ = r.decoder.w.WriteSample(buf) | ||
| } | ||
|
|
||
| // Should we reset for the next packet before calling DecodeFEC? | ||
| // This will update the decoder's state for the next packet so it might help. | ||
| // But, it might also cause some issues if the next packet is SFU generated silence. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should make this simple. Ideally, there is a pull and if there is data, it gets decoded, if not silence. So, maybe we need two more changes here
In this push mechanism, what happens if there is a burst loss of 300 ms? There will be no handling till the next packet arrives. At that time, it will do a large conceal it looks like.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The entire pipeline is in push mode right now with the writer design. In the current implementation (regular jitter buffer, no concealment), it is push as well.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think we may have to build a pull mode to make this cleaner. Otherwise, doing concealment after a burst loss is already too late. The time has passsed for playing out the missing audio.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point, initially I was doing only FEC and it needed the next packet, so I had this approach. Then, I realized if multiple packets are lost, then it can only recover the last one with FEC, n-1 packets needed PLC. But yea currently, it does a large conceal when the next valid packet is popped from the jitter buffer. |
||
| channels, err := r.decoder.resetForSample(r.nextPacket.Payload) | ||
| if err != nil { | ||
| r.logger.Warnw("failed to reset decoder for FEC", err) | ||
| return | ||
| } | ||
|
|
||
| buf := make([]int16, samplesPerPacket*channels) | ||
| err = r.decoder.DecodeFEC(r.nextPacket.Payload, buf) | ||
| if err != nil { | ||
| r.logger.Warnw("failed to recover last lost packet with FEC", err) | ||
| return | ||
| } | ||
| _ = r.decoder.w.WriteSample(buf) | ||
| } | ||
|
|
||
| func (r *opusJitterHandler) Close() error { | ||
| if r.decoder != nil { | ||
| return r.decoder.Close() | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to use RTP timestamp irrespective of DTX or not. DTX packet also should have an RTP Timestamp.
But, this interface may be tricky. How does it work now? Is this pushing packets to the app or the app pulling? Ideally, the app should be pulling every
x msand this code should check for available data and if nothing available should use plc/fec as applicable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is currently pushing to the app, jitter buffer does it's thing, the handler identifies the gaps and fills them with silence/FEC/PLC and writes to the application.
We're using the writer design SDK side, so it's all pushing to the app instead of the app polling it