Yves Gerey | 3a65f39 | 2019-11-11 18:05:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "modules/audio_coding/neteq/test/result_sink.h" |
| 12 | |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "rtc_base/ignore_wundef.h" |
| 16 | #include "rtc_base/message_digest.h" |
| 17 | #include "rtc_base/string_encode.h" |
| 18 | #include "test/gtest.h" |
| 19 | |
| 20 | #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 21 | RTC_PUSH_IGNORING_WUNDEF() |
| 22 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
| 23 | #include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h" |
| 24 | #else |
| 25 | #include "modules/audio_coding/neteq/neteq_unittest.pb.h" |
| 26 | #endif |
| 27 | RTC_POP_IGNORING_WUNDEF() |
| 28 | #endif |
| 29 | |
| 30 | namespace webrtc { |
| 31 | |
| 32 | #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 33 | void Convert(const webrtc::NetEqNetworkStatistics& stats_raw, |
| 34 | webrtc::neteq_unittest::NetEqNetworkStatistics* stats) { |
| 35 | stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms); |
| 36 | stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms); |
| 37 | stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found); |
| 38 | stats->set_packet_loss_rate(stats_raw.packet_loss_rate); |
| 39 | stats->set_expand_rate(stats_raw.expand_rate); |
| 40 | stats->set_speech_expand_rate(stats_raw.speech_expand_rate); |
| 41 | stats->set_preemptive_rate(stats_raw.preemptive_rate); |
| 42 | stats->set_accelerate_rate(stats_raw.accelerate_rate); |
| 43 | stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate); |
| 44 | stats->set_secondary_discarded_rate(stats_raw.secondary_discarded_rate); |
| 45 | stats->set_added_zero_samples(stats_raw.added_zero_samples); |
| 46 | stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms); |
| 47 | stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms); |
| 48 | stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms); |
| 49 | stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms); |
| 50 | } |
| 51 | |
| 52 | void Convert(const webrtc::RtcpStatistics& stats_raw, |
| 53 | webrtc::neteq_unittest::RtcpStatistics* stats) { |
| 54 | stats->set_fraction_lost(stats_raw.fraction_lost); |
| 55 | stats->set_cumulative_lost(stats_raw.packets_lost); |
| 56 | stats->set_extended_max_sequence_number( |
| 57 | stats_raw.extended_highest_sequence_number); |
| 58 | stats->set_jitter(stats_raw.jitter); |
| 59 | } |
| 60 | |
| 61 | void AddMessage(FILE* file, |
| 62 | rtc::MessageDigest* digest, |
| 63 | const std::string& message) { |
| 64 | int32_t size = message.length(); |
| 65 | if (file) |
| 66 | ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file)); |
| 67 | digest->Update(&size, sizeof(size)); |
| 68 | |
| 69 | if (file) |
| 70 | ASSERT_EQ(static_cast<size_t>(size), |
| 71 | fwrite(message.data(), sizeof(char), size, file)); |
| 72 | digest->Update(message.data(), sizeof(char) * size); |
| 73 | } |
| 74 | |
| 75 | #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 76 | |
| 77 | ResultSink::ResultSink(const std::string& output_file) |
| 78 | : output_fp_(nullptr), |
| 79 | digest_(rtc::MessageDigestFactory::Create(rtc::DIGEST_SHA_1)) { |
| 80 | if (!output_file.empty()) { |
| 81 | output_fp_ = fopen(output_file.c_str(), "wb"); |
| 82 | EXPECT_TRUE(output_fp_ != NULL); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | ResultSink::~ResultSink() { |
| 87 | if (output_fp_) |
| 88 | fclose(output_fp_); |
| 89 | } |
| 90 | |
| 91 | void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) { |
| 92 | #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 93 | neteq_unittest::NetEqNetworkStatistics stats; |
| 94 | Convert(stats_raw, &stats); |
| 95 | |
| 96 | std::string stats_string; |
| 97 | ASSERT_TRUE(stats.SerializeToString(&stats_string)); |
| 98 | AddMessage(output_fp_, digest_.get(), stats_string); |
| 99 | #else |
| 100 | FAIL() << "Writing to reference file requires Proto Buffer."; |
| 101 | #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 102 | } |
| 103 | |
| 104 | void ResultSink::AddResult(const RtcpStatistics& stats_raw) { |
| 105 | #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 106 | neteq_unittest::RtcpStatistics stats; |
| 107 | Convert(stats_raw, &stats); |
| 108 | |
| 109 | std::string stats_string; |
| 110 | ASSERT_TRUE(stats.SerializeToString(&stats_string)); |
| 111 | AddMessage(output_fp_, digest_.get(), stats_string); |
| 112 | #else |
| 113 | FAIL() << "Writing to reference file requires Proto Buffer."; |
| 114 | #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 115 | } |
| 116 | |
| 117 | void ResultSink::VerifyChecksum(const std::string& checksum) { |
| 118 | std::vector<char> buffer; |
| 119 | buffer.resize(digest_->Size()); |
| 120 | digest_->Finish(&buffer[0], buffer.size()); |
| 121 | const std::string result = rtc::hex_encode(&buffer[0], digest_->Size()); |
| 122 | if (checksum.size() == result.size()) { |
| 123 | EXPECT_EQ(checksum, result); |
| 124 | } else { |
| 125 | // Check result is one the '|'-separated checksums. |
| 126 | EXPECT_NE(checksum.find(result), std::string::npos) |
| 127 | << result << " should be one of these:\n" |
| 128 | << checksum; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } // namespace webrtc |