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 */
10 #include "test/direct_transport.h"
11
12 #include "absl/memory/memory.h"
13 #include "api/task_queue/task_queue_base.h"
14 #include "api/units/time_delta.h"
15 #include "call/call.h"
16 #include "call/fake_network_pipe.h"
17 #include "rtc_base/task_utils/repeating_task.h"
18 #include "rtc_base/time_utils.h"
19 #include "test/rtp_header_parser.h"
20
21 namespace webrtc {
22 namespace test {
23
Demuxer(const std::map<uint8_t,MediaType> & payload_type_map)24 Demuxer::Demuxer(const std::map<uint8_t, MediaType>& payload_type_map)
25 : payload_type_map_(payload_type_map) {}
26
GetMediaType(const uint8_t * packet_data,const size_t packet_length) const27 MediaType Demuxer::GetMediaType(const uint8_t* packet_data,
28 const size_t packet_length) const {
29 if (!RtpHeaderParser::IsRtcp(packet_data, packet_length)) {
30 RTC_CHECK_GE(packet_length, 2);
31 const uint8_t payload_type = packet_data[1] & 0x7f;
32 std::map<uint8_t, MediaType>::const_iterator it =
33 payload_type_map_.find(payload_type);
34 RTC_CHECK(it != payload_type_map_.end())
35 << "payload type " << static_cast<int>(payload_type) << " unknown.";
36 return it->second;
37 }
38 return MediaType::ANY;
39 }
40
DirectTransport(TaskQueueBase * task_queue,std::unique_ptr<SimulatedPacketReceiverInterface> pipe,Call * send_call,const std::map<uint8_t,MediaType> & payload_type_map)41 DirectTransport::DirectTransport(
42 TaskQueueBase* task_queue,
43 std::unique_ptr<SimulatedPacketReceiverInterface> pipe,
44 Call* send_call,
45 const std::map<uint8_t, MediaType>& payload_type_map)
46 : send_call_(send_call),
47 task_queue_(task_queue),
48 demuxer_(payload_type_map),
49 fake_network_(std::move(pipe)) {
50 Start();
51 }
52
~DirectTransport()53 DirectTransport::~DirectTransport() {
54 next_process_task_.Stop();
55 }
56
SetReceiver(PacketReceiver * receiver)57 void DirectTransport::SetReceiver(PacketReceiver* receiver) {
58 fake_network_->SetReceiver(receiver);
59 }
60
SendRtp(const uint8_t * data,size_t length,const PacketOptions & options)61 bool DirectTransport::SendRtp(const uint8_t* data,
62 size_t length,
63 const PacketOptions& options) {
64 if (send_call_) {
65 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis());
66 sent_packet.info.included_in_feedback = options.included_in_feedback;
67 sent_packet.info.included_in_allocation = options.included_in_allocation;
68 sent_packet.info.packet_size_bytes = length;
69 sent_packet.info.packet_type = rtc::PacketType::kData;
70 send_call_->OnSentPacket(sent_packet);
71 }
72 SendPacket(data, length);
73 return true;
74 }
75
SendRtcp(const uint8_t * data,size_t length)76 bool DirectTransport::SendRtcp(const uint8_t* data, size_t length) {
77 SendPacket(data, length);
78 return true;
79 }
80
SendPacket(const uint8_t * data,size_t length)81 void DirectTransport::SendPacket(const uint8_t* data, size_t length) {
82 MediaType media_type = demuxer_.GetMediaType(data, length);
83 int64_t send_time_us = rtc::TimeMicros();
84 fake_network_->DeliverPacket(media_type, rtc::CopyOnWriteBuffer(data, length),
85 send_time_us);
86 MutexLock lock(&process_lock_);
87 if (!next_process_task_.Running())
88 ProcessPackets();
89 }
90
GetAverageDelayMs()91 int DirectTransport::GetAverageDelayMs() {
92 return fake_network_->AverageDelay();
93 }
94
Start()95 void DirectTransport::Start() {
96 RTC_DCHECK(task_queue_);
97 if (send_call_) {
98 send_call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
99 send_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
100 }
101 }
102
ProcessPackets()103 void DirectTransport::ProcessPackets() {
104 absl::optional<int64_t> initial_delay_ms =
105 fake_network_->TimeUntilNextProcess();
106 if (initial_delay_ms == absl::nullopt)
107 return;
108
109 next_process_task_ = RepeatingTaskHandle::DelayedStart(
110 task_queue_, TimeDelta::Millis(*initial_delay_ms), [this] {
111 fake_network_->Process();
112 if (auto delay_ms = fake_network_->TimeUntilNextProcess())
113 return TimeDelta::Millis(*delay_ms);
114 // Otherwise stop the task.
115 MutexLock lock(&process_lock_);
116 next_process_task_.Stop();
117 // Since this task is stopped, return value doesn't matter.
118 return TimeDelta::Zero();
119 });
120 }
121 } // namespace test
122 } // namespace webrtc
123