blob: c28d34d6023e39bb084bad71dd173c2ed9976f10 [file] [log] [blame]
Glenn Kasten01066232012-02-27 11:50:44 -08001/*
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>
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070021#include <media/nbaio/AudioStreamOutSink.h>
Glenn Kasten01066232012-02-27 11:50:44 -080022
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{
Glenn Kasten6e0d67d2014-01-31 09:41:08 -080040 if (!Format_isValid(mFormat)) {
Glenn Kasten01066232012-02-27 11:50:44 -080041 mStreamBufferSizeBytes = mStream->common.get_buffer_size(&mStream->common);
42 audio_format_t streamFormat = mStream->common.get_format(&mStream->common);
Glenn Kasten43d9b872014-03-06 09:03:34 -080043 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);
Glenn Kastenf69f9862014-03-07 08:37:57 -080046 mFormat = Format_from_SR_C(sampleRate, popcount(channelMask), streamFormat);
Glenn Kasten43d9b872014-03-06 09:03:34 -080047 mFrameSize = Format_frameSize(mFormat);
Glenn Kasten01066232012-02-27 11:50:44 -080048 }
49 return NBAIO_Sink::negotiate(offers, numOffers, counterOffers, numCounterOffers);
50}
51
52ssize_t AudioStreamOutSink::write(const void *buffer, size_t count)
53{
54 if (!mNegotiated) {
55 return NEGOTIATE;
56 }
Glenn Kasten6e0d67d2014-01-31 09:41:08 -080057 ALOG_ASSERT(Format_isValid(mFormat));
Glenn Kasten4d693d62014-03-06 07:53:11 -080058 ssize_t ret = mStream->write(mStream, buffer, count * mFrameSize);
Glenn Kasten01066232012-02-27 11:50:44 -080059 if (ret > 0) {
Glenn Kasten4d693d62014-03-06 07:53:11 -080060 ret /= mFrameSize;
Glenn Kasten01066232012-02-27 11:50:44 -080061 mFramesWritten += ret;
62 } else {
63 // FIXME verify HAL implementations are returning the correct error codes e.g. WOULD_BLOCK
64 }
65 return ret;
66}
67
John Grossman2c3b2da2012-08-02 17:08:54 -070068status_t AudioStreamOutSink::getNextWriteTimestamp(int64_t *timestamp) {
69 ALOG_ASSERT(timestamp != NULL);
70
71 if (NULL == mStream)
72 return INVALID_OPERATION;
73
74 if (NULL == mStream->get_next_write_timestamp)
75 return INVALID_OPERATION;
76
77 return mStream->get_next_write_timestamp(mStream, timestamp);
78}
79
Glenn Kasten767094d2013-08-23 13:51:43 -070080status_t AudioStreamOutSink::getTimestamp(AudioTimestamp& timestamp)
81{
Glenn Kastenfe346c72013-08-30 13:28:22 -070082 if (mStream->get_presentation_position == NULL) {
83 return INVALID_OPERATION;
84 }
Glenn Kasten767094d2013-08-23 13:51:43 -070085 // FIXME position64 won't be needed after AudioTimestamp.mPosition is changed to uint64_t
86 uint64_t position64;
87 int ok = mStream->get_presentation_position(mStream, &position64, &timestamp.mTime);
88 if (ok != 0) {
89 return INVALID_OPERATION;
90 }
91 timestamp.mPosition = position64;
92 return OK;
93}
94
Glenn Kasten01066232012-02-27 11:50:44 -080095} // namespace android