blob: 0097f3925cd3a1dde1aab282bc330e2f25f4306a [file] [log] [blame]
Shraddha Basantwani6bb69632023-04-25 15:26:38 +05301/*
2 * Copyright (C) 2023 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
Mikhail Naganov2aab7662023-10-24 13:56:07 -070017#pragma once
18
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053019#include <mutex>
Mikhail Naganov9eb33142024-01-09 14:06:49 -080020#include <string>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053021
Mikhail Naganov2aab7662023-10-24 13:56:07 -070022#include <android-base/thread_annotations.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053023#include <audio_utils/clock.h>
24
25#include <media/nbaio/MonoPipe.h>
26#include <media/nbaio/MonoPipeReader.h>
27
Mikhail Naganove9766252024-10-03 16:56:06 -070028#include <Utils.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053029#include <aidl/android/media/audio/common/AudioChannelLayout.h>
Mikhail Naganov3b732892023-12-20 16:05:01 -080030#include <aidl/android/media/audio/common/AudioDeviceAddress.h>
Mikhail Naganov2aab7662023-10-24 13:56:07 -070031#include <aidl/android/media/audio/common/AudioFormatDescription.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053032
Mikhail Naganove9766252024-10-03 16:56:06 -070033using aidl::android::hardware::audio::common::getFrameSizeInBytes;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053034using aidl::android::media::audio::common::AudioChannelLayout;
35using aidl::android::media::audio::common::AudioFormatDescription;
36using aidl::android::media::audio::common::AudioFormatType;
37using aidl::android::media::audio::common::PcmType;
38using ::android::MonoPipe;
39using ::android::MonoPipeReader;
40using ::android::sp;
41
42namespace aidl::android::hardware::audio::core::r_submix {
43
44static constexpr int kDefaultSampleRateHz = 48000;
Mikhail Naganov13501872023-10-18 16:15:46 -070045// Value used to divide the MonoPipe buffer into segments that are written to the source and
46// read from the sink. The maximum latency of the device is the size of the MonoPipe's buffer
47// the minimum latency is the MonoPipe buffer size divided by this value.
48static constexpr int kDefaultPipePeriodCount = 4;
49// Size at the default sample rate
50// NOTE: This value will be rounded up to the nearest power of 2 by MonoPipe.
51static constexpr int kDefaultPipeSizeInFrames = 1024 * kDefaultPipePeriodCount;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053052
53// Configuration of the audio stream.
54struct AudioConfig {
55 int sampleRate = kDefaultSampleRateHz;
56 AudioFormatDescription format =
57 AudioFormatDescription{.type = AudioFormatType::PCM, .pcm = PcmType::INT_16_BIT};
58 AudioChannelLayout channelLayout =
59 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(
60 AudioChannelLayout::LAYOUT_STEREO);
Mikhail Naganove9766252024-10-03 16:56:06 -070061 size_t frameSize = getFrameSizeInBytes(format, channelLayout);
62 size_t frameCount = 0;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053063};
64
65class SubmixRoute {
66 public:
Mikhail Naganov3b732892023-12-20 16:05:01 -080067 static std::shared_ptr<SubmixRoute> findOrCreateRoute(
68 const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress,
69 const AudioConfig& pipeConfig);
70 static std::shared_ptr<SubmixRoute> findRoute(
71 const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress);
72 static void removeRoute(
73 const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress);
Mikhail Naganov9eb33142024-01-09 14:06:49 -080074 static std::string dumpRoutes();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053075
76 bool isStreamInOpen() {
77 std::lock_guard guard(mLock);
78 return mStreamInOpen;
79 }
80 bool getStreamInStandby() {
81 std::lock_guard guard(mLock);
82 return mStreamInStandby;
83 }
84 bool isStreamOutOpen() {
85 std::lock_guard guard(mLock);
86 return mStreamOutOpen;
87 }
88 bool getStreamOutStandby() {
89 std::lock_guard guard(mLock);
90 return mStreamOutStandby;
91 }
92 long getReadCounterFrames() {
93 std::lock_guard guard(mLock);
94 return mReadCounterFrames;
95 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053096 sp<MonoPipe> getSink() {
97 std::lock_guard guard(mLock);
98 return mSink;
99 }
100 sp<MonoPipeReader> getSource() {
101 std::lock_guard guard(mLock);
102 return mSource;
103 }
Mikhail Naganov3b732892023-12-20 16:05:01 -0800104 AudioConfig getPipeConfig() {
105 std::lock_guard guard(mLock);
106 return mPipeConfig;
107 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530108
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530109 bool isStreamConfigValid(bool isInput, const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530110 void closeStream(bool isInput);
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530111 ::android::status_t createPipe(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530112 void exitStandby(bool isInput);
113 bool hasAtleastOneStreamOpen();
114 int notifyReadError();
115 void openStream(bool isInput);
Mikhail Naganov3b732892023-12-20 16:05:01 -0800116 AudioConfig releasePipe();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530117 ::android::status_t resetPipe();
118 bool shouldBlockWrite();
119 void standby(bool isInput);
120 long updateReadCounterFrames(size_t frameCount);
121
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800122 std::string dump();
123
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530124 private:
Mikhail Naganov3b732892023-12-20 16:05:01 -0800125 using RoutesMap = std::map<::aidl::android::media::audio::common::AudioDeviceAddress,
126 std::shared_ptr<r_submix::SubmixRoute>>;
127 class RoutesMonitor {
128 public:
129 RoutesMonitor(std::mutex& mutex, RoutesMap& routes) : mLock(mutex), mRoutes(routes) {}
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800130 RoutesMonitor(std::mutex& mutex, RoutesMap& routes, bool /*tryLock*/)
131 : mLock(mutex, std::try_to_lock), mRoutes(routes) {}
Mikhail Naganov3b732892023-12-20 16:05:01 -0800132 RoutesMap* operator->() { return &mRoutes; }
133
134 private:
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800135 std::unique_lock<std::mutex> mLock;
Mikhail Naganov3b732892023-12-20 16:05:01 -0800136 RoutesMap& mRoutes;
137 };
138
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800139 static RoutesMonitor getRoutes(bool tryLock = false);
Mikhail Naganov3b732892023-12-20 16:05:01 -0800140
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530141 bool isStreamConfigCompatible(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530142
143 std::mutex mLock;
Mikhail Naganov3b732892023-12-20 16:05:01 -0800144 AudioConfig mPipeConfig GUARDED_BY(mLock);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530145 bool mStreamInOpen GUARDED_BY(mLock) = false;
146 int mInputRefCount GUARDED_BY(mLock) = 0;
147 bool mStreamInStandby GUARDED_BY(mLock) = true;
148 bool mStreamOutStandbyTransition GUARDED_BY(mLock) = false;
149 bool mStreamOutOpen GUARDED_BY(mLock) = false;
150 bool mStreamOutStandby GUARDED_BY(mLock) = true;
151 // how many frames have been requested to be read since standby
152 long mReadCounterFrames GUARDED_BY(mLock) = 0;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530153
154 // Pipe variables: they handle the ring buffer that "pipes" audio:
155 // - from the submix virtual audio output == what needs to be played
156 // remotely, seen as an output for the client
157 // - to the virtual audio source == what is captured by the component
158 // which "records" the submix / virtual audio source, and handles it as needed.
159 // A usecase example is one where the component capturing the audio is then sending it over
160 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
161 // TV with Wifi Display capabilities), or to a wireless audio player.
162 sp<MonoPipe> mSink GUARDED_BY(mLock);
163 sp<MonoPipeReader> mSource GUARDED_BY(mLock);
164};
165
166} // namespace aidl::android::hardware::audio::core::r_submix