blob: 42e3a3a3a0b3811bccdf219e8507f0d7a5883f30 [file] [log] [blame]
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_
12#define MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000013
Joachim Bauch4e909192017-12-19 22:27:51 +010014#include <memory>
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000015#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/neteq/tools/audio_sink.h"
Joachim Bauch4e909192017-12-19 22:27:51 +010018#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/message_digest.h"
20#include "rtc_base/string_encode.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020021#include "rtc_base/system/arch.h"
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000022
23namespace webrtc {
24namespace test {
25
26class AudioChecksum : public AudioSink {
27 public:
Joachim Bauch4e909192017-12-19 22:27:51 +010028 AudioChecksum()
29 : checksum_(rtc::MessageDigestFactory::Create(rtc::DIGEST_MD5)),
30 checksum_result_(checksum_->Size()),
31 finished_(false) {}
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000032
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090033 AudioChecksum(const AudioChecksum&) = delete;
34 AudioChecksum& operator=(const AudioChecksum&) = delete;
35
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000036 bool WriteArray(const int16_t* audio, size_t num_samples) override {
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000037 if (finished_)
38 return false;
39
40#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
41#error "Big-endian gives a different checksum"
42#endif
Joachim Bauch4e909192017-12-19 22:27:51 +010043 checksum_->Update(audio, num_samples * sizeof(*audio));
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000044 return true;
45 }
46
47 // Finalizes the computations, and returns the checksum.
48 std::string Finish() {
49 if (!finished_) {
50 finished_ = true;
Joachim Bauch4e909192017-12-19 22:27:51 +010051 checksum_->Finish(checksum_result_.data(), checksum_result_.size());
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000052 }
Ali Tofighfd6a4d62022-03-31 10:36:48 +020053 return rtc::hex_encode(checksum_result_);
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000054 }
55
56 private:
Joachim Bauch4e909192017-12-19 22:27:51 +010057 std::unique_ptr<rtc::MessageDigest> checksum_;
58 rtc::Buffer checksum_result_;
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000059 bool finished_;
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000060};
61
62} // namespace test
63} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020064#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_