gathering_complete_promise.go 1.2 KB

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