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