blob: b70016180e235bfd82e5e1d22f5770d0185a424f [file] [log] [blame]
Yves Gerey3a65f392019-11-11 18:05:42 +01001/*
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
21RTC_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
27RTC_POP_IGNORING_WUNDEF()
28#endif
29
30namespace webrtc {
31
32#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
33void 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);
Yves Gerey3a65f392019-11-11 18:05:42 +010038 stats->set_expand_rate(stats_raw.expand_rate);
39 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
40 stats->set_preemptive_rate(stats_raw.preemptive_rate);
41 stats->set_accelerate_rate(stats_raw.accelerate_rate);
42 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
43 stats->set_secondary_discarded_rate(stats_raw.secondary_discarded_rate);
Yves Gerey3a65f392019-11-11 18:05:42 +010044 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
45 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
46 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
47 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
48}
49
Yves Gerey3a65f392019-11-11 18:05:42 +010050void AddMessage(FILE* file,
51 rtc::MessageDigest* digest,
52 const std::string& message) {
53 int32_t size = message.length();
54 if (file)
55 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
56 digest->Update(&size, sizeof(size));
57
58 if (file)
59 ASSERT_EQ(static_cast<size_t>(size),
60 fwrite(message.data(), sizeof(char), size, file));
61 digest->Update(message.data(), sizeof(char) * size);
62}
63
64#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
65
66ResultSink::ResultSink(const std::string& output_file)
67 : output_fp_(nullptr),
68 digest_(rtc::MessageDigestFactory::Create(rtc::DIGEST_SHA_1)) {
69 if (!output_file.empty()) {
70 output_fp_ = fopen(output_file.c_str(), "wb");
71 EXPECT_TRUE(output_fp_ != NULL);
72 }
73}
74
75ResultSink::~ResultSink() {
76 if (output_fp_)
77 fclose(output_fp_);
78}
79
80void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
81#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
82 neteq_unittest::NetEqNetworkStatistics stats;
83 Convert(stats_raw, &stats);
84
85 std::string stats_string;
86 ASSERT_TRUE(stats.SerializeToString(&stats_string));
87 AddMessage(output_fp_, digest_.get(), stats_string);
88#else
89 FAIL() << "Writing to reference file requires Proto Buffer.";
90#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
91}
92
Yves Gerey3a65f392019-11-11 18:05:42 +010093void ResultSink::VerifyChecksum(const std::string& checksum) {
94 std::vector<char> buffer;
95 buffer.resize(digest_->Size());
96 digest_->Finish(&buffer[0], buffer.size());
97 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
98 if (checksum.size() == result.size()) {
99 EXPECT_EQ(checksum, result);
100 } else {
101 // Check result is one the '|'-separated checksums.
102 EXPECT_NE(checksum.find(result), std::string::npos)
103 << result << " should be one of these:\n"
104 << checksum;
105 }
106}
107
108} // namespace webrtc