blob: 1fe9ea21818f67315adebb1c992306378df5024e [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;
42// Size at default sample rate
43// NOTE: This value will be rounded up to the nearest power of 2 by MonoPipe().
44static constexpr int kDefaultPipeSizeInFrames = (1024 * 4);
45
46// Configuration of the audio stream.
47struct AudioConfig {
48 int sampleRate = kDefaultSampleRateHz;
49 AudioFormatDescription format =
50 AudioFormatDescription{.type = AudioFormatType::PCM, .pcm = PcmType::INT_16_BIT};
51 AudioChannelLayout channelLayout =
52 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(
53 AudioChannelLayout::LAYOUT_STEREO);
54 size_t frameSize;
55 size_t frameCount;
56};
57
58class SubmixRoute {
59 public:
60 AudioConfig mPipeConfig;
61
62 bool isStreamInOpen() {
63 std::lock_guard guard(mLock);
64 return mStreamInOpen;
65 }
66 bool getStreamInStandby() {
67 std::lock_guard guard(mLock);
68 return mStreamInStandby;
69 }
70 bool isStreamOutOpen() {
71 std::lock_guard guard(mLock);
72 return mStreamOutOpen;
73 }
74 bool getStreamOutStandby() {
75 std::lock_guard guard(mLock);
76 return mStreamOutStandby;
77 }
78 long getReadCounterFrames() {
79 std::lock_guard guard(mLock);
80 return mReadCounterFrames;
81 }
82 int getReadErrorCount() {
83 std::lock_guard guard(mLock);
84 return mReadErrorCount;
85 }
86 std::chrono::time_point<std::chrono::steady_clock> getRecordStartTime() {
87 std::lock_guard guard(mLock);
88 return mRecordStartTime;
89 }
90 sp<MonoPipe> getSink() {
91 std::lock_guard guard(mLock);
92 return mSink;
93 }
94 sp<MonoPipeReader> getSource() {
95 std::lock_guard guard(mLock);
96 return mSource;
97 }
98
Shraddha Basantwani7770c152023-07-19 16:43:50 +053099 bool isStreamConfigValid(bool isInput, const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530100 void closeStream(bool isInput);
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530101 ::android::status_t createPipe(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530102 void exitStandby(bool isInput);
103 bool hasAtleastOneStreamOpen();
104 int notifyReadError();
105 void openStream(bool isInput);
106 void releasePipe();
107 ::android::status_t resetPipe();
108 bool shouldBlockWrite();
109 void standby(bool isInput);
110 long updateReadCounterFrames(size_t frameCount);
111
112 private:
Shraddha Basantwani7770c152023-07-19 16:43:50 +0530113 bool isStreamConfigCompatible(const AudioConfig& streamConfig);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530114
115 std::mutex mLock;
116
117 bool mStreamInOpen GUARDED_BY(mLock) = false;
118 int mInputRefCount GUARDED_BY(mLock) = 0;
119 bool mStreamInStandby GUARDED_BY(mLock) = true;
120 bool mStreamOutStandbyTransition GUARDED_BY(mLock) = false;
121 bool mStreamOutOpen GUARDED_BY(mLock) = false;
122 bool mStreamOutStandby GUARDED_BY(mLock) = true;
123 // how many frames have been requested to be read since standby
124 long mReadCounterFrames GUARDED_BY(mLock) = 0;
125 int mReadErrorCount GUARDED_BY(mLock) = 0;
126 // wall clock when recording starts
127 std::chrono::time_point<std::chrono::steady_clock> mRecordStartTime GUARDED_BY(mLock);
128
129 // Pipe variables: they handle the ring buffer that "pipes" audio:
130 // - from the submix virtual audio output == what needs to be played
131 // remotely, seen as an output for the client
132 // - to the virtual audio source == what is captured by the component
133 // which "records" the submix / virtual audio source, and handles it as needed.
134 // A usecase example is one where the component capturing the audio is then sending it over
135 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
136 // TV with Wifi Display capabilities), or to a wireless audio player.
137 sp<MonoPipe> mSink GUARDED_BY(mLock);
138 sp<MonoPipeReader> mSource GUARDED_BY(mLock);
139};
140
141} // namespace aidl::android::hardware::audio::core::r_submix