• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <binder/Parcel.h>
28 #include <binder/Parcelable.h>
29 #include <media/AudioClient.h>
30 #include <media/IAudioTrack.h>
31 #include <media/IAudioFlingerClient.h>
32 #include <system/audio.h>
33 #include <system/audio_effect.h>
34 #include <system/audio_policy.h>
35 #include <media/IEffect.h>
36 #include <media/IEffectClient.h>
37 #include <utils/String8.h>
38 #include <media/MicrophoneInfo.h>
39 #include <vector>
40 
41 #include "android/media/IAudioRecord.h"
42 
43 namespace android {
44 
45 // ----------------------------------------------------------------------------
46 
47 class IAudioFlinger : public IInterface
48 {
49 public:
50     DECLARE_META_INTERFACE(AudioFlinger);
51 
52     /* CreateTrackInput contains all input arguments sent by AudioTrack to AudioFlinger
53      * when calling createTrack() including arguments that will be updated by AudioFlinger
54      * and returned in CreateTrackOutput object
55      */
56     class CreateTrackInput : public Parcelable {
57     public:
readFromParcel(const Parcel * parcel)58         status_t readFromParcel(const Parcel *parcel) override {
59             /* input arguments*/
60             memset(&attr, 0, sizeof(audio_attributes_t));
61             if (parcel->read(&attr, sizeof(audio_attributes_t)) != NO_ERROR) {
62                 return DEAD_OBJECT;
63             }
64             attr.tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE -1] = '\0';
65             memset(&config, 0, sizeof(audio_config_t));
66             if (parcel->read(&config, sizeof(audio_config_t)) != NO_ERROR) {
67                 return DEAD_OBJECT;
68             }
69             if (clientInfo.readFromParcel(parcel) != NO_ERROR) {
70                 return DEAD_OBJECT;
71             }
72             if (parcel->readInt32() != 0) {
73                 sharedBuffer = interface_cast<IMemory>(parcel->readStrongBinder());
74                 if (sharedBuffer == 0 || sharedBuffer->pointer() == NULL) {
75                     return BAD_VALUE;
76                 }
77             }
78             notificationsPerBuffer = parcel->readInt32();
79             speed = parcel->readFloat();
80 
81             /* input/output arguments*/
82             (void)parcel->read(&flags, sizeof(audio_output_flags_t));
83             frameCount = parcel->readInt64();
84             notificationFrameCount = parcel->readInt64();
85             (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t));
86             (void)parcel->read(&sessionId, sizeof(audio_session_t));
87             return NO_ERROR;
88         }
89 
writeToParcel(Parcel * parcel)90         status_t writeToParcel(Parcel *parcel) const override {
91             /* input arguments*/
92             (void)parcel->write(&attr, sizeof(audio_attributes_t));
93             (void)parcel->write(&config, sizeof(audio_config_t));
94             (void)clientInfo.writeToParcel(parcel);
95             if (sharedBuffer != 0) {
96                 (void)parcel->writeInt32(1);
97                 (void)parcel->writeStrongBinder(IInterface::asBinder(sharedBuffer));
98             } else {
99                 (void)parcel->writeInt32(0);
100             }
101             (void)parcel->writeInt32(notificationsPerBuffer);
102             (void)parcel->writeFloat(speed);
103 
104             /* input/output arguments*/
105             (void)parcel->write(&flags, sizeof(audio_output_flags_t));
106             (void)parcel->writeInt64(frameCount);
107             (void)parcel->writeInt64(notificationFrameCount);
108             (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t));
109             (void)parcel->write(&sessionId, sizeof(audio_session_t));
110             return NO_ERROR;
111         }
112 
113         /* input */
114         audio_attributes_t attr;
115         audio_config_t config;
116         AudioClient clientInfo;
117         sp<IMemory> sharedBuffer;
118         uint32_t notificationsPerBuffer;
119         float speed;
120 
121         /* input/output */
122         audio_output_flags_t flags;
123         size_t frameCount;
124         size_t notificationFrameCount;
125         audio_port_handle_t selectedDeviceId;
126         audio_session_t sessionId;
127     };
128 
129     /* CreateTrackOutput contains all output arguments returned by AudioFlinger to AudioTrack
130      * when calling createTrack() including arguments that were passed as I/O for update by
131      * CreateTrackInput.
132      */
133     class CreateTrackOutput : public Parcelable {
134     public:
readFromParcel(const Parcel * parcel)135         status_t readFromParcel(const Parcel *parcel) override {
136             /* input/output arguments*/
137             (void)parcel->read(&flags, sizeof(audio_output_flags_t));
138             frameCount = parcel->readInt64();
139             notificationFrameCount = parcel->readInt64();
140             (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t));
141             (void)parcel->read(&sessionId, sizeof(audio_session_t));
142 
143             /* output arguments*/
144             sampleRate = parcel->readUint32();
145             afFrameCount = parcel->readInt64();
146             afSampleRate = parcel->readInt64();
147             afLatencyMs = parcel->readInt32();
148             (void)parcel->read(&outputId, sizeof(audio_io_handle_t));
149             return NO_ERROR;
150         }
151 
writeToParcel(Parcel * parcel)152         status_t writeToParcel(Parcel *parcel) const override {
153             /* input/output arguments*/
154             (void)parcel->write(&flags, sizeof(audio_output_flags_t));
155             (void)parcel->writeInt64(frameCount);
156             (void)parcel->writeInt64(notificationFrameCount);
157             (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t));
158             (void)parcel->write(&sessionId, sizeof(audio_session_t));
159 
160             /* output arguments*/
161             (void)parcel->writeUint32(sampleRate);
162             (void)parcel->writeInt64(afFrameCount);
163             (void)parcel->writeInt64(afSampleRate);
164             (void)parcel->writeInt32(afLatencyMs);
165             (void)parcel->write(&outputId, sizeof(audio_io_handle_t));
166             return NO_ERROR;
167         }
168 
169         /* input/output */
170         audio_output_flags_t flags;
171         size_t frameCount;
172         size_t notificationFrameCount;
173         audio_port_handle_t selectedDeviceId;
174         audio_session_t sessionId;
175 
176         /* output */
177         uint32_t sampleRate;
178         size_t   afFrameCount;
179         uint32_t afSampleRate;
180         uint32_t afLatencyMs;
181         audio_io_handle_t outputId;
182     };
183 
184     /* CreateRecordInput contains all input arguments sent by AudioRecord to AudioFlinger
185      * when calling createRecord() including arguments that will be updated by AudioFlinger
186      * and returned in CreateRecordOutput object
187      */
188     class CreateRecordInput : public Parcelable {
189     public:
readFromParcel(const Parcel * parcel)190         status_t readFromParcel(const Parcel *parcel) override {
191             /* input arguments*/
192             memset(&attr, 0, sizeof(audio_attributes_t));
193             if (parcel->read(&attr, sizeof(audio_attributes_t)) != NO_ERROR) {
194                 return DEAD_OBJECT;
195             }
196             attr.tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE -1] = '\0';
197             memset(&config, 0, sizeof(audio_config_base_t));
198             if (parcel->read(&config, sizeof(audio_config_base_t)) != NO_ERROR) {
199                 return DEAD_OBJECT;
200             }
201             if (clientInfo.readFromParcel(parcel) != NO_ERROR) {
202                 return DEAD_OBJECT;
203             }
204             opPackageName = parcel->readString16();
205 
206             /* input/output arguments*/
207             (void)parcel->read(&flags, sizeof(audio_input_flags_t));
208             frameCount = parcel->readInt64();
209             notificationFrameCount = parcel->readInt64();
210             (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t));
211             (void)parcel->read(&sessionId, sizeof(audio_session_t));
212             return NO_ERROR;
213         }
214 
writeToParcel(Parcel * parcel)215         status_t writeToParcel(Parcel *parcel) const override {
216             /* input arguments*/
217             (void)parcel->write(&attr, sizeof(audio_attributes_t));
218             (void)parcel->write(&config, sizeof(audio_config_base_t));
219             (void)clientInfo.writeToParcel(parcel);
220             (void)parcel->writeString16(opPackageName);
221 
222             /* input/output arguments*/
223             (void)parcel->write(&flags, sizeof(audio_input_flags_t));
224             (void)parcel->writeInt64(frameCount);
225             (void)parcel->writeInt64(notificationFrameCount);
226             (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t));
227             (void)parcel->write(&sessionId, sizeof(audio_session_t));
228             return NO_ERROR;
229         }
230 
231         /* input */
232         audio_attributes_t attr;
233         audio_config_base_t config;
234         AudioClient clientInfo;
235         String16 opPackageName;
236 
237         /* input/output */
238         audio_input_flags_t flags;
239         size_t frameCount;
240         size_t notificationFrameCount;
241         audio_port_handle_t selectedDeviceId;
242         audio_session_t sessionId;
243     };
244 
245     /* CreateRecordOutput contains all output arguments returned by AudioFlinger to AudioRecord
246      * when calling createRecord() including arguments that were passed as I/O for update by
247      * CreateRecordInput.
248      */
249     class CreateRecordOutput : public Parcelable {
250     public:
readFromParcel(const Parcel * parcel)251         status_t readFromParcel(const Parcel *parcel) override {
252             /* input/output arguments*/
253             (void)parcel->read(&flags, sizeof(audio_input_flags_t));
254             frameCount = parcel->readInt64();
255             notificationFrameCount = parcel->readInt64();
256             (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t));
257             (void)parcel->read(&sessionId, sizeof(audio_session_t));
258 
259             /* output arguments*/
260             sampleRate = parcel->readUint32();
261             (void)parcel->read(&inputId, sizeof(audio_io_handle_t));
262             if (parcel->readInt32() != 0) {
263                 cblk = interface_cast<IMemory>(parcel->readStrongBinder());
264                 if (cblk == 0 || cblk->pointer() == NULL) {
265                     return BAD_VALUE;
266                 }
267             }
268             if (parcel->readInt32() != 0) {
269                 buffers = interface_cast<IMemory>(parcel->readStrongBinder());
270                 if (buffers == 0 || buffers->pointer() == NULL) {
271                     return BAD_VALUE;
272                 }
273             }
274             return NO_ERROR;
275         }
276 
writeToParcel(Parcel * parcel)277         status_t writeToParcel(Parcel *parcel) const override {
278             /* input/output arguments*/
279             (void)parcel->write(&flags, sizeof(audio_input_flags_t));
280             (void)parcel->writeInt64(frameCount);
281             (void)parcel->writeInt64(notificationFrameCount);
282             (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t));
283             (void)parcel->write(&sessionId, sizeof(audio_session_t));
284 
285             /* output arguments*/
286             (void)parcel->writeUint32(sampleRate);
287             (void)parcel->write(&inputId, sizeof(audio_io_handle_t));
288             if (cblk != 0) {
289                 (void)parcel->writeInt32(1);
290                 (void)parcel->writeStrongBinder(IInterface::asBinder(cblk));
291             } else {
292                 (void)parcel->writeInt32(0);
293             }
294             if (buffers != 0) {
295                 (void)parcel->writeInt32(1);
296                 (void)parcel->writeStrongBinder(IInterface::asBinder(buffers));
297             } else {
298                 (void)parcel->writeInt32(0);
299             }
300 
301             return NO_ERROR;
302         }
303 
304         /* input/output */
305         audio_input_flags_t flags;
306         size_t frameCount;
307         size_t notificationFrameCount;
308         audio_port_handle_t selectedDeviceId;
309         audio_session_t sessionId;
310 
311         /* output */
312         uint32_t sampleRate;
313         audio_io_handle_t inputId;
314         sp<IMemory> cblk;
315         sp<IMemory> buffers;
316     };
317 
318     // invariant on exit for all APIs that return an sp<>:
319     //   (return value != 0) == (*status == NO_ERROR)
320 
321     /* create an audio track and registers it with AudioFlinger.
322      * return null if the track cannot be created.
323      */
324     virtual sp<IAudioTrack> createTrack(const CreateTrackInput& input,
325                                         CreateTrackOutput& output,
326                                         status_t *status) = 0;
327 
328     virtual sp<media::IAudioRecord> createRecord(const CreateRecordInput& input,
329                                         CreateRecordOutput& output,
330                                         status_t *status) = 0;
331 
332     // FIXME Surprisingly, format/latency don't work for input handles
333 
334     /* query the audio hardware state. This state never changes,
335      * and therefore can be cached.
336      */
337     virtual     uint32_t    sampleRate(audio_io_handle_t ioHandle) const = 0;
338 
339     // reserved; formerly channelCount()
340 
341     virtual     audio_format_t format(audio_io_handle_t output) const = 0;
342     virtual     size_t      frameCount(audio_io_handle_t ioHandle) const = 0;
343 
344     // return estimated latency in milliseconds
345     virtual     uint32_t    latency(audio_io_handle_t output) const = 0;
346 
347     /* set/get the audio hardware state. This will probably be used by
348      * the preference panel, mostly.
349      */
350     virtual     status_t    setMasterVolume(float value) = 0;
351     virtual     status_t    setMasterMute(bool muted) = 0;
352 
353     virtual     float       masterVolume() const = 0;
354     virtual     bool        masterMute() const = 0;
355 
356     /* set/get stream type state. This will probably be used by
357      * the preference panel, mostly.
358      */
359     virtual     status_t    setStreamVolume(audio_stream_type_t stream, float value,
360                                     audio_io_handle_t output) = 0;
361     virtual     status_t    setStreamMute(audio_stream_type_t stream, bool muted) = 0;
362 
363     virtual     float       streamVolume(audio_stream_type_t stream,
364                                     audio_io_handle_t output) const = 0;
365     virtual     bool        streamMute(audio_stream_type_t stream) const = 0;
366 
367     // set audio mode
368     virtual     status_t    setMode(audio_mode_t mode) = 0;
369 
370     // mic mute/state
371     virtual     status_t    setMicMute(bool state) = 0;
372     virtual     bool        getMicMute() const = 0;
373     virtual     void        setRecordSilenced(uid_t uid, bool silenced) = 0;
374 
375     virtual     status_t    setParameters(audio_io_handle_t ioHandle,
376                                     const String8& keyValuePairs) = 0;
377     virtual     String8     getParameters(audio_io_handle_t ioHandle, const String8& keys)
378                                     const = 0;
379 
380     // Register an object to receive audio input/output change and track notifications.
381     // For a given calling pid, AudioFlinger disregards any registrations after the first.
382     // Thus the IAudioFlingerClient must be a singleton per process.
383     virtual void registerClient(const sp<IAudioFlingerClient>& client) = 0;
384 
385     // retrieve the audio recording buffer size
386     // FIXME This API assumes a route, and so should be deprecated.
387     virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
388             audio_channel_mask_t channelMask) const = 0;
389 
390     virtual status_t openOutput(audio_module_handle_t module,
391                                 audio_io_handle_t *output,
392                                 audio_config_t *config,
393                                 audio_devices_t *devices,
394                                 const String8& address,
395                                 uint32_t *latencyMs,
396                                 audio_output_flags_t flags) = 0;
397     virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
398                                     audio_io_handle_t output2) = 0;
399     virtual status_t closeOutput(audio_io_handle_t output) = 0;
400     virtual status_t suspendOutput(audio_io_handle_t output) = 0;
401     virtual status_t restoreOutput(audio_io_handle_t output) = 0;
402 
403     virtual status_t openInput(audio_module_handle_t module,
404                                audio_io_handle_t *input,
405                                audio_config_t *config,
406                                audio_devices_t *device,
407                                const String8& address,
408                                audio_source_t source,
409                                audio_input_flags_t flags) = 0;
410     virtual status_t closeInput(audio_io_handle_t input) = 0;
411 
412     virtual status_t invalidateStream(audio_stream_type_t stream) = 0;
413 
414     virtual status_t setVoiceVolume(float volume) = 0;
415 
416     virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
417                                     audio_io_handle_t output) const = 0;
418 
419     virtual uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const = 0;
420 
421     virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use) = 0;
422 
423     virtual void acquireAudioSessionId(audio_session_t audioSession, pid_t pid) = 0;
424     virtual void releaseAudioSessionId(audio_session_t audioSession, pid_t pid) = 0;
425 
426     virtual status_t queryNumberEffects(uint32_t *numEffects) const = 0;
427 
428     virtual status_t queryEffect(uint32_t index, effect_descriptor_t *pDescriptor) const = 0;
429 
430     virtual status_t getEffectDescriptor(const effect_uuid_t *pEffectUUID,
431                                         effect_descriptor_t *pDescriptor) const = 0;
432 
433     virtual sp<IEffect> createEffect(
434                                     effect_descriptor_t *pDesc,
435                                     const sp<IEffectClient>& client,
436                                     int32_t priority,
437                                     // AudioFlinger doesn't take over handle reference from client
438                                     audio_io_handle_t output,
439                                     audio_session_t sessionId,
440                                     const String16& callingPackage,
441                                     pid_t pid,
442                                     status_t *status,
443                                     int *id,
444                                     int *enabled) = 0;
445 
446     virtual status_t moveEffects(audio_session_t session, audio_io_handle_t srcOutput,
447                                     audio_io_handle_t dstOutput) = 0;
448 
449     virtual audio_module_handle_t loadHwModule(const char *name) = 0;
450 
451     // helpers for android.media.AudioManager.getProperty(), see description there for meaning
452     // FIXME move these APIs to AudioPolicy to permit a more accurate implementation
453     // that looks on primary device for a stream with fast flag, primary flag, or first one.
454     virtual uint32_t getPrimaryOutputSamplingRate() = 0;
455     virtual size_t getPrimaryOutputFrameCount() = 0;
456 
457     // Intended for AudioService to inform AudioFlinger of device's low RAM attribute,
458     // and should be called at most once.  For a definition of what "low RAM" means, see
459     // android.app.ActivityManager.isLowRamDevice().  The totalMemory parameter
460     // is obtained from android.app.ActivityManager.MemoryInfo.totalMem.
461     virtual status_t setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) = 0;
462 
463     /* List available audio ports and their attributes */
464     virtual status_t listAudioPorts(unsigned int *num_ports,
465                                     struct audio_port *ports) = 0;
466 
467     /* Get attributes for a given audio port */
468     virtual status_t getAudioPort(struct audio_port *port) = 0;
469 
470     /* Create an audio patch between several source and sink ports */
471     virtual status_t createAudioPatch(const struct audio_patch *patch,
472                                        audio_patch_handle_t *handle) = 0;
473 
474     /* Release an audio patch */
475     virtual status_t releaseAudioPatch(audio_patch_handle_t handle) = 0;
476 
477     /* List existing audio patches */
478     virtual status_t listAudioPatches(unsigned int *num_patches,
479                                       struct audio_patch *patches) = 0;
480     /* Set audio port configuration */
481     virtual status_t setAudioPortConfig(const struct audio_port_config *config) = 0;
482 
483     /* Get the HW synchronization source used for an audio session */
484     virtual audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId) = 0;
485 
486     /* Indicate JAVA services are ready (scheduling, power management ...) */
487     virtual status_t systemReady() = 0;
488 
489     // Returns the number of frames per audio HAL buffer.
490     virtual size_t frameCountHAL(audio_io_handle_t ioHandle) const = 0;
491 
492     /* List available microphones and their characteristics */
493     virtual status_t getMicrophones(std::vector<media::MicrophoneInfo> *microphones) = 0;
494 };
495 
496 
497 // ----------------------------------------------------------------------------
498 
499 class BnAudioFlinger : public BnInterface<IAudioFlinger>
500 {
501 public:
502     virtual status_t    onTransact( uint32_t code,
503                                     const Parcel& data,
504                                     Parcel* reply,
505                                     uint32_t flags = 0);
506 
507     // Requests media.log to start merging log buffers
508     virtual void requestLogMerge() = 0;
509 };
510 
511 // ----------------------------------------------------------------------------
512 
513 }; // namespace android
514 
515 #endif // ANDROID_IAUDIOFLINGER_H
516