1 /*
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 */
17 
18 #ifndef ANDROID_AUDIO_STREAM_IN_H
19 #define ANDROID_AUDIO_STREAM_IN_H
20 
21 #include <audio_utils/resampler.h>
22 #include <hardware/audio.h>
23 #include <tinyalsa/asoundlib.h>
24 #include <utils/Errors.h>
25 #include <utils/threads.h>
26 
27 #include "AudioHotplugThread.h"
28 
29 namespace android {
30 
31 class AudioHardwareInput;
32 
33 class AudioStreamIn {
34   public:
35     AudioStreamIn(AudioHardwareInput& owner);
36     ~AudioStreamIn();
37 
38     uint32_t          getSampleRate();
39     status_t          setSampleRate(uint32_t rate);
40     size_t            getBufferSize();
41     uint32_t          getChannelMask();
42     audio_format_t    getFormat();
43     status_t          setFormat(audio_format_t format);
44     status_t          standby();
45     status_t          dump(int fd);
46     status_t          setParameters(struct audio_stream* stream,
47                                     const char* kvpairs);
48     char*             getParameters(const char* keys);
49     status_t          setGain(float gain);
50     ssize_t           read(void* buffer, size_t bytes);
51     uint32_t          getInputFramesLost();
52     status_t          addAudioEffect(effect_handle_t effect);
53     status_t          removeAudioEffect(effect_handle_t effect);
54 
55     status_t          set(audio_format_t *pFormat,
56                           uint32_t       *pChannelMask,
57                           uint32_t       *pRate);
58 
getDeviceInfo()59     const AudioHotplugThread::DeviceInfo* getDeviceInfo() { return mCurrentDeviceInfo; };
60 
61   private:
62     static const uint32_t kChannelMask;
63     static const uint32_t kChannelCount;
64     static const audio_format_t kAudioFormat;
65 
getChannelCount()66     static uint32_t   getChannelCount() {
67         return audio_channel_count_from_in_mask(kChannelMask);
68     }
69 
getFrameSize()70     static uint32_t   getFrameSize() {
71         return getChannelCount() * audio_bytes_per_sample(kAudioFormat);
72     }
73 
74     void              setRemoteControlMicEnabled(bool flag);
75 
76     status_t          startInputStream_l();
77     status_t          standby_l();
78 
79     ssize_t           readFrames_l(void* buffer, ssize_t frames);
80 
81     // resampler buffer provider thunks
82     static int        getNextBufferThunk(
83             struct resampler_buffer_provider* bufferProvider,
84             struct resampler_buffer* buffer);
85     static void       releaseBufferThunk(
86             struct resampler_buffer_provider* bufferProvider,
87             struct resampler_buffer* buffer);
88 
89     // resampler buffer provider methods
90     int               getNextBuffer(struct resampler_buffer* buffer);
91     void              releaseBuffer(struct resampler_buffer* buffer);
92 
93     static const int  kPeriodCount;
94 
95     AudioHardwareInput& mOwnerHAL;
96     const AudioHotplugThread::DeviceInfo* mCurrentDeviceInfo;
97 
98     Mutex mLock;
99 
100     uint32_t          mRequestedSampleRate;
101     bool              mStandby;
102     bool              mDisabled;
103 
104     struct pcm*       mPcm;
105     struct pcm_config mPcmConfig;
106 
107     struct resampler_itfe*         mResampler;
108     struct ResamplerBufferProviderWrapper {
109         struct resampler_buffer_provider provider;
110         AudioStreamIn* thiz;
111     } mResamplerProviderWrapper;
112 
113     int16_t*          mBuffer;
114     size_t            mBufferSize;
115     int               mInputSource;
116     int               mReadStatus;
117     unsigned int      mFramesIn;
118     nsecs_t           mLastReadFinishedNs;
119     size_t            mLastBytesRead;
120     nsecs_t           mMinAllowedReadTimeNs;
121 };
122 
123 }; // namespace android
124 
125 #endif  // ANDROID_AUDIO_STREAM_IN_H
126