1496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org/*
2496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org *
4496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
5496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
6496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
7496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
8496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org */
10496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
11496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_
12496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_
13496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
14496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#include <string>
15496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
16496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#include "webrtc/base/constructormagic.h"
17496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#include "webrtc/base/md5digest.h"
18496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#include "webrtc/base/stringencode.h"
19496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/tools/audio_sink.h"
20496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#include "webrtc/typedefs.h"
21496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
22496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.orgnamespace webrtc {
23496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.orgnamespace test {
24496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
25496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.orgclass AudioChecksum : public AudioSink {
26496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org public:
27496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org  AudioChecksum() : finished_(false) {}
28496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
2914665ff7d4024d07e58622f498b23fd980001871kjellander@webrtc.org  bool WriteArray(const int16_t* audio, size_t num_samples) override {
30496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org    if (finished_)
31496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org      return false;
32496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
33496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
34496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#error "Big-endian gives a different checksum"
35496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#endif
36496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org    checksum_.Update(audio, num_samples * sizeof(*audio));
37496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org    return true;
38496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org  }
39496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
40496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org  // Finalizes the computations, and returns the checksum.
41496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org  std::string Finish() {
42496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org    if (!finished_) {
43496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org      finished_ = true;
44496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org      checksum_.Finish(checksum_result_, rtc::Md5Digest::kSize);
45496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org    }
46496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org    return rtc::hex_encode(checksum_result_, rtc::Md5Digest::kSize);
47496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org  }
48496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
49496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org private:
50496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org  rtc::Md5Digest checksum_;
51496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org  char checksum_result_[rtc::Md5Digest::kSize];
52496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org  bool finished_;
53496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
543c089d751ede283e21e186885eaf705c3257ccd2henrikg  RTC_DISALLOW_COPY_AND_ASSIGN(AudioChecksum);
55496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org};
56496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org
57496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org}  // namespace test
58496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org}  // namespace webrtc
59496a98463b1b10c024c0b773a37d25c902a43890henrik.lundin@webrtc.org#endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_TOOLS_AUDIO_CHECKSUM_H_
60