blob: fd7369171a2200c714d49727da1f60f57385cb99 [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,
Artem Titov46c4e602018-08-17 14:26:54 +020041 std::unique_ptr<SimulatedPacketReceiverInterface> pipe,
Sebastian Jansson09408112018-04-24 14:41:22 +020042 Call* send_call,
43 const std::map<uint8_t, MediaType>& payload_type_map)
Christoffer Rodbrod2817d82017-10-24 16:26:49 +020044 : send_call_(send_call),
45 clock_(Clock::GetRealTimeClock()),
46 task_queue_(task_queue),
Sebastian Jansson09408112018-04-24 14:41:22 +020047 demuxer_(payload_type_map),
Christoffer Rodbrod2817d82017-10-24 16:26:49 +020048 fake_network_(std::move(pipe)) {
49 Start();
pbos@webrtc.org96684672013-08-12 12:59:04 +000050}
51
eladalon413ee9a2017-08-22 04:02:52 -070052DirectTransport::~DirectTransport() {
53 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
54 // Constructor updates |next_scheduled_task_|, so it's guaranteed to
55 // be initialized.
56 task_queue_->CancelTask(next_scheduled_task_);
57}
pbos@webrtc.org96684672013-08-12 12:59:04 +000058
pbos@webrtc.org468e19a2013-08-12 14:28:00 +000059void DirectTransport::StopSending() {
eladaloncff98dc2017-08-24 05:04:56 -070060 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
eladalon413ee9a2017-08-22 04:02:52 -070061 task_queue_->CancelTask(next_scheduled_task_);
pbos@webrtc.org468e19a2013-08-12 14:28:00 +000062}
pbos@webrtc.org96684672013-08-12 12:59:04 +000063
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000064void DirectTransport::SetReceiver(PacketReceiver* receiver) {
eladalon413ee9a2017-08-22 04:02:52 -070065 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Christoffer Rodbrod2817d82017-10-24 16:26:49 +020066 fake_network_->SetReceiver(receiver);
pbos@webrtc.org96684672013-08-12 12:59:04 +000067}
68
stefan1d8a5062015-10-02 03:39:33 -070069bool DirectTransport::SendRtp(const uint8_t* data,
70 size_t length,
71 const PacketOptions& options) {
stefanf116bd02015-10-27 08:29:42 -070072 if (send_call_) {
73 rtc::SentPacket sent_packet(options.packet_id,
74 clock_->TimeInMilliseconds());
Sebastian Jansson03789972018-10-09 18:27:57 +020075 sent_packet.info.included_in_feedback = options.included_in_feedback;
76 sent_packet.info.included_in_allocation = options.included_in_allocation;
Sebastian Jansson156d11d2018-09-28 17:21:34 +020077 sent_packet.info.packet_size_bytes = length;
78 sent_packet.info.packet_type = rtc::PacketType::kData;
stefanf116bd02015-10-27 08:29:42 -070079 send_call_->OnSentPacket(sent_packet);
80 }
Sebastian Jansson09408112018-04-24 14:41:22 +020081 SendPacket(data, length);
pbos@webrtc.org96684672013-08-12 12:59:04 +000082 return true;
83}
84
pbos@webrtc.org27326b62013-11-20 12:17:04 +000085bool DirectTransport::SendRtcp(const uint8_t* data, size_t length) {
Sebastian Jansson09408112018-04-24 14:41:22 +020086 SendPacket(data, length);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000087 return true;
pbos@webrtc.org96684672013-08-12 12:59:04 +000088}
89
Sebastian Jansson09408112018-04-24 14:41:22 +020090void DirectTransport::SendPacket(const uint8_t* data, size_t length) {
91 MediaType media_type = demuxer_.GetMediaType(data, length);
92 int64_t send_time = clock_->TimeInMicroseconds();
93 fake_network_->DeliverPacket(media_type, rtc::CopyOnWriteBuffer(data, length),
Niels Möller70082872018-08-07 11:03:12 +020094 send_time);
Sebastian Jansson09408112018-04-24 14:41:22 +020095}
96
Stefan Holmerff2a6352016-01-14 10:00:21 +010097int DirectTransport::GetAverageDelayMs() {
Christoffer Rodbrod2817d82017-10-24 16:26:49 +020098 return fake_network_->AverageDelay();
99}
100
101void DirectTransport::Start() {
102 RTC_DCHECK(task_queue_);
103 if (send_call_) {
104 send_call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
105 send_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
106 }
107 SendPackets();
Stefan Holmerff2a6352016-01-14 10:00:21 +0100108}
109
eladalon413ee9a2017-08-22 04:02:52 -0700110void DirectTransport::SendPackets() {
111 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
pbos@webrtc.org96684672013-08-12 12:59:04 +0000112
Christoffer Rodbrod2817d82017-10-24 16:26:49 +0200113 fake_network_->Process();
eladalon413ee9a2017-08-22 04:02:52 -0700114
Christoffer Rodbrod2817d82017-10-24 16:26:49 +0200115 int64_t delay_ms = fake_network_->TimeUntilNextProcess();
Yves Gerey665174f2018-06-19 15:03:05 +0200116 next_scheduled_task_ =
117 task_queue_->PostDelayedTask([this]() { SendPackets(); }, delay_ms);
pbos@webrtc.org96684672013-08-12 12:59:04 +0000118}
119} // namespace test
120} // namespace webrtc