1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/ios/audio_manager_ios.h"
7 #import <AudioToolbox/AudioToolbox.h>
8 #import <AVFoundation/AVFoundation.h>
10 #include "base/sys_info.h"
11 #include "media/audio/audio_parameters.h"
12 #include "media/audio/fake_audio_input_stream.h"
13 #include "media/audio/ios/audio_session_util_ios.h"
14 #include "media/audio/mac/audio_input_mac.h"
15 #include "media/base/channel_layout.h"
16 #include "media/base/limits.h"
20 enum { kMaxInputChannels = 2 };
22 AudioManagerIOS::AudioManagerIOS() {
25 AudioManagerIOS::~AudioManagerIOS() {
29 bool AudioManagerIOS::HasAudioOutputDevices() {
33 bool AudioManagerIOS::HasAudioInputDevices() {
34 if (!InitAudioSessionIOS())
36 // Note that the |kAudioSessionProperty_AudioInputAvailable| property is a
37 // 32-bit integer, not a boolean.
40 AudioSessionGetPropertySize(kAudioSessionProperty_AudioInputAvailable,
42 if (error != kAudioSessionNoError)
44 UInt32 audio_input_is_available = false;
45 DCHECK(property_size == sizeof(audio_input_is_available));
46 error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable,
48 &audio_input_is_available);
49 return error == kAudioSessionNoError ? audio_input_is_available : false;
52 AudioParameters AudioManagerIOS::GetInputStreamParameters(
53 const std::string& device_id) {
54 // TODO(xians): figure out the right input sample rate and buffer size to
55 // achieve the best audio performance for iOS devices.
56 // TODO(xians): query the native channel layout for the specific device.
57 static const int kDefaultSampleRate = 48000;
58 static const int kDefaultBufferSize = 2048;
59 return AudioParameters(
60 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
61 kDefaultSampleRate, 16, kDefaultBufferSize);
64 AudioOutputStream* AudioManagerIOS::MakeAudioOutputStream(
65 const AudioParameters& params, const std::string& input_device_id) {
66 NOTIMPLEMENTED(); // Only input is supported on iOS.
70 AudioInputStream* AudioManagerIOS::MakeAudioInputStream(
71 const AudioParameters& params, const std::string& device_id) {
72 // Current line of iOS devices has only one audio input.
73 // Ignore the device_id (unittest uses a test value in it).
74 if (!params.IsValid() || (params.channels() > kMaxInputChannels))
77 if (params.format() == AudioParameters::AUDIO_FAKE)
78 return FakeAudioInputStream::MakeFakeStream(this, params);
79 else if (params.format() == AudioParameters::AUDIO_PCM_LINEAR)
80 return new PCMQueueInAudioInputStream(this, params);
84 AudioOutputStream* AudioManagerIOS::MakeLinearOutputStream(
85 const AudioParameters& params) {
86 NOTIMPLEMENTED(); // Only input is supported on iOS.
90 AudioOutputStream* AudioManagerIOS::MakeLowLatencyOutputStream(
91 const AudioParameters& params, const std::string& input_device_id) {
92 NOTIMPLEMENTED(); // Only input is supported on iOS.
96 AudioInputStream* AudioManagerIOS::MakeLinearInputStream(
97 const AudioParameters& params, const std::string& device_id) {
98 return MakeAudioInputStream(params, device_id);
101 AudioInputStream* AudioManagerIOS::MakeLowLatencyInputStream(
102 const AudioParameters& params, const std::string& device_id) {
103 NOTIMPLEMENTED(); // Only linear audio input is supported on iOS.
104 return MakeAudioInputStream(params, device_id);
108 AudioParameters AudioManagerIOS::GetPreferredOutputStreamParameters(
109 const AudioParameters& input_params) {
110 // TODO(xians): handle the case when input_params is valid.
111 // TODO(xians): figure out the right output sample rate and sample rate to
112 // achieve the best audio performance for iOS devices.
113 // TODO(xians): add support to --audio-buffer-size flag.
114 static const int kDefaultSampleRate = 48000;
115 static const int kDefaultBufferSize = 2048;
116 if (input_params.IsValid()) {
120 return AudioParameters(
121 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO,
122 kDefaultSampleRate, 16, kDefaultBufferSize);
125 // Called by the stream when it has been released by calling Close().
126 void AudioManagerIOS::ReleaseOutputStream(AudioOutputStream* stream) {
127 NOTIMPLEMENTED(); // Only input is supported on iOS.
130 // Called by the stream when it has been released by calling Close().
131 void AudioManagerIOS::ReleaseInputStream(AudioInputStream* stream) {
136 AudioManager* CreateAudioManager() {
137 return new AudioManagerIOS();