ortc_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. //go:build !js
  4. // +build !js
  5. package webrtc
  6. import (
  7. "github.com/pion/webrtc/v3/internal/util"
  8. )
  9. type testORTCStack struct {
  10. api *API
  11. gatherer *ICEGatherer
  12. ice *ICETransport
  13. dtls *DTLSTransport
  14. sctp *SCTPTransport
  15. }
  16. func (s *testORTCStack) setSignal(sig *testORTCSignal, isOffer bool) error {
  17. iceRole := ICERoleControlled
  18. if isOffer {
  19. iceRole = ICERoleControlling
  20. }
  21. err := s.ice.SetRemoteCandidates(sig.ICECandidates)
  22. if err != nil {
  23. return err
  24. }
  25. // Start the ICE transport
  26. err = s.ice.Start(nil, sig.ICEParameters, &iceRole)
  27. if err != nil {
  28. return err
  29. }
  30. // Start the DTLS transport
  31. err = s.dtls.Start(sig.DTLSParameters)
  32. if err != nil {
  33. return err
  34. }
  35. // Start the SCTP transport
  36. err = s.sctp.Start(sig.SCTPCapabilities)
  37. if err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. func (s *testORTCStack) getSignal() (*testORTCSignal, error) {
  43. gatherFinished := make(chan struct{})
  44. s.gatherer.OnLocalCandidate(func(i *ICECandidate) {
  45. if i == nil {
  46. close(gatherFinished)
  47. }
  48. })
  49. if err := s.gatherer.Gather(); err != nil {
  50. return nil, err
  51. }
  52. <-gatherFinished
  53. iceCandidates, err := s.gatherer.GetLocalCandidates()
  54. if err != nil {
  55. return nil, err
  56. }
  57. iceParams, err := s.gatherer.GetLocalParameters()
  58. if err != nil {
  59. return nil, err
  60. }
  61. dtlsParams, err := s.dtls.GetLocalParameters()
  62. if err != nil {
  63. return nil, err
  64. }
  65. sctpCapabilities := s.sctp.GetCapabilities()
  66. return &testORTCSignal{
  67. ICECandidates: iceCandidates,
  68. ICEParameters: iceParams,
  69. DTLSParameters: dtlsParams,
  70. SCTPCapabilities: sctpCapabilities,
  71. }, nil
  72. }
  73. func (s *testORTCStack) close() error {
  74. var closeErrs []error
  75. if err := s.sctp.Stop(); err != nil {
  76. closeErrs = append(closeErrs, err)
  77. }
  78. if err := s.ice.Stop(); err != nil {
  79. closeErrs = append(closeErrs, err)
  80. }
  81. return util.FlattenErrs(closeErrs)
  82. }
  83. type testORTCSignal struct {
  84. ICECandidates []ICECandidate
  85. ICEParameters ICEParameters
  86. DTLSParameters DTLSParameters
  87. SCTPCapabilities SCTPCapabilities
  88. }
  89. func newORTCPair() (stackA *testORTCStack, stackB *testORTCStack, err error) {
  90. sa, err := newORTCStack()
  91. if err != nil {
  92. return nil, nil, err
  93. }
  94. sb, err := newORTCStack()
  95. if err != nil {
  96. return nil, nil, err
  97. }
  98. return sa, sb, nil
  99. }
  100. func newORTCStack() (*testORTCStack, error) {
  101. // Create an API object
  102. api := NewAPI()
  103. // Create the ICE gatherer
  104. gatherer, err := api.NewICEGatherer(ICEGatherOptions{})
  105. if err != nil {
  106. return nil, err
  107. }
  108. // Construct the ICE transport
  109. ice := api.NewICETransport(gatherer)
  110. // Construct the DTLS transport
  111. dtls, err := api.NewDTLSTransport(ice, nil)
  112. if err != nil {
  113. return nil, err
  114. }
  115. // Construct the SCTP transport
  116. sctp := api.NewSCTPTransport(dtls)
  117. return &testORTCStack{
  118. api: api,
  119. gatherer: gatherer,
  120. ice: ice,
  121. dtls: dtls,
  122. sctp: sctp,
  123. }, nil
  124. }
  125. func signalORTCPair(stackA *testORTCStack, stackB *testORTCStack) error {
  126. sigA, err := stackA.getSignal()
  127. if err != nil {
  128. return err
  129. }
  130. sigB, err := stackB.getSignal()
  131. if err != nil {
  132. return err
  133. }
  134. a := make(chan error)
  135. b := make(chan error)
  136. go func() {
  137. a <- stackB.setSignal(sigA, false)
  138. }()
  139. go func() {
  140. b <- stackA.setSignal(sigB, true)
  141. }()
  142. errA := <-a
  143. errB := <-b
  144. closeErrs := []error{errA, errB}
  145. return util.FlattenErrs(closeErrs)
  146. }