summaryrefslogtreecommitdiff
path: root/audio/device_port_sink.cpp
blob: 12754ff42672edcfb29559d0383b27a75a527c9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include PATH(APM_XSD_ENUMS_H_FILENAME)
#include <android-base/properties.h>
#include <chrono>
#include <thread>
#include <log/log.h>
#include <utils/Mutex.h>
#include <utils/Timers.h>
#include <utils/ThreadDefs.h>
#include "device_port_sink.h"
#include "talsa.h"
#include "audio_ops.h"
#include "ring_buffer.h"
#include "util.h"
#include "debug.h"

using ::android::base::GetBoolProperty;

namespace xsd {
using namespace ::android::audio::policy::configuration::CPP_VERSION;
}

namespace android {
namespace hardware {
namespace audio {
namespace CPP_VERSION {
namespace implementation {

namespace {

constexpr int kMaxJitterUs = 3000;  // Enforced by CTS, should be <= 6ms

struct TinyalsaSink : public DevicePortSink {
    TinyalsaSink(unsigned pcmCard, unsigned pcmDevice,
                 const AudioConfig &cfg,
                 uint64_t &frames)
            : mStartNs(systemTime(SYSTEM_TIME_MONOTONIC))
            , mSampleRateHz(cfg.base.sampleRateHz)
            , mFrameSize(util::countChannels(cfg.base.channelMask) * sizeof(int16_t))
            , mWriteSizeFrames(cfg.frameCount)
            , mInitialFrames(frames)
            , mFrames(frames)
            , mRingBuffer(mFrameSize * cfg.frameCount * 3)
            , mMixer(pcmCard)
            , mPcm(talsa::pcmOpen(pcmCard, pcmDevice,
                                  util::countChannels(cfg.base.channelMask),
                                  cfg.base.sampleRateHz,
                                  cfg.frameCount,
                                  true /* isOut */)) {
        LOG_ALWAYS_FATAL_IF(::pcm_prepare(mPcm.get()));
        mConsumeThread = std::thread(&TinyalsaSink::consumeThread, this);
    }

    ~TinyalsaSink() {
        mConsumeThreadRunning = false;
        mConsumeThread.join();
    }

    Result start() override {
        return ::pcm_start(mPcm.get()) ? FAILURE(Result::INVALID_STATE) : Result::OK;
    }

    Result stop() override {
        return ::pcm_stop(mPcm.get()) ? FAILURE(Result::INVALID_STATE) : Result::OK;
    }

    Result getPresentationPosition(uint64_t &frames, TimeSpec &ts) override {
        const AutoMutex lock(mFrameCountersMutex);

        nsecs_t nowNs = systemTime(SYSTEM_TIME_MONOTONIC);
        const uint64_t nowFrames = getPresentationFramesLocked(nowNs);
        auto presentedFrames = nowFrames - mMissedFrames;
        if (presentedFrames > mReceivedFrames) {
          // There is another underrun that is not yet accounted for in mMissedFrames
          auto delta = presentedFrames - mReceivedFrames;
          presentedFrames -= delta;
          // The last frame was presented some time ago, reflect that in the result
          nowNs -= delta * 1000000000 / mSampleRateHz;
        }
        mFrames = presentedFrames + mInitialFrames;

        frames = mFrames;
        ts = util::nsecs2TimeSpec(nowNs);
        return Result::OK;
    }

    uint64_t getPresentationFramesLocked(const nsecs_t nowNs) const {
        return uint64_t(mSampleRateHz) * ns2us(nowNs - mStartNs) / 1000000;
    }

    size_t calcAvailableFramesNowLocked() {
        const nsecs_t nowNs = systemTime(SYSTEM_TIME_MONOTONIC);
        auto presentationFrames = getPresentationFramesLocked(nowNs);
        if (mReceivedFrames + mMissedFrames < presentationFrames) {
            // There has been an underrun
            mMissedFrames = presentationFrames - mReceivedFrames;
        }
        size_t pendingFrames = mReceivedFrames + mMissedFrames - presentationFrames;
        return mRingBuffer.capacity() / mFrameSize - pendingFrames;
    }

    size_t calcWaitFramesNowLocked(const size_t requestedFrames) {
        const size_t availableFrames = calcAvailableFramesNowLocked();
        return (requestedFrames > availableFrames)
            ? (requestedFrames - availableFrames) : 0;
    }

    size_t write(float volume, size_t bytesToWrite, IReader &reader) {
        const AutoMutex lock(mFrameCountersMutex);

        size_t framesLost = 0;
        const size_t waitFrames = calcWaitFramesNowLocked(bytesToWrite / mFrameSize);
        const auto blockUntil =
            std::chrono::high_resolution_clock::now() +
                + std::chrono::microseconds(waitFrames * 1000000 / mSampleRateHz);

        while (bytesToWrite > 0) {
            if (mRingBuffer.waitForProduceAvailable(blockUntil
                    + std::chrono::microseconds(kMaxJitterUs))) {
                auto produceChunk = mRingBuffer.getProduceChunk();
                if (produceChunk.size >= bytesToWrite) {
                    // Since the ring buffer has more bytes free than we need,
                    // make sure we are not too early here: tinyalsa is jittery,
                    // we don't want to go faster than SYSTEM_TIME_MONOTONIC
                    std::this_thread::sleep_until(blockUntil);
                }

                const size_t szFrames =
                    std::min(produceChunk.size, bytesToWrite) / mFrameSize;
                const size_t szBytes = szFrames * mFrameSize;
                LOG_ALWAYS_FATAL_IF(reader(produceChunk.data, szBytes) < szBytes);

                aops::multiplyByVolume(volume,
                                       static_cast<int16_t *>(produceChunk.data),
                                       szBytes / sizeof(int16_t));

                LOG_ALWAYS_FATAL_IF(mRingBuffer.produce(szBytes) < szBytes);
                mReceivedFrames += szFrames;
                bytesToWrite -= szBytes;
            } else {
                ALOGV("TinyalsaSink::%s:%d pcm_write was late reading "
                      "frames, dropping %zu us of audio",
                      __func__, __LINE__,
                      size_t(1000000 * bytesToWrite / mFrameSize / mSampleRateHz));

                // drop old audio to make room for new
                const size_t bytesLost = mRingBuffer.makeRoomForProduce(bytesToWrite);
                framesLost += bytesLost / mFrameSize;

                while (bytesToWrite > 0) {
                    auto produceChunk = mRingBuffer.getProduceChunk();
                    const size_t szFrames =
                        std::min(produceChunk.size, bytesToWrite) / mFrameSize;
                    const size_t szBytes = szFrames * mFrameSize;
                    LOG_ALWAYS_FATAL_IF(reader(produceChunk.data, szBytes) < szBytes);

                    aops::multiplyByVolume(volume,
                                           static_cast<int16_t *>(produceChunk.data),
                                           szBytes / sizeof(int16_t));

                    LOG_ALWAYS_FATAL_IF(mRingBuffer.produce(szBytes) < szBytes);
                    mReceivedFrames += szFrames;
                    bytesToWrite -= szBytes;
                }
                break;
            }
        }

        return framesLost;
    }

    void consumeThread() {
        util::setThreadPriority(PRIORITY_URGENT_AUDIO);
        std::vector<uint8_t> writeBuffer(mWriteSizeFrames * mFrameSize);

        while (mConsumeThreadRunning) {
            if (mRingBuffer.waitForConsumeAvailable(
                    std::chrono::high_resolution_clock::now()
                    + std::chrono::microseconds(100000))) {
                size_t szBytes;
                {
                    auto chunk = mRingBuffer.getConsumeChunk();
                    szBytes = std::min(writeBuffer.size(), chunk.size);
                    // We have to memcpy because the consumer holds the lock
                    // into RingBuffer and pcm_write takes too long to hold
                    // this lock.
                    memcpy(writeBuffer.data(), chunk.data, szBytes);
                    LOG_ALWAYS_FATAL_IF(mRingBuffer.consume(chunk, szBytes) < szBytes);
                }

                int res = ::pcm_write(mPcm.get(), writeBuffer.data(), szBytes);
                if (res < 0) {
                    ALOGW("TinyalsaSink::%s:%d pcm_write failed with res=%d",
                          __func__, __LINE__, res);
                }
            }
        }
    }

    static std::unique_ptr<TinyalsaSink> create(unsigned pcmCard,
                                                unsigned pcmDevice,
                                                const AudioConfig &cfg,
                                                size_t readerBufferSizeHint,
                                                uint64_t &frames) {
        (void)readerBufferSizeHint;
        auto sink = std::make_unique<TinyalsaSink>(pcmCard, pcmDevice,
                                                   cfg, frames);
        if (sink->mMixer && sink->mPcm) {
            return sink;
        } else {
            return FAILURE(nullptr);
        }
    }

private:
    const nsecs_t mStartNs;
    const unsigned mSampleRateHz;
    const unsigned mFrameSize;
    const unsigned mWriteSizeFrames;
    const uint64_t mInitialFrames;
    uint64_t &mFrames GUARDED_BY(mFrameCountersMutex);
    uint64_t mMissedFrames GUARDED_BY(mFrameCountersMutex) = 0;
    uint64_t mReceivedFrames GUARDED_BY(mFrameCountersMutex) = 0;
    RingBuffer mRingBuffer;
    talsa::Mixer mMixer;
    talsa::PcmPtr mPcm;
    std::thread mConsumeThread;
    std::atomic<bool> mConsumeThreadRunning = true;
    mutable Mutex mFrameCountersMutex;
};

struct NullSink : public DevicePortSink {
    NullSink(const AudioConfig &cfg, uint64_t &frames)
            : mStartNs(systemTime(SYSTEM_TIME_MONOTONIC))
            , mSampleRateHz(cfg.base.sampleRateHz)
            , mFrameSize(util::countChannels(cfg.base.channelMask) * sizeof(int16_t))
            , mInitialFrames(frames)
            , mFrames(frames) {}

    Result start() override { return Result::OK; }
    Result stop() override { return Result::OK; }

    Result getPresentationPosition(uint64_t &frames, TimeSpec &ts) override {
        const AutoMutex lock(mFrameCountersMutex);

        nsecs_t nowNs = systemTime(SYSTEM_TIME_MONOTONIC);
        const uint64_t nowFrames = getPresentationFramesLocked(nowNs);
        auto presentedFrames = nowFrames - mMissedFrames;
        if (presentedFrames > mReceivedFrames) {
          // There is another underrun that is not yet accounted for in mMissedFrames
          auto delta = presentedFrames - mReceivedFrames;
          presentedFrames -= delta;
          // The last frame was presented some time ago, reflect that in the result
          nowNs -= delta * 1000000000 / mSampleRateHz;
        }
        mFrames = presentedFrames + mInitialFrames;

        frames = mFrames;
        ts = util::nsecs2TimeSpec(nowNs);
        return Result::OK;
    }

    uint64_t getPresentationFramesLocked(const nsecs_t nowNs) const {
        return uint64_t(mSampleRateHz) * ns2us(nowNs - mStartNs) / 1000000;
    }

    size_t calcAvailableFramesNowLocked() {
        const nsecs_t nowNs = systemTime(SYSTEM_TIME_MONOTONIC);
        auto presentationFrames = getPresentationFramesLocked(nowNs);
        if (mReceivedFrames + mMissedFrames < presentationFrames) {
            // There has been an underrun
            mMissedFrames = presentationFrames - mReceivedFrames;
        }
        size_t pendingFrames = mReceivedFrames + mMissedFrames - presentationFrames;
        return sizeof(mWriteBuffer) / mFrameSize - pendingFrames;
    }

    size_t calcWaitFramesNowLocked(const size_t requestedFrames) {
        const size_t availableFrames = calcAvailableFramesNowLocked();
        return (requestedFrames > availableFrames)
            ? (requestedFrames - availableFrames) : 0;
    }

    size_t write(float volume, size_t bytesToWrite, IReader &reader) override {
        (void)volume;
        const AutoMutex lock(mFrameCountersMutex);

        const size_t waitFrames = calcWaitFramesNowLocked(bytesToWrite / mFrameSize);
        const auto blockUntil =
            std::chrono::high_resolution_clock::now() +
                + std::chrono::microseconds(waitFrames * 1000000 / mSampleRateHz);
        std::this_thread::sleep_until(blockUntil);

        while (bytesToWrite > 0) {
            size_t chunkSize =
                std::min(bytesToWrite, sizeof(mWriteBuffer)) / mFrameSize * mFrameSize;
            chunkSize = reader(mWriteBuffer, chunkSize);
            if (chunkSize > 0) {
                mReceivedFrames += chunkSize / mFrameSize;
                bytesToWrite -= chunkSize;
            } else {
                break; // reader failed
            }
        }

        return 0;
    }

    static std::unique_ptr<NullSink> create(const AudioConfig &cfg,
                                            size_t readerBufferSizeHint,
                                            uint64_t &frames) {
        (void)readerBufferSizeHint;
        return std::make_unique<NullSink>(cfg, frames);
    }

private:
    const nsecs_t mStartNs;
    const unsigned mSampleRateHz;
    const unsigned mFrameSize;
    const uint64_t mInitialFrames;
    uint64_t &mFrames GUARDED_BY(mFrameCountersMutex);
    uint64_t mMissedFrames GUARDED_BY(mFrameCountersMutex) = 0;
    uint64_t mReceivedFrames GUARDED_BY(mFrameCountersMutex) = 0;
    char mWriteBuffer[1024];
    mutable Mutex mFrameCountersMutex;
};

}  // namespace

std::unique_ptr<DevicePortSink>
DevicePortSink::create(size_t readerBufferSizeHint,
                       const DeviceAddress &address,
                       const AudioConfig &cfg,
                       const hidl_vec<AudioInOutFlag> &flags,
                       uint64_t &frames) {
    (void)flags;

    if (xsd::stringToAudioFormat(cfg.base.format) != xsd::AudioFormat::AUDIO_FORMAT_PCM_16_BIT) {
        ALOGE("%s:%d, unexpected format: '%s'", __func__, __LINE__, cfg.base.format.c_str());
        return FAILURE(nullptr);
    }

    if (GetBoolProperty("ro.boot.audio.tinyalsa.ignore_output", false)) {
        goto nullsink;
    }

    switch (xsd::stringToAudioDevice(address.deviceType)) {
    case xsd::AudioDevice::AUDIO_DEVICE_OUT_DEFAULT:
    case xsd::AudioDevice::AUDIO_DEVICE_OUT_SPEAKER:
        {
            auto sinkptr = TinyalsaSink::create(talsa::kPcmCard, talsa::kPcmDevice,
                                                cfg, readerBufferSizeHint, frames);
            if (sinkptr != nullptr) {
                return sinkptr;
            } else {
                ALOGW("%s:%d failed to create alsa sink for '%s'; creating NullSink instead.",
                      __func__, __LINE__, address.deviceType.c_str());
            }
        }
        break;

    case xsd::AudioDevice::AUDIO_DEVICE_OUT_TELEPHONY_TX:
    case xsd::AudioDevice::AUDIO_DEVICE_OUT_BUS:
        ALOGW("%s:%d creating NullSink for '%s'.", __func__, __LINE__, address.deviceType.c_str());
        break;

    default:
        ALOGW("%s:%d unsupported device: '%s', creating NullSink", __func__, __LINE__, address.deviceType.c_str());
        break;
    }

nullsink:
    return NullSink::create(cfg, readerBufferSizeHint, frames);
}

bool DevicePortSink::validateDeviceAddress(const DeviceAddress& address) {
    switch (xsd::stringToAudioDevice(address.deviceType)) {
    default:
        ALOGW("%s:%d unsupported device: '%s'", __func__, __LINE__, address.deviceType.c_str());
        return FAILURE(false);

    case xsd::AudioDevice::AUDIO_DEVICE_OUT_DEFAULT:
    case xsd::AudioDevice::AUDIO_DEVICE_OUT_SPEAKER:
    case xsd::AudioDevice::AUDIO_DEVICE_OUT_TELEPHONY_TX:
    case xsd::AudioDevice::AUDIO_DEVICE_OUT_BUS:
        break;
    }

    return true;
}

}  // namespace implementation
}  // namespace CPP_VERSION
}  // namespace audio
}  // namespace hardware
}  // namespace android