blob: 160df412657e275e07e0a347eb974692115c5707 [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>
20
Mikhail Naganov2aab7662023-10-24 13:56:07 -070021#include <android-base/thread_annotations.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053022#include <audio_utils/clock.h>
23
24#include <media/nbaio/MonoPipe.h>
25#include <media/nbaio/MonoPipeReader.h>
26
27#include <aidl/android/media/audio/common/AudioChannelLayout.h>
Mikhail Naganov3b732892023-12-20 16:05:01 -080028#include <aidl/android/media/audio/common/AudioDeviceAddress.h>
Mikhail Naganov2aab7662023-10-24 13:56:07 -070029#include <aidl/android/media/audio/common/AudioFormatDescription.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053030
31using aidl::android::media::audio::common::AudioChannelLayout;
32using aidl::android::media::audio::common::AudioFormatDescription;
33using aidl::android::media::audio::common::AudioFormatType;
34using aidl::android::media::audio::common::PcmType;
35using ::android::MonoPipe;
36using ::android::MonoPipeReader;
37using ::android::sp;
38
39namespace aidl::android::hardware::audio::core::r_submix {
40
41static constexpr int kDefaultSampleRateHz = 48000;
Mikhail Naganov13501872023-10-18 16:15:46 -070042// Value used to divide the MonoPipe buffer into segments that are written to the source and
43// read from the sink. The maximum latency of the device is the size of the MonoPipe's buffer
44// the minimum latency is the MonoPipe buffer size divided by this value.
45static constexpr int kDefaultPipePeriodCount = 4;
46// Size at the default sample rate
47// NOTE: This value will be rounded up to the nearest power of 2 by MonoPipe.
48static constexpr int kDefaultPipeSizeInFrames = 1024 * kDefaultPipePeriodCount;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053049
50// Configuration of the audio stream.
51struct AudioConfig {
52 int sampleRate = kDefaultSampleRateHz;
53 AudioFormatDescription format =
54 AudioFormatDescription{.type = AudioFormatType::PCM, .pcm = PcmType::INT_16_BIT};
55 AudioChannelLayout channelLayout =
56 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(
57 AudioChannelLayout::LAYOUT_STEREO);
58 size_t frameSize;
59 size_t frameCount;
60};
61
62class SubmixRoute {
63 public:
Mikhail Naganov3b732892023-12-20 16:05:01 -080064 static std::shared_ptr<SubmixRoute> findOrCreateRoute(
65 const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress,
66 const AudioConfig& pipeConfig);
67 static std::shared_ptr<SubmixRoute> findRoute(
68 const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress);
69 static void removeRoute(
70 const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053071
72 bool isStreamInOpen() {
73 std::lock_guard guard(mLock);
74 return mStreamInOpen;
75 }
76 bool getStreamInStandby() {
77 std::lock_guard guard(mLock);
78 return mStreamInStandby;
79 }
80 bool isStreamOutOpen() {
81 std::lock_guard guard(mLock);
82 return mStreamOutOpen;
83 }
84 bool getStreamOutStandby() {
85 std::lock_guard guard(mLock);
86 return mStreamOutStandby;
87 }
88 long getReadCounterFrames() {
89 std::lock_guard guard(mLock);
90 return mReadCounterFrames;
91 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053092 sp<MonoPipe> getSink() {
93 std::lock_guard guard(mLock);
94 return mSink;
95 }
96 sp<MonoPipeReader> getSource() {
97 std::lock_guard guard(mLock);
98 return mSource;
99 }
Mikhail Naganov3b732892023-12-20 16:05:01 -0800100 AudioConfig getPipeConfig() {
101 std::lock_guard guard(mLock);
102 return mPipeConfig;
103 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530104
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530105 bool isStreamConfigValid(bool isInput, const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530106 void closeStream(bool isInput);
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530107 ::android::status_t createPipe(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530108 void exitStandby(bool isInput);
109 bool hasAtleastOneStreamOpen();
110 int notifyReadError();
111 void openStream(bool isInput);
Mikhail Naganov3b732892023-12-20 16:05:01 -0800112 AudioConfig releasePipe();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530113 ::android::status_t resetPipe();
114 bool shouldBlockWrite();
115 void standby(bool isInput);
116 long updateReadCounterFrames(size_t frameCount);
117
118 private:
Mikhail Naganov3b732892023-12-20 16:05:01 -0800119 using RoutesMap = std::map<::aidl::android::media::audio::common::AudioDeviceAddress,
120 std::shared_ptr<r_submix::SubmixRoute>>;
121 class RoutesMonitor {
122 public:
123 RoutesMonitor(std::mutex& mutex, RoutesMap& routes) : mLock(mutex), mRoutes(routes) {}
124 RoutesMap* operator->() { return &mRoutes; }
125
126 private:
127 std::lock_guard<std::mutex> mLock;
128 RoutesMap& mRoutes;
129 };
130
131 static RoutesMonitor getRoutes();
132
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530133 bool isStreamConfigCompatible(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530134
135 std::mutex mLock;
Mikhail Naganov3b732892023-12-20 16:05:01 -0800136 AudioConfig mPipeConfig GUARDED_BY(mLock);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530137 bool mStreamInOpen GUARDED_BY(mLock) = false;
138 int mInputRefCount GUARDED_BY(mLock) = 0;
139 bool mStreamInStandby GUARDED_BY(mLock) = true;
140 bool mStreamOutStandbyTransition GUARDED_BY(mLock) = false;
141 bool mStreamOutOpen GUARDED_BY(mLock) = false;
142 bool mStreamOutStandby GUARDED_BY(mLock) = true;
143 // how many frames have been requested to be read since standby
144 long mReadCounterFrames GUARDED_BY(mLock) = 0;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530145
146 // Pipe variables: they handle the ring buffer that "pipes" audio:
147 // - from the submix virtual audio output == what needs to be played
148 // remotely, seen as an output for the client
149 // - to the virtual audio source == what is captured by the component
150 // which "records" the submix / virtual audio source, and handles it as needed.
151 // A usecase example is one where the component capturing the audio is then sending it over
152 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
153 // TV with Wifi Display capabilities), or to a wireless audio player.
154 sp<MonoPipe> mSink GUARDED_BY(mLock);
155 sp<MonoPipeReader> mSource GUARDED_BY(mLock);
156};
157
158} // namespace aidl::android::hardware::audio::core::r_submix