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 */ 10 #ifndef TEST_RTP_RTCP_OBSERVER_H_ 11 #define TEST_RTP_RTCP_OBSERVER_H_ 12 13 #include <map> 14 #include <memory> 15 #include <utility> 16 #include <vector> 17 18 #include "api/test/simulated_network.h" 19 #include "call/simulated_packet_receiver.h" 20 #include "call/video_send_stream.h" 21 #include "rtc_base/event.h" 22 #include "system_wrappers/include/field_trial.h" 23 #include "test/direct_transport.h" 24 #include "test/gtest.h" 25 #include "test/rtp_header_parser.h" 26 27 namespace { 28 const int kShortTimeoutMs = 500; 29 } 30 31 namespace webrtc { 32 namespace test { 33 34 class PacketTransport; 35 36 class RtpRtcpObserver { 37 public: 38 enum Action { 39 SEND_PACKET, 40 DROP_PACKET, 41 }; 42 ~RtpRtcpObserver()43 virtual ~RtpRtcpObserver() {} 44 Wait()45 virtual bool Wait() { 46 if (field_trial::IsEnabled("WebRTC-QuickPerfTest")) { 47 observation_complete_.Wait(kShortTimeoutMs); 48 return true; 49 } 50 return observation_complete_.Wait(timeout_ms_); 51 } 52 OnSendRtp(const uint8_t * packet,size_t length)53 virtual Action OnSendRtp(const uint8_t* packet, size_t length) { 54 return SEND_PACKET; 55 } 56 OnSendRtcp(const uint8_t * packet,size_t length)57 virtual Action OnSendRtcp(const uint8_t* packet, size_t length) { 58 return SEND_PACKET; 59 } 60 OnReceiveRtp(const uint8_t * packet,size_t length)61 virtual Action OnReceiveRtp(const uint8_t* packet, size_t length) { 62 return SEND_PACKET; 63 } 64 OnReceiveRtcp(const uint8_t * packet,size_t length)65 virtual Action OnReceiveRtcp(const uint8_t* packet, size_t length) { 66 return SEND_PACKET; 67 } 68 69 protected: RtpRtcpObserver()70 RtpRtcpObserver() : RtpRtcpObserver(0) {} RtpRtcpObserver(int event_timeout_ms)71 explicit RtpRtcpObserver(int event_timeout_ms) 72 : timeout_ms_(event_timeout_ms) {} 73 74 rtc::Event observation_complete_; 75 76 private: 77 const int timeout_ms_; 78 }; 79 80 class PacketTransport : public test::DirectTransport { 81 public: 82 enum TransportType { kReceiver, kSender }; 83 PacketTransport(TaskQueueBase * task_queue,Call * send_call,RtpRtcpObserver * observer,TransportType transport_type,const std::map<uint8_t,MediaType> & payload_type_map,std::unique_ptr<SimulatedPacketReceiverInterface> nw_pipe)84 PacketTransport(TaskQueueBase* task_queue, 85 Call* send_call, 86 RtpRtcpObserver* observer, 87 TransportType transport_type, 88 const std::map<uint8_t, MediaType>& payload_type_map, 89 std::unique_ptr<SimulatedPacketReceiverInterface> nw_pipe) 90 : test::DirectTransport(task_queue, 91 std::move(nw_pipe), 92 send_call, 93 payload_type_map), 94 observer_(observer), 95 transport_type_(transport_type) {} 96 97 private: SendRtp(const uint8_t * packet,size_t length,const PacketOptions & options)98 bool SendRtp(const uint8_t* packet, 99 size_t length, 100 const PacketOptions& options) override { 101 EXPECT_FALSE(RtpHeaderParser::IsRtcp(packet, length)); 102 RtpRtcpObserver::Action action; 103 { 104 if (transport_type_ == kSender) { 105 action = observer_->OnSendRtp(packet, length); 106 } else { 107 action = observer_->OnReceiveRtp(packet, length); 108 } 109 } 110 switch (action) { 111 case RtpRtcpObserver::DROP_PACKET: 112 // Drop packet silently. 113 return true; 114 case RtpRtcpObserver::SEND_PACKET: 115 return test::DirectTransport::SendRtp(packet, length, options); 116 } 117 return true; // Will never happen, makes compiler happy. 118 } 119 SendRtcp(const uint8_t * packet,size_t length)120 bool SendRtcp(const uint8_t* packet, size_t length) override { 121 EXPECT_TRUE(RtpHeaderParser::IsRtcp(packet, length)); 122 RtpRtcpObserver::Action action; 123 { 124 if (transport_type_ == kSender) { 125 action = observer_->OnSendRtcp(packet, length); 126 } else { 127 action = observer_->OnReceiveRtcp(packet, length); 128 } 129 } 130 switch (action) { 131 case RtpRtcpObserver::DROP_PACKET: 132 // Drop packet silently. 133 return true; 134 case RtpRtcpObserver::SEND_PACKET: 135 return test::DirectTransport::SendRtcp(packet, length); 136 } 137 return true; // Will never happen, makes compiler happy. 138 } 139 140 RtpRtcpObserver* const observer_; 141 TransportType transport_type_; 142 }; 143 } // namespace test 144 } // namespace webrtc 145 146 #endif // TEST_RTP_RTCP_OBSERVER_H_ 147