| 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 virtual ~MockAudioManager() {} |
| 97 | 97 |
| 98 virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() OVERRIDE { | 98 virtual 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 { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 virtual 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 virtual void OnError(AudioInputStream* stream) override {} |
| 226 | 226 |
| 227 // AudioOutputStream::AudioSourceCallback. | 227 // AudioOutputStream::AudioSourceCallback. |
| 228 virtual int OnMoreData(AudioBus* audio_bus, | 228 virtual int OnMoreData(AudioBus* audio_bus, |
| 229 uint32 total_bytes_delay) OVERRIDE { | 229 uint32 total_bytes_delay) override { |
| 230 base::AutoLock lock(lock_); | 230 base::AutoLock lock(lock_); |
| 231 | 231 |
| 232 // Update one component in the AudioDelayState for the packet | 232 // Update one component in the AudioDelayState for the packet |
| 233 // which is about to be played out. | 233 // which is about to be played out. |
| 234 if (output_elements_to_write_ < kMaxDelayMeasurements) { | 234 if (output_elements_to_write_ < kMaxDelayMeasurements) { |
| 235 delay_states_[output_elements_to_write_].output_delay_ms = | 235 delay_states_[output_elements_to_write_].output_delay_ms = |
| 236 BytesToMilliseconds(total_bytes_delay); | 236 BytesToMilliseconds(total_bytes_delay); |
| 237 ++output_elements_to_write_; | 237 ++output_elements_to_write_; |
| 238 } | 238 } |
| 239 | 239 |
| 240 int size; | 240 int size; |
| 241 const uint8* source; | 241 const uint8* source; |
| 242 // Read the data from the seekable media buffer which contains | 242 // Read the data from the seekable media buffer which contains |
| 243 // captured data at the same size and sample rate as the output side. | 243 // captured data at the same size and sample rate as the output side. |
| 244 if (buffer_->GetCurrentChunk(&source, &size) && size > 0) { | 244 if (buffer_->GetCurrentChunk(&source, &size) && size > 0) { |
| 245 EXPECT_EQ(channels_, audio_bus->channels()); | 245 EXPECT_EQ(channels_, audio_bus->channels()); |
| 246 size = std::min(audio_bus->frames() * frame_size_, size); | 246 size = std::min(audio_bus->frames() * frame_size_, size); |
| 247 EXPECT_EQ(static_cast<size_t>(size) % sizeof(*audio_bus->channel(0)), 0U); | 247 EXPECT_EQ(static_cast<size_t>(size) % sizeof(*audio_bus->channel(0)), 0U); |
| 248 audio_bus->FromInterleaved( | 248 audio_bus->FromInterleaved( |
| 249 source, size / frame_size_, frame_size_ / channels_); | 249 source, size / frame_size_, frame_size_ / channels_); |
| 250 buffer_->Seek(size); | 250 buffer_->Seek(size); |
| 251 return size / frame_size_; | 251 return size / frame_size_; |
| 252 } | 252 } |
| 253 | 253 |
| 254 return 0; | 254 return 0; |
| 255 } | 255 } |
| 256 | 256 |
| 257 virtual void OnError(AudioOutputStream* stream) OVERRIDE {} | 257 virtual void OnError(AudioOutputStream* stream) override {} |
| 258 | 258 |
| 259 protected: | 259 protected: |
| 260 // Converts from bytes to milliseconds taking the sample rate and size | 260 // Converts from bytes to milliseconds taking the sample rate and size |
| 261 // of an audio frame into account. | 261 // of an audio frame into account. |
| 262 int BytesToMilliseconds(uint32 delay_bytes) const { | 262 int BytesToMilliseconds(uint32 delay_bytes) const { |
| 263 return static_cast<int>((delay_bytes / frame_size_) * frames_to_ms_ + 0.5); | 263 return static_cast<int>((delay_bytes / frame_size_) * frames_to_ms_ + 0.5); |
| 264 } | 264 } |
| 265 | 265 |
| 266 private: | 266 private: |
| 267 base::Lock lock_; | 267 base::Lock lock_; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 | 437 |
| 438 // All Close() operations that run on the mocked audio thread, | 438 // All Close() operations that run on the mocked audio thread, |
| 439 // should be synchronous and not post additional close tasks to | 439 // should be synchronous and not post additional close tasks to |
| 440 // mocked the audio thread. Hence, there is no need to call | 440 // mocked the audio thread. Hence, there is no need to call |
| 441 // message_loop()->RunUntilIdle() after the Close() methods. | 441 // message_loop()->RunUntilIdle() after the Close() methods. |
| 442 aos->Close(); | 442 aos->Close(); |
| 443 ais->Close(); | 443 ais->Close(); |
| 444 } | 444 } |
| 445 | 445 |
| 446 } // namespace media | 446 } // namespace media |
| OLD | NEW |