Chromium Code Reviews| Index: webrtc/video/rtc_event_log_unittest.cc |
| diff --git a/webrtc/video/rtc_event_log_unittest.cc b/webrtc/video/rtc_event_log_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f0ccb70496cb9165b46339b4a3b39c5dfaa9a07a |
| --- /dev/null |
| +++ b/webrtc/video/rtc_event_log_unittest.cc |
| @@ -0,0 +1,425 @@ |
| +/* |
| + * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#ifdef ENABLE_RTC_EVENT_LOG |
| + |
| +#include <stdio.h> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webrtc/base/scoped_ptr.h" |
| +#include "webrtc/video/rtc_event_log.h" |
|
ivoc
2015/07/17 12:14:29
Could you sort these alphabetically?
terelius
2015/07/17 15:17:40
Done.
|
| +#include "webrtc/system_wrappers/interface/clock.h" |
| +#include "webrtc/test/test_suite.h" |
| +#include "webrtc/test/testsupport/fileutils.h" |
| +#include "webrtc/test/testsupport/gtest_disable.h" |
| + |
| +// Files generated at build-time by the protobuf compiler. |
| +#ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
| +#include "external/webrtc/webrtc/video/rtc_event_log.pb.h" |
| +#else |
| +#include "webrtc/video/rtc_event_log.pb.h" |
| +#endif |
| + |
| +namespace webrtc { |
| + |
| +// Checks that the event has a timestamp, a type and exactly the data field |
| +// corresponding to the type. |
| +::testing::AssertionResult IsValidBasicEvent(const RelEvent& event) { |
| + if (!event.has_timestamp_us()) |
| + return ::testing::AssertionFailure() << "Event has no timestamp"; |
| + if (!event.has_type()) |
| + return ::testing::AssertionFailure() << "Event has no event type"; |
| + RelEvent_EventType type = event.type(); |
| + if ((type == RelEvent::RTP_EVENT) != event.has_rtp_packet()) |
| + return ::testing::AssertionFailure() << "Event of type" << type |
| + << "has" << (event.has_rtp_packet()?"":"no") << "RTP packet"; |
|
ivoc
2015/07/17 12:14:29
I wonder how this error message would look. It see
terelius
2015/07/17 15:17:40
You're right about the spaces. I'll fix that.
We
|
| + if ((type == RelEvent::RTCP_EVENT) != event.has_rtcp_packet()) |
| + return ::testing::AssertionFailure() << "Event of type" << type |
| + << "has" << (event.has_rtcp_packet()?"":"no") << "RTCP packet"; |
| + if ((type == RelEvent::DEBUG_EVENT) != event.has_debug_event()) |
| + return ::testing::AssertionFailure() << "Event of type" << type |
| + << "has" << (event.has_debug_event()?"":"no") << "debug event"; |
| + if ((type == RelEvent::RECEIVER_CONFIG_EVENT) != event.has_receiver_config()) |
| + return ::testing::AssertionFailure() << "Event of type" << type |
| + << "has" << (event.has_receiver_config()?"":"no") << "receiver config"; |
| + if ((type == RelEvent::SENDER_CONFIG_EVENT) != event.has_sender_config()) |
| + return ::testing::AssertionFailure() << "Event of type" << type |
| + << "has" << (event.has_sender_config()?"":"no") << "sender config"; |
| + if ((type == RelEvent::AUDIO_RECEIVER_CONFIG_EVENT) |
| + != event.has_audio_receiver_config()) { |
| + return ::testing::AssertionFailure() << "Event of type" << type |
| + << "has" << (event.has_audio_receiver_config()?"":"no") |
| + << "audio receiver config"; |
| + } |
| + if ((type == RelEvent::AUDIO_SENDER_CONFIG_EVENT) |
| + != event.has_audio_sender_config()) { |
| + return ::testing::AssertionFailure() << "Event of type" << type |
| + << "has" << (event.has_audio_sender_config()?"":"no") |
| + << "audio sender config"; |
| + } |
| + return ::testing::AssertionSuccess(); |
| +} |
| + |
| +void VerifyReceiveStreamConfig(const RelEvent& event, |
| + const VideoReceiveStream::Config& config) { |
| + ASSERT_TRUE(IsValidBasicEvent(event)); |
| + ASSERT_EQ(RelEvent::RECEIVER_CONFIG_EVENT, event.type()); |
| + const RelVideoReceiveConfig& receiver_config = event.receiver_config(); |
| + // Check SSRCs. |
| + ASSERT_TRUE(receiver_config.has_remote_ssrc()); |
| + EXPECT_EQ(config.rtp.remote_ssrc, receiver_config.remote_ssrc()); |
| + ASSERT_TRUE(receiver_config.has_local_ssrc()); |
| + EXPECT_EQ(config.rtp.local_ssrc, receiver_config.local_ssrc()); |
| + // Check RTCP settings. |
| + ASSERT_TRUE(receiver_config.has_rtcp_mode()); |
| + if (config.rtp.rtcp_mode == newapi::kRtcpCompound) |
| + EXPECT_EQ(RelVideoReceiveConfig::RTCP_COMPOUND, |
| + receiver_config.rtcp_mode()); |
| + else |
| + EXPECT_EQ(RelVideoReceiveConfig::RTCP_REDUCEDSIZE, |
| + receiver_config.rtcp_mode()); |
| + ASSERT_TRUE(receiver_config.has_receiver_reference_time_report()); |
| + EXPECT_EQ(config.rtp.rtcp_xr.receiver_reference_time_report, |
| + receiver_config.receiver_reference_time_report()); |
| + ASSERT_TRUE(receiver_config.has_remb()); |
| + EXPECT_EQ(config.rtp.remb, receiver_config.remb()); |
| + // Check RTX map. |
| + ASSERT_EQ(static_cast<int>(config.rtp.rtx.size()), |
| + receiver_config.rtx_map_size()); |
| + for (int i = 0; i < receiver_config.rtx_map_size(); i++) { |
| + const RtxMap& mapping = receiver_config.rtx_map(i); |
| + ASSERT_TRUE(mapping.has_payload_type()); |
| + ASSERT_TRUE(mapping.has_config()); |
| + EXPECT_EQ(1, |
| + static_cast<int>(config.rtp.rtx.count(mapping.payload_type()))); |
| + const RtxConfig& rtx_config = mapping.config(); |
| + const VideoReceiveStream::Config::Rtp::Rtx& rtx = |
| + config.rtp.rtx.at(mapping.payload_type()); |
| + ASSERT_TRUE(rtx_config.has_rtx_ssrc()); |
| + ASSERT_TRUE(rtx_config.has_rtx_payload_type()); |
| + EXPECT_EQ(rtx.ssrc, rtx_config.rtx_ssrc()); |
| + EXPECT_EQ(rtx.payload_type, rtx_config.rtx_payload_type()); |
| + } |
| + // Check header extensions. |
| + ASSERT_EQ(static_cast<int>(config.rtp.extensions.size()), |
| + receiver_config.header_extensions_size()); |
| + for (int i = 0; i < receiver_config.header_extensions_size(); i++) { |
| + ASSERT_TRUE(receiver_config.header_extensions(i).has_name()); |
| + ASSERT_TRUE(receiver_config.header_extensions(i).has_id()); |
| + const std::string& name = receiver_config.header_extensions(i).name(); |
| + int id = receiver_config.header_extensions(i).id(); |
| + EXPECT_EQ(config.rtp.extensions[i].id, id); |
| + EXPECT_EQ(config.rtp.extensions[i].name, name); |
| + } |
| + // Check decoders. |
| + ASSERT_EQ(static_cast<int>(config.decoders.size()), |
| + receiver_config.decoders_size()); |
| + for (int i = 0; i < receiver_config.decoders_size(); i++) { |
| + ASSERT_TRUE(receiver_config.decoders(i).has_name()); |
| + ASSERT_TRUE(receiver_config.decoders(i).has_payload_type()); |
| + const std::string& decoder_name = receiver_config.decoders(i).name(); |
| + int decoder_type = receiver_config.decoders(i).payload_type(); |
| + EXPECT_EQ(config.decoders[i].payload_name, decoder_name); |
| + EXPECT_EQ(config.decoders[i].payload_type, decoder_type); |
| + } |
| +} |
| + |
| +void VerifySendStreamConfig(const RelEvent& event, |
| + const VideoSendStream::Config& config) { |
| + ASSERT_TRUE(IsValidBasicEvent(event)); |
| + ASSERT_EQ(RelEvent::SENDER_CONFIG_EVENT, event.type()); |
| + const RelVideoSendConfig& sender_config = event.sender_config(); |
| + // Check SSRCs. |
| + ASSERT_EQ(static_cast<int>(config.rtp.ssrcs.size()), |
| + sender_config.ssrcs_size()); |
| + for (int i = 0; i < sender_config.ssrcs_size(); i++) { |
| + EXPECT_EQ(config.rtp.ssrcs[i], sender_config.ssrcs(i)); |
| + } |
| + // Check header extensions. |
| + ASSERT_EQ(static_cast<int>(config.rtp.extensions.size()), |
| + sender_config.header_extensions_size()); |
| + for (int i = 0; i < sender_config.header_extensions_size(); i++) { |
| + ASSERT_TRUE(sender_config.header_extensions(i).has_name()); |
| + ASSERT_TRUE(sender_config.header_extensions(i).has_id()); |
| + const std::string& name = sender_config.header_extensions(i).name(); |
| + int id = sender_config.header_extensions(i).id(); |
| + EXPECT_EQ(config.rtp.extensions[i].id, id); |
| + EXPECT_EQ(config.rtp.extensions[i].name, name); |
| + } |
| + // Check RTX settings. |
| + ASSERT_EQ(static_cast<int>(config.rtp.rtx.ssrcs.size()), |
| + sender_config.rtx_ssrcs_size()); |
| + for (int i = 0; i < sender_config.rtx_ssrcs_size(); i++) { |
| + EXPECT_EQ(config.rtp.rtx.ssrcs[i], sender_config.rtx_ssrcs(i)); |
| + } |
| + if (sender_config.rtx_ssrcs_size() > 0) { |
| + ASSERT_TRUE(sender_config.has_rtx_payload_type()); |
| + EXPECT_EQ(config.rtp.rtx.payload_type, sender_config.rtx_payload_type()); |
| + } |
| + // Check CNAME. |
| + ASSERT_TRUE(sender_config.has_c_name()); |
| + EXPECT_EQ(config.rtp.c_name, sender_config.c_name()); |
| + // Check encoder. |
| + ASSERT_TRUE(sender_config.has_encoder()); |
| + ASSERT_TRUE(sender_config.encoder().has_name()); |
| + ASSERT_TRUE(sender_config.encoder().has_payload_type()); |
| + EXPECT_EQ(config.encoder_settings.payload_name, |
| + sender_config.encoder().name()); |
| + EXPECT_EQ(config.encoder_settings.payload_type, |
| + sender_config.encoder().payload_type()); |
| +} |
| + |
| +void VerifyRtpEvent(const RelEvent& event, |
| + bool incoming, |
| + MediaType media_type, |
| + uint8_t* header, |
| + size_t header_size, |
| + size_t total_size) { |
| + ASSERT_TRUE(IsValidBasicEvent(event)); |
| + ASSERT_EQ(RelEvent::RTP_EVENT, event.type()); |
| + const RelRtpPacket& rtp_packet = event.rtp_packet(); |
| + ASSERT_TRUE(rtp_packet.has_direction()); |
| + EXPECT_EQ(incoming ? RelRtpPacket::INCOMING : RelRtpPacket::OUTGOING, |
| + rtp_packet.direction()); |
| + ASSERT_TRUE(rtp_packet.has_type()); |
| + if (media_type == MediaType::VIDEO) |
| + EXPECT_EQ(RelRtpPacket::VIDEO, rtp_packet.type()); |
| + else if (media_type == MediaType::AUDIO) |
| + EXPECT_EQ(RelRtpPacket::AUDIO, rtp_packet.type()); |
| + else |
| + EXPECT_EQ(RelRtpPacket::UNKNOWN_TYPE, rtp_packet.type()); |
| + ASSERT_TRUE(rtp_packet.has_packet_length()); |
| + EXPECT_EQ(total_size, rtp_packet.packet_length()); |
| + ASSERT_TRUE(rtp_packet.has_header()); |
| + ASSERT_EQ(header_size, rtp_packet.header().size()); |
| + for (size_t i = 0; i < header_size; i++) { |
| + EXPECT_EQ(header[i], static_cast<uint8_t>(rtp_packet.header()[i])); |
| + } |
| +} |
| + |
| +void VerifyRtcpEvent(const RelEvent& event, |
| + bool incoming, |
| + MediaType media_type, |
| + uint8_t* packet, |
| + size_t total_size) { |
| + ASSERT_TRUE(IsValidBasicEvent(event)); |
| + ASSERT_EQ(RelEvent::RTCP_EVENT, event.type()); |
| + const RelRtcpPacket& rtcp_packet = event.rtcp_packet(); |
| + ASSERT_TRUE(rtcp_packet.has_direction()); |
| + EXPECT_EQ(incoming ? RelRtcpPacket::INCOMING : RelRtcpPacket::OUTGOING, |
| + rtcp_packet.direction()); |
| + ASSERT_TRUE(rtcp_packet.has_type()); |
| + if (media_type == MediaType::VIDEO) |
| + EXPECT_EQ(RelRtcpPacket::VIDEO, rtcp_packet.type()); |
| + else if (media_type == MediaType::AUDIO) |
| + EXPECT_EQ(RelRtcpPacket::AUDIO, rtcp_packet.type()); |
| + else |
| + EXPECT_EQ(RelRtcpPacket::UNKNOWN_TYPE, rtcp_packet.type()); |
| + ASSERT_TRUE(rtcp_packet.has_data()); |
| + ASSERT_EQ(total_size, rtcp_packet.data().size()); |
| + for (size_t i = 0; i < total_size; i++) { |
| + EXPECT_EQ(packet[i], static_cast<uint8_t>(rtcp_packet.data()[i])); |
| + } |
| +} |
| + |
| +void VerifyLogStartEvent(const RelEvent& event) { |
| + ASSERT_TRUE(IsValidBasicEvent(event)); |
| + ASSERT_EQ(RelEvent::DEBUG_EVENT, event.type()); |
| + const RelDebugEvent& debug_event = event.debug_event(); |
| + ASSERT_TRUE(debug_event.has_type()); |
| + EXPECT_EQ(RelDebugEvent::LOG_START, debug_event.type()); |
| + // TODO(terelius): Deliberately not verifying that there is a message field |
| + // since our protobuf file says that the message is optional. Make a decision. |
| +} |
| + |
| +void GenerateVideoReceiveConfig(webrtc::VideoReceiveStream::Config* config) { |
| + // Create a map from a payload type to an encoder name. |
| + VideoReceiveStream::Decoder decoder; |
| + decoder.payload_type = rand(); |
| + decoder.payload_name = (rand() % 2 ? "VP8" : "H264"); |
| + config->decoders.push_back(decoder); |
| + // Add SSRCs for the stream. |
| + config->rtp.remote_ssrc = rand(); |
| + config->rtp.local_ssrc = rand(); |
| + // Add extensions and settings for RTCP. |
| + config->rtp.rtcp_mode = rand() % 2 ? newapi::kRtcpCompound |
| + : newapi::kRtcpReducedSize; |
| + config->rtp.rtcp_xr.receiver_reference_time_report = |
| + static_cast<bool>(rand() % 2); |
| + config->rtp.remb = static_cast<bool>(rand() % 2); |
| + // Add a map from a payload type to a new ssrc and a new payload type for RTX. |
| + webrtc::VideoReceiveStream::Config::Rtp::Rtx rtx_pair; |
| + rtx_pair.ssrc = rand(); |
| + rtx_pair.payload_type = rand(); |
| + config->rtp.rtx.insert(std::make_pair(rand(), rtx_pair)); |
| + // Add two random header extensions. |
| + const char* extension_name = rand() % 2 ? RtpExtension::kTOffset |
| + : RtpExtension::kVideoRotation; |
| + config->rtp.extensions.push_back(RtpExtension(extension_name, rand())); |
| + extension_name = rand() % 2 ? RtpExtension::kAudioLevel |
| + : RtpExtension::kAbsSendTime; |
| + config->rtp.extensions.push_back(RtpExtension(extension_name, rand())); |
| +} |
| + |
| +void GenerateVideoSendConfig(webrtc::VideoSendStream::Config* config) { |
| + // Create a map from a payload type to an encoder name. |
| + config->encoder_settings.payload_type = rand(); |
| + config->encoder_settings.payload_name = (rand() % 2 ? "VP8" : "H264"); |
| + // Add SSRCs for the stream. |
| + config->rtp.ssrcs.push_back(rand()); |
| + // Add a map from a payload type to new ssrcs and a new payload type for RTX. |
| + config->rtp.rtx.ssrcs.push_back(rand()); |
| + config->rtp.rtx.payload_type = rand(); |
| + // Add a CNAME. |
| + config->rtp.c_name = "some.user@some.host"; |
| + // Add two random header extensions. |
| + const char* extension_name = rand() % 2 ? RtpExtension::kTOffset |
| + : RtpExtension::kVideoRotation; |
| + config->rtp.extensions.push_back(RtpExtension(extension_name, rand())); |
| + extension_name = rand() % 2 ? RtpExtension::kAudioLevel |
| + : RtpExtension::kAbsSendTime; |
| + config->rtp.extensions.push_back(RtpExtension(extension_name, rand())); |
| +} |
| + |
| +// Test for the RtcEventLog class. Dumps some RTP packets to disk, then reads |
| +// them back to see if they match. |
| +void LogSessionAndReadBack(size_t rtp_count, unsigned random_seed) { |
| + std::vector<std::vector<uint8_t>> rtp_packets; |
| + std::vector<uint8_t> incoming_rtcp_packet; |
| + std::vector<uint8_t> outgoing_rtcp_packet; |
| + |
| + webrtc::VideoReceiveStream::Config receiver_config; |
| + webrtc::VideoSendStream::Config sender_config; |
| + |
| + srand(random_seed); |
| + |
| + // Create rtp_count RTP packets containing random data. |
| + const size_t rtp_header_size = 20; |
| + for (size_t i = 0; i < rtp_count; i++) { |
| + size_t packet_size = 1000 + rand() % 30; |
| + rtp_packets.push_back(std::vector<uint8_t>()); |
|
ivoc
2015/07/17 12:14:29
It would be good to call reserve here as well.
terelius
2015/07/17 15:17:40
Done. Well spotted!
I initially used the construc
|
| + for (size_t j = 0; j < packet_size; j++) { |
| + rtp_packets[i].push_back(rand()); |
| + } |
| + } |
| + // Create two RTCP packets containing random data. |
| + size_t packet_size = 1000 + rand() % 30; |
| + outgoing_rtcp_packet.reserve(packet_size); |
| + for (size_t j = 0; j < packet_size; j++) { |
| + outgoing_rtcp_packet.push_back(rand()); |
| + } |
| + packet_size = 1000 + rand() % 30; |
| + incoming_rtcp_packet.reserve(packet_size); |
| + for (size_t j = 0; j < packet_size; j++) { |
| + incoming_rtcp_packet.push_back(rand()); |
| + } |
| + // Create configurations for the video streams. |
| + GenerateVideoReceiveConfig(&receiver_config); |
| + GenerateVideoSendConfig(&sender_config); |
| + |
| + // Find the name of the current test, in order to use it as a temporary |
| + // filename. |
| + auto test_info = ::testing::UnitTest::GetInstance()->current_test_info(); |
| + const std::string temp_filename = |
| + test::OutputPath() + test_info->test_case_name() + test_info->name(); |
| + |
| + // When log_dumper goes out of scope, it causes the log file to be flushed |
| + // to disk. |
| + { |
| + rtc::scoped_ptr<RtcEventLog> log_dumper(RtcEventLog::Create()); |
| + log_dumper->LogVideoReceiveStreamConfig(receiver_config); |
| + log_dumper->LogVideoSendStreamConfig(sender_config); |
| + size_t i = 0; |
| + for (; i < rtp_count / 2; i++) { |
| + log_dumper->LogRtpHeader( |
| + (i % 2 == 0), // Every second packet is incoming. |
| + (i % 3 == 0) ? MediaType::AUDIO : MediaType::VIDEO, |
| + rtp_packets[i].data(), |
| + rtp_header_size, |
| + rtp_packets[i].size()); |
| + } |
| + log_dumper->LogRtcpPacket(false, |
| + MediaType::AUDIO, |
| + outgoing_rtcp_packet.data(), |
| + outgoing_rtcp_packet.size()); |
| + log_dumper->StartLogging(temp_filename, 10000000); |
| + for (; i < rtp_count; i++) { |
| + log_dumper->LogRtpHeader( |
| + (i % 2 == 0), // Every second packet is incoming, |
| + (i % 3 == 0) ? MediaType::AUDIO : MediaType::VIDEO, |
| + rtp_packets[i].data(), |
| + rtp_header_size, |
| + rtp_packets[i].size()); |
| + } |
| + log_dumper->LogRtcpPacket(true, |
| + MediaType::VIDEO, |
| + incoming_rtcp_packet.data(), |
| + incoming_rtcp_packet.size()); |
| + } |
| + |
| + const int config_count = 2; |
| + const int rtcp_count = 2; |
| + const int debug_count = 1; // Only LogStart event, |
| + const int event_count = config_count + debug_count + rtcp_count + rtp_count; |
| + |
| + // Read the generated file from disk. |
| + RelEventStream parsed_stream; |
| + |
| + ASSERT_TRUE(RtcEventLog::ParseRtcEventLog(temp_filename, &parsed_stream)); |
| + |
| + // Verify the result. |
| + EXPECT_EQ(event_count, parsed_stream.stream_size()); |
| + VerifyReceiveStreamConfig(parsed_stream.stream(0), receiver_config); |
| + VerifySendStreamConfig(parsed_stream.stream(1), sender_config); |
| + size_t i = 0; |
| + for (; i < rtp_count / 2; i++) { |
| + VerifyRtpEvent(parsed_stream.stream(config_count + i), |
| + (i % 2 == 0), // Every second packet is incoming. |
| + (i % 3 == 0) ? MediaType::AUDIO : MediaType::VIDEO, |
| + rtp_packets[i].data(), |
| + rtp_header_size, |
| + rtp_packets[i].size()); |
| + } |
| + VerifyRtcpEvent(parsed_stream.stream(config_count + rtp_count / 2), |
| + false, |
| + MediaType::AUDIO, |
| + outgoing_rtcp_packet.data(), |
| + outgoing_rtcp_packet.size()); |
| + |
| + VerifyLogStartEvent(parsed_stream.stream(1 + config_count + rtp_count / 2)); |
| + for (; i < rtp_count; i++) { |
| + VerifyRtpEvent(parsed_stream.stream(2 + config_count + i), |
| + (i % 2 == 0), // Every second packet is incoming. |
| + (i % 3 == 0) ? MediaType::AUDIO : MediaType::VIDEO, |
| + rtp_packets[i].data(), |
| + rtp_header_size, |
| + rtp_packets[i].size()); |
| + } |
| + VerifyRtcpEvent(parsed_stream.stream(2 + config_count + rtp_count), |
| + true, |
| + MediaType::VIDEO, |
| + incoming_rtcp_packet.data(), |
| + incoming_rtcp_packet.size()); |
| + |
| + // Clean up temporary file - can be pretty slow. |
| + remove(temp_filename.c_str()); |
| +} |
| + |
| +TEST(RtcEventLogTest, LogSessionAndReadBack) { |
| + LogSessionAndReadBack(5, 321); |
| + LogSessionAndReadBack(8, 3141592653U); |
| + LogSessionAndReadBack(9, 2718281828U); |
| +} |
| + |
| +} // namespace webrtc |
| + |
| +#endif // ENABLE_RTC_EVENT_LOG |