OLD | NEW |
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2017 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/memory/ptr_util.h" | 5 #include "base/memory/ptr_util.h" |
| 6 #include "base/memory/shared_memory.h" |
6 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/process/process_handle.h" |
7 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
8 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/sync_socket.h" |
9 #include "media/audio/audio_input_device.h" | 12 #include "media/audio/audio_input_device.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gmock_mutant.h" | 14 #include "testing/gmock_mutant.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
13 | 16 |
| 17 using base::CancelableSyncSocket; |
| 18 using base::SharedMemory; |
| 19 using base::SyncSocket; |
14 using testing::_; | 20 using testing::_; |
15 using testing::DoAll; | 21 using testing::DoAll; |
| 22 using testing::Invoke; |
16 | 23 |
17 namespace media { | 24 namespace media { |
18 | 25 |
19 namespace { | 26 namespace { |
20 | 27 |
21 class MockAudioInputIPC : public AudioInputIPC { | 28 class MockAudioInputIPC : public AudioInputIPC { |
22 public: | 29 public: |
23 MockAudioInputIPC() {} | 30 MockAudioInputIPC() {} |
24 ~MockAudioInputIPC() override {} | 31 ~MockAudioInputIPC() override {} |
25 | 32 |
26 MOCK_METHOD5(CreateStream, | 33 MOCK_METHOD5(CreateStream, |
27 void(AudioInputIPCDelegate* delegate, | 34 void(AudioInputIPCDelegate* delegate, |
28 int session_id, | 35 int session_id, |
29 const AudioParameters& params, | 36 const AudioParameters& params, |
30 bool automatic_gain_control, | 37 bool automatic_gain_control, |
31 uint32_t total_segments)); | 38 uint32_t total_segments)); |
32 MOCK_METHOD0(RecordStream, void()); | 39 MOCK_METHOD0(RecordStream, void()); |
33 MOCK_METHOD1(SetVolume, void(double volume)); | 40 MOCK_METHOD1(SetVolume, void(double volume)); |
34 MOCK_METHOD0(CloseStream, void()); | 41 MOCK_METHOD0(CloseStream, void()); |
35 }; | 42 }; |
36 | 43 |
37 class MockCaptureCallback : public AudioCapturerSource::CaptureCallback { | 44 class MockCaptureCallback : public AudioCapturerSource::CaptureCallback { |
38 public: | 45 public: |
39 MockCaptureCallback() {} | 46 MockCaptureCallback() {} |
40 ~MockCaptureCallback() override {} | 47 ~MockCaptureCallback() override {} |
41 | 48 |
| 49 MOCK_METHOD0(OnCaptureStarted, void()); |
42 MOCK_METHOD4(Capture, | 50 MOCK_METHOD4(Capture, |
43 void(const AudioBus* audio_source, | 51 void(const AudioBus* audio_source, |
44 int audio_delay_milliseconds, | 52 int audio_delay_milliseconds, |
45 double volume, | 53 double volume, |
46 bool key_pressed)); | 54 bool key_pressed)); |
47 | 55 |
48 MOCK_METHOD1(OnCaptureError, void(const std::string& message)); | 56 MOCK_METHOD1(OnCaptureError, void(const std::string& message)); |
49 }; | 57 }; |
50 | 58 |
51 // Used to terminate a loop from a different thread than the loop belongs to. | 59 // Used to terminate a loop from a different thread than the loop belongs to. |
(...skipping 28 matching lines...) Expand all Loading... |
80 new AudioInputDevice(base::WrapUnique(input_ipc), io_loop.task_runner())); | 88 new AudioInputDevice(base::WrapUnique(input_ipc), io_loop.task_runner())); |
81 device->Initialize(params, &callback, 1); | 89 device->Initialize(params, &callback, 1); |
82 device->Start(); | 90 device->Start(); |
83 EXPECT_CALL(*input_ipc, CreateStream(_, _, _, _, _)) | 91 EXPECT_CALL(*input_ipc, CreateStream(_, _, _, _, _)) |
84 .WillOnce(ReportStateChange(device.get())); | 92 .WillOnce(ReportStateChange(device.get())); |
85 EXPECT_CALL(callback, OnCaptureError(_)) | 93 EXPECT_CALL(callback, OnCaptureError(_)) |
86 .WillOnce(QuitLoop(io_loop.task_runner())); | 94 .WillOnce(QuitLoop(io_loop.task_runner())); |
87 base::RunLoop().Run(); | 95 base::RunLoop().Run(); |
88 } | 96 } |
89 | 97 |
| 98 ACTION_P5(ReportOnStreamCreated, device, handle, socket, length, segments) { |
| 99 static_cast<AudioInputIPCDelegate*>(device)->OnStreamCreated( |
| 100 handle, socket, length, segments); |
| 101 } |
| 102 |
| 103 TEST(AudioInputDeviceTest, CreateStream) { |
| 104 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 105 CHANNEL_LAYOUT_STEREO, 48000, 16, 480); |
| 106 SharedMemory shared_memory; |
| 107 CancelableSyncSocket browser_socket; |
| 108 CancelableSyncSocket renderer_socket; |
| 109 |
| 110 const int memory_size = sizeof(AudioInputBufferParameters) + |
| 111 AudioBus::CalculateMemorySize(params); |
| 112 |
| 113 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(memory_size)); |
| 114 memset(shared_memory.memory(), 0xff, memory_size); |
| 115 |
| 116 ASSERT_TRUE( |
| 117 CancelableSyncSocket::CreatePair(&browser_socket, &renderer_socket)); |
| 118 SyncSocket::TransitDescriptor audio_device_socket_descriptor; |
| 119 ASSERT_TRUE(renderer_socket.PrepareTransitDescriptor( |
| 120 base::GetCurrentProcessHandle(), &audio_device_socket_descriptor)); |
| 121 base::SharedMemoryHandle duplicated_memory_handle; |
| 122 ASSERT_TRUE(shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), |
| 123 &duplicated_memory_handle)); |
| 124 |
| 125 base::MessageLoopForIO io_loop; |
| 126 MockCaptureCallback callback; |
| 127 MockAudioInputIPC* input_ipc = new MockAudioInputIPC(); |
| 128 scoped_refptr<AudioInputDevice> device( |
| 129 new AudioInputDevice(base::WrapUnique(input_ipc), io_loop.task_runner())); |
| 130 device->Initialize(params, &callback, 1); |
| 131 device->Start(); |
| 132 |
| 133 EXPECT_CALL(*input_ipc, CreateStream(_, _, _, _, _)) |
| 134 .WillOnce(ReportOnStreamCreated( |
| 135 device.get(), duplicated_memory_handle, |
| 136 SyncSocket::UnwrapHandle(audio_device_socket_descriptor), memory_size, |
| 137 1)); |
| 138 EXPECT_CALL(*input_ipc, RecordStream()); |
| 139 EXPECT_CALL(callback, OnCaptureStarted()) |
| 140 .WillOnce(QuitLoop(io_loop.task_runner())); |
| 141 base::RunLoop().Run(); |
| 142 } |
90 } // namespace media. | 143 } // namespace media. |
OLD | NEW |