Chromium Code Reviews| 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/android/audio_manager_android.h" | 5 #include "media/audio/android/audio_manager_android.h" |
| 6 | 6 |
| 7 #include "base/android/build_info.h" | |
| 8 #include "base/android/jni_array.h" | |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "base/android/scoped_java_ref.h" | |
| 7 #include "base/logging.h" | 11 #include "base/logging.h" |
| 8 #include "jni/AudioManagerAndroid_jni.h" | 12 #include "jni/AudioManagerAndroid_jni.h" |
| 9 #include "media/audio/android/opensles_input.h" | 13 #include "media/audio/android/opensles_input.h" |
| 10 #include "media/audio/android/opensles_output.h" | 14 #include "media/audio/android/opensles_output.h" |
| 11 #include "media/audio/audio_manager.h" | 15 #include "media/audio/audio_manager.h" |
| 12 #include "media/audio/audio_parameters.h" | 16 #include "media/audio/audio_parameters.h" |
| 13 #include "media/audio/fake_audio_input_stream.h" | 17 #include "media/audio/fake_audio_input_stream.h" |
| 14 #include "media/base/channel_layout.h" | 18 #include "media/base/channel_layout.h" |
| 15 | 19 |
| 20 using base::android::AppendJavaStringArrayToStringVector; | |
| 21 using base::android::AttachCurrentThread; | |
| 22 using base::android::ConvertJavaStringToUTF8; | |
| 23 using base::android::ScopedJavaLocalRef; | |
| 24 | |
| 16 namespace media { | 25 namespace media { |
| 17 | 26 |
| 18 static void AddDefaultDevice(AudioDeviceNames* device_names) { | 27 static void AddDefaultDevice(AudioDeviceNames* device_names) { |
| 19 DCHECK(device_names->empty()); | |
|
tommi (sloooow) - chröme
2013/11/22 14:59:10
nit: maybe just dcheck instead that the default de
henrika (OOO until Aug 14)
2013/11/25 09:38:31
Fixed based on you comment below ;-)
| |
| 20 device_names->push_front( | 28 device_names->push_front( |
| 21 AudioDeviceName(AudioManagerBase::kDefaultDeviceName, | 29 AudioDeviceName(AudioManagerBase::kDefaultDeviceName, |
| 22 AudioManagerBase::kDefaultDeviceId)); | 30 AudioManagerBase::kDefaultDeviceId)); |
| 23 } | 31 } |
| 24 | 32 |
| 25 // Maximum number of output streams that can be open simultaneously. | 33 // Maximum number of output streams that can be open simultaneously. |
| 26 static const int kMaxOutputStreams = 10; | 34 static const int kMaxOutputStreams = 10; |
| 27 | 35 |
| 28 static const int kAudioModeNormal = 0x00000000; | 36 static const int kAudioModeNormal = 0x00000000; |
| 29 static const int kAudioModeInCommunication = 0x00000003; | 37 static const int kAudioModeInCommunication = 0x00000003; |
| 30 | 38 |
| 31 static const int kDefaultInputBufferSize = 1024; | 39 static const int kDefaultInputBufferSize = 1024; |
| 32 static const int kDefaultOutputBufferSize = 2048; | 40 static const int kDefaultOutputBufferSize = 2048; |
| 33 | 41 |
| 34 AudioManager* CreateAudioManager() { | 42 AudioManager* CreateAudioManager() { |
| 35 return new AudioManagerAndroid(); | 43 return new AudioManagerAndroid(); |
| 36 } | 44 } |
| 37 | 45 |
| 38 AudioManagerAndroid::AudioManagerAndroid() { | 46 AudioManagerAndroid::AudioManagerAndroid() { |
| 39 SetMaxOutputStreamsAllowed(kMaxOutputStreams); | 47 SetMaxOutputStreamsAllowed(kMaxOutputStreams); |
| 40 | 48 |
| 41 j_audio_manager_.Reset( | 49 j_audio_manager_.Reset( |
| 42 Java_AudioManagerAndroid_createAudioManagerAndroid( | 50 Java_AudioManagerAndroid_createAudioManagerAndroid( |
| 43 base::android::AttachCurrentThread(), | 51 base::android::AttachCurrentThread(), |
| 44 base::android::GetApplicationContext())); | 52 base::android::GetApplicationContext())); |
| 53 | |
| 54 Java_AudioManagerAndroid_init( | |
| 55 base::android::AttachCurrentThread(), | |
| 56 j_audio_manager_.obj()); | |
| 45 } | 57 } |
| 46 | 58 |
| 47 AudioManagerAndroid::~AudioManagerAndroid() { | 59 AudioManagerAndroid::~AudioManagerAndroid() { |
| 60 Java_AudioManagerAndroid_close( | |
| 61 base::android::AttachCurrentThread(), | |
| 62 j_audio_manager_.obj()); | |
| 48 Shutdown(); | 63 Shutdown(); |
| 49 } | 64 } |
| 50 | 65 |
| 51 bool AudioManagerAndroid::HasAudioOutputDevices() { | 66 bool AudioManagerAndroid::HasAudioOutputDevices() { |
| 52 return true; | 67 return true; |
| 53 } | 68 } |
| 54 | 69 |
| 55 bool AudioManagerAndroid::HasAudioInputDevices() { | 70 bool AudioManagerAndroid::HasAudioInputDevices() { |
| 56 return true; | 71 return true; |
| 57 } | 72 } |
| 58 | 73 |
| 59 void AudioManagerAndroid::GetAudioInputDeviceNames( | 74 void AudioManagerAndroid::GetAudioInputDeviceNames( |
| 60 AudioDeviceNames* device_names) { | 75 AudioDeviceNames* device_names) { |
| 61 AddDefaultDevice(device_names); | 76 AddDefaultDevice(device_names); |
| 62 } | 77 } |
| 63 | 78 |
| 64 void AudioManagerAndroid::GetAudioOutputDeviceNames( | 79 void AudioManagerAndroid::GetAudioOutputDeviceNames( |
| 65 AudioDeviceNames* device_names) { | 80 AudioDeviceNames* device_names) { |
| 66 AddDefaultDevice(device_names); | 81 JNIEnv* env = AttachCurrentThread(); |
| 82 int sdk = base::android::BuildInfo::GetInstance()->sdk_int(); | |
| 83 if (sdk <= 16) { | |
| 84 // TODO(henrika): figure out where/if we must draw a limit here. | |
| 85 AddDefaultDevice(device_names); | |
| 86 } else { | |
| 87 ScopedJavaLocalRef<jobjectArray> j_device_array = | |
| 88 Java_AudioManagerAndroid_getAudioOutputDeviceNames( | |
| 89 env, j_audio_manager_.obj()); | |
|
tommi (sloooow) - chröme
2013/11/22 14:59:10
nit: indent
henrika (OOO until Aug 14)
2013/11/25 09:38:31
Done.
| |
| 90 std::vector<std::string> devices; | |
| 91 AppendJavaStringArrayToStringVector(env, j_device_array.obj(), &devices); | |
| 92 | |
| 93 std::vector<std::string>::iterator it = devices.begin(); | |
| 94 while (it != devices.end()) { | |
| 95 device_names->push_back(AudioDeviceName(*it, *it)); | |
| 96 DVLOG(1) << "ouput device name: " << *it; | |
| 97 ++it; | |
| 98 } | |
| 99 | |
| 100 // Always add default device parameters as first element. | |
| 101 // TODO(henrika): figure out how we shall handle the term "default" | |
| 102 // on Android. | |
| 103 AddDefaultDevice(device_names); | |
|
tommi (sloooow) - chröme
2013/11/22 14:59:10
couldn't you call this method before enumerating t
henrika (OOO until Aug 14)
2013/11/25 09:38:31
Thanks ;-)
| |
| 104 } | |
| 67 } | 105 } |
| 68 | 106 |
| 69 AudioParameters AudioManagerAndroid::GetInputStreamParameters( | 107 AudioParameters AudioManagerAndroid::GetInputStreamParameters( |
| 70 const std::string& device_id) { | 108 const std::string& device_id) { |
| 71 // Use mono as preferred number of input channels on Android to save | 109 // Use mono as preferred number of input channels on Android to save |
| 72 // resources. Using mono also avoids a driver issue seen on Samsung | 110 // resources. Using mono also avoids a driver issue seen on Samsung |
| 73 // Galaxy S3 and S4 devices. See http://crbug.com/256851 for details. | 111 // Galaxy S3 and S4 devices. See http://crbug.com/256851 for details. |
| 74 ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO; | 112 ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO; |
| 75 int buffer_size = Java_AudioManagerAndroid_getMinInputFrameSize( | 113 int buffer_size = Java_AudioManagerAndroid_getMinInputFrameSize( |
| 76 base::android::AttachCurrentThread(), GetNativeOutputSampleRate(), | 114 base::android::AttachCurrentThread(), GetNativeOutputSampleRate(), |
| 77 ChannelLayoutToChannelCount(channel_layout)); | 115 ChannelLayoutToChannelCount(channel_layout)); |
| 78 | 116 |
| 79 return AudioParameters( | 117 return AudioParameters( |
| 80 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, | 118 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
| 81 GetNativeOutputSampleRate(), 16, | 119 GetNativeOutputSampleRate(), 16, |
| 82 buffer_size <= 0 ? kDefaultInputBufferSize : buffer_size); | 120 buffer_size <= 0 ? kDefaultInputBufferSize : buffer_size); |
| 83 } | 121 } |
| 84 | 122 |
| 85 AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream( | 123 AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream( |
| 86 const AudioParameters& params, | 124 const AudioParameters& params, |
| 87 const std::string& device_id, | 125 const std::string& device_id, |
| 88 const std::string& input_device_id) { | 126 const std::string& input_device_id) { |
| 89 AudioOutputStream* stream = | 127 AudioOutputStream* stream = |
| 90 AudioManagerBase::MakeAudioOutputStream(params, std::string(), | 128 AudioManagerBase::MakeAudioOutputStream(params, std::string(), |
| 91 std::string()); | 129 std::string()); |
| 92 if (stream && output_stream_count() == 1) { | 130 if (stream && output_stream_count() == 1) { |
| 93 SetAudioMode(kAudioModeInCommunication); | 131 SetAudioMode(kAudioModeInCommunication); |
| 94 RegisterHeadsetReceiver(); | 132 // TODO(henrika): resolve conflict with device enumeration. |
| 133 // RegisterHeadsetReceiver(); | |
| 95 } | 134 } |
| 96 return stream; | 135 return stream; |
| 97 } | 136 } |
| 98 | 137 |
| 99 AudioInputStream* AudioManagerAndroid::MakeAudioInputStream( | 138 AudioInputStream* AudioManagerAndroid::MakeAudioInputStream( |
| 100 const AudioParameters& params, const std::string& device_id) { | 139 const AudioParameters& params, const std::string& device_id) { |
| 101 AudioInputStream* stream = | 140 AudioInputStream* stream = |
| 102 AudioManagerBase::MakeAudioInputStream(params, device_id); | 141 AudioManagerBase::MakeAudioInputStream(params, device_id); |
| 103 return stream; | 142 return stream; |
| 104 } | 143 } |
| 105 | 144 |
| 106 void AudioManagerAndroid::ReleaseOutputStream(AudioOutputStream* stream) { | 145 void AudioManagerAndroid::ReleaseOutputStream(AudioOutputStream* stream) { |
| 107 AudioManagerBase::ReleaseOutputStream(stream); | 146 AudioManagerBase::ReleaseOutputStream(stream); |
| 108 if (!output_stream_count()) { | 147 if (!output_stream_count()) { |
| 109 UnregisterHeadsetReceiver(); | 148 // TODO(henrika): resolve conflict with device enumeration. |
| 149 // UnregisterHeadsetReceiver(); | |
| 110 SetAudioMode(kAudioModeNormal); | 150 SetAudioMode(kAudioModeNormal); |
| 111 } | 151 } |
| 112 } | 152 } |
| 113 | 153 |
| 114 void AudioManagerAndroid::ReleaseInputStream(AudioInputStream* stream) { | 154 void AudioManagerAndroid::ReleaseInputStream(AudioInputStream* stream) { |
| 115 AudioManagerBase::ReleaseInputStream(stream); | 155 AudioManagerBase::ReleaseInputStream(stream); |
| 116 } | 156 } |
| 117 | 157 |
| 118 AudioOutputStream* AudioManagerAndroid::MakeLinearOutputStream( | 158 AudioOutputStream* AudioManagerAndroid::MakeLinearOutputStream( |
| 119 const AudioParameters& params) { | 159 const AudioParameters& params) { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 j_audio_manager_.obj()); | 258 j_audio_manager_.obj()); |
| 219 } | 259 } |
| 220 | 260 |
| 221 int AudioManagerAndroid::GetAudioLowLatencyOutputFrameSize() { | 261 int AudioManagerAndroid::GetAudioLowLatencyOutputFrameSize() { |
| 222 return Java_AudioManagerAndroid_getAudioLowLatencyOutputFrameSize( | 262 return Java_AudioManagerAndroid_getAudioLowLatencyOutputFrameSize( |
| 223 base::android::AttachCurrentThread(), | 263 base::android::AttachCurrentThread(), |
| 224 j_audio_manager_.obj()); | 264 j_audio_manager_.obj()); |
| 225 } | 265 } |
| 226 | 266 |
| 227 } // namespace media | 267 } // namespace media |
| OLD | NEW |