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 | */ |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 10 | #ifndef WEBRTC_TEST_RTP_RTCP_OBSERVER_H_ |
| 11 | #define WEBRTC_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 | |
pbos@webrtc.org | c279a5d | 2014-01-24 09:30:53 +0000 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 19 | #include "webrtc/base/criticalsection.h" |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 20 | #include "webrtc/base/event.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 21 | #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h" |
Stefan Holmer | 1295297 | 2015-10-29 15:13:24 +0100 | [diff] [blame] | 22 | #include "webrtc/test/constants.h" |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame] | 23 | #include "webrtc/test/direct_transport.h" |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 24 | #include "webrtc/typedefs.h" |
pbos@webrtc.org | 16e03b7 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 25 | #include "webrtc/video_send_stream.h" |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace test { |
| 29 | |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 30 | class PacketTransport; |
| 31 | |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 32 | class RtpRtcpObserver { |
| 33 | public: |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 34 | enum Action { |
| 35 | SEND_PACKET, |
| 36 | DROP_PACKET, |
| 37 | }; |
| 38 | |
pbos@webrtc.org | b3cc78d | 2013-11-21 11:42:02 +0000 | [diff] [blame] | 39 | virtual ~RtpRtcpObserver() {} |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 40 | |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 41 | virtual bool Wait() { return observation_complete_.Wait(timeout_ms_); } |
pbos@webrtc.org | 6917e19 | 2013-09-19 14:22:12 +0000 | [diff] [blame] | 42 | |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 43 | virtual Action OnSendRtp(const uint8_t* packet, size_t length) { |
| 44 | return SEND_PACKET; |
| 45 | } |
stefan@webrtc.org | b082ade | 2013-11-18 11:45:11 +0000 | [diff] [blame] | 46 | |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 47 | virtual Action OnSendRtcp(const uint8_t* packet, size_t length) { |
| 48 | return SEND_PACKET; |
| 49 | } |
| 50 | |
| 51 | virtual Action OnReceiveRtp(const uint8_t* packet, size_t length) { |
| 52 | return SEND_PACKET; |
| 53 | } |
| 54 | |
| 55 | virtual Action OnReceiveRtcp(const uint8_t* packet, size_t length) { |
| 56 | return SEND_PACKET; |
| 57 | } |
| 58 | |
| 59 | protected: |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 60 | explicit RtpRtcpObserver(int event_timeout_ms) |
| 61 | : observation_complete_(false, false), |
stefan@webrtc.org | 28bf50f | 2013-11-18 11:58:24 +0000 | [diff] [blame] | 62 | parser_(RtpHeaderParser::Create()), |
Stefan Holmer | 1295297 | 2015-10-29 15:13:24 +0100 | [diff] [blame] | 63 | timeout_ms_(event_timeout_ms) { |
| 64 | parser_->RegisterRtpHeaderExtension(kRtpExtensionTransmissionTimeOffset, |
| 65 | kTOffsetExtensionId); |
| 66 | parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime, |
| 67 | kAbsSendTimeExtensionId); |
| 68 | parser_->RegisterRtpHeaderExtension(kRtpExtensionTransportSequenceNumber, |
| 69 | kTransportSequenceNumberExtensionId); |
| 70 | } |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 71 | |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 72 | rtc::Event observation_complete_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame^] | 73 | const std::unique_ptr<RtpHeaderParser> parser_; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 74 | |
| 75 | private: |
Peter Boström | 5811a39 | 2015-12-10 13:02:50 +0100 | [diff] [blame] | 76 | const int timeout_ms_; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 77 | }; |
stefan | f116bd0 | 2015-10-27 08:29:42 -0700 | [diff] [blame] | 78 | |
| 79 | class PacketTransport : public test::DirectTransport { |
| 80 | public: |
| 81 | enum TransportType { kReceiver, kSender }; |
| 82 | |
| 83 | PacketTransport(Call* send_call, |
| 84 | RtpRtcpObserver* observer, |
| 85 | TransportType transport_type, |
| 86 | const FakeNetworkPipe::Config& configuration) |
| 87 | : test::DirectTransport(configuration, send_call), |
| 88 | observer_(observer), |
| 89 | transport_type_(transport_type) {} |
| 90 | |
| 91 | private: |
| 92 | bool SendRtp(const uint8_t* packet, |
| 93 | size_t length, |
| 94 | const PacketOptions& options) override { |
| 95 | EXPECT_FALSE(RtpHeaderParser::IsRtcp(packet, length)); |
| 96 | RtpRtcpObserver::Action action; |
| 97 | { |
| 98 | if (transport_type_ == kSender) { |
| 99 | action = observer_->OnSendRtp(packet, length); |
| 100 | } else { |
| 101 | action = observer_->OnReceiveRtp(packet, length); |
| 102 | } |
| 103 | } |
| 104 | switch (action) { |
| 105 | case RtpRtcpObserver::DROP_PACKET: |
| 106 | // Drop packet silently. |
| 107 | return true; |
| 108 | case RtpRtcpObserver::SEND_PACKET: |
| 109 | return test::DirectTransport::SendRtp(packet, length, options); |
| 110 | } |
| 111 | return true; // Will never happen, makes compiler happy. |
| 112 | } |
| 113 | |
| 114 | bool SendRtcp(const uint8_t* packet, size_t length) override { |
| 115 | EXPECT_TRUE(RtpHeaderParser::IsRtcp(packet, length)); |
| 116 | RtpRtcpObserver::Action action; |
| 117 | { |
| 118 | if (transport_type_ == kSender) { |
| 119 | action = observer_->OnSendRtcp(packet, length); |
| 120 | } else { |
| 121 | action = observer_->OnReceiveRtcp(packet, length); |
| 122 | } |
| 123 | } |
| 124 | switch (action) { |
| 125 | case RtpRtcpObserver::DROP_PACKET: |
| 126 | // Drop packet silently. |
| 127 | return true; |
| 128 | case RtpRtcpObserver::SEND_PACKET: |
| 129 | return test::DirectTransport::SendRtcp(packet, length); |
| 130 | } |
| 131 | return true; // Will never happen, makes compiler happy. |
| 132 | } |
| 133 | |
| 134 | RtpRtcpObserver* const observer_; |
| 135 | TransportType transport_type_; |
| 136 | }; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 137 | } // namespace test |
| 138 | } // namespace webrtc |
| 139 | |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 140 | #endif // WEBRTC_TEST_RTP_RTCP_OBSERVER_H_ |