Skip to content
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

Fix ice candidate gather and extended candidate buffer #166

Merged
merged 1 commit into from
Feb 11, 2025

Conversation

cedricve
Copy link
Member

@cedricve cedricve commented Feb 11, 2025

Description

Pull Request Description: Fix Ice Candidate Gather and Extend Candidate Buffer

Motivation

The current implementation of the WebRTC connection in our project has shown inefficiencies and potential issues related to ICE candidate gathering and processing. Specifically, the existing unbuffered channels for ICE candidates can lead to blocking and delays, negatively impacting the connection setup process. Additionally, some redundant code and potential memory leaks have been identified, which need to be addressed to improve the stability and performance of the WebRTC connections.

Changes and Improvements

  1. Buffered ICE Candidate Channels:

    • Changed the ICE candidate channels to be buffered with a size of 100. This prevents blocking when candidates are being gathered and sent, allowing for smoother and faster connection setup.
    CandidateArrays[key] = make(chan string, 100)
    CandidateArrays[sessionKey] = make(chan string, 100)
  2. Improved ICE Candidate Handling:

    • Moved the candidate processing loop into a separate goroutine to ensure non-blocking behavior and continuous processing of incoming candidates.
    go func() {
        for candidate := range CandidateArrays[sessionKey] {
            CandidatesMutex.Lock()
            log.Log.Info("Received candidate from channel: " + candidate)
            if candidateErr := peerConnection.AddICECandidate(pionWebRTC.ICECandidateInit{Candidate: string(candidate)}); candidateErr != nil {
                log.Log.Error("Error adding candidate: " + candidateErr.Error())
            }
            CandidatesMutex.Unlock()
        }
    }()
  3. Connection Cleanup Enhancements:

    • Added deletion of peer connections from the map upon closure to prevent potential memory leaks and ensure proper cleanup.
    delete(peerConnections, handshake.SessionID)
  4. Code Cleanup:

    • Removed commented out and redundant code to enhance code readability and maintainability.
    • Removed unnecessary loop closing peer connections in WriteToTrack function, ensuring proper resource management.

Why It Improves the Project

  • Enhanced Performance: By using buffered channels and separating candidate processing into a goroutine, the changes ensure that ICE candidates are handled more efficiently, reducing connection setup times and avoiding potential blocking issues.
  • Improved Stability: Proper cleanup of peer connections and deletion from the map helps prevent memory leaks and ensures that resources are managed correctly, leading to a more stable application.
  • Code Quality: Removing redundant and commented-out code improves the readability and maintainability of the codebase, making it easier for future development and debugging.

Overall, these changes aim to enhance the performance, stability, and maintainability of the WebRTC connection handling in our project.

@cedricve cedricve merged commit 481f917 into master Feb 11, 2025
10 checks passed
@cedricve cedricve deleted the fix/candidategather-extended-buffer branch February 11, 2025 20:56
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.

1 participant