summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/audio_renderer_impl_unittest.cc
blob: 9ddb77e70f40e85da5df119b79fb1a5cdbbbd622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/message_loop.h"
#include "base/process_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "base/time.h"
#include "content/common/child_process.h"
#include "content/common/child_thread.h"
#include "content/common/media/audio_messages.h"
#include "content/renderer/media/audio_renderer_impl.h"
#include "content/renderer/mock_content_renderer_client.h"
#include "content/renderer/render_process.h"
#include "content/renderer/render_thread_impl.h"
#include "ipc/ipc_channel.h"
#include "media/base/data_buffer.h"
#include "media/base/mock_callback.h"
#include "media/base/mock_filter_host.h"
#include "media/base/mock_filters.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::Return;

namespace {
// This class is a mock of the child process singleton which is needed
// to be able to create a RenderThread object.
class MockRenderProcess : public RenderProcess {
 public:
  MockRenderProcess() {}
  virtual ~MockRenderProcess() {}

  // RenderProcess implementation.
  virtual skia::PlatformCanvas* GetDrawingCanvas(TransportDIB** memory,
      const gfx::Rect& rect) { return NULL; }
  virtual void ReleaseTransportDIB(TransportDIB* memory) {}
  virtual bool UseInProcessPlugins() const { return false; }
  virtual bool HasInitializedMediaLibrary() const { return false; }

 private:
  DISALLOW_COPY_AND_ASSIGN(MockRenderProcess);
};
}

// This class defines a set of methods which will be used in combination
// with NewRunnableMethod to form tasks which will be posted on the
// IO thread. All methods emulate AudioMessageFilter::Delegate calls.
class DelegateCaller : public base::RefCountedThreadSafe<DelegateCaller> {
 public:
  explicit DelegateCaller(AudioRendererImpl* renderer)
      : renderer_(renderer) {}

  void OnCreated(base::SharedMemoryHandle handle, uint32 length) {
    if (renderer_->latency_type() == AudioRendererImpl::kHighLatency) {
      renderer_->OnCreated(handle, length);
    } else {
      renderer_->OnLowLatencyCreated(handle, 0, length);
    }
  }
  void OnStateChanged(AudioStreamState state) {
    renderer_->OnStateChanged(state);
  }
  void OnRequestPacket(AudioBuffersState buffers_state) {
    renderer_->OnRequestPacket(buffers_state);
  }
  void OnVolume(double volume) {
    renderer_->OnVolume(volume);
  }
  void DestroyCurrentMessageLoop() {
    renderer_->WillDestroyCurrentMessageLoop();
  }
 private:
  friend class base::RefCountedThreadSafe<DelegateCaller>;
  virtual ~DelegateCaller() {}

  scoped_refptr<AudioRendererImpl> renderer_;
  DISALLOW_COPY_AND_ASSIGN(DelegateCaller);
};

// This task can be posted on the IO thread and will signal an event when
// done. The caller can then wait for this signal to ensure that no
// additional tasks remain in the task queue.
class WaitTask : public Task {
 public:
  explicit WaitTask(base::WaitableEvent* event)
      : event_(event) {}
  virtual ~WaitTask() {}
  virtual void Run() {
    event_->Signal();
  }

 private:
  base::WaitableEvent* event_;
  DISALLOW_COPY_AND_ASSIGN(WaitTask);
};

// Class we would be testing. The only difference between it and "real" one
// is that test class does not open sockets and launch audio thread.
class TestAudioRendererImpl : public AudioRendererImpl {
 public:
  explicit TestAudioRendererImpl()
      : AudioRendererImpl() {
  }
 private:
  virtual void CreateSocket(base::SyncSocket::Handle socket_handle) {}
  virtual void CreateAudioThread() {}
};

class AudioRendererImplTest
    : public ::testing::Test,
      public IPC::Channel::Listener {
 public:
  static void SetUpTestCase() {
    // Set low latency mode, as it soon would be on by default.
    if (AudioRendererImpl::latency_type() ==
        AudioRendererImpl::kUninitializedLatency) {
      AudioRendererImpl::set_latency_type(AudioRendererImpl::kLowLatency);
    }
    DCHECK_EQ(AudioRendererImpl::kLowLatency,
              AudioRendererImpl::latency_type());
  }

  // IPC::Channel::Listener implementation.
  virtual bool OnMessageReceived(const IPC::Message& message) {
      NOTIMPLEMENTED();
      return true;
  }

  static const int kSize;

  AudioRendererImplTest() {}
  virtual ~AudioRendererImplTest() {}

  virtual void SetUp() {
    // This part sets up a RenderThread environment to ensure that
    // RenderThread::current() (<=> TLS pointer) is valid.
    // Main parts are inspired by the RenderViewFakeResourcesTest.
    // Note that, the IPC part is not utilized in this test.
    content::GetContentClient()->set_renderer(&mock_content_renderer_client_);

    static const char kThreadName[] = "RenderThread";
    channel_.reset(new IPC::Channel(kThreadName,
        IPC::Channel::MODE_SERVER, this));
    ASSERT_TRUE(channel_->Connect());

    mock_process_.reset(new MockRenderProcess);
    render_thread_ = new RenderThreadImpl(kThreadName);
    mock_process_->set_main_thread(render_thread_);

    // Create temporary shared memory.
    CHECK(shared_mem_.CreateAnonymous(kSize));

    // Setup expectations for initialization.
    decoder_ = new media::MockAudioDecoder();

    EXPECT_CALL(*decoder_, bits_per_channel())
        .WillRepeatedly(Return(16));
    EXPECT_CALL(*decoder_, channel_layout())
        .WillRepeatedly(Return(CHANNEL_LAYOUT_MONO));
    EXPECT_CALL(*decoder_, samples_per_second())
        .WillRepeatedly(Return(44100));

    // Create and initialize the audio renderer.
    renderer_ = new TestAudioRendererImpl();
    renderer_->set_host(&host_);
    renderer_->Initialize(decoder_, media::NewExpectedClosure());

    // Wraps delegate calls into tasks.
    delegate_caller_ = new DelegateCaller(renderer_);

    // We need an event to verify that all tasks are done before leaving
    // our tests.
    event_.reset(new base::WaitableEvent(false, false));

    // Duplicate the shared memory handle so both the test and the callee can
    // close their copy.
    base::SharedMemoryHandle duplicated_handle;
    EXPECT_TRUE(shared_mem_.ShareToProcess(base::GetCurrentProcessHandle(),
      &duplicated_handle));

    // Set things up and ensure that the call comes from the IO thread
    // as all AudioMessageFilter::Delegate methods.
    ChildProcess::current()->io_message_loop()->PostTask(
        FROM_HERE,
        base::Bind(&DelegateCaller::OnCreated, delegate_caller_.get(),
                   duplicated_handle, kSize));
    WaitForIOThreadCompletion();
  }

  virtual void TearDown() {
    mock_process_.reset();
  }

 protected:
  // Posts a final task to the IO message loop and waits for completion.
  void WaitForIOThreadCompletion() {
    ChildProcess::current()->io_message_loop()->PostTask(
        FROM_HERE, new WaitTask(event_.get()));
    EXPECT_TRUE(event_->TimedWait(
        base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms())));
  }

  MessageLoopForIO message_loop_;
  content::MockContentRendererClient mock_content_renderer_client_;
  scoped_ptr<IPC::Channel> channel_;
  RenderThreadImpl* render_thread_;  // owned by mock_process_
  scoped_ptr<MockRenderProcess> mock_process_;
  base::SharedMemory shared_mem_;
  media::MockFilterHost host_;
  scoped_refptr<media::MockAudioDecoder> decoder_;
  scoped_refptr<AudioRendererImpl> renderer_;
  scoped_ptr<base::WaitableEvent> event_;
  scoped_refptr<DelegateCaller> delegate_caller_;

 private:
  DISALLOW_COPY_AND_ASSIGN(AudioRendererImplTest);
};

const int AudioRendererImplTest::kSize = 1024;

TEST_F(AudioRendererImplTest, SetPlaybackRate) {
  // Execute SetPlaybackRate() codepath by toggling play/pause.
  // These methods will be called on the pipeline thread but calling from
  // here is fine for this test. Tasks will be posted internally on
  // the IO thread.
  renderer_->SetPlaybackRate(0.0f);
  renderer_->SetPlaybackRate(1.0f);
  renderer_->SetPlaybackRate(0.0f);

  renderer_->Stop(media::NewExpectedClosure());
  WaitForIOThreadCompletion();
}

TEST_F(AudioRendererImplTest, SetVolume) {
  // Execute SetVolume() codepath.
  // This method will be called on the pipeline thread IRL.
  // Tasks will be posted internally on the IO thread.
  renderer_->SetVolume(0.5f);

  renderer_->Stop(media::NewExpectedClosure());
  WaitForIOThreadCompletion();
}

TEST_F(AudioRendererImplTest, Stop) {
  // Execute Stop() codepath.
  // Tasks will be posted internally on the IO thread.
  renderer_->Stop(media::NewExpectedClosure());

  // Run AudioMessageFilter::Delegate methods, which can be executed after being
  // stopped. AudioRendererImpl shouldn't create any messages in this state.
  // All delegate method calls are posted on the IO thread since it is
  // a requirement.
  if (renderer_->latency_type() == AudioRendererImpl::kHighLatency) {
    ChildProcess::current()->io_message_loop()->PostTask(
        FROM_HERE,
        base::Bind(&DelegateCaller::OnRequestPacket,
                   delegate_caller_.get(), AudioBuffersState(kSize, 0)));
  }
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&DelegateCaller::OnStateChanged,
                 delegate_caller_.get(), kAudioStreamError));
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&DelegateCaller::OnStateChanged,
                 delegate_caller_.get(), kAudioStreamPlaying));
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&DelegateCaller::OnStateChanged,
                 delegate_caller_.get(), kAudioStreamPaused));
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&DelegateCaller::OnCreated,
                 delegate_caller_.get(), shared_mem_.handle(), kSize));
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&DelegateCaller::OnVolume,
                 delegate_caller_.get(), 0.5));

  WaitForIOThreadCompletion();

  // It's possible that the upstream decoder replies right after being stopped.
  scoped_refptr<media::Buffer> buffer(new media::DataBuffer(kSize));
  renderer_->ConsumeAudioSamples(buffer);
}

TEST_F(AudioRendererImplTest, DestroyedMessageLoop_SetPlaybackRate) {
  // Emulate "killing the message loop" and verify that SetPlaybackRate()
  // still works.
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&DelegateCaller::DestroyCurrentMessageLoop,
                 delegate_caller_.get()));
  WaitForIOThreadCompletion();

  // No tasks will be posted on the IO thread here since we are in
  // a "stopped" state.
  renderer_->SetPlaybackRate(0.0f);
  renderer_->SetPlaybackRate(1.0f);
  renderer_->SetPlaybackRate(0.0f);
  renderer_->Stop(media::NewExpectedClosure());
}

TEST_F(AudioRendererImplTest, DestroyedMessageLoop_SetVolume) {
  // Emulate "killing the message loop" and verify that SetVolume()
  // still works.
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&DelegateCaller::DestroyCurrentMessageLoop,
                 delegate_caller_.get()));
  WaitForIOThreadCompletion();

  // No tasks will be posted on the IO thread here since we are in
  // a "stopped" state.
  renderer_->SetVolume(0.5f);
  renderer_->Stop(media::NewExpectedClosure());
}

TEST_F(AudioRendererImplTest, DestroyedMessageLoop_ConsumeAudioSamples) {
  // Emulate "killing the message loop" and verify that OnReadComplete()
  // still works.
  ChildProcess::current()->io_message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&DelegateCaller::DestroyCurrentMessageLoop,
                 delegate_caller_.get()));
  WaitForIOThreadCompletion();

  // No tasks will be posted on the IO thread here since we are in
  // a "stopped" state.
  scoped_refptr<media::Buffer> buffer(new media::DataBuffer(kSize));
  renderer_->ConsumeAudioSamples(buffer);
  renderer_->Stop(media::NewExpectedClosure());
}

TEST_F(AudioRendererImplTest, UpdateEarliestEndTime) {
  renderer_->SetPlaybackRate(1.0f);
  WaitForIOThreadCompletion();
  base::Time time_now = base::Time();  // Null time by default.
  renderer_->set_earliest_end_time(time_now);
  renderer_->UpdateEarliestEndTime(renderer_->bytes_per_second(),
                                   base::TimeDelta::FromMilliseconds(100),
                                   time_now);
  int time_delta = (renderer_->earliest_end_time() - time_now).InMilliseconds();
  EXPECT_EQ(1100, time_delta);
  renderer_->Stop(media::NewExpectedClosure());
  WaitForIOThreadCompletion();
}