Skip to content

Commit

Permalink
correct webrtc audio buffer duration
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricve committed Jan 14, 2024
1 parent 14d38ec commit 803e8f5
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions machinery/src/webrtc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ func WriteToTrack(livestreamCursor *packets.QueueCursor, configuration *models.C

var cursorError error
var pkt packets.Packet
var previousTime time.Duration
var previousTimeVideo time.Duration
var previousTimeAudio time.Duration

start := false
receivedKeyFrame := false
Expand All @@ -336,8 +337,6 @@ func WriteToTrack(livestreamCursor *packets.QueueCursor, configuration *models.C
for cursorError == nil {

pkt, cursorError = livestreamCursor.ReadPacket()
bufferDuration := pkt.Time - previousTime
previousTime = pkt.Time

if config.Capture.ForwardWebRTC != "true" && peerConnectionCount == 0 {
start = false
Expand Down Expand Up @@ -385,6 +384,11 @@ func WriteToTrack(livestreamCursor *packets.QueueCursor, configuration *models.C
//}

if pkt.IsVideo {

// Calculate the difference
bufferDuration := pkt.Time - previousTimeVideo
previousTimeVideo = pkt.Time

// Start at the first keyframe
if pkt.IsKeyFrame {
start = true
Expand All @@ -401,8 +405,13 @@ func WriteToTrack(livestreamCursor *packets.QueueCursor, configuration *models.C
}
}
} else if pkt.IsAudio {

// Calculate the difference
bufferDuration := pkt.Time - previousTimeAudio
previousTimeAudio = pkt.Time

// We will send the audio
sample := pionMedia.Sample{Data: pkt.Data, Duration: pkt.Time}
sample := pionMedia.Sample{Data: pkt.Data, Duration: bufferDuration}
if err := audioTrack.WriteSample(sample); err != nil && err != io.ErrClosedPipe {
log.Log.Error("webrtc.main.WriteToTrack(): something went wrong while writing sample: " + err.Error())
}
Expand Down

0 comments on commit 803e8f5

Please sign in to comment.