1 /* 2 * Copyright (c) 2012 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 11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "webrtc/common_types.h" 13 #include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h" 14 #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h" 15 #include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h" 16 #include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h" 17 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" 18 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" 19 #include "webrtc/system_wrappers/interface/scoped_ptr.h" 20 21 namespace webrtc { 22 23 // This class sends all its packet straight to the provided RtpRtcp module. 24 // with optional packet loss. 25 class LoopBackTransport : public webrtc::Transport { 26 public: 27 LoopBackTransport() 28 : _count(0), 29 _packetLoss(0), 30 rtp_payload_registry_(NULL), 31 rtp_receiver_(NULL), 32 _rtpRtcpModule(NULL) { 33 } 34 void SetSendModule(RtpRtcp* rtpRtcpModule, 35 RTPPayloadRegistry* payload_registry, 36 RtpReceiver* receiver, 37 ReceiveStatistics* receive_statistics) { 38 _rtpRtcpModule = rtpRtcpModule; 39 rtp_payload_registry_ = payload_registry; 40 rtp_receiver_ = receiver; 41 receive_statistics_ = receive_statistics; 42 } 43 void DropEveryNthPacket(int n) { 44 _packetLoss = n; 45 } 46 virtual int SendPacket(int channel, const void *data, int len) { 47 _count++; 48 if (_packetLoss > 0) { 49 if ((_count % _packetLoss) == 0) { 50 return len; 51 } 52 } 53 RTPHeader header; 54 scoped_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create()); 55 if (!parser->Parse(static_cast<const uint8_t*>(data), len, &header)) { 56 return -1; 57 } 58 PayloadUnion payload_specific; 59 if (!rtp_payload_registry_->GetPayloadSpecifics( 60 header.payloadType, &payload_specific)) { 61 return -1; 62 } 63 receive_statistics_->IncomingPacket(header, len, false); 64 if (!rtp_receiver_->IncomingRtpPacket(header, 65 static_cast<const uint8_t*>(data), 66 len, payload_specific, true)) { 67 return -1; 68 } 69 return len; 70 } 71 virtual int SendRTCPPacket(int channel, const void *data, int len) { 72 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, len) < 0) { 73 return -1; 74 } 75 return len; 76 } 77 private: 78 int _count; 79 int _packetLoss; 80 ReceiveStatistics* receive_statistics_; 81 RTPPayloadRegistry* rtp_payload_registry_; 82 RtpReceiver* rtp_receiver_; 83 RtpRtcp* _rtpRtcpModule; 84 }; 85 86 class TestRtpReceiver : public NullRtpData { 87 public: 88 89 virtual int32_t OnReceivedPayloadData( 90 const uint8_t* payloadData, 91 const uint16_t payloadSize, 92 const webrtc::WebRtcRTPHeader* rtpHeader) { 93 EXPECT_LE(payloadSize, sizeof(_payloadData)); 94 memcpy(_payloadData, payloadData, payloadSize); 95 memcpy(&_rtpHeader, rtpHeader, sizeof(_rtpHeader)); 96 _payloadSize = payloadSize; 97 return 0; 98 } 99 100 const uint8_t* payload_data() const { 101 return _payloadData; 102 } 103 104 uint16_t payload_size() const { 105 return _payloadSize; 106 } 107 108 webrtc::WebRtcRTPHeader rtp_header() const { 109 return _rtpHeader; 110 } 111 112 private: 113 uint8_t _payloadData[1500]; 114 uint16_t _payloadSize; 115 webrtc::WebRtcRTPHeader _rtpHeader; 116 }; 117 118 } // namespace webrtc 119