1 /*
2  * Copyright (C) 2007 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 ANDROID_IAUDIOFLINGER_H
18 #define ANDROID_IAUDIOFLINGER_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include <utils/RefBase.h>
25 #include <utils/Errors.h>
26 #include <binder/IInterface.h>
27 #include <media/IAudioTrack.h>
28 #include <media/IAudioRecord.h>
29 #include <media/IAudioFlingerClient.h>
30 #include <system/audio.h>
31 #include <system/audio_policy.h>
32 #include <hardware/audio_policy.h>
33 #include <hardware/audio_effect.h>
34 #include <media/IEffect.h>
35 #include <media/IEffectClient.h>
36 #include <utils/String8.h>
37 
38 namespace android {
39 
40 // ----------------------------------------------------------------------------
41 
42 class IAudioFlinger : public IInterface
43 {
44 public:
45     DECLARE_META_INTERFACE(AudioFlinger);
46 
47     // or-able bits shared by createTrack and openRecord, but not all combinations make sense
48     enum {
49         TRACK_DEFAULT = 0,  // client requests a default AudioTrack
50         // FIXME: obsolete
51         // TRACK_TIMED= 1,  // client requests a TimedAudioTrack
52         TRACK_FAST    = 2,  // client requests a fast AudioTrack or AudioRecord
53         TRACK_OFFLOAD = 4,  // client requests offload to hw codec
54         TRACK_DIRECT = 8,   // client requests a direct output
55     };
56     typedef uint32_t track_flags_t;
57 
58     // invariant on exit for all APIs that return an sp<>:
59     //   (return value != 0) == (*status == NO_ERROR)
60 
61     /* create an audio track and registers it with AudioFlinger.
62      * return null if the track cannot be created.
63      */
64     virtual sp<IAudioTrack> createTrack(
65                                 audio_stream_type_t streamType,
66                                 uint32_t sampleRate,
67                                 audio_format_t format,
68                                 audio_channel_mask_t channelMask,
69                                 size_t *pFrameCount,
70                                 track_flags_t *flags,
71                                 const sp<IMemory>& sharedBuffer,
72                                 // On successful return, AudioFlinger takes over the handle
73                                 // reference and will release it when the track is destroyed.
74                                 // However on failure, the client is responsible for release.
75                                 audio_io_handle_t output,
76                                 pid_t pid,
77                                 pid_t tid,  // -1 means unused, otherwise must be valid non-0
78                                 audio_session_t *sessionId,
79                                 int clientUid,
80                                 status_t *status) = 0;
81 
82     virtual sp<IAudioRecord> openRecord(
83                                 // On successful return, AudioFlinger takes over the handle
84                                 // reference and will release it when the track is destroyed.
85                                 // However on failure, the client is responsible for release.
86                                 audio_io_handle_t input,
87                                 uint32_t sampleRate,
88                                 audio_format_t format,
89                                 audio_channel_mask_t channelMask,
90                                 const String16& callingPackage,
91                                 size_t *pFrameCount,
92                                 track_flags_t *flags,
93                                 pid_t pid,
94                                 pid_t tid,  // -1 means unused, otherwise must be valid non-0
95                                 int clientUid,
96                                 audio_session_t *sessionId,
97                                 size_t *notificationFrames,
98                                 sp<IMemory>& cblk,
99                                 sp<IMemory>& buffers,   // return value 0 means it follows cblk
100                                 status_t *status) = 0;
101 
102     // FIXME Surprisingly, format/latency don't work for input handles
103 
104     /* query the audio hardware state. This state never changes,
105      * and therefore can be cached.
106      */
107     virtual     uint32_t    sampleRate(audio_io_handle_t ioHandle) const = 0;
108 
109     // reserved; formerly channelCount()
110 
111     virtual     audio_format_t format(audio_io_handle_t output) const = 0;
112     virtual     size_t      frameCount(audio_io_handle_t ioHandle) const = 0;
113 
114     // return estimated latency in milliseconds
115     virtual     uint32_t    latency(audio_io_handle_t output) const = 0;
116 
117     /* set/get the audio hardware state. This will probably be used by
118      * the preference panel, mostly.
119      */
120     virtual     status_t    setMasterVolume(float value) = 0;
121     virtual     status_t    setMasterMute(bool muted) = 0;
122 
123     virtual     float       masterVolume() const = 0;
124     virtual     bool        masterMute() const = 0;
125 
126     /* set/get stream type state. This will probably be used by
127      * the preference panel, mostly.
128      */
129     virtual     status_t    setStreamVolume(audio_stream_type_t stream, float value,
130                                     audio_io_handle_t output) = 0;
131     virtual     status_t    setStreamMute(audio_stream_type_t stream, bool muted) = 0;
132 
133     virtual     float       streamVolume(audio_stream_type_t stream,
134                                     audio_io_handle_t output) const = 0;
135     virtual     bool        streamMute(audio_stream_type_t stream) const = 0;
136 
137     // set audio mode
138     virtual     status_t    setMode(audio_mode_t mode) = 0;
139 
140     // mic mute/state
141     virtual     status_t    setMicMute(bool state) = 0;
142     virtual     bool        getMicMute() const = 0;
143 
144     virtual     status_t    setParameters(audio_io_handle_t ioHandle,
145                                     const String8& keyValuePairs) = 0;
146     virtual     String8     getParameters(audio_io_handle_t ioHandle, const String8& keys)
147                                     const = 0;
148 
149     // Register an object to receive audio input/output change and track notifications.
150     // For a given calling pid, AudioFlinger disregards any registrations after the first.
151     // Thus the IAudioFlingerClient must be a singleton per process.
152     virtual void registerClient(const sp<IAudioFlingerClient>& client) = 0;
153 
154     // retrieve the audio recording buffer size
155     // FIXME This API assumes a route, and so should be deprecated.
156     virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
157             audio_channel_mask_t channelMask) const = 0;
158 
159     virtual status_t openOutput(audio_module_handle_t module,
160                                 audio_io_handle_t *output,
161                                 audio_config_t *config,
162                                 audio_devices_t *devices,
163                                 const String8& address,
164                                 uint32_t *latencyMs,
165                                 audio_output_flags_t flags) = 0;
166     virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
167                                     audio_io_handle_t output2) = 0;
168     virtual status_t closeOutput(audio_io_handle_t output) = 0;
169     virtual status_t suspendOutput(audio_io_handle_t output) = 0;
170     virtual status_t restoreOutput(audio_io_handle_t output) = 0;
171 
172     virtual status_t openInput(audio_module_handle_t module,
173                                audio_io_handle_t *input,
174                                audio_config_t *config,
175                                audio_devices_t *device,
176                                const String8& address,
177                                audio_source_t source,
178                                audio_input_flags_t flags) = 0;
179     virtual status_t closeInput(audio_io_handle_t input) = 0;
180 
181     virtual status_t invalidateStream(audio_stream_type_t stream) = 0;
182 
183     virtual status_t setVoiceVolume(float volume) = 0;
184 
185     virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
186                                     audio_io_handle_t output) const = 0;
187 
188     virtual uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const = 0;
189 
190     virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use) = 0;
191 
192     virtual void acquireAudioSessionId(audio_session_t audioSession, pid_t pid) = 0;
193     virtual void releaseAudioSessionId(audio_session_t audioSession, pid_t pid) = 0;
194 
195     virtual status_t queryNumberEffects(uint32_t *numEffects) const = 0;
196 
197     virtual status_t queryEffect(uint32_t index, effect_descriptor_t *pDescriptor) const = 0;
198 
199     virtual status_t getEffectDescriptor(const effect_uuid_t *pEffectUUID,
200                                         effect_descriptor_t *pDescriptor) const = 0;
201 
202     virtual sp<IEffect> createEffect(
203                                     effect_descriptor_t *pDesc,
204                                     const sp<IEffectClient>& client,
205                                     int32_t priority,
206                                     // AudioFlinger doesn't take over handle reference from client
207                                     audio_io_handle_t output,
208                                     audio_session_t sessionId,
209                                     const String16& callingPackage,
210                                     status_t *status,
211                                     int *id,
212                                     int *enabled) = 0;
213 
214     virtual status_t moveEffects(audio_session_t session, audio_io_handle_t srcOutput,
215                                     audio_io_handle_t dstOutput) = 0;
216 
217     virtual audio_module_handle_t loadHwModule(const char *name) = 0;
218 
219     // helpers for android.media.AudioManager.getProperty(), see description there for meaning
220     // FIXME move these APIs to AudioPolicy to permit a more accurate implementation
221     // that looks on primary device for a stream with fast flag, primary flag, or first one.
222     virtual uint32_t getPrimaryOutputSamplingRate() = 0;
223     virtual size_t getPrimaryOutputFrameCount() = 0;
224 
225     // Intended for AudioService to inform AudioFlinger of device's low RAM attribute,
226     // and should be called at most once.  For a definition of what "low RAM" means, see
227     // android.app.ActivityManager.isLowRamDevice().
228     virtual status_t setLowRamDevice(bool isLowRamDevice) = 0;
229 
230     /* List available audio ports and their attributes */
231     virtual status_t listAudioPorts(unsigned int *num_ports,
232                                     struct audio_port *ports) = 0;
233 
234     /* Get attributes for a given audio port */
235     virtual status_t getAudioPort(struct audio_port *port) = 0;
236 
237     /* Create an audio patch between several source and sink ports */
238     virtual status_t createAudioPatch(const struct audio_patch *patch,
239                                        audio_patch_handle_t *handle) = 0;
240 
241     /* Release an audio patch */
242     virtual status_t releaseAudioPatch(audio_patch_handle_t handle) = 0;
243 
244     /* List existing audio patches */
245     virtual status_t listAudioPatches(unsigned int *num_patches,
246                                       struct audio_patch *patches) = 0;
247     /* Set audio port configuration */
248     virtual status_t setAudioPortConfig(const struct audio_port_config *config) = 0;
249 
250     /* Get the HW synchronization source used for an audio session */
251     virtual audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId) = 0;
252 
253     /* Indicate JAVA services are ready (scheduling, power management ...) */
254     virtual status_t systemReady() = 0;
255 
256     // Returns the number of frames per audio HAL buffer.
257     virtual size_t frameCountHAL(audio_io_handle_t ioHandle) const = 0;
258 };
259 
260 
261 // ----------------------------------------------------------------------------
262 
263 class BnAudioFlinger : public BnInterface<IAudioFlinger>
264 {
265 public:
266     virtual status_t    onTransact( uint32_t code,
267                                     const Parcel& data,
268                                     Parcel* reply,
269                                     uint32_t flags = 0);
270 };
271 
272 // ----------------------------------------------------------------------------
273 
274 }; // namespace android
275 
276 #endif // ANDROID_IAUDIOFLINGER_H
277