henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_ |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 13 | |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 14 | #include <memory> |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_coding/neteq/tools/audio_sink.h" |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 18 | #include "rtc_base/buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/constructormagic.h" |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 20 | #include "rtc_base/messagedigest.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/stringencode.h" |
Niels Möller | a12c42a | 2018-07-25 16:05:48 +0200 | [diff] [blame^] | 22 | #include "rtc_base/system/arch.h" |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace test { |
| 26 | |
| 27 | class AudioChecksum : public AudioSink { |
| 28 | public: |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 29 | AudioChecksum() |
| 30 | : checksum_(rtc::MessageDigestFactory::Create(rtc::DIGEST_MD5)), |
| 31 | checksum_result_(checksum_->Size()), |
| 32 | finished_(false) {} |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 33 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 34 | bool WriteArray(const int16_t* audio, size_t num_samples) override { |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 35 | if (finished_) |
| 36 | return false; |
| 37 | |
| 38 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 39 | #error "Big-endian gives a different checksum" |
| 40 | #endif |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 41 | checksum_->Update(audio, num_samples * sizeof(*audio)); |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
| 45 | // Finalizes the computations, and returns the checksum. |
| 46 | std::string Finish() { |
| 47 | if (!finished_) { |
| 48 | finished_ = true; |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 49 | checksum_->Finish(checksum_result_.data(), checksum_result_.size()); |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 50 | } |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 51 | return rtc::hex_encode(checksum_result_.data<char>(), |
| 52 | checksum_result_.size()); |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | private: |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 56 | std::unique_ptr<rtc::MessageDigest> checksum_; |
| 57 | rtc::Buffer checksum_result_; |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 58 | bool finished_; |
| 59 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 60 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioChecksum); |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | } // namespace test |
| 64 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 65 | #endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_ |