pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +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 | #ifndef TEST_RTP_RTCP_OBSERVER_H_ |
| 11 | #define TEST_RTP_RTCP_OBSERVER_H_ |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 12 | |
| 13 | #include <map> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 14 | #include <memory> |
Elad Alon | d8d3248 | 2019-02-18 23:45:57 +0100 | [diff] [blame] | 15 | #include <utility> |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Artem Titov | 46c4e60 | 2018-08-17 14:26:54 +0200 | [diff] [blame] | 18 | #include "api/test/simulated_network.h" |
| 19 | #include "call/simulated_packet_receiver.h" |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 20 | #include "call/video_send_stream.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/event.h" |
| 23 | #include "system_wrappers/include/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "test/direct_transport.h" |
| 25 | #include "test/gtest.h" |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 26 | #include "test/rtp_header_parser.h" |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 27 | |
ilnik | 5328b9e | 2017-02-21 05:20:28 -0800 | [diff] [blame] | 28 | namespace { |
| 29 | const int kShortTimeoutMs = 500; |
| 30 | } |
| 31 | |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 32 | namespace webrtc { |
| 33 | namespace test { |
| 34 | |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 35 | class PacketTransport; |
| 36 | |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 37 | class RtpRtcpObserver { |
| 38 | public: |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 39 | enum Action { |
| 40 | SEND_PACKET, |
| 41 | DROP_PACKET, |
| 42 | }; |
| 43 | |
pbos@webrtc.org | b3cc78d | 2013-11-21 11:42:02 +0000 | [diff] [blame] | 44 | virtual ~RtpRtcpObserver() {} |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 45 | |
ilnik | 5328b9e | 2017-02-21 05:20:28 -0800 | [diff] [blame] | 46 | virtual bool Wait() { |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 47 | if (field_trial::IsEnabled("WebRTC-QuickPerfTest")) { |
ilnik | 5328b9e | 2017-02-21 05:20:28 -0800 | [diff] [blame] | 48 | observation_complete_.Wait(kShortTimeoutMs); |
| 49 | return true; |
| 50 | } |
| 51 | return observation_complete_.Wait(timeout_ms_); |
| 52 | } |
pbos@webrtc.org | 6917e19 | 2013-09-19 14:22:12 +0000 | [diff] [blame] | 53 | |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 54 | virtual Action OnSendRtp(const uint8_t* packet, size_t length) { |
| 55 | return SEND_PACKET; |
| 56 | } |
stefan@webrtc.org | b082ade | 2013-11-18 11:45:11 +0000 | [diff] [blame] | 57 | |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 58 | virtual Action OnSendRtcp(const uint8_t* packet, size_t length) { |
| 59 | return SEND_PACKET; |
| 60 | } |
| 61 | |
| 62 | virtual Action OnReceiveRtp(const uint8_t* packet, size_t length) { |
| 63 | return SEND_PACKET; |
| 64 | } |
| 65 | |
| 66 | virtual Action OnReceiveRtcp(const uint8_t* packet, size_t length) { |
| 67 | return SEND_PACKET; |
| 68 | } |
| 69 | |
| 70 | protected: |
philipel | e828c96 | 2017-03-21 03:24:27 -0700 | [diff] [blame] | 71 | RtpRtcpObserver() : RtpRtcpObserver(0) {} |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 72 | explicit RtpRtcpObserver(int event_timeout_ms) |
Tommi | 25eb47c | 2019-08-29 16:39:05 +0200 | [diff] [blame] | 73 | : parser_(RtpHeaderParser::CreateForTest()), |
| 74 | timeout_ms_(event_timeout_ms) {} |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 75 | |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 76 | rtc::Event observation_complete_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 77 | const std::unique_ptr<RtpHeaderParser> parser_; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 80 | const int timeout_ms_; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 81 | }; |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 82 | |
| 83 | class PacketTransport : public test::DirectTransport { |
| 84 | public: |
| 85 | enum TransportType { kReceiver, kSender }; |
| 86 | |
Danil Chapovalov | 44db436 | 2019-09-30 04:16:28 +0200 | [diff] [blame] | 87 | PacketTransport(TaskQueueBase* task_queue, |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 88 | Call* send_call, |
| 89 | RtpRtcpObserver* observer, |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 90 | TransportType transport_type, |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 91 | const std::map<uint8_t, MediaType>& payload_type_map, |
Artem Titov | 46c4e60 | 2018-08-17 14:26:54 +0200 | [diff] [blame] | 92 | std::unique_ptr<SimulatedPacketReceiverInterface> nw_pipe) |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 93 | : test::DirectTransport(task_queue, |
| 94 | std::move(nw_pipe), |
| 95 | send_call, |
| 96 | payload_type_map), |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 97 | observer_(observer), |
| 98 | transport_type_(transport_type) {} |
| 99 | |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 100 | private: |
| 101 | bool SendRtp(const uint8_t* packet, |
| 102 | size_t length, |
| 103 | const PacketOptions& options) override { |
| 104 | EXPECT_FALSE(RtpHeaderParser::IsRtcp(packet, length)); |
| 105 | RtpRtcpObserver::Action action; |
| 106 | { |
| 107 | if (transport_type_ == kSender) { |
| 108 | action = observer_->OnSendRtp(packet, length); |
| 109 | } else { |
| 110 | action = observer_->OnReceiveRtp(packet, length); |
| 111 | } |
| 112 | } |
| 113 | switch (action) { |
| 114 | case RtpRtcpObserver::DROP_PACKET: |
| 115 | // Drop packet silently. |
| 116 | return true; |
| 117 | case RtpRtcpObserver::SEND_PACKET: |
| 118 | return test::DirectTransport::SendRtp(packet, length, options); |
| 119 | } |
| 120 | return true; // Will never happen, makes compiler happy. |
| 121 | } |
| 122 | |
| 123 | bool SendRtcp(const uint8_t* packet, size_t length) override { |
| 124 | EXPECT_TRUE(RtpHeaderParser::IsRtcp(packet, length)); |
| 125 | RtpRtcpObserver::Action action; |
| 126 | { |
| 127 | if (transport_type_ == kSender) { |
| 128 | action = observer_->OnSendRtcp(packet, length); |
| 129 | } else { |
| 130 | action = observer_->OnReceiveRtcp(packet, length); |
| 131 | } |
| 132 | } |
| 133 | switch (action) { |
| 134 | case RtpRtcpObserver::DROP_PACKET: |
| 135 | // Drop packet silently. |
| 136 | return true; |
| 137 | case RtpRtcpObserver::SEND_PACKET: |
| 138 | return test::DirectTransport::SendRtcp(packet, length); |
| 139 | } |
| 140 | return true; // Will never happen, makes compiler happy. |
| 141 | } |
| 142 | |
| 143 | RtpRtcpObserver* const observer_; |
| 144 | TransportType transport_type_; |
| 145 | }; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 146 | } // namespace test |
| 147 | } // namespace webrtc |
| 148 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 149 | #endif // TEST_RTP_RTCP_OBSERVER_H_ |