Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / neteq4 / tools / neteq_quality_test.cc
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 #include <stdio.h>
12 #include "webrtc/modules/audio_coding/neteq4/tools/neteq_quality_test.h"
13
14 namespace webrtc {
15 namespace test {
16
17 const uint8_t kPayloadType = 95;
18 const int kOutputSizeMs = 10;
19
20 NetEqQualityTest::NetEqQualityTest(int block_duration_ms,
21                                    int in_sampling_khz,
22                                    int out_sampling_khz,
23                                    enum NetEqDecoder decoder_type,
24                                    int channels,
25                                    double drift_factor,
26                                    std::string in_filename,
27                                    std::string out_filename)
28     : decoded_time_ms_(0),
29       decodable_time_ms_(0),
30       drift_factor_(drift_factor),
31       block_duration_ms_(block_duration_ms),
32       in_sampling_khz_(in_sampling_khz),
33       out_sampling_khz_(out_sampling_khz),
34       decoder_type_(decoder_type),
35       channels_(channels),
36       in_filename_(in_filename),
37       out_filename_(out_filename),
38       in_size_samples_(in_sampling_khz_ * block_duration_ms_),
39       out_size_samples_(out_sampling_khz_ * kOutputSizeMs),
40       payload_size_bytes_(0),
41       max_payload_bytes_(0),
42       in_file_(new InputAudioFile(in_filename_)),
43       out_file_(NULL),
44       rtp_generator_(new RtpGenerator(in_sampling_khz_, 0, 0,
45                                       decodable_time_ms_)),
46       neteq_(NetEq::Create(out_sampling_khz_ * 1000)) {
47   max_payload_bytes_ = in_size_samples_ * channels_ * sizeof(int16_t);
48   in_data_.reset(new int16_t[in_size_samples_ * channels_]);
49   payload_.reset(new uint8_t[max_payload_bytes_]);
50   out_data_.reset(new int16_t[out_size_samples_ * channels_]);
51 }
52
53 void NetEqQualityTest::SetUp() {
54   out_file_ = fopen(out_filename_.c_str(), "wb");
55   ASSERT_TRUE(out_file_ != NULL);
56   ASSERT_EQ(0, neteq_->RegisterPayloadType(decoder_type_, kPayloadType));
57   rtp_generator_->set_drift_factor(drift_factor_);
58 }
59
60 void NetEqQualityTest::TearDown() {
61   fclose(out_file_);
62 }
63
64 int NetEqQualityTest::Transmit() {
65   int packet_input_time_ms =
66       rtp_generator_->GetRtpHeader(kPayloadType, in_size_samples_,
67                                    &rtp_header_);
68   if (!PacketLost(packet_input_time_ms) && payload_size_bytes_ > 0) {
69     int ret = neteq_->InsertPacket(rtp_header_, &payload_[0],
70                                    payload_size_bytes_,
71                                    packet_input_time_ms * in_sampling_khz_);
72     if (ret != NetEq::kOK)
73       return -1;
74   }
75   return packet_input_time_ms;
76 }
77
78 int NetEqQualityTest::DecodeBlock() {
79   int channels;
80   int samples;
81   int ret = neteq_->GetAudio(out_size_samples_ * channels_, &out_data_[0],
82                              &samples, &channels, NULL);
83
84   if (ret != NetEq::kOK) {
85     return -1;
86   } else {
87     assert(channels == channels_);
88     assert(samples == kOutputSizeMs * out_sampling_khz_);
89     fwrite(&out_data_[0], sizeof(int16_t), samples * channels, out_file_);
90     return samples;
91   }
92 }
93
94 void NetEqQualityTest::Simulate(int end_time_ms) {
95   int audio_size_samples;
96
97   while (decoded_time_ms_ < end_time_ms) {
98     while (decodable_time_ms_ - kOutputSizeMs < decoded_time_ms_) {
99       ASSERT_TRUE(in_file_->Read(in_size_samples_ * channels_, &in_data_[0]));
100       payload_size_bytes_ = EncodeBlock(&in_data_[0],
101                                         in_size_samples_, &payload_[0],
102                                         max_payload_bytes_);
103       decodable_time_ms_ = Transmit() + block_duration_ms_;
104     }
105     audio_size_samples = DecodeBlock();
106     if (audio_size_samples > 0) {
107       decoded_time_ms_ += audio_size_samples / out_sampling_khz_;
108     }
109   }
110 }
111
112 }  // namespace test
113 }  // namespace webrtc