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