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