You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "test/pc/e2e/test_peer.h"
  11. #include <utility>
  12. #include "absl/memory/memory.h"
  13. #include "absl/types/optional.h"
  14. #include "api/rtc_event_log/rtc_event_log_factory.h"
  15. #include "api/scoped_refptr.h"
  16. #include "api/task_queue/default_task_queue_factory.h"
  17. #include "api/task_queue/task_queue_factory.h"
  18. #include "api/video_codecs/builtin_video_decoder_factory.h"
  19. #include "api/video_codecs/builtin_video_encoder_factory.h"
  20. #include "media/engine/webrtc_media_engine.h"
  21. #include "media/engine/webrtc_media_engine_defaults.h"
  22. #include "modules/audio_device/include/audio_device.h"
  23. #include "modules/audio_processing/aec_dump/aec_dump_factory.h"
  24. #include "modules/audio_processing/include/audio_processing.h"
  25. #include "p2p/client/basic_port_allocator.h"
  26. #include "rtc_base/location.h"
  27. #include "test/pc/e2e/echo/echo_emulation.h"
  28. #include "test/testsupport/copy_to_file_audio_capturer.h"
  29. namespace webrtc {
  30. namespace webrtc_pc_e2e {
  31. namespace {
  32. using RemotePeerAudioConfig =
  33. ::webrtc::webrtc_pc_e2e::TestPeer::RemotePeerAudioConfig;
  34. using AudioConfig =
  35. ::webrtc::webrtc_pc_e2e::PeerConnectionE2EQualityTestFixture::AudioConfig;
  36. using EchoEmulationConfig = ::webrtc::webrtc_pc_e2e::
  37. PeerConnectionE2EQualityTestFixture::EchoEmulationConfig;
  38. constexpr int16_t kGeneratedAudioMaxAmplitude = 32000;
  39. constexpr int kDefaultSamplingFrequencyInHz = 48000;
  40. // Sets mandatory entities in injectable components like |pcf_dependencies|
  41. // and |pc_dependencies| if they are omitted. Also setup required
  42. // dependencies, that won't be specially provided by factory and will be just
  43. // transferred to peer connection creation code.
  44. void SetMandatoryEntities(InjectableComponents* components) {
  45. RTC_DCHECK(components->pcf_dependencies);
  46. RTC_DCHECK(components->pc_dependencies);
  47. // Setup required peer connection factory dependencies.
  48. if (components->pcf_dependencies->task_queue_factory == nullptr) {
  49. components->pcf_dependencies->task_queue_factory =
  50. CreateDefaultTaskQueueFactory();
  51. }
  52. if (components->pcf_dependencies->call_factory == nullptr) {
  53. components->pcf_dependencies->call_factory = webrtc::CreateCallFactory();
  54. }
  55. if (components->pcf_dependencies->event_log_factory == nullptr) {
  56. components->pcf_dependencies->event_log_factory =
  57. std::make_unique<RtcEventLogFactory>(
  58. components->pcf_dependencies->task_queue_factory.get());
  59. }
  60. }
  61. class TestPeerComponents {
  62. public:
  63. TestPeerComponents(std::unique_ptr<InjectableComponents> components,
  64. const Params& params,
  65. MockPeerConnectionObserver* observer,
  66. VideoQualityAnalyzerInjectionHelper* video_analyzer_helper,
  67. rtc::Thread* signaling_thread,
  68. absl::optional<RemotePeerAudioConfig> remote_audio_config,
  69. double bitrate_multiplier,
  70. absl::optional<EchoEmulationConfig> echo_emulation_config,
  71. rtc::TaskQueue* task_queue)
  72. : audio_config_opt_(params.audio_config),
  73. observer_(observer),
  74. video_analyzer_helper_(video_analyzer_helper),
  75. signaling_thread_(signaling_thread),
  76. remote_audio_config_(std::move(remote_audio_config)),
  77. bitrate_multiplier_(bitrate_multiplier),
  78. echo_emulation_config_(std::move(echo_emulation_config)) {
  79. for (auto& video_config : params.video_configs) {
  80. // Stream label should be set by fixture implementation here.
  81. RTC_DCHECK(video_config.stream_label);
  82. bool res =
  83. stream_required_spatial_index_
  84. .insert({*video_config.stream_label,
  85. video_config.simulcast_config
  86. ? absl::optional<int>(video_config.simulcast_config
  87. ->target_spatial_index)
  88. : absl::nullopt})
  89. .second;
  90. RTC_DCHECK(res) << "Duplicate video_config.stream_label="
  91. << *video_config.stream_label;
  92. }
  93. // Create audio processing, that will be used to create media engine that
  94. // then will be added into peer connection. See CreateMediaEngine(...).
  95. audio_processing_ = webrtc::AudioProcessingBuilder().Create();
  96. if (params.aec_dump_path) {
  97. audio_processing_->AttachAecDump(
  98. AecDumpFactory::Create(*params.aec_dump_path, -1, task_queue));
  99. }
  100. // Create peer connection factory.
  101. PeerConnectionFactoryDependencies pcf_deps = CreatePCFDependencies(
  102. std::move(components->pcf_dependencies), components->network_thread);
  103. peer_connection_factory_ =
  104. CreateModularPeerConnectionFactory(std::move(pcf_deps));
  105. // Create peer connection.
  106. PeerConnectionDependencies pc_deps =
  107. CreatePCDependencies(std::move(components->pc_dependencies));
  108. peer_connection_ = peer_connection_factory_->CreatePeerConnection(
  109. params.rtc_configuration, std::move(pc_deps));
  110. peer_connection_->SetBitrate(params.bitrate_params);
  111. }
  112. rtc::scoped_refptr<PeerConnectionFactoryInterface> peer_connection_factory()
  113. const {
  114. return peer_connection_factory_;
  115. }
  116. rtc::scoped_refptr<PeerConnectionInterface> peer_connection() const {
  117. return peer_connection_;
  118. }
  119. rtc::scoped_refptr<AudioProcessing> audio_processing() const {
  120. return audio_processing_;
  121. }
  122. private:
  123. // Creates PeerConnectionFactoryDependencies objects, providing entities
  124. // from InjectableComponents::PeerConnectionFactoryComponents and also
  125. // creating entities, that are required for correct injection of media quality
  126. // analyzers.
  127. PeerConnectionFactoryDependencies CreatePCFDependencies(
  128. std::unique_ptr<PeerConnectionFactoryComponents> pcf_dependencies,
  129. rtc::Thread* network_thread) {
  130. PeerConnectionFactoryDependencies pcf_deps;
  131. pcf_deps.network_thread = network_thread;
  132. pcf_deps.signaling_thread = signaling_thread_;
  133. pcf_deps.media_engine = CreateMediaEngine(pcf_dependencies.get());
  134. pcf_deps.call_factory = std::move(pcf_dependencies->call_factory);
  135. pcf_deps.event_log_factory = std::move(pcf_dependencies->event_log_factory);
  136. pcf_deps.task_queue_factory =
  137. std::move(pcf_dependencies->task_queue_factory);
  138. if (pcf_dependencies->fec_controller_factory != nullptr) {
  139. pcf_deps.fec_controller_factory =
  140. std::move(pcf_dependencies->fec_controller_factory);
  141. }
  142. if (pcf_dependencies->network_controller_factory != nullptr) {
  143. pcf_deps.network_controller_factory =
  144. std::move(pcf_dependencies->network_controller_factory);
  145. }
  146. if (pcf_dependencies->media_transport_factory != nullptr) {
  147. pcf_deps.media_transport_factory =
  148. std::move(pcf_dependencies->media_transport_factory);
  149. }
  150. return pcf_deps;
  151. }
  152. std::unique_ptr<cricket::MediaEngineInterface> CreateMediaEngine(
  153. PeerConnectionFactoryComponents* pcf_dependencies) {
  154. cricket::MediaEngineDependencies media_deps;
  155. media_deps.task_queue_factory = pcf_dependencies->task_queue_factory.get();
  156. media_deps.adm = CreateAudioDeviceModule(media_deps.task_queue_factory);
  157. media_deps.audio_processing = audio_processing_;
  158. media_deps.video_encoder_factory =
  159. CreateVideoEncoderFactory(pcf_dependencies);
  160. media_deps.video_decoder_factory =
  161. CreateVideoDecoderFactory(pcf_dependencies);
  162. webrtc::SetMediaEngineDefaults(&media_deps);
  163. return cricket::CreateMediaEngine(std::move(media_deps));
  164. }
  165. rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceModule(
  166. TaskQueueFactory* task_queue_factory) {
  167. std::unique_ptr<TestAudioDeviceModule::Renderer> renderer =
  168. CreateAudioRenderer(remote_audio_config_);
  169. std::unique_ptr<TestAudioDeviceModule::Capturer> capturer =
  170. CreateAudioCapturer(audio_config_opt_);
  171. RTC_DCHECK(renderer);
  172. RTC_DCHECK(capturer);
  173. // Setup echo emulation if required.
  174. if (echo_emulation_config_) {
  175. capturer = std::make_unique<EchoEmulatingCapturer>(
  176. std::move(capturer), *echo_emulation_config_);
  177. renderer = std::make_unique<EchoEmulatingRenderer>(
  178. std::move(renderer),
  179. static_cast<EchoEmulatingCapturer*>(capturer.get()));
  180. }
  181. // Setup input stream dumping if required.
  182. if (audio_config_opt_ && audio_config_opt_->input_dump_file_name) {
  183. capturer = std::make_unique<test::CopyToFileAudioCapturer>(
  184. std::move(capturer), audio_config_opt_->input_dump_file_name.value());
  185. }
  186. return TestAudioDeviceModule::Create(task_queue_factory,
  187. std::move(capturer),
  188. std::move(renderer), /*speed=*/1.f);
  189. }
  190. std::unique_ptr<TestAudioDeviceModule::Renderer> CreateAudioRenderer(
  191. const absl::optional<RemotePeerAudioConfig>& config) {
  192. if (!config) {
  193. // Return default renderer because we always require some renderer.
  194. return TestAudioDeviceModule::CreateDiscardRenderer(
  195. kDefaultSamplingFrequencyInHz);
  196. }
  197. if (config->output_file_name) {
  198. return TestAudioDeviceModule::CreateBoundedWavFileWriter(
  199. config->output_file_name.value(), config->sampling_frequency_in_hz);
  200. }
  201. return TestAudioDeviceModule::CreateDiscardRenderer(
  202. config->sampling_frequency_in_hz);
  203. }
  204. std::unique_ptr<TestAudioDeviceModule::Capturer> CreateAudioCapturer(
  205. const absl::optional<AudioConfig>& audio_config) {
  206. if (!audio_config) {
  207. // If we have no audio config we still need to provide some audio device.
  208. // In such case use generated capturer. Despite of we provided audio here,
  209. // in test media setup audio stream won't be added into peer connection.
  210. return TestAudioDeviceModule::CreatePulsedNoiseCapturer(
  211. kGeneratedAudioMaxAmplitude, kDefaultSamplingFrequencyInHz);
  212. }
  213. switch (audio_config->mode) {
  214. case AudioConfig::Mode::kGenerated:
  215. return TestAudioDeviceModule::CreatePulsedNoiseCapturer(
  216. kGeneratedAudioMaxAmplitude,
  217. audio_config->sampling_frequency_in_hz);
  218. case AudioConfig::Mode::kFile:
  219. RTC_DCHECK(audio_config->input_file_name);
  220. return TestAudioDeviceModule::CreateWavFileReader(
  221. audio_config->input_file_name.value(), /*repeat=*/true);
  222. }
  223. }
  224. std::unique_ptr<VideoEncoderFactory> CreateVideoEncoderFactory(
  225. PeerConnectionFactoryComponents* pcf_dependencies) {
  226. std::unique_ptr<VideoEncoderFactory> video_encoder_factory;
  227. if (pcf_dependencies->video_encoder_factory != nullptr) {
  228. video_encoder_factory =
  229. std::move(pcf_dependencies->video_encoder_factory);
  230. } else {
  231. video_encoder_factory = CreateBuiltinVideoEncoderFactory();
  232. }
  233. return video_analyzer_helper_->WrapVideoEncoderFactory(
  234. std::move(video_encoder_factory), bitrate_multiplier_,
  235. stream_required_spatial_index_);
  236. }
  237. std::unique_ptr<VideoDecoderFactory> CreateVideoDecoderFactory(
  238. PeerConnectionFactoryComponents* pcf_dependencies) {
  239. std::unique_ptr<VideoDecoderFactory> video_decoder_factory;
  240. if (pcf_dependencies->video_decoder_factory != nullptr) {
  241. video_decoder_factory =
  242. std::move(pcf_dependencies->video_decoder_factory);
  243. } else {
  244. video_decoder_factory = CreateBuiltinVideoDecoderFactory();
  245. }
  246. return video_analyzer_helper_->WrapVideoDecoderFactory(
  247. std::move(video_decoder_factory));
  248. }
  249. // Creates PeerConnectionDependencies objects, providing entities
  250. // from InjectableComponents::PeerConnectionComponents.
  251. PeerConnectionDependencies CreatePCDependencies(
  252. std::unique_ptr<PeerConnectionComponents> pc_dependencies) {
  253. PeerConnectionDependencies pc_deps(observer_);
  254. auto port_allocator = std::make_unique<cricket::BasicPortAllocator>(
  255. pc_dependencies->network_manager);
  256. // This test does not support TCP
  257. int flags = cricket::PORTALLOCATOR_DISABLE_TCP;
  258. port_allocator->set_flags(port_allocator->flags() | flags);
  259. pc_deps.allocator = std::move(port_allocator);
  260. if (pc_dependencies->async_resolver_factory != nullptr) {
  261. pc_deps.async_resolver_factory =
  262. std::move(pc_dependencies->async_resolver_factory);
  263. }
  264. if (pc_dependencies->cert_generator != nullptr) {
  265. pc_deps.cert_generator = std::move(pc_dependencies->cert_generator);
  266. }
  267. if (pc_dependencies->tls_cert_verifier != nullptr) {
  268. pc_deps.tls_cert_verifier = std::move(pc_dependencies->tls_cert_verifier);
  269. }
  270. return pc_deps;
  271. }
  272. rtc::scoped_refptr<PeerConnectionFactoryInterface> peer_connection_factory_;
  273. rtc::scoped_refptr<PeerConnectionInterface> peer_connection_;
  274. rtc::scoped_refptr<AudioProcessing> audio_processing_;
  275. std::map<std::string, absl::optional<int>> stream_required_spatial_index_;
  276. absl::optional<AudioConfig> audio_config_opt_;
  277. MockPeerConnectionObserver* observer_;
  278. VideoQualityAnalyzerInjectionHelper* video_analyzer_helper_;
  279. rtc::Thread* signaling_thread_;
  280. absl::optional<RemotePeerAudioConfig> remote_audio_config_;
  281. double bitrate_multiplier_;
  282. absl::optional<EchoEmulationConfig> echo_emulation_config_;
  283. };
  284. } // namespace
  285. absl::optional<RemotePeerAudioConfig> TestPeer::CreateRemoteAudioConfig(
  286. absl::optional<AudioConfig> config) {
  287. if (!config) {
  288. return absl::nullopt;
  289. }
  290. return RemotePeerAudioConfig(config.value());
  291. }
  292. std::unique_ptr<TestPeer> TestPeer::CreateTestPeer(
  293. std::unique_ptr<InjectableComponents> components,
  294. std::unique_ptr<Params> params,
  295. std::unique_ptr<MockPeerConnectionObserver> observer,
  296. VideoQualityAnalyzerInjectionHelper* video_analyzer_helper,
  297. rtc::Thread* signaling_thread,
  298. absl::optional<RemotePeerAudioConfig> remote_audio_config,
  299. double bitrate_multiplier,
  300. absl::optional<EchoEmulationConfig> echo_emulation_config,
  301. rtc::TaskQueue* task_queue) {
  302. RTC_DCHECK(components);
  303. RTC_DCHECK(params);
  304. SetMandatoryEntities(components.get());
  305. params->rtc_configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
  306. TestPeerComponents tpc(std::move(components), *params, observer.get(),
  307. video_analyzer_helper, signaling_thread,
  308. std::move(remote_audio_config), bitrate_multiplier,
  309. echo_emulation_config, task_queue);
  310. return absl::WrapUnique(new TestPeer(
  311. tpc.peer_connection_factory(), tpc.peer_connection(), std::move(observer),
  312. std::move(params), tpc.audio_processing()));
  313. }
  314. bool TestPeer::AddIceCandidates(
  315. std::vector<std::unique_ptr<IceCandidateInterface>> candidates) {
  316. bool success = true;
  317. for (auto& candidate : candidates) {
  318. if (!pc()->AddIceCandidate(candidate.get())) {
  319. std::string candidate_str;
  320. bool res = candidate->ToString(&candidate_str);
  321. RTC_CHECK(res);
  322. RTC_LOG(LS_ERROR) << "Failed to add ICE candidate, candidate_str="
  323. << candidate_str;
  324. success = false;
  325. } else {
  326. remote_ice_candidates_.push_back(std::move(candidate));
  327. }
  328. }
  329. return success;
  330. }
  331. TestPeer::TestPeer(
  332. rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
  333. rtc::scoped_refptr<PeerConnectionInterface> pc,
  334. std::unique_ptr<MockPeerConnectionObserver> observer,
  335. std::unique_ptr<Params> params,
  336. rtc::scoped_refptr<AudioProcessing> audio_processing)
  337. : PeerConnectionWrapper::PeerConnectionWrapper(std::move(pc_factory),
  338. std::move(pc),
  339. std::move(observer)),
  340. params_(std::move(params)),
  341. audio_processing_(audio_processing) {}
  342. } // namespace webrtc_pc_e2e
  343. } // namespace webrtc