blob: a6b9f5d7b50154eb773be5a8cd7ad232dcf688de [file] [log] [blame]
Phil Burke4d7bb42017-03-28 11:32:39 -07001/*
2 * Copyright 2017 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#define LOG_TAG "AudioStreamLegacy"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <stdint.h>
Phil Burk3d786cb2018-04-09 11:58:09 -070022
23#include <aaudio/AAudio.h>
24#include <audio_utils/primitives.h>
Phil Burke4d7bb42017-03-28 11:32:39 -070025#include <media/AudioTrack.h>
Phil Burk7328a802017-08-30 09:29:48 -070026#include <media/AudioTimestamp.h>
Phil Burk3d786cb2018-04-09 11:58:09 -070027#include <utils/String16.h>
Phil Burke4d7bb42017-03-28 11:32:39 -070028
29#include "core/AudioStream.h"
30#include "legacy/AudioStreamLegacy.h"
31
32using namespace android;
33using namespace aaudio;
34
35AudioStreamLegacy::AudioStreamLegacy()
Phil Burk2d5ba532017-09-06 14:36:11 -070036 : AudioStream()
37 , mDeviceCallback(new StreamDeviceCallback(this)) {
Phil Burke4d7bb42017-03-28 11:32:39 -070038}
39
40AudioStreamLegacy::~AudioStreamLegacy() {
41}
42
43// Called from AudioTrack.cpp or AudioRecord.cpp
44static void AudioStreamLegacy_callback(int event, void* userData, void *info) {
45 AudioStreamLegacy *streamLegacy = (AudioStreamLegacy *) userData;
46 streamLegacy->processCallback(event, info);
47}
48
49aaudio_legacy_callback_t AudioStreamLegacy::getLegacyCallback() {
50 return AudioStreamLegacy_callback;
51}
52
Phil Burk3d786cb2018-04-09 11:58:09 -070053aaudio_data_callback_result_t AudioStreamLegacy::callDataCallbackFrames(uint8_t *buffer,
54 int32_t numFrames) {
55 void *finalAudioData = buffer;
Phil Burk7328a802017-08-30 09:29:48 -070056 if (getDirection() == AAUDIO_DIRECTION_INPUT) {
57 // Increment before because we already got the data from the device.
58 incrementFramesRead(numFrames);
Phil Burk3d786cb2018-04-09 11:58:09 -070059 finalAudioData = (void *) maybeConvertDeviceData(buffer, numFrames);
Phil Burk7328a802017-08-30 09:29:48 -070060 }
61
Phil Burke4d7bb42017-03-28 11:32:39 -070062 // Call using the AAudio callback interface.
Phil Burk3d786cb2018-04-09 11:58:09 -070063 aaudio_data_callback_result_t callbackResult = maybeCallDataCallback(finalAudioData, numFrames);
Phil Burk7328a802017-08-30 09:29:48 -070064
65 if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE
66 && getDirection() == AAUDIO_DIRECTION_OUTPUT) {
67 // Increment after because we are going to write the data to the device.
68 incrementFramesWritten(numFrames);
69 }
70 return callbackResult;
71}
72
73// Implement FixedBlockProcessor
74int32_t AudioStreamLegacy::onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) {
Phil Burk3d786cb2018-04-09 11:58:09 -070075 int32_t numFrames = numBytes / getBytesPerDeviceFrame();
Phil Burk134f1972017-12-08 13:06:11 -080076 return (int32_t) callDataCallbackFrames(buffer, numFrames);
Phil Burke4d7bb42017-03-28 11:32:39 -070077}
78
79void AudioStreamLegacy::processCallbackCommon(aaudio_callback_operation_t opcode, void *info) {
80 aaudio_data_callback_result_t callbackResult;
Phil Burk3d786cb2018-04-09 11:58:09 -070081 // This illegal size can be used to tell AudioFlinger to stop calling us.
Phil Burk134f1972017-12-08 13:06:11 -080082 // This takes advantage of AudioFlinger killing the stream.
Phil Burk3d786cb2018-04-09 11:58:09 -070083 // TODO add to API in AudioRecord and AudioTrack
Phil Burk134f1972017-12-08 13:06:11 -080084 const size_t SIZE_STOP_CALLBACKS = SIZE_MAX;
Eric Laurentfb00fc72017-05-25 18:17:12 -070085
Phil Burke4d7bb42017-03-28 11:32:39 -070086 switch (opcode) {
87 case AAUDIO_CALLBACK_OPERATION_PROCESS_DATA: {
Phil Burk134f1972017-12-08 13:06:11 -080088 (void) checkForDisconnectRequest(true);
Phil Burk2d5ba532017-09-06 14:36:11 -070089
90 // Note that this code assumes an AudioTrack::Buffer is the same as
91 // AudioRecord::Buffer
92 // TODO define our own AudioBuffer and pass it from the subclasses.
93 AudioTrack::Buffer *audioBuffer = static_cast<AudioTrack::Buffer *>(info);
Phil Burk9e9a95b2018-01-18 12:14:15 -080094 if (getState() == AAUDIO_STREAM_STATE_DISCONNECTED) {
95 ALOGW("processCallbackCommon() data, stream disconnected");
96 audioBuffer->size = SIZE_STOP_CALLBACKS;
97 } else if (!mCallbackEnabled.load()) {
98 ALOGW("processCallbackCommon() stopping because callback disabled");
Phil Burk134f1972017-12-08 13:06:11 -080099 audioBuffer->size = SIZE_STOP_CALLBACKS;
Phil Burk2d5ba532017-09-06 14:36:11 -0700100 } else {
101 if (audioBuffer->frameCount == 0) {
Phil Burk9e9a95b2018-01-18 12:14:15 -0800102 ALOGW("processCallbackCommon() data, frameCount is zero");
Phil Burk2d5ba532017-09-06 14:36:11 -0700103 return;
104 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700105
Eric Laurentfb00fc72017-05-25 18:17:12 -0700106 // If the caller specified an exact size then use a block size adapter.
107 if (mBlockAdapter != nullptr) {
Phil Burk3d786cb2018-04-09 11:58:09 -0700108 int32_t byteCount = audioBuffer->frameCount * getBytesPerDeviceFrame();
Eric Laurentfb00fc72017-05-25 18:17:12 -0700109 callbackResult = mBlockAdapter->processVariableBlock(
110 (uint8_t *) audioBuffer->raw, byteCount);
111 } else {
112 // Call using the AAudio callback interface.
Phil Burk7328a802017-08-30 09:29:48 -0700113 callbackResult = callDataCallbackFrames((uint8_t *)audioBuffer->raw,
114 audioBuffer->frameCount);
Eric Laurentfb00fc72017-05-25 18:17:12 -0700115 }
116 if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE) {
Phil Burk3d786cb2018-04-09 11:58:09 -0700117 audioBuffer->size = audioBuffer->frameCount * getBytesPerDeviceFrame();
Phil Burk134f1972017-12-08 13:06:11 -0800118 } else { // STOP or invalid result
Phil Burk9e9a95b2018-01-18 12:14:15 -0800119 ALOGW("%s() callback requested stop, fake an error", __func__);
Phil Burk134f1972017-12-08 13:06:11 -0800120 audioBuffer->size = SIZE_STOP_CALLBACKS;
121 // Disable the callback just in case AudioFlinger keeps trying to call us.
122 mCallbackEnabled.store(false);
Eric Laurentfb00fc72017-05-25 18:17:12 -0700123 }
Phil Burk0befec62017-07-28 15:12:13 -0700124
Phil Burk2d5ba532017-09-06 14:36:11 -0700125 if (updateStateMachine() != AAUDIO_OK) {
126 forceDisconnect();
127 mCallbackEnabled.store(false);
Phil Burk0befec62017-07-28 15:12:13 -0700128 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700129 }
130 }
Phil Burk2d5ba532017-09-06 14:36:11 -0700131 break;
Phil Burke4d7bb42017-03-28 11:32:39 -0700132
Phil Burk0befec62017-07-28 15:12:13 -0700133 // Stream got rerouted so we disconnect.
Phil Burk2d5ba532017-09-06 14:36:11 -0700134 case AAUDIO_CALLBACK_OPERATION_DISCONNECTED:
Eric Laurentfb00fc72017-05-25 18:17:12 -0700135 ALOGD("processCallbackCommon() stream disconnected");
Phil Burk2d5ba532017-09-06 14:36:11 -0700136 forceDisconnect();
Phil Burke4d7bb42017-03-28 11:32:39 -0700137 mCallbackEnabled.store(false);
Phil Burke4d7bb42017-03-28 11:32:39 -0700138 break;
139
140 default:
141 break;
142 }
143}
Phil Burk5204d312017-05-04 17:16:13 -0700144
Phil Burk134f1972017-12-08 13:06:11 -0800145aaudio_result_t AudioStreamLegacy::checkForDisconnectRequest(bool errorCallbackEnabled) {
Phil Burk2d5ba532017-09-06 14:36:11 -0700146 if (mRequestDisconnect.isRequested()) {
147 ALOGD("checkForDisconnectRequest() mRequestDisconnect acknowledged");
Phil Burk134f1972017-12-08 13:06:11 -0800148 forceDisconnect(errorCallbackEnabled);
Phil Burk2d5ba532017-09-06 14:36:11 -0700149 mRequestDisconnect.acknowledge();
150 mCallbackEnabled.store(false);
Phil Burk134f1972017-12-08 13:06:11 -0800151 return AAUDIO_ERROR_DISCONNECTED;
152 } else {
153 return AAUDIO_OK;
Phil Burk2d5ba532017-09-06 14:36:11 -0700154 }
155}
156
Phil Burk134f1972017-12-08 13:06:11 -0800157void AudioStreamLegacy::forceDisconnect(bool errorCallbackEnabled) {
Phil Burk2d5ba532017-09-06 14:36:11 -0700158 if (getState() != AAUDIO_STREAM_STATE_DISCONNECTED) {
159 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burk134f1972017-12-08 13:06:11 -0800160 if (errorCallbackEnabled) {
161 maybeCallErrorCallback(AAUDIO_ERROR_DISCONNECTED);
Phil Burk2d5ba532017-09-06 14:36:11 -0700162 }
163 }
164}
165
Phil Burk5204d312017-05-04 17:16:13 -0700166aaudio_result_t AudioStreamLegacy::getBestTimestamp(clockid_t clockId,
167 int64_t *framePosition,
168 int64_t *timeNanoseconds,
169 ExtendedTimestamp *extendedTimestamp) {
170 int timebase;
171 switch (clockId) {
172 case CLOCK_BOOTTIME:
173 timebase = ExtendedTimestamp::TIMEBASE_BOOTTIME;
174 break;
175 case CLOCK_MONOTONIC:
176 timebase = ExtendedTimestamp::TIMEBASE_MONOTONIC;
177 break;
178 default:
179 ALOGE("getTimestamp() - Unrecognized clock type %d", (int) clockId);
Phil Burk17fff382017-05-16 14:06:45 -0700180 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
Phil Burk5204d312017-05-04 17:16:13 -0700181 break;
182 }
Phil Burk7328a802017-08-30 09:29:48 -0700183 ExtendedTimestamp::Location location = ExtendedTimestamp::Location::LOCATION_INVALID;
184 int64_t localPosition;
185 status_t status = extendedTimestamp->getBestTimestamp(&localPosition, timeNanoseconds,
186 timebase, &location);
Phil Burkc0959642018-04-05 18:11:01 -0700187 if (status == OK) {
188 // use MonotonicCounter to prevent retrograde motion.
189 mTimestampPosition.update32((int32_t) localPosition);
190 *framePosition = mTimestampPosition.get();
191 }
Phil Burk7328a802017-08-30 09:29:48 -0700192
193// ALOGD("getBestTimestamp() fposition: server = %6lld, kernel = %6lld, location = %d",
194// (long long) extendedTimestamp->mPosition[ExtendedTimestamp::Location::LOCATION_SERVER],
195// (long long) extendedTimestamp->mPosition[ExtendedTimestamp::Location::LOCATION_KERNEL],
196// (int)location);
Phil Burkc0959642018-04-05 18:11:01 -0700197 return AAudioConvert_androidToAAudioResult(status);
Phil Burk5204d312017-05-04 17:16:13 -0700198}
Eric Laurentfb00fc72017-05-25 18:17:12 -0700199
200void AudioStreamLegacy::onAudioDeviceUpdate(audio_port_handle_t deviceId)
201{
202 ALOGD("onAudioDeviceUpdate() deviceId %d", (int)deviceId);
203 if (getDeviceId() != AAUDIO_UNSPECIFIED && getDeviceId() != deviceId &&
204 getState() != AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk2d5ba532017-09-06 14:36:11 -0700205 // Note that isDataCallbackActive() is affected by state so call it before DISCONNECTING.
206 // If we have a data callback and the stream is active, then ask the data callback
207 // to DISCONNECT and call the error callback.
208 if (isDataCallbackActive()) {
209 ALOGD("onAudioDeviceUpdate() request DISCONNECT in data callback due to device change");
210 // If the stream is stopped before the data callback has a chance to handle the
211 // request then the requestStop() and requestPause() methods will handle it after
212 // the callback has stopped.
213 mRequestDisconnect.request();
214 } else {
215 ALOGD("onAudioDeviceUpdate() DISCONNECT the stream now");
216 forceDisconnect();
Eric Laurentfb00fc72017-05-25 18:17:12 -0700217 }
218 }
219 setDeviceId(deviceId);
220}