OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // MSVC++ requires this to get M_PI. | 5 // MSVC++ requires this to get M_PI. |
6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
| 7 |
| 8 #include "remoting/codec/audio_encoder_opus.h" |
| 9 |
7 #include <math.h> | 10 #include <math.h> |
8 #include <stddef.h> | 11 #include <stddef.h> |
9 #include <stdint.h> | 12 #include <stdint.h> |
10 | 13 |
11 #include "remoting/codec/audio_encoder_opus.h" | 14 #include <utility> |
12 | 15 |
13 #include "base/logging.h" | 16 #include "base/logging.h" |
14 #include "remoting/codec/audio_decoder_opus.h" | 17 #include "remoting/codec/audio_decoder_opus.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
16 | 19 |
17 namespace remoting { | 20 namespace remoting { |
18 | 21 |
19 namespace { | 22 namespace { |
20 | 23 |
21 // Maximum value that can be encoded in a 16-bit signed sample. | 24 // Maximum value that can be encoded in a 16-bit signed sample. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 data[i * kChannels + 1] = GetSampleValue(rate, frequency_hz, i + pos, 1); | 78 data[i * kChannels + 1] = GetSampleValue(rate, frequency_hz, i + pos, 1); |
76 } | 79 } |
77 | 80 |
78 scoped_ptr<AudioPacket> packet(new AudioPacket()); | 81 scoped_ptr<AudioPacket> packet(new AudioPacket()); |
79 packet->add_data(reinterpret_cast<char*>(&(data[0])), | 82 packet->add_data(reinterpret_cast<char*>(&(data[0])), |
80 samples * kChannels * sizeof(int16_t)); | 83 samples * kChannels * sizeof(int16_t)); |
81 packet->set_encoding(AudioPacket::ENCODING_RAW); | 84 packet->set_encoding(AudioPacket::ENCODING_RAW); |
82 packet->set_sampling_rate(rate); | 85 packet->set_sampling_rate(rate); |
83 packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); | 86 packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); |
84 packet->set_channels(AudioPacket::CHANNELS_STEREO); | 87 packet->set_channels(AudioPacket::CHANNELS_STEREO); |
85 return packet.Pass(); | 88 return packet; |
86 } | 89 } |
87 | 90 |
88 // Decoded data is normally shifted in phase relative to the original signal. | 91 // Decoded data is normally shifted in phase relative to the original signal. |
89 // This function returns the approximate shift in samples by finding the first | 92 // This function returns the approximate shift in samples by finding the first |
90 // point when signal goes from negative to positive. | 93 // point when signal goes from negative to positive. |
91 double EstimateSignalShift(const std::vector<int16_t>& received_data) { | 94 double EstimateSignalShift(const std::vector<int16_t>& received_data) { |
92 for (size_t i = kSkippedFirstSamples; | 95 for (size_t i = kSkippedFirstSamples; |
93 i < received_data.size() / kChannels - 1; i++) { | 96 i < received_data.size() / kChannels - 1; i++) { |
94 int16_t this_sample = received_data[i * kChannels]; | 97 int16_t this_sample = received_data[i * kChannels]; |
95 int16_t next_sample = received_data[(i + 1) * kChannels]; | 98 int16_t next_sample = received_data[(i + 1) * kChannels]; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 135 |
133 encoder_.reset(new AudioEncoderOpus()); | 136 encoder_.reset(new AudioEncoderOpus()); |
134 decoder_.reset(new AudioDecoderOpus()); | 137 decoder_.reset(new AudioDecoderOpus()); |
135 | 138 |
136 std::vector<int16_t> received_data; | 139 std::vector<int16_t> received_data; |
137 int pos = 0; | 140 int pos = 0; |
138 for (; pos < kTotalTestSamples; pos += packet_size) { | 141 for (; pos < kTotalTestSamples; pos += packet_size) { |
139 scoped_ptr<AudioPacket> source_packet = | 142 scoped_ptr<AudioPacket> source_packet = |
140 CreatePacket(packet_size, rate, frequency_hz, pos); | 143 CreatePacket(packet_size, rate, frequency_hz, pos); |
141 scoped_ptr<AudioPacket> encoded = | 144 scoped_ptr<AudioPacket> encoded = |
142 encoder_->Encode(source_packet.Pass()); | 145 encoder_->Encode(std::move(source_packet)); |
143 if (encoded.get()) { | 146 if (encoded.get()) { |
144 scoped_ptr<AudioPacket> decoded = decoder_->Decode(encoded.Pass()); | 147 scoped_ptr<AudioPacket> decoded = |
| 148 decoder_->Decode(std::move(encoded)); |
145 EXPECT_EQ(kDefaultSamplingRate, decoded->sampling_rate()); | 149 EXPECT_EQ(kDefaultSamplingRate, decoded->sampling_rate()); |
146 for (int i = 0; i < decoded->data_size(); ++i) { | 150 for (int i = 0; i < decoded->data_size(); ++i) { |
147 const int16_t* data = | 151 const int16_t* data = |
148 reinterpret_cast<const int16_t*>(decoded->data(i).data()); | 152 reinterpret_cast<const int16_t*>(decoded->data(i).data()); |
149 received_data.insert( | 153 received_data.insert( |
150 received_data.end(), data, | 154 received_data.end(), data, |
151 data + decoded->data(i).size() / sizeof(int16_t)); | 155 data + decoded->data(i).size() / sizeof(int16_t)); |
152 } | 156 } |
153 } | 157 } |
154 } | 158 } |
(...skipping 27 matching lines...) Expand all Loading... |
182 TestEncodeDecode(2000, 10000, AudioPacket::SAMPLING_RATE_44100); | 186 TestEncodeDecode(2000, 10000, AudioPacket::SAMPLING_RATE_44100); |
183 } | 187 } |
184 | 188 |
185 TEST_F(OpusAudioEncoderTest, BufferSizeAndResampling) { | 189 TEST_F(OpusAudioEncoderTest, BufferSizeAndResampling) { |
186 TestEncodeDecode(500, 3000, AudioPacket::SAMPLING_RATE_44100); | 190 TestEncodeDecode(500, 3000, AudioPacket::SAMPLING_RATE_44100); |
187 TestEncodeDecode(1000, 3000, AudioPacket::SAMPLING_RATE_44100); | 191 TestEncodeDecode(1000, 3000, AudioPacket::SAMPLING_RATE_44100); |
188 TestEncodeDecode(5000, 3000, AudioPacket::SAMPLING_RATE_44100); | 192 TestEncodeDecode(5000, 3000, AudioPacket::SAMPLING_RATE_44100); |
189 } | 193 } |
190 | 194 |
191 } // namespace remoting | 195 } // namespace remoting |
OLD | NEW |