gathering_complete_promise.go 1.1 KB

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