blob: 686cf7d70a781ddd5ef2ca4dceef25bcaa825990 [file] [log] [blame]
Lei Qian7bf98232018-09-20 17:56:38 +08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16** @author Hugo Hong
17** @version 1.0
18** @date 2018/04/01
19** @par function description:
20** - 1 bluetooth rc audio stream in base class
21*/
22
23#define LOG_TAG "AudioHAL:AudioStreamIn"
24#include <utils/Log.h>
25
26#include "AudioStreamIn.h"
27#include "AudioHardwareInput.h"
28
29#include <assert.h>
30#include <stdio.h>
31#include <string.h>
32#include <unistd.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <netinet/in.h>
37#include <sys/select.h>
38#include <sys/socket.h>
39#include <sys/un.h>
40
41#include <utils/String8.h>
42#include <media/AudioParameter.h>
43
44
45namespace android {
46
47const audio_format_t AudioStreamIn::kAudioFormat = AUDIO_FORMAT_PCM_16_BIT;
48const uint32_t AudioStreamIn::kChannelMask = AUDIO_CHANNEL_IN_MONO;
49bool AudioStreamIn::mStandby = true;
50
51AudioStreamIn::AudioStreamIn(AudioHardwareInput& owner)
52 : mOwnerHAL(owner)
53 , mCurrentDeviceInfo(NULL)
54 , mRequestedSampleRate(0)
55 , mDisabled(false)
56 , mInputSource(AUDIO_SOURCE_VOICE_RECOGNITION)
57 , mReadStatus(0)
58{
59 mCurrentDeviceInfo = mOwnerHAL.getBestDevice(mInputSource);
60}
61
62AudioStreamIn::~AudioStreamIn()
63{
64}
65
66// Perform stream initialization that may fail.
67// Must only be called once at construction time.
68status_t AudioStreamIn::set(struct audio_stream_in *stream,
69 audio_format_t *pFormat,
70 uint32_t *pChannelMask,
71 uint32_t *pRate)
72{
73 Mutex::Autolock _l(mLock);
74
75 (void) stream;
76
77 // Respond with a request for mono if a different format is given.
78 if (*pChannelMask != kChannelMask) {
79 *pChannelMask = kChannelMask;
80 return BAD_VALUE;
81 }
82
83 if (*pFormat != kAudioFormat) {
84 *pFormat = kAudioFormat;
85 return BAD_VALUE;
86 }
87
88 mRequestedSampleRate = *pRate;
89
90 return NO_ERROR;
91}
92
93
94uint32_t AudioStreamIn::getSampleRate()
95{
96 Mutex::Autolock _l(mLock);
97 return mRequestedSampleRate;
98}
99
100status_t AudioStreamIn::setSampleRate(uint32_t rate)
101{
102 (void) rate;
103 // this is a no-op in other audio HALs
104 return NO_ERROR;
105}
106
107size_t AudioStreamIn::getBufferSize()
108{
109 Mutex::Autolock _l(mLock);
110
111 size_t size = AudioHardwareInput::calculateInputBufferSize(
112 mRequestedSampleRate, kAudioFormat, getChannelCount());
113 return size;
114}
115
116uint32_t AudioStreamIn::getChannelMask()
117{
118 return kChannelMask;
119}
120
121audio_format_t AudioStreamIn::getFormat()
122{
123 return kAudioFormat;
124}
125
126status_t AudioStreamIn::setFormat(audio_format_t format)
127{
128 (void) format;
129 // other audio HALs fail any call to this API (even if the format matches
130 // the current format)
131 return INVALID_OPERATION;
132}
133
134status_t AudioStreamIn::standby()
135{
136 Mutex::Autolock _l(mLock);
137 return standby_l();
138}
139
140status_t AudioStreamIn::dump(int fd)
141{
142 (void) fd;
143 return NO_ERROR;
144}
145
146status_t AudioStreamIn::setParameters(struct audio_stream* stream,
147 const char* kvpairs)
148{
149 (void) stream;
150 (void) kvpairs;
151
152 return NO_ERROR;
153}
154
155char* AudioStreamIn::getParameters(const char* keys)
156{
157 (void) keys;
158 return strdup("");
159}
160
161status_t AudioStreamIn::setGain(float gain)
162{
163 (void) gain;
164 // In other HALs, this is a no-op and returns success.
165 return NO_ERROR;
166}
167
168uint32_t AudioStreamIn::getInputFramesLost()
169{
170 return 0;
171}
172
173status_t AudioStreamIn::addAudioEffect(effect_handle_t effect)
174{
175 (void) effect;
176 // In other HALs, this is a no-op and returns success.
177 return 0;
178}
179
180status_t AudioStreamIn::removeAudioEffect(effect_handle_t effect)
181{
182 (void) effect;
183 // In other HALs, this is a no-op and returns success.
184 return 0;
185}
186
187ssize_t AudioStreamIn::read(void* buffer, size_t bytes)
188{
189 Mutex::Autolock _l(mLock);
190
191 status_t status = NO_ERROR;
192
193 (void) buffer;
194
195 if (mStandby) {
196 status = startInputStream_l();
197 // Only try to start once to prevent pointless spew.
198 // If mic is not available then read will return silence.
199 // This is needed to prevent apps from hanging.
200 mStandby = false;
201 if (status != NO_ERROR) {
202 mDisabled = true;
203 }
204 }
205
206 return bytes;
207}
208
209status_t AudioStreamIn::startInputStream_l()
210{
211 // Get the most appropriate device for the given input source, eg VOICE_RECOGNITION
212 const AudioHotplugThread::DeviceInfo *deviceInfo = mOwnerHAL.getBestDevice(mInputSource);
213 if (deviceInfo == NULL) {
214 return INVALID_OPERATION;
215 }
216
217 ALOGD("AudioStreamIn::startInputStream_l, mRequestedSampleRate = %d", mRequestedSampleRate);
218
219 // Turn on RemoteControl MIC if we are recording from it.
220 if (deviceInfo->forVoiceRecognition) {
221 mOwnerHAL.setRemoteControlMicEnabled(true);
222 }
223
224 mCurrentDeviceInfo = deviceInfo;
225
226 return NO_ERROR;
227}
228
229status_t AudioStreamIn::standby_l()
230{
231 if (mStandby) {
232 return NO_ERROR;
233 }
234
235 // Turn OFF Remote MIC if we were recording from Remote.
236 if (mCurrentDeviceInfo != NULL) {
237 if (mCurrentDeviceInfo->forVoiceRecognition) {
238 mOwnerHAL.setRemoteControlMicEnabled(false);
239 }
240 }
241
242 mCurrentDeviceInfo = NULL;
243 mStandby = true;
244 mDisabled = false;
245
246 return NO_ERROR;
247}
248
249}; // namespace android