Home | History | Annotate | Download | only in test
      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_TEST_RTP_RTCP_OBSERVER_H_
     11 #define WEBRTC_TEST_RTP_RTCP_OBSERVER_H_
     12 
     13 #include <map>
     14 #include <vector>
     15 
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 #include "webrtc/base/criticalsection.h"
     19 #include "webrtc/base/event.h"
     20 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
     21 #include "webrtc/test/constants.h"
     22 #include "webrtc/test/direct_transport.h"
     23 #include "webrtc/typedefs.h"
     24 #include "webrtc/video_send_stream.h"
     25 
     26 namespace webrtc {
     27 namespace test {
     28 
     29 class PacketTransport;
     30 
     31 class RtpRtcpObserver {
     32  public:
     33   enum Action {
     34     SEND_PACKET,
     35     DROP_PACKET,
     36   };
     37 
     38   virtual ~RtpRtcpObserver() {}
     39 
     40   virtual bool Wait() { return observation_complete_.Wait(timeout_ms_); }
     41 
     42   virtual Action OnSendRtp(const uint8_t* packet, size_t length) {
     43     return SEND_PACKET;
     44   }
     45 
     46   virtual Action OnSendRtcp(const uint8_t* packet, size_t length) {
     47     return SEND_PACKET;
     48   }
     49 
     50   virtual Action OnReceiveRtp(const uint8_t* packet, size_t length) {
     51     return SEND_PACKET;
     52   }
     53 
     54   virtual Action OnReceiveRtcp(const uint8_t* packet, size_t length) {
     55     return SEND_PACKET;
     56   }
     57 
     58  protected:
     59   explicit RtpRtcpObserver(int event_timeout_ms)
     60       : observation_complete_(false, false),
     61         parser_(RtpHeaderParser::Create()),
     62         timeout_ms_(event_timeout_ms) {
     63     parser_->RegisterRtpHeaderExtension(kRtpExtensionTransmissionTimeOffset,
     64                                         kTOffsetExtensionId);
     65     parser_->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteSendTime,
     66                                         kAbsSendTimeExtensionId);
     67     parser_->RegisterRtpHeaderExtension(kRtpExtensionTransportSequenceNumber,
     68                                         kTransportSequenceNumberExtensionId);
     69   }
     70 
     71   rtc::Event observation_complete_;
     72   const rtc::scoped_ptr<RtpHeaderParser> parser_;
     73 
     74  private:
     75   const int timeout_ms_;
     76 };
     77 
     78 class PacketTransport : public test::DirectTransport {
     79  public:
     80   enum TransportType { kReceiver, kSender };
     81 
     82   PacketTransport(Call* send_call,
     83                   RtpRtcpObserver* observer,
     84                   TransportType transport_type,
     85                   const FakeNetworkPipe::Config& configuration)
     86       : test::DirectTransport(configuration, send_call),
     87         observer_(observer),
     88         transport_type_(transport_type) {}
     89 
     90  private:
     91   bool SendRtp(const uint8_t* packet,
     92                size_t length,
     93                const PacketOptions& options) override {
     94     EXPECT_FALSE(RtpHeaderParser::IsRtcp(packet, length));
     95     RtpRtcpObserver::Action action;
     96     {
     97       if (transport_type_ == kSender) {
     98         action = observer_->OnSendRtp(packet, length);
     99       } else {
    100         action = observer_->OnReceiveRtp(packet, length);
    101       }
    102     }
    103     switch (action) {
    104       case RtpRtcpObserver::DROP_PACKET:
    105         // Drop packet silently.
    106         return true;
    107       case RtpRtcpObserver::SEND_PACKET:
    108         return test::DirectTransport::SendRtp(packet, length, options);
    109     }
    110     return true;  // Will never happen, makes compiler happy.
    111   }
    112 
    113   bool SendRtcp(const uint8_t* packet, size_t length) override {
    114     EXPECT_TRUE(RtpHeaderParser::IsRtcp(packet, length));
    115     RtpRtcpObserver::Action action;
    116     {
    117       if (transport_type_ == kSender) {
    118         action = observer_->OnSendRtcp(packet, length);
    119       } else {
    120         action = observer_->OnReceiveRtcp(packet, length);
    121       }
    122     }
    123     switch (action) {
    124       case RtpRtcpObserver::DROP_PACKET:
    125         // Drop packet silently.
    126         return true;
    127       case RtpRtcpObserver::SEND_PACKET:
    128         return test::DirectTransport::SendRtcp(packet, length);
    129     }
    130     return true;  // Will never happen, makes compiler happy.
    131   }
    132 
    133   RtpRtcpObserver* const observer_;
    134   TransportType transport_type_;
    135 };
    136 }  // namespace test
    137 }  // namespace webrtc
    138 
    139 #endif  // WEBRTC_TEST_RTP_RTCP_OBSERVER_H_
    140