| 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 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/environment.h" | 6 #include "base/environment.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 // Reported render/output delay. Typical value is ~40 [ms]. | 86 // Reported render/output delay. Typical value is ~40 [ms]. |
| 87 int output_delay_ms; | 87 int output_delay_ms; |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 // This class mocks the platform specific audio manager and overrides | 90 // This class mocks the platform specific audio manager and overrides |
| 91 // the GetMessageLoop() method to ensure that we can run our tests on | 91 // the GetMessageLoop() method to ensure that we can run our tests on |
| 92 // the main thread instead of the audio thread. | 92 // the main thread instead of the audio thread. |
| 93 class MockAudioManager : public AudioManagerAnyPlatform { | 93 class MockAudioManager : public AudioManagerAnyPlatform { |
| 94 public: | 94 public: |
| 95 MockAudioManager() : AudioManagerAnyPlatform(&fake_audio_log_factory_) {} | 95 MockAudioManager() : AudioManagerAnyPlatform(&fake_audio_log_factory_) {} |
| 96 virtual ~MockAudioManager() {} | 96 ~MockAudioManager() override {} |
| 97 | 97 |
| 98 virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() override { | 98 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() override { |
| 99 return base::MessageLoop::current()->message_loop_proxy(); | 99 return base::MessageLoop::current()->message_loop_proxy(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 private: | 102 private: |
| 103 FakeAudioLogFactory fake_audio_log_factory_; | 103 FakeAudioLogFactory fake_audio_log_factory_; |
| 104 DISALLOW_COPY_AND_ASSIGN(MockAudioManager); | 104 DISALLOW_COPY_AND_ASSIGN(MockAudioManager); |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 // Test fixture class. | 107 // Test fixture class. |
| 108 class AudioLowLatencyInputOutputTest : public testing::Test { | 108 class AudioLowLatencyInputOutputTest : public testing::Test { |
| 109 protected: | 109 protected: |
| 110 AudioLowLatencyInputOutputTest() {} | 110 AudioLowLatencyInputOutputTest() {} |
| 111 | 111 |
| 112 virtual ~AudioLowLatencyInputOutputTest() {} | 112 ~AudioLowLatencyInputOutputTest() override {} |
| 113 | 113 |
| 114 AudioManager* audio_manager() { return &mock_audio_manager_; } | 114 AudioManager* audio_manager() { return &mock_audio_manager_; } |
| 115 base::MessageLoopForUI* message_loop() { return &message_loop_; } | 115 base::MessageLoopForUI* message_loop() { return &message_loop_; } |
| 116 | 116 |
| 117 // Convenience method which ensures that we are not running on the build | 117 // Convenience method which ensures that we are not running on the build |
| 118 // bots and that at least one valid input and output device can be found. | 118 // bots and that at least one valid input and output device can be found. |
| 119 bool CanRunAudioTests() { | 119 bool CanRunAudioTests() { |
| 120 bool input = audio_manager()->HasAudioInputDevices(); | 120 bool input = audio_manager()->HasAudioInputDevices(); |
| 121 bool output = audio_manager()->HasAudioOutputDevices(); | 121 bool output = audio_manager()->HasAudioOutputDevices(); |
| 122 LOG_IF(WARNING, !input) << "No input device detected."; | 122 LOG_IF(WARNING, !input) << "No input device detected."; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 155 |
| 156 // Start with the smallest possible buffer size. It will be increased | 156 // Start with the smallest possible buffer size. It will be increased |
| 157 // dynamically during the test if required. | 157 // dynamically during the test if required. |
| 158 buffer_.reset( | 158 buffer_.reset( |
| 159 new media::SeekableBuffer(0, samples_per_packet_ * frame_size_)); | 159 new media::SeekableBuffer(0, samples_per_packet_ * frame_size_)); |
| 160 | 160 |
| 161 frames_to_ms_ = static_cast<double>(1000.0 / sample_rate_); | 161 frames_to_ms_ = static_cast<double>(1000.0 / sample_rate_); |
| 162 delay_states_.reset(new AudioDelayState[kMaxDelayMeasurements]); | 162 delay_states_.reset(new AudioDelayState[kMaxDelayMeasurements]); |
| 163 } | 163 } |
| 164 | 164 |
| 165 virtual ~FullDuplexAudioSinkSource() { | 165 ~FullDuplexAudioSinkSource() override { |
| 166 // Get complete file path to output file in the directory containing | 166 // Get complete file path to output file in the directory containing |
| 167 // media_unittests.exe. Example: src/build/Debug/audio_delay_values_ms.txt. | 167 // media_unittests.exe. Example: src/build/Debug/audio_delay_values_ms.txt. |
| 168 base::FilePath file_name; | 168 base::FilePath file_name; |
| 169 EXPECT_TRUE(PathService::Get(base::DIR_EXE, &file_name)); | 169 EXPECT_TRUE(PathService::Get(base::DIR_EXE, &file_name)); |
| 170 file_name = file_name.AppendASCII(kDelayValuesFileName); | 170 file_name = file_name.AppendASCII(kDelayValuesFileName); |
| 171 | 171 |
| 172 FILE* text_file = base::OpenFile(file_name, "wt"); | 172 FILE* text_file = base::OpenFile(file_name, "wt"); |
| 173 DLOG_IF(ERROR, !text_file) << "Failed to open log file."; | 173 DLOG_IF(ERROR, !text_file) << "Failed to open log file."; |
| 174 VLOG(0) << ">> Output file " << file_name.value() << " has been created."; | 174 VLOG(0) << ">> Output file " << file_name.value() << " has been created."; |
| 175 | 175 |
| 176 // Write the array which contains time-stamps, buffer size and | 176 // Write the array which contains time-stamps, buffer size and |
| 177 // audio delays values to a text file. | 177 // audio delays values to a text file. |
| 178 size_t elements_written = 0; | 178 size_t elements_written = 0; |
| 179 while (elements_written < | 179 while (elements_written < |
| 180 std::min(input_elements_to_write_, output_elements_to_write_)) { | 180 std::min(input_elements_to_write_, output_elements_to_write_)) { |
| 181 const AudioDelayState state = delay_states_[elements_written]; | 181 const AudioDelayState state = delay_states_[elements_written]; |
| 182 fprintf(text_file, "%d %d %d %d\n", | 182 fprintf(text_file, "%d %d %d %d\n", |
| 183 state.delta_time_ms, | 183 state.delta_time_ms, |
| 184 state.buffer_delay_ms, | 184 state.buffer_delay_ms, |
| 185 state.input_delay_ms, | 185 state.input_delay_ms, |
| 186 state.output_delay_ms); | 186 state.output_delay_ms); |
| 187 ++elements_written; | 187 ++elements_written; |
| 188 } | 188 } |
| 189 | 189 |
| 190 base::CloseFile(text_file); | 190 base::CloseFile(text_file); |
| 191 } | 191 } |
| 192 | 192 |
| 193 // AudioInputStream::AudioInputCallback. | 193 // AudioInputStream::AudioInputCallback. |
| 194 virtual void OnData(AudioInputStream* stream, | 194 void OnData(AudioInputStream* stream, |
| 195 const AudioBus* src, | 195 const AudioBus* src, |
| 196 uint32 hardware_delay_bytes, | 196 uint32 hardware_delay_bytes, |
| 197 double volume) override { | 197 double volume) override { |
| 198 base::AutoLock lock(lock_); | 198 base::AutoLock lock(lock_); |
| 199 | 199 |
| 200 // Update three components in the AudioDelayState for this recorded | 200 // Update three components in the AudioDelayState for this recorded |
| 201 // audio packet. | 201 // audio packet. |
| 202 const base::TimeTicks now_time = base::TimeTicks::Now(); | 202 const base::TimeTicks now_time = base::TimeTicks::Now(); |
| 203 const int diff = (now_time - previous_write_time_).InMilliseconds(); | 203 const int diff = (now_time - previous_write_time_).InMilliseconds(); |
| 204 previous_write_time_ = now_time; | 204 previous_write_time_ = now_time; |
| 205 if (input_elements_to_write_ < kMaxDelayMeasurements) { | 205 if (input_elements_to_write_ < kMaxDelayMeasurements) { |
| 206 delay_states_[input_elements_to_write_].delta_time_ms = diff; | 206 delay_states_[input_elements_to_write_].delta_time_ms = diff; |
| 207 delay_states_[input_elements_to_write_].buffer_delay_ms = | 207 delay_states_[input_elements_to_write_].buffer_delay_ms = |
| 208 BytesToMilliseconds(buffer_->forward_bytes()); | 208 BytesToMilliseconds(buffer_->forward_bytes()); |
| 209 delay_states_[input_elements_to_write_].input_delay_ms = | 209 delay_states_[input_elements_to_write_].input_delay_ms = |
| 210 BytesToMilliseconds(hardware_delay_bytes); | 210 BytesToMilliseconds(hardware_delay_bytes); |
| 211 ++input_elements_to_write_; | 211 ++input_elements_to_write_; |
| 212 } | 212 } |
| 213 | 213 |
| 214 // TODO(henrika): fix this and use AudioFifo instead. | 214 // TODO(henrika): fix this and use AudioFifo instead. |
| 215 // Store the captured audio packet in a seekable media buffer. | 215 // Store the captured audio packet in a seekable media buffer. |
| 216 // if (!buffer_->Append(src, size)) { | 216 // if (!buffer_->Append(src, size)) { |
| 217 // An attempt to write outside the buffer limits has been made. | 217 // An attempt to write outside the buffer limits has been made. |
| 218 // Double the buffer capacity to ensure that we have a buffer large | 218 // Double the buffer capacity to ensure that we have a buffer large |
| 219 // enough to handle the current sample test scenario. | 219 // enough to handle the current sample test scenario. |
| 220 // buffer_->set_forward_capacity(2 * buffer_->forward_capacity()); | 220 // buffer_->set_forward_capacity(2 * buffer_->forward_capacity()); |
| 221 // buffer_->Clear(); | 221 // buffer_->Clear(); |
| 222 // } | 222 // } |
| 223 } | 223 } |
| 224 | 224 |
| 225 virtual void OnError(AudioInputStream* stream) override {} | 225 void OnError(AudioInputStream* stream) override {} |
| 226 | 226 |
| 227 // AudioOutputStream::AudioSourceCallback. | 227 // AudioOutputStream::AudioSourceCallback. |
| 228 virtual int OnMoreData(AudioBus* audio_bus, | 228 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override { |
| 229 uint32 total_bytes_delay) override { | |
| 230 base::AutoLock lock(lock_); | 229 base::AutoLock lock(lock_); |
| 231 | 230 |
| 232 // Update one component in the AudioDelayState for the packet | 231 // Update one component in the AudioDelayState for the packet |
| 233 // which is about to be played out. | 232 // which is about to be played out. |
| 234 if (output_elements_to_write_ < kMaxDelayMeasurements) { | 233 if (output_elements_to_write_ < kMaxDelayMeasurements) { |
| 235 delay_states_[output_elements_to_write_].output_delay_ms = | 234 delay_states_[output_elements_to_write_].output_delay_ms = |
| 236 BytesToMilliseconds(total_bytes_delay); | 235 BytesToMilliseconds(total_bytes_delay); |
| 237 ++output_elements_to_write_; | 236 ++output_elements_to_write_; |
| 238 } | 237 } |
| 239 | 238 |
| 240 int size; | 239 int size; |
| 241 const uint8* source; | 240 const uint8* source; |
| 242 // Read the data from the seekable media buffer which contains | 241 // Read the data from the seekable media buffer which contains |
| 243 // captured data at the same size and sample rate as the output side. | 242 // captured data at the same size and sample rate as the output side. |
| 244 if (buffer_->GetCurrentChunk(&source, &size) && size > 0) { | 243 if (buffer_->GetCurrentChunk(&source, &size) && size > 0) { |
| 245 EXPECT_EQ(channels_, audio_bus->channels()); | 244 EXPECT_EQ(channels_, audio_bus->channels()); |
| 246 size = std::min(audio_bus->frames() * frame_size_, size); | 245 size = std::min(audio_bus->frames() * frame_size_, size); |
| 247 EXPECT_EQ(static_cast<size_t>(size) % sizeof(*audio_bus->channel(0)), 0U); | 246 EXPECT_EQ(static_cast<size_t>(size) % sizeof(*audio_bus->channel(0)), 0U); |
| 248 audio_bus->FromInterleaved( | 247 audio_bus->FromInterleaved( |
| 249 source, size / frame_size_, frame_size_ / channels_); | 248 source, size / frame_size_, frame_size_ / channels_); |
| 250 buffer_->Seek(size); | 249 buffer_->Seek(size); |
| 251 return size / frame_size_; | 250 return size / frame_size_; |
| 252 } | 251 } |
| 253 | 252 |
| 254 return 0; | 253 return 0; |
| 255 } | 254 } |
| 256 | 255 |
| 257 virtual void OnError(AudioOutputStream* stream) override {} | 256 void OnError(AudioOutputStream* stream) override {} |
| 258 | 257 |
| 259 protected: | 258 protected: |
| 260 // Converts from bytes to milliseconds taking the sample rate and size | 259 // Converts from bytes to milliseconds taking the sample rate and size |
| 261 // of an audio frame into account. | 260 // of an audio frame into account. |
| 262 int BytesToMilliseconds(uint32 delay_bytes) const { | 261 int BytesToMilliseconds(uint32 delay_bytes) const { |
| 263 return static_cast<int>((delay_bytes / frame_size_) * frames_to_ms_ + 0.5); | 262 return static_cast<int>((delay_bytes / frame_size_) * frames_to_ms_ + 0.5); |
| 264 } | 263 } |
| 265 | 264 |
| 266 private: | 265 private: |
| 267 base::Lock lock_; | 266 base::Lock lock_; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 | 436 |
| 438 // All Close() operations that run on the mocked audio thread, | 437 // All Close() operations that run on the mocked audio thread, |
| 439 // should be synchronous and not post additional close tasks to | 438 // should be synchronous and not post additional close tasks to |
| 440 // mocked the audio thread. Hence, there is no need to call | 439 // mocked the audio thread. Hence, there is no need to call |
| 441 // message_loop()->RunUntilIdle() after the Close() methods. | 440 // message_loop()->RunUntilIdle() after the Close() methods. |
| 442 aos->Close(); | 441 aos->Close(); |
| 443 ais->Close(); | 442 ais->Close(); |
| 444 } | 443 } |
| 445 | 444 |
| 446 } // namespace media | 445 } // namespace media |
| OLD | NEW |