github.com/pion/webrtc/v3@v3.2.24/gathering_complete_promise.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package webrtc
     5  
     6  import (
     7  	"context"
     8  )
     9  
    10  // GatheringCompletePromise is a Pion specific helper function that returns a channel that is closed when gathering is complete.
    11  // This function may be helpful in cases where you are unable to trickle your ICE Candidates.
    12  //
    13  // It is better to not use this function, and instead trickle candidates. If you use this function you will see longer connection startup times.
    14  // When the call is connected you will see no impact however.
    15  func GatheringCompletePromise(pc *PeerConnection) (gatherComplete <-chan struct{}) {
    16  	gatheringComplete, done := context.WithCancel(context.Background())
    17  
    18  	// It's possible to miss the GatherComplete event since setGatherCompleteHandler is an atomic operation and the
    19  	// promise might have been created after the gathering is finished. Therefore, we need to check if the ICE gathering
    20  	// state has changed to complete so that we don't block the caller forever.
    21  	pc.setGatherCompleteHandler(func() { done() })
    22  	if pc.ICEGatheringState() == ICEGatheringStateComplete {
    23  		done()
    24  	}
    25  
    26  	return gatheringComplete.Done()
    27  }