xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/tools/audio_checksum.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_
12 #define MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_
13 
14 #include <memory>
15 #include <string>
16 
17 #include "modules/audio_coding/neteq/tools/audio_sink.h"
18 #include "rtc_base/buffer.h"
19 #include "rtc_base/message_digest.h"
20 #include "rtc_base/string_encode.h"
21 #include "rtc_base/system/arch.h"
22 
23 namespace webrtc {
24 namespace test {
25 
26 class AudioChecksum : public AudioSink {
27  public:
AudioChecksum()28   AudioChecksum()
29       : checksum_(rtc::MessageDigestFactory::Create(rtc::DIGEST_MD5)),
30         checksum_result_(checksum_->Size()),
31         finished_(false) {}
32 
33   AudioChecksum(const AudioChecksum&) = delete;
34   AudioChecksum& operator=(const AudioChecksum&) = delete;
35 
WriteArray(const int16_t * audio,size_t num_samples)36   bool WriteArray(const int16_t* audio, size_t num_samples) override {
37     if (finished_)
38       return false;
39 
40 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN
41 #error "Big-endian gives a different checksum"
42 #endif
43     checksum_->Update(audio, num_samples * sizeof(*audio));
44     return true;
45   }
46 
47   // Finalizes the computations, and returns the checksum.
Finish()48   std::string Finish() {
49     if (!finished_) {
50       finished_ = true;
51       checksum_->Finish(checksum_result_.data(), checksum_result_.size());
52     }
53     return rtc::hex_encode(checksum_result_);
54   }
55 
56  private:
57   std::unique_ptr<rtc::MessageDigest> checksum_;
58   rtc::Buffer checksum_result_;
59   bool finished_;
60 };
61 
62 }  // namespace test
63 }  // namespace webrtc
64 #endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_
65