Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / cast / net / rtp / packet_storage_unittest.cc
blobddebb7ea57396306d22980f57ce7d22d35a653ec
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/cast/net/rtp/packet_storage.h"
7 #include <stdint.h>
9 #include <algorithm>
10 #include <vector>
12 #include "base/test/simple_test_tick_clock.h"
13 #include "base/time/time.h"
14 #include "media/cast/cast_defines.h"
15 #include "testing/gmock/include/gmock/gmock.h"
17 namespace media {
18 namespace cast {
20 static const size_t kStoredFrames = 10;
22 // Generate |number_of_frames| and store into |*storage|.
23 // First frame has 1 packet, second frame has 2 packets, etc.
24 static void StoreFrames(size_t number_of_frames,
25 uint32 first_frame_id,
26 PacketStorage* storage) {
27 const base::TimeTicks kTicks;
28 const int kSsrc = 1;
29 for (size_t i = 0; i < number_of_frames; ++i) {
30 SendPacketVector packets;
31 // First frame has 1 packet, second frame has 2 packets, etc.
32 const size_t kNumberOfPackets = i + 1;
33 for (size_t j = 0; j < kNumberOfPackets; ++j) {
34 Packet test_packet(1, 0);
35 packets.push_back(
36 std::make_pair(
37 PacedPacketSender::MakePacketKey(kTicks, kSsrc, j),
38 new base::RefCountedData<Packet>(test_packet)));
40 storage->StoreFrame(first_frame_id, packets);
41 ++first_frame_id;
45 TEST(PacketStorageTest, NumberOfStoredFrames) {
46 PacketStorage storage;
48 uint32 frame_id = 0;
49 frame_id = ~frame_id; // The maximum value of uint32.
50 StoreFrames(kMaxUnackedFrames / 2, frame_id, &storage);
51 EXPECT_EQ(static_cast<size_t>(kMaxUnackedFrames / 2),
52 storage.GetNumberOfStoredFrames());
55 TEST(PacketStorageTest, GetFrameWrapAround8bits) {
56 PacketStorage storage;
58 const uint32 kFirstFrameId = 250;
59 StoreFrames(kStoredFrames, kFirstFrameId, &storage);
60 EXPECT_EQ(std::min<size_t>(kMaxUnackedFrames, kStoredFrames),
61 storage.GetNumberOfStoredFrames());
63 // Expect we get the correct frames by looking at the number of
64 // packets.
65 uint32 frame_id = kFirstFrameId;
66 for (size_t i = 0; i < kStoredFrames; ++i) {
67 ASSERT_TRUE(storage.GetFrame8(frame_id));
68 EXPECT_EQ(i + 1, storage.GetFrame8(frame_id)->size());
69 ++frame_id;
73 TEST(PacketStorageTest, GetFrameWrapAround32bits) {
74 PacketStorage storage;
76 // First frame ID is close to the maximum value of uint32.
77 uint32 first_frame_id = 0xffffffff - 5;
78 StoreFrames(kStoredFrames, first_frame_id, &storage);
79 EXPECT_EQ(std::min<size_t>(kMaxUnackedFrames, kStoredFrames),
80 storage.GetNumberOfStoredFrames());
82 // Expect we get the correct frames by looking at the number of
83 // packets.
84 uint32 frame_id = first_frame_id;
85 for (size_t i = 0; i < kStoredFrames; ++i) {
86 ASSERT_TRUE(storage.GetFrame8(frame_id));
87 EXPECT_EQ(i + 1, storage.GetFrame8(frame_id)->size());
88 ++frame_id;
92 TEST(PacketStorageTest, FramesReleased) {
93 PacketStorage storage;
95 const uint32 kFirstFrameId = 0;
96 StoreFrames(5, kFirstFrameId, &storage);
97 EXPECT_EQ(std::min<size_t>(kMaxUnackedFrames, 5),
98 storage.GetNumberOfStoredFrames());
100 for (uint32 frame_id = kFirstFrameId; frame_id < kFirstFrameId + 5;
101 ++frame_id) {
102 EXPECT_TRUE(storage.GetFrame8(frame_id));
105 storage.ReleaseFrame(kFirstFrameId + 2);
106 EXPECT_EQ(4u, storage.GetNumberOfStoredFrames());
107 EXPECT_FALSE(storage.GetFrame8(kFirstFrameId + 2));
109 storage.ReleaseFrame(kFirstFrameId + 0);
110 EXPECT_EQ(3u, storage.GetNumberOfStoredFrames());
111 EXPECT_FALSE(storage.GetFrame8(kFirstFrameId + 0));
113 storage.ReleaseFrame(kFirstFrameId + 3);
114 EXPECT_EQ(2u, storage.GetNumberOfStoredFrames());
115 EXPECT_FALSE(storage.GetFrame8(kFirstFrameId + 3));
117 storage.ReleaseFrame(kFirstFrameId + 4);
118 EXPECT_EQ(1u, storage.GetNumberOfStoredFrames());
119 EXPECT_FALSE(storage.GetFrame8(kFirstFrameId + 4));
121 storage.ReleaseFrame(kFirstFrameId + 1);
122 EXPECT_EQ(0u, storage.GetNumberOfStoredFrames());
123 EXPECT_FALSE(storage.GetFrame8(kFirstFrameId + 1));
126 } // namespace cast
127 } // namespace media