| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 #ifndef WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_ | 10 #ifndef WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_ |
| 11 #define WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_ | 11 #define WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_ |
| 12 | 12 |
| 13 #include <map> | 13 #include <map> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 #include "webrtc/base/criticalsection.h" | 18 #include "webrtc/base/criticalsection.h" |
| 19 #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h" | 19 #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h" |
| 20 #include "webrtc/test/direct_transport.h" | 20 #include "webrtc/test/direct_transport.h" |
| 21 #include "webrtc/typedefs.h" | 21 #include "webrtc/typedefs.h" |
| 22 #include "webrtc/video_send_stream.h" | 22 #include "webrtc/video_send_stream.h" |
| 23 | 23 |
| 24 namespace webrtc { | 24 namespace webrtc { |
| 25 namespace test { | 25 namespace test { |
| 26 | 26 |
| 27 class PacketTransport; |
| 28 |
| 27 class RtpRtcpObserver { | 29 class RtpRtcpObserver { |
| 28 public: | 30 public: |
| 31 enum Action { |
| 32 SEND_PACKET, |
| 33 DROP_PACKET, |
| 34 }; |
| 35 |
| 29 virtual ~RtpRtcpObserver() {} | 36 virtual ~RtpRtcpObserver() {} |
| 30 Transport* SendTransport() { | |
| 31 return &send_transport_; | |
| 32 } | |
| 33 | |
| 34 Transport* ReceiveTransport() { | |
| 35 return &receive_transport_; | |
| 36 } | |
| 37 | |
| 38 virtual void SetReceivers(PacketReceiver* send_transport_receiver, | |
| 39 PacketReceiver* receive_transport_receiver) { | |
| 40 send_transport_.SetReceiver(send_transport_receiver); | |
| 41 receive_transport_.SetReceiver(receive_transport_receiver); | |
| 42 } | |
| 43 | |
| 44 void StopSending() { | |
| 45 send_transport_.StopSending(); | |
| 46 receive_transport_.StopSending(); | |
| 47 } | |
| 48 | 37 |
| 49 virtual EventTypeWrapper Wait() { | 38 virtual EventTypeWrapper Wait() { |
| 50 EventTypeWrapper result = observation_complete_->Wait(timeout_ms_); | 39 EventTypeWrapper result = observation_complete_->Wait(timeout_ms_); |
| 51 return result; | 40 return result; |
| 52 } | 41 } |
| 53 | 42 |
| 43 virtual Action OnSendRtp(const uint8_t* packet, size_t length) { |
| 44 return SEND_PACKET; |
| 45 } |
| 46 |
| 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 |
| 54 protected: | 59 protected: |
| 55 RtpRtcpObserver(unsigned int event_timeout_ms, | |
| 56 const FakeNetworkPipe::Config& configuration) | |
| 57 : observation_complete_(EventWrapper::Create()), | |
| 58 parser_(RtpHeaderParser::Create()), | |
| 59 send_transport_(&crit_, | |
| 60 this, | |
| 61 &RtpRtcpObserver::OnSendRtp, | |
| 62 &RtpRtcpObserver::OnSendRtcp, | |
| 63 configuration), | |
| 64 receive_transport_(&crit_, | |
| 65 this, | |
| 66 &RtpRtcpObserver::OnReceiveRtp, | |
| 67 &RtpRtcpObserver::OnReceiveRtcp, | |
| 68 configuration), | |
| 69 timeout_ms_(event_timeout_ms) {} | |
| 70 | |
| 71 explicit RtpRtcpObserver(unsigned int event_timeout_ms) | 60 explicit RtpRtcpObserver(unsigned int event_timeout_ms) |
| 72 : observation_complete_(EventWrapper::Create()), | 61 : observation_complete_(EventWrapper::Create()), |
| 73 parser_(RtpHeaderParser::Create()), | 62 parser_(RtpHeaderParser::Create()), |
| 74 send_transport_(&crit_, | |
| 75 this, | |
| 76 &RtpRtcpObserver::OnSendRtp, | |
| 77 &RtpRtcpObserver::OnSendRtcp, | |
| 78 FakeNetworkPipe::Config()), | |
| 79 receive_transport_(&crit_, | |
| 80 this, | |
| 81 &RtpRtcpObserver::OnReceiveRtp, | |
| 82 &RtpRtcpObserver::OnReceiveRtcp, | |
| 83 FakeNetworkPipe::Config()), | |
| 84 timeout_ms_(event_timeout_ms) {} | 63 timeout_ms_(event_timeout_ms) {} |
| 85 | 64 |
| 86 enum Action { | |
| 87 SEND_PACKET, | |
| 88 DROP_PACKET, | |
| 89 }; | |
| 90 | |
| 91 virtual Action OnSendRtp(const uint8_t* packet, size_t length) | |
| 92 EXCLUSIVE_LOCKS_REQUIRED(crit_) { | |
| 93 return SEND_PACKET; | |
| 94 } | |
| 95 | |
| 96 virtual Action OnSendRtcp(const uint8_t* packet, size_t length) | |
| 97 EXCLUSIVE_LOCKS_REQUIRED(crit_) { | |
| 98 return SEND_PACKET; | |
| 99 } | |
| 100 | |
| 101 virtual Action OnReceiveRtp(const uint8_t* packet, size_t length) | |
| 102 EXCLUSIVE_LOCKS_REQUIRED(crit_) { | |
| 103 return SEND_PACKET; | |
| 104 } | |
| 105 | |
| 106 virtual Action OnReceiveRtcp(const uint8_t* packet, size_t length) | |
| 107 EXCLUSIVE_LOCKS_REQUIRED(crit_) { | |
| 108 return SEND_PACKET; | |
| 109 } | |
| 110 | |
| 111 private: | |
| 112 class PacketTransport : public test::DirectTransport { | |
| 113 public: | |
| 114 typedef Action (RtpRtcpObserver::*PacketTransportAction)(const uint8_t*, | |
| 115 size_t); | |
| 116 | |
| 117 PacketTransport(rtc::CriticalSection* lock, | |
| 118 RtpRtcpObserver* observer, | |
| 119 PacketTransportAction on_rtp, | |
| 120 PacketTransportAction on_rtcp, | |
| 121 const FakeNetworkPipe::Config& configuration) | |
| 122 : test::DirectTransport(configuration), | |
| 123 crit_(lock), | |
| 124 observer_(observer), | |
| 125 on_rtp_(on_rtp), | |
| 126 on_rtcp_(on_rtcp) {} | |
| 127 | |
| 128 private: | |
| 129 bool SendRtp(const uint8_t* packet, | |
| 130 size_t length, | |
| 131 const PacketOptions& options) override { | |
| 132 EXPECT_FALSE(RtpHeaderParser::IsRtcp(packet, length)); | |
| 133 Action action; | |
| 134 { | |
| 135 rtc::CritScope lock(crit_); | |
| 136 action = (observer_->*on_rtp_)(packet, length); | |
| 137 } | |
| 138 switch (action) { | |
| 139 case DROP_PACKET: | |
| 140 // Drop packet silently. | |
| 141 return true; | |
| 142 case SEND_PACKET: | |
| 143 return test::DirectTransport::SendRtp(packet, length, options); | |
| 144 } | |
| 145 return true; // Will never happen, makes compiler happy. | |
| 146 } | |
| 147 | |
| 148 bool SendRtcp(const uint8_t* packet, size_t length) override { | |
| 149 EXPECT_TRUE(RtpHeaderParser::IsRtcp(packet, length)); | |
| 150 Action action; | |
| 151 { | |
| 152 rtc::CritScope lock(crit_); | |
| 153 action = (observer_->*on_rtcp_)(packet, length); | |
| 154 } | |
| 155 switch (action) { | |
| 156 case DROP_PACKET: | |
| 157 // Drop packet silently. | |
| 158 return true; | |
| 159 case SEND_PACKET: | |
| 160 return test::DirectTransport::SendRtcp(packet, length); | |
| 161 } | |
| 162 return true; // Will never happen, makes compiler happy. | |
| 163 } | |
| 164 | |
| 165 // Pointer to shared lock instance protecting on_rtp_/on_rtcp_ calls. | |
| 166 rtc::CriticalSection* const crit_; | |
| 167 | |
| 168 RtpRtcpObserver* const observer_; | |
| 169 const PacketTransportAction on_rtp_, on_rtcp_; | |
| 170 }; | |
| 171 | |
| 172 protected: | |
| 173 rtc::CriticalSection crit_; | |
| 174 const rtc::scoped_ptr<EventWrapper> observation_complete_; | 65 const rtc::scoped_ptr<EventWrapper> observation_complete_; |
| 175 const rtc::scoped_ptr<RtpHeaderParser> parser_; | 66 const rtc::scoped_ptr<RtpHeaderParser> parser_; |
| 176 PacketTransport send_transport_, receive_transport_; | |
| 177 | 67 |
| 178 private: | 68 private: |
| 179 unsigned int timeout_ms_; | 69 unsigned int timeout_ms_; |
| 180 }; | 70 }; |
| 71 |
| 72 class PacketTransport : public test::DirectTransport { |
| 73 public: |
| 74 enum TransportType { kReceiver, kSender }; |
| 75 |
| 76 PacketTransport(Call* send_call, |
| 77 RtpRtcpObserver* observer, |
| 78 TransportType transport_type, |
| 79 const FakeNetworkPipe::Config& configuration) |
| 80 : test::DirectTransport(configuration, send_call), |
| 81 observer_(observer), |
| 82 transport_type_(transport_type) {} |
| 83 |
| 84 private: |
| 85 bool SendRtp(const uint8_t* packet, |
| 86 size_t length, |
| 87 const PacketOptions& options) override { |
| 88 EXPECT_FALSE(RtpHeaderParser::IsRtcp(packet, length)); |
| 89 RtpRtcpObserver::Action action; |
| 90 { |
| 91 if (transport_type_ == kSender) { |
| 92 action = observer_->OnSendRtp(packet, length); |
| 93 } else { |
| 94 action = observer_->OnReceiveRtp(packet, length); |
| 95 } |
| 96 } |
| 97 switch (action) { |
| 98 case RtpRtcpObserver::DROP_PACKET: |
| 99 // Drop packet silently. |
| 100 return true; |
| 101 case RtpRtcpObserver::SEND_PACKET: |
| 102 return test::DirectTransport::SendRtp(packet, length, options); |
| 103 } |
| 104 return true; // Will never happen, makes compiler happy. |
| 105 } |
| 106 |
| 107 bool SendRtcp(const uint8_t* packet, size_t length) override { |
| 108 EXPECT_TRUE(RtpHeaderParser::IsRtcp(packet, length)); |
| 109 RtpRtcpObserver::Action action; |
| 110 { |
| 111 if (transport_type_ == kSender) { |
| 112 action = observer_->OnSendRtcp(packet, length); |
| 113 } else { |
| 114 action = observer_->OnReceiveRtcp(packet, length); |
| 115 } |
| 116 } |
| 117 switch (action) { |
| 118 case RtpRtcpObserver::DROP_PACKET: |
| 119 // Drop packet silently. |
| 120 return true; |
| 121 case RtpRtcpObserver::SEND_PACKET: |
| 122 return test::DirectTransport::SendRtcp(packet, length); |
| 123 } |
| 124 return true; // Will never happen, makes compiler happy. |
| 125 } |
| 126 |
| 127 RtpRtcpObserver* const observer_; |
| 128 TransportType transport_type_; |
| 129 }; |
| 181 } // namespace test | 130 } // namespace test |
| 182 } // namespace webrtc | 131 } // namespace webrtc |
| 183 | 132 |
| 184 #endif // WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_ | 133 #endif // WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_ |
| OLD | NEW |