blob: 252b1c9524882fcbc73093d40f401fe885038673 [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 Naganov2aab7662023-10-24 13:56:07 -070028#include <aidl/android/media/audio/common/AudioFormatDescription.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053029
30using aidl::android::media::audio::common::AudioChannelLayout;
31using aidl::android::media::audio::common::AudioFormatDescription;
32using aidl::android::media::audio::common::AudioFormatType;
33using aidl::android::media::audio::common::PcmType;
34using ::android::MonoPipe;
35using ::android::MonoPipeReader;
36using ::android::sp;
37
38namespace aidl::android::hardware::audio::core::r_submix {
39
40static constexpr int kDefaultSampleRateHz = 48000;
Mikhail Naganov13501872023-10-18 16:15:46 -070041// Value used to divide the MonoPipe buffer into segments that are written to the source and
42// read from the sink. The maximum latency of the device is the size of the MonoPipe's buffer
43// the minimum latency is the MonoPipe buffer size divided by this value.
44static constexpr int kDefaultPipePeriodCount = 4;
45// Size at the default sample rate
46// NOTE: This value will be rounded up to the nearest power of 2 by MonoPipe.
47static constexpr int kDefaultPipeSizeInFrames = 1024 * kDefaultPipePeriodCount;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053048
49// Configuration of the audio stream.
50struct AudioConfig {
51 int sampleRate = kDefaultSampleRateHz;
52 AudioFormatDescription format =
53 AudioFormatDescription{.type = AudioFormatType::PCM, .pcm = PcmType::INT_16_BIT};
54 AudioChannelLayout channelLayout =
55 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(
56 AudioChannelLayout::LAYOUT_STEREO);
57 size_t frameSize;
58 size_t frameCount;
59};
60
61class SubmixRoute {
62 public:
63 AudioConfig mPipeConfig;
64
65 bool isStreamInOpen() {
66 std::lock_guard guard(mLock);
67 return mStreamInOpen;
68 }
69 bool getStreamInStandby() {
70 std::lock_guard guard(mLock);
71 return mStreamInStandby;
72 }
73 bool isStreamOutOpen() {
74 std::lock_guard guard(mLock);
75 return mStreamOutOpen;
76 }
77 bool getStreamOutStandby() {
78 std::lock_guard guard(mLock);
79 return mStreamOutStandby;
80 }
81 long getReadCounterFrames() {
82 std::lock_guard guard(mLock);
83 return mReadCounterFrames;
84 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053085 sp<MonoPipe> getSink() {
86 std::lock_guard guard(mLock);
87 return mSink;
88 }
89 sp<MonoPipeReader> getSource() {
90 std::lock_guard guard(mLock);
91 return mSource;
92 }
93
Shraddha Basantwani7770c152023-07-19 16:43:50 +053094 bool isStreamConfigValid(bool isInput, const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053095 void closeStream(bool isInput);
Shraddha Basantwani7770c152023-07-19 16:43:50 +053096 ::android::status_t createPipe(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053097 void exitStandby(bool isInput);
98 bool hasAtleastOneStreamOpen();
99 int notifyReadError();
100 void openStream(bool isInput);
101 void releasePipe();
102 ::android::status_t resetPipe();
103 bool shouldBlockWrite();
104 void standby(bool isInput);
105 long updateReadCounterFrames(size_t frameCount);
106
107 private:
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530108 bool isStreamConfigCompatible(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530109
110 std::mutex mLock;
111
112 bool mStreamInOpen GUARDED_BY(mLock) = false;
113 int mInputRefCount GUARDED_BY(mLock) = 0;
114 bool mStreamInStandby GUARDED_BY(mLock) = true;
115 bool mStreamOutStandbyTransition GUARDED_BY(mLock) = false;
116 bool mStreamOutOpen GUARDED_BY(mLock) = false;
117 bool mStreamOutStandby GUARDED_BY(mLock) = true;
118 // how many frames have been requested to be read since standby
119 long mReadCounterFrames GUARDED_BY(mLock) = 0;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530120
121 // Pipe variables: they handle the ring buffer that "pipes" audio:
122 // - from the submix virtual audio output == what needs to be played
123 // remotely, seen as an output for the client
124 // - to the virtual audio source == what is captured by the component
125 // which "records" the submix / virtual audio source, and handles it as needed.
126 // A usecase example is one where the component capturing the audio is then sending it over
127 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
128 // TV with Wifi Display capabilities), or to a wireless audio player.
129 sp<MonoPipe> mSink GUARDED_BY(mLock);
130 sp<MonoPipeReader> mSource GUARDED_BY(mLock);
131};
132
133} // namespace aidl::android::hardware::audio::core::r_submix