AudioRecord.cpp revision 4a0efb77198c69df711ab369ac482a42dbdfab07
189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project/*
289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Copyright 2008, 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 "AudioRecord"
2089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <sys/resource.h>
22868a6a357018e5872e064b7a13a9b891e2078962Glenn Kasten#include <binder/IPCThreadState.h>
2389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/AudioRecord.h>
2489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <utils/Log.h>
25868a6a357018e5872e064b7a13a9b891e2078962Glenn Kasten#include <private/media/AudioTrackShared.h>
261ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/IAudioFlinger.h>
2789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#define WAIT_PERIOD_MS          10
299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
3089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectnamespace android {
3115304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh// ---------------------------------------------------------------------------
3215304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh
3315304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh// static
3415304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yehstatus_t AudioRecord::getMinFrameCount(
35e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten        size_t* frameCount,
3615304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh        uint32_t sampleRate,
3758f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
38dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten        audio_channel_mask_t channelMask)
3915304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh{
409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (frameCount == NULL) {
419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return BAD_VALUE;
429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
4304cd0186305e2b59d23c9147787046c6662029ccGlenn Kasten
4404cd0186305e2b59d23c9147787046c6662029ccGlenn Kasten    // default to 0 in case of error
4504cd0186305e2b59d23c9147787046c6662029ccGlenn Kasten    *frameCount = 0;
4604cd0186305e2b59d23c9147787046c6662029ccGlenn Kasten
4715304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh    size_t size = 0;
48c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten    status_t status = AudioSystem::getInputBufferSize(sampleRate, format, channelMask, &size);
49c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten    if (status != NO_ERROR) {
50c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten        ALOGE("AudioSystem could not query the input buffer size; status %d", status);
5115304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh        return NO_INIT;
5215304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh    }
5315304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh
5415304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh    if (size == 0) {
553b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten        ALOGE("Unsupported configuration: sampleRate %u, format %d, channelMask %#x",
56dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten            sampleRate, format, channelMask);
5715304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh        return BAD_VALUE;
5815304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh    }
5915304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh
6015304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh    // We double the size of input buffer for ping pong use of record buffer.
6115304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh    size <<= 1;
6215304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh
63954315a10089fa3684ac94db5be77c6655c08fc0Glenn Kasten    // Assumes audio_is_linear_pcm(format)
64954315a10089fa3684ac94db5be77c6655c08fc0Glenn Kasten    uint32_t channelCount = popcount(channelMask);
65954315a10089fa3684ac94db5be77c6655c08fc0Glenn Kasten    size /= channelCount * audio_bytes_per_sample(format);
6615304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh
6715304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh    *frameCount = size;
6815304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh    return NO_ERROR;
6915304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh}
7089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
7189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// ---------------------------------------------------------------------------
7289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
7389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioRecord::AudioRecord()
74879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten    : mStatus(NO_INIT), mSessionId(0),
759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten      mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT)
7689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
7789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
7889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
7989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioRecord::AudioRecord(
80eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kasten        audio_source_t inputSource,
8189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t sampleRate,
8258f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
83624a7fcb377f2a40109c16de5109ae8ea1f67a69Glenn Kasten        audio_channel_mask_t channelMask,
8489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        int frameCount,
8589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        callback_t cbf,
8689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        void* user,
87be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        int notificationFrames,
889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        int sessionId,
8927f7b2a8fe899565487d8a326676a5f7d0a05a37Glenn Kasten        transfer_type transferType,
9027f7b2a8fe899565487d8a326676a5f7d0a05a37Glenn Kasten        audio_input_flags_t flags)
91879135196fd1c97deefc538c888037c56c2879a7Glenn Kasten    : mStatus(NO_INIT), mSessionId(0),
92e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten      mPreviousPriority(ANDROID_PRIORITY_NORMAL),
93e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten      mPreviousSchedulingGroup(SP_DEFAULT),
94e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten      mProxy(NULL)
9589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mStatus = set(inputSource, sampleRate, format, channelMask, frameCount, cbf, user,
979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            notificationFrames, false /*threadCanCallJava*/, sessionId, transferType);
9889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
9989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
10089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectAudioRecord::~AudioRecord()
10189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
10289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (mStatus == NO_ERROR) {
10389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Make sure that callback function exits in the case where
10489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // it is looping on buffer empty condition in obtainBuffer().
10589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Otherwise the callback thread will never exit.
10689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        stop();
10768337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten        if (mAudioRecordThread != 0) {
10868337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten            mAudioRecordThread->requestExit();  // see comment in AudioRecord.h
10968337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten            mAudioRecordThread->requestExitAndWait();
11068337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten            mAudioRecordThread.clear();
11189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
1129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (mAudioRecord != 0) {
1139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mAudioRecord->asBinder()->unlinkToDeath(mDeathNotifier, this);
1149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mAudioRecord.clear();
1159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
11689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        IPCThreadState::self()->flushCommands();
1173a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSystem::releaseAudioSessionId(mSessionId);
11889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
11989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
12089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
12189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioRecord::set(
122eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kasten        audio_source_t inputSource,
12389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t sampleRate,
12458f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
125624a7fcb377f2a40109c16de5109ae8ea1f67a69Glenn Kasten        audio_channel_mask_t channelMask,
126e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten        int frameCountInt,
12789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        callback_t cbf,
12889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        void* user,
12989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        int notificationFrames,
130be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent        bool threadCanCallJava,
1319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        int sessionId,
13227f7b2a8fe899565487d8a326676a5f7d0a05a37Glenn Kasten        transfer_type transferType,
13327f7b2a8fe899565487d8a326676a5f7d0a05a37Glenn Kasten        audio_input_flags_t flags)
13489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
1359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    switch (transferType) {
1369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case TRANSFER_DEFAULT:
1379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (cbf == NULL || threadCanCallJava) {
1389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            transferType = TRANSFER_SYNC;
1399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        } else {
1409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            transferType = TRANSFER_CALLBACK;
1419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
1429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        break;
1439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case TRANSFER_CALLBACK:
1449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (cbf == NULL) {
1459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL");
1469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return BAD_VALUE;
1479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
1489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        break;
1499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case TRANSFER_OBTAIN:
1509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case TRANSFER_SYNC:
1519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        break;
1529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    default:
1539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("Invalid transfer type %d", transferType);
1549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return BAD_VALUE;
1559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
1569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mTransfer = transferType;
1579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
158e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten    // FIXME "int" here is legacy and will be replaced by size_t later
159e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten    if (frameCountInt < 0) {
160e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten        ALOGE("Invalid frame count %d", frameCountInt);
161e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten        return BAD_VALUE;
162e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten    }
163e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten    size_t frameCount = frameCountInt;
16489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
165e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten    ALOGV("set(): sampleRate %u, channelMask %#x, frameCount %u", sampleRate, channelMask,
16685ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten            frameCount);
1671703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
1681703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
1691703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
1701dd70b9f04961a06fcb73a97fca10a53b3245d3cEric Laurent    if (mAudioRecord != 0) {
1719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("Track already in use");
17289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return INVALID_OPERATION;
17389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
17489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
175c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    if (inputSource == AUDIO_SOURCE_DEFAULT) {
176c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        inputSource = AUDIO_SOURCE_MIC;
17789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
17889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
17989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (sampleRate == 0) {
180f0f33c4acd231fa95deb9eeef2c46b0129e64463Glenn Kasten        ALOGE("Invalid sample rate %u", sampleRate);
181f0f33c4acd231fa95deb9eeef2c46b0129e64463Glenn Kasten        return BAD_VALUE;
18289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
183e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    mSampleRate = sampleRate;
184e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
18589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // these below should probably come from the audioFlinger too...
18658f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten    if (format == AUDIO_FORMAT_DEFAULT) {
187fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        format = AUDIO_FORMAT_PCM_16_BIT;
18889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
1899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
190c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    // validate parameters
191fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (!audio_is_valid_format(format)) {
1929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("Invalid format %d", format);
193c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        return BAD_VALUE;
19489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
195291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    // Temporary restriction: AudioFlinger currently supports 16-bit PCM only
196291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    if (format != AUDIO_FORMAT_PCM_16_BIT) {
197291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        ALOGE("Format %d is not supported", format);
198291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        return BAD_VALUE;
199291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
2002e4664677d72ce54201d3fd0beb0e10280add93cGlenn Kasten    mFormat = format;
20189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2020d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi    if (!audio_is_input_channel(channelMask)) {
2039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("Invalid channel mask %#x", channelMask);
20489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
20589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
206a42ff007a17d63df22c60dd5e5fd811ee45ca1b3Glenn Kasten    mChannelMask = channelMask;
207a42ff007a17d63df22c60dd5e5fd811ee45ca1b3Glenn Kasten    uint32_t channelCount = popcount(channelMask);
208a42ff007a17d63df22c60dd5e5fd811ee45ca1b3Glenn Kasten    mChannelCount = channelCount;
209c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
210954315a10089fa3684ac94db5be77c6655c08fc0Glenn Kasten    // Assumes audio_is_linear_pcm(format), else sizeof(uint8_t)
211954315a10089fa3684ac94db5be77c6655c08fc0Glenn Kasten    mFrameSize = channelCount * audio_bytes_per_sample(format);
212e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
2137c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (sessionId == 0 ) {
2147c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        mSessionId = AudioSystem::newAudioSessionId();
2157c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    } else {
2167c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        mSessionId = sessionId;
2177c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
2183856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("set(): mSessionId %d", mSessionId);
2197c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
22027f7b2a8fe899565487d8a326676a5f7d0a05a37Glenn Kasten    mFlags = flags;
22127f7b2a8fe899565487d8a326676a5f7d0a05a37Glenn Kasten
2226100d2d60517ff33ed8eb35d0b7ea63cde0831c9Eric Laurent    audio_io_handle_t input = AudioSystem::getInput(inputSource,
2237c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                                    sampleRate,
2247c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                                    format,
2257c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                                    channelMask,
2267c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                                    mSessionId);
2276100d2d60517ff33ed8eb35d0b7ea63cde0831c9Eric Laurent    if (input == 0) {
22829357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Could not get audio input for record source %d", inputSource);
22989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
23089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
23189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
23289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // validate framecount
233e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten    size_t minFrameCount = 0;
2346f744d75d3439f7984245e3c489cc7cf91cea41cEric Laurent    status_t status = getMinFrameCount(&minFrameCount, sampleRate, format, channelMask);
23515304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh    if (status != NO_ERROR) {
2369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("getMinFrameCount() failed; status %d", status);
23715304d601cbf83be6519ca53e1a26b97d50d0192Chia-chi Yeh        return status;
238c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
2393856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioRecord::set() minFrameCount = %d", minFrameCount);
24089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
24189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (frameCount == 0) {
24289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        frameCount = minFrameCount;
24389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    } else if (frameCount < minFrameCount) {
2449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("frameCount %u < minFrameCount %u", frameCount, minFrameCount);
24589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
24689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
24789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2484a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten    mNotificationFramesReq = notificationFrames;
2494a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten    mNotificationFramesAct = 0;
25089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
25134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    // create the IAudioRecord
25273493688f4190f790ee15d9ca54831cd64f4e195Glenn Kasten    status = openRecord_l(sampleRate, format, frameCount, mFlags, input, 0 /*epoch*/);
25334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (status != NO_ERROR) {
25489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return status;
25589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
25634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
257a0d68338a88c2ddb4502f95017b546d603ef1ec7Glenn Kasten    if (cbf != NULL) {
25868337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten        mAudioRecordThread = new AudioRecordThread(*this, threadCanCallJava);
25968337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten        mAudioRecordThread->run("AudioRecord", ANDROID_PRIORITY_AUDIO);
26089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
26189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
26289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mStatus = NO_ERROR;
26389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
26489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // Update buffer size in case it has been limited by AudioFlinger during track creation
265b603744e96b07b1d5bf745bde593fb2c025cefcfGlenn Kasten    mFrameCount = mCblk->frameCount_;
26683a0382dc17364567667a4e6135db43f5bd92efcGlenn Kasten
26768337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    mActive = false;
26889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mCbf = cbf;
2699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mRefreshRemaining = true;
27089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mUserData = user;
27189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // TODO: add audio hardware input latency here
272573266210fb2b2e7d86fbd46d0dfe16763611d91Eric Laurent    mLatency = (1000*mFrameCount) / sampleRate;
27389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mMarkerPosition = 0;
2747d563247cdac0509009d579bbf849157d47c38a9Jean-Michel Trivi    mMarkerReached = false;
27589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mNewPosition = 0;
27689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mUpdatePeriod = 0;
277eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kasten    mInputSource = inputSource;
27805bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent    mInput = input;
2793a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    AudioSystem::acquireAudioSessionId(mSessionId);
2809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mSequence = 1;
2819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mObservedSequence = mSequence;
2829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mInOverrun = false;
28389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
28489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
28589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
28689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
28789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
28889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
289a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurentstatus_t AudioRecord::start(AudioSystem::sync_event_t event, int triggerSession)
29089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
291a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    ALOGV("start, sync event %d trigger session %d", event, triggerSession);
29289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
293f5aafb209d01ba2ab6cb55d1a12cfc653e2b4be0Eric Laurent    AutoMutex lock(mLock);
2949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mActive) {
2959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return NO_ERROR;
2969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
2971703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
2989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // reset current position as seen by client to 0
2999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
3006dbc1359f778575d09d6da722b060a6d72c2e7c5Glenn Kasten
3019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mNewPosition = mProxy->getPosition() + mUpdatePeriod;
30296f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten    int32_t flags = android_atomic_acquire_load(&mCblk->mFlags);
3039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
3049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    status_t status = NO_ERROR;
3059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (!(flags & CBLK_INVALID)) {
3069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGV("mAudioRecord->start()");
3079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        status = mAudioRecord->start(event, triggerSession);
3089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (status == DEAD_OBJECT) {
3099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            flags |= CBLK_INVALID;
3101703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent        }
3119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
3129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (flags & CBLK_INVALID) {
3139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        status = restoreRecord_l("start");
3149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
3159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
3169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (status != NO_ERROR) {
3179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("start() status %d", status);
3189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
3199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mActive = true;
3209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        sp<AudioRecordThread> t = mAudioRecordThread;
3219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (t != 0) {
3229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            t->resume();
3236100d2d60517ff33ed8eb35d0b7ea63cde0831c9Eric Laurent        } else {
3249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mPreviousPriority = getpriority(PRIO_PROCESS, 0);
3259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            get_sched_policy(0, &mPreviousSchedulingGroup);
3269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
32789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
32889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
32989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
3309f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return status;
33189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
33289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
333d64cd233eef39430561c1e1df423336a199cc5d7Glenn Kastenvoid AudioRecord::stop()
33489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
335f5aafb209d01ba2ab6cb55d1a12cfc653e2b4be0Eric Laurent    AutoMutex lock(mLock);
3369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (!mActive) {
3379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return;
3389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
3399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
3409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mActive = false;
3419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy->interrupt();
3429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mAudioRecord->stop();
3439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // the record head position will reset to 0, so if a marker is set, we need
3449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // to activate it again
3459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mMarkerReached = false;
3469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    sp<AudioRecordThread> t = mAudioRecordThread;
3479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (t != 0) {
3489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        t->pause();
3499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
3509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        setpriority(PRIO_PROCESS, 0, mPreviousPriority);
3519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        set_sched_policy(0, mPreviousSchedulingGroup);
35289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
35389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
35489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
35589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectbool AudioRecord::stopped() const
35689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
35768337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    AutoMutex lock(mLock);
35889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return !mActive;
35989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
36089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
36189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioRecord::setMarkerPosition(uint32_t marker)
36289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
3639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mCbf == NULL) {
3649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return INVALID_OPERATION;
3659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
36689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
367955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten    AutoMutex lock(mLock);
36889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mMarkerPosition = marker;
3697d563247cdac0509009d579bbf849157d47c38a9Jean-Michel Trivi    mMarkerReached = false;
37089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
37189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
37289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
37389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
374606ee61616efdba4696ae591ad10a4be33d8c946Glenn Kastenstatus_t AudioRecord::getMarkerPosition(uint32_t *marker) const
37589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
3769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (marker == NULL) {
3779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return BAD_VALUE;
3789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
37989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
380955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten    AutoMutex lock(mLock);
38189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    *marker = mMarkerPosition;
38289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
38389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
38489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
38589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
38689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioRecord::setPositionUpdatePeriod(uint32_t updatePeriod)
38789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
3889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mCbf == NULL) {
3899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return INVALID_OPERATION;
3909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
391955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten
392955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten    AutoMutex lock(mLock);
3939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mNewPosition = mProxy->getPosition() + updatePeriod;
39489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    mUpdatePeriod = updatePeriod;
39589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
39689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
39789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
39889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
399606ee61616efdba4696ae591ad10a4be33d8c946Glenn Kastenstatus_t AudioRecord::getPositionUpdatePeriod(uint32_t *updatePeriod) const
40089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
4019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (updatePeriod == NULL) {
4029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return BAD_VALUE;
4039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
40489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
405955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten    AutoMutex lock(mLock);
40689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    *updatePeriod = mUpdatePeriod;
40789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
40889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
40989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
41089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
411606ee61616efdba4696ae591ad10a4be33d8c946Glenn Kastenstatus_t AudioRecord::getPosition(uint32_t *position) const
41289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
4139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (position == NULL) {
4149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return BAD_VALUE;
4159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
41689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
4171703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
4189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    *position = mProxy->getPosition();
41989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
42089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
42189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
42289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
423606ee61616efdba4696ae591ad10a4be33d8c946Glenn Kastenunsigned int AudioRecord::getInputFramesLost() const
42405bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent{
425bf04a5d7f287fc712e0ed91849dc85c90c1e182dGlenn Kasten    // no need to check mActive, because if inactive this will return 0, which is what we want
4269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return AudioSystem::getInputFramesLost(getInput());
42705bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent}
42889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
42989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
43089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
4311703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// must be called with mLock held
4321703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurentstatus_t AudioRecord::openRecord_l(
43334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        uint32_t sampleRate,
43458f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
435e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten        size_t frameCount,
43673493688f4190f790ee15d9ca54831cd64f4e195Glenn Kasten        audio_input_flags_t flags,
4379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audio_io_handle_t input,
4389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t epoch)
43934f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent{
44034f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    status_t status;
44134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
44234f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (audioFlinger == 0) {
443b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten        ALOGE("Could not get audioflinger");
44434f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        return NO_INIT;
44534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
44634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
4473151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten    IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
4481879fff068422852c1483dcf8365c2ff0e2fadfcGlenn Kasten    pid_t tid = -1;
4491879fff068422852c1483dcf8365c2ff0e2fadfcGlenn Kasten
4503151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten    // Client can only express a preference for FAST.  Server will perform additional tests.
4513151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten    // The only supported use case for FAST is callback transfer mode.
4523151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten    if (flags & AUDIO_INPUT_FLAG_FAST) {
4533151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten        if ((mTransfer != TRANSFER_CALLBACK) || (mAudioRecordThread == 0)) {
4543151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten            ALOGW("AUDIO_INPUT_FLAG_FAST denied by client");
4553151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten            // once denied, do not request again if IAudioRecord is re-created
4567cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            flags = (audio_input_flags_t) (flags & ~AUDIO_INPUT_FLAG_FAST);
4577cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            mFlags = flags;
4583151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten        } else {
4593151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten            trackFlags |= IAudioFlinger::TRACK_FAST;
4603151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten            tid = mAudioRecordThread->getTid();
4613151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten        }
4623151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten    }
4633151427b6b0adf99929433715bab6f1e505100c1Glenn Kasten
4644a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten    mNotificationFramesAct = mNotificationFramesReq;
4654a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten
4664a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten    if (!(flags & AUDIO_INPUT_FLAG_FAST)) {
4674a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten        // Make sure that application is notified with sufficient margin before overrun
4684a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten        if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/2) {
4694a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten            mNotificationFramesAct = frameCount/2;
4704a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten        }
4714a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten    }
4724a0efb77198c69df711ab369ac482a42dbdfab07Glenn Kasten
473955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten    int originalSessionId = mSessionId;
4748d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten    sp<IAudioRecord> record = audioFlinger->openRecord(input,
47534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                       sampleRate, format,
476a42ff007a17d63df22c60dd5e5fd811ee45ca1b3Glenn Kasten                                                       mChannelMask,
47734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                       frameCount,
478eeca32671896739e84050da5992d5f151a1629deGlenn Kasten                                                       &trackFlags,
4791879fff068422852c1483dcf8365c2ff0e2fadfcGlenn Kasten                                                       tid,
480be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent                                                       &mSessionId,
48134f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                                                       &status);
482955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten    ALOGE_IF(originalSessionId != 0 && mSessionId != originalSessionId,
483955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten            "session ID changed from %d to %d", originalSessionId, mSessionId);
4843a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
48534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    if (record == 0) {
48629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("AudioFlinger could not create record track, status: %d", status);
48734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        return status;
48834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
489b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    sp<IMemory> iMem = record->getCblk();
490b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    if (iMem == 0) {
49129357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Could not get control block");
49234f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent        return NO_INIT;
49334f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    }
4949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mAudioRecord != 0) {
4959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mAudioRecord->asBinder()->unlinkToDeath(mDeathNotifier, this);
4969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mDeathNotifier.clear();
4979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
49834f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    mAudioRecord = record;
499b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    mCblkMemory = iMem;
500b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
501b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    mCblk = cblk;
5027cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten    // FIXME missing fast track frameCount logic
5037cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten    mAwaitBoost = false;
5047cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten    if (flags & AUDIO_INPUT_FLAG_FAST) {
5057cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten        if (trackFlags & IAudioFlinger::TRACK_FAST) {
5067cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            ALOGV("AUDIO_INPUT_FLAG_FAST successful; frameCount %u", frameCount);
5077cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            mAwaitBoost = true;
5087cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            // double-buffering is not required for fast tracks, due to tighter scheduling
5097cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount) {
5107cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten                mNotificationFramesAct = frameCount;
5117cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            }
5127cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten        } else {
5137cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            ALOGV("AUDIO_INPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
5147cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            // once denied, do not request again if IAudioRecord is re-created
5157cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            flags = (audio_input_flags_t) (flags & ~AUDIO_INPUT_FLAG_FAST);
5167cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            mFlags = flags;
5177cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/2) {
5187cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten                mNotificationFramesAct = frameCount/2;
5197cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten            }
5207cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten        }
5217cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten    }
5229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
5239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // starting address of buffers in shared memory
5249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    void *buffers = (char*)cblk + sizeof(audio_track_cblk_t);
525e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
526e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten    // update proxy
5279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy = new AudioRecordClientProxy(cblk, buffers, frameCount, mFrameSize);
5289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy->setEpoch(epoch);
5297cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten    mProxy->setMinimum(mNotificationFramesAct);
5309f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
5319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mDeathNotifier = new DeathNotifier(this);
5329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mAudioRecord->asBinder()->linkToDeath(mDeathNotifier, this);
533e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
53434f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent    return NO_ERROR;
53534f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent}
53634f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent
53789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioRecord::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
53889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
5399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (audioBuffer == NULL) {
5409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return BAD_VALUE;
5419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
5429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mTransfer != TRANSFER_OBTAIN) {
5439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer->frameCount = 0;
5449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer->size = 0;
5459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer->raw = NULL;
5469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return INVALID_OPERATION;
5479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
548e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
5499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    const struct timespec *requested;
5509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (waitCount == -1) {
5519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &ClientProxy::kForever;
5529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else if (waitCount == 0) {
5539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &ClientProxy::kNonBlocking;
5549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else if (waitCount > 0) {
5559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        long long ms = WAIT_PERIOD_MS * (long long) waitCount;
5569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        struct timespec timeout;
5579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        timeout.tv_sec = ms / 1000;
5589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        timeout.tv_nsec = (int) (ms % 1000) * 1000000;
5599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &timeout;
5609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
5619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("%s invalid waitCount %d", __func__, waitCount);
5629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = NULL;
5639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
5649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return obtainBuffer(audioBuffer, requested);
5659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten}
5669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
5679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenstatus_t AudioRecord::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
5689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        struct timespec *elapsed, size_t *nonContig)
5699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{
5709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // previous and new IAudioRecord sequence numbers are used to detect track re-creation
5719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    uint32_t oldSequence = 0;
5729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    uint32_t newSequence;
5739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
5749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    Proxy::Buffer buffer;
5759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    status_t status = NO_ERROR;
5769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
5779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    static const int32_t kMaxTries = 5;
5789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    int32_t tryCounter = kMaxTries;
5799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
5809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    do {
5819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
5829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // keep them from going away if another thread re-creates the track during obtainBuffer()
5839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        sp<AudioRecordClientProxy> proxy;
5849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        sp<IMemory> iMem;
5859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        {
5869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // start of lock scope
5879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            AutoMutex lock(mLock);
5889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
5899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            newSequence = mSequence;
5909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
5919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (status == DEAD_OBJECT) {
5929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                // re-create track, unless someone else has already done so
5939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                if (newSequence == oldSequence) {
5949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                    status = restoreRecord_l("obtainBuffer");
5959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                    if (status != NO_ERROR) {
5969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                        break;
59734f1d8ecd23169a5f299937e3aaf1bd7937578a0Eric Laurent                    }
59889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                }
59989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
6009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            oldSequence = newSequence;
60189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // Keep the extra references
6039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            proxy = mProxy;
6049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            iMem = mCblkMemory;
605c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
6069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // Non-blocking if track is stopped
6079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (!mActive) {
6089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                requested = &ClientProxy::kNonBlocking;
6099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            }
61089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }   // end of lock scope
61289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        buffer.mFrameCount = audioBuffer->frameCount;
6149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // FIXME starts the requested timeout and elapsed over from scratch
6159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        status = proxy->obtainBuffer(&buffer, requested, elapsed);
6169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
6179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
61889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    audioBuffer->frameCount = buffer.mFrameCount;
6209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    audioBuffer->size = buffer.mFrameCount * mFrameSize;
6219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    audioBuffer->raw = buffer.mRaw;
6229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (nonContig != NULL) {
6239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        *nonContig = buffer.mNonContig;
6249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
6259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return status;
62689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
62789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
62889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectvoid AudioRecord::releaseBuffer(Buffer* audioBuffer)
62989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
6309f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // all TRANSFER_* are valid
6319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
6329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t stepCount = audioBuffer->size / mFrameSize;
6339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (stepCount == 0) {
6349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return;
6359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
6369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
6379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    Proxy::Buffer buffer;
6389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    buffer.mFrameCount = stepCount;
6399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    buffer.mRaw = audioBuffer->raw;
640e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten
6411703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
6429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mInOverrun = false;
6439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mProxy->releaseBuffer(&buffer);
6449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
6459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // the server does not automatically disable recorder on overrun, so no need to restart
64689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
64789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
648606ee61616efdba4696ae591ad10a4be33d8c946Glenn Kastenaudio_io_handle_t AudioRecord::getInput() const
6496100d2d60517ff33ed8eb35d0b7ea63cde0831c9Eric Laurent{
6501703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    AutoMutex lock(mLock);
651d1a243e41caffa8fd346907eed4625c9c47c1a86Eric Laurent    return mInput;
6521703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent}
6531703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
6541703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent// must be called with mLock held
6551703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurentaudio_io_handle_t AudioRecord::getInput_l()
6561703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent{
65705bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent    mInput = AudioSystem::getInput(mInputSource,
658e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                                mSampleRate,
6597c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                mFormat,
6607c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                mChannelMask,
6617c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                mSessionId);
66205bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent    return mInput;
6636100d2d60517ff33ed8eb35d0b7ea63cde0831c9Eric Laurent}
6646100d2d60517ff33ed8eb35d0b7ea63cde0831c9Eric Laurent
66589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
66689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
66789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectssize_t AudioRecord::read(void* buffer, size_t userSize)
66889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
6699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mTransfer != TRANSFER_SYNC) {
6709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return INVALID_OPERATION;
6719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
67289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
6749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // sanity-check. user is most-likely passing an error code, and it would
6759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // make the return value ambiguous (actualSize vs error).
6769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGE("AudioRecord::read(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
67789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        return BAD_VALUE;
67889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
67989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    ssize_t read = 0;
6819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    Buffer audioBuffer;
68289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    while (userSize >= mFrameSize) {
6849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer.frameCount = userSize / mFrameSize;
68589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
68789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        if (err < 0) {
6889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (read > 0) {
68989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                break;
690d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            }
69189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            return ssize_t(err);
69289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
69389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
69489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        size_t bytesRead = audioBuffer.size;
6959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        memcpy(buffer, audioBuffer.i8, bytesRead);
6969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        buffer = ((char *) buffer) + bytesRead;
69789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        userSize -= bytesRead;
69889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        read += bytesRead;
69989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
70089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        releaseBuffer(&audioBuffer);
7019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
70289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
70389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return read;
70489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
70589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
70689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
70789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
7089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastennsecs_t AudioRecord::processAudioBuffer(const sp<AudioRecordThread>& thread)
70989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
7101703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    mLock.lock();
71128f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten    if (mAwaitBoost) {
71228f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        mAwaitBoost = false;
71328f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        mLock.unlock();
71428f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        static const int32_t kMaxTries = 5;
71528f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        int32_t tryCounter = kMaxTries;
71628f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        uint32_t pollUs = 10000;
71728f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        do {
71828f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten            int policy = sched_getscheduler(0);
71928f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten            if (policy == SCHED_FIFO || policy == SCHED_RR) {
72028f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten                break;
72128f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten            }
72228f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten            usleep(pollUs);
72328f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten            pollUs <<= 1;
72428f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        } while (tryCounter-- > 0);
72528f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        if (tryCounter < 0) {
72628f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten            ALOGE("did not receive expected priority boost on time");
72728f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        }
72828f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        // Run again immediately
72928f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten        return 0;
73028f1351369682801e1bb40a835bdae3c97b73c1cGlenn Kasten    }
7319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
7329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Can only reference mCblk while locked
73396f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten    int32_t flags = android_atomic_and(~CBLK_OVERRUN, &mCblk->mFlags);
7349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
7359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Check for track invalidation
7369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (flags & CBLK_INVALID) {
7379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        (void) restoreRecord_l("processAudioBuffer");
7389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mLock.unlock();
7399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // Run again immediately, but with a new IAudioRecord
7409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return 0;
7419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
7429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
74368337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    bool active = mActive;
7449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
7459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Manage overrun callback, must be done under lock to avoid race with releaseBuffer()
7469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    bool newOverrun = false;
7479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (flags & CBLK_OVERRUN) {
7489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (!mInOverrun) {
7499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mInOverrun = true;
7509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            newOverrun = true;
7519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
7529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
7539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
7549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Get current position of server
7559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t position = mProxy->getPosition();
7569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
7579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Manage marker callback
7589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    bool markerReached = false;
7599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t markerPosition = mMarkerPosition;
7609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // FIXME fails for wraparound, need 64 bits
7619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
7629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mMarkerReached = markerReached = true;
7639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
7649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
7659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Determine the number of new position callback(s) that will be needed, while locked
7669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t newPosCount = 0;
7679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t newPosition = mNewPosition;
768955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten    uint32_t updatePeriod = mUpdatePeriod;
7699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // FIXME fails for wraparound, need 64 bits
7709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (updatePeriod > 0 && position >= newPosition) {
7719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        newPosCount = ((position - newPosition) / updatePeriod) + 1;
7729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mNewPosition += updatePeriod * newPosCount;
7739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
7749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
7759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Cache other fields that will be needed soon
7767cd9cf70e36ad4b8eb12e24f9adbbe6fd69edebdGlenn Kasten    size_t notificationFrames = mNotificationFramesAct;
7779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mRefreshRemaining) {
7789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mRefreshRemaining = false;
7799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mRemainingFrames = notificationFrames;
7809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mRetryOnPartialBuffer = false;
7819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
7829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t misalignment = mProxy->getMisalignment();
7839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    int32_t sequence = mSequence;
7849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
7859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // These fields don't need to be cached, because they are assigned only by set():
7869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    //      mTransfer, mCbf, mUserData, mSampleRate
7879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
7881703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    mLock.unlock();
7891703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
7909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // perform callbacks while unlocked
7919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (newOverrun) {
7929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mCbf(EVENT_OVERRUN, mUserData, NULL);
7939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
7949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (markerReached) {
795955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten        mCbf(EVENT_MARKER, mUserData, &markerPosition);
79689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
7979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    while (newPosCount > 0) {
7989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t temp = newPosition;
799955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten        mCbf(EVENT_NEW_POS, mUserData, &temp);
800955e78180ac6111c54f50930b0c4c12395e86cf7Glenn Kasten        newPosition += updatePeriod;
8019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        newPosCount--;
8029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
8039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mObservedSequence != sequence) {
8049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mObservedSequence = sequence;
8059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mCbf(EVENT_NEW_IAUDIORECORD, mUserData, NULL);
80689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
80789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
8089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // if inactive, then don't run me again until re-started
8099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (!active) {
8109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return NS_INACTIVE;
8119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
8129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
8139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Compute the estimated time until the next timed event (position, markers)
8149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    uint32_t minFrames = ~0;
8159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (!markerReached && position < markerPosition) {
8169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        minFrames = markerPosition - position;
8179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
8189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (updatePeriod > 0 && updatePeriod < minFrames) {
8199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        minFrames = updatePeriod;
8209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
8219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
8229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // If > 0, poll periodically to recover from a stuck server.  A good value is 2.
8239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    static const uint32_t kPoll = 0;
8249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
8259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        minFrames = kPoll * notificationFrames;
8269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
8279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
8289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // Convert frame units to time units
8299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    nsecs_t ns = NS_WHENEVER;
8309f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (minFrames != (uint32_t) ~0) {
8319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
8329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
8339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ns = ((minFrames * 1000000000LL) / mSampleRate) + kFudgeNs;
8349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
8359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
8369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // If not supplying data by EVENT_MORE_DATA, then we're done
8379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mTransfer != TRANSFER_CALLBACK) {
8389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return ns;
8399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
8409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
8419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    struct timespec timeout;
8429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    const struct timespec *requested = &ClientProxy::kForever;
8439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (ns != NS_WHENEVER) {
8449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        timeout.tv_sec = ns / 1000000000LL;
8459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        timeout.tv_nsec = ns % 1000000000LL;
8469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
8479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &timeout;
8489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
8499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
8509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    while (mRemainingFrames > 0) {
8519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
8529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        Buffer audioBuffer;
8539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer.frameCount = mRemainingFrames;
8549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t nonContig;
8559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
8569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
8579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
8589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        requested = &ClientProxy::kNonBlocking;
8599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t avail = audioBuffer.frameCount + nonContig;
8609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGV("obtainBuffer(%u) returned %u = %u + %u",
8619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                mRemainingFrames, avail, audioBuffer.frameCount, nonContig);
8629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (err != NO_ERROR) {
8639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR) {
8649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                break;
8659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            }
8669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            ALOGE("Error %d obtaining an audio buffer, giving up.", err);
8679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return NS_NEVER;
8689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
8699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
8709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (mRetryOnPartialBuffer) {
8719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mRetryOnPartialBuffer = false;
8729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (avail < mRemainingFrames) {
8739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                int64_t myns = ((mRemainingFrames - avail) *
8749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                        1100000000LL) / mSampleRate;
8759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                if (ns < 0 || myns < ns) {
8769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                    ns = myns;
8779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                }
8789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                return ns;
87989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
88089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
88189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
88289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        size_t reqSize = audioBuffer.size;
88389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
8849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t readSize = audioBuffer.size;
88589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
88689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // Sanity check on returned size
8879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (ssize_t(readSize) < 0 || readSize > reqSize) {
8889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
8899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                    reqSize, (int) readSize);
8909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return NS_NEVER;
8919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
8929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
8939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (readSize == 0) {
8949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // The callback is done consuming buffers
895cd6725a333395ffeac3215ea4bf834a95aaa8defEric Laurent            // Keep this thread going to handle timed events and
8969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // still try to provide more data in intervals of WAIT_PERIOD_MS
897cd6725a333395ffeac3215ea4bf834a95aaa8defEric Laurent            // but don't just loop and block the CPU, so wait
8989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return WAIT_PERIOD_MS * 1000000LL;
899cd6725a333395ffeac3215ea4bf834a95aaa8defEric Laurent        }
90089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t releasedFrames = readSize / mFrameSize;
9029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioBuffer.frameCount = releasedFrames;
9039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mRemainingFrames -= releasedFrames;
9049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (misalignment >= releasedFrames) {
9059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            misalignment -= releasedFrames;
9069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        } else {
9079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            misalignment = 0;
9089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
90989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
91089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        releaseBuffer(&audioBuffer);
91189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
9139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // if callback doesn't like to accept the full chunk
9149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (readSize < reqSize) {
9159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            continue;
9169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
91789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // There could be enough non-contiguous frames available to satisfy the remaining request
9199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (mRemainingFrames <= nonContig) {
9209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            continue;
9219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
922c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
9239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#if 0
9249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
9259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // sum <= notificationFrames.  It replaces that series by at most two EVENT_MORE_DATA
9269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        // that total to a sum == notificationFrames.
9279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (0 < misalignment && misalignment <= mRemainingFrames) {
9289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            mRemainingFrames = misalignment;
9299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            return (mRemainingFrames * 1100000000LL) / mSampleRate;
93089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        }
9319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#endif
93289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
93389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
9349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mRemainingFrames = notificationFrames;
9359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mRetryOnPartialBuffer = true;
9369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
9379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    // A lot has transpired since ns was calculated, so run again immediately and re-calculate
9389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return 0;
93989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
94089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
9419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenstatus_t AudioRecord::restoreRecord_l(const char *from)
9421703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent{
9439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    ALOGW("dead IAudioRecord, creating a new one from %s()", from);
9449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    ++mSequence;
9451703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    status_t result;
9461703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
947b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    // if the new IAudioRecord is created, openRecord_l() will modify the
948b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    // following member variables: mAudioRecord, mCblkMemory and mCblk.
949b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    // It will also delete the strong references on previous IAudioRecord and IMemory
9509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    size_t position = mProxy->getPosition();
9519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mNewPosition = position + mUpdatePeriod;
95273493688f4190f790ee15d9ca54831cd64f4e195Glenn Kasten    result = openRecord_l(mSampleRate, mFormat, mFrameCount, mFlags, getInput_l(), position);
953b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    if (result == NO_ERROR) {
9549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if (mActive) {
9559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // callback thread or sync event hasn't changed
9569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // FIXME this fails if we have a new AudioFlinger instance
9579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            result = mAudioRecord->start(AudioSystem::SYNC_EVENT_SAME, 0);
9589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
959b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    }
960b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten    if (result != NO_ERROR) {
9619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        ALOGW("restoreRecord_l() failed status %d", result);
962b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten        mActive = false;
9631703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent    }
964b36a7a68af073b1e7fd5cad6aa2c52223fd30efdGlenn Kasten
9659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    return result;
9669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten}
9671703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
9689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// =========================================================================
9691703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
9709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid AudioRecord::DeathNotifier::binderDied(const wp<IBinder>& who)
9719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{
9729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    sp<AudioRecord> audioRecord = mAudioRecord.promote();
9739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (audioRecord != 0) {
9749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        AutoMutex lock(audioRecord->mLock);
9759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        audioRecord->mProxy->binderDied();
9769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
9771703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent}
9781703cdfee717b1b312bf8979816a9e2f16a82e5dEric Laurent
97989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// =========================================================================
98089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
98168337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn KastenAudioRecord::AudioRecordThread::AudioRecordThread(AudioRecord& receiver, bool bCanCallJava)
9829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mResumeLatch(false)
98368337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten{
98468337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten}
98568337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten
98668337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn KastenAudioRecord::AudioRecordThread::~AudioRecordThread()
98768337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten{
98868337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten}
98968337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten
99068337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kastenbool AudioRecord::AudioRecordThread::threadLoop()
99168337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten{
99268337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    {
99368337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten        AutoMutex _l(mMyLock);
99468337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten        if (mPaused) {
99568337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten            mMyCond.wait(mMyLock);
99668337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten            // caller will check for exitPending()
99768337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten            return true;
99868337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten        }
99968337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    }
10009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    nsecs_t ns =  mReceiver.processAudioBuffer(this);
10019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    switch (ns) {
10029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case 0:
10039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return true;
10049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case NS_WHENEVER:
10059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        sleep(1);
10069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return true;
10079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case NS_INACTIVE:
10089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        pauseConditional();
10099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return true;
10109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    case NS_NEVER:
10119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return false;
10129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    default:
10139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
10149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        struct timespec req;
10159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        req.tv_sec = ns / 1000000000LL;
10169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        req.tv_nsec = ns % 1000000000LL;
10179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        nanosleep(&req, NULL /*rem*/);
10189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        return true;
101968337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    }
102068337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten}
102168337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten
102268337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kastenvoid AudioRecord::AudioRecordThread::requestExit()
102389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
102468337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    // must be in this order to avoid a race condition
102568337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    Thread::requestExit();
102668337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    resume();
102789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
102889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
102968337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kastenvoid AudioRecord::AudioRecordThread::pause()
103089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
103168337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    AutoMutex _l(mMyLock);
103268337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    mPaused = true;
10339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    mResumeLatch = false;
10349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten}
10359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten
10369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid AudioRecord::AudioRecordThread::pauseConditional()
10379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{
10389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    AutoMutex _l(mMyLock);
10399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    if (mResumeLatch) {
10409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mResumeLatch = false;
10419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
10429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mPaused = true;
10439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    }
104489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
104589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
104668337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kastenvoid AudioRecord::AudioRecordThread::resume()
10476dbc1359f778575d09d6da722b060a6d72c2e7c5Glenn Kasten{
104868337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    AutoMutex _l(mMyLock);
104968337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten    if (mPaused) {
105068337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten        mPaused = false;
10519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mResumeLatch = false;
105268337edf595a0c345ba4b8adcd4f1e541a1d7eb7Glenn Kasten        mMyCond.signal();
10539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten    } else {
10549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        mResumeLatch = true;
10556dbc1359f778575d09d6da722b060a6d72c2e7c5Glenn Kasten    }
10566dbc1359f778575d09d6da722b060a6d72c2e7c5Glenn Kasten}
10576dbc1359f778575d09d6da722b060a6d72c2e7c5Glenn Kasten
105889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// -------------------------------------------------------------------------
105989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
106089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}; // namespace android
1061