AudioTrack.cpp revision 818e7a32ce3633980138aff2c2bfcc5158b3cfcc
199e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten/*
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//#define LOG_NDEBUG 0
1989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#define LOG_TAG "AudioTrack"
2089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2134fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn#include <inttypes.h>
22c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten#include <math.h>
2389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <sys/resource.h>
2434fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn
259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#include <audio_utils/primitives.h>
269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#include <binder/IPCThreadState.h>
2789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/AudioTrack.h>
2889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <utils/Log.h>
299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#include <private/media/AudioTrackShared.h>
301ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/IAudioFlinger.h>
31e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent#include <media/AudioPolicyHelper.h>
32cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung#include <media/AudioResamplerPublic.h>
33fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
34b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald#define WAIT_PERIOD_MS                  10
35b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald#define WAIT_STREAM_END_TIMEOUT_SEC     120
3653c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hungstatic const int kMaxLoopCountNotifications = 32;
37511754b5839fd9b09fc56b89ae007fbc39084a33Glenn Kasten
3889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectnamespace android {
3933005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh// ---------------------------------------------------------------------------
4033005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh
41a7f03353d5f172016f324e2a01f301cca6794152Andy Hung// TODO: Move to a separate .h
42a7f03353d5f172016f324e2a01f301cca6794152Andy Hung
434ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hungtemplate <typename T>
44a7f03353d5f172016f324e2a01f301cca6794152Andy Hungstatic inline const T &min(const T &x, const T &y) {
454ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    return x < y ? x : y;
464ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung}
474ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung
48a7f03353d5f172016f324e2a01f301cca6794152Andy Hungtemplate <typename T>
49a7f03353d5f172016f324e2a01f301cca6794152Andy Hungstatic inline const T &max(const T &x, const T &y) {
50a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    return x > y ? x : y;
51a7f03353d5f172016f324e2a01f301cca6794152Andy Hung}
52a7f03353d5f172016f324e2a01f301cca6794152Andy Hung
53a7f03353d5f172016f324e2a01f301cca6794152Andy Hungstatic inline nsecs_t framesToNanoseconds(ssize_t frames, uint32_t sampleRate, float speed)
54a7f03353d5f172016f324e2a01f301cca6794152Andy Hung{
55a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    return ((double)frames * 1000000000) / ((double)sampleRate * speed);
56a7f03353d5f172016f324e2a01f301cca6794152Andy Hung}
57a7f03353d5f172016f324e2a01f301cca6794152Andy Hung
587f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hungstatic int64_t convertTimespecToUs(const struct timespec &tv)
597f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung{
607f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    return tv.tv_sec * 1000000ll + tv.tv_nsec / 1000;
617f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung}
627f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
637f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung// current monotonic time in microseconds.
647f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hungstatic int64_t getNowUs()
657f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung{
667f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    struct timespec tv;
677f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    (void) clock_gettime(CLOCK_MONOTONIC, &tv);
687f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    return convertTimespecToUs(tv);
697f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung}
707f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
7126145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung// FIXME: we don't use the pitch setting in the time stretcher (not working);
7226145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung// instead we emulate it using our sample rate converter.
7326145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hungstatic const bool kFixPitch = true; // enable pitch fix
7426145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hungstatic inline uint32_t adjustSampleRate(uint32_t sampleRate, float pitch)
7526145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung{
7626145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    return kFixPitch ? (sampleRate * pitch + 0.5) : sampleRate;
7726145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung}
7826145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung
7926145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hungstatic inline float adjustSpeed(float speed, float pitch)
8026145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung{
816c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    return kFixPitch ? speed / max(pitch, AUDIO_TIMESTRETCH_PITCH_MIN_DELTA) : speed;
8226145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung}
8326145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung
8426145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hungstatic inline float adjustPitch(float pitch)
8526145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung{
8626145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    return kFixPitch ? AUDIO_TIMESTRETCH_PITCH_NORMAL : pitch;
8726145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung}
8826145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung
898edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung// Must match similar computation in createTrack_l in Threads.cpp.
908edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung// TODO: Move to a common library
918edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hungstatic size_t calculateMinFrameCount(
928edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
938edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        uint32_t sampleRate, float speed)
948edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung{
958edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    // Ensure that buffer depth covers at least audio hardware latency
968edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
978edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    if (minBufCount < 2) {
988edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        minBufCount = 2;
998edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    }
1008edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    ALOGV("calculateMinFrameCount afLatency %u  afFrameCount %u  afSampleRate %u  "
1018edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            "sampleRate %u  speed %f  minBufCount: %u",
1028edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount);
1038edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    return minBufCount * sourceFramesNeededWithTimestretch(
1048edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            sampleRate, afFrameCount, afSampleRate, speed);
1058edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung}
1068edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung
10733005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh// static
10833005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yehstatus_t AudioTrack::getMinFrameCount(
109e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten        size_t* frameCount,
110fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
11133005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh        uint32_t sampleRate)
11233005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh{
113d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    if (frameCount == NULL) {
114d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return BAD_VALUE;
115d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
11604cd0186305e2b59d23c9147787046c6662029ccGlenn Kasten
1170e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // FIXME handle in server, like createTrack_l(), possible missing info:
118e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten    //          audio_io_handle_t output
119e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten    //          audio_format_t format
120e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten    //          audio_channel_mask_t channelMask
1210e48d25606c82def035ad10a5b3923767a765cddAndy Hung    //          audio_output_flags_t flags (FAST)
1223b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten    uint32_t afSampleRate;
12366a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten    status_t status;
12466a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten    status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);
12566a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten    if (status != NO_ERROR) {
12670c0bfbe5ec88dcc3efa2bd8df26f36cff1cf03aGlenn Kasten        ALOGE("Unable to query output sample rate for stream type %d; status %d",
12770c0bfbe5ec88dcc3efa2bd8df26f36cff1cf03aGlenn Kasten                streamType, status);
12866a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten        return status;
12933005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    }
130e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten    size_t afFrameCount;
13166a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten    status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
13266a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten    if (status != NO_ERROR) {
13370c0bfbe5ec88dcc3efa2bd8df26f36cff1cf03aGlenn Kasten        ALOGE("Unable to query output frame count for stream type %d; status %d",
13470c0bfbe5ec88dcc3efa2bd8df26f36cff1cf03aGlenn Kasten                streamType, status);
13566a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten        return status;
13633005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    }
13733005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    uint32_t afLatency;
13866a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten    status = AudioSystem::getOutputLatency(&afLatency, streamType);
13966a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten    if (status != NO_ERROR) {
14070c0bfbe5ec88dcc3efa2bd8df26f36cff1cf03aGlenn Kasten        ALOGE("Unable to query output latency for stream type %d; status %d",
14170c0bfbe5ec88dcc3efa2bd8df26f36cff1cf03aGlenn Kasten                streamType, status);
14266a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten        return status;
14333005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    }
14433005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh
1458edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    // When called from createTrack, speed is 1.0f (normal speed).
1468edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    // This is rechecked again on setting playback rate (TODO: on setting sample rate, too).
1478edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    *frameCount = calculateMinFrameCount(afLatency, afFrameCount, afSampleRate, sampleRate, 1.0f);
14833005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh
1490e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // The formula above should always produce a non-zero value under normal circumstances:
1500e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // AudioTrack.SAMPLE_RATE_HZ_MIN <= sampleRate <= AudioTrack.SAMPLE_RATE_HZ_MAX.
1510e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // Return error in the unlikely event that it does not, as that's part of the API contract.
15266a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten    if (*frameCount == 0) {
1530e48d25606c82def035ad10a5b3923767a765cddAndy Hung        ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %u",
15466a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten                streamType, sampleRate);
15566a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten        return BAD_VALUE;
15666a0467fdddada4caabd0f0a999fbb367fea7beeGlenn Kasten    }
1578edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    ALOGV("getMinFrameCount=%zu: afFrameCount=%zu, afSampleRate=%u, afLatency=%u",
1588edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            *frameCount, afFrameCount, afSampleRate, afLatency);
15933005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh    return NO_ERROR;
16033005a932c60a0780fe9b7307d5988df3d9f6c26Chia-chi Yeh}
16189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
16289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// ---------------------------------------------------------------------------
16389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
16489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioTrack::AudioTrack()
165879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten    : mStatus(NO_INIT),
1664ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPreviousPriority(ANDROID_PRIORITY_NORMAL),
1677064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George      mPreviousSchedulingGroup(SP_DEFAULT),
168aa9811945f575614b3482d09e4d969792701cebbPaul McLean      mPausedPosition(0),
169aa9811945f575614b3482d09e4d969792701cebbPaul McLean      mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE)
17089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
171faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi    mAttributes.content_type = AUDIO_CONTENT_TYPE_UNKNOWN;
172faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi    mAttributes.usage = AUDIO_USAGE_UNKNOWN;
173faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi    mAttributes.flags = 0x0;
174faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi    strcpy(mAttributes.tags, "");
17589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
17689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
17789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioTrack::AudioTrack(
178fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
17989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t sampleRate,
180e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten        audio_format_t format,
18128b76b334f92a15a2be3cc9e2f7d229a3275d1acGlenn Kasten        audio_channel_mask_t channelMask,
182bce50bfc3846ab008bafa75c5d3f29fd7b5395f7Glenn Kasten        size_t frameCount,
1830ca3cf94c0dfc173ad7886ae162c4b67067539f6Eric Laurent        audio_output_flags_t flags,
18489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        callback_t cbf,
18589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        void* user,
186838b3d8bafa4a781e277870dee4e0390165cff52Glenn Kasten        uint32_t notificationFrames,
1879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        int sessionId,
188ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald        transfer_type transferType,
189462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        const audio_offload_info_t *offloadInfo,
190d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        int uid,
191d9d7fa0873796ac661c44a7fcd6ad5ff697ff01fJean-Michel Trivi        pid_t pid,
192faeb0f291330134dc4468359a36e099aae508449Ronghua Wu        const audio_attributes_t* pAttributes,
193faeb0f291330134dc4468359a36e099aae508449Ronghua Wu        bool doNotReconnect)
194879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten    : mStatus(NO_INIT),
1954ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPreviousPriority(ANDROID_PRIORITY_NORMAL),
1967064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George      mPreviousSchedulingGroup(SP_DEFAULT),
197aa9811945f575614b3482d09e4d969792701cebbPaul McLean      mPausedPosition(0),
198aa9811945f575614b3482d09e4d969792701cebbPaul McLean      mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE)
19989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
2000d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi    mStatus = set(streamType, sampleRate, format, channelMask,
201a514bdb58b5de4986679f72b7204b4764f7a2778Eric Laurent            frameCount, flags, cbf, user, notificationFrames,
202462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
203faeb0f291330134dc4468359a36e099aae508449Ronghua Wu            offloadInfo, uid, pid, pAttributes, doNotReconnect);
20489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
20589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
206c813985abd8ba61e999b3505f6a332574f87a1beAndreas HuberAudioTrack::AudioTrack(
207fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
20889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t sampleRate,
209e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten        audio_format_t format,
21028b76b334f92a15a2be3cc9e2f7d229a3275d1acGlenn Kasten        audio_channel_mask_t channelMask,
21189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        const sp<IMemory>& sharedBuffer,
2120ca3cf94c0dfc173ad7886ae162c4b67067539f6Eric Laurent        audio_output_flags_t flags,
21389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        callback_t cbf,
21489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        void* user,
215838b3d8bafa4a781e277870dee4e0390165cff52Glenn Kasten        uint32_t notificationFrames,
2169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        int sessionId,
217ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald        transfer_type transferType,
218462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        const audio_offload_info_t *offloadInfo,
219d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        int uid,
220d9d7fa0873796ac661c44a7fcd6ad5ff697ff01fJean-Michel Trivi        pid_t pid,
221faeb0f291330134dc4468359a36e099aae508449Ronghua Wu        const audio_attributes_t* pAttributes,
222faeb0f291330134dc4468359a36e099aae508449Ronghua Wu        bool doNotReconnect)
223879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten    : mStatus(NO_INIT),
2244ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPreviousPriority(ANDROID_PRIORITY_NORMAL),
2257064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George      mPreviousSchedulingGroup(SP_DEFAULT),
226aa9811945f575614b3482d09e4d969792701cebbPaul McLean      mPausedPosition(0),
227aa9811945f575614b3482d09e4d969792701cebbPaul McLean      mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE)
22889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
2290d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi    mStatus = set(streamType, sampleRate, format, channelMask,
23017a736c3e1d062d7fc916329eb32aef8935614afGlenn Kasten            0 /*frameCount*/, flags, cbf, user, notificationFrames,
231d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen            sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo,
232faeb0f291330134dc4468359a36e099aae508449Ronghua Wu            uid, pid, pAttributes, doNotReconnect);
23389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
23489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
23589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioTrack::~AudioTrack()
23689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
23789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mStatus == NO_ERROR) {
23889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Make sure that callback function exits in the case where
23989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // it is looping on buffer full condition in obtainBuffer().
24089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Otherwise the callback thread will never exit.
24189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        stop();
24289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (mAudioTrackThread != 0) {
243b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            mProxy->interrupt();
2443acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten            mAudioTrackThread->requestExit();   // see comment in AudioTrack.h
24589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mAudioTrackThread->requestExitAndWait();
24689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            mAudioTrackThread.clear();
24789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
248296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        // No lock here: worst case we remove a NULL callback which will be a nop
249296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        if (mDeviceCallback != 0 && mOutput != AUDIO_IO_HANDLE_NONE) {
250296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            AudioSystem::removeAudioDeviceCallback(mDeviceCallback, mOutput);
251296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        }
252f888020c6e2735624f2b2a30e72aca24e17b8b4dMarco Nelissen        IInterface::asBinder(mAudioTrack)->unlinkToDeath(mDeathNotifier, this);
25353cec22821072719ee02c856e9ac2dda2496c570Glenn Kasten        mAudioTrack.clear();
2543bcffa136909c1fb6e88ee4efd12ccac18360a85Eric Laurent        mCblkMemory.clear();
2553bcffa136909c1fb6e88ee4efd12ccac18360a85Eric Laurent        mSharedBuffer.clear();
25689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        IPCThreadState::self()->flushCommands();
2574c36d6f48f4eecacc148317f775f65961a4f03d2Glenn Kasten        ALOGV("~AudioTrack, releasing session id %d from %d on behalf of %d",
2584c36d6f48f4eecacc148317f775f65961a4f03d2Glenn Kasten                mSessionId, IPCThreadState::self()->getCallingPid(), mClientPid);
259d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
26089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
26189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
26289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
26389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::set(
264fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
26589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t sampleRate,
266e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten        audio_format_t format,
26728b76b334f92a15a2be3cc9e2f7d229a3275d1acGlenn Kasten        audio_channel_mask_t channelMask,
268bce50bfc3846ab008bafa75c5d3f29fd7b5395f7Glenn Kasten        size_t frameCount,
2690ca3cf94c0dfc173ad7886ae162c4b67067539f6Eric Laurent        audio_output_flags_t flags,
27089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        callback_t cbf,
27189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        void* user,
272838b3d8bafa4a781e277870dee4e0390165cff52Glenn Kasten        uint32_t notificationFrames,
27389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        const sp<IMemory>& sharedBuffer,
274be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        bool threadCanCallJava,
2759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        int sessionId,
276ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald        transfer_type transferType,
277462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        const audio_offload_info_t *offloadInfo,
278d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        int uid,
279faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi        pid_t pid,
280faeb0f291330134dc4468359a36e099aae508449Ronghua Wu        const audio_attributes_t* pAttributes,
281faeb0f291330134dc4468359a36e099aae508449Ronghua Wu        bool doNotReconnect)
28289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
283bce50bfc3846ab008bafa75c5d3f29fd7b5395f7Glenn Kasten    ALOGV("set(): streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, "
2844c36d6f48f4eecacc148317f775f65961a4f03d2Glenn Kasten          "flags #%x, notificationFrames %u, sessionId %d, transferType %d, uid %d, pid %d",
285bce50bfc3846ab008bafa75c5d3f29fd7b5395f7Glenn Kasten          streamType, sampleRate, format, channelMask, frameCount, flags, notificationFrames,
2864c36d6f48f4eecacc148317f775f65961a4f03d2Glenn Kasten          sessionId, transferType, uid, pid);
28786f04663032ddaa25110149d709bbf896ad83b02Glenn Kasten
28833ff89ba94a527e4293ee5349da01483252d5c83Phil Burk    mThreadCanCallJava = threadCanCallJava;
28933ff89ba94a527e4293ee5349da01483252d5c83Phil Burk
2909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    switch (transferType) {
2919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case TRANSFER_DEFAULT:
2929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (sharedBuffer != 0) {
2939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            transferType = TRANSFER_SHARED;
2949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        } else if (cbf == NULL || threadCanCallJava) {
2959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            transferType = TRANSFER_SYNC;
2969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        } else {
2979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            transferType = TRANSFER_CALLBACK;
2989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
2999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        break;
3009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case TRANSFER_CALLBACK:
3019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (cbf == NULL || sharedBuffer != 0) {
3029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
3039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return BAD_VALUE;
3049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
3059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        break;
3069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case TRANSFER_OBTAIN:
3079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case TRANSFER_SYNC:
3089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (sharedBuffer != 0) {
3099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
3109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return BAD_VALUE;
3119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
3129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        break;
3139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case TRANSFER_SHARED:
3149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (sharedBuffer == 0) {
3159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
3169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return BAD_VALUE;
3179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
3189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        break;
3199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    default:
3209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("Invalid transfer type %d", transferType);
3219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return BAD_VALUE;
3229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
323dd5f4c8c4059f890e81b28b026a688febb4e1dd9Glenn Kasten    mSharedBuffer = sharedBuffer;
3249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mTransfer = transferType;
325faeb0f291330134dc4468359a36e099aae508449Ronghua Wu    mDoNotReconnect = doNotReconnect;
3269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
32797c9f4fd7bc31e2968d816402659ba1c64f10b42Eric Laurent    ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %zu", sharedBuffer->pointer(),
32885ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten            sharedBuffer->size());
32989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
33034fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn    ALOGV("set() streamType %d frameCount %zu flags %04x", streamType, frameCount, flags);
3311a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent
33253cec22821072719ee02c856e9ac2dda2496c570Glenn Kasten    // invariant that mAudioTrack != 0 is true only after set() returns successfully
3331dd70b9f04961a06fcb73a97fca10a53b3245d3cEric Laurent    if (mAudioTrack != 0) {
33429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Track already in use");
33589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return INVALID_OPERATION;
33689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
33789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
33889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // handle default values first.
339e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    if (streamType == AUDIO_STREAM_DEFAULT) {
340fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        streamType = AUDIO_STREAM_MUSIC;
34189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
342faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi    if (pAttributes == NULL) {
343223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        if (uint32_t(streamType) >= AUDIO_STREAM_PUBLIC_CNT) {
344faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi            ALOGE("Invalid stream type %d", streamType);
345faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi            return BAD_VALUE;
346faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi        }
347faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi        mStreamType = streamType;
348e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent
349faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi    } else {
350faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi        // stream type shouldn't be looked at, this track has audio attributes
351faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi        memcpy(&mAttributes, pAttributes, sizeof(audio_attributes_t));
352faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi        ALOGV("Building AudioTrack with attributes: usage=%d content=%d flags=0x%x tags=[%s]",
353faabb51ceef13bf1e3f692219ac410c1cd75d0deJean-Michel Trivi                mAttributes.usage, mAttributes.content_type, mAttributes.flags, mAttributes.tags);
354e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent        mStreamType = AUDIO_STREAM_DEFAULT;
355c6bd5db9d9cf4bba1649b5b7ddea2d23f5de23a9Eric Laurent        if ((mAttributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
356c6bd5db9d9cf4bba1649b5b7ddea2d23f5de23a9Eric Laurent            flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
357c6bd5db9d9cf4bba1649b5b7ddea2d23f5de23a9Eric Laurent        }
35833ff89ba94a527e4293ee5349da01483252d5c83Phil Burk        if ((mAttributes.flags & AUDIO_FLAG_LOW_LATENCY) != 0) {
35933ff89ba94a527e4293ee5349da01483252d5c83Phil Burk            flags = (audio_output_flags_t) (flags | AUDIO_OUTPUT_FLAG_FAST);
36033ff89ba94a527e4293ee5349da01483252d5c83Phil Burk        }
361dd5f4c8c4059f890e81b28b026a688febb4e1dd9Glenn Kasten    }
362ea7939a079b3600cab955760839b021326f8cfc3Glenn Kasten
36389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // these below should probably come from the audioFlinger too...
364e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten    if (format == AUDIO_FORMAT_DEFAULT) {
365fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        format = AUDIO_FORMAT_PCM_16_BIT;
366fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk    } else if (format == AUDIO_FORMAT_IEC61937) { // HDMI pass-through?
367fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk        mAttributes.flags |= AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO;
36889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
36989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
37089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // validate parameters
371fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (!audio_is_valid_format(format)) {
372cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("Invalid format %#x", format);
37389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
37489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
375dd5f4c8c4059f890e81b28b026a688febb4e1dd9Glenn Kasten    mFormat = format;
376c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
3778ba90326d683b035d99e24db669093e4602a7149Glenn Kasten    if (!audio_is_output_channel(channelMask)) {
3788ba90326d683b035d99e24db669093e4602a7149Glenn Kasten        ALOGE("Invalid channel mask %#x", channelMask);
3798ba90326d683b035d99e24db669093e4602a7149Glenn Kasten        return BAD_VALUE;
3808ba90326d683b035d99e24db669093e4602a7149Glenn Kasten    }
381e3247bf8dd4f8fa8dfa3a108260241ae4a967569Glenn Kasten    mChannelMask = channelMask;
382e541269be94f3a1072932d51537905b120ef4733Andy Hung    uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
383e3247bf8dd4f8fa8dfa3a108260241ae4a967569Glenn Kasten    mChannelCount = channelCount;
3848ba90326d683b035d99e24db669093e4602a7149Glenn Kasten
385c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    // force direct flag if format is not linear PCM
386b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    // or offload was requested
387b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
388b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            || !audio_is_linear_pcm(format)) {
389b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
390b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                    ? "Offload request, forcing to Direct Output"
391b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                    : "Not linear PCM, forcing to Direct Output");
3920ca3cf94c0dfc173ad7886ae162c4b67067539f6Eric Laurent        flags = (audio_output_flags_t)
3933acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten                // FIXME why can't we allow direct AND fast?
3940ca3cf94c0dfc173ad7886ae162c4b67067539f6Eric Laurent                ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
395c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
396c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
397d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    // force direct flag if HW A/V sync requested
398d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
399d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent        flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
400d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    }
401d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent
402b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten    if (flags & AUDIO_OUTPUT_FLAG_DIRECT) {
403fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk        if (audio_has_proportional_frames(format)) {
404b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten            mFrameSize = channelCount * audio_bytes_per_sample(format);
405b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten        } else {
406b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten            mFrameSize = sizeof(uint8_t);
407b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten        }
408e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    } else {
409fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk        ALOG_ASSERT(audio_has_proportional_frames(format));
410b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten        mFrameSize = channelCount * audio_bytes_per_sample(format);
411b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten        // createTrack will return an error if PCM format is not supported by server,
412b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten        // so no need to check for specific PCM formats here
413e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    }
414e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
4150d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    // sampling rate must be specified for direct outputs
4160d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    if (sampleRate == 0 && (flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
4170d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent        return BAD_VALUE;
4180d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    }
4190d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    mSampleRate = sampleRate;
4203a474aa67fc31505740526dd249d96204c08bf79Lajos Molnar    mOriginalSampleRate = sampleRate;
4215a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT;
4220d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent
423b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten    // Make copy of input parameter offloadInfo so that in the future:
424b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten    //  (a) createTrack_l doesn't need it as an input parameter
425b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten    //  (b) we can support re-creation of offloaded tracks
426b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten    if (offloadInfo != NULL) {
427b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten        mOffloadInfoCopy = *offloadInfo;
428b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten        mOffloadInfo = &mOffloadInfoCopy;
429b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten    } else {
430b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten        mOffloadInfo = NULL;
431b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten    }
432b5ccb2d99b2af400c70777a6452f090ac14f5a96Glenn Kasten
43366e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten    mVolume[AUDIO_INTERLEAVE_LEFT] = 1.0f;
43466e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten    mVolume[AUDIO_INTERLEAVE_RIGHT] = 1.0f;
43505632a5fa4b88ca474294887fc92a9fcdf0e2352Glenn Kasten    mSendLevel = 0.0f;
436396fabdb6efcdac5aea3d9f559d1beedf6a4cedcGlenn Kasten    // mFrameCount is initialized in createTrack_l
437b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten    mReqFrameCount = frameCount;
438d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    mNotificationFramesReq = notificationFrames;
4399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mNotificationFramesAct = 0;
440caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent    if (sessionId == AUDIO_SESSION_ALLOCATE) {
441caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent        mSessionId = AudioSystem::newAudioUniqueId();
442caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent    } else {
443caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent        mSessionId = sessionId;
444caf7f48a0ef558689d39aafd187c1571ff4128b4Eric Laurent    }
445d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    int callingpid = IPCThreadState::self()->getCallingPid();
446d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    int mypid = getpid();
447d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (uid == -1 || (callingpid != mypid)) {
448462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        mClientUid = IPCThreadState::self()->getCallingUid();
449462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    } else {
450462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        mClientUid = uid;
451462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    }
452d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid == -1 || (callingpid != mypid)) {
453d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        mClientPid = callingpid;
454d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    } else {
455d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        mClientPid = pid;
456d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
4572beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    mAuxEffectId = 0;
458093000f7d11839b920e8dfaa42ed1d09f48e24b8Glenn Kasten    mFlags = flags;
4594a4a0959bca78e03e3c3f486ba17829c28314d8cGlenn Kasten    mCbf = cbf;
460be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
461a997e7a7c5aa2fc7e95316218316f8b7b98786baGlenn Kasten    if (cbf != NULL) {
462896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent        mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
463a997e7a7c5aa2fc7e95316218316f8b7b98786baGlenn Kasten        mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
464bfd318402924414fbde4d31c9d23cc423f10b630Glenn Kasten        // thread begins in paused state, and will not reference us until start()
465a997e7a7c5aa2fc7e95316218316f8b7b98786baGlenn Kasten    }
466a997e7a7c5aa2fc7e95316218316f8b7b98786baGlenn Kasten
46734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    // create the IAudioTrack
4680d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    status_t status = createTrack_l();
469a997e7a7c5aa2fc7e95316218316f8b7b98786baGlenn Kasten
47034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (status != NO_ERROR) {
471a997e7a7c5aa2fc7e95316218316f8b7b98786baGlenn Kasten        if (mAudioTrackThread != 0) {
472b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            mAudioTrackThread->requestExit();   // see comment in AudioTrack.h
473b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            mAudioTrackThread->requestExitAndWait();
474a997e7a7c5aa2fc7e95316218316f8b7b98786baGlenn Kasten            mAudioTrackThread.clear();
475a997e7a7c5aa2fc7e95316218316f8b7b98786baGlenn Kasten        }
47689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return status;
47789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
47834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
47989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mStatus = NO_ERROR;
4809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mState = STATE_STOPPED;
48189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mUserData = user;
4824ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    mLoopCount = 0;
4834ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    mLoopStart = 0;
4844ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    mLoopEnd = 0;
48553c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    mLoopCountNotified = 0;
48689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mMarkerPosition = 0;
4872c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mMarkerReached = false;
48889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mNewPosition = 0;
48989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mUpdatePeriod = 0;
490200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    mPosition = 0;
491200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    mReleased = 0;
4927f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    mStartUs = 0;
493d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
4949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mSequence = 1;
4959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mObservedSequence = mSequence;
4969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mInUnderrun = false;
4971b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk    mPreviousTimestampValid = false;
498c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung    mTimestampStartupGlitchReported = false;
499c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung    mRetrogradeMotionReported = false;
5002812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk    mUnderrunCountOffset = 0;
5019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
50289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
50389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
50489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
50589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
50689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
507b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgeraldstatus_t AudioTrack::start()
50889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
509f5aafb209d01ba2ab6cb55d1a12cfc653e2b4be0Eric Laurent    AutoMutex lock(mLock);
510b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
5119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mState == STATE_ACTIVE) {
512b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        return INVALID_OPERATION;
5139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
5141703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
5159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mInUnderrun = true;
5162b584244930c9de0e3bc46898a801e9ccb731900Eric Laurent
5179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    State previousState = mState;
518b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    if (previousState == STATE_PAUSED_STOPPING) {
519b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        mState = STATE_STOPPING;
520b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    } else {
521b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        mState = STATE_ACTIVE;
522b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
523200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    (void) updateAndGetPosition_l();
5249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
5259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // reset current position as seen by client to 0
526200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        mPosition = 0;
5271b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk        mPreviousTimestampValid = false;
528c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung        mTimestampStartupGlitchReported = false;
529c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung        mRetrogradeMotionReported = false;
5301b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk
5316ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        // If previousState == STATE_STOPPED, we clear the timestamp so that it
5326ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        // needs a new server push. We also reactivate markers (mMarkerPosition != 0)
53361be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung        // as the position is reset to 0. This is legacy behavior. This is not done
53461be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung        // in stop() to avoid a race condition where the last marker event is issued twice.
53561be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung        // Note: the if is technically unnecessary because previousState == STATE_FLUSHED
53661be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung        // is only for streaming tracks, and mMarkerReached is already set to false.
53761be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung        if (previousState == STATE_STOPPED) {
5386ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung            mProxy->clearTimestamp(); // need new server push for valid timestamp
53961be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung            mMarkerReached = false;
54061be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung        }
54161be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung
5427f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        // For offloaded tracks, we don't know if the hardware counters are really zero here,
5437f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        // since the flush is asynchronous and stop may not fully drain.
5447f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        // We save the time when the track is started to later verify whether
5457f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        // the counters are realistic (i.e. start from zero after this time).
5467f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        mStartUs = getNowUs();
5477f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
548ec9a032c2e104ab1e3b5bf73e69dab1408ced0adEric Laurent        // force refresh of remaining frames by processAudioBuffer() as last
549ec9a032c2e104ab1e3b5bf73e69dab1408ced0adEric Laurent        // write before stop could be partial.
550ec9a032c2e104ab1e3b5bf73e69dab1408ced0adEric Laurent        mRefreshRemaining = true;
55189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
552200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    mNewPosition = mPosition + mUpdatePeriod;
55396f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten    int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
55489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
55589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    sp<AudioTrackThread> t = mAudioTrackThread;
5569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (t != 0) {
557b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        if (previousState == STATE_STOPPING) {
558b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            mProxy->interrupt();
559b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        } else {
560b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            t->resume();
561b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        }
5629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
5639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mPreviousPriority = getpriority(PRIO_PROCESS, 0);
5649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        get_sched_policy(0, &mPreviousSchedulingGroup);
5659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
5669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
56789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
5689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    status_t status = NO_ERROR;
5699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (!(flags & CBLK_INVALID)) {
5709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        status = mAudioTrack->start();
5719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (status == DEAD_OBJECT) {
5729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            flags |= CBLK_INVALID;
57389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
5749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
5759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (flags & CBLK_INVALID) {
5769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        status = restoreTrack_l("start");
5779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
5789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
5799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (status != NO_ERROR) {
5809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("start() status %d", status);
5819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mState = previousState;
58289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (t != 0) {
583b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            if (previousState != STATE_STOPPING) {
584b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                t->pause();
585b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            }
58689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        } else {
587879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten            setpriority(PRIO_PROCESS, 0, mPreviousPriority);
588a636433cbd09c0708b85f337ef45c0cdef3bcb4dGlenn Kasten            set_sched_policy(0, mPreviousSchedulingGroup);
58989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
59089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
59189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
592b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    return status;
5939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten}
5949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
5959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid AudioTrack::stop()
5969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{
5979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
598397edb3377e5775f4df60afb8bf6d4711e5adc0eGlenn Kasten    if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
5999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return;
6009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
6019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
60223a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kasten    if (isOffloaded_l()) {
603b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        mState = STATE_STOPPING;
604b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    } else {
605b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        mState = STATE_STOPPED;
606c2813e568aa476e03d159529c0af28c99536db8dAndy Hung        mReleased = 0;
607b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
608b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
6099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy->interrupt();
6109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mAudioTrack->stop();
61161be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung
61261be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung    // Note: legacy handling - stop does not clear playback marker
61361be84167c1a11c8a030e00a2b79b2bc4fdf617fAndy Hung    // and periodic update counter, but flush does for streaming tracks.
6149b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung
6159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mSharedBuffer != 0) {
6169b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung        // clear buffer position and loop count.
6179b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung        mStaticProxy->setBufferPositionAndLoop(0 /* position */,
6189b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung                0 /* loopStart */, 0 /* loopEnd */, 0 /* loopCount */);
6199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
620b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
6219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    sp<AudioTrackThread> t = mAudioTrackThread;
6229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (t != 0) {
62323a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kasten        if (!isOffloaded_l()) {
624b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            t->pause();
625b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        }
6269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
6279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        setpriority(PRIO_PROCESS, 0, mPreviousPriority);
6289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        set_sched_policy(0, mPreviousSchedulingGroup);
6299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
63089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
63189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
63289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectbool AudioTrack::stopped() const
63389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
6349a2aaf927e56a4b4acab23ef16b3f133a9f48a63Glenn Kasten    AutoMutex lock(mLock);
6359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return mState != STATE_ACTIVE;
63689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
63789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
63889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::flush()
63989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
6409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mSharedBuffer != 0) {
6419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return;
6429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
6431703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
6449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
6459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return;
6464bae3649d504d590a546717a8e49f96a30d9a745Glenn Kasten    }
6479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    flush_l();
6481703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent}
6491703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
6501703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurentvoid AudioTrack::flush_l()
6511703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent{
6529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    ALOG_ASSERT(mState != STATE_ACTIVE);
653c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
6542c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    // clear playback marker and periodic update counter
6552c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mMarkerPosition = 0;
6562c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mMarkerReached = false;
6572c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mUpdatePeriod = 0;
658b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    mRefreshRemaining = true;
659c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
6609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mState = STATE_FLUSHED;
661c2813e568aa476e03d159529c0af28c99536db8dAndy Hung    mReleased = 0;
66223a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kasten    if (isOffloaded_l()) {
663b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        mProxy->interrupt();
664b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
6659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy->flush();
6664bae3649d504d590a546717a8e49f96a30d9a745Glenn Kasten    mAudioTrack->flush();
66789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
66889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
66989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioTrack::pause()
67089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
671f5aafb209d01ba2ab6cb55d1a12cfc653e2b4be0Eric Laurent    AutoMutex lock(mLock);
672b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    if (mState == STATE_ACTIVE) {
673b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        mState = STATE_PAUSED;
674b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    } else if (mState == STATE_STOPPING) {
675b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        mState = STATE_PAUSED_STOPPING;
676b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    } else {
6779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return;
67889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
6799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy->interrupt();
6809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mAudioTrack->pause();
6817064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George
6823a90f2849d49bf65f2d6257fd6da30ae46d745faMarco Nelissen    if (isOffloaded_l()) {
683142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (mOutput != AUDIO_IO_HANDLE_NONE) {
6847f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            // An offload output can be re-used between two audio tracks having
6857f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            // the same configuration. A timestamp query for a paused track
6867f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            // while the other is running would return an incorrect time.
6877f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            // To fix this, cache the playback position on a pause() and return
6887f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            // this time when requested until the track is resumed.
6897f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
6907f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            // OffloadThread sends HAL pause in its threadLoop. Time saved
6917f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            // here can be slightly off.
6927f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
6937f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            // TODO: check return code for getRenderPosition.
6947f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
6957064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George            uint32_t halFrames;
6967064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George            AudioSystem::getRenderPosition(mOutput, &halFrames, &mPausedPosition);
6977064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George            ALOGV("AudioTrack::pause for offload, cache current position %u", mPausedPosition);
6987064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George        }
6997064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George    }
70089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
70189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
702be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurentstatus_t AudioTrack::setVolume(float left, float right)
70389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
704c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten    // This duplicates a test by AudioTrack JNI, but that is not the only caller
705c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten    if (isnanf(left) || left < GAIN_FLOAT_ZERO || left > GAIN_FLOAT_UNITY ||
706c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten            isnanf(right) || right < GAIN_FLOAT_ZERO || right > GAIN_FLOAT_UNITY) {
707be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        return BAD_VALUE;
708be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    }
709be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
7101703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
71166e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten    mVolume[AUDIO_INTERLEAVE_LEFT] = left;
71266e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten    mVolume[AUDIO_INTERLEAVE_RIGHT] = right;
71389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
714c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten    mProxy->setVolumeLR(gain_minifloat_pack(gain_from_float(left), gain_from_float(right)));
715be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
71623a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kasten    if (isOffloaded_l()) {
71759fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        mAudioTrack->signal();
71859fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent    }
719be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    return NO_ERROR;
72089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
72189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
722b1c0993b215c5c3eebd1c6bafc22bba23d57a70bGlenn Kastenstatus_t AudioTrack::setVolume(float volume)
72389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
724b1c0993b215c5c3eebd1c6bafc22bba23d57a70bGlenn Kasten    return setVolume(volume, volume);
725be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent}
726be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
7272beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurentstatus_t AudioTrack::setAuxEffectSendLevel(float level)
728be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent{
729c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten    // This duplicates a test by AudioTrack JNI, but that is not the only caller
730c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten    if (isnanf(level) || level < GAIN_FLOAT_ZERO || level > GAIN_FLOAT_UNITY) {
731be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        return BAD_VALUE;
732be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    }
733be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
7349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
735be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    mSendLevel = level;
736e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    mProxy->setSendLevel(level);
737be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
738be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    return NO_ERROR;
739be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent}
740be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
741a5224f319e2ba4b51ddb4287705ccf8d4b8ecc51Glenn Kastenvoid AudioTrack::getAuxEffectSendLevel(float* level) const
742be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent{
743be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    if (level != NULL) {
7449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        *level = mSendLevel;
745be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    }
74689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
74789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
7483b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kastenstatus_t AudioTrack::setSampleRate(uint32_t rate)
74989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
7505cbb5785a58655e21f5f7805274b8c78b986216bAndy Hung    AutoMutex lock(mLock);
7515cbb5785a58655e21f5f7805274b8c78b986216bAndy Hung    if (rate == mSampleRate) {
7525cbb5785a58655e21f5f7805274b8c78b986216bAndy Hung        return NO_ERROR;
7535cbb5785a58655e21f5f7805274b8c78b986216bAndy Hung    }
754d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    if (isOffloadedOrDirect_l() || (mFlags & AUDIO_OUTPUT_FLAG_FAST)) {
7554ff14bae91075eb274eb1c2975982358946e7e63John Grossman        return INVALID_OPERATION;
7564ff14bae91075eb274eb1c2975982358946e7e63John Grossman    }
7570d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    if (mOutput == AUDIO_IO_HANDLE_NONE) {
7580d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent        return NO_INIT;
7590d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    }
7605cbb5785a58655e21f5f7805274b8c78b986216bAndy Hung    // NOTE: it is theoretically possible, but highly unlikely, that a device change
7615cbb5785a58655e21f5f7805274b8c78b986216bAndy Hung    // could mean a previously allowed sampling rate is no longer allowed.
7629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    uint32_t afSamplingRate;
7630d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    if (AudioSystem::getSamplingRate(mOutput, &afSamplingRate) != NO_ERROR) {
764573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurent        return NO_INIT;
76589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
76626145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    // pitch is emulated by adjusting speed and sampleRate
7675a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    const uint32_t effectiveSampleRate = adjustSampleRate(rate, mPlaybackRate.mPitch);
76826145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    if (rate == 0 || effectiveSampleRate > afSamplingRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) {
769d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return BAD_VALUE;
770d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
7718edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    // TODO: Should we also check if the buffer size is compatible?
77289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
773e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    mSampleRate = rate;
77426145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    mProxy->setSampleRate(effectiveSampleRate);
775e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
776573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurent    return NO_ERROR;
77789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
77889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
779a5224f319e2ba4b51ddb4287705ccf8d4b8ecc51Glenn Kastenuint32_t AudioTrack::getSampleRate() const
78089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
7811703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
7826f59db12a64f4496866952a251122ccb77a36c6bEric Laurent
7836f59db12a64f4496866952a251122ccb77a36c6bEric Laurent    // sample rate can be updated during playback by the offloaded decoder so we need to
7846f59db12a64f4496866952a251122ccb77a36c6bEric Laurent    // query the HAL and update if needed.
7856f59db12a64f4496866952a251122ccb77a36c6bEric Laurent// FIXME use Proxy return channel to update the rate from server and avoid polling here
786ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (isOffloadedOrDirect_l()) {
787142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (mOutput != AUDIO_IO_HANDLE_NONE) {
7886f59db12a64f4496866952a251122ccb77a36c6bEric Laurent            uint32_t sampleRate = 0;
789b7f24b101d43139b4c747129bfbc4ecf5c468b86Jean-Michel Trivi            status_t status = AudioSystem::getSamplingRate(mOutput, &sampleRate);
7906f59db12a64f4496866952a251122ccb77a36c6bEric Laurent            if (status == NO_ERROR) {
7916f59db12a64f4496866952a251122ccb77a36c6bEric Laurent                mSampleRate = sampleRate;
7926f59db12a64f4496866952a251122ccb77a36c6bEric Laurent            }
7936f59db12a64f4496866952a251122ccb77a36c6bEric Laurent        }
7946f59db12a64f4496866952a251122ccb77a36c6bEric Laurent    }
795e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    return mSampleRate;
79689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
79789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
7983a474aa67fc31505740526dd249d96204c08bf79Lajos Molnaruint32_t AudioTrack::getOriginalSampleRate() const
7993a474aa67fc31505740526dd249d96204c08bf79Lajos Molnar{
8003a474aa67fc31505740526dd249d96204c08bf79Lajos Molnar    return mOriginalSampleRate;
8013a474aa67fc31505740526dd249d96204c08bf79Lajos Molnar}
8023a474aa67fc31505740526dd249d96204c08bf79Lajos Molnar
8035a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garciastatus_t AudioTrack::setPlaybackRate(const AudioPlaybackRate &playbackRate)
8048edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung{
8058edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    AutoMutex lock(mLock);
8065a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    if (isAudioPlaybackRateEqual(playbackRate, mPlaybackRate)) {
8078edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        return NO_ERROR;
8088edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    }
809d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    if (isOffloadedOrDirect_l()) {
8108edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        return INVALID_OPERATION;
8118edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    }
8128edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
8138edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        return INVALID_OPERATION;
8148edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    }
81526145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    // pitch is emulated by adjusting speed and sampleRate
8165a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    const uint32_t effectiveRate = adjustSampleRate(mSampleRate, playbackRate.mPitch);
8175a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    const float effectiveSpeed = adjustSpeed(playbackRate.mSpeed, playbackRate.mPitch);
8185a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    const float effectivePitch = adjustPitch(playbackRate.mPitch);
8196c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    AudioPlaybackRate playbackRateTemp = playbackRate;
8206c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    playbackRateTemp.mSpeed = effectiveSpeed;
8216c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    playbackRateTemp.mPitch = effectivePitch;
8226c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia
8236c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    if (!isAudioPlaybackRateValid(playbackRateTemp)) {
82426145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung        return BAD_VALUE;
82526145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    }
8268edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    // Check if the buffer size is compatible.
82726145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    if (!isSampleRateSpeedAllowed_l(effectiveRate, effectiveSpeed)) {
8285a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia        ALOGV("setPlaybackRate(%f, %f) failed", playbackRate.mSpeed, playbackRate.mPitch);
8298edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        return BAD_VALUE;
8308edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    }
8315a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia
8326c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    // Check resampler ratios are within bounds
833e34eae2de7bd6dcba5dea132dabb09f63ac46adcDan Austin    if ((uint64_t)effectiveRate > (uint64_t)mSampleRate * (uint64_t)AUDIO_RESAMPLER_DOWN_RATIO_MAX) {
8346c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia        ALOGV("setPlaybackRate(%f, %f) failed. Resample rate exceeds max accepted value",
8356c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia                playbackRate.mSpeed, playbackRate.mPitch);
8366c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia        return BAD_VALUE;
8376c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    }
8386c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia
839e34eae2de7bd6dcba5dea132dabb09f63ac46adcDan Austin    if ((uint64_t)effectiveRate * (uint64_t)AUDIO_RESAMPLER_UP_RATIO_MAX < (uint64_t)mSampleRate) {
8406c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia        ALOGV("setPlaybackRate(%f, %f) failed. Resample rate below min accepted value",
8416c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia                        playbackRate.mSpeed, playbackRate.mPitch);
8426c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia        return BAD_VALUE;
8436c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    }
8446c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    mPlaybackRate = playbackRate;
8456c7f062d3149d6890daaee64828959ad6f61ea54Ricardo Garcia    //set effective rates
8465a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    mProxy->setPlaybackRate(playbackRateTemp);
84726145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    mProxy->setSampleRate(effectiveRate); // FIXME: not quite "atomic" with setPlaybackRate
8488edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    return NO_ERROR;
8498edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung}
8508edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung
8515a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garciaconst AudioPlaybackRate& AudioTrack::getPlaybackRate() const
8528edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung{
8538edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    AutoMutex lock(mLock);
8545a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    return mPlaybackRate;
8558edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung}
8568edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung
857c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burkssize_t AudioTrack::getBufferSizeInFrames()
858c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk{
859c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    AutoMutex lock(mLock);
860c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    if (mOutput == AUDIO_IO_HANDLE_NONE || mProxy.get() == 0) {
861c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk        return NO_INIT;
862c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    }
863c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    return mProxy->getBufferSizeInFrames();
864c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk}
865c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk
866c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burkssize_t AudioTrack::setBufferSizeInFrames(size_t bufferSizeInFrames)
867c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk{
868c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    AutoMutex lock(mLock);
869c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    if (mOutput == AUDIO_IO_HANDLE_NONE || mProxy.get() == 0) {
870c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk        return NO_INIT;
871c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    }
872c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    // Reject if timed track or compressed audio.
873d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    if (!audio_is_linear_pcm(mFormat)) {
874c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk        return INVALID_OPERATION;
875c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    }
876c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    // TODO also need to inform the server side (through mAudioTrack) that
877c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    // the buffer count is reduced, otherwise the track may never start
878c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    // because the server thinks it is never filled.
879c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk    return mProxy->setBufferSizeInFrames(bufferSizeInFrames);
880c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk}
881c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk
88289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
88389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
884d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    if (mSharedBuffer == 0 || isOffloadedOrDirect()) {
885083d1c1492d496960d5b28f4664ff02101736677Glenn Kasten        return INVALID_OPERATION;
886083d1c1492d496960d5b28f4664ff02101736677Glenn Kasten    }
887083d1c1492d496960d5b28f4664ff02101736677Glenn Kasten
88889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (loopCount == 0) {
8899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ;
8909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
8919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            loopEnd - loopStart >= MIN_LOOP) {
8929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ;
8939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
89489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
89589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
89689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
8979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
8989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // See setPosition() regarding setting parameters such as loop points or position while active
8999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mState == STATE_ACTIVE) {
9009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return INVALID_OPERATION;
901c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
9029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    setLoop_l(loopStart, loopEnd, loopCount);
90389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
90489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
90589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
9079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{
9084ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    // We do not update the periodic notification point.
9094ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    // mNewPosition = updateAndGetPosition_l() + mUpdatePeriod;
9104ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    mLoopCount = loopCount;
9114ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    mLoopEnd = loopEnd;
9124ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    mLoopStart = loopStart;
91353c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    mLoopCountNotified = loopCount;
9149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
9153c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung
9163c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    // Waking the AudioTrackThread is not needed as this cannot be called when active.
9179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten}
9189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
91989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::setMarkerPosition(uint32_t marker)
92089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
921fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten    // The only purpose of setting marker position is to get a callback
922ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (mCbf == NULL || isOffloadedOrDirect()) {
923d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return INVALID_OPERATION;
924d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
92589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
92789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mMarkerPosition = marker;
9282c22aeb65e801f663a754d043062f85e49f77739Jean-Michel Trivi    mMarkerReached = false;
92989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9303c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    sp<AudioTrackThread> t = mAudioTrackThread;
9313c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    if (t != 0) {
9323c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung        t->wake();
9333c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    }
93489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
93589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
93689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
937a5224f319e2ba4b51ddb4287705ccf8d4b8ecc51Glenn Kastenstatus_t AudioTrack::getMarkerPosition(uint32_t *marker) const
93889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
939ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (isOffloadedOrDirect()) {
940b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        return INVALID_OPERATION;
941b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
942d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    if (marker == NULL) {
943d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return BAD_VALUE;
944d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
94589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
94790e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    mMarkerPosition.getValue(marker);
94889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
94989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
95089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
95189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
95289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
95389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
954fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten    // The only purpose of setting position update period is to get a callback
955ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (mCbf == NULL || isOffloadedOrDirect()) {
956d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return INVALID_OPERATION;
957d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
95889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
960200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    mNewPosition = updateAndGetPosition_l() + updatePeriod;
96189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mUpdatePeriod = updatePeriod;
9622b2165c75790050810460c8de3f414876bce4c0eGlenn Kasten
9633c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    sp<AudioTrackThread> t = mAudioTrackThread;
9643c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    if (t != 0) {
9653c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung        t->wake();
9663c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    }
96789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
96889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
96989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
970a5224f319e2ba4b51ddb4287705ccf8d4b8ecc51Glenn Kastenstatus_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
97189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
972ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (isOffloadedOrDirect()) {
973b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        return INVALID_OPERATION;
974b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
975d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    if (updatePeriod == NULL) {
976d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return BAD_VALUE;
977d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
97889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
98089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    *updatePeriod = mUpdatePeriod;
98189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
98289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
98389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
98489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
98589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::setPosition(uint32_t position)
98689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
987d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    if (mSharedBuffer == 0 || isOffloadedOrDirect()) {
988d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return INVALID_OPERATION;
989d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
9909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (position > mFrameCount) {
9919c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten        return BAD_VALUE;
9929c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten    }
9939c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten
9949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
9959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Currently we require that the player is inactive before setting parameters such as position
9969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // or loop points.  Otherwise, there could be a race condition: the application could read the
9979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // current position, compute a new position or loop parameters, and then set that position or
9989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // loop parameters but it would do the "wrong" thing since the position has continued to advance
9999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // in the mean time.  If we ever provide a sequencer in server, we could allow a way for the app
10009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // to specify how it wants to handle such scenarios.
10019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mState == STATE_ACTIVE) {
10029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return INVALID_OPERATION;
1003d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
10049b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung    // After setting the position, use full update period before notification.
1005200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    mNewPosition = updateAndGetPosition_l() + mUpdatePeriod;
10069b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung    mStaticProxy->setBufferPosition(position);
10073c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung
10083c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    // Waking the AudioTrackThread is not needed as this cannot be called when active.
100989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
101089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
101189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1012200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kastenstatus_t AudioTrack::getPosition(uint32_t *position)
101389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
1014d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    if (position == NULL) {
1015d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return BAD_VALUE;
1016d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
10179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
10181703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
1019ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (isOffloadedOrDirect_l()) {
1020b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        uint32_t dspFrames = 0;
102189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1022ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        if (isOffloaded_l() && ((mState == STATE_PAUSED) || (mState == STATE_PAUSED_STOPPING))) {
10237064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George            ALOGV("getPosition called in paused state, return cached position %u", mPausedPosition);
10247064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George            *position = mPausedPosition;
10257064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George            return NO_ERROR;
10267064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George        }
10277064fd2dcdfeafea53cd5a992bb78c413542f29fHaynes Mathew George
1028142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (mOutput != AUDIO_IO_HANDLE_NONE) {
10291f1db8356b599bc40703c907fb69e6e539343532Andy Hung            uint32_t halFrames; // actually unused
10301f1db8356b599bc40703c907fb69e6e539343532Andy Hung            (void) AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
10311f1db8356b599bc40703c907fb69e6e539343532Andy Hung            // FIXME: on getRenderPosition() error, we return OK with frame position 0.
1032b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        }
10337f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        // FIXME: dspFrames may not be zero in (mState == STATE_STOPPED || mState == STATE_FLUSHED)
10347f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        // due to hardware latency. We leave this behavior for now.
1035b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        *position = dspFrames;
1036b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    } else {
1037275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        if (mCblk->mFlags & CBLK_INVALID) {
10381f1db8356b599bc40703c907fb69e6e539343532Andy Hung            (void) restoreTrack_l("getPosition");
10391f1db8356b599bc40703c907fb69e6e539343532Andy Hung            // FIXME: for compatibility with the Java API we ignore the restoreTrack_l()
10401f1db8356b599bc40703c907fb69e6e539343532Andy Hung            // error here (e.g. DEAD_OBJECT) and return OK with the last recorded server position.
1041275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent        }
1042275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent
1043b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
1044200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ?
104590e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung                0 : updateAndGetPosition_l().value();
1046b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
104789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
104889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
104989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1050377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioTrack::getBufferPosition(uint32_t *position)
10519c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten{
1052d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    if (mSharedBuffer == 0) {
10539c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten        return INVALID_OPERATION;
10549c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten    }
10559c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten    if (position == NULL) {
10569c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten        return BAD_VALUE;
10579c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten    }
10589c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten
10599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
10609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    *position = mStaticProxy->getBufferPosition();
10619c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten    return NO_ERROR;
10629c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten}
10639c6745f128648f6e0144b74ee593911a9fa10d51Glenn Kasten
106489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioTrack::reload()
106589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
1066d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    if (mSharedBuffer == 0 || isOffloadedOrDirect()) {
1067083d1c1492d496960d5b28f4664ff02101736677Glenn Kasten        return INVALID_OPERATION;
1068083d1c1492d496960d5b28f4664ff02101736677Glenn Kasten    }
1069083d1c1492d496960d5b28f4664ff02101736677Glenn Kasten
10701703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
10719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // See setPosition() regarding setting parameters such as loop points or position while active
10729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mState == STATE_ACTIVE) {
1073d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return INVALID_OPERATION;
1074d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
10759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mNewPosition = mUpdatePeriod;
10769b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung    (void) updateAndGetPosition_l();
10779b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung    mPosition = 0;
10781b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk    mPreviousTimestampValid = false;
107953c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung#if 0
10809b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung    // The documentation is not clear on the behavior of reload() and the restoration
108153c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    // of loop count. Historically we have not restored loop count, start, end,
108253c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    // but it makes sense if one desires to repeat playing a particular sound.
108353c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    if (mLoopCount != 0) {
108453c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        mLoopCountNotified = mLoopCount;
108553c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        mStaticProxy->setLoop(mLoopStart, mLoopEnd, mLoopCount);
108653c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    }
108753c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung#endif
10889b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung    mStaticProxy->setBufferPosition(0);
108989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
109089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
109189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
109238e905b3cbba4da443d799b16999989781afc6d8Glenn Kastenaudio_io_handle_t AudioTrack::getOutput() const
1093c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent{
10941703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
1095b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    return mOutput;
10961703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent}
10971703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
1098aa9811945f575614b3482d09e4d969792701cebbPaul McLeanstatus_t AudioTrack::setOutputDevice(audio_port_handle_t deviceId) {
1099aa9811945f575614b3482d09e4d969792701cebbPaul McLean    AutoMutex lock(mLock);
1100aa9811945f575614b3482d09e4d969792701cebbPaul McLean    if (mSelectedDeviceId != deviceId) {
1101aa9811945f575614b3482d09e4d969792701cebbPaul McLean        mSelectedDeviceId = deviceId;
1102493404d8c396e15ed73455acd39aa0f1940996e2Eric Laurent        android_atomic_or(CBLK_INVALID, &mCblk->mFlags);
1103aa9811945f575614b3482d09e4d969792701cebbPaul McLean    }
1104493404d8c396e15ed73455acd39aa0f1940996e2Eric Laurent    return NO_ERROR;
1105aa9811945f575614b3482d09e4d969792701cebbPaul McLean}
1106aa9811945f575614b3482d09e4d969792701cebbPaul McLean
1107aa9811945f575614b3482d09e4d969792701cebbPaul McLeanaudio_port_handle_t AudioTrack::getOutputDevice() {
1108aa9811945f575614b3482d09e4d969792701cebbPaul McLean    AutoMutex lock(mLock);
1109aa9811945f575614b3482d09e4d969792701cebbPaul McLean    return mSelectedDeviceId;
1110aa9811945f575614b3482d09e4d969792701cebbPaul McLean}
1111aa9811945f575614b3482d09e4d969792701cebbPaul McLean
1112296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurentaudio_port_handle_t AudioTrack::getRoutedDeviceId() {
1113296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    AutoMutex lock(mLock);
1114296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (mOutput == AUDIO_IO_HANDLE_NONE) {
1115296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        return AUDIO_PORT_HANDLE_NONE;
1116296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
1117296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    return AudioSystem::getDeviceIdForIo(mOutput);
1118296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent}
1119296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
1120be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurentstatus_t AudioTrack::attachAuxEffect(int effectId)
1121be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent{
11229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
11232beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    status_t status = mAudioTrack->attachAuxEffect(effectId);
11242beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    if (status == NO_ERROR) {
11252beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent        mAuxEffectId = effectId;
11262beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    }
11272beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    return status;
1128be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent}
1129be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
1130e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurentaudio_stream_type_t AudioTrack::streamType() const
1131e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent{
1132e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    if (mStreamType == AUDIO_STREAM_DEFAULT) {
1133e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent        return audio_attributes_to_stream_type(&mAttributes);
1134e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    }
1135e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    return mStreamType;
1136e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent}
1137e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent
113889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
113989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
11401703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// must be called with mLock held
1141200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kastenstatus_t AudioTrack::createTrack_l()
114234f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent{
114334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
114434f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (audioFlinger == 0) {
1145e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        ALOGE("Could not get audioflinger");
1146e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        return NO_INIT;
114734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
114834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
1149296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (mDeviceCallback != 0 && mOutput != AUDIO_IO_HANDLE_NONE) {
1150296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        AudioSystem::removeAudioDeviceCallback(mDeviceCallback, mOutput);
1151296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
1152e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    audio_io_handle_t output;
1153e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    audio_stream_type_t streamType = mStreamType;
1154e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    audio_attributes_t *attr = (mStreamType == AUDIO_STREAM_DEFAULT) ? &mAttributes : NULL;
1155e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent
1156aa9811945f575614b3482d09e4d969792701cebbPaul McLean    status_t status;
1157aa9811945f575614b3482d09e4d969792701cebbPaul McLean    status = AudioSystem::getOutputForAttr(attr, &output,
11588c7e6dac6f5eb38cef627dab92eac8b38513450cEric Laurent                                           (audio_session_t)mSessionId, &streamType, mClientUid,
1159aa9811945f575614b3482d09e4d969792701cebbPaul McLean                                           mSampleRate, mFormat, mChannelMask,
1160aa9811945f575614b3482d09e4d969792701cebbPaul McLean                                           mFlags, mSelectedDeviceId, mOffloadInfo);
1161e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent
1162e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    if (status != NO_ERROR || output == AUDIO_IO_HANDLE_NONE) {
11634c36d6f48f4eecacc148317f775f65961a4f03d2Glenn Kasten        ALOGE("Could not get audio output for session %d, stream type %d, usage %d, sample rate %u, format %#x,"
11645bd3f38638acab633d181359cc9ec27b80f84d43Jean-Michel Trivi              " channel mask %#x, flags %#x",
11654c36d6f48f4eecacc148317f775f65961a4f03d2Glenn Kasten              mSessionId, streamType, mAttributes.usage, mSampleRate, mFormat, mChannelMask, mFlags);
116638e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten        return BAD_VALUE;
116738e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    }
116838e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    {
116938e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
117038e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    // we must release it ourselves if anything goes wrong.
117138e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten
1172ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten    // Not all of these values are needed under all conditions, but it is easier to get them all
11739f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung    status = AudioSystem::getLatency(output, &mAfLatency);
1174ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten    if (status != NO_ERROR) {
11759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("getLatency(%d) failed status %d", output, status);
117638e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten        goto release;
1177d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    }
11789f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung    ALOGV("createTrack_l() output %d afLatency %u", output, mAfLatency);
1179d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent
11809f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung    status = AudioSystem::getFrameCount(output, &mAfFrameCount);
1181ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten    if (status != NO_ERROR) {
1182b7f24b101d43139b4c747129bfbc4ecf5c468b86Jean-Michel Trivi        ALOGE("getFrameCount(output=%d) status %d", output, status);
118338e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten        goto release;
1184ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten    }
1185ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten
11869f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung    status = AudioSystem::getSamplingRate(output, &mAfSampleRate);
1187ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten    if (status != NO_ERROR) {
1188b7f24b101d43139b4c747129bfbc4ecf5c468b86Jean-Michel Trivi        ALOGE("getSamplingRate(output=%d) status %d", output, status);
118938e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten        goto release;
1190ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten    }
11910d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    if (mSampleRate == 0) {
11929f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung        mSampleRate = mAfSampleRate;
11939f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung        mOriginalSampleRate = mAfSampleRate;
11940d6db582f2ccc9f8943c5f3965e2994b7d137158Eric Laurent    }
1195d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    // Client can only express a preference for FAST.  Server will perform additional tests.
119633ff89ba94a527e4293ee5349da01483252d5c83Phil Burk    if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
119733ff89ba94a527e4293ee5349da01483252d5c83Phil Burk        bool useCaseAllowed =
11984a4a0959bca78e03e3c3f486ba17829c28314d8cGlenn Kasten            // either of these use cases:
11994a4a0959bca78e03e3c3f486ba17829c28314d8cGlenn Kasten            // use case 1: shared buffer
1200363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten            (mSharedBuffer != 0) ||
1201c6ba823a397abf865b02f4f48fe18231f94d8e87Glenn Kasten            // use case 2: callback transfer mode
12021dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten            (mTransfer == TRANSFER_CALLBACK) ||
12031dfe2f9c2d03fc8d0ed0cdfe0b9fb894bc0bcc11Glenn Kasten            // use case 3: obtain/release mode
120433ff89ba94a527e4293ee5349da01483252d5c83Phil Burk            (mTransfer == TRANSFER_OBTAIN) ||
120533ff89ba94a527e4293ee5349da01483252d5c83Phil Burk            // use case 4: synchronous write
120633ff89ba94a527e4293ee5349da01483252d5c83Phil Burk            ((mTransfer == TRANSFER_SYNC) && mThreadCanCallJava);
120733ff89ba94a527e4293ee5349da01483252d5c83Phil Burk        // sample rates must also match
120833ff89ba94a527e4293ee5349da01483252d5c83Phil Burk        bool fastAllowed = useCaseAllowed && (mSampleRate == mAfSampleRate);
120933ff89ba94a527e4293ee5349da01483252d5c83Phil Burk        if (!fastAllowed) {
121033ff89ba94a527e4293ee5349da01483252d5c83Phil Burk            ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client; transfer %d,"
121133ff89ba94a527e4293ee5349da01483252d5c83Phil Burk                "track %u Hz, output %u Hz",
12129f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung                mTransfer, mSampleRate, mAfSampleRate);
121333ff89ba94a527e4293ee5349da01483252d5c83Phil Burk            // once denied, do not request again if IAudioTrack is re-created
121433ff89ba94a527e4293ee5349da01483252d5c83Phil Burk            mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
121533ff89ba94a527e4293ee5349da01483252d5c83Phil Burk        }
12164a4a0959bca78e03e3c3f486ba17829c28314d8cGlenn Kasten    }
12174a4a0959bca78e03e3c3f486ba17829c28314d8cGlenn Kasten
1218ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten    // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
1219b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten    //  n = 1   fast track with single buffering; nBuffering is ignored
1220b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten    //  n = 2   fast track with double buffering
12210e48d25606c82def035ad10a5b3923767a765cddAndy Hung    //  n = 2   normal track, (including those with sample rate conversion)
12220e48d25606c82def035ad10a5b3923767a765cddAndy Hung    //  n >= 3  very high latency or very small notification interval (unused).
12230e48d25606c82def035ad10a5b3923767a765cddAndy Hung    const uint32_t nBuffering = 2;
1224ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten
1225d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    mNotificationFramesAct = mNotificationFramesReq;
1226e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten
1227363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten    size_t frameCount = mReqFrameCount;
1228fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk    if (!audio_has_proportional_frames(mFormat)) {
1229e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten
1230363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten        if (mSharedBuffer != 0) {
1231e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten            // Same comment as below about ignoring frameCount parameter for set()
1232363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten            frameCount = mSharedBuffer->size();
1233e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten        } else if (frameCount == 0) {
12349f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung            frameCount = mAfFrameCount;
1235d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent        }
1236b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        if (mNotificationFramesAct != frameCount) {
1237b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            mNotificationFramesAct = frameCount;
1238b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        }
1239363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten    } else if (mSharedBuffer != 0) {
1240abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung        // FIXME: Ensure client side memory buffers need
1241abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung        // not have additional alignment beyond sample
1242abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung        // (e.g. 16 bit stereo accessed as 32 bit frame).
1243abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung        size_t alignment = audio_bytes_per_sample(mFormat);
1244b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten        if (alignment & 1) {
1245abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung            // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java).
1246b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten            alignment = 1;
1247b773038277ba328696832b690b91ce6e95bd1a6cGlenn Kasten        }
1248a42ff007a17d63df22c60dd5e5fd811ee45ca1b3Glenn Kasten        if (mChannelCount > 1) {
1249e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten            // More than 2 channels does not require stronger alignment than stereo
1250e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten            alignment <<= 1;
1251e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten        }
12521d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath        if (((uintptr_t)mSharedBuffer->pointer() & (alignment - 1)) != 0) {
1253a42ff007a17d63df22c60dd5e5fd811ee45ca1b3Glenn Kasten            ALOGE("Invalid buffer alignment: address %p, channel count %u",
1254363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten                    mSharedBuffer->pointer(), mChannelCount);
125538e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten            status = BAD_VALUE;
125638e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten            goto release;
1257e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten        }
1258e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten
1259e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten        // When initializing a shared buffer AudioTrack via constructors,
1260e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten        // there's no frameCount parameter.
1261e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten        // But when initializing a shared buffer AudioTrack via set(),
1262e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten        // there _is_ a frameCount parameter.  We silently ignore it.
1263abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung        frameCount = mSharedBuffer->size() / mFrameSize;
1264e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten    } else {
12658edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        // For fast tracks the frame count calculations and checks are done by server
12668edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung
12678edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        if ((mFlags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
12688edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            // for normal tracks precompute the frame count based on speed.
12698edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            const size_t minFrameCount = calculateMinFrameCount(
12709f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung                    mAfLatency, mAfFrameCount, mAfSampleRate, mSampleRate,
12715a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia                    mPlaybackRate.mSpeed);
12728edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            if (frameCount < minFrameCount) {
12738edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung                frameCount = minFrameCount;
12748edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            }
12758edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        }
1276d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent    }
1277d1b449aad6c087a69f5ec66b7facb2845b73f1cbEric Laurent
1278a075db4ff9b086ac2885df77bb6da0869293df92Glenn Kasten    IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
12793acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
12803acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    pid_t tid = -1;
1281363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten    if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
12824a4a0959bca78e03e3c3f486ba17829c28314d8cGlenn Kasten        trackFlags |= IAudioFlinger::TRACK_FAST;
128333ff89ba94a527e4293ee5349da01483252d5c83Phil Burk        if (mAudioTrackThread != 0 && !mThreadCanCallJava) {
12843acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten            tid = mAudioTrackThread->getTid();
12853acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        }
12864a4a0959bca78e03e3c3f486ba17829c28314d8cGlenn Kasten    }
12874a4a0959bca78e03e3c3f486ba17829c28314d8cGlenn Kasten
1288363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten    if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1289b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
1290b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
1291b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
1292ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
1293ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        trackFlags |= IAudioFlinger::TRACK_DIRECT;
1294ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    }
1295ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent
129674935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    size_t temp = frameCount;   // temp may be replaced by a revised value of frameCount,
129774935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                                // but we will still need the original value also
1298138d6f9b0b77d94773a8a29d3c5bdb78f38ee142Glenn Kasten    int originalSessionId = mSessionId;
1299e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
1300363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten                                                      mSampleRate,
1301abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung                                                      mFormat,
1302a42ff007a17d63df22c60dd5e5fd811ee45ca1b3Glenn Kasten                                                      mChannelMask,
130374935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                                                      &temp,
1304e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten                                                      &trackFlags,
1305363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten                                                      mSharedBuffer,
130634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      output,
13073acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten                                                      tid,
1308be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent                                                      &mSessionId,
1309462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                      mClientUid,
131034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                      &status);
1311138d6f9b0b77d94773a8a29d3c5bdb78f38ee142Glenn Kasten    ALOGE_IF(originalSessionId != AUDIO_SESSION_ALLOCATE && mSessionId != originalSessionId,
1312138d6f9b0b77d94773a8a29d3c5bdb78f38ee142Glenn Kasten            "session ID changed from %d to %d", originalSessionId, mSessionId);
131334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
1314c08d20b6a37122ebf116262c9372509ed060d4c1Glenn Kasten    if (status != NO_ERROR) {
131529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("AudioFlinger could not create track, status: %d", status);
131638e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten        goto release;
131734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
1318c08d20b6a37122ebf116262c9372509ed060d4c1Glenn Kasten    ALOG_ASSERT(track != 0);
1319c08d20b6a37122ebf116262c9372509ed060d4c1Glenn Kasten
132038e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    // AudioFlinger now owns the reference to the I/O handle,
132138e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    // so we are no longer responsible for releasing it.
132238e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten
1323d2c38fc4d5dc742d7441444316849510dd2b7363Glenn Kasten    sp<IMemory> iMem = track->getCblk();
1324d2c38fc4d5dc742d7441444316849510dd2b7363Glenn Kasten    if (iMem == 0) {
132529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Could not get control block");
132634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        return NO_INIT;
132734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
13280cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten    void *iMemPointer = iMem->pointer();
13290cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten    if (iMemPointer == NULL) {
13300cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten        ALOGE("Could not get control block pointer");
13310cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten        return NO_INIT;
13320cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten    }
133353cec22821072719ee02c856e9ac2dda2496c570Glenn Kasten    // invariant that mAudioTrack != 0 is true only after set() returns successfully
13349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mAudioTrack != 0) {
1335f888020c6e2735624f2b2a30e72aca24e17b8b4dMarco Nelissen        IInterface::asBinder(mAudioTrack)->unlinkToDeath(mDeathNotifier, this);
13369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mDeathNotifier.clear();
13379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
133834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    mAudioTrack = track;
1339d2c38fc4d5dc742d7441444316849510dd2b7363Glenn Kasten    mCblkMemory = iMem;
13403bcffa136909c1fb6e88ee4efd12ccac18360a85Eric Laurent    IPCThreadState::self()->flushCommands();
13413bcffa136909c1fb6e88ee4efd12ccac18360a85Eric Laurent
13420cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten    audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
1343d2c38fc4d5dc742d7441444316849510dd2b7363Glenn Kasten    mCblk = cblk;
134474935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    // note that temp is the (possibly revised) value of frameCount
1345b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten    if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1346b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten        // In current design, AudioTrack client checks and ensures frame count validity before
1347b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten        // passing it to AudioFlinger so AudioFlinger should not return a different value except
1348b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten        // for fast track as it uses a special method of assigning frame count.
134934fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn        ALOGW("Requested frameCount %zu but received frameCount %zu", frameCount, temp);
1350b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten    }
1351b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten    frameCount = temp;
13525f631515d098c29603cda88f7a7e7580a2d55b57Glenn Kasten
1353a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten    mAwaitBoost = false;
1354363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten    if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1355e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten        if (trackFlags & IAudioFlinger::TRACK_FAST) {
135634fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn            ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %zu", frameCount);
135733ff89ba94a527e4293ee5349da01483252d5c83Phil Burk            if (!mThreadCanCallJava) {
135833ff89ba94a527e4293ee5349da01483252d5c83Phil Burk                mAwaitBoost = true;
135933ff89ba94a527e4293ee5349da01483252d5c83Phil Burk            }
13603acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        } else {
136134fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn            ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %zu", frameCount);
1362093000f7d11839b920e8dfaa42ed1d09f48e24b8Glenn Kasten            // once denied, do not request again if IAudioTrack is re-created
1363363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten            mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
1364e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten        }
13653acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
1366363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten    if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1367b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1368b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1369b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        } else {
1370b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1371363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten            mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
137238e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten            // FIXME This is a warning, not an error, so don't return error status
137338e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten            //return NO_INIT;
1374b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        }
1375b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
1376ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
1377ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        if (trackFlags & IAudioFlinger::TRACK_DIRECT) {
1378ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            ALOGV("AUDIO_OUTPUT_FLAG_DIRECT successful");
1379ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        } else {
1380ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            ALOGW("AUDIO_OUTPUT_FLAG_DIRECT denied by server");
1381ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1382ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            // FIXME This is a warning, not an error, so don't return error status
1383ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            //return NO_INIT;
1384ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        }
1385ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    }
13860e48d25606c82def035ad10a5b3923767a765cddAndy Hung    // Make sure that application is notified with sufficient margin before underrun
13870e48d25606c82def035ad10a5b3923767a765cddAndy Hung    if (mSharedBuffer == 0 && audio_is_linear_pcm(mFormat)) {
13880e48d25606c82def035ad10a5b3923767a765cddAndy Hung        // Theoretically double-buffering is not required for fast tracks,
13890e48d25606c82def035ad10a5b3923767a765cddAndy Hung        // due to tighter scheduling.  But in practice, to accommodate kernels with
13900e48d25606c82def035ad10a5b3923767a765cddAndy Hung        // scheduling jitter, and apps with computation jitter, we use double-buffering
13910e48d25606c82def035ad10a5b3923767a765cddAndy Hung        // for fast tracks just like normal streaming tracks.
13920e48d25606c82def035ad10a5b3923767a765cddAndy Hung        if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount / nBuffering) {
13930e48d25606c82def035ad10a5b3923767a765cddAndy Hung            mNotificationFramesAct = frameCount / nBuffering;
13940e48d25606c82def035ad10a5b3923767a765cddAndy Hung        }
13950e48d25606c82def035ad10a5b3923767a765cddAndy Hung    }
1396b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
139738e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    // We retain a copy of the I/O handle, but don't own the reference
139838e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    mOutput = output;
13999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mRefreshRemaining = true;
14009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
14019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Starting address of buffers in shared memory.  If there is a shared buffer, buffers
14029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // is the value of pointer() for the shared buffer, otherwise buffers points
14039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // immediately after the control block.  This address is for the mapping within client
14049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // address space.  AudioFlinger::TrackBase::mBuffer is for the server address space.
14059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    void* buffers;
1406363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten    if (mSharedBuffer == 0) {
1407138d6f9b0b77d94773a8a29d3c5bdb78f38ee142Glenn Kasten        buffers = cblk + 1;
140834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    } else {
1409363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten        buffers = mSharedBuffer->pointer();
1410138d6f9b0b77d94773a8a29d3c5bdb78f38ee142Glenn Kasten        if (buffers == NULL) {
1411138d6f9b0b77d94773a8a29d3c5bdb78f38ee142Glenn Kasten            ALOGE("Could not get buffer pointer");
1412138d6f9b0b77d94773a8a29d3c5bdb78f38ee142Glenn Kasten            return NO_INIT;
1413138d6f9b0b77d94773a8a29d3c5bdb78f38ee142Glenn Kasten        }
141434f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
141534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
14162beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent    mAudioTrack->attachAuxEffect(mAuxEffectId);
14178edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    // FIXME doesn't take into account speed or future sample rate changes (until restoreTrack)
1418e0fa467e1150c65a7b1b1ed904c579b40f97c9dfGlenn Kasten    // FIXME don't believe this lie
14199f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung    mLatency = mAfLatency + (1000*frameCount) / mSampleRate;
14205f631515d098c29603cda88f7a7e7580a2d55b57Glenn Kasten
1421b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten    mFrameCount = frameCount;
1422093000f7d11839b920e8dfaa42ed1d09f48e24b8Glenn Kasten    // If IAudioTrack is re-created, don't let the requested frameCount
1423093000f7d11839b920e8dfaa42ed1d09f48e24b8Glenn Kasten    // decrease.  This can confuse clients that cache frameCount().
1424b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten    if (frameCount > mReqFrameCount) {
1425b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten        mReqFrameCount = frameCount;
1426093000f7d11839b920e8dfaa42ed1d09f48e24b8Glenn Kasten    }
1427e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
1428d7bd69e4d1e74f25413721804d5290c53ea87f2bAndy Hung    // reset server position to 0 as we have new cblk.
1429d7bd69e4d1e74f25413721804d5290c53ea87f2bAndy Hung    mServer = 0;
1430d7bd69e4d1e74f25413721804d5290c53ea87f2bAndy Hung
1431e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    // update proxy
1432363fb75db26698cbb50065506e0c80b61d1fbf92Glenn Kasten    if (mSharedBuffer == 0) {
14339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mStaticProxy.clear();
1434abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung        mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSize);
14359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
1436abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung        mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSize);
14379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mProxy = mStaticProxy;
14389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
1439e6a9d65845032644b0865b6e3d0b5a820f35d9e4seunghak.han
1440e6a9d65845032644b0865b6e3d0b5a820f35d9e4seunghak.han    mProxy->setVolumeLR(gain_minifloat_pack(
1441e6a9d65845032644b0865b6e3d0b5a820f35d9e4seunghak.han            gain_from_float(mVolume[AUDIO_INTERLEAVE_LEFT]),
1442e6a9d65845032644b0865b6e3d0b5a820f35d9e4seunghak.han            gain_from_float(mVolume[AUDIO_INTERLEAVE_RIGHT])));
1443e6a9d65845032644b0865b6e3d0b5a820f35d9e4seunghak.han
1444e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    mProxy->setSendLevel(mSendLevel);
14455a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    const uint32_t effectiveSampleRate = adjustSampleRate(mSampleRate, mPlaybackRate.mPitch);
14465a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    const float effectiveSpeed = adjustSpeed(mPlaybackRate.mSpeed, mPlaybackRate.mPitch);
14475a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    const float effectivePitch = adjustPitch(mPlaybackRate.mPitch);
144826145643ce22d797b3b1675c82c47a2d8c79ecaaAndy Hung    mProxy->setSampleRate(effectiveSampleRate);
14495a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia
14505a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    AudioPlaybackRate playbackRateTemp = mPlaybackRate;
14515a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    playbackRateTemp.mSpeed = effectiveSpeed;
14525a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    playbackRateTemp.mPitch = effectivePitch;
14535a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    mProxy->setPlaybackRate(playbackRateTemp);
14549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy->setMinimum(mNotificationFramesAct);
14559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
14569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mDeathNotifier = new DeathNotifier(this);
1457f888020c6e2735624f2b2a30e72aca24e17b8b4dMarco Nelissen    IInterface::asBinder(mAudioTrack)->linkToDeath(mDeathNotifier, this);
1458e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
1459296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (mDeviceCallback != 0) {
1460296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        AudioSystem::addAudioDeviceCallback(mDeviceCallback, mOutput);
1461296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
1462296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
146334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    return NO_ERROR;
146438e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    }
146538e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten
146638e905b3cbba4da443d799b16999989781afc6d8Glenn Kastenrelease:
1467e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    AudioSystem::releaseOutput(output, streamType, (audio_session_t)mSessionId);
146838e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    if (status == NO_ERROR) {
146938e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten        status = NO_INIT;
147038e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    }
147138e905b3cbba4da443d799b16999989781afc6d8Glenn Kasten    return status;
147234f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent}
147334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
1474b46f394a85d704dd05287cf9bb77cf86e3c02a38Glenn Kastenstatus_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount, size_t *nonContig)
147589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
14769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (audioBuffer == NULL) {
1477551b5355d34aa42890811fc3606d3b63429296cdGlenn Kasten        if (nonContig != NULL) {
1478551b5355d34aa42890811fc3606d3b63429296cdGlenn Kasten            *nonContig = 0;
1479551b5355d34aa42890811fc3606d3b63429296cdGlenn Kasten        }
14809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return BAD_VALUE;
14819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
14829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mTransfer != TRANSFER_OBTAIN) {
14839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer->frameCount = 0;
14849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer->size = 0;
14859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer->raw = NULL;
1486551b5355d34aa42890811fc3606d3b63429296cdGlenn Kasten        if (nonContig != NULL) {
1487551b5355d34aa42890811fc3606d3b63429296cdGlenn Kasten            *nonContig = 0;
1488551b5355d34aa42890811fc3606d3b63429296cdGlenn Kasten        }
14899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return INVALID_OPERATION;
14909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
14919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
14929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    const struct timespec *requested;
1493df576995a5aad3428aeeef765387d650945c161dEric Laurent    struct timespec timeout;
14949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (waitCount == -1) {
14959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &ClientProxy::kForever;
14969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else if (waitCount == 0) {
14979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &ClientProxy::kNonBlocking;
14989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else if (waitCount > 0) {
14999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        long long ms = WAIT_PERIOD_MS * (long long) waitCount;
15009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        timeout.tv_sec = ms / 1000;
15019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        timeout.tv_nsec = (int) (ms % 1000) * 1000000;
15029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &timeout;
15039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
15049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("%s invalid waitCount %d", __func__, waitCount);
15059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = NULL;
15069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
1507b46f394a85d704dd05287cf9bb77cf86e3c02a38Glenn Kasten    return obtainBuffer(audioBuffer, requested, NULL /*elapsed*/, nonContig);
15089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten}
15099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
15109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenstatus_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
15119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        struct timespec *elapsed, size_t *nonContig)
15129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{
15139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // previous and new IAudioTrack sequence numbers are used to detect track re-creation
15149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    uint32_t oldSequence = 0;
15159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    uint32_t newSequence;
1516e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
15179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    Proxy::Buffer buffer;
15189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    status_t status = NO_ERROR;
151989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
15209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    static const int32_t kMaxTries = 5;
15219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    int32_t tryCounter = kMaxTries;
152289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
15239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    do {
15249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
15259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // keep them from going away if another thread re-creates the track during obtainBuffer()
15269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        sp<AudioTrackClientProxy> proxy;
15279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        sp<IMemory> iMem;
152889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
15299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        {   // start of lock scope
15309f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            AutoMutex lock(mLock);
15311703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
15329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            newSequence = mSequence;
15339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
15349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (status == DEAD_OBJECT) {
15359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                // re-create track, unless someone else has already done so
15369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                if (newSequence == oldSequence) {
15379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                    status = restoreTrack_l("obtainBuffer");
15389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                    if (status != NO_ERROR) {
1539b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                        buffer.mFrameCount = 0;
1540b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                        buffer.mRaw = NULL;
1541b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                        buffer.mNonContig = 0;
15429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                        break;
154389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                    }
154489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                }
15459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            }
15469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            oldSequence = newSequence;
1547c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
15489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // Keep the extra references
15499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            proxy = mProxy;
15509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            iMem = mCblkMemory;
15519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
1552b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            if (mState == STATE_STOPPING) {
1553b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                status = -EINTR;
1554b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                buffer.mFrameCount = 0;
1555b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                buffer.mRaw = NULL;
1556b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                buffer.mNonContig = 0;
1557b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                break;
1558b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            }
1559b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
15609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // Non-blocking if track is stopped or paused
15619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (mState != STATE_ACTIVE) {
15629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                requested = &ClientProxy::kNonBlocking;
156389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
156489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
15659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }   // end of lock scope
1566c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
15679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        buffer.mFrameCount = audioBuffer->frameCount;
15689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // FIXME starts the requested timeout and elapsed over from scratch
15699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        status = proxy->obtainBuffer(&buffer, requested, elapsed);
157089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
15719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
157289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
15739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    audioBuffer->frameCount = buffer.mFrameCount;
1574abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung    audioBuffer->size = buffer.mFrameCount * mFrameSize;
15759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    audioBuffer->raw = buffer.mRaw;
15769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (nonContig != NULL) {
15779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        *nonContig = buffer.mNonContig;
157889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
15799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return status;
158089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
158189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
158254a8a456c2b4b382b3ffe3d99e40703df79c0717Glenn Kastenvoid AudioTrack::releaseBuffer(const Buffer* audioBuffer)
158389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
15843f02be2ceeaa4b67dc0b1a81aebcfa049276fad8Glenn Kasten    // FIXME add error checking on mode, by adding an internal version
15859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mTransfer == TRANSFER_SHARED) {
15869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return;
15879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
15889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
1589abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung    size_t stepCount = audioBuffer->size / mFrameSize;
15909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (stepCount == 0) {
15919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return;
15929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
15939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
15949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    Proxy::Buffer buffer;
15959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    buffer.mFrameCount = stepCount;
15969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    buffer.mRaw = audioBuffer->raw;
1597e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
15981703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
1599200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    mReleased += stepCount;
16009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mInUnderrun = false;
16019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy->releaseBuffer(&buffer);
16029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
16039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // restart track if it was disabled by audioflinger due to previous underrun
16049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mState == STATE_ACTIVE) {
16059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audio_track_cblk_t* cblk = mCblk;
160696f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten        if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
1607c5a17425986b4ce3384e6956762c86018b49c4a0Glenn Kasten            ALOGW("releaseBuffer() track %p disabled due to previous underrun, restarting", this);
16089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // FIXME ignoring status
1609df839841d2db4cb8e2acb10205b3942622b3e7a2Eric Laurent            mAudioTrack->start();
1610df839841d2db4cb8e2acb10205b3942622b3e7a2Eric Laurent        }
1611df839841d2db4cb8e2acb10205b3942622b3e7a2Eric Laurent    }
161289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
161389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
161489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
161589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1616720ad9ddb2ac6b55b0dfbfcd2d8360151d8ac427Jean-Michel Trivissize_t AudioTrack::write(const void* buffer, size_t userSize, bool blocking)
161789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
1618d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    if (mTransfer != TRANSFER_SYNC) {
1619d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        return INVALID_OPERATION;
1620d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten    }
162189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1622ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (isDirect()) {
1623ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        AutoMutex lock(mLock);
1624ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        int32_t flags = android_atomic_and(
1625ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                            ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END),
1626ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                            &mCblk->mFlags);
1627ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        if (flags & CBLK_INVALID) {
1628ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            return DEAD_OBJECT;
1629ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        }
1630ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    }
1631ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent
16329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
163399e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten        // Sanity-check: user is most-likely passing an error code, and it would
163499e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten        // make the return value ambiguous (actualSize vs error).
1635377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize);
163689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
163789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
163889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
16399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t written = 0;
164089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Buffer audioBuffer;
164189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
16429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    while (userSize >= mFrameSize) {
16439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer.frameCount = userSize / mFrameSize;
1644c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
1645720ad9ddb2ac6b55b0dfbfcd2d8360151d8ac427Jean-Michel Trivi        status_t err = obtainBuffer(&audioBuffer,
1646720ad9ddb2ac6b55b0dfbfcd2d8360151d8ac427Jean-Michel Trivi                blocking ? &ClientProxy::kForever : &ClientProxy::kNonBlocking);
164789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (err < 0) {
16489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (written > 0) {
164989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                break;
1650d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            }
165189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            return ssize_t(err);
165289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
165389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1654ae4b879d04c4b07c62241a8f507c8dce11e628e4Glenn Kasten        size_t toWrite = audioBuffer.size;
1655abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung        memcpy(audioBuffer.i8, buffer, toWrite);
16569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        buffer = ((const char *) buffer) + toWrite;
165789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        userSize -= toWrite;
165889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        written += toWrite;
165989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
166089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        releaseBuffer(&audioBuffer);
16619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
166289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
166389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return written;
166489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
166589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
166689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
166789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
16687c7be1e05634d96d08210efb4bdeb012ffba440dGlenn Kastennsecs_t AudioTrack::processAudioBuffer()
166989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
1670fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten    // Currently the AudioTrack thread is not created if there are no callbacks.
1671fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten    // Would it ever make sense to run the thread, even without callbacks?
1672fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten    // If so, then replace this by checks at each use for mCbf != NULL.
1673fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten    LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1674fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten
16751703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    mLock.lock();
1676a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten    if (mAwaitBoost) {
1677a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten        mAwaitBoost = false;
1678a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten        mLock.unlock();
1679a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten        static const int32_t kMaxTries = 5;
1680a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten        int32_t tryCounter = kMaxTries;
1681a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten        uint32_t pollUs = 10000;
1682a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten        do {
1683a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten            int policy = sched_getscheduler(0);
1684a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten            if (policy == SCHED_FIFO || policy == SCHED_RR) {
1685a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten                break;
1686a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten            }
1687a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten            usleep(pollUs);
1688a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten            pollUs <<= 1;
1689a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten        } while (tryCounter-- > 0);
1690a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten        if (tryCounter < 0) {
1691a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten            ALOGE("did not receive expected priority boost on time");
1692a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten        }
1693b0dfd4613225a3b2a17bdf8d85e89a4b04d65ef3Glenn Kasten        // Run again immediately
1694b0dfd4613225a3b2a17bdf8d85e89a4b04d65ef3Glenn Kasten        return 0;
1695a07f17ca46db04c9d5d9e7d6b2878db59ca2b9eaGlenn Kasten    }
16961703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
16979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Can only reference mCblk while locked
16989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    int32_t flags = android_atomic_and(
169996f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten        ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
1700a47f3165f53c8e8fb8907a94de7417e2c3047eebGlenn Kasten
17019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Check for track invalidation
17029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (flags & CBLK_INVALID) {
1703b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1704b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        // AudioSystem cache. We should not exit here but after calling the callback so
1705b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        // that the upper layers can recreate the track
1706ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        if (!isOffloadedOrDirect_l() || (mSequence == mObservedSequence)) {
1707f1063e2659cad01ec334a265c31734c63c319cc4Lajos Molnar            status_t status __unused = restoreTrack_l("processAudioBuffer");
1708f1063e2659cad01ec334a265c31734c63c319cc4Lajos Molnar            // FIXME unused status
170953c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung            // after restoration, continue below to make sure that the loop and buffer events
171053c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung            // are notified because they have been cleared from mCblk->mFlags above.
1711b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        }
171289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
1713c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
1714b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    bool waitStreamEnd = mState == STATE_STOPPING;
17159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    bool active = mState == STATE_ACTIVE;
171689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
17179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
17189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    bool newUnderrun = false;
17199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (flags & CBLK_UNDERRUN) {
17209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#if 0
17219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // Currently in shared buffer mode, when the server reaches the end of buffer,
17229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // the track stays active in continuous underrun state.  It's up to the application
17239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // to pause or stop the track, or set the position to a new offset within buffer.
17249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // This was some experimental code to auto-pause on underrun.   Keeping it here
17259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
17269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (mTransfer == TRANSFER_SHARED) {
17279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mState = STATE_PAUSED;
17289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            active = false;
17299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
17309f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#endif
17319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (!mInUnderrun) {
17329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mInUnderrun = true;
17339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            newUnderrun = true;
17349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
173589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
173689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
17379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Get current position of server
173890e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    Modulo<uint32_t> position(updateAndGetPosition_l());
17399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
174089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // Manage marker callback
17419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    bool markerReached = false;
174290e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    Modulo<uint32_t> markerPosition(mMarkerPosition);
174390e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    // uses 32 bit wraparound for comparison with position.
174490e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    if (!mMarkerReached && markerPosition.value() > 0 && position >= markerPosition) {
17459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mMarkerReached = markerReached = true;
17469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
17479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
17489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Determine number of new position callback(s) that will be needed, while locked
17499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t newPosCount = 0;
175090e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    Modulo<uint32_t> newPosition(mNewPosition);
175190e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    uint32_t updatePeriod = mUpdatePeriod;
17529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // FIXME fails for wraparound, need 64 bits
17539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (updatePeriod > 0 && position >= newPosition) {
175490e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        newPosCount = ((position - newPosition).value() / updatePeriod) + 1;
17559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mNewPosition += updatePeriod * newPosCount;
17569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
17579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
17589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Cache other fields that will be needed soon
17599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    uint32_t sampleRate = mSampleRate;
17605a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia    float speed = mPlaybackRate.mSpeed;
1761a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    const uint32_t notificationFrames = mNotificationFramesAct;
17629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mRefreshRemaining) {
17639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mRefreshRemaining = false;
17649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mRemainingFrames = notificationFrames;
17659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mRetryOnPartialBuffer = false;
17669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
17679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t misalignment = mProxy->getMisalignment();
1768b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    uint32_t sequence = mSequence;
176996f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten    sp<AudioTrackClientProxy> proxy = mProxy;
17709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
177153c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    // Determine the number of new loop callback(s) that will be needed, while locked.
177253c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    int loopCountNotifications = 0;
177353c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    uint32_t loopPeriod = 0; // time in frames for next EVENT_LOOP_END or EVENT_BUFFER_END
177453c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung
177553c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    if (mLoopCount > 0) {
177653c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        int loopCount;
177753c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        size_t bufferPosition;
177853c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        mStaticProxy->getBufferPositionAndLoopCount(&bufferPosition, &loopCount);
177953c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        loopPeriod = ((loopCount > 0) ? mLoopEnd : mFrameCount) - bufferPosition;
178053c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        loopCountNotifications = min(mLoopCountNotified - loopCount, kMaxLoopCountNotifications);
178153c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        mLoopCountNotified = loopCount; // discard any excess notifications
178253c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    } else if (mLoopCount < 0) {
178353c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        // FIXME: We're not accurate with notification count and position with infinite looping
178453c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        // since loopCount from server side will always return -1 (we could decrement it).
178553c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        size_t bufferPosition = mStaticProxy->getBufferPosition();
178653c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        loopCountNotifications = int((flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) != 0);
178753c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        loopPeriod = mLoopEnd - bufferPosition;
178853c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    } else if (/* mLoopCount == 0 && */ mSharedBuffer != 0) {
178953c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        size_t bufferPosition = mStaticProxy->getBufferPosition();
179053c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        loopPeriod = mFrameCount - bufferPosition;
179153c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    }
179253c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung
17939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // These fields don't need to be cached, because they are assigned only by set():
1794abdb990953ffe94a9dc544aea0bed17ef7d5f484Andy Hung    //     mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFlags
17959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // mFlags is also assigned by createTrack_l(), but not the bit we care about.
17969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
17979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mLock.unlock();
17989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
1799a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    // get anchor time to account for callbacks.
1800a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    const nsecs_t timeBeforeCallbacks = systemTime();
1801a7f03353d5f172016f324e2a01f301cca6794152Andy Hung
1802b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    if (waitStreamEnd) {
1803a7f03353d5f172016f324e2a01f301cca6794152Andy Hung        // FIXME:  Instead of blocking in proxy->waitStreamEndDone(), Callback thread
1804a7f03353d5f172016f324e2a01f301cca6794152Andy Hung        // should wait on proxy futex and handle CBLK_STREAM_END_DONE within this function
1805a7f03353d5f172016f324e2a01f301cca6794152Andy Hung        // (and make sure we don't callback for more data while we're stopping).
1806a7f03353d5f172016f324e2a01f301cca6794152Andy Hung        // This helps with position, marker notifications, and track invalidation.
1807b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        struct timespec timeout;
1808b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1809b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        timeout.tv_nsec = 0;
1810b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
181196f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten        status_t status = proxy->waitStreamEndDone(&timeout);
1812b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        switch (status) {
1813b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        case NO_ERROR:
1814b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        case DEAD_OBJECT:
1815b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        case TIMED_OUT:
181639609a0a6d95d003b0f237e6c139b070b4df7071Andy Hung            if (status != DEAD_OBJECT) {
181739609a0a6d95d003b0f237e6c139b070b4df7071Andy Hung                // for DEAD_OBJECT, we do not send a EVENT_STREAM_END after stop();
181839609a0a6d95d003b0f237e6c139b070b4df7071Andy Hung                // instead, the application should handle the EVENT_NEW_IAUDIOTRACK.
181939609a0a6d95d003b0f237e6c139b070b4df7071Andy Hung                mCbf(EVENT_STREAM_END, mUserData, NULL);
182039609a0a6d95d003b0f237e6c139b070b4df7071Andy Hung            }
182196f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten            {
182296f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten                AutoMutex lock(mLock);
182396f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten                // The previously assigned value of waitStreamEnd is no longer valid,
182496f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten                // since the mutex has been unlocked and either the callback handler
182596f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten                // or another thread could have re-started the AudioTrack during that time.
182696f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten                waitStreamEnd = mState == STATE_STOPPING;
182796f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten                if (waitStreamEnd) {
182896f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten                    mState = STATE_STOPPED;
1829c2813e568aa476e03d159529c0af28c99536db8dAndy Hung                    mReleased = 0;
1830b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                }
1831b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            }
183296f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten            if (waitStreamEnd && status != DEAD_OBJECT) {
183396f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten               return NS_INACTIVE;
183496f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten            }
183596f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten            break;
1836b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        }
183796f04886d1c1bfbc422e2be033ea66be83e42441Glenn Kasten        return 0;
1838b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
1839b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
18409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // perform callbacks while unlocked
18419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (newUnderrun) {
18429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mCbf(EVENT_UNDERRUN, mUserData, NULL);
18439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
184453c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung    while (loopCountNotifications > 0) {
18459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mCbf(EVENT_LOOP_END, mUserData, NULL);
184653c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung        --loopCountNotifications;
18479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
18489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (flags & CBLK_BUFFER_END) {
18499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mCbf(EVENT_BUFFER_END, mUserData, NULL);
18509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
18519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (markerReached) {
18529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mCbf(EVENT_MARKER, mUserData, &markerPosition);
18539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
18549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    while (newPosCount > 0) {
185590e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        size_t temp = newPosition.value(); // FIXME size_t != uint32_t
18569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mCbf(EVENT_NEW_POS, mUserData, &temp);
18579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        newPosition += updatePeriod;
18589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        newPosCount--;
18599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
1860b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
18619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mObservedSequence != sequence) {
18629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mObservedSequence = sequence;
18639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
1864b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        // for offloaded tracks, just wait for the upper layers to recreate the track
1865ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        if (isOffloadedOrDirect()) {
1866b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            return NS_INACTIVE;
1867b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        }
186889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
186989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
18709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // if inactive, then don't run me again until re-started
18719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (!active) {
18729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return NS_INACTIVE;
187389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
187489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
18759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Compute the estimated time until the next timed event (position, markers, loops)
18769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // FIXME only for non-compressed audio
18779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    uint32_t minFrames = ~0;
18789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (!markerReached && position < markerPosition) {
187990e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        minFrames = (markerPosition - position).value();
18809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
18819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (loopPeriod > 0 && loopPeriod < minFrames) {
18822d85f097d653b21d1ff2c34f0b732c674d20ccc2Andy Hung        // loopPeriod is already adjusted for actual position.
18839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        minFrames = loopPeriod;
18849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
18852d85f097d653b21d1ff2c34f0b732c674d20ccc2Andy Hung    if (updatePeriod > 0) {
188690e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        minFrames = min(minFrames, (newPosition - position).value());
188789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
188889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
18899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // If > 0, poll periodically to recover from a stuck server.  A good value is 2.
18909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    static const uint32_t kPoll = 0;
18919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
18929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        minFrames = kPoll * notificationFrames;
18932267ba18d0d2b2d4bd7f5411821ad89ac2659a88Eric Laurent    }
18942267ba18d0d2b2d4bd7f5411821ad89ac2659a88Eric Laurent
1895a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1896a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    static const nsecs_t kWaitPeriodNs = WAIT_PERIOD_MS * 1000000LL;
1897a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    const nsecs_t timeAfterCallbacks = systemTime();
1898a7f03353d5f172016f324e2a01f301cca6794152Andy Hung
18999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Convert frame units to time units
19009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    nsecs_t ns = NS_WHENEVER;
19019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (minFrames != (uint32_t) ~0) {
1902a7f03353d5f172016f324e2a01f301cca6794152Andy Hung        ns = framesToNanoseconds(minFrames, sampleRate, speed) + kWaitPeriodNs;
1903a7f03353d5f172016f324e2a01f301cca6794152Andy Hung        ns -= (timeAfterCallbacks - timeBeforeCallbacks);  // account for callback time
1904a7f03353d5f172016f324e2a01f301cca6794152Andy Hung        // TODO: Should we warn if the callback time is too long?
1905a7f03353d5f172016f324e2a01f301cca6794152Andy Hung        if (ns < 0) ns = 0;
19069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
19079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
19089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // If not supplying data by EVENT_MORE_DATA, then we're done
19099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mTransfer != TRANSFER_CALLBACK) {
19109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return ns;
19119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
191289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1913a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    // EVENT_MORE_DATA callback handling.
1914a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    // Timing for linear pcm audio data formats can be derived directly from the
1915a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    // buffer fill level.
1916a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    // Timing for compressed data is not directly available from the buffer fill level,
1917a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    // rather indirectly from waiting for blocking mode callbacks or waiting for obtain()
1918a7f03353d5f172016f324e2a01f301cca6794152Andy Hung    // to return a certain fill level.
1919a7f03353d5f172016f324e2a01f301cca6794152Andy Hung
19209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    struct timespec timeout;
19219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    const struct timespec *requested = &ClientProxy::kForever;
19229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (ns != NS_WHENEVER) {
19239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        timeout.tv_sec = ns / 1000000000LL;
19249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        timeout.tv_nsec = ns % 1000000000LL;
19259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
19269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &timeout;
19279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
19289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
19299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    while (mRemainingFrames > 0) {
1930c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
19319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        Buffer audioBuffer;
19329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer.frameCount = mRemainingFrames;
19339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t nonContig;
19349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
19359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
193634fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn                "obtainBuffer() err=%d frameCount=%zu", err, audioBuffer.frameCount);
19379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &ClientProxy::kNonBlocking;
19389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t avail = audioBuffer.frameCount + nonContig;
193934fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn        ALOGV("obtainBuffer(%u) returned %zu = %zu + %zu err %d",
1940b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
19419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (err != NO_ERROR) {
1942b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald            if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1943b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald                    (isOffloaded() && (err == DEAD_OBJECT))) {
1944606fbc1030d686bf1f8dc03d14a79342aca2ca41Glenn Kasten                // FIXME bug 25195759
1945606fbc1030d686bf1f8dc03d14a79342aca2ca41Glenn Kasten                return 1000000;
194689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
19479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            ALOGE("Error %d obtaining an audio buffer, giving up.", err);
19489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return NS_NEVER;
194989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
19509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
1951fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk        if (mRetryOnPartialBuffer && audio_has_proportional_frames(mFormat)) {
19529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mRetryOnPartialBuffer = false;
19539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (avail < mRemainingFrames) {
1954a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                if (ns > 0) { // account for obtain time
1955a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                    const nsecs_t timeNow = systemTime();
1956a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                    ns = max((nsecs_t)0, ns - (timeNow - timeAfterCallbacks));
1957a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                }
1958a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                nsecs_t myns = framesToNanoseconds(mRemainingFrames - avail, sampleRate, speed);
1959a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                if (ns < 0 /* NS_WHENEVER */ || myns < ns) {
19609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                    ns = myns;
19619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                }
19629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                return ns;
19639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            }
1964d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        }
196589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
196689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        size_t reqSize = audioBuffer.size;
196789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
19689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t writtenSize = audioBuffer.size;
196989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
197089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Sanity check on returned size
19719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
197234fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn            ALOGE("EVENT_MORE_DATA requested %zu bytes but callback returned %zd bytes",
197334fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn                    reqSize, ssize_t(writtenSize));
19749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return NS_NEVER;
19759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
19769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
19779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (writtenSize == 0) {
19788555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            // The callback is done filling buffers
19798555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            // Keep this thread going to handle timed events and
19808555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            // still try to get more data in intervals of WAIT_PERIOD_MS
19818555d0867c3e8fe6cc5c7ad40af557fe6b92fa72The Android Open Source Project            // but don't just loop and block the CPU, so wait
1982a7f03353d5f172016f324e2a01f301cca6794152Andy Hung
1983a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // mCbf(EVENT_MORE_DATA, ...) might either
1984a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // (1) Block until it can fill the buffer, returning 0 size on EOS.
1985a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // (2) Block until it can fill the buffer, returning 0 data (silence) on EOS.
1986a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // (3) Return 0 size when no data is available, does not wait for more data.
1987a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            //
1988a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // (1) and (2) occurs with AudioPlayer/AwesomePlayer; (3) occurs with NuPlayer.
1989a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // We try to compute the wait time to avoid a tight sleep-wait cycle,
1990a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // especially for case (3).
1991a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            //
1992a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // The decision to support (1) and (2) affect the sizing of mRemainingFrames
1993a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // and this loop; whereas for case (3) we could simply check once with the full
1994a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            // buffer size and skip the loop entirely.
1995a7f03353d5f172016f324e2a01f301cca6794152Andy Hung
1996a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            nsecs_t myns;
1997fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk            if (audio_has_proportional_frames(mFormat)) {
1998a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                // time to wait based on buffer occupancy
1999a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                const nsecs_t datans = mRemainingFrames <= avail ? 0 :
2000a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                        framesToNanoseconds(mRemainingFrames - avail, sampleRate, speed);
2001a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                // audio flinger thread buffer size (TODO: adjust for fast tracks)
2002a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                const nsecs_t afns = framesToNanoseconds(mAfFrameCount, mAfSampleRate, speed);
2003a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                // add a half the AudioFlinger buffer time to avoid soaking CPU if datans is 0.
2004a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                myns = datans + (afns / 2);
2005a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            } else {
2006a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                // FIXME: This could ping quite a bit if the buffer isn't full.
2007a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                // Note that when mState is stopping we waitStreamEnd, so it never gets here.
2008a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                myns = kWaitPeriodNs;
2009a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            }
2010a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            if (ns > 0) { // account for obtain and callback time
2011a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                const nsecs_t timeNow = systemTime();
2012a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                ns = max((nsecs_t)0, ns - (timeNow - timeAfterCallbacks));
2013a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            }
2014a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            if (ns < 0 /* NS_WHENEVER */ || myns < ns) {
2015a7f03353d5f172016f324e2a01f301cca6794152Andy Hung                ns = myns;
2016a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            }
2017a7f03353d5f172016f324e2a01f301cca6794152Andy Hung            return ns;
2018d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        }
201989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2020138d6f9b0b77d94773a8a29d3c5bdb78f38ee142Glenn Kasten        size_t releasedFrames = writtenSize / mFrameSize;
20219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer.frameCount = releasedFrames;
20229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mRemainingFrames -= releasedFrames;
20239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (misalignment >= releasedFrames) {
20249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            misalignment -= releasedFrames;
20259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        } else {
20269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            misalignment = 0;
20279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
202889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
202989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        releaseBuffer(&audioBuffer);
203089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
20319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
20329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // if callback doesn't like to accept the full chunk
20339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (writtenSize < reqSize) {
20349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            continue;
20359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
20369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
20379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // There could be enough non-contiguous frames available to satisfy the remaining request
20389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (mRemainingFrames <= nonContig) {
20399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            continue;
20409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
20419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
20429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#if 0
20439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
20449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // sum <= notificationFrames.  It replaces that series by at most two EVENT_MORE_DATA
20459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // that total to a sum == notificationFrames.
20469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (0 < misalignment && misalignment <= mRemainingFrames) {
20479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mRemainingFrames = misalignment;
20488edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            return ((double)mRemainingFrames * 1100000000) / ((double)sampleRate * speed);
20499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
20509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#endif
20519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
205289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
20539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mRemainingFrames = notificationFrames;
20549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mRetryOnPartialBuffer = true;
20559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
20569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // A lot has transpired since ns was calculated, so run again immediately and re-calculate
20579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return 0;
205889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
205989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
20609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenstatus_t AudioTrack::restoreTrack_l(const char *from)
20611703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent{
2062b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
2063ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent          isOffloadedOrDirect_l() ? "Offloaded or Direct" : "PCM", from);
20649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    ++mSequence;
20651703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
2066a47f3165f53c8e8fb8907a94de7417e2c3047eebGlenn Kasten    // refresh the audio configuration cache in this process to make sure we get new
2067d2d089fc86c62843992e7d5b371ee9227189a1e6Glenn Kasten    // output parameters and new IAudioFlinger in createTrack_l()
2068a47f3165f53c8e8fb8907a94de7417e2c3047eebGlenn Kasten    AudioSystem::clearAudioConfigCache();
2069a47f3165f53c8e8fb8907a94de7417e2c3047eebGlenn Kasten
2070faeb0f291330134dc4468359a36e099aae508449Ronghua Wu    if (isOffloadedOrDirect_l() || mDoNotReconnect) {
20711f1db8356b599bc40703c907fb69e6e539343532Andy Hung        // FIXME re-creation of offloaded and direct tracks is not yet implemented;
20721f1db8356b599bc40703c907fb69e6e539343532Andy Hung        // reconsider enabling for linear PCM encodings when position can be preserved.
2073b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        return DEAD_OBJECT;
2074b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
2075b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald
20762812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk    // Save so we can return count since creation.
20772812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk    mUnderrunCountOffset = getUnderrunCount_l();
20782812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk
2079200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    // save the old static buffer position
20804ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    size_t bufferPosition = 0;
20814ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    int loopCount = 0;
20824ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    if (mStaticProxy != 0) {
20834ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung        mStaticProxy->getBufferPositionAndLoopCount(&bufferPosition, &loopCount);
20844ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung    }
2085200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten
2086200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    // If a new IAudioTrack is successfully created, createTrack_l() will modify the
2087a47f3165f53c8e8fb8907a94de7417e2c3047eebGlenn Kasten    // following member variables: mAudioTrack, mCblkMemory and mCblk.
2088200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    // It will also delete the strong references on previous IAudioTrack and IMemory.
2089200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    // If a new IAudioTrack cannot be created, the previous (dead) instance will be left intact.
2090ae4b879d04c4b07c62241a8f507c8dce11e628e4Glenn Kasten    status_t result = createTrack_l();
2091cc21e4f1e41dfa17e7e2bef995fcd22c45f6bcd0Eric Laurent
2092a47f3165f53c8e8fb8907a94de7417e2c3047eebGlenn Kasten    if (result == NO_ERROR) {
2093d7bd69e4d1e74f25413721804d5290c53ea87f2bAndy Hung        // take the frames that will be lost by track recreation into account in saved position
2094d7bd69e4d1e74f25413721804d5290c53ea87f2bAndy Hung        // For streaming tracks, this is the amount we obtained from the user/client
2095d7bd69e4d1e74f25413721804d5290c53ea87f2bAndy Hung        // (not the number actually consumed at the server - those are already lost).
2096d7bd69e4d1e74f25413721804d5290c53ea87f2bAndy Hung        if (mStaticProxy == 0) {
2097d7bd69e4d1e74f25413721804d5290c53ea87f2bAndy Hung            mPosition = mReleased;
2098d7bd69e4d1e74f25413721804d5290c53ea87f2bAndy Hung        }
20994ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung        // Continue playback from last known position and restore loop.
21004ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung        if (mStaticProxy != 0) {
21014ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung            if (loopCount != 0) {
21024ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung                mStaticProxy->setBufferPositionAndLoop(bufferPosition,
21034ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung                        mLoopStart, mLoopEnd, loopCount);
21044ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung            } else {
21054ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung                mStaticProxy->setBufferPosition(bufferPosition);
210653c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung                if (bufferPosition == mFrameCount) {
210753c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung                    ALOGD("restoring track at end of static buffer");
210853c3b5fc1afe162a8669cb3d27d6bb14e1847e39Andy Hung                }
21091703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent            }
21101703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        }
21119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (mState == STATE_ACTIVE) {
2112a47f3165f53c8e8fb8907a94de7417e2c3047eebGlenn Kasten            result = mAudioTrack->start();
21131703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        }
21141703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    }
21159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (result != NO_ERROR) {
21169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGW("restoreTrack_l() failed status %d", result);
21179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mState = STATE_STOPPED;
2118c2813e568aa476e03d159529c0af28c99536db8dAndy Hung        mReleased = 0;
21191703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    }
21201703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
21211703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    return result;
21221703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent}
21231703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
212490e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy HungModulo<uint32_t> AudioTrack::updateAndGetPosition_l()
2125200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten{
2126200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    // This is the sole place to read server consumed frames
212790e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    Modulo<uint32_t> newServer(mProxy->getPosition());
212890e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    const int32_t delta = (newServer - mServer).signedValue();
2129200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    // TODO There is controversy about whether there can be "negative jitter" in server position.
2130200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    //      This should be investigated further, and if possible, it should be addressed.
2131200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    //      A more definite failure mode is infrequent polling by client.
2132200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    //      One could call (void)getPosition_l() in releaseBuffer(),
2133200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    //      so mReleased and mPosition are always lock-step as best possible.
2134200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    //      That should ensure delta never goes negative for infrequent polling
2135200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    //      unless the server has more than 2^31 frames in its buffer,
2136200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    //      in which case the use of uint32_t for these counters has bigger issues.
213790e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    ALOGE_IF(delta < 0,
213890e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung            "detected illegal retrograde motion by the server: mServer advanced by %d",
213990e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung            delta);
2140039c27aeaee809182fc1491311ca6dcbc1ec441bChad Brubaker    mServer = newServer;
214190e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    if (delta > 0) { // avoid retrograde
214290e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        mPosition += delta;
214390e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    }
214490e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung    return mPosition;
2145200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten}
2146200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten
21478edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hungbool AudioTrack::isSampleRateSpeedAllowed_l(uint32_t sampleRate, float speed) const
21488edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung{
21498edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    // applicable for mixing tracks only (not offloaded or direct)
21508edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    if (mStaticProxy != 0) {
21518edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung        return true; // static tracks do not have issues with buffer sizing.
21528edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    }
21538edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    const size_t minFrameCount =
21549f9e21ed9a342e0ca945818bdc6c0c4fb3bfb9bdAndy Hung            calculateMinFrameCount(mAfLatency, mAfFrameCount, mAfSampleRate, sampleRate, speed);
21558edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    ALOGV("isSampleRateSpeedAllowed_l mFrameCount %zu  minFrameCount %zu",
21568edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung            mFrameCount, minFrameCount);
21578edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    return mFrameCount >= minFrameCount;
21588edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung}
21598edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung
2160ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgeraldstatus_t AudioTrack::setParameters(const String8& keyValuePairs)
2161ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald{
2162ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    AutoMutex lock(mLock);
216353cec22821072719ee02c856e9ac2dda2496c570Glenn Kasten    return mAudioTrack->setParameters(keyValuePairs);
2164ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald}
2165ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
2166ce70374bf105f8a6160bf5dd70affea012b2a464Glenn Kastenstatus_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
2167ce70374bf105f8a6160bf5dd70affea012b2a464Glenn Kasten{
216853cec22821072719ee02c856e9ac2dda2496c570Glenn Kasten    AutoMutex lock(mLock);
21691b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk
21701b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk    bool previousTimestampValid = mPreviousTimestampValid;
21711b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk    // Set false here to cover all the error return cases.
21721b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk    mPreviousTimestampValid = false;
21731b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk
21747f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    switch (mState) {
21757f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    case STATE_ACTIVE:
21767f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    case STATE_PAUSED:
21777f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        break; // handle below
21787f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    case STATE_FLUSHED:
21797f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    case STATE_STOPPED:
21807f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        return WOULD_BLOCK;
21817f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    case STATE_STOPPING:
21827f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    case STATE_PAUSED_STOPPING:
21837f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        if (!isOffloaded_l()) {
21847f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            return INVALID_OPERATION;
21857f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        }
21867f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        break; // offloaded tracks handled below
21877f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    default:
21887f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        LOG_ALWAYS_FATAL("Invalid mState in getTimestamp(): %d", mState);
21897f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        break;
2190fe346c707f59d763ded93bc3d27b51f0c0408258Glenn Kasten    }
21917f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
2192275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent    if (mCblk->mFlags & CBLK_INVALID) {
21936653c935d2518a713587b3887ca09aa09ebfa7fdAndy Hung        const status_t status = restoreTrack_l("getTimestamp");
21946653c935d2518a713587b3887ca09aa09ebfa7fdAndy Hung        if (status != OK) {
21956653c935d2518a713587b3887ca09aa09ebfa7fdAndy Hung            // per getTimestamp() API doc in header, we return DEAD_OBJECT here,
21966653c935d2518a713587b3887ca09aa09ebfa7fdAndy Hung            // recommending that the track be recreated.
21976653c935d2518a713587b3887ca09aa09ebfa7fdAndy Hung            return DEAD_OBJECT;
21986653c935d2518a713587b3887ca09aa09ebfa7fdAndy Hung        }
2199275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent    }
2200275e8e9de2e11b4b344f5a201f1f0e51fda02d9cEric Laurent
2201200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    // The presented frame count must always lag behind the consumed frame count.
2202200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten    // To avoid a race, read the presented frames first.  This ensures that presented <= consumed.
22036ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung
22046ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung    status_t status;
2205818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung    if (isOffloadedOrDirect_l()) {
22066ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        // use Binder to get timestamp
22076ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        status = mAudioTrack->getTimestamp(timestamp);
22086ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung    } else {
22096ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        // read timestamp from shared memory
22106ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        ExtendedTimestamp ets;
22116ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        status = mProxy->getTimestamp(&ets);
22126ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        if (status == OK) {
22136ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung            status = ets.getBestTimestamp(&timestamp);
22146ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        }
22156ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        if (status == INVALID_OPERATION) {
22166ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung            status = WOULD_BLOCK;
22176ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung        }
22186ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung    }
22197f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    if (status != NO_ERROR) {
2220dfc34daba6d6cb923683d96689a0cb7c7006eee5Glenn Kasten        ALOGV_IF(status != WOULD_BLOCK, "getTimestamp error:%#x", status);
22217f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        return status;
22227f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    }
22237f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    if (isOffloadedOrDirect_l()) {
22247f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        if (isOffloaded_l() && (mState == STATE_PAUSED || mState == STATE_PAUSED_STOPPING)) {
22257f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            // use cached paused position in case another offloaded track is running.
22267f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            timestamp.mPosition = mPausedPosition;
22277f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            clock_gettime(CLOCK_MONOTONIC, &timestamp.mTime);
22287f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            return NO_ERROR;
22297f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        }
22307f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
22317f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        // Check whether a pending flush or stop has completed, as those commands may
2232c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung        // be asynchronous or return near finish or exhibit glitchy behavior.
2233c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung        //
2234c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung        // Originally this showed up as the first timestamp being a continuation of
2235c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung        // the previous song under gapless playback.
2236c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung        // However, we sometimes see zero timestamps, then a glitch of
2237c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung        // the previous song's position, and then correct timestamps afterwards.
22387f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        if (mStartUs != 0 && mSampleRate != 0) {
22397f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            static const int kTimeJitterUs = 100000; // 100 ms
22407f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            static const int k1SecUs = 1000000;
22417f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
22427f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            const int64_t timeNow = getNowUs();
22437f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
22447f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            if (timeNow < mStartUs + k1SecUs) { // within first second of starting
22457f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                const int64_t timestampTimeUs = convertTimespecToUs(timestamp.mTime);
22467f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                if (timestampTimeUs < mStartUs) {
22477f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                    return WOULD_BLOCK;  // stale timestamp time, occurs before start.
22487f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                }
22497f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                const int64_t deltaTimeUs = timestampTimeUs - mStartUs;
22508edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung                const int64_t deltaPositionByUs = (double)timestamp.mPosition * 1000000
22515a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia                        / ((double)mSampleRate * mPlaybackRate.mSpeed);
22527f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung
22537f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                if (deltaPositionByUs > deltaTimeUs + kTimeJitterUs) {
22547f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                    // Verify that the counter can't count faster than the sample rate
2255c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                    // since the start time.  If greater, then that means we may have failed
22567f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                    // to completely flush or stop the previous playing track.
2257c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                    ALOGW_IF(!mTimestampStartupGlitchReported,
2258c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                            "getTimestamp startup glitch detected"
22597f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                            " deltaTimeUs(%lld) deltaPositionUs(%lld) tsmPosition(%u)",
22607f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                            (long long)deltaTimeUs, (long long)deltaPositionByUs,
22617f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                            timestamp.mPosition);
2262c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                    mTimestampStartupGlitchReported = true;
2263c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                    if (previousTimestampValid
2264c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                            && mPreviousTimestamp.mPosition == 0 /* should be true if valid */) {
2265c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                        timestamp = mPreviousTimestamp;
2266c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                        mPreviousTimestampValid = true;
2267c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                        return NO_ERROR;
2268c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                    }
22697f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                    return WOULD_BLOCK;
22707f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung                }
2271c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                if (deltaPositionByUs != 0) {
2272c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                    mStartUs = 0; // don't check again, we got valid nonzero position.
2273c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                }
2274c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung            } else {
2275c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung                mStartUs = 0; // don't check again, start time expired.
22767f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung            }
2277c8e09c610fabc7390297ecb48e939acbdfe27325Andy Hung            mTimestampStartupGlitchReported = false;
22787f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung        }
22797f1bc8af1c46695191bf7e2aba6467f3616629c0Andy Hung    } else {
2280200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // Update the mapping between local consumed (mPosition) and server consumed (mServer)
2281200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        (void) updateAndGetPosition_l();
2282200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // Server consumed (mServer) and presented both use the same server time base,
2283200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // and server consumed is always >= presented.
2284200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // The delta between these represents the number of frames in the buffer pipeline.
2285200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // If this delta between these is greater than the client position, it means that
2286200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // actually presented is still stuck at the starting line (figuratively speaking),
2287200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // waiting for the first frame to go by.  So we can't report a valid timestamp yet.
228890e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        // Note: We explicitly use non-Modulo comparison here - potential wrap issue when
228990e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        // mPosition exceeds 32 bits.
229090e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        // TODO Remove when timestamp is updated to contain pipeline status info.
229190e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        const int32_t pipelineDepthInFrames = (mServer - timestamp.mPosition).signedValue();
229290e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        if (pipelineDepthInFrames > 0 /* should be true, but we check anyways */
229390e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung                && (uint32_t)pipelineDepthInFrames > mPosition.value()) {
2294200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten            return INVALID_OPERATION;
2295200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        }
2296200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // Convert timestamp position from server time base to client time base.
2297200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // TODO The following code should work OK now because timestamp.mPosition is 32-bit.
2298200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // But if we change it to 64-bit then this could fail.
229990e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        // Use Modulo computation here.
230090e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung        timestamp.mPosition = (mPosition - mServer + timestamp.mPosition).value();
2301200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // Immediately after a call to getPosition_l(), mPosition and
2302200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // mServer both represent the same frame position.  mPosition is
2303200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // in client's point of view, and mServer is in server's point of
2304200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // view.  So the difference between them is the "fudge factor"
2305200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // between client and server views due to stop() and/or new
2306200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // IAudioTrack.  And timestamp.mPosition is initially in server's
2307200092b7f21d2b98f30b800e79d152636f9ba225Glenn Kasten        // point of view, so we need to apply the same fudge factor to it.
2308fe346c707f59d763ded93bc3d27b51f0c0408258Glenn Kasten    }
23091b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk
23101b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk    // Prevent retrograde motion in timestamp.
23111b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk    // This is sometimes caused by erratic reports of the available space in the ALSA drivers.
23121b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk    if (status == NO_ERROR) {
23131b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk        if (previousTimestampValid) {
231490e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung#define TIME_TO_NANOS(time) ((int64_t)time.tv_sec * 1000000000 + time.tv_nsec)
231590e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung            const int64_t previousTimeNanos = TIME_TO_NANOS(mPreviousTimestamp.mTime);
231690e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung            const int64_t currentTimeNanos = TIME_TO_NANOS(timestamp.mTime);
23171b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk#undef TIME_TO_NANOS
23181b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk            if (currentTimeNanos < previousTimeNanos) {
23191b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk                ALOGW("retrograde timestamp time");
23201b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk                // FIXME Consider blocking this from propagating upwards.
23211b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk            }
23221b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk
23231b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk            // Looking at signed delta will work even when the timestamps
23241b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk            // are wrapping around.
232590e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung            int32_t deltaPosition = (Modulo<uint32_t>(timestamp.mPosition)
232690e8a97dd5c9d391d7a2ea6a2290ed976f928379Andy Hung                    - mPreviousTimestamp.mPosition).signedValue();
23271b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk            // position can bobble slightly as an artifact; this hides the bobble
23281b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk            static const int32_t MINIMUM_POSITION_DELTA = 8;
23294c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk            if (deltaPosition < 0) {
23304c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk                // Only report once per position instead of spamming the log.
23314c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk                if (!mRetrogradeMotionReported) {
23324c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk                    ALOGW("retrograde timestamp position corrected, %d = %u - %u",
23334c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk                            deltaPosition,
23344c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk                            timestamp.mPosition,
23354c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk                            mPreviousTimestamp.mPosition);
23364c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk                    mRetrogradeMotionReported = true;
23374c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk                }
23384c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk            } else {
23394c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk                mRetrogradeMotionReported = false;
23404c5a3675ed6b429dfaa2707acf2b3faf2808baf2Phil Burk            }
23411b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk            if (deltaPosition < MINIMUM_POSITION_DELTA) {
23421b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk                timestamp = mPreviousTimestamp;  // Use last valid timestamp.
23431b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk            }
23441b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk        }
23451b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk        mPreviousTimestamp = timestamp;
23461b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk        mPreviousTimestampValid = true;
23471b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk    }
23481b42097f38e72574ed853a35f4e8a66e4739c421Phil Burk
2349fe346c707f59d763ded93bc3d27b51f0c0408258Glenn Kasten    return status;
2350ce70374bf105f8a6160bf5dd70affea012b2a464Glenn Kasten}
2351ce70374bf105f8a6160bf5dd70affea012b2a464Glenn Kasten
2352ad3af3305f024bcbbd55c894a4995e449498e1baRichard FitzgeraldString8 AudioTrack::getParameters(const String8& keys)
2353ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald{
23542c6c5294388e251ebc1e00d6c8785190561c27fcGlenn Kasten    audio_io_handle_t output = getOutput();
2355142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
23562c6c5294388e251ebc1e00d6c8785190561c27fcGlenn Kasten        return AudioSystem::getParameters(output, keys);
2357b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    } else {
2358b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald        return String8::empty();
2359b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald    }
2360ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald}
2361ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
236223a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kastenbool AudioTrack::isOffloaded() const
236323a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kasten{
236423a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kasten    AutoMutex lock(mLock);
236523a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kasten    return isOffloaded_l();
236623a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kasten}
236723a7545c4de71e989c2d8ebf1d5b9dcf463c36a9Glenn Kasten
2368ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurentbool AudioTrack::isDirect() const
2369ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent{
2370ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    AutoMutex lock(mLock);
2371ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    return isDirect_l();
2372ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent}
2373ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent
2374ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurentbool AudioTrack::isOffloadedOrDirect() const
2375ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent{
2376ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    AutoMutex lock(mLock);
2377ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    return isOffloadedOrDirect_l();
2378ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent}
2379ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent
2380ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent
23817c7be1e05634d96d08210efb4bdeb012ffba440dGlenn Kastenstatus_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
238289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
238389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
238489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const size_t SIZE = 256;
238589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    char buffer[SIZE];
238689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    String8 result;
238789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
238889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(" AudioTrack::dump\n");
238985ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten    snprintf(buffer, 255, "  stream type(%d), left - right volume(%f, %f)\n", mStreamType,
2390877a0ac7e8677fa1cbd0e8565ad9d38ba0db5fc0Glenn Kasten            mVolume[AUDIO_INTERLEAVE_LEFT], mVolume[AUDIO_INTERLEAVE_RIGHT]);
239189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
2392377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT    snprintf(buffer, 255, "  format(%d), channel count(%d), frame count(%zu)\n", mFormat,
2393b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten            mChannelCount, mFrameCount);
239489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
23958edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung    snprintf(buffer, 255, "  sample rate(%u), speed(%f), status(%d)\n",
23965a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia            mSampleRate, mPlaybackRate.mSpeed, mStatus);
239789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
23989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    snprintf(buffer, 255, "  state(%d), latency (%d)\n", mState, mLatency);
239989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    result.append(buffer);
240089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    ::write(fd, result.string(), result.size());
240189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
240289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
240389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
24042812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burkuint32_t AudioTrack::getUnderrunCount() const
24052812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk{
24062812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk    AutoMutex lock(mLock);
24072812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk    return getUnderrunCount_l();
24082812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk}
24092812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk
24102812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burkuint32_t AudioTrack::getUnderrunCount_l() const
24112812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk{
24122812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk    return mProxy->getUnderrunCount() + mUnderrunCountOffset;
24132812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk}
24142812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk
24159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenuint32_t AudioTrack::getUnderrunFrames() const
24169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{
24179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex lock(mLock);
24189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return mProxy->getUnderrunFrames();
24199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten}
24209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
2421296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurentstatus_t AudioTrack::addAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback)
2422296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent{
2423296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (callback == 0) {
2424296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        ALOGW("%s adding NULL callback!", __FUNCTION__);
2425296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        return BAD_VALUE;
2426296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
2427296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    AutoMutex lock(mLock);
2428296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (mDeviceCallback == callback) {
2429296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        ALOGW("%s adding same callback!", __FUNCTION__);
2430296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        return INVALID_OPERATION;
2431296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
2432296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    status_t status = NO_ERROR;
2433296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (mOutput != AUDIO_IO_HANDLE_NONE) {
2434296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        if (mDeviceCallback != 0) {
2435296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            ALOGW("%s callback already present!", __FUNCTION__);
2436296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            AudioSystem::removeAudioDeviceCallback(mDeviceCallback, mOutput);
2437296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        }
2438296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        status = AudioSystem::addAudioDeviceCallback(callback, mOutput);
2439296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
2440296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    mDeviceCallback = callback;
2441296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    return status;
2442296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent}
2443296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
2444296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurentstatus_t AudioTrack::removeAudioDeviceCallback(
2445296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        const sp<AudioSystem::AudioDeviceCallback>& callback)
2446296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent{
2447296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (callback == 0) {
2448296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        ALOGW("%s removing NULL callback!", __FUNCTION__);
2449296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        return BAD_VALUE;
2450296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
2451296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    AutoMutex lock(mLock);
2452296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (mDeviceCallback != callback) {
2453296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        ALOGW("%s removing different callback!", __FUNCTION__);
2454296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        return INVALID_OPERATION;
2455296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
2456296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (mOutput != AUDIO_IO_HANDLE_NONE) {
2457296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        AudioSystem::removeAudioDeviceCallback(mDeviceCallback, mOutput);
2458296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
2459296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    mDeviceCallback = 0;
2460296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    return NO_ERROR;
2461296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent}
2462296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
24639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// =========================================================================
24649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
24657c7be1e05634d96d08210efb4bdeb012ffba440dGlenn Kastenvoid AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
24669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{
24679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    sp<AudioTrack> audioTrack = mAudioTrack.promote();
24689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (audioTrack != 0) {
24699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        AutoMutex lock(audioTrack->mLock);
24709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioTrack->mProxy->binderDied();
24719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
24729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten}
24739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
247489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// =========================================================================
247589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
247689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
2477598de6c701e989385eeffa7c5dfd61f0459a2631Glenn Kasten    : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
2478598de6c701e989385eeffa7c5dfd61f0459a2631Glenn Kasten      mIgnoreNextPausedInt(false)
24793acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten{
24803acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten}
24813acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
24823acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn KastenAudioTrack::AudioTrackThread::~AudioTrackThread()
248389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
248489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
248589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
248689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectbool AudioTrack::AudioTrackThread::threadLoop()
248789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
24883acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    {
24893acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        AutoMutex _l(mMyLock);
24903acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        if (mPaused) {
24913acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten            mMyCond.wait(mMyLock);
24923acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten            // caller will check for exitPending()
24933acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten            return true;
24943acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        }
2495598de6c701e989385eeffa7c5dfd61f0459a2631Glenn Kasten        if (mIgnoreNextPausedInt) {
2496598de6c701e989385eeffa7c5dfd61f0459a2631Glenn Kasten            mIgnoreNextPausedInt = false;
2497598de6c701e989385eeffa7c5dfd61f0459a2631Glenn Kasten            mPausedInt = false;
2498598de6c701e989385eeffa7c5dfd61f0459a2631Glenn Kasten        }
24995a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten        if (mPausedInt) {
25005a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten            if (mPausedNs > 0) {
25015a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten                (void) mMyCond.waitRelative(mMyLock, mPausedNs);
25025a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten            } else {
25035a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten                mMyCond.wait(mMyLock);
25045a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten            }
25059d2c78c4798ffd8c276c1bf0eaa0b34bc255a2daEric Laurent            mPausedInt = false;
25065a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten            return true;
25075a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten        }
25083acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
25097985dcb06e0c29d5cc12d0c0e17e03d5d863cf53Eric Laurent    if (exitPending()) {
25107985dcb06e0c29d5cc12d0c0e17e03d5d863cf53Eric Laurent        return false;
25117985dcb06e0c29d5cc12d0c0e17e03d5d863cf53Eric Laurent    }
25127c7be1e05634d96d08210efb4bdeb012ffba440dGlenn Kasten    nsecs_t ns = mReceiver.processAudioBuffer();
25139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    switch (ns) {
25149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case 0:
25159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return true;
25169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case NS_INACTIVE:
25175a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten        pauseInternal();
25189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return true;
25199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case NS_NEVER:
25209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return false;
25215a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten    case NS_WHENEVER:
25223c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung        // Event driven: call wake() when callback notifications conditions change.
25233c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung        ns = INT64_MAX;
25245a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten        // fall through
25259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    default:
252634fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn        LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %" PRId64, ns);
25275a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten        pauseInternal(ns);
25289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return true;
2529ca8b28013c0558a4a3323a1a0f58520277200086Glenn Kasten    }
253089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
253189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
25323acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kastenvoid AudioTrack::AudioTrackThread::requestExit()
25333acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten{
25343acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    // must be in this order to avoid a race condition
25353acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    Thread::requestExit();
2536598de6c701e989385eeffa7c5dfd61f0459a2631Glenn Kasten    resume();
25373acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten}
25383acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
25393acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kastenvoid AudioTrack::AudioTrackThread::pause()
25403acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten{
25413acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    AutoMutex _l(mMyLock);
25423acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    mPaused = true;
25433acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten}
25443acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
25453acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kastenvoid AudioTrack::AudioTrackThread::resume()
25463acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten{
25473acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    AutoMutex _l(mMyLock);
2548598de6c701e989385eeffa7c5dfd61f0459a2631Glenn Kasten    mIgnoreNextPausedInt = true;
25499d2c78c4798ffd8c276c1bf0eaa0b34bc255a2daEric Laurent    if (mPaused || mPausedInt) {
25503acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        mPaused = false;
25519d2c78c4798ffd8c276c1bf0eaa0b34bc255a2daEric Laurent        mPausedInt = false;
25523acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        mMyCond.signal();
25533acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
25543acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten}
25553acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
25563c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hungvoid AudioTrack::AudioTrackThread::wake()
25573c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung{
25583c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    AutoMutex _l(mMyLock);
2559a8d0890ca969ec40fdcf1cfcbeef85c0c379cfafAndy Hung    if (!mPaused) {
2560a8d0890ca969ec40fdcf1cfcbeef85c0c379cfafAndy Hung        // wake() might be called while servicing a callback - ignore the next
2561a8d0890ca969ec40fdcf1cfcbeef85c0c379cfafAndy Hung        // pause time and call processAudioBuffer.
25623c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung        mIgnoreNextPausedInt = true;
2563a8d0890ca969ec40fdcf1cfcbeef85c0c379cfafAndy Hung        if (mPausedInt && mPausedNs > 0) {
2564a8d0890ca969ec40fdcf1cfcbeef85c0c379cfafAndy Hung            // audio track is active and internally paused with timeout.
2565a8d0890ca969ec40fdcf1cfcbeef85c0c379cfafAndy Hung            mPausedInt = false;
2566a8d0890ca969ec40fdcf1cfcbeef85c0c379cfafAndy Hung            mMyCond.signal();
2567a8d0890ca969ec40fdcf1cfcbeef85c0c379cfafAndy Hung        }
25683c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung    }
25693c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung}
25703c09c78aabcb9d314dfb400258fb99a37c7b50c9Andy Hung
25715a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kastenvoid AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
25725a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten{
25735a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten    AutoMutex _l(mMyLock);
25745a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten    mPausedInt = true;
25755a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten    mPausedNs = ns;
25765a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten}
25775a6cd224d07c05b496b6aca050ce5ecf96f125afGlenn Kasten
257840bc906252974d0b389ae4a147232d0c9a97193fGlenn Kasten} // namespace android
2579