OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "base/process_util.h" | 7 #include "base/process_util.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "content/common/child_process.h" | 11 #include "content/common/child_process.h" |
12 #include "content/common/child_thread.h" | 12 #include "content/common/child_thread.h" |
13 #include "content/common/media/audio_messages.h" | |
14 #include "content/renderer/media/audio_renderer_impl.h" | 13 #include "content/renderer/media/audio_renderer_impl.h" |
15 #include "content/renderer/mock_content_renderer_client.h" | 14 #include "content/renderer/mock_content_renderer_client.h" |
16 #include "content/renderer/render_process.h" | 15 #include "content/renderer/render_process.h" |
17 #include "content/renderer/render_thread_impl.h" | 16 #include "content/renderer/render_thread_impl.h" |
18 #include "ipc/ipc_channel.h" | 17 #include "ipc/ipc_channel.h" |
19 #include "media/base/data_buffer.h" | 18 #include "media/base/data_buffer.h" |
20 #include "media/base/mock_callback.h" | 19 #include "media/base/mock_callback.h" |
21 #include "media/base/mock_filter_host.h" | 20 #include "media/base/mock_filter_host.h" |
22 #include "media/base/mock_filters.h" | 21 #include "media/base/mock_filters.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 15 matching lines...) Expand all Loading... |
39 virtual bool UseInProcessPlugins() const { return false; } | 38 virtual bool UseInProcessPlugins() const { return false; } |
40 virtual bool HasInitializedMediaLibrary() const { return false; } | 39 virtual bool HasInitializedMediaLibrary() const { return false; } |
41 | 40 |
42 private: | 41 private: |
43 DISALLOW_COPY_AND_ASSIGN(MockRenderProcess); | 42 DISALLOW_COPY_AND_ASSIGN(MockRenderProcess); |
44 }; | 43 }; |
45 } | 44 } |
46 | 45 |
47 // This class defines a set of methods which will be used in combination | 46 // This class defines a set of methods which will be used in combination |
48 // with NewRunnableMethod to form tasks which will be posted on the | 47 // with NewRunnableMethod to form tasks which will be posted on the |
49 // IO thread. All methods emulate AudioMessageFilter::Delegate calls. | 48 // IO thread. This emulates MessageLoop::DestructionObserver. |
50 class DelegateCaller : public base::RefCountedThreadSafe<DelegateCaller> { | 49 class DelegateCaller : public base::RefCountedThreadSafe<DelegateCaller> { |
51 public: | 50 public: |
52 explicit DelegateCaller(AudioRendererImpl* renderer) | 51 explicit DelegateCaller(AudioRendererImpl* renderer) |
53 : renderer_(renderer) {} | 52 : renderer_(renderer) {} |
54 | 53 |
55 void OnCreated(base::SharedMemoryHandle handle, uint32 length) { | |
56 if (renderer_->latency_type() == AudioRendererImpl::kHighLatency) { | |
57 renderer_->OnCreated(handle, length); | |
58 } else { | |
59 renderer_->OnLowLatencyCreated(handle, 0, length); | |
60 } | |
61 } | |
62 void OnStateChanged(AudioStreamState state) { | |
63 renderer_->OnStateChanged(state); | |
64 } | |
65 void OnRequestPacket(AudioBuffersState buffers_state) { | |
66 renderer_->OnRequestPacket(buffers_state); | |
67 } | |
68 void OnVolume(double volume) { | |
69 renderer_->OnVolume(volume); | |
70 } | |
71 void DestroyCurrentMessageLoop() { | 54 void DestroyCurrentMessageLoop() { |
72 renderer_->WillDestroyCurrentMessageLoop(); | 55 renderer_->WillDestroyCurrentMessageLoop(); |
73 } | 56 } |
74 private: | 57 private: |
75 friend class base::RefCountedThreadSafe<DelegateCaller>; | 58 friend class base::RefCountedThreadSafe<DelegateCaller>; |
76 virtual ~DelegateCaller() {} | 59 virtual ~DelegateCaller() {} |
77 | 60 |
78 scoped_refptr<AudioRendererImpl> renderer_; | 61 scoped_refptr<AudioRendererImpl> renderer_; |
79 DISALLOW_COPY_AND_ASSIGN(DelegateCaller); | 62 DISALLOW_COPY_AND_ASSIGN(DelegateCaller); |
80 }; | 63 }; |
81 | 64 |
82 // This task can be posted on the IO thread and will signal an event when | 65 // This task can be posted on the IO thread and will signal an event when |
83 // done. The caller can then wait for this signal to ensure that no | 66 // done. The caller can then wait for this signal to ensure that no |
84 // additional tasks remain in the task queue. | 67 // additional tasks remain in the task queue. |
85 class WaitTask : public Task { | 68 class WaitTask : public Task { |
86 public: | 69 public: |
87 explicit WaitTask(base::WaitableEvent* event) | 70 explicit WaitTask(base::WaitableEvent* event) |
88 : event_(event) {} | 71 : event_(event) {} |
89 virtual ~WaitTask() {} | 72 virtual ~WaitTask() {} |
90 virtual void Run() { | 73 virtual void Run() { |
91 event_->Signal(); | 74 event_->Signal(); |
92 } | 75 } |
93 | 76 |
94 private: | 77 private: |
95 base::WaitableEvent* event_; | 78 base::WaitableEvent* event_; |
96 DISALLOW_COPY_AND_ASSIGN(WaitTask); | 79 DISALLOW_COPY_AND_ASSIGN(WaitTask); |
97 }; | 80 }; |
98 | 81 |
99 // Class we would be testing. The only difference between it and "real" one | 82 // Class we would be testing. |
100 // is that test class does not open sockets and launch audio thread. | |
101 class TestAudioRendererImpl : public AudioRendererImpl { | 83 class TestAudioRendererImpl : public AudioRendererImpl { |
102 public: | 84 public: |
103 explicit TestAudioRendererImpl() | 85 explicit TestAudioRendererImpl() |
104 : AudioRendererImpl() { | 86 : AudioRendererImpl() { |
105 } | 87 } |
106 private: | |
107 virtual void CreateSocket(base::SyncSocket::Handle socket_handle) {} | |
108 virtual void CreateAudioThread() {} | |
109 }; | 88 }; |
110 | 89 |
111 class AudioRendererImplTest | 90 class AudioRendererImplTest |
112 : public ::testing::Test, | 91 : public ::testing::Test, |
113 public IPC::Channel::Listener { | 92 public IPC::Channel::Listener { |
114 public: | 93 public: |
115 static void SetUpTestCase() { | 94 static void SetUpTestCase() {} |
116 // Set low latency mode, as it soon would be on by default. | |
117 if (AudioRendererImpl::latency_type() == | |
118 AudioRendererImpl::kUninitializedLatency) { | |
119 AudioRendererImpl::set_latency_type(AudioRendererImpl::kLowLatency); | |
120 } | |
121 DCHECK_EQ(AudioRendererImpl::kLowLatency, | |
122 AudioRendererImpl::latency_type()); | |
123 } | |
124 | 95 |
125 // IPC::Channel::Listener implementation. | 96 // IPC::Channel::Listener implementation. |
126 virtual bool OnMessageReceived(const IPC::Message& message) { | 97 virtual bool OnMessageReceived(const IPC::Message& message) { |
127 NOTIMPLEMENTED(); | 98 NOTIMPLEMENTED(); |
128 return true; | 99 return true; |
129 } | 100 } |
130 | 101 |
131 static const int kSize; | 102 static const int kSize; |
132 | 103 |
133 AudioRendererImplTest() {} | 104 AudioRendererImplTest() {} |
134 virtual ~AudioRendererImplTest() {} | 105 virtual ~AudioRendererImplTest() {} |
135 | 106 |
136 virtual void SetUp() { | 107 virtual void SetUp() { |
137 // This part sets up a RenderThread environment to ensure that | 108 // This part sets up a RenderThread environment to ensure that |
138 // RenderThread::current() (<=> TLS pointer) is valid. | 109 // RenderThread::current() (<=> TLS pointer) is valid. |
139 // Main parts are inspired by the RenderViewFakeResourcesTest. | 110 // Main parts are inspired by the RenderViewFakeResourcesTest. |
140 // Note that, the IPC part is not utilized in this test. | 111 // Note that, the IPC part is not utilized in this test. |
141 content::GetContentClient()->set_renderer(&mock_content_renderer_client_); | 112 content::GetContentClient()->set_renderer(&mock_content_renderer_client_); |
142 | 113 |
143 static const char kThreadName[] = "RenderThread"; | 114 static const char kThreadName[] = "RenderThread"; |
144 channel_.reset(new IPC::Channel(kThreadName, | 115 channel_.reset(new IPC::Channel(kThreadName, |
145 IPC::Channel::MODE_SERVER, this)); | 116 IPC::Channel::MODE_SERVER, this)); |
146 ASSERT_TRUE(channel_->Connect()); | 117 ASSERT_TRUE(channel_->Connect()); |
147 | 118 |
148 mock_process_.reset(new MockRenderProcess); | 119 mock_process_.reset(new MockRenderProcess); |
149 render_thread_ = new RenderThreadImpl(kThreadName); | 120 render_thread_ = new RenderThreadImpl(kThreadName); |
150 mock_process_->set_main_thread(render_thread_); | 121 mock_process_->set_main_thread(render_thread_); |
151 | 122 |
152 // Create temporary shared memory. | |
153 CHECK(shared_mem_.CreateAnonymous(kSize)); | |
154 | |
155 // Setup expectations for initialization. | 123 // Setup expectations for initialization. |
156 decoder_ = new media::MockAudioDecoder(); | 124 decoder_ = new media::MockAudioDecoder(); |
157 | 125 |
158 EXPECT_CALL(*decoder_, bits_per_channel()) | 126 EXPECT_CALL(*decoder_, bits_per_channel()) |
159 .WillRepeatedly(Return(16)); | 127 .WillRepeatedly(Return(16)); |
160 EXPECT_CALL(*decoder_, channel_layout()) | 128 EXPECT_CALL(*decoder_, channel_layout()) |
161 .WillRepeatedly(Return(CHANNEL_LAYOUT_MONO)); | 129 .WillRepeatedly(Return(CHANNEL_LAYOUT_MONO)); |
162 EXPECT_CALL(*decoder_, samples_per_second()) | 130 EXPECT_CALL(*decoder_, samples_per_second()) |
163 .WillRepeatedly(Return(44100)); | 131 .WillRepeatedly(Return(44100)); |
164 | 132 |
165 // Create and initialize the audio renderer. | 133 // Create and initialize the audio renderer. |
166 renderer_ = new TestAudioRendererImpl(); | 134 renderer_ = new TestAudioRendererImpl(); |
167 renderer_->set_host(&host_); | |
168 renderer_->Initialize(decoder_, media::NewExpectedClosure(), | 135 renderer_->Initialize(decoder_, media::NewExpectedClosure(), |
169 NewUnderflowClosure()); | 136 NewUnderflowClosure()); |
170 | 137 |
171 // Wraps delegate calls into tasks. | 138 // Wraps delegate calls into tasks. |
172 delegate_caller_ = new DelegateCaller(renderer_); | 139 delegate_caller_ = new DelegateCaller(renderer_); |
173 | 140 |
174 // We need an event to verify that all tasks are done before leaving | 141 // We need an event to verify that all tasks are done before leaving |
175 // our tests. | 142 // our tests. |
176 event_.reset(new base::WaitableEvent(false, false)); | 143 event_.reset(new base::WaitableEvent(false, false)); |
177 | |
178 // Duplicate the shared memory handle so both the test and the callee can | |
179 // close their copy. | |
180 base::SharedMemoryHandle duplicated_handle; | |
181 EXPECT_TRUE(shared_mem_.ShareToProcess(base::GetCurrentProcessHandle(), | |
182 &duplicated_handle)); | |
183 | |
184 // Set things up and ensure that the call comes from the IO thread | |
185 // as all AudioMessageFilter::Delegate methods. | |
186 ChildProcess::current()->io_message_loop()->PostTask( | |
187 FROM_HERE, | |
188 base::Bind(&DelegateCaller::OnCreated, delegate_caller_.get(), | |
189 duplicated_handle, kSize)); | |
190 WaitForIOThreadCompletion(); | |
191 } | 144 } |
192 | 145 |
193 virtual void TearDown() { | 146 virtual void TearDown() { |
194 mock_process_.reset(); | 147 mock_process_.reset(); |
195 } | 148 } |
196 | 149 |
197 MOCK_METHOD0(OnUnderflow, void()); | 150 MOCK_METHOD0(OnUnderflow, void()); |
198 | 151 |
199 base::Closure NewUnderflowClosure() { | 152 base::Closure NewUnderflowClosure() { |
200 return base::Bind(&AudioRendererImplTest::OnUnderflow, | 153 return base::Bind(&AudioRendererImplTest::OnUnderflow, |
201 base::Unretained(this)); | 154 base::Unretained(this)); |
202 } | 155 } |
203 | 156 |
204 protected: | 157 protected: |
205 // Posts a final task to the IO message loop and waits for completion. | 158 // Posts a final task to the IO message loop and waits for completion. |
206 void WaitForIOThreadCompletion() { | 159 void WaitForIOThreadCompletion() { |
207 ChildProcess::current()->io_message_loop()->PostTask( | 160 ChildProcess::current()->io_message_loop()->PostTask( |
208 FROM_HERE, new WaitTask(event_.get())); | 161 FROM_HERE, new WaitTask(event_.get())); |
209 EXPECT_TRUE(event_->TimedWait( | 162 EXPECT_TRUE(event_->TimedWait( |
210 base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()))); | 163 base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()))); |
211 } | 164 } |
212 | 165 |
213 MessageLoopForIO message_loop_; | 166 MessageLoopForIO message_loop_; |
214 content::MockContentRendererClient mock_content_renderer_client_; | 167 content::MockContentRendererClient mock_content_renderer_client_; |
215 scoped_ptr<IPC::Channel> channel_; | 168 scoped_ptr<IPC::Channel> channel_; |
216 RenderThreadImpl* render_thread_; // owned by mock_process_ | 169 RenderThreadImpl* render_thread_; // owned by mock_process_ |
217 scoped_ptr<MockRenderProcess> mock_process_; | 170 scoped_ptr<MockRenderProcess> mock_process_; |
218 base::SharedMemory shared_mem_; | |
219 media::MockFilterHost host_; | |
220 scoped_refptr<media::MockAudioDecoder> decoder_; | 171 scoped_refptr<media::MockAudioDecoder> decoder_; |
221 scoped_refptr<AudioRendererImpl> renderer_; | 172 scoped_refptr<AudioRendererImpl> renderer_; |
222 scoped_ptr<base::WaitableEvent> event_; | 173 scoped_ptr<base::WaitableEvent> event_; |
223 scoped_refptr<DelegateCaller> delegate_caller_; | 174 scoped_refptr<DelegateCaller> delegate_caller_; |
224 | 175 |
225 private: | 176 private: |
226 DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest); | 177 DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest); |
227 }; | 178 }; |
228 | 179 |
229 const int AudioRendererImplTest::kSize = 1024; | 180 const int AudioRendererImplTest::kSize = 1024; |
(...skipping 19 matching lines...) Expand all Loading... |
249 | 200 |
250 renderer_->Stop(media::NewExpectedClosure()); | 201 renderer_->Stop(media::NewExpectedClosure()); |
251 WaitForIOThreadCompletion(); | 202 WaitForIOThreadCompletion(); |
252 } | 203 } |
253 | 204 |
254 TEST_F(AudioRendererImplTest, Stop) { | 205 TEST_F(AudioRendererImplTest, Stop) { |
255 // Execute Stop() codepath. | 206 // Execute Stop() codepath. |
256 // Tasks will be posted internally on the IO thread. | 207 // Tasks will be posted internally on the IO thread. |
257 renderer_->Stop(media::NewExpectedClosure()); | 208 renderer_->Stop(media::NewExpectedClosure()); |
258 | 209 |
259 // Run AudioMessageFilter::Delegate methods, which can be executed after being | |
260 // stopped. AudioRendererImpl shouldn't create any messages in this state. | |
261 // All delegate method calls are posted on the IO thread since it is | |
262 // a requirement. | |
263 if (renderer_->latency_type() == AudioRendererImpl::kHighLatency) { | |
264 ChildProcess::current()->io_message_loop()->PostTask( | |
265 FROM_HERE, | |
266 base::Bind(&DelegateCaller::OnRequestPacket, | |
267 delegate_caller_.get(), AudioBuffersState(kSize, 0))); | |
268 } | |
269 ChildProcess::current()->io_message_loop()->PostTask( | |
270 FROM_HERE, | |
271 base::Bind(&DelegateCaller::OnStateChanged, | |
272 delegate_caller_.get(), kAudioStreamError)); | |
273 ChildProcess::current()->io_message_loop()->PostTask( | |
274 FROM_HERE, | |
275 base::Bind(&DelegateCaller::OnStateChanged, | |
276 delegate_caller_.get(), kAudioStreamPlaying)); | |
277 ChildProcess::current()->io_message_loop()->PostTask( | |
278 FROM_HERE, | |
279 base::Bind(&DelegateCaller::OnStateChanged, | |
280 delegate_caller_.get(), kAudioStreamPaused)); | |
281 ChildProcess::current()->io_message_loop()->PostTask( | |
282 FROM_HERE, | |
283 base::Bind(&DelegateCaller::OnCreated, | |
284 delegate_caller_.get(), shared_mem_.handle(), kSize)); | |
285 ChildProcess::current()->io_message_loop()->PostTask( | |
286 FROM_HERE, | |
287 base::Bind(&DelegateCaller::OnVolume, | |
288 delegate_caller_.get(), 0.5)); | |
289 | |
290 WaitForIOThreadCompletion(); | 210 WaitForIOThreadCompletion(); |
291 | 211 |
292 // It's possible that the upstream decoder replies right after being stopped. | 212 // It's possible that the upstream decoder replies right after being stopped. |
293 scoped_refptr<media::Buffer> buffer(new media::DataBuffer(kSize)); | 213 scoped_refptr<media::Buffer> buffer(new media::DataBuffer(kSize)); |
294 renderer_->ConsumeAudioSamples(buffer); | 214 renderer_->ConsumeAudioSamples(buffer); |
295 } | 215 } |
296 | 216 |
297 TEST_F(AudioRendererImplTest, DestroyedMessageLoop_SetPlaybackRate) { | 217 TEST_F(AudioRendererImplTest, DestroyedMessageLoop_SetPlaybackRate) { |
298 // Emulate "killing the message loop" and verify that SetPlaybackRate() | 218 // Emulate "killing the message loop" and verify that SetPlaybackRate() |
299 // still works. | 219 // still works. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 base::Time time_now = base::Time(); // Null time by default. | 268 base::Time time_now = base::Time(); // Null time by default. |
349 renderer_->set_earliest_end_time(time_now); | 269 renderer_->set_earliest_end_time(time_now); |
350 renderer_->UpdateEarliestEndTime(renderer_->bytes_per_second(), | 270 renderer_->UpdateEarliestEndTime(renderer_->bytes_per_second(), |
351 base::TimeDelta::FromMilliseconds(100), | 271 base::TimeDelta::FromMilliseconds(100), |
352 time_now); | 272 time_now); |
353 int time_delta = (renderer_->earliest_end_time() - time_now).InMilliseconds(); | 273 int time_delta = (renderer_->earliest_end_time() - time_now).InMilliseconds(); |
354 EXPECT_EQ(1100, time_delta); | 274 EXPECT_EQ(1100, time_delta); |
355 renderer_->Stop(media::NewExpectedClosure()); | 275 renderer_->Stop(media::NewExpectedClosure()); |
356 WaitForIOThreadCompletion(); | 276 WaitForIOThreadCompletion(); |
357 } | 277 } |
OLD | NEW |