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 | */ |
| 10 | #ifndef WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_ |
| 11 | #define WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_ |
| 12 | |
| 13 | #include <map> |
| 14 | #include <vector> |
| 15 | |
stefan@webrtc.org | 69969e2 | 2013-11-15 12:32:15 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h" |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 17 | #include "webrtc/typedefs.h" |
pbos@webrtc.org | 16e03b7 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 18 | #include "webrtc/video_send_stream.h" |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace test { |
| 22 | |
| 23 | class RtpRtcpObserver { |
| 24 | public: |
| 25 | newapi::Transport* SendTransport() { |
| 26 | return &send_transport_; |
| 27 | } |
| 28 | |
| 29 | newapi::Transport* ReceiveTransport() { |
| 30 | return &receive_transport_; |
| 31 | } |
| 32 | |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 33 | void SetReceivers(PacketReceiver* send_transport_receiver, |
| 34 | PacketReceiver* receive_transport_receiver) { |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 35 | send_transport_.SetReceiver(send_transport_receiver); |
| 36 | receive_transport_.SetReceiver(receive_transport_receiver); |
| 37 | } |
| 38 | |
| 39 | void StopSending() { |
| 40 | send_transport_.StopSending(); |
| 41 | receive_transport_.StopSending(); |
| 42 | } |
| 43 | |
stefan@webrtc.org | 69969e2 | 2013-11-15 12:32:15 +0000 | [diff] [blame] | 44 | virtual EventTypeWrapper Wait() { |
| 45 | return observation_complete_->Wait(timeout_ms_); |
| 46 | } |
pbos@webrtc.org | 6917e19 | 2013-09-19 14:22:12 +0000 | [diff] [blame] | 47 | |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 48 | protected: |
stefan@webrtc.org | b082ade | 2013-11-18 11:45:11 +0000 | [diff] [blame^] | 49 | RtpRtcpObserver(unsigned int event_timeout_ms, int delay_ms) |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 50 | : lock_(CriticalSectionWrapper::CreateCriticalSection()), |
pbos@webrtc.org | 6917e19 | 2013-09-19 14:22:12 +0000 | [diff] [blame] | 51 | observation_complete_(EventWrapper::Create()), |
stefan@webrtc.org | 69969e2 | 2013-11-15 12:32:15 +0000 | [diff] [blame] | 52 | parser_(RtpHeaderParser::Create()), |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 53 | send_transport_(lock_.get(), |
pbos@webrtc.org | 6917e19 | 2013-09-19 14:22:12 +0000 | [diff] [blame] | 54 | this, |
| 55 | &RtpRtcpObserver::OnSendRtp, |
stefan@webrtc.org | b082ade | 2013-11-18 11:45:11 +0000 | [diff] [blame^] | 56 | &RtpRtcpObserver::OnSendRtcp, |
| 57 | delay_ms), |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 58 | receive_transport_(lock_.get(), |
pbos@webrtc.org | 6917e19 | 2013-09-19 14:22:12 +0000 | [diff] [blame] | 59 | this, |
| 60 | &RtpRtcpObserver::OnReceiveRtp, |
stefan@webrtc.org | b082ade | 2013-11-18 11:45:11 +0000 | [diff] [blame^] | 61 | &RtpRtcpObserver::OnReceiveRtcp, |
| 62 | delay_ms), |
| 63 | timeout_ms_(event_timeout_ms) {} |
| 64 | |
| 65 | explicit RtpRtcpObserver(unsigned int event_timeout_ms) |
| 66 | : lock_(CriticalSectionWrapper::CreateCriticalSection()), |
| 67 | observation_complete_(EventWrapper::Create()), |
| 68 | send_transport_(lock_.get(), |
| 69 | this, |
| 70 | &RtpRtcpObserver::OnSendRtp, |
| 71 | &RtpRtcpObserver::OnSendRtcp, |
| 72 | 0), |
| 73 | receive_transport_(lock_.get(), |
| 74 | this, |
| 75 | &RtpRtcpObserver::OnReceiveRtp, |
| 76 | &RtpRtcpObserver::OnReceiveRtcp, |
| 77 | 0), |
pbos@webrtc.org | 6917e19 | 2013-09-19 14:22:12 +0000 | [diff] [blame] | 78 | timeout_ms_(event_timeout_ms) {} |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 79 | |
| 80 | enum Action { |
| 81 | SEND_PACKET, |
| 82 | DROP_PACKET, |
| 83 | }; |
| 84 | |
| 85 | virtual Action OnSendRtp(const uint8_t* packet, size_t length) { |
| 86 | return SEND_PACKET; |
| 87 | } |
| 88 | |
| 89 | virtual Action OnSendRtcp(const uint8_t* packet, size_t length) { |
| 90 | return SEND_PACKET; |
| 91 | } |
| 92 | |
| 93 | virtual Action OnReceiveRtp(const uint8_t* packet, size_t length) { |
| 94 | return SEND_PACKET; |
| 95 | } |
| 96 | |
| 97 | virtual Action OnReceiveRtcp(const uint8_t* packet, size_t length) { |
| 98 | return SEND_PACKET; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | private: |
| 103 | class PacketTransport : public test::DirectTransport { |
| 104 | public: |
| 105 | typedef Action (RtpRtcpObserver::*PacketTransportAction)(const uint8_t*, |
| 106 | size_t); |
stefan@webrtc.org | b082ade | 2013-11-18 11:45:11 +0000 | [diff] [blame^] | 107 | |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 108 | PacketTransport(CriticalSectionWrapper* lock, |
| 109 | RtpRtcpObserver* observer, |
| 110 | PacketTransportAction on_rtp, |
stefan@webrtc.org | b082ade | 2013-11-18 11:45:11 +0000 | [diff] [blame^] | 111 | PacketTransportAction on_rtcp, |
| 112 | int delay_ms) |
| 113 | : test::DirectTransport(delay_ms), |
| 114 | lock_(lock), |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 115 | observer_(observer), |
| 116 | on_rtp_(on_rtp), |
| 117 | on_rtcp_(on_rtcp) {} |
| 118 | |
| 119 | private: |
| 120 | virtual bool SendRTP(const uint8_t* packet, size_t length) OVERRIDE { |
| 121 | Action action; |
| 122 | { |
| 123 | CriticalSectionScoped crit_(lock_); |
| 124 | action = (observer_->*on_rtp_)(packet, length); |
| 125 | } |
| 126 | switch (action) { |
| 127 | case DROP_PACKET: |
| 128 | // Drop packet silently. |
| 129 | return true; |
| 130 | case SEND_PACKET: |
| 131 | return test::DirectTransport::SendRTP(packet, length); |
| 132 | } |
pbos@webrtc.org | d5f4c15 | 2013-08-19 16:35:36 +0000 | [diff] [blame] | 133 | return true; // Will never happen, makes compiler happy. |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 134 | } |
pbos@webrtc.org | d5f4c15 | 2013-08-19 16:35:36 +0000 | [diff] [blame] | 135 | |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 136 | virtual bool SendRTCP(const uint8_t* packet, size_t length) OVERRIDE { |
| 137 | Action action; |
| 138 | { |
| 139 | CriticalSectionScoped crit_(lock_); |
| 140 | action = (observer_->*on_rtcp_)(packet, length); |
| 141 | } |
| 142 | switch (action) { |
| 143 | case DROP_PACKET: |
| 144 | // Drop packet silently. |
| 145 | return true; |
| 146 | case SEND_PACKET: |
| 147 | return test::DirectTransport::SendRTCP(packet, length); |
| 148 | } |
pbos@webrtc.org | d5f4c15 | 2013-08-19 16:35:36 +0000 | [diff] [blame] | 149 | return true; // Will never happen, makes compiler happy. |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // Pointer to shared lock instance protecting on_rtp_/on_rtcp_ calls. |
| 153 | CriticalSectionWrapper* lock_; |
| 154 | |
| 155 | RtpRtcpObserver* observer_; |
| 156 | PacketTransportAction on_rtp_, on_rtcp_; |
| 157 | }; |
| 158 | |
| 159 | protected: |
| 160 | scoped_ptr<CriticalSectionWrapper> lock_; |
pbos@webrtc.org | 6917e19 | 2013-09-19 14:22:12 +0000 | [diff] [blame] | 161 | scoped_ptr<EventWrapper> observation_complete_; |
stefan@webrtc.org | 69969e2 | 2013-11-15 12:32:15 +0000 | [diff] [blame] | 162 | scoped_ptr<RtpHeaderParser> parser_; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 163 | |
| 164 | private: |
| 165 | PacketTransport send_transport_, receive_transport_; |
pbos@webrtc.org | 6917e19 | 2013-09-19 14:22:12 +0000 | [diff] [blame] | 166 | unsigned int timeout_ms_; |
pbos@webrtc.org | e7f056e | 2013-08-19 16:09:34 +0000 | [diff] [blame] | 167 | }; |
| 168 | } // namespace test |
| 169 | } // namespace webrtc |
| 170 | |
| 171 | #endif // WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_ |