blob: 1054b68e4e918f3765796a400ca5f8a6551465f3 [file] [log] [blame]
Glenn Kasten01066232012-02-27 11:50:44 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioStreamInSource"
18//#define LOG_NDEBUG 0
19
20#include <cutils/compiler.h>
21#include <utils/Log.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070022#include <media/audiohal/StreamHalInterface.h>
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070023#include <media/nbaio/AudioStreamInSource.h>
Glenn Kasten01066232012-02-27 11:50:44 -080024
25namespace android {
26
Mikhail Naganova0c91332016-09-19 10:01:12 -070027AudioStreamInSource::AudioStreamInSource(sp<StreamInHalInterface> stream) :
Glenn Kasten01066232012-02-27 11:50:44 -080028 NBAIO_Source(),
29 mStream(stream),
30 mStreamBufferSizeBytes(0),
31 mFramesOverrun(0),
32 mOverruns(0)
33{
Mikhail Naganova0c91332016-09-19 10:01:12 -070034 ALOG_ASSERT(stream != 0);
Glenn Kasten01066232012-02-27 11:50:44 -080035}
36
37AudioStreamInSource::~AudioStreamInSource()
38{
Mikhail Naganova0c91332016-09-19 10:01:12 -070039 mStream.clear();
Glenn Kasten01066232012-02-27 11:50:44 -080040}
41
42ssize_t AudioStreamInSource::negotiate(const NBAIO_Format offers[], size_t numOffers,
43 NBAIO_Format counterOffers[], size_t& numCounterOffers)
44{
Glenn Kasten6e0d67d2014-01-31 09:41:08 -080045 if (!Format_isValid(mFormat)) {
Mikhail Naganova0c91332016-09-19 10:01:12 -070046 status_t result;
47 result = mStream->getBufferSize(&mStreamBufferSizeBytes);
48 if (result != OK) return result;
49 audio_format_t streamFormat;
50 uint32_t sampleRate;
51 audio_channel_mask_t channelMask;
52 result = mStream->getAudioProperties(&sampleRate, &channelMask, &streamFormat);
53 if (result != OK) return result;
Andy Hunge5412692014-05-16 11:25:07 -070054 mFormat = Format_from_SR_C(sampleRate,
55 audio_channel_count_from_in_mask(channelMask), streamFormat);
Glenn Kasten43d9b872014-03-06 09:03:34 -080056 mFrameSize = Format_frameSize(mFormat);
Glenn Kasten01066232012-02-27 11:50:44 -080057 }
58 return NBAIO_Source::negotiate(offers, numOffers, counterOffers, numCounterOffers);
59}
60
Andy Hung818e7a32016-02-16 18:08:07 -080061int64_t AudioStreamInSource::framesOverrun()
Glenn Kasten01066232012-02-27 11:50:44 -080062{
Mikhail Naganova0c91332016-09-19 10:01:12 -070063 uint32_t framesOverrun;
64 status_t result = mStream->getInputFramesLost(&framesOverrun);
65 if (result == OK && framesOverrun > 0) {
Glenn Kasten01066232012-02-27 11:50:44 -080066 mFramesOverrun += framesOverrun;
67 // FIXME only increment for contiguous ranges
68 ++mOverruns;
Mikhail Naganova0c91332016-09-19 10:01:12 -070069 } else if (result != OK) {
70 ALOGE("Error when retrieving lost frames count from HAL: %d", result);
Glenn Kasten01066232012-02-27 11:50:44 -080071 }
72 return mFramesOverrun;
73}
74
Glenn Kastend79072e2016-01-06 08:41:20 -080075ssize_t AudioStreamInSource::read(void *buffer, size_t count)
Glenn Kasten01066232012-02-27 11:50:44 -080076{
Glenn Kasten6e0d67d2014-01-31 09:41:08 -080077 if (CC_UNLIKELY(!Format_isValid(mFormat))) {
Glenn Kasten01066232012-02-27 11:50:44 -080078 return NEGOTIATE;
79 }
Mikhail Naganova0c91332016-09-19 10:01:12 -070080 size_t bytesRead;
81 status_t result = mStream->read(buffer, count * mFrameSize, &bytesRead);
82 if (result == OK && bytesRead > 0) {
Glenn Kasten4d693d62014-03-06 07:53:11 -080083 size_t framesRead = bytesRead / mFrameSize;
Glenn Kasten01066232012-02-27 11:50:44 -080084 mFramesRead += framesRead;
85 return framesRead;
86 } else {
Mikhail Naganova0c91332016-09-19 10:01:12 -070087 ALOGE_IF(result != OK, "Error while reading data from HAL: %d", result);
Glenn Kasten01066232012-02-27 11:50:44 -080088 return bytesRead;
89 }
90}
91
92} // namespace android