blob: 92b95e9adec9b640f7123c324b86a349dc11222c [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
19#include <chrono>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053020#include <mutex>
21
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 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:
64 AudioConfig mPipeConfig;
65
66 bool isStreamInOpen() {
67 std::lock_guard guard(mLock);
68 return mStreamInOpen;
69 }
70 bool getStreamInStandby() {
71 std::lock_guard guard(mLock);
72 return mStreamInStandby;
73 }
74 bool isStreamOutOpen() {
75 std::lock_guard guard(mLock);
76 return mStreamOutOpen;
77 }
78 bool getStreamOutStandby() {
79 std::lock_guard guard(mLock);
80 return mStreamOutStandby;
81 }
82 long getReadCounterFrames() {
83 std::lock_guard guard(mLock);
84 return mReadCounterFrames;
85 }
86 int getReadErrorCount() {
87 std::lock_guard guard(mLock);
88 return mReadErrorCount;
89 }
90 std::chrono::time_point<std::chrono::steady_clock> getRecordStartTime() {
91 std::lock_guard guard(mLock);
92 return mRecordStartTime;
93 }
94 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 }
102
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530103 bool isStreamConfigValid(bool isInput, const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530104 void closeStream(bool isInput);
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530105 ::android::status_t createPipe(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530106 void exitStandby(bool isInput);
107 bool hasAtleastOneStreamOpen();
108 int notifyReadError();
109 void openStream(bool isInput);
110 void releasePipe();
111 ::android::status_t resetPipe();
112 bool shouldBlockWrite();
113 void standby(bool isInput);
114 long updateReadCounterFrames(size_t frameCount);
115
116 private:
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530117 bool isStreamConfigCompatible(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530118
119 std::mutex mLock;
120
121 bool mStreamInOpen GUARDED_BY(mLock) = false;
122 int mInputRefCount GUARDED_BY(mLock) = 0;
123 bool mStreamInStandby GUARDED_BY(mLock) = true;
124 bool mStreamOutStandbyTransition GUARDED_BY(mLock) = false;
125 bool mStreamOutOpen GUARDED_BY(mLock) = false;
126 bool mStreamOutStandby GUARDED_BY(mLock) = true;
127 // how many frames have been requested to be read since standby
128 long mReadCounterFrames GUARDED_BY(mLock) = 0;
129 int mReadErrorCount GUARDED_BY(mLock) = 0;
130 // wall clock when recording starts
131 std::chrono::time_point<std::chrono::steady_clock> mRecordStartTime GUARDED_BY(mLock);
132
133 // Pipe variables: they handle the ring buffer that "pipes" audio:
134 // - from the submix virtual audio output == what needs to be played
135 // remotely, seen as an output for the client
136 // - to the virtual audio source == what is captured by the component
137 // which "records" the submix / virtual audio source, and handles it as needed.
138 // A usecase example is one where the component capturing the audio is then sending it over
139 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
140 // TV with Wifi Display capabilities), or to a wireless audio player.
141 sp<MonoPipe> mSink GUARDED_BY(mLock);
142 sp<MonoPipeReader> mSource GUARDED_BY(mLock);
143};
144
145} // namespace aidl::android::hardware::audio::core::r_submix