AudioPolicyManager.h revision 4dc680607181e6a76f4e91a39366c4f5dfb7b03e
14b3de47071d875faaa7d419d050a464b09538797Jason Sams/*
24b3de47071d875faaa7d419d050a464b09538797Jason Sams * Copyright (C) 2009 The Android Open Source Project
34b3de47071d875faaa7d419d050a464b09538797Jason Sams *
44b3de47071d875faaa7d419d050a464b09538797Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
54b3de47071d875faaa7d419d050a464b09538797Jason Sams * you may not use this file except in compliance with the License.
64b3de47071d875faaa7d419d050a464b09538797Jason Sams * You may obtain a copy of the License at
74b3de47071d875faaa7d419d050a464b09538797Jason Sams *
84b3de47071d875faaa7d419d050a464b09538797Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
94b3de47071d875faaa7d419d050a464b09538797Jason Sams *
104b3de47071d875faaa7d419d050a464b09538797Jason Sams * Unless required by applicable law or agreed to in writing, software
114b3de47071d875faaa7d419d050a464b09538797Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
124b3de47071d875faaa7d419d050a464b09538797Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134b3de47071d875faaa7d419d050a464b09538797Jason Sams * See the License for the specific language governing permissions and
144b3de47071d875faaa7d419d050a464b09538797Jason Sams * limitations under the License.
154b3de47071d875faaa7d419d050a464b09538797Jason Sams */
164b3de47071d875faaa7d419d050a464b09538797Jason Sams
174b3de47071d875faaa7d419d050a464b09538797Jason Sams
184b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <stdint.h>
19b8eba193f3f0d25a6156236fa80f79a5b1dc89f6Mathias Agopian#include <sys/types.h>
20b8eba193f3f0d25a6156236fa80f79a5b1dc89f6Mathias Agopian#include <cutils/config_utils.h>
214b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <cutils/misc.h>
224b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <utils/Timers.h>
234b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <utils/Errors.h>
244b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <utils/KeyedVector.h>
254b3de47071d875faaa7d419d050a464b09538797Jason Sams#include <utils/SortedVector.h>
264b3de47071d875faaa7d419d050a464b09538797Jason Sams#include "AudioPolicyInterface.h"
274b3de47071d875faaa7d419d050a464b09538797Jason Sams
284b3de47071d875faaa7d419d050a464b09538797Jason Sams
294b3de47071d875faaa7d419d050a464b09538797Jason Samsnamespace android {
304b3de47071d875faaa7d419d050a464b09538797Jason Sams
314b3de47071d875faaa7d419d050a464b09538797Jason Sams// ----------------------------------------------------------------------------
324b3de47071d875faaa7d419d050a464b09538797Jason Sams
334b3de47071d875faaa7d419d050a464b09538797Jason Sams// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
344b3de47071d875faaa7d419d050a464b09538797Jason Sams#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
354b3de47071d875faaa7d419d050a464b09538797Jason Sams// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
364b3de47071d875faaa7d419d050a464b09538797Jason Sams#define SONIFICATION_HEADSET_VOLUME_MIN  0.016
374b3de47071d875faaa7d419d050a464b09538797Jason Sams// Time in milliseconds during which we consider that music is still active after a music
384b3de47071d875faaa7d419d050a464b09538797Jason Sams// track was stopped - see computeVolume()
394b3de47071d875faaa7d419d050a464b09538797Jason Sams#define SONIFICATION_HEADSET_MUSIC_DELAY  5000
40a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk// Time in milliseconds after media stopped playing during which we consider that the
41a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk// sonification should be as unobtrusive as during the time media was playing.
42a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk#define SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY 5000
434b3de47071d875faaa7d419d050a464b09538797Jason Sams// Time in milliseconds during witch some streams are muted while the audio path
444b3de47071d875faaa7d419d050a464b09538797Jason Sams// is switched
454b3de47071d875faaa7d419d050a464b09538797Jason Sams#define MUTE_TIME_MS 2000
464b3de47071d875faaa7d419d050a464b09538797Jason Sams
474b3de47071d875faaa7d419d050a464b09538797Jason Sams#define NUM_TEST_OUTPUTS 5
484b3de47071d875faaa7d419d050a464b09538797Jason Sams
494b3de47071d875faaa7d419d050a464b09538797Jason Sams#define NUM_VOL_CURVE_KNEES 2
50e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian
51e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian// Default minimum length allowed for offloading a compressed track
52e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian// Can be overridden by the audio.offload.min.duration.secs property
53e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian#define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
54e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian
55e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian#define MAX_MIXER_SAMPLING_RATE 48000
56e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian#define MAX_MIXER_CHANNEL_COUNT 2
57e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian// See AudioPort::compareFormats()
58e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian#define WORST_MIXER_FORMAT AUDIO_FORMAT_PCM_16_BIT
59e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian#define BEST_MIXER_FORMAT AUDIO_FORMAT_PCM_24_BIT_PACKED
60e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian
61e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian// ----------------------------------------------------------------------------
62e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian// AudioPolicyManager implements audio policy manager behavior common to all platforms.
63e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian// ----------------------------------------------------------------------------
64e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian
65e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopianclass AudioPolicyManager: public AudioPolicyInterface
66e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian#ifdef AUDIO_POLICY_TEST
67e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian    , public Thread
68e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian#endif //AUDIO_POLICY_TEST
69e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian{
70e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian
71e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopianpublic:
72e255929025c4ec3fd91518350e3e7b447c745529Mathias Agopian                AudioPolicyManager(AudioPolicyClientInterface *clientInterface);
734b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual ~AudioPolicyManager();
744b3de47071d875faaa7d419d050a464b09538797Jason Sams
754b3de47071d875faaa7d419d050a464b09538797Jason Sams        // AudioPolicyInterface
764b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t setDeviceConnectionState(audio_devices_t device,
774b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                          audio_policy_dev_state_t state,
784b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                          const char *device_address);
794b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
804b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                                              const char *device_address);
814b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual void setPhoneState(audio_mode_t state);
824b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual void setForceUse(audio_policy_force_use_t usage,
834b3de47071d875faaa7d419d050a464b09538797Jason Sams                                 audio_policy_forced_cfg_t config);
844b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
854b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual void setSystemProperty(const char* property, const char* value);
864b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t initCheck();
874b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
884b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            uint32_t samplingRate,
894b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_format_t format,
904b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_channel_mask_t channelMask,
914b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_output_flags_t flags,
924b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            const audio_offload_info_t *offloadInfo);
934b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual audio_io_handle_t getOutputForAttr(const audio_attributes_t *attr,
944b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            uint32_t samplingRate,
954b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_format_t format,
964b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_channel_mask_t channelMask,
974b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_output_flags_t flags,
984b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            const audio_offload_info_t *offloadInfo);
994b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t startOutput(audio_io_handle_t output,
1004b3de47071d875faaa7d419d050a464b09538797Jason Sams                                     audio_stream_type_t stream,
1014b3de47071d875faaa7d419d050a464b09538797Jason Sams                                     int session = 0);
1024b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t stopOutput(audio_io_handle_t output,
1034b3de47071d875faaa7d419d050a464b09538797Jason Sams                                    audio_stream_type_t stream,
1044b3de47071d875faaa7d419d050a464b09538797Jason Sams                                    int session = 0);
1054b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual void releaseOutput(audio_io_handle_t output);
1064b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual audio_io_handle_t getInput(audio_source_t inputSource,
1074b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            uint32_t samplingRate,
1084b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_format_t format,
1094b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_channel_mask_t channelMask,
1104b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_session_t session,
1114b3de47071d875faaa7d419d050a464b09538797Jason Sams                                            audio_input_flags_t flags);
1124b3de47071d875faaa7d419d050a464b09538797Jason Sams
1134b3de47071d875faaa7d419d050a464b09538797Jason Sams        // indicates to the audio policy manager that the input starts being used.
1144b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t startInput(audio_io_handle_t input,
1154b3de47071d875faaa7d419d050a464b09538797Jason Sams                                    audio_session_t session);
1164b3de47071d875faaa7d419d050a464b09538797Jason Sams
1174b3de47071d875faaa7d419d050a464b09538797Jason Sams        // indicates to the audio policy manager that the input stops being used.
1184b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t stopInput(audio_io_handle_t input,
1194b3de47071d875faaa7d419d050a464b09538797Jason Sams                                   audio_session_t session);
1204b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual void releaseInput(audio_io_handle_t input,
1214b3de47071d875faaa7d419d050a464b09538797Jason Sams                                  audio_session_t session);
1224b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual void closeAllInputs();
1234b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual void initStreamVolume(audio_stream_type_t stream,
1244b3de47071d875faaa7d419d050a464b09538797Jason Sams                                                    int indexMin,
12567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                                                    int indexMax);
12667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
1276598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block                                              int index,
1284b3de47071d875faaa7d419d050a464b09538797Jason Sams                                              audio_devices_t device);
1294b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
1304b3de47071d875faaa7d419d050a464b09538797Jason Sams                                              int *index,
1314b3de47071d875faaa7d419d050a464b09538797Jason Sams                                              audio_devices_t device);
13287fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams
133af12ac6a08651464f8d823add667c706f993b587Steve Block        // return the strategy corresponding to a given stream type
134af12ac6a08651464f8d823add667c706f993b587Steve Block        virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
1354b3de47071d875faaa7d419d050a464b09538797Jason Sams        // return the strategy corresponding to the given audio attributes
136af12ac6a08651464f8d823add667c706f993b587Steve Block        virtual uint32_t getStrategyForAttr(const audio_attributes_t *attr);
137af12ac6a08651464f8d823add667c706f993b587Steve Block
138af12ac6a08651464f8d823add667c706f993b587Steve Block        // return the enabled output devices for the given stream type
139af12ac6a08651464f8d823add667c706f993b587Steve Block        virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
140af12ac6a08651464f8d823add667c706f993b587Steve Block
1414b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
1426598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        virtual status_t registerEffect(const effect_descriptor_t *desc,
1434b3de47071d875faaa7d419d050a464b09538797Jason Sams                                        audio_io_handle_t io,
1446598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block                                        uint32_t strategy,
1456598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block                                        int session,
1464b3de47071d875faaa7d419d050a464b09538797Jason Sams                                        int id);
1476598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        virtual status_t unregisterEffect(int id);
1484b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t setEffectEnabled(int id, bool enabled);
1494b3de47071d875faaa7d419d050a464b09538797Jason Sams
1504b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
15187fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams        // return whether a stream is playing remotely, override to change the definition of
1524b3de47071d875faaa7d419d050a464b09538797Jason Sams        //   local/remote playback, used for instance by notification manager to not make
153a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk        //   media players lose audio focus when not playing locally
154a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk        virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
155a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk        virtual bool isSourceActive(audio_source_t source) const;
156a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
1574b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t dump(int fd);
1582382aba4a55c6ae74789c478eead8fbd96593321Jason Sams
1592382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
1602382aba4a55c6ae74789c478eead8fbd96593321Jason Sams
1614b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t listAudioPorts(audio_port_role_t role,
1622382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                                        audio_port_type_t type,
1634b3de47071d875faaa7d419d050a464b09538797Jason Sams                                        unsigned int *num_ports,
1642382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                                        struct audio_port *ports,
1654b3de47071d875faaa7d419d050a464b09538797Jason Sams                                        unsigned int *generation);
1664b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t getAudioPort(struct audio_port *port);
1674b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t createAudioPatch(const struct audio_patch *patch,
1684b3de47071d875faaa7d419d050a464b09538797Jason Sams                                           audio_patch_handle_t *handle,
1694b3de47071d875faaa7d419d050a464b09538797Jason Sams                                           uid_t uid);
1702382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
1714b3de47071d875faaa7d419d050a464b09538797Jason Sams                                              uid_t uid);
1724b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual status_t listAudioPatches(unsigned int *num_patches,
1734b3de47071d875faaa7d419d050a464b09538797Jason Sams                                          struct audio_patch *patches,
1744b3de47071d875faaa7d419d050a464b09538797Jason Sams                                          unsigned int *generation);
17587fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams        virtual status_t setAudioPortConfig(const struct audio_port_config *config);
1764b3de47071d875faaa7d419d050a464b09538797Jason Sams        virtual void clearAudioPatches(uid_t uid);
1774b3de47071d875faaa7d419d050a464b09538797Jason Sams
1784b3de47071d875faaa7d419d050a464b09538797Jason Samsprotected:
1794b3de47071d875faaa7d419d050a464b09538797Jason Sams
1804b3de47071d875faaa7d419d050a464b09538797Jason Sams        enum routing_strategy {
1814b3de47071d875faaa7d419d050a464b09538797Jason Sams            STRATEGY_MEDIA,
1824b3de47071d875faaa7d419d050a464b09538797Jason Sams            STRATEGY_PHONE,
1834b3de47071d875faaa7d419d050a464b09538797Jason Sams            STRATEGY_SONIFICATION,
1844b3de47071d875faaa7d419d050a464b09538797Jason Sams            STRATEGY_SONIFICATION_RESPECTFUL,
1854b3de47071d875faaa7d419d050a464b09538797Jason Sams            STRATEGY_DTMF,
1864b3de47071d875faaa7d419d050a464b09538797Jason Sams            STRATEGY_ENFORCED_AUDIBLE,
1874b3de47071d875faaa7d419d050a464b09538797Jason Sams            NUM_STRATEGIES
1884b3de47071d875faaa7d419d050a464b09538797Jason Sams        };
1894b3de47071d875faaa7d419d050a464b09538797Jason Sams
1904b3de47071d875faaa7d419d050a464b09538797Jason Sams        // 4 points to define the volume attenuation curve, each characterized by the volume
1914b3de47071d875faaa7d419d050a464b09538797Jason Sams        // index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
19267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        // we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl()
19367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
19467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3, VOLCNT = 4};
19567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
19667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        class VolumeCurvePoint
19767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        {
19867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        public:
19967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            int mIndex;
20067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            float mDBAttenuation;
20167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        };
20267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
20367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        // device categories used for volume curve management.
20467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        enum device_category {
20567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            DEVICE_CATEGORY_HEADSET,
20667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            DEVICE_CATEGORY_SPEAKER,
20767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            DEVICE_CATEGORY_EARPIECE,
20867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            DEVICE_CATEGORY_CNT
20967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        };
2104b3de47071d875faaa7d419d050a464b09538797Jason Sams
2114b3de47071d875faaa7d419d050a464b09538797Jason Sams        class HwModule;
2124b3de47071d875faaa7d419d050a464b09538797Jason Sams
2134b3de47071d875faaa7d419d050a464b09538797Jason Sams        class AudioGain: public RefBase
2144b3de47071d875faaa7d419d050a464b09538797Jason Sams        {
2154b3de47071d875faaa7d419d050a464b09538797Jason Sams        public:
2164b3de47071d875faaa7d419d050a464b09538797Jason Sams            AudioGain(int index, bool useInChannelMask);
2174b3de47071d875faaa7d419d050a464b09538797Jason Sams            virtual ~AudioGain() {}
2184b3de47071d875faaa7d419d050a464b09538797Jason Sams
2194b3de47071d875faaa7d419d050a464b09538797Jason Sams            void dump(int fd, int spaces, int index) const;
2204b3de47071d875faaa7d419d050a464b09538797Jason Sams
2214b3de47071d875faaa7d419d050a464b09538797Jason Sams            void getDefaultConfig(struct audio_gain_config *config);
2224b3de47071d875faaa7d419d050a464b09538797Jason Sams            status_t checkConfig(const struct audio_gain_config *config);
2234b3de47071d875faaa7d419d050a464b09538797Jason Sams            int               mIndex;
2244b3de47071d875faaa7d419d050a464b09538797Jason Sams            struct audio_gain mGain;
2256598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block            bool              mUseInChannelMask;
2262382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        };
2274b3de47071d875faaa7d419d050a464b09538797Jason Sams
2284b3de47071d875faaa7d419d050a464b09538797Jason Sams        class AudioPort: public virtual RefBase
2294b3de47071d875faaa7d419d050a464b09538797Jason Sams        {
2302382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        public:
2312382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            AudioPort(const String8& name, audio_port_type_t type,
2324b3de47071d875faaa7d419d050a464b09538797Jason Sams                      audio_port_role_t role, const sp<HwModule>& module);
2334b3de47071d875faaa7d419d050a464b09538797Jason Sams            virtual ~AudioPort() {}
23467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
23567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            virtual void toAudioPort(struct audio_port *port) const;
23667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2372382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            void loadSamplingRates(char *name);
23867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            void loadFormats(char *name);
23967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            void loadOutChannels(char *name);
24067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            void loadInChannels(char *name);
2415f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams
2425f27d6fb0b0b9184ba9820c629fc1354a635e515Jason Sams            audio_gain_mode_t loadGainMode(char *name);
24367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            void loadGain(cnode *root, int index);
24467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            void loadGains(cnode *root);
24567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2462382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            // searches for an exact match
24767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            status_t checkExactSamplingRate(uint32_t samplingRate) const;
24867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            // searches for a compatible match, and returns the best match via updatedSamplingRate
24967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            status_t checkCompatibleSamplingRate(uint32_t samplingRate,
25067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                    uint32_t *updatedSamplingRate) const;
251af12ac6a08651464f8d823add667c706f993b587Steve Block            // searches for an exact match
25267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            status_t checkExactChannelMask(audio_channel_mask_t channelMask) const;
25367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            // searches for a compatible match, currently implemented for input channel masks only
25467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            status_t checkCompatibleChannelMask(audio_channel_mask_t channelMask) const;
25567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            status_t checkFormat(audio_format_t format) const;
25667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            status_t checkGain(const struct audio_gain_config *gainConfig, int index) const;
25767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
25867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            uint32_t pickSamplingRate() const;
25967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            audio_channel_mask_t pickChannelMask() const;
26067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            audio_format_t pickFormat() const;
26167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
26267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            static const audio_format_t sPcmFormatCompareTable[];
26367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            static int compareFormats(audio_format_t format1, audio_format_t format2);
26467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
26567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            void dump(int fd, int spaces) const;
26667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
26767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            String8           mName;
26867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            audio_port_type_t mType;
26967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            audio_port_role_t mRole;
27067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            bool              mUseInChannelMask;
27167605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats
27267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            // indicates the supported parameters should be read from the output stream
27367605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            // after it is opened for the first time
27467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            Vector <uint32_t> mSamplingRates; // supported sampling rates
27567605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks
27667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            Vector <audio_format_t> mFormats; // supported audio formats
27767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            Vector < sp<AudioGain> > mGains; // gain controllers
27867605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            sp<HwModule> mModule;                 // audio HW module exposing this I/O stream
27967605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            audio_output_flags_t mFlags; // attribute flags (e.g primary output,
28067605d7185c384469952b10d75c3d201b19cc07fMathias Agopian                                                // direct output...). For outputs only.
2814b3de47071d875faaa7d419d050a464b09538797Jason Sams        };
28267605d7185c384469952b10d75c3d201b19cc07fMathias Agopian
2834b3de47071d875faaa7d419d050a464b09538797Jason Sams        class AudioPortConfig: public virtual RefBase
28467605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        {
2854b3de47071d875faaa7d419d050a464b09538797Jason Sams        public:
2869db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines            AudioPortConfig();
2874b3de47071d875faaa7d419d050a464b09538797Jason Sams            virtual ~AudioPortConfig() {}
2889db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines
2894b3de47071d875faaa7d419d050a464b09538797Jason Sams            status_t applyAudioPortConfig(const struct audio_port_config *config,
2904b3de47071d875faaa7d419d050a464b09538797Jason Sams                                          struct audio_port_config *backupConfig = NULL);
2912382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
2924b3de47071d875faaa7d419d050a464b09538797Jason Sams                                   const struct audio_port_config *srcConfig = NULL) const = 0;
2934b3de47071d875faaa7d419d050a464b09538797Jason Sams            sp<AudioPort> mAudioPort;
2944b3de47071d875faaa7d419d050a464b09538797Jason Sams            uint32_t mSamplingRate;
2954b3de47071d875faaa7d419d050a464b09538797Jason Sams            audio_format_t mFormat;
296af12ac6a08651464f8d823add667c706f993b587Steve Block            audio_channel_mask_t mChannelMask;
2972382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            struct audio_gain_config mGain;
2984b3de47071d875faaa7d419d050a464b09538797Jason Sams        };
2994b3de47071d875faaa7d419d050a464b09538797Jason Sams
3004b3de47071d875faaa7d419d050a464b09538797Jason Sams
3014b3de47071d875faaa7d419d050a464b09538797Jason Sams        class AudioPatch: public RefBase
3024b3de47071d875faaa7d419d050a464b09538797Jason Sams        {
3034b3de47071d875faaa7d419d050a464b09538797Jason Sams        public:
3042382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            AudioPatch(audio_patch_handle_t handle,
3054b3de47071d875faaa7d419d050a464b09538797Jason Sams                       const struct audio_patch *patch, uid_t uid) :
3064b3de47071d875faaa7d419d050a464b09538797Jason Sams                           mHandle(handle), mPatch(*patch), mUid(uid), mAfPatchHandle(0) {}
3074b3de47071d875faaa7d419d050a464b09538797Jason Sams
3084b3de47071d875faaa7d419d050a464b09538797Jason Sams            audio_patch_handle_t mHandle;
309af12ac6a08651464f8d823add667c706f993b587Steve Block            struct audio_patch mPatch;
3104b3de47071d875faaa7d419d050a464b09538797Jason Sams            uid_t mUid;
3112382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            audio_patch_handle_t mAfPatchHandle;
3124b3de47071d875faaa7d419d050a464b09538797Jason Sams        };
3134b3de47071d875faaa7d419d050a464b09538797Jason Sams
3144b3de47071d875faaa7d419d050a464b09538797Jason Sams        class DeviceDescriptor: public AudioPort, public AudioPortConfig
3152382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        {
31667605d7185c384469952b10d75c3d201b19cc07fMathias Agopian        public:
31767605d7185c384469952b10d75c3d201b19cc07fMathias Agopian            DeviceDescriptor(const String8& name, audio_devices_t type);
3184b3de47071d875faaa7d419d050a464b09538797Jason Sams
319af12ac6a08651464f8d823add667c706f993b587Steve Block            virtual ~DeviceDescriptor() {}
3204b3de47071d875faaa7d419d050a464b09538797Jason Sams
3214b3de47071d875faaa7d419d050a464b09538797Jason Sams            bool equals(const sp<DeviceDescriptor>& other) const;
3222382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
3234b3de47071d875faaa7d419d050a464b09538797Jason Sams                                   const struct audio_port_config *srcConfig = NULL) const;
3244b3de47071d875faaa7d419d050a464b09538797Jason Sams
3254b3de47071d875faaa7d419d050a464b09538797Jason Sams            virtual void toAudioPort(struct audio_port *port) const;
3264b3de47071d875faaa7d419d050a464b09538797Jason Sams
3274b3de47071d875faaa7d419d050a464b09538797Jason Sams            status_t dump(int fd, int spaces, int index) const;
3284b3de47071d875faaa7d419d050a464b09538797Jason Sams
3294b3de47071d875faaa7d419d050a464b09538797Jason Sams            audio_devices_t mDeviceType;
3304b3de47071d875faaa7d419d050a464b09538797Jason Sams            String8 mAddress;
3316598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block            audio_port_handle_t mId;
3326598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        };
3336598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block
3346598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        class DeviceVector : public SortedVector< sp<DeviceDescriptor> >
3356598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        {
3364b3de47071d875faaa7d419d050a464b09538797Jason Sams        public:
3374b3de47071d875faaa7d419d050a464b09538797Jason Sams            DeviceVector() : SortedVector(), mDeviceTypes(AUDIO_DEVICE_NONE) {}
3384b3de47071d875faaa7d419d050a464b09538797Jason Sams
3394b3de47071d875faaa7d419d050a464b09538797Jason Sams            ssize_t         add(const sp<DeviceDescriptor>& item);
3404b3de47071d875faaa7d419d050a464b09538797Jason Sams            ssize_t         remove(const sp<DeviceDescriptor>& item);
3414b3de47071d875faaa7d419d050a464b09538797Jason Sams            ssize_t         indexOf(const sp<DeviceDescriptor>& item) const;
3424b3de47071d875faaa7d419d050a464b09538797Jason Sams
3434b3de47071d875faaa7d419d050a464b09538797Jason Sams            audio_devices_t types() const { return mDeviceTypes; }
3444b3de47071d875faaa7d419d050a464b09538797Jason Sams
3454b3de47071d875faaa7d419d050a464b09538797Jason Sams            void loadDevicesFromType(audio_devices_t types);
3464b3de47071d875faaa7d419d050a464b09538797Jason Sams            void loadDevicesFromName(char *name, const DeviceVector& declaredDevices);
3474b3de47071d875faaa7d419d050a464b09538797Jason Sams
348af12ac6a08651464f8d823add667c706f993b587Steve Block            sp<DeviceDescriptor> getDevice(audio_devices_t type, String8 address) const;
3494b3de47071d875faaa7d419d050a464b09538797Jason Sams            DeviceVector getDevicesFromType(audio_devices_t types) const;
3502382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            sp<DeviceDescriptor> getDeviceFromId(audio_port_handle_t id) const;
3514b3de47071d875faaa7d419d050a464b09538797Jason Sams            sp<DeviceDescriptor> getDeviceFromName(const String8& name) const;
3524b3de47071d875faaa7d419d050a464b09538797Jason Sams            DeviceVector getDevicesFromTypeAddr(audio_devices_t type, String8 address)
3534b3de47071d875faaa7d419d050a464b09538797Jason Sams                    const;
3544b3de47071d875faaa7d419d050a464b09538797Jason Sams
3554b3de47071d875faaa7d419d050a464b09538797Jason Sams        private:
3564b3de47071d875faaa7d419d050a464b09538797Jason Sams            void refreshTypes();
3574b3de47071d875faaa7d419d050a464b09538797Jason Sams            audio_devices_t mDeviceTypes;
3584b3de47071d875faaa7d419d050a464b09538797Jason Sams        };
3594b3de47071d875faaa7d419d050a464b09538797Jason Sams
3604b3de47071d875faaa7d419d050a464b09538797Jason Sams        // the IOProfile class describes the capabilities of an output or input stream.
3614b3de47071d875faaa7d419d050a464b09538797Jason Sams        // It is currently assumed that all combination of listed parameters are supported.
3624b3de47071d875faaa7d419d050a464b09538797Jason Sams        // It is used by the policy manager to determine if an output or input is suitable for
3634b3de47071d875faaa7d419d050a464b09538797Jason Sams        // a given use case,  open/close it accordingly and connect/disconnect audio tracks
3644b3de47071d875faaa7d419d050a464b09538797Jason Sams        // to/from it.
3654b3de47071d875faaa7d419d050a464b09538797Jason Sams        class IOProfile : public AudioPort
3664b3de47071d875faaa7d419d050a464b09538797Jason Sams        {
3674b3de47071d875faaa7d419d050a464b09538797Jason Sams        public:
368917027582745bbf27335a70360a4e6da51961978Mathias Agopian            IOProfile(const String8& name, audio_port_role_t role, const sp<HwModule>& module);
3694b3de47071d875faaa7d419d050a464b09538797Jason Sams            virtual ~IOProfile();
370917027582745bbf27335a70360a4e6da51961978Mathias Agopian
3714b3de47071d875faaa7d419d050a464b09538797Jason Sams            // This method is used for both output and input.
3724b3de47071d875faaa7d419d050a464b09538797Jason Sams            // If parameter updatedSamplingRate is non-NULL, it is assigned the actual sample rate.
3734b3de47071d875faaa7d419d050a464b09538797Jason Sams            // For input, flags is interpreted as audio_input_flags_t.
3744b3de47071d875faaa7d419d050a464b09538797Jason Sams            // TODO: merge audio_output_flags_t and audio_input_flags_t.
3754b3de47071d875faaa7d419d050a464b09538797Jason Sams            bool isCompatibleProfile(audio_devices_t device,
3764b3de47071d875faaa7d419d050a464b09538797Jason Sams                                     uint32_t samplingRate,
3774b3de47071d875faaa7d419d050a464b09538797Jason Sams                                     uint32_t *updatedSamplingRate,
3784b3de47071d875faaa7d419d050a464b09538797Jason Sams                                     audio_format_t format,
3799db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines                                     audio_channel_mask_t channelMask,
3809db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines                                     audio_output_flags_t flags) const;
3819db7fe256cf6b77eafafb42d024128701faee6bbStephen Hines
3824b3de47071d875faaa7d419d050a464b09538797Jason Sams            void dump(int fd);
383a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk            void log();
384a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
385a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk            DeviceVector  mSupportedDevices; // supported devices
386a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk                                             // (devices this output can be routed to)
3879719bd4a0187c400ba868712612fe66da4635aacJason Sams        };
388a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
3896598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        class HwModule : public RefBase
3902382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        {
3914b3de47071d875faaa7d419d050a464b09538797Jason Sams        public:
3924b3de47071d875faaa7d419d050a464b09538797Jason Sams                    HwModule(const char *name);
3934b3de47071d875faaa7d419d050a464b09538797Jason Sams                    ~HwModule();
3944b3de47071d875faaa7d419d050a464b09538797Jason Sams
3957257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchouk            status_t loadOutput(cnode *root);
39687fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams            status_t loadInput(cnode *root);
3974b3de47071d875faaa7d419d050a464b09538797Jason Sams            status_t loadDevice(cnode *root);
3984b3de47071d875faaa7d419d050a464b09538797Jason Sams
3994b3de47071d875faaa7d419d050a464b09538797Jason Sams            void dump(int fd);
4004b3de47071d875faaa7d419d050a464b09538797Jason Sams
4014b3de47071d875faaa7d419d050a464b09538797Jason Sams            const char *const        mName; // base name of the audio HW module (primary, a2dp ...)
4022382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            uint32_t                 mHalVersion; // audio HAL API version
4034b3de47071d875faaa7d419d050a464b09538797Jason Sams            audio_module_handle_t    mHandle;
4044b3de47071d875faaa7d419d050a464b09538797Jason Sams            Vector < sp<IOProfile> > mOutputProfiles; // output profiles exposed by this module
4054b3de47071d875faaa7d419d050a464b09538797Jason Sams            Vector < sp<IOProfile> > mInputProfiles;  // input profiles exposed by this module
4064b3de47071d875faaa7d419d050a464b09538797Jason Sams            DeviceVector             mDeclaredDevices; // devices declared in audio_policy.conf
4072382aba4a55c6ae74789c478eead8fbd96593321Jason Sams
4084b3de47071d875faaa7d419d050a464b09538797Jason Sams        };
4094b3de47071d875faaa7d419d050a464b09538797Jason Sams
4104b3de47071d875faaa7d419d050a464b09538797Jason Sams        // default volume curve
4114b3de47071d875faaa7d419d050a464b09538797Jason Sams        static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManager::VOLCNT];
4124b3de47071d875faaa7d419d050a464b09538797Jason Sams        // default volume curve for media strategy
4134b3de47071d875faaa7d419d050a464b09538797Jason Sams        static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT];
4144b3de47071d875faaa7d419d050a464b09538797Jason Sams        // volume curve for media strategy on speakers
4154b3de47071d875faaa7d419d050a464b09538797Jason Sams        static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT];
416c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams        static const VolumeCurvePoint sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT];
417c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams        // volume curve for sonification strategy on speakers
418c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams        static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT];
419c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams        static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT];
4207257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchouk        static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT];
4214b3de47071d875faaa7d419d050a464b09538797Jason Sams        static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT];
422c33e6903d25b9cd9d88f45c7320534a5a05c9064Jason Sams        static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT];
4234b3de47071d875faaa7d419d050a464b09538797Jason Sams        static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
4244b3de47071d875faaa7d419d050a464b09538797Jason Sams        static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
4254b3de47071d875faaa7d419d050a464b09538797Jason Sams        // default volume curves per stream and device category. See initializeVolumeCurves()
4262382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        static const VolumeCurvePoint *sVolumeProfiles[AUDIO_STREAM_CNT][DEVICE_CATEGORY_CNT];
4274b3de47071d875faaa7d419d050a464b09538797Jason Sams
4284b3de47071d875faaa7d419d050a464b09538797Jason Sams        // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
4294b3de47071d875faaa7d419d050a464b09538797Jason Sams        // and keep track of the usage of this output by each audio stream type.
4304b3de47071d875faaa7d419d050a464b09538797Jason Sams        class AudioOutputDescriptor: public AudioPortConfig
431af12ac6a08651464f8d823add667c706f993b587Steve Block        {
4324b3de47071d875faaa7d419d050a464b09538797Jason Sams        public:
4334b3de47071d875faaa7d419d050a464b09538797Jason Sams            AudioOutputDescriptor(const sp<IOProfile>& profile);
4342382aba4a55c6ae74789c478eead8fbd96593321Jason Sams
4354b3de47071d875faaa7d419d050a464b09538797Jason Sams            status_t    dump(int fd);
4364b3de47071d875faaa7d419d050a464b09538797Jason Sams
4374b3de47071d875faaa7d419d050a464b09538797Jason Sams            audio_devices_t device() const;
4384b3de47071d875faaa7d419d050a464b09538797Jason Sams            void changeRefCount(audio_stream_type_t stream, int delta);
4392382aba4a55c6ae74789c478eead8fbd96593321Jason Sams
4404b3de47071d875faaa7d419d050a464b09538797Jason Sams            bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
4414b3de47071d875faaa7d419d050a464b09538797Jason Sams            audio_devices_t supportedDevices();
4424b3de47071d875faaa7d419d050a464b09538797Jason Sams            uint32_t latency();
4434b3de47071d875faaa7d419d050a464b09538797Jason Sams            bool sharesHwModuleWith(const sp<AudioOutputDescriptor> outputDesc);
44487fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams            bool isActive(uint32_t inPastMs = 0) const;
4452382aba4a55c6ae74789c478eead8fbd96593321Jason Sams            bool isStreamActive(audio_stream_type_t stream,
4464b3de47071d875faaa7d419d050a464b09538797Jason Sams                                uint32_t inPastMs = 0,
4474b3de47071d875faaa7d419d050a464b09538797Jason Sams                                nsecs_t sysTime = 0) const;
4489719bd4a0187c400ba868712612fe66da4635aacJason Sams            bool isStrategyActive(routing_strategy strategy,
4499719bd4a0187c400ba868712612fe66da4635aacJason Sams                             uint32_t inPastMs = 0,
4509719bd4a0187c400ba868712612fe66da4635aacJason Sams                             nsecs_t sysTime = 0) const;
4519719bd4a0187c400ba868712612fe66da4635aacJason Sams
4529719bd4a0187c400ba868712612fe66da4635aacJason Sams            virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
4539719bd4a0187c400ba868712612fe66da4635aacJason Sams                                   const struct audio_port_config *srcConfig = NULL) const;
4549719bd4a0187c400ba868712612fe66da4635aacJason Sams            void toAudioPort(struct audio_port *port) const;
4559719bd4a0187c400ba868712612fe66da4635aacJason Sams
4569719bd4a0187c400ba868712612fe66da4635aacJason Sams            audio_port_handle_t mId;
457c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            audio_io_handle_t mIoHandle;              // output handle
458c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            uint32_t mLatency;                  //
459c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            audio_output_flags_t mFlags;   //
460c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            audio_devices_t mDevice;                   // current device this output is routed to
461c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            audio_patch_handle_t mPatchHandle;
462c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output
463c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            nsecs_t mStopTime[AUDIO_STREAM_CNT];
464c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            sp<AudioOutputDescriptor> mOutput1;    // used by duplicated outputs: first output
465c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            sp<AudioOutputDescriptor> mOutput2;    // used by duplicated outputs: second output
466c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            float mCurVolume[AUDIO_STREAM_CNT];   // current stream volume
467c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            int mMuteCount[AUDIO_STREAM_CNT];     // mute request counter
468c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            const sp<IOProfile> mProfile;          // I/O profile this output derives from
469c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
470c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk                                                // device selection. See checkDeviceMuteStrategies()
471c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
472c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        };
473c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
474c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
475c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        // and keep track of the usage of this input.
476c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        class AudioInputDescriptor: public AudioPortConfig
477af12ac6a08651464f8d823add667c706f993b587Steve Block        {
478c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk        public:
479c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk            AudioInputDescriptor(const sp<IOProfile>& profile);
480c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchouk
481            status_t    dump(int fd);
482
483            audio_port_handle_t mId;
484            audio_io_handle_t mIoHandle;              // input handle
485            audio_devices_t mDevice;                    // current device this input is routed to
486            audio_patch_handle_t mPatchHandle;
487            uint32_t mRefCount;                         // number of AudioRecord clients using this output
488            uint32_t mOpenRefCount;
489            audio_source_t mInputSource;                // input source selected by application (mediarecorder.h)
490            const sp<IOProfile> mProfile;                  // I/O profile this output derives from
491            SortedVector<audio_session_t> mSessions;  // audio sessions attached to this input
492
493            virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
494                                   const struct audio_port_config *srcConfig = NULL) const;
495            void toAudioPort(struct audio_port *port) const;
496        };
497
498        // stream descriptor used for volume control
499        class StreamDescriptor
500        {
501        public:
502            StreamDescriptor();
503
504            int getVolumeIndex(audio_devices_t device);
505            void dump(int fd);
506
507            int mIndexMin;      // min volume index
508            int mIndexMax;      // max volume index
509            KeyedVector<audio_devices_t, int> mIndexCur;   // current volume index per device
510            bool mCanBeMuted;   // true is the stream can be muted
511
512            const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT];
513        };
514
515        // stream descriptor used for volume control
516        class EffectDescriptor : public RefBase
517        {
518        public:
519
520            status_t dump(int fd);
521
522            int mIo;                // io the effect is attached to
523            routing_strategy mStrategy; // routing strategy the effect is associated to
524            int mSession;               // audio session the effect is on
525            effect_descriptor_t mDesc;  // effect descriptor
526            bool mEnabled;              // enabled state: CPU load being used or not
527        };
528
529        void addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc);
530        void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
531
532        // return the strategy corresponding to a given stream type
533        static routing_strategy getStrategy(audio_stream_type_t stream);
534
535        // return appropriate device for streams handled by the specified strategy according to current
536        // phone state, connected devices...
537        // if fromCache is true, the device is returned from mDeviceForStrategy[],
538        // otherwise it is determine by current state
539        // (device connected,phone state, force use, a2dp output...)
540        // This allows to:
541        //  1 speed up process when the state is stable (when starting or stopping an output)
542        //  2 access to either current device selection (fromCache == true) or
543        // "future" device selection (fromCache == false) when called from a context
544        //  where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
545        //  before updateDevicesAndOutputs() is called.
546        virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
547                                                     bool fromCache);
548
549        // change the route of the specified output. Returns the number of ms we have slept to
550        // allow new routing to take effect in certain cases.
551        uint32_t setOutputDevice(audio_io_handle_t output,
552                             audio_devices_t device,
553                             bool force = false,
554                             int delayMs = 0,
555                             audio_patch_handle_t *patchHandle = NULL,
556                             const char* address = NULL);
557        status_t resetOutputDevice(audio_io_handle_t output,
558                                   int delayMs = 0,
559                                   audio_patch_handle_t *patchHandle = NULL);
560        status_t setInputDevice(audio_io_handle_t input,
561                                audio_devices_t device,
562                                bool force = false,
563                                audio_patch_handle_t *patchHandle = NULL);
564        status_t resetInputDevice(audio_io_handle_t input,
565                                  audio_patch_handle_t *patchHandle = NULL);
566
567        // select input device corresponding to requested audio source
568        virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
569
570        // return io handle of active input or 0 if no input is active
571        //    Only considers inputs from physical devices (e.g. main mic, headset mic) when
572        //    ignoreVirtualInputs is true.
573        audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true);
574
575        // initialize volume curves for each strategy and device category
576        void initializeVolumeCurves();
577
578        // compute the actual volume for a given stream according to the requested index and a particular
579        // device
580        virtual float computeVolume(audio_stream_type_t stream, int index,
581                                    audio_io_handle_t output, audio_devices_t device);
582
583        // check that volume change is permitted, compute and send new volume to audio hardware
584        status_t checkAndSetVolume(audio_stream_type_t stream, int index, audio_io_handle_t output,
585                                   audio_devices_t device, int delayMs = 0, bool force = false);
586
587        // apply all stream volumes to the specified output and device
588        void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
589
590        // Mute or unmute all streams handled by the specified strategy on the specified output
591        void setStrategyMute(routing_strategy strategy,
592                             bool on,
593                             audio_io_handle_t output,
594                             int delayMs = 0,
595                             audio_devices_t device = (audio_devices_t)0);
596
597        // Mute or unmute the stream on the specified output
598        void setStreamMute(audio_stream_type_t stream,
599                           bool on,
600                           audio_io_handle_t output,
601                           int delayMs = 0,
602                           audio_devices_t device = (audio_devices_t)0);
603
604        // handle special cases for sonification strategy while in call: mute streams or replace by
605        // a special tone in the device used for communication
606        void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
607
608        // true if device is in a telephony or VoIP call
609        virtual bool isInCall();
610
611        // true if given state represents a device in a telephony or VoIP call
612        virtual bool isStateInCall(int state);
613
614        // when a device is connected, checks if an open output can be routed
615        // to this device. If none is open, tries to open one of the available outputs.
616        // Returns an output suitable to this device or 0.
617        // when a device is disconnected, checks if an output is not used any more and
618        // returns its handle if any.
619        // transfers the audio tracks and effects from one output thread to another accordingly.
620        status_t checkOutputsForDevice(audio_devices_t device,
621                                       audio_policy_dev_state_t state,
622                                       SortedVector<audio_io_handle_t>& outputs,
623                                       const String8 address);
624
625        status_t checkInputsForDevice(audio_devices_t device,
626                                      audio_policy_dev_state_t state,
627                                      SortedVector<audio_io_handle_t>& inputs,
628                                      const String8 address);
629
630        // close an output and its companion duplicating output.
631        void closeOutput(audio_io_handle_t output);
632
633        // checks and if necessary changes outputs used for all strategies.
634        // must be called every time a condition that affects the output choice for a given strategy
635        // changes: connected device, phone state, force use...
636        // Must be called before updateDevicesAndOutputs()
637        void checkOutputForStrategy(routing_strategy strategy);
638
639        // Same as checkOutputForStrategy() but for a all strategies in order of priority
640        void checkOutputForAllStrategies();
641
642        // manages A2DP output suspend/restore according to phone state and BT SCO usage
643        void checkA2dpSuspend();
644
645        // returns the A2DP output handle if it is open or 0 otherwise
646        audio_io_handle_t getA2dpOutput();
647
648        // selects the most appropriate device on output for current state
649        // must be called every time a condition that affects the device choice for a given output is
650        // changed: connected device, phone state, force use, output start, output stop..
651        // see getDeviceForStrategy() for the use of fromCache parameter
652        audio_devices_t getNewOutputDevice(audio_io_handle_t output, bool fromCache);
653
654        // updates cache of device used by all strategies (mDeviceForStrategy[])
655        // must be called every time a condition that affects the device choice for a given strategy is
656        // changed: connected device, phone state, force use...
657        // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
658         // Must be called after checkOutputForAllStrategies()
659        void updateDevicesAndOutputs();
660
661        // selects the most appropriate device on input for current state
662        audio_devices_t getNewInputDevice(audio_io_handle_t input);
663
664        virtual uint32_t getMaxEffectsCpuLoad();
665        virtual uint32_t getMaxEffectsMemory();
666#ifdef AUDIO_POLICY_TEST
667        virtual     bool        threadLoop();
668                    void        exit();
669        int testOutputIndex(audio_io_handle_t output);
670#endif //AUDIO_POLICY_TEST
671
672        status_t setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled);
673
674        // returns the category the device belongs to with regard to volume curve management
675        static device_category getDeviceCategory(audio_devices_t device);
676
677        // extract one device relevant for volume control from multiple device selection
678        static audio_devices_t getDeviceForVolume(audio_devices_t device);
679
680        SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
681                        DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs);
682        bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
683                                           SortedVector<audio_io_handle_t>& outputs2);
684
685        // mute/unmute strategies using an incompatible device combination
686        // if muting, wait for the audio in pcm buffer to be drained before proceeding
687        // if unmuting, unmute only after the specified delay
688        // Returns the number of ms waited
689        uint32_t  checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
690                                            audio_devices_t prevDevice,
691                                            uint32_t delayMs);
692
693        audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
694                                       audio_output_flags_t flags);
695        // samplingRate parameter is an in/out and so may be modified
696        sp<IOProfile> getInputProfile(audio_devices_t device,
697                                   uint32_t& samplingRate,
698                                   audio_format_t format,
699                                   audio_channel_mask_t channelMask,
700                                   audio_input_flags_t flags);
701        sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
702                                                       uint32_t samplingRate,
703                                                       audio_format_t format,
704                                                       audio_channel_mask_t channelMask,
705                                                       audio_output_flags_t flags);
706
707        audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
708
709        bool isNonOffloadableEffectEnabled();
710
711        status_t addAudioPatch(audio_patch_handle_t handle,
712                               const sp<AudioPatch>& patch);
713        status_t removeAudioPatch(audio_patch_handle_t handle);
714
715        sp<AudioOutputDescriptor> getOutputFromId(audio_port_handle_t id) const;
716        sp<AudioInputDescriptor> getInputFromId(audio_port_handle_t id) const;
717        sp<HwModule> getModuleForDevice(audio_devices_t device) const;
718        sp<HwModule> getModuleFromName(const char *name) const;
719        //
720        // Audio policy configuration file parsing (audio_policy.conf)
721        //
722        static uint32_t stringToEnum(const struct StringToEnum *table,
723                                     size_t size,
724                                     const char *name);
725        static const char *enumToString(const struct StringToEnum *table,
726                                      size_t size,
727                                      uint32_t value);
728        static bool stringToBool(const char *value);
729        static audio_output_flags_t parseFlagNames(char *name);
730        static audio_devices_t parseDeviceNames(char *name);
731        void loadHwModule(cnode *root);
732        void loadHwModules(cnode *root);
733        void loadGlobalConfig(cnode *root, const sp<HwModule>& module);
734        status_t loadAudioPolicyConfig(const char *path);
735        void defaultAudioPolicyConfig(void);
736
737
738        uid_t mUidCached;
739        AudioPolicyClientInterface *mpClientInterface;  // audio policy client interface
740        audio_io_handle_t mPrimaryOutput;              // primary output handle
741        // list of descriptors for outputs currently opened
742        DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mOutputs;
743        // copy of mOutputs before setDeviceConnectionState() opens new outputs
744        // reset to mOutputs when updateDevicesAndOutputs() is called.
745        DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mPreviousOutputs;
746        DefaultKeyedVector<audio_io_handle_t, sp<AudioInputDescriptor> > mInputs;     // list of input descriptors
747        DeviceVector  mAvailableOutputDevices; // all available output devices
748        DeviceVector  mAvailableInputDevices;  // all available input devices
749        int mPhoneState;                                                    // current phone state
750        audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT];   // current forced use configuration
751
752        StreamDescriptor mStreams[AUDIO_STREAM_CNT];           // stream descriptors for volume control
753        bool    mLimitRingtoneVolume;                                       // limit ringtone volume to music volume if headset connected
754        audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
755        float   mLastVoiceVolume;                                           // last voice volume value sent to audio HAL
756
757        // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
758        static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
759        // Maximum memory allocated to audio effects in KB
760        static const uint32_t MAX_EFFECTS_MEMORY = 512;
761        uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
762        uint32_t mTotalEffectsMemory;  // current memory used by effects
763        KeyedVector<int, sp<EffectDescriptor> > mEffects;  // list of registered audio effects
764        bool    mA2dpSuspended;  // true if A2DP output is suspended
765        sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
766        bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
767                                // to boost soft sounds, used to adjust volume curves accordingly
768
769        Vector < sp<HwModule> > mHwModules;
770        volatile int32_t mNextUniqueId;
771        volatile int32_t mAudioPortGeneration;
772
773        DefaultKeyedVector<audio_patch_handle_t, sp<AudioPatch> > mAudioPatches;
774
775#ifdef AUDIO_POLICY_TEST
776        Mutex   mLock;
777        Condition mWaitWorkCV;
778
779        int             mCurOutput;
780        bool            mDirectOutput;
781        audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
782        int             mTestInput;
783        uint32_t        mTestDevice;
784        uint32_t        mTestSamplingRate;
785        uint32_t        mTestFormat;
786        uint32_t        mTestChannels;
787        uint32_t        mTestLatencyMs;
788#endif //AUDIO_POLICY_TEST
789
790private:
791        static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
792                int indexInUi);
793        // updates device caching and output for streams that can influence the
794        //    routing of notifications
795        void handleNotificationRoutingForStream(audio_stream_type_t stream);
796        static bool isVirtualInputDevice(audio_devices_t device);
797        static bool deviceDistinguishesOnAddress(audio_devices_t device);
798        // find the outputs on a given output descriptor that have the given address.
799        // to be called on an AudioOutputDescriptor whose supported devices (as defined
800        //   in mProfile->mSupportedDevices) matches the device whose address is to be matched.
801        // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
802        //   where addresses are used to distinguish between one connected device and another.
803        void findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/,
804                const String8 address /*in*/,
805                SortedVector<audio_io_handle_t>& outputs /*out*/);
806        uint32_t nextUniqueId();
807        uint32_t nextAudioPortGeneration();
808        uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
809        // internal method to return the output handle for the given device and format
810        audio_io_handle_t getOutputForDevice(
811                audio_devices_t device,
812                audio_stream_type_t stream,
813                uint32_t samplingRate,
814                audio_format_t format,
815                audio_channel_mask_t channelMask,
816                audio_output_flags_t flags,
817                const audio_offload_info_t *offloadInfo);
818        // internal function to derive a stream type value from audio attributes
819        audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
820};
821
822};
823