1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef A2DP_AUDIO_HARDWARE_H
18 #define A2DP_AUDIO_HARDWARE_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <utils/threads.h>
24 
25 #include <hardware_legacy/AudioHardwareBase.h>
26 
27 
28 namespace android_audio_legacy {
29     using android::Mutex;
30 
31 class A2dpAudioInterface : public AudioHardwareBase
32 {
33     class A2dpAudioStreamOut;
34 
35 public:
36                         A2dpAudioInterface(AudioHardwareInterface* hw);
37     virtual             ~A2dpAudioInterface();
38     virtual status_t    initCheck();
39 
40     virtual status_t    setVoiceVolume(float volume);
41     virtual status_t    setMasterVolume(float volume);
42 
43     virtual status_t    setMode(int mode);
44 
45     // mic mute
46     virtual status_t    setMicMute(bool state);
47     virtual status_t    getMicMute(bool* state);
48 
49     virtual status_t    setParameters(const String8& keyValuePairs);
50     virtual String8     getParameters(const String8& keys);
51 
52     virtual size_t      getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
53 
54     // create I/O streams
55     virtual AudioStreamOut* openOutputStream(
56                                 uint32_t devices,
57                                 int *format=0,
58                                 uint32_t *channels=0,
59                                 uint32_t *sampleRate=0,
60                                 status_t *status=0);
61     virtual    void        closeOutputStream(AudioStreamOut* out);
62 
63     virtual AudioStreamIn* openInputStream(
64                                 uint32_t devices,
65                                 int *format,
66                                 uint32_t *channels,
67                                 uint32_t *sampleRate,
68                                 status_t *status,
69                                 AudioSystem::audio_in_acoustics acoustics);
70     virtual    void        closeInputStream(AudioStreamIn* in);
71 //    static AudioHardwareInterface* createA2dpInterface();
72 
73 protected:
74     virtual status_t    dump(int fd, const Vector<String16>& args);
75 
76 private:
77     class A2dpAudioStreamOut : public AudioStreamOut {
78     public:
79                             A2dpAudioStreamOut();
80         virtual             ~A2dpAudioStreamOut();
81                 status_t    set(uint32_t device,
82                                 int *pFormat,
83                                 uint32_t *pChannels,
84                                 uint32_t *pRate);
sampleRate()85         virtual uint32_t    sampleRate() const { return 44100; }
86         // SBC codec wants a multiple of 512
bufferSize()87         virtual size_t      bufferSize() const { return 512 * 20; }
channels()88         virtual uint32_t    channels() const { return AudioSystem::CHANNEL_OUT_STEREO; }
format()89         virtual int         format() const { return AudioSystem::PCM_16_BIT; }
latency()90         virtual uint32_t    latency() const { return ((1000*bufferSize())/frameSize())/sampleRate() + 200; }
setVolume(float left,float right)91         virtual status_t    setVolume(float left, float right) { return INVALID_OPERATION; }
92         virtual ssize_t     write(const void* buffer, size_t bytes);
93                 status_t    standby();
94         virtual status_t    dump(int fd, const Vector<String16>& args);
95         virtual status_t    setParameters(const String8& keyValuePairs);
96         virtual String8     getParameters(const String8& keys);
97         virtual status_t    getRenderPosition(uint32_t *dspFrames);
98 
99     private:
100         friend class A2dpAudioInterface;
101                 status_t    init();
102                 status_t    close();
103                 status_t    close_l();
104                 status_t    setAddress(const char* address);
105                 status_t    setBluetoothEnabled(bool enabled);
106                 status_t    setSuspended(bool onOff);
107                 status_t    standby_l();
108 
109     private:
110                 int         mFd;
111                 bool        mStandby;
112                 int         mStartCount;
113                 int         mRetryCount;
114                 char        mA2dpAddress[20];
115                 void*       mData;
116                 Mutex       mLock;
117                 bool        mBluetoothEnabled;
118                 uint32_t    mDevice;
119                 bool        mClosing;
120                 bool        mSuspended;
121                 nsecs_t     mLastWriteTime;
122                 uint32_t    mBufferDurationUs;
123     };
124 
125     friend class A2dpAudioStreamOut;
126 
127     A2dpAudioStreamOut*     mOutput;
128     AudioHardwareInterface  *mHardwareInterface;
129     char        mA2dpAddress[20];
130     bool        mBluetoothEnabled;
131     bool        mSuspended;
132 };
133 
134 
135 // ----------------------------------------------------------------------------
136 
137 }; // namespace android
138 
139 #endif // A2DP_AUDIO_HARDWARE_H
140