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
21namespace webrtc {
22
23// This class sends all its packet straight to the provided RtpRtcp module.
24// with optional packet loss.
25class 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),
56                       static_cast<size_t>(len),
57                       &header)) {
58      return -1;
59    }
60    PayloadUnion payload_specific;
61    if (!rtp_payload_registry_->GetPayloadSpecifics(
62        header.payloadType, &payload_specific)) {
63      return -1;
64    }
65    receive_statistics_->IncomingPacket(header, len, false);
66    if (!rtp_receiver_->IncomingRtpPacket(header,
67                                          static_cast<const uint8_t*>(data),
68                                          len, payload_specific, true)) {
69      return -1;
70    }
71    return len;
72  }
73  virtual int SendRTCPPacket(int channel, const void *data, int len) {
74    if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, len) < 0) {
75      return -1;
76    }
77    return len;
78  }
79 private:
80  int _count;
81  int _packetLoss;
82  ReceiveStatistics* receive_statistics_;
83  RTPPayloadRegistry* rtp_payload_registry_;
84  RtpReceiver* rtp_receiver_;
85  RtpRtcp* _rtpRtcpModule;
86};
87
88class TestRtpReceiver : public NullRtpData {
89 public:
90
91  virtual int32_t OnReceivedPayloadData(
92      const uint8_t* payloadData,
93      const uint16_t payloadSize,
94      const webrtc::WebRtcRTPHeader* rtpHeader) {
95    EXPECT_LE(payloadSize, sizeof(_payloadData));
96    memcpy(_payloadData, payloadData, payloadSize);
97    memcpy(&_rtpHeader, rtpHeader, sizeof(_rtpHeader));
98    _payloadSize = payloadSize;
99    return 0;
100  }
101
102  const uint8_t* payload_data() const {
103    return _payloadData;
104  }
105
106  uint16_t payload_size() const {
107    return _payloadSize;
108  }
109
110  webrtc::WebRtcRTPHeader rtp_header() const {
111    return _rtpHeader;
112  }
113
114 private:
115  uint8_t _payloadData[1500];
116  uint16_t _payloadSize;
117  webrtc::WebRtcRTPHeader _rtpHeader;
118};
119
120}  // namespace webrtc
121