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 53 public: isFlushSupported()54 bool isFlushSupported() const override { 55 // Only implement FLUSH for OUTPUT streams. 56 return true; 57 } 58 isPauseSupported()59 bool isPauseSupported() const override { 60 // Only implement PAUSE for OUTPUT streams. 61 return true; 62 } 63 64 aaudio_result_t getTimestamp(clockid_t clockId, 65 int64_t *framePosition, 66 int64_t *timeNanoseconds) override; 67 68 aaudio_result_t write(const void *buffer, 69 int32_t numFrames, 70 int64_t timeoutNanoseconds) override; 71 72 aaudio_result_t setBufferSize(int32_t requestedFrames) override; 73 int32_t getBufferSize() const override; 74 int32_t getXRunCount() const override; 75 76 int64_t getFramesRead() override; 77 getDirection()78 aaudio_direction_t getDirection() const override { 79 return AAUDIO_DIRECTION_OUTPUT; 80 } 81 82 aaudio_result_t updateStateMachine() override; 83 84 // This is public so it can be called from the C callback function. 85 void processCallback(int event, void *info) override; 86 incrementClientFrameCounter(int32_t frames)87 int64_t incrementClientFrameCounter(int32_t frames) override { 88 return incrementFramesWritten(frames); 89 } 90 91 android::status_t doSetVolume() override; 92 93 #if AAUDIO_USE_VOLUME_SHAPER 94 virtual android::binder::Status applyVolumeShaper( 95 const android::media::VolumeShaper::Configuration& configuration, 96 const android::media::VolumeShaper::Operation& operation) override; 97 #endif 98 99 protected: 100 101 int32_t getFramesPerBurstFromDevice() const override; 102 int32_t getBufferCapacityFromDevice() const override; 103 104 private: 105 106 android::sp<android::AudioTrack> mAudioTrack; 107 108 // adapts between variable sized blocks and fixed size blocks 109 FixedBlockReader mFixedBlockReader; 110 111 // TODO add 64-bit position reporting to AudioTrack and use it. 112 aaudio_wrapping_frames_t mPositionWhenPausing = 0; 113 }; 114 115 } /* namespace aaudio */ 116 117 #endif /* LEGACY_AUDIO_STREAM_TRACK_H */ 118