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

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

Issue 12974004: Add speaker on/off control on Android for WebRTC (Closed) Base URL: https://src.chromium.org/svn/trunk/src/
Patch Set: check mReceiver Created 7 years, 9 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_manager_base.h" 5 #include "media/audio/audio_manager_base.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/message_loop_proxy.h" 10 #include "base/message_loop_proxy.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 #endif 56 #endif
57 #if defined(OS_MACOSX) 57 #if defined(OS_MACOSX)
58 // On Mac, use a UI loop to get native message pump so that CoreAudio property 58 // On Mac, use a UI loop to get native message pump so that CoreAudio property
59 // listener callbacks fire. 59 // listener callbacks fire.
60 CHECK(audio_thread_->StartWithOptions( 60 CHECK(audio_thread_->StartWithOptions(
61 base::Thread::Options(MessageLoop::TYPE_UI, 0))); 61 base::Thread::Options(MessageLoop::TYPE_UI, 0)));
62 #else 62 #else
63 CHECK(audio_thread_->Start()); 63 CHECK(audio_thread_->Start());
64 #endif 64 #endif
65 message_loop_ = audio_thread_->message_loop_proxy(); 65 message_loop_ = audio_thread_->message_loop_proxy();
66
67 #if defined(OS_ANDROID)
68 JNIEnv* env = base::android::AttachCurrentThread();
69 jobject context = base::android::GetApplicationContext();
70 DCHECK(context);
Ami GONE FROM CHROMIUM 2013/03/21 21:26:21 unnecessary- GAC() does this
leozwang1 2013/03/21 23:10:07 Done.
71 j_audio_manager_.Reset(
72 Java_AudioManagerAndroid_createAudioManagerAndroid(env, context));
73 #endif
66 } 74 }
67 75
68 AudioManagerBase::~AudioManagerBase() { 76 AudioManagerBase::~AudioManagerBase() {
69 // The platform specific AudioManager implementation must have already 77 // The platform specific AudioManager implementation must have already
70 // stopped the audio thread. Otherwise, we may destroy audio streams before 78 // stopped the audio thread. Otherwise, we may destroy audio streams before
71 // stopping the thread, resulting an unexpected behavior. 79 // stopping the thread, resulting an unexpected behavior.
72 // This way we make sure activities of the audio streams are all stopped 80 // This way we make sure activities of the audio streams are all stopped
73 // before we destroy them. 81 // before we destroy them.
74 CHECK(!audio_thread_.get()); 82 CHECK(!audio_thread_.get());
75 // All the output streams should have been deleted. 83 // All the output streams should have been deleted.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 stream = FakeAudioOutputStream::MakeFakeStream(this, params); 129 stream = FakeAudioOutputStream::MakeFakeStream(this, params);
122 break; 130 break;
123 default: 131 default:
124 stream = NULL; 132 stream = NULL;
125 break; 133 break;
126 } 134 }
127 135
128 if (stream) 136 if (stream)
129 ++num_output_streams_; 137 ++num_output_streams_;
130 138
139 #if defined(OS_ANDROID)
140 if (1 == num_output_streams_)
tommi (sloooow) - chröme 2013/03/21 20:58:16 To be on the safe side: if (stream && 1 == num_ou
Ami GONE FROM CHROMIUM 2013/03/21 21:26:21 better yet, move this into the if at l.136 above?
leozwang1 2013/03/21 23:10:07 Done.
leozwang1 2013/03/21 23:10:07 Done.
leozwang1 2013/03/21 23:10:07 Done.
141 RegisterHeadsetReceiver();
142 #endif
143
131 return stream; 144 return stream;
132 } 145 }
133 146
134 AudioInputStream* AudioManagerBase::MakeAudioInputStream( 147 AudioInputStream* AudioManagerBase::MakeAudioInputStream(
135 const AudioParameters& params, const std::string& device_id) { 148 const AudioParameters& params, const std::string& device_id) {
136 // TODO(miu): Fix ~20 call points across several unit test modules to call 149 // TODO(miu): Fix ~20 call points across several unit test modules to call
137 // this method on the audio thread, then uncomment the following: 150 // this method on the audio thread, then uncomment the following:
138 // DCHECK(message_loop_->BelongsToCurrentThread()); 151 // DCHECK(message_loop_->BelongsToCurrentThread());
139 152
140 if (!params.IsValid() || (params.channels() > kMaxInputChannels) || 153 if (!params.IsValid() || (params.channels() > kMaxInputChannels) ||
(...skipping 22 matching lines...) Expand all
163 break; 176 break;
164 default: 177 default:
165 stream = NULL; 178 stream = NULL;
166 break; 179 break;
167 } 180 }
168 181
169 if (stream) 182 if (stream)
170 ++num_input_streams_; 183 ++num_input_streams_;
171 184
172 #if defined(OS_ANDROID) 185 #if defined(OS_ANDROID)
173 if (num_input_streams_ == 1) 186 if (1 == num_input_streams_)
tommi (sloooow) - chröme 2013/03/21 20:58:16 same here
leozwang1 2013/03/21 23:10:07 Done.
174 SetAudioMode(kAudioModeInCommunication); 187 SetAudioMode(kAudioModeInCommunication);
175 #endif 188 #endif
176 189
177 return stream; 190 return stream;
178 } 191 }
179 192
180 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( 193 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy(
181 const AudioParameters& params) { 194 const AudioParameters& params) {
182 #if defined(OS_IOS) 195 #if defined(OS_IOS)
183 // IOS implements audio input only. 196 // IOS implements audio input only.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 media::AudioDeviceNames* device_names) { 262 media::AudioDeviceNames* device_names) {
250 } 263 }
251 264
252 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) { 265 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) {
253 DCHECK(stream); 266 DCHECK(stream);
254 // TODO(xians) : Have a clearer destruction path for the AudioOutputStream. 267 // TODO(xians) : Have a clearer destruction path for the AudioOutputStream.
255 // For example, pass the ownership to AudioManager so it can delete the 268 // For example, pass the ownership to AudioManager so it can delete the
256 // streams. 269 // streams.
257 --num_output_streams_; 270 --num_output_streams_;
258 delete stream; 271 delete stream;
272 #if defined(OS_ANDROID)
273 if (!num_output_streams_)
274 UnregisterHeadsetReceiver();
275 #endif
259 } 276 }
260 277
261 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { 278 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) {
262 DCHECK(stream); 279 DCHECK(stream);
263 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. 280 // TODO(xians) : Have a clearer destruction path for the AudioInputStream.
264 --num_input_streams_; 281 --num_input_streams_;
265 delete stream; 282 delete stream;
266 #if defined(OS_ANDROID) 283 #if defined(OS_ANDROID)
267 if (!num_input_streams_) 284 if (!num_input_streams_)
268 SetAudioMode(kAudioModeNormal); 285 SetAudioMode(kAudioModeNormal);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 } 381 }
365 382
366 AudioParameters AudioManagerBase::GetInputStreamParameters( 383 AudioParameters AudioManagerBase::GetInputStreamParameters(
367 const std::string& device_id) { 384 const std::string& device_id) {
368 NOTREACHED(); 385 NOTREACHED();
369 return AudioParameters(); 386 return AudioParameters();
370 } 387 }
371 388
372 #if defined(OS_ANDROID) 389 #if defined(OS_ANDROID)
373 void AudioManagerBase::SetAudioMode(int mode) { 390 void AudioManagerBase::SetAudioMode(int mode) {
374 JNIEnv* env = base::android::AttachCurrentThread(); 391 Java_AudioManagerAndroid_setMode(
375 jobject context = base::android::GetApplicationContext(); 392 base::android::AttachCurrentThread(),
376 DCHECK(context); 393 j_audio_manager_.obj(), mode);
394 }
377 395
378 Java_AudioManagerAndroid_setMode(env, context, mode); 396 void AudioManagerBase::RegisterHeadsetReceiver() {
397 Java_AudioManagerAndroid_registerHeadsetReceiver(
398 base::android::AttachCurrentThread(),
399 j_audio_manager_.obj());
379 } 400 }
380 #endif 401
402 void AudioManagerBase::UnregisterHeadsetReceiver() {
403 Java_AudioManagerAndroid_unregisterHeadsetReceiver(
404 base::android::AttachCurrentThread(),
405 j_audio_manager_.obj());
406 }
407 #endif // defined(OS_ANDROID)
381 408
382 } // namespace media 409 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698