1 /*
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 
17 #pragma once
18 
19 #include <vector>
20 
21 #include "core-impl/Stream.h"
22 #include "core-impl/StreamSwitcher.h"
23 #include "r_submix/SubmixRoute.h"
24 
25 namespace aidl::android::hardware::audio::core {
26 
27 class StreamRemoteSubmix : public StreamCommonImpl {
28   public:
29     StreamRemoteSubmix(
30             StreamContext* context, const Metadata& metadata,
31             const ::aidl::android::media::audio::common::AudioDeviceAddress& deviceAddress);
32 
33     ::android::status_t init() override;
34     ::android::status_t drain(StreamDescriptor::DrainMode) override;
35     ::android::status_t flush() override;
36     ::android::status_t pause() override;
37     ::android::status_t standby() override;
38     ::android::status_t start() override;
39     ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
40                                  int32_t* latencyMs) override;
41     ::android::status_t refinePosition(StreamDescriptor::Position* position) override;
42     void shutdown() override;
43 
44     // Overridden methods of 'StreamCommonImpl', called on a Binder thread.
45     ndk::ScopedAStatus prepareToClose() override;
46 
47   private:
48     long getDelayInUsForFrameCount(size_t frameCount);
49     size_t getStreamPipeSizeInFrames();
50     ::android::status_t outWrite(void* buffer, size_t frameCount, size_t* actualFrameCount);
51     ::android::status_t inRead(void* buffer, size_t frameCount, size_t* actualFrameCount);
52 
53     const ::aidl::android::media::audio::common::AudioDeviceAddress mDeviceAddress;
54     const bool mIsInput;
55     r_submix::AudioConfig mStreamConfig;
56     std::shared_ptr<r_submix::SubmixRoute> mCurrentRoute = nullptr;
57 
58     // Limit for the number of error log entries to avoid spamming the logs.
59     static constexpr int kMaxErrorLogs = 5;
60     // The duration of kMaxReadFailureAttempts * READ_ATTEMPT_SLEEP_MS must be strictly inferior
61     // to the duration of a record buffer at the current record sample rate (of the device, not of
62     // the recording itself). Here we have: 3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms
63     static constexpr int kMaxReadFailureAttempts = 3;
64     // 5ms between two read attempts when pipe is empty
65     static constexpr int kReadAttemptSleepUs = 5000;
66 
67     int64_t mStartTimeNs = 0;
68     long mFramesSinceStart = 0;
69     int mReadErrorCount = 0;
70     int mReadFailureCount = 0;
71     int mWriteShutdownCount = 0;
72 };
73 
74 class StreamInRemoteSubmix final : public StreamIn, public StreamSwitcher {
75   public:
76     friend class ndk::SharedRefBase;
77     StreamInRemoteSubmix(
78             StreamContext&& context,
79             const ::aidl::android::hardware::audio::common::SinkMetadata& sinkMetadata,
80             const std::vector<::aidl::android::media::audio::common::MicrophoneInfo>& microphones);
81 
82   private:
83     DeviceSwitchBehavior switchCurrentStream(
84             const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices)
85             override;
86     std::unique_ptr<StreamCommonInterfaceEx> createNewStream(
87             const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices,
88             StreamContext* context, const Metadata& metadata) override;
onClose(StreamDescriptor::State)89     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
90     ndk::ScopedAStatus getActiveMicrophones(
91             std::vector<::aidl::android::media::audio::common::MicrophoneDynamicInfo>* _aidl_return)
92             override;
93 };
94 
95 class StreamOutRemoteSubmix final : public StreamOut, public StreamSwitcher {
96   public:
97     friend class ndk::SharedRefBase;
98     StreamOutRemoteSubmix(
99             StreamContext&& context,
100             const ::aidl::android::hardware::audio::common::SourceMetadata& sourceMetadata,
101             const std::optional<::aidl::android::media::audio::common::AudioOffloadInfo>&
102                     offloadInfo);
103 
104   private:
105     DeviceSwitchBehavior switchCurrentStream(
106             const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices)
107             override;
108     std::unique_ptr<StreamCommonInterfaceEx> createNewStream(
109             const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices,
110             StreamContext* context, const Metadata& metadata) override;
onClose(StreamDescriptor::State)111     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
112 };
113 
114 }  // namespace aidl::android::hardware::audio::core
115