blob: 0ab58743f8ecadd2d9451edb7d094ad158b0c065 [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>
Andy Hung2b01f002017-07-05 12:01:36 -070021#include <audio_utils/clock.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070022#include <media/audiohal/StreamHalInterface.h>
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070023#include <media/nbaio/AudioStreamOutSink.h>
Glenn Kasten01066232012-02-27 11:50:44 -080024
25namespace android {
26
Mikhail Naganova0c91332016-09-19 10:01:12 -070027AudioStreamOutSink::AudioStreamOutSink(sp<StreamOutHalInterface> stream) :
Glenn Kasten01066232012-02-27 11:50:44 -080028 NBAIO_Sink(),
29 mStream(stream),
30 mStreamBufferSizeBytes(0)
31{
Mikhail Naganova0c91332016-09-19 10:01:12 -070032 ALOG_ASSERT(stream != 0);
Glenn Kasten01066232012-02-27 11:50:44 -080033}
34
35AudioStreamOutSink::~AudioStreamOutSink()
36{
Mikhail Naganova0c91332016-09-19 10:01:12 -070037 mStream.clear();
Glenn Kasten01066232012-02-27 11:50:44 -080038}
39
40ssize_t AudioStreamOutSink::negotiate(const NBAIO_Format offers[], size_t numOffers,
41 NBAIO_Format counterOffers[], size_t& numCounterOffers)
42{
Glenn Kasten6e0d67d2014-01-31 09:41:08 -080043 if (!Format_isValid(mFormat)) {
Mikhail Naganova0c91332016-09-19 10:01:12 -070044 status_t result;
45 result = mStream->getBufferSize(&mStreamBufferSizeBytes);
46 if (result != OK) return result;
Mikhail Naganov560637e2021-03-31 22:40:13 +000047 audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER;
48 result = mStream->getAudioProperties(&config);
Mikhail Naganova0c91332016-09-19 10:01:12 -070049 if (result != OK) return result;
Mikhail Naganov560637e2021-03-31 22:40:13 +000050 mFormat = Format_from_SR_C(config.sample_rate,
51 audio_channel_count_from_out_mask(config.channel_mask), config.format);
Glenn Kasten43d9b872014-03-06 09:03:34 -080052 mFrameSize = Format_frameSize(mFormat);
Vlad Popa3c7a2662023-02-14 20:09:47 +010053
54 // update format for MEL computation
55 auto processor = mMelProcessor.load();
56 if (processor) {
57 processor->updateAudioFormat(config.sample_rate,
58 audio_channel_count_from_out_mask(config.channel_mask),
59 config.format);
60 }
Glenn Kasten01066232012-02-27 11:50:44 -080061 }
62 return NBAIO_Sink::negotiate(offers, numOffers, counterOffers, numCounterOffers);
63}
64
65ssize_t AudioStreamOutSink::write(const void *buffer, size_t count)
66{
67 if (!mNegotiated) {
68 return NEGOTIATE;
69 }
Glenn Kasten6e0d67d2014-01-31 09:41:08 -080070 ALOG_ASSERT(Format_isValid(mFormat));
Mikhail Naganova0c91332016-09-19 10:01:12 -070071 size_t written;
72 status_t ret = mStream->write(buffer, count * mFrameSize, &written);
73 if (ret == OK && written > 0) {
Vlad Popa3c7a2662023-02-14 20:09:47 +010074 // Send to MelProcessor for sound dose measurement.
75 auto processor = mMelProcessor.load();
76 if (processor) {
77 processor->process(buffer, written);
78 }
79
Mikhail Naganova0c91332016-09-19 10:01:12 -070080 written /= mFrameSize;
81 mFramesWritten += written;
Vlad Popa3c7a2662023-02-14 20:09:47 +010082
Mikhail Naganova0c91332016-09-19 10:01:12 -070083 return written;
Glenn Kasten01066232012-02-27 11:50:44 -080084 } else {
85 // FIXME verify HAL implementations are returning the correct error codes e.g. WOULD_BLOCK
Mikhail Naganova0c91332016-09-19 10:01:12 -070086 ALOGE_IF(ret != OK, "Error while writing data to HAL: %d", ret);
87 return ret;
Glenn Kasten01066232012-02-27 11:50:44 -080088 }
Glenn Kasten01066232012-02-27 11:50:44 -080089}
90
Andy Hung818e7a32016-02-16 18:08:07 -080091status_t AudioStreamOutSink::getTimestamp(ExtendedTimestamp &timestamp)
Glenn Kasten767094d2013-08-23 13:51:43 -070092{
Glenn Kasten767094d2013-08-23 13:51:43 -070093 uint64_t position64;
Andy Hung818e7a32016-02-16 18:08:07 -080094 struct timespec time;
Mikhail Naganova0c91332016-09-19 10:01:12 -070095 if (mStream->getPresentationPosition(&position64, &time) != OK) {
Glenn Kasten767094d2013-08-23 13:51:43 -070096 return INVALID_OPERATION;
97 }
Andy Hung818e7a32016-02-16 18:08:07 -080098 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position64;
Andy Hung2b01f002017-07-05 12:01:36 -070099 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = audio_utils_ns_from_timespec(&time);
Glenn Kasten767094d2013-08-23 13:51:43 -0700100 return OK;
101}
102
Vlad Popa3c7a2662023-02-14 20:09:47 +0100103void AudioStreamOutSink::startMelComputation(const sp<audio_utils::MelProcessor>& processor)
104{
105 ALOGV("%s start mel computation for device %d", __func__, processor->getDeviceId());
Vlad Popa1c2f7e12023-03-28 02:08:56 +0200106
107 mMelProcessor.store(processor);
Vlad Popa3c7a2662023-02-14 20:09:47 +0100108 if (processor) {
Vlad Popa1c2f7e12023-03-28 02:08:56 +0200109 // update format for MEL computation
110 processor->updateAudioFormat(mFormat.mSampleRate,
111 mFormat.mChannelCount,
112 mFormat.mFormat);
113 processor->resume();
Vlad Popa3c7a2662023-02-14 20:09:47 +0100114 }
Vlad Popa1c2f7e12023-03-28 02:08:56 +0200115
Vlad Popa3c7a2662023-02-14 20:09:47 +0100116}
117
118void AudioStreamOutSink::stopMelComputation()
119{
120 auto melProcessor = mMelProcessor.load();
121 if (melProcessor != nullptr) {
Vlad Popa1c2f7e12023-03-28 02:08:56 +0200122 ALOGV("%s pause mel computation for device %d", __func__, melProcessor->getDeviceId());
123 melProcessor->pause();
Vlad Popa3c7a2662023-02-14 20:09:47 +0100124 }
125}
126
Glenn Kasten01066232012-02-27 11:50:44 -0800127} // namespace android