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. 4 5 // Joint LogSerializer and LogDeserializer testing to make sure they stay in 6 // sync. 7 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" 14 15 using media::cast::proto::AggregatedFrameEvent; 16 using media::cast::proto::AggregatedPacketEvent; 17 using media::cast::proto::BasePacketEvent; 18 using media::cast::proto::LogMetadata; 19 20 namespace { 21 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 }; 26 27 const media::cast::CastLoggingEvent kVideoPacketEvents[] = { 28 media::cast::PACKET_SENT_TO_NETWORK, media::cast::PACKET_RECEIVED}; 29 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}; 33 34 const int kMaxSerializedBytes = 10000; 35 36 } 37 38 namespace media { 39 namespace cast { 40 41 class SerializeDeserializeTest : public ::testing::Test { 42 protected: 43 SerializeDeserializeTest() 44 : serialized_(new char[kMaxSerializedBytes]), output_bytes_(0) {} 45 46 virtual ~SerializeDeserializeTest() {} 47 48 void Init() { 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); 53 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); 60 ++event_index) { 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; 65 } 66 frame_event->set_encoded_frame_size( 67 kEncodedFrameSize[i % arraysize(kEncodedFrameSize)]); 68 frame_event->set_delay_millis(kDelayMillis[i % arraysize(kDelayMillis)]); 69 70 frame_event_list_.push_back(frame_event); 71 } 72 73 event_time_ms = 0; 74 int packet_id = 0; 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); 81 packet_id++; 82 for (uint32 event_index = 0; 83 event_index < arraysize(kVideoPacketEvents); 84 ++event_index) { 85 base_event->add_event_type( 86 ToProtoEventType(kVideoPacketEvents[event_index])); 87 base_event->add_event_timestamp_ms(event_time_ms); 88 event_time_ms += 256; 89 } 90 } 91 packet_event_list_.push_back(packet_event); 92 } 93 } 94 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; 99 100 EXPECT_EQ(metadata_.SerializeAsString(), 101 returned_metadata.SerializeAsString()); 102 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(); 107 ++frame_it) { 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()); 114 } 115 EXPECT_TRUE(frame_event_list_.empty()); 116 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(); 121 ++packet_it) { 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()); 128 } 129 EXPECT_TRUE(packet_event_list_.empty()); 130 } 131 132 LogMetadata metadata_; 133 FrameEventList frame_event_list_; 134 PacketEventList packet_event_list_; 135 scoped_ptr<char[]> serialized_; 136 int output_bytes_; 137 }; 138 139 TEST_F(SerializeDeserializeTest, Uncompressed) { 140 bool compressed = false; 141 Init(); 142 143 bool success = SerializeEvents(metadata_, 144 frame_event_list_, 145 packet_event_list_, 146 compressed, 147 kMaxSerializedBytes, 148 serialized_.get(), 149 &output_bytes_); 150 ASSERT_TRUE(success); 151 ASSERT_GT(output_bytes_, 0); 152 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); 158 159 Verify(video_log); 160 } 161 162 TEST_F(SerializeDeserializeTest, UncompressedInsufficientSpace) { 163 bool compressed = false; 164 Init(); 165 serialized_.reset(new char[100]); 166 bool success = SerializeEvents(metadata_, 167 frame_event_list_, 168 packet_event_list_, 169 compressed, 170 100, 171 serialized_.get(), 172 &output_bytes_); 173 EXPECT_FALSE(success); 174 EXPECT_EQ(0, output_bytes_); 175 } 176 177 TEST_F(SerializeDeserializeTest, Compressed) { 178 bool compressed = true; 179 Init(); 180 bool success = SerializeEvents(metadata_, 181 frame_event_list_, 182 packet_event_list_, 183 compressed, 184 kMaxSerializedBytes, 185 serialized_.get(), 186 &output_bytes_); 187 ASSERT_TRUE(success); 188 ASSERT_GT(output_bytes_, 0); 189 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); 195 Verify(video_log); 196 } 197 198 TEST_F(SerializeDeserializeTest, CompressedInsufficientSpace) { 199 bool compressed = true; 200 Init(); 201 serialized_.reset(new char[100]); 202 bool success = SerializeEvents(metadata_, 203 frame_event_list_, 204 packet_event_list_, 205 compressed, 206 100, 207 serialized_.get(), 208 &output_bytes_); 209 EXPECT_FALSE(success); 210 EXPECT_EQ(0, output_bytes_); 211 } 212 213 } // namespace cast 214 } // namespace media 215