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
11#include <map>
12
13#include "testing/gtest/include/gtest/gtest.h"
14#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
15#include "webrtc/system_wrappers/interface/scoped_ptr.h"
16#include "webrtc/test/rtp_file_reader.h"
17#include "webrtc/test/testsupport/fileutils.h"
18
19namespace webrtc {
20
21class TestRtpFileReader : public ::testing::Test {
22 public:
23  void Init(const std::string& filename) {
24    std::string filepath =
25        test::ResourcePath("video_coding/" + filename, "rtp");
26    rtp_packet_source_.reset(
27        test::RtpFileReader::Create(test::RtpFileReader::kRtpDump, filepath));
28    ASSERT_TRUE(rtp_packet_source_.get() != NULL);
29  }
30
31  int CountRtpPackets() {
32    test::RtpFileReader::Packet packet;
33    int c = 0;
34    while (rtp_packet_source_->NextPacket(&packet))
35      c++;
36    return c;
37  }
38
39 private:
40  scoped_ptr<test::RtpFileReader> rtp_packet_source_;
41};
42
43TEST_F(TestRtpFileReader, Test60Packets) {
44  Init("pltype103");
45  EXPECT_EQ(60, CountRtpPackets());
46}
47
48typedef std::map<uint32_t, int> PacketsPerSsrc;
49
50class TestPcapFileReader : public ::testing::Test {
51 public:
52  void Init(const std::string& filename) {
53    std::string filepath =
54        test::ResourcePath("video_coding/" + filename, "pcap");
55    rtp_packet_source_.reset(
56        test::RtpFileReader::Create(test::RtpFileReader::kPcap, filepath));
57    ASSERT_TRUE(rtp_packet_source_.get() != NULL);
58  }
59
60  int CountRtpPackets() {
61    int c = 0;
62    test::RtpFileReader::Packet packet;
63    while (rtp_packet_source_->NextPacket(&packet))
64      c++;
65    return c;
66  }
67
68  PacketsPerSsrc CountRtpPacketsPerSsrc() {
69    PacketsPerSsrc pps;
70    test::RtpFileReader::Packet packet;
71    while (rtp_packet_source_->NextPacket(&packet)) {
72      RtpUtility::RtpHeaderParser rtp_header_parser(packet.data, packet.length);
73      webrtc::RTPHeader header;
74      if (!rtp_header_parser.RTCP() && rtp_header_parser.Parse(header, NULL)) {
75        pps[header.ssrc]++;
76      }
77    }
78    return pps;
79  }
80
81 private:
82  scoped_ptr<test::RtpFileReader> rtp_packet_source_;
83};
84
85TEST_F(TestPcapFileReader, TestEthernetIIFrame) {
86  Init("frame-ethernet-ii");
87  EXPECT_EQ(368, CountRtpPackets());
88}
89
90TEST_F(TestPcapFileReader, TestLoopbackFrame) {
91  Init("frame-loopback");
92  EXPECT_EQ(491, CountRtpPackets());
93}
94
95TEST_F(TestPcapFileReader, TestTwoSsrc) {
96  Init("ssrcs-2");
97  PacketsPerSsrc pps = CountRtpPacketsPerSsrc();
98  EXPECT_EQ(2UL, pps.size());
99  EXPECT_EQ(370, pps[0x78d48f61]);
100  EXPECT_EQ(60, pps[0xae94130b]);
101}
102
103TEST_F(TestPcapFileReader, TestThreeSsrc) {
104  Init("ssrcs-3");
105  PacketsPerSsrc pps = CountRtpPacketsPerSsrc();
106  EXPECT_EQ(3UL, pps.size());
107  EXPECT_EQ(162, pps[0x938c5eaa]);
108  EXPECT_EQ(113, pps[0x59fe6ef0]);
109  EXPECT_EQ(61, pps[0xed2bd2ac]);
110}
111}  // namespace webrtc
112