Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 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> |
Andy Hung | 2b01f00 | 2017-07-05 12:01:36 -0700 | [diff] [blame] | 21 | #include <audio_utils/clock.h> |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 22 | #include <media/audiohal/StreamHalInterface.h> |
Glenn Kasten | 2dd4bdd | 2012-08-29 11:10:32 -0700 | [diff] [blame] | 23 | #include <media/nbaio/AudioStreamOutSink.h> |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 27 | AudioStreamOutSink::AudioStreamOutSink(sp<StreamOutHalInterface> stream) : |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 28 | NBAIO_Sink(), |
| 29 | mStream(stream), |
| 30 | mStreamBufferSizeBytes(0) |
| 31 | { |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 32 | ALOG_ASSERT(stream != 0); |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | AudioStreamOutSink::~AudioStreamOutSink() |
| 36 | { |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 37 | mStream.clear(); |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | ssize_t AudioStreamOutSink::negotiate(const NBAIO_Format offers[], size_t numOffers, |
| 41 | NBAIO_Format counterOffers[], size_t& numCounterOffers) |
| 42 | { |
Glenn Kasten | 6e0d67d | 2014-01-31 09:41:08 -0800 | [diff] [blame] | 43 | if (!Format_isValid(mFormat)) { |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 44 | status_t result; |
| 45 | result = mStream->getBufferSize(&mStreamBufferSizeBytes); |
| 46 | if (result != OK) return result; |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 47 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 48 | result = mStream->getAudioProperties(&config); |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 49 | if (result != OK) return result; |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 50 | mFormat = Format_from_SR_C(config.sample_rate, |
| 51 | audio_channel_count_from_out_mask(config.channel_mask), config.format); |
Glenn Kasten | 43d9b87 | 2014-03-06 09:03:34 -0800 | [diff] [blame] | 52 | mFrameSize = Format_frameSize(mFormat); |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 53 | |
| 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 Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 61 | } |
| 62 | return NBAIO_Sink::negotiate(offers, numOffers, counterOffers, numCounterOffers); |
| 63 | } |
| 64 | |
| 65 | ssize_t AudioStreamOutSink::write(const void *buffer, size_t count) |
| 66 | { |
| 67 | if (!mNegotiated) { |
| 68 | return NEGOTIATE; |
| 69 | } |
Glenn Kasten | 6e0d67d | 2014-01-31 09:41:08 -0800 | [diff] [blame] | 70 | ALOG_ASSERT(Format_isValid(mFormat)); |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 71 | size_t written; |
| 72 | status_t ret = mStream->write(buffer, count * mFrameSize, &written); |
| 73 | if (ret == OK && written > 0) { |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 74 | // Send to MelProcessor for sound dose measurement. |
| 75 | auto processor = mMelProcessor.load(); |
| 76 | if (processor) { |
| 77 | processor->process(buffer, written); |
| 78 | } |
| 79 | |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 80 | written /= mFrameSize; |
| 81 | mFramesWritten += written; |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 82 | |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 83 | return written; |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 84 | } else { |
| 85 | // FIXME verify HAL implementations are returning the correct error codes e.g. WOULD_BLOCK |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 86 | ALOGE_IF(ret != OK, "Error while writing data to HAL: %d", ret); |
| 87 | return ret; |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 88 | } |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 91 | status_t AudioStreamOutSink::getTimestamp(ExtendedTimestamp ×tamp) |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 92 | { |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 93 | uint64_t position64; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 94 | struct timespec time; |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 95 | if (mStream->getPresentationPosition(&position64, &time) != OK) { |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 96 | return INVALID_OPERATION; |
| 97 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 98 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position64; |
Andy Hung | 2b01f00 | 2017-07-05 12:01:36 -0700 | [diff] [blame] | 99 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = audio_utils_ns_from_timespec(&time); |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 100 | return OK; |
| 101 | } |
| 102 | |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 103 | void AudioStreamOutSink::startMelComputation(const sp<audio_utils::MelProcessor>& processor) |
| 104 | { |
| 105 | ALOGV("%s start mel computation for device %d", __func__, processor->getDeviceId()); |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 106 | |
| 107 | mMelProcessor.store(processor); |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 108 | if (processor) { |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 109 | // update format for MEL computation |
| 110 | processor->updateAudioFormat(mFormat.mSampleRate, |
| 111 | mFormat.mChannelCount, |
| 112 | mFormat.mFormat); |
| 113 | processor->resume(); |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 114 | } |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 115 | |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void AudioStreamOutSink::stopMelComputation() |
| 119 | { |
| 120 | auto melProcessor = mMelProcessor.load(); |
| 121 | if (melProcessor != nullptr) { |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 122 | ALOGV("%s pause mel computation for device %d", __func__, melProcessor->getDeviceId()); |
| 123 | melProcessor->pause(); |
Vlad Popa | 3c7a266 | 2023-02-14 20:09:47 +0100 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 127 | } // namespace android |