1 /*
2 **
3 ** Copyright 2014, 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 */
17 
18 #ifndef ANDROID_AUDIO_HARDWARE_INPUT_H
19 #define ANDROID_AUDIO_HARDWARE_INPUT_H
20 
21 #include <hardware/audio.h>
22 #include <system/audio.h>
23 #include <utils/Errors.h>
24 #include <utils/threads.h>
25 #include <utils/Vector.h>
26 
27 #include "AudioHotplugThread.h"
28 
29 namespace android {
30 
31 class AudioStreamIn;
32 
33 class AudioHardwareInput : public AudioHotplugThread::Callback {
34   public:
35     AudioHardwareInput();
36     ~AudioHardwareInput();
37 
38     status_t    setMicMute(bool mute);
39     status_t    getMicMute(bool* mute);
40     status_t    getInputBufferSize(const audio_config* config);
41 
42     AudioStreamIn* openInputStream(uint32_t devices,
43                                    audio_format_t* format,
44                                    uint32_t* channelMask,
45                                    uint32_t* sampleRate,
46                                    status_t* status);
47     void           closeInputStream(AudioStreamIn* in);
48 
49     status_t       dump(int fd);
50 
51     // AudioHotplugThread callbacks
52     virtual void onDeviceFound(const AudioHotplugThread::DeviceInfo& devInfo);
53     virtual void onDeviceRemoved(unsigned int pcmCard, unsigned int pcmDevice);
54 
55     static size_t calculateInputBufferSize(uint32_t outputSampleRate,
56                                            audio_format_t format,
57                                            uint32_t channelCount);
58 
59     static const uint32_t kPeriodMsec;
60 
61     /**
62      * Decide which device to use for the given input.
63      */
64     const AudioHotplugThread::DeviceInfo* getBestDevice(int inputSource);
65 
66   private:
67     static const int    kMaxDevices = 8; // TODO review strategy for adding more devices
68     void                closeAllInputStreams();
69     // Place all input streams using the specified device into standby. If deviceInfo is NULL,
70     // all input streams are placed into standby.
71     void                standbyAllInputStreams(const AudioHotplugThread::DeviceInfo* deviceInfo);
72     Mutex               mLock;
73     bool                mMicMute;
74     Vector<AudioStreamIn*> mInputStreams;
75 
76     sp<AudioHotplugThread> mHotplugThread;
77 
78     AudioHotplugThread::DeviceInfo mDeviceInfos[kMaxDevices];
79 };
80 
81 }; // namespace android
82 
83 #endif  // ANDROID_AUDIO_HARDWARE_INPUT_H
84