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