AudioTrack.cpp revision 511754b5839fd9b09fc56b89ae007fbc39084a33
1362c4e697d8e9c034e964ac7b40227e054491547Glenn Kasten/* frameworks/base/media/libmedia/AudioTrack.cpp
289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Copyright 2007, The Android Open Source Project
489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");
689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** you may not use this file except in compliance with the License.
789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** You may obtain a copy of the License at
889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**     http://www.apache.org/licenses/LICENSE-2.0
1089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
1189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Unless required by applicable law or agreed to in writing, software
1289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** distributed under the License is distributed on an "AS IS" BASIS,
1389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** See the License for the specific language governing permissions and
1589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** limitations under the License.
1689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project*/
1789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project//#define LOG_NDEBUG 0
2089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#define LOG_TAG "AudioTrack"
2189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <stdint.h>
2389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <sys/types.h>
2489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <limits.h>
2589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <sched.h>
2789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <sys/resource.h>
2889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <private/media/AudioTrackShared.h>
3089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
3189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/AudioSystem.h>
3289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/AudioTrack.h>
3389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
3489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <utils/Log.h>
357562408b2261d38415453378b6188f74fda99d88Mathias Agopian#include <binder/Parcel.h>
367562408b2261d38415453378b6188f74fda99d88Mathias Agopian#include <binder/IPCThreadState.h>
3789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <utils/Timers.h>
3838ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent#include <utils/Atomic.h>
3989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
40fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <cutils/bitops.h>
41f6b1678f8f508b447155a81b44e214475ab634a8Glenn Kasten#include <cutils/compiler.h>
42fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
4364760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
447394a4f358fa9908a9f0a7c954b65c399f4268e6Dima Zavin#include <system/audio_policy.h>
45fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
46511754b5839fd9b09fc56b89ae007fbc39084a33Glenn Kasten#include <audio_utils/primitives.h>
47511754b5839fd9b09fc56b89ae007fbc39084a33Glenn Kasten
4889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectnamespace android {
4933005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh// ---------------------------------------------------------------------------
5033005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh
5133005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh// static
5233005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yehstatus_t AudioTrack::getMinFrameCount(
5333005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh        int* frameCount,
54fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
5533005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh        uint32_t sampleRate)
5633005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh{
5733005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    int afSampleRate;
5833005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
5933005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh        return NO_INIT;
6033005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    }
6133005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    int afFrameCount;
6233005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
6333005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh        return NO_INIT;
6433005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    }
6533005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    uint32_t afLatency;
6633005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
6733005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh        return NO_INIT;
6833005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    }
6933005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh
7033005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    // Ensure that buffer depth covers at least audio hardware latency
7133005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
7233005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    if (minBufCount < 2) minBufCount = 2;
7333005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh
7433005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
7533005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh              afFrameCount * minBufCount * sampleRate / afSampleRate;
7633005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    return NO_ERROR;
7733005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh}
7889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
7989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// ---------------------------------------------------------------------------
8089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
8189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioTrack::AudioTrack()
82879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten    : mStatus(NO_INIT),
83879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
8489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
8589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
8689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
8789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioTrack::AudioTrack(
88fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
8989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t sampleRate,
90e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten        audio_format_t format,
910d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi        int channelMask,
9289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        int frameCount,
9389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t flags,
9489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        callback_t cbf,
9589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        void* user,
96be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        int notificationFrames,
97be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        int sessionId)
98879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten    : mStatus(NO_INIT),
99879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
10089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
1010d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi    mStatus = set(streamType, sampleRate, format, channelMask,
102a514bdb58b5de4986679f72b7204b4764f7a2778Eric Laurent            frameCount, flags, cbf, user, notificationFrames,
103a514bdb58b5de4986679f72b7204b4764f7a2778Eric Laurent            0, false, sessionId);
10489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
10589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
10689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioTrack::AudioTrack(
107fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
10889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t sampleRate,
109e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten        audio_format_t format,
1100d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi        int channelMask,
11189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        const sp<IMemory>& sharedBuffer,
11289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t flags,
11389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        callback_t cbf,
11489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        void* user,
115be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        int notificationFrames,
116be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        int sessionId)
117879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten    : mStatus(NO_INIT),
118879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
11989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
1200d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi    mStatus = set(streamType, sampleRate, format, channelMask,
121a514bdb58b5de4986679f72b7204b4764f7a2778Eric Laurent            0, flags, cbf, user, notificationFrames,
122a514bdb58b5de4986679f72b7204b4764f7a2778Eric Laurent            sharedBuffer, false, sessionId);
12389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
12489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
12589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioTrack::~AudioTrack()
12689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
1273856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
12889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
12989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mStatus == NO_ERROR) {
13089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Make sure that callback function exits in the case where
13189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // it is looping on buffer full condition in obtainBuffer().
13289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Otherwise the callback thread will never exit.
13389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        stop();
13489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (mAudioTrackThread != 0) {
13589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mAudioTrackThread->requestExitAndWait();
13689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mAudioTrackThread.clear();
13789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
13889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mAudioTrack.clear();
13989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        IPCThreadState::self()->flushCommands();
1403a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSystem::releaseAudioSessionId(mSessionId);
14189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
14289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
14389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
14489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::set(
145fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
14689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t sampleRate,
147e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten        audio_format_t format,
1480d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi        int channelMask,
14989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        int frameCount,
15089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t flags,
15189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        callback_t cbf,
15289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        void* user,
15389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        int notificationFrames,
15489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        const sp<IMemory>& sharedBuffer,
155be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        bool threadCanCallJava,
156be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        int sessionId)
15789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
15889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1593856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
16089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1611703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
1621dd70b9f04961a06fcb73a97fca10a53b3245d3cEric Laurent    if (mAudioTrack != 0) {
16329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Track already in use");
16489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return INVALID_OPERATION;
16589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
16689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
16789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    int afSampleRate;
16889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
16989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return NO_INIT;
17089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
17189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    uint32_t afLatency;
17289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
17389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return NO_INIT;
17489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
17589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
17689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // handle default values first.
177fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (streamType == AUDIO_STREAM_DEFAULT) {
178fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        streamType = AUDIO_STREAM_MUSIC;
17989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
18089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (sampleRate == 0) {
18189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        sampleRate = afSampleRate;
18289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
18389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // these below should probably come from the audioFlinger too...
184e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten    if (format == AUDIO_FORMAT_DEFAULT) {
185fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        format = AUDIO_FORMAT_PCM_16_BIT;
18689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
1870d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi    if (channelMask == 0) {
1880d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi        channelMask = AUDIO_CHANNEL_OUT_STEREO;
18989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
19089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
19189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // validate parameters
192fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (!audio_is_valid_format(format)) {
19329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Invalid format");
19489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
19589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
196c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
197c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    // force direct flag if format is not linear PCM
198fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (!audio_is_linear_pcm(format)) {
199fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT;
200c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
201c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
2020d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi    if (!audio_is_output_channel(channelMask)) {
20329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Invalid channel mask");
20489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
20589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
2060d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi    uint32_t channelCount = popcount(channelMask);
20789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
208fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    audio_io_handle_t output = AudioSystem::getOutput(
209fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten                                    streamType,
210e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten                                    sampleRate, format, channelMask,
211fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                    (audio_policy_output_flags_t)flags);
21289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
213c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    if (output == 0) {
21429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Could not get audio output for stream type %d", streamType);
215c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        return BAD_VALUE;
21689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
21789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
21834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    mVolume[LEFT] = 1.0f;
21934f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    mVolume[RIGHT] = 1.0f;
22005632a5fa4b88ca474294887fc92a9fcdf0e2352Glenn Kasten    mSendLevel = 0.0f;
221d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    mFrameCount = frameCount;
222d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    mNotificationFramesReq = notificationFrames;
223be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    mSessionId = sessionId;
2242beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    mAuxEffectId = 0;
225be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
22634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    // create the IAudioTrack
2271703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    status_t status = createTrack_l(streamType,
2281703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                                  sampleRate,
229e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten                                  format,
2300d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi                                  (uint32_t)channelMask,
2311703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                                  frameCount,
2321703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                                  flags,
2331703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                                  sharedBuffer,
2341703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                                  output,
2351703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                                  true);
23689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
23734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (status != NO_ERROR) {
23889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return status;
23989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
24034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
24189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (cbf != 0) {
24289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
24389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
24489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
24589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mStatus = NO_ERROR;
24689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
24789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mStreamType = streamType;
248e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten    mFormat = format;
2490d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi    mChannelMask = (uint32_t)channelMask;
25089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mChannelCount = channelCount;
25189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mSharedBuffer = sharedBuffer;
25289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mMuted = false;
2539a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    mActive = false;
25489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mCbf = cbf;
25589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mUserData = user;
25689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mLoopCount = 0;
25789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mMarkerPosition = 0;
2582c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mMarkerReached = false;
25989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mNewPosition = 0;
26089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mUpdatePeriod = 0;
261cd07594333cbe8b2c86c6609cce01a74d6cc33f8Jean-Michel Trivi    mFlushed = false;
262c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    mFlags = flags;
2633a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    AudioSystem::acquireAudioSessionId(mSessionId);
264cfe2ba6b01a258e39f9c215ffc7b750e0b68f708Eric Laurent    mRestoreStatus = NO_ERROR;
26589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
26689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
26789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
26889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::initCheck() const
26989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
27089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return mStatus;
27189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
27289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
27389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
27489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
27589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectuint32_t AudioTrack::latency() const
27689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
27789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return mLatency;
27889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
27989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
280fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenaudio_stream_type_t AudioTrack::streamType() const
28189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
28289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return mStreamType;
28389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
28489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
285e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kastenaudio_format_t AudioTrack::format() const
28689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
28789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return mFormat;
28889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
28989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
29089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectint AudioTrack::channelCount() const
29189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
29289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return mChannelCount;
29389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
29489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
29589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectuint32_t AudioTrack::frameCount() const
29689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
297d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    return mCblk->frameCount;
29889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
29989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
300b9980659501d0428d65d8292f3c32da69d37fbd2Glenn Kastensize_t AudioTrack::frameSize() const
30189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
302fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (audio_is_linear_pcm(mFormat)) {
303671a636931295d9c33ffca74551a804479d01241Eric Laurent        return channelCount()*audio_bytes_per_sample(mFormat);
304c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    } else {
305c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        return sizeof(uint8_t);
306c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
30789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
30889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
30989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectsp<IMemory>& AudioTrack::sharedBuffer()
31089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
31189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return mSharedBuffer;
31289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
31389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
31489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
31589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
31689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::start()
31789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
31889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    sp<AudioTrackThread> t = mAudioTrackThread;
319d0965dde97f2815ae0a15fe6b40946f8a741a81eGlenn Kasten    status_t status = NO_ERROR;
32089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
3213856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("start %p", this);
32289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (t != 0) {
32389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (t->exitPending()) {
32489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            if (t->requestExitAndWait() == WOULD_BLOCK) {
32529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                ALOGE("AudioTrack::start called from thread");
32689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                return;
32789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
32889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
32989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        t->mLock.lock();
33089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project     }
33189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
332f5aafb209d01ba2ab6cb55d1a12cfc653e2b4be0Eric Laurent    AutoMutex lock(mLock);
3331703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
3341703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    // while we are accessing the cblk
3351703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    sp <IAudioTrack> audioTrack = mAudioTrack;
3361703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    sp <IMemory> iMem = mCblkMemory;
3371703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    audio_track_cblk_t* cblk = mCblk;
3381703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
3399a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    if (!mActive) {
340cd07594333cbe8b2c86c6609cce01a74d6cc33f8Jean-Michel Trivi        mFlushed = false;
3419a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten        mActive = true;
3421703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        mNewPosition = cblk->server + mUpdatePeriod;
34333797ea64d067dfeaacbfd7ebe7f3383b73961b5Eric Laurent        cblk->lock.lock();
3441703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
3451703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        cblk->waitTimeMs = 0;
34638ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
3472b584244930c9de0e3bc46898a801e9ccb731900Eric Laurent        if (t != 0) {
348879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten            t->run("AudioTrackThread", ANDROID_PRIORITY_AUDIO);
3492b584244930c9de0e3bc46898a801e9ccb731900Eric Laurent        } else {
350879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten            mPreviousPriority = getpriority(PRIO_PROCESS, 0);
351879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten            mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
352879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten            androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
3532b584244930c9de0e3bc46898a801e9ccb731900Eric Laurent        }
3542b584244930c9de0e3bc46898a801e9ccb731900Eric Laurent
3553856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("start %p before lock cblk %p", this, mCblk);
3561703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        if (!(cblk->flags & CBLK_INVALID_MSK)) {
3571703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            cblk->lock.unlock();
358d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            status = mAudioTrack->start();
3591703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            cblk->lock.lock();
3601703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            if (status == DEAD_OBJECT) {
36138ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent                android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
3626100d2d60517ff33ed8eb35d0b7ea63cde0831c9Eric Laurent            }
3632b584244930c9de0e3bc46898a801e9ccb731900Eric Laurent        }
3641703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        if (cblk->flags & CBLK_INVALID_MSK) {
3651703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            status = restoreTrack_l(cblk, true);
3661703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        }
3671703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        cblk->lock.unlock();
3682b584244930c9de0e3bc46898a801e9ccb731900Eric Laurent        if (status != NO_ERROR) {
3693856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("start() failed");
3709a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten            mActive = false;
37134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent            if (t != 0) {
3722b584244930c9de0e3bc46898a801e9ccb731900Eric Laurent                t->requestExit();
37334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent            } else {
374879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten                setpriority(PRIO_PROCESS, 0, mPreviousPriority);
375879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten                androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
37634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent            }
37789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
37889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
37989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
38089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (t != 0) {
38189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        t->mLock.unlock();
38289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
38389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
38489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
38589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::stop()
38689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
38789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    sp<AudioTrackThread> t = mAudioTrackThread;
38889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
3893856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("stop %p", this);
39089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (t != 0) {
39189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        t->mLock.lock();
39289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
39389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
394f5aafb209d01ba2ab6cb55d1a12cfc653e2b4be0Eric Laurent    AutoMutex lock(mLock);
3959a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    if (mActive) {
3969a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten        mActive = false;
3971dd70b9f04961a06fcb73a97fca10a53b3245d3cEric Laurent        mCblk->cv.signal();
39889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mAudioTrack->stop();
39989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Cancel loops (If we are in the middle of a loop, playback
40089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // would not stop until loopCount reaches 0).
4011703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        setLoop_l(0, 0, 0);
4022c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi        // the playback head position will reset to 0, so if a marker is set, we need
4032c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi        // to activate it again
4042c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi        mMarkerReached = false;
40589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Force flush if a shared buffer is used otherwise audioflinger
40689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // will not stop before end of buffer is reached.
40789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (mSharedBuffer != 0) {
4081703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            flush_l();
40989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
41089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (t != 0) {
41189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            t->requestExit();
41289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        } else {
413879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten            setpriority(PRIO_PROCESS, 0, mPreviousPriority);
414879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten            androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
41589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
41689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
41789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
41889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (t != 0) {
41989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        t->mLock.unlock();
42089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
42189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
42289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
42389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectbool AudioTrack::stopped() const
42489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
4259a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    AutoMutex lock(mLock);
4269a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    return stopped_l();
42789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
42889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
42989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::flush()
43089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
4311703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
4321703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    flush_l();
4331703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent}
4341703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
4351703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// must be called with mLock held
4361703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurentvoid AudioTrack::flush_l()
4371703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent{
4383856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("flush");
439c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
4402c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    // clear playback marker and periodic update counter
4412c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mMarkerPosition = 0;
4422c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mMarkerReached = false;
4432c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mUpdatePeriod = 0;
444c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
44589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (!mActive) {
446cd07594333cbe8b2c86c6609cce01a74d6cc33f8Jean-Michel Trivi        mFlushed = true;
44789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mAudioTrack->flush();
44889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Release AudioTrack callback thread in case it was waiting for new buffers
44989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // in AudioTrack::obtainBuffer()
45089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mCblk->cv.signal();
45189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
45289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
45389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
45489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::pause()
45589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
4563856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("pause");
457f5aafb209d01ba2ab6cb55d1a12cfc653e2b4be0Eric Laurent    AutoMutex lock(mLock);
4589a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    if (mActive) {
4599a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten        mActive = false;
46089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mAudioTrack->pause();
46189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
46289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
46389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
46489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::mute(bool e)
46589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
46689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mAudioTrack->mute(e);
46789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mMuted = e;
46889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
46989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
47089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectbool AudioTrack::muted() const
47189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
47289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return mMuted;
47389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
47489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
475be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurentstatus_t AudioTrack::setVolume(float left, float right)
47689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
477f0c495012bad92230604a9a12a907812ec49ee8fGlenn Kasten    if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
478be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        return BAD_VALUE;
479be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    }
480be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
4811703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
48289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mVolume[LEFT] = left;
48389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mVolume[RIGHT] = right;
48489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
48589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // write must be atomic
486be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000);
487be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
488be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    return NO_ERROR;
48989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
49089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
49189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::getVolume(float* left, float* right)
49289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
493be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    if (left != NULL) {
494be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        *left  = mVolume[LEFT];
495be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    }
496be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    if (right != NULL) {
497be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        *right = mVolume[RIGHT];
498be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    }
499be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent}
500be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
5012beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurentstatus_t AudioTrack::setAuxEffectSendLevel(float level)
502be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent{
5033856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("setAuxEffectSendLevel(%f)", level);
50405632a5fa4b88ca474294887fc92a9fcdf0e2352Glenn Kasten    if (level < 0.0f || level > 1.0f) {
505be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        return BAD_VALUE;
506be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    }
5071703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
508be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
509be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    mSendLevel = level;
510be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
51105632a5fa4b88ca474294887fc92a9fcdf0e2352Glenn Kasten    mCblk->setSendLevel(level);
512be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
513be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    return NO_ERROR;
514be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent}
515be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
5162beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurentvoid AudioTrack::getAuxEffectSendLevel(float* level)
517be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent{
518be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    if (level != NULL) {
519be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        *level  = mSendLevel;
520be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    }
52189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
52289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
523573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurentstatus_t AudioTrack::setSampleRate(int rate)
52489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
52589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    int afSamplingRate;
52689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
52789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
528573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurent        return NO_INIT;
52989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
53089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // Resampler implementation limits input sampling rate to 2 x output sampling rate.
531573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurent    if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
53289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
5331703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
534573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurent    mCblk->sampleRate = rate;
535573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurent    return NO_ERROR;
53689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
53789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
53889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectuint32_t AudioTrack::getSampleRate()
53989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
5401703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
541573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurent    return mCblk->sampleRate;
54289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
54389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
54489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
54589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
5461703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
5471703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    return setLoop_l(loopStart, loopEnd, loopCount);
5481703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent}
5491703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
5501703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// must be called with mLock held
5511703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurentstatus_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
5521703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent{
55389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    audio_track_cblk_t* cblk = mCblk;
55489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
55589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Mutex::Autolock _l(cblk->lock);
55689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
55789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (loopCount == 0) {
55889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        cblk->loopStart = UINT_MAX;
55989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        cblk->loopEnd = UINT_MAX;
56089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        cblk->loopCount = 0;
56189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mLoopCount = 0;
56289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return NO_ERROR;
56389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
56489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
56589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (loopStart >= loopEnd ||
5669b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent        loopEnd - loopStart > cblk->frameCount ||
5679b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent        cblk->server > loopStart) {
56829357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
56989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
57089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
57189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
5729b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent    if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
57329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
574d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            loopStart, loopEnd, cblk->frameCount);
57589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
576c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
57789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
57889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    cblk->loopStart = loopStart;
57989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    cblk->loopEnd = loopEnd;
58089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    cblk->loopCount = loopCount;
58189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mLoopCount = loopCount;
58289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
58389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
58489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
58589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
58689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount)
58789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
5881703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
58989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (loopStart != 0) {
59089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        *loopStart = mCblk->loopStart;
59189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
59289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (loopEnd != 0) {
59389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        *loopEnd = mCblk->loopEnd;
59489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
59589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (loopCount != 0) {
59689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (mCblk->loopCount < 0) {
59789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            *loopCount = -1;
59889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        } else {
59989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            *loopCount = mCblk->loopCount;
60089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
60189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
60289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
60389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
60489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
60589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
60689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::setMarkerPosition(uint32_t marker)
60789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
60889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mCbf == 0) return INVALID_OPERATION;
60989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
61089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mMarkerPosition = marker;
6112c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mMarkerReached = false;
61289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
61389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
61489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
61589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
61689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::getMarkerPosition(uint32_t *marker)
61789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
61889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (marker == 0) return BAD_VALUE;
61989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
62089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    *marker = mMarkerPosition;
62189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
62289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
62389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
62489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
62589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
62689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
62789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mCbf == 0) return INVALID_OPERATION;
62889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
62989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    uint32_t curPosition;
63089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    getPosition(&curPosition);
63189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mNewPosition = curPosition + updatePeriod;
63289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mUpdatePeriod = updatePeriod;
63389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
63489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
63589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
63689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
63789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod)
63889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
63989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (updatePeriod == 0) return BAD_VALUE;
64089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
64189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    *updatePeriod = mUpdatePeriod;
64289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
64389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
64489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
64589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
64689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::setPosition(uint32_t position)
64789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
6481703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
64989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6509a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    if (!stopped_l()) return INVALID_OPERATION;
6519a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten
6529a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    Mutex::Autolock _l(mCblk->lock);
65389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
65489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (position > mCblk->user) return BAD_VALUE;
65589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
65689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mCblk->server = position;
65738ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
658c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
65989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
66089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
66189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
66289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::getPosition(uint32_t *position)
66389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
66489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (position == 0) return BAD_VALUE;
6651703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
666cd07594333cbe8b2c86c6609cce01a74d6cc33f8Jean-Michel Trivi    *position = mFlushed ? 0 : mCblk->server;
66789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
66889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
66989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
67089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
67189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::reload()
67289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
6731703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
6741703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
6759a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    if (!stopped_l()) return INVALID_OPERATION;
676c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
6771703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    flush_l();
67889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
679d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    mCblk->stepUser(mCblk->frameCount);
68089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
68189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
68289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
68389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
684c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurentaudio_io_handle_t AudioTrack::getOutput()
685c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent{
6861703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
6871703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    return getOutput_l();
6881703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent}
6891703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
6901703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// must be called with mLock held
6911703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurentaudio_io_handle_t AudioTrack::getOutput_l()
6921703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent{
693fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    return AudioSystem::getOutput(mStreamType,
6940d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi            mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags);
695c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent}
696c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
697be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurentint AudioTrack::getSessionId()
698be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent{
699be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    return mSessionId;
700be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent}
701be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
702be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurentstatus_t AudioTrack::attachAuxEffect(int effectId)
703be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent{
7043856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("attachAuxEffect(%d)", effectId);
7052beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    status_t status = mAudioTrack->attachAuxEffect(effectId);
7062beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    if (status == NO_ERROR) {
7072beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent        mAuxEffectId = effectId;
7082beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    }
7092beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    return status;
710be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent}
711be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
71289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
71389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
7141703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// must be called with mLock held
7151703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurentstatus_t AudioTrack::createTrack_l(
716fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
71734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        uint32_t sampleRate,
718e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten        audio_format_t format,
7190d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi        uint32_t channelMask,
72034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        int frameCount,
72134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        uint32_t flags,
72234f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        const sp<IMemory>& sharedBuffer,
723d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        audio_io_handle_t output,
724d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        bool enforceFrameCount)
72534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent{
72634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    status_t status;
72734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
72834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (audioFlinger == 0) {
72929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block       ALOGE("Could not get audioflinger");
73034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent       return NO_INIT;
73134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
73234f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
733d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    int afSampleRate;
734d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
735d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        return NO_INIT;
736d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    }
737d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    int afFrameCount;
738d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
739d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        return NO_INIT;
740d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    }
741d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    uint32_t afLatency;
742d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
743d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        return NO_INIT;
744d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    }
745d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent
746d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    mNotificationFramesAct = mNotificationFramesReq;
747fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (!audio_is_linear_pcm(format)) {
748d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        if (sharedBuffer != 0) {
749d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            frameCount = sharedBuffer->size();
750d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        }
751d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    } else {
752d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        // Ensure that buffer depth covers at least audio hardware latency
753d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
754d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        if (minBufCount < 2) minBufCount = 2;
755d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent
756d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
757d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent
758d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        if (sharedBuffer == 0) {
759d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            if (frameCount == 0) {
760d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                frameCount = minFrameCount;
761d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            }
762d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            if (mNotificationFramesAct == 0) {
763d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                mNotificationFramesAct = frameCount/2;
764d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            }
765d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            // Make sure that application is notified with sufficient margin
766d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            // before underrun
767d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            if (mNotificationFramesAct > (uint32_t)frameCount/2) {
768d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                mNotificationFramesAct = frameCount/2;
769d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            }
770d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            if (frameCount < minFrameCount) {
771d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                if (enforceFrameCount) {
77229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                    ALOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount);
773d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                    return BAD_VALUE;
774d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                } else {
775d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                    frameCount = minFrameCount;
776d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                }
777d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            }
778d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        } else {
779d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            // Ensure that buffer alignment matches channelcount
7800d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi            int channelCount = popcount(channelMask);
781d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
78229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
783d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                return BAD_VALUE;
784d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            }
785d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
786d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        }
787d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    }
788d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent
78934f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
79034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      streamType,
79134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      sampleRate,
79234f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      format,
7930d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi                                                      channelMask,
79434f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      frameCount,
79534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      ((uint16_t)flags) << 16,
79634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      sharedBuffer,
79734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      output,
798be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent                                                      &mSessionId,
79934f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      &status);
80034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
80134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (track == 0) {
80229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("AudioFlinger could not create track, status: %d", status);
80334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        return status;
80434f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
80534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    sp<IMemory> cblk = track->getCblk();
80634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (cblk == 0) {
80729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Could not get control block");
80834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        return NO_INIT;
80934f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
81034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    mAudioTrack = track;
81134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    mCblkMemory = cblk;
81234f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
81338ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
81434f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (sharedBuffer == 0) {
81534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
81634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    } else {
81734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        mCblk->buffers = sharedBuffer->pointer();
81834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent         // Force buffer full condition as data is already present in shared memory
819d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        mCblk->stepUser(mCblk->frameCount);
82034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
82134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
822be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    mCblk->volumeLR = (uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000);
82305632a5fa4b88ca474294887fc92a9fcdf0e2352Glenn Kasten    mCblk->setSendLevel(mSendLevel);
8242beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    mAudioTrack->attachAuxEffect(mAuxEffectId);
8256100d2d60517ff33ed8eb35d0b7ea63cde0831c9Eric Laurent    mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
8266100d2d60517ff33ed8eb35d0b7ea63cde0831c9Eric Laurent    mCblk->waitTimeMs = 0;
827d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    mRemainingFrames = mNotificationFramesAct;
828d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
82934f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    return NO_ERROR;
83034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent}
83134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
83289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
83389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
8341703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
8359a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    bool active;
836d0965dde97f2815ae0a15fe6b40946f8a741a81eGlenn Kasten    status_t result = NO_ERROR;
83789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    audio_track_cblk_t* cblk = mCblk;
83889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    uint32_t framesReq = audioBuffer->frameCount;
8391dd70b9f04961a06fcb73a97fca10a53b3245d3cEric Laurent    uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
84089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
84189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    audioBuffer->frameCount  = 0;
84289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    audioBuffer->size = 0;
84389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
84489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    uint32_t framesAvail = cblk->framesAvailable();
84589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
8469b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent    cblk->lock.lock();
8479b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent    if (cblk->flags & CBLK_INVALID_MSK) {
8489b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent        goto create_new_track;
8499b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent    }
8509b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent    cblk->lock.unlock();
8519b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent
85289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (framesAvail == 0) {
85334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        cblk->lock.lock();
85489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        goto start_loop_here;
85589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        while (framesAvail == 0) {
85689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            active = mActive;
857f6b1678f8f508b447155a81b44e214475ab634a8Glenn Kasten            if (CC_UNLIKELY(!active)) {
8583856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("Not active and NO_MORE_BUFFERS");
85934f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                cblk->lock.unlock();
86089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                return NO_MORE_BUFFERS;
86189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
862f6b1678f8f508b447155a81b44e214475ab634a8Glenn Kasten            if (CC_UNLIKELY(!waitCount)) {
86334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                cblk->lock.unlock();
86489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                return WOULD_BLOCK;
86534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent            }
866d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            if (!(cblk->flags & CBLK_INVALID_MSK)) {
8671703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                mLock.unlock();
868d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
8691703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                cblk->lock.unlock();
8701703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                mLock.lock();
8719a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten                if (!mActive) {
8721703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                    return status_t(STOPPED);
8731703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                }
8741703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                cblk->lock.lock();
875d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            }
8761703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
877d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            if (cblk->flags & CBLK_INVALID_MSK) {
878d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent                goto create_new_track;
879d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent            }
880f6b1678f8f508b447155a81b44e214475ab634a8Glenn Kasten            if (CC_UNLIKELY(result != NO_ERROR)) {
8811dd70b9f04961a06fcb73a97fca10a53b3245d3cEric Laurent                cblk->waitTimeMs += waitTimeMs;
88289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
88389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    // timing out when a loop has been set and we have already written upto loop end
88489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    // is a normal condition: no need to wake AudioFlinger up.
88589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    if (cblk->user < cblk->loopEnd) {
8865ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block                        ALOGW(   "obtainBuffer timed out (is the CPU pegged?) %p "
88789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                "user=%08x, server=%08x", this, cblk->user, cblk->server);
888c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent                        //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
88989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                        cblk->lock.unlock();
89034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                        result = mAudioTrack->start();
8911703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                        cblk->lock.lock();
89234f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                        if (result == DEAD_OBJECT) {
89338ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent                            android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
894d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurentcreate_new_track:
8951703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                            result = restoreTrack_l(cblk, false);
8961703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                        }
8971703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                        if (result != NO_ERROR) {
8985ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block                            ALOGW("obtainBuffer create Track error %d", result);
8991703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                            cblk->lock.unlock();
9001703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                            return result;
90134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                        }
90289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    }
90389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    cblk->waitTimeMs = 0;
90489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                }
905c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
90689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                if (--waitCount == 0) {
90734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                    cblk->lock.unlock();
90889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    return TIMED_OUT;
90989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                }
91089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
91189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            // read the server count again
91289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        start_loop_here:
91389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            framesAvail = cblk->framesAvailable_l();
91489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
91534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        cblk->lock.unlock();
91689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
91789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
91844d9848d6656777a18019223e0d35f2fcc67719aEric Laurent    // restart track if it was disabled by audioflinger due to previous underrun
9191703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
92038ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
9215ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("obtainBuffer() track %p disabled, restarting", this);
92238ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        mAudioTrack->start();
92344d9848d6656777a18019223e0d35f2fcc67719aEric Laurent    }
92444d9848d6656777a18019223e0d35f2fcc67719aEric Laurent
92589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    cblk->waitTimeMs = 0;
926c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
92789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (framesReq > framesAvail) {
92889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        framesReq = framesAvail;
92989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
93089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
93189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    uint32_t u = cblk->user;
93289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
93389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
93489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (u + framesReq > bufferEnd) {
93589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        framesReq = bufferEnd - u;
93689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
93789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
938c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
939c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    audioBuffer->channelCount = mChannelCount;
940c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    audioBuffer->frameCount = framesReq;
941c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    audioBuffer->size = framesReq * cblk->frameSize;
942fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (audio_is_linear_pcm(mFormat)) {
943fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
944c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    } else {
945c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        audioBuffer->format = mFormat;
946c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
947c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    audioBuffer->raw = (int8_t *)cblk->buffer(u);
94889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    active = mActive;
94989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return active ? status_t(NO_ERROR) : status_t(STOPPED);
95089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
95189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
95289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::releaseBuffer(Buffer* audioBuffer)
95389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
9541703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
9551703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    mCblk->stepUser(audioBuffer->frameCount);
95689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
95789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
95889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
95989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
96089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectssize_t AudioTrack::write(const void* buffer, size_t userSize)
96189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
96289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
96389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mSharedBuffer != 0) return INVALID_OPERATION;
96489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
96589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (ssize_t(userSize) < 0) {
96689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // sanity-check. user is most-likely passing an error code.
96729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
96889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                buffer, userSize, userSize);
96989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
97089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
97189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9723856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
97389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9741703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
9751703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    // while we are accessing the cblk
9761703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    mLock.lock();
9771703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    sp <IAudioTrack> audioTrack = mAudioTrack;
9781703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    sp <IMemory> iMem = mCblkMemory;
9791703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    mLock.unlock();
9801703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
98189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    ssize_t written = 0;
98289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const int8_t *src = (const int8_t *)buffer;
98389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Buffer audioBuffer;
984b9980659501d0428d65d8292f3c32da69d37fbd2Glenn Kasten    size_t frameSz = frameSize();
98589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
98689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    do {
98733797ea64d067dfeaacbfd7ebe7f3383b73961b5Eric Laurent        audioBuffer.frameCount = userSize/frameSz;
988c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
98989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Calling obtainBuffer() with a negative wait count causes
99089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // an (almost) infinite wait time.
99189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        status_t err = obtainBuffer(&audioBuffer, -1);
99289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (err < 0) {
99389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            // out of buffers, return #bytes written
99489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            if (err == status_t(NO_MORE_BUFFERS))
99589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                break;
99689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            return ssize_t(err);
99789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
99889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
99989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        size_t toWrite;
1000c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
1001fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
100289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            // Divide capacity by 2 to take expansion into account
100389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            toWrite = audioBuffer.size>>1;
1004511754b5839fd9b09fc56b89ae007fbc39084a33Glenn Kasten            memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
10053302526f6276911b2dc40c731ea7fa0e7972d908Eric Laurent        } else {
100689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            toWrite = audioBuffer.size;
100789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            memcpy(audioBuffer.i8, src, toWrite);
100889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            src += toWrite;
100989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
101089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        userSize -= toWrite;
101189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        written += toWrite;
101289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
101389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        releaseBuffer(&audioBuffer);
101433797ea64d067dfeaacbfd7ebe7f3383b73961b5Eric Laurent    } while (userSize >= frameSz);
101589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
101689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return written;
101789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
101889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
101989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
102089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
102189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectbool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
102289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
102389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Buffer audioBuffer;
102489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    uint32_t frames;
102589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    size_t writtenSize;
102689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
10271703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    mLock.lock();
10281703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
10291703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    // while we are accessing the cblk
10301703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    sp <IAudioTrack> audioTrack = mAudioTrack;
10311703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    sp <IMemory> iMem = mCblkMemory;
10321703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    audio_track_cblk_t* cblk = mCblk;
10339a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    bool active = mActive;
10341703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    mLock.unlock();
10351703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
103689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // Manage underrun callback
10379a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    if (active && (cblk->framesAvailable() == cblk->frameCount)) {
10383856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
103938ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
104089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mCbf(EVENT_UNDERRUN, mUserData, 0);
10411703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            if (cblk->server == cblk->frameCount) {
1042c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent                mCbf(EVENT_BUFFER_END, mUserData, 0);
104389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
104489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            if (mSharedBuffer != 0) return false;
104589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
104689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
1047c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
104889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // Manage loop end callback
10491703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    while (mLoopCount > cblk->loopCount) {
105089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        int loopCount = -1;
105189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mLoopCount--;
105289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (mLoopCount >= 0) loopCount = mLoopCount;
105389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
105489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
105589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
105689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
105789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // Manage marker callback
10582c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    if (!mMarkerReached && (mMarkerPosition > 0)) {
10591703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        if (cblk->server >= mMarkerPosition) {
106089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
10612c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi            mMarkerReached = true;
106289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
106389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
106489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
106589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // Manage new position callback
1066c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    if (mUpdatePeriod > 0) {
10671703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        while (cblk->server >= mNewPosition) {
106889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
106989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mNewPosition += mUpdatePeriod;
107089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
107189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
107289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
107389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // If Shared buffer is used, no data is requested from client.
107489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mSharedBuffer != 0) {
107589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        frames = 0;
107689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    } else {
107789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        frames = mRemainingFrames;
107889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
107989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
10802267ba18d0d2b2d4bd7f5411821ad89ac2659a88Eric Laurent    int32_t waitCount = -1;
10812267ba18d0d2b2d4bd7f5411821ad89ac2659a88Eric Laurent    if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
10822267ba18d0d2b2d4bd7f5411821ad89ac2659a88Eric Laurent        waitCount = 1;
10832267ba18d0d2b2d4bd7f5411821ad89ac2659a88Eric Laurent    }
10842267ba18d0d2b2d4bd7f5411821ad89ac2659a88Eric Laurent
108589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    do {
108689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
108789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        audioBuffer.frameCount = frames;
1088c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
1089c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        // Calling obtainBuffer() with a wait count of 1
1090c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        // limits wait time to WAIT_PERIOD_MS. This prevents from being
1091c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        // stuck here not being able to handle timed events (position, markers, loops).
10922267ba18d0d2b2d4bd7f5411821ad89ac2659a88Eric Laurent        status_t err = obtainBuffer(&audioBuffer, waitCount);
109389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (err < NO_ERROR) {
109489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            if (err != TIMED_OUT) {
109529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up.");
109689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                return false;
109789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
109889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            break;
109989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
110089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (err == status_t(STOPPED)) return false;
110189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
110289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Divide buffer size by 2 to take into account the expansion
110389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // due to 8 to 16 bit conversion: the callback must fill only half
110489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // of the destination buffer
1105fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
110689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            audioBuffer.size >>= 1;
110789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
110889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
110989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        size_t reqSize = audioBuffer.size;
111089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
111189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        writtenSize = audioBuffer.size;
111289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
111389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Sanity check on returned size
11148555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project        if (ssize_t(writtenSize) <= 0) {
11158555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            // The callback is done filling buffers
11168555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            // Keep this thread going to handle timed events and
11178555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            // still try to get more data in intervals of WAIT_PERIOD_MS
11188555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            // but don't just loop and block the CPU, so wait
11198555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            usleep(WAIT_PERIOD_MS*1000);
11208555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            break;
11218555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project        }
112289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (writtenSize > reqSize) writtenSize = reqSize;
112389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1124fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
1125511754b5839fd9b09fc56b89ae007fbc39084a33Glenn Kasten            // 8 to 16 bit conversion, note that source and destination are the same address
1126511754b5839fd9b09fc56b89ae007fbc39084a33Glenn Kasten            memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
112789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            writtenSize <<= 1;
112889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
112989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
113089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        audioBuffer.size = writtenSize;
1131c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
1132b9980659501d0428d65d8292f3c32da69d37fbd2Glenn Kasten        // 8 bit PCM data: in this case,  mCblk->frameSize is based on a sample size of
1133c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        // 16 bit.
1134c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1135c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
113689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        frames -= audioBuffer.frameCount;
113789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
113889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        releaseBuffer(&audioBuffer);
113989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
114089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    while (frames);
114189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
114289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (frames == 0) {
1143d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        mRemainingFrames = mNotificationFramesAct;
114489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    } else {
114589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mRemainingFrames = frames;
114689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
114789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return true;
114889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
114989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
11501703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// must be called with mLock and cblk.lock held. Callers must also hold strong references on
11511703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// the IAudioTrack and IMemory in case they are recreated here.
11521703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// If the IAudioTrack is successfully restored, the cblk pointer is updated
11531703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurentstatus_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
11541703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent{
11551703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    status_t result;
11561703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
115738ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
11585ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
1159cfe2ba6b01a258e39f9c215ffc7b750e0b68f708Eric Laurent             fromStart ? "start()" : "obtainBuffer()", gettid());
11601703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
11611703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        // signal old cblk condition so that other threads waiting for available buffers stop
11621703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        // waiting now
11631703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        cblk->cv.broadcast();
11641703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        cblk->lock.unlock();
11651703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
11669f6530f53ae9eda43f4e7c1cb30d2379db00aa00Eric Laurent        // refresh the audio configuration cache in this process to make sure we get new
11679f6530f53ae9eda43f4e7c1cb30d2379db00aa00Eric Laurent        // output parameters in getOutput_l() and createTrack_l()
11689f6530f53ae9eda43f4e7c1cb30d2379db00aa00Eric Laurent        AudioSystem::clearAudioConfigCache();
11699f6530f53ae9eda43f4e7c1cb30d2379db00aa00Eric Laurent
11701703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        // if the new IAudioTrack is created, createTrack_l() will modify the
11711703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        // following member variables: mAudioTrack, mCblkMemory and mCblk.
11721703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        // It will also delete the strong references on previous IAudioTrack and IMemory
11731703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        result = createTrack_l(mStreamType,
11741703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                               cblk->sampleRate,
11751703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                               mFormat,
11760d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi                               mChannelMask,
11771703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                               mFrameCount,
11781703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                               mFlags,
11791703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                               mSharedBuffer,
11801703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                               getOutput_l(),
11811703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                               false);
11821703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
11831703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        if (result == NO_ERROR) {
1184408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent            uint32_t user = cblk->user;
1185408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent            uint32_t server = cblk->server;
11869b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent            // restore write index and set other indexes to reflect empty buffer status
1187408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent            mCblk->user = user;
1188408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent            mCblk->server = user;
1189408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent            mCblk->userBase = user;
1190408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent            mCblk->serverBase = user;
11919b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent            // restore loop: this is not guaranteed to succeed if new frame count is not
11929b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent            // compatible with loop length
11939b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent            setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
11941703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            if (!fromStart) {
11951703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1196408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                // Make sure that a client relying on callback events indicating underrun or
1197408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                // the actual amount of audio frames played (e.g SoundPool) receives them.
1198408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                if (mSharedBuffer == 0) {
1199408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                    uint32_t frames = 0;
1200408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                    if (user > server) {
1201408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                        frames = ((user - server) > mCblk->frameCount) ?
1202408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                                mCblk->frameCount : (user - server);
1203408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                        memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1204408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                    }
1205408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                    // restart playback even if buffer is not completely filled.
1206408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                    android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1207408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                    // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1208408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                    // the client
1209408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                    mCblk->stepUser(frames);
1210408b8dc3c0a364c6f6b4991d15da9e6bcc2b8008Eric Laurent                }
12111703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            }
12129b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent            if (mActive) {
12139b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent                result = mAudioTrack->start();
12145ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block                ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
12159b7d950f1f3b0c526712b713dbceb0e22762c015Eric Laurent            }
12161703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            if (fromStart && result == NO_ERROR) {
12171703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent                mNewPosition = mCblk->server + mUpdatePeriod;
12181703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            }
12191703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        }
12201703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        if (result != NO_ERROR) {
1221cfe2ba6b01a258e39f9c215ffc7b750e0b68f708Eric Laurent            android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
12225ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block            ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
12231703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        }
1224cfe2ba6b01a258e39f9c215ffc7b750e0b68f708Eric Laurent        mRestoreStatus = result;
12251703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        // signal old cblk condition for other threads waiting for restore completion
122638ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
12271703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        cblk->cv.broadcast();
12281703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    } else {
12291703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        if (!(cblk->flags & CBLK_RESTORED_MSK)) {
12305ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block            ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
12311703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            mLock.unlock();
12321703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
1233cfe2ba6b01a258e39f9c215ffc7b750e0b68f708Eric Laurent            if (result == NO_ERROR) {
1234cfe2ba6b01a258e39f9c215ffc7b750e0b68f708Eric Laurent                result = mRestoreStatus;
1235cfe2ba6b01a258e39f9c215ffc7b750e0b68f708Eric Laurent            }
12361703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            cblk->lock.unlock();
12371703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            mLock.lock();
12381703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        } else {
12395ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block            ALOGW("dead IAudioTrack, already restored TID %d", gettid());
1240cfe2ba6b01a258e39f9c215ffc7b750e0b68f708Eric Laurent            result = mRestoreStatus;
12411703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            cblk->lock.unlock();
12421703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        }
12431703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    }
12443856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
12451703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent         result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
12461703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
12471703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    if (result == NO_ERROR) {
12481703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        // from now on we switch to the newly created cblk
12491703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        cblk = mCblk;
12501703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    }
12511703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    cblk->lock.lock();
12521703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
12535ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block    ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
12541703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
12551703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    return result;
12561703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent}
12571703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
125889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::dump(int fd, const Vector<String16>& args) const
125989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
126089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
126189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const size_t SIZE = 256;
126289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    char buffer[SIZE];
126389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    String8 result;
126489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
126589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(" AudioTrack::dump\n");
126689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    snprintf(buffer, 255, "  stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
126789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
1268d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    snprintf(buffer, 255, "  format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount);
126989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
1270573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurent    snprintf(buffer, 255, "  sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
127189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
127289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    snprintf(buffer, 255, "  active(%d), latency (%d)\n", mActive, mLatency);
127389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
127489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    ::write(fd, result.string(), result.size());
127589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
127689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
127789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
127889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// =========================================================================
127989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
128089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
128189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    : Thread(bCanCallJava), mReceiver(receiver)
128289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
128389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
128489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
128589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectbool AudioTrack::AudioTrackThread::threadLoop()
128689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
128789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return mReceiver.processAudioBuffer(this);
128889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
128989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
129089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::AudioTrackThread::readyToRun()
129189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
129289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
129389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
129489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
129589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::AudioTrackThread::onFirstRef()
129689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
129789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
129889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
129989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// =========================================================================
130089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
130138ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent
130289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectaudio_track_cblk_t::audio_track_cblk_t()
130354b1a0550411c2fd2084d82d28934d505c37349aMathias Agopian    : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
130454b1a0550411c2fd2084d82d28934d505c37349aMathias Agopian    userBase(0), serverBase(0), buffers(0), frameCount(0),
130554b1a0550411c2fd2084d82d28934d505c37349aMathias Agopian    loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0),
130605632a5fa4b88ca474294887fc92a9fcdf0e2352Glenn Kasten    mSendLevel(0), flags(0)
130789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
130889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
130989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
131089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectuint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
131189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
13129054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    uint32_t u = user;
131389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
131489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    u += frameCount;
131589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // Ensure that user is never ahead of server for AudioRecord
1316d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    if (flags & CBLK_DIRECTION_MSK) {
131789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
131889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
131989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
132089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
13219054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    } else if (u > server) {
13225ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("stepServer occurred after track reset");
13239054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten        u = server;
132489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
132589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
132689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (u >= userBase + this->frameCount) {
132789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        userBase += this->frameCount;
132889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
132989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
13309054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    user = u;
133189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
133289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // Clear flow control error condition as new data has been written/read to/from buffer.
133333797ea64d067dfeaacbfd7ebe7f3383b73961b5Eric Laurent    if (flags & CBLK_UNDERRUN_MSK) {
133438ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
133533797ea64d067dfeaacbfd7ebe7f3383b73961b5Eric Laurent    }
133689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
133789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return u;
133889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
133989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
134089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectbool audio_track_cblk_t::stepServer(uint32_t frameCount)
134189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
134238ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    if (!tryLock()) {
13435ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("stepServer() could not lock cblk");
134489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return false;
134589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
134689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
13479054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    uint32_t s = server;
134889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
134989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    s += frameCount;
1350d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    if (flags & CBLK_DIRECTION_MSK) {
135189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Mark that we have read the first buffer so that next time stepUser() is called
135289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // we switch to normal obtainBuffer() timeout period
135389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
135434f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent            bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
1355c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        }
135689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // It is possible that we receive a flush()
135789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // while the mixer is processing a block: in this case,
135889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // stepServer() is called After the flush() has reset u & s and
135989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // we have s > u
13609054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten        if (s > user) {
13615ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block            ALOGW("stepServer occurred after track reset");
13629054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten            s = user;
136389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
136489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
136589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
136689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (s >= loopEnd) {
13675ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
136889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        s = loopStart;
136989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (--loopCount == 0) {
137089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            loopEnd = UINT_MAX;
137189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            loopStart = UINT_MAX;
137289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
137389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
137489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (s >= serverBase + this->frameCount) {
137589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        serverBase += this->frameCount;
137689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
137789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
13789054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    server = s;
137989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
13801703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    if (!(flags & CBLK_INVALID_MSK)) {
13811703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        cv.signal();
13821703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    }
138389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    lock.unlock();
138489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return true;
138589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
138689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
138789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid* audio_track_cblk_t::buffer(uint32_t offset) const
138889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
13899054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    return (int8_t *)buffers + (offset - userBase) * frameSize;
139089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
139189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
139289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectuint32_t audio_track_cblk_t::framesAvailable()
139389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
139489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Mutex::Autolock _l(lock);
139589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return framesAvailable_l();
139689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
139789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
139889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectuint32_t audio_track_cblk_t::framesAvailable_l()
139989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
14009054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    uint32_t u = user;
14019054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    uint32_t s = server;
140289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1403d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    if (flags & CBLK_DIRECTION_MSK) {
140489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t limit = (s < loopStart) ? s : loopStart;
140589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return limit + frameCount - u;
140689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    } else {
140789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return frameCount + u - s;
140889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
140989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
141089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
141189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectuint32_t audio_track_cblk_t::framesReady()
141289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
14139054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    uint32_t u = user;
14149054897ab9ffb307fadae81b774a1fc61cb542e7Glenn Kasten    uint32_t s = server;
141589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1416d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    if (flags & CBLK_DIRECTION_MSK) {
141789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (u < loopEnd) {
141889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            return u - s;
141989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        } else {
142038ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent            // do not block on mutex shared with client on AudioFlinger side
142138ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent            if (!tryLock()) {
14225ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block                ALOGW("framesReady() could not lock cblk");
142338ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent                return 0;
142438ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent            }
142538ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent            uint32_t frames = UINT_MAX;
142689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            if (loopCount >= 0) {
142738ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent                frames = (loopEnd - loopStart)*loopCount + u - s;
142889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
142938ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent            lock.unlock();
143038ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent            return frames;
143189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
143289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    } else {
143389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return s - u;
143489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
143589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
143689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
143738ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurentbool audio_track_cblk_t::tryLock()
143838ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent{
143938ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    // the code below simulates lock-with-timeout
144038ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    // we MUST do this to protect the AudioFlinger server
144138ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    // as this lock is shared with the client.
144238ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    status_t err;
144338ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent
144438ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    err = lock.tryLock();
144538ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    if (err == -EBUSY) { // just wait a bit
144638ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        usleep(1000);
144738ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        err = lock.tryLock();
144838ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    }
144938ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    if (err != NO_ERROR) {
145038ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        // probably, the client just died.
145138ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent        return false;
145238ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    }
145338ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent    return true;
145438ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent}
145538ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent
145689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
145789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
145889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}; // namespace android
1459