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_TRACK_H 18 #define LEGACY_AUDIO_STREAM_TRACK_H 19 20 #include <math.h> 21 #include <media/TrackPlayerBase.h> 22 #include <media/AudioTrack.h> 23 #include <aaudio/AAudio.h> 24 25 #include "AudioStreamBuilder.h" 26 #include "AudioStream.h" 27 #include "legacy/AAudioLegacy.h" 28 #include "legacy/AudioStreamLegacy.h" 29 #include "utility/FixedBlockReader.h" 30 31 namespace aaudio { 32 33 /** 34 * Internal stream that uses the legacy AudioTrack path. 35 */ 36 class AudioStreamTrack : public AudioStreamLegacy { 37 public: 38 AudioStreamTrack(); 39 40 virtual ~AudioStreamTrack(); 41 42 43 aaudio_result_t open(const AudioStreamBuilder & builder) override; 44 aaudio_result_t release_l() override; 45 void close_l() override; 46 47 protected: 48 aaudio_result_t requestStart_l() REQUIRES(mStreamLock) override; 49 aaudio_result_t requestPause_l() REQUIRES(mStreamLock) override; 50 aaudio_result_t requestFlush_l() REQUIRES(mStreamLock) override; 51 aaudio_result_t requestStop_l() REQUIRES(mStreamLock) override; 52 aaudio_result_t systemStopInternal_l() REQUIRES(mStreamLock) final; 53 54 bool collidesWithCallback() const final; 55 56 void onStreamEnd() final; 57 58 public: isFlushSupported()59 bool isFlushSupported() const override { 60 // Only implement FLUSH for OUTPUT streams. 61 return true; 62 } 63 isPauseSupported()64 bool isPauseSupported() const override { 65 // Only implement PAUSE for OUTPUT streams. 66 return true; 67 } 68 69 aaudio_result_t getTimestamp(clockid_t clockId, 70 int64_t *framePosition, 71 int64_t *timeNanoseconds) override; 72 73 aaudio_result_t write(const void *buffer, 74 int32_t numFrames, 75 int64_t timeoutNanoseconds) override; 76 77 aaudio_result_t setBufferSize(int32_t requestedFrames) override; 78 int32_t getBufferSize() const override; 79 int32_t getXRunCount() const override; 80 81 int64_t getFramesRead() override; 82 getDirection()83 aaudio_direction_t getDirection() const override { 84 return AAUDIO_DIRECTION_OUTPUT; 85 } 86 87 aaudio_result_t processCommands() override; 88 incrementClientFrameCounter(int32_t frames)89 int64_t incrementClientFrameCounter(int32_t frames) override { 90 return incrementFramesWritten(frames); 91 } 92 93 android::status_t doSetVolume() override; 94 95 void registerPlayerBase() override; 96 97 // Offload begin -------------------------------------- 98 aaudio_result_t setOffloadDelayPadding(int32_t delayInFrames, int32_t paddingInFrames) final; 99 100 int32_t getOffloadDelay() final; 101 102 int32_t getOffloadPadding() final; 103 104 aaudio_result_t setOffloadEndOfStream() EXCLUDES(mStreamLock) final; 105 setPresentationEndCallbackProc(AAudioStream_presentationEndCallback proc)106 void setPresentationEndCallbackProc(AAudioStream_presentationEndCallback proc) final { 107 mPresentationEndCallbackProc = proc; 108 } 109 setPresentationEndCallbackUserData(void * userData)110 virtual void setPresentationEndCallbackUserData(void *userData) final { 111 mPresentationEndCallbackUserData = userData; 112 } 113 114 void maybeCallPresentationEndCallback(); 115 // Offload end ---------------------------------------- 116 117 #if AAUDIO_USE_VOLUME_SHAPER 118 virtual android::binder::Status applyVolumeShaper( 119 const android::media::VolumeShaper::Configuration& configuration, 120 const android::media::VolumeShaper::Operation& operation) override; 121 #endif 122 123 protected: 124 125 int32_t getFramesPerBurstFromDevice() const override; 126 int32_t getBufferCapacityFromDevice() const override; 127 void onNewIAudioTrack() override; 128 129 private: 130 131 android::sp<android::AudioTrack> mAudioTrack; 132 133 // adapts between variable sized blocks and fixed size blocks 134 FixedBlockReader mFixedBlockReader; 135 136 // TODO add 64-bit position reporting to AudioTrack and use it. 137 aaudio_wrapping_frames_t mPositionWhenPausing = 0; 138 139 // Offload -------------------------------------------- 140 std::atomic<int32_t> mOffloadDelayFrames = 0; 141 std::atomic<int32_t> mOffloadPaddingFrames = 0; 142 bool mOffloadEosPending GUARDED_BY(mStreamLock) = false; 143 144 AAudioStream_presentationEndCallback mPresentationEndCallbackProc = nullptr; 145 void *mPresentationEndCallbackUserData = nullptr; 146 std::atomic<pid_t> mPresentationEndCallbackThread{CALLBACK_THREAD_NONE}; 147 }; 148 149 } /* namespace aaudio */ 150 151 #endif /* LEGACY_AUDIO_STREAM_TRACK_H */ 152