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