Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / cast / logging / serialize_deserialize_test.cc
blobf1766d040cd23a4b9e47d54e871ab6af00b1ff18
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 // Joint LogSerializer and LogDeserializer testing to make sure they stay in
6 // sync.
8 #include "base/memory/scoped_ptr.h"
9 #include "media/cast/logging/log_deserializer.h"
10 #include "media/cast/logging/log_serializer.h"
11 #include "media/cast/logging/logging_defines.h"
12 #include "media/cast/logging/proto/proto_utils.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using media::cast::proto::AggregatedFrameEvent;
16 using media::cast::proto::AggregatedPacketEvent;
17 using media::cast::proto::BasePacketEvent;
18 using media::cast::proto::LogMetadata;
20 namespace {
22 const media::cast::CastLoggingEvent kVideoFrameEvents[] = {
23 media::cast::FRAME_CAPTURE_BEGIN, media::cast::FRAME_CAPTURE_END,
24 media::cast::FRAME_ENCODED, media::cast::FRAME_DECODED,
25 media::cast::FRAME_PLAYOUT };
27 const media::cast::CastLoggingEvent kVideoPacketEvents[] = {
28 media::cast::PACKET_SENT_TO_NETWORK, media::cast::PACKET_RECEIVED};
30 // The frame event fields cycle through these numbers.
31 const int kWidth[] = {1280, 1280, 1280, 1280, 1920, 1920, 1920, 1920};
32 const int kHeight[] = {720, 720, 720, 720, 1080, 1080, 1080, 1080};
33 const int kEncodedFrameSize[] = {512, 425, 399, 400, 237};
34 const int64 kDelayMillis[] = {15, 4, 8, 42, 23, 16};
35 const int kEncoderCPUPercentUtilized[] = {10, 9, 42, 3, 11, 12, 15, 7};
36 const int kIdealizedBitratePercentUtilized[] = {9, 9, 9, 15, 36, 38, 35, 40};
38 const int kMaxSerializedBytes = 10000;
42 namespace media {
43 namespace cast {
45 class SerializeDeserializeTest : public ::testing::Test {
46 protected:
47 SerializeDeserializeTest()
48 : serialized_(new char[kMaxSerializedBytes]), output_bytes_(0) {}
50 ~SerializeDeserializeTest() override {}
52 void Init() {
53 metadata_.set_first_rtp_timestamp(12345678 * 90);
54 metadata_.set_is_audio(false);
55 metadata_.set_num_frame_events(10);
56 metadata_.set_num_packet_events(10);
58 int64 event_time_ms = 0;
59 // Insert frame and packet events with RTP timestamps 0, 90, 180, ...
60 for (int i = 0; i < metadata_.num_frame_events(); i++) {
61 linked_ptr<AggregatedFrameEvent> frame_event(new AggregatedFrameEvent);
62 frame_event->set_relative_rtp_timestamp(i * 90);
63 for (uint32 event_index = 0; event_index < arraysize(kVideoFrameEvents);
64 ++event_index) {
65 frame_event->add_event_type(
66 ToProtoEventType(kVideoFrameEvents[event_index]));
67 frame_event->add_event_timestamp_ms(event_time_ms);
68 event_time_ms += 1024;
70 frame_event->set_width(kWidth[i % arraysize(kWidth)]);
71 frame_event->set_height(kHeight[i % arraysize(kHeight)]);
72 frame_event->set_encoded_frame_size(
73 kEncodedFrameSize[i % arraysize(kEncodedFrameSize)]);
74 frame_event->set_delay_millis(kDelayMillis[i % arraysize(kDelayMillis)]);
75 frame_event->set_encoder_cpu_percent_utilized(kEncoderCPUPercentUtilized[
76 i % arraysize(kEncoderCPUPercentUtilized)]);
77 frame_event->set_idealized_bitrate_percent_utilized(
78 kIdealizedBitratePercentUtilized[
79 i % arraysize(kIdealizedBitratePercentUtilized)]);
81 frame_event_list_.push_back(frame_event);
84 event_time_ms = 0;
85 int packet_id = 0;
86 for (int i = 0; i < metadata_.num_packet_events(); i++) {
87 linked_ptr<AggregatedPacketEvent> packet_event(new AggregatedPacketEvent);
88 packet_event->set_relative_rtp_timestamp(i * 90);
89 for (int j = 0; j < 10; j++) {
90 BasePacketEvent* base_event = packet_event->add_base_packet_event();
91 base_event->set_packet_id(packet_id);
92 packet_id++;
93 for (uint32 event_index = 0;
94 event_index < arraysize(kVideoPacketEvents);
95 ++event_index) {
96 base_event->add_event_type(
97 ToProtoEventType(kVideoPacketEvents[event_index]));
98 base_event->add_event_timestamp_ms(event_time_ms);
99 event_time_ms += 256;
102 packet_event_list_.push_back(packet_event);
106 void Verify(const DeserializedLog& video_log) {
107 const LogMetadata& returned_metadata = video_log.metadata;
108 const FrameEventMap& returned_frame_events = video_log.frame_events;
109 const PacketEventMap& returned_packet_events = video_log.packet_events;
111 EXPECT_EQ(metadata_.SerializeAsString(),
112 returned_metadata.SerializeAsString());
114 // Check that the returned map is equal to the original map.
115 EXPECT_EQ(frame_event_list_.size(), returned_frame_events.size());
116 for (FrameEventMap::const_iterator frame_it = returned_frame_events.begin();
117 frame_it != returned_frame_events.end();
118 ++frame_it) {
119 FrameEventList::iterator original_it = frame_event_list_.begin();
120 ASSERT_NE(frame_event_list_.end(), original_it);
121 // Compare protos by serializing and checking the bytes.
122 EXPECT_EQ((*original_it)->SerializeAsString(),
123 frame_it->second->SerializeAsString());
124 frame_event_list_.erase(frame_event_list_.begin());
126 EXPECT_TRUE(frame_event_list_.empty());
128 EXPECT_EQ(packet_event_list_.size(), returned_packet_events.size());
129 for (PacketEventMap::const_iterator packet_it =
130 returned_packet_events.begin();
131 packet_it != returned_packet_events.end();
132 ++packet_it) {
133 PacketEventList::iterator original_it = packet_event_list_.begin();
134 ASSERT_NE(packet_event_list_.end(), original_it);
135 // Compare protos by serializing and checking the bytes.
136 EXPECT_EQ((*original_it)->SerializeAsString(),
137 packet_it->second->SerializeAsString());
138 packet_event_list_.erase(packet_event_list_.begin());
140 EXPECT_TRUE(packet_event_list_.empty());
143 LogMetadata metadata_;
144 FrameEventList frame_event_list_;
145 PacketEventList packet_event_list_;
146 scoped_ptr<char[]> serialized_;
147 int output_bytes_;
150 TEST_F(SerializeDeserializeTest, Uncompressed) {
151 bool compressed = false;
152 Init();
154 bool success = SerializeEvents(metadata_,
155 frame_event_list_,
156 packet_event_list_,
157 compressed,
158 kMaxSerializedBytes,
159 serialized_.get(),
160 &output_bytes_);
161 ASSERT_TRUE(success);
162 ASSERT_GT(output_bytes_, 0);
164 DeserializedLog audio_log;
165 DeserializedLog video_log;
166 success = DeserializeEvents(
167 serialized_.get(), output_bytes_, compressed, &audio_log, &video_log);
168 ASSERT_TRUE(success);
170 Verify(video_log);
173 TEST_F(SerializeDeserializeTest, UncompressedInsufficientSpace) {
174 bool compressed = false;
175 Init();
176 serialized_.reset(new char[100]);
177 bool success = SerializeEvents(metadata_,
178 frame_event_list_,
179 packet_event_list_,
180 compressed,
181 100,
182 serialized_.get(),
183 &output_bytes_);
184 EXPECT_FALSE(success);
185 EXPECT_EQ(0, output_bytes_);
188 TEST_F(SerializeDeserializeTest, Compressed) {
189 bool compressed = true;
190 Init();
191 bool success = SerializeEvents(metadata_,
192 frame_event_list_,
193 packet_event_list_,
194 compressed,
195 kMaxSerializedBytes,
196 serialized_.get(),
197 &output_bytes_);
198 ASSERT_TRUE(success);
199 ASSERT_GT(output_bytes_, 0);
201 DeserializedLog audio_log;
202 DeserializedLog video_log;
203 success = DeserializeEvents(
204 serialized_.get(), output_bytes_, compressed, &audio_log, &video_log);
205 ASSERT_TRUE(success);
206 Verify(video_log);
209 TEST_F(SerializeDeserializeTest, CompressedInsufficientSpace) {
210 bool compressed = true;
211 Init();
212 serialized_.reset(new char[100]);
213 bool success = SerializeEvents(metadata_,
214 frame_event_list_,
215 packet_event_list_,
216 compressed,
217 100,
218 serialized_.get(),
219 &output_bytes_);
220 EXPECT_FALSE(success);
221 EXPECT_EQ(0, output_bytes_);
224 } // namespace cast
225 } // namespace media