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" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/message_digest.h" |
| 20 | #include "rtc_base/string_encode.h" |
Niels Möller | a12c42a | 2018-07-25 16:05:48 +0200 | [diff] [blame] | 21 | #include "rtc_base/system/arch.h" |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace test { |
| 25 | |
| 26 | class AudioChecksum : public AudioSink { |
| 27 | public: |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 28 | AudioChecksum() |
| 29 | : checksum_(rtc::MessageDigestFactory::Create(rtc::DIGEST_MD5)), |
| 30 | checksum_result_(checksum_->Size()), |
| 31 | finished_(false) {} |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 32 | |
Byoungchan Lee | 604fd2f | 2022-01-21 09:49:39 +0900 | [diff] [blame] | 33 | AudioChecksum(const AudioChecksum&) = delete; |
| 34 | AudioChecksum& operator=(const AudioChecksum&) = delete; |
| 35 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 36 | 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] | 37 | if (finished_) |
| 38 | return false; |
| 39 | |
| 40 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 41 | #error "Big-endian gives a different checksum" |
| 42 | #endif |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 43 | checksum_->Update(audio, num_samples * sizeof(*audio)); |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 44 | return true; |
| 45 | } |
| 46 | |
| 47 | // Finalizes the computations, and returns the checksum. |
| 48 | std::string Finish() { |
| 49 | if (!finished_) { |
| 50 | finished_ = true; |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 51 | checksum_->Finish(checksum_result_.data(), checksum_result_.size()); |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 52 | } |
Ali Tofigh | fd6a4d6 | 2022-03-31 10:36:48 +0200 | [diff] [blame] | 53 | return rtc::hex_encode(checksum_result_); |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private: |
Joachim Bauch | 4e90919 | 2017-12-19 22:27:51 +0100 | [diff] [blame] | 57 | std::unique_ptr<rtc::MessageDigest> checksum_; |
| 58 | rtc::Buffer checksum_result_; |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 59 | bool finished_; |
henrik.lundin@webrtc.org | 496a984 | 2014-06-19 10:02:11 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // namespace test |
| 63 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 64 | #endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_ |