Home | History | Annotate | Download | only in libnbaio
      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 
     23 namespace android {
     24 
     25 AudioStreamOutSink::AudioStreamOutSink(audio_stream_out *stream) :
     26         NBAIO_Sink(),
     27         mStream(stream),
     28         mStreamBufferSizeBytes(0)
     29 {
     30     ALOG_ASSERT(stream != NULL);
     31 }
     32 
     33 AudioStreamOutSink::~AudioStreamOutSink()
     34 {
     35 }
     36 
     37 ssize_t AudioStreamOutSink::negotiate(const NBAIO_Format offers[], size_t numOffers,
     38                                       NBAIO_Format counterOffers[], size_t& numCounterOffers)
     39 {
     40     if (mFormat == Format_Invalid) {
     41         mStreamBufferSizeBytes = mStream->common.get_buffer_size(&mStream->common);
     42         audio_format_t streamFormat = mStream->common.get_format(&mStream->common);
     43         if (streamFormat == AUDIO_FORMAT_PCM_16_BIT) {
     44             uint32_t sampleRate = mStream->common.get_sample_rate(&mStream->common);
     45             audio_channel_mask_t channelMask =
     46                     (audio_channel_mask_t) mStream->common.get_channels(&mStream->common);
     47             mFormat = Format_from_SR_C(sampleRate, popcount(channelMask));
     48             mBitShift = Format_frameBitShift(mFormat);
     49         }
     50     }
     51     return NBAIO_Sink::negotiate(offers, numOffers, counterOffers, numCounterOffers);
     52 }
     53 
     54 ssize_t AudioStreamOutSink::write(const void *buffer, size_t count)
     55 {
     56     if (!mNegotiated) {
     57         return NEGOTIATE;
     58     }
     59     ALOG_ASSERT(mFormat != Format_Invalid);
     60     ssize_t ret = mStream->write(mStream, buffer, count << mBitShift);
     61     if (ret > 0) {
     62         ret >>= mBitShift;
     63         mFramesWritten += ret;
     64     } else {
     65         // FIXME verify HAL implementations are returning the correct error codes e.g. WOULD_BLOCK
     66     }
     67     return ret;
     68 }
     69 
     70 status_t AudioStreamOutSink::getNextWriteTimestamp(int64_t *timestamp) {
     71     ALOG_ASSERT(timestamp != NULL);
     72 
     73     if (NULL == mStream)
     74         return INVALID_OPERATION;
     75 
     76     if (NULL == mStream->get_next_write_timestamp)
     77         return INVALID_OPERATION;
     78 
     79     return mStream->get_next_write_timestamp(mStream, timestamp);
     80 }
     81 
     82 }   // namespace android
     83