blob: 5425f12e1722938ca5aa356cceccb8be1cd61dd4 [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
28#include <aidl/android/media/audio/common/AudioChannelLayout.h>
Mikhail Naganov3b732892023-12-20 16:05:01 -080029#include <aidl/android/media/audio/common/AudioDeviceAddress.h>
Mikhail Naganov2aab7662023-10-24 13:56:07 -070030#include <aidl/android/media/audio/common/AudioFormatDescription.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053031
32using aidl::android::media::audio::common::AudioChannelLayout;
33using aidl::android::media::audio::common::AudioFormatDescription;
34using aidl::android::media::audio::common::AudioFormatType;
35using aidl::android::media::audio::common::PcmType;
36using ::android::MonoPipe;
37using ::android::MonoPipeReader;
38using ::android::sp;
39
40namespace aidl::android::hardware::audio::core::r_submix {
41
42static constexpr int kDefaultSampleRateHz = 48000;
Mikhail Naganov13501872023-10-18 16:15:46 -070043// Value used to divide the MonoPipe buffer into segments that are written to the source and
44// read from the sink. The maximum latency of the device is the size of the MonoPipe's buffer
45// the minimum latency is the MonoPipe buffer size divided by this value.
46static constexpr int kDefaultPipePeriodCount = 4;
47// Size at the default sample rate
48// NOTE: This value will be rounded up to the nearest power of 2 by MonoPipe.
49static constexpr int kDefaultPipeSizeInFrames = 1024 * kDefaultPipePeriodCount;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053050
51// Configuration of the audio stream.
52struct AudioConfig {
53 int sampleRate = kDefaultSampleRateHz;
54 AudioFormatDescription format =
55 AudioFormatDescription{.type = AudioFormatType::PCM, .pcm = PcmType::INT_16_BIT};
56 AudioChannelLayout channelLayout =
57 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(
58 AudioChannelLayout::LAYOUT_STEREO);
59 size_t frameSize;
60 size_t frameCount;
61};
62
63class SubmixRoute {
64 public:
Mikhail Naganov3b732892023-12-20 16:05:01 -080065 static std::shared_ptr<SubmixRoute> findOrCreateRoute(
66 const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress,
67 const AudioConfig& pipeConfig);
68 static std::shared_ptr<SubmixRoute> findRoute(
69 const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress);
70 static void removeRoute(
71 const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress);
Mikhail Naganov9eb33142024-01-09 14:06:49 -080072 static std::string dumpRoutes();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053073
74 bool isStreamInOpen() {
75 std::lock_guard guard(mLock);
76 return mStreamInOpen;
77 }
78 bool getStreamInStandby() {
79 std::lock_guard guard(mLock);
80 return mStreamInStandby;
81 }
82 bool isStreamOutOpen() {
83 std::lock_guard guard(mLock);
84 return mStreamOutOpen;
85 }
86 bool getStreamOutStandby() {
87 std::lock_guard guard(mLock);
88 return mStreamOutStandby;
89 }
90 long getReadCounterFrames() {
91 std::lock_guard guard(mLock);
92 return mReadCounterFrames;
93 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053094 sp<MonoPipe> getSink() {
95 std::lock_guard guard(mLock);
96 return mSink;
97 }
98 sp<MonoPipeReader> getSource() {
99 std::lock_guard guard(mLock);
100 return mSource;
101 }
Mikhail Naganov3b732892023-12-20 16:05:01 -0800102 AudioConfig getPipeConfig() {
103 std::lock_guard guard(mLock);
104 return mPipeConfig;
105 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530106
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530107 bool isStreamConfigValid(bool isInput, const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530108 void closeStream(bool isInput);
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530109 ::android::status_t createPipe(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530110 void exitStandby(bool isInput);
111 bool hasAtleastOneStreamOpen();
112 int notifyReadError();
113 void openStream(bool isInput);
Mikhail Naganov3b732892023-12-20 16:05:01 -0800114 AudioConfig releasePipe();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530115 ::android::status_t resetPipe();
116 bool shouldBlockWrite();
117 void standby(bool isInput);
118 long updateReadCounterFrames(size_t frameCount);
119
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800120 std::string dump();
121
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530122 private:
Mikhail Naganov3b732892023-12-20 16:05:01 -0800123 using RoutesMap = std::map<::aidl::android::media::audio::common::AudioDeviceAddress,
124 std::shared_ptr<r_submix::SubmixRoute>>;
125 class RoutesMonitor {
126 public:
127 RoutesMonitor(std::mutex& mutex, RoutesMap& routes) : mLock(mutex), mRoutes(routes) {}
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800128 RoutesMonitor(std::mutex& mutex, RoutesMap& routes, bool /*tryLock*/)
129 : mLock(mutex, std::try_to_lock), mRoutes(routes) {}
Mikhail Naganov3b732892023-12-20 16:05:01 -0800130 RoutesMap* operator->() { return &mRoutes; }
131
132 private:
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800133 std::unique_lock<std::mutex> mLock;
Mikhail Naganov3b732892023-12-20 16:05:01 -0800134 RoutesMap& mRoutes;
135 };
136
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800137 static RoutesMonitor getRoutes(bool tryLock = false);
Mikhail Naganov3b732892023-12-20 16:05:01 -0800138
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530139 bool isStreamConfigCompatible(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530140
141 std::mutex mLock;
Mikhail Naganov3b732892023-12-20 16:05:01 -0800142 AudioConfig mPipeConfig GUARDED_BY(mLock);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530143 bool mStreamInOpen GUARDED_BY(mLock) = false;
144 int mInputRefCount GUARDED_BY(mLock) = 0;
145 bool mStreamInStandby GUARDED_BY(mLock) = true;
146 bool mStreamOutStandbyTransition GUARDED_BY(mLock) = false;
147 bool mStreamOutOpen GUARDED_BY(mLock) = false;
148 bool mStreamOutStandby GUARDED_BY(mLock) = true;
149 // how many frames have been requested to be read since standby
150 long mReadCounterFrames GUARDED_BY(mLock) = 0;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530151
152 // Pipe variables: they handle the ring buffer that "pipes" audio:
153 // - from the submix virtual audio output == what needs to be played
154 // remotely, seen as an output for the client
155 // - to the virtual audio source == what is captured by the component
156 // which "records" the submix / virtual audio source, and handles it as needed.
157 // A usecase example is one where the component capturing the audio is then sending it over
158 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
159 // TV with Wifi Display capabilities), or to a wireless audio player.
160 sp<MonoPipe> mSink GUARDED_BY(mLock);
161 sp<MonoPipeReader> mSource GUARDED_BY(mLock);
162};
163
164} // namespace aidl::android::hardware::audio::core::r_submix