audio/media: convert to using the audio HAL and new audio defs

Change-Id: Ibc637918637329e4f2b62f4ac7781102fbc269f5
Signed-off-by: Dima Zavin <dima@android.com>
diff --git a/services/audioflinger/Android.mk b/services/audioflinger/Android.mk
index 6d78614..2222e8b 100644
--- a/services/audioflinger/Android.mk
+++ b/services/audioflinger/Android.mk
@@ -15,9 +15,12 @@
     libutils \
     libbinder \
     libmedia \
+    libhardware \
     libhardware_legacy \
     libeffects
 
+LOCAL_STATIC_LIBRARIES := \
+    libmedia_helper
 
 ifeq ($(TARGET_SIMULATOR),true)
  LOCAL_LDLIBS += -ldl
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index 04cfa08..301f467 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -33,6 +33,7 @@
 #include <utils/threads.h>
 #include <utils/Atomic.h>
 
+#include <cutils/bitops.h>
 #include <cutils/properties.h>
 
 #include <media/AudioTrack.h>
@@ -41,15 +42,13 @@
 
 #include <private/media/AudioTrackShared.h>
 #include <private/media/AudioEffectShared.h>
-#include <hardware_legacy/AudioHardwareInterface.h>
+
+#include <hardware/audio.h>
+#include <hardware/audio_hal.h>
 
 #include "AudioMixer.h"
 #include "AudioFlinger.h"
 
-#ifdef WITH_A2DP
-#include "A2dpAudioInterface.h"
-#endif
-
 #include <media/EffectsFactoryApi.h>
 #include <media/EffectVisualizerApi.h>
 
@@ -141,22 +140,37 @@
 
 AudioFlinger::AudioFlinger()
     : BnAudioFlinger(),
-        mAudioHardware(0), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1)
+        mAudioHardwareDev(0), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1)
 {
+    const hw_module_t *module;
+    int rc;
+    char mod_name[PATH_MAX];
+
     Mutex::Autolock _l(mLock);
 
     mHardwareStatus = AUDIO_HW_IDLE;
 
-    mAudioHardware = AudioHardwareInterface::create();
+    /* get the audio hw module and create an audio_hw device */
+    snprintf(mod_name, PATH_MAX, "%s.%s", AUDIO_HARDWARE_MODULE_ID, "primary");
+    rc = hw_get_module(mod_name, &module);
+    if (rc)
+        return;
+
+    rc = audio_hw_device_open(module, &mAudioHardwareDev);
+    LOGE_IF(rc, "couldn't open audio hw device (%s)", strerror(-rc));
+    if (rc)
+        return;
 
     mHardwareStatus = AUDIO_HW_INIT;
-    if (mAudioHardware->initCheck() == NO_ERROR) {
+
+    rc = mAudioHardwareDev->init_check(mAudioHardwareDev);
+    if (rc == 0) {
         AutoMutex lock(mHardwareLock);
-        mMode = AudioSystem::MODE_NORMAL;
+        mMode = AUDIO_MODE_NORMAL;
         mHardwareStatus = AUDIO_HW_SET_MODE;
-        mAudioHardware->setMode(mMode);
+        mAudioHardwareDev->set_mode(mAudioHardwareDev, mMode);
         mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
-        mAudioHardware->setMasterVolume(1.0f);
+        mAudioHardwareDev->set_master_volume(mAudioHardwareDev, 1.0f);
         mHardwareStatus = AUDIO_HW_IDLE;
     } else {
         LOGE("Couldn't even initialize the stubbed audio hardware!");
@@ -173,8 +187,8 @@
         // closeOutput() will remove first entry from mPlaybackThreads
         closeOutput(mPlaybackThreads.keyAt(0));
     }
-    if (mAudioHardware) {
-        delete mAudioHardware;
+    if (mAudioHardwareDev) {
+        audio_hw_device_close(mAudioHardwareDev);
     }
 }
 
@@ -277,8 +291,8 @@
             mRecordThreads.valueAt(i)->dump(fd, args);
         }
 
-        if (mAudioHardware) {
-            mAudioHardware->dumpState(fd, args);
+        if (mAudioHardwareDev) {
+            mAudioHardwareDev->dump(mAudioHardwareDev, fd);
         }
         if (locked) mLock.unlock();
     }
@@ -309,7 +323,7 @@
     status_t lStatus;
     int lSessionId;
 
-    if (streamType >= AudioSystem::NUM_STREAM_TYPES) {
+    if (streamType >= AUDIO_STREAM_CNT) {
         LOGE("invalid stream type");
         lStatus = BAD_VALUE;
         goto Exit;
@@ -335,7 +349,7 @@
         }
 
         LOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
-        if (sessionId != NULL && *sessionId != AudioSystem::SESSION_OUTPUT_MIX) {
+        if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
             for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
                 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
                 if (mPlaybackThreads.keyAt(i) != output) {
@@ -454,7 +468,7 @@
     { // scope for the lock
         AutoMutex lock(mHardwareLock);
         mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
-        if (mAudioHardware->setMasterVolume(value) == NO_ERROR) {
+        if (mAudioHardwareDev->set_master_volume(mAudioHardwareDev, value) == NO_ERROR) {
             value = 1.0f;
         }
         mHardwareStatus = AUDIO_HW_IDLE;
@@ -476,7 +490,7 @@
     if (!settingsAllowed()) {
         return PERMISSION_DENIED;
     }
-    if ((mode < 0) || (mode >= AudioSystem::NUM_MODES)) {
+    if ((mode < 0) || (mode >= AUDIO_MODE_CNT)) {
         LOGW("Illegal value: setMode(%d)", mode);
         return BAD_VALUE;
     }
@@ -484,7 +498,7 @@
     { // scope for the lock
         AutoMutex lock(mHardwareLock);
         mHardwareStatus = AUDIO_HW_SET_MODE;
-        ret = mAudioHardware->setMode(mode);
+        ret = mAudioHardwareDev->set_mode(mAudioHardwareDev, mode);
         mHardwareStatus = AUDIO_HW_IDLE;
     }
 
@@ -507,16 +521,16 @@
 
     AutoMutex lock(mHardwareLock);
     mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
-    status_t ret = mAudioHardware->setMicMute(state);
+    status_t ret = mAudioHardwareDev->set_mic_mute(mAudioHardwareDev, state);
     mHardwareStatus = AUDIO_HW_IDLE;
     return ret;
 }
 
 bool AudioFlinger::getMicMute() const
 {
-    bool state = AudioSystem::MODE_INVALID;
+    bool state = AUDIO_MODE_INVALID;
     mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
-    mAudioHardware->getMicMute(&state);
+    mAudioHardwareDev->get_mic_mute(mAudioHardwareDev, &state);
     mHardwareStatus = AUDIO_HW_IDLE;
     return state;
 }
@@ -553,7 +567,7 @@
         return PERMISSION_DENIED;
     }
 
-    if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES) {
+    if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT) {
         return BAD_VALUE;
     }
 
@@ -586,8 +600,8 @@
         return PERMISSION_DENIED;
     }
 
-    if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES ||
-        uint32_t(stream) == AudioSystem::ENFORCED_AUDIBLE) {
+    if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT ||
+        uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
         return BAD_VALUE;
     }
 
@@ -601,7 +615,7 @@
 
 float AudioFlinger::streamVolume(int stream, int output) const
 {
-    if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES) {
+    if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT) {
         return 0.0f;
     }
 
@@ -622,7 +636,7 @@
 
 bool AudioFlinger::streamMute(int stream) const
 {
-    if (stream < 0 || stream >= (int)AudioSystem::NUM_STREAM_TYPES) {
+    if (stream < 0 || stream >= (int)AUDIO_STREAM_CNT) {
         return true;
     }
 
@@ -644,7 +658,7 @@
     if (ioHandle == 0) {
         AutoMutex lock(mHardwareLock);
         mHardwareStatus = AUDIO_SET_PARAMETER;
-        result = mAudioHardware->setParameters(keyValuePairs);
+        result = mAudioHardwareDev->set_parameters(mAudioHardwareDev, keyValuePairs.string());
         mHardwareStatus = AUDIO_HW_IDLE;
         return result;
     }
@@ -672,7 +686,13 @@
 //            ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid());
 
     if (ioHandle == 0) {
-        return mAudioHardware->getParameters(keys);
+        char *s;
+        String8 out_s8;
+
+        s = mAudioHardwareDev->get_parameters(mAudioHardwareDev, keys.string());
+        out_s8 = String8(s);
+        free(s);
+        return out_s8;
     }
 
     Mutex::Autolock _l(mLock);
@@ -690,7 +710,7 @@
 
 size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
 {
-    return mAudioHardware->getInputBufferSize(sampleRate, format, channelCount);
+    return mAudioHardwareDev->get_input_buffer_size(mAudioHardwareDev, sampleRate, format, channelCount);
 }
 
 unsigned int AudioFlinger::getInputFramesLost(int ioHandle)
@@ -717,7 +737,7 @@
 
     AutoMutex lock(mHardwareLock);
     mHardwareStatus = AUDIO_SET_VOICE_VOLUME;
-    status_t ret = mAudioHardware->setVoiceVolume(value);
+    status_t ret = mAudioHardwareDev->set_voice_volume(mAudioHardwareDev, value);
     mHardwareStatus = AUDIO_HW_IDLE;
 
     return ret;
@@ -957,7 +977,7 @@
 
 // ----------------------------------------------------------------------------
 
-AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device)
+AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, struct audio_stream_out* output, int id, uint32_t device)
     :   ThreadBase(audioFlinger, id),
         mMixBuffer(0), mSuspended(0), mBytesWritten(0), mOutput(output),
         mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
@@ -968,7 +988,7 @@
     mMasterVolume = mAudioFlinger->masterVolume();
     mMasterMute = mAudioFlinger->masterMute();
 
-    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
+    for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
         mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream);
         mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream);
     }
@@ -1131,12 +1151,12 @@
         // conflicts will happen when tracks are moved from one output to another by audio policy
         // manager
         uint32_t strategy =
-                AudioSystem::getStrategyForStream((AudioSystem::stream_type)streamType);
+                AudioSystem::getStrategyForStream((audio_stream_type_t)streamType);
         for (size_t i = 0; i < mTracks.size(); ++i) {
             sp<Track> t = mTracks[i];
             if (t != 0) {
                 if (sessionId == t->sessionId() &&
-                        strategy != AudioSystem::getStrategyForStream((AudioSystem::stream_type)t->type())) {
+                        strategy != AudioSystem::getStrategyForStream((audio_stream_type_t)t->type())) {
                     lStatus = BAD_VALUE;
                     goto Exit;
                 }
@@ -1155,7 +1175,7 @@
         if (chain != 0) {
             LOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
             track->setMainBuffer(chain->inBuffer());
-            chain->setStrategy(AudioSystem::getStrategyForStream((AudioSystem::stream_type)track->type()));
+            chain->setStrategy(AudioSystem::getStrategyForStream((audio_stream_type_t)track->type()));
         }
     }
     lStatus = NO_ERROR;
@@ -1170,7 +1190,7 @@
 uint32_t AudioFlinger::PlaybackThread::latency() const
 {
     if (mOutput) {
-        return mOutput->latency();
+        return mOutput->get_latency(mOutput);
     }
     else {
         return 0;
@@ -1264,7 +1284,13 @@
 
 String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
 {
-    return mOutput->getParameters(keys);
+    String8 out_s8;
+    char *s;
+
+    s = mOutput->common.get_parameters(&mOutput->common, keys.string());
+    out_s8 = String8(s);
+    free(s);
+    return out_s8;
 }
 
 // destroyTrack_l() must be called with AudioFlinger::mLock held
@@ -1296,12 +1322,12 @@
 
 void AudioFlinger::PlaybackThread::readOutputParameters()
 {
-    mSampleRate = mOutput->sampleRate();
-    mChannels = mOutput->channels();
-    mChannelCount = (uint16_t)AudioSystem::popCount(mChannels);
-    mFormat = mOutput->format();
-    mFrameSize = (uint16_t)mOutput->frameSize();
-    mFrameCount = mOutput->bufferSize() / mFrameSize;
+    mSampleRate = mOutput->common.get_sample_rate(&mOutput->common);
+    mChannels = mOutput->common.get_channels(&mOutput->common);
+    mChannelCount = (uint16_t)popcount(mChannels);
+    mFormat = mOutput->common.get_format(&mOutput->common);
+    mFrameSize = (uint16_t)audio_stream_frame_size(&mOutput->common);
+    mFrameCount = mOutput->common.get_buffer_size(&mOutput->common) / mFrameSize;
 
     // FIXME - Current mixer implementation only supports stereo output: Always
     // Allocate a stereo buffer even if HW output is mono.
@@ -1329,9 +1355,9 @@
     if (mOutput == 0) {
         return INVALID_OPERATION;
     }
-    *halFrames = mBytesWritten/mOutput->frameSize();
+    *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->common);
 
-    return mOutput->getRenderPosition(dspFrames);
+    return mOutput->get_render_position(mOutput, dspFrames);
 }
 
 uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId)
@@ -1356,19 +1382,19 @@
 
 uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId)
 {
-    // session AudioSystem::SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
+    // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
     // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
-    if (sessionId == AudioSystem::SESSION_OUTPUT_MIX) {
-        return AudioSystem::getStrategyForStream(AudioSystem::MUSIC);
+    if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
+        return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
     }
     for (size_t i = 0; i < mTracks.size(); i++) {
         sp<Track> track = mTracks[i];
         if (sessionId == track->sessionId() &&
                 !(track->mCblk->flags & CBLK_INVALID_MSK)) {
-            return AudioSystem::getStrategyForStream((AudioSystem::stream_type) track->type());
+            return AudioSystem::getStrategyForStream((audio_stream_type_t) track->type());
         }
     }
-    return AudioSystem::getStrategyForStream(AudioSystem::MUSIC);
+    return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
 }
 
 sp<AudioFlinger::EffectChain> AudioFlinger::PlaybackThread::getEffectChain(int sessionId)
@@ -1402,7 +1428,7 @@
 
 // ----------------------------------------------------------------------------
 
-AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device)
+AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, struct audio_stream_out* output, int id, uint32_t device)
     :   PlaybackThread(audioFlinger, output, id, device),
         mAudioMixer(0)
 {
@@ -1461,7 +1487,7 @@
                         mSuspended) {
                 if (!mStandby) {
                     LOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended);
-                    mOutput->standby();
+                    mOutput->common.standby(&mOutput->common);
                     mStandby = true;
                     mBytesWritten = 0;
                 }
@@ -1538,7 +1564,7 @@
             mInWrite = true;
             mBytesWritten += mixBufferSize;
 
-            int bytesWritten = (int)mOutput->write(mMixBuffer, mixBufferSize);
+            int bytesWritten = (int)mOutput->write(mOutput, mMixBuffer, mixBufferSize);
             if (bytesWritten < 0) mBytesWritten -= mixBufferSize;
             mNumWrites++;
             mInWrite = false;
@@ -1573,7 +1599,7 @@
     }
 
     if (!mStandby) {
-        mOutput->standby();
+        mOutput->common.standby(&mOutput->common);
     }
 
     LOGV("MixerThread %p exiting", this);
@@ -1597,7 +1623,7 @@
         masterVolume = 0;
     }
     // Delegate master volume control to effect in output mix effect chain if needed
-    sp<EffectChain> chain = getEffectChain_l(AudioSystem::SESSION_OUTPUT_MIX);
+    sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
     if (chain != 0) {
         uint32_t v = (uint32_t)(masterVolume * (1 << 24));
         chain->setVolume_l(&v, &v);
@@ -1823,14 +1849,14 @@
             reconfig = true;
         }
         if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
-            if (value != AudioSystem::PCM_16_BIT) {
+            if (value != AUDIO_FORMAT_PCM_16_BIT) {
                 status = BAD_VALUE;
             } else {
                 reconfig = true;
             }
         }
         if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
-            if (value != AudioSystem::CHANNEL_OUT_STEREO) {
+            if (value != AUDIO_CHANNEL_OUT_STEREO) {
                 status = BAD_VALUE;
             } else {
                 reconfig = true;
@@ -1852,12 +1878,12 @@
             if (mDevice != value) {
                 uint32_t params = 0;
                 // check whether speaker is on
-                if (value & AudioSystem::DEVICE_OUT_SPEAKER) {
+                if (value & AUDIO_DEVICE_OUT_SPEAKER) {
                     params |= IMediaPlayerService::kBatteryDataSpeakerOn;
                 }
 
                 int deviceWithoutSpeaker
-                    = AudioSystem::DEVICE_OUT_ALL & ~AudioSystem::DEVICE_OUT_SPEAKER;
+                    = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
                 // check if any other device (except speaker) is on
                 if (value & deviceWithoutSpeaker ) {
                     params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
@@ -1877,12 +1903,14 @@
         }
 
         if (status == NO_ERROR) {
-            status = mOutput->setParameters(keyValuePair);
+            status = mOutput->common.set_parameters(&mOutput->common,
+                                                    keyValuePair.string());
             if (!mStandby && status == INVALID_OPERATION) {
-               mOutput->standby();
+               mOutput->common.standby(&mOutput->common);
                mStandby = true;
                mBytesWritten = 0;
-               status = mOutput->setParameters(keyValuePair);
+               status = mOutput->common.set_parameters(&mOutput->common,
+                                                       keyValuePair.string());
             }
             if (status == NO_ERROR && reconfig) {
                 delete mAudioMixer;
@@ -1926,7 +1954,7 @@
 
 uint32_t AudioFlinger::MixerThread::activeSleepTimeUs()
 {
-    return (uint32_t)(mOutput->latency() * 1000) / 2;
+    return (uint32_t)(mOutput->get_latency(mOutput) * 1000) / 2;
 }
 
 uint32_t AudioFlinger::MixerThread::idleSleepTimeUs()
@@ -1940,7 +1968,7 @@
 }
 
 // ----------------------------------------------------------------------------
-AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device)
+AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, struct audio_stream_out* output, int id, uint32_t device)
     :   PlaybackThread(audioFlinger, output, id, device)
 {
     mType = PlaybackThread::DIRECT;
@@ -1976,12 +2004,12 @@
 void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp)
 {
     // Do not apply volume on compressed audio
-    if (!AudioSystem::isLinearPCM(mFormat)) {
+    if (!audio_is_linear_pcm(mFormat)) {
         return;
     }
 
     // convert to signed 16 bit before volume calculation
-    if (mFormat == AudioSystem::PCM_8_BIT) {
+    if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
         size_t count = mFrameCount * mChannelCount;
         uint8_t *src = (uint8_t *)mMixBuffer + count-1;
         int16_t *dst = mMixBuffer + count-1;
@@ -2034,7 +2062,7 @@
     }
 
     // convert back to unsigned 8 bit after volume calculation
-    if (mFormat == AudioSystem::PCM_8_BIT) {
+    if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
         size_t count = mFrameCount * mChannelCount;
         int16_t *src = mMixBuffer;
         uint8_t *dst = (uint8_t *)mMixBuffer;
@@ -2090,7 +2118,7 @@
                 // wait until we have something to do...
                 if (!mStandby) {
                     LOGV("Audio hardware entering standby, mixer %p\n", this);
-                    mOutput->standby();
+                    mOutput->common.standby(&mOutput->common);
                     mStandby = true;
                     mBytesWritten = 0;
                 }
@@ -2175,7 +2203,7 @@
 
                         // If audio HAL implements volume control,
                         // force software volume to nominal value
-                        if (mOutput->setVolume(left, right) == NO_ERROR) {
+                        if (mOutput->set_volume(mOutput, left, right) == NO_ERROR) {
                             left = 1.0f;
                             right = 1.0f;
                         }
@@ -2276,7 +2304,7 @@
                 } else {
                     sleepTime = idleSleepTime;
                 }
-            } else if (mBytesWritten != 0 && AudioSystem::isLinearPCM(mFormat)) {
+            } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) {
                 memset (mMixBuffer, 0, mFrameCount * mFrameSize);
                 sleepTime = 0;
             }
@@ -2298,7 +2326,7 @@
             mLastWriteTime = systemTime();
             mInWrite = true;
             mBytesWritten += mixBufferSize;
-            int bytesWritten = (int)mOutput->write(mMixBuffer, mixBufferSize);
+            int bytesWritten = (int)mOutput->write(mOutput, mMixBuffer, mixBufferSize);
             if (bytesWritten < 0) mBytesWritten -= mixBufferSize;
             mNumWrites++;
             mInWrite = false;
@@ -2320,7 +2348,7 @@
     }
 
     if (!mStandby) {
-        mOutput->standby();
+        mOutput->common.standby(&mOutput->common);
     }
 
     LOGV("DirectOutputThread %p exiting", this);
@@ -2360,12 +2388,14 @@
             }
         }
         if (status == NO_ERROR) {
-            status = mOutput->setParameters(keyValuePair);
+            status = mOutput->common.set_parameters(&mOutput->common,
+                                                    keyValuePair.string());
             if (!mStandby && status == INVALID_OPERATION) {
-               mOutput->standby();
+               mOutput->common.standby(&mOutput->common);
                mStandby = true;
                mBytesWritten = 0;
-               status = mOutput->setParameters(keyValuePair);
+               status = mOutput->common.set_parameters(&mOutput->common,
+                                                       keyValuePair.string());
             }
             if (status == NO_ERROR && reconfig) {
                 readOutputParameters();
@@ -2385,8 +2415,8 @@
 uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs()
 {
     uint32_t time;
-    if (AudioSystem::isLinearPCM(mFormat)) {
-        time = (uint32_t)(mOutput->latency() * 1000) / 2;
+    if (audio_is_linear_pcm(mFormat)) {
+        time = (uint32_t)(mOutput->get_latency(mOutput) * 1000) / 2;
     } else {
         time = 10000;
     }
@@ -2396,7 +2426,7 @@
 uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs()
 {
     uint32_t time;
-    if (AudioSystem::isLinearPCM(mFormat)) {
+    if (audio_is_linear_pcm(mFormat)) {
         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
     } else {
         time = 10000;
@@ -2407,7 +2437,7 @@
 uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs()
 {
     uint32_t time;
-    if (AudioSystem::isLinearPCM(mFormat)) {
+    if (audio_is_linear_pcm(mFormat)) {
         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
     } else {
         time = 10000;
@@ -2588,7 +2618,7 @@
                                             mChannelCount,
                                             frameCount);
     if (outputTrack->cblk() != NULL) {
-        thread->setStreamVolume(AudioSystem::NUM_STREAM_TYPES, 1.0f);
+        thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f);
         mOutputTracks.add(outputTrack);
         LOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread);
         updateWaitTime();
@@ -2833,7 +2863,7 @@
         mStreamType = streamType;
         // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
         // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
-        mCblk->frameSize = AudioSystem::isLinearPCM(format) ? channelCount * sizeof(int16_t) : sizeof(int8_t);
+        mCblk->frameSize = audio_is_linear_pcm(format) ? channelCount * sizeof(int16_t) : sizeof(int8_t);
     }
 }
 
@@ -2864,7 +2894,7 @@
             if (!isOutputTrack()) {
                 if (mState == ACTIVE || mState == RESUMING) {
                     AudioSystem::stopOutput(thread->id(),
-                                            (AudioSystem::stream_type)mStreamType,
+                                            (audio_stream_type_t)mStreamType,
                                             mSessionId);
 
                     // to track the speaker usage
@@ -2976,7 +3006,7 @@
         if (!isOutputTrack() && state != ACTIVE && state != RESUMING) {
             thread->mLock.unlock();
             status = AudioSystem::startOutput(thread->id(),
-                                              (AudioSystem::stream_type)mStreamType,
+                                              (audio_stream_type_t)mStreamType,
                                               mSessionId);
             thread->mLock.lock();
 
@@ -3016,7 +3046,7 @@
         if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) {
             thread->mLock.unlock();
             AudioSystem::stopOutput(thread->id(),
-                                    (AudioSystem::stream_type)mStreamType,
+                                    (audio_stream_type_t)mStreamType,
                                     mSessionId);
             thread->mLock.lock();
 
@@ -3038,7 +3068,7 @@
             if (!isOutputTrack()) {
                 thread->mLock.unlock();
                 AudioSystem::stopOutput(thread->id(),
-                                        (AudioSystem::stream_type)mStreamType,
+                                        (audio_stream_type_t)mStreamType,
                                         mSessionId);
                 thread->mLock.lock();
 
@@ -3132,9 +3162,9 @@
 {
     if (mCblk != NULL) {
        LOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
-       if (format == AudioSystem::PCM_16_BIT) {
+       if (format == AUDIO_FORMAT_PCM_16_BIT) {
            mCblk->frameSize = channelCount * sizeof(int16_t);
-       } else if (format == AudioSystem::PCM_8_BIT) {
+       } else if (format == AUDIO_FORMAT_PCM_8_BIT) {
            mCblk->frameSize = channelCount * sizeof(int8_t);
        } else {
            mCblk->frameSize = sizeof(int8_t);
@@ -3237,7 +3267,7 @@
             int format,
             int channelCount,
             int frameCount)
-    :   Track(thread, NULL, AudioSystem::NUM_STREAM_TYPES, sampleRate, format, channelCount, frameCount, NULL, 0),
+    :   Track(thread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelCount, frameCount, NULL, 0),
     mActive(false), mSourceThread(sourceThread)
 {
 
@@ -3609,7 +3639,7 @@
         }
 
         // If no audio session id is provided, create one here
-        if (sessionId != NULL && *sessionId != AudioSystem::SESSION_OUTPUT_MIX) {
+        if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
             lSessionId = *sessionId;
         } else {
             lSessionId = nextUniqueId_l();
@@ -3675,11 +3705,11 @@
 
 // ----------------------------------------------------------------------------
 
-AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, AudioStreamIn *input, uint32_t sampleRate, uint32_t channels, int id) :
+AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, struct audio_stream_in *input, uint32_t sampleRate, uint32_t channels, int id) :
     ThreadBase(audioFlinger, id),
     mInput(input), mResampler(0), mRsmpOutBuffer(0), mRsmpInBuffer(0)
 {
-    mReqChannelCount = AudioSystem::popCount(channels);
+    mReqChannelCount = popcount(channels);
     mReqSampleRate = sampleRate;
     readInputParameters();
 }
@@ -3721,7 +3751,7 @@
             checkForNewParameters_l();
             if (mActiveTrack == 0 && mConfigEvents.isEmpty()) {
                 if (!mStandby) {
-                    mInput->standby();
+                    mInput->common.standby(&mInput->common);
                     mStandby = true;
                 }
 
@@ -3736,7 +3766,7 @@
             if (mActiveTrack != 0) {
                 if (mActiveTrack->mState == TrackBase::PAUSING) {
                     if (!mStandby) {
-                        mInput->standby();
+                        mInput->common.standby(&mInput->common);
                         mStandby = true;
                     }
                     mActiveTrack.clear();
@@ -3781,7 +3811,7 @@
                             mRsmpInIndex += framesIn;
                             framesOut -= framesIn;
                             if ((int)mChannelCount == mReqChannelCount ||
-                                mFormat != AudioSystem::PCM_16_BIT) {
+                                mFormat != AUDIO_FORMAT_PCM_16_BIT) {
                                 memcpy(dst, src, framesIn * mFrameSize);
                             } else {
                                 int16_t *src16 = (int16_t *)src;
@@ -3801,11 +3831,11 @@
                         }
                         if (framesOut && mFrameCount == mRsmpInIndex) {
                             if (framesOut == mFrameCount &&
-                                ((int)mChannelCount == mReqChannelCount || mFormat != AudioSystem::PCM_16_BIT)) {
-                                mBytesRead = mInput->read(buffer.raw, mInputBytes);
+                                ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) {
+                                mBytesRead = mInput->read(mInput, buffer.raw, mInputBytes);
                                 framesOut = 0;
                             } else {
-                                mBytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
+                                mBytesRead = mInput->read(mInput, mRsmpInBuffer, mInputBytes);
                                 mRsmpInIndex = 0;
                             }
                             if (mBytesRead < 0) {
@@ -3813,7 +3843,7 @@
                                 if (mActiveTrack->mState == TrackBase::ACTIVE) {
                                     // Force input into standby so that it tries to
                                     // recover at next read attempt
-                                    mInput->standby();
+                                    mInput->common.standby(&mInput->common);
                                     usleep(5000);
                                 }
                                 mRsmpInIndex = mFrameCount;
@@ -3868,7 +3898,7 @@
     }
 
     if (!mStandby) {
-        mInput->standby();
+        mInput->common.standby(&mInput->common);
     }
     mActiveTrack.clear();
 
@@ -4000,13 +4030,13 @@
     int channelCount;
 
     if (framesReady == 0) {
-        mBytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
+        mBytesRead = mInput->read(mInput, mRsmpInBuffer, mInputBytes);
         if (mBytesRead < 0) {
             LOGE("RecordThread::getNextBuffer() Error reading audio input");
             if (mActiveTrack->mState == TrackBase::ACTIVE) {
                 // Force input into standby so that it tries to
                 // recover at next read attempt
-                mInput->standby();
+                mInput->common.standby(&mInput->common);
                 usleep(5000);
             }
             buffer->raw = 0;
@@ -4059,7 +4089,7 @@
             reconfig = true;
         }
         if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
-            reqChannelCount = AudioSystem::popCount(value);
+            reqChannelCount = popcount(value);
             reconfig = true;
         }
         if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
@@ -4073,16 +4103,18 @@
             }
         }
         if (status == NO_ERROR) {
-            status = mInput->setParameters(keyValuePair);
+            status = mInput->common.set_parameters(&mInput->common, keyValuePair.string());
             if (status == INVALID_OPERATION) {
-               mInput->standby();
-               status = mInput->setParameters(keyValuePair);
+               mInput->common.standby(&mInput->common);
+               status = mInput->common.set_parameters(&mInput->common, keyValuePair.string());
             }
             if (reconfig) {
                 if (status == BAD_VALUE &&
-                    reqFormat == mInput->format() && reqFormat == AudioSystem::PCM_16_BIT &&
-                    ((int)mInput->sampleRate() <= 2 * reqSamplingRate) &&
-                    (AudioSystem::popCount(mInput->channels()) < 3) && (reqChannelCount < 3)) {
+                    reqFormat == mInput->common.get_format(&mInput->common) &&
+                    reqFormat == AUDIO_FORMAT_PCM_16_BIT &&
+                    ((int)mInput->common.get_sample_rate(&mInput->common) <= (2 * reqSamplingRate)) &&
+                    (popcount(mInput->common.get_channels(&mInput->common)) < 3) &&
+                    (reqChannelCount < 3)) {
                     status = NO_ERROR;
                 }
                 if (status == NO_ERROR) {
@@ -4103,7 +4135,13 @@
 
 String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
 {
-    return mInput->getParameters(keys);
+    char *s;
+    String8 out_s8;
+
+    s = mInput->common.get_parameters(&mInput->common, keys.string());
+    out_s8 = String8(s);
+    free(s);
+    return out_s8;
 }
 
 void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) {
@@ -4135,12 +4173,12 @@
     if (mResampler) delete mResampler;
     mResampler = 0;
 
-    mSampleRate = mInput->sampleRate();
-    mChannels = mInput->channels();
-    mChannelCount = (uint16_t)AudioSystem::popCount(mChannels);
-    mFormat = mInput->format();
-    mFrameSize = (uint16_t)mInput->frameSize();
-    mInputBytes = mInput->bufferSize();
+    mSampleRate = mInput->common.get_sample_rate(&mInput->common);
+    mChannels = mInput->common.get_channels(&mInput->common);
+    mChannelCount = (uint16_t)popcount(mChannels);
+    mFormat = mInput->common.get_format(&mInput->common);
+    mFrameSize = (uint16_t)audio_stream_frame_size(&mInput->common);
+    mInputBytes = mInput->common.get_buffer_size(&mInput->common);
     mFrameCount = mInputBytes / mFrameSize;
     mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount];
 
@@ -4170,7 +4208,7 @@
 
 unsigned int AudioFlinger::RecordThread::getInputFramesLost()
 {
-    return mInput->getInputFramesLost();
+    return mInput->get_input_frames_lost(mInput);
 }
 
 // ----------------------------------------------------------------------------
@@ -4189,6 +4227,7 @@
     uint32_t format = pFormat ? *pFormat : 0;
     uint32_t channels = pChannels ? *pChannels : 0;
     uint32_t latency = pLatencyMs ? *pLatencyMs : 0;
+    struct audio_stream_out *output;
 
     LOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
             pDevices ? *pDevices : 0,
@@ -4202,11 +4241,11 @@
     }
     Mutex::Autolock _l(mLock);
 
-    AudioStreamOut *output = mAudioHardware->openOutputStream(*pDevices,
-                                                             (int *)&format,
-                                                             &channels,
-                                                             &samplingRate,
-                                                             &status);
+    status = mAudioHardwareDev->open_output_stream(mAudioHardwareDev, *pDevices,
+                                                   (int *)&format,
+                                                   &channels,
+                                                   &samplingRate,
+                                                   &output);
     LOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d",
             output,
             samplingRate,
@@ -4217,9 +4256,9 @@
     mHardwareStatus = AUDIO_HW_IDLE;
     if (output != 0) {
         int id = nextUniqueId_l();
-        if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
-            (format != AudioSystem::PCM_16_BIT) ||
-            (channels != AudioSystem::CHANNEL_OUT_STEREO)) {
+        if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) ||
+            (format != AUDIO_FORMAT_PCM_16_BIT) ||
+            (channels != AUDIO_CHANNEL_OUT_STEREO)) {
             thread = new DirectOutputThread(this, output, id, *pDevices);
             LOGV("openOutput() created direct output: ID %d thread %p", id, thread);
         } else {
@@ -4290,7 +4329,7 @@
     thread->exit();
 
     if (thread->type() != PlaybackThread::DUPLICATING) {
-        mAudioHardware->closeOutputStream(thread->getOutput());
+        mAudioHardwareDev->close_output_stream(mAudioHardwareDev, thread->getOutput());
     }
     return NO_ERROR;
 }
@@ -4340,18 +4379,20 @@
     uint32_t reqSamplingRate = samplingRate;
     uint32_t reqFormat = format;
     uint32_t reqChannels = channels;
+    struct audio_stream_in *input;
 
     if (pDevices == NULL || *pDevices == 0) {
         return 0;
     }
     Mutex::Autolock _l(mLock);
 
-    AudioStreamIn *input = mAudioHardware->openInputStream(*pDevices,
-                                                             (int *)&format,
-                                                             &channels,
-                                                             &samplingRate,
-                                                             &status,
-                                                             (AudioSystem::audio_in_acoustics)acoustics);
+    status = mAudioHardwareDev->open_input_stream(mAudioHardwareDev,
+                                        *pDevices,
+                                        (int *)&format,
+                                        &channels,
+                                        &samplingRate,
+                                        (audio_in_acoustics_t)acoustics,
+                                        &input);
     LOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d",
             input,
             samplingRate,
@@ -4364,16 +4405,17 @@
     // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo
     // or stereo to mono conversions on 16 bit PCM inputs.
     if (input == 0 && status == BAD_VALUE &&
-        reqFormat == format && format == AudioSystem::PCM_16_BIT &&
+        reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT &&
         (samplingRate <= 2 * reqSamplingRate) &&
-        (AudioSystem::popCount(channels) < 3) && (AudioSystem::popCount(reqChannels) < 3)) {
+        (popcount(channels) < 3) && (popcount(reqChannels) < 3)) {
         LOGV("openInput() reopening with proposed sampling rate and channels");
-        input = mAudioHardware->openInputStream(*pDevices,
-                                                 (int *)&format,
-                                                 &channels,
-                                                 &samplingRate,
-                                                 &status,
-                                                 (AudioSystem::audio_in_acoustics)acoustics);
+        status = mAudioHardwareDev->open_input_stream(mAudioHardwareDev,
+                                            *pDevices,
+                                            (int *)&format,
+                                            &channels,
+                                            &samplingRate,
+                                            (audio_in_acoustics_t)acoustics,
+                                            &input);
     }
 
     if (input != 0) {
@@ -4386,7 +4428,7 @@
         if (pFormat) *pFormat = format;
         if (pChannels) *pChannels = reqChannels;
 
-        input->standby();
+        input->common.standby(&input->common);
 
         // notify client processes of the new input creation
         thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
@@ -4415,7 +4457,7 @@
     }
     thread->exit();
 
-    mAudioHardware->closeInputStream(thread->getInput());
+    mAudioHardwareDev->close_input_stream(mAudioHardwareDev, thread->getInput());
 
     return NO_ERROR;
 }
@@ -4569,14 +4611,14 @@
     }
 
     // check audio settings permission for global effects
-    if (sessionId == AudioSystem::SESSION_OUTPUT_MIX && !settingsAllowed()) {
+    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
         lStatus = PERMISSION_DENIED;
         goto Exit;
     }
 
-    // Session AudioSystem::SESSION_OUTPUT_STAGE is reserved for output stage effects
+    // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
     // that can only be created by audio policy manager (running in same process)
-    if (sessionId == AudioSystem::SESSION_OUTPUT_STAGE && getpid() != pid) {
+    if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid() != pid) {
         lStatus = PERMISSION_DENIED;
         goto Exit;
     }
@@ -4590,12 +4632,12 @@
     }
 
     if (output == 0) {
-        if (sessionId == AudioSystem::SESSION_OUTPUT_STAGE) {
+        if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
             // output must be specified by AudioPolicyManager when using session
-            // AudioSystem::SESSION_OUTPUT_STAGE
+            // AUDIO_SESSION_OUTPUT_STAGE
             lStatus = BAD_VALUE;
             goto Exit;
-        } else if (sessionId == AudioSystem::SESSION_OUTPUT_MIX) {
+        } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
             // if the output returned by getOutputForEffect() is removed before we lock the
             // mutex below, the call to checkPlaybackThread_l(output) below will detect it
             // and we will exit safely
@@ -4643,7 +4685,7 @@
                     // an auxiliary version of this effect type is available
                     found = true;
                     memcpy(&d, &desc, sizeof(effect_descriptor_t));
-                    if (sessionId != AudioSystem::SESSION_OUTPUT_MIX ||
+                    if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
                             (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
                         break;
                     }
@@ -4656,14 +4698,14 @@
             }
             // For same effect type, chose auxiliary version over insert version if
             // connect to output mix (Compliance to OpenSL ES)
-            if (sessionId == AudioSystem::SESSION_OUTPUT_MIX &&
+            if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
                     (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
                 memcpy(&desc, &d, sizeof(effect_descriptor_t));
             }
         }
 
         // Do not allow auxiliary effects on a session different from 0 (output mix)
-        if (sessionId != AudioSystem::SESSION_OUTPUT_MIX &&
+        if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
              (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
             lStatus = INVALID_OPERATION;
             goto Exit;
@@ -4836,7 +4878,7 @@
 
     // Do not allow auxiliary effect on session other than 0
     if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY &&
-        sessionId != AudioSystem::SESSION_OUTPUT_MIX) {
+        sessionId != AUDIO_SESSION_OUTPUT_MIX) {
         LOGW("createEffect_l() Cannot add auxiliary effect %s to session %d",
                 desc->name, sessionId);
         lStatus = BAD_VALUE;
@@ -4845,7 +4887,7 @@
 
     // Do not allow effects with session ID 0 on direct output or duplicating threads
     // TODO: add rule for hw accelerated effects on direct outputs with non PCM format
-    if (sessionId == AudioSystem::SESSION_OUTPUT_MIX && mType != MIXER) {
+    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) {
         LOGW("createEffect_l() Cannot add auxiliary effect %s to session %d",
                 desc->name, sessionId);
         lStatus = BAD_VALUE;
@@ -5032,13 +5074,13 @@
 
     chain->setInBuffer(buffer, ownsBuffer);
     chain->setOutBuffer(mMixBuffer);
-    // Effect chain for session AudioSystem::SESSION_OUTPUT_STAGE is inserted at end of effect
+    // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect
     // chains list in order to be processed last as it contains output stage effects
-    // Effect chain for session AudioSystem::SESSION_OUTPUT_MIX is inserted before
-    // session AudioSystem::SESSION_OUTPUT_STAGE to be processed
+    // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
+    // session AUDIO_SESSION_OUTPUT_STAGE to be processed
     // after track specific effects and before output stage
-    // It is therefore mandatory that AudioSystem::SESSION_OUTPUT_MIX == 0 and
-    // that AudioSystem::SESSION_OUTPUT_STAGE < AudioSystem::SESSION_OUTPUT_MIX
+    // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
+    // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX
     // Effect chain for other sessions are inserted at beginning of effect
     // chains list to be processed before output mix effects. Relative order between other
     // sessions is not important
@@ -5118,8 +5160,8 @@
     if (EffectId == 0) {
         track->setAuxBuffer(0, NULL);
     } else {
-        // Auxiliary effects are always in audio session AudioSystem::SESSION_OUTPUT_MIX
-        sp<EffectModule> effect = getEffect_l(AudioSystem::SESSION_OUTPUT_MIX, EffectId);
+        // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
+        sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
         if (effect != 0) {
             if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
                 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
@@ -5403,7 +5445,7 @@
     mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
     mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
     // Insert effect:
-    // - in session AudioSystem::SESSION_OUTPUT_MIX or AudioSystem::SESSION_OUTPUT_STAGE,
+    // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
     // always overwrites output buffer: input buffer == output buffer
     // - in other sessions:
     //      last effect in the chain accumulates in output buffer: input buffer != output buffer
@@ -5684,17 +5726,17 @@
 
 // update this table when AudioSystem::audio_devices or audio_device_e (in EffectApi.h) are modified
 const uint32_t AudioFlinger::EffectModule::sDeviceConvTable[] = {
-    DEVICE_EARPIECE, // AudioSystem::DEVICE_OUT_EARPIECE
-    DEVICE_SPEAKER, // AudioSystem::DEVICE_OUT_SPEAKER
-    DEVICE_WIRED_HEADSET, // case AudioSystem::DEVICE_OUT_WIRED_HEADSET
-    DEVICE_WIRED_HEADPHONE, // AudioSystem::DEVICE_OUT_WIRED_HEADPHONE
-    DEVICE_BLUETOOTH_SCO, // AudioSystem::DEVICE_OUT_BLUETOOTH_SCO
-    DEVICE_BLUETOOTH_SCO_HEADSET, // AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET
-    DEVICE_BLUETOOTH_SCO_CARKIT, //  AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT
-    DEVICE_BLUETOOTH_A2DP, //  AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP
-    DEVICE_BLUETOOTH_A2DP_HEADPHONES, // AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES
-    DEVICE_BLUETOOTH_A2DP_SPEAKER, // AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER
-    DEVICE_AUX_DIGITAL // AudioSystem::DEVICE_OUT_AUX_DIGITAL
+    DEVICE_EARPIECE, // AUDIO_DEVICE_OUT_EARPIECE
+    DEVICE_SPEAKER, // AUDIO_DEVICE_OUT_SPEAKER
+    DEVICE_WIRED_HEADSET, // case AUDIO_DEVICE_OUT_WIRED_HEADSET
+    DEVICE_WIRED_HEADPHONE, // AUDIO_DEVICE_OUT_WIRED_HEADPHONE
+    DEVICE_BLUETOOTH_SCO, // AUDIO_DEVICE_OUT_BLUETOOTH_SCO
+    DEVICE_BLUETOOTH_SCO_HEADSET, // AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET
+    DEVICE_BLUETOOTH_SCO_CARKIT, //  AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT
+    DEVICE_BLUETOOTH_A2DP, //  AUDIO_DEVICE_OUT_BLUETOOTH_A2DP
+    DEVICE_BLUETOOTH_A2DP_HEADPHONES, // AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES
+    DEVICE_BLUETOOTH_A2DP_SPEAKER, // AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER
+    DEVICE_AUX_DIGITAL // AUDIO_DEVICE_OUT_AUX_DIGITAL
 };
 
 uint32_t AudioFlinger::EffectModule::deviceAudioSystemToEffectApi(uint32_t device)
@@ -5714,10 +5756,10 @@
 
 // update this table when AudioSystem::audio_mode or audio_mode_e (in EffectApi.h) are modified
 const uint32_t AudioFlinger::EffectModule::sModeConvTable[] = {
-    AUDIO_MODE_NORMAL,   // AudioSystem::MODE_NORMAL
-    AUDIO_MODE_RINGTONE, // AudioSystem::MODE_RINGTONE
-    AUDIO_MODE_IN_CALL,  // AudioSystem::MODE_IN_CALL
-    AUDIO_MODE_IN_CALL   // AudioSystem::MODE_IN_COMMUNICATION, same conversion as for MODE_IN_CALL
+    AUDIO_EFFECT_MODE_NORMAL,   // AUDIO_MODE_NORMAL
+    AUDIO_EFFECT_MODE_RINGTONE, // AUDIO_MODE_RINGTONE
+    AUDIO_EFFECT_MODE_IN_CALL,  // AUDIO_MODE_IN_CALL
+    AUDIO_EFFECT_MODE_IN_CALL   // AUDIO_MODE_IN_COMMUNICATION, same conversion as for AUDIO_MODE_IN_CALL
 };
 
 int AudioFlinger::EffectModule::modeAudioSystemToEffectApi(uint32_t mode)
@@ -6027,7 +6069,7 @@
             mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
             mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
 {
-    mStrategy = AudioSystem::getStrategyForStream(AudioSystem::MUSIC);
+    mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
 }
 
 AudioFlinger::EffectChain::~EffectChain()
@@ -6078,8 +6120,8 @@
         return;
     }
     PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
-    bool isGlobalSession = (mSessionId == AudioSystem::SESSION_OUTPUT_MIX) ||
-            (mSessionId == AudioSystem::SESSION_OUTPUT_STAGE);
+    bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
+            (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
     bool tracksOnSession = false;
     if (!isGlobalSession) {
         tracksOnSession =
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index ec3d202..4ac1ee3 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -37,7 +37,7 @@
 #include <binder/BinderService.h>
 #include <binder/MemoryDealer.h>
 
-#include <hardware_legacy/AudioHardwareInterface.h>
+#include <hardware/audio.h>
 
 #include "AudioBufferProvider.h"
 
@@ -495,7 +495,7 @@
             void reset();
 
             bool isOutputTrack() const {
-                return (mStreamType == AudioSystem::NUM_STREAM_TYPES);
+                return (mStreamType == AUDIO_STREAM_CNT);
             }
 
             // we don't really need a lock for these
@@ -554,7 +554,7 @@
             DuplicatingThread*          mSourceThread;
         };  // end of OutputTrack
 
-        PlaybackThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
+        PlaybackThread (const sp<AudioFlinger>& audioFlinger, struct audio_stream_out* output, int id, uint32_t device);
         virtual             ~PlaybackThread();
 
         virtual     status_t    dump(int fd, const Vector<String16>& args);
@@ -588,7 +588,7 @@
                                     int sessionId,
                                     status_t *status);
 
-                    AudioStreamOut* getOutput() { return mOutput; }
+                    struct audio_stream_out* getOutput() { return mOutput; }
 
         virtual     int         type() const { return mType; }
                     void        suspend() { mSuspended++; }
@@ -689,8 +689,8 @@
 
         SortedVector< sp<Track> >       mTracks;
         // mStreamTypes[] uses 1 additionnal stream type internally for the OutputTrack used by DuplicatingThread
-        stream_type_t                   mStreamTypes[AudioSystem::NUM_STREAM_TYPES + 1];
-        AudioStreamOut*                 mOutput;
+        stream_type_t                   mStreamTypes[AUDIO_STREAM_CNT + 1];
+        struct audio_stream_out*        mOutput;
         float                           mMasterVolume;
         nsecs_t                         mLastWriteTime;
         int                             mNumWrites;
@@ -703,7 +703,7 @@
     class MixerThread : public PlaybackThread {
     public:
         MixerThread (const sp<AudioFlinger>& audioFlinger,
-                     AudioStreamOut* output,
+                     struct audio_stream_out* output,
                      int id,
                      uint32_t device);
         virtual             ~MixerThread();
@@ -730,7 +730,7 @@
     class DirectOutputThread : public PlaybackThread {
     public:
 
-        DirectOutputThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device);
+        DirectOutputThread (const sp<AudioFlinger>& audioFlinger, struct audio_stream_out* output, int id, uint32_t device);
         ~DirectOutputThread();
 
         // Thread virtuals
@@ -854,7 +854,7 @@
 
 
                 RecordThread(const sp<AudioFlinger>& audioFlinger,
-                        AudioStreamIn *input,
+                        struct audio_stream_in *input,
                         uint32_t sampleRate,
                         uint32_t channels,
                         int id);
@@ -867,7 +867,7 @@
                 status_t    start(RecordTrack* recordTrack);
                 void        stop(RecordTrack* recordTrack);
                 status_t    dump(int fd, const Vector<String16>& args);
-                AudioStreamIn* getInput() { return mInput; }
+                struct audio_stream_in* getInput() { return mInput; }
 
         virtual status_t    getNextBuffer(AudioBufferProvider::Buffer* buffer);
         virtual void        releaseBuffer(AudioBufferProvider::Buffer* buffer);
@@ -879,7 +879,7 @@
 
     private:
                 RecordThread();
-                AudioStreamIn                       *mInput;
+                struct audio_stream_in              *mInput;
                 sp<RecordTrack>                     mActiveTrack;
                 Condition                           mStartStopCond;
                 AudioResampler                      *mResampler;
@@ -1168,12 +1168,13 @@
                 DefaultKeyedVector< pid_t, wp<Client> >     mClients;
 
                 mutable     Mutex                   mHardwareLock;
-                AudioHardwareInterface*             mAudioHardware;
+                struct audio_hw_device*             mAudioHardwareDev;
+                Vector<struct audio_hw_device *>    mAudioHwDevs;
     mutable     int                                 mHardwareStatus;
 
 
                 DefaultKeyedVector< int, sp<PlaybackThread> >  mPlaybackThreads;
-                PlaybackThread::stream_type_t       mStreamTypes[AudioSystem::NUM_STREAM_TYPES];
+                PlaybackThread::stream_type_t       mStreamTypes[AUDIO_STREAM_CNT];
                 float                               mMasterVolume;
                 bool                                mMasterMute;
 
diff --git a/services/audioflinger/AudioPolicyService.cpp b/services/audioflinger/AudioPolicyService.cpp
index b614c48..eebc1b3 100644
--- a/services/audioflinger/AudioPolicyService.cpp
+++ b/services/audioflinger/AudioPolicyService.cpp
@@ -30,11 +30,15 @@
 #include <utils/String16.h>
 #include <utils/threads.h>
 #include "AudioPolicyService.h"
-#include <hardware_legacy/AudioPolicyManagerBase.h>
 #include <cutils/properties.h>
 #include <dlfcn.h>
 #include <hardware_legacy/power.h>
 
+#include <hardware/hardware.h>
+#include <hardware/audio.h>
+#include <hardware/audio_policy.h>
+#include <hardware/audio_policy_hal.h>
+
 // ----------------------------------------------------------------------------
 // the sim build doesn't have gettid
 
@@ -44,7 +48,6 @@
 
 namespace android {
 
-
 static const char *kDeadlockedString = "AudioPolicyService may be deadlocked\n";
 static const char *kCmdDeadlockedString = "AudioPolicyService command thread may be deadlocked\n";
 
@@ -61,12 +64,19 @@
     return ok;
 }
 
+namespace {
+    extern struct audio_policy_service_ops aps_ops;
+};
+
 // ----------------------------------------------------------------------------
 
 AudioPolicyService::AudioPolicyService()
-    : BnAudioPolicyService() , mpPolicyManager(NULL)
+    : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
 {
     char value[PROPERTY_VALUE_MAX];
+    const struct hw_module_t *module;
+    int forced_val;
+    int rc;
 
     Mutex::Autolock _l(mLock);
 
@@ -75,33 +85,32 @@
     // start audio commands thread
     mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread"));
 
-#if (defined GENERIC_AUDIO) || (defined AUDIO_POLICY_TEST)
-    mpPolicyManager = new AudioPolicyManagerBase(this);
-    LOGV("build for GENERIC_AUDIO - using generic audio policy");
-#else
-    // if running in emulation - use the emulator driver
-    if (property_get("ro.kernel.qemu", value, 0)) {
-        LOGV("Running in emulation - using generic audio policy");
-        mpPolicyManager = new AudioPolicyManagerBase(this);
-    }
-    else {
-        LOGV("Using hardware specific audio policy");
-        mpPolicyManager = createAudioPolicyManager(this);
-    }
-#endif
+    /* instantiate the audio policy manager */
+    rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
+    if (rc)
+        return;
 
-    if ((mpPolicyManager != NULL) && (mpPolicyManager->initCheck() != NO_ERROR)) {
-        delete mpPolicyManager;
-        mpPolicyManager = NULL;
-    }
+    rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
+    LOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
+    if (rc)
+        return;
 
-    if (mpPolicyManager == NULL) {
-        LOGE("Could not create AudioPolicyManager");
-    } else {
-        // load properties
-        property_get("ro.camera.sound.forced", value, "0");
-        mpPolicyManager->setSystemProperty("ro.camera.sound.forced", value);
-    }
+    rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
+                                               &mpAudioPolicy);
+    LOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
+    if (rc)
+        return;
+
+    rc = mpAudioPolicy->init_check(mpAudioPolicy);
+    LOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
+    if (rc)
+        return;
+
+    property_get("ro.camera.sound.forced", value, "0");
+    forced_val = strtol(value, NULL, 0);
+    mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val);
+
+    LOGI("Loaded audio policy from %s (%s)", module->name, module->id);
 }
 
 AudioPolicyService::~AudioPolicyService()
@@ -111,57 +120,59 @@
     mAudioCommandThread->exit();
     mAudioCommandThread.clear();
 
-    if (mpPolicyManager) {
-        delete mpPolicyManager;
-    }
+    if (mpAudioPolicy && mpAudioPolicyDev)
+        mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
+    if (mpAudioPolicyDev)
+        audio_policy_dev_close(mpAudioPolicyDev);
 }
 
-
-status_t AudioPolicyService::setDeviceConnectionState(AudioSystem::audio_devices device,
-                                                  AudioSystem::device_connection_state state,
+status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
+                                                  audio_policy_dev_state_t state,
                                                   const char *device_address)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     if (!checkPermission()) {
         return PERMISSION_DENIED;
     }
-    if (!AudioSystem::isOutputDevice(device) && !AudioSystem::isInputDevice(device)) {
+    if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
         return BAD_VALUE;
     }
-    if (state != AudioSystem::DEVICE_STATE_AVAILABLE &&
-            state != AudioSystem::DEVICE_STATE_UNAVAILABLE) {
+    if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
+            state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
         return BAD_VALUE;
     }
 
     LOGV("setDeviceConnectionState() tid %d", gettid());
     Mutex::Autolock _l(mLock);
-    return mpPolicyManager->setDeviceConnectionState(device, state, device_address);
+    return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
+                                                      state, device_address);
 }
 
-AudioSystem::device_connection_state AudioPolicyService::getDeviceConnectionState(
-                                                              AudioSystem::audio_devices device,
+audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
+                                                              audio_devices_t device,
                                                               const char *device_address)
 {
-    if (mpPolicyManager == NULL) {
-        return AudioSystem::DEVICE_STATE_UNAVAILABLE;
+    if (mpAudioPolicy == NULL) {
+        return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
     }
     if (!checkPermission()) {
-        return AudioSystem::DEVICE_STATE_UNAVAILABLE;
+        return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
     }
-    return mpPolicyManager->getDeviceConnectionState(device, device_address);
+    return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
+                                                      device_address);
 }
 
 status_t AudioPolicyService::setPhoneState(int state)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     if (!checkPermission()) {
         return PERMISSION_DENIED;
     }
-    if (state < 0 || state >= AudioSystem::NUM_MODES) {
+    if (state < 0 || state >= AUDIO_MODE_CNT) {
         return BAD_VALUE;
     }
 
@@ -171,215 +182,215 @@
     AudioSystem::setMode(state);
 
     Mutex::Autolock _l(mLock);
-    mpPolicyManager->setPhoneState(state);
+    mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
     return NO_ERROR;
 }
 
 status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     if (!checkPermission()) {
         return PERMISSION_DENIED;
     }
 
-    mpPolicyManager->setRingerMode(mode, mask);
+    mpAudioPolicy->set_ringer_mode(mpAudioPolicy, mode, mask);
     return NO_ERROR;
 }
 
-status_t AudioPolicyService::setForceUse(AudioSystem::force_use usage,
-                                         AudioSystem::forced_config config)
+status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
+                                         audio_policy_forced_cfg_t config)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     if (!checkPermission()) {
         return PERMISSION_DENIED;
     }
-    if (usage < 0 || usage >= AudioSystem::NUM_FORCE_USE) {
+    if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
         return BAD_VALUE;
     }
-    if (config < 0 || config >= AudioSystem::NUM_FORCE_CONFIG) {
+    if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
         return BAD_VALUE;
     }
     LOGV("setForceUse() tid %d", gettid());
     Mutex::Autolock _l(mLock);
-    mpPolicyManager->setForceUse(usage, config);
+    mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
     return NO_ERROR;
 }
 
-AudioSystem::forced_config AudioPolicyService::getForceUse(AudioSystem::force_use usage)
+audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
 {
-    if (mpPolicyManager == NULL) {
-        return AudioSystem::FORCE_NONE;
+    if (mpAudioPolicy == NULL) {
+        return AUDIO_POLICY_FORCE_NONE;
     }
     if (!checkPermission()) {
-        return AudioSystem::FORCE_NONE;
+        return AUDIO_POLICY_FORCE_NONE;
     }
-    if (usage < 0 || usage >= AudioSystem::NUM_FORCE_USE) {
-        return AudioSystem::FORCE_NONE;
+    if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
+        return AUDIO_POLICY_FORCE_NONE;
     }
-    return mpPolicyManager->getForceUse(usage);
+    return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
 }
 
-audio_io_handle_t AudioPolicyService::getOutput(AudioSystem::stream_type stream,
+audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
                                     uint32_t samplingRate,
                                     uint32_t format,
                                     uint32_t channels,
-                                    AudioSystem::output_flags flags)
+                                    audio_policy_output_flags_t flags)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return 0;
     }
     LOGV("getOutput() tid %d", gettid());
     Mutex::Autolock _l(mLock);
-    return mpPolicyManager->getOutput(stream, samplingRate, format, channels, flags);
+    return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags);
 }
 
 status_t AudioPolicyService::startOutput(audio_io_handle_t output,
-                                         AudioSystem::stream_type stream,
+                                         audio_stream_type_t stream,
                                          int session)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     LOGV("startOutput() tid %d", gettid());
     Mutex::Autolock _l(mLock);
-    return mpPolicyManager->startOutput(output, stream, session);
+    return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
 }
 
 status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
-                                        AudioSystem::stream_type stream,
+                                        audio_stream_type_t stream,
                                         int session)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     LOGV("stopOutput() tid %d", gettid());
     Mutex::Autolock _l(mLock);
-    return mpPolicyManager->stopOutput(output, stream, session);
+    return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
 }
 
 void AudioPolicyService::releaseOutput(audio_io_handle_t output)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return;
     }
     LOGV("releaseOutput() tid %d", gettid());
     Mutex::Autolock _l(mLock);
-    mpPolicyManager->releaseOutput(output);
+    mpAudioPolicy->release_output(mpAudioPolicy, output);
 }
 
 audio_io_handle_t AudioPolicyService::getInput(int inputSource,
                                     uint32_t samplingRate,
                                     uint32_t format,
                                     uint32_t channels,
-                                    AudioSystem::audio_in_acoustics acoustics)
+                                    audio_in_acoustics_t acoustics)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return 0;
     }
     Mutex::Autolock _l(mLock);
-    return mpPolicyManager->getInput(inputSource, samplingRate, format, channels, acoustics);
+    return mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, format, channels, acoustics);
 }
 
 status_t AudioPolicyService::startInput(audio_io_handle_t input)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     Mutex::Autolock _l(mLock);
-    return mpPolicyManager->startInput(input);
+    return mpAudioPolicy->start_input(mpAudioPolicy, input);
 }
 
 status_t AudioPolicyService::stopInput(audio_io_handle_t input)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     Mutex::Autolock _l(mLock);
-    return mpPolicyManager->stopInput(input);
+    return mpAudioPolicy->stop_input(mpAudioPolicy, input);
 }
 
 void AudioPolicyService::releaseInput(audio_io_handle_t input)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return;
     }
     Mutex::Autolock _l(mLock);
-    mpPolicyManager->releaseInput(input);
+    mpAudioPolicy->release_input(mpAudioPolicy, input);
 }
 
-status_t AudioPolicyService::initStreamVolume(AudioSystem::stream_type stream,
+status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
                                             int indexMin,
                                             int indexMax)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     if (!checkPermission()) {
         return PERMISSION_DENIED;
     }
-    if (stream < 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
+    if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
         return BAD_VALUE;
     }
-    mpPolicyManager->initStreamVolume(stream, indexMin, indexMax);
+    mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
     return NO_ERROR;
 }
 
-status_t AudioPolicyService::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)
+status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, int index)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     if (!checkPermission()) {
         return PERMISSION_DENIED;
     }
-    if (stream < 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
+    if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
         return BAD_VALUE;
     }
 
-    return mpPolicyManager->setStreamVolumeIndex(stream, index);
+    return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
 }
 
-status_t AudioPolicyService::getStreamVolumeIndex(AudioSystem::stream_type stream, int *index)
+status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, int *index)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     if (!checkPermission()) {
         return PERMISSION_DENIED;
     }
-    if (stream < 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
+    if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
         return BAD_VALUE;
     }
-    return mpPolicyManager->getStreamVolumeIndex(stream, index);
+    return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
 }
 
-uint32_t AudioPolicyService::getStrategyForStream(AudioSystem::stream_type stream)
+uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return 0;
     }
-    return mpPolicyManager->getStrategyForStream(stream);
+    return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
 }
 
-uint32_t AudioPolicyService::getDevicesForStream(AudioSystem::stream_type stream)
+uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return 0;
     }
-    return mpPolicyManager->getDevicesForStream(stream);
+    return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
 }
 
 audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
     Mutex::Autolock _l(mLock);
-    return mpPolicyManager->getOutputForEffect(desc);
+    return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
 }
 
 status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc,
@@ -388,27 +399,27 @@
                                 int session,
                                 int id)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
-    return mpPolicyManager->registerEffect(desc, output, strategy, session, id);
+    return mpAudioPolicy->register_effect(mpAudioPolicy, desc, output, strategy, session, id);
 }
 
 status_t AudioPolicyService::unregisterEffect(int id)
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return NO_INIT;
     }
-    return mpPolicyManager->unregisterEffect(id);
+    return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
 }
 
 bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const
 {
-    if (mpPolicyManager == NULL) {
+    if (mpAudioPolicy == NULL) {
         return 0;
     }
     Mutex::Autolock _l(mLock);
-    return mpPolicyManager->isStreamActive(stream, inPastMs);
+    return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
 }
 
 void AudioPolicyService::binderDied(const wp<IBinder>& who) {
@@ -435,7 +446,7 @@
     char buffer[SIZE];
     String8 result;
 
-    snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpPolicyManager);
+    snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
     result.append(buffer);
     snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
     result.append(buffer);
@@ -465,8 +476,8 @@
             mTonePlaybackThread->dump(fd);
         }
 
-        if (mpPolicyManager) {
-            mpPolicyManager->dump(fd);
+        if (mpAudioPolicy) {
+            mpAudioPolicy->dump(mpAudioPolicy, fd);
         }
 
         if (locked) mLock.unlock();
@@ -495,154 +506,6 @@
 }
 
 
-// ----------------------------------------------------------------------------
-// AudioPolicyClientInterface implementation
-// ----------------------------------------------------------------------------
-
-
-audio_io_handle_t AudioPolicyService::openOutput(uint32_t *pDevices,
-                                uint32_t *pSamplingRate,
-                                uint32_t *pFormat,
-                                uint32_t *pChannels,
-                                uint32_t *pLatencyMs,
-                                AudioSystem::output_flags flags)
-{
-    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
-    if (af == 0) {
-        LOGW("openOutput() could not get AudioFlinger");
-        return 0;
-    }
-
-    return af->openOutput(pDevices,
-                          pSamplingRate,
-                          (uint32_t *)pFormat,
-                          pChannels,
-                          pLatencyMs,
-                          flags);
-}
-
-audio_io_handle_t AudioPolicyService::openDuplicateOutput(audio_io_handle_t output1,
-                                                          audio_io_handle_t output2)
-{
-    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
-    if (af == 0) {
-        LOGW("openDuplicateOutput() could not get AudioFlinger");
-        return 0;
-    }
-    return af->openDuplicateOutput(output1, output2);
-}
-
-status_t AudioPolicyService::closeOutput(audio_io_handle_t output)
-{
-    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
-    if (af == 0) return PERMISSION_DENIED;
-
-    return af->closeOutput(output);
-}
-
-
-status_t AudioPolicyService::suspendOutput(audio_io_handle_t output)
-{
-    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
-    if (af == 0) {
-        LOGW("suspendOutput() could not get AudioFlinger");
-        return PERMISSION_DENIED;
-    }
-
-    return af->suspendOutput(output);
-}
-
-status_t AudioPolicyService::restoreOutput(audio_io_handle_t output)
-{
-    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
-    if (af == 0) {
-        LOGW("restoreOutput() could not get AudioFlinger");
-        return PERMISSION_DENIED;
-    }
-
-    return af->restoreOutput(output);
-}
-
-audio_io_handle_t AudioPolicyService::openInput(uint32_t *pDevices,
-                                uint32_t *pSamplingRate,
-                                uint32_t *pFormat,
-                                uint32_t *pChannels,
-                                uint32_t acoustics)
-{
-    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
-    if (af == 0) {
-        LOGW("openInput() could not get AudioFlinger");
-        return 0;
-    }
-
-    return af->openInput(pDevices, pSamplingRate, (uint32_t *)pFormat, pChannels, acoustics);
-}
-
-status_t AudioPolicyService::closeInput(audio_io_handle_t input)
-{
-    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
-    if (af == 0) return PERMISSION_DENIED;
-
-    return af->closeInput(input);
-}
-
-status_t AudioPolicyService::setStreamVolume(AudioSystem::stream_type stream,
-                                             float volume,
-                                             audio_io_handle_t output,
-                                             int delayMs)
-{
-    return mAudioCommandThread->volumeCommand((int)stream, volume, (int)output, delayMs);
-}
-
-status_t AudioPolicyService::setStreamOutput(AudioSystem::stream_type stream,
-                                             audio_io_handle_t output)
-{
-    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
-    if (af == 0) return PERMISSION_DENIED;
-
-    return af->setStreamOutput(stream, output);
-}
-
-status_t AudioPolicyService::moveEffects(int session, audio_io_handle_t srcOutput,
-                                               audio_io_handle_t dstOutput)
-{
-    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
-    if (af == 0) return PERMISSION_DENIED;
-
-    return af->moveEffects(session, (int)srcOutput, (int)dstOutput);
-}
-
-void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
-                                       const String8& keyValuePairs,
-                                       int delayMs)
-{
-    mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs, delayMs);
-}
-
-String8 AudioPolicyService::getParameters(audio_io_handle_t ioHandle, const String8& keys)
-{
-    String8 result = AudioSystem::getParameters(ioHandle, keys);
-    return result;
-}
-
-status_t AudioPolicyService::startTone(ToneGenerator::tone_type tone,
-                                       AudioSystem::stream_type stream)
-{
-    mTonePlaybackThread->startToneCommand(tone, stream);
-    return NO_ERROR;
-}
-
-status_t AudioPolicyService::stopTone()
-{
-    mTonePlaybackThread->stopToneCommand();
-    return NO_ERROR;
-}
-
-status_t AudioPolicyService::setVoiceVolume(float volume, int delayMs)
-{
-    return mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
-}
-
 // -----------  AudioPolicyService::AudioCommandThread implementation ----------
 
 AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
@@ -859,7 +722,7 @@
 }
 
 status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle,
-                                                                   const String8& keyValuePairs,
+                                                                   const char *keyValuePairs,
                                                                    int delayMs)
 {
     status_t status = NO_ERROR;
@@ -868,7 +731,7 @@
     command->mCommand = SET_PARAMETERS;
     ParametersData *data = new ParametersData();
     data->mIO = ioHandle;
-    data->mKeyValuePairs = keyValuePairs;
+    data->mKeyValuePairs = String8(keyValuePairs);
     command->mParam = data;
     if (delayMs == 0) {
         command->mWaitStatus = true;
@@ -878,7 +741,7 @@
     Mutex::Autolock _l(mLock);
     insertCommand_l(command, delayMs);
     LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
-            keyValuePairs.string(), ioHandle, delayMs);
+            keyValuePairs, ioHandle, delayMs);
     mWaitWorkCV.signal();
     if (command->mWaitStatus) {
         command->mCond.wait(mLock);
@@ -1023,4 +886,226 @@
             mParam);
 }
 
+/******* helpers for the service_ops callbacks defined below *********/
+void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
+                                       const char *keyValuePairs,
+                                       int delayMs)
+{
+    mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs,
+                                           delayMs);
+}
+
+int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
+                                        float volume,
+                                        audio_io_handle_t output,
+                                        int delayMs)
+{
+    return (int)mAudioCommandThread->volumeCommand((int)stream, volume,
+                                                   (int)output, delayMs);
+}
+
+int AudioPolicyService::startTone(audio_policy_tone_t tone,
+                                  audio_stream_type_t stream)
+{
+    if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
+        LOGE("startTone: illegal tone requested (%d)", tone);
+    if (stream != AUDIO_STREAM_VOICE_CALL)
+        LOGE("startTone: illegal stream (%d) requested for tone %d", stream,
+             tone);
+    mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
+                                          AUDIO_STREAM_VOICE_CALL);
+    return 0;
+}
+
+int AudioPolicyService::stopTone()
+{
+    mTonePlaybackThread->stopToneCommand();
+    return 0;
+}
+
+int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
+{
+    return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
+}
+
+/* implementation of the interface to the policy manager */
+extern "C" {
+
+static audio_io_handle_t aps_open_output(void *service,
+                                             uint32_t *pDevices,
+                                             uint32_t *pSamplingRate,
+                                             uint32_t *pFormat,
+                                             uint32_t *pChannels,
+                                             uint32_t *pLatencyMs,
+                                             audio_policy_output_flags_t flags)
+{
+    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
+    if (af == NULL) {
+        LOGW("%s: could not get AudioFlinger", __func__);
+        return 0;
+    }
+
+    return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels,
+                          pLatencyMs, flags);
+}
+
+static audio_io_handle_t aps_open_dup_output(void *service,
+                                                 audio_io_handle_t output1,
+                                                 audio_io_handle_t output2)
+{
+    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
+    if (af == NULL) {
+        LOGW("%s: could not get AudioFlinger", __func__);
+        return 0;
+    }
+    return af->openDuplicateOutput(output1, output2);
+}
+
+static int aps_close_output(void *service, audio_io_handle_t output)
+{
+    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
+    if (af == NULL)
+        return PERMISSION_DENIED;
+
+    return af->closeOutput(output);
+}
+
+static int aps_suspend_output(void *service, audio_io_handle_t output)
+{
+    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
+    if (af == NULL) {
+        LOGW("%s: could not get AudioFlinger", __func__);
+        return PERMISSION_DENIED;
+    }
+
+    return af->suspendOutput(output);
+}
+
+static int aps_restore_output(void *service, audio_io_handle_t output)
+{
+    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
+    if (af == NULL) {
+        LOGW("%s: could not get AudioFlinger", __func__);
+        return PERMISSION_DENIED;
+    }
+
+    return af->restoreOutput(output);
+}
+
+static audio_io_handle_t aps_open_input(void *service,
+                                            uint32_t *pDevices,
+                                            uint32_t *pSamplingRate,
+                                            uint32_t *pFormat,
+                                            uint32_t *pChannels,
+                                            uint32_t acoustics)
+{
+    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
+    if (af == NULL) {
+        LOGW("%s: could not get AudioFlinger", __func__);
+        return 0;
+    }
+
+    return af->openInput(pDevices, pSamplingRate, pFormat, pChannels,
+                         acoustics);
+}
+
+static int aps_close_input(void *service, audio_io_handle_t input)
+{
+    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
+    if (af == NULL)
+        return PERMISSION_DENIED;
+
+    return af->closeInput(input);
+}
+
+static int aps_set_stream_output(void *service, audio_stream_type_t stream,
+                                     audio_io_handle_t output)
+{
+    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
+    if (af == NULL)
+        return PERMISSION_DENIED;
+
+    return af->setStreamOutput(stream, output);
+}
+
+static int aps_move_effects(void *service, int session,
+                                audio_io_handle_t src_output,
+                                audio_io_handle_t dst_output)
+{
+    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
+    if (af == NULL)
+        return PERMISSION_DENIED;
+
+    return af->moveEffects(session, (int)src_output, (int)dst_output);
+}
+
+static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
+                                     const char *keys)
+{
+    String8 result = AudioSystem::getParameters(io_handle, String8(keys));
+    return strdup(result.string());
+}
+
+static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
+                                   const char *kv_pairs, int delay_ms)
+{
+    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
+
+    audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
+}
+
+static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
+                                     float volume, audio_io_handle_t output,
+                                     int delay_ms)
+{
+    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
+
+    return audioPolicyService->setStreamVolume(stream, volume, output,
+                                               delay_ms);
+}
+
+static int aps_start_tone(void *service, audio_policy_tone_t tone,
+                              audio_stream_type_t stream)
+{
+    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
+
+    return audioPolicyService->startTone(tone, stream);
+}
+
+static int aps_stop_tone(void *service)
+{
+    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
+
+    return audioPolicyService->stopTone();
+}
+
+static int aps_set_voice_volume(void *service, float volume, int delay_ms)
+{
+    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
+
+    return audioPolicyService->setVoiceVolume(volume, delay_ms);
+}
+
+}; // extern "C"
+
+namespace {
+    struct audio_policy_service_ops aps_ops = {
+        open_output           : aps_open_output,
+        open_duplicate_output : aps_open_dup_output,
+        close_output          : aps_close_output,
+        suspend_output        : aps_suspend_output,
+        restore_output        : aps_restore_output,
+        open_input            : aps_open_input,
+        close_input           : aps_close_input,
+        set_stream_volume     : aps_set_stream_volume,
+        set_stream_output     : aps_set_stream_output,
+        set_parameters        : aps_set_parameters,
+        get_parameters        : aps_get_parameters,
+        start_tone            : aps_start_tone,
+        stop_tone             : aps_stop_tone,
+        set_voice_volume      : aps_set_voice_volume,
+        move_effects          : aps_move_effects,
+    };
+}; // namespace <unnamed>
+
 }; // namespace android
diff --git a/services/audioflinger/AudioPolicyService.h b/services/audioflinger/AudioPolicyService.h
index faad893..01e592b 100644
--- a/services/audioflinger/AudioPolicyService.h
+++ b/services/audioflinger/AudioPolicyService.h
@@ -18,11 +18,14 @@
 #define ANDROID_AUDIOPOLICYSERVICE_H
 
 #include <media/IAudioPolicyService.h>
-#include <hardware_legacy/AudioPolicyInterface.h>
 #include <media/ToneGenerator.h>
 #include <utils/Vector.h>
 #include <binder/BinderService.h>
 
+#include <hardware/audio.h>
+#include <hardware/audio_policy.h>
+#include <hardware/audio_policy_hal.h>
+
 namespace android {
 
 class String8;
@@ -32,7 +35,7 @@
 class AudioPolicyService :
     public BinderService<AudioPolicyService>,
     public BnAudioPolicyService,
-    public AudioPolicyClientInterface,
+//    public AudioPolicyClientInterface,
     public IBinder::DeathRecipient
 {
     friend class BinderService<AudioPolicyService>;
@@ -47,46 +50,46 @@
     // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
     //
 
-    virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
-                                              AudioSystem::device_connection_state state,
+    virtual status_t setDeviceConnectionState(audio_devices_t device,
+                                              audio_policy_dev_state_t state,
                                               const char *device_address);
-    virtual AudioSystem::device_connection_state getDeviceConnectionState(
-                                                                AudioSystem::audio_devices device,
+    virtual audio_policy_dev_state_t getDeviceConnectionState(
+                                                                audio_devices_t device,
                                                                 const char *device_address);
     virtual status_t setPhoneState(int state);
     virtual status_t setRingerMode(uint32_t mode, uint32_t mask);
-    virtual status_t setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config);
-    virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage);
-    virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
+    virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
+    virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
+    virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
                                         uint32_t samplingRate = 0,
-                                        uint32_t format = AudioSystem::FORMAT_DEFAULT,
+                                        uint32_t format = AUDIO_FORMAT_DEFAULT,
                                         uint32_t channels = 0,
-                                        AudioSystem::output_flags flags =
-                                                AudioSystem::OUTPUT_FLAG_INDIRECT);
+                                        audio_policy_output_flags_t flags =
+                                            AUDIO_POLICY_OUTPUT_FLAG_INDIRECT);
     virtual status_t startOutput(audio_io_handle_t output,
-                                 AudioSystem::stream_type stream,
+                                 audio_stream_type_t stream,
                                  int session = 0);
     virtual status_t stopOutput(audio_io_handle_t output,
-                                AudioSystem::stream_type stream,
+                                audio_stream_type_t stream,
                                 int session = 0);
     virtual void releaseOutput(audio_io_handle_t output);
     virtual audio_io_handle_t getInput(int inputSource,
                                     uint32_t samplingRate = 0,
-                                    uint32_t format = AudioSystem::FORMAT_DEFAULT,
+                                    uint32_t format = AUDIO_FORMAT_DEFAULT,
                                     uint32_t channels = 0,
-                                    AudioSystem::audio_in_acoustics acoustics =
-                                            (AudioSystem::audio_in_acoustics)0);
+                                    audio_in_acoustics_t acoustics =
+                                            (audio_in_acoustics_t)0);
     virtual status_t startInput(audio_io_handle_t input);
     virtual status_t stopInput(audio_io_handle_t input);
     virtual void releaseInput(audio_io_handle_t input);
-    virtual status_t initStreamVolume(AudioSystem::stream_type stream,
+    virtual status_t initStreamVolume(audio_stream_type_t stream,
                                       int indexMin,
                                       int indexMax);
-    virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index);
-    virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index);
+    virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, int index);
+    virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, int *index);
 
-    virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream);
-    virtual uint32_t getDevicesForStream(AudioSystem::stream_type stream);
+    virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
+    virtual uint32_t getDevicesForStream(audio_stream_type_t stream);
 
     virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc);
     virtual status_t registerEffect(effect_descriptor_t *desc,
@@ -107,40 +110,21 @@
     virtual     void        binderDied(const wp<IBinder>& who);
 
     //
-    // AudioPolicyClientInterface
+    // Helpers for the struct audio_policy_service_ops implementation.
+    // This is used by the audio policy manager for certain operations that
+    // are implemented by the policy service.
     //
-    virtual audio_io_handle_t openOutput(uint32_t *pDevices,
-                                    uint32_t *pSamplingRate,
-                                    uint32_t *pFormat,
-                                    uint32_t *pChannels,
-                                    uint32_t *pLatencyMs,
-                                    AudioSystem::output_flags flags);
-    virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
-                                                  audio_io_handle_t output2);
-    virtual status_t closeOutput(audio_io_handle_t output);
-    virtual status_t suspendOutput(audio_io_handle_t output);
-    virtual status_t restoreOutput(audio_io_handle_t output);
-    virtual audio_io_handle_t openInput(uint32_t *pDevices,
-                                    uint32_t *pSamplingRate,
-                                    uint32_t *pFormat,
-                                    uint32_t *pChannels,
-                                    uint32_t acoustics);
-    virtual status_t closeInput(audio_io_handle_t input);
-    virtual status_t setStreamVolume(AudioSystem::stream_type stream,
+    virtual void setParameters(audio_io_handle_t ioHandle,
+                               const char *keyValuePairs,
+                               int delayMs);
+
+    virtual status_t setStreamVolume(audio_stream_type_t stream,
                                      float volume,
                                      audio_io_handle_t output,
                                      int delayMs = 0);
-    virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output);
-    virtual void setParameters(audio_io_handle_t ioHandle,
-                               const String8& keyValuePairs,
-                               int delayMs = 0);
-    virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
-    virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream);
+    virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
     virtual status_t stopTone();
     virtual status_t setVoiceVolume(float volume, int delayMs = 0);
-    virtual status_t moveEffects(int session,
-                                     audio_io_handle_t srcOutput,
-                                     audio_io_handle_t dstOutput);
 
 private:
                         AudioPolicyService();
@@ -180,7 +164,7 @@
                     void        startToneCommand(int type = 0, int stream = 0);
                     void        stopToneCommand();
                     status_t    volumeCommand(int stream, float volume, int output, int delayMs = 0);
-                    status_t    parametersCommand(int ioHandle, const String8& keyValuePairs, int delayMs = 0);
+                    status_t    parametersCommand(int ioHandle, const char *keyValuePairs, int delayMs = 0);
                     status_t    voiceVolumeCommand(float volume, int delayMs = 0);
                     void        insertCommand_l(AudioCommand *command, int delayMs = 0);
 
@@ -240,9 +224,11 @@
 
     mutable Mutex mLock;    // prevents concurrent access to AudioPolicy manager functions changing
                             // device connection state  or routing
-    AudioPolicyInterface* mpPolicyManager;          // the platform specific policy manager
     sp <AudioCommandThread> mAudioCommandThread;    // audio commands thread
     sp <AudioCommandThread> mTonePlaybackThread;     // tone playback thread
+
+    struct audio_policy_device *mpAudioPolicyDev;
+    struct audio_policy *mpAudioPolicy;
 };
 
 }; // namespace android
diff --git a/services/camera/libcameraservice/CameraService.cpp b/services/camera/libcameraservice/CameraService.cpp
index 7e3c643..f4859ec 100644
--- a/services/camera/libcameraservice/CameraService.cpp
+++ b/services/camera/libcameraservice/CameraService.cpp
@@ -244,7 +244,7 @@
 static MediaPlayer* newMediaPlayer(const char *file) {
     MediaPlayer* mp = new MediaPlayer();
     if (mp->setDataSource(file, NULL) == NO_ERROR) {
-        mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
+        mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
         mp->prepare();
     } else {
         LOGE("Failed to load CameraService sounds: %s", file);
@@ -283,7 +283,7 @@
         // do not play the sound if stream volume is 0
         // (typically because ringer mode is silent).
         int index;
-        AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
+        AudioSystem::getStreamVolumeIndex(AUDIO_STREAM_ENFORCED_AUDIBLE, &index);
         if (index != 0) {
             player->seekTo(0);
             player->start();