Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [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 | |
| 17 | #ifndef LEGACY_AUDIO_STREAM_LEGACY_H |
| 18 | #define LEGACY_AUDIO_STREAM_LEGACY_H |
| 19 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 20 | #include <media/AudioTimestamp.h> |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 21 | #include <media/AudioSystem.h> |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 22 | |
| 23 | #include <aaudio/AAudio.h> |
| 24 | |
| 25 | #include "AudioStream.h" |
| 26 | #include "AAudioLegacy.h" |
Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 27 | #include "utility/AAudioUtilities.h" |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 28 | #include "utility/FixedBlockAdapter.h" |
| 29 | |
| 30 | namespace aaudio { |
| 31 | |
| 32 | |
| 33 | typedef void (*aaudio_legacy_callback_t)(int event, void* user, void *info); |
| 34 | |
| 35 | enum { |
| 36 | /** |
| 37 | * Request that the callback function should fill the data buffer of an output stream, |
| 38 | * or process the data of an input stream. |
| 39 | * The address parameter passed to the callback function will point to a data buffer. |
| 40 | * For an input stream, the data is read-only. |
| 41 | * The value1 parameter will be the number of frames. |
| 42 | * The value2 parameter is reserved and will be set to zero. |
| 43 | * The callback should return AAUDIO_CALLBACK_RESULT_CONTINUE or AAUDIO_CALLBACK_RESULT_STOP. |
| 44 | */ |
| 45 | AAUDIO_CALLBACK_OPERATION_PROCESS_DATA, |
| 46 | |
| 47 | /** |
| 48 | * Inform the callback function that the stream was disconnected. |
| 49 | * The address parameter passed to the callback function will be NULL. |
| 50 | * The value1 will be an error code or AAUDIO_OK. |
| 51 | * The value2 parameter is reserved and will be set to zero. |
| 52 | * The callback return value will be ignored. |
| 53 | */ |
| 54 | AAUDIO_CALLBACK_OPERATION_DISCONNECTED, |
| 55 | }; |
| 56 | typedef int32_t aaudio_callback_operation_t; |
| 57 | |
| 58 | |
| 59 | class AudioStreamLegacy : public AudioStream, public FixedBlockProcessor { |
| 60 | public: |
| 61 | AudioStreamLegacy(); |
| 62 | |
| 63 | virtual ~AudioStreamLegacy(); |
| 64 | |
| 65 | aaudio_legacy_callback_t getLegacyCallback(); |
| 66 | |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 67 | int32_t callDataCallbackFrames(uint8_t *buffer, int32_t numFrames); |
| 68 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 69 | // This is public so it can be called from the C callback function. |
| 70 | // This is called from the AudioTrack/AudioRecord client. |
| 71 | virtual void processCallback(int event, void *info) = 0; |
| 72 | |
| 73 | void processCallbackCommon(aaudio_callback_operation_t opcode, void *info); |
| 74 | |
| 75 | // Implement FixedBlockProcessor |
| 76 | int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override; |
| 77 | |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 78 | virtual int64_t incrementClientFrameCounter(int32_t frames) = 0; |
| 79 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 80 | virtual int64_t getFramesWritten() override { |
| 81 | return mFramesWritten.get(); |
| 82 | } |
| 83 | |
| 84 | virtual int64_t getFramesRead() override { |
| 85 | return mFramesRead.get(); |
| 86 | } |
| 87 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 88 | protected: |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 89 | |
| 90 | aaudio_result_t getBestTimestamp(clockid_t clockId, |
| 91 | int64_t *framePosition, |
| 92 | int64_t *timeNanoseconds, |
| 93 | android::ExtendedTimestamp *extendedTimestamp); |
| 94 | |
Phil Burk | 58f5ce1 | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 95 | void onAudioDeviceUpdate(audio_io_handle_t audioIo, |
| 96 | audio_port_handle_t deviceId) override; |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 97 | |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 98 | /* |
| 99 | * Check to see whether a callback thread has requested a disconnected. |
| 100 | * @param errorCallbackEnabled set true to call errorCallback on disconnect |
| 101 | * @return AAUDIO_OK or AAUDIO_ERROR_DISCONNECTED |
| 102 | */ |
| 103 | aaudio_result_t checkForDisconnectRequest(bool errorCallbackEnabled); |
Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 104 | |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 105 | void forceDisconnect(bool errorCallbackEnabled = true); |
Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 106 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 107 | int64_t incrementFramesWritten(int32_t frames) { |
| 108 | return mFramesWritten.increment(frames); |
| 109 | } |
| 110 | |
| 111 | int64_t incrementFramesRead(int32_t frames) { |
| 112 | return mFramesRead.increment(frames); |
| 113 | } |
| 114 | |
Phil Burk | 8d97b8e | 2020-09-25 23:18:14 +0000 | [diff] [blame^] | 115 | /** |
| 116 | * Get the framesPerBurst from the underlying API. |
| 117 | * @return framesPerBurst |
| 118 | */ |
| 119 | virtual int32_t getFramesPerBurstFromDevice() const = 0; |
| 120 | |
| 121 | /** |
| 122 | * Get the bufferCapacity from the underlying API. |
| 123 | * @return bufferCapacity in frames |
| 124 | */ |
| 125 | virtual int32_t getBufferCapacityFromDevice() const = 0; |
| 126 | |
Phil Burk | d3813f3 | 2020-04-23 16:26:15 -0700 | [diff] [blame] | 127 | // This is used for exact matching by MediaMetrics. So do not change it. |
Andy Hung | a6b2703 | 2020-04-27 10:34:24 -0700 | [diff] [blame] | 128 | // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_AAUDIO |
Phil Burk | d3813f3 | 2020-04-23 16:26:15 -0700 | [diff] [blame] | 129 | static constexpr char kCallerName[] = "aaudio"; |
| 130 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 131 | MonotonicCounter mFramesWritten; |
| 132 | MonotonicCounter mFramesRead; |
Phil Burk | 7328a80 | 2017-08-30 09:29:48 -0700 | [diff] [blame] | 133 | MonotonicCounter mTimestampPosition; |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 134 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 135 | FixedBlockAdapter *mBlockAdapter = nullptr; |
Phil Burk | 4b86749 | 2020-02-12 10:58:05 -0800 | [diff] [blame] | 136 | int32_t mBlockAdapterBytesPerFrame = 0; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 137 | aaudio_wrapping_frames_t mPositionWhenStarting = 0; |
| 138 | int32_t mCallbackBufferSize = 0; |
Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 139 | |
| 140 | AtomicRequestor mRequestDisconnect; |
Phil Burk | d3813f3 | 2020-04-23 16:26:15 -0700 | [diff] [blame] | 141 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | } /* namespace aaudio */ |
| 145 | |
| 146 | #endif //LEGACY_AUDIO_STREAM_LEGACY_H |