• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/audiohal/StreamHalInterface.h>
22 #include <media/nbaio/AudioStreamOutSink.h>
23 
24 namespace android {
25 
AudioStreamOutSink(sp<StreamOutHalInterface> stream)26 AudioStreamOutSink::AudioStreamOutSink(sp<StreamOutHalInterface> stream) :
27         NBAIO_Sink(),
28         mStream(stream),
29         mStreamBufferSizeBytes(0)
30 {
31     ALOG_ASSERT(stream != 0);
32 }
33 
~AudioStreamOutSink()34 AudioStreamOutSink::~AudioStreamOutSink()
35 {
36     mStream.clear();
37 }
38 
negotiate(const NBAIO_Format offers[],size_t numOffers,NBAIO_Format counterOffers[],size_t & numCounterOffers)39 ssize_t AudioStreamOutSink::negotiate(const NBAIO_Format offers[], size_t numOffers,
40                                       NBAIO_Format counterOffers[], size_t& numCounterOffers)
41 {
42     if (!Format_isValid(mFormat)) {
43         status_t result;
44         result = mStream->getBufferSize(&mStreamBufferSizeBytes);
45         if (result != OK) return result;
46         audio_format_t streamFormat;
47         uint32_t sampleRate;
48         audio_channel_mask_t channelMask;
49         result = mStream->getAudioProperties(&sampleRate, &channelMask, &streamFormat);
50         if (result != OK) return result;
51         mFormat = Format_from_SR_C(sampleRate,
52                 audio_channel_count_from_out_mask(channelMask), streamFormat);
53         mFrameSize = Format_frameSize(mFormat);
54     }
55     return NBAIO_Sink::negotiate(offers, numOffers, counterOffers, numCounterOffers);
56 }
57 
write(const void * buffer,size_t count)58 ssize_t AudioStreamOutSink::write(const void *buffer, size_t count)
59 {
60     if (!mNegotiated) {
61         return NEGOTIATE;
62     }
63     ALOG_ASSERT(Format_isValid(mFormat));
64     size_t written;
65     status_t ret = mStream->write(buffer, count * mFrameSize, &written);
66     if (ret == OK && written > 0) {
67         written /= mFrameSize;
68         mFramesWritten += written;
69         return written;
70     } else {
71         // FIXME verify HAL implementations are returning the correct error codes e.g. WOULD_BLOCK
72         ALOGE_IF(ret != OK, "Error while writing data to HAL: %d", ret);
73         return ret;
74     }
75 }
76 
getTimestamp(ExtendedTimestamp & timestamp)77 status_t AudioStreamOutSink::getTimestamp(ExtendedTimestamp &timestamp)
78 {
79     uint64_t position64;
80     struct timespec time;
81     if (mStream->getPresentationPosition(&position64, &time) != OK) {
82         return INVALID_OPERATION;
83     }
84     timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position64;
85     timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
86             time.tv_sec * 1000000000LL + time.tv_nsec;
87     return OK;
88 }
89 
90 }   // namespace android
91