AudioFlinger.h 38 KB

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