AudioSystem.h revision df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871
14b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis/*
24b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * Copyright (C) 2008 The Android Open Source Project
34b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis *
44b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * Licensed under the Apache License, Version 2.0 (the "License");
54b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * you may not use this file except in compliance with the License.
64b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * You may obtain a copy of the License at
74b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis *
84b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis *      http://www.apache.org/licenses/LICENSE-2.0
94b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis *
104b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * Unless required by applicable law or agreed to in writing, software
114b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * distributed under the License is distributed on an "AS IS" BASIS,
124b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * See the License for the specific language governing permissions and
140853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis * limitations under the License.
150853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis */
160853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
17eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor#ifndef ANDROID_AUDIOSYSTEM_H_
1805a07605322dfef2b017781042043a261c5a89cdSebastian Redl#define ANDROID_AUDIOSYSTEM_H_
19914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
200a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall#include <hardware/audio_effect.h>
214ae8f298b1ea51b4c2234f9148e2e4349c9bdd23Douglas Gregor#include <media/IAudioFlingerClient.h>
2231b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar#include <media/IAudioPolicyServiceClient.h>
23eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor#include <system/audio.h>
24389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis#include <system/audio_policy.h>
2587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor#include <utils/Errors.h>
2628019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include <utils/Mutex.h>
270853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
28a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregornamespace android {
29cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
3003013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencertypedef void (*audio_error_callback)(status_t err);
31788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
320853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisclass IAudioFlinger;
33f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbarclass IAudioPolicyService;
34f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbarclass String8;
354db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
36cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregorclass AudioSystem
374db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor{
384db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregorpublic:
394db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
404db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor    /* These are static methods to control the system-wide AudioFlinger
410853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis     * only privileged processes can have access to them
420853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis     */
43521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
441abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor    // mute/unmute microphone
45521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar    static status_t muteMicrophone(bool state);
46521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar    static status_t isMicrophoneMuted(bool *state);
47521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
48521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar    // set/get master volume
49521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar    static status_t setMasterVolume(float value);
50521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar    static status_t getMasterVolume(float* volume);
51521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
52521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar    // mute/unmute audio outputs
53521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar    static status_t setMasterMute(bool mute);
54d808bd2978bd4ac95a92b309b038452b533fd7a0Argyrios Kyrtzidis    static status_t getMasterMute(bool* mute);
550853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
56f96b524306ccfa623235d375deee79637bd38f29Steve Naroff    // set/get stream volume on specified output
5744c181aec37789f25f6c15543c164416f72e562aDouglas Gregor    static status_t setStreamVolume(audio_stream_type_t stream, float value,
5848601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor                                    audio_io_handle_t output);
5948601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    static status_t getStreamVolume(audio_stream_type_t stream, float* volume,
6048601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor                                    audio_io_handle_t output);
6148601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
6248601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    // mute/unmute stream
6348601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    static status_t setStreamMute(audio_stream_type_t stream, bool mute);
6448601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    static status_t getStreamMute(audio_stream_type_t stream, bool* mute);
6548601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
663c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl    // set audio mode in audio hardware
670853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis    static status_t setMode(audio_mode_t mode);
680853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
69788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    // returns true in *state if tracks are active on the specified stream or have been active
70788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    // in the past inPastMs milliseconds
71788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    static status_t isStreamActive(audio_stream_type_t stream, bool *state, uint32_t inPastMs);
72bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor    // returns true in *state if tracks are active for what qualifies as remote playback
73788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    // on the specified stream or have been active in the past inPastMs milliseconds. Remote
7428019772db70d4547be05a042eb950bc910f134fDouglas Gregor    // playback isn't mutually exclusive with local playback.
754f32786ac45210143654390177105eb749b614e9Ted Kremenek    static status_t isStreamActiveRemotely(audio_stream_type_t stream, bool *state,
764f32786ac45210143654390177105eb749b614e9Ted Kremenek            uint32_t inPastMs);
770853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis    // returns true in *state if a recorder is currently recording with the specified source
784f32786ac45210143654390177105eb749b614e9Ted Kremenek    static status_t isSourceActive(audio_source_t source, bool *state);
794f32786ac45210143654390177105eb749b614e9Ted Kremenek
804f32786ac45210143654390177105eb749b614e9Ted Kremenek    // set/get audio hardware parameters. The function accepts a list of parameters
81389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis    // key value pairs in the form: key1=value1;key2=value2;...
82389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis    // Some keys are reserved for standard parameters (See AudioParameter class).
83389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis    // The versions with audio_io_handle_t are intended for internal media framework use only.
84914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    static status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
85914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    static String8  getParameters(audio_io_handle_t ioHandle, const String8& keys);
86914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    // The versions without audio_io_handle_t are intended for JNI.
87914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    static status_t setParameters(const String8& keyValuePairs);
88914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    static String8  getParameters(const String8& keys);
89914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
90914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    static void setErrorCallback(audio_error_callback cb);
91914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor
92807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar    // helper function to obtain AudioFlinger service handle
93807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar    static const sp<IAudioFlinger>& get_audio_flinger();
944f32786ac45210143654390177105eb749b614e9Ted Kremenek
95914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor    static float linearToLog(int volume);
961aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor    static int logToLinear(float volume);
971aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor
981aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor    // Returned samplingRate and frameCount output values are guaranteed
991aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor    // to be non-zero if status == NO_ERROR
1001aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor    static status_t getOutputSamplingRate(uint32_t* samplingRate,
1011aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor            audio_stream_type_t stream);
1021aa27307c462baaa9e5fda14ff6797dd39fe8b84Douglas Gregor    static status_t getOutputSamplingRateForAttr(uint32_t* samplingRate,
1037d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor                const audio_attributes_t *attr);
1047d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor    static status_t getOutputFrameCount(size_t* frameCount,
1057d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor            audio_stream_type_t stream);
1067d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor    static status_t getOutputLatency(uint32_t* latency,
107c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar            audio_stream_type_t stream);
108abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor    static status_t getSamplingRate(audio_io_handle_t output,
109abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor                                          uint32_t* samplingRate);
110e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor    // returns the number of frames per audio HAL write buffer. Corresponds to
111df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // audio_stream->get_buffer_size()/audio_stream_out_frame_size()
112c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar    static status_t getFrameCount(audio_io_handle_t output,
113c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar                                  size_t* frameCount);
114df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // returns the audio output stream latency in ms. Corresponds to
115df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    // audio_stream_out->get_latency()
116df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    static status_t getLatency(audio_io_handle_t output,
117213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor                               uint32_t* latency);
118213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor
11915727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis    static bool routedToA2dpOutput(audio_stream_type_t streamType);
12015727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis
12115727ddb11405c45372150b5bfb07dbfa4c9960bArgyrios Kyrtzidis    // return status NO_ERROR implies *buffSize > 0
122213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor    static status_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
123f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar        audio_channel_mask_t channelMask, size_t* buffSize);
124f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
125f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    static status_t setVoiceVolume(float volume);
126f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
127f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    // return the number of audio frames written by AudioFlinger to audio HAL and
128f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    // audio dsp to DAC since the specified output I/O handle has exited standby.
129f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    // returned status (from utils/Errors.h) can be:
130f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    // - NO_ERROR: successful operation, halFrames and dspFrames point to valid data
131f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    // - INVALID_OPERATION: Not supported on current hardware platform
13289d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    // - BAD_VALUE: invalid parameter
13389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    // NOTE: this feature is not supported on all hardware platforms and it is
13489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    // necessary to check returned status before using the returned values.
13589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    static status_t getRenderPosition(audio_io_handle_t output,
13689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor                                      uint32_t *halFrames,
13789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor                                      uint32_t *dspFrames);
13889d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
13989d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    // return the number of input frames lost by HAL implementation, or 0 if the handle is invalid
14068d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar    static uint32_t getInputFramesLost(audio_io_handle_t ioHandle);
14168d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
14268d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar    // Allocate a new unique ID for use as an audio session ID or I/O handle.
143f96b524306ccfa623235d375deee79637bd38f29Steve Naroff    // If unable to contact AudioFlinger, returns AUDIO_UNIQUE_ID_ALLOCATE instead.
144f96b524306ccfa623235d375deee79637bd38f29Steve Naroff    // FIXME If AudioFlinger were to ever exhaust the unique ID namespace,
145f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    //       this method could fail by returning either AUDIO_UNIQUE_ID_ALLOCATE
146a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor    //       or an unspecified existing unique ID.
147a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor    static audio_unique_id_t newAudioUniqueId();
148405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor
149a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor    static void acquireAudioSessionId(int audioSession, pid_t pid);
1504cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor    static void releaseAudioSessionId(int audioSession, pid_t pid);
1514cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor
1524cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor    // types of io configuration change events received with ioConfigChanged()
1534cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor    enum io_config_event {
1544cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor        OUTPUT_OPENED,
1554cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor        OUTPUT_CLOSED,
1564cd912aa94656697a44c3ebb159f05060300524eDouglas Gregor        OUTPUT_CONFIG_CHANGED,
157313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor        INPUT_OPENED,
158313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor        INPUT_CLOSED,
159313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor        INPUT_CONFIG_CHANGED,
160bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor        STREAM_CONFIG_CHANGED,
161788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor        NUM_CONFIG_EVENTS
162788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    };
163788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
164788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    // audio output descriptor used to cache output configurations in client process to avoid
165788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    // frequent calls through IAudioFlinger
166788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    class OutputDescriptor {
167788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    public:
168788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor        OutputDescriptor()
169788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor        : samplingRate(0), format(AUDIO_FORMAT_DEFAULT), channelMask(0), frameCount(0), latency(0)
170bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor            {}
171bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
172bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor        uint32_t samplingRate;
173bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor        audio_format_t format;
174bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor        audio_channel_mask_t channelMask;
175bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor        size_t frameCount;
176bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor        uint32_t latency;
177bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    };
178bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
179175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor    // Events used to synchronize actions between audio sessions.
180eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor    // For instance SYNC_EVENT_PRESENTATION_COMPLETE can be used to delay recording start until
181eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor    // playback is complete on another audio session.
182eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor    // See definitions in MediaSyncEvent.java
183eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor    enum sync_event_t {
184eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor        SYNC_EVENT_SAME = -1,             // used internally to indicate restart with same event
185eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor        SYNC_EVENT_NONE = 0,
186eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor        SYNC_EVENT_PRESENTATION_COMPLETE,
187eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor
188eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor        //
189eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor        // Define new events here: SYNC_EVENT_START, SYNC_EVENT_STOP, SYNC_EVENT_TIME ...
190eababfbddb74d186f78783a9731a78ad371c9800Douglas Gregor        //
191175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor        SYNC_EVENT_CNT,
192385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor    };
193175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
194175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor    // Timeout for synchronous record start. Prevents from blocking the record thread forever
195175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor    // if the trigger event is not fired.
196175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor    static const uint32_t kSyncRecordStartTimeOutMs = 30000;
197175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
198f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    //
199f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    // IAudioPolicyService interface (see AudioPolicyInterface for method descriptions)
200f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    //
201f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    static status_t setDeviceConnectionState(audio_devices_t device, audio_policy_dev_state_t state,
202f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                                                const char *device_address);
203f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    static audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
204175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor                                                                const char *device_address);
205175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor    static status_t setPhoneState(audio_mode_t state);
206175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor    static status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
207cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    static audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
208cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
209cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    // Client must successfully hand off the handle reference to AudioFlinger via createTrack(),
210cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    // or release it with releaseOutput().
211cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    static audio_io_handle_t getOutput(audio_stream_type_t stream,
212cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor                                        uint32_t samplingRate = 0,
213cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor                                        audio_format_t format = AUDIO_FORMAT_DEFAULT,
214cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor                                        audio_channel_mask_t channelMask = AUDIO_CHANNEL_OUT_STEREO,
21528233428da1ebec20c893d6297ae3191318940ddDouglas Gregor                                        audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
21628233428da1ebec20c893d6297ae3191318940ddDouglas Gregor                                        const audio_offload_info_t *offloadInfo = NULL);
21728233428da1ebec20c893d6297ae3191318940ddDouglas Gregor    static audio_io_handle_t getOutputForAttr(const audio_attributes_t *attr,
21828233428da1ebec20c893d6297ae3191318940ddDouglas Gregor                                        uint32_t samplingRate = 0,
219c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor                                        audio_format_t format = AUDIO_FORMAT_DEFAULT,
220671947b18dba342f9aba022ee992babef325a833Douglas Gregor                                        audio_channel_mask_t channelMask = AUDIO_CHANNEL_OUT_STEREO,
221671947b18dba342f9aba022ee992babef325a833Douglas Gregor                                        audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
222671947b18dba342f9aba022ee992babef325a833Douglas Gregor                                        const audio_offload_info_t *offloadInfo = NULL);
223671947b18dba342f9aba022ee992babef325a833Douglas Gregor    static status_t startOutput(audio_io_handle_t output,
224671947b18dba342f9aba022ee992babef325a833Douglas Gregor                                audio_stream_type_t stream,
225c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor                                int session);
226c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor    static status_t stopOutput(audio_io_handle_t output,
227c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor                               audio_stream_type_t stream,
228c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor                               int session);
229c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor    static void releaseOutput(audio_io_handle_t output);
230c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
231c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor    // Client must successfully hand off the handle reference to AudioFlinger via openRecord(),
232c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor    // or release it with releaseInput().
233c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor    static audio_io_handle_t getInput(audio_source_t inputSource,
234c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor                                    uint32_t samplingRate,
235c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor                                    audio_format_t format,
236c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor                                    audio_channel_mask_t channelMask,
237c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor                                    int sessionId,
238c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor                                    audio_input_flags_t);
239c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor
240c0659ec614c428c7d15746fcad15d50a2703751dDouglas Gregor    static status_t startInput(audio_io_handle_t input,
2413c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl                               audio_session_t session);
242eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    static status_t stopInput(audio_io_handle_t input,
2438538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                              audio_session_t session);
244eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    static void releaseInput(audio_io_handle_t input,
24589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor                             audio_session_t session);
24689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    static status_t initStreamVolume(audio_stream_type_t stream,
24789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor                                      int indexMin,
24889d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor                                      int indexMax);
24987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static status_t setStreamVolumeIndex(audio_stream_type_t stream,
25087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                         int index,
25187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                         audio_devices_t device);
252dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor    static status_t getStreamVolumeIndex(audio_stream_type_t stream,
253dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor                                         int *index,
254dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor                                         audio_devices_t device);
255dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor
256e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor    static uint32_t getStrategyForStream(audio_stream_type_t stream);
2570b53cf834346d78985aaa9e7300445a39c245614Douglas Gregor    static audio_devices_t getDevicesForStream(audio_stream_type_t stream);
258e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor
259e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor    static audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
26087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static status_t registerEffect(const effect_descriptor_t *desc,
26187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                    audio_io_handle_t io,
26287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                    uint32_t strategy,
26387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                    int session,
26487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor                                    int id);
26587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static status_t unregisterEffect(int id);
26687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static status_t setEffectEnabled(int id, bool enabled);
26787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
26887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    // clear stream to output mapping cache (gStreamOutputMap)
26987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    // and output configuration cache (gOutputs)
27087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static void clearAudioConfigCache();
27187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
27287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static const sp<IAudioPolicyService>& get_audio_policy_service();
27387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
27487c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    // helpers for android.media.AudioManager.getProperty(), see description there for meaning
27587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static uint32_t getPrimaryOutputSamplingRate();
27687c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static size_t getPrimaryOutputFrameCount();
27787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
27887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static status_t setLowRamDevice(bool isLowRamDevice);
27987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
28087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    // Check if hw offload is possible for given format, stream type, sample rate,
28187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    // bit rate, duration, video and streaming or offload property is enabled
28287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static bool isOffloadSupported(const audio_offload_info_t& info);
28387c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
2841827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    // check presence of audio flinger service.
28558ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    // returns NO_ERROR if binding to service succeeds, DEAD_OBJECT otherwise
28658ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor    static status_t checkAudioFlinger();
28758ddb60f409125eda5436c4a1f070f7fa4744295Douglas Gregor
2881827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    /* List available audio ports and their attributes */
2891827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    static status_t listAudioPorts(audio_port_role_t role,
290f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor                                   audio_port_type_t type,
291f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor                                   unsigned int *num_ports,
292f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor                                   struct audio_port *ports,
293f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor                                   unsigned int *generation);
294f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
295f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /* Get attributes for a given audio port */
296f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    static status_t getAudioPort(struct audio_port *port);
297f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
29887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    /* Create an audio patch between several source and sink ports */
29987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    static status_t createAudioPatch(const struct audio_patch *patch,
300f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor                                       audio_patch_handle_t *handle);
301f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
302f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /* Release an audio patch */
303f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    static status_t releaseAudioPatch(audio_patch_handle_t handle);
304f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
305f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    /* List existing audio patches */
30648601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    static status_t listAudioPatches(unsigned int *num_patches,
30748601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor                                      struct audio_patch *patches,
30848601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor                                      unsigned int *generation);
30948601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    /* Set audio port configuration */
31048601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor    static status_t setAudioPortConfig(const struct audio_port_config *config);
31148601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor
31287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
313218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor    static status_t acquireSoundTriggerSession(audio_session_t *session,
31448601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor                                           audio_io_handle_t *ioHandle,
31548601b32321496b07a18fb6631f8563275d8c5fbDouglas Gregor                                           audio_devices_t *device);
316218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor    static status_t releaseSoundTriggerSession(audio_session_t session);
31787c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
31887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    // ----------------------------------------------------------------------------
31987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
320f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    class AudioPortCallback : public RefBase
321f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    {
322f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor    public:
323f5586f6b311c98e1022a8fe0609053849b70d323Douglas Gregor
3249b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor                AudioPortCallback() {}
3259b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor        virtual ~AudioPortCallback() {}
326727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor
3279b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor        virtual void onAudioPortListUpdate() = 0;
3289b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor        virtual void onAudioPatchListUpdate() = 0;
3299b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor        virtual void onServiceDied() = 0;
330727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor
3319b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor    };
3329b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
333727d93ef49e18147149354fadd10e86b13bc4ab0Douglas Gregor    static void setAudioPortCallback(sp<AudioPortCallback> callBack);
3349b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
3359b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregorprivate:
3369b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor
337ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar    class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient
3389b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor    {
3399b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor    public:
3409b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor        AudioFlingerClient() {
3419b7db6200d366e4964d63ae1f33c7b9d7b9831cbDouglas Gregor        }
342ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
343ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar        // DeathRecipient
344ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar        virtual void binderDied(const wp<IBinder>& who);
345ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar
346c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor        // IAudioFlingerClient
347c4421e966d77a18f815284175b3fcbb46f36fa39Douglas Gregor
34887c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor        // indicate a change in the configuration of an output or input: keeps the cached
34987c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor        // values for output/input parameters up-to-date in client process
35087c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor        virtual void ioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2);
35187c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor    };
35287c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor
35331b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar    class AudioPolicyServiceClient: public IBinder::DeathRecipient,
35431b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar                                    public BnAudioPolicyServiceClient
355bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
3563687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor    public:
3578b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor        AudioPolicyServiceClient() {
358abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor        }
359754f3490c5b0f5d83361f001bc87944f23644abbDouglas Gregor
360175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor        // DeathRecipient
361f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor        virtual void binderDied(const wp<IBinder>& who);
362df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
363df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor        // IAudioPolicyServiceClient
364175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor        virtual void onAudioPortListUpdate();
365df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor        virtual void onAudioPatchListUpdate();
36601b6e31a62e2265849f4388b9be6be0a5d13348dDouglas Gregor    };
367df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor
368df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor    static sp<AudioFlingerClient> gAudioFlingerClient;
369eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor    static sp<AudioPolicyServiceClient> gAudioPolicyServiceClient;
37089d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    friend class AudioFlingerClient;
37189d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    friend class AudioPolicyServiceClient;
3720853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
373bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static Mutex gLock;
374bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static sp<IAudioFlinger> gAudioFlinger;
375bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static audio_error_callback gAudioErrorCallback;
376bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
377bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static size_t gInBuffSize;
378bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    // previous parameters for recording buffer size queries
379bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static uint32_t gPrevInSamplingRate;
380bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static audio_format_t gPrevInFormat;
381bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static audio_channel_mask_t gPrevInChannelMask;
382bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
383bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static sp<IAudioPolicyService> gAudioPolicyService;
384bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
385bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    // list of output descriptors containing cached parameters
386bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    // (sampling rate, framecount, channel count...)
387bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static DefaultKeyedVector<audio_io_handle_t, OutputDescriptor *> gOutputs;
388bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
389bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    static sp<AudioPortCallback> gAudioPortCallback;
390bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor};
3910853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
3920853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};  // namespace android
393c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
394c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar#endif  /*ANDROID_AUDIOSYSTEM_H_*/
395ea94bbc4769697143e717df9b0310f874102b6c1Daniel Dunbar