1 /*
2 **
3 ** Copyright 2007, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef ANDROID_AUDIO_FLINGER_H
19 #define ANDROID_AUDIO_FLINGER_H
20 
21 #include "Configuration.h"
22 #include <atomic>
23 #include <mutex>
24 #include <chrono>
25 #include <deque>
26 #include <map>
27 #include <numeric>
28 #include <optional>
29 #include <set>
30 #include <string>
31 #include <vector>
32 #include <stdint.h>
33 #include <sys/types.h>
34 #include <limits.h>
35 
36 #include <android/media/IAudioTrackCallback.h>
37 #include <android/os/BnExternalVibrationController.h>
38 #include <android-base/macros.h>
39 
40 #include <cutils/atomic.h>
41 #include <cutils/compiler.h>
42 #include <cutils/properties.h>
43 
44 #include <media/IAudioFlinger.h>
45 #include <media/IAudioFlingerClient.h>
46 #include <media/IAudioTrack.h>
47 #include <media/AudioSystem.h>
48 #include <media/AudioTrack.h>
49 #include <media/MmapStreamInterface.h>
50 #include <media/MmapStreamCallback.h>
51 
52 #include <utils/Errors.h>
53 #include <utils/threads.h>
54 #include <utils/SortedVector.h>
55 #include <utils/TypeHelpers.h>
56 #include <utils/Vector.h>
57 
58 #include <binder/AppOpsManager.h>
59 #include <binder/BinderService.h>
60 #include <binder/IAppOpsCallback.h>
61 #include <binder/MemoryDealer.h>
62 
63 #include <system/audio.h>
64 #include <system/audio_policy.h>
65 
66 #include <media/audiohal/EffectBufferHalInterface.h>
67 #include <media/audiohal/StreamHalInterface.h>
68 #include <media/AudioBufferProvider.h>
69 #include <media/AudioContainers.h>
70 #include <media/AudioDeviceTypeAddr.h>
71 #include <media/AudioMixer.h>
72 #include <media/DeviceDescriptorBase.h>
73 #include <media/ExtendedAudioBufferProvider.h>
74 #include <media/VolumeShaper.h>
75 #include <mediautils/ServiceUtilities.h>
76 
77 #include <audio_utils/clock.h>
78 #include <audio_utils/FdToString.h>
79 #include <audio_utils/LinearMap.h>
80 #include <audio_utils/SimpleLog.h>
81 #include <audio_utils/TimestampVerifier.h>
82 
83 #include "FastCapture.h"
84 #include "FastMixer.h"
85 #include <media/nbaio/NBAIO.h>
86 #include "AudioWatchdog.h"
87 #include "AudioStreamOut.h"
88 #include "SpdifStreamOut.h"
89 #include "AudioHwDevice.h"
90 #include "NBAIO_Tee.h"
91 #include "ThreadMetrics.h"
92 #include "TrackMetrics.h"
93 
94 #include <powermanager/IPowerManager.h>
95 
96 #include <media/nblog/NBLog.h>
97 #include <private/media/AudioEffectShared.h>
98 #include <private/media/AudioTrackShared.h>
99 
100 #include <vibrator/ExternalVibration.h>
101 
102 #include "android/media/BnAudioRecord.h"
103 
104 namespace android {
105 
106 class AudioMixer;
107 class AudioBuffer;
108 class AudioResampler;
109 class DeviceHalInterface;
110 class DevicesFactoryHalCallback;
111 class DevicesFactoryHalInterface;
112 class EffectsFactoryHalInterface;
113 class FastMixer;
114 class PassthruBufferProvider;
115 class RecordBufferConverter;
116 class ServerProxy;
117 
118 // ----------------------------------------------------------------------------
119 
120 static const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3);
121 
122 #define INCLUDING_FROM_AUDIOFLINGER_H
123 
124 class AudioFlinger :
125     public BinderService<AudioFlinger>,
126     public BnAudioFlinger
127 {
128     friend class BinderService<AudioFlinger>;   // for AudioFlinger()
129 
130 public:
getServiceName()131     static const char* getServiceName() ANDROID_API { return "media.audio_flinger"; }
132 
133     virtual     status_t    dump(int fd, const Vector<String16>& args);
134 
135     // IAudioFlinger interface, in binder opcode order
136     virtual sp<IAudioTrack> createTrack(const CreateTrackInput& input,
137                                         CreateTrackOutput& output,
138                                         status_t *status);
139 
140     virtual sp<media::IAudioRecord> createRecord(const CreateRecordInput& input,
141                                                  CreateRecordOutput& output,
142                                                  status_t *status);
143 
144     virtual     uint32_t    sampleRate(audio_io_handle_t ioHandle) const;
145     virtual     audio_format_t format(audio_io_handle_t output) const;
146     virtual     size_t      frameCount(audio_io_handle_t ioHandle) const;
147     virtual     size_t      frameCountHAL(audio_io_handle_t ioHandle) const;
148     virtual     uint32_t    latency(audio_io_handle_t output) const;
149 
150     virtual     status_t    setMasterVolume(float value);
151     virtual     status_t    setMasterMute(bool muted);
152 
153     virtual     float       masterVolume() const;
154     virtual     bool        masterMute() const;
155 
156     // Balance value must be within -1.f (left only) to 1.f (right only) inclusive.
157                 status_t    setMasterBalance(float balance) override;
158                 status_t    getMasterBalance(float *balance) const override;
159 
160     virtual     status_t    setStreamVolume(audio_stream_type_t stream, float value,
161                                             audio_io_handle_t output);
162     virtual     status_t    setStreamMute(audio_stream_type_t stream, bool muted);
163 
164     virtual     float       streamVolume(audio_stream_type_t stream,
165                                          audio_io_handle_t output) const;
166     virtual     bool        streamMute(audio_stream_type_t stream) const;
167 
168     virtual     status_t    setMode(audio_mode_t mode);
169 
170     virtual     status_t    setMicMute(bool state);
171     virtual     bool        getMicMute() const;
172 
173     virtual     void        setRecordSilenced(audio_port_handle_t portId, bool silenced);
174 
175     virtual     status_t    setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
176     virtual     String8     getParameters(audio_io_handle_t ioHandle, const String8& keys) const;
177 
178     virtual     void        registerClient(const sp<IAudioFlingerClient>& client);
179 
180     virtual     size_t      getInputBufferSize(uint32_t sampleRate, audio_format_t format,
181                                                audio_channel_mask_t channelMask) const;
182 
183     virtual status_t openOutput(audio_module_handle_t module,
184                                 audio_io_handle_t *output,
185                                 audio_config_t *config,
186                                 const sp<DeviceDescriptorBase>& device,
187                                 uint32_t *latencyMs,
188                                 audio_output_flags_t flags);
189 
190     virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
191                                                   audio_io_handle_t output2);
192 
193     virtual status_t closeOutput(audio_io_handle_t output);
194 
195     virtual status_t suspendOutput(audio_io_handle_t output);
196 
197     virtual status_t restoreOutput(audio_io_handle_t output);
198 
199     virtual status_t openInput(audio_module_handle_t module,
200                                audio_io_handle_t *input,
201                                audio_config_t *config,
202                                audio_devices_t *device,
203                                const String8& address,
204                                audio_source_t source,
205                                audio_input_flags_t flags);
206 
207     virtual status_t closeInput(audio_io_handle_t input);
208 
209     virtual status_t invalidateStream(audio_stream_type_t stream);
210 
211     virtual status_t setVoiceVolume(float volume);
212 
213     virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
214                                        audio_io_handle_t output) const;
215 
216     virtual uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const;
217 
218     // This is the binder API.  For the internal API see nextUniqueId().
219     virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use);
220 
221     void acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) override;
222 
223     virtual void releaseAudioSessionId(audio_session_t audioSession, pid_t pid);
224 
225     virtual status_t queryNumberEffects(uint32_t *numEffects) const;
226 
227     virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor) const;
228 
229     virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid,
230                                          const effect_uuid_t *pTypeUuid,
231                                          uint32_t preferredTypeFlag,
232                                          effect_descriptor_t *descriptor) const;
233 
234     virtual sp<IEffect> createEffect(
235                         effect_descriptor_t *pDesc,
236                         const sp<IEffectClient>& effectClient,
237                         int32_t priority,
238                         audio_io_handle_t io,
239                         audio_session_t sessionId,
240                         const AudioDeviceTypeAddr& device,
241                         const String16& opPackageName,
242                         pid_t pid,
243                         bool probe,
244                         status_t *status /*non-NULL*/,
245                         int *id,
246                         int *enabled);
247 
248     virtual status_t moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
249                         audio_io_handle_t dstOutput);
250 
251             void setEffectSuspended(int effectId,
252                                     audio_session_t sessionId,
253                                     bool suspended) override;
254 
255     virtual audio_module_handle_t loadHwModule(const char *name);
256 
257     virtual uint32_t getPrimaryOutputSamplingRate();
258     virtual size_t getPrimaryOutputFrameCount();
259 
260     virtual status_t setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) override;
261 
262     /* List available audio ports and their attributes */
263     virtual status_t listAudioPorts(unsigned int *num_ports,
264                                     struct audio_port *ports);
265 
266     /* Get attributes for a given audio port */
267     virtual status_t getAudioPort(struct audio_port *port);
268 
269     /* Create an audio patch between several source and sink ports */
270     virtual status_t createAudioPatch(const struct audio_patch *patch,
271                                        audio_patch_handle_t *handle);
272 
273     /* Release an audio patch */
274     virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
275 
276     /* List existing audio patches */
277     virtual status_t listAudioPatches(unsigned int *num_patches,
278                                       struct audio_patch *patches);
279 
280     /* Set audio port configuration */
281     virtual status_t setAudioPortConfig(const struct audio_port_config *config);
282 
283     /* Get the HW synchronization source used for an audio session */
284     virtual audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId);
285 
286     /* Indicate JAVA services are ready (scheduling, power management ...) */
287     virtual status_t systemReady();
288 
289     virtual status_t getMicrophones(std::vector<media::MicrophoneInfo> *microphones);
290 
291     virtual status_t setAudioHalPids(const std::vector<pid_t>& pids);
292 
293     virtual     status_t    onTransact(
294                                 uint32_t code,
295                                 const Parcel& data,
296                                 Parcel* reply,
297                                 uint32_t flags);
298 
299     // end of IAudioFlinger interface
300 
301     sp<NBLog::Writer>   newWriter_l(size_t size, const char *name);
302     void                unregisterWriter(const sp<NBLog::Writer>& writer);
303     sp<EffectsFactoryHalInterface> getEffectsFactory();
304 
305     status_t openMmapStream(MmapStreamInterface::stream_direction_t direction,
306                             const audio_attributes_t *attr,
307                             audio_config_base_t *config,
308                             const AudioClient& client,
309                             audio_port_handle_t *deviceId,
310                             audio_session_t *sessionId,
311                             const sp<MmapStreamCallback>& callback,
312                             sp<MmapStreamInterface>& interface,
313                             audio_port_handle_t *handle);
314 
315     static int onExternalVibrationStart(const sp<os::ExternalVibration>& externalVibration);
316     static void onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration);
317 
318     status_t addEffectToHal(audio_port_handle_t deviceId,
319             audio_module_handle_t hwModuleId, sp<EffectHalInterface> effect);
320     status_t removeEffectFromHal(audio_port_handle_t deviceId,
321             audio_module_handle_t hwModuleId, sp<EffectHalInterface> effect);
322 
323 private:
324     // FIXME The 400 is temporarily too high until a leak of writers in media.log is fixed.
325     static const size_t kLogMemorySize = 400 * 1024;
326     sp<MemoryDealer>    mLogMemoryDealer;   // == 0 when NBLog is disabled
327     // When a log writer is unregistered, it is done lazily so that media.log can continue to see it
328     // for as long as possible.  The memory is only freed when it is needed for another log writer.
329     Vector< sp<NBLog::Writer> > mUnregisteredWriters;
330     Mutex               mUnregisteredWritersLock;
331 
332 public:
333 
334     class SyncEvent;
335 
336     typedef void (*sync_event_callback_t)(const wp<SyncEvent>& event) ;
337 
338     class SyncEvent : public RefBase {
339     public:
SyncEvent(AudioSystem::sync_event_t type,audio_session_t triggerSession,audio_session_t listenerSession,sync_event_callback_t callBack,wp<RefBase> cookie)340         SyncEvent(AudioSystem::sync_event_t type,
341                   audio_session_t triggerSession,
342                   audio_session_t listenerSession,
343                   sync_event_callback_t callBack,
344                   wp<RefBase> cookie)
345         : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession),
346           mCallback(callBack), mCookie(cookie)
347         {}
348 
~SyncEvent()349         virtual ~SyncEvent() {}
350 
trigger()351         void trigger() {
352             Mutex::Autolock _l(mLock);
353             if (mCallback) mCallback(wp<SyncEvent>(this));
354         }
isCancelled()355         bool isCancelled() const { Mutex::Autolock _l(mLock); return (mCallback == NULL); }
cancel()356         void cancel() { Mutex::Autolock _l(mLock); mCallback = NULL; }
type()357         AudioSystem::sync_event_t type() const { return mType; }
triggerSession()358         audio_session_t triggerSession() const { return mTriggerSession; }
listenerSession()359         audio_session_t listenerSession() const { return mListenerSession; }
cookie()360         wp<RefBase> cookie() const { return mCookie; }
361 
362     private:
363           const AudioSystem::sync_event_t mType;
364           const audio_session_t mTriggerSession;
365           const audio_session_t mListenerSession;
366           sync_event_callback_t mCallback;
367           const wp<RefBase> mCookie;
368           mutable Mutex mLock;
369     };
370 
371     sp<SyncEvent> createSyncEvent(AudioSystem::sync_event_t type,
372                                         audio_session_t triggerSession,
373                                         audio_session_t listenerSession,
374                                         sync_event_callback_t callBack,
375                                         const wp<RefBase>& cookie);
376 
btNrecIsOff()377     bool        btNrecIsOff() const { return mBtNrecIsOff.load(); }
378 
379 
380 private:
381 
getMode()382                audio_mode_t getMode() const { return mMode; }
383 
384                             AudioFlinger() ANDROID_API;
385     virtual                 ~AudioFlinger();
386 
387     // call in any IAudioFlinger method that accesses mPrimaryHardwareDev
initCheck()388     status_t                initCheck() const { return mPrimaryHardwareDev == NULL ?
389                                                         NO_INIT : NO_ERROR; }
390 
391     // RefBase
392     virtual     void        onFirstRef();
393 
394     AudioHwDevice*          findSuitableHwDev_l(audio_module_handle_t module,
395                                                 audio_devices_t deviceType);
396 
397     // Set kEnableExtendedChannels to true to enable greater than stereo output
398     // for the MixerThread and device sink.  Number of channels allowed is
399     // FCC_2 <= channels <= AudioMixer::MAX_NUM_CHANNELS.
400     static const bool kEnableExtendedChannels = true;
401 
402     // Returns true if channel mask is permitted for the PCM sink in the MixerThread
isValidPcmSinkChannelMask(audio_channel_mask_t channelMask)403     static inline bool isValidPcmSinkChannelMask(audio_channel_mask_t channelMask) {
404         switch (audio_channel_mask_get_representation(channelMask)) {
405         case AUDIO_CHANNEL_REPRESENTATION_POSITION: {
406             // Haptic channel mask is only applicable for channel position mask.
407             const uint32_t channelCount = audio_channel_count_from_out_mask(
408                     channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL);
409             const uint32_t maxChannelCount = kEnableExtendedChannels
410                     ? AudioMixer::MAX_NUM_CHANNELS : FCC_2;
411             if (channelCount < FCC_2 // mono is not supported at this time
412                     || channelCount > maxChannelCount) {
413                 return false;
414             }
415             // check that channelMask is the "canonical" one we expect for the channelCount.
416             return audio_channel_position_mask_is_out_canonical(channelMask);
417             }
418         case AUDIO_CHANNEL_REPRESENTATION_INDEX:
419             if (kEnableExtendedChannels) {
420                 const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
421                 if (channelCount >= FCC_2 // mono is not supported at this time
422                         && channelCount <= AudioMixer::MAX_NUM_CHANNELS) {
423                     return true;
424                 }
425             }
426             return false;
427         default:
428             return false;
429         }
430     }
431 
432     // Set kEnableExtendedPrecision to true to use extended precision in MixerThread
433     static const bool kEnableExtendedPrecision = true;
434 
435     // Returns true if format is permitted for the PCM sink in the MixerThread
isValidPcmSinkFormat(audio_format_t format)436     static inline bool isValidPcmSinkFormat(audio_format_t format) {
437         switch (format) {
438         case AUDIO_FORMAT_PCM_16_BIT:
439             return true;
440         case AUDIO_FORMAT_PCM_FLOAT:
441         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
442         case AUDIO_FORMAT_PCM_32_BIT:
443         case AUDIO_FORMAT_PCM_8_24_BIT:
444             return kEnableExtendedPrecision;
445         default:
446             return false;
447         }
448     }
449 
450     // standby delay for MIXER and DUPLICATING playback threads is read from property
451     // ro.audio.flinger_standbytime_ms or defaults to kDefaultStandbyTimeInNsecs
452     static nsecs_t          mStandbyTimeInNsecs;
453 
454     // incremented by 2 when screen state changes, bit 0 == 1 means "off"
455     // AudioFlinger::setParameters() updates, other threads read w/o lock
456     static uint32_t         mScreenState;
457 
458     // Internal dump utilities.
459     static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
460     static bool dumpTryLock(Mutex& mutex);
461     void dumpPermissionDenial(int fd, const Vector<String16>& args);
462     void dumpClients(int fd, const Vector<String16>& args);
463     void dumpInternals(int fd, const Vector<String16>& args);
464 
465     SimpleLog mThreadLog{16}; // 16 Thread history limit
466 
467     class ThreadBase;
468     void dumpToThreadLog_l(const sp<ThreadBase> &thread);
469 
470     // --- Client ---
471     class Client : public RefBase {
472     public:
473                             Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
474         virtual             ~Client();
475         sp<MemoryDealer>    heap() const;
pid()476         pid_t               pid() const { return mPid; }
audioFlinger()477         sp<AudioFlinger>    audioFlinger() const { return mAudioFlinger; }
478 
479     private:
480         DISALLOW_COPY_AND_ASSIGN(Client);
481 
482         const sp<AudioFlinger> mAudioFlinger;
483               sp<MemoryDealer> mMemoryDealer;
484         const pid_t         mPid;
485     };
486 
487     // --- Notification Client ---
488     class NotificationClient : public IBinder::DeathRecipient {
489     public:
490                             NotificationClient(const sp<AudioFlinger>& audioFlinger,
491                                                 const sp<IAudioFlingerClient>& client,
492                                                 pid_t pid,
493                                                 uid_t uid);
494         virtual             ~NotificationClient();
495 
audioFlingerClient()496                 sp<IAudioFlingerClient> audioFlingerClient() const { return mAudioFlingerClient; }
getPid()497                 pid_t getPid() const { return mPid; }
getUid()498                 uid_t getUid() const { return mUid; }
499 
500                 // IBinder::DeathRecipient
501                 virtual     void        binderDied(const wp<IBinder>& who);
502 
503     private:
504         DISALLOW_COPY_AND_ASSIGN(NotificationClient);
505 
506         const sp<AudioFlinger>  mAudioFlinger;
507         const pid_t             mPid;
508         const uid_t             mUid;
509         const sp<IAudioFlingerClient> mAudioFlingerClient;
510     };
511 
512     // --- MediaLogNotifier ---
513     // Thread in charge of notifying MediaLogService to start merging.
514     // Receives requests from AudioFlinger's binder activity. It is used to reduce the amount of
515     // binder calls to MediaLogService in case of bursts of AudioFlinger binder calls.
516     class MediaLogNotifier : public Thread {
517     public:
518         MediaLogNotifier();
519 
520         // Requests a MediaLogService notification. It's ignored if there has recently been another
521         void requestMerge();
522     private:
523         // Every iteration blocks waiting for a request, then interacts with MediaLogService to
524         // start merging.
525         // As every MediaLogService binder call is expensive, once it gets a request it ignores the
526         // following ones for a period of time.
527         virtual bool threadLoop() override;
528 
529         bool mPendingRequests;
530 
531         // Mutex and condition variable around mPendingRequests' value
532         Mutex       mMutex;
533         Condition   mCond;
534 
535         // Duration of the sleep period after a processed request
536         static const int kPostTriggerSleepPeriod = 1000000;
537     };
538 
539     const sp<MediaLogNotifier> mMediaLogNotifier;
540 
541     // This is a helper that is called during incoming binder calls.
542     void requestLogMerge();
543 
544     class TrackHandle;
545     class RecordHandle;
546     class RecordThread;
547     class PlaybackThread;
548     class MixerThread;
549     class DirectOutputThread;
550     class OffloadThread;
551     class DuplicatingThread;
552     class AsyncCallbackThread;
553     class Track;
554     class RecordTrack;
555     class EffectBase;
556     class EffectModule;
557     class EffectHandle;
558     class EffectChain;
559     class DeviceEffectProxy;
560     class DeviceEffectManager;
561     class PatchPanel;
562     class DeviceEffectManagerCallback;
563 
564     struct AudioStreamIn;
565     struct TeePatch;
566     using TeePatches = std::vector<TeePatch>;
567 
568 
569     struct  stream_type_t {
stream_type_tstream_type_t570         stream_type_t()
571             :   volume(1.0f),
572                 mute(false)
573         {
574         }
575         float       volume;
576         bool        mute;
577     };
578 
579     // Abstraction for the Audio Source for the RecordThread (HAL or PassthruPatchRecord).
580     struct Source
581     {
582         virtual ~Source() = default;
583         // The following methods have the same signatures as in StreamHalInterface.
584         virtual status_t read(void *buffer, size_t bytes, size_t *read) = 0;
585         virtual status_t getCapturePosition(int64_t *frames, int64_t *time) = 0;
586         virtual status_t standby() = 0;
587     };
588 
589     // --- PlaybackThread ---
590 #ifdef FLOAT_EFFECT_CHAIN
591 #define EFFECT_BUFFER_FORMAT AUDIO_FORMAT_PCM_FLOAT
592 using effect_buffer_t = float;
593 #else
594 #define EFFECT_BUFFER_FORMAT AUDIO_FORMAT_PCM_16_BIT
595 using effect_buffer_t = int16_t;
596 #endif
597 
598 #include "Threads.h"
599 
600 #include "PatchPanel.h"
601 
602 #include "Effects.h"
603 
604 #include "DeviceEffectManager.h"
605 
606     // Find io handle by session id.
607     // Preference is given to an io handle with a matching effect chain to session id.
608     // If none found, AUDIO_IO_HANDLE_NONE is returned.
609     template <typename T>
findIoHandleBySessionId_l(audio_session_t sessionId,const T & threads)610     static audio_io_handle_t findIoHandleBySessionId_l(
611             audio_session_t sessionId, const T& threads) {
612         audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
613 
614         for (size_t i = 0; i < threads.size(); i++) {
615             const uint32_t sessionType = threads.valueAt(i)->hasAudioSession(sessionId);
616             if (sessionType != 0) {
617                 io = threads.keyAt(i);
618                 if ((sessionType & AudioFlinger::ThreadBase::EFFECT_SESSION) != 0) {
619                     break; // effect chain here.
620                 }
621             }
622         }
623         return io;
624     }
625 
626     // server side of the client's IAudioTrack
627     class TrackHandle : public android::BnAudioTrack {
628     public:
629         explicit            TrackHandle(const sp<PlaybackThread::Track>& track);
630         virtual             ~TrackHandle();
631         virtual sp<IMemory> getCblk() const;
632         virtual status_t    start();
633         virtual void        stop();
634         virtual void        flush();
635         virtual void        pause();
636         virtual status_t    attachAuxEffect(int effectId);
637         virtual status_t    setParameters(const String8& keyValuePairs);
638         virtual status_t    selectPresentation(int presentationId, int programId);
639         virtual media::VolumeShaper::Status applyVolumeShaper(
640                 const sp<media::VolumeShaper::Configuration>& configuration,
641                 const sp<media::VolumeShaper::Operation>& operation) override;
642         virtual sp<media::VolumeShaper::State> getVolumeShaperState(int id) override;
643         virtual status_t    getTimestamp(AudioTimestamp& timestamp);
644         virtual void        signal(); // signal playback thread for a change in control block
645 
646         virtual status_t onTransact(
647             uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
648 
649     private:
650         const sp<PlaybackThread::Track> mTrack;
651     };
652 
653     // server side of the client's IAudioRecord
654     class RecordHandle : public android::media::BnAudioRecord {
655     public:
656         explicit RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
657         virtual             ~RecordHandle();
658         virtual binder::Status    start(int /*AudioSystem::sync_event_t*/ event,
659                 int /*audio_session_t*/ triggerSession);
660         virtual binder::Status   stop();
661         virtual binder::Status   getActiveMicrophones(
662                 std::vector<media::MicrophoneInfo>* activeMicrophones);
663         virtual binder::Status   setPreferredMicrophoneDirection(
664                 int /*audio_microphone_direction_t*/ direction);
665         virtual binder::Status   setPreferredMicrophoneFieldDimension(float zoom);
666 
667     private:
668         const sp<RecordThread::RecordTrack> mRecordTrack;
669 
670         // for use from destructor
671         void                stop_nonvirtual();
672     };
673 
674     // Mmap stream control interface implementation. Each MmapThreadHandle controls one
675     // MmapPlaybackThread or MmapCaptureThread instance.
676     class MmapThreadHandle : public MmapStreamInterface {
677     public:
678         explicit            MmapThreadHandle(const sp<MmapThread>& thread);
679         virtual             ~MmapThreadHandle();
680 
681         // MmapStreamInterface virtuals
682         virtual status_t createMmapBuffer(int32_t minSizeFrames,
683                                           struct audio_mmap_buffer_info *info);
684         virtual status_t getMmapPosition(struct audio_mmap_position *position);
685         virtual status_t start(const AudioClient& client,
686                                const audio_attributes_t *attr,
687                                audio_port_handle_t *handle);
688         virtual status_t stop(audio_port_handle_t handle);
689         virtual status_t standby();
690 
691     private:
692         const sp<MmapThread> mThread;
693     };
694 
695               ThreadBase *checkThread_l(audio_io_handle_t ioHandle) const;
696               PlaybackThread *checkPlaybackThread_l(audio_io_handle_t output) const;
697               MixerThread *checkMixerThread_l(audio_io_handle_t output) const;
698               RecordThread *checkRecordThread_l(audio_io_handle_t input) const;
699               MmapThread *checkMmapThread_l(audio_io_handle_t io) const;
700               VolumeInterface *getVolumeInterface_l(audio_io_handle_t output) const;
701               Vector <VolumeInterface *> getAllVolumeInterfaces_l() const;
702 
703               sp<ThreadBase> openInput_l(audio_module_handle_t module,
704                                            audio_io_handle_t *input,
705                                            audio_config_t *config,
706                                            audio_devices_t device,
707                                            const String8& address,
708                                            audio_source_t source,
709                                            audio_input_flags_t flags,
710                                            audio_devices_t outputDevice,
711                                            const String8& outputDeviceAddress);
712               sp<ThreadBase> openOutput_l(audio_module_handle_t module,
713                                           audio_io_handle_t *output,
714                                           audio_config_t *config,
715                                           audio_devices_t deviceType,
716                                           const String8& address,
717                                           audio_output_flags_t flags);
718 
719               void closeOutputFinish(const sp<PlaybackThread>& thread);
720               void closeInputFinish(const sp<RecordThread>& thread);
721 
722               // no range check, AudioFlinger::mLock held
streamMute_l(audio_stream_type_t stream)723               bool streamMute_l(audio_stream_type_t stream) const
724                                 { return mStreamTypes[stream].mute; }
725               void ioConfigChanged(audio_io_config_event event,
726                                    const sp<AudioIoDescriptor>& ioDesc,
727                                    pid_t pid = 0);
728 
729               // Allocate an audio_unique_id_t.
730               // Specific types are audio_io_handle_t, audio_session_t, effect ID (int),
731               // audio_module_handle_t, and audio_patch_handle_t.
732               // They all share the same ID space, but the namespaces are actually independent
733               // because there are separate KeyedVectors for each kind of ID.
734               // The return value is cast to the specific type depending on how the ID will be used.
735               // FIXME This API does not handle rollover to zero (for unsigned IDs),
736               //       or from positive to negative (for signed IDs).
737               //       Thus it may fail by returning an ID of the wrong sign,
738               //       or by returning a non-unique ID.
739               // This is the internal API.  For the binder API see newAudioUniqueId().
740               audio_unique_id_t nextUniqueId(audio_unique_id_use_t use);
741 
742               status_t moveEffectChain_l(audio_session_t sessionId,
743                                      PlaybackThread *srcThread,
744                                      PlaybackThread *dstThread);
745 
746               status_t moveAuxEffectToIo(int EffectId,
747                                          const sp<PlaybackThread>& dstThread,
748                                          sp<PlaybackThread> *srcThread);
749 
750               // return thread associated with primary hardware device, or NULL
751               PlaybackThread *primaryPlaybackThread_l() const;
752               DeviceTypeSet primaryOutputDevice_l() const;
753 
754               // return the playback thread with smallest HAL buffer size, and prefer fast
755               PlaybackThread *fastPlaybackThread_l() const;
756 
757               sp<ThreadBase> getEffectThread_l(audio_session_t sessionId, int effectId);
758 
759 
760                 void        removeClient_l(pid_t pid);
761                 void        removeNotificationClient(pid_t pid);
762                 bool isNonOffloadableGlobalEffectEnabled_l();
763                 void onNonOffloadableGlobalEffectEnable();
764                 bool isSessionAcquired_l(audio_session_t audioSession);
765 
766                 // Store an effect chain to mOrphanEffectChains keyed vector.
767                 // Called when a thread exits and effects are still attached to it.
768                 // If effects are later created on the same session, they will reuse the same
769                 // effect chain and same instances in the effect library.
770                 // return ALREADY_EXISTS if a chain with the same session already exists in
771                 // mOrphanEffectChains. Note that this should never happen as there is only one
772                 // chain for a given session and it is attached to only one thread at a time.
773                 status_t        putOrphanEffectChain_l(const sp<EffectChain>& chain);
774                 // Get an effect chain for the specified session in mOrphanEffectChains and remove
775                 // it if found. Returns 0 if not found (this is the most common case).
776                 sp<EffectChain> getOrphanEffectChain_l(audio_session_t session);
777                 // Called when the last effect handle on an effect instance is removed. If this
778                 // effect belongs to an effect chain in mOrphanEffectChains, the chain is updated
779                 // and removed from mOrphanEffectChains if it does not contain any effect.
780                 // Return true if the effect was found in mOrphanEffectChains, false otherwise.
781                 bool            updateOrphanEffectChains(const sp<EffectModule>& effect);
782 
783                 std::vector< sp<EffectModule> > purgeStaleEffects_l();
784 
785                 void broacastParametersToRecordThreads_l(const String8& keyValuePairs);
786                 void updateOutDevicesForRecordThreads_l(const DeviceDescriptorBaseVector& devices);
787                 void forwardParametersToDownstreamPatches_l(
788                         audio_io_handle_t upStream, const String8& keyValuePairs,
789                         std::function<bool(const sp<PlaybackThread>&)> useThread = nullptr);
790 
791     // AudioStreamIn is immutable, so their fields are const.
792     // For emphasis, we could also make all pointers to them be "const *",
793     // but that would clutter the code unnecessarily.
794 
795     struct AudioStreamIn : public Source {
796         AudioHwDevice* const audioHwDev;
797         sp<StreamInHalInterface> stream;
798         audio_input_flags_t flags;
799 
hwDevAudioStreamIn800         sp<DeviceHalInterface> hwDev() const { return audioHwDev->hwDevice(); }
801 
AudioStreamInAudioStreamIn802         AudioStreamIn(AudioHwDevice *dev, sp<StreamInHalInterface> in, audio_input_flags_t flags) :
803             audioHwDev(dev), stream(in), flags(flags) {}
readAudioStreamIn804         status_t read(void *buffer, size_t bytes, size_t *read) override {
805             return stream->read(buffer, bytes, read);
806         }
getCapturePositionAudioStreamIn807         status_t getCapturePosition(int64_t *frames, int64_t *time) override {
808             return stream->getCapturePosition(frames, time);
809         }
standbyAudioStreamIn810         status_t standby() override { return stream->standby(); }
811     };
812 
813     struct TeePatch {
814         sp<RecordThread::PatchRecord> patchRecord;
815         sp<PlaybackThread::PatchTrack> patchTrack;
816     };
817 
818     // for mAudioSessionRefs only
819     struct AudioSessionRef {
AudioSessionRefAudioSessionRef820         AudioSessionRef(audio_session_t sessionid, pid_t pid, uid_t uid) :
821             mSessionid(sessionid), mPid(pid), mUid(uid), mCnt(1) {}
822         const audio_session_t mSessionid;
823         const pid_t mPid;
824         const uid_t mUid;
825         int         mCnt;
826     };
827 
828     mutable     Mutex                               mLock;
829                 // protects mClients and mNotificationClients.
830                 // must be locked after mLock and ThreadBase::mLock if both must be locked
831                 // avoids acquiring AudioFlinger::mLock from inside thread loop.
832     mutable     Mutex                               mClientLock;
833                 // protected by mClientLock
834                 DefaultKeyedVector< pid_t, wp<Client> >     mClients;   // see ~Client()
835 
836                 mutable     Mutex                   mHardwareLock;
837                 // NOTE: If both mLock and mHardwareLock mutexes must be held,
838                 // always take mLock before mHardwareLock
839 
840                 // guarded by mHardwareLock
841                 AudioHwDevice* mPrimaryHardwareDev;
842                 DefaultKeyedVector<audio_module_handle_t, AudioHwDevice*>  mAudioHwDevs;
843 
844                 // These two fields are immutable after onFirstRef(), so no lock needed to access
845                 sp<DevicesFactoryHalInterface> mDevicesFactoryHal;
846                 sp<DevicesFactoryHalCallback> mDevicesFactoryHalCallback;
847 
848     // for dump, indicates which hardware operation is currently in progress (but not stream ops)
849     enum hardware_call_state {
850         AUDIO_HW_IDLE = 0,              // no operation in progress
851         AUDIO_HW_INIT,                  // init_check
852         AUDIO_HW_OUTPUT_OPEN,           // open_output_stream
853         AUDIO_HW_OUTPUT_CLOSE,          // unused
854         AUDIO_HW_INPUT_OPEN,            // unused
855         AUDIO_HW_INPUT_CLOSE,           // unused
856         AUDIO_HW_STANDBY,               // unused
857         AUDIO_HW_SET_MASTER_VOLUME,     // set_master_volume
858         AUDIO_HW_GET_ROUTING,           // unused
859         AUDIO_HW_SET_ROUTING,           // unused
860         AUDIO_HW_GET_MODE,              // unused
861         AUDIO_HW_SET_MODE,              // set_mode
862         AUDIO_HW_GET_MIC_MUTE,          // get_mic_mute
863         AUDIO_HW_SET_MIC_MUTE,          // set_mic_mute
864         AUDIO_HW_SET_VOICE_VOLUME,      // set_voice_volume
865         AUDIO_HW_SET_PARAMETER,         // set_parameters
866         AUDIO_HW_GET_INPUT_BUFFER_SIZE, // get_input_buffer_size
867         AUDIO_HW_GET_MASTER_VOLUME,     // get_master_volume
868         AUDIO_HW_GET_PARAMETER,         // get_parameters
869         AUDIO_HW_SET_MASTER_MUTE,       // set_master_mute
870         AUDIO_HW_GET_MASTER_MUTE,       // get_master_mute
871         AUDIO_HW_GET_MICROPHONES,       // getMicrophones
872     };
873 
874     mutable     hardware_call_state                 mHardwareStatus;    // for dump only
875 
876 
877                 DefaultKeyedVector< audio_io_handle_t, sp<PlaybackThread> >  mPlaybackThreads;
878                 stream_type_t                       mStreamTypes[AUDIO_STREAM_CNT];
879 
880                 // member variables below are protected by mLock
881                 float                               mMasterVolume;
882                 bool                                mMasterMute;
883                 float                               mMasterBalance = 0.f;
884                 // end of variables protected by mLock
885 
886                 DefaultKeyedVector< audio_io_handle_t, sp<RecordThread> >    mRecordThreads;
887 
888                 // protected by mClientLock
889                 DefaultKeyedVector< pid_t, sp<NotificationClient> >    mNotificationClients;
890 
891                 // updated by atomic_fetch_add_explicit
892                 volatile atomic_uint_fast32_t       mNextUniqueIds[AUDIO_UNIQUE_ID_USE_MAX];
893 
894                 audio_mode_t                        mMode;
895                 std::atomic_bool                    mBtNrecIsOff;
896 
897                 // protected by mLock
898                 Vector<AudioSessionRef*> mAudioSessionRefs;
899 
900                 float       masterVolume_l() const;
901                 float       getMasterBalance_l() const;
902                 bool        masterMute_l() const;
903                 audio_module_handle_t loadHwModule_l(const char *name);
904 
905                 Vector < sp<SyncEvent> > mPendingSyncEvents; // sync events awaiting for a session
906                                                              // to be created
907 
908                 // Effect chains without a valid thread
909                 DefaultKeyedVector< audio_session_t , sp<EffectChain> > mOrphanEffectChains;
910 
911                 // list of sessions for which a valid HW A/V sync ID was retrieved from the HAL
912                 DefaultKeyedVector< audio_session_t , audio_hw_sync_t >mHwAvSyncIds;
913 
914                 // list of MMAP stream control threads. Those threads allow for wake lock, routing
915                 // and volume control for activity on the associated MMAP stream at the HAL.
916                 // Audio data transfer is directly handled by the client creating the MMAP stream
917                 DefaultKeyedVector< audio_io_handle_t, sp<MmapThread> >  mMmapThreads;
918 
919 private:
920     sp<Client>  registerPid(pid_t pid);    // always returns non-0
921 
922     // for use from destructor
923     status_t    closeOutput_nonvirtual(audio_io_handle_t output);
924     void        closeThreadInternal_l(const sp<PlaybackThread>& thread);
925     status_t    closeInput_nonvirtual(audio_io_handle_t input);
926     void        closeThreadInternal_l(const sp<RecordThread>& thread);
927     void        setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId);
928 
929     status_t    checkStreamType(audio_stream_type_t stream) const;
930 
931     void        filterReservedParameters(String8& keyValuePairs, uid_t callingUid);
932     void        logFilteredParameters(size_t originalKVPSize, const String8& originalKVPs,
933                                       size_t rejectedKVPSize, const String8& rejectedKVPs,
934                                       uid_t callingUid);
935 
936 public:
937     // These methods read variables atomically without mLock,
938     // though the variables are updated with mLock.
isLowRamDevice()939     bool    isLowRamDevice() const { return mIsLowRamDevice; }
940     size_t getClientSharedHeapSize() const;
941 
942 private:
943     std::atomic<bool> mIsLowRamDevice;
944     bool    mIsDeviceTypeKnown;
945     int64_t mTotalMemory;
946     std::atomic<size_t> mClientSharedHeapSize;
947     static constexpr size_t kMinimumClientSharedHeapSizeBytes = 1024 * 1024; // 1MB
948 
949     nsecs_t mGlobalEffectEnableTime;  // when a global effect was last enabled
950 
951     // protected by mLock
952     PatchPanel mPatchPanel;
953     sp<EffectsFactoryHalInterface> mEffectsFactoryHal;
954 
955     DeviceEffectManager mDeviceEffectManager;
956 
957     bool       mSystemReady;
958 
959     mediautils::UidInfo mUidInfo;
960 
961     SimpleLog  mRejectedSetParameterLog;
962     SimpleLog  mAppSetParameterLog;
963     SimpleLog  mSystemSetParameterLog;
964 
965     static inline constexpr const char *mMetricsId = AMEDIAMETRICS_KEY_AUDIO_FLINGER;
966 };
967 
968 #undef INCLUDING_FROM_AUDIOFLINGER_H
969 
970 std::string formatToString(audio_format_t format);
971 std::string inputFlagsToString(audio_input_flags_t flags);
972 std::string outputFlagsToString(audio_output_flags_t flags);
973 std::string devicesToString(audio_devices_t devices);
974 const char *sourceToString(audio_source_t source);
975 
976 // ----------------------------------------------------------------------------
977 
978 } // namespace android
979 
980 #endif // ANDROID_AUDIO_FLINGER_H
981