1 /*
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>
22 #include <media/audiohal/StreamHalInterface.h>
23 #include <media/nbaio/AudioStreamInSource.h>
24 
25 namespace android {
26 
AudioStreamInSource(sp<StreamInHalInterface> stream)27 AudioStreamInSource::AudioStreamInSource(sp<StreamInHalInterface> stream) :
28         NBAIO_Source(),
29         mStream(stream),
30         mStreamBufferSizeBytes(0),
31         mFramesOverrun(0),
32         mOverruns(0)
33 {
34     ALOG_ASSERT(stream != 0);
35 }
36 
~AudioStreamInSource()37 AudioStreamInSource::~AudioStreamInSource()
38 {
39     mStream.clear();
40 }
41 
negotiate(const NBAIO_Format offers[],size_t numOffers,NBAIO_Format counterOffers[],size_t & numCounterOffers)42 ssize_t AudioStreamInSource::negotiate(const NBAIO_Format offers[], size_t numOffers,
43                                       NBAIO_Format counterOffers[], size_t& numCounterOffers)
44 {
45     if (!Format_isValid(mFormat)) {
46         status_t result;
47         result = mStream->getBufferSize(&mStreamBufferSizeBytes);
48         if (result != OK) return result;
49         audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER;
50         result = mStream->getAudioProperties(&config);
51         if (result != OK) return result;
52         mFormat = Format_from_SR_C(config.sample_rate,
53                 audio_channel_count_from_in_mask(config.channel_mask), config.format);
54         mFrameSize = Format_frameSize(mFormat);
55     }
56     return NBAIO_Source::negotiate(offers, numOffers, counterOffers, numCounterOffers);
57 }
58 
framesOverrun()59 int64_t AudioStreamInSource::framesOverrun()
60 {
61     uint32_t framesOverrun;
62     status_t result = mStream->getInputFramesLost(&framesOverrun);
63     if (result == OK && framesOverrun > 0) {
64         mFramesOverrun += framesOverrun;
65         // FIXME only increment for contiguous ranges
66         ++mOverruns;
67     } else if (result != OK) {
68         ALOGE("Error when retrieving lost frames count from HAL: %d", result);
69     }
70     return mFramesOverrun;
71 }
72 
read(void * buffer,size_t count)73 ssize_t AudioStreamInSource::read(void *buffer, size_t count)
74 {
75     if (CC_UNLIKELY(!Format_isValid(mFormat))) {
76         return NEGOTIATE;
77     }
78     size_t bytesRead;
79     status_t result = mStream->read(buffer, count * mFrameSize, &bytesRead);
80     if (result == OK && bytesRead > 0) {
81         size_t framesRead = bytesRead / mFrameSize;
82         mFramesRead += framesRead;
83         return framesRead;
84     } else {
85         ALOGE_IF(result != OK, "Error while reading data from HAL: %d", result);
86         return bytesRead;
87     }
88 }
89 
90 }   // namespace android
91