AudioPolicyService.h revision de3f8392fbf380ba6f09d009b00d7172477389a2
140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin/*
240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin * Copyright (C) 2009 The Android Open Source Project
340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin *
440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin * Licensed under the Apache License, Version 2.0 (the "License");
540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin * you may not use this file except in compliance with the License.
640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin * You may obtain a copy of the License at
740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin *
840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin *      http://www.apache.org/licenses/LICENSE-2.0
940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin *
1040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin * Unless required by applicable law or agreed to in writing, software
1140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin * distributed under the License is distributed on an "AS IS" BASIS,
1240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin * See the License for the specific language governing permissions and
1440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin * limitations under the License.
1540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin */
1640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
1740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#ifndef ANDROID_AUDIOPOLICYSERVICE_H
1840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#define ANDROID_AUDIOPOLICYSERVICE_H
1940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
2040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <cutils/misc.h>
2140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <cutils/config_utils.h>
2240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <cutils/compiler.h>
2340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <utils/String8.h>
2440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <utils/Vector.h>
2540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <utils/SortedVector.h>
2640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <binder/BinderService.h>
2740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <system/audio.h>
2840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <system/audio_policy.h>
2940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <hardware/audio_policy.h>
3040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <media/IAudioPolicyService.h>
3140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <media/ToneGenerator.h>
3240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <media/AudioEffect.h>
3340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include <hardware_legacy/AudioPolicyInterface.h>
3440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include "AudioPolicyEffects.h"
3540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin#include "AudioPolicyManager.h"
3640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
3740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
3840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkinnamespace android {
3940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
4040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin// ----------------------------------------------------------------------------
4140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
4240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkinclass AudioPolicyService :
4340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    public BinderService<AudioPolicyService>,
4440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    public BnAudioPolicyService,
4540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    public IBinder::DeathRecipient
4640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin{
4740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    friend class BinderService<AudioPolicyService>;
4840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
4940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkinpublic:
5040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    // for BinderService
5140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    static const char *getServiceName() ANDROID_API { return "media.audio_policy"; }
5240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
5340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t    dump(int fd, const Vector<String16>& args);
5440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
5540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    //
5640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
5740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    //
5840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
5940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t setDeviceConnectionState(audio_devices_t device,
6040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                              audio_policy_dev_state_t state,
6140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                              const char *device_address);
6240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual audio_policy_dev_state_t getDeviceConnectionState(
6340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                                                audio_devices_t device,
6440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                                                const char *device_address);
6540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t setPhoneState(audio_mode_t state);
6640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
6740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
6840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
6940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                        uint32_t samplingRate = 0,
7040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                        audio_format_t format = AUDIO_FORMAT_DEFAULT,
7140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                        audio_channel_mask_t channelMask = 0,
7240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                        audio_output_flags_t flags =
7340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                                AUDIO_OUTPUT_FLAG_NONE,
7440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                        const audio_offload_info_t *offloadInfo = NULL);
7540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual audio_io_handle_t getOutputForAttr(const audio_attributes_t *attr,
7640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                            uint32_t samplingRate = 0,
7740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                            audio_format_t format = AUDIO_FORMAT_DEFAULT,
7840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                            audio_channel_mask_t channelMask = 0,
7940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                            audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
8040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                            const audio_offload_info_t *offloadInfo = NULL);
8140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t startOutput(audio_io_handle_t output,
8240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                 audio_stream_type_t stream,
8340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                 int session = 0);
8440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t stopOutput(audio_io_handle_t output,
8540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                audio_stream_type_t stream,
8640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                int session = 0);
8740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual void releaseOutput(audio_io_handle_t output);
8840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual audio_io_handle_t getInput(audio_source_t inputSource,
8940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    uint32_t samplingRate,
9040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    audio_format_t format,
9140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    audio_channel_mask_t channelMask,
9240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    int audioSession,
9340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    audio_input_flags_t flags);
9440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t startInput(audio_io_handle_t input);
9540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t stopInput(audio_io_handle_t input);
9640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual void releaseInput(audio_io_handle_t input);
9740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t initStreamVolume(audio_stream_type_t stream,
9840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                      int indexMin,
9940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                      int indexMax);
10040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
10140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                          int index,
10240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                          audio_devices_t device);
10340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
10440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                          int *index,
10540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                          audio_devices_t device);
10640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
10740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
10840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
10940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
11040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
11140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t registerEffect(const effect_descriptor_t *desc,
11240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    audio_io_handle_t io,
11340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    uint32_t strategy,
11440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    int session,
11540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    int id);
11640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t unregisterEffect(int id);
11740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t setEffectEnabled(int id, bool enabled);
11840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
11940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
12040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual bool isSourceActive(audio_source_t source) const;
12140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
12240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t queryDefaultPreProcessing(int audioSession,
12340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                              effect_descriptor_t *descriptors,
12440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                              uint32_t *count);
12540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual     status_t    onTransact(
12640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                uint32_t code,
12740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                const Parcel& data,
12840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                Parcel* reply,
12940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                uint32_t flags);
13040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
13140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    // IBinder::DeathRecipient
13240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual     void        binderDied(const wp<IBinder>& who);
13340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
13440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    //
13540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    // Helpers for the struct audio_policy_service_ops implementation.
13640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    // This is used by the audio policy manager for certain operations that
13740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    // are implemented by the policy service.
13840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    //
13940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual void setParameters(audio_io_handle_t ioHandle,
14040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                               const char *keyValuePairs,
14140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                               int delayMs);
14240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
14340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t setStreamVolume(audio_stream_type_t stream,
14440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                     float volume,
14540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                     audio_io_handle_t output,
14640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                     int delayMs = 0);
14740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
14840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t stopTone();
14940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t setVoiceVolume(float volume, int delayMs = 0);
15040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual bool isOffloadSupported(const audio_offload_info_t &config);
15140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
15240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t listAudioPorts(audio_port_role_t role,
15340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    audio_port_type_t type,
15440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    unsigned int *num_ports,
15540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    struct audio_port *ports,
15640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                    unsigned int *generation);
15740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t getAudioPort(struct audio_port *port);
15840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t createAudioPatch(const struct audio_patch *patch,
15940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                       audio_patch_handle_t *handle);
16040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
16140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t listAudioPatches(unsigned int *num_patches,
16240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                      struct audio_patch *patches,
16340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                      unsigned int *generation);
16440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual status_t setAudioPortConfig(const struct audio_port_config *config);
16540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
16640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual void registerClient(const sp<IAudioPolicyServiceClient>& client);
16740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
16840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            status_t doStopOutput(audio_io_handle_t output,
16940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                  audio_stream_type_t stream,
17040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                  int session = 0);
17140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            void doReleaseOutput(audio_io_handle_t output);
17240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
17340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            status_t clientCreateAudioPatch(const struct audio_patch *patch,
17440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                      audio_patch_handle_t *handle,
17540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                      int delayMs);
17640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            status_t clientReleaseAudioPatch(audio_patch_handle_t handle,
17740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                             int delayMs);
17840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config,
17940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                                                      int delayMs);
18040602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
18140602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            void removeNotificationClient(uid_t uid);
18240602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            void onAudioPortListUpdate();
18340602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            void doOnAudioPortListUpdate();
18440602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            void onAudioPatchListUpdate();
18540602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin            void doOnAudioPatchListUpdate();
18640602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin
18740602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkinprivate:
18840602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin                        AudioPolicyService() ANDROID_API;
18940602741ae87e6bf368c17dd28db4d2db344bdedIgor Murashkin    virtual             ~AudioPolicyService();
190
191            status_t dumpInternals(int fd);
192
193    // Thread used for tone playback and to send audio config commands to audio flinger
194    // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
195    // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
196    // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
197    // For audio config commands, it is necessary because audio flinger requires that the calling
198    // process (user) has permission to modify audio settings.
199    class AudioCommandThread : public Thread {
200        class AudioCommand;
201    public:
202
203        // commands for tone AudioCommand
204        enum {
205            START_TONE,
206            STOP_TONE,
207            SET_VOLUME,
208            SET_PARAMETERS,
209            SET_VOICE_VOLUME,
210            STOP_OUTPUT,
211            RELEASE_OUTPUT,
212            CREATE_AUDIO_PATCH,
213            RELEASE_AUDIO_PATCH,
214            UPDATE_AUDIOPORT_LIST,
215            UPDATE_AUDIOPATCH_LIST,
216            SET_AUDIOPORT_CONFIG,
217        };
218
219        AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
220        virtual             ~AudioCommandThread();
221
222                    status_t    dump(int fd);
223
224        // Thread virtuals
225        virtual     void        onFirstRef();
226        virtual     bool        threadLoop();
227
228                    void        exit();
229                    void        startToneCommand(ToneGenerator::tone_type type,
230                                                 audio_stream_type_t stream);
231                    void        stopToneCommand();
232                    status_t    volumeCommand(audio_stream_type_t stream, float volume,
233                                            audio_io_handle_t output, int delayMs = 0);
234                    status_t    parametersCommand(audio_io_handle_t ioHandle,
235                                            const char *keyValuePairs, int delayMs = 0);
236                    status_t    voiceVolumeCommand(float volume, int delayMs = 0);
237                    void        stopOutputCommand(audio_io_handle_t output,
238                                                  audio_stream_type_t stream,
239                                                  int session);
240                    void        releaseOutputCommand(audio_io_handle_t output);
241                    status_t    sendCommand(sp<AudioCommand>& command, int delayMs = 0);
242                    void        insertCommand_l(sp<AudioCommand>& command, int delayMs = 0);
243                    status_t    createAudioPatchCommand(const struct audio_patch *patch,
244                                                        audio_patch_handle_t *handle,
245                                                        int delayMs);
246                    status_t    releaseAudioPatchCommand(audio_patch_handle_t handle,
247                                                         int delayMs);
248                    void        updateAudioPortListCommand();
249                    void        updateAudioPatchListCommand();
250                    status_t    setAudioPortConfigCommand(const struct audio_port_config *config,
251                                                          int delayMs);
252                    void        insertCommand_l(AudioCommand *command, int delayMs = 0);
253
254    private:
255        class AudioCommandData;
256
257        // descriptor for requested tone playback event
258        class AudioCommand: public RefBase {
259
260        public:
261            AudioCommand()
262            : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {}
263
264            void dump(char* buffer, size_t size);
265
266            int mCommand;   // START_TONE, STOP_TONE ...
267            nsecs_t mTime;  // time stamp
268            Mutex mLock;    // mutex associated to mCond
269            Condition mCond; // condition for status return
270            status_t mStatus; // command status
271            bool mWaitStatus; // true if caller is waiting for status
272            sp<AudioCommandData> mParam;     // command specific parameter data
273        };
274
275        class AudioCommandData: public RefBase {
276        public:
277            virtual ~AudioCommandData() {}
278        protected:
279            AudioCommandData() {}
280        };
281
282        class ToneData : public AudioCommandData {
283        public:
284            ToneGenerator::tone_type mType; // tone type (START_TONE only)
285            audio_stream_type_t mStream;    // stream type (START_TONE only)
286        };
287
288        class VolumeData : public AudioCommandData {
289        public:
290            audio_stream_type_t mStream;
291            float mVolume;
292            audio_io_handle_t mIO;
293        };
294
295        class ParametersData : public AudioCommandData {
296        public:
297            audio_io_handle_t mIO;
298            String8 mKeyValuePairs;
299        };
300
301        class VoiceVolumeData : public AudioCommandData {
302        public:
303            float mVolume;
304        };
305
306        class StopOutputData : public AudioCommandData {
307        public:
308            audio_io_handle_t mIO;
309            audio_stream_type_t mStream;
310            int mSession;
311        };
312
313        class ReleaseOutputData : public AudioCommandData {
314        public:
315            audio_io_handle_t mIO;
316        };
317
318        class CreateAudioPatchData : public AudioCommandData {
319        public:
320            struct audio_patch mPatch;
321            audio_patch_handle_t mHandle;
322        };
323
324        class ReleaseAudioPatchData : public AudioCommandData {
325        public:
326            audio_patch_handle_t mHandle;
327        };
328
329        class SetAudioPortConfigData : public AudioCommandData {
330        public:
331            struct audio_port_config mConfig;
332        };
333
334        Mutex   mLock;
335        Condition mWaitWorkCV;
336        Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands
337        ToneGenerator *mpToneGenerator;     // the tone generator
338        sp<AudioCommand> mLastCommand;      // last processed command (used by dump)
339        String8 mName;                      // string used by wake lock fo delayed commands
340        wp<AudioPolicyService> mService;
341    };
342
343    class AudioPolicyClient : public AudioPolicyClientInterface
344    {
345     public:
346        AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {}
347        virtual ~AudioPolicyClient() {}
348
349        //
350        // Audio HW module functions
351        //
352
353        // loads a HW module.
354        virtual audio_module_handle_t loadHwModule(const char *name);
355
356        //
357        // Audio output Control functions
358        //
359
360        // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
361        // in case the audio policy manager has no specific requirements for the output being opened.
362        // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
363        // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
364        virtual status_t openOutput(audio_module_handle_t module,
365                                    audio_io_handle_t *output,
366                                    audio_config_t *config,
367                                    audio_devices_t *devices,
368                                    const String8& address,
369                                    uint32_t *latencyMs,
370                                    audio_output_flags_t flags);
371        // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
372        // a special mixer thread in the AudioFlinger.
373        virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
374        // closes the output stream
375        virtual status_t closeOutput(audio_io_handle_t output);
376        // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
377        // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
378        virtual status_t suspendOutput(audio_io_handle_t output);
379        // restores a suspended output.
380        virtual status_t restoreOutput(audio_io_handle_t output);
381
382        //
383        // Audio input Control functions
384        //
385
386        // opens an audio input
387        virtual audio_io_handle_t openInput(audio_module_handle_t module,
388                                            audio_io_handle_t *input,
389                                            audio_config_t *config,
390                                            audio_devices_t *devices,
391                                            const String8& address,
392                                            audio_source_t source,
393                                            audio_input_flags_t flags);
394        // closes an audio input
395        virtual status_t closeInput(audio_io_handle_t input);
396        //
397        // misc control functions
398        //
399
400        // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
401        // for each output (destination device) it is attached to.
402        virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0);
403
404        // invalidate a stream type, causing a reroute to an unspecified new output
405        virtual status_t invalidateStream(audio_stream_type_t stream);
406
407        // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
408        virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);
409        // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
410        virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
411
412        // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
413        // over a telephony device during a phone call.
414        virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
415        virtual status_t stopTone();
416
417        // set down link audio volume.
418        virtual status_t setVoiceVolume(float volume, int delayMs = 0);
419
420        // move effect to the specified output
421        virtual status_t moveEffects(int session,
422                                         audio_io_handle_t srcOutput,
423                                         audio_io_handle_t dstOutput);
424
425        /* Create a patch between several source and sink ports */
426        virtual status_t createAudioPatch(const struct audio_patch *patch,
427                                           audio_patch_handle_t *handle,
428                                           int delayMs);
429
430        /* Release a patch */
431        virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
432                                           int delayMs);
433
434        /* Set audio port configuration */
435        virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs);
436
437        virtual void onAudioPortListUpdate();
438        virtual void onAudioPatchListUpdate();
439
440        virtual audio_unique_id_t newAudioUniqueId();
441
442     private:
443        AudioPolicyService *mAudioPolicyService;
444    };
445
446    // --- Notification Client ---
447    class NotificationClient : public IBinder::DeathRecipient {
448    public:
449                            NotificationClient(const sp<AudioPolicyService>& service,
450                                                const sp<IAudioPolicyServiceClient>& client,
451                                                uid_t uid);
452        virtual             ~NotificationClient();
453
454                            void        onAudioPortListUpdate();
455                            void        onAudioPatchListUpdate();
456
457                // IBinder::DeathRecipient
458                virtual     void        binderDied(const wp<IBinder>& who);
459
460    private:
461                            NotificationClient(const NotificationClient&);
462                            NotificationClient& operator = (const NotificationClient&);
463
464        const wp<AudioPolicyService>        mService;
465        const uid_t                         mUid;
466        const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient;
467    };
468
469    // Internal dump utilities.
470    status_t dumpPermissionDenial(int fd);
471
472
473    mutable Mutex mLock;    // prevents concurrent access to AudioPolicy manager functions changing
474                            // device connection state  or routing
475    sp<AudioCommandThread> mAudioCommandThread;     // audio commands thread
476    sp<AudioCommandThread> mTonePlaybackThread;     // tone playback thread
477    sp<AudioCommandThread> mOutputCommandThread;    // process stop and release output
478    struct audio_policy_device *mpAudioPolicyDev;
479    struct audio_policy *mpAudioPolicy;
480    AudioPolicyInterface *mAudioPolicyManager;
481    AudioPolicyClient *mAudioPolicyClient;
482
483    DefaultKeyedVector< uid_t, sp<NotificationClient> >    mNotificationClients;
484
485    // Manage all effects configured in audio_effects.conf
486    sp<AudioPolicyEffects> mAudioPolicyEffects;
487};
488
489}; // namespace android
490
491#endif // ANDROID_AUDIOPOLICYSERVICE_H
492