Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: media/audio/audio_system_impl_unittest.cc

Issue 2784433002: Ensures that audio tasks cannot run after AudioManager is deleted. (Closed)
Patch Set: rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/audio/audio_output_proxy_unittest.cc ('k') | media/audio/audio_thread.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 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 "media/audio/audio_system_impl.h" 5 #include "media/audio/audio_system_impl.h"
6 #include "base/memory/ptr_util.h" 6 #include "base/memory/ptr_util.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
10 #include "base/task_runner_util.h" 10 #include "base/task_runner_util.h"
11 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
12 #include "base/threading/thread_checker.h" 12 #include "base/threading/thread_checker.h"
13 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
14 #include "media/audio/audio_device_description.h" 14 #include "media/audio/audio_device_description.h"
15 #include "media/audio/audio_thread_impl.h"
15 #include "media/audio/mock_audio_manager.h" 16 #include "media/audio/mock_audio_manager.h"
17 #include "media/audio/test_audio_thread.h"
16 #include "media/base/test_helpers.h" 18 #include "media/base/test_helpers.h"
17 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 21
20 namespace { 22 namespace {
21 const char* kNonDefaultDeviceId = "non-default-device-id"; 23 const char* kNonDefaultDeviceId = "non-default-device-id";
22 } 24 }
23 25
24 namespace media { 26 namespace media {
25 27
26 bool operator==(const media::AudioDeviceDescription& lhs, 28 bool operator==(const media::AudioDeviceDescription& lhs,
27 const media::AudioDeviceDescription& rhs) { 29 const media::AudioDeviceDescription& rhs) {
28 return lhs.device_name == rhs.device_name && lhs.unique_id == rhs.unique_id && 30 return lhs.device_name == rhs.device_name && lhs.unique_id == rhs.unique_id &&
29 lhs.group_id == rhs.group_id; 31 lhs.group_id == rhs.group_id;
30 } 32 }
31 33
32 class AudioSystemImplTest : public testing::TestWithParam<bool> { 34 class AudioSystemImplTest : public testing::TestWithParam<bool> {
33 public: 35 public:
34 AudioSystemImplTest() 36 AudioSystemImplTest()
35 : use_audio_thread_(GetParam()), 37 : use_audio_thread_(GetParam()),
36 audio_thread_("AudioSystemThread"),
37 input_params_(AudioParameters::AUDIO_PCM_LINEAR, 38 input_params_(AudioParameters::AUDIO_PCM_LINEAR,
38 CHANNEL_LAYOUT_MONO, 39 CHANNEL_LAYOUT_MONO,
39 AudioParameters::kTelephoneSampleRate, 40 AudioParameters::kTelephoneSampleRate,
40 16, 41 16,
41 AudioParameters::kTelephoneSampleRate / 10), 42 AudioParameters::kTelephoneSampleRate / 10),
42 output_params_(AudioParameters::AUDIO_PCM_LINEAR, 43 output_params_(AudioParameters::AUDIO_PCM_LINEAR,
43 CHANNEL_LAYOUT_MONO, 44 CHANNEL_LAYOUT_MONO,
44 AudioParameters::kTelephoneSampleRate, 45 AudioParameters::kTelephoneSampleRate,
45 16, 46 16,
46 AudioParameters::kTelephoneSampleRate / 20), 47 AudioParameters::kTelephoneSampleRate / 20),
47 default_output_params_(AudioParameters::AUDIO_PCM_LINEAR, 48 default_output_params_(AudioParameters::AUDIO_PCM_LINEAR,
48 CHANNEL_LAYOUT_MONO, 49 CHANNEL_LAYOUT_MONO,
49 AudioParameters::kTelephoneSampleRate, 50 AudioParameters::kTelephoneSampleRate,
50 16, 51 16,
51 AudioParameters::kTelephoneSampleRate / 30) { 52 AudioParameters::kTelephoneSampleRate / 30) {
52 if (use_audio_thread_) { 53 audio_manager_ = base::MakeUnique<MockAudioManager>(
53 audio_thread_.StartAndWaitForTesting(); 54 base::MakeUnique<TestAudioThread>(use_audio_thread_));
54 audio_manager_.reset(
55 new media::MockAudioManager(audio_thread_.task_runner()));
56 } else {
57 audio_manager_.reset(new media::MockAudioManager(
58 base::ThreadTaskRunnerHandle::Get().get()));
59 }
60
61 audio_manager_->SetInputStreamParameters(input_params_); 55 audio_manager_->SetInputStreamParameters(input_params_);
62 audio_manager_->SetOutputStreamParameters(output_params_); 56 audio_manager_->SetOutputStreamParameters(output_params_);
63 audio_manager_->SetDefaultOutputStreamParameters(default_output_params_); 57 audio_manager_->SetDefaultOutputStreamParameters(default_output_params_);
64 58
65 auto get_device_descriptions = [](const AudioDeviceDescriptions* source, 59 auto get_device_descriptions = [](const AudioDeviceDescriptions* source,
66 AudioDeviceDescriptions* destination) { 60 AudioDeviceDescriptions* destination) {
67 destination->insert(destination->end(), source->begin(), source->end()); 61 destination->insert(destination->end(), source->begin(), source->end());
68 }; 62 };
69 63
70 audio_manager_->SetInputDeviceDescriptionsCallback( 64 audio_manager_->SetInputDeviceDescriptionsCallback(
71 base::Bind(get_device_descriptions, 65 base::Bind(get_device_descriptions,
72 base::Unretained(&input_device_descriptions_))); 66 base::Unretained(&input_device_descriptions_)));
73 audio_manager_->SetOutputDeviceDescriptionsCallback( 67 audio_manager_->SetOutputDeviceDescriptionsCallback(
74 base::Bind(get_device_descriptions, 68 base::Bind(get_device_descriptions,
75 base::Unretained(&output_device_descriptions_))); 69 base::Unretained(&output_device_descriptions_)));
76 70
77 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); 71 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get());
78 EXPECT_EQ(AudioSystem::Get(), audio_system_.get()); 72 EXPECT_EQ(AudioSystem::Get(), audio_system_.get());
79 } 73 }
80 74
81 ~AudioSystemImplTest() override { 75 ~AudioSystemImplTest() override { audio_manager_->Shutdown(); }
82 // Deleting |audio_manager_| on its thread.
83 audio_system_.reset();
84 EXPECT_EQ(AudioSystem::Get(), nullptr);
85 audio_manager_.reset();
86 audio_thread_.Stop();
87 }
88 76
89 void OnAudioParams(const AudioParameters& expected, 77 void OnAudioParams(const AudioParameters& expected,
90 const AudioParameters& received) { 78 const AudioParameters& received) {
91 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); 79 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
92 EXPECT_EQ(expected.AsHumanReadableString(), 80 EXPECT_EQ(expected.AsHumanReadableString(),
93 received.AsHumanReadableString()); 81 received.AsHumanReadableString());
94 AudioParametersReceived(); 82 AudioParametersReceived();
95 } 83 }
96 84
97 void OnHasInputDevices(bool result) { 85 void OnHasInputDevices(bool result) {
(...skipping 28 matching lines...) Expand all
126 EXPECT_EQ(expected_associated_device_id, associated_device_id); 114 EXPECT_EQ(expected_associated_device_id, associated_device_id);
127 InputDeviceInfoReceived(); 115 InputDeviceInfoReceived();
128 } 116 }
129 117
130 void WaitForCallback() { 118 void WaitForCallback() {
131 if (!use_audio_thread_) { 119 if (!use_audio_thread_) {
132 base::RunLoop().RunUntilIdle(); 120 base::RunLoop().RunUntilIdle();
133 return; 121 return;
134 } 122 }
135 WaitableMessageLoopEvent event; 123 WaitableMessageLoopEvent event;
136 audio_thread_.task_runner()->PostTaskAndReply( 124 audio_manager_->GetTaskRunner()->PostTaskAndReply(
137 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); 125 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure());
138 // Runs the loop and waits for the |audio_thread_| to call event's closure, 126 // Runs the loop and waits for the |audio_thread_| to call event's closure,
139 // which means AudioSystem reply containing device parameters is already 127 // which means AudioSystem reply containing device parameters is already
140 // queued on the main thread. 128 // queued on the main thread.
141 event.RunAndWait(); 129 event.RunAndWait();
142 base::RunLoop().RunUntilIdle(); 130 base::RunLoop().RunUntilIdle();
143 } 131 }
144 132
145 // Mocks to verify that AudioSystem replied with an expected callback. 133 // Mocks to verify that AudioSystem replied with an expected callback.
146 MOCK_METHOD0(AudioParametersReceived, void(void)); 134 MOCK_METHOD0(AudioParametersReceived, void(void));
147 MOCK_METHOD1(HasInputDevicesCallback, void(bool)); 135 MOCK_METHOD1(HasInputDevicesCallback, void(bool));
148 MOCK_METHOD1(HasOutputDevicesCallback, void(bool)); 136 MOCK_METHOD1(HasOutputDevicesCallback, void(bool));
149 MOCK_METHOD0(DeviceDescriptionsReceived, void(void)); 137 MOCK_METHOD0(DeviceDescriptionsReceived, void(void));
150 MOCK_METHOD1(AssociatedOutputDeviceIDReceived, void(const std::string&)); 138 MOCK_METHOD1(AssociatedOutputDeviceIDReceived, void(const std::string&));
151 MOCK_METHOD0(InputDeviceInfoReceived, void(void)); 139 MOCK_METHOD0(InputDeviceInfoReceived, void(void));
152 140
153 protected: 141 protected:
154 base::MessageLoop message_loop_; 142 base::MessageLoop message_loop_;
155 base::ThreadChecker thread_checker_; 143 base::ThreadChecker thread_checker_;
156 bool use_audio_thread_; 144 bool use_audio_thread_;
157 base::Thread audio_thread_; 145 std::unique_ptr<media::MockAudioManager> audio_manager_;
158 MockAudioManager::UniquePtr audio_manager_;
159 std::unique_ptr<media::AudioSystem> audio_system_; 146 std::unique_ptr<media::AudioSystem> audio_system_;
160 AudioParameters input_params_; 147 AudioParameters input_params_;
161 AudioParameters output_params_; 148 AudioParameters output_params_;
162 AudioParameters default_output_params_; 149 AudioParameters default_output_params_;
163 AudioDeviceDescriptions input_device_descriptions_; 150 AudioDeviceDescriptions input_device_descriptions_;
164 AudioDeviceDescriptions output_device_descriptions_; 151 AudioDeviceDescriptions output_device_descriptions_;
165 }; 152 };
166 153
167 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { 154 TEST_P(AudioSystemImplTest, GetInputStreamParameters) {
168 EXPECT_CALL(*this, AudioParametersReceived()); 155 EXPECT_CALL(*this, AudioParametersReceived());
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 audio_system_->GetInputDeviceInfo( 332 audio_system_->GetInputDeviceInfo(
346 kNonDefaultDeviceId, base::Bind(&AudioSystemImplTest::OnInputDeviceInfo, 333 kNonDefaultDeviceId, base::Bind(&AudioSystemImplTest::OnInputDeviceInfo,
347 base::Unretained(this), input_params_, 334 base::Unretained(this), input_params_,
348 output_params_, associated_id)); 335 output_params_, associated_id));
349 WaitForCallback(); 336 WaitForCallback();
350 } 337 }
351 338
352 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); 339 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true));
353 340
354 } // namespace media 341 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/audio_output_proxy_unittest.cc ('k') | media/audio/audio_thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698