blob: b7554496c613032a13e400b26873a89348c32120 [file] [log] [blame]
pbos@webrtc.org96684672013-08-12 12:59:041/*
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 04:47:3110#include "test/direct_transport.h"
pbos@webrtc.org96684672013-08-12 12:59:0411
Karl Wiberg918f50c2018-07-05 09:40:3312#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "call/call.h"
Artem Titov46c4e602018-08-17 12:26:5414#include "call/fake_network_pipe.h"
Sebastian Jansson09408112018-04-24 12:41:2215#include "modules/rtp_rtcp/include/rtp_header_parser.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "system_wrappers/include/clock.h"
17#include "test/single_threaded_task_queue.h"
pbos@webrtc.org96684672013-08-12 12:59:0418
19namespace webrtc {
20namespace test {
21
Sebastian Jansson09408112018-04-24 12:41:2222Demuxer::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 23:57:5739DirectTransport::DirectTransport(
eladalon413ee9a2017-08-22 11:02:5240 SingleThreadedTaskQueueForTesting* task_queue,
Artem Titov46c4e602018-08-17 12:26:5441 std::unique_ptr<SimulatedPacketReceiverInterface> pipe,
Sebastian Jansson09408112018-04-24 12:41:2242 Call* send_call,
43 const std::map<uint8_t, MediaType>& payload_type_map)
Christoffer Rodbrod2817d82017-10-24 14:26:4944 : send_call_(send_call),
45 clock_(Clock::GetRealTimeClock()),
46 task_queue_(task_queue),
Sebastian Jansson09408112018-04-24 12:41:2247 demuxer_(payload_type_map),
Christoffer Rodbrod2817d82017-10-24 14:26:4948 fake_network_(std::move(pipe)) {
49 Start();
pbos@webrtc.org96684672013-08-12 12:59:0450}
51
eladalon413ee9a2017-08-22 11:02:5252DirectTransport::~DirectTransport() {
Sebastian Jansson836fee12019-02-08 15:08:1053 if (next_process_task_)
54 task_queue_->CancelTask(*next_process_task_);
eladalon413ee9a2017-08-22 11:02:5255}
pbos@webrtc.org96684672013-08-12 12:59:0456
pbos@webrtc.org468e19a2013-08-12 14:28:0057void DirectTransport::StopSending() {
Sebastian Jansson836fee12019-02-08 15:08:1058 rtc::CritScope cs(&process_lock_);
59 if (next_process_task_)
60 task_queue_->CancelTask(*next_process_task_);
pbos@webrtc.org468e19a2013-08-12 14:28:0061}
pbos@webrtc.org96684672013-08-12 12:59:0462
pbos@webrtc.org74fa4892013-08-23 09:19:3063void DirectTransport::SetReceiver(PacketReceiver* receiver) {
Sebastian Jansson836fee12019-02-08 15:08:1064 rtc::CritScope cs(&process_lock_);
Christoffer Rodbrod2817d82017-10-24 14:26:4965 fake_network_->SetReceiver(receiver);
pbos@webrtc.org96684672013-08-12 12:59:0466}
67
stefan1d8a5062015-10-02 10:39:3368bool DirectTransport::SendRtp(const uint8_t* data,
69 size_t length,
70 const PacketOptions& options) {
stefanf116bd02015-10-27 15:29:4271 if (send_call_) {
72 rtc::SentPacket sent_packet(options.packet_id,
73 clock_->TimeInMilliseconds());
Sebastian Jansson03789972018-10-09 16:27:5774 sent_packet.info.included_in_feedback = options.included_in_feedback;
75 sent_packet.info.included_in_allocation = options.included_in_allocation;
Sebastian Jansson156d11d2018-09-28 15:21:3476 sent_packet.info.packet_size_bytes = length;
77 sent_packet.info.packet_type = rtc::PacketType::kData;
stefanf116bd02015-10-27 15:29:4278 send_call_->OnSentPacket(sent_packet);
79 }
Sebastian Jansson09408112018-04-24 12:41:2280 SendPacket(data, length);
pbos@webrtc.org96684672013-08-12 12:59:0481 return true;
82}
83
pbos@webrtc.org27326b62013-11-20 12:17:0484bool DirectTransport::SendRtcp(const uint8_t* data, size_t length) {
Sebastian Jansson09408112018-04-24 12:41:2285 SendPacket(data, length);
stefan@webrtc.orgfaada6e2013-12-18 20:28:2586 return true;
pbos@webrtc.org96684672013-08-12 12:59:0487}
88
Sebastian Jansson09408112018-04-24 12:41:2289void DirectTransport::SendPacket(const uint8_t* data, size_t length) {
90 MediaType media_type = demuxer_.GetMediaType(data, length);
91 int64_t send_time = clock_->TimeInMicroseconds();
92 fake_network_->DeliverPacket(media_type, rtc::CopyOnWriteBuffer(data, length),
Niels Möller70082872018-08-07 09:03:1293 send_time);
Sebastian Jansson836fee12019-02-08 15:08:1094 rtc::CritScope cs(&process_lock_);
95 if (!next_process_task_)
96 ProcessPackets();
Sebastian Jansson09408112018-04-24 12:41:2297}
98
Stefan Holmerff2a6352016-01-14 09:00:2199int DirectTransport::GetAverageDelayMs() {
Christoffer Rodbrod2817d82017-10-24 14:26:49100 return fake_network_->AverageDelay();
101}
102
103void DirectTransport::Start() {
104 RTC_DCHECK(task_queue_);
105 if (send_call_) {
106 send_call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
107 send_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
108 }
Stefan Holmerff2a6352016-01-14 09:00:21109}
110
Sebastian Jansson836fee12019-02-08 15:08:10111void DirectTransport::ProcessPackets() {
112 next_process_task_.reset();
113 auto delay_ms = fake_network_->TimeUntilNextProcess();
114 if (delay_ms) {
115 next_process_task_ = task_queue_->PostDelayedTask(
116 [this]() {
117 fake_network_->Process();
118 rtc::CritScope cs(&process_lock_);
119 ProcessPackets();
120 },
121 *delay_ms);
122 }
pbos@webrtc.org96684672013-08-12 12:59:04123}
124} // namespace test
125} // namespace webrtc