1 /*
2 *
3 * Copyright 2023, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #define LOG_TAG "AudioFlinger"
19 //#define LOG_NDEBUG 0
20 #include "AudioStreamIn.h"
21
22 #include <media/audiohal/DeviceHalInterface.h>
23 #include <media/audiohal/StreamHalInterface.h>
24 #include <system/audio.h>
25 #include <utils/Log.h>
26
27 #include "AudioHwDevice.h"
28
29 namespace android {
30
31 // ----------------------------------------------------------------------------
AudioStreamIn(AudioHwDevice * dev,audio_input_flags_t flags)32 AudioStreamIn::AudioStreamIn(AudioHwDevice *dev, audio_input_flags_t flags)
33 : audioHwDev(dev)
34 , flags(flags)
35 {
36 }
37
38 // This must be defined here together with the HAL includes above and
39 // not solely in the header.
40 AudioStreamIn::~AudioStreamIn() = default;
41
hwDev() const42 sp<DeviceHalInterface> AudioStreamIn::hwDev() const
43 {
44 return audioHwDev->hwDevice();
45 }
46
getCapturePosition(int64_t * frames,int64_t * time)47 status_t AudioStreamIn::getCapturePosition(int64_t* frames, int64_t* time)
48 {
49 if (stream == nullptr) {
50 return NO_INIT;
51 }
52
53 int64_t halPosition = 0;
54 const status_t status = stream->getCapturePosition(&halPosition, time);
55 if (status != NO_ERROR) {
56 return status;
57 }
58
59 if (mHalFormatHasProportionalFrames &&
60 (flags & AUDIO_INPUT_FLAG_DIRECT) == AUDIO_INPUT_FLAG_DIRECT) {
61 // For DirectRecord reset position to 0 on standby.
62 const uint64_t adjustedPosition = (halPosition <= mFramesReadAtStandby) ?
63 0 : (halPosition - mFramesReadAtStandby);
64 // Scale from HAL sample rate to application rate.
65 *frames = adjustedPosition / mRateMultiplier;
66 } else {
67 // For compressed formats and linear PCM.
68 *frames = halPosition;
69 }
70
71 return status;
72 }
73
open(audio_io_handle_t handle,audio_devices_t deviceType,struct audio_config * config,const char * address,audio_source_t source,audio_devices_t outputDevice,const char * outputDeviceAddress)74 status_t AudioStreamIn::open(
75 audio_io_handle_t handle,
76 audio_devices_t deviceType,
77 struct audio_config *config,
78 const char *address,
79 audio_source_t source,
80 audio_devices_t outputDevice,
81 const char *outputDeviceAddress)
82 {
83 sp<StreamInHalInterface> inStream;
84
85 int status = hwDev()->openInputStream(
86 handle,
87 deviceType,
88 config,
89 flags,
90 address,
91 source,
92 outputDevice,
93 outputDeviceAddress,
94 &inStream);
95 ALOGV("AudioStreamIn::open(), HAL returned stream %p, sampleRate %d, format %#x,"
96 " channelMask %#x, status %d", inStream.get(), config->sample_rate, config->format,
97 config->channel_mask, status);
98
99 if (status == NO_ERROR) {
100 stream = inStream;
101 mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
102 status = stream->getFrameSize(&mHalFrameSize);
103 LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status);
104 LOG_ALWAYS_FATAL_IF(mHalFrameSize == 0, "Error frame size was %zu but must be greater than"
105 " zero", mHalFrameSize);
106 }
107
108 return status;
109 }
110
getAudioProperties() const111 audio_config_base_t AudioStreamIn::getAudioProperties() const
112 {
113 audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
114 if (stream->getAudioProperties(&result) != OK) {
115 result.sample_rate = 0;
116 result.channel_mask = AUDIO_CHANNEL_INVALID;
117 result.format = AUDIO_FORMAT_INVALID;
118 }
119 return result;
120 }
121
standby()122 status_t AudioStreamIn::standby()
123 {
124 mFramesReadAtStandby = mFramesRead;
125 return stream->standby();
126 }
127
read(void * buffer,size_t bytes,size_t * read)128 status_t AudioStreamIn::read(void* buffer, size_t bytes, size_t* read)
129 {
130 const status_t result = stream->read(buffer, bytes, read);
131 if (result == OK && *read > 0 && mHalFrameSize > 0) {
132 mFramesRead += *read / mHalFrameSize;
133 }
134 return result;
135 }
136
137 } // namespace android
138