199e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten/*
265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Copyright 2007, The Android Open Source Project
465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Licensed under the Apache License, Version 2.0 (the "License");
665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** you may not use this file except in compliance with the License.
765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** You may obtain a copy of the License at
865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**     http://www.apache.org/licenses/LICENSE-2.0
1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Unless required by applicable law or agreed to in writing, software
1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** distributed under the License is distributed on an "AS IS" BASIS,
1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** See the License for the specific language governing permissions and
1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** limitations under the License.
1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian*/
1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#ifndef ANDROID_AUDIO_FLINGER_H
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define ANDROID_AUDIO_FLINGER_H
2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
215b17c0b5b418c340d3e5201a72ee8c88c1755355Glenn Kasten#include "Configuration.h"
222148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung#include <deque>
23dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung#include <map>
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdint.h>
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/types.h>
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <limits.h>
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
28e762be91c3280d837b1d48455cba90459ced7511Mathias Agopian#include <cutils/compiler.h>
29f28bcf5c057dd9738a09f3cb367cf0b44087135dAndy Hung#include <cutils/properties.h>
30e762be91c3280d837b1d48455cba90459ced7511Mathias Agopian
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <media/IAudioFlinger.h>
3265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <media/IAudioFlingerClient.h>
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <media/IAudioTrack.h>
3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <media/IAudioRecord.h>
35335787fe43596f38ea2fa50b24c54d0823a3fb1dGlenn Kasten#include <media/AudioSystem.h>
364ff14bae91075eb274eb1c2975982358946e7e63John Grossman#include <media/AudioTrack.h>
37fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent#include <media/MmapStreamInterface.h>
38fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent#include <media/MmapStreamCallback.h>
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Atomic.h>
4165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Errors.h>
4265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/threads.h>
4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/SortedVector.h>
44799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin#include <utils/TypeHelpers.h>
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Vector.h>
4665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
475462fc9a38fa8c9dff434cd53fa5fb1782ae3042Mathias Agopian#include <binder/BinderService.h>
485462fc9a38fa8c9dff434cd53fa5fb1782ae3042Mathias Agopian#include <binder/MemoryDealer.h>
495462fc9a38fa8c9dff434cd53fa5fb1782ae3042Mathias Agopian
5064760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
5161a4fac2e3ec271241e3a4f405d7357b7f6ca4c2Mikhail Naganov#include <system/audio_policy.h>
5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
53022b9953153bdb1984f0abb17d21ef8c1826ad49Mikhail Naganov#include <media/audiohal/EffectBufferHalInterface.h>
54a0c91339814f37ea78365afb436c9f3d1f0a0090Mikhail Naganov#include <media/audiohal/StreamHalInterface.h>
552dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0Glenn Kasten#include <media/AudioBufferProvider.h>
56068561c8e84569d51df2adbbb53b56fdfd09c06bAndy Hung#include <media/AudioMixer.h>
572dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0Glenn Kasten#include <media/ExtendedAudioBufferProvider.h>
58068561c8e84569d51df2adbbb53b56fdfd09c06bAndy Hung#include <media/LinearMap.h>
599fc8b5cd4a64ef07e84c69112461324d5c13a0b0Andy Hung#include <media/VolumeShaper.h>
606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
61293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung#include <audio_utils/SimpleLog.h>
62293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung
636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#include "FastCapture.h"
6458912562617941964939a4182cda71eaeb153d4bGlenn Kasten#include "FastMixer.h"
652dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0Glenn Kasten#include <media/nbaio/NBAIO.h>
66c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten#include "AudioWatchdog.h"
67062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include "AudioStreamOut.h"
68062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include "SpdifStreamOut.h"
69062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include "AudioHwDevice.h"
7065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
71feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent#include <powermanager/IPowerManager.h>
72feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
739e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten#include <media/nbaio/NBLog.h>
749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#include <private/media/AudioTrackShared.h>
759e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
7665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
7765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7801d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kastenstruct audio_track_cblk_t;
7901d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kastenstruct effect_param_cblk_t;
8065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass AudioMixer;
8165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass AudioBuffer;
8265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass AudioResampler;
83e4f1f63a2c54ee8687ad8cca18df0f6639ad7c81Mikhail Naganovclass DeviceHalInterface;
84e4f1f63a2c54ee8687ad8cca18df0f6639ad7c81Mikhail Naganovclass DevicesFactoryHalInterface;
854a3d5c23f79189eb7ab9f31c440c7da5b15947a2Mikhail Naganovclass EffectsFactoryHalInterface;
8658912562617941964939a4182cda71eaeb153d4bGlenn Kastenclass FastMixer;
87d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hungclass PassthruBufferProvider;
888981605d43e24c46d395acb5f145b99589d45917Andy Hungclass RecordBufferConverter;
89e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kastenclass ServerProxy;
9065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
9265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
934ff14bae91075eb274eb1c2975982358946e7e63John Grossmanstatic const nsecs_t kDefaultStandbyTimeInNsecs = seconds(3);
9465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
95da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent
96da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent// Max shared memory size for audio tracks and audio records per client process
97da73b6c7474aaa5616f0214e238776f12717f32bEric Laurentstatic const size_t kClientSharedHeapSizeBytes = 1024*1024;
98da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent// Shared memory size multiplier for non low ram devices
99da73b6c7474aaa5616f0214e238776f12717f32bEric Laurentstatic const size_t kClientSharedHeapSizeMultiplier = 4;
100da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent
10181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define INCLUDING_FROM_AUDIOFLINGER_H
10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1035462fc9a38fa8c9dff434cd53fa5fb1782ae3042Mathias Agopianclass AudioFlinger :
1045462fc9a38fa8c9dff434cd53fa5fb1782ae3042Mathias Agopian    public BinderService<AudioFlinger>,
1055462fc9a38fa8c9dff434cd53fa5fb1782ae3042Mathias Agopian    public BnAudioFlinger
10665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1071998661fdb6b0b5ae103e047e3d653c5da1b99e3Glenn Kasten    friend class BinderService<AudioFlinger>;   // for AudioFlinger()
108fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent
10965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
110e762be91c3280d837b1d48455cba90459ced7511Mathias Agopian    static const char* getServiceName() ANDROID_API { return "media.audio_flinger"; }
11165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
11265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual     status_t    dump(int fd, const Vector<String16>& args);
11365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1142f732eb768004c6362fae8a02c60b69c9400b032Glenn Kasten    // IAudioFlinger interface, in binder opcode order
11565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual sp<IAudioTrack> createTrack(
116fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten                                audio_stream_type_t streamType,
11765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                uint32_t sampleRate,
11858f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten                                audio_format_t format,
119dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten                                audio_channel_mask_t channelMask,
12074935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                                size_t *pFrameCount,
121050677873c10d4da308ac222f8533c96cca3207eEric Laurent                                audio_output_flags_t *flags,
12265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                const sp<IMemory>& sharedBuffer,
12372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                audio_io_handle_t output,
1249ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George                                pid_t pid,
1253acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten                                pid_t tid,
126d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                audio_session_t *sessionId,
127462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                int clientUid,
12820b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                status_t *status /*non-NULL*/,
12920b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                audio_port_handle_t portId);
13065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1312f732eb768004c6362fae8a02c60b69c9400b032Glenn Kasten    virtual sp<IAudioRecord> openRecord(
1322f732eb768004c6362fae8a02c60b69c9400b032Glenn Kasten                                audio_io_handle_t input,
1332f732eb768004c6362fae8a02c60b69c9400b032Glenn Kasten                                uint32_t sampleRate,
1342f732eb768004c6362fae8a02c60b69c9400b032Glenn Kasten                                audio_format_t format,
135dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten                                audio_channel_mask_t channelMask,
136be71aa29a3c86d2e01cd17839d2a72ab09a1bce5Svet Ganov                                const String16& opPackageName,
13774935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                                size_t *pFrameCount,
138050677873c10d4da308ac222f8533c96cca3207eEric Laurent                                audio_input_flags_t *flags,
1399ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George                                pid_t pid,
1401879fff068422852c1483dcf8365c2ff0e2fadfcGlenn Kasten                                pid_t tid,
1414cb668392ee0433462251afbee109405c6efacc8Jean-Michel Trivi                                int clientUid,
142d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                audio_session_t *sessionId,
1437df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten                                size_t *notificationFrames,
144d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten                                sp<IMemory>& cblk,
145d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten                                sp<IMemory>& buffers,
14620b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                status_t *status /*non-NULL*/,
14720b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                audio_port_handle_t portId);
1482f732eb768004c6362fae8a02c60b69c9400b032Glenn Kasten
1492c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    virtual     uint32_t    sampleRate(audio_io_handle_t ioHandle) const;
15072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual     audio_format_t format(audio_io_handle_t output) const;
1512c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    virtual     size_t      frameCount(audio_io_handle_t ioHandle) const;
1524a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    virtual     size_t      frameCountHAL(audio_io_handle_t ioHandle) const;
15372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual     uint32_t    latency(audio_io_handle_t output) const;
15465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual     status_t    setMasterVolume(float value);
15665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual     status_t    setMasterMute(bool muted);
15765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual     float       masterVolume() const;
15965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual     bool        masterMute() const;
16065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual     status_t    setStreamVolume(audio_stream_type_t stream, float value,
16272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                            audio_io_handle_t output);
163fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    virtual     status_t    setStreamMute(audio_stream_type_t stream, bool muted);
16465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual     float       streamVolume(audio_stream_type_t stream,
16672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                         audio_io_handle_t output) const;
167fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    virtual     bool        streamMute(audio_stream_type_t stream) const;
16865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
169f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kasten    virtual     status_t    setMode(audio_mode_t mode);
17065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual     status_t    setMicMute(bool state);
17265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual     bool        getMicMute() const;
17365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual     status_t    setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
17572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual     String8     getParameters(audio_io_handle_t ioHandle, const String8& keys) const;
17665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual     void        registerClient(const sp<IAudioFlingerClient>& client);
17865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
179dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten    virtual     size_t      getInputBufferSize(uint32_t sampleRate, audio_format_t format,
180dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten                                               audio_channel_mask_t channelMask) const;
18165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
182cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    virtual status_t openOutput(audio_module_handle_t module,
183cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                audio_io_handle_t *output,
184cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                audio_config_t *config,
185cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                audio_devices_t *devices,
186cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                const String8& address,
187cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                uint32_t *latencyMs,
188cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                audio_output_flags_t flags);
18965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
19172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                                  audio_io_handle_t output2);
19265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual status_t closeOutput(audio_io_handle_t output);
19465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual status_t suspendOutput(audio_io_handle_t output);
19665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual status_t restoreOutput(audio_io_handle_t output);
19865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
199cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    virtual status_t openInput(audio_module_handle_t module,
200cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                               audio_io_handle_t *input,
201cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                               audio_config_t *config,
202cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                               audio_devices_t *device,
203cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                               const String8& address,
204cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                               audio_source_t source,
205cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                               audio_input_flags_t flags);
20665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
20772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    virtual status_t closeInput(audio_io_handle_t input);
20865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
209d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten    virtual status_t invalidateStream(audio_stream_type_t stream);
21065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
21165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual status_t setVoiceVolume(float volume);
21265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
213377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT    virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
21472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                       audio_io_handle_t output) const;
21565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2165f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kasten    virtual uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const;
2172f732eb768004c6362fae8a02c60b69c9400b032Glenn Kasten
2189d003131329450081c8129b3fddd85cf20d2d9d9Glenn Kasten    // This is the binder API.  For the internal API see nextUniqueId().
219eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use);
22065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
221d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten    virtual void acquireAudioSessionId(audio_session_t audioSession, pid_t pid);
2223a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
223d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten    virtual void releaseAudioSessionId(audio_session_t audioSession, pid_t pid);
2243a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
225f587ba5b991c7cd91e4df093d0d796bd419e5d67Glenn Kasten    virtual status_t queryNumberEffects(uint32_t *numEffects) const;
22665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
227f587ba5b991c7cd91e4df093d0d796bd419e5d67Glenn Kasten    virtual status_t queryEffect(uint32_t index, effect_descriptor_t *descriptor) const;
22865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2295e92a7861196ddae14638d4b7a63fc4892b7ef59Glenn Kasten    virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid,
230f587ba5b991c7cd91e4df093d0d796bd419e5d67Glenn Kasten                                         effect_descriptor_t *descriptor) const;
23165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2328d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten    virtual sp<IEffect> createEffect(
23365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        effect_descriptor_t *pDesc,
23465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        const sp<IEffectClient>& effectClient,
23565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        int32_t priority,
23672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                        audio_io_handle_t io,
237d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                        audio_session_t sessionId,
238be71aa29a3c86d2e01cd17839d2a72ab09a1bce5Svet Ganov                        const String16& opPackageName,
239b643627a557e44b9ab5879cf71e162af2d514ce3Eric Laurent                        pid_t pid,
2409156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                        status_t *status /*non-NULL*/,
24165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        int *id,
24265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        int *enabled);
24365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
244d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten    virtual status_t moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
24572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                        audio_io_handle_t dstOutput);
24665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
247a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    virtual audio_module_handle_t loadHwModule(const char *name);
248a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
2493b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten    virtual uint32_t getPrimaryOutputSamplingRate();
250e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten    virtual size_t getPrimaryOutputFrameCount();
251cc0f1cfb69ce8b8985fc2c0984847a06a13ad22dGlenn Kasten
2524182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    virtual status_t setLowRamDevice(bool isLowRamDevice);
2534182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
2544b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent    /* List available audio ports and their attributes */
2554b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent    virtual status_t listAudioPorts(unsigned int *num_ports,
2561c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                    struct audio_port *ports);
2574b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent
2584b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent    /* Get attributes for a given audio port */
2591c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t getAudioPort(struct audio_port *port);
2604b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent
2614b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent    /* Create an audio patch between several source and sink ports */
2624b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent    virtual status_t createAudioPatch(const struct audio_patch *patch,
2631c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                       audio_patch_handle_t *handle);
2644b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent
2654b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent    /* Release an audio patch */
2661c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
2674b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent
2684b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent    /* List existing audio patches */
2694b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent    virtual status_t listAudioPatches(unsigned int *num_patches,
2701c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                      struct audio_patch *patches);
2711c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2724b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent    /* Set audio port configuration */
2731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t setAudioPortConfig(const struct audio_port_config *config);
2744b123406c10c17852734a1b691bb9ce2a4cb7cafEric Laurent
27593c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    /* Get the HW synchronization source used for an audio session */
27693c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    virtual audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId);
27793c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent
27872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    /* Indicate JAVA services are ready (scheduling, power management ...) */
27972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    virtual status_t systemReady();
28072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
28165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual     status_t    onTransact(
28265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                uint32_t code,
28365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                const Parcel& data,
28465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                Parcel* reply,
28565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                uint32_t flags);
28665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2872f732eb768004c6362fae8a02c60b69c9400b032Glenn Kasten    // end of IAudioFlinger interface
2882f732eb768004c6362fae8a02c60b69c9400b032Glenn Kasten
2899e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<NBLog::Writer>   newWriter_l(size_t size, const char *name);
2909e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    void                unregisterWriter(const sp<NBLog::Writer>& writer);
2914a3d5c23f79189eb7ab9f31c440c7da5b15947a2Mikhail Naganov    sp<EffectsFactoryHalInterface> getEffectsFactory();
292fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent
293fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent    status_t openMmapStream(MmapStreamInterface::stream_direction_t direction,
294fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent                            const audio_attributes_t *attr,
295fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent                            audio_config_base_t *config,
296fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent                            const MmapStreamInterface::Client& client,
297fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent                            audio_port_handle_t *deviceId,
298fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent                            const sp<MmapStreamCallback>& callback,
299fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent                            sp<MmapStreamInterface>& interface);
3009e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenprivate:
301481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    static const size_t kLogMemorySize = 40 * 1024;
3029e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<MemoryDealer>    mLogMemoryDealer;   // == 0 when NBLog is disabled
303481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // When a log writer is unregistered, it is done lazily so that media.log can continue to see it
304481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // for as long as possible.  The memory is only freed when it is needed for another log writer.
305481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    Vector< sp<NBLog::Writer> > mUnregisteredWriters;
306481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    Mutex               mUnregisteredWritersLock;
307fc23520d9c3f15e28baa81de5f7dfa6c1b0af426Eric Laurent
3089e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenpublic:
3099e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
310a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    class SyncEvent;
311a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
312a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    typedef void (*sync_event_callback_t)(const wp<SyncEvent>& event) ;
313a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
314a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    class SyncEvent : public RefBase {
315a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    public:
316a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        SyncEvent(AudioSystem::sync_event_t type,
317d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                  audio_session_t triggerSession,
318d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                  audio_session_t listenerSession,
319a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                  sync_event_callback_t callBack,
3208ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent                  wp<RefBase> cookie)
321a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession),
322a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent          mCallback(callBack), mCookie(cookie)
323a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        {}
324a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
325a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        virtual ~SyncEvent() {}
326a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
327a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        void trigger() { Mutex::Autolock _l(mLock); if (mCallback) mCallback(this); }
328106e8a42038f9e90d5ff97f8ab6f1a42258bde9eGlenn Kasten        bool isCancelled() const { Mutex::Autolock _l(mLock); return (mCallback == NULL); }
329106e8a42038f9e90d5ff97f8ab6f1a42258bde9eGlenn Kasten        void cancel() { Mutex::Autolock _l(mLock); mCallback = NULL; }
330a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        AudioSystem::sync_event_t type() const { return mType; }
331d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten        audio_session_t triggerSession() const { return mTriggerSession; }
332d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten        audio_session_t listenerSession() const { return mListenerSession; }
3338ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent        wp<RefBase> cookie() const { return mCookie; }
334a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
335a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    private:
336a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent          const AudioSystem::sync_event_t mType;
337d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten          const audio_session_t mTriggerSession;
338d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten          const audio_session_t mListenerSession;
339a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent          sync_event_callback_t mCallback;
3408ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent          const wp<RefBase> mCookie;
341106e8a42038f9e90d5ff97f8ab6f1a42258bde9eGlenn Kasten          mutable Mutex mLock;
342a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    };
343a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
344a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    sp<SyncEvent> createSyncEvent(AudioSystem::sync_event_t type,
345d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                        audio_session_t triggerSession,
346d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                        audio_session_t listenerSession,
347a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                                        sync_event_callback_t callBack,
34836d0ca16024820df9a12903d2ac443fabcc180bcChih-Hung Hsieh                                        const wp<RefBase>& cookie);
349717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
3502f732eb768004c6362fae8a02c60b69c9400b032Glenn Kastenprivate:
351ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman
352f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kasten               audio_mode_t getMode() const { return mMode; }
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
354c59c004a3a6042c0990d71179f88eee2ce781e3cGlenn Kasten                bool        btNrecIsOff() const { return mBtNrecIsOff; }
35559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent
356e762be91c3280d837b1d48455cba90459ced7511Mathias Agopian                            AudioFlinger() ANDROID_API;
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual                 ~AudioFlinger();
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3592b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    // call in any IAudioFlinger method that accesses mPrimaryHardwareDev
36085ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten    status_t                initCheck() const { return mPrimaryHardwareDev == NULL ?
36185ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten                                                        NO_INIT : NO_ERROR; }
3622b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten
363000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten    // RefBase
3645a61d2f277af3098fc10b2881babca16391362daDima Zavin    virtual     void        onFirstRef();
365000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
36685ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten    AudioHwDevice*          findSuitableHwDev_l(audio_module_handle_t module,
36785ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten                                                audio_devices_t devices);
3683a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    void                    purgeStaleEffects_l();
36965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3709a59276fb465e492138e0576523b54079671e8f4Andy Hung    // Set kEnableExtendedChannels to true to enable greater than stereo output
3719a59276fb465e492138e0576523b54079671e8f4Andy Hung    // for the MixerThread and device sink.  Number of channels allowed is
3729a59276fb465e492138e0576523b54079671e8f4Andy Hung    // FCC_2 <= channels <= AudioMixer::MAX_NUM_CHANNELS.
373b1ee3954153e9c40280f68d278526ca43c31fdcfAndy Hung    static const bool kEnableExtendedChannels = true;
3749a59276fb465e492138e0576523b54079671e8f4Andy Hung
3759a59276fb465e492138e0576523b54079671e8f4Andy Hung    // Returns true if channel mask is permitted for the PCM sink in the MixerThread
3769a59276fb465e492138e0576523b54079671e8f4Andy Hung    static inline bool isValidPcmSinkChannelMask(audio_channel_mask_t channelMask) {
3779a59276fb465e492138e0576523b54079671e8f4Andy Hung        switch (audio_channel_mask_get_representation(channelMask)) {
3789a59276fb465e492138e0576523b54079671e8f4Andy Hung        case AUDIO_CHANNEL_REPRESENTATION_POSITION: {
3799a59276fb465e492138e0576523b54079671e8f4Andy Hung            uint32_t channelCount = FCC_2; // stereo is default
3809a59276fb465e492138e0576523b54079671e8f4Andy Hung            if (kEnableExtendedChannels) {
3819a59276fb465e492138e0576523b54079671e8f4Andy Hung                channelCount = audio_channel_count_from_out_mask(channelMask);
382389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung                if (channelCount < FCC_2 // mono is not supported at this time
383389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung                        || channelCount > AudioMixer::MAX_NUM_CHANNELS) {
3849a59276fb465e492138e0576523b54079671e8f4Andy Hung                    return false;
3859a59276fb465e492138e0576523b54079671e8f4Andy Hung                }
3869a59276fb465e492138e0576523b54079671e8f4Andy Hung            }
3879a59276fb465e492138e0576523b54079671e8f4Andy Hung            // check that channelMask is the "canonical" one we expect for the channelCount.
3889a59276fb465e492138e0576523b54079671e8f4Andy Hung            return channelMask == audio_channel_out_mask_from_count(channelCount);
3899a59276fb465e492138e0576523b54079671e8f4Andy Hung            }
39064b6cb2cd5646a5ffcdb2cccee09a170ef2882d5Andy Hung        case AUDIO_CHANNEL_REPRESENTATION_INDEX:
39164b6cb2cd5646a5ffcdb2cccee09a170ef2882d5Andy Hung            if (kEnableExtendedChannels) {
39264b6cb2cd5646a5ffcdb2cccee09a170ef2882d5Andy Hung                const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
39364b6cb2cd5646a5ffcdb2cccee09a170ef2882d5Andy Hung                if (channelCount >= FCC_2 // mono is not supported at this time
39464b6cb2cd5646a5ffcdb2cccee09a170ef2882d5Andy Hung                        && channelCount <= AudioMixer::MAX_NUM_CHANNELS) {
39564b6cb2cd5646a5ffcdb2cccee09a170ef2882d5Andy Hung                    return true;
39664b6cb2cd5646a5ffcdb2cccee09a170ef2882d5Andy Hung                }
39764b6cb2cd5646a5ffcdb2cccee09a170ef2882d5Andy Hung            }
39864b6cb2cd5646a5ffcdb2cccee09a170ef2882d5Andy Hung            return false;
3999a59276fb465e492138e0576523b54079671e8f4Andy Hung        default:
4009a59276fb465e492138e0576523b54079671e8f4Andy Hung            return false;
4019a59276fb465e492138e0576523b54079671e8f4Andy Hung        }
4029a59276fb465e492138e0576523b54079671e8f4Andy Hung    }
4039a59276fb465e492138e0576523b54079671e8f4Andy Hung
4046146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    // Set kEnableExtendedPrecision to true to use extended precision in MixerThread
405a494e82c3c73508b4d3cfe89e9134de94e12fd31Andy Hung    static const bool kEnableExtendedPrecision = true;
4066146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung
4076146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    // Returns true if format is permitted for the PCM sink in the MixerThread
4086146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    static inline bool isValidPcmSinkFormat(audio_format_t format) {
4096146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        switch (format) {
4106146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        case AUDIO_FORMAT_PCM_16_BIT:
4116146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung            return true;
4126146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        case AUDIO_FORMAT_PCM_FLOAT:
4136146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        case AUDIO_FORMAT_PCM_24_BIT_PACKED:
4146146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        case AUDIO_FORMAT_PCM_32_BIT:
4156146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        case AUDIO_FORMAT_PCM_8_24_BIT:
4166146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung            return kEnableExtendedPrecision;
4176146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        default:
4186146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung            return false;
4196146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        }
4206146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    }
4216146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung
42266fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten    // standby delay for MIXER and DUPLICATING playback threads is read from property
42366fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten    // ro.audio.flinger_standbytime_ms or defaults to kDefaultStandbyTimeInNsecs
4244ff14bae91075eb274eb1c2975982358946e7e63John Grossman    static nsecs_t          mStandbyTimeInNsecs;
4254ff14bae91075eb274eb1c2975982358946e7e63John Grossman
42681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // incremented by 2 when screen state changes, bit 0 == 1 means "off"
42781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // AudioFlinger::setParameters() updates, other threads read w/o lock
42881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static uint32_t         mScreenState;
42981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
430be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kasten    // Internal dump utilities.
43181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static const int kDumpLockRetries = 50;
43281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static const int kDumpLockSleepUs = 20000;
43381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static bool dumpTryLock(Mutex& mutex);
434be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kasten    void dumpPermissionDenial(int fd, const Vector<String16>& args);
435be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kasten    void dumpClients(int fd, const Vector<String16>& args);
436be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kasten    void dumpInternals(int fd, const Vector<String16>& args);
43765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
43865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // --- Client ---
43965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class Client : public RefBase {
44065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public:
44165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            Client(const sp<AudioFlinger>& audioFlinger, pid_t pid);
44265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual             ~Client();
443435dbe6c3ecd04bcb4bd80584064e287ebccd720Glenn Kasten        sp<MemoryDealer>    heap() const;
44465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        pid_t               pid() const { return mPid; }
445c59c004a3a6042c0990d71179f88eee2ce781e3cGlenn Kasten        sp<AudioFlinger>    audioFlinger() const { return mAudioFlinger; }
44665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
44765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private:
44865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            Client(const Client&);
44965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            Client& operator = (const Client&);
45084afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        const sp<AudioFlinger> mAudioFlinger;
451da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent              sp<MemoryDealer> mMemoryDealer;
45284afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        const pid_t         mPid;
45365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    };
45465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
45565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // --- Notification Client ---
45665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class NotificationClient : public IBinder::DeathRecipient {
45765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public:
45865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            NotificationClient(const sp<AudioFlinger>& audioFlinger,
45965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                                const sp<IAudioFlingerClient>& client,
46065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                                pid_t pid);
46165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual             ~NotificationClient();
46265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
46384afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten                sp<IAudioFlingerClient> audioFlingerClient() const { return mAudioFlingerClient; }
46465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
46565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                // IBinder::DeathRecipient
46665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                virtual     void        binderDied(const wp<IBinder>& who);
46765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
46865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private:
46965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            NotificationClient(const NotificationClient&);
47065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            NotificationClient& operator = (const NotificationClient&);
47165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
47284afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        const sp<AudioFlinger>  mAudioFlinger;
47384afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        const pid_t             mPid;
47484afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        const sp<IAudioFlingerClient> mAudioFlingerClient;
47565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    };
47665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
477dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    // --- MediaLogNotifier ---
478dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    // Thread in charge of notifying MediaLogService to start merging.
479dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    // Receives requests from AudioFlinger's binder activity. It is used to reduce the amount of
480dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    // binder calls to MediaLogService in case of bursts of AudioFlinger binder calls.
481dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    class MediaLogNotifier : public Thread {
482dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    public:
483dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        MediaLogNotifier();
484dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet
485dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        // Requests a MediaLogService notification. It's ignored if there has recently been another
486dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        void requestMerge();
487dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    private:
488dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        // Every iteration blocks waiting for a request, then interacts with MediaLogService to
489dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        // start merging.
490dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        // As every MediaLogService binder call is expensive, once it gets a request it ignores the
491dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        // following ones for a period of time.
492dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        virtual bool threadLoop() override;
493dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet
494dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        bool mPendingRequests;
495dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet
496dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        // Mutex and condition variable around mPendingRequests' value
497dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        Mutex       mMutex;
498dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        Condition   mCond;
499dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet
500dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        // Duration of the sleep period after a processed request
501dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet        static const int kPostTriggerSleepPeriod = 1000000;
502dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    };
503dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet
504dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    const sp<MediaLogNotifier> mMediaLogNotifier;
505dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet
506dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    // This is a helper that is called during incoming binder calls.
507dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet    void requestLogMerge();
508dcdfaecc1fa630a799e1fdb508a9b92da55abc36Nicolas Roulet
50965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class TrackHandle;
51065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class RecordHandle;
51165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class RecordThread;
51265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class PlaybackThread;
51365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class MixerThread;
51465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class DirectOutputThread;
515bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    class OffloadThread;
51665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class DuplicatingThread;
517bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    class AsyncCallbackThread;
51865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class Track;
51965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class RecordTrack;
52065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class EffectModule;
52165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class EffectHandle;
52265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class EffectChain;
523062e67a26e0553dd142be622821f493df541f0c6Phil Burk
524799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    struct AudioStreamIn;
52565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
526b7bf796b758e144f94f6ed4d16c21bf22a118fb3Glenn Kasten    struct  stream_type_t {
527b7bf796b758e144f94f6ed4d16c21bf22a118fb3Glenn Kasten        stream_type_t()
528b7bf796b758e144f94f6ed4d16c21bf22a118fb3Glenn Kasten            :   volume(1.0f),
5291a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent                mute(false)
530b7bf796b758e144f94f6ed4d16c21bf22a118fb3Glenn Kasten        {
531b7bf796b758e144f94f6ed4d16c21bf22a118fb3Glenn Kasten        }
532b7bf796b758e144f94f6ed4d16c21bf22a118fb3Glenn Kasten        float       volume;
533b7bf796b758e144f94f6ed4d16c21bf22a118fb3Glenn Kasten        bool        mute;
534b7bf796b758e144f94f6ed4d16c21bf22a118fb3Glenn Kasten    };
535b7bf796b758e144f94f6ed4d16c21bf22a118fb3Glenn Kasten
53665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // --- PlaybackThread ---
53772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten
53881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "Threads.h"
53965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
54081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "Effects.h"
541717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
5421c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent#include "PatchPanel.h"
5431c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
54499e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten    // server side of the client's IAudioTrack
54565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class TrackHandle : public android::BnAudioTrack {
54665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public:
54736d0ca16024820df9a12903d2ac443fabcc180bcChih-Hung Hsieh        explicit            TrackHandle(const sp<PlaybackThread::Track>& track);
54865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual             ~TrackHandle();
54990716c5728b37637b2d0a730a721bfc9fad299e0Glenn Kasten        virtual sp<IMemory> getCblk() const;
5503acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        virtual status_t    start();
55165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual void        stop();
55265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual void        flush();
55365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual void        pause();
55465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual status_t    attachAuxEffect(int effectId);
5553dcd00dddec86a1c5133083ad7ba2265d49c048cGlenn Kasten        virtual status_t    setParameters(const String8& keyValuePairs);
5569fc8b5cd4a64ef07e84c69112461324d5c13a0b0Andy Hung        virtual VolumeShaper::Status applyVolumeShaper(
5579fc8b5cd4a64ef07e84c69112461324d5c13a0b0Andy Hung                const sp<VolumeShaper::Configuration>& configuration,
5589fc8b5cd4a64ef07e84c69112461324d5c13a0b0Andy Hung                const sp<VolumeShaper::Operation>& operation) override;
5599fc8b5cd4a64ef07e84c69112461324d5c13a0b0Andy Hung        virtual sp<VolumeShaper::State> getVolumeShaperState(int id) override;
56053cec22821072719ee02c856e9ac2dda2496c570Glenn Kasten        virtual status_t    getTimestamp(AudioTimestamp& timestamp);
56159fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        virtual void        signal(); // signal playback thread for a change in control block
5623dcd00dddec86a1c5133083ad7ba2265d49c048cGlenn Kasten
56365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual status_t onTransact(
56465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
565ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
56665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private:
56784afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        const sp<PlaybackThread::Track> mTrack;
56865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    };
56965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
57099e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten    // server side of the client's IAudioRecord
57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    class RecordHandle : public android::BnAudioRecord {
57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public:
57336d0ca16024820df9a12903d2ac443fabcc180bcChih-Hung Hsieh        explicit RecordHandle(const sp<RecordThread::RecordTrack>& recordTrack);
57465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual             ~RecordHandle();
575d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten        virtual status_t    start(int /*AudioSystem::sync_event_t*/ event,
576d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                audio_session_t triggerSession);
57765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual void        stop();
57865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        virtual status_t onTransact(
57965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
58065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private:
58184afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        const sp<RecordThread::RecordTrack> mRecordTrack;
582d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten
583d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        // for use from destructor
584d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        void                stop_nonvirtual();
58565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    };
58665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5876acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    // Mmap stream control interface implementation. Each MmapThreadHandle controls one
5886acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    // MmapPlaybackThread or MmapCaptureThread instance.
5896acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    class MmapThreadHandle : public MmapStreamInterface {
5906acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    public:
5916acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        explicit            MmapThreadHandle(const sp<MmapThread>& thread);
5926acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        virtual             ~MmapThreadHandle();
5936acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
5946acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        // MmapStreamInterface virtuals
5956acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        virtual status_t createMmapBuffer(int32_t minSizeFrames,
5966acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                          struct audio_mmap_buffer_info *info);
5976acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        virtual status_t getMmapPosition(struct audio_mmap_position *position);
598d3bb645f0b7f567b033b8664499d685f8ec10628Glenn Kasten        virtual status_t start(const MmapStreamInterface::Client& client,
599d3bb645f0b7f567b033b8664499d685f8ec10628Glenn Kasten                                         audio_port_handle_t *handle);
6006acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        virtual status_t stop(audio_port_handle_t handle);
60118b570146c971fe729c391bfbb869391084e623dEric Laurent        virtual status_t standby();
6026acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
6036acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    private:
6046acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        sp<MmapThread> mThread;
6056acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    };
606bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
607eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten              ThreadBase *checkThread_l(audio_io_handle_t ioHandle) const;
60881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              PlaybackThread *checkPlaybackThread_l(audio_io_handle_t output) const;
60981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              MixerThread *checkMixerThread_l(audio_io_handle_t output) const;
61081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              RecordThread *checkRecordThread_l(audio_io_handle_t input) const;
6116acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent              MmapThread *checkMmapThread_l(audio_io_handle_t io) const;
6126acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent              VolumeInterface *getVolumeInterface_l(audio_io_handle_t output) const;
6136acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent              Vector <VolumeInterface *> getAllVolumeInterfaces_l() const;
6146acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
6156acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent              sp<ThreadBase> openInput_l(audio_module_handle_t module,
616cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                           audio_io_handle_t *input,
617cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                           audio_config_t *config,
61883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                           audio_devices_t device,
619cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                           const String8& address,
620cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                           audio_source_t source,
62183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                           audio_input_flags_t flags);
6226acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent              sp<ThreadBase> openOutput_l(audio_module_handle_t module,
623cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                              audio_io_handle_t *output,
624cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                              audio_config_t *config,
625cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                              audio_devices_t devices,
626cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                              const String8& address,
62783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                              audio_output_flags_t flags);
62883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
62936d0ca16024820df9a12903d2ac443fabcc180bcChih-Hung Hsieh              void closeOutputFinish(const sp<PlaybackThread>& thread);
63036d0ca16024820df9a12903d2ac443fabcc180bcChih-Hung Hsieh              void closeInputFinish(const sp<RecordThread>& thread);
63183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
63281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              // no range check, AudioFlinger::mLock held
63381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              bool streamMute_l(audio_stream_type_t stream) const
63481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { return mStreamTypes[stream].mute; }
63581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              // no range check, doesn't check per-thread stream volume, AudioFlinger::mLock held
63681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              float streamVolume_l(audio_stream_type_t stream) const
63781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { return mStreamTypes[stream].volume; }
63873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent              void ioConfigChanged(audio_io_config_event event,
6397c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                                   const sp<AudioIoDescriptor>& ioDesc,
6407c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                                   pid_t pid = 0);
64191b14c4c144d0cc957a427cffc02ba10d0615677Eric Laurent
642eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten              // Allocate an audio_unique_id_t.
643eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten              // Specific types are audio_io_handle_t, audio_session_t, effect ID (int),
644eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten              // audio_module_handle_t, and audio_patch_handle_t.
645bcefec31bd1346133052356ffc8d7ac8a5b13fabGlenn Kasten              // They all share the same ID space, but the namespaces are actually independent
646bcefec31bd1346133052356ffc8d7ac8a5b13fabGlenn Kasten              // because there are separate KeyedVectors for each kind of ID.
647eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten              // The return value is cast to the specific type depending on how the ID will be used.
648bcefec31bd1346133052356ffc8d7ac8a5b13fabGlenn Kasten              // FIXME This API does not handle rollover to zero (for unsigned IDs),
649bcefec31bd1346133052356ffc8d7ac8a5b13fabGlenn Kasten              //       or from positive to negative (for signed IDs).
650bcefec31bd1346133052356ffc8d7ac8a5b13fabGlenn Kasten              //       Thus it may fail by returning an ID of the wrong sign,
651bcefec31bd1346133052356ffc8d7ac8a5b13fabGlenn Kasten              //       or by returning a non-unique ID.
6529d003131329450081c8129b3fddd85cf20d2d9d9Glenn Kasten              // This is the internal API.  For the binder API see newAudioUniqueId().
653eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten              audio_unique_id_t nextUniqueId(audio_unique_id_use_t use);
65465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
655d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten              status_t moveEffectChain_l(audio_session_t sessionId,
65681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                     PlaybackThread *srcThread,
65781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                     PlaybackThread *dstThread,
65881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                     bool reRegister);
6597fd0422fbd17af3b24eb04421d37fce50f3826e2Glenn Kasten
66081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              // return thread associated with primary hardware device, or NULL
66181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              PlaybackThread *primaryPlaybackThread_l() const;
66281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              audio_devices_t primaryOutputDevice_l() const;
66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
664a73356393315ad3ad7cd9196640823d1a9a620ddGlenn Kasten              // return the playback thread with smallest HAL buffer size, and prefer fast
665a73356393315ad3ad7cd9196640823d1a9a620ddGlenn Kasten              PlaybackThread *fastPlaybackThread_l() const;
666a73356393315ad3ad7cd9196640823d1a9a620ddGlenn Kasten
667d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten              sp<PlaybackThread> getEffectThread_l(audio_session_t sessionId, int EffectId);
66859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
66959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
67081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        removeClient_l(pid_t pid);
67181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        removeNotificationClient(pid_t pid);
6725baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                bool isNonOffloadableGlobalEffectEnabled_l();
6735baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                void onNonOffloadableGlobalEffectEnable();
6740d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                bool isSessionAcquired_l(audio_session_t audioSession);
675813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
676aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // Store an effect chain to mOrphanEffectChains keyed vector.
677aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // Called when a thread exits and effects are still attached to it.
678aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // If effects are later created on the same session, they will reuse the same
679aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // effect chain and same instances in the effect library.
680aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // return ALREADY_EXISTS if a chain with the same session already exists in
681aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // mOrphanEffectChains. Note that this should never happen as there is only one
682aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // chain for a given session and it is attached to only one thread at a time.
683aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                status_t        putOrphanEffectChain_l(const sp<EffectChain>& chain);
684aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // Get an effect chain for the specified session in mOrphanEffectChains and remove
685aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // it if found. Returns 0 if not found (this is the most common case).
686aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                sp<EffectChain> getOrphanEffectChain_l(audio_session_t session);
687aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // Called when the last effect handle on an effect instance is removed. If this
688aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // effect belongs to an effect chain in mOrphanEffectChains, the chain is updated
689aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // and removed from mOrphanEffectChains if it does not contain any effect.
690aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                // Return true if the effect was found in mOrphanEffectChains, false otherwise.
691aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                bool            updateOrphanEffectChains(const sp<EffectModule>& effect);
692aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
693054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                void broacastParametersToRecordThreads_l(const String8& keyValuePairs);
6941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
695062e67a26e0553dd142be622821f493df541f0c6Phil Burk    // AudioStreamIn is immutable, so their fields are const.
696aed850d0d3b3c8cf3feaf1438076f33db2a60946Glenn Kasten    // For emphasis, we could also make all pointers to them be "const *",
697aed850d0d3b3c8cf3feaf1438076f33db2a60946Glenn Kasten    // but that would clutter the code unnecessarily.
698aed850d0d3b3c8cf3feaf1438076f33db2a60946Glenn Kasten
699