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
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
;
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 kEncodedFrameSize
[] = {512, 425, 399, 400, 237};
32 const int kDelayMillis
[] = {15, 4, 8, 42, 23, 16};
34 const int kMaxSerializedBytes
= 10000;
41 class SerializeDeserializeTest
: public ::testing::Test
{
43 SerializeDeserializeTest()
44 : serialized_(new char[kMaxSerializedBytes
]), output_bytes_(0) {}
46 virtual ~SerializeDeserializeTest() {}
49 metadata_
.set_first_rtp_timestamp(12345678 * 90);
50 metadata_
.set_is_audio(false);
51 metadata_
.set_num_frame_events(10);
52 metadata_
.set_num_packet_events(10);
54 int64 event_time_ms
= 0;
55 // Insert frame and packet events with RTP timestamps 0, 90, 180, ...
56 for (int i
= 0; i
< metadata_
.num_frame_events(); i
++) {
57 linked_ptr
<AggregatedFrameEvent
> frame_event(new AggregatedFrameEvent
);
58 frame_event
->set_relative_rtp_timestamp(i
* 90);
59 for (uint32 event_index
= 0; event_index
< arraysize(kVideoFrameEvents
);
61 frame_event
->add_event_type(
62 ToProtoEventType(kVideoFrameEvents
[event_index
]));
63 frame_event
->add_event_timestamp_ms(event_time_ms
);
64 event_time_ms
+= 1024;
66 frame_event
->set_encoded_frame_size(
67 kEncodedFrameSize
[i
% arraysize(kEncodedFrameSize
)]);
68 frame_event
->set_delay_millis(kDelayMillis
[i
% arraysize(kDelayMillis
)]);
70 frame_event_list_
.push_back(frame_event
);
75 for (int i
= 0; i
< metadata_
.num_packet_events(); i
++) {
76 linked_ptr
<AggregatedPacketEvent
> packet_event(new AggregatedPacketEvent
);
77 packet_event
->set_relative_rtp_timestamp(i
* 90);
78 for (int j
= 0; j
< 10; j
++) {
79 BasePacketEvent
* base_event
= packet_event
->add_base_packet_event();
80 base_event
->set_packet_id(packet_id
);
82 for (uint32 event_index
= 0;
83 event_index
< arraysize(kVideoPacketEvents
);
85 base_event
->add_event_type(
86 ToProtoEventType(kVideoPacketEvents
[event_index
]));
87 base_event
->add_event_timestamp_ms(event_time_ms
);
91 packet_event_list_
.push_back(packet_event
);
95 void Verify(const DeserializedLog
& video_log
) {
96 const LogMetadata
& returned_metadata
= video_log
.metadata
;
97 const FrameEventMap
& returned_frame_events
= video_log
.frame_events
;
98 const PacketEventMap
& returned_packet_events
= video_log
.packet_events
;
100 EXPECT_EQ(metadata_
.SerializeAsString(),
101 returned_metadata
.SerializeAsString());
103 // Check that the returned map is equal to the original map.
104 EXPECT_EQ(frame_event_list_
.size(), returned_frame_events
.size());
105 for (FrameEventMap::const_iterator frame_it
= returned_frame_events
.begin();
106 frame_it
!= returned_frame_events
.end();
108 FrameEventList::iterator original_it
= frame_event_list_
.begin();
109 ASSERT_NE(frame_event_list_
.end(), original_it
);
110 // Compare protos by serializing and checking the bytes.
111 EXPECT_EQ((*original_it
)->SerializeAsString(),
112 frame_it
->second
->SerializeAsString());
113 frame_event_list_
.erase(frame_event_list_
.begin());
115 EXPECT_TRUE(frame_event_list_
.empty());
117 EXPECT_EQ(packet_event_list_
.size(), returned_packet_events
.size());
118 for (PacketEventMap::const_iterator packet_it
=
119 returned_packet_events
.begin();
120 packet_it
!= returned_packet_events
.end();
122 PacketEventList::iterator original_it
= packet_event_list_
.begin();
123 ASSERT_NE(packet_event_list_
.end(), original_it
);
124 // Compare protos by serializing and checking the bytes.
125 EXPECT_EQ((*original_it
)->SerializeAsString(),
126 packet_it
->second
->SerializeAsString());
127 packet_event_list_
.erase(packet_event_list_
.begin());
129 EXPECT_TRUE(packet_event_list_
.empty());
132 LogMetadata metadata_
;
133 FrameEventList frame_event_list_
;
134 PacketEventList packet_event_list_
;
135 scoped_ptr
<char[]> serialized_
;
139 TEST_F(SerializeDeserializeTest
, Uncompressed
) {
140 bool compressed
= false;
143 bool success
= SerializeEvents(metadata_
,
150 ASSERT_TRUE(success
);
151 ASSERT_GT(output_bytes_
, 0);
153 DeserializedLog audio_log
;
154 DeserializedLog video_log
;
155 success
= DeserializeEvents(
156 serialized_
.get(), output_bytes_
, compressed
, &audio_log
, &video_log
);
157 ASSERT_TRUE(success
);
162 TEST_F(SerializeDeserializeTest
, UncompressedInsufficientSpace
) {
163 bool compressed
= false;
165 serialized_
.reset(new char[100]);
166 bool success
= SerializeEvents(metadata_
,
173 EXPECT_FALSE(success
);
174 EXPECT_EQ(0, output_bytes_
);
177 TEST_F(SerializeDeserializeTest
, Compressed
) {
178 bool compressed
= true;
180 bool success
= SerializeEvents(metadata_
,
187 ASSERT_TRUE(success
);
188 ASSERT_GT(output_bytes_
, 0);
190 DeserializedLog audio_log
;
191 DeserializedLog video_log
;
192 success
= DeserializeEvents(
193 serialized_
.get(), output_bytes_
, compressed
, &audio_log
, &video_log
);
194 ASSERT_TRUE(success
);
198 TEST_F(SerializeDeserializeTest
, CompressedInsufficientSpace
) {
199 bool compressed
= true;
201 serialized_
.reset(new char[100]);
202 bool success
= SerializeEvents(metadata_
,
209 EXPECT_FALSE(success
);
210 EXPECT_EQ(0, output_bytes_
);