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         // 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         }
61     }
62     return NBAIO_Sink::negotiate(offers, numOffers, counterOffers, numCounterOffers);
63 }
64 
write(const void * buffer,size_t count)65 ssize_t AudioStreamOutSink::write(const void *buffer, size_t count)
66 {
67     if (!mNegotiated) {
68         return NEGOTIATE;
69     }
70     ALOG_ASSERT(Format_isValid(mFormat));
71     size_t written;
72     status_t ret = mStream->write(buffer, count * mFrameSize, &written);
73     if (ret == OK && written > 0) {
74         // Send to MelProcessor for sound dose measurement.
75         auto processor = mMelProcessor.load();
76         if (processor) {
77             processor->process(buffer, written);
78         }
79 
80         written /= mFrameSize;
81         mFramesWritten += written;
82 
83         return written;
84     } else {
85         // FIXME verify HAL implementations are returning the correct error codes e.g. WOULD_BLOCK
86         ALOGE_IF(ret != OK, "Error while writing data to HAL: %d", ret);
87         return ret;
88     }
89 }
90 
getTimestamp(ExtendedTimestamp & timestamp)91 status_t AudioStreamOutSink::getTimestamp(ExtendedTimestamp &timestamp)
92 {
93     uint64_t position64;
94     struct timespec time;
95     if (mStream->getPresentationPosition(&position64, &time) != OK) {
96         return INVALID_OPERATION;
97     }
98     timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position64;
99     timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = audio_utils_ns_from_timespec(&time);
100     return OK;
101 }
102 
startMelComputation(const sp<audio_utils::MelProcessor> & processor)103 void AudioStreamOutSink::startMelComputation(const sp<audio_utils::MelProcessor>& processor)
104 {
105     ALOGV("%s start mel computation for device %d", __func__, processor->getDeviceId());
106 
107     mMelProcessor.store(processor);
108     if (processor) {
109         // update format for MEL computation
110         processor->updateAudioFormat(mFormat.mSampleRate,
111                                      mFormat.mChannelCount,
112                                      mFormat.mFormat);
113         processor->resume();
114     }
115 
116 }
117 
stopMelComputation()118 void AudioStreamOutSink::stopMelComputation()
119 {
120     auto melProcessor = mMelProcessor.load();
121     if (melProcessor != nullptr) {
122         ALOGV("%s pause mel computation for device %d", __func__, melProcessor->getDeviceId());
123         melProcessor->pause();
124     }
125 }
126 
127 }   // namespace android
128