Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | |
Steve Anton | 40d5533 | 2019-01-07 10:21:47 -0800 | [diff] [blame] | 11 | #include "absl/memory/memory.h" |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 12 | #include "api/audio_codecs/audio_decoder_factory_template.h" |
| 13 | #include "api/audio_codecs/audio_encoder_factory_template.h" |
| 14 | #include "api/audio_codecs/opus/audio_decoder_opus.h" |
| 15 | #include "api/audio_codecs/opus/audio_encoder_opus.h" |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 16 | #include "api/media_transport_config.h" |
Danil Chapovalov | 31660fd | 2019-03-22 12:59:48 +0100 | [diff] [blame] | 17 | #include "api/task_queue/default_task_queue_factory.h" |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 18 | #include "api/test/loopback_media_transport.h" |
| 19 | #include "api/test/mock_audio_mixer.h" |
| 20 | #include "audio/audio_receive_stream.h" |
| 21 | #include "audio/audio_send_stream.h" |
Sebastian Jansson | 0b69826 | 2019-03-07 09:17:19 +0100 | [diff] [blame] | 22 | #include "call/rtp_transport_controller_send.h" |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 23 | #include "call/test/mock_bitrate_allocator.h" |
| 24 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 25 | #include "modules/audio_device/include/test_audio_device.h" |
| 26 | #include "modules/audio_mixer/audio_mixer_impl.h" |
| 27 | #include "modules/audio_processing/include/mock_audio_processing.h" |
| 28 | #include "modules/utility/include/process_thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "rtc_base/time_utils.h" |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 30 | #include "test/gtest.h" |
| 31 | #include "test/mock_transport.h" |
| 32 | |
| 33 | namespace webrtc { |
| 34 | namespace test { |
| 35 | |
| 36 | namespace { |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 37 | using ::testing::NiceMock; |
Sebastian Jansson | 0b69826 | 2019-03-07 09:17:19 +0100 | [diff] [blame] | 38 | |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 39 | constexpr int kPayloadTypeOpus = 17; |
| 40 | constexpr int kSamplingFrequency = 48000; |
| 41 | constexpr int kNumChannels = 2; |
| 42 | constexpr int kWantedSamples = 3000; |
| 43 | constexpr int kTestTimeoutMs = 2 * rtc::kNumMillisecsPerSec; |
| 44 | |
| 45 | class TestRenderer : public TestAudioDeviceModule::Renderer { |
| 46 | public: |
| 47 | TestRenderer(int sampling_frequency, int num_channels, size_t wanted_samples) |
| 48 | : sampling_frequency_(sampling_frequency), |
| 49 | num_channels_(num_channels), |
| 50 | wanted_samples_(wanted_samples) {} |
| 51 | ~TestRenderer() override = default; |
| 52 | |
| 53 | int SamplingFrequency() const override { return sampling_frequency_; } |
| 54 | int NumChannels() const override { return num_channels_; } |
| 55 | |
| 56 | bool Render(rtc::ArrayView<const int16_t> data) override { |
| 57 | if (data.size() >= wanted_samples_) { |
| 58 | return false; |
| 59 | } |
| 60 | wanted_samples_ -= data.size(); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | const int sampling_frequency_; |
| 66 | const int num_channels_; |
| 67 | size_t wanted_samples_; |
| 68 | }; |
| 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | TEST(AudioWithMediaTransport, DeliversAudio) { |
Bjorn Mellem | 273d029 | 2018-11-01 16:42:44 -0700 | [diff] [blame] | 73 | std::unique_ptr<rtc::Thread> transport_thread = rtc::Thread::Create(); |
| 74 | transport_thread->Start(); |
| 75 | MediaTransportPair transport_pair(transport_thread.get()); |
Sebastian Jansson | 0b69826 | 2019-03-07 09:17:19 +0100 | [diff] [blame] | 76 | NiceMock<MockTransport> rtcp_send_transport; |
| 77 | NiceMock<MockTransport> send_transport; |
Danil Chapovalov | b32f2c7 | 2019-05-22 13:39:25 +0200 | [diff] [blame^] | 78 | RtcEventLogNull null_event_log; |
Sebastian Jansson | 0b69826 | 2019-03-07 09:17:19 +0100 | [diff] [blame] | 79 | NiceMock<MockBitrateAllocator> bitrate_allocator; |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 80 | |
| 81 | rtc::scoped_refptr<TestAudioDeviceModule> audio_device = |
| 82 | TestAudioDeviceModule::CreateTestAudioDeviceModule( |
| 83 | TestAudioDeviceModule::CreatePulsedNoiseCapturer( |
| 84 | /* max_amplitude= */ 10000, kSamplingFrequency, kNumChannels), |
| 85 | absl::make_unique<TestRenderer>(kSamplingFrequency, kNumChannels, |
| 86 | kWantedSamples)); |
| 87 | |
| 88 | AudioState::Config audio_config; |
| 89 | audio_config.audio_mixer = AudioMixerImpl::Create(); |
| 90 | // TODO(nisse): Is a mock AudioProcessing enough? |
| 91 | audio_config.audio_processing = |
| 92 | new rtc::RefCountedObject<MockAudioProcessing>(); |
| 93 | audio_config.audio_device_module = audio_device; |
| 94 | rtc::scoped_refptr<AudioState> audio_state = AudioState::Create(audio_config); |
| 95 | |
| 96 | // TODO(nisse): Use some lossless codec? |
| 97 | const SdpAudioFormat audio_format("opus", kSamplingFrequency, kNumChannels); |
| 98 | |
| 99 | // Setup receive stream; |
| 100 | webrtc::AudioReceiveStream::Config receive_config; |
| 101 | // TODO(nisse): Update AudioReceiveStream to not require rtcp_send_transport |
| 102 | // when a MediaTransport is provided. |
| 103 | receive_config.rtcp_send_transport = &rtcp_send_transport; |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 104 | receive_config.media_transport_config.media_transport = |
| 105 | transport_pair.first(); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 106 | receive_config.decoder_map.emplace(kPayloadTypeOpus, audio_format); |
| 107 | receive_config.decoder_factory = |
| 108 | CreateAudioDecoderFactory<AudioDecoderOpus>(); |
| 109 | |
| 110 | std::unique_ptr<ProcessThread> receive_process_thread = |
| 111 | ProcessThread::Create("audio recv thread"); |
| 112 | |
| 113 | webrtc::internal::AudioReceiveStream receive_stream( |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 114 | Clock::GetRealTimeClock(), |
Benjamin Wright | 17b050f | 2019-03-13 17:35:46 -0700 | [diff] [blame] | 115 | /*receiver_controller=*/nullptr, |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 116 | /*packet_router=*/nullptr, receive_process_thread.get(), receive_config, |
Danil Chapovalov | b32f2c7 | 2019-05-22 13:39:25 +0200 | [diff] [blame^] | 117 | audio_state, &null_event_log); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 118 | |
| 119 | // TODO(nisse): Update AudioSendStream to not require send_transport when a |
| 120 | // MediaTransport is provided. |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 121 | AudioSendStream::Config send_config( |
| 122 | &send_transport, webrtc::MediaTransportConfig(transport_pair.second())); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 123 | send_config.send_codec_spec = |
| 124 | AudioSendStream::Config::SendCodecSpec(kPayloadTypeOpus, audio_format); |
| 125 | send_config.encoder_factory = CreateAudioEncoderFactory<AudioEncoderOpus>(); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 126 | std::unique_ptr<ProcessThread> send_process_thread = |
| 127 | ProcessThread::Create("audio send thread"); |
Danil Chapovalov | 31660fd | 2019-03-22 12:59:48 +0100 | [diff] [blame] | 128 | std::unique_ptr<TaskQueueFactory> task_queue_factory = |
| 129 | CreateDefaultTaskQueueFactory(); |
Sebastian Jansson | 0b69826 | 2019-03-07 09:17:19 +0100 | [diff] [blame] | 130 | RtpTransportControllerSend rtp_transport( |
Danil Chapovalov | b32f2c7 | 2019-05-22 13:39:25 +0200 | [diff] [blame^] | 131 | Clock::GetRealTimeClock(), &null_event_log, nullptr, nullptr, |
Sebastian Jansson | 0b69826 | 2019-03-07 09:17:19 +0100 | [diff] [blame] | 132 | BitrateConstraints(), ProcessThread::Create("Pacer"), |
Danil Chapovalov | 31660fd | 2019-03-22 12:59:48 +0100 | [diff] [blame] | 133 | task_queue_factory.get()); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 134 | webrtc::internal::AudioSendStream send_stream( |
Sebastian Jansson | 0b69826 | 2019-03-07 09:17:19 +0100 | [diff] [blame] | 135 | Clock::GetRealTimeClock(), send_config, audio_state, |
Danil Chapovalov | 31660fd | 2019-03-22 12:59:48 +0100 | [diff] [blame] | 136 | task_queue_factory.get(), send_process_thread.get(), &rtp_transport, |
Danil Chapovalov | b32f2c7 | 2019-05-22 13:39:25 +0200 | [diff] [blame^] | 137 | &bitrate_allocator, &null_event_log, |
Sam Zackrisson | ff05816 | 2018-11-20 17:15:13 +0100 | [diff] [blame] | 138 | /*rtcp_rtt_stats=*/nullptr, absl::optional<RtpState>()); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 139 | |
| 140 | audio_device->Init(); // Starts thread. |
| 141 | audio_device->RegisterAudioCallback(audio_state->audio_transport()); |
| 142 | |
| 143 | receive_stream.Start(); |
| 144 | send_stream.Start(); |
| 145 | audio_device->StartPlayout(); |
| 146 | audio_device->StartRecording(); |
| 147 | |
| 148 | EXPECT_TRUE(audio_device->WaitForPlayoutEnd(kTestTimeoutMs)); |
| 149 | |
| 150 | audio_device->StopRecording(); |
| 151 | audio_device->StopPlayout(); |
| 152 | receive_stream.Stop(); |
| 153 | send_stream.Stop(); |
| 154 | } |
| 155 | |
| 156 | } // namespace test |
| 157 | } // namespace webrtc |