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