blob: ee8c58238064fa3de02455ccc9adb9a40984144b [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/constructormagic.h"
Joachim Bauch4e909192017-12-19 22:27:51 +010020#include "rtc_base/messagedigest.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/stringencode.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020022#include "rtc_base/system/arch.h"
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000023
24namespace webrtc {
25namespace test {
26
27class AudioChecksum : public AudioSink {
28 public:
Joachim Bauch4e909192017-12-19 22:27:51 +010029 AudioChecksum()
30 : checksum_(rtc::MessageDigestFactory::Create(rtc::DIGEST_MD5)),
31 checksum_result_(checksum_->Size()),
32 finished_(false) {}
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000033
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000034 bool WriteArray(const int16_t* audio, size_t num_samples) override {
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000035 if (finished_)
36 return false;
37
38#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
39#error "Big-endian gives a different checksum"
40#endif
Joachim Bauch4e909192017-12-19 22:27:51 +010041 checksum_->Update(audio, num_samples * sizeof(*audio));
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000042 return true;
43 }
44
45 // Finalizes the computations, and returns the checksum.
46 std::string Finish() {
47 if (!finished_) {
48 finished_ = true;
Joachim Bauch4e909192017-12-19 22:27:51 +010049 checksum_->Finish(checksum_result_.data(), checksum_result_.size());
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000050 }
Joachim Bauch4e909192017-12-19 22:27:51 +010051 return rtc::hex_encode(checksum_result_.data<char>(),
52 checksum_result_.size());
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000053 }
54
55 private:
Joachim Bauch4e909192017-12-19 22:27:51 +010056 std::unique_ptr<rtc::MessageDigest> checksum_;
57 rtc::Buffer checksum_result_;
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000058 bool finished_;
59
henrikg3c089d72015-09-16 05:37:44 -070060 RTC_DISALLOW_COPY_AND_ASSIGN(AudioChecksum);
henrik.lundin@webrtc.org496a9842014-06-19 10:02:11 +000061};
62
63} // namespace test
64} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_