rtp_rtcp_observer.h 4.0 KB

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