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 "AudioStreamOutSink" 18//#define LOG_NDEBUG 0 19 20#include <utils/Log.h> 21#include <media/nbaio/AudioStreamOutSink.h> 22 23namespace android { 24 25AudioStreamOutSink::AudioStreamOutSink(audio_stream_out *stream) : 26 NBAIO_Sink(), 27 mStream(stream), 28 mStreamBufferSizeBytes(0) 29{ 30 ALOG_ASSERT(stream != NULL); 31} 32 33AudioStreamOutSink::~AudioStreamOutSink() 34{ 35} 36 37ssize_t AudioStreamOutSink::negotiate(const NBAIO_Format offers[], size_t numOffers, 38 NBAIO_Format counterOffers[], size_t& numCounterOffers) 39{ 40 if (!Format_isValid(mFormat)) { 41 mStreamBufferSizeBytes = mStream->common.get_buffer_size(&mStream->common); 42 audio_format_t streamFormat = mStream->common.get_format(&mStream->common); 43 uint32_t sampleRate = mStream->common.get_sample_rate(&mStream->common); 44 audio_channel_mask_t channelMask = 45 (audio_channel_mask_t) mStream->common.get_channels(&mStream->common); 46 mFormat = Format_from_SR_C(sampleRate, 47 audio_channel_count_from_out_mask(channelMask), streamFormat); 48 mFrameSize = Format_frameSize(mFormat); 49 } 50 return NBAIO_Sink::negotiate(offers, numOffers, counterOffers, numCounterOffers); 51} 52 53ssize_t AudioStreamOutSink::write(const void *buffer, size_t count) 54{ 55 if (!mNegotiated) { 56 return NEGOTIATE; 57 } 58 ALOG_ASSERT(Format_isValid(mFormat)); 59 ssize_t ret = mStream->write(mStream, buffer, count * mFrameSize); 60 if (ret > 0) { 61 ret /= mFrameSize; 62 mFramesWritten += ret; 63 } else { 64 // FIXME verify HAL implementations are returning the correct error codes e.g. WOULD_BLOCK 65 } 66 return ret; 67} 68 69status_t AudioStreamOutSink::getTimestamp(ExtendedTimestamp ×tamp) 70{ 71 if (mStream->get_presentation_position == NULL) { 72 return INVALID_OPERATION; 73 } 74 75 uint64_t position64; 76 struct timespec time; 77 if (mStream->get_presentation_position(mStream, &position64, &time) != OK) { 78 return INVALID_OPERATION; 79 } 80 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position64; 81 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = 82 time.tv_sec * 1000000000LL + time.tv_nsec; 83 return OK; 84} 85 86} // namespace android 87