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

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

Issue 2869733005: Convert some audio code to OnceCallback. (Closed)
Patch Set: Rebase, comments on unretained. 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_system.h ('k') | media/mojo/services/mojo_audio_output_stream_provider.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 6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
7 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
8 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
9 #include "base/task_runner_util.h" 13 #include "base/task_runner_util.h"
10 #include "media/audio/audio_device_description.h" 14 #include "media/audio/audio_device_description.h"
11 #include "media/audio/audio_manager.h" 15 #include "media/audio/audio_manager.h"
12 #include "media/base/bind_to_current_loop.h" 16 #include "media/base/bind_to_current_loop.h"
13 17
14 // Using base::Unretained for |audio_manager_| is safe since it is deleted after 18 // Using base::Unretained for |audio_manager_| is safe since it is deleted after
15 // its task runner, and AudioSystemImpl is deleted on the UI thread after the IO 19 // its task runner, and AudioSystemImpl is deleted on the UI thread after the IO
16 // thread has been stopped and before |audio_manager_| deletion is scheduled. 20 // thread has been stopped and before |audio_manager_| deletion is scheduled.
(...skipping 20 matching lines...) Expand all
37 OnAudioParamsCallback on_params_cb) const { 41 OnAudioParamsCallback on_params_cb) const {
38 if (GetTaskRunner()->BelongsToCurrentThread()) { 42 if (GetTaskRunner()->BelongsToCurrentThread()) {
39 GetTaskRunner()->PostTask(FROM_HERE, 43 GetTaskRunner()->PostTask(FROM_HERE,
40 base::BindOnce(std::move(on_params_cb), 44 base::BindOnce(std::move(on_params_cb),
41 GetInputParametersOnDeviceThread( 45 GetInputParametersOnDeviceThread(
42 audio_manager_, device_id))); 46 audio_manager_, device_id)));
43 return; 47 return;
44 } 48 }
45 base::PostTaskAndReplyWithResult( 49 base::PostTaskAndReplyWithResult(
46 GetTaskRunner(), FROM_HERE, 50 GetTaskRunner(), FROM_HERE,
47 base::Bind(&AudioSystemImpl::GetInputParametersOnDeviceThread, 51 base::BindOnce(&AudioSystemImpl::GetInputParametersOnDeviceThread,
48 base::Unretained(audio_manager_), device_id), 52 base::Unretained(audio_manager_), device_id),
49 std::move(on_params_cb)); 53 std::move(on_params_cb));
50 } 54 }
51 55
52 void AudioSystemImpl::GetOutputStreamParameters( 56 void AudioSystemImpl::GetOutputStreamParameters(
53 const std::string& device_id, 57 const std::string& device_id,
54 OnAudioParamsCallback on_params_cb) const { 58 OnAudioParamsCallback on_params_cb) const {
55 if (GetTaskRunner()->BelongsToCurrentThread()) { 59 if (GetTaskRunner()->BelongsToCurrentThread()) {
56 GetTaskRunner()->PostTask(FROM_HERE, 60 GetTaskRunner()->PostTask(FROM_HERE,
57 base::BindOnce(std::move(on_params_cb), 61 base::BindOnce(std::move(on_params_cb),
58 GetOutputParametersOnDeviceThread( 62 GetOutputParametersOnDeviceThread(
59 audio_manager_, device_id))); 63 audio_manager_, device_id)));
60 return; 64 return;
61 } 65 }
62 base::PostTaskAndReplyWithResult( 66 base::PostTaskAndReplyWithResult(
63 GetTaskRunner(), FROM_HERE, 67 GetTaskRunner(), FROM_HERE,
64 base::Bind(&AudioSystemImpl::GetOutputParametersOnDeviceThread, 68 base::BindOnce(&AudioSystemImpl::GetOutputParametersOnDeviceThread,
65 base::Unretained(audio_manager_), device_id), 69 base::Unretained(audio_manager_), device_id),
66 std::move(on_params_cb)); 70 std::move(on_params_cb));
67 } 71 }
68 72
69 void AudioSystemImpl::HasInputDevices(OnBoolCallback on_has_devices_cb) const { 73 void AudioSystemImpl::HasInputDevices(OnBoolCallback on_has_devices_cb) const {
70 if (GetTaskRunner()->BelongsToCurrentThread()) { 74 if (GetTaskRunner()->BelongsToCurrentThread()) {
71 GetTaskRunner()->PostTask( 75 GetTaskRunner()->PostTask(
72 FROM_HERE, base::BindOnce(std::move(on_has_devices_cb), 76 FROM_HERE, base::BindOnce(std::move(on_has_devices_cb),
73 audio_manager_->HasAudioInputDevices())); 77 audio_manager_->HasAudioInputDevices()));
74 return; 78 return;
75 } 79 }
76 base::PostTaskAndReplyWithResult( 80 base::PostTaskAndReplyWithResult(
77 GetTaskRunner(), FROM_HERE, 81 GetTaskRunner(), FROM_HERE,
78 base::Bind(&AudioManager::HasAudioInputDevices, 82 base::BindOnce(&AudioManager::HasAudioInputDevices,
79 base::Unretained(audio_manager_)), 83 base::Unretained(audio_manager_)),
80 std::move(on_has_devices_cb)); 84 std::move(on_has_devices_cb));
81 } 85 }
82 86
83 void AudioSystemImpl::HasOutputDevices(OnBoolCallback on_has_devices_cb) const { 87 void AudioSystemImpl::HasOutputDevices(OnBoolCallback on_has_devices_cb) const {
84 if (GetTaskRunner()->BelongsToCurrentThread()) { 88 if (GetTaskRunner()->BelongsToCurrentThread()) {
85 GetTaskRunner()->PostTask( 89 GetTaskRunner()->PostTask(
86 FROM_HERE, base::BindOnce(std::move(on_has_devices_cb), 90 FROM_HERE, base::BindOnce(std::move(on_has_devices_cb),
87 audio_manager_->HasAudioOutputDevices())); 91 audio_manager_->HasAudioOutputDevices()));
88 return; 92 return;
89 } 93 }
90 base::PostTaskAndReplyWithResult( 94 base::PostTaskAndReplyWithResult(
91 GetTaskRunner(), FROM_HERE, 95 GetTaskRunner(), FROM_HERE,
92 base::Bind(&AudioManager::HasAudioOutputDevices, 96 base::BindOnce(&AudioManager::HasAudioOutputDevices,
93 base::Unretained(audio_manager_)), 97 base::Unretained(audio_manager_)),
94 std::move(on_has_devices_cb)); 98 std::move(on_has_devices_cb));
95 } 99 }
96 100
97 void AudioSystemImpl::GetDeviceDescriptions( 101 void AudioSystemImpl::GetDeviceDescriptions(
98 OnDeviceDescriptionsCallback on_descriptions_cb, 102 OnDeviceDescriptionsCallback on_descriptions_cb,
99 bool for_input) { 103 bool for_input) {
100 if (GetTaskRunner()->BelongsToCurrentThread()) { 104 if (GetTaskRunner()->BelongsToCurrentThread()) {
101 GetTaskRunner()->PostTask( 105 GetTaskRunner()->PostTask(
102 FROM_HERE, 106 FROM_HERE,
103 base::BindOnce(std::move(on_descriptions_cb), 107 base::BindOnce(std::move(on_descriptions_cb),
104 base::Passed(GetDeviceDescriptionsOnDeviceThread( 108 base::Passed(GetDeviceDescriptionsOnDeviceThread(
105 audio_manager_, for_input)))); 109 audio_manager_, for_input))));
106 return; 110 return;
107 } 111 }
108 112
109 base::PostTaskAndReplyWithResult( 113 base::PostTaskAndReplyWithResult(
110 GetTaskRunner(), FROM_HERE, 114 GetTaskRunner(), FROM_HERE,
111 base::Bind(&AudioSystemImpl::GetDeviceDescriptionsOnDeviceThread, 115 base::BindOnce(&AudioSystemImpl::GetDeviceDescriptionsOnDeviceThread,
112 base::Unretained(audio_manager_), for_input), 116 base::Unretained(audio_manager_), for_input),
113 std::move(on_descriptions_cb)); 117 std::move(on_descriptions_cb));
114 } 118 }
115 119
116 void AudioSystemImpl::GetAssociatedOutputDeviceID( 120 void AudioSystemImpl::GetAssociatedOutputDeviceID(
117 const std::string& input_device_id, 121 const std::string& input_device_id,
118 OnDeviceIdCallback on_device_id_cb) { 122 OnDeviceIdCallback on_device_id_cb) {
119 if (GetTaskRunner()->BelongsToCurrentThread()) { 123 if (GetTaskRunner()->BelongsToCurrentThread()) {
120 GetTaskRunner()->PostTask( 124 GetTaskRunner()->PostTask(
121 FROM_HERE, base::BindOnce(std::move(on_device_id_cb), 125 FROM_HERE, base::BindOnce(std::move(on_device_id_cb),
122 audio_manager_->GetAssociatedOutputDeviceID( 126 audio_manager_->GetAssociatedOutputDeviceID(
123 input_device_id))); 127 input_device_id)));
124 return; 128 return;
125 } 129 }
126 base::PostTaskAndReplyWithResult( 130 base::PostTaskAndReplyWithResult(
127 GetTaskRunner(), FROM_HERE, 131 GetTaskRunner(), FROM_HERE,
128 base::Bind(&AudioManager::GetAssociatedOutputDeviceID, 132 base::BindOnce(&AudioManager::GetAssociatedOutputDeviceID,
129 base::Unretained(audio_manager_), input_device_id), 133 base::Unretained(audio_manager_), input_device_id),
130 std::move(on_device_id_cb)); 134 std::move(on_device_id_cb));
131 } 135 }
132 136
133 void AudioSystemImpl::GetInputDeviceInfo( 137 void AudioSystemImpl::GetInputDeviceInfo(
134 const std::string& input_device_id, 138 const std::string& input_device_id,
135 OnInputDeviceInfoCallback on_input_device_info_cb) { 139 OnInputDeviceInfoCallback on_input_device_info_cb) {
136 // No need to bind |on_input_device_info_cb| to the current loop if we are on 140 // No need to bind |on_input_device_info_cb| to the current loop if we are on
137 // the audio thread. However, the client still expect to receive the reply 141 // the audio thread. However, the client still expect to receive the reply
138 // asynchronously, so we always post GetInputDeviceInfoOnDeviceThread(), which 142 // asynchronously, so we always post GetInputDeviceInfoOnDeviceThread(), which
139 // will syncronously call the (bound to current loop or not) callback. 143 // will syncronously call the (bound to current loop or not) callback.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 201
198 // static 202 // static
199 void AudioSystemImpl::GetInputDeviceInfoOnDeviceThread( 203 void AudioSystemImpl::GetInputDeviceInfoOnDeviceThread(
200 AudioManager* audio_manager, 204 AudioManager* audio_manager,
201 const std::string& input_device_id, 205 const std::string& input_device_id,
202 AudioSystem::OnInputDeviceInfoCallback on_input_device_info_cb) { 206 AudioSystem::OnInputDeviceInfoCallback on_input_device_info_cb) {
203 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); 207 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread());
204 const std::string associated_output_device_id = 208 const std::string associated_output_device_id =
205 audio_manager->GetAssociatedOutputDeviceID(input_device_id); 209 audio_manager->GetAssociatedOutputDeviceID(input_device_id);
206 210
207 on_input_device_info_cb.Run( 211 std::move(on_input_device_info_cb)
208 GetInputParametersOnDeviceThread(audio_manager, input_device_id), 212 .Run(GetInputParametersOnDeviceThread(audio_manager, input_device_id),
209 associated_output_device_id.empty() 213 associated_output_device_id.empty()
210 ? AudioParameters() 214 ? AudioParameters()
211 : GetOutputParametersOnDeviceThread(audio_manager, 215 : GetOutputParametersOnDeviceThread(audio_manager,
212 associated_output_device_id), 216 associated_output_device_id),
213 associated_output_device_id); 217 associated_output_device_id);
214 } 218 }
215 219
216 } // namespace media 220 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/audio_system.h ('k') | media/mojo/services/mojo_audio_output_stream_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698