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