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 aaudio_result_t requestStart() override; 48 aaudio_result_t requestPause() override; 49 aaudio_result_t requestFlush() override; 50 aaudio_result_t requestStop() override; 51 isFlushSupported()52 bool isFlushSupported() const override { 53 // Only implement FLUSH for OUTPUT streams. 54 return true; 55 } 56 isPauseSupported()57 bool isPauseSupported() const override { 58 // Only implement PAUSE for OUTPUT streams. 59 return true; 60 } 61 62 aaudio_result_t getTimestamp(clockid_t clockId, 63 int64_t *framePosition, 64 int64_t *timeNanoseconds) override; 65 66 aaudio_result_t write(const void *buffer, 67 int32_t numFrames, 68 int64_t timeoutNanoseconds) override; 69 70 aaudio_result_t setBufferSize(int32_t requestedFrames) override; 71 int32_t getBufferSize() const override; 72 int32_t getBufferCapacity() const override; 73 int32_t getFramesPerBurst()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 private: 100 101 android::sp<android::AudioTrack> mAudioTrack; 102 103 // adapts between variable sized blocks and fixed size blocks 104 FixedBlockReader mFixedBlockReader; 105 106 // TODO add 64-bit position reporting to AudioTrack and use it. 107 aaudio_wrapping_frames_t mPositionWhenPausing = 0; 108 109 // initial AudioTrack frame count and notification period 110 int32_t mInitialBufferCapacity = 0; 111 int32_t mInitialFramesPerBurst = 0; 112 }; 113 114 } /* namespace aaudio */ 115 116 #endif /* LEGACY_AUDIO_STREAM_TRACK_H */ 117