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