blob: 351210ee5ca44f1f9f36ce53cbecbdbbc00534c2 [file] [log] [blame]
pbos@webrtc.org96684672013-08-12 12:59:04 +00001/*
2 * Copyright (c) 2013 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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "test/direct_transport.h"
pbos@webrtc.org96684672013-08-12 12:59:04 +000011
Karl Wiberg918f50c2018-07-05 11:40:33 +020012#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "call/call.h"
Artem Titov46c4e602018-08-17 14:26:54 +020014#include "call/fake_network_pipe.h"
Sebastian Jansson09408112018-04-24 14:41:22 +020015#include "modules/rtp_rtcp/include/rtp_header_parser.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "system_wrappers/include/clock.h"
17#include "test/single_threaded_task_queue.h"
pbos@webrtc.org96684672013-08-12 12:59:04 +000018
19namespace webrtc {
20namespace test {
21
Sebastian Jansson09408112018-04-24 14:41:22 +020022Demuxer::Demuxer(const std::map<uint8_t, MediaType>& payload_type_map)
23 : payload_type_map_(payload_type_map) {}
24
25MediaType Demuxer::GetMediaType(const uint8_t* packet_data,
26 const size_t packet_length) const {
27 if (!RtpHeaderParser::IsRtcp(packet_data, packet_length)) {
28 RTC_CHECK_GE(packet_length, 2);
29 const uint8_t payload_type = packet_data[1] & 0x7f;
30 std::map<uint8_t, MediaType>::const_iterator it =
31 payload_type_map_.find(payload_type);
32 RTC_CHECK(it != payload_type_map_.end())
33 << "payload type " << static_cast<int>(payload_type) << " unknown.";
34 return it->second;
35 }
36 return MediaType::ANY;
37}
38
minyue20c84cc2017-04-10 16:57:57 -070039DirectTransport::DirectTransport(
eladalon413ee9a2017-08-22 04:02:52 -070040 SingleThreadedTaskQueueForTesting* task_queue,
41 Call* send_call,
42 const std::map<uint8_t, MediaType>& payload_type_map)
43 : DirectTransport(task_queue,
Artem Titov46c4e602018-08-17 14:26:54 +020044 DefaultNetworkSimulationConfig(),
eladalon413ee9a2017-08-22 04:02:52 -070045 send_call,
Yves Gerey665174f2018-06-19 15:03:05 +020046 payload_type_map) {}
eladalon413ee9a2017-08-22 04:02:52 -070047
48DirectTransport::DirectTransport(
49 SingleThreadedTaskQueueForTesting* task_queue,
Artem Titov46c4e602018-08-17 14:26:54 +020050 const DefaultNetworkSimulationConfig& config,
eladalon413ee9a2017-08-22 04:02:52 -070051 Call* send_call,
52 const std::map<uint8_t, MediaType>& payload_type_map)
stefanf116bd02015-10-27 08:29:42 -070053 : send_call_(send_call),
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000054 clock_(Clock::GetRealTimeClock()),
eladalon413ee9a2017-08-22 04:02:52 -070055 task_queue_(task_queue),
Sebastian Jansson09408112018-04-24 14:41:22 +020056 demuxer_(payload_type_map),
Karl Wiberg918f50c2018-07-05 11:40:33 +020057 fake_network_(absl::make_unique<FakeNetworkPipe>(clock_, config)) {
Christoffer Rodbrod2817d82017-10-24 16:26:49 +020058 Start();
59}
60
Sebastian Jansson09408112018-04-24 14:41:22 +020061DirectTransport::DirectTransport(
62 SingleThreadedTaskQueueForTesting* task_queue,
Artem Titov46c4e602018-08-17 14:26:54 +020063 std::unique_ptr<SimulatedPacketReceiverInterface> pipe,
Sebastian Jansson09408112018-04-24 14:41:22 +020064 Call* send_call,
65 const std::map<uint8_t, MediaType>& payload_type_map)
Christoffer Rodbrod2817d82017-10-24 16:26:49 +020066 : send_call_(send_call),
67 clock_(Clock::GetRealTimeClock()),
68 task_queue_(task_queue),
Sebastian Jansson09408112018-04-24 14:41:22 +020069 demuxer_(payload_type_map),
Christoffer Rodbrod2817d82017-10-24 16:26:49 +020070 fake_network_(std::move(pipe)) {
71 Start();
pbos@webrtc.org96684672013-08-12 12:59:04 +000072}
73
eladalon413ee9a2017-08-22 04:02:52 -070074DirectTransport::~DirectTransport() {
75 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
76 // Constructor updates |next_scheduled_task_|, so it's guaranteed to
77 // be initialized.
78 task_queue_->CancelTask(next_scheduled_task_);
79}
pbos@webrtc.org96684672013-08-12 12:59:04 +000080
Sebastian Jansson7e85d672018-04-06 09:56:21 +020081void DirectTransport::SetClockOffset(int64_t offset_ms) {
82 fake_network_->SetClockOffset(offset_ms);
83}
84
Artem Titov46c4e602018-08-17 14:26:54 +020085void DirectTransport::SetConfig(const DefaultNetworkSimulationConfig& config) {
Christoffer Rodbrod2817d82017-10-24 16:26:49 +020086 fake_network_->SetConfig(config);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +000087}
88
pbos@webrtc.org468e19a2013-08-12 14:28:00 +000089void DirectTransport::StopSending() {
eladaloncff98dc2017-08-24 05:04:56 -070090 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
eladalon413ee9a2017-08-22 04:02:52 -070091 task_queue_->CancelTask(next_scheduled_task_);
pbos@webrtc.org468e19a2013-08-12 14:28:00 +000092}
pbos@webrtc.org96684672013-08-12 12:59:04 +000093
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000094void DirectTransport::SetReceiver(PacketReceiver* receiver) {
eladalon413ee9a2017-08-22 04:02:52 -070095 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Christoffer Rodbrod2817d82017-10-24 16:26:49 +020096 fake_network_->SetReceiver(receiver);
pbos@webrtc.org96684672013-08-12 12:59:04 +000097}
98
stefan1d8a5062015-10-02 03:39:33 -070099bool DirectTransport::SendRtp(const uint8_t* data,
100 size_t length,
101 const PacketOptions& options) {
stefanf116bd02015-10-27 08:29:42 -0700102 if (send_call_) {
103 rtc::SentPacket sent_packet(options.packet_id,
104 clock_->TimeInMilliseconds());
105 send_call_->OnSentPacket(sent_packet);
106 }
Sebastian Jansson09408112018-04-24 14:41:22 +0200107 SendPacket(data, length);
pbos@webrtc.org96684672013-08-12 12:59:04 +0000108 return true;
109}
110
pbos@webrtc.org27326b62013-11-20 12:17:04 +0000111bool DirectTransport::SendRtcp(const uint8_t* data, size_t length) {
Sebastian Jansson09408112018-04-24 14:41:22 +0200112 SendPacket(data, length);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000113 return true;
pbos@webrtc.org96684672013-08-12 12:59:04 +0000114}
115
Sebastian Jansson09408112018-04-24 14:41:22 +0200116void DirectTransport::SendPacket(const uint8_t* data, size_t length) {
117 MediaType media_type = demuxer_.GetMediaType(data, length);
118 int64_t send_time = clock_->TimeInMicroseconds();
119 fake_network_->DeliverPacket(media_type, rtc::CopyOnWriteBuffer(data, length),
Niels Möller70082872018-08-07 11:03:12 +0200120 send_time);
Sebastian Jansson09408112018-04-24 14:41:22 +0200121}
122
Stefan Holmerff2a6352016-01-14 10:00:21 +0100123int DirectTransport::GetAverageDelayMs() {
Christoffer Rodbrod2817d82017-10-24 16:26:49 +0200124 return fake_network_->AverageDelay();
125}
126
127void DirectTransport::Start() {
128 RTC_DCHECK(task_queue_);
129 if (send_call_) {
130 send_call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
131 send_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
132 }
133 SendPackets();
Stefan Holmerff2a6352016-01-14 10:00:21 +0100134}
135
eladalon413ee9a2017-08-22 04:02:52 -0700136void DirectTransport::SendPackets() {
137 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
pbos@webrtc.org96684672013-08-12 12:59:04 +0000138
Christoffer Rodbrod2817d82017-10-24 16:26:49 +0200139 fake_network_->Process();
eladalon413ee9a2017-08-22 04:02:52 -0700140
Christoffer Rodbrod2817d82017-10-24 16:26:49 +0200141 int64_t delay_ms = fake_network_->TimeUntilNextProcess();
Yves Gerey665174f2018-06-19 15:03:05 +0200142 next_scheduled_task_ =
143 task_queue_->PostDelayedTask([this]() { SendPackets(); }, delay_ms);
pbos@webrtc.org96684672013-08-12 12:59:04 +0000144}
145} // namespace test
146} // namespace webrtc