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 | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 17 | #ifndef LEGACY_AUDIO_STREAM_TRACK_H |
| 18 | #define LEGACY_AUDIO_STREAM_TRACK_H |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 19 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 20 | #include <math.h> |
Eric Laurent | 1d32e9f | 2017-06-02 14:01:32 -0700 | [diff] [blame] | 21 | #include <media/TrackPlayerBase.h> |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 22 | #include <media/AudioTrack.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 23 | #include <aaudio/AAudio.h> |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 24 | |
| 25 | #include "AudioStreamBuilder.h" |
| 26 | #include "AudioStream.h" |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 27 | #include "legacy/AAudioLegacy.h" |
| 28 | #include "legacy/AudioStreamLegacy.h" |
| 29 | #include "utility/FixedBlockReader.h" |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 30 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 31 | namespace aaudio { |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 32 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Internal stream that uses the legacy AudioTrack path. |
| 35 | */ |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 36 | class AudioStreamTrack : public AudioStreamLegacy { |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 37 | public: |
| 38 | AudioStreamTrack(); |
| 39 | |
| 40 | virtual ~AudioStreamTrack(); |
| 41 | |
| 42 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 43 | aaudio_result_t open(const AudioStreamBuilder & builder) override; |
Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 44 | aaudio_result_t release_l() override; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 45 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 46 | aaudio_result_t requestStart() override; |
| 47 | aaudio_result_t requestPause() override; |
| 48 | aaudio_result_t requestFlush() override; |
| 49 | aaudio_result_t requestStop() override; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 50 | |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 51 | bool isFlushSupported() const override { |
| 52 | // Only implement FLUSH for OUTPUT streams. |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | bool isPauseSupported() const override { |
| 57 | // Only implement PAUSE for OUTPUT streams. |
| 58 | return true; |
| 59 | } |
| 60 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 61 | aaudio_result_t getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 62 | int64_t *framePosition, |
Phil Burk | 35e80f3 | 2017-03-28 10:25:21 -0700 | [diff] [blame] | 63 | int64_t *timeNanoseconds) override; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 64 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 65 | aaudio_result_t write(const void *buffer, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 66 | int32_t numFrames, |
| 67 | int64_t timeoutNanoseconds) override; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 68 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 69 | aaudio_result_t setBufferSize(int32_t requestedFrames) override; |
| 70 | int32_t getBufferSize() const override; |
| 71 | int32_t getBufferCapacity() const override; |
| 72 | int32_t getFramesPerBurst()const override; |
| 73 | int32_t getXRunCount() const override; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 74 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 75 | int64_t getFramesRead() override; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 76 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 77 | aaudio_direction_t getDirection() const override { |
| 78 | return AAUDIO_DIRECTION_OUTPUT; |
| 79 | } |
| 80 | |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 81 | aaudio_result_t updateStateMachine() override; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 82 | |
| 83 | // This is public so it can be called from the C callback function. |
| 84 | void processCallback(int event, void *info) override; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 85 | |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 86 | int64_t incrementClientFrameCounter(int32_t frames) override { |
| 87 | return incrementFramesWritten(frames); |
| 88 | } |
| 89 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 90 | android::status_t doSetVolume() override; |
| 91 | |
| 92 | #if AAUDIO_USE_VOLUME_SHAPER |
| 93 | virtual android::binder::Status applyVolumeShaper( |
| 94 | const android::media::VolumeShaper::Configuration& configuration, |
| 95 | const android::media::VolumeShaper::Operation& operation) override; |
| 96 | #endif |
| 97 | |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 98 | private: |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 99 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 100 | android::sp<android::AudioTrack> mAudioTrack; |
| 101 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 102 | // adapts between variable sized blocks and fixed size blocks |
| 103 | FixedBlockReader mFixedBlockReader; |
| 104 | |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 105 | // TODO add 64-bit position reporting to AudioTrack and use it. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 106 | aaudio_wrapping_frames_t mPositionWhenPausing = 0; |
Eric Laurent | 68eff05 | 2020-04-10 18:28:41 -0700 | [diff] [blame] | 107 | |
| 108 | // initial AudioTrack frame count and notification period |
| 109 | int32_t mInitialBufferCapacity = 0; |
| 110 | int32_t mInitialFramesPerBurst = 0; |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 113 | } /* namespace aaudio */ |
Phil Burk | e1ce491 | 2016-11-21 10:40:25 -0800 | [diff] [blame] | 114 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 115 | #endif /* LEGACY_AUDIO_STREAM_TRACK_H */ |