pbos@webrtc.org | 9668467 | 2013-08-12 12:59:04 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "test/direct_transport.h" |
pbos@webrtc.org | 9668467 | 2013-08-12 12:59:04 +0000 | [diff] [blame] | 11 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 12 | #include "absl/memory/memory.h" |
Danil Chapovalov | ba2ba59 | 2019-09-27 11:10:45 +0200 | [diff] [blame^] | 13 | #include "api/task_queue/task_queue_base.h" |
| 14 | #include "api/units/time_delta.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "call/call.h" |
Artem Titov | 46c4e60 | 2018-08-17 14:26:54 +0200 | [diff] [blame] | 16 | #include "call/fake_network_pipe.h" |
Danil Chapovalov | ba2ba59 | 2019-09-27 11:10:45 +0200 | [diff] [blame^] | 17 | #include "rtc_base/task_utils/repeating_task.h" |
Sebastian Jansson | 11c012a | 2019-03-29 14:17:26 +0100 | [diff] [blame] | 18 | #include "rtc_base/time_utils.h" |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 19 | #include "test/rtp_header_parser.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "test/single_threaded_task_queue.h" |
pbos@webrtc.org | 9668467 | 2013-08-12 12:59:04 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | namespace test { |
| 24 | |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 25 | Demuxer::Demuxer(const std::map<uint8_t, MediaType>& payload_type_map) |
| 26 | : payload_type_map_(payload_type_map) {} |
| 27 | |
| 28 | MediaType Demuxer::GetMediaType(const uint8_t* packet_data, |
| 29 | const size_t packet_length) const { |
| 30 | if (!RtpHeaderParser::IsRtcp(packet_data, packet_length)) { |
| 31 | RTC_CHECK_GE(packet_length, 2); |
| 32 | const uint8_t payload_type = packet_data[1] & 0x7f; |
| 33 | std::map<uint8_t, MediaType>::const_iterator it = |
| 34 | payload_type_map_.find(payload_type); |
| 35 | RTC_CHECK(it != payload_type_map_.end()) |
| 36 | << "payload type " << static_cast<int>(payload_type) << " unknown."; |
| 37 | return it->second; |
| 38 | } |
| 39 | return MediaType::ANY; |
| 40 | } |
| 41 | |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 42 | DirectTransport::DirectTransport( |
Danil Chapovalov | ba2ba59 | 2019-09-27 11:10:45 +0200 | [diff] [blame^] | 43 | TaskQueueBase* task_queue, |
Artem Titov | 46c4e60 | 2018-08-17 14:26:54 +0200 | [diff] [blame] | 44 | std::unique_ptr<SimulatedPacketReceiverInterface> pipe, |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 45 | Call* send_call, |
| 46 | const std::map<uint8_t, MediaType>& payload_type_map) |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 47 | : send_call_(send_call), |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 48 | task_queue_(task_queue), |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 49 | demuxer_(payload_type_map), |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 50 | fake_network_(std::move(pipe)) { |
| 51 | Start(); |
pbos@webrtc.org | 9668467 | 2013-08-12 12:59:04 +0000 | [diff] [blame] | 52 | } |
| 53 | |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 54 | DirectTransport::~DirectTransport() { |
Danil Chapovalov | ba2ba59 | 2019-09-27 11:10:45 +0200 | [diff] [blame^] | 55 | next_process_task_.Stop(); |
pbos@webrtc.org | 468e19a | 2013-08-12 14:28:00 +0000 | [diff] [blame] | 56 | } |
pbos@webrtc.org | 9668467 | 2013-08-12 12:59:04 +0000 | [diff] [blame] | 57 | |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 58 | void DirectTransport::SetReceiver(PacketReceiver* receiver) { |
Sebastian Jansson | 836fee1 | 2019-02-08 16:08:10 +0100 | [diff] [blame] | 59 | rtc::CritScope cs(&process_lock_); |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 60 | fake_network_->SetReceiver(receiver); |
pbos@webrtc.org | 9668467 | 2013-08-12 12:59:04 +0000 | [diff] [blame] | 61 | } |
| 62 | |
stefan | 1d8a506 | 2015-10-02 03:39:33 -0700 | [diff] [blame] | 63 | bool DirectTransport::SendRtp(const uint8_t* data, |
| 64 | size_t length, |
| 65 | const PacketOptions& options) { |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 66 | if (send_call_) { |
Sebastian Jansson | 11c012a | 2019-03-29 14:17:26 +0100 | [diff] [blame] | 67 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis()); |
Sebastian Jansson | 0378997 | 2018-10-09 18:27:57 +0200 | [diff] [blame] | 68 | sent_packet.info.included_in_feedback = options.included_in_feedback; |
| 69 | sent_packet.info.included_in_allocation = options.included_in_allocation; |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 70 | sent_packet.info.packet_size_bytes = length; |
| 71 | sent_packet.info.packet_type = rtc::PacketType::kData; |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 72 | send_call_->OnSentPacket(sent_packet); |
| 73 | } |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 74 | SendPacket(data, length); |
pbos@webrtc.org | 9668467 | 2013-08-12 12:59:04 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
pbos@webrtc.org | 27326b6 | 2013-11-20 12:17:04 +0000 | [diff] [blame] | 78 | bool DirectTransport::SendRtcp(const uint8_t* data, size_t length) { |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 79 | SendPacket(data, length); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 80 | return true; |
pbos@webrtc.org | 9668467 | 2013-08-12 12:59:04 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 83 | void DirectTransport::SendPacket(const uint8_t* data, size_t length) { |
| 84 | MediaType media_type = demuxer_.GetMediaType(data, length); |
Sebastian Jansson | 11c012a | 2019-03-29 14:17:26 +0100 | [diff] [blame] | 85 | int64_t send_time_us = rtc::TimeMicros(); |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 86 | fake_network_->DeliverPacket(media_type, rtc::CopyOnWriteBuffer(data, length), |
Sebastian Jansson | 11c012a | 2019-03-29 14:17:26 +0100 | [diff] [blame] | 87 | send_time_us); |
Sebastian Jansson | 836fee1 | 2019-02-08 16:08:10 +0100 | [diff] [blame] | 88 | rtc::CritScope cs(&process_lock_); |
Danil Chapovalov | ba2ba59 | 2019-09-27 11:10:45 +0200 | [diff] [blame^] | 89 | if (!next_process_task_.Running()) |
Sebastian Jansson | 836fee1 | 2019-02-08 16:08:10 +0100 | [diff] [blame] | 90 | ProcessPackets(); |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Stefan Holmer | ff2a635 | 2016-01-14 10:00:21 +0100 | [diff] [blame] | 93 | int DirectTransport::GetAverageDelayMs() { |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 94 | return fake_network_->AverageDelay(); |
| 95 | } |
| 96 | |
| 97 | void DirectTransport::Start() { |
| 98 | RTC_DCHECK(task_queue_); |
| 99 | if (send_call_) { |
| 100 | send_call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp); |
| 101 | send_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp); |
| 102 | } |
Stefan Holmer | ff2a635 | 2016-01-14 10:00:21 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Sebastian Jansson | 836fee1 | 2019-02-08 16:08:10 +0100 | [diff] [blame] | 105 | void DirectTransport::ProcessPackets() { |
Danil Chapovalov | ba2ba59 | 2019-09-27 11:10:45 +0200 | [diff] [blame^] | 106 | absl::optional<int64_t> initial_delay_ms = |
| 107 | fake_network_->TimeUntilNextProcess(); |
| 108 | if (initial_delay_ms == absl::nullopt) |
| 109 | return; |
| 110 | |
| 111 | next_process_task_ = RepeatingTaskHandle::DelayedStart( |
| 112 | task_queue_, TimeDelta::ms(*initial_delay_ms), [this] { |
| 113 | fake_network_->Process(); |
| 114 | if (auto delay_ms = fake_network_->TimeUntilNextProcess()) |
| 115 | return TimeDelta::ms(*delay_ms); |
| 116 | // Otherwise stop the task. |
| 117 | rtc::CritScope cs(&process_lock_); |
| 118 | next_process_task_.Stop(); |
| 119 | // Since this task is stopped, return value doesn't matter. |
| 120 | return TimeDelta::Zero(); |
| 121 | }); |
pbos@webrtc.org | 9668467 | 2013-08-12 12:59:04 +0000 | [diff] [blame] | 122 | } |
| 123 | } // namespace test |
| 124 | } // namespace webrtc |