• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #include <log/log.h>
18 #include <utils/Timers.h>
19 #include "device_port_sink.h"
20 #include "talsa.h"
21 #include "util.h"
22 #include "debug.h"
23 
24 namespace android {
25 namespace hardware {
26 namespace audio {
27 namespace V6_0 {
28 namespace implementation {
29 
30 namespace {
31 
32 struct TinyalsaSink : public DevicePortSink {
TinyalsaSinkandroid::hardware::audio::V6_0::implementation::__anon2786f88e0111::TinyalsaSink33     TinyalsaSink(unsigned pcmCard, unsigned pcmDevice,
34                  const AudioConfig &cfg, uint64_t &frames)
35             : mFrames(frames)
36             , mPcm(talsa::pcmOpen(pcmCard, pcmDevice,
37                                   util::countChannels(cfg.channelMask),
38                                   cfg.sampleRateHz,
39                                   cfg.frameCount,
40                                   true /* isOut */)) {}
41 
getPresentationPositionandroid::hardware::audio::V6_0::implementation::__anon2786f88e0111::TinyalsaSink42     Result getPresentationPosition(uint64_t &frames, TimeSpec &ts) override {
43         frames = mFrames;
44         ts = util::nsecs2TimeSpec(systemTime(SYSTEM_TIME_MONOTONIC));
45         return Result::OK;
46     }
47 
writeandroid::hardware::audio::V6_0::implementation::__anon2786f88e0111::TinyalsaSink48     int write(const void *data, size_t nBytes) override {
49         const int res = ::pcm_write(mPcm.get(), data, nBytes);
50         if (res < 0) {
51             return FAILURE(res);
52         } else if (res == 0) {
53             mFrames += ::pcm_bytes_to_frames(mPcm.get(), nBytes);
54             return nBytes;
55         } else {
56             mFrames += ::pcm_bytes_to_frames(mPcm.get(), res);
57             return res;
58         }
59     }
60 
createandroid::hardware::audio::V6_0::implementation::__anon2786f88e0111::TinyalsaSink61     static std::unique_ptr<TinyalsaSink> create(unsigned pcmCard,
62                                                 unsigned pcmDevice,
63                                                 const AudioConfig &cfg,
64                                                 uint64_t &frames) {
65         auto src = std::make_unique<TinyalsaSink>(pcmCard, pcmDevice, cfg, frames);
66         if (src->mPcm) {
67             return src;
68         } else {
69             return FAILURE(nullptr);
70         }
71     }
72 
73 private:
74     uint64_t &mFrames;
75     talsa::PcmPtr mPcm;
76 };
77 
78 struct NullSink : public DevicePortSink {
NullSinkandroid::hardware::audio::V6_0::implementation::__anon2786f88e0111::NullSink79     NullSink(const AudioConfig &cfg, uint64_t &frames)
80             : mFrames(frames)
81             , mSampleRateHz(cfg.sampleRateHz)
82             , mNChannels(util::countChannels(cfg.channelMask))
83             , mTimestamp(systemTime(SYSTEM_TIME_MONOTONIC)) {}
84 
getPresentationPositionandroid::hardware::audio::V6_0::implementation::__anon2786f88e0111::NullSink85     Result getPresentationPosition(uint64_t &frames, TimeSpec &ts) override {
86         simulatePresentationPosition();
87         frames = mFrames;
88         ts = util::nsecs2TimeSpec(mTimestamp);
89         return Result::OK;
90     }
91 
writeandroid::hardware::audio::V6_0::implementation::__anon2786f88e0111::NullSink92     int write(const void *, size_t nBytes) override {
93         simulatePresentationPosition();
94         mAvailableFrames += nBytes / mNChannels / sizeof(int16_t);
95         return nBytes;
96     }
97 
simulatePresentationPositionandroid::hardware::audio::V6_0::implementation::__anon2786f88e0111::NullSink98     void simulatePresentationPosition() {
99         const nsecs_t nowNs = systemTime(SYSTEM_TIME_MONOTONIC);
100         const nsecs_t deltaNs = nowNs - mTimestamp;
101         const uint64_t deltaFrames = uint64_t(mSampleRateHz) * ns2ms(deltaNs) / 1000;
102         const uint64_t f = std::min(deltaFrames, mAvailableFrames);
103 
104         mFrames += f;
105         mAvailableFrames -= f;
106         if (mAvailableFrames) {
107             mTimestamp += us2ns(f * 1000000 / mSampleRateHz);
108         } else {
109             mTimestamp = nowNs;
110         }
111     }
112 
createandroid::hardware::audio::V6_0::implementation::__anon2786f88e0111::NullSink113     static std::unique_ptr<NullSink> create(const AudioConfig &cfg,
114                                             uint64_t &frames) {
115         return std::make_unique<NullSink>(cfg, frames);
116     }
117 
118 private:
119     uint64_t &mFrames;
120     const unsigned mSampleRateHz;
121     const unsigned mNChannels;
122     uint64_t mAvailableFrames = 0;
123     nsecs_t mTimestamp;
124 };
125 
126 }  // namespace
127 
128 std::unique_ptr<DevicePortSink>
create(const DeviceAddress & address,const AudioConfig & cfg,const hidl_bitfield<AudioOutputFlag> & flags,uint64_t & frames)129 DevicePortSink::create(const DeviceAddress &address,
130                        const AudioConfig &cfg,
131                        const hidl_bitfield<AudioOutputFlag> &flags,
132                        uint64_t &frames) {
133     (void)flags;
134 
135     if (cfg.format != AudioFormat::PCM_16_BIT) {
136         ALOGE("%s:%d Only PCM_16_BIT is supported", __func__, __LINE__);
137         return FAILURE(nullptr);
138     }
139 
140     switch (address.device) {
141     case AudioDevice::OUT_SPEAKER:
142         return TinyalsaSink::create(talsa::kPcmCard, talsa::kPcmDevice,
143                                     cfg, frames);
144 
145     case AudioDevice::OUT_TELEPHONY_TX:
146         return NullSink::create(cfg, frames);
147 
148     default:
149         ALOGE("%s:%d unsupported device: %x", __func__, __LINE__, address.device);
150         return FAILURE(nullptr);
151     }
152 }
153 
154 }  // namespace implementation
155 }  // namespace V6_0
156 }  // namespace audio
157 }  // namespace hardware
158 }  // namespace android
159