• 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 <media/AidlConversion.h>
28 #include <media/AudioClient.h>
29 #include <media/AudioCommonTypes.h>
30 #include <media/DeviceDescriptorBase.h>
31 #include <system/audio.h>
32 #include <system/audio_effect.h>
33 #include <system/audio_policy.h>
34 #include <utils/String8.h>
35 #include <map>
36 #include <string>
37 #include <vector>
38 
39 #include <android/content/AttributionSourceState.h>
40 #include <android/media/AudioVibratorInfo.h>
41 #include <android/media/BnAudioFlingerService.h>
42 #include <android/media/BpAudioFlingerService.h>
43 #include <android/media/audio/common/AudioMMapPolicyInfo.h>
44 #include <android/media/audio/common/AudioMMapPolicyType.h>
45 #include "android/media/CreateEffectRequest.h"
46 #include "android/media/CreateEffectResponse.h"
47 #include "android/media/CreateRecordRequest.h"
48 #include "android/media/CreateRecordResponse.h"
49 #include "android/media/CreateTrackRequest.h"
50 #include "android/media/CreateTrackResponse.h"
51 #include "android/media/IAudioRecord.h"
52 #include "android/media/IAudioFlingerClient.h"
53 #include "android/media/IAudioTrack.h"
54 #include "android/media/IAudioTrackCallback.h"
55 #include "android/media/IEffect.h"
56 #include "android/media/IEffectClient.h"
57 #include "android/media/ISoundDose.h"
58 #include "android/media/ISoundDoseCallback.h"
59 #include "android/media/OpenInputRequest.h"
60 #include "android/media/OpenInputResponse.h"
61 #include "android/media/OpenOutputRequest.h"
62 #include "android/media/OpenOutputResponse.h"
63 #include "android/media/TrackInternalMuteInfo.h"
64 #include "android/media/TrackSecondaryOutputInfo.h"
65 
66 namespace android {
67 
68 // ----------------------------------------------------------------------------
69 
70 class IAudioFlinger : public virtual RefBase {
71 public:
72     static constexpr char DEFAULT_SERVICE_NAME[] = "media.audio_flinger";
73 
74     virtual ~IAudioFlinger() = default;
75 
76     /* CreateTrackInput contains all input arguments sent by AudioTrack to AudioFlinger
77      * when calling createTrack() including arguments that will be updated by AudioFlinger
78      * and returned in CreateTrackOutput object
79      */
80     class CreateTrackInput {
81     public:
82         /* input */
83         audio_attributes_t attr;
84         audio_config_t config;
85         AudioClient clientInfo;
86         sp<IMemory> sharedBuffer;
87         uint32_t notificationsPerBuffer;
88         float speed;
89         sp<media::IAudioTrackCallback> audioTrackCallback;
90 
91         /* input/output */
92         audio_output_flags_t flags;
93         size_t frameCount;
94         size_t notificationFrameCount;
95         audio_port_handle_t selectedDeviceId;
96         audio_session_t sessionId;
97 
98         ConversionResult<media::CreateTrackRequest> toAidl() const;
99         static ConversionResult<CreateTrackInput> fromAidl(const media::CreateTrackRequest& aidl);
100     };
101 
102     /* CreateTrackOutput contains all output arguments returned by AudioFlinger to AudioTrack
103      * when calling createTrack() including arguments that were passed as I/O for update by
104      * CreateTrackInput.
105      */
106     class CreateTrackOutput {
107     public:
108         /* input/output */
109         audio_output_flags_t flags;
110         size_t frameCount;
111         size_t notificationFrameCount;
112         audio_port_handle_t selectedDeviceId;
113         audio_session_t sessionId;
114 
115         /* output */
116         uint32_t sampleRate;
117         audio_stream_type_t streamType;
118         size_t   afFrameCount;
119         uint32_t afSampleRate;
120         uint32_t afLatencyMs;
121         audio_channel_mask_t afChannelMask;
122         audio_format_t afFormat;
123         audio_output_flags_t afTrackFlags;
124         audio_io_handle_t outputId;
125         audio_port_handle_t portId;
126         sp<media::IAudioTrack> audioTrack;
127 
128         ConversionResult<media::CreateTrackResponse> toAidl() const;
129         static ConversionResult<CreateTrackOutput> fromAidl(const media::CreateTrackResponse& aidl);
130     };
131 
132     /* CreateRecordInput contains all input arguments sent by AudioRecord to AudioFlinger
133      * when calling createRecord() including arguments that will be updated by AudioFlinger
134      * and returned in CreateRecordOutput object
135      */
136     class CreateRecordInput {
137     public:
138         /* input */
139         audio_attributes_t attr;
140         audio_config_base_t config;
141         AudioClient clientInfo;
142         audio_unique_id_t riid;
143         int32_t maxSharedAudioHistoryMs;
144 
145         /* input/output */
146         audio_input_flags_t flags;
147         size_t frameCount;
148         size_t notificationFrameCount;
149         audio_port_handle_t selectedDeviceId;
150         audio_session_t sessionId;
151 
152         ConversionResult<media::CreateRecordRequest> toAidl() const;
153         static ConversionResult<CreateRecordInput> fromAidl(const media::CreateRecordRequest& aidl);
154     };
155 
156     /* CreateRecordOutput contains all output arguments returned by AudioFlinger to AudioRecord
157      * when calling createRecord() including arguments that were passed as I/O for update by
158      * CreateRecordInput.
159      */
160     class CreateRecordOutput {
161     public:
162         /* input/output */
163         audio_input_flags_t flags;
164         size_t frameCount;
165         size_t notificationFrameCount;
166         audio_port_handle_t selectedDeviceId;
167         audio_session_t sessionId;
168 
169         /* output */
170         uint32_t sampleRate;
171         audio_io_handle_t inputId;
172         sp<IMemory> cblk;
173         sp<IMemory> buffers;
174         audio_port_handle_t portId;
175         sp<media::IAudioRecord> audioRecord;
176         audio_config_base_t serverConfig;
177         audio_config_base_t halConfig;
178 
179         ConversionResult<media::CreateRecordResponse> toAidl() const;
180         static ConversionResult<CreateRecordOutput>
181         fromAidl(const media::CreateRecordResponse& aidl);
182     };
183 
184     /* create an audio track and registers it with AudioFlinger.
185      * The audioTrack field will be null if the track cannot be created and the status will reflect
186      * failure.
187      */
188     virtual status_t createTrack(const media::CreateTrackRequest& input,
189                                  media::CreateTrackResponse& output) = 0;
190 
191     /* create an audio record and registers it with AudioFlinger.
192      * The audioRecord field will be null if the track cannot be created and the status will reflect
193      * failure.
194      */
195     virtual status_t createRecord(const media::CreateRecordRequest& input,
196                                   media::CreateRecordResponse& output) = 0;
197 
198     // FIXME Surprisingly, format/latency don't work for input handles
199 
200     /* query the audio hardware state. This state never changes,
201      * and therefore can be cached.
202      */
203     virtual     uint32_t    sampleRate(audio_io_handle_t ioHandle) const = 0;
204 
205     // reserved; formerly channelCount()
206 
207     virtual     audio_format_t format(audio_io_handle_t output) const = 0;
208     virtual     size_t      frameCount(audio_io_handle_t ioHandle) const = 0;
209 
210     // return estimated latency in milliseconds
211     virtual     uint32_t    latency(audio_io_handle_t output) const = 0;
212 
213     /* set/get the audio hardware state. This will probably be used by
214      * the preference panel, mostly.
215      */
216     virtual     status_t    setMasterVolume(float value) = 0;
217     virtual     status_t    setMasterMute(bool muted) = 0;
218 
219     virtual     float       masterVolume() const = 0;
220     virtual     bool        masterMute() const = 0;
221 
222     virtual     status_t    setMasterBalance(float balance) = 0;
223     virtual     status_t    getMasterBalance(float *balance) const = 0;
224 
225     /* set/get stream type state. This will probably be used by
226      * the preference panel, mostly.
227      */
228     virtual     status_t    setStreamVolume(audio_stream_type_t stream, float value,
229                                     audio_io_handle_t output) = 0;
230     virtual     status_t    setStreamMute(audio_stream_type_t stream, bool muted) = 0;
231 
232     virtual     float       streamVolume(audio_stream_type_t stream,
233                                     audio_io_handle_t output) const = 0;
234     virtual     bool        streamMute(audio_stream_type_t stream) const = 0;
235 
236     // set audio mode
237     virtual     status_t    setMode(audio_mode_t mode) = 0;
238 
239     // mic mute/state
240     virtual     status_t    setMicMute(bool state) = 0;
241     virtual     bool        getMicMute() const = 0;
242     virtual     void        setRecordSilenced(audio_port_handle_t portId, bool silenced) = 0;
243 
244     virtual     status_t    setParameters(audio_io_handle_t ioHandle,
245                                     const String8& keyValuePairs) = 0;
246     virtual     String8     getParameters(audio_io_handle_t ioHandle, const String8& keys)
247                                     const = 0;
248 
249     // Register an object to receive audio input/output change and track notifications.
250     // For a given calling pid, AudioFlinger disregards any registrations after the first.
251     // Thus the IAudioFlingerClient must be a singleton per process.
252     virtual void registerClient(const sp<media::IAudioFlingerClient>& client) = 0;
253 
254     // retrieve the audio recording buffer size in bytes
255     // FIXME This API assumes a route, and so should be deprecated.
256     virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
257             audio_channel_mask_t channelMask) const = 0;
258 
259     virtual status_t openOutput(const media::OpenOutputRequest& request,
260                                 media::OpenOutputResponse* response) = 0;
261     virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
262                                     audio_io_handle_t output2) = 0;
263     virtual status_t closeOutput(audio_io_handle_t output) = 0;
264     virtual status_t suspendOutput(audio_io_handle_t output) = 0;
265     virtual status_t restoreOutput(audio_io_handle_t output) = 0;
266 
267     virtual status_t openInput(const media::OpenInputRequest& request,
268                                media::OpenInputResponse* response) = 0;
269 
270     virtual status_t closeInput(audio_io_handle_t input) = 0;
271 
272     virtual status_t setVoiceVolume(float volume) = 0;
273 
274     virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
275                                     audio_io_handle_t output) const = 0;
276 
277     virtual uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const = 0;
278 
279     virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use) = 0;
280 
281     virtual void acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) = 0;
282     virtual void releaseAudioSessionId(audio_session_t audioSession, pid_t pid) = 0;
283 
284     virtual status_t queryNumberEffects(uint32_t *numEffects) const = 0;
285 
286     virtual status_t queryEffect(uint32_t index, effect_descriptor_t *pDescriptor) const = 0;
287 
288     virtual status_t getEffectDescriptor(const effect_uuid_t *pEffectUUID,
289                                          const effect_uuid_t *pTypeUUID,
290                                          uint32_t preferredTypeFlag,
291                                          effect_descriptor_t *pDescriptor) const = 0;
292 
293     virtual status_t createEffect(const media::CreateEffectRequest& request,
294                                   media::CreateEffectResponse* response) = 0;
295 
296     virtual status_t moveEffects(audio_session_t session, audio_io_handle_t srcOutput,
297                                     audio_io_handle_t dstOutput) = 0;
298 
299     virtual void setEffectSuspended(int effectId,
300                                     audio_session_t sessionId,
301                                     bool suspended) = 0;
302 
303     virtual audio_module_handle_t loadHwModule(const char *name) = 0;
304 
305     // helpers for android.media.AudioManager.getProperty(), see description there for meaning
306     // FIXME move these APIs to AudioPolicy to permit a more accurate implementation
307     // that looks on primary device for a stream with fast flag, primary flag, or first one.
308     virtual uint32_t getPrimaryOutputSamplingRate() const = 0;
309     virtual size_t getPrimaryOutputFrameCount() const = 0;
310 
311     // Intended for AudioService to inform AudioFlinger of device's low RAM attribute,
312     // and should be called at most once.  For a definition of what "low RAM" means, see
313     // android.app.ActivityManager.isLowRamDevice().  The totalMemory parameter
314     // is obtained from android.app.ActivityManager.MemoryInfo.totalMem.
315     virtual status_t setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) = 0;
316 
317     /* Get attributes for a given audio port */
318     virtual status_t getAudioPort(struct audio_port_v7* port) const = 0;
319 
320     /* Create an audio patch between several source and sink ports */
321     virtual status_t createAudioPatch(const struct audio_patch *patch,
322                                        audio_patch_handle_t *handle) = 0;
323 
324     /* Release an audio patch */
325     virtual status_t releaseAudioPatch(audio_patch_handle_t handle) = 0;
326 
327     /* List existing audio patches */
328     virtual status_t listAudioPatches(unsigned int *num_patches,
329                                       struct audio_patch* patches) const = 0;
330     /* Set audio port configuration */
331     virtual status_t setAudioPortConfig(const struct audio_port_config *config) = 0;
332 
333     /* Get the HW synchronization source used for an audio session */
334     virtual audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId) = 0;
335 
336     /* Indicate JAVA services are ready (scheduling, power management ...) */
337     virtual status_t systemReady() = 0;
338 
339     // Indicate audio policy service is ready
340     virtual status_t audioPolicyReady() = 0;
341 
342     // Returns the number of frames per audio HAL buffer.
343     virtual size_t frameCountHAL(audio_io_handle_t ioHandle) const = 0;
344 
345     /* List available microphones and their characteristics */
346     virtual status_t getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) const = 0;
347 
348     virtual status_t setAudioHalPids(const std::vector<pid_t>& pids) = 0;
349 
350     // Set vibrators' information.
351     // The values will be used to initialize HapticGenerator.
352     virtual status_t setVibratorInfos(
353             const std::vector<media::AudioVibratorInfo>& vibratorInfos) = 0;
354 
355     virtual status_t updateSecondaryOutputs(
356             const TrackSecondaryOutputsMap& trackSecondaryOutputs) = 0;
357 
358     virtual status_t getMmapPolicyInfos(
359             media::audio::common::AudioMMapPolicyType policyType,
360             std::vector<media::audio::common::AudioMMapPolicyInfo> *policyInfos) = 0;
361 
362     virtual int32_t getAAudioMixerBurstCount() const = 0;
363 
364     virtual int32_t getAAudioHardwareBurstMinUsec() const = 0;
365 
366     virtual status_t setDeviceConnectedState(const struct audio_port_v7 *port,
367                                              media::DeviceConnectedState state) = 0;
368 
369     virtual status_t setSimulateDeviceConnections(bool enabled) = 0;
370 
371     virtual status_t setRequestedLatencyMode(
372             audio_io_handle_t output, audio_latency_mode_t mode) = 0;
373 
374     virtual status_t getSupportedLatencyModes(audio_io_handle_t output,
375             std::vector<audio_latency_mode_t>* modes) const = 0;
376 
377     virtual status_t getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
378                                            sp<media::ISoundDose>* soundDose) const = 0;
379 
380     virtual status_t invalidateTracks(const std::vector<audio_port_handle_t>& portIds) = 0;
381 
382     virtual status_t setBluetoothVariableLatencyEnabled(bool enabled) = 0;
383 
384     virtual status_t isBluetoothVariableLatencyEnabled(bool* enabled) const = 0;
385 
386     virtual status_t supportsBluetoothVariableLatency(bool* support) const = 0;
387 
388     virtual status_t getAudioPolicyConfig(media::AudioPolicyConfig* output) = 0;
389 
390     virtual status_t getAudioMixPort(const struct audio_port_v7 *devicePort,
391                                      struct audio_port_v7 *mixPort) const = 0;
392 
393     virtual status_t setTracksInternalMute(
394             const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) = 0;
395 
396     virtual status_t resetReferencesForTest() = 0;
397 };
398 
399 /**
400  * A client-side adapter, wrapping an IAudioFlingerService instance and presenting it as an
401  * IAudioFlinger. Intended to be used by legacy client code that was written against IAudioFlinger,
402  * before IAudioFlingerService was introduced as an AIDL service.
403  * New clients should not use this adapter, but rather IAudioFlingerService directly, via
404  * BpAudioFlingerService.
405  */
406 class AudioFlingerClientAdapter : public IAudioFlinger {
407 public:
408     explicit AudioFlingerClientAdapter(const sp<media::IAudioFlingerService> delegate);
409 
410     status_t createTrack(const media::CreateTrackRequest& input,
411                          media::CreateTrackResponse& output) override;
412     status_t createRecord(const media::CreateRecordRequest& input,
413                           media::CreateRecordResponse& output) override;
414     uint32_t sampleRate(audio_io_handle_t ioHandle) const override;
415     audio_format_t format(audio_io_handle_t output) const override;
416     size_t frameCount(audio_io_handle_t ioHandle) const override;
417     uint32_t latency(audio_io_handle_t output) const override;
418     status_t setMasterVolume(float value) override;
419     status_t setMasterMute(bool muted) override;
420     float masterVolume() const override;
421     bool masterMute() const override;
422     status_t setMasterBalance(float balance) override;
423     status_t getMasterBalance(float* balance) const override;
424     status_t setStreamVolume(audio_stream_type_t stream, float value,
425                              audio_io_handle_t output) override;
426     status_t setStreamMute(audio_stream_type_t stream, bool muted) override;
427     float streamVolume(audio_stream_type_t stream,
428                        audio_io_handle_t output) const override;
429     bool streamMute(audio_stream_type_t stream) const override;
430     status_t setMode(audio_mode_t mode) override;
431     status_t setMicMute(bool state) override;
432     bool getMicMute() const override;
433     void setRecordSilenced(audio_port_handle_t portId, bool silenced) override;
434     status_t setParameters(audio_io_handle_t ioHandle,
435                            const String8& keyValuePairs) override;
436     String8 getParameters(audio_io_handle_t ioHandle, const String8& keys)
437     const override;
438     void registerClient(const sp<media::IAudioFlingerClient>& client) override;
439     size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
440                               audio_channel_mask_t channelMask) const override;
441     status_t openOutput(const media::OpenOutputRequest& request,
442                         media::OpenOutputResponse* response) override;
443     audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
444                                           audio_io_handle_t output2) override;
445     status_t closeOutput(audio_io_handle_t output) override;
446     status_t suspendOutput(audio_io_handle_t output) override;
447     status_t restoreOutput(audio_io_handle_t output) override;
448     status_t openInput(const media::OpenInputRequest& request,
449                        media::OpenInputResponse* response) override;
450     status_t closeInput(audio_io_handle_t input) override;
451     status_t setVoiceVolume(float volume) override;
452     status_t getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames,
453                                audio_io_handle_t output) const override;
454     uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const override;
455     audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use) override;
456     void acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) override;
457     void releaseAudioSessionId(audio_session_t audioSession, pid_t pid) override;
458     status_t queryNumberEffects(uint32_t* numEffects) const override;
459     status_t queryEffect(uint32_t index, effect_descriptor_t* pDescriptor) const override;
460     status_t getEffectDescriptor(const effect_uuid_t* pEffectUUID,
461                                  const effect_uuid_t* pTypeUUID,
462                                  uint32_t preferredTypeFlag,
463                                  effect_descriptor_t* pDescriptor) const override;
464     status_t createEffect(const media::CreateEffectRequest& request,
465                           media::CreateEffectResponse* response) override;
466     status_t moveEffects(audio_session_t session, audio_io_handle_t srcOutput,
467                          audio_io_handle_t dstOutput) override;
468     void setEffectSuspended(int effectId,
469                             audio_session_t sessionId,
470                             bool suspended) override;
471     audio_module_handle_t loadHwModule(const char* name) override;
472     uint32_t getPrimaryOutputSamplingRate() const override;
473     size_t getPrimaryOutputFrameCount() const override;
474     status_t setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) override;
475     status_t getAudioPort(struct audio_port_v7* port) const override;
476     status_t createAudioPatch(const struct audio_patch* patch,
477                               audio_patch_handle_t* handle) override;
478     status_t releaseAudioPatch(audio_patch_handle_t handle) override;
479     status_t listAudioPatches(unsigned int* num_patches,
480                               struct audio_patch* patches) const override;
481     status_t setAudioPortConfig(const struct audio_port_config* config) override;
482     audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId) override;
483     status_t systemReady() override;
484     status_t audioPolicyReady() override;
485 
486     size_t frameCountHAL(audio_io_handle_t ioHandle) const override;
487     status_t getMicrophones(std::vector<media::MicrophoneInfoFw>* microphones) const override;
488     status_t setAudioHalPids(const std::vector<pid_t>& pids) override;
489     status_t setVibratorInfos(const std::vector<media::AudioVibratorInfo>& vibratorInfos) override;
490     status_t updateSecondaryOutputs(
491             const TrackSecondaryOutputsMap& trackSecondaryOutputs) override;
492     status_t getMmapPolicyInfos(
493             media::audio::common::AudioMMapPolicyType policyType,
494             std::vector<media::audio::common::AudioMMapPolicyInfo> *policyInfos) override;
495     int32_t getAAudioMixerBurstCount() const override;
496     int32_t getAAudioHardwareBurstMinUsec() const override;
497     status_t setDeviceConnectedState(const struct audio_port_v7 *port,
498                                      media::DeviceConnectedState state) override;
499     status_t setSimulateDeviceConnections(bool enabled) override;
500     status_t setRequestedLatencyMode(audio_io_handle_t output,
501             audio_latency_mode_t mode) override;
502     status_t getSupportedLatencyModes(
503             audio_io_handle_t output, std::vector<audio_latency_mode_t>* modes) const override;
504     status_t setBluetoothVariableLatencyEnabled(bool enabled) override;
505     status_t isBluetoothVariableLatencyEnabled(bool* enabled) const override;
506     status_t supportsBluetoothVariableLatency(bool* support) const override;
507     status_t getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
508                                    sp<media::ISoundDose>* soundDose) const override;
509     status_t invalidateTracks(const std::vector<audio_port_handle_t>& portIds) override;
510     status_t getAudioPolicyConfig(media::AudioPolicyConfig* output) override;
511     status_t getAudioMixPort(const struct audio_port_v7 *devicePort,
512                              struct audio_port_v7 *mixPort) const override;
513     status_t setTracksInternalMute(
514             const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) override;
515     status_t resetReferencesForTest() override;
516 
517 private:
518     const sp<media::IAudioFlingerService> mDelegate;
519 };
520 
521 /**
522  * A server-side adapter, wrapping an IAudioFlinger instance and presenting it as an
523  * IAudioFlingerService. Intended to be used by legacy server code that was written against
524  * IAudioFlinger, before IAudioFlingerService was introduced as an AIDL service.
525  * New servers should not use this adapter, but rather implement IAudioFlingerService directly, via
526  * BnAudioFlingerService.
527  */
528 class AudioFlingerServerAdapter : public media::BnAudioFlingerService {
529 public:
530     using Status = binder::Status;
531 
532     /**
533      * Legacy server should implement this interface in order to be wrapped.
534      */
535     class Delegate : public IAudioFlinger {
536         friend class AudioFlingerServerAdapter;
537     public:
538         // expose the TransactionCode enum for TimeCheck purposes.
539         enum class TransactionCode {
540             CREATE_TRACK = media::BnAudioFlingerService::TRANSACTION_createTrack,
541             CREATE_RECORD = media::BnAudioFlingerService::TRANSACTION_createRecord,
542             SAMPLE_RATE = media::BnAudioFlingerService::TRANSACTION_sampleRate,
543             FORMAT = media::BnAudioFlingerService::TRANSACTION_format,
544             FRAME_COUNT = media::BnAudioFlingerService::TRANSACTION_frameCount,
545             LATENCY = media::BnAudioFlingerService::TRANSACTION_latency,
546             SET_MASTER_VOLUME = media::BnAudioFlingerService::TRANSACTION_setMasterVolume,
547             SET_MASTER_MUTE = media::BnAudioFlingerService::TRANSACTION_setMasterMute,
548             MASTER_VOLUME = media::BnAudioFlingerService::TRANSACTION_masterVolume,
549             MASTER_MUTE = media::BnAudioFlingerService::TRANSACTION_masterMute,
550             SET_STREAM_VOLUME = media::BnAudioFlingerService::TRANSACTION_setStreamVolume,
551             SET_STREAM_MUTE = media::BnAudioFlingerService::TRANSACTION_setStreamMute,
552             STREAM_VOLUME = media::BnAudioFlingerService::TRANSACTION_streamVolume,
553             STREAM_MUTE = media::BnAudioFlingerService::TRANSACTION_streamMute,
554             SET_MODE = media::BnAudioFlingerService::TRANSACTION_setMode,
555             SET_MIC_MUTE = media::BnAudioFlingerService::TRANSACTION_setMicMute,
556             GET_MIC_MUTE = media::BnAudioFlingerService::TRANSACTION_getMicMute,
557             SET_RECORD_SILENCED = media::BnAudioFlingerService::TRANSACTION_setRecordSilenced,
558             SET_PARAMETERS = media::BnAudioFlingerService::TRANSACTION_setParameters,
559             GET_PARAMETERS = media::BnAudioFlingerService::TRANSACTION_getParameters,
560             REGISTER_CLIENT = media::BnAudioFlingerService::TRANSACTION_registerClient,
561             GET_INPUTBUFFERSIZE = media::BnAudioFlingerService::TRANSACTION_getInputBufferSize,
562             OPEN_OUTPUT = media::BnAudioFlingerService::TRANSACTION_openOutput,
563             OPEN_DUPLICATE_OUTPUT = media::BnAudioFlingerService::TRANSACTION_openDuplicateOutput,
564             CLOSE_OUTPUT = media::BnAudioFlingerService::TRANSACTION_closeOutput,
565             SUSPEND_OUTPUT = media::BnAudioFlingerService::TRANSACTION_suspendOutput,
566             RESTORE_OUTPUT = media::BnAudioFlingerService::TRANSACTION_restoreOutput,
567             OPEN_INPUT = media::BnAudioFlingerService::TRANSACTION_openInput,
568             CLOSE_INPUT = media::BnAudioFlingerService::TRANSACTION_closeInput,
569             SET_VOICE_VOLUME = media::BnAudioFlingerService::TRANSACTION_setVoiceVolume,
570             GET_RENDER_POSITION = media::BnAudioFlingerService::TRANSACTION_getRenderPosition,
571             GET_INPUT_FRAMES_LOST = media::BnAudioFlingerService::TRANSACTION_getInputFramesLost,
572             NEW_AUDIO_UNIQUE_ID = media::BnAudioFlingerService::TRANSACTION_newAudioUniqueId,
573             ACQUIRE_AUDIO_SESSION_ID = media::BnAudioFlingerService::TRANSACTION_acquireAudioSessionId,
574             RELEASE_AUDIO_SESSION_ID = media::BnAudioFlingerService::TRANSACTION_releaseAudioSessionId,
575             QUERY_NUM_EFFECTS = media::BnAudioFlingerService::TRANSACTION_queryNumberEffects,
576             QUERY_EFFECT = media::BnAudioFlingerService::TRANSACTION_queryEffect,
577             GET_EFFECT_DESCRIPTOR = media::BnAudioFlingerService::TRANSACTION_getEffectDescriptor,
578             CREATE_EFFECT = media::BnAudioFlingerService::TRANSACTION_createEffect,
579             MOVE_EFFECTS = media::BnAudioFlingerService::TRANSACTION_moveEffects,
580             LOAD_HW_MODULE = media::BnAudioFlingerService::TRANSACTION_loadHwModule,
581             GET_PRIMARY_OUTPUT_SAMPLING_RATE = media::BnAudioFlingerService::TRANSACTION_getPrimaryOutputSamplingRate,
582             GET_PRIMARY_OUTPUT_FRAME_COUNT = media::BnAudioFlingerService::TRANSACTION_getPrimaryOutputFrameCount,
583             SET_LOW_RAM_DEVICE = media::BnAudioFlingerService::TRANSACTION_setLowRamDevice,
584             GET_AUDIO_PORT = media::BnAudioFlingerService::TRANSACTION_getAudioPort,
585             CREATE_AUDIO_PATCH = media::BnAudioFlingerService::TRANSACTION_createAudioPatch,
586             RELEASE_AUDIO_PATCH = media::BnAudioFlingerService::TRANSACTION_releaseAudioPatch,
587             LIST_AUDIO_PATCHES = media::BnAudioFlingerService::TRANSACTION_listAudioPatches,
588             SET_AUDIO_PORT_CONFIG = media::BnAudioFlingerService::TRANSACTION_setAudioPortConfig,
589             GET_AUDIO_HW_SYNC_FOR_SESSION = media::BnAudioFlingerService::TRANSACTION_getAudioHwSyncForSession,
590             SYSTEM_READY = media::BnAudioFlingerService::TRANSACTION_systemReady,
591             AUDIO_POLICY_READY = media::BnAudioFlingerService::TRANSACTION_audioPolicyReady,
592             FRAME_COUNT_HAL = media::BnAudioFlingerService::TRANSACTION_frameCountHAL,
593             GET_MICROPHONES = media::BnAudioFlingerService::TRANSACTION_getMicrophones,
594             SET_MASTER_BALANCE = media::BnAudioFlingerService::TRANSACTION_setMasterBalance,
595             GET_MASTER_BALANCE = media::BnAudioFlingerService::TRANSACTION_getMasterBalance,
596             SET_EFFECT_SUSPENDED = media::BnAudioFlingerService::TRANSACTION_setEffectSuspended,
597             SET_AUDIO_HAL_PIDS = media::BnAudioFlingerService::TRANSACTION_setAudioHalPids,
598             SET_VIBRATOR_INFOS = media::BnAudioFlingerService::TRANSACTION_setVibratorInfos,
599             UPDATE_SECONDARY_OUTPUTS = media::BnAudioFlingerService::TRANSACTION_updateSecondaryOutputs,
600             GET_MMAP_POLICY_INFOS = media::BnAudioFlingerService::TRANSACTION_getMmapPolicyInfos,
601             GET_AAUDIO_MIXER_BURST_COUNT = media::BnAudioFlingerService::TRANSACTION_getAAudioMixerBurstCount,
602             GET_AAUDIO_HARDWARE_BURST_MIN_USEC = media::BnAudioFlingerService::TRANSACTION_getAAudioHardwareBurstMinUsec,
603             SET_DEVICE_CONNECTED_STATE = media::BnAudioFlingerService::TRANSACTION_setDeviceConnectedState,
604             SET_SIMULATE_DEVICE_CONNECTIONS = media::BnAudioFlingerService::TRANSACTION_setSimulateDeviceConnections,
605             SET_REQUESTED_LATENCY_MODE = media::BnAudioFlingerService::TRANSACTION_setRequestedLatencyMode,
606             GET_SUPPORTED_LATENCY_MODES = media::BnAudioFlingerService::TRANSACTION_getSupportedLatencyModes,
607             SET_BLUETOOTH_VARIABLE_LATENCY_ENABLED =
608                     media::BnAudioFlingerService::TRANSACTION_setBluetoothVariableLatencyEnabled,
609             IS_BLUETOOTH_VARIABLE_LATENCY_ENABLED =
610                     media::BnAudioFlingerService::TRANSACTION_isBluetoothVariableLatencyEnabled,
611             SUPPORTS_BLUETOOTH_VARIABLE_LATENCY =
612                     media::BnAudioFlingerService::TRANSACTION_supportsBluetoothVariableLatency,
613             GET_SOUND_DOSE_INTERFACE = media::BnAudioFlingerService::TRANSACTION_getSoundDoseInterface,
614             INVALIDATE_TRACKS = media::BnAudioFlingerService::TRANSACTION_invalidateTracks,
615             GET_AUDIO_POLICY_CONFIG =
616                     media::BnAudioFlingerService::TRANSACTION_getAudioPolicyConfig,
617             GET_AUDIO_MIX_PORT = media::BnAudioFlingerService::TRANSACTION_getAudioMixPort,
618             SET_TRACKS_INTERNAL_MUTE = media::BnAudioFlingerService::TRANSACTION_setTracksInternalMute,
619             RESET_REFERENCES_FOR_TEST =
620                     media::BnAudioFlingerService::TRANSACTION_resetReferencesForTest,
621         };
622 
623     protected:
624         /**
625          * And optional hook, called on every transaction, allowing additional operations to be
626          * performed before/after the unparceling  ofthe data and dispatching to the respective
627          * method. Useful for bulk operations, such as logging or permission checks.
628          * The implementer is responsible to invoke the provided delegate function, which is the
629          * actual onTransact(), unless an error occurs.
630          * By default, this is just a pass-through to the delegate.
631          */
onTransactWrapper(TransactionCode code,const Parcel & data,uint32_t flags,const std::function<status_t ()> & delegate)632         virtual status_t onTransactWrapper(TransactionCode code,
633                                            const Parcel& data,
634                                            uint32_t flags,
635                                            const std::function<status_t()>& delegate) {
636             (void) code;
637             (void) data;
638             (void) flags;
639             return delegate();
640         }
641 
642         /**
643          * An optional hook for implementing diagnostics dumping.
644          */
dump(int fd,const Vector<String16> & args)645         virtual status_t dump(int fd, const Vector<String16>& args) {
646             (void) fd;
647             (void) args;
648             return OK;
649         }
650     };
651 
652     explicit AudioFlingerServerAdapter(
653             const sp<AudioFlingerServerAdapter::Delegate>& delegate);
654 
655     status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) override;
656     status_t dump(int fd, const Vector<String16>& args) override;
657 
658     Status createTrack(const media::CreateTrackRequest& request,
659                        media::CreateTrackResponse* _aidl_return) override;
660     Status createRecord(const media::CreateRecordRequest& request,
661                         media::CreateRecordResponse* _aidl_return) override;
662     Status sampleRate(int32_t ioHandle, int32_t* _aidl_return) override;
663     Status format(int32_t output,
664                   media::audio::common::AudioFormatDescription* _aidl_return) override;
665     Status frameCount(int32_t ioHandle, int64_t* _aidl_return) override;
666     Status latency(int32_t output, int32_t* _aidl_return) override;
667     Status setMasterVolume(float value) override;
668     Status setMasterMute(bool muted) override;
669     Status masterVolume(float* _aidl_return) override;
670     Status masterMute(bool* _aidl_return) override;
671     Status setMasterBalance(float balance) override;
672     Status getMasterBalance(float* _aidl_return) override;
673     Status setStreamVolume(media::audio::common::AudioStreamType stream,
674                            float value, int32_t output) override;
675     Status setStreamMute(media::audio::common::AudioStreamType stream, bool muted) override;
676     Status streamVolume(media::audio::common::AudioStreamType stream,
677                         int32_t output, float* _aidl_return) override;
678     Status streamMute(media::audio::common::AudioStreamType stream, bool* _aidl_return) override;
679     Status setMode(media::audio::common::AudioMode mode) override;
680     Status setMicMute(bool state) override;
681     Status getMicMute(bool* _aidl_return) override;
682     Status setRecordSilenced(int32_t portId, bool silenced) override;
683     Status setParameters(int32_t ioHandle, const std::string& keyValuePairs) override;
684     Status
685     getParameters(int32_t ioHandle, const std::string& keys, std::string* _aidl_return) override;
686     Status registerClient(const sp<media::IAudioFlingerClient>& client) override;
687     Status getInputBufferSize(int32_t sampleRate,
688                               const media::audio::common::AudioFormatDescription& format,
689                               const media::audio::common::AudioChannelLayout& channelMask,
690                               int64_t* _aidl_return) override;
691     Status openOutput(const media::OpenOutputRequest& request,
692                       media::OpenOutputResponse* _aidl_return) override;
693     Status openDuplicateOutput(int32_t output1, int32_t output2, int32_t* _aidl_return) override;
694     Status closeOutput(int32_t output) override;
695     Status suspendOutput(int32_t output) override;
696     Status restoreOutput(int32_t output) override;
697     Status openInput(const media::OpenInputRequest& request,
698                      media::OpenInputResponse* _aidl_return) override;
699     Status closeInput(int32_t input) override;
700     Status setVoiceVolume(float volume) override;
701     Status getRenderPosition(int32_t output, media::RenderPosition* _aidl_return) override;
702     Status getInputFramesLost(int32_t ioHandle, int32_t* _aidl_return) override;
703     Status newAudioUniqueId(media::AudioUniqueIdUse use, int32_t* _aidl_return) override;
704     Status acquireAudioSessionId(int32_t audioSession, int32_t pid, int32_t uid) override;
705     Status releaseAudioSessionId(int32_t audioSession, int32_t pid) override;
706     Status queryNumberEffects(int32_t* _aidl_return) override;
707     Status queryEffect(int32_t index, media::EffectDescriptor* _aidl_return) override;
708     Status getEffectDescriptor(const media::audio::common::AudioUuid& effectUUID,
709                                const media::audio::common::AudioUuid& typeUUID,
710                                int32_t preferredTypeFlag,
711                                media::EffectDescriptor* _aidl_return) override;
712     Status createEffect(const media::CreateEffectRequest& request,
713                         media::CreateEffectResponse* _aidl_return) override;
714     Status moveEffects(int32_t session, int32_t srcOutput, int32_t dstOutput) override;
715     Status setEffectSuspended(int32_t effectId, int32_t sessionId, bool suspended) override;
716     Status loadHwModule(const std::string& name, int32_t* _aidl_return) override;
717     Status getPrimaryOutputSamplingRate(int32_t* _aidl_return) override;
718     Status getPrimaryOutputFrameCount(int64_t* _aidl_return) override;
719     Status setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) override;
720     Status getAudioPort(const media::AudioPortFw& port, media::AudioPortFw* _aidl_return) override;
721     Status createAudioPatch(const media::AudioPatchFw& patch, int32_t* _aidl_return) override;
722     Status releaseAudioPatch(int32_t handle) override;
723     Status listAudioPatches(int32_t maxCount,
724                             std::vector<media::AudioPatchFw>* _aidl_return) override;
725     Status setAudioPortConfig(const media::AudioPortConfigFw& config) override;
726     Status getAudioHwSyncForSession(int32_t sessionId, int32_t* _aidl_return) override;
727     Status systemReady() override;
728     Status audioPolicyReady() override;
729     Status frameCountHAL(int32_t ioHandle, int64_t* _aidl_return) override;
730     Status getMicrophones(std::vector<media::MicrophoneInfoFw>* _aidl_return) override;
731     Status setAudioHalPids(const std::vector<int32_t>& pids) override;
732     Status setVibratorInfos(const std::vector<media::AudioVibratorInfo>& vibratorInfos) override;
733     Status updateSecondaryOutputs(
734             const std::vector<media::TrackSecondaryOutputInfo>& trackSecondaryOutputInfos) override;
735     Status getMmapPolicyInfos(
736             media::audio::common::AudioMMapPolicyType policyType,
737             std::vector<media::audio::common::AudioMMapPolicyInfo> *_aidl_return) override;
738     Status getAAudioMixerBurstCount(int32_t* _aidl_return) override;
739     Status getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) override;
740     Status setDeviceConnectedState(const media::AudioPortFw& port,
741                                    media::DeviceConnectedState state) override;
742     Status setSimulateDeviceConnections(bool enabled) override;
743     Status setRequestedLatencyMode(
744             int output, media::audio::common::AudioLatencyMode mode) override;
745     Status getSupportedLatencyModes(int output,
746             std::vector<media::audio::common::AudioLatencyMode>* _aidl_return) override;
747     Status setBluetoothVariableLatencyEnabled(bool enabled) override;
748     Status isBluetoothVariableLatencyEnabled(bool* enabled) override;
749     Status supportsBluetoothVariableLatency(bool* support) override;
750     Status getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback,
751                                  sp<media::ISoundDose>* _aidl_return) override;
752     Status invalidateTracks(const std::vector<int32_t>& portIds) override;
753     Status getAudioPolicyConfig(media::AudioPolicyConfig* _aidl_return) override;
754     Status getAudioMixPort(const media::AudioPortFw& devicePort,
755                            const media::AudioPortFw& mixPort,
756                            media::AudioPortFw* _aidl_return) override;
757     Status setTracksInternalMute(
758             const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) override;
759     Status resetReferencesForTest() override;
760 private:
761     const sp<AudioFlingerServerAdapter::Delegate> mDelegate;
762 };
763 
764 }; // namespace android
765 
766 #endif // ANDROID_IAUDIOFLINGER_H
767