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_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;
54         mFormat = Format_from_SR_C(sampleRate,
55                 audio_channel_count_from_in_mask(channelMask), streamFormat);
56         mFrameSize = Format_frameSize(mFormat);
57     }
58     return NBAIO_Source::negotiate(offers, numOffers, counterOffers, numCounterOffers);
59 }
60 
framesOverrun()61 int64_t AudioStreamInSource::framesOverrun()
62 {
63     uint32_t framesOverrun;
64     status_t result = mStream->getInputFramesLost(&framesOverrun);
65     if (result == OK && framesOverrun > 0) {
66         mFramesOverrun += framesOverrun;
67         // FIXME only increment for contiguous ranges
68         ++mOverruns;
69     } else if (result != OK) {
70         ALOGE("Error when retrieving lost frames count from HAL: %d", result);
71     }
72     return mFramesOverrun;
73 }
74 
read(void * buffer,size_t count)75 ssize_t AudioStreamInSource::read(void *buffer, size_t count)
76 {
77     if (CC_UNLIKELY(!Format_isValid(mFormat))) {
78         return NEGOTIATE;
79     }
80     size_t bytesRead;
81     status_t result = mStream->read(buffer, count * mFrameSize, &bytesRead);
82     if (result == OK && bytesRead > 0) {
83         size_t framesRead = bytesRead / mFrameSize;
84         mFramesRead += framesRead;
85         return framesRead;
86     } else {
87         ALOGE_IF(result != OK, "Error while reading data from HAL: %d", result);
88         return bytesRead;
89     }
90 }
91 
92 }   // namespace android
93