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