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> |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/rtp_rtcp/include/rtp_header_parser.h" |
| 18 | #include "rtc_base/criticalsection.h" |
| 19 | #include "rtc_base/event.h" |
| 20 | #include "system_wrappers/include/field_trial.h" |
| 21 | #include "test/constants.h" |
| 22 | #include "test/direct_transport.h" |
| 23 | #include "test/gtest.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 24 | #include "typedefs.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "call/video_send_stream.h" |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 26 | |
ilnik | 5328b9e | 2017-02-21 05:20:28 -0800 | [diff] [blame] | 27 | namespace { |
| 28 | const int kShortTimeoutMs = 500; |
| 29 | } |
| 30 | |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 31 | namespace webrtc { |
| 32 | namespace test { |
| 33 | |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 34 | class PacketTransport; |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 35 | class SingleThreadedTaskQueueForTesting; |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 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) |
| 73 | : observation_complete_(false, false), |
stefan@webrtc.org | 28bf50f | 2013-11-18 11:58:24 +0000 | [diff] [blame] | 74 | parser_(RtpHeaderParser::Create()), |
Stefan Holmer | 1295297 | 2015-10-29 15:13:24 +0100 | [diff] [blame] | 75 | timeout_ms_(event_timeout_ms) { |
| 76 | parser_->RegisterRtpHeaderExtension(kRtpExtensionTransmissionTimeOffset, |
| 77 | kTOffsetExtensionId); |
| 78 | parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime, |
| 79 | kAbsSendTimeExtensionId); |
| 80 | parser_->RegisterRtpHeaderExtension(kRtpExtensionTransportSequenceNumber, |
| 81 | kTransportSequenceNumberExtensionId); |
| 82 | } |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 83 | |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 84 | rtc::Event observation_complete_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 85 | const std::unique_ptr<RtpHeaderParser> parser_; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 86 | |
| 87 | private: |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 88 | const int timeout_ms_; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 89 | }; |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 90 | |
| 91 | class PacketTransport : public test::DirectTransport { |
| 92 | public: |
| 93 | enum TransportType { kReceiver, kSender }; |
| 94 | |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 95 | PacketTransport(SingleThreadedTaskQueueForTesting* task_queue, |
| 96 | Call* send_call, |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 97 | RtpRtcpObserver* observer, |
| 98 | TransportType transport_type, |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 99 | const std::map<uint8_t, MediaType>& payload_type_map, |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 100 | const FakeNetworkPipe::Config& configuration) |
eladalon | 413ee9a | 2017-08-22 04:02:52 -0700 | [diff] [blame] | 101 | : test::DirectTransport(task_queue, |
| 102 | configuration, |
| 103 | send_call, |
| 104 | payload_type_map), |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 105 | observer_(observer), |
| 106 | transport_type_(transport_type) {} |
| 107 | |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 108 | PacketTransport(SingleThreadedTaskQueueForTesting* task_queue, |
| 109 | Call* send_call, RtpRtcpObserver* observer, |
| 110 | TransportType transport_type, |
| 111 | std::unique_ptr<FakeNetworkPipe> nw_pipe) |
| 112 | : test::DirectTransport(task_queue, std::move(nw_pipe), send_call), |
| 113 | observer_(observer), |
| 114 | transport_type_(transport_type) {} |
| 115 | |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 116 | private: |
| 117 | bool SendRtp(const uint8_t* packet, |
| 118 | size_t length, |
| 119 | const PacketOptions& options) override { |
| 120 | EXPECT_FALSE(RtpHeaderParser::IsRtcp(packet, length)); |
| 121 | RtpRtcpObserver::Action action; |
| 122 | { |
| 123 | if (transport_type_ == kSender) { |
| 124 | action = observer_->OnSendRtp(packet, length); |
| 125 | } else { |
| 126 | action = observer_->OnReceiveRtp(packet, length); |
| 127 | } |
| 128 | } |
| 129 | switch (action) { |
| 130 | case RtpRtcpObserver::DROP_PACKET: |
| 131 | // Drop packet silently. |
| 132 | return true; |
| 133 | case RtpRtcpObserver::SEND_PACKET: |
| 134 | return test::DirectTransport::SendRtp(packet, length, options); |
| 135 | } |
| 136 | return true; // Will never happen, makes compiler happy. |
| 137 | } |
| 138 | |
| 139 | bool SendRtcp(const uint8_t* packet, size_t length) override { |
| 140 | EXPECT_TRUE(RtpHeaderParser::IsRtcp(packet, length)); |
| 141 | RtpRtcpObserver::Action action; |
| 142 | { |
| 143 | if (transport_type_ == kSender) { |
| 144 | action = observer_->OnSendRtcp(packet, length); |
| 145 | } else { |
| 146 | action = observer_->OnReceiveRtcp(packet, length); |
| 147 | } |
| 148 | } |
| 149 | switch (action) { |
| 150 | case RtpRtcpObserver::DROP_PACKET: |
| 151 | // Drop packet silently. |
| 152 | return true; |
| 153 | case RtpRtcpObserver::SEND_PACKET: |
| 154 | return test::DirectTransport::SendRtcp(packet, length); |
| 155 | } |
| 156 | return true; // Will never happen, makes compiler happy. |
| 157 | } |
| 158 | |
| 159 | RtpRtcpObserver* const observer_; |
| 160 | TransportType transport_type_; |
| 161 | }; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 162 | } // namespace test |
| 163 | } // namespace webrtc |
| 164 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 165 | #endif // TEST_RTP_RTCP_OBSERVER_H_ |