OLD | NEW |
---|---|
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 Loading... | |
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); | |
71 j_am_.Reset(Java_AudioManagerAndroid_createAudioManagerAndroid(env, context)); | |
72 #endif | |
66 } | 73 } |
67 | 74 |
68 AudioManagerBase::~AudioManagerBase() { | 75 AudioManagerBase::~AudioManagerBase() { |
69 // The platform specific AudioManager implementation must have already | 76 // The platform specific AudioManager implementation must have already |
70 // stopped the audio thread. Otherwise, we may destroy audio streams before | 77 // stopped the audio thread. Otherwise, we may destroy audio streams before |
71 // stopping the thread, resulting an unexpected behavior. | 78 // stopping the thread, resulting an unexpected behavior. |
72 // This way we make sure activities of the audio streams are all stopped | 79 // This way we make sure activities of the audio streams are all stopped |
73 // before we destroy them. | 80 // before we destroy them. |
74 CHECK(!audio_thread_.get()); | 81 CHECK(!audio_thread_.get()); |
75 // All the output streams should have been deleted. | 82 // All the output streams should have been deleted. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 stream = FakeAudioOutputStream::MakeFakeStream(this, params); | 128 stream = FakeAudioOutputStream::MakeFakeStream(this, params); |
122 break; | 129 break; |
123 default: | 130 default: |
124 stream = NULL; | 131 stream = NULL; |
125 break; | 132 break; |
126 } | 133 } |
127 | 134 |
128 if (stream) | 135 if (stream) |
129 ++num_output_streams_; | 136 ++num_output_streams_; |
130 | 137 |
138 #if defined(OS_ANDROID) | |
139 if (1 == num_output_streams_) | |
140 RegisterHeadsetReceiver(); | |
141 #endif | |
142 | |
131 return stream; | 143 return stream; |
132 } | 144 } |
133 | 145 |
134 AudioInputStream* AudioManagerBase::MakeAudioInputStream( | 146 AudioInputStream* AudioManagerBase::MakeAudioInputStream( |
135 const AudioParameters& params, const std::string& device_id) { | 147 const AudioParameters& params, const std::string& device_id) { |
136 // TODO(miu): Fix ~20 call points across several unit test modules to call | 148 // TODO(miu): Fix ~20 call points across several unit test modules to call |
137 // this method on the audio thread, then uncomment the following: | 149 // this method on the audio thread, then uncomment the following: |
138 // DCHECK(message_loop_->BelongsToCurrentThread()); | 150 // DCHECK(message_loop_->BelongsToCurrentThread()); |
139 | 151 |
140 if (!params.IsValid() || (params.channels() > kMaxInputChannels) || | 152 if (!params.IsValid() || (params.channels() > kMaxInputChannels) || |
(...skipping 22 matching lines...) Expand all Loading... | |
163 break; | 175 break; |
164 default: | 176 default: |
165 stream = NULL; | 177 stream = NULL; |
166 break; | 178 break; |
167 } | 179 } |
168 | 180 |
169 if (stream) | 181 if (stream) |
170 ++num_input_streams_; | 182 ++num_input_streams_; |
171 | 183 |
172 #if defined(OS_ANDROID) | 184 #if defined(OS_ANDROID) |
173 if (num_input_streams_ == 1) | 185 if (1 == num_input_streams_) |
174 SetAudioMode(kAudioModeInCommunication); | 186 SetAudioMode(kAudioModeInCommunication); |
175 #endif | 187 #endif |
176 | 188 |
177 return stream; | 189 return stream; |
178 } | 190 } |
179 | 191 |
180 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 192 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
181 const AudioParameters& params) { | 193 const AudioParameters& params) { |
182 #if defined(OS_IOS) | 194 #if defined(OS_IOS) |
183 // IOS implements audio input only. | 195 // IOS implements audio input only. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 media::AudioDeviceNames* device_names) { | 261 media::AudioDeviceNames* device_names) { |
250 } | 262 } |
251 | 263 |
252 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) { | 264 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) { |
253 DCHECK(stream); | 265 DCHECK(stream); |
254 // TODO(xians) : Have a clearer destruction path for the AudioOutputStream. | 266 // TODO(xians) : Have a clearer destruction path for the AudioOutputStream. |
255 // For example, pass the ownership to AudioManager so it can delete the | 267 // For example, pass the ownership to AudioManager so it can delete the |
256 // streams. | 268 // streams. |
257 --num_output_streams_; | 269 --num_output_streams_; |
258 delete stream; | 270 delete stream; |
271 #if defined(OS_ANDROID) | |
272 if (!num_output_streams_) | |
273 UnRegisterHeadsetReceiver(); | |
274 #endif | |
259 } | 275 } |
260 | 276 |
261 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | 277 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
262 DCHECK(stream); | 278 DCHECK(stream); |
263 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 279 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
264 --num_input_streams_; | 280 --num_input_streams_; |
265 delete stream; | 281 delete stream; |
266 #if defined(OS_ANDROID) | 282 #if defined(OS_ANDROID) |
267 if (!num_input_streams_) | 283 if (!num_input_streams_) |
268 SetAudioMode(kAudioModeNormal); | 284 SetAudioMode(kAudioModeNormal); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 } | 386 } |
371 | 387 |
372 #if defined(OS_ANDROID) | 388 #if defined(OS_ANDROID) |
373 void AudioManagerBase::SetAudioMode(int mode) { | 389 void AudioManagerBase::SetAudioMode(int mode) { |
374 JNIEnv* env = base::android::AttachCurrentThread(); | 390 JNIEnv* env = base::android::AttachCurrentThread(); |
375 jobject context = base::android::GetApplicationContext(); | 391 jobject context = base::android::GetApplicationContext(); |
376 DCHECK(context); | 392 DCHECK(context); |
377 | 393 |
378 Java_AudioManagerAndroid_setMode(env, context, mode); | 394 Java_AudioManagerAndroid_setMode(env, context, mode); |
379 } | 395 } |
380 #endif | 396 |
397 void AudioManagerBase::RegisterHeadsetReceiver() { | |
398 JNIEnv* env = base::android::AttachCurrentThread(); | |
399 jobject context = base::android::GetApplicationContext(); | |
400 DCHECK(context); | |
401 | |
402 Java_AudioManagerAndroid_registerHeadsetReceiver(env, j_am_.obj(), context); | |
403 } | |
404 | |
405 void AudioManagerBase::UnRegisterHeadsetReceiver() { | |
406 JNIEnv* env = base::android::AttachCurrentThread(); | |
407 jobject context = base::android::GetApplicationContext(); | |
Yaron
2013/03/21 16:51:31
You don't use the context here. Remove it
leozwang1
2013/03/21 18:51:35
Done.
| |
408 DCHECK(context); | |
409 | |
410 Java_AudioManagerAndroid_unregisterHeadsetReceiver(env, j_am_.obj()); | |
411 } | |
412 #endif // defined(OS_ANDROID) | |
381 | 413 |
382 } // namespace media | 414 } // namespace media |
OLD | NEW |