rtp_rtcp_observer.h 4.1 KB

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