blob: 50fd86485f3765d52a1a5250dc719ccade28f53f [file] [log] [blame]
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +00001/*
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
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +000016#include "testing/gtest/include/gtest/gtest.h"
17
Peter Boströmf2f82832015-05-01 13:00:41 +020018#include "webrtc/base/criticalsection.h"
stefan@webrtc.org69969e22013-11-15 12:32:15 +000019#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000020#include "webrtc/test/direct_transport.h"
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +000021#include "webrtc/typedefs.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000022#include "webrtc/video_send_stream.h"
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +000023
24namespace webrtc {
25namespace test {
26
stefanf116bd02015-10-27 08:29:42 -070027class PacketTransport;
28
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +000029class RtpRtcpObserver {
30 public:
stefanf116bd02015-10-27 08:29:42 -070031 enum Action {
32 SEND_PACKET,
33 DROP_PACKET,
34 };
35
pbos@webrtc.orgb3cc78d2013-11-21 11:42:02 +000036 virtual ~RtpRtcpObserver() {}
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +000037
stefan@webrtc.org69969e22013-11-15 12:32:15 +000038 virtual EventTypeWrapper Wait() {
pbos@webrtc.org13d38a12013-11-28 11:59:31 +000039 EventTypeWrapper result = observation_complete_->Wait(timeout_ms_);
pbos@webrtc.org13d38a12013-11-28 11:59:31 +000040 return result;
stefan@webrtc.org69969e22013-11-15 12:32:15 +000041 }
pbos@webrtc.org6917e192013-09-19 14:22:12 +000042
stefanf116bd02015-10-27 08:29:42 -070043 virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
44 return SEND_PACKET;
45 }
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000046
stefanf116bd02015-10-27 08:29:42 -070047 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:
stefan@webrtc.orgb082ade2013-11-18 11:45:11 +000060 explicit RtpRtcpObserver(unsigned int event_timeout_ms)
Peter Boströmf2f82832015-05-01 13:00:41 +020061 : observation_complete_(EventWrapper::Create()),
stefan@webrtc.org28bf50f2013-11-18 11:58:24 +000062 parser_(RtpHeaderParser::Create()),
pbos@webrtc.org6917e192013-09-19 14:22:12 +000063 timeout_ms_(event_timeout_ms) {}
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +000064
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000065 const rtc::scoped_ptr<EventWrapper> observation_complete_;
66 const rtc::scoped_ptr<RtpHeaderParser> parser_;
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +000067
68 private:
pbos@webrtc.org6917e192013-09-19 14:22:12 +000069 unsigned int timeout_ms_;
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +000070};
stefanf116bd02015-10-27 08:29:42 -070071
72class 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};
pbos@webrtc.orge7f056e2013-08-19 16:09:34 +0000130} // namespace test
131} // namespace webrtc
132
133#endif // WEBRTC_VIDEO_ENGINE_TEST_COMMON_RTP_RTCP_OBSERVER_H_