peer_scenario_client.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef TEST_PEER_SCENARIO_PEER_SCENARIO_CLIENT_H_
  11. #define TEST_PEER_SCENARIO_PEER_SCENARIO_CLIENT_H_
  12. #include <functional>
  13. #include <list>
  14. #include <map>
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "absl/memory/memory.h"
  19. #include "api/peer_connection_interface.h"
  20. #include "api/test/network_emulation_manager.h"
  21. #include "api/test/time_controller.h"
  22. #include "pc/test/frame_generator_capturer_video_track_source.h"
  23. #include "test/logging/log_writer.h"
  24. namespace webrtc {
  25. namespace test {
  26. // Wrapper for a PeerConnection for use in PeerScenario tests. It's intended to
  27. // be a minimal wrapper for a peer connection that's simple to use in testing.
  28. // In particular the constructor hides a lot of the required setup for a peer
  29. // connection.
  30. class PeerScenarioClient {
  31. public:
  32. struct CallbackHandlers {
  33. std::vector<std::function<void(PeerConnectionInterface::SignalingState)>>
  34. on_signaling_change;
  35. std::vector<std::function<void(rtc::scoped_refptr<DataChannelInterface>)>>
  36. on_data_channel;
  37. std::vector<std::function<void()>> on_renegotiation_needed;
  38. std::vector<
  39. std::function<void(PeerConnectionInterface::IceConnectionState)>>
  40. on_standardized_ice_connection_change;
  41. std::vector<
  42. std::function<void(PeerConnectionInterface::PeerConnectionState)>>
  43. on_connection_change;
  44. std::vector<std::function<void(PeerConnectionInterface::IceGatheringState)>>
  45. on_ice_gathering_change;
  46. std::vector<std::function<void(const IceCandidateInterface*)>>
  47. on_ice_candidate;
  48. std::vector<std::function<void(const std::string&,
  49. int,
  50. const std::string&,
  51. int,
  52. const std::string&)>>
  53. on_ice_candidate_error;
  54. std::vector<std::function<void(const std::vector<cricket::Candidate>&)>>
  55. on_ice_candidates_removed;
  56. std::vector<std::function<void(
  57. rtc::scoped_refptr<RtpReceiverInterface>,
  58. const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&)>>
  59. on_add_track;
  60. std::vector<
  61. std::function<void(rtc::scoped_refptr<RtpTransceiverInterface>)>>
  62. on_track;
  63. std::vector<std::function<void(rtc::scoped_refptr<RtpReceiverInterface>)>>
  64. on_remove_track;
  65. };
  66. struct Config {
  67. // WebRTC only support one audio device that is setup up on construction, so
  68. // we provide the audio generator configuration here rather than on creation
  69. // of the tracks. This is unlike video, where multiple capture sources can
  70. // be used at the same time.
  71. struct AudioSource {
  72. int sample_rate = 48000;
  73. int channels = 1;
  74. struct PulsedNoise {
  75. double amplitude = 0.1;
  76. };
  77. absl::optional<PulsedNoise> pulsed_noise = PulsedNoise();
  78. } audio;
  79. struct Video {
  80. bool use_fake_codecs = false;
  81. } video;
  82. // The created endpoints can be accessed using the map key as |index| in
  83. // PeerScenarioClient::endpoint(index).
  84. std::map<int, EmulatedEndpointConfig> endpoints = {
  85. {0, EmulatedEndpointConfig()}};
  86. CallbackHandlers handlers;
  87. PeerConnectionInterface::RTCConfiguration rtc_config;
  88. bool disable_encryption = false;
  89. Config() { rtc_config.sdp_semantics = SdpSemantics::kUnifiedPlan; }
  90. };
  91. struct VideoSendTrackConfig {
  92. FrameGeneratorCapturerConfig generator;
  93. bool screencast = false;
  94. };
  95. struct AudioSendTrack {
  96. rtc::scoped_refptr<AudioTrackInterface> track;
  97. rtc::scoped_refptr<RtpSenderInterface> sender;
  98. };
  99. struct VideoSendTrack {
  100. FrameGeneratorCapturer* capturer;
  101. FrameGeneratorCapturerVideoTrackSource* source;
  102. VideoTrackInterface* track;
  103. RtpSenderInterface* sender;
  104. };
  105. PeerScenarioClient(
  106. NetworkEmulationManager* net,
  107. rtc::Thread* signaling_thread,
  108. std::unique_ptr<LogWriterFactoryInterface> log_writer_factory,
  109. Config config);
  110. PeerConnectionFactoryInterface* factory() { return pc_factory_.get(); }
  111. PeerConnectionInterface* pc() {
  112. RTC_DCHECK_RUN_ON(signaling_thread_);
  113. return peer_connection_.get();
  114. }
  115. rtc::Thread* thread() { return signaling_thread_; }
  116. Clock* clock() { return Clock::GetRealTimeClock(); }
  117. // Returns the endpoint created from the EmulatedEndpointConfig with the same
  118. // index in PeerScenarioClient::config.
  119. EmulatedEndpoint* endpoint(int index = 0);
  120. AudioSendTrack CreateAudio(std::string track_id,
  121. cricket::AudioOptions options);
  122. VideoSendTrack CreateVideo(std::string track_id, VideoSendTrackConfig config);
  123. void AddVideoReceiveSink(std::string track_id,
  124. rtc::VideoSinkInterface<VideoFrame>* video_sink);
  125. CallbackHandlers* handlers() { return &handlers_; }
  126. // The |munge_offer| function can be used to munge the SDP, i.e. modify a
  127. // local description afer creating it but before setting it. Note that this is
  128. // legacy behavior. It's added here only to be able to have test coverage for
  129. // scenarios even if they are not spec compliant.
  130. void CreateAndSetSdp(
  131. std::function<void(SessionDescriptionInterface*)> munge_offer,
  132. std::function<void(std::string)> offer_handler);
  133. void SetSdpOfferAndGetAnswer(std::string remote_offer,
  134. std::function<void(std::string)> answer_handler);
  135. void SetSdpAnswer(
  136. std::string remote_answer,
  137. std::function<void(const SessionDescriptionInterface& answer)>
  138. done_handler);
  139. // Adds the given ice candidate when the peer connection is ready.
  140. void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate);
  141. private:
  142. const std::map<int, EmulatedEndpoint*> endpoints_;
  143. TaskQueueFactory* const task_queue_factory_;
  144. rtc::Thread* const signaling_thread_;
  145. const std::unique_ptr<LogWriterFactoryInterface> log_writer_factory_;
  146. const std::unique_ptr<rtc::Thread> worker_thread_;
  147. CallbackHandlers handlers_ RTC_GUARDED_BY(signaling_thread_);
  148. const std::unique_ptr<PeerConnectionObserver> observer_;
  149. std::map<std::string, std::vector<rtc::VideoSinkInterface<VideoFrame>*>>
  150. track_id_to_video_sinks_ RTC_GUARDED_BY(signaling_thread_);
  151. std::list<std::unique_ptr<IceCandidateInterface>> pending_ice_candidates_
  152. RTC_GUARDED_BY(signaling_thread_);
  153. rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
  154. rtc::scoped_refptr<PeerConnectionInterface> peer_connection_
  155. RTC_GUARDED_BY(signaling_thread_);
  156. };
  157. } // namespace test
  158. } // namespace webrtc
  159. #endif // TEST_PEER_SCENARIO_PEER_SCENARIO_CLIENT_H_