Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AudioStreamTrack" |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <media/AudioTrack.h> |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 23 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 24 | #include <aaudio/AAudio.h> |
Phil Burk | ed81601 | 2018-02-06 12:40:50 -0800 | [diff] [blame] | 25 | #include <system/audio.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 26 | #include "utility/AudioClock.h" |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 27 | #include "legacy/AudioStreamLegacy.h" |
| 28 | #include "legacy/AudioStreamTrack.h" |
| 29 | #include "utility/FixedBlockReader.h" |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 30 | |
| 31 | using namespace android; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | using namespace aaudio; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 33 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 34 | // Arbitrary and somewhat generous number of bursts. |
| 35 | #define DEFAULT_BURSTS_PER_BUFFER_CAPACITY 8 |
| 36 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 37 | /* |
| 38 | * Create a stream that uses the AudioTrack. |
| 39 | */ |
| 40 | AudioStreamTrack::AudioStreamTrack() |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 41 | : AudioStreamLegacy() |
| 42 | , mFixedBlockReader(*this) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | AudioStreamTrack::~AudioStreamTrack() |
| 47 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 48 | const aaudio_stream_state_t state = getState(); |
| 49 | bool bad = !(state == AAUDIO_STREAM_STATE_UNINITIALIZED || state == AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 50 | ALOGE_IF(bad, "stream not closed, in state %d", state); |
| 51 | } |
| 52 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 53 | aaudio_result_t AudioStreamTrack::open(const AudioStreamBuilder& builder) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 54 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 55 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 56 | |
| 57 | result = AudioStream::open(builder); |
| 58 | if (result != OK) { |
| 59 | return result; |
| 60 | } |
| 61 | |
Phil Burk | 8ffcf61 | 2018-05-23 14:38:07 -0700 | [diff] [blame] | 62 | const aaudio_session_id_t requestedSessionId = builder.getSessionId(); |
| 63 | const audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId); |
| 64 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 65 | // Try to create an AudioTrack |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 66 | // Use stereo if unspecified. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 67 | int32_t samplesPerFrame = (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 68 | ? 2 : getSamplesPerFrame(); |
| 69 | audio_channel_mask_t channelMask = audio_channel_out_mask_from_count(samplesPerFrame); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 70 | |
Phil Burk | 8ffcf61 | 2018-05-23 14:38:07 -0700 | [diff] [blame] | 71 | audio_output_flags_t flags; |
Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 72 | aaudio_performance_mode_t perfMode = getPerformanceMode(); |
| 73 | switch(perfMode) { |
Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 74 | case AAUDIO_PERFORMANCE_MODE_LOW_LATENCY: |
| 75 | // Bypass the normal mixer and go straight to the FAST mixer. |
Phil Burk | 8ffcf61 | 2018-05-23 14:38:07 -0700 | [diff] [blame] | 76 | // If the app asks for a sessionId then it means they want to use effects. |
| 77 | // So don't use RAW flag. |
| 78 | flags = (audio_output_flags_t) ((requestedSessionId == AAUDIO_SESSION_ID_NONE) |
| 79 | ? (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW) |
| 80 | : (AUDIO_OUTPUT_FLAG_FAST)); |
Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 81 | break; |
| 82 | |
| 83 | case AAUDIO_PERFORMANCE_MODE_POWER_SAVING: |
| 84 | // This uses a mixer that wakes up less often than the FAST mixer. |
| 85 | flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
| 86 | break; |
| 87 | |
| 88 | case AAUDIO_PERFORMANCE_MODE_NONE: |
| 89 | default: |
| 90 | // No flags. Use a normal mixer in front of the FAST mixer. |
Phil Burk | 8ffcf61 | 2018-05-23 14:38:07 -0700 | [diff] [blame] | 91 | flags = AUDIO_OUTPUT_FLAG_NONE; |
Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 92 | break; |
| 93 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 94 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 95 | size_t frameCount = (size_t)builder.getBufferCapacity(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 96 | |
| 97 | int32_t notificationFrames = 0; |
| 98 | |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 99 | const audio_format_t format = (getFormat() == AUDIO_FORMAT_DEFAULT) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 100 | ? AUDIO_FORMAT_PCM_FLOAT |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 101 | : getFormat(); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 102 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 103 | // Setup the callback if there is one. |
| 104 | AudioTrack::callback_t callback = nullptr; |
| 105 | void *callbackData = nullptr; |
| 106 | // Note that TRANSFER_SYNC does not allow FAST track |
| 107 | AudioTrack::transfer_type streamTransferType = AudioTrack::transfer_type::TRANSFER_SYNC; |
| 108 | if (builder.getDataCallbackProc() != nullptr) { |
| 109 | streamTransferType = AudioTrack::transfer_type::TRANSFER_CALLBACK; |
| 110 | callback = getLegacyCallback(); |
| 111 | callbackData = this; |
| 112 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 113 | // If the total buffer size is unspecified then base the size on the burst size. |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 114 | if (frameCount == 0 |
| 115 | && ((flags & AUDIO_OUTPUT_FLAG_FAST) != 0)) { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 116 | // Take advantage of a special trick that allows us to create a buffer |
| 117 | // that is some multiple of the burst size. |
| 118 | notificationFrames = 0 - DEFAULT_BURSTS_PER_BUFFER_CAPACITY; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 119 | } else { |
| 120 | notificationFrames = builder.getFramesPerDataCallback(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | mCallbackBufferSize = builder.getFramesPerDataCallback(); |
| 124 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 125 | ALOGD("open(), request notificationFrames = %d, frameCount = %u", |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 126 | notificationFrames, (uint)frameCount); |
Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 127 | |
| 128 | // Don't call mAudioTrack->setDeviceId() because it will be overwritten by set()! |
| 129 | audio_port_handle_t selectedDeviceId = (getDeviceId() == AAUDIO_UNSPECIFIED) |
| 130 | ? AUDIO_PORT_HANDLE_NONE |
| 131 | : getDeviceId(); |
| 132 | |
Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 133 | const audio_content_type_t contentType = |
| 134 | AAudioConvert_contentTypeToInternal(builder.getContentType()); |
| 135 | const audio_usage_t usage = |
| 136 | AAudioConvert_usageToInternal(builder.getUsage()); |
Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame^] | 137 | const audio_flags_mask_t attributesFlags = |
| 138 | AAudioConvert_allowCapturePolicyToAudioFlagsMask(builder.getAllowedCapturePolicy()); |
Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 139 | |
| 140 | const audio_attributes_t attributes = { |
| 141 | .content_type = contentType, |
| 142 | .usage = usage, |
| 143 | .source = AUDIO_SOURCE_DEFAULT, // only used for recording |
Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame^] | 144 | .flags = attributesFlags, |
Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 145 | .tags = "" |
| 146 | }; |
| 147 | |
Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 148 | mAudioTrack = new AudioTrack(); |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 149 | mAudioTrack->set( |
Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 150 | AUDIO_STREAM_DEFAULT, // ignored because we pass attributes below |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 151 | getSampleRate(), |
| 152 | format, |
| 153 | channelMask, |
| 154 | frameCount, |
| 155 | flags, |
| 156 | callback, |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 157 | callbackData, |
| 158 | notificationFrames, |
Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 159 | 0, // DEFAULT sharedBuffer*/, |
| 160 | false, // DEFAULT threadCanCallJava |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 161 | sessionId, |
Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 162 | streamTransferType, |
| 163 | NULL, // DEFAULT audio_offload_info_t |
| 164 | AUDIO_UID_INVALID, // DEFAULT uid |
| 165 | -1, // DEFAULT pid |
Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 166 | &attributes, |
Phil Burk | ee99539 | 2017-12-11 11:44:36 -0800 | [diff] [blame] | 167 | // WARNING - If doNotReconnect set true then audio stops after plugging and unplugging |
| 168 | // headphones a few times. |
| 169 | false, // DEFAULT doNotReconnect, |
| 170 | 1.0f, // DEFAULT maxRequiredSpeed |
| 171 | selectedDeviceId |
| 172 | ); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 173 | |
| 174 | // Did we get a valid track? |
| 175 | status_t status = mAudioTrack->initCheck(); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 176 | if (status != NO_ERROR) { |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 177 | close(); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 178 | ALOGE("open(), initCheck() returned %d", status); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 179 | return AAudioConvert_androidToAAudioResult(status); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 182 | doSetVolume(); |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 183 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 184 | // Get the actual values from the AudioTrack. |
| 185 | setSamplesPerFrame(mAudioTrack->channelCount()); |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 186 | setFormat(mAudioTrack->format()); |
| 187 | setDeviceFormat(mAudioTrack->format()); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 188 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 189 | int32_t actualSampleRate = mAudioTrack->getSampleRate(); |
| 190 | ALOGW_IF(actualSampleRate != getSampleRate(), |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 191 | "open() sampleRate changed from %d to %d", |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 192 | getSampleRate(), actualSampleRate); |
| 193 | setSampleRate(actualSampleRate); |
| 194 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 195 | // We may need to pass the data through a block size adapter to guarantee constant size. |
| 196 | if (mCallbackBufferSize != AAUDIO_UNSPECIFIED) { |
| 197 | int callbackSizeBytes = getBytesPerFrame() * mCallbackBufferSize; |
| 198 | mFixedBlockReader.open(callbackSizeBytes); |
| 199 | mBlockAdapter = &mFixedBlockReader; |
| 200 | } else { |
| 201 | mBlockAdapter = nullptr; |
| 202 | } |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 203 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 204 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 205 | setDeviceId(mAudioTrack->getRoutedDeviceId()); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 206 | |
| 207 | aaudio_session_id_t actualSessionId = |
| 208 | (requestedSessionId == AAUDIO_SESSION_ID_NONE) |
| 209 | ? AAUDIO_SESSION_ID_NONE |
| 210 | : (aaudio_session_id_t) mAudioTrack->getSessionId(); |
| 211 | setSessionId(actualSessionId); |
| 212 | |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 213 | mAudioTrack->addAudioDeviceCallback(mDeviceCallback); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 214 | |
Phil Burk | 09c27ca | 2017-08-24 22:12:56 -0700 | [diff] [blame] | 215 | // Update performance mode based on the actual stream flags. |
Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 216 | // For example, if the sample rate is not allowed then you won't get a FAST track. |
| 217 | audio_output_flags_t actualFlags = mAudioTrack->getFlags(); |
| 218 | aaudio_performance_mode_t actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE; |
Phil Burk | 09c27ca | 2017-08-24 22:12:56 -0700 | [diff] [blame] | 219 | // We may not get the RAW flag. But as long as we get the FAST flag we can call it LOW_LATENCY. |
| 220 | if ((actualFlags & AUDIO_OUTPUT_FLAG_FAST) != 0) { |
Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 221 | actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY; |
Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 222 | } else if ((actualFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 223 | actualPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING; |
| 224 | } |
| 225 | setPerformanceMode(actualPerformanceMode); |
Phil Burk | efa5600 | 2017-08-02 15:07:21 -0700 | [diff] [blame] | 226 | |
| 227 | setSharingMode(AAUDIO_SHARING_MODE_SHARED); // EXCLUSIVE mode not supported in legacy |
| 228 | |
Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 229 | // Log warning if we did not get what we asked for. |
| 230 | ALOGW_IF(actualFlags != flags, |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 231 | "open() flags changed from 0x%08X to 0x%08X", |
Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 232 | flags, actualFlags); |
| 233 | ALOGW_IF(actualPerformanceMode != perfMode, |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 234 | "open() perfMode changed from %d to %d", |
Phil Burk | 30a7077 | 2017-05-16 11:20:36 -0700 | [diff] [blame] | 235 | perfMode, actualPerformanceMode); |
| 236 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 237 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 240 | aaudio_result_t AudioStreamTrack::close() |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 241 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 242 | if (getState() != AAUDIO_STREAM_STATE_CLOSED) { |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 243 | mAudioTrack->removeAudioDeviceCallback(mDeviceCallback); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 244 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 245 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 246 | mFixedBlockReader.close(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 247 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 250 | void AudioStreamTrack::processCallback(int event, void *info) { |
| 251 | |
| 252 | switch (event) { |
| 253 | case AudioTrack::EVENT_MORE_DATA: |
| 254 | processCallbackCommon(AAUDIO_CALLBACK_OPERATION_PROCESS_DATA, info); |
| 255 | break; |
| 256 | |
| 257 | // Stream got rerouted so we disconnect. |
| 258 | case AudioTrack::EVENT_NEW_IAUDIOTRACK: |
| 259 | processCallbackCommon(AAUDIO_CALLBACK_OPERATION_DISCONNECTED, info); |
| 260 | break; |
| 261 | |
| 262 | default: |
| 263 | break; |
| 264 | } |
| 265 | return; |
| 266 | } |
| 267 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 268 | aaudio_result_t AudioStreamTrack::requestStart() { |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 269 | if (mAudioTrack.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 270 | ALOGE("requestStart() no AudioTrack"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 271 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 272 | } |
| 273 | // Get current position so we can detect when the track is playing. |
| 274 | status_t err = mAudioTrack->getPosition(&mPositionWhenStarting); |
| 275 | if (err != OK) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 276 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 277 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 278 | |
Phil Burk | 9e9a95b | 2018-01-18 12:14:15 -0800 | [diff] [blame] | 279 | // Enable callback before starting AudioTrack to avoid shutting |
| 280 | // down because of a race condition. |
| 281 | mCallbackEnabled.store(true); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 282 | err = mAudioTrack->start(); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 283 | if (err != OK) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 284 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 285 | } else { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 286 | setState(AAUDIO_STREAM_STATE_STARTING); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 287 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 288 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 291 | aaudio_result_t AudioStreamTrack::requestPause() { |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 292 | if (mAudioTrack.get() == nullptr) { |
Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 293 | ALOGE("%s() no AudioTrack", __func__); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 294 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 295 | } |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 296 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 297 | setState(AAUDIO_STREAM_STATE_PAUSING); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 298 | mAudioTrack->pause(); |
Phil Burk | 9e9a95b | 2018-01-18 12:14:15 -0800 | [diff] [blame] | 299 | mCallbackEnabled.store(false); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 300 | status_t err = mAudioTrack->getPosition(&mPositionWhenPausing); |
| 301 | if (err != OK) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 302 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 303 | } |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 304 | return checkForDisconnectRequest(false); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 307 | aaudio_result_t AudioStreamTrack::requestFlush() { |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 308 | if (mAudioTrack.get() == nullptr) { |
Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 309 | ALOGE("%s() no AudioTrack", __func__); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 310 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 311 | } |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 312 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 313 | setState(AAUDIO_STREAM_STATE_FLUSHING); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 314 | incrementFramesRead(getFramesWritten() - getFramesRead()); |
| 315 | mAudioTrack->flush(); |
Phil Burk | 2b6f128 | 2018-05-10 15:26:30 -0700 | [diff] [blame] | 316 | mFramesRead.reset32(); // service reads frames, service position reset on flush |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 317 | mTimestampPosition.reset32(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 318 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 321 | aaudio_result_t AudioStreamTrack::requestStop() { |
Phil Burk | d8bdcab | 2017-01-03 17:20:30 -0800 | [diff] [blame] | 322 | if (mAudioTrack.get() == nullptr) { |
Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 323 | ALOGE("%s() no AudioTrack", __func__); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 324 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 325 | } |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 326 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 327 | setState(AAUDIO_STREAM_STATE_STOPPING); |
Phil Burk | 18c8476 | 2018-12-18 12:15:35 -0800 | [diff] [blame] | 328 | mFramesRead.catchUpTo(getFramesWritten()); |
| 329 | mTimestampPosition.catchUpTo(getFramesWritten()); |
Phil Burk | 2b6f128 | 2018-05-10 15:26:30 -0700 | [diff] [blame] | 330 | mFramesRead.reset32(); // service reads frames, service position reset on stop |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 331 | mTimestampPosition.reset32(); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 332 | mAudioTrack->stop(); |
Phil Burk | 9e9a95b | 2018-01-18 12:14:15 -0800 | [diff] [blame] | 333 | mCallbackEnabled.store(false); |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 334 | return checkForDisconnectRequest(false);; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 337 | aaudio_result_t AudioStreamTrack::updateStateMachine() |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 338 | { |
| 339 | status_t err; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 340 | aaudio_wrapping_frames_t position; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 341 | switch (getState()) { |
| 342 | // TODO add better state visibility to AudioTrack |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 343 | case AAUDIO_STREAM_STATE_STARTING: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 344 | if (mAudioTrack->hasStarted()) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 345 | setState(AAUDIO_STREAM_STATE_STARTED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 346 | } |
| 347 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 348 | case AAUDIO_STREAM_STATE_PAUSING: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 349 | if (mAudioTrack->stopped()) { |
| 350 | err = mAudioTrack->getPosition(&position); |
| 351 | if (err != OK) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 352 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 353 | } else if (position == mPositionWhenPausing) { |
| 354 | // Has stream really stopped advancing? |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 355 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 356 | } |
| 357 | mPositionWhenPausing = position; |
| 358 | } |
| 359 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 360 | case AAUDIO_STREAM_STATE_FLUSHING: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 361 | { |
| 362 | err = mAudioTrack->getPosition(&position); |
| 363 | if (err != OK) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 364 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 365 | } else if (position == 0) { |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 366 | // TODO Advance frames read to match written. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 367 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 371 | case AAUDIO_STREAM_STATE_STOPPING: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 372 | if (mAudioTrack->stopped()) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 373 | setState(AAUDIO_STREAM_STATE_STOPPED); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 374 | } |
| 375 | break; |
| 376 | default: |
| 377 | break; |
| 378 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 379 | return AAUDIO_OK; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 382 | aaudio_result_t AudioStreamTrack::write(const void *buffer, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 383 | int32_t numFrames, |
| 384 | int64_t timeoutNanoseconds) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 385 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 386 | int32_t bytesPerFrame = getBytesPerFrame(); |
| 387 | int32_t numBytes; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 388 | aaudio_result_t result = AAudioConvert_framesToBytes(numFrames, bytesPerFrame, &numBytes); |
| 389 | if (result != AAUDIO_OK) { |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 390 | return result; |
| 391 | } |
| 392 | |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 393 | if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) { |
| 394 | return AAUDIO_ERROR_DISCONNECTED; |
| 395 | } |
| 396 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 397 | // TODO add timeout to AudioTrack |
| 398 | bool blocking = timeoutNanoseconds > 0; |
| 399 | ssize_t bytesWritten = mAudioTrack->write(buffer, numBytes, blocking); |
| 400 | if (bytesWritten == WOULD_BLOCK) { |
| 401 | return 0; |
| 402 | } else if (bytesWritten < 0) { |
| 403 | ALOGE("invalid write, returned %d", (int)bytesWritten); |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 404 | // in this context, a DEAD_OBJECT is more likely to be a disconnect notification due to |
| 405 | // AudioTrack invalidation |
| 406 | if (bytesWritten == DEAD_OBJECT) { |
| 407 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
| 408 | return AAUDIO_ERROR_DISCONNECTED; |
| 409 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 410 | return AAudioConvert_androidToAAudioResult(bytesWritten); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 411 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 412 | int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 413 | incrementFramesWritten(framesWritten); |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 414 | |
| 415 | result = updateStateMachine(); |
| 416 | if (result != AAUDIO_OK) { |
| 417 | return result; |
| 418 | } |
| 419 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 420 | return framesWritten; |
| 421 | } |
| 422 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 423 | aaudio_result_t AudioStreamTrack::setBufferSize(int32_t requestedFrames) |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 424 | { |
| 425 | ssize_t result = mAudioTrack->setBufferSizeInFrames(requestedFrames); |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 426 | if (result < 0) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 427 | return AAudioConvert_androidToAAudioResult(result); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 428 | } else { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 429 | return result; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 433 | int32_t AudioStreamTrack::getBufferSize() const |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 434 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 435 | return static_cast<int32_t>(mAudioTrack->getBufferSizeInFrames()); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 438 | int32_t AudioStreamTrack::getBufferCapacity() const |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 439 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 440 | return static_cast<int32_t>(mAudioTrack->frameCount()); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | int32_t AudioStreamTrack::getXRunCount() const |
| 444 | { |
| 445 | return static_cast<int32_t>(mAudioTrack->getUnderrunCount()); |
| 446 | } |
| 447 | |
| 448 | int32_t AudioStreamTrack::getFramesPerBurst() const |
| 449 | { |
Phil Burk | b588402 | 2017-03-27 15:26:14 -0700 | [diff] [blame] | 450 | return static_cast<int32_t>(mAudioTrack->getNotificationPeriodInFrames()); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 453 | int64_t AudioStreamTrack::getFramesRead() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 454 | aaudio_wrapping_frames_t position; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 455 | status_t result; |
| 456 | switch (getState()) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 457 | case AAUDIO_STREAM_STATE_STARTING: |
| 458 | case AAUDIO_STREAM_STATE_STARTED: |
| 459 | case AAUDIO_STREAM_STATE_STOPPING: |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 460 | case AAUDIO_STREAM_STATE_PAUSING: |
| 461 | case AAUDIO_STREAM_STATE_PAUSED: |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 462 | result = mAudioTrack->getPosition(&position); |
| 463 | if (result == OK) { |
| 464 | mFramesRead.update32(position); |
| 465 | } |
| 466 | break; |
| 467 | default: |
| 468 | break; |
| 469 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 470 | return AudioStreamLegacy::getFramesRead(); |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 471 | } |
Phil Burk | 35e80f3 | 2017-03-28 10:25:21 -0700 | [diff] [blame] | 472 | |
| 473 | aaudio_result_t AudioStreamTrack::getTimestamp(clockid_t clockId, |
| 474 | int64_t *framePosition, |
| 475 | int64_t *timeNanoseconds) { |
| 476 | ExtendedTimestamp extendedTimestamp; |
| 477 | status_t status = mAudioTrack->getTimestamp(&extendedTimestamp); |
Phil Burk | c75d97f | 2017-09-08 15:48:36 -0700 | [diff] [blame] | 478 | if (status == WOULD_BLOCK) { |
| 479 | return AAUDIO_ERROR_INVALID_STATE; |
| 480 | } if (status != NO_ERROR) { |
Phil Burk | 35e80f3 | 2017-03-28 10:25:21 -0700 | [diff] [blame] | 481 | return AAudioConvert_androidToAAudioResult(status); |
| 482 | } |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 483 | int64_t position = 0; |
| 484 | int64_t nanoseconds = 0; |
| 485 | aaudio_result_t result = getBestTimestamp(clockId, &position, |
| 486 | &nanoseconds, &extendedTimestamp); |
| 487 | if (result == AAUDIO_OK) { |
| 488 | if (position < getFramesWritten()) { |
| 489 | *framePosition = position; |
| 490 | *timeNanoseconds = nanoseconds; |
| 491 | return result; |
| 492 | } else { |
| 493 | return AAUDIO_ERROR_INVALID_STATE; // TODO review, documented but not consistent |
| 494 | } |
| 495 | } |
| 496 | return result; |
Phil Burk | 35e80f3 | 2017-03-28 10:25:21 -0700 | [diff] [blame] | 497 | } |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 498 | |
| 499 | status_t AudioStreamTrack::doSetVolume() { |
| 500 | status_t status = NO_INIT; |
| 501 | if (mAudioTrack.get() != nullptr) { |
| 502 | float volume = getDuckAndMuteVolume(); |
| 503 | mAudioTrack->setVolume(volume, volume); |
| 504 | status = NO_ERROR; |
| 505 | } |
| 506 | return status; |
| 507 | } |
| 508 | |
| 509 | #if AAUDIO_USE_VOLUME_SHAPER |
Phil Burk | 8a8a9e5 | 2017-09-12 16:25:15 -0700 | [diff] [blame] | 510 | |
| 511 | using namespace android::media::VolumeShaper; |
| 512 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 513 | binder::Status AudioStreamTrack::applyVolumeShaper( |
| 514 | const VolumeShaper::Configuration& configuration, |
| 515 | const VolumeShaper::Operation& operation) { |
| 516 | |
| 517 | sp<VolumeShaper::Configuration> spConfiguration = new VolumeShaper::Configuration(configuration); |
| 518 | sp<VolumeShaper::Operation> spOperation = new VolumeShaper::Operation(operation); |
| 519 | |
| 520 | if (mAudioTrack.get() != nullptr) { |
| 521 | ALOGD("applyVolumeShaper() from IPlayer"); |
Phil Burk | 8a8a9e5 | 2017-09-12 16:25:15 -0700 | [diff] [blame] | 522 | binder::Status status = mAudioTrack->applyVolumeShaper(spConfiguration, spOperation); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 523 | if (status < 0) { // a non-negative value is the volume shaper id. |
| 524 | ALOGE("applyVolumeShaper() failed with status %d", status); |
| 525 | } |
| 526 | return binder::Status::fromStatusT(status); |
| 527 | } else { |
| 528 | ALOGD("applyVolumeShaper()" |
| 529 | " no AudioTrack for volume control from IPlayer"); |
| 530 | return binder::Status::ok(); |
| 531 | } |
| 532 | } |
| 533 | #endif |