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/nbaio/AudioStreamInSource.h>
23
24 namespace android {
25
AudioStreamInSource(audio_stream_in * stream)26 AudioStreamInSource::AudioStreamInSource(audio_stream_in *stream) :
27 NBAIO_Source(),
28 mStream(stream),
29 mStreamBufferSizeBytes(0),
30 mFramesOverrun(0),
31 mOverruns(0)
32 {
33 ALOG_ASSERT(stream != NULL);
34 }
35
~AudioStreamInSource()36 AudioStreamInSource::~AudioStreamInSource()
37 {
38 }
39
negotiate(const NBAIO_Format offers[],size_t numOffers,NBAIO_Format counterOffers[],size_t & numCounterOffers)40 ssize_t AudioStreamInSource::negotiate(const NBAIO_Format offers[], size_t numOffers,
41 NBAIO_Format counterOffers[], size_t& numCounterOffers)
42 {
43 if (!Format_isValid(mFormat)) {
44 mStreamBufferSizeBytes = mStream->common.get_buffer_size(&mStream->common);
45 audio_format_t streamFormat = mStream->common.get_format(&mStream->common);
46 uint32_t sampleRate = mStream->common.get_sample_rate(&mStream->common);
47 audio_channel_mask_t channelMask =
48 (audio_channel_mask_t) mStream->common.get_channels(&mStream->common);
49 mFormat = Format_from_SR_C(sampleRate,
50 audio_channel_count_from_in_mask(channelMask), streamFormat);
51 mFrameSize = Format_frameSize(mFormat);
52 }
53 return NBAIO_Source::negotiate(offers, numOffers, counterOffers, numCounterOffers);
54 }
55
framesOverrun()56 size_t AudioStreamInSource::framesOverrun()
57 {
58 uint32_t framesOverrun = mStream->get_input_frames_lost(mStream);
59 if (framesOverrun > 0) {
60 mFramesOverrun += framesOverrun;
61 // FIXME only increment for contiguous ranges
62 ++mOverruns;
63 }
64 return mFramesOverrun;
65 }
66
read(void * buffer,size_t count,int64_t readPTS __unused)67 ssize_t AudioStreamInSource::read(void *buffer, size_t count, int64_t readPTS __unused)
68 {
69 if (CC_UNLIKELY(!Format_isValid(mFormat))) {
70 return NEGOTIATE;
71 }
72 ssize_t bytesRead = mStream->read(mStream, buffer, count * mFrameSize);
73 if (bytesRead > 0) {
74 size_t framesRead = bytesRead / mFrameSize;
75 mFramesRead += framesRead;
76 return framesRead;
77 } else {
78 return bytesRead;
79 }
80 }
81
82 } // namespace android
83