1/*
2**
3** Copyright 2007, 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_GENERIC_H
19#define ANDROID_AUDIO_HARDWARE_GENERIC_H
20
21#include <stdint.h>
22#include <sys/types.h>
23
24#include <utils/threads.h>
25
26#include <hardware_legacy/AudioHardwareBase.h>
27
28namespace android_audio_legacy {
29    using android::Mutex;
30    using android::AutoMutex;
31
32// ----------------------------------------------------------------------------
33
34class AudioHardwareGeneric;
35
36class AudioStreamOutGeneric : public AudioStreamOut {
37public:
38                        AudioStreamOutGeneric() : mAudioHardware(0), mFd(-1) {}
39    virtual             ~AudioStreamOutGeneric();
40
41    virtual status_t    set(
42            AudioHardwareGeneric *hw,
43            int mFd,
44            uint32_t devices,
45            int *pFormat,
46            uint32_t *pChannels,
47            uint32_t *pRate);
48
49    virtual uint32_t    sampleRate() const { return 44100; }
50    virtual size_t      bufferSize() const { return 4096; }
51    virtual uint32_t    channels() const { return AudioSystem::CHANNEL_OUT_STEREO; }
52    virtual int         format() const { return AudioSystem::PCM_16_BIT; }
53    virtual uint32_t    latency() const { return 20; }
54    virtual status_t    setVolume(float left, float right) { return INVALID_OPERATION; }
55    virtual ssize_t     write(const void* buffer, size_t bytes);
56    virtual status_t    standby();
57    virtual status_t    dump(int fd, const Vector<String16>& args);
58    virtual status_t    setParameters(const String8& keyValuePairs);
59    virtual String8     getParameters(const String8& keys);
60    virtual status_t    getRenderPosition(uint32_t *dspFrames);
61
62private:
63    AudioHardwareGeneric *mAudioHardware;
64    Mutex   mLock;
65    int     mFd;
66    uint32_t mDevice;
67};
68
69class AudioStreamInGeneric : public AudioStreamIn {
70public:
71                        AudioStreamInGeneric() : mAudioHardware(0), mFd(-1) {}
72    virtual             ~AudioStreamInGeneric();
73
74    virtual status_t    set(
75            AudioHardwareGeneric *hw,
76            int mFd,
77            uint32_t devices,
78            int *pFormat,
79            uint32_t *pChannels,
80            uint32_t *pRate,
81            AudioSystem::audio_in_acoustics acoustics);
82
83    virtual uint32_t    sampleRate() const { return 8000; }
84    virtual size_t      bufferSize() const { return 320; }
85    virtual uint32_t    channels() const { return AudioSystem::CHANNEL_IN_MONO; }
86    virtual int         format() const { return AudioSystem::PCM_16_BIT; }
87    virtual status_t    setGain(float gain) { return INVALID_OPERATION; }
88    virtual ssize_t     read(void* buffer, ssize_t bytes);
89    virtual status_t    dump(int fd, const Vector<String16>& args);
90    virtual status_t    standby() { return NO_ERROR; }
91    virtual status_t    setParameters(const String8& keyValuePairs);
92    virtual String8     getParameters(const String8& keys);
93    virtual unsigned int  getInputFramesLost() const { return 0; }
94    virtual status_t    addAudioEffect(effect_handle_t effect) { return NO_ERROR; }
95    virtual status_t    removeAudioEffect(effect_handle_t effect) { return NO_ERROR; }
96
97private:
98    AudioHardwareGeneric *mAudioHardware;
99    Mutex   mLock;
100    int     mFd;
101    uint32_t mDevice;
102};
103
104
105class AudioHardwareGeneric : public AudioHardwareBase
106{
107public:
108                        AudioHardwareGeneric();
109    virtual             ~AudioHardwareGeneric();
110    virtual status_t    initCheck();
111    virtual status_t    setVoiceVolume(float volume);
112    virtual status_t    setMasterVolume(float volume);
113
114    // mic mute
115    virtual status_t    setMicMute(bool state);
116    virtual status_t    getMicMute(bool* state);
117
118    // create I/O streams
119    virtual AudioStreamOut* openOutputStream(
120            uint32_t devices,
121            int *format=0,
122            uint32_t *channels=0,
123            uint32_t *sampleRate=0,
124            status_t *status=0);
125    virtual    void        closeOutputStream(AudioStreamOut* out);
126
127    virtual AudioStreamIn* openInputStream(
128            uint32_t devices,
129            int *format,
130            uint32_t *channels,
131            uint32_t *sampleRate,
132            status_t *status,
133            AudioSystem::audio_in_acoustics acoustics);
134    virtual    void        closeInputStream(AudioStreamIn* in);
135
136            void            closeOutputStream(AudioStreamOutGeneric* out);
137            void            closeInputStream(AudioStreamInGeneric* in);
138protected:
139    virtual status_t        dump(int fd, const Vector<String16>& args);
140
141private:
142    status_t                dumpInternals(int fd, const Vector<String16>& args);
143
144    Mutex                   mLock;
145    AudioStreamOutGeneric   *mOutput;
146    AudioStreamInGeneric    *mInput;
147    int                     mFd;
148    bool                    mMicMute;
149};
150
151// ----------------------------------------------------------------------------
152
153}; // namespace android
154
155#endif // ANDROID_AUDIO_HARDWARE_GENERIC_H
156