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 "AudioHwDevice.h"
20 #include <media/audiohal/DeviceHalInterface.h>
21 #include <media/audiohal/StreamHalInterface.h>
22 
23 namespace android {
24 
25 // Abstraction for the Audio Source for the RecordThread (HAL or PassthruPatchRecord).
26 struct Source {
27     virtual ~Source() = default;
28     // The following methods have the same signatures as in StreamHalInterface.
29     virtual status_t read(void* buffer, size_t bytes, size_t* read) = 0;
30     virtual status_t getCapturePosition(int64_t* frames, int64_t* time) = 0;
31     virtual status_t standby() = 0;
32 };
33 
34 /**
35  * Managed access to a HAL input stream.
36  */
37 class AudioStreamIn : public Source {
38 public:
39     const AudioHwDevice* const audioHwDev;
40     sp<StreamInHalInterface> stream;
41     const audio_input_flags_t flags;
42 
43     [[nodiscard]] sp<DeviceHalInterface> hwDev() const;
44 
45     AudioStreamIn(AudioHwDevice *dev, audio_input_flags_t flags);
46 
47     virtual status_t open(
48             audio_io_handle_t handle,
49             audio_devices_t deviceType,
50             struct audio_config *config,
51             const char *address,
52             audio_source_t source,
53             audio_devices_t outputDevice,
54             const char *outputDeviceAddress);
55 
56     ~AudioStreamIn() override;
57 
58     status_t getCapturePosition(int64_t* frames, int64_t* time) override;
59 
60     status_t read(void* buffer, size_t bytes, size_t* read) override;
61 
62     /**
63      * @return frame size from the perspective of the application and the AudioFlinger.
64      */
getFrameSize()65     [[nodiscard]] virtual size_t getFrameSize() const { return mHalFrameSize; }
66 
67     /**
68      * @return audio stream configuration: channel mask, format, sample rate:
69      *   - channel mask from the perspective of the application and the AudioFlinger,
70      *     The HAL is in stereo mode when playing multi-channel compressed audio over HDMI;
71      *   - format from the perspective of the application and the AudioFlinger;
72      *   - sample rate from the perspective of the application and the AudioFlinger,
73      *     The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3.
74      */
75     [[nodiscard]] virtual audio_config_base_t getAudioProperties() const;
76 
77     status_t standby() override;
78 
79 protected:
80     uint64_t mFramesRead = 0;
81     int64_t mFramesReadAtStandby = 0;
82     int mRateMultiplier = 1;
83     bool mHalFormatHasProportionalFrames = false;
84     size_t mHalFrameSize = 0;
85 };
86 
87 }  // namespace android
88