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 | 7 |
8 #include "remoting/codec/audio_encoder_opus.h" | 8 #include "remoting/codec/audio_encoder_opus.h" |
9 | 9 |
10 #include <math.h> | 10 #include <math.h> |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 double frequency_hz, | 60 double frequency_hz, |
61 double pos, | 61 double pos, |
62 int channel) { | 62 int channel) { |
63 double angle = pos * 2 * M_PI * frequency_hz / rate + | 63 double angle = pos * 2 * M_PI * frequency_hz / rate + |
64 kChannelPhaseShift * channel; | 64 kChannelPhaseShift * channel; |
65 return static_cast<int>(sin(angle) * kMaxSampleValue + 0.5); | 65 return static_cast<int>(sin(angle) * kMaxSampleValue + 0.5); |
66 } | 66 } |
67 | 67 |
68 // Creates audio packet filled with a test signal with the specified | 68 // Creates audio packet filled with a test signal with the specified |
69 // |frequency_hz|. | 69 // |frequency_hz|. |
70 scoped_ptr<AudioPacket> CreatePacket( | 70 std::unique_ptr<AudioPacket> CreatePacket(int samples, |
71 int samples, | 71 AudioPacket::SamplingRate rate, |
72 AudioPacket::SamplingRate rate, | 72 double frequency_hz, |
73 double frequency_hz, | 73 int pos) { |
74 int pos) { | |
75 std::vector<int16_t> data(samples * kChannels); | 74 std::vector<int16_t> data(samples * kChannels); |
76 for (int i = 0; i < samples; ++i) { | 75 for (int i = 0; i < samples; ++i) { |
77 data[i * kChannels] = GetSampleValue(rate, frequency_hz, i + pos, 0); | 76 data[i * kChannels] = GetSampleValue(rate, frequency_hz, i + pos, 0); |
78 data[i * kChannels + 1] = GetSampleValue(rate, frequency_hz, i + pos, 1); | 77 data[i * kChannels + 1] = GetSampleValue(rate, frequency_hz, i + pos, 1); |
79 } | 78 } |
80 | 79 |
81 scoped_ptr<AudioPacket> packet(new AudioPacket()); | 80 std::unique_ptr<AudioPacket> packet(new AudioPacket()); |
82 packet->add_data(reinterpret_cast<char*>(&(data[0])), | 81 packet->add_data(reinterpret_cast<char*>(&(data[0])), |
83 samples * kChannels * sizeof(int16_t)); | 82 samples * kChannels * sizeof(int16_t)); |
84 packet->set_encoding(AudioPacket::ENCODING_RAW); | 83 packet->set_encoding(AudioPacket::ENCODING_RAW); |
85 packet->set_sampling_rate(rate); | 84 packet->set_sampling_rate(rate); |
86 packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); | 85 packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2); |
87 packet->set_channels(AudioPacket::CHANNELS_STEREO); | 86 packet->set_channels(AudioPacket::CHANNELS_STEREO); |
88 return packet; | 87 return packet; |
89 } | 88 } |
90 | 89 |
91 // Decoded data is normally shifted in phase relative to the original signal. | 90 // Decoded data is normally shifted in phase relative to the original signal. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 double frequency_hz, | 131 double frequency_hz, |
133 AudioPacket::SamplingRate rate) { | 132 AudioPacket::SamplingRate rate) { |
134 const int kTotalTestSamples = 24000; | 133 const int kTotalTestSamples = 24000; |
135 | 134 |
136 encoder_.reset(new AudioEncoderOpus()); | 135 encoder_.reset(new AudioEncoderOpus()); |
137 decoder_.reset(new AudioDecoderOpus()); | 136 decoder_.reset(new AudioDecoderOpus()); |
138 | 137 |
139 std::vector<int16_t> received_data; | 138 std::vector<int16_t> received_data; |
140 int pos = 0; | 139 int pos = 0; |
141 for (; pos < kTotalTestSamples; pos += packet_size) { | 140 for (; pos < kTotalTestSamples; pos += packet_size) { |
142 scoped_ptr<AudioPacket> source_packet = | 141 std::unique_ptr<AudioPacket> source_packet = |
143 CreatePacket(packet_size, rate, frequency_hz, pos); | 142 CreatePacket(packet_size, rate, frequency_hz, pos); |
144 scoped_ptr<AudioPacket> encoded = | 143 std::unique_ptr<AudioPacket> encoded = |
145 encoder_->Encode(std::move(source_packet)); | 144 encoder_->Encode(std::move(source_packet)); |
146 if (encoded.get()) { | 145 if (encoded.get()) { |
147 scoped_ptr<AudioPacket> decoded = | 146 std::unique_ptr<AudioPacket> decoded = |
148 decoder_->Decode(std::move(encoded)); | 147 decoder_->Decode(std::move(encoded)); |
149 EXPECT_EQ(kDefaultSamplingRate, decoded->sampling_rate()); | 148 EXPECT_EQ(kDefaultSamplingRate, decoded->sampling_rate()); |
150 for (int i = 0; i < decoded->data_size(); ++i) { | 149 for (int i = 0; i < decoded->data_size(); ++i) { |
151 const int16_t* data = | 150 const int16_t* data = |
152 reinterpret_cast<const int16_t*>(decoded->data(i).data()); | 151 reinterpret_cast<const int16_t*>(decoded->data(i).data()); |
153 received_data.insert( | 152 received_data.insert( |
154 received_data.end(), data, | 153 received_data.end(), data, |
155 data + decoded->data(i).size() / sizeof(int16_t)); | 154 data + decoded->data(i).size() / sizeof(int16_t)); |
156 } | 155 } |
157 } | 156 } |
158 } | 157 } |
159 | 158 |
160 // Verify that at most kMaxLatencyMs worth of samples is buffered inside | 159 // Verify that at most kMaxLatencyMs worth of samples is buffered inside |
161 // |encoder_| and |decoder_|. | 160 // |encoder_| and |decoder_|. |
162 EXPECT_GE(static_cast<int>(received_data.size()) / kChannels, | 161 EXPECT_GE(static_cast<int>(received_data.size()) / kChannels, |
163 pos - rate * kMaxLatencyMs / 1000); | 162 pos - rate * kMaxLatencyMs / 1000); |
164 | 163 |
165 ValidateReceivedData(packet_size, kDefaultSamplingRate, | 164 ValidateReceivedData(packet_size, kDefaultSamplingRate, |
166 frequency_hz, received_data); | 165 frequency_hz, received_data); |
167 } | 166 } |
168 | 167 |
169 protected: | 168 protected: |
170 scoped_ptr<AudioEncoderOpus> encoder_; | 169 std::unique_ptr<AudioEncoderOpus> encoder_; |
171 scoped_ptr<AudioDecoderOpus> decoder_; | 170 std::unique_ptr<AudioDecoderOpus> decoder_; |
172 }; | 171 }; |
173 | 172 |
174 TEST_F(OpusAudioEncoderTest, CreateAndDestroy) { | 173 TEST_F(OpusAudioEncoderTest, CreateAndDestroy) { |
175 } | 174 } |
176 | 175 |
177 TEST_F(OpusAudioEncoderTest, NoResampling) { | 176 TEST_F(OpusAudioEncoderTest, NoResampling) { |
178 TestEncodeDecode(2000, 50, AudioPacket::SAMPLING_RATE_48000); | 177 TestEncodeDecode(2000, 50, AudioPacket::SAMPLING_RATE_48000); |
179 TestEncodeDecode(2000, 3000, AudioPacket::SAMPLING_RATE_48000); | 178 TestEncodeDecode(2000, 3000, AudioPacket::SAMPLING_RATE_48000); |
180 TestEncodeDecode(2000, 10000, AudioPacket::SAMPLING_RATE_48000); | 179 TestEncodeDecode(2000, 10000, AudioPacket::SAMPLING_RATE_48000); |
181 } | 180 } |
182 | 181 |
183 TEST_F(OpusAudioEncoderTest, Resampling) { | 182 TEST_F(OpusAudioEncoderTest, Resampling) { |
184 TestEncodeDecode(2000, 50, AudioPacket::SAMPLING_RATE_44100); | 183 TestEncodeDecode(2000, 50, AudioPacket::SAMPLING_RATE_44100); |
185 TestEncodeDecode(2000, 3000, AudioPacket::SAMPLING_RATE_44100); | 184 TestEncodeDecode(2000, 3000, AudioPacket::SAMPLING_RATE_44100); |
186 TestEncodeDecode(2000, 10000, AudioPacket::SAMPLING_RATE_44100); | 185 TestEncodeDecode(2000, 10000, AudioPacket::SAMPLING_RATE_44100); |
187 } | 186 } |
188 | 187 |
189 TEST_F(OpusAudioEncoderTest, BufferSizeAndResampling) { | 188 TEST_F(OpusAudioEncoderTest, BufferSizeAndResampling) { |
190 TestEncodeDecode(500, 3000, AudioPacket::SAMPLING_RATE_44100); | 189 TestEncodeDecode(500, 3000, AudioPacket::SAMPLING_RATE_44100); |
191 TestEncodeDecode(1000, 3000, AudioPacket::SAMPLING_RATE_44100); | 190 TestEncodeDecode(1000, 3000, AudioPacket::SAMPLING_RATE_44100); |
192 TestEncodeDecode(5000, 3000, AudioPacket::SAMPLING_RATE_44100); | 191 TestEncodeDecode(5000, 3000, AudioPacket::SAMPLING_RATE_44100); |
193 } | 192 } |
194 | 193 |
195 } // namespace remoting | 194 } // namespace remoting |
OLD | NEW |